1 /*****************************************************************************
2 * Copyright 2004 - 2008 Broadcom Corporation. All rights reserved.
4 * Unless you and Broadcom execute a separate written software license
5 * agreement governing use of this software, this software is licensed to you
6 * under the terms of the GNU General Public License version 2, available at
7 * http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
9 * Notwithstanding the above, under no circumstances may you combine this
10 * software in any way with any other Broadcom software provided under a
11 * license other than the GPL, without Broadcom's express prior written
13 *****************************************************************************/
15 /****************************************************************************/
19 * @brief Implements the DMA interface.
21 /****************************************************************************/
23 /* ---- Include Files ---------------------------------------------------- */
25 #include <linux/module.h>
26 #include <linux/device.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/interrupt.h>
29 #include <linux/irqreturn.h>
30 #include <linux/proc_fs.h>
32 #include <mach/timer.h>
35 #include <linux/pfn.h>
36 #include <asm/atomic.h>
39 /* I don't quite understand why dc4 fails when this is set to 1 and DMA is enabled */
40 /* especially since dc4 doesn't use kmalloc'd memory. */
42 #define ALLOW_MAP_OF_KMALLOC_MEMORY 0
44 /* ---- Public Variables ------------------------------------------------- */
46 /* ---- Private Constants and Types -------------------------------------- */
48 #define MAKE_HANDLE(controllerIdx, channelIdx) (((controllerIdx) << 4) | (channelIdx))
50 #define CONTROLLER_FROM_HANDLE(handle) (((handle) >> 4) & 0x0f)
51 #define CHANNEL_FROM_HANDLE(handle) ((handle) & 0x0f)
53 #define DMA_MAP_DEBUG 0
56 # define DMA_MAP_PRINT(fmt, args...) printk("%s: " fmt, __func__, ## args)
58 # define DMA_MAP_PRINT(fmt, args...)
61 /* ---- Private Variables ------------------------------------------------ */
63 static DMA_Global_t gDMA
;
64 static struct proc_dir_entry
*gDmaDir
;
66 static atomic_t gDmaStatMemTypeKmalloc
= ATOMIC_INIT(0);
67 static atomic_t gDmaStatMemTypeVmalloc
= ATOMIC_INIT(0);
68 static atomic_t gDmaStatMemTypeUser
= ATOMIC_INIT(0);
69 static atomic_t gDmaStatMemTypeCoherent
= ATOMIC_INIT(0);
71 #include "dma_device.c"
73 /* ---- Private Function Prototypes -------------------------------------- */
75 /* ---- Functions ------------------------------------------------------- */
77 /****************************************************************************/
79 * Displays information for /proc/dma/mem-type
81 /****************************************************************************/
83 static int dma_proc_read_mem_type(char *buf
, char **start
, off_t offset
,
84 int count
, int *eof
, void *data
)
88 len
+= sprintf(buf
+ len
, "dma_map_mem statistics\n");
90 sprintf(buf
+ len
, "coherent: %d\n",
91 atomic_read(&gDmaStatMemTypeCoherent
));
93 sprintf(buf
+ len
, "kmalloc: %d\n",
94 atomic_read(&gDmaStatMemTypeKmalloc
));
96 sprintf(buf
+ len
, "vmalloc: %d\n",
97 atomic_read(&gDmaStatMemTypeVmalloc
));
99 sprintf(buf
+ len
, "user: %d\n",
100 atomic_read(&gDmaStatMemTypeUser
));
105 /****************************************************************************/
107 * Displays information for /proc/dma/channels
109 /****************************************************************************/
111 static int dma_proc_read_channels(char *buf
, char **start
, off_t offset
,
112 int count
, int *eof
, void *data
)
116 int limit
= count
- 200;
118 DMA_Channel_t
*channel
;
120 if (down_interruptible(&gDMA
.lock
) < 0) {
124 for (controllerIdx
= 0; controllerIdx
< DMA_NUM_CONTROLLERS
;
126 for (channelIdx
= 0; channelIdx
< DMA_NUM_CHANNELS
;
133 &gDMA
.controller
[controllerIdx
].channel
[channelIdx
];
136 sprintf(buf
+ len
, "%d:%d ", controllerIdx
,
139 if ((channel
->flags
& DMA_CHANNEL_FLAG_IS_DEDICATED
) !=
142 sprintf(buf
+ len
, "Dedicated for %s ",
143 DMA_gDeviceAttribute
[channel
->
146 len
+= sprintf(buf
+ len
, "Shared ");
149 if ((channel
->flags
& DMA_CHANNEL_FLAG_NO_ISR
) != 0) {
150 len
+= sprintf(buf
+ len
, "No ISR ");
153 if ((channel
->flags
& DMA_CHANNEL_FLAG_LARGE_FIFO
) != 0) {
154 len
+= sprintf(buf
+ len
, "Fifo: 128 ");
156 len
+= sprintf(buf
+ len
, "Fifo: 64 ");
159 if ((channel
->flags
& DMA_CHANNEL_FLAG_IN_USE
) != 0) {
161 sprintf(buf
+ len
, "InUse by %s",
162 DMA_gDeviceAttribute
[channel
->
164 #if (DMA_DEBUG_TRACK_RESERVATION)
166 sprintf(buf
+ len
, " (%s:%d)",
171 len
+= sprintf(buf
+ len
, "Avail ");
174 if (channel
->lastDevType
!= DMA_DEVICE_NONE
) {
176 sprintf(buf
+ len
, "Last use: %s ",
177 DMA_gDeviceAttribute
[channel
->
182 len
+= sprintf(buf
+ len
, "\n");
191 /****************************************************************************/
193 * Displays information for /proc/dma/devices
195 /****************************************************************************/
197 static int dma_proc_read_devices(char *buf
, char **start
, off_t offset
,
198 int count
, int *eof
, void *data
)
200 int limit
= count
- 200;
204 if (down_interruptible(&gDMA
.lock
) < 0) {
208 for (devIdx
= 0; devIdx
< DMA_NUM_DEVICE_ENTRIES
; devIdx
++) {
209 DMA_DeviceAttribute_t
*devAttr
= &DMA_gDeviceAttribute
[devIdx
];
211 if (devAttr
->name
== NULL
) {
219 len
+= sprintf(buf
+ len
, "%-12s ", devAttr
->name
);
221 if ((devAttr
->flags
& DMA_DEVICE_FLAG_IS_DEDICATED
) != 0) {
223 sprintf(buf
+ len
, "Dedicated %d:%d ",
224 devAttr
->dedicatedController
,
225 devAttr
->dedicatedChannel
);
227 len
+= sprintf(buf
+ len
, "Shared DMA:");
228 if ((devAttr
->flags
& DMA_DEVICE_FLAG_ON_DMA0
) != 0) {
229 len
+= sprintf(buf
+ len
, "0");
231 if ((devAttr
->flags
& DMA_DEVICE_FLAG_ON_DMA1
) != 0) {
232 len
+= sprintf(buf
+ len
, "1");
234 len
+= sprintf(buf
+ len
, " ");
236 if ((devAttr
->flags
& DMA_DEVICE_FLAG_NO_ISR
) != 0) {
237 len
+= sprintf(buf
+ len
, "NoISR ");
239 if ((devAttr
->flags
& DMA_DEVICE_FLAG_ALLOW_LARGE_FIFO
) != 0) {
240 len
+= sprintf(buf
+ len
, "Allow-128 ");
245 "Xfer #: %Lu Ticks: %Lu Bytes: %Lu DescLen: %u\n",
246 devAttr
->numTransfers
, devAttr
->transferTicks
,
247 devAttr
->transferBytes
,
248 devAttr
->ring
.bytesAllocated
);
258 /****************************************************************************/
260 * Determines if a DMA_Device_t is "valid".
263 * TRUE - dma device is valid
264 * FALSE - dma device isn't valid
266 /****************************************************************************/
268 static inline int IsDeviceValid(DMA_Device_t device
)
270 return (device
>= 0) && (device
< DMA_NUM_DEVICE_ENTRIES
);
273 /****************************************************************************/
275 * Translates a DMA handle into a pointer to a channel.
278 * non-NULL - pointer to DMA_Channel_t
279 * NULL - DMA Handle was invalid
281 /****************************************************************************/
283 static inline DMA_Channel_t
*HandleToChannel(DMA_Handle_t handle
)
288 controllerIdx
= CONTROLLER_FROM_HANDLE(handle
);
289 channelIdx
= CHANNEL_FROM_HANDLE(handle
);
291 if ((controllerIdx
> DMA_NUM_CONTROLLERS
)
292 || (channelIdx
> DMA_NUM_CHANNELS
)) {
295 return &gDMA
.controller
[controllerIdx
].channel
[channelIdx
];
298 /****************************************************************************/
300 * Interrupt handler which is called to process DMA interrupts.
302 /****************************************************************************/
304 static irqreturn_t
dma_interrupt_handler(int irq
, void *dev_id
)
306 DMA_Channel_t
*channel
;
307 DMA_DeviceAttribute_t
*devAttr
;
310 channel
= (DMA_Channel_t
*) dev_id
;
312 /* Figure out why we were called, and knock down the interrupt */
314 irqStatus
= dmacHw_getInterruptStatus(channel
->dmacHwHandle
);
315 dmacHw_clearInterrupt(channel
->dmacHwHandle
);
317 if ((channel
->devType
< 0)
318 || (channel
->devType
> DMA_NUM_DEVICE_ENTRIES
)) {
319 printk(KERN_ERR
"dma_interrupt_handler: Invalid devType: %d\n",
323 devAttr
= &DMA_gDeviceAttribute
[channel
->devType
];
327 if ((irqStatus
& dmacHw_INTERRUPT_STATUS_TRANS
) != 0) {
328 devAttr
->transferTicks
+=
329 (timer_get_tick_count() - devAttr
->transferStartTime
);
332 if ((irqStatus
& dmacHw_INTERRUPT_STATUS_ERROR
) != 0) {
334 "dma_interrupt_handler: devType :%d DMA error (%s)\n",
335 channel
->devType
, devAttr
->name
);
337 devAttr
->numTransfers
++;
338 devAttr
->transferBytes
+= devAttr
->numBytes
;
341 /* Call any installed handler */
343 if (devAttr
->devHandler
!= NULL
) {
344 devAttr
->devHandler(channel
->devType
, irqStatus
,
351 /****************************************************************************/
353 * Allocates memory to hold a descriptor ring. The descriptor ring then
354 * needs to be populated by making one or more calls to
355 * dna_add_descriptors.
357 * The returned descriptor ring will be automatically initialized.
360 * 0 Descriptor ring was allocated successfully
361 * -EINVAL Invalid parameters passed in
362 * -ENOMEM Unable to allocate memory for the desired number of descriptors.
364 /****************************************************************************/
366 int dma_alloc_descriptor_ring(DMA_DescriptorRing_t
*ring
, /* Descriptor ring to populate */
367 int numDescriptors
/* Number of descriptors that need to be allocated. */
369 size_t bytesToAlloc
= dmacHw_descriptorLen(numDescriptors
);
371 if ((ring
== NULL
) || (numDescriptors
<= 0)) {
376 ring
->descriptorsAllocated
= 0;
377 ring
->bytesAllocated
= 0;
379 ring
->virtAddr
= dma_alloc_writecombine(NULL
,
383 if (ring
->virtAddr
== NULL
) {
387 ring
->bytesAllocated
= bytesToAlloc
;
388 ring
->descriptorsAllocated
= numDescriptors
;
390 return dma_init_descriptor_ring(ring
, numDescriptors
);
393 EXPORT_SYMBOL(dma_alloc_descriptor_ring
);
395 /****************************************************************************/
397 * Releases the memory which was previously allocated for a descriptor ring.
399 /****************************************************************************/
401 void dma_free_descriptor_ring(DMA_DescriptorRing_t
*ring
/* Descriptor to release */
403 if (ring
->virtAddr
!= NULL
) {
404 dma_free_writecombine(NULL
,
405 ring
->bytesAllocated
,
406 ring
->virtAddr
, ring
->physAddr
);
409 ring
->bytesAllocated
= 0;
410 ring
->descriptorsAllocated
= 0;
411 ring
->virtAddr
= NULL
;
415 EXPORT_SYMBOL(dma_free_descriptor_ring
);
417 /****************************************************************************/
419 * Initializes a descriptor ring, so that descriptors can be added to it.
420 * Once a descriptor ring has been allocated, it may be reinitialized for
421 * use with additional/different regions of memory.
423 * Note that if 7 descriptors are allocated, it's perfectly acceptable to
424 * initialize the ring with a smaller number of descriptors. The amount
425 * of memory allocated for the descriptor ring will not be reduced, and
426 * the descriptor ring may be reinitialized later
429 * 0 Descriptor ring was initialized successfully
430 * -ENOMEM The descriptor which was passed in has insufficient space
431 * to hold the desired number of descriptors.
433 /****************************************************************************/
435 int dma_init_descriptor_ring(DMA_DescriptorRing_t
*ring
, /* Descriptor ring to initialize */
436 int numDescriptors
/* Number of descriptors to initialize. */
438 if (ring
->virtAddr
== NULL
) {
441 if (dmacHw_initDescriptor(ring
->virtAddr
,
443 ring
->bytesAllocated
, numDescriptors
) < 0) {
445 "dma_init_descriptor_ring: dmacHw_initDescriptor failed\n");
452 EXPORT_SYMBOL(dma_init_descriptor_ring
);
454 /****************************************************************************/
456 * Determines the number of descriptors which would be required for a
457 * transfer of the indicated memory region.
459 * This function also needs to know which DMA device this transfer will
460 * be destined for, so that the appropriate DMA configuration can be retrieved.
461 * DMA parameters such as transfer width, and whether this is a memory-to-memory
462 * or memory-to-peripheral, etc can all affect the actual number of descriptors
466 * > 0 Returns the number of descriptors required for the indicated transfer
467 * -ENODEV - Device handed in is invalid.
468 * -EINVAL Invalid parameters
469 * -ENOMEM Memory exhausted
471 /****************************************************************************/
473 int dma_calculate_descriptor_count(DMA_Device_t device
, /* DMA Device that this will be associated with */
474 dma_addr_t srcData
, /* Place to get data to write to device */
475 dma_addr_t dstData
, /* Pointer to device data address */
476 size_t numBytes
/* Number of bytes to transfer to the device */
479 DMA_DeviceAttribute_t
*devAttr
;
481 if (!IsDeviceValid(device
)) {
484 devAttr
= &DMA_gDeviceAttribute
[device
];
486 numDescriptors
= dmacHw_calculateDescriptorCount(&devAttr
->config
,
490 if (numDescriptors
< 0) {
492 "dma_calculate_descriptor_count: dmacHw_calculateDescriptorCount failed\n");
496 return numDescriptors
;
499 EXPORT_SYMBOL(dma_calculate_descriptor_count
);
501 /****************************************************************************/
503 * Adds a region of memory to the descriptor ring. Note that it may take
504 * multiple descriptors for each region of memory. It is the callers
505 * responsibility to allocate a sufficiently large descriptor ring.
508 * 0 Descriptors were added successfully
509 * -ENODEV Device handed in is invalid.
510 * -EINVAL Invalid parameters
511 * -ENOMEM Memory exhausted
513 /****************************************************************************/
515 int dma_add_descriptors(DMA_DescriptorRing_t
*ring
, /* Descriptor ring to add descriptors to */
516 DMA_Device_t device
, /* DMA Device that descriptors are for */
517 dma_addr_t srcData
, /* Place to get data (memory or device) */
518 dma_addr_t dstData
, /* Place to put data (memory or device) */
519 size_t numBytes
/* Number of bytes to transfer to the device */
522 DMA_DeviceAttribute_t
*devAttr
;
524 if (!IsDeviceValid(device
)) {
527 devAttr
= &DMA_gDeviceAttribute
[device
];
529 rc
= dmacHw_setDataDescriptor(&devAttr
->config
,
532 (void *)dstData
, numBytes
);
535 "dma_add_descriptors: dmacHw_setDataDescriptor failed with code: %d\n",
543 EXPORT_SYMBOL(dma_add_descriptors
);
545 /****************************************************************************/
547 * Sets the descriptor ring associated with a device.
549 * Once set, the descriptor ring will be associated with the device, even
550 * across channel request/free calls. Passing in a NULL descriptor ring
551 * will release any descriptor ring currently associated with the device.
553 * Note: If you call dma_transfer, or one of the other dma_alloc_ functions
554 * the descriptor ring may be released and reallocated.
556 * Note: This function will release the descriptor memory for any current
557 * descriptor ring associated with this device.
560 * 0 Descriptors were added successfully
561 * -ENODEV Device handed in is invalid.
563 /****************************************************************************/
565 int dma_set_device_descriptor_ring(DMA_Device_t device
, /* Device to update the descriptor ring for. */
566 DMA_DescriptorRing_t
*ring
/* Descriptor ring to add descriptors to */
568 DMA_DeviceAttribute_t
*devAttr
;
570 if (!IsDeviceValid(device
)) {
573 devAttr
= &DMA_gDeviceAttribute
[device
];
575 /* Free the previously allocated descriptor ring */
577 dma_free_descriptor_ring(&devAttr
->ring
);
580 /* Copy in the new one */
582 devAttr
->ring
= *ring
;
585 /* Set things up so that if dma_transfer is called then this descriptor */
586 /* ring will get freed. */
588 devAttr
->prevSrcData
= 0;
589 devAttr
->prevDstData
= 0;
590 devAttr
->prevNumBytes
= 0;
595 EXPORT_SYMBOL(dma_set_device_descriptor_ring
);
597 /****************************************************************************/
599 * Retrieves the descriptor ring associated with a device.
602 * 0 Descriptors were added successfully
603 * -ENODEV Device handed in is invalid.
605 /****************************************************************************/
607 int dma_get_device_descriptor_ring(DMA_Device_t device
, /* Device to retrieve the descriptor ring for. */
608 DMA_DescriptorRing_t
*ring
/* Place to store retrieved ring */
610 DMA_DeviceAttribute_t
*devAttr
;
612 memset(ring
, 0, sizeof(*ring
));
614 if (!IsDeviceValid(device
)) {
617 devAttr
= &DMA_gDeviceAttribute
[device
];
619 *ring
= devAttr
->ring
;
624 EXPORT_SYMBOL(dma_get_device_descriptor_ring
);
626 /****************************************************************************/
628 * Configures a DMA channel.
631 * >= 0 - Initialization was successfull.
633 * -EBUSY - Device is currently being used.
634 * -ENODEV - Device handed in is invalid.
636 /****************************************************************************/
638 static int ConfigChannel(DMA_Handle_t handle
)
640 DMA_Channel_t
*channel
;
641 DMA_DeviceAttribute_t
*devAttr
;
644 channel
= HandleToChannel(handle
);
645 if (channel
== NULL
) {
648 devAttr
= &DMA_gDeviceAttribute
[channel
->devType
];
649 controllerIdx
= CONTROLLER_FROM_HANDLE(handle
);
651 if ((devAttr
->flags
& DMA_DEVICE_FLAG_PORT_PER_DMAC
) != 0) {
652 if (devAttr
->config
.transferType
==
653 dmacHw_TRANSFER_TYPE_MEM_TO_PERIPHERAL
) {
654 devAttr
->config
.dstPeripheralPort
=
655 devAttr
->dmacPort
[controllerIdx
];
656 } else if (devAttr
->config
.transferType
==
657 dmacHw_TRANSFER_TYPE_PERIPHERAL_TO_MEM
) {
658 devAttr
->config
.srcPeripheralPort
=
659 devAttr
->dmacPort
[controllerIdx
];
663 if (dmacHw_configChannel(channel
->dmacHwHandle
, &devAttr
->config
) != 0) {
664 printk(KERN_ERR
"ConfigChannel: dmacHw_configChannel failed\n");
671 /****************************************************************************/
673 * Intializes all of the data structures associated with the DMA.
675 * >= 0 - Initialization was successfull.
677 * -EBUSY - Device is currently being used.
678 * -ENODEV - Device handed in is invalid.
680 /****************************************************************************/
688 DMA_Channel_t
*channel
;
689 DMA_Handle_t dedicatedHandle
;
691 memset(&gDMA
, 0, sizeof(gDMA
));
693 init_MUTEX_LOCKED(&gDMA
.lock
);
694 init_waitqueue_head(&gDMA
.freeChannelQ
);
696 /* Initialize the Hardware */
700 /* Start off by marking all of the DMA channels as shared. */
702 for (controllerIdx
= 0; controllerIdx
< DMA_NUM_CONTROLLERS
;
704 for (channelIdx
= 0; channelIdx
< DMA_NUM_CHANNELS
;
707 &gDMA
.controller
[controllerIdx
].channel
[channelIdx
];
710 channel
->devType
= DMA_DEVICE_NONE
;
711 channel
->lastDevType
= DMA_DEVICE_NONE
;
713 #if (DMA_DEBUG_TRACK_RESERVATION)
714 channel
->fileName
= "";
715 channel
->lineNum
= 0;
718 channel
->dmacHwHandle
=
719 dmacHw_getChannelHandle(dmacHw_MAKE_CHANNEL_ID
722 dmacHw_initChannel(channel
->dmacHwHandle
);
726 /* Record any special attributes that channels may have */
728 gDMA
.controller
[0].channel
[0].flags
|= DMA_CHANNEL_FLAG_LARGE_FIFO
;
729 gDMA
.controller
[0].channel
[1].flags
|= DMA_CHANNEL_FLAG_LARGE_FIFO
;
730 gDMA
.controller
[1].channel
[0].flags
|= DMA_CHANNEL_FLAG_LARGE_FIFO
;
731 gDMA
.controller
[1].channel
[1].flags
|= DMA_CHANNEL_FLAG_LARGE_FIFO
;
733 /* Now walk through and record the dedicated channels. */
735 for (devIdx
= 0; devIdx
< DMA_NUM_DEVICE_ENTRIES
; devIdx
++) {
736 DMA_DeviceAttribute_t
*devAttr
= &DMA_gDeviceAttribute
[devIdx
];
738 if (((devAttr
->flags
& DMA_DEVICE_FLAG_NO_ISR
) != 0)
739 && ((devAttr
->flags
& DMA_DEVICE_FLAG_IS_DEDICATED
) == 0)) {
741 "DMA Device: %s Can only request NO_ISR for dedicated devices\n",
747 if ((devAttr
->flags
& DMA_DEVICE_FLAG_IS_DEDICATED
) != 0) {
748 /* This is a dedicated device. Mark the channel as being reserved. */
750 if (devAttr
->dedicatedController
>= DMA_NUM_CONTROLLERS
) {
752 "DMA Device: %s DMA Controller %d is out of range\n",
754 devAttr
->dedicatedController
);
759 if (devAttr
->dedicatedChannel
>= DMA_NUM_CHANNELS
) {
761 "DMA Device: %s DMA Channel %d is out of range\n",
763 devAttr
->dedicatedChannel
);
769 MAKE_HANDLE(devAttr
->dedicatedController
,
770 devAttr
->dedicatedChannel
);
771 channel
= HandleToChannel(dedicatedHandle
);
773 if ((channel
->flags
& DMA_CHANNEL_FLAG_IS_DEDICATED
) !=
776 ("DMA Device: %s attempting to use same DMA Controller:Channel (%d:%d) as %s\n",
778 devAttr
->dedicatedController
,
779 devAttr
->dedicatedChannel
,
780 DMA_gDeviceAttribute
[channel
->devType
].
786 channel
->flags
|= DMA_CHANNEL_FLAG_IS_DEDICATED
;
787 channel
->devType
= devIdx
;
789 if (devAttr
->flags
& DMA_DEVICE_FLAG_NO_ISR
) {
790 channel
->flags
|= DMA_CHANNEL_FLAG_NO_ISR
;
793 /* For dedicated channels, we can go ahead and configure the DMA channel now */
796 ConfigChannel(dedicatedHandle
);
800 /* Go through and register the interrupt handlers */
802 for (controllerIdx
= 0; controllerIdx
< DMA_NUM_CONTROLLERS
;
804 for (channelIdx
= 0; channelIdx
< DMA_NUM_CHANNELS
;
807 &gDMA
.controller
[controllerIdx
].channel
[channelIdx
];
809 if ((channel
->flags
& DMA_CHANNEL_FLAG_NO_ISR
) == 0) {
810 snprintf(channel
->name
, sizeof(channel
->name
),
811 "dma %d:%d %s", controllerIdx
,
814 DMA_DEVICE_NONE
? "" :
815 DMA_gDeviceAttribute
[channel
->devType
].
819 request_irq(IRQ_DMA0C0
+
823 dma_interrupt_handler
,
824 IRQF_DISABLED
, channel
->name
,
828 "request_irq for IRQ_DMA%dC%d failed\n",
829 controllerIdx
, channelIdx
);
835 /* Create /proc/dma/channels and /proc/dma/devices */
837 gDmaDir
= create_proc_entry("dma", S_IFDIR
| S_IRUGO
| S_IXUGO
, NULL
);
839 if (gDmaDir
== NULL
) {
840 printk(KERN_ERR
"Unable to create /proc/dma\n");
842 create_proc_read_entry("channels", 0, gDmaDir
,
843 dma_proc_read_channels
, NULL
);
844 create_proc_read_entry("devices", 0, gDmaDir
,
845 dma_proc_read_devices
, NULL
);
846 create_proc_read_entry("mem-type", 0, gDmaDir
,
847 dma_proc_read_mem_type
, NULL
);
857 /****************************************************************************/
859 * Reserves a channel for use with @a dev. If the device is setup to use
860 * a shared channel, then this function will block until a free channel
864 * >= 0 - A valid DMA Handle.
865 * -EBUSY - Device is currently being used.
866 * -ENODEV - Device handed in is invalid.
868 /****************************************************************************/
870 #if (DMA_DEBUG_TRACK_RESERVATION)
871 DMA_Handle_t dma_request_channel_dbg
872 (DMA_Device_t dev
, const char *fileName
, int lineNum
)
874 DMA_Handle_t
dma_request_channel(DMA_Device_t dev
)
878 DMA_DeviceAttribute_t
*devAttr
;
879 DMA_Channel_t
*channel
;
884 if (down_interruptible(&gDMA
.lock
) < 0) {
888 if ((dev
< 0) || (dev
>= DMA_NUM_DEVICE_ENTRIES
)) {
892 devAttr
= &DMA_gDeviceAttribute
[dev
];
894 #if (DMA_DEBUG_TRACK_RESERVATION)
898 s
= strrchr(fileName
, '/');
904 if ((devAttr
->flags
& DMA_DEVICE_FLAG_IN_USE
) != 0) {
905 /* This device has already been requested and not been freed */
907 printk(KERN_ERR
"%s: device %s is already requested\n",
908 __func__
, devAttr
->name
);
913 if ((devAttr
->flags
& DMA_DEVICE_FLAG_IS_DEDICATED
) != 0) {
914 /* This device has a dedicated channel. */
917 &gDMA
.controller
[devAttr
->dedicatedController
].
918 channel
[devAttr
->dedicatedChannel
];
919 if ((channel
->flags
& DMA_CHANNEL_FLAG_IN_USE
) != 0) {
924 channel
->flags
|= DMA_CHANNEL_FLAG_IN_USE
;
925 devAttr
->flags
|= DMA_DEVICE_FLAG_IN_USE
;
927 #if (DMA_DEBUG_TRACK_RESERVATION)
928 channel
->fileName
= fileName
;
929 channel
->lineNum
= lineNum
;
932 MAKE_HANDLE(devAttr
->dedicatedController
,
933 devAttr
->dedicatedChannel
);
937 /* This device needs to use one of the shared channels. */
939 handle
= DMA_INVALID_HANDLE
;
940 while (handle
== DMA_INVALID_HANDLE
) {
941 /* Scan through the shared channels and see if one is available */
943 for (controllerIdx2
= 0; controllerIdx2
< DMA_NUM_CONTROLLERS
;
945 /* Check to see if we should try on controller 1 first. */
947 controllerIdx
= controllerIdx2
;
949 flags
& DMA_DEVICE_FLAG_ALLOC_DMA1_FIRST
) != 0) {
950 controllerIdx
= 1 - controllerIdx
;
953 /* See if the device is available on the controller being tested */
956 flags
& (DMA_DEVICE_FLAG_ON_DMA0
<< controllerIdx
))
959 channelIdx
< DMA_NUM_CHANNELS
;
962 &gDMA
.controller
[controllerIdx
].
967 DMA_CHANNEL_FLAG_IS_DEDICATED
) ==
971 flags
& DMA_CHANNEL_FLAG_IN_USE
)
975 DMA_CHANNEL_FLAG_LARGE_FIFO
)
980 DMA_DEVICE_FLAG_ALLOW_LARGE_FIFO
)
982 /* This channel is a large fifo - don't tie it up */
983 /* with devices that we don't want using it. */
989 DMA_CHANNEL_FLAG_IN_USE
;
990 channel
->devType
= dev
;
992 DMA_DEVICE_FLAG_IN_USE
;
994 #if (DMA_DEBUG_TRACK_RESERVATION)
995 channel
->fileName
= fileName
;
996 channel
->lineNum
= lineNum
;
999 MAKE_HANDLE(controllerIdx
,
1002 /* Now that we've reserved the channel - we can go ahead and configure it */
1004 if (ConfigChannel(handle
) != 0) {
1007 "dma_request_channel: ConfigChannel failed\n");
1015 /* No channels are currently available. Let's wait for one to free up. */
1020 prepare_to_wait(&gDMA
.freeChannelQ
, &wait
,
1021 TASK_INTERRUPTIBLE
);
1024 finish_wait(&gDMA
.freeChannelQ
, &wait
);
1026 if (signal_pending(current
)) {
1027 /* We don't currently hold gDMA.lock, so we return directly */
1029 return -ERESTARTSYS
;
1033 if (down_interruptible(&gDMA
.lock
)) {
1034 return -ERESTARTSYS
;
1044 /* Create both _dbg and non _dbg functions for modules. */
1046 #if (DMA_DEBUG_TRACK_RESERVATION)
1047 #undef dma_request_channel
1048 DMA_Handle_t
dma_request_channel(DMA_Device_t dev
)
1050 return dma_request_channel_dbg(dev
, __FILE__
, __LINE__
);
1053 EXPORT_SYMBOL(dma_request_channel_dbg
);
1055 EXPORT_SYMBOL(dma_request_channel
);
1057 /****************************************************************************/
1059 * Frees a previously allocated DMA Handle.
1061 /****************************************************************************/
1063 int dma_free_channel(DMA_Handle_t handle
/* DMA handle. */
1066 DMA_Channel_t
*channel
;
1067 DMA_DeviceAttribute_t
*devAttr
;
1069 if (down_interruptible(&gDMA
.lock
) < 0) {
1070 return -ERESTARTSYS
;
1073 channel
= HandleToChannel(handle
);
1074 if (channel
== NULL
) {
1079 devAttr
= &DMA_gDeviceAttribute
[channel
->devType
];
1081 if ((channel
->flags
& DMA_CHANNEL_FLAG_IS_DEDICATED
) == 0) {
1082 channel
->lastDevType
= channel
->devType
;
1083 channel
->devType
= DMA_DEVICE_NONE
;
1085 channel
->flags
&= ~DMA_CHANNEL_FLAG_IN_USE
;
1086 devAttr
->flags
&= ~DMA_DEVICE_FLAG_IN_USE
;
1091 wake_up_interruptible(&gDMA
.freeChannelQ
);
1096 EXPORT_SYMBOL(dma_free_channel
);
1098 /****************************************************************************/
1100 * Determines if a given device has been configured as using a shared
1104 * 0 Device uses a dedicated channel
1105 * > zero Device uses a shared channel
1108 /****************************************************************************/
1110 int dma_device_is_channel_shared(DMA_Device_t device
/* Device to check. */
1112 DMA_DeviceAttribute_t
*devAttr
;
1114 if (!IsDeviceValid(device
)) {
1117 devAttr
= &DMA_gDeviceAttribute
[device
];
1119 return ((devAttr
->flags
& DMA_DEVICE_FLAG_IS_DEDICATED
) == 0);
1122 EXPORT_SYMBOL(dma_device_is_channel_shared
);
1124 /****************************************************************************/
1126 * Allocates buffers for the descriptors. This is normally done automatically
1127 * but needs to be done explicitly when initiating a dma from interrupt
1131 * 0 Descriptors were allocated successfully
1132 * -EINVAL Invalid device type for this kind of transfer
1133 * (i.e. the device is _MEM_TO_DEV and not _DEV_TO_MEM)
1134 * -ENOMEM Memory exhausted
1136 /****************************************************************************/
1138 int dma_alloc_descriptors(DMA_Handle_t handle
, /* DMA Handle */
1139 dmacHw_TRANSFER_TYPE_e transferType
, /* Type of transfer being performed */
1140 dma_addr_t srcData
, /* Place to get data to write to device */
1141 dma_addr_t dstData
, /* Pointer to device data address */
1142 size_t numBytes
/* Number of bytes to transfer to the device */
1144 DMA_Channel_t
*channel
;
1145 DMA_DeviceAttribute_t
*devAttr
;
1147 size_t ringBytesRequired
;
1150 channel
= HandleToChannel(handle
);
1151 if (channel
== NULL
) {
1155 devAttr
= &DMA_gDeviceAttribute
[channel
->devType
];
1157 if (devAttr
->config
.transferType
!= transferType
) {
1161 /* Figure out how many descriptors we need. */
1163 /* printk("srcData: 0x%08x dstData: 0x%08x, numBytes: %d\n", */
1164 /* srcData, dstData, numBytes); */
1166 numDescriptors
= dmacHw_calculateDescriptorCount(&devAttr
->config
,
1170 if (numDescriptors
< 0) {
1171 printk(KERN_ERR
"%s: dmacHw_calculateDescriptorCount failed\n",
1176 /* Check to see if we can reuse the existing descriptor ring, or if we need to allocate */
1179 ringBytesRequired
= dmacHw_descriptorLen(numDescriptors
);
1181 /* printk("ringBytesRequired: %d\n", ringBytesRequired); */
1183 if (ringBytesRequired
> devAttr
->ring
.bytesAllocated
) {
1184 /* Make sure that this code path is never taken from interrupt context. */
1185 /* It's OK for an interrupt to initiate a DMA transfer, but the descriptor */
1186 /* allocation needs to have already been done. */
1190 /* Free the old descriptor ring and allocate a new one. */
1192 dma_free_descriptor_ring(&devAttr
->ring
);
1194 /* And allocate a new one. */
1197 dma_alloc_descriptor_ring(&devAttr
->ring
,
1201 "%s: dma_alloc_descriptor_ring(%d) failed\n",
1202 __func__
, numDescriptors
);
1205 /* Setup the descriptor for this transfer */
1207 if (dmacHw_initDescriptor(devAttr
->ring
.virtAddr
,
1208 devAttr
->ring
.physAddr
,
1209 devAttr
->ring
.bytesAllocated
,
1210 numDescriptors
) < 0) {
1211 printk(KERN_ERR
"%s: dmacHw_initDescriptor failed\n",
1216 /* We've already got enough ring buffer allocated. All we need to do is reset */
1217 /* any control information, just in case the previous DMA was stopped. */
1219 dmacHw_resetDescriptorControl(devAttr
->ring
.virtAddr
);
1222 /* dma_alloc/free both set the prevSrc/DstData to 0. If they happen to be the same */
1223 /* as last time, then we don't need to call setDataDescriptor again. */
1225 if (dmacHw_setDataDescriptor(&devAttr
->config
,
1226 devAttr
->ring
.virtAddr
,
1228 (void *)dstData
, numBytes
) < 0) {
1229 printk(KERN_ERR
"%s: dmacHw_setDataDescriptor failed\n",
1234 /* Remember the critical information for this transfer so that we can eliminate */
1235 /* another call to dma_alloc_descriptors if the caller reuses the same buffers */
1237 devAttr
->prevSrcData
= srcData
;
1238 devAttr
->prevDstData
= dstData
;
1239 devAttr
->prevNumBytes
= numBytes
;
1244 EXPORT_SYMBOL(dma_alloc_descriptors
);
1246 /****************************************************************************/
1248 * Allocates and sets up descriptors for a double buffered circular buffer.
1250 * This is primarily intended to be used for things like the ingress samples
1251 * from a microphone.
1254 * > 0 Number of descriptors actually allocated.
1255 * -EINVAL Invalid device type for this kind of transfer
1256 * (i.e. the device is _MEM_TO_DEV and not _DEV_TO_MEM)
1257 * -ENOMEM Memory exhausted
1259 /****************************************************************************/
1261 int dma_alloc_double_dst_descriptors(DMA_Handle_t handle
, /* DMA Handle */
1262 dma_addr_t srcData
, /* Physical address of source data */
1263 dma_addr_t dstData1
, /* Physical address of first destination buffer */
1264 dma_addr_t dstData2
, /* Physical address of second destination buffer */
1265 size_t numBytes
/* Number of bytes in each destination buffer */
1267 DMA_Channel_t
*channel
;
1268 DMA_DeviceAttribute_t
*devAttr
;
1269 int numDst1Descriptors
;
1270 int numDst2Descriptors
;
1272 size_t ringBytesRequired
;
1275 channel
= HandleToChannel(handle
);
1276 if (channel
== NULL
) {
1280 devAttr
= &DMA_gDeviceAttribute
[channel
->devType
];
1282 /* Figure out how many descriptors we need. */
1284 /* printk("srcData: 0x%08x dstData: 0x%08x, numBytes: %d\n", */
1285 /* srcData, dstData, numBytes); */
1287 numDst1Descriptors
=
1288 dmacHw_calculateDescriptorCount(&devAttr
->config
, (void *)srcData
,
1289 (void *)dstData1
, numBytes
);
1290 if (numDst1Descriptors
< 0) {
1293 numDst2Descriptors
=
1294 dmacHw_calculateDescriptorCount(&devAttr
->config
, (void *)srcData
,
1295 (void *)dstData2
, numBytes
);
1296 if (numDst2Descriptors
< 0) {
1299 numDescriptors
= numDst1Descriptors
+ numDst2Descriptors
;
1300 /* printk("numDescriptors: %d\n", numDescriptors); */
1302 /* Check to see if we can reuse the existing descriptor ring, or if we need to allocate */
1305 ringBytesRequired
= dmacHw_descriptorLen(numDescriptors
);
1307 /* printk("ringBytesRequired: %d\n", ringBytesRequired); */
1309 if (ringBytesRequired
> devAttr
->ring
.bytesAllocated
) {
1310 /* Make sure that this code path is never taken from interrupt context. */
1311 /* It's OK for an interrupt to initiate a DMA transfer, but the descriptor */
1312 /* allocation needs to have already been done. */
1316 /* Free the old descriptor ring and allocate a new one. */
1318 dma_free_descriptor_ring(&devAttr
->ring
);
1320 /* And allocate a new one. */
1323 dma_alloc_descriptor_ring(&devAttr
->ring
,
1327 "%s: dma_alloc_descriptor_ring(%d) failed\n",
1328 __func__
, ringBytesRequired
);
1333 /* Setup the descriptor for this transfer. Since this function is used with */
1334 /* CONTINUOUS DMA operations, we need to reinitialize every time, otherwise */
1335 /* setDataDescriptor will keep trying to append onto the end. */
1337 if (dmacHw_initDescriptor(devAttr
->ring
.virtAddr
,
1338 devAttr
->ring
.physAddr
,
1339 devAttr
->ring
.bytesAllocated
,
1340 numDescriptors
) < 0) {
1341 printk(KERN_ERR
"%s: dmacHw_initDescriptor failed\n", __func__
);
1345 /* dma_alloc/free both set the prevSrc/DstData to 0. If they happen to be the same */
1346 /* as last time, then we don't need to call setDataDescriptor again. */
1348 if (dmacHw_setDataDescriptor(&devAttr
->config
,
1349 devAttr
->ring
.virtAddr
,
1351 (void *)dstData1
, numBytes
) < 0) {
1352 printk(KERN_ERR
"%s: dmacHw_setDataDescriptor 1 failed\n",
1356 if (dmacHw_setDataDescriptor(&devAttr
->config
,
1357 devAttr
->ring
.virtAddr
,
1359 (void *)dstData2
, numBytes
) < 0) {
1360 printk(KERN_ERR
"%s: dmacHw_setDataDescriptor 2 failed\n",
1365 /* You should use dma_start_transfer rather than dma_transfer_xxx so we don't */
1366 /* try to make the 'prev' variables right. */
1368 devAttr
->prevSrcData
= 0;
1369 devAttr
->prevDstData
= 0;
1370 devAttr
->prevNumBytes
= 0;
1372 return numDescriptors
;
1375 EXPORT_SYMBOL(dma_alloc_double_dst_descriptors
);
1377 /****************************************************************************/
1379 * Initiates a transfer when the descriptors have already been setup.
1381 * This is a special case, and normally, the dma_transfer_xxx functions should
1385 * 0 Transfer was started successfully
1386 * -ENODEV Invalid handle
1388 /****************************************************************************/
1390 int dma_start_transfer(DMA_Handle_t handle
)
1392 DMA_Channel_t
*channel
;
1393 DMA_DeviceAttribute_t
*devAttr
;
1395 channel
= HandleToChannel(handle
);
1396 if (channel
== NULL
) {
1399 devAttr
= &DMA_gDeviceAttribute
[channel
->devType
];
1401 dmacHw_initiateTransfer(channel
->dmacHwHandle
, &devAttr
->config
,
1402 devAttr
->ring
.virtAddr
);
1404 /* Since we got this far, everything went successfully */
1409 EXPORT_SYMBOL(dma_start_transfer
);
1411 /****************************************************************************/
1413 * Stops a previously started DMA transfer.
1416 * 0 Transfer was stopped successfully
1417 * -ENODEV Invalid handle
1419 /****************************************************************************/
1421 int dma_stop_transfer(DMA_Handle_t handle
)
1423 DMA_Channel_t
*channel
;
1425 channel
= HandleToChannel(handle
);
1426 if (channel
== NULL
) {
1430 dmacHw_stopTransfer(channel
->dmacHwHandle
);
1435 EXPORT_SYMBOL(dma_stop_transfer
);
1437 /****************************************************************************/
1439 * Waits for a DMA to complete by polling. This function is only intended
1440 * to be used for testing. Interrupts should be used for most DMA operations.
1442 /****************************************************************************/
1444 int dma_wait_transfer_done(DMA_Handle_t handle
)
1446 DMA_Channel_t
*channel
;
1447 dmacHw_TRANSFER_STATUS_e status
;
1449 channel
= HandleToChannel(handle
);
1450 if (channel
== NULL
) {
1455 dmacHw_transferCompleted(channel
->dmacHwHandle
)) ==
1456 dmacHw_TRANSFER_STATUS_BUSY
) {
1460 if (status
== dmacHw_TRANSFER_STATUS_ERROR
) {
1461 printk(KERN_ERR
"%s: DMA transfer failed\n", __func__
);
1467 EXPORT_SYMBOL(dma_wait_transfer_done
);
1469 /****************************************************************************/
1471 * Initiates a DMA, allocating the descriptors as required.
1474 * 0 Transfer was started successfully
1475 * -EINVAL Invalid device type for this kind of transfer
1476 * (i.e. the device is _DEV_TO_MEM and not _MEM_TO_DEV)
1478 /****************************************************************************/
1480 int dma_transfer(DMA_Handle_t handle
, /* DMA Handle */
1481 dmacHw_TRANSFER_TYPE_e transferType
, /* Type of transfer being performed */
1482 dma_addr_t srcData
, /* Place to get data to write to device */
1483 dma_addr_t dstData
, /* Pointer to device data address */
1484 size_t numBytes
/* Number of bytes to transfer to the device */
1486 DMA_Channel_t
*channel
;
1487 DMA_DeviceAttribute_t
*devAttr
;
1490 channel
= HandleToChannel(handle
);
1491 if (channel
== NULL
) {
1495 devAttr
= &DMA_gDeviceAttribute
[channel
->devType
];
1497 if (devAttr
->config
.transferType
!= transferType
) {
1501 /* We keep track of the information about the previous request for this */
1502 /* device, and if the attributes match, then we can use the descriptors we setup */
1503 /* the last time, and not have to reinitialize everything. */
1507 dma_alloc_descriptors(handle
, transferType
, srcData
,
1514 /* And kick off the transfer */
1516 devAttr
->numBytes
= numBytes
;
1517 devAttr
->transferStartTime
= timer_get_tick_count();
1519 dmacHw_initiateTransfer(channel
->dmacHwHandle
, &devAttr
->config
,
1520 devAttr
->ring
.virtAddr
);
1522 /* Since we got this far, everything went successfully */
1527 EXPORT_SYMBOL(dma_transfer
);
1529 /****************************************************************************/
1531 * Set the callback function which will be called when a transfer completes.
1532 * If a NULL callback function is set, then no callback will occur.
1534 * @note @a devHandler will be called from IRQ context.
1538 * -ENODEV - Device handed in is invalid.
1540 /****************************************************************************/
1542 int dma_set_device_handler(DMA_Device_t dev
, /* Device to set the callback for. */
1543 DMA_DeviceHandler_t devHandler
, /* Function to call when the DMA completes */
1544 void *userData
/* Pointer which will be passed to devHandler. */
1546 DMA_DeviceAttribute_t
*devAttr
;
1547 unsigned long flags
;
1549 if (!IsDeviceValid(dev
)) {
1552 devAttr
= &DMA_gDeviceAttribute
[dev
];
1554 local_irq_save(flags
);
1556 devAttr
->userData
= userData
;
1557 devAttr
->devHandler
= devHandler
;
1559 local_irq_restore(flags
);
1564 EXPORT_SYMBOL(dma_set_device_handler
);
1566 /****************************************************************************/
1568 * Initializes a memory mapping structure
1570 /****************************************************************************/
1572 int dma_init_mem_map(DMA_MemMap_t
*memMap
)
1574 memset(memMap
, 0, sizeof(*memMap
));
1576 init_MUTEX(&memMap
->lock
);
1581 EXPORT_SYMBOL(dma_init_mem_map
);
1583 /****************************************************************************/
1585 * Releases any memory currently being held by a memory mapping structure.
1587 /****************************************************************************/
1589 int dma_term_mem_map(DMA_MemMap_t
*memMap
)
1591 down(&memMap
->lock
); /* Just being paranoid */
1593 /* Free up any allocated memory */
1596 memset(memMap
, 0, sizeof(*memMap
));
1601 EXPORT_SYMBOL(dma_term_mem_map
);
1603 /****************************************************************************/
1605 * Looks at a memory address and categorizes it.
1607 * @return One of the values from the DMA_MemType_t enumeration.
1609 /****************************************************************************/
1611 DMA_MemType_t
dma_mem_type(void *addr
)
1613 unsigned long addrVal
= (unsigned long)addr
;
1615 if (addrVal
>= VMALLOC_END
) {
1616 /* NOTE: DMA virtual memory space starts at 0xFFxxxxxx */
1618 /* dma_alloc_xxx pages are physically and virtually contiguous */
1620 return DMA_MEM_TYPE_DMA
;
1623 /* Technically, we could add one more classification. Addresses between VMALLOC_END */
1624 /* and the beginning of the DMA virtual address could be considered to be I/O space. */
1625 /* Right now, nobody cares about this particular classification, so we ignore it. */
1627 if (is_vmalloc_addr(addr
)) {
1628 /* Address comes from the vmalloc'd region. Pages are virtually */
1629 /* contiguous but NOT physically contiguous */
1631 return DMA_MEM_TYPE_VMALLOC
;
1634 if (addrVal
>= PAGE_OFFSET
) {
1635 /* PAGE_OFFSET is typically 0xC0000000 */
1637 /* kmalloc'd pages are physically contiguous */
1639 return DMA_MEM_TYPE_KMALLOC
;
1642 return DMA_MEM_TYPE_USER
;
1645 EXPORT_SYMBOL(dma_mem_type
);
1647 /****************************************************************************/
1649 * Looks at a memory address and determines if we support DMA'ing to/from
1650 * that type of memory.
1653 * return value != 0 means dma supported
1654 * return value == 0 means dma not supported
1656 /****************************************************************************/
1658 int dma_mem_supports_dma(void *addr
)
1660 DMA_MemType_t memType
= dma_mem_type(addr
);
1662 return (memType
== DMA_MEM_TYPE_DMA
)
1663 #if ALLOW_MAP_OF_KMALLOC_MEMORY
1664 || (memType
== DMA_MEM_TYPE_KMALLOC
)
1666 || (memType
== DMA_MEM_TYPE_USER
);
1669 EXPORT_SYMBOL(dma_mem_supports_dma
);
1671 /****************************************************************************/
1673 * Maps in a memory region such that it can be used for performing a DMA.
1677 /****************************************************************************/
1679 int dma_map_start(DMA_MemMap_t
*memMap
, /* Stores state information about the map */
1680 enum dma_data_direction dir
/* Direction that the mapping will be going */
1684 down(&memMap
->lock
);
1686 DMA_MAP_PRINT("memMap: %p\n", memMap
);
1688 if (memMap
->inUse
) {
1689 printk(KERN_ERR
"%s: memory map %p is already being used\n",
1697 memMap
->numRegionsUsed
= 0;
1703 DMA_MAP_PRINT("returning %d", rc
);
1710 EXPORT_SYMBOL(dma_map_start
);
1712 /****************************************************************************/
1714 * Adds a segment of memory to a memory map. Each segment is both
1715 * physically and virtually contiguous.
1717 * @return 0 on success, error code otherwise.
1719 /****************************************************************************/
1721 static int dma_map_add_segment(DMA_MemMap_t
*memMap
, /* Stores state information about the map */
1722 DMA_Region_t
*region
, /* Region that the segment belongs to */
1723 void *virtAddr
, /* Virtual address of the segment being added */
1724 dma_addr_t physAddr
, /* Physical address of the segment being added */
1725 size_t numBytes
/* Number of bytes of the segment being added */
1727 DMA_Segment_t
*segment
;
1729 DMA_MAP_PRINT("memMap:%p va:%p pa:0x%x #:%d\n", memMap
, virtAddr
,
1730 physAddr
, numBytes
);
1734 if (((unsigned long)virtAddr
< (unsigned long)region
->virtAddr
)
1735 || (((unsigned long)virtAddr
+ numBytes
)) >
1736 ((unsigned long)region
->virtAddr
+ region
->numBytes
)) {
1738 "%s: virtAddr %p is outside region @ %p len: %d\n",
1739 __func__
, virtAddr
, region
->virtAddr
, region
->numBytes
);
1743 if (region
->numSegmentsUsed
> 0) {
1744 /* Check to see if this segment is physically contiguous with the previous one */
1746 segment
= ®ion
->segment
[region
->numSegmentsUsed
- 1];
1748 if ((segment
->physAddr
+ segment
->numBytes
) == physAddr
) {
1749 /* It is - just add on to the end */
1751 DMA_MAP_PRINT("appending %d bytes to last segment\n",
1754 segment
->numBytes
+= numBytes
;
1760 /* Reallocate to hold more segments, if required. */
1762 if (region
->numSegmentsUsed
>= region
->numSegmentsAllocated
) {
1763 DMA_Segment_t
*newSegment
;
1765 region
->numSegmentsAllocated
* sizeof(*newSegment
);
1766 int newAlloc
= region
->numSegmentsAllocated
+ 4;
1767 size_t newSize
= newAlloc
* sizeof(*newSegment
);
1769 newSegment
= kmalloc(newSize
, GFP_KERNEL
);
1770 if (newSegment
== NULL
) {
1773 memcpy(newSegment
, region
->segment
, oldSize
);
1774 memset(&((uint8_t *) newSegment
)[oldSize
], 0,
1776 kfree(region
->segment
);
1778 region
->numSegmentsAllocated
= newAlloc
;
1779 region
->segment
= newSegment
;
1782 segment
= ®ion
->segment
[region
->numSegmentsUsed
];
1783 region
->numSegmentsUsed
++;
1785 segment
->virtAddr
= virtAddr
;
1786 segment
->physAddr
= physAddr
;
1787 segment
->numBytes
= numBytes
;
1789 DMA_MAP_PRINT("returning success\n");
1794 /****************************************************************************/
1796 * Adds a region of memory to a memory map. Each region is virtually
1797 * contiguous, but not necessarily physically contiguous.
1799 * @return 0 on success, error code otherwise.
1801 /****************************************************************************/
1803 int dma_map_add_region(DMA_MemMap_t
*memMap
, /* Stores state information about the map */
1804 void *mem
, /* Virtual address that we want to get a map of */
1805 size_t numBytes
/* Number of bytes being mapped */
1807 unsigned long addr
= (unsigned long)mem
;
1808 unsigned int offset
;
1810 DMA_Region_t
*region
;
1811 dma_addr_t physAddr
;
1813 down(&memMap
->lock
);
1815 DMA_MAP_PRINT("memMap:%p va:%p #:%d\n", memMap
, mem
, numBytes
);
1817 if (!memMap
->inUse
) {
1818 printk(KERN_ERR
"%s: Make sure you call dma_map_start first\n",
1824 /* Reallocate to hold more regions. */
1826 if (memMap
->numRegionsUsed
>= memMap
->numRegionsAllocated
) {
1827 DMA_Region_t
*newRegion
;
1829 memMap
->numRegionsAllocated
* sizeof(*newRegion
);
1830 int newAlloc
= memMap
->numRegionsAllocated
+ 4;
1831 size_t newSize
= newAlloc
* sizeof(*newRegion
);
1833 newRegion
= kmalloc(newSize
, GFP_KERNEL
);
1834 if (newRegion
== NULL
) {
1838 memcpy(newRegion
, memMap
->region
, oldSize
);
1839 memset(&((uint8_t *) newRegion
)[oldSize
], 0, newSize
- oldSize
);
1841 kfree(memMap
->region
);
1843 memMap
->numRegionsAllocated
= newAlloc
;
1844 memMap
->region
= newRegion
;
1847 region
= &memMap
->region
[memMap
->numRegionsUsed
];
1848 memMap
->numRegionsUsed
++;
1850 offset
= addr
& ~PAGE_MASK
;
1852 region
->memType
= dma_mem_type(mem
);
1853 region
->virtAddr
= mem
;
1854 region
->numBytes
= numBytes
;
1855 region
->numSegmentsUsed
= 0;
1856 region
->numLockedPages
= 0;
1857 region
->lockedPages
= NULL
;
1859 switch (region
->memType
) {
1860 case DMA_MEM_TYPE_VMALLOC
:
1862 atomic_inc(&gDmaStatMemTypeVmalloc
);
1864 /* printk(KERN_ERR "%s: vmalloc'd pages are not supported\n", __func__); */
1866 /* vmalloc'd pages are not physically contiguous */
1872 case DMA_MEM_TYPE_KMALLOC
:
1874 atomic_inc(&gDmaStatMemTypeKmalloc
);
1876 /* kmalloc'd pages are physically contiguous, so they'll have exactly */
1879 #if ALLOW_MAP_OF_KMALLOC_MEMORY
1881 dma_map_single(NULL
, mem
, numBytes
, memMap
->dir
);
1882 rc
= dma_map_add_segment(memMap
, region
, mem
, physAddr
,
1890 case DMA_MEM_TYPE_DMA
:
1892 /* dma_alloc_xxx pages are physically contiguous */
1894 atomic_inc(&gDmaStatMemTypeCoherent
);
1896 physAddr
= (vmalloc_to_pfn(mem
) << PAGE_SHIFT
) + offset
;
1898 dma_sync_single_for_cpu(NULL
, physAddr
, numBytes
,
1900 rc
= dma_map_add_segment(memMap
, region
, mem
, physAddr
,
1905 case DMA_MEM_TYPE_USER
:
1907 size_t firstPageOffset
;
1908 size_t firstPageSize
;
1909 struct page
**pages
;
1910 struct task_struct
*userTask
;
1912 atomic_inc(&gDmaStatMemTypeUser
);
1915 /* If the pages are user pages, then the dma_mem_map_set_user_task function */
1916 /* must have been previously called. */
1918 if (memMap
->userTask
== NULL
) {
1920 "%s: must call dma_mem_map_set_user_task when using user-mode memory\n",
1925 /* User pages need to be locked. */
1928 (unsigned long)region
->virtAddr
& (PAGE_SIZE
- 1);
1929 firstPageSize
= PAGE_SIZE
- firstPageOffset
;
1931 region
->numLockedPages
= (firstPageOffset
1932 + region
->numBytes
+
1933 PAGE_SIZE
- 1) / PAGE_SIZE
;
1935 kmalloc(region
->numLockedPages
*
1936 sizeof(struct page
*), GFP_KERNEL
);
1938 if (pages
== NULL
) {
1939 region
->numLockedPages
= 0;
1943 userTask
= memMap
->userTask
;
1945 down_read(&userTask
->mm
->mmap_sem
);
1946 rc
= get_user_pages(userTask
, /* task */
1947 userTask
->mm
, /* mm */
1948 (unsigned long)region
->virtAddr
, /* start */
1949 region
->numLockedPages
, /* len */
1950 memMap
->dir
== DMA_FROM_DEVICE
, /* write */
1952 pages
, /* pages (array of pointers to page) */
1954 up_read(&userTask
->mm
->mmap_sem
);
1956 if (rc
!= region
->numLockedPages
) {
1958 region
->numLockedPages
= 0;
1964 uint8_t *virtAddr
= region
->virtAddr
;
1965 size_t bytesRemaining
;
1968 rc
= 0; /* Since get_user_pages returns +ve number */
1970 region
->lockedPages
= pages
;
1972 /* We've locked the user pages. Now we need to walk them and figure */
1973 /* out the physical addresses. */
1975 /* The first page may be partial */
1977 dma_map_add_segment(memMap
,
1980 PFN_PHYS(page_to_pfn
1985 virtAddr
+= firstPageSize
;
1987 region
->numBytes
- firstPageSize
;
1990 pageIdx
< region
->numLockedPages
;
1992 size_t bytesThisPage
=
1994 PAGE_SIZE
? PAGE_SIZE
:
1998 ("pageIdx:%d pages[pageIdx]=%p pfn=%u phys=%u\n",
1999 pageIdx
, pages
[pageIdx
],
2000 page_to_pfn(pages
[pageIdx
]),
2001 PFN_PHYS(page_to_pfn
2004 dma_map_add_segment(memMap
,
2007 PFN_PHYS(page_to_pfn
2012 virtAddr
+= bytesThisPage
;
2013 bytesRemaining
-= bytesThisPage
;
2018 "%s: User mode pages are not yet supported\n",
2021 /* user pages are not physically contiguous */
2030 printk(KERN_ERR
"%s: Unsupported memory type: %d\n",
2031 __func__
, region
->memType
);
2039 memMap
->numRegionsUsed
--;
2044 DMA_MAP_PRINT("returning %d\n", rc
);
2051 EXPORT_SYMBOL(dma_map_add_segment
);
2053 /****************************************************************************/
2055 * Maps in a memory region such that it can be used for performing a DMA.
2057 * @return 0 on success, error code otherwise.
2059 /****************************************************************************/
2061 int dma_map_mem(DMA_MemMap_t
*memMap
, /* Stores state information about the map */
2062 void *mem
, /* Virtual address that we want to get a map of */
2063 size_t numBytes
, /* Number of bytes being mapped */
2064 enum dma_data_direction dir
/* Direction that the mapping will be going */
2068 rc
= dma_map_start(memMap
, dir
);
2070 rc
= dma_map_add_region(memMap
, mem
, numBytes
);
2072 /* Since the add fails, this function will fail, and the caller won't */
2073 /* call unmap, so we need to do it here. */
2075 dma_unmap(memMap
, 0);
2082 EXPORT_SYMBOL(dma_map_mem
);
2084 /****************************************************************************/
2086 * Setup a descriptor ring for a given memory map.
2088 * It is assumed that the descriptor ring has already been initialized, and
2089 * this routine will only reallocate a new descriptor ring if the existing
2092 * @return 0 on success, error code otherwise.
2094 /****************************************************************************/
2096 int dma_map_create_descriptor_ring(DMA_Device_t dev
, /* DMA device (where the ring is stored) */
2097 DMA_MemMap_t
*memMap
, /* Memory map that will be used */
2098 dma_addr_t devPhysAddr
/* Physical address of device */
2102 DMA_DeviceAttribute_t
*devAttr
;
2103 DMA_Region_t
*region
;
2104 DMA_Segment_t
*segment
;
2105 dma_addr_t srcPhysAddr
;
2106 dma_addr_t dstPhysAddr
;
2110 devAttr
= &DMA_gDeviceAttribute
[dev
];
2112 down(&memMap
->lock
);
2114 /* Figure out how many descriptors we need */
2117 for (regionIdx
= 0; regionIdx
< memMap
->numRegionsUsed
; regionIdx
++) {
2118 region
= &memMap
->region
[regionIdx
];
2120 for (segmentIdx
= 0; segmentIdx
< region
->numSegmentsUsed
;
2122 segment
= ®ion
->segment
[segmentIdx
];
2124 if (memMap
->dir
== DMA_TO_DEVICE
) {
2125 srcPhysAddr
= segment
->physAddr
;
2126 dstPhysAddr
= devPhysAddr
;
2128 srcPhysAddr
= devPhysAddr
;
2129 dstPhysAddr
= segment
->physAddr
;
2133 dma_calculate_descriptor_count(dev
, srcPhysAddr
,
2139 "%s: dma_calculate_descriptor_count failed: %d\n",
2143 numDescriptors
+= rc
;
2147 /* Adjust the size of the ring, if it isn't big enough */
2149 if (numDescriptors
> devAttr
->ring
.descriptorsAllocated
) {
2150 dma_free_descriptor_ring(&devAttr
->ring
);
2152 dma_alloc_descriptor_ring(&devAttr
->ring
,
2156 "%s: dma_alloc_descriptor_ring failed: %d\n",
2162 dma_init_descriptor_ring(&devAttr
->ring
,
2166 "%s: dma_init_descriptor_ring failed: %d\n",
2172 /* Populate the descriptors */
2174 for (regionIdx
= 0; regionIdx
< memMap
->numRegionsUsed
; regionIdx
++) {
2175 region
= &memMap
->region
[regionIdx
];
2177 for (segmentIdx
= 0; segmentIdx
< region
->numSegmentsUsed
;
2179 segment
= ®ion
->segment
[segmentIdx
];
2181 if (memMap
->dir
== DMA_TO_DEVICE
) {
2182 srcPhysAddr
= segment
->physAddr
;
2183 dstPhysAddr
= devPhysAddr
;
2185 srcPhysAddr
= devPhysAddr
;
2186 dstPhysAddr
= segment
->physAddr
;
2190 dma_add_descriptors(&devAttr
->ring
, dev
,
2191 srcPhysAddr
, dstPhysAddr
,
2195 "%s: dma_add_descriptors failed: %d\n",
2210 EXPORT_SYMBOL(dma_map_create_descriptor_ring
);
2212 /****************************************************************************/
2214 * Maps in a memory region such that it can be used for performing a DMA.
2218 /****************************************************************************/
2220 int dma_unmap(DMA_MemMap_t
*memMap
, /* Stores state information about the map */
2221 int dirtied
/* non-zero if any of the pages were modified */
2225 DMA_Region_t
*region
;
2226 DMA_Segment_t
*segment
;
2228 for (regionIdx
= 0; regionIdx
< memMap
->numRegionsUsed
; regionIdx
++) {
2229 region
= &memMap
->region
[regionIdx
];
2231 for (segmentIdx
= 0; segmentIdx
< region
->numSegmentsUsed
;
2233 segment
= ®ion
->segment
[segmentIdx
];
2235 switch (region
->memType
) {
2236 case DMA_MEM_TYPE_VMALLOC
:
2239 "%s: vmalloc'd pages are not yet supported\n",
2244 case DMA_MEM_TYPE_KMALLOC
:
2246 #if ALLOW_MAP_OF_KMALLOC_MEMORY
2247 dma_unmap_single(NULL
,
2255 case DMA_MEM_TYPE_DMA
:
2257 dma_sync_single_for_cpu(NULL
,
2266 case DMA_MEM_TYPE_USER
:
2268 /* Nothing to do here. */
2276 "%s: Unsupported memory type: %d\n",
2277 __func__
, region
->memType
);
2282 segment
->virtAddr
= NULL
;
2283 segment
->physAddr
= 0;
2284 segment
->numBytes
= 0;
2287 if (region
->numLockedPages
> 0) {
2290 /* Some user pages were locked. We need to go and unlock them now. */
2292 for (pageIdx
= 0; pageIdx
< region
->numLockedPages
;
2295 region
->lockedPages
[pageIdx
];
2297 if (memMap
->dir
== DMA_FROM_DEVICE
) {
2300 page_cache_release(page
);
2302 kfree(region
->lockedPages
);
2303 region
->numLockedPages
= 0;
2304 region
->lockedPages
= NULL
;
2307 region
->memType
= DMA_MEM_TYPE_NONE
;
2308 region
->virtAddr
= NULL
;
2309 region
->numBytes
= 0;
2310 region
->numSegmentsUsed
= 0;
2312 memMap
->userTask
= NULL
;
2313 memMap
->numRegionsUsed
= 0;
2321 EXPORT_SYMBOL(dma_unmap
);