Staging: hv: mousevsc: Cleanup alloc_input_device()
[zen-stable.git] / arch / arm / mach-bcmring / dma.c
blob0ca00050666aa56cc920bc1f81f0af3196bb234a
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/irqreturn.h>
30 #include <linux/proc_fs.h>
31 #include <linux/slab.h>
33 #include <mach/timer.h>
35 #include <linux/mm.h>
36 #include <linux/pfn.h>
37 #include <linux/atomic.h>
38 #include <mach/dma.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
56 #if DMA_MAP_DEBUG
57 # define DMA_MAP_PRINT(fmt, args...) printk("%s: " fmt, __func__, ## args)
58 #else
59 # define DMA_MAP_PRINT(fmt, args...)
60 #endif
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 /****************************************************************************/
79 /**
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)
87 int len = 0;
89 len += sprintf(buf + len, "dma_map_mem statistics\n");
90 len +=
91 sprintf(buf + len, "coherent: %d\n",
92 atomic_read(&gDmaStatMemTypeCoherent));
93 len +=
94 sprintf(buf + len, "kmalloc: %d\n",
95 atomic_read(&gDmaStatMemTypeKmalloc));
96 len +=
97 sprintf(buf + len, "vmalloc: %d\n",
98 atomic_read(&gDmaStatMemTypeVmalloc));
99 len +=
100 sprintf(buf + len, "user: %d\n",
101 atomic_read(&gDmaStatMemTypeUser));
103 return len;
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)
115 int controllerIdx;
116 int channelIdx;
117 int limit = count - 200;
118 int len = 0;
119 DMA_Channel_t *channel;
121 if (down_interruptible(&gDMA.lock) < 0) {
122 return -ERESTARTSYS;
125 for (controllerIdx = 0; controllerIdx < DMA_NUM_CONTROLLERS;
126 controllerIdx++) {
127 for (channelIdx = 0; channelIdx < DMA_NUM_CHANNELS;
128 channelIdx++) {
129 if (len >= limit) {
130 break;
133 channel =
134 &gDMA.controller[controllerIdx].channel[channelIdx];
136 len +=
137 sprintf(buf + len, "%d:%d ", controllerIdx,
138 channelIdx);
140 if ((channel->flags & DMA_CHANNEL_FLAG_IS_DEDICATED) !=
141 0) {
142 len +=
143 sprintf(buf + len, "Dedicated for %s ",
144 DMA_gDeviceAttribute[channel->
145 devType].name);
146 } else {
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 ");
156 } else {
157 len += sprintf(buf + len, "Fifo: 64 ");
160 if ((channel->flags & DMA_CHANNEL_FLAG_IN_USE) != 0) {
161 len +=
162 sprintf(buf + len, "InUse by %s",
163 DMA_gDeviceAttribute[channel->
164 devType].name);
165 #if (DMA_DEBUG_TRACK_RESERVATION)
166 len +=
167 sprintf(buf + len, " (%s:%d)",
168 channel->fileName,
169 channel->lineNum);
170 #endif
171 } else {
172 len += sprintf(buf + len, "Avail ");
175 if (channel->lastDevType != DMA_DEVICE_NONE) {
176 len +=
177 sprintf(buf + len, "Last use: %s ",
178 DMA_gDeviceAttribute[channel->
179 lastDevType].
180 name);
183 len += sprintf(buf + len, "\n");
186 up(&gDMA.lock);
187 *eof = 1;
189 return len;
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;
202 int len = 0;
203 int devIdx;
205 if (down_interruptible(&gDMA.lock) < 0) {
206 return -ERESTARTSYS;
209 for (devIdx = 0; devIdx < DMA_NUM_DEVICE_ENTRIES; devIdx++) {
210 DMA_DeviceAttribute_t *devAttr = &DMA_gDeviceAttribute[devIdx];
212 if (devAttr->name == NULL) {
213 continue;
216 if (len >= limit) {
217 break;
220 len += sprintf(buf + len, "%-12s ", devAttr->name);
222 if ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) != 0) {
223 len +=
224 sprintf(buf + len, "Dedicated %d:%d ",
225 devAttr->dedicatedController,
226 devAttr->dedicatedChannel);
227 } else {
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 ");
244 len +=
245 sprintf(buf + len,
246 "Xfer #: %Lu Ticks: %Lu Bytes: %Lu DescLen: %u\n",
247 devAttr->numTransfers, devAttr->transferTicks,
248 devAttr->transferBytes,
249 devAttr->ring.bytesAllocated);
253 up(&gDMA.lock);
254 *eof = 1;
256 return len;
259 /****************************************************************************/
261 * Determines if a DMA_Device_t is "valid".
263 * @return
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.
278 * @return
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)
286 int controllerIdx;
287 int channelIdx;
289 controllerIdx = CONTROLLER_FROM_HANDLE(handle);
290 channelIdx = CHANNEL_FROM_HANDLE(handle);
292 if ((controllerIdx > DMA_NUM_CONTROLLERS)
293 || (channelIdx > DMA_NUM_CHANNELS)) {
294 return NULL;
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;
309 int irqStatus;
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",
321 channel->devType);
322 return IRQ_NONE;
324 devAttr = &DMA_gDeviceAttribute[channel->devType];
326 /* Update stats */
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) {
334 printk(KERN_ERR
335 "dma_interrupt_handler: devType :%d DMA error (%s)\n",
336 channel->devType, devAttr->name);
337 } else {
338 devAttr->numTransfers++;
339 devAttr->transferBytes += devAttr->numBytes;
342 /* Call any installed handler */
344 if (devAttr->devHandler != NULL) {
345 devAttr->devHandler(channel->devType, irqStatus,
346 devAttr->userData);
349 return IRQ_HANDLED;
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.
360 * @return
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)) {
373 return -EINVAL;
376 ring->physAddr = 0;
377 ring->descriptorsAllocated = 0;
378 ring->bytesAllocated = 0;
380 ring->virtAddr = dma_alloc_writecombine(NULL,
381 bytesToAlloc,
382 &ring->physAddr,
383 GFP_KERNEL);
384 if (ring->virtAddr == NULL) {
385 return -ENOMEM;
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;
413 ring->physAddr = 0;
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
429 * @return
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) {
440 return -EINVAL;
442 if (dmacHw_initDescriptor(ring->virtAddr,
443 ring->physAddr,
444 ring->bytesAllocated, numDescriptors) < 0) {
445 printk(KERN_ERR
446 "dma_init_descriptor_ring: dmacHw_initDescriptor failed\n");
447 return -ENOMEM;
450 return 0;
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
464 * required.
466 * @return
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 */
479 int numDescriptors;
480 DMA_DeviceAttribute_t *devAttr;
482 if (!IsDeviceValid(device)) {
483 return -ENODEV;
485 devAttr = &DMA_gDeviceAttribute[device];
487 numDescriptors = dmacHw_calculateDescriptorCount(&devAttr->config,
488 (void *)srcData,
489 (void *)dstData,
490 numBytes);
491 if (numDescriptors < 0) {
492 printk(KERN_ERR
493 "dma_calculate_descriptor_count: dmacHw_calculateDescriptorCount failed\n");
494 return -EINVAL;
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.
508 * @return
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 */
522 int rc;
523 DMA_DeviceAttribute_t *devAttr;
525 if (!IsDeviceValid(device)) {
526 return -ENODEV;
528 devAttr = &DMA_gDeviceAttribute[device];
530 rc = dmacHw_setDataDescriptor(&devAttr->config,
531 ring->virtAddr,
532 (void *)srcData,
533 (void *)dstData, numBytes);
534 if (rc < 0) {
535 printk(KERN_ERR
536 "dma_add_descriptors: dmacHw_setDataDescriptor failed with code: %d\n",
537 rc);
538 return -ENOMEM;
541 return 0;
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.
560 * @return
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)) {
572 return -ENODEV;
574 devAttr = &DMA_gDeviceAttribute[device];
576 /* Free the previously allocated descriptor ring */
578 dma_free_descriptor_ring(&devAttr->ring);
580 if (ring != NULL) {
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;
593 return 0;
596 EXPORT_SYMBOL(dma_set_device_descriptor_ring);
598 /****************************************************************************/
600 * Retrieves the descriptor ring associated with a device.
602 * @return
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)) {
616 return -ENODEV;
618 devAttr = &DMA_gDeviceAttribute[device];
620 *ring = devAttr->ring;
622 return 0;
625 EXPORT_SYMBOL(dma_get_device_descriptor_ring);
627 /****************************************************************************/
629 * Configures a DMA channel.
631 * @return
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;
643 int controllerIdx;
645 channel = HandleToChannel(handle);
646 if (channel == NULL) {
647 return -ENODEV;
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");
666 return -EIO;
669 return 0;
672 /****************************************************************************/
674 * Initializes all of the data structures associated with the DMA.
675 * @return
676 * >= 0 - Initialization was successful.
678 * -EBUSY - Device is currently being used.
679 * -ENODEV - Device handed in is invalid.
681 /****************************************************************************/
683 int dma_init(void)
685 int rc = 0;
686 int controllerIdx;
687 int channelIdx;
688 DMA_Device_t devIdx;
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 */
699 dmacHw_initDma();
701 /* Start off by marking all of the DMA channels as shared. */
703 for (controllerIdx = 0; controllerIdx < DMA_NUM_CONTROLLERS;
704 controllerIdx++) {
705 for (channelIdx = 0; channelIdx < DMA_NUM_CHANNELS;
706 channelIdx++) {
707 channel =
708 &gDMA.controller[controllerIdx].channel[channelIdx];
710 channel->flags = 0;
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;
717 #endif
719 channel->dmacHwHandle =
720 dmacHw_getChannelHandle(dmacHw_MAKE_CHANNEL_ID
721 (controllerIdx,
722 channelIdx));
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)) {
741 printk(KERN_ERR
742 "DMA Device: %s Can only request NO_ISR for dedicated devices\n",
743 devAttr->name);
744 rc = -EINVAL;
745 goto out;
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) {
752 printk(KERN_ERR
753 "DMA Device: %s DMA Controller %d is out of range\n",
754 devAttr->name,
755 devAttr->dedicatedController);
756 rc = -EINVAL;
757 goto out;
760 if (devAttr->dedicatedChannel >= DMA_NUM_CHANNELS) {
761 printk(KERN_ERR
762 "DMA Device: %s DMA Channel %d is out of range\n",
763 devAttr->name,
764 devAttr->dedicatedChannel);
765 rc = -EINVAL;
766 goto out;
769 dedicatedHandle =
770 MAKE_HANDLE(devAttr->dedicatedController,
771 devAttr->dedicatedChannel);
772 channel = HandleToChannel(dedicatedHandle);
774 if ((channel->flags & DMA_CHANNEL_FLAG_IS_DEDICATED) !=
775 0) {
776 printk
777 ("DMA Device: %s attempting to use same DMA Controller:Channel (%d:%d) as %s\n",
778 devAttr->name,
779 devAttr->dedicatedController,
780 devAttr->dedicatedChannel,
781 DMA_gDeviceAttribute[channel->devType].
782 name);
783 rc = -EBUSY;
784 goto out;
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 */
795 /* as well. */
797 ConfigChannel(dedicatedHandle);
801 /* Go through and register the interrupt handlers */
803 for (controllerIdx = 0; controllerIdx < DMA_NUM_CONTROLLERS;
804 controllerIdx++) {
805 for (channelIdx = 0; channelIdx < DMA_NUM_CHANNELS;
806 channelIdx++) {
807 channel =
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,
813 channelIdx,
814 channel->devType ==
815 DMA_DEVICE_NONE ? "" :
816 DMA_gDeviceAttribute[channel->devType].
817 name);
819 rc =
820 request_irq(IRQ_DMA0C0 +
821 (controllerIdx *
822 DMA_NUM_CHANNELS) +
823 channelIdx,
824 dma_interrupt_handler,
825 IRQF_DISABLED, channel->name,
826 channel);
827 if (rc != 0) {
828 printk(KERN_ERR
829 "request_irq for IRQ_DMA%dC%d failed\n",
830 controllerIdx, channelIdx);
836 /* Create /proc/dma/channels and /proc/dma/devices */
838 gDmaDir = proc_mkdir("dma", NULL);
840 if (gDmaDir == NULL) {
841 printk(KERN_ERR "Unable to create /proc/dma\n");
842 } else {
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);
851 out:
853 up(&gDMA.lock);
855 return rc;
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
862 * becomes available.
864 * @return
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)
874 #else
875 DMA_Handle_t dma_request_channel(DMA_Device_t dev)
876 #endif
878 DMA_Handle_t handle;
879 DMA_DeviceAttribute_t *devAttr;
880 DMA_Channel_t *channel;
881 int controllerIdx;
882 int controllerIdx2;
883 int channelIdx;
885 if (down_interruptible(&gDMA.lock) < 0) {
886 return -ERESTARTSYS;
889 if ((dev < 0) || (dev >= DMA_NUM_DEVICE_ENTRIES)) {
890 handle = -ENODEV;
891 goto out;
893 devAttr = &DMA_gDeviceAttribute[dev];
895 #if (DMA_DEBUG_TRACK_RESERVATION)
897 char *s;
899 s = strrchr(fileName, '/');
900 if (s != NULL) {
901 fileName = s + 1;
904 #endif
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);
910 handle = -EBUSY;
911 goto out;
914 if ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) != 0) {
915 /* This device has a dedicated channel. */
917 channel =
918 &gDMA.controller[devAttr->dedicatedController].
919 channel[devAttr->dedicatedChannel];
920 if ((channel->flags & DMA_CHANNEL_FLAG_IN_USE) != 0) {
921 handle = -EBUSY;
922 goto out;
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;
931 #endif
932 handle =
933 MAKE_HANDLE(devAttr->dedicatedController,
934 devAttr->dedicatedChannel);
935 goto out;
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;
945 controllerIdx2++) {
946 /* Check to see if we should try on controller 1 first. */
948 controllerIdx = controllerIdx2;
949 if ((devAttr->
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 */
956 if ((devAttr->
957 flags & (DMA_DEVICE_FLAG_ON_DMA0 << controllerIdx))
958 != 0) {
959 for (channelIdx = 0;
960 channelIdx < DMA_NUM_CHANNELS;
961 channelIdx++) {
962 channel =
963 &gDMA.controller[controllerIdx].
964 channel[channelIdx];
966 if (((channel->
967 flags &
968 DMA_CHANNEL_FLAG_IS_DEDICATED) ==
971 ((channel->
972 flags & DMA_CHANNEL_FLAG_IN_USE)
973 == 0)) {
974 if (((channel->
975 flags &
976 DMA_CHANNEL_FLAG_LARGE_FIFO)
977 != 0)
979 ((devAttr->
980 flags &
981 DMA_DEVICE_FLAG_ALLOW_LARGE_FIFO)
982 == 0)) {
983 /* This channel is a large fifo - don't tie it up */
984 /* with devices that we don't want using it. */
986 continue;
989 channel->flags |=
990 DMA_CHANNEL_FLAG_IN_USE;
991 channel->devType = dev;
992 devAttr->flags |=
993 DMA_DEVICE_FLAG_IN_USE;
995 #if (DMA_DEBUG_TRACK_RESERVATION)
996 channel->fileName = fileName;
997 channel->lineNum = lineNum;
998 #endif
999 handle =
1000 MAKE_HANDLE(controllerIdx,
1001 channelIdx);
1003 /* Now that we've reserved the channel - we can go ahead and configure it */
1005 if (ConfigChannel(handle) != 0) {
1006 handle = -EIO;
1007 printk(KERN_ERR
1008 "dma_request_channel: ConfigChannel failed\n");
1010 goto out;
1016 /* No channels are currently available. Let's wait for one to free up. */
1019 DEFINE_WAIT(wait);
1021 prepare_to_wait(&gDMA.freeChannelQ, &wait,
1022 TASK_INTERRUPTIBLE);
1023 up(&gDMA.lock);
1024 schedule();
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;
1039 out:
1040 up(&gDMA.lock);
1042 return handle;
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);
1055 #endif
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. */
1066 int rc = 0;
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) {
1076 rc = -EINVAL;
1077 goto out;
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;
1089 out:
1090 up(&gDMA.lock);
1092 wake_up_interruptible(&gDMA.freeChannelQ);
1094 return rc;
1097 EXPORT_SYMBOL(dma_free_channel);
1099 /****************************************************************************/
1101 * Determines if a given device has been configured as using a shared
1102 * channel.
1104 * @return
1105 * 0 Device uses a dedicated channel
1106 * > zero Device uses a shared channel
1107 * < zero Error code
1109 /****************************************************************************/
1111 int dma_device_is_channel_shared(DMA_Device_t device /* Device to check. */
1113 DMA_DeviceAttribute_t *devAttr;
1115 if (!IsDeviceValid(device)) {
1116 return -ENODEV;
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
1129 * context.
1131 * @return
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;
1147 int numDescriptors;
1148 size_t ringBytesRequired;
1149 int rc = 0;
1151 channel = HandleToChannel(handle);
1152 if (channel == NULL) {
1153 return -ENODEV;
1156 devAttr = &DMA_gDeviceAttribute[channel->devType];
1158 if (devAttr->config.transferType != transferType) {
1159 return -EINVAL;
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,
1168 (void *)srcData,
1169 (void *)dstData,
1170 numBytes);
1171 if (numDescriptors < 0) {
1172 printk(KERN_ERR "%s: dmacHw_calculateDescriptorCount failed\n",
1173 __func__);
1174 return -EINVAL;
1177 /* Check to see if we can reuse the existing descriptor ring, or if we need to allocate */
1178 /* a new one. */
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. */
1189 might_sleep();
1191 /* Free the old descriptor ring and allocate a new one. */
1193 dma_free_descriptor_ring(&devAttr->ring);
1195 /* And allocate a new one. */
1197 rc =
1198 dma_alloc_descriptor_ring(&devAttr->ring,
1199 numDescriptors);
1200 if (rc < 0) {
1201 printk(KERN_ERR
1202 "%s: dma_alloc_descriptor_ring(%d) failed\n",
1203 __func__, numDescriptors);
1204 return rc;
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",
1213 __func__);
1214 return -EINVAL;
1216 } else {
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,
1228 (void *)srcData,
1229 (void *)dstData, numBytes) < 0) {
1230 printk(KERN_ERR "%s: dmacHw_setDataDescriptor failed\n",
1231 __func__);
1232 return -EINVAL;
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;
1242 return 0;
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.
1254 * @return
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;
1272 int numDescriptors;
1273 size_t ringBytesRequired;
1274 int rc = 0;
1276 channel = HandleToChannel(handle);
1277 if (channel == NULL) {
1278 return -ENODEV;
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) {
1292 return -EINVAL;
1294 numDst2Descriptors =
1295 dmacHw_calculateDescriptorCount(&devAttr->config, (void *)srcData,
1296 (void *)dstData2, numBytes);
1297 if (numDst2Descriptors < 0) {
1298 return -EINVAL;
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 */
1304 /* a new one. */
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. */
1315 might_sleep();
1317 /* Free the old descriptor ring and allocate a new one. */
1319 dma_free_descriptor_ring(&devAttr->ring);
1321 /* And allocate a new one. */
1323 rc =
1324 dma_alloc_descriptor_ring(&devAttr->ring,
1325 numDescriptors);
1326 if (rc < 0) {
1327 printk(KERN_ERR
1328 "%s: dma_alloc_descriptor_ring(%d) failed\n",
1329 __func__, ringBytesRequired);
1330 return rc;
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__);
1343 return -EINVAL;
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,
1351 (void *)srcData,
1352 (void *)dstData1, numBytes) < 0) {
1353 printk(KERN_ERR "%s: dmacHw_setDataDescriptor 1 failed\n",
1354 __func__);
1355 return -EINVAL;
1357 if (dmacHw_setDataDescriptor(&devAttr->config,
1358 devAttr->ring.virtAddr,
1359 (void *)srcData,
1360 (void *)dstData2, numBytes) < 0) {
1361 printk(KERN_ERR "%s: dmacHw_setDataDescriptor 2 failed\n",
1362 __func__);
1363 return -EINVAL;
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
1383 * be used.
1385 * @return
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) {
1398 return -ENODEV;
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 */
1407 return 0;
1410 EXPORT_SYMBOL(dma_start_transfer);
1412 /****************************************************************************/
1414 * Stops a previously started DMA transfer.
1416 * @return
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) {
1428 return -ENODEV;
1431 dmacHw_stopTransfer(channel->dmacHwHandle);
1433 return 0;
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) {
1452 return -ENODEV;
1455 while ((status =
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__);
1463 return -EIO;
1465 return 0;
1468 EXPORT_SYMBOL(dma_wait_transfer_done);
1470 /****************************************************************************/
1472 * Initiates a DMA, allocating the descriptors as required.
1474 * @return
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;
1489 int rc = 0;
1491 channel = HandleToChannel(handle);
1492 if (channel == NULL) {
1493 return -ENODEV;
1496 devAttr = &DMA_gDeviceAttribute[channel->devType];
1498 if (devAttr->config.transferType != transferType) {
1499 return -EINVAL;
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. */
1507 rc =
1508 dma_alloc_descriptors(handle, transferType, srcData,
1509 dstData, numBytes);
1510 if (rc != 0) {
1511 return rc;
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 */
1525 return 0;
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.
1537 * @return
1538 * 0 - Success
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)) {
1551 return -ENODEV;
1553 devAttr = &DMA_gDeviceAttribute[dev];
1555 local_irq_save(flags);
1557 devAttr->userData = userData;
1558 devAttr->devHandler = devHandler;
1560 local_irq_restore(flags);
1562 return 0;
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);
1579 return 0;
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 */
1596 up(&memMap->lock);
1597 memset(memMap, 0, sizeof(*memMap));
1599 return 0;
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.
1653 * @return boolean -
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)
1666 #endif
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.
1676 * @return
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 */
1683 int rc;
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",
1691 __func__, memMap);
1692 rc = -EBUSY;
1693 goto out;
1696 memMap->inUse = 1;
1697 memMap->dir = dir;
1698 memMap->numRegionsUsed = 0;
1700 rc = 0;
1702 out:
1704 DMA_MAP_PRINT("returning %d", rc);
1706 up(&memMap->lock);
1708 return 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);
1733 /* Sanity check */
1735 if (((unsigned long)virtAddr < (unsigned long)region->virtAddr)
1736 || (((unsigned long)virtAddr + numBytes)) >
1737 ((unsigned long)region->virtAddr + region->numBytes)) {
1738 printk(KERN_ERR
1739 "%s: virtAddr %p is outside region @ %p len: %d\n",
1740 __func__, virtAddr, region->virtAddr, region->numBytes);
1741 return -EINVAL;
1744 if (region->numSegmentsUsed > 0) {
1745 /* Check to see if this segment is physically contiguous with the previous one */
1747 segment = &region->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",
1753 numBytes);
1755 segment->numBytes += numBytes;
1757 return 0;
1761 /* Reallocate to hold more segments, if required. */
1763 if (region->numSegmentsUsed >= region->numSegmentsAllocated) {
1764 DMA_Segment_t *newSegment;
1765 size_t oldSize =
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) {
1772 return -ENOMEM;
1774 memcpy(newSegment, region->segment, oldSize);
1775 memset(&((uint8_t *) newSegment)[oldSize], 0,
1776 newSize - oldSize);
1777 kfree(region->segment);
1779 region->numSegmentsAllocated = newAlloc;
1780 region->segment = newSegment;
1783 segment = &region->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");
1792 return 0;
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;
1810 int rc = 0;
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",
1820 __func__);
1821 rc = -EINVAL;
1822 goto out;
1825 /* Reallocate to hold more regions. */
1827 if (memMap->numRegionsUsed >= memMap->numRegionsAllocated) {
1828 DMA_Region_t *newRegion;
1829 size_t oldSize =
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) {
1836 rc = -ENOMEM;
1837 goto out;
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 */
1869 rc = -EINVAL;
1870 break;
1873 case DMA_MEM_TYPE_KMALLOC:
1875 atomic_inc(&gDmaStatMemTypeKmalloc);
1877 /* kmalloc'd pages are physically contiguous, so they'll have exactly */
1878 /* one segment */
1880 #if ALLOW_MAP_OF_KMALLOC_MEMORY
1881 physAddr =
1882 dma_map_single(NULL, mem, numBytes, memMap->dir);
1883 rc = dma_map_add_segment(memMap, region, mem, physAddr,
1884 numBytes);
1885 #else
1886 rc = -EINVAL;
1887 #endif
1888 break;
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,
1900 memMap->dir);
1901 rc = dma_map_add_segment(memMap, region, mem, physAddr,
1902 numBytes);
1903 break;
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);
1915 #if 1
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) {
1920 printk(KERN_ERR
1921 "%s: must call dma_mem_map_set_user_task when using user-mode memory\n",
1922 __func__);
1923 return -EINVAL;
1926 /* User pages need to be locked. */
1928 firstPageOffset =
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;
1935 pages =
1936 kmalloc(region->numLockedPages *
1937 sizeof(struct page *), GFP_KERNEL);
1939 if (pages == NULL) {
1940 region->numLockedPages = 0;
1941 return -ENOMEM;
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 */
1952 0, /* force */
1953 pages, /* pages (array of pointers to page) */
1954 NULL); /* vmas */
1955 up_read(&userTask->mm->mmap_sem);
1957 if (rc != region->numLockedPages) {
1958 kfree(pages);
1959 region->numLockedPages = 0;
1961 if (rc >= 0) {
1962 rc = -EINVAL;
1964 } else {
1965 uint8_t *virtAddr = region->virtAddr;
1966 size_t bytesRemaining;
1967 int pageIdx;
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,
1979 region,
1980 virtAddr,
1981 PFN_PHYS(page_to_pfn
1982 (pages[0])) +
1983 firstPageOffset,
1984 firstPageSize);
1986 virtAddr += firstPageSize;
1987 bytesRemaining =
1988 region->numBytes - firstPageSize;
1990 for (pageIdx = 1;
1991 pageIdx < region->numLockedPages;
1992 pageIdx++) {
1993 size_t bytesThisPage =
1994 (bytesRemaining >
1995 PAGE_SIZE ? PAGE_SIZE :
1996 bytesRemaining);
1998 DMA_MAP_PRINT
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
2003 (pages[pageIdx])));
2005 dma_map_add_segment(memMap,
2006 region,
2007 virtAddr,
2008 PFN_PHYS(page_to_pfn
2009 (pages
2010 [pageIdx])),
2011 bytesThisPage);
2013 virtAddr += bytesThisPage;
2014 bytesRemaining -= bytesThisPage;
2017 #else
2018 printk(KERN_ERR
2019 "%s: User mode pages are not yet supported\n",
2020 __func__);
2022 /* user pages are not physically contiguous */
2024 rc = -EINVAL;
2025 #endif
2026 break;
2029 default:
2031 printk(KERN_ERR "%s: Unsupported memory type: %d\n",
2032 __func__, region->memType);
2034 rc = -EINVAL;
2035 break;
2039 if (rc != 0) {
2040 memMap->numRegionsUsed--;
2043 out:
2045 DMA_MAP_PRINT("returning %d\n", rc);
2047 up(&memMap->lock);
2049 return 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 */
2067 int rc;
2069 rc = dma_map_start(memMap, dir);
2070 if (rc == 0) {
2071 rc = dma_map_add_region(memMap, mem, numBytes);
2072 if (rc < 0) {
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);
2080 return rc;
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
2091 * one is too small.
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 */
2101 int rc;
2102 int numDescriptors;
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;
2108 int regionIdx;
2109 int segmentIdx;
2111 devAttr = &DMA_gDeviceAttribute[dev];
2113 down(&memMap->lock);
2115 /* Figure out how many descriptors we need */
2117 numDescriptors = 0;
2118 for (regionIdx = 0; regionIdx < memMap->numRegionsUsed; regionIdx++) {
2119 region = &memMap->region[regionIdx];
2121 for (segmentIdx = 0; segmentIdx < region->numSegmentsUsed;
2122 segmentIdx++) {
2123 segment = &region->segment[segmentIdx];
2125 if (memMap->dir == DMA_TO_DEVICE) {
2126 srcPhysAddr = segment->physAddr;
2127 dstPhysAddr = devPhysAddr;
2128 } else {
2129 srcPhysAddr = devPhysAddr;
2130 dstPhysAddr = segment->physAddr;
2133 rc =
2134 dma_calculate_descriptor_count(dev, srcPhysAddr,
2135 dstPhysAddr,
2136 segment->
2137 numBytes);
2138 if (rc < 0) {
2139 printk(KERN_ERR
2140 "%s: dma_calculate_descriptor_count failed: %d\n",
2141 __func__, rc);
2142 goto out;
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);
2152 rc =
2153 dma_alloc_descriptor_ring(&devAttr->ring,
2154 numDescriptors);
2155 if (rc < 0) {
2156 printk(KERN_ERR
2157 "%s: dma_alloc_descriptor_ring failed: %d\n",
2158 __func__, rc);
2159 goto out;
2161 } else {
2162 rc =
2163 dma_init_descriptor_ring(&devAttr->ring,
2164 numDescriptors);
2165 if (rc < 0) {
2166 printk(KERN_ERR
2167 "%s: dma_init_descriptor_ring failed: %d\n",
2168 __func__, rc);
2169 goto out;
2173 /* Populate the descriptors */
2175 for (regionIdx = 0; regionIdx < memMap->numRegionsUsed; regionIdx++) {
2176 region = &memMap->region[regionIdx];
2178 for (segmentIdx = 0; segmentIdx < region->numSegmentsUsed;
2179 segmentIdx++) {
2180 segment = &region->segment[segmentIdx];
2182 if (memMap->dir == DMA_TO_DEVICE) {
2183 srcPhysAddr = segment->physAddr;
2184 dstPhysAddr = devPhysAddr;
2185 } else {
2186 srcPhysAddr = devPhysAddr;
2187 dstPhysAddr = segment->physAddr;
2190 rc =
2191 dma_add_descriptors(&devAttr->ring, dev,
2192 srcPhysAddr, dstPhysAddr,
2193 segment->numBytes);
2194 if (rc < 0) {
2195 printk(KERN_ERR
2196 "%s: dma_add_descriptors failed: %d\n",
2197 __func__, rc);
2198 goto out;
2203 rc = 0;
2205 out:
2207 up(&memMap->lock);
2208 return rc;
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.
2217 * @return
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 */
2225 int rc = 0;
2226 int regionIdx;
2227 int segmentIdx;
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;
2237 segmentIdx++) {
2238 segment = &region->segment[segmentIdx];
2240 switch (region->memType) {
2241 case DMA_MEM_TYPE_VMALLOC:
2243 printk(KERN_ERR
2244 "%s: vmalloc'd pages are not yet supported\n",
2245 __func__);
2246 rc = -EINVAL;
2247 goto out;
2250 case DMA_MEM_TYPE_KMALLOC:
2252 #if ALLOW_MAP_OF_KMALLOC_MEMORY
2253 dma_unmap_single(NULL,
2254 segment->physAddr,
2255 segment->numBytes,
2256 memMap->dir);
2257 #endif
2258 break;
2261 case DMA_MEM_TYPE_DMA:
2263 dma_sync_single_for_cpu(NULL,
2264 segment->
2265 physAddr,
2266 segment->
2267 numBytes,
2268 memMap->dir);
2269 break;
2272 case DMA_MEM_TYPE_USER:
2274 /* Nothing to do here. */
2276 break;
2279 default:
2281 printk(KERN_ERR
2282 "%s: Unsupported memory type: %d\n",
2283 __func__, region->memType);
2284 rc = -EINVAL;
2285 goto out;
2289 segment->virtAddr = NULL;
2290 segment->physAddr = 0;
2291 segment->numBytes = 0;
2294 if (region->numLockedPages > 0) {
2295 int pageIdx;
2297 /* Some user pages were locked. We need to go and unlock them now. */
2299 for (pageIdx = 0; pageIdx < region->numLockedPages;
2300 pageIdx++) {
2301 struct page *page =
2302 region->lockedPages[pageIdx];
2304 if (memMap->dir == DMA_FROM_DEVICE) {
2305 SetPageDirty(page);
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;
2321 memMap->inUse = 0;
2323 out:
2324 up(&memMap->lock);
2326 return rc;
2329 EXPORT_SYMBOL(dma_unmap);