arm: fix implicit use of sched.h in bcmring/dma.c
[linux-2.6/libata-dev.git] / arch / arm / mach-bcmring / dma.c
blobb52b8de91bde72c2a4847c1a37a44f425974612d
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
12 * consent.
13 *****************************************************************************/
15 /****************************************************************************/
16 /**
17 * @file dma.c
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/sched.h>
30 #include <linux/irqreturn.h>
31 #include <linux/proc_fs.h>
32 #include <linux/slab.h>
34 #include <mach/timer.h>
36 #include <linux/mm.h>
37 #include <linux/pfn.h>
38 #include <linux/atomic.h>
39 #include <mach/dma.h>
41 /* I don't quite understand why dc4 fails when this is set to 1 and DMA is enabled */
42 /* especially since dc4 doesn't use kmalloc'd memory. */
44 #define ALLOW_MAP_OF_KMALLOC_MEMORY 0
46 /* ---- Public Variables ------------------------------------------------- */
48 /* ---- Private Constants and Types -------------------------------------- */
50 #define MAKE_HANDLE(controllerIdx, channelIdx) (((controllerIdx) << 4) | (channelIdx))
52 #define CONTROLLER_FROM_HANDLE(handle) (((handle) >> 4) & 0x0f)
53 #define CHANNEL_FROM_HANDLE(handle) ((handle) & 0x0f)
55 #define DMA_MAP_DEBUG 0
57 #if DMA_MAP_DEBUG
58 # define DMA_MAP_PRINT(fmt, args...) printk("%s: " fmt, __func__, ## args)
59 #else
60 # define DMA_MAP_PRINT(fmt, args...)
61 #endif
63 /* ---- Private Variables ------------------------------------------------ */
65 static DMA_Global_t gDMA;
66 static struct proc_dir_entry *gDmaDir;
68 static atomic_t gDmaStatMemTypeKmalloc = ATOMIC_INIT(0);
69 static atomic_t gDmaStatMemTypeVmalloc = ATOMIC_INIT(0);
70 static atomic_t gDmaStatMemTypeUser = ATOMIC_INIT(0);
71 static atomic_t gDmaStatMemTypeCoherent = ATOMIC_INIT(0);
73 #include "dma_device.c"
75 /* ---- Private Function Prototypes -------------------------------------- */
77 /* ---- Functions ------------------------------------------------------- */
79 /****************************************************************************/
80 /**
81 * Displays information for /proc/dma/mem-type
83 /****************************************************************************/
85 static int dma_proc_read_mem_type(char *buf, char **start, off_t offset,
86 int count, int *eof, void *data)
88 int len = 0;
90 len += sprintf(buf + len, "dma_map_mem statistics\n");
91 len +=
92 sprintf(buf + len, "coherent: %d\n",
93 atomic_read(&gDmaStatMemTypeCoherent));
94 len +=
95 sprintf(buf + len, "kmalloc: %d\n",
96 atomic_read(&gDmaStatMemTypeKmalloc));
97 len +=
98 sprintf(buf + len, "vmalloc: %d\n",
99 atomic_read(&gDmaStatMemTypeVmalloc));
100 len +=
101 sprintf(buf + len, "user: %d\n",
102 atomic_read(&gDmaStatMemTypeUser));
104 return len;
107 /****************************************************************************/
109 * Displays information for /proc/dma/channels
111 /****************************************************************************/
113 static int dma_proc_read_channels(char *buf, char **start, off_t offset,
114 int count, int *eof, void *data)
116 int controllerIdx;
117 int channelIdx;
118 int limit = count - 200;
119 int len = 0;
120 DMA_Channel_t *channel;
122 if (down_interruptible(&gDMA.lock) < 0) {
123 return -ERESTARTSYS;
126 for (controllerIdx = 0; controllerIdx < DMA_NUM_CONTROLLERS;
127 controllerIdx++) {
128 for (channelIdx = 0; channelIdx < DMA_NUM_CHANNELS;
129 channelIdx++) {
130 if (len >= limit) {
131 break;
134 channel =
135 &gDMA.controller[controllerIdx].channel[channelIdx];
137 len +=
138 sprintf(buf + len, "%d:%d ", controllerIdx,
139 channelIdx);
141 if ((channel->flags & DMA_CHANNEL_FLAG_IS_DEDICATED) !=
142 0) {
143 len +=
144 sprintf(buf + len, "Dedicated for %s ",
145 DMA_gDeviceAttribute[channel->
146 devType].name);
147 } else {
148 len += sprintf(buf + len, "Shared ");
151 if ((channel->flags & DMA_CHANNEL_FLAG_NO_ISR) != 0) {
152 len += sprintf(buf + len, "No ISR ");
155 if ((channel->flags & DMA_CHANNEL_FLAG_LARGE_FIFO) != 0) {
156 len += sprintf(buf + len, "Fifo: 128 ");
157 } else {
158 len += sprintf(buf + len, "Fifo: 64 ");
161 if ((channel->flags & DMA_CHANNEL_FLAG_IN_USE) != 0) {
162 len +=
163 sprintf(buf + len, "InUse by %s",
164 DMA_gDeviceAttribute[channel->
165 devType].name);
166 #if (DMA_DEBUG_TRACK_RESERVATION)
167 len +=
168 sprintf(buf + len, " (%s:%d)",
169 channel->fileName,
170 channel->lineNum);
171 #endif
172 } else {
173 len += sprintf(buf + len, "Avail ");
176 if (channel->lastDevType != DMA_DEVICE_NONE) {
177 len +=
178 sprintf(buf + len, "Last use: %s ",
179 DMA_gDeviceAttribute[channel->
180 lastDevType].
181 name);
184 len += sprintf(buf + len, "\n");
187 up(&gDMA.lock);
188 *eof = 1;
190 return len;
193 /****************************************************************************/
195 * Displays information for /proc/dma/devices
197 /****************************************************************************/
199 static int dma_proc_read_devices(char *buf, char **start, off_t offset,
200 int count, int *eof, void *data)
202 int limit = count - 200;
203 int len = 0;
204 int devIdx;
206 if (down_interruptible(&gDMA.lock) < 0) {
207 return -ERESTARTSYS;
210 for (devIdx = 0; devIdx < DMA_NUM_DEVICE_ENTRIES; devIdx++) {
211 DMA_DeviceAttribute_t *devAttr = &DMA_gDeviceAttribute[devIdx];
213 if (devAttr->name == NULL) {
214 continue;
217 if (len >= limit) {
218 break;
221 len += sprintf(buf + len, "%-12s ", devAttr->name);
223 if ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) != 0) {
224 len +=
225 sprintf(buf + len, "Dedicated %d:%d ",
226 devAttr->dedicatedController,
227 devAttr->dedicatedChannel);
228 } else {
229 len += sprintf(buf + len, "Shared DMA:");
230 if ((devAttr->flags & DMA_DEVICE_FLAG_ON_DMA0) != 0) {
231 len += sprintf(buf + len, "0");
233 if ((devAttr->flags & DMA_DEVICE_FLAG_ON_DMA1) != 0) {
234 len += sprintf(buf + len, "1");
236 len += sprintf(buf + len, " ");
238 if ((devAttr->flags & DMA_DEVICE_FLAG_NO_ISR) != 0) {
239 len += sprintf(buf + len, "NoISR ");
241 if ((devAttr->flags & DMA_DEVICE_FLAG_ALLOW_LARGE_FIFO) != 0) {
242 len += sprintf(buf + len, "Allow-128 ");
245 len +=
246 sprintf(buf + len,
247 "Xfer #: %Lu Ticks: %Lu Bytes: %Lu DescLen: %u\n",
248 devAttr->numTransfers, devAttr->transferTicks,
249 devAttr->transferBytes,
250 devAttr->ring.bytesAllocated);
254 up(&gDMA.lock);
255 *eof = 1;
257 return len;
260 /****************************************************************************/
262 * Determines if a DMA_Device_t is "valid".
264 * @return
265 * TRUE - dma device is valid
266 * FALSE - dma device isn't valid
268 /****************************************************************************/
270 static inline int IsDeviceValid(DMA_Device_t device)
272 return (device >= 0) && (device < DMA_NUM_DEVICE_ENTRIES);
275 /****************************************************************************/
277 * Translates a DMA handle into a pointer to a channel.
279 * @return
280 * non-NULL - pointer to DMA_Channel_t
281 * NULL - DMA Handle was invalid
283 /****************************************************************************/
285 static inline DMA_Channel_t *HandleToChannel(DMA_Handle_t handle)
287 int controllerIdx;
288 int channelIdx;
290 controllerIdx = CONTROLLER_FROM_HANDLE(handle);
291 channelIdx = CHANNEL_FROM_HANDLE(handle);
293 if ((controllerIdx > DMA_NUM_CONTROLLERS)
294 || (channelIdx > DMA_NUM_CHANNELS)) {
295 return NULL;
297 return &gDMA.controller[controllerIdx].channel[channelIdx];
300 /****************************************************************************/
302 * Interrupt handler which is called to process DMA interrupts.
304 /****************************************************************************/
306 static irqreturn_t dma_interrupt_handler(int irq, void *dev_id)
308 DMA_Channel_t *channel;
309 DMA_DeviceAttribute_t *devAttr;
310 int irqStatus;
312 channel = (DMA_Channel_t *) dev_id;
314 /* Figure out why we were called, and knock down the interrupt */
316 irqStatus = dmacHw_getInterruptStatus(channel->dmacHwHandle);
317 dmacHw_clearInterrupt(channel->dmacHwHandle);
319 if ((channel->devType < 0)
320 || (channel->devType > DMA_NUM_DEVICE_ENTRIES)) {
321 printk(KERN_ERR "dma_interrupt_handler: Invalid devType: %d\n",
322 channel->devType);
323 return IRQ_NONE;
325 devAttr = &DMA_gDeviceAttribute[channel->devType];
327 /* Update stats */
329 if ((irqStatus & dmacHw_INTERRUPT_STATUS_TRANS) != 0) {
330 devAttr->transferTicks +=
331 (timer_get_tick_count() - devAttr->transferStartTime);
334 if ((irqStatus & dmacHw_INTERRUPT_STATUS_ERROR) != 0) {
335 printk(KERN_ERR
336 "dma_interrupt_handler: devType :%d DMA error (%s)\n",
337 channel->devType, devAttr->name);
338 } else {
339 devAttr->numTransfers++;
340 devAttr->transferBytes += devAttr->numBytes;
343 /* Call any installed handler */
345 if (devAttr->devHandler != NULL) {
346 devAttr->devHandler(channel->devType, irqStatus,
347 devAttr->userData);
350 return IRQ_HANDLED;
353 /****************************************************************************/
355 * Allocates memory to hold a descriptor ring. The descriptor ring then
356 * needs to be populated by making one or more calls to
357 * dna_add_descriptors.
359 * The returned descriptor ring will be automatically initialized.
361 * @return
362 * 0 Descriptor ring was allocated successfully
363 * -EINVAL Invalid parameters passed in
364 * -ENOMEM Unable to allocate memory for the desired number of descriptors.
366 /****************************************************************************/
368 int dma_alloc_descriptor_ring(DMA_DescriptorRing_t *ring, /* Descriptor ring to populate */
369 int numDescriptors /* Number of descriptors that need to be allocated. */
371 size_t bytesToAlloc = dmacHw_descriptorLen(numDescriptors);
373 if ((ring == NULL) || (numDescriptors <= 0)) {
374 return -EINVAL;
377 ring->physAddr = 0;
378 ring->descriptorsAllocated = 0;
379 ring->bytesAllocated = 0;
381 ring->virtAddr = dma_alloc_writecombine(NULL,
382 bytesToAlloc,
383 &ring->physAddr,
384 GFP_KERNEL);
385 if (ring->virtAddr == NULL) {
386 return -ENOMEM;
389 ring->bytesAllocated = bytesToAlloc;
390 ring->descriptorsAllocated = numDescriptors;
392 return dma_init_descriptor_ring(ring, numDescriptors);
395 EXPORT_SYMBOL(dma_alloc_descriptor_ring);
397 /****************************************************************************/
399 * Releases the memory which was previously allocated for a descriptor ring.
401 /****************************************************************************/
403 void dma_free_descriptor_ring(DMA_DescriptorRing_t *ring /* Descriptor to release */
405 if (ring->virtAddr != NULL) {
406 dma_free_writecombine(NULL,
407 ring->bytesAllocated,
408 ring->virtAddr, ring->physAddr);
411 ring->bytesAllocated = 0;
412 ring->descriptorsAllocated = 0;
413 ring->virtAddr = NULL;
414 ring->physAddr = 0;
417 EXPORT_SYMBOL(dma_free_descriptor_ring);
419 /****************************************************************************/
421 * Initializes a descriptor ring, so that descriptors can be added to it.
422 * Once a descriptor ring has been allocated, it may be reinitialized for
423 * use with additional/different regions of memory.
425 * Note that if 7 descriptors are allocated, it's perfectly acceptable to
426 * initialize the ring with a smaller number of descriptors. The amount
427 * of memory allocated for the descriptor ring will not be reduced, and
428 * the descriptor ring may be reinitialized later
430 * @return
431 * 0 Descriptor ring was initialized successfully
432 * -ENOMEM The descriptor which was passed in has insufficient space
433 * to hold the desired number of descriptors.
435 /****************************************************************************/
437 int dma_init_descriptor_ring(DMA_DescriptorRing_t *ring, /* Descriptor ring to initialize */
438 int numDescriptors /* Number of descriptors to initialize. */
440 if (ring->virtAddr == NULL) {
441 return -EINVAL;
443 if (dmacHw_initDescriptor(ring->virtAddr,
444 ring->physAddr,
445 ring->bytesAllocated, numDescriptors) < 0) {
446 printk(KERN_ERR
447 "dma_init_descriptor_ring: dmacHw_initDescriptor failed\n");
448 return -ENOMEM;
451 return 0;
454 EXPORT_SYMBOL(dma_init_descriptor_ring);
456 /****************************************************************************/
458 * Determines the number of descriptors which would be required for a
459 * transfer of the indicated memory region.
461 * This function also needs to know which DMA device this transfer will
462 * be destined for, so that the appropriate DMA configuration can be retrieved.
463 * DMA parameters such as transfer width, and whether this is a memory-to-memory
464 * or memory-to-peripheral, etc can all affect the actual number of descriptors
465 * required.
467 * @return
468 * > 0 Returns the number of descriptors required for the indicated transfer
469 * -ENODEV - Device handed in is invalid.
470 * -EINVAL Invalid parameters
471 * -ENOMEM Memory exhausted
473 /****************************************************************************/
475 int dma_calculate_descriptor_count(DMA_Device_t device, /* DMA Device that this will be associated with */
476 dma_addr_t srcData, /* Place to get data to write to device */
477 dma_addr_t dstData, /* Pointer to device data address */
478 size_t numBytes /* Number of bytes to transfer to the device */
480 int numDescriptors;
481 DMA_DeviceAttribute_t *devAttr;
483 if (!IsDeviceValid(device)) {
484 return -ENODEV;
486 devAttr = &DMA_gDeviceAttribute[device];
488 numDescriptors = dmacHw_calculateDescriptorCount(&devAttr->config,
489 (void *)srcData,
490 (void *)dstData,
491 numBytes);
492 if (numDescriptors < 0) {
493 printk(KERN_ERR
494 "dma_calculate_descriptor_count: dmacHw_calculateDescriptorCount failed\n");
495 return -EINVAL;
498 return numDescriptors;
501 EXPORT_SYMBOL(dma_calculate_descriptor_count);
503 /****************************************************************************/
505 * Adds a region of memory to the descriptor ring. Note that it may take
506 * multiple descriptors for each region of memory. It is the callers
507 * responsibility to allocate a sufficiently large descriptor ring.
509 * @return
510 * 0 Descriptors were added successfully
511 * -ENODEV Device handed in is invalid.
512 * -EINVAL Invalid parameters
513 * -ENOMEM Memory exhausted
515 /****************************************************************************/
517 int dma_add_descriptors(DMA_DescriptorRing_t *ring, /* Descriptor ring to add descriptors to */
518 DMA_Device_t device, /* DMA Device that descriptors are for */
519 dma_addr_t srcData, /* Place to get data (memory or device) */
520 dma_addr_t dstData, /* Place to put data (memory or device) */
521 size_t numBytes /* Number of bytes to transfer to the device */
523 int rc;
524 DMA_DeviceAttribute_t *devAttr;
526 if (!IsDeviceValid(device)) {
527 return -ENODEV;
529 devAttr = &DMA_gDeviceAttribute[device];
531 rc = dmacHw_setDataDescriptor(&devAttr->config,
532 ring->virtAddr,
533 (void *)srcData,
534 (void *)dstData, numBytes);
535 if (rc < 0) {
536 printk(KERN_ERR
537 "dma_add_descriptors: dmacHw_setDataDescriptor failed with code: %d\n",
538 rc);
539 return -ENOMEM;
542 return 0;
545 EXPORT_SYMBOL(dma_add_descriptors);
547 /****************************************************************************/
549 * Sets the descriptor ring associated with a device.
551 * Once set, the descriptor ring will be associated with the device, even
552 * across channel request/free calls. Passing in a NULL descriptor ring
553 * will release any descriptor ring currently associated with the device.
555 * Note: If you call dma_transfer, or one of the other dma_alloc_ functions
556 * the descriptor ring may be released and reallocated.
558 * Note: This function will release the descriptor memory for any current
559 * descriptor ring associated with this device.
561 * @return
562 * 0 Descriptors were added successfully
563 * -ENODEV Device handed in is invalid.
565 /****************************************************************************/
567 int dma_set_device_descriptor_ring(DMA_Device_t device, /* Device to update the descriptor ring for. */
568 DMA_DescriptorRing_t *ring /* Descriptor ring to add descriptors to */
570 DMA_DeviceAttribute_t *devAttr;
572 if (!IsDeviceValid(device)) {
573 return -ENODEV;
575 devAttr = &DMA_gDeviceAttribute[device];
577 /* Free the previously allocated descriptor ring */
579 dma_free_descriptor_ring(&devAttr->ring);
581 if (ring != NULL) {
582 /* Copy in the new one */
584 devAttr->ring = *ring;
587 /* Set things up so that if dma_transfer is called then this descriptor */
588 /* ring will get freed. */
590 devAttr->prevSrcData = 0;
591 devAttr->prevDstData = 0;
592 devAttr->prevNumBytes = 0;
594 return 0;
597 EXPORT_SYMBOL(dma_set_device_descriptor_ring);
599 /****************************************************************************/
601 * Retrieves the descriptor ring associated with a device.
603 * @return
604 * 0 Descriptors were added successfully
605 * -ENODEV Device handed in is invalid.
607 /****************************************************************************/
609 int dma_get_device_descriptor_ring(DMA_Device_t device, /* Device to retrieve the descriptor ring for. */
610 DMA_DescriptorRing_t *ring /* Place to store retrieved ring */
612 DMA_DeviceAttribute_t *devAttr;
614 memset(ring, 0, sizeof(*ring));
616 if (!IsDeviceValid(device)) {
617 return -ENODEV;
619 devAttr = &DMA_gDeviceAttribute[device];
621 *ring = devAttr->ring;
623 return 0;
626 EXPORT_SYMBOL(dma_get_device_descriptor_ring);
628 /****************************************************************************/
630 * Configures a DMA channel.
632 * @return
633 * >= 0 - Initialization was successful.
635 * -EBUSY - Device is currently being used.
636 * -ENODEV - Device handed in is invalid.
638 /****************************************************************************/
640 static int ConfigChannel(DMA_Handle_t handle)
642 DMA_Channel_t *channel;
643 DMA_DeviceAttribute_t *devAttr;
644 int controllerIdx;
646 channel = HandleToChannel(handle);
647 if (channel == NULL) {
648 return -ENODEV;
650 devAttr = &DMA_gDeviceAttribute[channel->devType];
651 controllerIdx = CONTROLLER_FROM_HANDLE(handle);
653 if ((devAttr->flags & DMA_DEVICE_FLAG_PORT_PER_DMAC) != 0) {
654 if (devAttr->config.transferType ==
655 dmacHw_TRANSFER_TYPE_MEM_TO_PERIPHERAL) {
656 devAttr->config.dstPeripheralPort =
657 devAttr->dmacPort[controllerIdx];
658 } else if (devAttr->config.transferType ==
659 dmacHw_TRANSFER_TYPE_PERIPHERAL_TO_MEM) {
660 devAttr->config.srcPeripheralPort =
661 devAttr->dmacPort[controllerIdx];
665 if (dmacHw_configChannel(channel->dmacHwHandle, &devAttr->config) != 0) {
666 printk(KERN_ERR "ConfigChannel: dmacHw_configChannel failed\n");
667 return -EIO;
670 return 0;
673 /****************************************************************************/
675 * Initializes all of the data structures associated with the DMA.
676 * @return
677 * >= 0 - Initialization was successful.
679 * -EBUSY - Device is currently being used.
680 * -ENODEV - Device handed in is invalid.
682 /****************************************************************************/
684 int dma_init(void)
686 int rc = 0;
687 int controllerIdx;
688 int channelIdx;
689 DMA_Device_t devIdx;
690 DMA_Channel_t *channel;
691 DMA_Handle_t dedicatedHandle;
693 memset(&gDMA, 0, sizeof(gDMA));
695 sema_init(&gDMA.lock, 0);
696 init_waitqueue_head(&gDMA.freeChannelQ);
698 /* Initialize the Hardware */
700 dmacHw_initDma();
702 /* Start off by marking all of the DMA channels as shared. */
704 for (controllerIdx = 0; controllerIdx < DMA_NUM_CONTROLLERS;
705 controllerIdx++) {
706 for (channelIdx = 0; channelIdx < DMA_NUM_CHANNELS;
707 channelIdx++) {
708 channel =
709 &gDMA.controller[controllerIdx].channel[channelIdx];
711 channel->flags = 0;
712 channel->devType = DMA_DEVICE_NONE;
713 channel->lastDevType = DMA_DEVICE_NONE;
715 #if (DMA_DEBUG_TRACK_RESERVATION)
716 channel->fileName = "";
717 channel->lineNum = 0;
718 #endif
720 channel->dmacHwHandle =
721 dmacHw_getChannelHandle(dmacHw_MAKE_CHANNEL_ID
722 (controllerIdx,
723 channelIdx));
724 dmacHw_initChannel(channel->dmacHwHandle);
728 /* Record any special attributes that channels may have */
730 gDMA.controller[0].channel[0].flags |= DMA_CHANNEL_FLAG_LARGE_FIFO;
731 gDMA.controller[0].channel[1].flags |= DMA_CHANNEL_FLAG_LARGE_FIFO;
732 gDMA.controller[1].channel[0].flags |= DMA_CHANNEL_FLAG_LARGE_FIFO;
733 gDMA.controller[1].channel[1].flags |= DMA_CHANNEL_FLAG_LARGE_FIFO;
735 /* Now walk through and record the dedicated channels. */
737 for (devIdx = 0; devIdx < DMA_NUM_DEVICE_ENTRIES; devIdx++) {
738 DMA_DeviceAttribute_t *devAttr = &DMA_gDeviceAttribute[devIdx];
740 if (((devAttr->flags & DMA_DEVICE_FLAG_NO_ISR) != 0)
741 && ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) == 0)) {
742 printk(KERN_ERR
743 "DMA Device: %s Can only request NO_ISR for dedicated devices\n",
744 devAttr->name);
745 rc = -EINVAL;
746 goto out;
749 if ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) != 0) {
750 /* This is a dedicated device. Mark the channel as being reserved. */
752 if (devAttr->dedicatedController >= DMA_NUM_CONTROLLERS) {
753 printk(KERN_ERR
754 "DMA Device: %s DMA Controller %d is out of range\n",
755 devAttr->name,
756 devAttr->dedicatedController);
757 rc = -EINVAL;
758 goto out;
761 if (devAttr->dedicatedChannel >= DMA_NUM_CHANNELS) {
762 printk(KERN_ERR
763 "DMA Device: %s DMA Channel %d is out of range\n",
764 devAttr->name,
765 devAttr->dedicatedChannel);
766 rc = -EINVAL;
767 goto out;
770 dedicatedHandle =
771 MAKE_HANDLE(devAttr->dedicatedController,
772 devAttr->dedicatedChannel);
773 channel = HandleToChannel(dedicatedHandle);
775 if ((channel->flags & DMA_CHANNEL_FLAG_IS_DEDICATED) !=
776 0) {
777 printk
778 ("DMA Device: %s attempting to use same DMA Controller:Channel (%d:%d) as %s\n",
779 devAttr->name,
780 devAttr->dedicatedController,
781 devAttr->dedicatedChannel,
782 DMA_gDeviceAttribute[channel->devType].
783 name);
784 rc = -EBUSY;
785 goto out;
788 channel->flags |= DMA_CHANNEL_FLAG_IS_DEDICATED;
789 channel->devType = devIdx;
791 if (devAttr->flags & DMA_DEVICE_FLAG_NO_ISR) {
792 channel->flags |= DMA_CHANNEL_FLAG_NO_ISR;
795 /* For dedicated channels, we can go ahead and configure the DMA channel now */
796 /* as well. */
798 ConfigChannel(dedicatedHandle);
802 /* Go through and register the interrupt handlers */
804 for (controllerIdx = 0; controllerIdx < DMA_NUM_CONTROLLERS;
805 controllerIdx++) {
806 for (channelIdx = 0; channelIdx < DMA_NUM_CHANNELS;
807 channelIdx++) {
808 channel =
809 &gDMA.controller[controllerIdx].channel[channelIdx];
811 if ((channel->flags & DMA_CHANNEL_FLAG_NO_ISR) == 0) {
812 snprintf(channel->name, sizeof(channel->name),
813 "dma %d:%d %s", controllerIdx,
814 channelIdx,
815 channel->devType ==
816 DMA_DEVICE_NONE ? "" :
817 DMA_gDeviceAttribute[channel->devType].
818 name);
820 rc =
821 request_irq(IRQ_DMA0C0 +
822 (controllerIdx *
823 DMA_NUM_CHANNELS) +
824 channelIdx,
825 dma_interrupt_handler,
826 IRQF_DISABLED, channel->name,
827 channel);
828 if (rc != 0) {
829 printk(KERN_ERR
830 "request_irq for IRQ_DMA%dC%d failed\n",
831 controllerIdx, channelIdx);
837 /* Create /proc/dma/channels and /proc/dma/devices */
839 gDmaDir = proc_mkdir("dma", NULL);
841 if (gDmaDir == NULL) {
842 printk(KERN_ERR "Unable to create /proc/dma\n");
843 } else {
844 create_proc_read_entry("channels", 0, gDmaDir,
845 dma_proc_read_channels, NULL);
846 create_proc_read_entry("devices", 0, gDmaDir,
847 dma_proc_read_devices, NULL);
848 create_proc_read_entry("mem-type", 0, gDmaDir,
849 dma_proc_read_mem_type, NULL);
852 out:
854 up(&gDMA.lock);
856 return rc;
859 /****************************************************************************/
861 * Reserves a channel for use with @a dev. If the device is setup to use
862 * a shared channel, then this function will block until a free channel
863 * becomes available.
865 * @return
866 * >= 0 - A valid DMA Handle.
867 * -EBUSY - Device is currently being used.
868 * -ENODEV - Device handed in is invalid.
870 /****************************************************************************/
872 #if (DMA_DEBUG_TRACK_RESERVATION)
873 DMA_Handle_t dma_request_channel_dbg
874 (DMA_Device_t dev, const char *fileName, int lineNum)
875 #else
876 DMA_Handle_t dma_request_channel(DMA_Device_t dev)
877 #endif
879 DMA_Handle_t handle;
880 DMA_DeviceAttribute_t *devAttr;
881 DMA_Channel_t *channel;
882 int controllerIdx;
883 int controllerIdx2;
884 int channelIdx;
886 if (down_interruptible(&gDMA.lock) < 0) {
887 return -ERESTARTSYS;
890 if ((dev < 0) || (dev >= DMA_NUM_DEVICE_ENTRIES)) {
891 handle = -ENODEV;
892 goto out;
894 devAttr = &DMA_gDeviceAttribute[dev];
896 #if (DMA_DEBUG_TRACK_RESERVATION)
898 char *s;
900 s = strrchr(fileName, '/');
901 if (s != NULL) {
902 fileName = s + 1;
905 #endif
906 if ((devAttr->flags & DMA_DEVICE_FLAG_IN_USE) != 0) {
907 /* This device has already been requested and not been freed */
909 printk(KERN_ERR "%s: device %s is already requested\n",
910 __func__, devAttr->name);
911 handle = -EBUSY;
912 goto out;
915 if ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) != 0) {
916 /* This device has a dedicated channel. */
918 channel =
919 &gDMA.controller[devAttr->dedicatedController].
920 channel[devAttr->dedicatedChannel];
921 if ((channel->flags & DMA_CHANNEL_FLAG_IN_USE) != 0) {
922 handle = -EBUSY;
923 goto out;
926 channel->flags |= DMA_CHANNEL_FLAG_IN_USE;
927 devAttr->flags |= DMA_DEVICE_FLAG_IN_USE;
929 #if (DMA_DEBUG_TRACK_RESERVATION)
930 channel->fileName = fileName;
931 channel->lineNum = lineNum;
932 #endif
933 handle =
934 MAKE_HANDLE(devAttr->dedicatedController,
935 devAttr->dedicatedChannel);
936 goto out;
939 /* This device needs to use one of the shared channels. */
941 handle = DMA_INVALID_HANDLE;
942 while (handle == DMA_INVALID_HANDLE) {
943 /* Scan through the shared channels and see if one is available */
945 for (controllerIdx2 = 0; controllerIdx2 < DMA_NUM_CONTROLLERS;
946 controllerIdx2++) {
947 /* Check to see if we should try on controller 1 first. */
949 controllerIdx = controllerIdx2;
950 if ((devAttr->
951 flags & DMA_DEVICE_FLAG_ALLOC_DMA1_FIRST) != 0) {
952 controllerIdx = 1 - controllerIdx;
955 /* See if the device is available on the controller being tested */
957 if ((devAttr->
958 flags & (DMA_DEVICE_FLAG_ON_DMA0 << controllerIdx))
959 != 0) {
960 for (channelIdx = 0;
961 channelIdx < DMA_NUM_CHANNELS;
962 channelIdx++) {
963 channel =
964 &gDMA.controller[controllerIdx].
965 channel[channelIdx];
967 if (((channel->
968 flags &
969 DMA_CHANNEL_FLAG_IS_DEDICATED) ==
972 ((channel->
973 flags & DMA_CHANNEL_FLAG_IN_USE)
974 == 0)) {
975 if (((channel->
976 flags &
977 DMA_CHANNEL_FLAG_LARGE_FIFO)
978 != 0)
980 ((devAttr->
981 flags &
982 DMA_DEVICE_FLAG_ALLOW_LARGE_FIFO)
983 == 0)) {
984 /* This channel is a large fifo - don't tie it up */
985 /* with devices that we don't want using it. */
987 continue;
990 channel->flags |=
991 DMA_CHANNEL_FLAG_IN_USE;
992 channel->devType = dev;
993 devAttr->flags |=
994 DMA_DEVICE_FLAG_IN_USE;
996 #if (DMA_DEBUG_TRACK_RESERVATION)
997 channel->fileName = fileName;
998 channel->lineNum = lineNum;
999 #endif
1000 handle =
1001 MAKE_HANDLE(controllerIdx,
1002 channelIdx);
1004 /* Now that we've reserved the channel - we can go ahead and configure it */
1006 if (ConfigChannel(handle) != 0) {
1007 handle = -EIO;
1008 printk(KERN_ERR
1009 "dma_request_channel: ConfigChannel failed\n");
1011 goto out;
1017 /* No channels are currently available. Let's wait for one to free up. */
1020 DEFINE_WAIT(wait);
1022 prepare_to_wait(&gDMA.freeChannelQ, &wait,
1023 TASK_INTERRUPTIBLE);
1024 up(&gDMA.lock);
1025 schedule();
1026 finish_wait(&gDMA.freeChannelQ, &wait);
1028 if (signal_pending(current)) {
1029 /* We don't currently hold gDMA.lock, so we return directly */
1031 return -ERESTARTSYS;
1035 if (down_interruptible(&gDMA.lock)) {
1036 return -ERESTARTSYS;
1040 out:
1041 up(&gDMA.lock);
1043 return handle;
1046 /* Create both _dbg and non _dbg functions for modules. */
1048 #if (DMA_DEBUG_TRACK_RESERVATION)
1049 #undef dma_request_channel
1050 DMA_Handle_t dma_request_channel(DMA_Device_t dev)
1052 return dma_request_channel_dbg(dev, __FILE__, __LINE__);
1055 EXPORT_SYMBOL(dma_request_channel_dbg);
1056 #endif
1057 EXPORT_SYMBOL(dma_request_channel);
1059 /****************************************************************************/
1061 * Frees a previously allocated DMA Handle.
1063 /****************************************************************************/
1065 int dma_free_channel(DMA_Handle_t handle /* DMA handle. */
1067 int rc = 0;
1068 DMA_Channel_t *channel;
1069 DMA_DeviceAttribute_t *devAttr;
1071 if (down_interruptible(&gDMA.lock) < 0) {
1072 return -ERESTARTSYS;
1075 channel = HandleToChannel(handle);
1076 if (channel == NULL) {
1077 rc = -EINVAL;
1078 goto out;
1081 devAttr = &DMA_gDeviceAttribute[channel->devType];
1083 if ((channel->flags & DMA_CHANNEL_FLAG_IS_DEDICATED) == 0) {
1084 channel->lastDevType = channel->devType;
1085 channel->devType = DMA_DEVICE_NONE;
1087 channel->flags &= ~DMA_CHANNEL_FLAG_IN_USE;
1088 devAttr->flags &= ~DMA_DEVICE_FLAG_IN_USE;
1090 out:
1091 up(&gDMA.lock);
1093 wake_up_interruptible(&gDMA.freeChannelQ);
1095 return rc;
1098 EXPORT_SYMBOL(dma_free_channel);
1100 /****************************************************************************/
1102 * Determines if a given device has been configured as using a shared
1103 * channel.
1105 * @return
1106 * 0 Device uses a dedicated channel
1107 * > zero Device uses a shared channel
1108 * < zero Error code
1110 /****************************************************************************/
1112 int dma_device_is_channel_shared(DMA_Device_t device /* Device to check. */
1114 DMA_DeviceAttribute_t *devAttr;
1116 if (!IsDeviceValid(device)) {
1117 return -ENODEV;
1119 devAttr = &DMA_gDeviceAttribute[device];
1121 return ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) == 0);
1124 EXPORT_SYMBOL(dma_device_is_channel_shared);
1126 /****************************************************************************/
1128 * Allocates buffers for the descriptors. This is normally done automatically
1129 * but needs to be done explicitly when initiating a dma from interrupt
1130 * context.
1132 * @return
1133 * 0 Descriptors were allocated successfully
1134 * -EINVAL Invalid device type for this kind of transfer
1135 * (i.e. the device is _MEM_TO_DEV and not _DEV_TO_MEM)
1136 * -ENOMEM Memory exhausted
1138 /****************************************************************************/
1140 int dma_alloc_descriptors(DMA_Handle_t handle, /* DMA Handle */
1141 dmacHw_TRANSFER_TYPE_e transferType, /* Type of transfer being performed */
1142 dma_addr_t srcData, /* Place to get data to write to device */
1143 dma_addr_t dstData, /* Pointer to device data address */
1144 size_t numBytes /* Number of bytes to transfer to the device */
1146 DMA_Channel_t *channel;
1147 DMA_DeviceAttribute_t *devAttr;
1148 int numDescriptors;
1149 size_t ringBytesRequired;
1150 int rc = 0;
1152 channel = HandleToChannel(handle);
1153 if (channel == NULL) {
1154 return -ENODEV;
1157 devAttr = &DMA_gDeviceAttribute[channel->devType];
1159 if (devAttr->config.transferType != transferType) {
1160 return -EINVAL;
1163 /* Figure out how many descriptors we need. */
1165 /* printk("srcData: 0x%08x dstData: 0x%08x, numBytes: %d\n", */
1166 /* srcData, dstData, numBytes); */
1168 numDescriptors = dmacHw_calculateDescriptorCount(&devAttr->config,
1169 (void *)srcData,
1170 (void *)dstData,
1171 numBytes);
1172 if (numDescriptors < 0) {
1173 printk(KERN_ERR "%s: dmacHw_calculateDescriptorCount failed\n",
1174 __func__);
1175 return -EINVAL;
1178 /* Check to see if we can reuse the existing descriptor ring, or if we need to allocate */
1179 /* a new one. */
1181 ringBytesRequired = dmacHw_descriptorLen(numDescriptors);
1183 /* printk("ringBytesRequired: %d\n", ringBytesRequired); */
1185 if (ringBytesRequired > devAttr->ring.bytesAllocated) {
1186 /* Make sure that this code path is never taken from interrupt context. */
1187 /* It's OK for an interrupt to initiate a DMA transfer, but the descriptor */
1188 /* allocation needs to have already been done. */
1190 might_sleep();
1192 /* Free the old descriptor ring and allocate a new one. */
1194 dma_free_descriptor_ring(&devAttr->ring);
1196 /* And allocate a new one. */
1198 rc =
1199 dma_alloc_descriptor_ring(&devAttr->ring,
1200 numDescriptors);
1201 if (rc < 0) {
1202 printk(KERN_ERR
1203 "%s: dma_alloc_descriptor_ring(%d) failed\n",
1204 __func__, numDescriptors);
1205 return rc;
1207 /* Setup the descriptor for this transfer */
1209 if (dmacHw_initDescriptor(devAttr->ring.virtAddr,
1210 devAttr->ring.physAddr,
1211 devAttr->ring.bytesAllocated,
1212 numDescriptors) < 0) {
1213 printk(KERN_ERR "%s: dmacHw_initDescriptor failed\n",
1214 __func__);
1215 return -EINVAL;
1217 } else {
1218 /* We've already got enough ring buffer allocated. All we need to do is reset */
1219 /* any control information, just in case the previous DMA was stopped. */
1221 dmacHw_resetDescriptorControl(devAttr->ring.virtAddr);
1224 /* dma_alloc/free both set the prevSrc/DstData to 0. If they happen to be the same */
1225 /* as last time, then we don't need to call setDataDescriptor again. */
1227 if (dmacHw_setDataDescriptor(&devAttr->config,
1228 devAttr->ring.virtAddr,
1229 (void *)srcData,
1230 (void *)dstData, numBytes) < 0) {
1231 printk(KERN_ERR "%s: dmacHw_setDataDescriptor failed\n",
1232 __func__);
1233 return -EINVAL;
1236 /* Remember the critical information for this transfer so that we can eliminate */
1237 /* another call to dma_alloc_descriptors if the caller reuses the same buffers */
1239 devAttr->prevSrcData = srcData;
1240 devAttr->prevDstData = dstData;
1241 devAttr->prevNumBytes = numBytes;
1243 return 0;
1246 EXPORT_SYMBOL(dma_alloc_descriptors);
1248 /****************************************************************************/
1250 * Allocates and sets up descriptors for a double buffered circular buffer.
1252 * This is primarily intended to be used for things like the ingress samples
1253 * from a microphone.
1255 * @return
1256 * > 0 Number of descriptors actually allocated.
1257 * -EINVAL Invalid device type for this kind of transfer
1258 * (i.e. the device is _MEM_TO_DEV and not _DEV_TO_MEM)
1259 * -ENOMEM Memory exhausted
1261 /****************************************************************************/
1263 int dma_alloc_double_dst_descriptors(DMA_Handle_t handle, /* DMA Handle */
1264 dma_addr_t srcData, /* Physical address of source data */
1265 dma_addr_t dstData1, /* Physical address of first destination buffer */
1266 dma_addr_t dstData2, /* Physical address of second destination buffer */
1267 size_t numBytes /* Number of bytes in each destination buffer */
1269 DMA_Channel_t *channel;
1270 DMA_DeviceAttribute_t *devAttr;
1271 int numDst1Descriptors;
1272 int numDst2Descriptors;
1273 int numDescriptors;
1274 size_t ringBytesRequired;
1275 int rc = 0;
1277 channel = HandleToChannel(handle);
1278 if (channel == NULL) {
1279 return -ENODEV;
1282 devAttr = &DMA_gDeviceAttribute[channel->devType];
1284 /* Figure out how many descriptors we need. */
1286 /* printk("srcData: 0x%08x dstData: 0x%08x, numBytes: %d\n", */
1287 /* srcData, dstData, numBytes); */
1289 numDst1Descriptors =
1290 dmacHw_calculateDescriptorCount(&devAttr->config, (void *)srcData,
1291 (void *)dstData1, numBytes);
1292 if (numDst1Descriptors < 0) {
1293 return -EINVAL;
1295 numDst2Descriptors =
1296 dmacHw_calculateDescriptorCount(&devAttr->config, (void *)srcData,
1297 (void *)dstData2, numBytes);
1298 if (numDst2Descriptors < 0) {
1299 return -EINVAL;
1301 numDescriptors = numDst1Descriptors + numDst2Descriptors;
1302 /* printk("numDescriptors: %d\n", numDescriptors); */
1304 /* Check to see if we can reuse the existing descriptor ring, or if we need to allocate */
1305 /* a new one. */
1307 ringBytesRequired = dmacHw_descriptorLen(numDescriptors);
1309 /* printk("ringBytesRequired: %d\n", ringBytesRequired); */
1311 if (ringBytesRequired > devAttr->ring.bytesAllocated) {
1312 /* Make sure that this code path is never taken from interrupt context. */
1313 /* It's OK for an interrupt to initiate a DMA transfer, but the descriptor */
1314 /* allocation needs to have already been done. */
1316 might_sleep();
1318 /* Free the old descriptor ring and allocate a new one. */
1320 dma_free_descriptor_ring(&devAttr->ring);
1322 /* And allocate a new one. */
1324 rc =
1325 dma_alloc_descriptor_ring(&devAttr->ring,
1326 numDescriptors);
1327 if (rc < 0) {
1328 printk(KERN_ERR
1329 "%s: dma_alloc_descriptor_ring(%d) failed\n",
1330 __func__, ringBytesRequired);
1331 return rc;
1335 /* Setup the descriptor for this transfer. Since this function is used with */
1336 /* CONTINUOUS DMA operations, we need to reinitialize every time, otherwise */
1337 /* setDataDescriptor will keep trying to append onto the end. */
1339 if (dmacHw_initDescriptor(devAttr->ring.virtAddr,
1340 devAttr->ring.physAddr,
1341 devAttr->ring.bytesAllocated,
1342 numDescriptors) < 0) {
1343 printk(KERN_ERR "%s: dmacHw_initDescriptor failed\n", __func__);
1344 return -EINVAL;
1347 /* dma_alloc/free both set the prevSrc/DstData to 0. If they happen to be the same */
1348 /* as last time, then we don't need to call setDataDescriptor again. */
1350 if (dmacHw_setDataDescriptor(&devAttr->config,
1351 devAttr->ring.virtAddr,
1352 (void *)srcData,
1353 (void *)dstData1, numBytes) < 0) {
1354 printk(KERN_ERR "%s: dmacHw_setDataDescriptor 1 failed\n",
1355 __func__);
1356 return -EINVAL;
1358 if (dmacHw_setDataDescriptor(&devAttr->config,
1359 devAttr->ring.virtAddr,
1360 (void *)srcData,
1361 (void *)dstData2, numBytes) < 0) {
1362 printk(KERN_ERR "%s: dmacHw_setDataDescriptor 2 failed\n",
1363 __func__);
1364 return -EINVAL;
1367 /* You should use dma_start_transfer rather than dma_transfer_xxx so we don't */
1368 /* try to make the 'prev' variables right. */
1370 devAttr->prevSrcData = 0;
1371 devAttr->prevDstData = 0;
1372 devAttr->prevNumBytes = 0;
1374 return numDescriptors;
1377 EXPORT_SYMBOL(dma_alloc_double_dst_descriptors);
1379 /****************************************************************************/
1381 * Initiates a transfer when the descriptors have already been setup.
1383 * This is a special case, and normally, the dma_transfer_xxx functions should
1384 * be used.
1386 * @return
1387 * 0 Transfer was started successfully
1388 * -ENODEV Invalid handle
1390 /****************************************************************************/
1392 int dma_start_transfer(DMA_Handle_t handle)
1394 DMA_Channel_t *channel;
1395 DMA_DeviceAttribute_t *devAttr;
1397 channel = HandleToChannel(handle);
1398 if (channel == NULL) {
1399 return -ENODEV;
1401 devAttr = &DMA_gDeviceAttribute[channel->devType];
1403 dmacHw_initiateTransfer(channel->dmacHwHandle, &devAttr->config,
1404 devAttr->ring.virtAddr);
1406 /* Since we got this far, everything went successfully */
1408 return 0;
1411 EXPORT_SYMBOL(dma_start_transfer);
1413 /****************************************************************************/
1415 * Stops a previously started DMA transfer.
1417 * @return
1418 * 0 Transfer was stopped successfully
1419 * -ENODEV Invalid handle
1421 /****************************************************************************/
1423 int dma_stop_transfer(DMA_Handle_t handle)
1425 DMA_Channel_t *channel;
1427 channel = HandleToChannel(handle);
1428 if (channel == NULL) {
1429 return -ENODEV;
1432 dmacHw_stopTransfer(channel->dmacHwHandle);
1434 return 0;
1437 EXPORT_SYMBOL(dma_stop_transfer);
1439 /****************************************************************************/
1441 * Waits for a DMA to complete by polling. This function is only intended
1442 * to be used for testing. Interrupts should be used for most DMA operations.
1444 /****************************************************************************/
1446 int dma_wait_transfer_done(DMA_Handle_t handle)
1448 DMA_Channel_t *channel;
1449 dmacHw_TRANSFER_STATUS_e status;
1451 channel = HandleToChannel(handle);
1452 if (channel == NULL) {
1453 return -ENODEV;
1456 while ((status =
1457 dmacHw_transferCompleted(channel->dmacHwHandle)) ==
1458 dmacHw_TRANSFER_STATUS_BUSY) {
1462 if (status == dmacHw_TRANSFER_STATUS_ERROR) {
1463 printk(KERN_ERR "%s: DMA transfer failed\n", __func__);
1464 return -EIO;
1466 return 0;
1469 EXPORT_SYMBOL(dma_wait_transfer_done);
1471 /****************************************************************************/
1473 * Initiates a DMA, allocating the descriptors as required.
1475 * @return
1476 * 0 Transfer was started successfully
1477 * -EINVAL Invalid device type for this kind of transfer
1478 * (i.e. the device is _DEV_TO_MEM and not _MEM_TO_DEV)
1480 /****************************************************************************/
1482 int dma_transfer(DMA_Handle_t handle, /* DMA Handle */
1483 dmacHw_TRANSFER_TYPE_e transferType, /* Type of transfer being performed */
1484 dma_addr_t srcData, /* Place to get data to write to device */
1485 dma_addr_t dstData, /* Pointer to device data address */
1486 size_t numBytes /* Number of bytes to transfer to the device */
1488 DMA_Channel_t *channel;
1489 DMA_DeviceAttribute_t *devAttr;
1490 int rc = 0;
1492 channel = HandleToChannel(handle);
1493 if (channel == NULL) {
1494 return -ENODEV;
1497 devAttr = &DMA_gDeviceAttribute[channel->devType];
1499 if (devAttr->config.transferType != transferType) {
1500 return -EINVAL;
1503 /* We keep track of the information about the previous request for this */
1504 /* device, and if the attributes match, then we can use the descriptors we setup */
1505 /* the last time, and not have to reinitialize everything. */
1508 rc =
1509 dma_alloc_descriptors(handle, transferType, srcData,
1510 dstData, numBytes);
1511 if (rc != 0) {
1512 return rc;
1516 /* And kick off the transfer */
1518 devAttr->numBytes = numBytes;
1519 devAttr->transferStartTime = timer_get_tick_count();
1521 dmacHw_initiateTransfer(channel->dmacHwHandle, &devAttr->config,
1522 devAttr->ring.virtAddr);
1524 /* Since we got this far, everything went successfully */
1526 return 0;
1529 EXPORT_SYMBOL(dma_transfer);
1531 /****************************************************************************/
1533 * Set the callback function which will be called when a transfer completes.
1534 * If a NULL callback function is set, then no callback will occur.
1536 * @note @a devHandler will be called from IRQ context.
1538 * @return
1539 * 0 - Success
1540 * -ENODEV - Device handed in is invalid.
1542 /****************************************************************************/
1544 int dma_set_device_handler(DMA_Device_t dev, /* Device to set the callback for. */
1545 DMA_DeviceHandler_t devHandler, /* Function to call when the DMA completes */
1546 void *userData /* Pointer which will be passed to devHandler. */
1548 DMA_DeviceAttribute_t *devAttr;
1549 unsigned long flags;
1551 if (!IsDeviceValid(dev)) {
1552 return -ENODEV;
1554 devAttr = &DMA_gDeviceAttribute[dev];
1556 local_irq_save(flags);
1558 devAttr->userData = userData;
1559 devAttr->devHandler = devHandler;
1561 local_irq_restore(flags);
1563 return 0;
1566 EXPORT_SYMBOL(dma_set_device_handler);
1568 /****************************************************************************/
1570 * Initializes a memory mapping structure
1572 /****************************************************************************/
1574 int dma_init_mem_map(DMA_MemMap_t *memMap)
1576 memset(memMap, 0, sizeof(*memMap));
1578 sema_init(&memMap->lock, 1);
1580 return 0;
1583 EXPORT_SYMBOL(dma_init_mem_map);
1585 /****************************************************************************/
1587 * Releases any memory currently being held by a memory mapping structure.
1589 /****************************************************************************/
1591 int dma_term_mem_map(DMA_MemMap_t *memMap)
1593 down(&memMap->lock); /* Just being paranoid */
1595 /* Free up any allocated memory */
1597 up(&memMap->lock);
1598 memset(memMap, 0, sizeof(*memMap));
1600 return 0;
1603 EXPORT_SYMBOL(dma_term_mem_map);
1605 /****************************************************************************/
1607 * Looks at a memory address and categorizes it.
1609 * @return One of the values from the DMA_MemType_t enumeration.
1611 /****************************************************************************/
1613 DMA_MemType_t dma_mem_type(void *addr)
1615 unsigned long addrVal = (unsigned long)addr;
1617 if (addrVal >= VMALLOC_END) {
1618 /* NOTE: DMA virtual memory space starts at 0xFFxxxxxx */
1620 /* dma_alloc_xxx pages are physically and virtually contiguous */
1622 return DMA_MEM_TYPE_DMA;
1625 /* Technically, we could add one more classification. Addresses between VMALLOC_END */
1626 /* and the beginning of the DMA virtual address could be considered to be I/O space. */
1627 /* Right now, nobody cares about this particular classification, so we ignore it. */
1629 if (is_vmalloc_addr(addr)) {
1630 /* Address comes from the vmalloc'd region. Pages are virtually */
1631 /* contiguous but NOT physically contiguous */
1633 return DMA_MEM_TYPE_VMALLOC;
1636 if (addrVal >= PAGE_OFFSET) {
1637 /* PAGE_OFFSET is typically 0xC0000000 */
1639 /* kmalloc'd pages are physically contiguous */
1641 return DMA_MEM_TYPE_KMALLOC;
1644 return DMA_MEM_TYPE_USER;
1647 EXPORT_SYMBOL(dma_mem_type);
1649 /****************************************************************************/
1651 * Looks at a memory address and determines if we support DMA'ing to/from
1652 * that type of memory.
1654 * @return boolean -
1655 * return value != 0 means dma supported
1656 * return value == 0 means dma not supported
1658 /****************************************************************************/
1660 int dma_mem_supports_dma(void *addr)
1662 DMA_MemType_t memType = dma_mem_type(addr);
1664 return (memType == DMA_MEM_TYPE_DMA)
1665 #if ALLOW_MAP_OF_KMALLOC_MEMORY
1666 || (memType == DMA_MEM_TYPE_KMALLOC)
1667 #endif
1668 || (memType == DMA_MEM_TYPE_USER);
1671 EXPORT_SYMBOL(dma_mem_supports_dma);
1673 /****************************************************************************/
1675 * Maps in a memory region such that it can be used for performing a DMA.
1677 * @return
1679 /****************************************************************************/
1681 int dma_map_start(DMA_MemMap_t *memMap, /* Stores state information about the map */
1682 enum dma_data_direction dir /* Direction that the mapping will be going */
1684 int rc;
1686 down(&memMap->lock);
1688 DMA_MAP_PRINT("memMap: %p\n", memMap);
1690 if (memMap->inUse) {
1691 printk(KERN_ERR "%s: memory map %p is already being used\n",
1692 __func__, memMap);
1693 rc = -EBUSY;
1694 goto out;
1697 memMap->inUse = 1;
1698 memMap->dir = dir;
1699 memMap->numRegionsUsed = 0;
1701 rc = 0;
1703 out:
1705 DMA_MAP_PRINT("returning %d", rc);
1707 up(&memMap->lock);
1709 return rc;
1712 EXPORT_SYMBOL(dma_map_start);
1714 /****************************************************************************/
1716 * Adds a segment of memory to a memory map. Each segment is both
1717 * physically and virtually contiguous.
1719 * @return 0 on success, error code otherwise.
1721 /****************************************************************************/
1723 static int dma_map_add_segment(DMA_MemMap_t *memMap, /* Stores state information about the map */
1724 DMA_Region_t *region, /* Region that the segment belongs to */
1725 void *virtAddr, /* Virtual address of the segment being added */
1726 dma_addr_t physAddr, /* Physical address of the segment being added */
1727 size_t numBytes /* Number of bytes of the segment being added */
1729 DMA_Segment_t *segment;
1731 DMA_MAP_PRINT("memMap:%p va:%p pa:0x%x #:%d\n", memMap, virtAddr,
1732 physAddr, numBytes);
1734 /* Sanity check */
1736 if (((unsigned long)virtAddr < (unsigned long)region->virtAddr)
1737 || (((unsigned long)virtAddr + numBytes)) >
1738 ((unsigned long)region->virtAddr + region->numBytes)) {
1739 printk(KERN_ERR
1740 "%s: virtAddr %p is outside region @ %p len: %d\n",
1741 __func__, virtAddr, region->virtAddr, region->numBytes);
1742 return -EINVAL;
1745 if (region->numSegmentsUsed > 0) {
1746 /* Check to see if this segment is physically contiguous with the previous one */
1748 segment = &region->segment[region->numSegmentsUsed - 1];
1750 if ((segment->physAddr + segment->numBytes) == physAddr) {
1751 /* It is - just add on to the end */
1753 DMA_MAP_PRINT("appending %d bytes to last segment\n",
1754 numBytes);
1756 segment->numBytes += numBytes;
1758 return 0;
1762 /* Reallocate to hold more segments, if required. */
1764 if (region->numSegmentsUsed >= region->numSegmentsAllocated) {
1765 DMA_Segment_t *newSegment;
1766 size_t oldSize =
1767 region->numSegmentsAllocated * sizeof(*newSegment);
1768 int newAlloc = region->numSegmentsAllocated + 4;
1769 size_t newSize = newAlloc * sizeof(*newSegment);
1771 newSegment = kmalloc(newSize, GFP_KERNEL);
1772 if (newSegment == NULL) {
1773 return -ENOMEM;
1775 memcpy(newSegment, region->segment, oldSize);
1776 memset(&((uint8_t *) newSegment)[oldSize], 0,
1777 newSize - oldSize);
1778 kfree(region->segment);
1780 region->numSegmentsAllocated = newAlloc;
1781 region->segment = newSegment;
1784 segment = &region->segment[region->numSegmentsUsed];
1785 region->numSegmentsUsed++;
1787 segment->virtAddr = virtAddr;
1788 segment->physAddr = physAddr;
1789 segment->numBytes = numBytes;
1791 DMA_MAP_PRINT("returning success\n");
1793 return 0;
1796 /****************************************************************************/
1798 * Adds a region of memory to a memory map. Each region is virtually
1799 * contiguous, but not necessarily physically contiguous.
1801 * @return 0 on success, error code otherwise.
1803 /****************************************************************************/
1805 int dma_map_add_region(DMA_MemMap_t *memMap, /* Stores state information about the map */
1806 void *mem, /* Virtual address that we want to get a map of */
1807 size_t numBytes /* Number of bytes being mapped */
1809 unsigned long addr = (unsigned long)mem;
1810 unsigned int offset;
1811 int rc = 0;
1812 DMA_Region_t *region;
1813 dma_addr_t physAddr;
1815 down(&memMap->lock);
1817 DMA_MAP_PRINT("memMap:%p va:%p #:%d\n", memMap, mem, numBytes);
1819 if (!memMap->inUse) {
1820 printk(KERN_ERR "%s: Make sure you call dma_map_start first\n",
1821 __func__);
1822 rc = -EINVAL;
1823 goto out;
1826 /* Reallocate to hold more regions. */
1828 if (memMap->numRegionsUsed >= memMap->numRegionsAllocated) {
1829 DMA_Region_t *newRegion;
1830 size_t oldSize =
1831 memMap->numRegionsAllocated * sizeof(*newRegion);
1832 int newAlloc = memMap->numRegionsAllocated + 4;
1833 size_t newSize = newAlloc * sizeof(*newRegion);
1835 newRegion = kmalloc(newSize, GFP_KERNEL);
1836 if (newRegion == NULL) {
1837 rc = -ENOMEM;
1838 goto out;
1840 memcpy(newRegion, memMap->region, oldSize);
1841 memset(&((uint8_t *) newRegion)[oldSize], 0, newSize - oldSize);
1843 kfree(memMap->region);
1845 memMap->numRegionsAllocated = newAlloc;
1846 memMap->region = newRegion;
1849 region = &memMap->region[memMap->numRegionsUsed];
1850 memMap->numRegionsUsed++;
1852 offset = addr & ~PAGE_MASK;
1854 region->memType = dma_mem_type(mem);
1855 region->virtAddr = mem;
1856 region->numBytes = numBytes;
1857 region->numSegmentsUsed = 0;
1858 region->numLockedPages = 0;
1859 region->lockedPages = NULL;
1861 switch (region->memType) {
1862 case DMA_MEM_TYPE_VMALLOC:
1864 atomic_inc(&gDmaStatMemTypeVmalloc);
1866 /* printk(KERN_ERR "%s: vmalloc'd pages are not supported\n", __func__); */
1868 /* vmalloc'd pages are not physically contiguous */
1870 rc = -EINVAL;
1871 break;
1874 case DMA_MEM_TYPE_KMALLOC:
1876 atomic_inc(&gDmaStatMemTypeKmalloc);
1878 /* kmalloc'd pages are physically contiguous, so they'll have exactly */
1879 /* one segment */
1881 #if ALLOW_MAP_OF_KMALLOC_MEMORY
1882 physAddr =
1883 dma_map_single(NULL, mem, numBytes, memMap->dir);
1884 rc = dma_map_add_segment(memMap, region, mem, physAddr,
1885 numBytes);
1886 #else
1887 rc = -EINVAL;
1888 #endif
1889 break;
1892 case DMA_MEM_TYPE_DMA:
1894 /* dma_alloc_xxx pages are physically contiguous */
1896 atomic_inc(&gDmaStatMemTypeCoherent);
1898 physAddr = (vmalloc_to_pfn(mem) << PAGE_SHIFT) + offset;
1900 dma_sync_single_for_cpu(NULL, physAddr, numBytes,
1901 memMap->dir);
1902 rc = dma_map_add_segment(memMap, region, mem, physAddr,
1903 numBytes);
1904 break;
1907 case DMA_MEM_TYPE_USER:
1909 size_t firstPageOffset;
1910 size_t firstPageSize;
1911 struct page **pages;
1912 struct task_struct *userTask;
1914 atomic_inc(&gDmaStatMemTypeUser);
1916 #if 1
1917 /* If the pages are user pages, then the dma_mem_map_set_user_task function */
1918 /* must have been previously called. */
1920 if (memMap->userTask == NULL) {
1921 printk(KERN_ERR
1922 "%s: must call dma_mem_map_set_user_task when using user-mode memory\n",
1923 __func__);
1924 return -EINVAL;
1927 /* User pages need to be locked. */
1929 firstPageOffset =
1930 (unsigned long)region->virtAddr & (PAGE_SIZE - 1);
1931 firstPageSize = PAGE_SIZE - firstPageOffset;
1933 region->numLockedPages = (firstPageOffset
1934 + region->numBytes +
1935 PAGE_SIZE - 1) / PAGE_SIZE;
1936 pages =
1937 kmalloc(region->numLockedPages *
1938 sizeof(struct page *), GFP_KERNEL);
1940 if (pages == NULL) {
1941 region->numLockedPages = 0;
1942 return -ENOMEM;
1945 userTask = memMap->userTask;
1947 down_read(&userTask->mm->mmap_sem);
1948 rc = get_user_pages(userTask, /* task */
1949 userTask->mm, /* mm */
1950 (unsigned long)region->virtAddr, /* start */
1951 region->numLockedPages, /* len */
1952 memMap->dir == DMA_FROM_DEVICE, /* write */
1953 0, /* force */
1954 pages, /* pages (array of pointers to page) */
1955 NULL); /* vmas */
1956 up_read(&userTask->mm->mmap_sem);
1958 if (rc != region->numLockedPages) {
1959 kfree(pages);
1960 region->numLockedPages = 0;
1962 if (rc >= 0) {
1963 rc = -EINVAL;
1965 } else {
1966 uint8_t *virtAddr = region->virtAddr;
1967 size_t bytesRemaining;
1968 int pageIdx;
1970 rc = 0; /* Since get_user_pages returns +ve number */
1972 region->lockedPages = pages;
1974 /* We've locked the user pages. Now we need to walk them and figure */
1975 /* out the physical addresses. */
1977 /* The first page may be partial */
1979 dma_map_add_segment(memMap,
1980 region,
1981 virtAddr,
1982 PFN_PHYS(page_to_pfn
1983 (pages[0])) +
1984 firstPageOffset,
1985 firstPageSize);
1987 virtAddr += firstPageSize;
1988 bytesRemaining =
1989 region->numBytes - firstPageSize;
1991 for (pageIdx = 1;
1992 pageIdx < region->numLockedPages;
1993 pageIdx++) {
1994 size_t bytesThisPage =
1995 (bytesRemaining >
1996 PAGE_SIZE ? PAGE_SIZE :
1997 bytesRemaining);
1999 DMA_MAP_PRINT
2000 ("pageIdx:%d pages[pageIdx]=%p pfn=%u phys=%u\n",
2001 pageIdx, pages[pageIdx],
2002 page_to_pfn(pages[pageIdx]),
2003 PFN_PHYS(page_to_pfn
2004 (pages[pageIdx])));
2006 dma_map_add_segment(memMap,
2007 region,
2008 virtAddr,
2009 PFN_PHYS(page_to_pfn
2010 (pages
2011 [pageIdx])),
2012 bytesThisPage);
2014 virtAddr += bytesThisPage;
2015 bytesRemaining -= bytesThisPage;
2018 #else
2019 printk(KERN_ERR
2020 "%s: User mode pages are not yet supported\n",
2021 __func__);
2023 /* user pages are not physically contiguous */
2025 rc = -EINVAL;
2026 #endif
2027 break;
2030 default:
2032 printk(KERN_ERR "%s: Unsupported memory type: %d\n",
2033 __func__, region->memType);
2035 rc = -EINVAL;
2036 break;
2040 if (rc != 0) {
2041 memMap->numRegionsUsed--;
2044 out:
2046 DMA_MAP_PRINT("returning %d\n", rc);
2048 up(&memMap->lock);
2050 return rc;
2053 EXPORT_SYMBOL(dma_map_add_segment);
2055 /****************************************************************************/
2057 * Maps in a memory region such that it can be used for performing a DMA.
2059 * @return 0 on success, error code otherwise.
2061 /****************************************************************************/
2063 int dma_map_mem(DMA_MemMap_t *memMap, /* Stores state information about the map */
2064 void *mem, /* Virtual address that we want to get a map of */
2065 size_t numBytes, /* Number of bytes being mapped */
2066 enum dma_data_direction dir /* Direction that the mapping will be going */
2068 int rc;
2070 rc = dma_map_start(memMap, dir);
2071 if (rc == 0) {
2072 rc = dma_map_add_region(memMap, mem, numBytes);
2073 if (rc < 0) {
2074 /* Since the add fails, this function will fail, and the caller won't */
2075 /* call unmap, so we need to do it here. */
2077 dma_unmap(memMap, 0);
2081 return rc;
2084 EXPORT_SYMBOL(dma_map_mem);
2086 /****************************************************************************/
2088 * Setup a descriptor ring for a given memory map.
2090 * It is assumed that the descriptor ring has already been initialized, and
2091 * this routine will only reallocate a new descriptor ring if the existing
2092 * one is too small.
2094 * @return 0 on success, error code otherwise.
2096 /****************************************************************************/
2098 int dma_map_create_descriptor_ring(DMA_Device_t dev, /* DMA device (where the ring is stored) */
2099 DMA_MemMap_t *memMap, /* Memory map that will be used */
2100 dma_addr_t devPhysAddr /* Physical address of device */
2102 int rc;
2103 int numDescriptors;
2104 DMA_DeviceAttribute_t *devAttr;
2105 DMA_Region_t *region;
2106 DMA_Segment_t *segment;
2107 dma_addr_t srcPhysAddr;
2108 dma_addr_t dstPhysAddr;
2109 int regionIdx;
2110 int segmentIdx;
2112 devAttr = &DMA_gDeviceAttribute[dev];
2114 down(&memMap->lock);
2116 /* Figure out how many descriptors we need */
2118 numDescriptors = 0;
2119 for (regionIdx = 0; regionIdx < memMap->numRegionsUsed; regionIdx++) {
2120 region = &memMap->region[regionIdx];
2122 for (segmentIdx = 0; segmentIdx < region->numSegmentsUsed;
2123 segmentIdx++) {
2124 segment = &region->segment[segmentIdx];
2126 if (memMap->dir == DMA_TO_DEVICE) {
2127 srcPhysAddr = segment->physAddr;
2128 dstPhysAddr = devPhysAddr;
2129 } else {
2130 srcPhysAddr = devPhysAddr;
2131 dstPhysAddr = segment->physAddr;
2134 rc =
2135 dma_calculate_descriptor_count(dev, srcPhysAddr,
2136 dstPhysAddr,
2137 segment->
2138 numBytes);
2139 if (rc < 0) {
2140 printk(KERN_ERR
2141 "%s: dma_calculate_descriptor_count failed: %d\n",
2142 __func__, rc);
2143 goto out;
2145 numDescriptors += rc;
2149 /* Adjust the size of the ring, if it isn't big enough */
2151 if (numDescriptors > devAttr->ring.descriptorsAllocated) {
2152 dma_free_descriptor_ring(&devAttr->ring);
2153 rc =
2154 dma_alloc_descriptor_ring(&devAttr->ring,
2155 numDescriptors);
2156 if (rc < 0) {
2157 printk(KERN_ERR
2158 "%s: dma_alloc_descriptor_ring failed: %d\n",
2159 __func__, rc);
2160 goto out;
2162 } else {
2163 rc =
2164 dma_init_descriptor_ring(&devAttr->ring,
2165 numDescriptors);
2166 if (rc < 0) {
2167 printk(KERN_ERR
2168 "%s: dma_init_descriptor_ring failed: %d\n",
2169 __func__, rc);
2170 goto out;
2174 /* Populate the descriptors */
2176 for (regionIdx = 0; regionIdx < memMap->numRegionsUsed; regionIdx++) {
2177 region = &memMap->region[regionIdx];
2179 for (segmentIdx = 0; segmentIdx < region->numSegmentsUsed;
2180 segmentIdx++) {
2181 segment = &region->segment[segmentIdx];
2183 if (memMap->dir == DMA_TO_DEVICE) {
2184 srcPhysAddr = segment->physAddr;
2185 dstPhysAddr = devPhysAddr;
2186 } else {
2187 srcPhysAddr = devPhysAddr;
2188 dstPhysAddr = segment->physAddr;
2191 rc =
2192 dma_add_descriptors(&devAttr->ring, dev,
2193 srcPhysAddr, dstPhysAddr,
2194 segment->numBytes);
2195 if (rc < 0) {
2196 printk(KERN_ERR
2197 "%s: dma_add_descriptors failed: %d\n",
2198 __func__, rc);
2199 goto out;
2204 rc = 0;
2206 out:
2208 up(&memMap->lock);
2209 return rc;
2212 EXPORT_SYMBOL(dma_map_create_descriptor_ring);
2214 /****************************************************************************/
2216 * Maps in a memory region such that it can be used for performing a DMA.
2218 * @return
2220 /****************************************************************************/
2222 int dma_unmap(DMA_MemMap_t *memMap, /* Stores state information about the map */
2223 int dirtied /* non-zero if any of the pages were modified */
2226 int rc = 0;
2227 int regionIdx;
2228 int segmentIdx;
2229 DMA_Region_t *region;
2230 DMA_Segment_t *segment;
2232 down(&memMap->lock);
2234 for (regionIdx = 0; regionIdx < memMap->numRegionsUsed; regionIdx++) {
2235 region = &memMap->region[regionIdx];
2237 for (segmentIdx = 0; segmentIdx < region->numSegmentsUsed;
2238 segmentIdx++) {
2239 segment = &region->segment[segmentIdx];
2241 switch (region->memType) {
2242 case DMA_MEM_TYPE_VMALLOC:
2244 printk(KERN_ERR
2245 "%s: vmalloc'd pages are not yet supported\n",
2246 __func__);
2247 rc = -EINVAL;
2248 goto out;
2251 case DMA_MEM_TYPE_KMALLOC:
2253 #if ALLOW_MAP_OF_KMALLOC_MEMORY
2254 dma_unmap_single(NULL,
2255 segment->physAddr,
2256 segment->numBytes,
2257 memMap->dir);
2258 #endif
2259 break;
2262 case DMA_MEM_TYPE_DMA:
2264 dma_sync_single_for_cpu(NULL,
2265 segment->
2266 physAddr,
2267 segment->
2268 numBytes,
2269 memMap->dir);
2270 break;
2273 case DMA_MEM_TYPE_USER:
2275 /* Nothing to do here. */
2277 break;
2280 default:
2282 printk(KERN_ERR
2283 "%s: Unsupported memory type: %d\n",
2284 __func__, region->memType);
2285 rc = -EINVAL;
2286 goto out;
2290 segment->virtAddr = NULL;
2291 segment->physAddr = 0;
2292 segment->numBytes = 0;
2295 if (region->numLockedPages > 0) {
2296 int pageIdx;
2298 /* Some user pages were locked. We need to go and unlock them now. */
2300 for (pageIdx = 0; pageIdx < region->numLockedPages;
2301 pageIdx++) {
2302 struct page *page =
2303 region->lockedPages[pageIdx];
2305 if (memMap->dir == DMA_FROM_DEVICE) {
2306 SetPageDirty(page);
2308 page_cache_release(page);
2310 kfree(region->lockedPages);
2311 region->numLockedPages = 0;
2312 region->lockedPages = NULL;
2315 region->memType = DMA_MEM_TYPE_NONE;
2316 region->virtAddr = NULL;
2317 region->numBytes = 0;
2318 region->numSegmentsUsed = 0;
2320 memMap->userTask = NULL;
2321 memMap->numRegionsUsed = 0;
2322 memMap->inUse = 0;
2324 out:
2325 up(&memMap->lock);
2327 return rc;
2330 EXPORT_SYMBOL(dma_unmap);