Skip to contentSkip to conversation

Patch: linux-2.5.45/drivers/base/bus.c - new field to consolidate memory allocation in many drivers

DISCUSSION Adam J. Richter 2 participants 2 messages Nov 3, 2002 – Nov 4, 2002

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Here is a new version of my patch that also allows drivers to
ask the generic driver code to allocate a fixed-size per-device blob of consistent DMA memory. It includes an example patch to via-rhine.c, removing another 14 lines, including another memory allocation failure error branch. I am running the resulting via-rhine driver now.

If I can eliminate an average of 5 line from each of the 557
modules in my modules.pcimap (never mind other busses for the moment), that would be over 2000 lines of code. More importantly, I'm sure it would eliminate a lot of bugs in untested error branches.

Dave M.: I am cc'ing this to because you asked for a
bus-independent API for allocating DMA consistent memory when I discusssed consolidating some other driver DMA mapping operations about six months ago. This patch adds the wrapper functions dma_{alloc,free}_consistent() for this purpose which are not optimized right now, but provide an API and could be expanded to support more the the DMA allocation API as it is actually used. The implementation can be optimized later, but I think it addresses your concern about propagating artificial PCI dependence.

Signature · 4 lines
Adam J. Richter     __     ______________   575 Oroville Road
[email protected]     \ /                  Milpitas, California 95035
+1 408 309-6081         | g g d r a s i l   United States of America
                         "Free Software For The Rest Of Us."
include/linux/dma-map.h +53 -0
@@ -0,0 +1,53 @@
+/*
+ * Generic DMA region interface.
+ */
+
+#ifndef LINUX_DMA_MAP_H
+#define LINUX_DMA_MAP_H
+
+#include <asm/types.h>
+#include <linux/device.h>
+
+struct device;
+
+struct dma_ops {
+	void *(*alloc_consistent)(struct device *dev, size_t size,
+				  dma_addr_t *dma_handle);
+
+	void (*free_consistent)(struct device *dev, size_t size,
+				void *vaddr, dma_addr_t dma_handle);
+
+	/* Other operations (map_page, map_single, etc.) will be added
+	   when they are actually used. */
+};
+
+/* So we only have one place to change if dma_ops is moved: */
+static inline struct dma_ops *DEV_DMA_OPS(struct device *dev)
+{
+	return dev->bus->dma_ops;
+}
+
+static inline void *
+dma_alloc_consistent(struct device *dev, size_t size, dma_addr_t *dma_handle)
+{
+	void *ptr;
+	ptr = (*DEV_DMA_OPS(dev)->alloc_consistent)(dev, size, dma_handle);
+
+#if 0
+	/* Currently every alloc_consistent implementation replicates this.
+	   In the future, only do the memset here. */
+	if (ptr)
+		memset(ptr, 0, size);
+#endif
+
+	return ptr;
+}
+
+static inline void dma_free_consistent(struct device *dev, size_t size,
+				       void *vaddr, dma_addr_t dma_handle)
+{
+	(*DEV_DMA_OPS(dev)->free_consistent)(dev, size, vaddr, dma_handle);
+}
+
+
+#endif /* LINUX_DMA_MAP_H */
drivers/pci/dma-ops.c +22 -0
@@ -0,0 +1,22 @@
+#include <linux/dma-map.h>
+#include <linux/pci.h>
+
+static void *_pci_alloc_consistent(struct device *dev, size_t size,
+				   dma_addr_t *dma_handle)
+{
+	struct pci_dev *pcidev = container_of(dev, struct pci_dev, dev);
+	return pci_alloc_consistent(pcidev, size, dma_handle);
+}
+
+static void _pci_free_consistent(struct device *dev, size_t size,
+				 void *vaddr, dma_addr_t dma_handle)
+{
+	struct pci_dev *pcidev = container_of(dev, struct pci_dev, dev);
+	pci_free_consistent(pcidev, size, vaddr, dma_handle);
+}
+
+
+struct dma_ops pci_dma_ops = {
+	.alloc_consistent =	_pci_alloc_consistent,
+	.free_consistent =	_pci_free_consistent,
+};
include/linux/device.h +6 -0
@@ -58,6 +58,7 @@
 struct device;
 struct device_driver;
 struct device_class;
+struct dma_ops;
 
 struct bus_type {
 	char			* name;
@@ -68,6 +69,7 @@
 	struct list_head	node;
 	struct list_head	devices;
 	struct list_head	drivers;
+	struct dma_ops		*dma_ops; /* For CPU busses: pci, isa, sbus */
 
 	struct driver_dir_entry	dir;
 	struct driver_dir_entry	device_dir;
@@ -114,6 +116,8 @@
 	char			* name;
 	struct bus_type		* bus;
 	struct device_class	* devclass;
+	int			drv_alloc;
+	int			dma_alloc;
 
 	rwlock_t		lock;
 	atomic_t		refcount;
@@ -290,6 +294,8 @@
 	struct device_driver *driver;	/* which driver has allocated this
 					   device */
 	void		*driver_data;	/* data private to the driver */
+	void		*dma_vaddr;
+	dma_addr_t	dma_busaddr;
 
 	u32		class_num;	/* class-enumerated value */
 	void		* class_data;	/* class-specific data */
drivers/base/bus.c +63 -5

113 lines not on this page · raw message

drivers/net/via-rhine.c +20 -37

142 lines not on this page · raw message

#1 lore raw

From: "Adam J. Richter" <[email protected]>
Date: Sun, 3 Nov 2002 14:40:22 -0800

Dave M.: I am cc'ing this to because you asked for a
bus-independent API for allocating DMA consistent memory when I discusssed consolidating some other driver DMA mapping operations about six months ago.

I don't know how much I like the DMA memory being allocated transparently based upon some structure initialization values.

I'd rather the DMA alloc/free be explicit in the drivers.

Otherwise, the ->ops->alloc_consistent et al. abstraction looks ok.