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>
31 #include <linux/slab.h>
33 #include <mach/timer.h>
36 #include <linux/pfn.h>
37 #include <asm/atomic.h>
40 /* I don't quite understand why dc4 fails when this is set to 1 and DMA is enabled */
41 /* especially since dc4 doesn't use kmalloc'd memory. */
43 #define ALLOW_MAP_OF_KMALLOC_MEMORY 0
45 /* ---- Public Variables ------------------------------------------------- */
47 /* ---- Private Constants and Types -------------------------------------- */
49 #define MAKE_HANDLE(controllerIdx, channelIdx) (((controllerIdx) << 4) | (channelIdx))
51 #define CONTROLLER_FROM_HANDLE(handle) (((handle) >> 4) & 0x0f)
52 #define CHANNEL_FROM_HANDLE(handle) ((handle) & 0x0f)
54 #define DMA_MAP_DEBUG 0
57 # define DMA_MAP_PRINT(fmt, args...) printk("%s: " fmt, __func__, ## args)
59 # define DMA_MAP_PRINT(fmt, args...)
62 /* ---- Private Variables ------------------------------------------------ */
64 static DMA_Global_t gDMA
;
65 static struct proc_dir_entry
*gDmaDir
;
67 static atomic_t gDmaStatMemTypeKmalloc
= ATOMIC_INIT(0);
68 static atomic_t gDmaStatMemTypeVmalloc
= ATOMIC_INIT(0);
69 static atomic_t gDmaStatMemTypeUser
= ATOMIC_INIT(0);
70 static atomic_t gDmaStatMemTypeCoherent
= ATOMIC_INIT(0);
72 #include "dma_device.c"
74 /* ---- Private Function Prototypes -------------------------------------- */
76 /* ---- Functions ------------------------------------------------------- */
78 /****************************************************************************/
80 * Displays information for /proc/dma/mem-type
82 /****************************************************************************/
84 static int dma_proc_read_mem_type(char *buf
, char **start
, off_t offset
,
85 int count
, int *eof
, void *data
)
89 len
+= sprintf(buf
+ len
, "dma_map_mem statistics\n");
91 sprintf(buf
+ len
, "coherent: %d\n",
92 atomic_read(&gDmaStatMemTypeCoherent
));
94 sprintf(buf
+ len
, "kmalloc: %d\n",
95 atomic_read(&gDmaStatMemTypeKmalloc
));
97 sprintf(buf
+ len
, "vmalloc: %d\n",
98 atomic_read(&gDmaStatMemTypeVmalloc
));
100 sprintf(buf
+ len
, "user: %d\n",
101 atomic_read(&gDmaStatMemTypeUser
));
106 /****************************************************************************/
108 * Displays information for /proc/dma/channels
110 /****************************************************************************/
112 static int dma_proc_read_channels(char *buf
, char **start
, off_t offset
,
113 int count
, int *eof
, void *data
)
117 int limit
= count
- 200;
119 DMA_Channel_t
*channel
;
121 if (down_interruptible(&gDMA
.lock
) < 0) {
125 for (controllerIdx
= 0; controllerIdx
< DMA_NUM_CONTROLLERS
;
127 for (channelIdx
= 0; channelIdx
< DMA_NUM_CHANNELS
;
134 &gDMA
.controller
[controllerIdx
].channel
[channelIdx
];
137 sprintf(buf
+ len
, "%d:%d ", controllerIdx
,
140 if ((channel
->flags
& DMA_CHANNEL_FLAG_IS_DEDICATED
) !=
143 sprintf(buf
+ len
, "Dedicated for %s ",
144 DMA_gDeviceAttribute
[channel
->
147 len
+= sprintf(buf
+ len
, "Shared ");
150 if ((channel
->flags
& DMA_CHANNEL_FLAG_NO_ISR
) != 0) {
151 len
+= sprintf(buf
+ len
, "No ISR ");
154 if ((channel
->flags
& DMA_CHANNEL_FLAG_LARGE_FIFO
) != 0) {
155 len
+= sprintf(buf
+ len
, "Fifo: 128 ");
157 len
+= sprintf(buf
+ len
, "Fifo: 64 ");
160 if ((channel
->flags
& DMA_CHANNEL_FLAG_IN_USE
) != 0) {
162 sprintf(buf
+ len
, "InUse by %s",
163 DMA_gDeviceAttribute
[channel
->
165 #if (DMA_DEBUG_TRACK_RESERVATION)
167 sprintf(buf
+ len
, " (%s:%d)",
172 len
+= sprintf(buf
+ len
, "Avail ");
175 if (channel
->lastDevType
!= DMA_DEVICE_NONE
) {
177 sprintf(buf
+ len
, "Last use: %s ",
178 DMA_gDeviceAttribute
[channel
->
183 len
+= sprintf(buf
+ len
, "\n");
192 /****************************************************************************/
194 * Displays information for /proc/dma/devices
196 /****************************************************************************/
198 static int dma_proc_read_devices(char *buf
, char **start
, off_t offset
,
199 int count
, int *eof
, void *data
)
201 int limit
= count
- 200;
205 if (down_interruptible(&gDMA
.lock
) < 0) {
209 for (devIdx
= 0; devIdx
< DMA_NUM_DEVICE_ENTRIES
; devIdx
++) {
210 DMA_DeviceAttribute_t
*devAttr
= &DMA_gDeviceAttribute
[devIdx
];
212 if (devAttr
->name
== NULL
) {
220 len
+= sprintf(buf
+ len
, "%-12s ", devAttr
->name
);
222 if ((devAttr
->flags
& DMA_DEVICE_FLAG_IS_DEDICATED
) != 0) {
224 sprintf(buf
+ len
, "Dedicated %d:%d ",
225 devAttr
->dedicatedController
,
226 devAttr
->dedicatedChannel
);
228 len
+= sprintf(buf
+ len
, "Shared DMA:");
229 if ((devAttr
->flags
& DMA_DEVICE_FLAG_ON_DMA0
) != 0) {
230 len
+= sprintf(buf
+ len
, "0");
232 if ((devAttr
->flags
& DMA_DEVICE_FLAG_ON_DMA1
) != 0) {
233 len
+= sprintf(buf
+ len
, "1");
235 len
+= sprintf(buf
+ len
, " ");
237 if ((devAttr
->flags
& DMA_DEVICE_FLAG_NO_ISR
) != 0) {
238 len
+= sprintf(buf
+ len
, "NoISR ");
240 if ((devAttr
->flags
& DMA_DEVICE_FLAG_ALLOW_LARGE_FIFO
) != 0) {
241 len
+= sprintf(buf
+ len
, "Allow-128 ");
246 "Xfer #: %Lu Ticks: %Lu Bytes: %Lu DescLen: %u\n",
247 devAttr
->numTransfers
, devAttr
->transferTicks
,
248 devAttr
->transferBytes
,
249 devAttr
->ring
.bytesAllocated
);
259 /****************************************************************************/
261 * Determines if a DMA_Device_t is "valid".
264 * TRUE - dma device is valid
265 * FALSE - dma device isn't valid
267 /****************************************************************************/
269 static inline int IsDeviceValid(DMA_Device_t device
)
271 return (device
>= 0) && (device
< DMA_NUM_DEVICE_ENTRIES
);
274 /****************************************************************************/
276 * Translates a DMA handle into a pointer to a channel.
279 * non-NULL - pointer to DMA_Channel_t
280 * NULL - DMA Handle was invalid
282 /****************************************************************************/
284 static inline DMA_Channel_t
*HandleToChannel(DMA_Handle_t handle
)
289 controllerIdx
= CONTROLLER_FROM_HANDLE(handle
);
290 channelIdx
= CHANNEL_FROM_HANDLE(handle
);
292 if ((controllerIdx
> DMA_NUM_CONTROLLERS
)
293 || (channelIdx
> DMA_NUM_CHANNELS
)) {
296 return &gDMA
.controller
[controllerIdx
].channel
[channelIdx
];
299 /****************************************************************************/
301 * Interrupt handler which is called to process DMA interrupts.
303 /****************************************************************************/
305 static irqreturn_t
dma_interrupt_handler(int irq
, void *dev_id
)
307 DMA_Channel_t
*channel
;
308 DMA_DeviceAttribute_t
*devAttr
;
311 channel
= (DMA_Channel_t
*) dev_id
;
313 /* Figure out why we were called, and knock down the interrupt */
315 irqStatus
= dmacHw_getInterruptStatus(channel
->dmacHwHandle
);
316 dmacHw_clearInterrupt(channel
->dmacHwHandle
);
318 if ((channel
->devType
< 0)
319 || (channel
->devType
> DMA_NUM_DEVICE_ENTRIES
)) {
320 printk(KERN_ERR
"dma_interrupt_handler: Invalid devType: %d\n",
324 devAttr
= &DMA_gDeviceAttribute
[channel
->devType
];
328 if ((irqStatus
& dmacHw_INTERRUPT_STATUS_TRANS
) != 0) {
329 devAttr
->transferTicks
+=
330 (timer_get_tick_count() - devAttr
->transferStartTime
);
333 if ((irqStatus
& dmacHw_INTERRUPT_STATUS_ERROR
) != 0) {
335 "dma_interrupt_handler: devType :%d DMA error (%s)\n",
336 channel
->devType
, devAttr
->name
);
338 devAttr
->numTransfers
++;
339 devAttr
->transferBytes
+= devAttr
->numBytes
;
342 /* Call any installed handler */
344 if (devAttr
->devHandler
!= NULL
) {
345 devAttr
->devHandler(channel
->devType
, irqStatus
,
352 /****************************************************************************/
354 * Allocates memory to hold a descriptor ring. The descriptor ring then
355 * needs to be populated by making one or more calls to
356 * dna_add_descriptors.
358 * The returned descriptor ring will be automatically initialized.
361 * 0 Descriptor ring was allocated successfully
362 * -EINVAL Invalid parameters passed in
363 * -ENOMEM Unable to allocate memory for the desired number of descriptors.
365 /****************************************************************************/
367 int dma_alloc_descriptor_ring(DMA_DescriptorRing_t
*ring
, /* Descriptor ring to populate */
368 int numDescriptors
/* Number of descriptors that need to be allocated. */
370 size_t bytesToAlloc
= dmacHw_descriptorLen(numDescriptors
);
372 if ((ring
== NULL
) || (numDescriptors
<= 0)) {
377 ring
->descriptorsAllocated
= 0;
378 ring
->bytesAllocated
= 0;
380 ring
->virtAddr
= dma_alloc_writecombine(NULL
,
384 if (ring
->virtAddr
== NULL
) {
388 ring
->bytesAllocated
= bytesToAlloc
;
389 ring
->descriptorsAllocated
= numDescriptors
;
391 return dma_init_descriptor_ring(ring
, numDescriptors
);
394 EXPORT_SYMBOL(dma_alloc_descriptor_ring
);
396 /****************************************************************************/
398 * Releases the memory which was previously allocated for a descriptor ring.
400 /****************************************************************************/
402 void dma_free_descriptor_ring(DMA_DescriptorRing_t
*ring
/* Descriptor to release */
404 if (ring
->virtAddr
!= NULL
) {
405 dma_free_writecombine(NULL
,
406 ring
->bytesAllocated
,
407 ring
->virtAddr
, ring
->physAddr
);
410 ring
->bytesAllocated
= 0;
411 ring
->descriptorsAllocated
= 0;
412 ring
->virtAddr
= NULL
;
416 EXPORT_SYMBOL(dma_free_descriptor_ring
);
418 /****************************************************************************/
420 * Initializes a descriptor ring, so that descriptors can be added to it.
421 * Once a descriptor ring has been allocated, it may be reinitialized for
422 * use with additional/different regions of memory.
424 * Note that if 7 descriptors are allocated, it's perfectly acceptable to
425 * initialize the ring with a smaller number of descriptors. The amount
426 * of memory allocated for the descriptor ring will not be reduced, and
427 * the descriptor ring may be reinitialized later
430 * 0 Descriptor ring was initialized successfully
431 * -ENOMEM The descriptor which was passed in has insufficient space
432 * to hold the desired number of descriptors.
434 /****************************************************************************/
436 int dma_init_descriptor_ring(DMA_DescriptorRing_t
*ring
, /* Descriptor ring to initialize */
437 int numDescriptors
/* Number of descriptors to initialize. */
439 if (ring
->virtAddr
== NULL
) {
442 if (dmacHw_initDescriptor(ring
->virtAddr
,
444 ring
->bytesAllocated
, numDescriptors
) < 0) {
446 "dma_init_descriptor_ring: dmacHw_initDescriptor failed\n");
453 EXPORT_SYMBOL(dma_init_descriptor_ring
);
455 /****************************************************************************/
457 * Determines the number of descriptors which would be required for a
458 * transfer of the indicated memory region.
460 * This function also needs to know which DMA device this transfer will
461 * be destined for, so that the appropriate DMA configuration can be retrieved.
462 * DMA parameters such as transfer width, and whether this is a memory-to-memory
463 * or memory-to-peripheral, etc can all affect the actual number of descriptors
467 * > 0 Returns the number of descriptors required for the indicated transfer
468 * -ENODEV - Device handed in is invalid.
469 * -EINVAL Invalid parameters
470 * -ENOMEM Memory exhausted
472 /****************************************************************************/
474 int dma_calculate_descriptor_count(DMA_Device_t device
, /* DMA Device that this will be associated with */
475 dma_addr_t srcData
, /* Place to get data to write to device */
476 dma_addr_t dstData
, /* Pointer to device data address */
477 size_t numBytes
/* Number of bytes to transfer to the device */
480 DMA_DeviceAttribute_t
*devAttr
;
482 if (!IsDeviceValid(device
)) {
485 devAttr
= &DMA_gDeviceAttribute
[device
];
487 numDescriptors
= dmacHw_calculateDescriptorCount(&devAttr
->config
,
491 if (numDescriptors
< 0) {
493 "dma_calculate_descriptor_count: dmacHw_calculateDescriptorCount failed\n");
497 return numDescriptors
;
500 EXPORT_SYMBOL(dma_calculate_descriptor_count
);
502 /****************************************************************************/
504 * Adds a region of memory to the descriptor ring. Note that it may take
505 * multiple descriptors for each region of memory. It is the callers
506 * responsibility to allocate a sufficiently large descriptor ring.
509 * 0 Descriptors were added successfully
510 * -ENODEV Device handed in is invalid.
511 * -EINVAL Invalid parameters
512 * -ENOMEM Memory exhausted
514 /****************************************************************************/
516 int dma_add_descriptors(DMA_DescriptorRing_t
*ring
, /* Descriptor ring to add descriptors to */
517 DMA_Device_t device
, /* DMA Device that descriptors are for */
518 dma_addr_t srcData
, /* Place to get data (memory or device) */
519 dma_addr_t dstData
, /* Place to put data (memory or device) */
520 size_t numBytes
/* Number of bytes to transfer to the device */
523 DMA_DeviceAttribute_t
*devAttr
;
525 if (!IsDeviceValid(device
)) {
528 devAttr
= &DMA_gDeviceAttribute
[device
];
530 rc
= dmacHw_setDataDescriptor(&devAttr
->config
,
533 (void *)dstData
, numBytes
);
536 "dma_add_descriptors: dmacHw_setDataDescriptor failed with code: %d\n",
544 EXPORT_SYMBOL(dma_add_descriptors
);
546 /****************************************************************************/
548 * Sets the descriptor ring associated with a device.
550 * Once set, the descriptor ring will be associated with the device, even
551 * across channel request/free calls. Passing in a NULL descriptor ring
552 * will release any descriptor ring currently associated with the device.
554 * Note: If you call dma_transfer, or one of the other dma_alloc_ functions
555 * the descriptor ring may be released and reallocated.
557 * Note: This function will release the descriptor memory for any current
558 * descriptor ring associated with this device.
561 * 0 Descriptors were added successfully
562 * -ENODEV Device handed in is invalid.
564 /****************************************************************************/
566 int dma_set_device_descriptor_ring(DMA_Device_t device
, /* Device to update the descriptor ring for. */
567 DMA_DescriptorRing_t
*ring
/* Descriptor ring to add descriptors to */
569 DMA_DeviceAttribute_t
*devAttr
;
571 if (!IsDeviceValid(device
)) {
574 devAttr
= &DMA_gDeviceAttribute
[device
];
576 /* Free the previously allocated descriptor ring */
578 dma_free_descriptor_ring(&devAttr
->ring
);
581 /* Copy in the new one */
583 devAttr
->ring
= *ring
;
586 /* Set things up so that if dma_transfer is called then this descriptor */
587 /* ring will get freed. */
589 devAttr
->prevSrcData
= 0;
590 devAttr
->prevDstData
= 0;
591 devAttr
->prevNumBytes
= 0;
596 EXPORT_SYMBOL(dma_set_device_descriptor_ring
);
598 /****************************************************************************/
600 * Retrieves the descriptor ring associated with a device.
603 * 0 Descriptors were added successfully
604 * -ENODEV Device handed in is invalid.
606 /****************************************************************************/
608 int dma_get_device_descriptor_ring(DMA_Device_t device
, /* Device to retrieve the descriptor ring for. */
609 DMA_DescriptorRing_t
*ring
/* Place to store retrieved ring */
611 DMA_DeviceAttribute_t
*devAttr
;
613 memset(ring
, 0, sizeof(*ring
));
615 if (!IsDeviceValid(device
)) {
618 devAttr
= &DMA_gDeviceAttribute
[device
];
620 *ring
= devAttr
->ring
;
625 EXPORT_SYMBOL(dma_get_device_descriptor_ring
);
627 /****************************************************************************/
629 * Configures a DMA channel.
632 * >= 0 - Initialization was successful.
634 * -EBUSY - Device is currently being used.
635 * -ENODEV - Device handed in is invalid.
637 /****************************************************************************/
639 static int ConfigChannel(DMA_Handle_t handle
)
641 DMA_Channel_t
*channel
;
642 DMA_DeviceAttribute_t
*devAttr
;
645 channel
= HandleToChannel(handle
);
646 if (channel
== NULL
) {
649 devAttr
= &DMA_gDeviceAttribute
[channel
->devType
];
650 controllerIdx
= CONTROLLER_FROM_HANDLE(handle
);
652 if ((devAttr
->flags
& DMA_DEVICE_FLAG_PORT_PER_DMAC
) != 0) {
653 if (devAttr
->config
.transferType
==
654 dmacHw_TRANSFER_TYPE_MEM_TO_PERIPHERAL
) {
655 devAttr
->config
.dstPeripheralPort
=
656 devAttr
->dmacPort
[controllerIdx
];
657 } else if (devAttr
->config
.transferType
==
658 dmacHw_TRANSFER_TYPE_PERIPHERAL_TO_MEM
) {
659 devAttr
->config
.srcPeripheralPort
=
660 devAttr
->dmacPort
[controllerIdx
];
664 if (dmacHw_configChannel(channel
->dmacHwHandle
, &devAttr
->config
) != 0) {
665 printk(KERN_ERR
"ConfigChannel: dmacHw_configChannel failed\n");
672 /****************************************************************************/
674 * Initializes all of the data structures associated with the DMA.
676 * >= 0 - Initialization was successful.
678 * -EBUSY - Device is currently being used.
679 * -ENODEV - Device handed in is invalid.
681 /****************************************************************************/
689 DMA_Channel_t
*channel
;
690 DMA_Handle_t dedicatedHandle
;
692 memset(&gDMA
, 0, sizeof(gDMA
));
694 sema_init(&gDMA
.lock
, 0);
695 init_waitqueue_head(&gDMA
.freeChannelQ
);
697 /* Initialize the Hardware */
701 /* Start off by marking all of the DMA channels as shared. */
703 for (controllerIdx
= 0; controllerIdx
< DMA_NUM_CONTROLLERS
;
705 for (channelIdx
= 0; channelIdx
< DMA_NUM_CHANNELS
;
708 &gDMA
.controller
[controllerIdx
].channel
[channelIdx
];
711 channel
->devType
= DMA_DEVICE_NONE
;
712 channel
->lastDevType
= DMA_DEVICE_NONE
;
714 #if (DMA_DEBUG_TRACK_RESERVATION)
715 channel
->fileName
= "";
716 channel
->lineNum
= 0;
719 channel
->dmacHwHandle
=
720 dmacHw_getChannelHandle(dmacHw_MAKE_CHANNEL_ID
723 dmacHw_initChannel(channel
->dmacHwHandle
);
727 /* Record any special attributes that channels may have */
729 gDMA
.controller
[0].channel
[0].flags
|= DMA_CHANNEL_FLAG_LARGE_FIFO
;
730 gDMA
.controller
[0].channel
[1].flags
|= DMA_CHANNEL_FLAG_LARGE_FIFO
;
731 gDMA
.controller
[1].channel
[0].flags
|= DMA_CHANNEL_FLAG_LARGE_FIFO
;
732 gDMA
.controller
[1].channel
[1].flags
|= DMA_CHANNEL_FLAG_LARGE_FIFO
;
734 /* Now walk through and record the dedicated channels. */
736 for (devIdx
= 0; devIdx
< DMA_NUM_DEVICE_ENTRIES
; devIdx
++) {
737 DMA_DeviceAttribute_t
*devAttr
= &DMA_gDeviceAttribute
[devIdx
];
739 if (((devAttr
->flags
& DMA_DEVICE_FLAG_NO_ISR
) != 0)
740 && ((devAttr
->flags
& DMA_DEVICE_FLAG_IS_DEDICATED
) == 0)) {
742 "DMA Device: %s Can only request NO_ISR for dedicated devices\n",
748 if ((devAttr
->flags
& DMA_DEVICE_FLAG_IS_DEDICATED
) != 0) {
749 /* This is a dedicated device. Mark the channel as being reserved. */
751 if (devAttr
->dedicatedController
>= DMA_NUM_CONTROLLERS
) {
753 "DMA Device: %s DMA Controller %d is out of range\n",
755 devAttr
->dedicatedController
);
760 if (devAttr
->dedicatedChannel
>= DMA_NUM_CHANNELS
) {
762 "DMA Device: %s DMA Channel %d is out of range\n",
764 devAttr
->dedicatedChannel
);
770 MAKE_HANDLE(devAttr
->dedicatedController
,
771 devAttr
->dedicatedChannel
);
772 channel
= HandleToChannel(dedicatedHandle
);
774 if ((channel
->flags
& DMA_CHANNEL_FLAG_IS_DEDICATED
) !=
777 ("DMA Device: %s attempting to use same DMA Controller:Channel (%d:%d) as %s\n",
779 devAttr
->dedicatedController
,
780 devAttr
->dedicatedChannel
,
781 DMA_gDeviceAttribute
[channel
->devType
].
787 channel
->flags
|= DMA_CHANNEL_FLAG_IS_DEDICATED
;
788 channel
->devType
= devIdx
;
790 if (devAttr
->flags
& DMA_DEVICE_FLAG_NO_ISR
) {
791 channel
->flags
|= DMA_CHANNEL_FLAG_NO_ISR
;
794 /* For dedicated channels, we can go ahead and configure the DMA channel now */
797 ConfigChannel(dedicatedHandle
);
801 /* Go through and register the interrupt handlers */
803 for (controllerIdx
= 0; controllerIdx
< DMA_NUM_CONTROLLERS
;
805 for (channelIdx
= 0; channelIdx
< DMA_NUM_CHANNELS
;
808 &gDMA
.controller
[controllerIdx
].channel
[channelIdx
];
810 if ((channel
->flags
& DMA_CHANNEL_FLAG_NO_ISR
) == 0) {
811 snprintf(channel
->name
, sizeof(channel
->name
),
812 "dma %d:%d %s", controllerIdx
,
815 DMA_DEVICE_NONE
? "" :
816 DMA_gDeviceAttribute
[channel
->devType
].
820 request_irq(IRQ_DMA0C0
+
824 dma_interrupt_handler
,
825 IRQF_DISABLED
, channel
->name
,
829 "request_irq for IRQ_DMA%dC%d failed\n",
830 controllerIdx
, channelIdx
);
836 /* Create /proc/dma/channels and /proc/dma/devices */
838 gDmaDir
= create_proc_entry("dma", S_IFDIR
| S_IRUGO
| S_IXUGO
, NULL
);
840 if (gDmaDir
== NULL
) {
841 printk(KERN_ERR
"Unable to create /proc/dma\n");
843 create_proc_read_entry("channels", 0, gDmaDir
,
844 dma_proc_read_channels
, NULL
);
845 create_proc_read_entry("devices", 0, gDmaDir
,
846 dma_proc_read_devices
, NULL
);
847 create_proc_read_entry("mem-type", 0, gDmaDir
,
848 dma_proc_read_mem_type
, NULL
);
858 /****************************************************************************/
860 * Reserves a channel for use with @a dev. If the device is setup to use
861 * a shared channel, then this function will block until a free channel
865 * >= 0 - A valid DMA Handle.
866 * -EBUSY - Device is currently being used.
867 * -ENODEV - Device handed in is invalid.
869 /****************************************************************************/
871 #if (DMA_DEBUG_TRACK_RESERVATION)
872 DMA_Handle_t dma_request_channel_dbg
873 (DMA_Device_t dev
, const char *fileName
, int lineNum
)
875 DMA_Handle_t
dma_request_channel(DMA_Device_t dev
)
879 DMA_DeviceAttribute_t
*devAttr
;
880 DMA_Channel_t
*channel
;
885 if (down_interruptible(&gDMA
.lock
) < 0) {
889 if ((dev
< 0) || (dev
>= DMA_NUM_DEVICE_ENTRIES
)) {
893 devAttr
= &DMA_gDeviceAttribute
[dev
];
895 #if (DMA_DEBUG_TRACK_RESERVATION)
899 s
= strrchr(fileName
, '/');
905 if ((devAttr
->flags
& DMA_DEVICE_FLAG_IN_USE
) != 0) {
906 /* This device has already been requested and not been freed */
908 printk(KERN_ERR
"%s: device %s is already requested\n",
909 __func__
, devAttr
->name
);
914 if ((devAttr
->flags
& DMA_DEVICE_FLAG_IS_DEDICATED
) != 0) {
915 /* This device has a dedicated channel. */
918 &gDMA
.controller
[devAttr
->dedicatedController
].
919 channel
[devAttr
->dedicatedChannel
];
920 if ((channel
->flags
& DMA_CHANNEL_FLAG_IN_USE
) != 0) {
925 channel
->flags
|= DMA_CHANNEL_FLAG_IN_USE
;
926 devAttr
->flags
|= DMA_DEVICE_FLAG_IN_USE
;
928 #if (DMA_DEBUG_TRACK_RESERVATION)
929 channel
->fileName
= fileName
;
930 channel
->lineNum
= lineNum
;
933 MAKE_HANDLE(devAttr
->dedicatedController
,
934 devAttr
->dedicatedChannel
);
938 /* This device needs to use one of the shared channels. */
940 handle
= DMA_INVALID_HANDLE
;
941 while (handle
== DMA_INVALID_HANDLE
) {
942 /* Scan through the shared channels and see if one is available */
944 for (controllerIdx2
= 0; controllerIdx2
< DMA_NUM_CONTROLLERS
;
946 /* Check to see if we should try on controller 1 first. */
948 controllerIdx
= controllerIdx2
;
950 flags
& DMA_DEVICE_FLAG_ALLOC_DMA1_FIRST
) != 0) {
951 controllerIdx
= 1 - controllerIdx
;
954 /* See if the device is available on the controller being tested */
957 flags
& (DMA_DEVICE_FLAG_ON_DMA0
<< controllerIdx
))
960 channelIdx
< DMA_NUM_CHANNELS
;
963 &gDMA
.controller
[controllerIdx
].
968 DMA_CHANNEL_FLAG_IS_DEDICATED
) ==
972 flags
& DMA_CHANNEL_FLAG_IN_USE
)
976 DMA_CHANNEL_FLAG_LARGE_FIFO
)
981 DMA_DEVICE_FLAG_ALLOW_LARGE_FIFO
)
983 /* This channel is a large fifo - don't tie it up */
984 /* with devices that we don't want using it. */
990 DMA_CHANNEL_FLAG_IN_USE
;
991 channel
->devType
= dev
;
993 DMA_DEVICE_FLAG_IN_USE
;
995 #if (DMA_DEBUG_TRACK_RESERVATION)
996 channel
->fileName
= fileName
;
997 channel
->lineNum
= lineNum
;
1000 MAKE_HANDLE(controllerIdx
,
1003 /* Now that we've reserved the channel - we can go ahead and configure it */
1005 if (ConfigChannel(handle
) != 0) {
1008 "dma_request_channel: ConfigChannel failed\n");
1016 /* No channels are currently available. Let's wait for one to free up. */
1021 prepare_to_wait(&gDMA
.freeChannelQ
, &wait
,
1022 TASK_INTERRUPTIBLE
);
1025 finish_wait(&gDMA
.freeChannelQ
, &wait
);
1027 if (signal_pending(current
)) {
1028 /* We don't currently hold gDMA.lock, so we return directly */
1030 return -ERESTARTSYS
;
1034 if (down_interruptible(&gDMA
.lock
)) {
1035 return -ERESTARTSYS
;
1045 /* Create both _dbg and non _dbg functions for modules. */
1047 #if (DMA_DEBUG_TRACK_RESERVATION)
1048 #undef dma_request_channel
1049 DMA_Handle_t
dma_request_channel(DMA_Device_t dev
)
1051 return dma_request_channel_dbg(dev
, __FILE__
, __LINE__
);
1054 EXPORT_SYMBOL(dma_request_channel_dbg
);
1056 EXPORT_SYMBOL(dma_request_channel
);
1058 /****************************************************************************/
1060 * Frees a previously allocated DMA Handle.
1062 /****************************************************************************/
1064 int dma_free_channel(DMA_Handle_t handle
/* DMA handle. */
1067 DMA_Channel_t
*channel
;
1068 DMA_DeviceAttribute_t
*devAttr
;
1070 if (down_interruptible(&gDMA
.lock
) < 0) {
1071 return -ERESTARTSYS
;
1074 channel
= HandleToChannel(handle
);
1075 if (channel
== NULL
) {
1080 devAttr
= &DMA_gDeviceAttribute
[channel
->devType
];
1082 if ((channel
->flags
& DMA_CHANNEL_FLAG_IS_DEDICATED
) == 0) {
1083 channel
->lastDevType
= channel
->devType
;
1084 channel
->devType
= DMA_DEVICE_NONE
;
1086 channel
->flags
&= ~DMA_CHANNEL_FLAG_IN_USE
;
1087 devAttr
->flags
&= ~DMA_DEVICE_FLAG_IN_USE
;
1092 wake_up_interruptible(&gDMA
.freeChannelQ
);
1097 EXPORT_SYMBOL(dma_free_channel
);
1099 /****************************************************************************/
1101 * Determines if a given device has been configured as using a shared
1105 * 0 Device uses a dedicated channel
1106 * > zero Device uses a shared channel
1109 /****************************************************************************/
1111 int dma_device_is_channel_shared(DMA_Device_t device
/* Device to check. */
1113 DMA_DeviceAttribute_t
*devAttr
;
1115 if (!IsDeviceValid(device
)) {
1118 devAttr
= &DMA_gDeviceAttribute
[device
];
1120 return ((devAttr
->flags
& DMA_DEVICE_FLAG_IS_DEDICATED
) == 0);
1123 EXPORT_SYMBOL(dma_device_is_channel_shared
);
1125 /****************************************************************************/
1127 * Allocates buffers for the descriptors. This is normally done automatically
1128 * but needs to be done explicitly when initiating a dma from interrupt
1132 * 0 Descriptors were allocated successfully
1133 * -EINVAL Invalid device type for this kind of transfer
1134 * (i.e. the device is _MEM_TO_DEV and not _DEV_TO_MEM)
1135 * -ENOMEM Memory exhausted
1137 /****************************************************************************/
1139 int dma_alloc_descriptors(DMA_Handle_t handle
, /* DMA Handle */
1140 dmacHw_TRANSFER_TYPE_e transferType
, /* Type of transfer being performed */
1141 dma_addr_t srcData
, /* Place to get data to write to device */
1142 dma_addr_t dstData
, /* Pointer to device data address */
1143 size_t numBytes
/* Number of bytes to transfer to the device */
1145 DMA_Channel_t
*channel
;
1146 DMA_DeviceAttribute_t
*devAttr
;
1148 size_t ringBytesRequired
;
1151 channel
= HandleToChannel(handle
);
1152 if (channel
== NULL
) {
1156 devAttr
= &DMA_gDeviceAttribute
[channel
->devType
];
1158 if (devAttr
->config
.transferType
!= transferType
) {
1162 /* Figure out how many descriptors we need. */
1164 /* printk("srcData: 0x%08x dstData: 0x%08x, numBytes: %d\n", */
1165 /* srcData, dstData, numBytes); */
1167 numDescriptors
= dmacHw_calculateDescriptorCount(&devAttr
->config
,
1171 if (numDescriptors
< 0) {
1172 printk(KERN_ERR
"%s: dmacHw_calculateDescriptorCount failed\n",
1177 /* Check to see if we can reuse the existing descriptor ring, or if we need to allocate */
1180 ringBytesRequired
= dmacHw_descriptorLen(numDescriptors
);
1182 /* printk("ringBytesRequired: %d\n", ringBytesRequired); */
1184 if (ringBytesRequired
> devAttr
->ring
.bytesAllocated
) {
1185 /* Make sure that this code path is never taken from interrupt context. */
1186 /* It's OK for an interrupt to initiate a DMA transfer, but the descriptor */
1187 /* allocation needs to have already been done. */
1191 /* Free the old descriptor ring and allocate a new one. */
1193 dma_free_descriptor_ring(&devAttr
->ring
);
1195 /* And allocate a new one. */
1198 dma_alloc_descriptor_ring(&devAttr
->ring
,
1202 "%s: dma_alloc_descriptor_ring(%d) failed\n",
1203 __func__
, numDescriptors
);
1206 /* Setup the descriptor for this transfer */
1208 if (dmacHw_initDescriptor(devAttr
->ring
.virtAddr
,
1209 devAttr
->ring
.physAddr
,
1210 devAttr
->ring
.bytesAllocated
,
1211 numDescriptors
) < 0) {
1212 printk(KERN_ERR
"%s: dmacHw_initDescriptor failed\n",
1217 /* We've already got enough ring buffer allocated. All we need to do is reset */
1218 /* any control information, just in case the previous DMA was stopped. */
1220 dmacHw_resetDescriptorControl(devAttr
->ring
.virtAddr
);
1223 /* dma_alloc/free both set the prevSrc/DstData to 0. If they happen to be the same */
1224 /* as last time, then we don't need to call setDataDescriptor again. */
1226 if (dmacHw_setDataDescriptor(&devAttr
->config
,
1227 devAttr
->ring
.virtAddr
,
1229 (void *)dstData
, numBytes
) < 0) {
1230 printk(KERN_ERR
"%s: dmacHw_setDataDescriptor failed\n",
1235 /* Remember the critical information for this transfer so that we can eliminate */
1236 /* another call to dma_alloc_descriptors if the caller reuses the same buffers */
1238 devAttr
->prevSrcData
= srcData
;
1239 devAttr
->prevDstData
= dstData
;
1240 devAttr
->prevNumBytes
= numBytes
;
1245 EXPORT_SYMBOL(dma_alloc_descriptors
);
1247 /****************************************************************************/
1249 * Allocates and sets up descriptors for a double buffered circular buffer.
1251 * This is primarily intended to be used for things like the ingress samples
1252 * from a microphone.
1255 * > 0 Number of descriptors actually allocated.
1256 * -EINVAL Invalid device type for this kind of transfer
1257 * (i.e. the device is _MEM_TO_DEV and not _DEV_TO_MEM)
1258 * -ENOMEM Memory exhausted
1260 /****************************************************************************/
1262 int dma_alloc_double_dst_descriptors(DMA_Handle_t handle
, /* DMA Handle */
1263 dma_addr_t srcData
, /* Physical address of source data */
1264 dma_addr_t dstData1
, /* Physical address of first destination buffer */
1265 dma_addr_t dstData2
, /* Physical address of second destination buffer */
1266 size_t numBytes
/* Number of bytes in each destination buffer */
1268 DMA_Channel_t
*channel
;
1269 DMA_DeviceAttribute_t
*devAttr
;
1270 int numDst1Descriptors
;
1271 int numDst2Descriptors
;
1273 size_t ringBytesRequired
;
1276 channel
= HandleToChannel(handle
);
1277 if (channel
== NULL
) {
1281 devAttr
= &DMA_gDeviceAttribute
[channel
->devType
];
1283 /* Figure out how many descriptors we need. */
1285 /* printk("srcData: 0x%08x dstData: 0x%08x, numBytes: %d\n", */
1286 /* srcData, dstData, numBytes); */
1288 numDst1Descriptors
=
1289 dmacHw_calculateDescriptorCount(&devAttr
->config
, (void *)srcData
,
1290 (void *)dstData1
, numBytes
);
1291 if (numDst1Descriptors
< 0) {
1294 numDst2Descriptors
=
1295 dmacHw_calculateDescriptorCount(&devAttr
->config
, (void *)srcData
,
1296 (void *)dstData2
, numBytes
);
1297 if (numDst2Descriptors
< 0) {
1300 numDescriptors
= numDst1Descriptors
+ numDst2Descriptors
;
1301 /* printk("numDescriptors: %d\n", numDescriptors); */
1303 /* Check to see if we can reuse the existing descriptor ring, or if we need to allocate */
1306 ringBytesRequired
= dmacHw_descriptorLen(numDescriptors
);
1308 /* printk("ringBytesRequired: %d\n", ringBytesRequired); */
1310 if (ringBytesRequired
> devAttr
->ring
.bytesAllocated
) {
1311 /* Make sure that this code path is never taken from interrupt context. */
1312 /* It's OK for an interrupt to initiate a DMA transfer, but the descriptor */
1313 /* allocation needs to have already been done. */
1317 /* Free the old descriptor ring and allocate a new one. */
1319 dma_free_descriptor_ring(&devAttr
->ring
);
1321 /* And allocate a new one. */
1324 dma_alloc_descriptor_ring(&devAttr
->ring
,
1328 "%s: dma_alloc_descriptor_ring(%d) failed\n",
1329 __func__
, ringBytesRequired
);
1334 /* Setup the descriptor for this transfer. Since this function is used with */
1335 /* CONTINUOUS DMA operations, we need to reinitialize every time, otherwise */
1336 /* setDataDescriptor will keep trying to append onto the end. */
1338 if (dmacHw_initDescriptor(devAttr
->ring
.virtAddr
,
1339 devAttr
->ring
.physAddr
,
1340 devAttr
->ring
.bytesAllocated
,
1341 numDescriptors
) < 0) {
1342 printk(KERN_ERR
"%s: dmacHw_initDescriptor failed\n", __func__
);
1346 /* dma_alloc/free both set the prevSrc/DstData to 0. If they happen to be the same */
1347 /* as last time, then we don't need to call setDataDescriptor again. */
1349 if (dmacHw_setDataDescriptor(&devAttr
->config
,
1350 devAttr
->ring
.virtAddr
,
1352 (void *)dstData1
, numBytes
) < 0) {
1353 printk(KERN_ERR
"%s: dmacHw_setDataDescriptor 1 failed\n",
1357 if (dmacHw_setDataDescriptor(&devAttr
->config
,
1358 devAttr
->ring
.virtAddr
,
1360 (void *)dstData2
, numBytes
) < 0) {
1361 printk(KERN_ERR
"%s: dmacHw_setDataDescriptor 2 failed\n",
1366 /* You should use dma_start_transfer rather than dma_transfer_xxx so we don't */
1367 /* try to make the 'prev' variables right. */
1369 devAttr
->prevSrcData
= 0;
1370 devAttr
->prevDstData
= 0;
1371 devAttr
->prevNumBytes
= 0;
1373 return numDescriptors
;
1376 EXPORT_SYMBOL(dma_alloc_double_dst_descriptors
);
1378 /****************************************************************************/
1380 * Initiates a transfer when the descriptors have already been setup.
1382 * This is a special case, and normally, the dma_transfer_xxx functions should
1386 * 0 Transfer was started successfully
1387 * -ENODEV Invalid handle
1389 /****************************************************************************/
1391 int dma_start_transfer(DMA_Handle_t handle
)
1393 DMA_Channel_t
*channel
;
1394 DMA_DeviceAttribute_t
*devAttr
;
1396 channel
= HandleToChannel(handle
);
1397 if (channel
== NULL
) {
1400 devAttr
= &DMA_gDeviceAttribute
[channel
->devType
];
1402 dmacHw_initiateTransfer(channel
->dmacHwHandle
, &devAttr
->config
,
1403 devAttr
->ring
.virtAddr
);
1405 /* Since we got this far, everything went successfully */
1410 EXPORT_SYMBOL(dma_start_transfer
);
1412 /****************************************************************************/
1414 * Stops a previously started DMA transfer.
1417 * 0 Transfer was stopped successfully
1418 * -ENODEV Invalid handle
1420 /****************************************************************************/
1422 int dma_stop_transfer(DMA_Handle_t handle
)
1424 DMA_Channel_t
*channel
;
1426 channel
= HandleToChannel(handle
);
1427 if (channel
== NULL
) {
1431 dmacHw_stopTransfer(channel
->dmacHwHandle
);
1436 EXPORT_SYMBOL(dma_stop_transfer
);
1438 /****************************************************************************/
1440 * Waits for a DMA to complete by polling. This function is only intended
1441 * to be used for testing. Interrupts should be used for most DMA operations.
1443 /****************************************************************************/
1445 int dma_wait_transfer_done(DMA_Handle_t handle
)
1447 DMA_Channel_t
*channel
;
1448 dmacHw_TRANSFER_STATUS_e status
;
1450 channel
= HandleToChannel(handle
);
1451 if (channel
== NULL
) {
1456 dmacHw_transferCompleted(channel
->dmacHwHandle
)) ==
1457 dmacHw_TRANSFER_STATUS_BUSY
) {
1461 if (status
== dmacHw_TRANSFER_STATUS_ERROR
) {
1462 printk(KERN_ERR
"%s: DMA transfer failed\n", __func__
);
1468 EXPORT_SYMBOL(dma_wait_transfer_done
);
1470 /****************************************************************************/
1472 * Initiates a DMA, allocating the descriptors as required.
1475 * 0 Transfer was started successfully
1476 * -EINVAL Invalid device type for this kind of transfer
1477 * (i.e. the device is _DEV_TO_MEM and not _MEM_TO_DEV)
1479 /****************************************************************************/
1481 int dma_transfer(DMA_Handle_t handle
, /* DMA Handle */
1482 dmacHw_TRANSFER_TYPE_e transferType
, /* Type of transfer being performed */
1483 dma_addr_t srcData
, /* Place to get data to write to device */
1484 dma_addr_t dstData
, /* Pointer to device data address */
1485 size_t numBytes
/* Number of bytes to transfer to the device */
1487 DMA_Channel_t
*channel
;
1488 DMA_DeviceAttribute_t
*devAttr
;
1491 channel
= HandleToChannel(handle
);
1492 if (channel
== NULL
) {
1496 devAttr
= &DMA_gDeviceAttribute
[channel
->devType
];
1498 if (devAttr
->config
.transferType
!= transferType
) {
1502 /* We keep track of the information about the previous request for this */
1503 /* device, and if the attributes match, then we can use the descriptors we setup */
1504 /* the last time, and not have to reinitialize everything. */
1508 dma_alloc_descriptors(handle
, transferType
, srcData
,
1515 /* And kick off the transfer */
1517 devAttr
->numBytes
= numBytes
;
1518 devAttr
->transferStartTime
= timer_get_tick_count();
1520 dmacHw_initiateTransfer(channel
->dmacHwHandle
, &devAttr
->config
,
1521 devAttr
->ring
.virtAddr
);
1523 /* Since we got this far, everything went successfully */
1528 EXPORT_SYMBOL(dma_transfer
);
1530 /****************************************************************************/
1532 * Set the callback function which will be called when a transfer completes.
1533 * If a NULL callback function is set, then no callback will occur.
1535 * @note @a devHandler will be called from IRQ context.
1539 * -ENODEV - Device handed in is invalid.
1541 /****************************************************************************/
1543 int dma_set_device_handler(DMA_Device_t dev
, /* Device to set the callback for. */
1544 DMA_DeviceHandler_t devHandler
, /* Function to call when the DMA completes */
1545 void *userData
/* Pointer which will be passed to devHandler. */
1547 DMA_DeviceAttribute_t
*devAttr
;
1548 unsigned long flags
;
1550 if (!IsDeviceValid(dev
)) {
1553 devAttr
= &DMA_gDeviceAttribute
[dev
];
1555 local_irq_save(flags
);
1557 devAttr
->userData
= userData
;
1558 devAttr
->devHandler
= devHandler
;
1560 local_irq_restore(flags
);
1565 EXPORT_SYMBOL(dma_set_device_handler
);
1567 /****************************************************************************/
1569 * Initializes a memory mapping structure
1571 /****************************************************************************/
1573 int dma_init_mem_map(DMA_MemMap_t
*memMap
)
1575 memset(memMap
, 0, sizeof(*memMap
));
1577 sema_init(&memMap
->lock
, 1);
1582 EXPORT_SYMBOL(dma_init_mem_map
);
1584 /****************************************************************************/
1586 * Releases any memory currently being held by a memory mapping structure.
1588 /****************************************************************************/
1590 int dma_term_mem_map(DMA_MemMap_t
*memMap
)
1592 down(&memMap
->lock
); /* Just being paranoid */
1594 /* Free up any allocated memory */
1597 memset(memMap
, 0, sizeof(*memMap
));
1602 EXPORT_SYMBOL(dma_term_mem_map
);
1604 /****************************************************************************/
1606 * Looks at a memory address and categorizes it.
1608 * @return One of the values from the DMA_MemType_t enumeration.
1610 /****************************************************************************/
1612 DMA_MemType_t
dma_mem_type(void *addr
)
1614 unsigned long addrVal
= (unsigned long)addr
;
1616 if (addrVal
>= VMALLOC_END
) {
1617 /* NOTE: DMA virtual memory space starts at 0xFFxxxxxx */
1619 /* dma_alloc_xxx pages are physically and virtually contiguous */
1621 return DMA_MEM_TYPE_DMA
;
1624 /* Technically, we could add one more classification. Addresses between VMALLOC_END */
1625 /* and the beginning of the DMA virtual address could be considered to be I/O space. */
1626 /* Right now, nobody cares about this particular classification, so we ignore it. */
1628 if (is_vmalloc_addr(addr
)) {
1629 /* Address comes from the vmalloc'd region. Pages are virtually */
1630 /* contiguous but NOT physically contiguous */
1632 return DMA_MEM_TYPE_VMALLOC
;
1635 if (addrVal
>= PAGE_OFFSET
) {
1636 /* PAGE_OFFSET is typically 0xC0000000 */
1638 /* kmalloc'd pages are physically contiguous */
1640 return DMA_MEM_TYPE_KMALLOC
;
1643 return DMA_MEM_TYPE_USER
;
1646 EXPORT_SYMBOL(dma_mem_type
);
1648 /****************************************************************************/
1650 * Looks at a memory address and determines if we support DMA'ing to/from
1651 * that type of memory.
1654 * return value != 0 means dma supported
1655 * return value == 0 means dma not supported
1657 /****************************************************************************/
1659 int dma_mem_supports_dma(void *addr
)
1661 DMA_MemType_t memType
= dma_mem_type(addr
);
1663 return (memType
== DMA_MEM_TYPE_DMA
)
1664 #if ALLOW_MAP_OF_KMALLOC_MEMORY
1665 || (memType
== DMA_MEM_TYPE_KMALLOC
)
1667 || (memType
== DMA_MEM_TYPE_USER
);
1670 EXPORT_SYMBOL(dma_mem_supports_dma
);
1672 /****************************************************************************/
1674 * Maps in a memory region such that it can be used for performing a DMA.
1678 /****************************************************************************/
1680 int dma_map_start(DMA_MemMap_t
*memMap
, /* Stores state information about the map */
1681 enum dma_data_direction dir
/* Direction that the mapping will be going */
1685 down(&memMap
->lock
);
1687 DMA_MAP_PRINT("memMap: %p\n", memMap
);
1689 if (memMap
->inUse
) {
1690 printk(KERN_ERR
"%s: memory map %p is already being used\n",
1698 memMap
->numRegionsUsed
= 0;
1704 DMA_MAP_PRINT("returning %d", rc
);
1711 EXPORT_SYMBOL(dma_map_start
);
1713 /****************************************************************************/
1715 * Adds a segment of memory to a memory map. Each segment is both
1716 * physically and virtually contiguous.
1718 * @return 0 on success, error code otherwise.
1720 /****************************************************************************/
1722 static int dma_map_add_segment(DMA_MemMap_t
*memMap
, /* Stores state information about the map */
1723 DMA_Region_t
*region
, /* Region that the segment belongs to */
1724 void *virtAddr
, /* Virtual address of the segment being added */
1725 dma_addr_t physAddr
, /* Physical address of the segment being added */
1726 size_t numBytes
/* Number of bytes of the segment being added */
1728 DMA_Segment_t
*segment
;
1730 DMA_MAP_PRINT("memMap:%p va:%p pa:0x%x #:%d\n", memMap
, virtAddr
,
1731 physAddr
, numBytes
);
1735 if (((unsigned long)virtAddr
< (unsigned long)region
->virtAddr
)
1736 || (((unsigned long)virtAddr
+ numBytes
)) >
1737 ((unsigned long)region
->virtAddr
+ region
->numBytes
)) {
1739 "%s: virtAddr %p is outside region @ %p len: %d\n",
1740 __func__
, virtAddr
, region
->virtAddr
, region
->numBytes
);
1744 if (region
->numSegmentsUsed
> 0) {
1745 /* Check to see if this segment is physically contiguous with the previous one */
1747 segment
= ®ion
->segment
[region
->numSegmentsUsed
- 1];
1749 if ((segment
->physAddr
+ segment
->numBytes
) == physAddr
) {
1750 /* It is - just add on to the end */
1752 DMA_MAP_PRINT("appending %d bytes to last segment\n",
1755 segment
->numBytes
+= numBytes
;
1761 /* Reallocate to hold more segments, if required. */
1763 if (region
->numSegmentsUsed
>= region
->numSegmentsAllocated
) {
1764 DMA_Segment_t
*newSegment
;
1766 region
->numSegmentsAllocated
* sizeof(*newSegment
);
1767 int newAlloc
= region
->numSegmentsAllocated
+ 4;
1768 size_t newSize
= newAlloc
* sizeof(*newSegment
);
1770 newSegment
= kmalloc(newSize
, GFP_KERNEL
);
1771 if (newSegment
== NULL
) {
1774 memcpy(newSegment
, region
->segment
, oldSize
);
1775 memset(&((uint8_t *) newSegment
)[oldSize
], 0,
1777 kfree(region
->segment
);
1779 region
->numSegmentsAllocated
= newAlloc
;
1780 region
->segment
= newSegment
;
1783 segment
= ®ion
->segment
[region
->numSegmentsUsed
];
1784 region
->numSegmentsUsed
++;
1786 segment
->virtAddr
= virtAddr
;
1787 segment
->physAddr
= physAddr
;
1788 segment
->numBytes
= numBytes
;
1790 DMA_MAP_PRINT("returning success\n");
1795 /****************************************************************************/
1797 * Adds a region of memory to a memory map. Each region is virtually
1798 * contiguous, but not necessarily physically contiguous.
1800 * @return 0 on success, error code otherwise.
1802 /****************************************************************************/
1804 int dma_map_add_region(DMA_MemMap_t
*memMap
, /* Stores state information about the map */
1805 void *mem
, /* Virtual address that we want to get a map of */
1806 size_t numBytes
/* Number of bytes being mapped */
1808 unsigned long addr
= (unsigned long)mem
;
1809 unsigned int offset
;
1811 DMA_Region_t
*region
;
1812 dma_addr_t physAddr
;
1814 down(&memMap
->lock
);
1816 DMA_MAP_PRINT("memMap:%p va:%p #:%d\n", memMap
, mem
, numBytes
);
1818 if (!memMap
->inUse
) {
1819 printk(KERN_ERR
"%s: Make sure you call dma_map_start first\n",
1825 /* Reallocate to hold more regions. */
1827 if (memMap
->numRegionsUsed
>= memMap
->numRegionsAllocated
) {
1828 DMA_Region_t
*newRegion
;
1830 memMap
->numRegionsAllocated
* sizeof(*newRegion
);
1831 int newAlloc
= memMap
->numRegionsAllocated
+ 4;
1832 size_t newSize
= newAlloc
* sizeof(*newRegion
);
1834 newRegion
= kmalloc(newSize
, GFP_KERNEL
);
1835 if (newRegion
== NULL
) {
1839 memcpy(newRegion
, memMap
->region
, oldSize
);
1840 memset(&((uint8_t *) newRegion
)[oldSize
], 0, newSize
- oldSize
);
1842 kfree(memMap
->region
);
1844 memMap
->numRegionsAllocated
= newAlloc
;
1845 memMap
->region
= newRegion
;
1848 region
= &memMap
->region
[memMap
->numRegionsUsed
];
1849 memMap
->numRegionsUsed
++;
1851 offset
= addr
& ~PAGE_MASK
;
1853 region
->memType
= dma_mem_type(mem
);
1854 region
->virtAddr
= mem
;
1855 region
->numBytes
= numBytes
;
1856 region
->numSegmentsUsed
= 0;
1857 region
->numLockedPages
= 0;
1858 region
->lockedPages
= NULL
;
1860 switch (region
->memType
) {
1861 case DMA_MEM_TYPE_VMALLOC
:
1863 atomic_inc(&gDmaStatMemTypeVmalloc
);
1865 /* printk(KERN_ERR "%s: vmalloc'd pages are not supported\n", __func__); */
1867 /* vmalloc'd pages are not physically contiguous */
1873 case DMA_MEM_TYPE_KMALLOC
:
1875 atomic_inc(&gDmaStatMemTypeKmalloc
);
1877 /* kmalloc'd pages are physically contiguous, so they'll have exactly */
1880 #if ALLOW_MAP_OF_KMALLOC_MEMORY
1882 dma_map_single(NULL
, mem
, numBytes
, memMap
->dir
);
1883 rc
= dma_map_add_segment(memMap
, region
, mem
, physAddr
,
1891 case DMA_MEM_TYPE_DMA
:
1893 /* dma_alloc_xxx pages are physically contiguous */
1895 atomic_inc(&gDmaStatMemTypeCoherent
);
1897 physAddr
= (vmalloc_to_pfn(mem
) << PAGE_SHIFT
) + offset
;
1899 dma_sync_single_for_cpu(NULL
, physAddr
, numBytes
,
1901 rc
= dma_map_add_segment(memMap
, region
, mem
, physAddr
,
1906 case DMA_MEM_TYPE_USER
:
1908 size_t firstPageOffset
;
1909 size_t firstPageSize
;
1910 struct page
**pages
;
1911 struct task_struct
*userTask
;
1913 atomic_inc(&gDmaStatMemTypeUser
);
1916 /* If the pages are user pages, then the dma_mem_map_set_user_task function */
1917 /* must have been previously called. */
1919 if (memMap
->userTask
== NULL
) {
1921 "%s: must call dma_mem_map_set_user_task when using user-mode memory\n",
1926 /* User pages need to be locked. */
1929 (unsigned long)region
->virtAddr
& (PAGE_SIZE
- 1);
1930 firstPageSize
= PAGE_SIZE
- firstPageOffset
;
1932 region
->numLockedPages
= (firstPageOffset
1933 + region
->numBytes
+
1934 PAGE_SIZE
- 1) / PAGE_SIZE
;
1936 kmalloc(region
->numLockedPages
*
1937 sizeof(struct page
*), GFP_KERNEL
);
1939 if (pages
== NULL
) {
1940 region
->numLockedPages
= 0;
1944 userTask
= memMap
->userTask
;
1946 down_read(&userTask
->mm
->mmap_sem
);
1947 rc
= get_user_pages(userTask
, /* task */
1948 userTask
->mm
, /* mm */
1949 (unsigned long)region
->virtAddr
, /* start */
1950 region
->numLockedPages
, /* len */
1951 memMap
->dir
== DMA_FROM_DEVICE
, /* write */
1953 pages
, /* pages (array of pointers to page) */
1955 up_read(&userTask
->mm
->mmap_sem
);
1957 if (rc
!= region
->numLockedPages
) {
1959 region
->numLockedPages
= 0;
1965 uint8_t *virtAddr
= region
->virtAddr
;
1966 size_t bytesRemaining
;
1969 rc
= 0; /* Since get_user_pages returns +ve number */
1971 region
->lockedPages
= pages
;
1973 /* We've locked the user pages. Now we need to walk them and figure */
1974 /* out the physical addresses. */
1976 /* The first page may be partial */
1978 dma_map_add_segment(memMap
,
1981 PFN_PHYS(page_to_pfn
1986 virtAddr
+= firstPageSize
;
1988 region
->numBytes
- firstPageSize
;
1991 pageIdx
< region
->numLockedPages
;
1993 size_t bytesThisPage
=
1995 PAGE_SIZE
? PAGE_SIZE
:
1999 ("pageIdx:%d pages[pageIdx]=%p pfn=%u phys=%u\n",
2000 pageIdx
, pages
[pageIdx
],
2001 page_to_pfn(pages
[pageIdx
]),
2002 PFN_PHYS(page_to_pfn
2005 dma_map_add_segment(memMap
,
2008 PFN_PHYS(page_to_pfn
2013 virtAddr
+= bytesThisPage
;
2014 bytesRemaining
-= bytesThisPage
;
2019 "%s: User mode pages are not yet supported\n",
2022 /* user pages are not physically contiguous */
2031 printk(KERN_ERR
"%s: Unsupported memory type: %d\n",
2032 __func__
, region
->memType
);
2040 memMap
->numRegionsUsed
--;
2045 DMA_MAP_PRINT("returning %d\n", rc
);
2052 EXPORT_SYMBOL(dma_map_add_segment
);
2054 /****************************************************************************/
2056 * Maps in a memory region such that it can be used for performing a DMA.
2058 * @return 0 on success, error code otherwise.
2060 /****************************************************************************/
2062 int dma_map_mem(DMA_MemMap_t
*memMap
, /* Stores state information about the map */
2063 void *mem
, /* Virtual address that we want to get a map of */
2064 size_t numBytes
, /* Number of bytes being mapped */
2065 enum dma_data_direction dir
/* Direction that the mapping will be going */
2069 rc
= dma_map_start(memMap
, dir
);
2071 rc
= dma_map_add_region(memMap
, mem
, numBytes
);
2073 /* Since the add fails, this function will fail, and the caller won't */
2074 /* call unmap, so we need to do it here. */
2076 dma_unmap(memMap
, 0);
2083 EXPORT_SYMBOL(dma_map_mem
);
2085 /****************************************************************************/
2087 * Setup a descriptor ring for a given memory map.
2089 * It is assumed that the descriptor ring has already been initialized, and
2090 * this routine will only reallocate a new descriptor ring if the existing
2093 * @return 0 on success, error code otherwise.
2095 /****************************************************************************/
2097 int dma_map_create_descriptor_ring(DMA_Device_t dev
, /* DMA device (where the ring is stored) */
2098 DMA_MemMap_t
*memMap
, /* Memory map that will be used */
2099 dma_addr_t devPhysAddr
/* Physical address of device */
2103 DMA_DeviceAttribute_t
*devAttr
;
2104 DMA_Region_t
*region
;
2105 DMA_Segment_t
*segment
;
2106 dma_addr_t srcPhysAddr
;
2107 dma_addr_t dstPhysAddr
;
2111 devAttr
= &DMA_gDeviceAttribute
[dev
];
2113 down(&memMap
->lock
);
2115 /* Figure out how many descriptors we need */
2118 for (regionIdx
= 0; regionIdx
< memMap
->numRegionsUsed
; regionIdx
++) {
2119 region
= &memMap
->region
[regionIdx
];
2121 for (segmentIdx
= 0; segmentIdx
< region
->numSegmentsUsed
;
2123 segment
= ®ion
->segment
[segmentIdx
];
2125 if (memMap
->dir
== DMA_TO_DEVICE
) {
2126 srcPhysAddr
= segment
->physAddr
;
2127 dstPhysAddr
= devPhysAddr
;
2129 srcPhysAddr
= devPhysAddr
;
2130 dstPhysAddr
= segment
->physAddr
;
2134 dma_calculate_descriptor_count(dev
, srcPhysAddr
,
2140 "%s: dma_calculate_descriptor_count failed: %d\n",
2144 numDescriptors
+= rc
;
2148 /* Adjust the size of the ring, if it isn't big enough */
2150 if (numDescriptors
> devAttr
->ring
.descriptorsAllocated
) {
2151 dma_free_descriptor_ring(&devAttr
->ring
);
2153 dma_alloc_descriptor_ring(&devAttr
->ring
,
2157 "%s: dma_alloc_descriptor_ring failed: %d\n",
2163 dma_init_descriptor_ring(&devAttr
->ring
,
2167 "%s: dma_init_descriptor_ring failed: %d\n",
2173 /* Populate the descriptors */
2175 for (regionIdx
= 0; regionIdx
< memMap
->numRegionsUsed
; regionIdx
++) {
2176 region
= &memMap
->region
[regionIdx
];
2178 for (segmentIdx
= 0; segmentIdx
< region
->numSegmentsUsed
;
2180 segment
= ®ion
->segment
[segmentIdx
];
2182 if (memMap
->dir
== DMA_TO_DEVICE
) {
2183 srcPhysAddr
= segment
->physAddr
;
2184 dstPhysAddr
= devPhysAddr
;
2186 srcPhysAddr
= devPhysAddr
;
2187 dstPhysAddr
= segment
->physAddr
;
2191 dma_add_descriptors(&devAttr
->ring
, dev
,
2192 srcPhysAddr
, dstPhysAddr
,
2196 "%s: dma_add_descriptors failed: %d\n",
2211 EXPORT_SYMBOL(dma_map_create_descriptor_ring
);
2213 /****************************************************************************/
2215 * Maps in a memory region such that it can be used for performing a DMA.
2219 /****************************************************************************/
2221 int dma_unmap(DMA_MemMap_t
*memMap
, /* Stores state information about the map */
2222 int dirtied
/* non-zero if any of the pages were modified */
2228 DMA_Region_t
*region
;
2229 DMA_Segment_t
*segment
;
2231 down(&memMap
->lock
);
2233 for (regionIdx
= 0; regionIdx
< memMap
->numRegionsUsed
; regionIdx
++) {
2234 region
= &memMap
->region
[regionIdx
];
2236 for (segmentIdx
= 0; segmentIdx
< region
->numSegmentsUsed
;
2238 segment
= ®ion
->segment
[segmentIdx
];
2240 switch (region
->memType
) {
2241 case DMA_MEM_TYPE_VMALLOC
:
2244 "%s: vmalloc'd pages are not yet supported\n",
2250 case DMA_MEM_TYPE_KMALLOC
:
2252 #if ALLOW_MAP_OF_KMALLOC_MEMORY
2253 dma_unmap_single(NULL
,
2261 case DMA_MEM_TYPE_DMA
:
2263 dma_sync_single_for_cpu(NULL
,
2272 case DMA_MEM_TYPE_USER
:
2274 /* Nothing to do here. */
2282 "%s: Unsupported memory type: %d\n",
2283 __func__
, region
->memType
);
2289 segment
->virtAddr
= NULL
;
2290 segment
->physAddr
= 0;
2291 segment
->numBytes
= 0;
2294 if (region
->numLockedPages
> 0) {
2297 /* Some user pages were locked. We need to go and unlock them now. */
2299 for (pageIdx
= 0; pageIdx
< region
->numLockedPages
;
2302 region
->lockedPages
[pageIdx
];
2304 if (memMap
->dir
== DMA_FROM_DEVICE
) {
2307 page_cache_release(page
);
2309 kfree(region
->lockedPages
);
2310 region
->numLockedPages
= 0;
2311 region
->lockedPages
= NULL
;
2314 region
->memType
= DMA_MEM_TYPE_NONE
;
2315 region
->virtAddr
= NULL
;
2316 region
->numBytes
= 0;
2317 region
->numSegmentsUsed
= 0;
2319 memMap
->userTask
= NULL
;
2320 memMap
->numRegionsUsed
= 0;
2329 EXPORT_SYMBOL(dma_unmap
);