dma.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330
  1. /*****************************************************************************
  2. * Copyright 2004 - 2008 Broadcom Corporation. All rights reserved.
  3. *
  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").
  8. *
  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. *****************************************************************************/
  14. /****************************************************************************/
  15. /**
  16. * @file dma.c
  17. *
  18. * @brief Implements the DMA interface.
  19. */
  20. /****************************************************************************/
  21. /* ---- Include Files ---------------------------------------------------- */
  22. #include <linux/module.h>
  23. #include <linux/device.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/sched.h>
  27. #include <linux/irqreturn.h>
  28. #include <linux/proc_fs.h>
  29. #include <linux/slab.h>
  30. #include <mach/timer.h>
  31. #include <linux/mm.h>
  32. #include <linux/pfn.h>
  33. #include <linux/atomic.h>
  34. #include <mach/dma.h>
  35. /* I don't quite understand why dc4 fails when this is set to 1 and DMA is enabled */
  36. /* especially since dc4 doesn't use kmalloc'd memory. */
  37. #define ALLOW_MAP_OF_KMALLOC_MEMORY 0
  38. /* ---- Public Variables ------------------------------------------------- */
  39. /* ---- Private Constants and Types -------------------------------------- */
  40. #define MAKE_HANDLE(controllerIdx, channelIdx) (((controllerIdx) << 4) | (channelIdx))
  41. #define CONTROLLER_FROM_HANDLE(handle) (((handle) >> 4) & 0x0f)
  42. #define CHANNEL_FROM_HANDLE(handle) ((handle) & 0x0f)
  43. #define DMA_MAP_DEBUG 0
  44. #if DMA_MAP_DEBUG
  45. # define DMA_MAP_PRINT(fmt, args...) printk("%s: " fmt, __func__, ## args)
  46. #else
  47. # define DMA_MAP_PRINT(fmt, args...)
  48. #endif
  49. /* ---- Private Variables ------------------------------------------------ */
  50. static DMA_Global_t gDMA;
  51. static struct proc_dir_entry *gDmaDir;
  52. static atomic_t gDmaStatMemTypeKmalloc = ATOMIC_INIT(0);
  53. static atomic_t gDmaStatMemTypeVmalloc = ATOMIC_INIT(0);
  54. static atomic_t gDmaStatMemTypeUser = ATOMIC_INIT(0);
  55. static atomic_t gDmaStatMemTypeCoherent = ATOMIC_INIT(0);
  56. #include "dma_device.c"
  57. /* ---- Private Function Prototypes -------------------------------------- */
  58. /* ---- Functions ------------------------------------------------------- */
  59. /****************************************************************************/
  60. /**
  61. * Displays information for /proc/dma/mem-type
  62. */
  63. /****************************************************************************/
  64. static int dma_proc_read_mem_type(char *buf, char **start, off_t offset,
  65. int count, int *eof, void *data)
  66. {
  67. int len = 0;
  68. len += sprintf(buf + len, "dma_map_mem statistics\n");
  69. len +=
  70. sprintf(buf + len, "coherent: %d\n",
  71. atomic_read(&gDmaStatMemTypeCoherent));
  72. len +=
  73. sprintf(buf + len, "kmalloc: %d\n",
  74. atomic_read(&gDmaStatMemTypeKmalloc));
  75. len +=
  76. sprintf(buf + len, "vmalloc: %d\n",
  77. atomic_read(&gDmaStatMemTypeVmalloc));
  78. len +=
  79. sprintf(buf + len, "user: %d\n",
  80. atomic_read(&gDmaStatMemTypeUser));
  81. return len;
  82. }
  83. /****************************************************************************/
  84. /**
  85. * Displays information for /proc/dma/channels
  86. */
  87. /****************************************************************************/
  88. static int dma_proc_read_channels(char *buf, char **start, off_t offset,
  89. int count, int *eof, void *data)
  90. {
  91. int controllerIdx;
  92. int channelIdx;
  93. int limit = count - 200;
  94. int len = 0;
  95. DMA_Channel_t *channel;
  96. if (down_interruptible(&gDMA.lock) < 0) {
  97. return -ERESTARTSYS;
  98. }
  99. for (controllerIdx = 0; controllerIdx < DMA_NUM_CONTROLLERS;
  100. controllerIdx++) {
  101. for (channelIdx = 0; channelIdx < DMA_NUM_CHANNELS;
  102. channelIdx++) {
  103. if (len >= limit) {
  104. break;
  105. }
  106. channel =
  107. &gDMA.controller[controllerIdx].channel[channelIdx];
  108. len +=
  109. sprintf(buf + len, "%d:%d ", controllerIdx,
  110. channelIdx);
  111. if ((channel->flags & DMA_CHANNEL_FLAG_IS_DEDICATED) !=
  112. 0) {
  113. len +=
  114. sprintf(buf + len, "Dedicated for %s ",
  115. DMA_gDeviceAttribute[channel->
  116. devType].name);
  117. } else {
  118. len += sprintf(buf + len, "Shared ");
  119. }
  120. if ((channel->flags & DMA_CHANNEL_FLAG_NO_ISR) != 0) {
  121. len += sprintf(buf + len, "No ISR ");
  122. }
  123. if ((channel->flags & DMA_CHANNEL_FLAG_LARGE_FIFO) != 0) {
  124. len += sprintf(buf + len, "Fifo: 128 ");
  125. } else {
  126. len += sprintf(buf + len, "Fifo: 64 ");
  127. }
  128. if ((channel->flags & DMA_CHANNEL_FLAG_IN_USE) != 0) {
  129. len +=
  130. sprintf(buf + len, "InUse by %s",
  131. DMA_gDeviceAttribute[channel->
  132. devType].name);
  133. #if (DMA_DEBUG_TRACK_RESERVATION)
  134. len +=
  135. sprintf(buf + len, " (%s:%d)",
  136. channel->fileName,
  137. channel->lineNum);
  138. #endif
  139. } else {
  140. len += sprintf(buf + len, "Avail ");
  141. }
  142. if (channel->lastDevType != DMA_DEVICE_NONE) {
  143. len +=
  144. sprintf(buf + len, "Last use: %s ",
  145. DMA_gDeviceAttribute[channel->
  146. lastDevType].
  147. name);
  148. }
  149. len += sprintf(buf + len, "\n");
  150. }
  151. }
  152. up(&gDMA.lock);
  153. *eof = 1;
  154. return len;
  155. }
  156. /****************************************************************************/
  157. /**
  158. * Displays information for /proc/dma/devices
  159. */
  160. /****************************************************************************/
  161. static int dma_proc_read_devices(char *buf, char **start, off_t offset,
  162. int count, int *eof, void *data)
  163. {
  164. int limit = count - 200;
  165. int len = 0;
  166. int devIdx;
  167. if (down_interruptible(&gDMA.lock) < 0) {
  168. return -ERESTARTSYS;
  169. }
  170. for (devIdx = 0; devIdx < DMA_NUM_DEVICE_ENTRIES; devIdx++) {
  171. DMA_DeviceAttribute_t *devAttr = &DMA_gDeviceAttribute[devIdx];
  172. if (devAttr->name == NULL) {
  173. continue;
  174. }
  175. if (len >= limit) {
  176. break;
  177. }
  178. len += sprintf(buf + len, "%-12s ", devAttr->name);
  179. if ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) != 0) {
  180. len +=
  181. sprintf(buf + len, "Dedicated %d:%d ",
  182. devAttr->dedicatedController,
  183. devAttr->dedicatedChannel);
  184. } else {
  185. len += sprintf(buf + len, "Shared DMA:");
  186. if ((devAttr->flags & DMA_DEVICE_FLAG_ON_DMA0) != 0) {
  187. len += sprintf(buf + len, "0");
  188. }
  189. if ((devAttr->flags & DMA_DEVICE_FLAG_ON_DMA1) != 0) {
  190. len += sprintf(buf + len, "1");
  191. }
  192. len += sprintf(buf + len, " ");
  193. }
  194. if ((devAttr->flags & DMA_DEVICE_FLAG_NO_ISR) != 0) {
  195. len += sprintf(buf + len, "NoISR ");
  196. }
  197. if ((devAttr->flags & DMA_DEVICE_FLAG_ALLOW_LARGE_FIFO) != 0) {
  198. len += sprintf(buf + len, "Allow-128 ");
  199. }
  200. len +=
  201. sprintf(buf + len,
  202. "Xfer #: %Lu Ticks: %Lu Bytes: %Lu DescLen: %u\n",
  203. devAttr->numTransfers, devAttr->transferTicks,
  204. devAttr->transferBytes,
  205. devAttr->ring.bytesAllocated);
  206. }
  207. up(&gDMA.lock);
  208. *eof = 1;
  209. return len;
  210. }
  211. /****************************************************************************/
  212. /**
  213. * Determines if a DMA_Device_t is "valid".
  214. *
  215. * @return
  216. * TRUE - dma device is valid
  217. * FALSE - dma device isn't valid
  218. */
  219. /****************************************************************************/
  220. static inline int IsDeviceValid(DMA_Device_t device)
  221. {
  222. return (device >= 0) && (device < DMA_NUM_DEVICE_ENTRIES);
  223. }
  224. /****************************************************************************/
  225. /**
  226. * Translates a DMA handle into a pointer to a channel.
  227. *
  228. * @return
  229. * non-NULL - pointer to DMA_Channel_t
  230. * NULL - DMA Handle was invalid
  231. */
  232. /****************************************************************************/
  233. static inline DMA_Channel_t *HandleToChannel(DMA_Handle_t handle)
  234. {
  235. int controllerIdx;
  236. int channelIdx;
  237. controllerIdx = CONTROLLER_FROM_HANDLE(handle);
  238. channelIdx = CHANNEL_FROM_HANDLE(handle);
  239. if ((controllerIdx > DMA_NUM_CONTROLLERS)
  240. || (channelIdx > DMA_NUM_CHANNELS)) {
  241. return NULL;
  242. }
  243. return &gDMA.controller[controllerIdx].channel[channelIdx];
  244. }
  245. /****************************************************************************/
  246. /**
  247. * Interrupt handler which is called to process DMA interrupts.
  248. */
  249. /****************************************************************************/
  250. static irqreturn_t dma_interrupt_handler(int irq, void *dev_id)
  251. {
  252. DMA_Channel_t *channel;
  253. DMA_DeviceAttribute_t *devAttr;
  254. int irqStatus;
  255. channel = (DMA_Channel_t *) dev_id;
  256. /* Figure out why we were called, and knock down the interrupt */
  257. irqStatus = dmacHw_getInterruptStatus(channel->dmacHwHandle);
  258. dmacHw_clearInterrupt(channel->dmacHwHandle);
  259. if ((channel->devType < 0)
  260. || (channel->devType > DMA_NUM_DEVICE_ENTRIES)) {
  261. printk(KERN_ERR "dma_interrupt_handler: Invalid devType: %d\n",
  262. channel->devType);
  263. return IRQ_NONE;
  264. }
  265. devAttr = &DMA_gDeviceAttribute[channel->devType];
  266. /* Update stats */
  267. if ((irqStatus & dmacHw_INTERRUPT_STATUS_TRANS) != 0) {
  268. devAttr->transferTicks +=
  269. (timer_get_tick_count() - devAttr->transferStartTime);
  270. }
  271. if ((irqStatus & dmacHw_INTERRUPT_STATUS_ERROR) != 0) {
  272. printk(KERN_ERR
  273. "dma_interrupt_handler: devType :%d DMA error (%s)\n",
  274. channel->devType, devAttr->name);
  275. } else {
  276. devAttr->numTransfers++;
  277. devAttr->transferBytes += devAttr->numBytes;
  278. }
  279. /* Call any installed handler */
  280. if (devAttr->devHandler != NULL) {
  281. devAttr->devHandler(channel->devType, irqStatus,
  282. devAttr->userData);
  283. }
  284. return IRQ_HANDLED;
  285. }
  286. /****************************************************************************/
  287. /**
  288. * Allocates memory to hold a descriptor ring. The descriptor ring then
  289. * needs to be populated by making one or more calls to
  290. * dna_add_descriptors.
  291. *
  292. * The returned descriptor ring will be automatically initialized.
  293. *
  294. * @return
  295. * 0 Descriptor ring was allocated successfully
  296. * -EINVAL Invalid parameters passed in
  297. * -ENOMEM Unable to allocate memory for the desired number of descriptors.
  298. */
  299. /****************************************************************************/
  300. int dma_alloc_descriptor_ring(DMA_DescriptorRing_t *ring, /* Descriptor ring to populate */
  301. int numDescriptors /* Number of descriptors that need to be allocated. */
  302. ) {
  303. size_t bytesToAlloc = dmacHw_descriptorLen(numDescriptors);
  304. if ((ring == NULL) || (numDescriptors <= 0)) {
  305. return -EINVAL;
  306. }
  307. ring->physAddr = 0;
  308. ring->descriptorsAllocated = 0;
  309. ring->bytesAllocated = 0;
  310. ring->virtAddr = dma_alloc_writecombine(NULL,
  311. bytesToAlloc,
  312. &ring->physAddr,
  313. GFP_KERNEL);
  314. if (ring->virtAddr == NULL) {
  315. return -ENOMEM;
  316. }
  317. ring->bytesAllocated = bytesToAlloc;
  318. ring->descriptorsAllocated = numDescriptors;
  319. return dma_init_descriptor_ring(ring, numDescriptors);
  320. }
  321. EXPORT_SYMBOL(dma_alloc_descriptor_ring);
  322. /****************************************************************************/
  323. /**
  324. * Releases the memory which was previously allocated for a descriptor ring.
  325. */
  326. /****************************************************************************/
  327. void dma_free_descriptor_ring(DMA_DescriptorRing_t *ring /* Descriptor to release */
  328. ) {
  329. if (ring->virtAddr != NULL) {
  330. dma_free_writecombine(NULL,
  331. ring->bytesAllocated,
  332. ring->virtAddr, ring->physAddr);
  333. }
  334. ring->bytesAllocated = 0;
  335. ring->descriptorsAllocated = 0;
  336. ring->virtAddr = NULL;
  337. ring->physAddr = 0;
  338. }
  339. EXPORT_SYMBOL(dma_free_descriptor_ring);
  340. /****************************************************************************/
  341. /**
  342. * Initializes a descriptor ring, so that descriptors can be added to it.
  343. * Once a descriptor ring has been allocated, it may be reinitialized for
  344. * use with additional/different regions of memory.
  345. *
  346. * Note that if 7 descriptors are allocated, it's perfectly acceptable to
  347. * initialize the ring with a smaller number of descriptors. The amount
  348. * of memory allocated for the descriptor ring will not be reduced, and
  349. * the descriptor ring may be reinitialized later
  350. *
  351. * @return
  352. * 0 Descriptor ring was initialized successfully
  353. * -ENOMEM The descriptor which was passed in has insufficient space
  354. * to hold the desired number of descriptors.
  355. */
  356. /****************************************************************************/
  357. int dma_init_descriptor_ring(DMA_DescriptorRing_t *ring, /* Descriptor ring to initialize */
  358. int numDescriptors /* Number of descriptors to initialize. */
  359. ) {
  360. if (ring->virtAddr == NULL) {
  361. return -EINVAL;
  362. }
  363. if (dmacHw_initDescriptor(ring->virtAddr,
  364. ring->physAddr,
  365. ring->bytesAllocated, numDescriptors) < 0) {
  366. printk(KERN_ERR
  367. "dma_init_descriptor_ring: dmacHw_initDescriptor failed\n");
  368. return -ENOMEM;
  369. }
  370. return 0;
  371. }
  372. EXPORT_SYMBOL(dma_init_descriptor_ring);
  373. /****************************************************************************/
  374. /**
  375. * Determines the number of descriptors which would be required for a
  376. * transfer of the indicated memory region.
  377. *
  378. * This function also needs to know which DMA device this transfer will
  379. * be destined for, so that the appropriate DMA configuration can be retrieved.
  380. * DMA parameters such as transfer width, and whether this is a memory-to-memory
  381. * or memory-to-peripheral, etc can all affect the actual number of descriptors
  382. * required.
  383. *
  384. * @return
  385. * > 0 Returns the number of descriptors required for the indicated transfer
  386. * -ENODEV - Device handed in is invalid.
  387. * -EINVAL Invalid parameters
  388. * -ENOMEM Memory exhausted
  389. */
  390. /****************************************************************************/
  391. int dma_calculate_descriptor_count(DMA_Device_t device, /* DMA Device that this will be associated with */
  392. dma_addr_t srcData, /* Place to get data to write to device */
  393. dma_addr_t dstData, /* Pointer to device data address */
  394. size_t numBytes /* Number of bytes to transfer to the device */
  395. ) {
  396. int numDescriptors;
  397. DMA_DeviceAttribute_t *devAttr;
  398. if (!IsDeviceValid(device)) {
  399. return -ENODEV;
  400. }
  401. devAttr = &DMA_gDeviceAttribute[device];
  402. numDescriptors = dmacHw_calculateDescriptorCount(&devAttr->config,
  403. (void *)srcData,
  404. (void *)dstData,
  405. numBytes);
  406. if (numDescriptors < 0) {
  407. printk(KERN_ERR
  408. "dma_calculate_descriptor_count: dmacHw_calculateDescriptorCount failed\n");
  409. return -EINVAL;
  410. }
  411. return numDescriptors;
  412. }
  413. EXPORT_SYMBOL(dma_calculate_descriptor_count);
  414. /****************************************************************************/
  415. /**
  416. * Adds a region of memory to the descriptor ring. Note that it may take
  417. * multiple descriptors for each region of memory. It is the callers
  418. * responsibility to allocate a sufficiently large descriptor ring.
  419. *
  420. * @return
  421. * 0 Descriptors were added successfully
  422. * -ENODEV Device handed in is invalid.
  423. * -EINVAL Invalid parameters
  424. * -ENOMEM Memory exhausted
  425. */
  426. /****************************************************************************/
  427. int dma_add_descriptors(DMA_DescriptorRing_t *ring, /* Descriptor ring to add descriptors to */
  428. DMA_Device_t device, /* DMA Device that descriptors are for */
  429. dma_addr_t srcData, /* Place to get data (memory or device) */
  430. dma_addr_t dstData, /* Place to put data (memory or device) */
  431. size_t numBytes /* Number of bytes to transfer to the device */
  432. ) {
  433. int rc;
  434. DMA_DeviceAttribute_t *devAttr;
  435. if (!IsDeviceValid(device)) {
  436. return -ENODEV;
  437. }
  438. devAttr = &DMA_gDeviceAttribute[device];
  439. rc = dmacHw_setDataDescriptor(&devAttr->config,
  440. ring->virtAddr,
  441. (void *)srcData,
  442. (void *)dstData, numBytes);
  443. if (rc < 0) {
  444. printk(KERN_ERR
  445. "dma_add_descriptors: dmacHw_setDataDescriptor failed with code: %d\n",
  446. rc);
  447. return -ENOMEM;
  448. }
  449. return 0;
  450. }
  451. EXPORT_SYMBOL(dma_add_descriptors);
  452. /****************************************************************************/
  453. /**
  454. * Sets the descriptor ring associated with a device.
  455. *
  456. * Once set, the descriptor ring will be associated with the device, even
  457. * across channel request/free calls. Passing in a NULL descriptor ring
  458. * will release any descriptor ring currently associated with the device.
  459. *
  460. * Note: If you call dma_transfer, or one of the other dma_alloc_ functions
  461. * the descriptor ring may be released and reallocated.
  462. *
  463. * Note: This function will release the descriptor memory for any current
  464. * descriptor ring associated with this device.
  465. *
  466. * @return
  467. * 0 Descriptors were added successfully
  468. * -ENODEV Device handed in is invalid.
  469. */
  470. /****************************************************************************/
  471. int dma_set_device_descriptor_ring(DMA_Device_t device, /* Device to update the descriptor ring for. */
  472. DMA_DescriptorRing_t *ring /* Descriptor ring to add descriptors to */
  473. ) {
  474. DMA_DeviceAttribute_t *devAttr;
  475. if (!IsDeviceValid(device)) {
  476. return -ENODEV;
  477. }
  478. devAttr = &DMA_gDeviceAttribute[device];
  479. /* Free the previously allocated descriptor ring */
  480. dma_free_descriptor_ring(&devAttr->ring);
  481. if (ring != NULL) {
  482. /* Copy in the new one */
  483. devAttr->ring = *ring;
  484. }
  485. /* Set things up so that if dma_transfer is called then this descriptor */
  486. /* ring will get freed. */
  487. devAttr->prevSrcData = 0;
  488. devAttr->prevDstData = 0;
  489. devAttr->prevNumBytes = 0;
  490. return 0;
  491. }
  492. EXPORT_SYMBOL(dma_set_device_descriptor_ring);
  493. /****************************************************************************/
  494. /**
  495. * Retrieves the descriptor ring associated with a device.
  496. *
  497. * @return
  498. * 0 Descriptors were added successfully
  499. * -ENODEV Device handed in is invalid.
  500. */
  501. /****************************************************************************/
  502. int dma_get_device_descriptor_ring(DMA_Device_t device, /* Device to retrieve the descriptor ring for. */
  503. DMA_DescriptorRing_t *ring /* Place to store retrieved ring */
  504. ) {
  505. DMA_DeviceAttribute_t *devAttr;
  506. memset(ring, 0, sizeof(*ring));
  507. if (!IsDeviceValid(device)) {
  508. return -ENODEV;
  509. }
  510. devAttr = &DMA_gDeviceAttribute[device];
  511. *ring = devAttr->ring;
  512. return 0;
  513. }
  514. EXPORT_SYMBOL(dma_get_device_descriptor_ring);
  515. /****************************************************************************/
  516. /**
  517. * Configures a DMA channel.
  518. *
  519. * @return
  520. * >= 0 - Initialization was successful.
  521. *
  522. * -EBUSY - Device is currently being used.
  523. * -ENODEV - Device handed in is invalid.
  524. */
  525. /****************************************************************************/
  526. static int ConfigChannel(DMA_Handle_t handle)
  527. {
  528. DMA_Channel_t *channel;
  529. DMA_DeviceAttribute_t *devAttr;
  530. int controllerIdx;
  531. channel = HandleToChannel(handle);
  532. if (channel == NULL) {
  533. return -ENODEV;
  534. }
  535. devAttr = &DMA_gDeviceAttribute[channel->devType];
  536. controllerIdx = CONTROLLER_FROM_HANDLE(handle);
  537. if ((devAttr->flags & DMA_DEVICE_FLAG_PORT_PER_DMAC) != 0) {
  538. if (devAttr->config.transferType ==
  539. dmacHw_TRANSFER_TYPE_MEM_TO_PERIPHERAL) {
  540. devAttr->config.dstPeripheralPort =
  541. devAttr->dmacPort[controllerIdx];
  542. } else if (devAttr->config.transferType ==
  543. dmacHw_TRANSFER_TYPE_PERIPHERAL_TO_MEM) {
  544. devAttr->config.srcPeripheralPort =
  545. devAttr->dmacPort[controllerIdx];
  546. }
  547. }
  548. if (dmacHw_configChannel(channel->dmacHwHandle, &devAttr->config) != 0) {
  549. printk(KERN_ERR "ConfigChannel: dmacHw_configChannel failed\n");
  550. return -EIO;
  551. }
  552. return 0;
  553. }
  554. /****************************************************************************/
  555. /**
  556. * Initializes all of the data structures associated with the DMA.
  557. * @return
  558. * >= 0 - Initialization was successful.
  559. *
  560. * -EBUSY - Device is currently being used.
  561. * -ENODEV - Device handed in is invalid.
  562. */
  563. /****************************************************************************/
  564. int dma_init(void)
  565. {
  566. int rc = 0;
  567. int controllerIdx;
  568. int channelIdx;
  569. DMA_Device_t devIdx;
  570. DMA_Channel_t *channel;
  571. DMA_Handle_t dedicatedHandle;
  572. memset(&gDMA, 0, sizeof(gDMA));
  573. sema_init(&gDMA.lock, 0);
  574. init_waitqueue_head(&gDMA.freeChannelQ);
  575. /* Initialize the Hardware */
  576. dmacHw_initDma();
  577. /* Start off by marking all of the DMA channels as shared. */
  578. for (controllerIdx = 0; controllerIdx < DMA_NUM_CONTROLLERS;
  579. controllerIdx++) {
  580. for (channelIdx = 0; channelIdx < DMA_NUM_CHANNELS;
  581. channelIdx++) {
  582. channel =
  583. &gDMA.controller[controllerIdx].channel[channelIdx];
  584. channel->flags = 0;
  585. channel->devType = DMA_DEVICE_NONE;
  586. channel->lastDevType = DMA_DEVICE_NONE;
  587. #if (DMA_DEBUG_TRACK_RESERVATION)
  588. channel->fileName = "";
  589. channel->lineNum = 0;
  590. #endif
  591. channel->dmacHwHandle =
  592. dmacHw_getChannelHandle(dmacHw_MAKE_CHANNEL_ID
  593. (controllerIdx,
  594. channelIdx));
  595. dmacHw_initChannel(channel->dmacHwHandle);
  596. }
  597. }
  598. /* Record any special attributes that channels may have */
  599. gDMA.controller[0].channel[0].flags |= DMA_CHANNEL_FLAG_LARGE_FIFO;
  600. gDMA.controller[0].channel[1].flags |= DMA_CHANNEL_FLAG_LARGE_FIFO;
  601. gDMA.controller[1].channel[0].flags |= DMA_CHANNEL_FLAG_LARGE_FIFO;
  602. gDMA.controller[1].channel[1].flags |= DMA_CHANNEL_FLAG_LARGE_FIFO;
  603. /* Now walk through and record the dedicated channels. */
  604. for (devIdx = 0; devIdx < DMA_NUM_DEVICE_ENTRIES; devIdx++) {
  605. DMA_DeviceAttribute_t *devAttr = &DMA_gDeviceAttribute[devIdx];
  606. if (((devAttr->flags & DMA_DEVICE_FLAG_NO_ISR) != 0)
  607. && ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) == 0)) {
  608. printk(KERN_ERR
  609. "DMA Device: %s Can only request NO_ISR for dedicated devices\n",
  610. devAttr->name);
  611. rc = -EINVAL;
  612. goto out;
  613. }
  614. if ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) != 0) {
  615. /* This is a dedicated device. Mark the channel as being reserved. */
  616. if (devAttr->dedicatedController >= DMA_NUM_CONTROLLERS) {
  617. printk(KERN_ERR
  618. "DMA Device: %s DMA Controller %d is out of range\n",
  619. devAttr->name,
  620. devAttr->dedicatedController);
  621. rc = -EINVAL;
  622. goto out;
  623. }
  624. if (devAttr->dedicatedChannel >= DMA_NUM_CHANNELS) {
  625. printk(KERN_ERR
  626. "DMA Device: %s DMA Channel %d is out of range\n",
  627. devAttr->name,
  628. devAttr->dedicatedChannel);
  629. rc = -EINVAL;
  630. goto out;
  631. }
  632. dedicatedHandle =
  633. MAKE_HANDLE(devAttr->dedicatedController,
  634. devAttr->dedicatedChannel);
  635. channel = HandleToChannel(dedicatedHandle);
  636. if ((channel->flags & DMA_CHANNEL_FLAG_IS_DEDICATED) !=
  637. 0) {
  638. printk
  639. ("DMA Device: %s attempting to use same DMA Controller:Channel (%d:%d) as %s\n",
  640. devAttr->name,
  641. devAttr->dedicatedController,
  642. devAttr->dedicatedChannel,
  643. DMA_gDeviceAttribute[channel->devType].
  644. name);
  645. rc = -EBUSY;
  646. goto out;
  647. }
  648. channel->flags |= DMA_CHANNEL_FLAG_IS_DEDICATED;
  649. channel->devType = devIdx;
  650. if (devAttr->flags & DMA_DEVICE_FLAG_NO_ISR) {
  651. channel->flags |= DMA_CHANNEL_FLAG_NO_ISR;
  652. }
  653. /* For dedicated channels, we can go ahead and configure the DMA channel now */
  654. /* as well. */
  655. ConfigChannel(dedicatedHandle);
  656. }
  657. }
  658. /* Go through and register the interrupt handlers */
  659. for (controllerIdx = 0; controllerIdx < DMA_NUM_CONTROLLERS;
  660. controllerIdx++) {
  661. for (channelIdx = 0; channelIdx < DMA_NUM_CHANNELS;
  662. channelIdx++) {
  663. channel =
  664. &gDMA.controller[controllerIdx].channel[channelIdx];
  665. if ((channel->flags & DMA_CHANNEL_FLAG_NO_ISR) == 0) {
  666. snprintf(channel->name, sizeof(channel->name),
  667. "dma %d:%d %s", controllerIdx,
  668. channelIdx,
  669. channel->devType ==
  670. DMA_DEVICE_NONE ? "" :
  671. DMA_gDeviceAttribute[channel->devType].
  672. name);
  673. rc =
  674. request_irq(IRQ_DMA0C0 +
  675. (controllerIdx *
  676. DMA_NUM_CHANNELS) +
  677. channelIdx,
  678. dma_interrupt_handler,
  679. IRQF_DISABLED, channel->name,
  680. channel);
  681. if (rc != 0) {
  682. printk(KERN_ERR
  683. "request_irq for IRQ_DMA%dC%d failed\n",
  684. controllerIdx, channelIdx);
  685. }
  686. }
  687. }
  688. }
  689. /* Create /proc/dma/channels and /proc/dma/devices */
  690. gDmaDir = proc_mkdir("dma", NULL);
  691. if (gDmaDir == NULL) {
  692. printk(KERN_ERR "Unable to create /proc/dma\n");
  693. } else {
  694. create_proc_read_entry("channels", 0, gDmaDir,
  695. dma_proc_read_channels, NULL);
  696. create_proc_read_entry("devices", 0, gDmaDir,
  697. dma_proc_read_devices, NULL);
  698. create_proc_read_entry("mem-type", 0, gDmaDir,
  699. dma_proc_read_mem_type, NULL);
  700. }
  701. out:
  702. up(&gDMA.lock);
  703. return rc;
  704. }
  705. /****************************************************************************/
  706. /**
  707. * Reserves a channel for use with @a dev. If the device is setup to use
  708. * a shared channel, then this function will block until a free channel
  709. * becomes available.
  710. *
  711. * @return
  712. * >= 0 - A valid DMA Handle.
  713. * -EBUSY - Device is currently being used.
  714. * -ENODEV - Device handed in is invalid.
  715. */
  716. /****************************************************************************/
  717. #if (DMA_DEBUG_TRACK_RESERVATION)
  718. DMA_Handle_t dma_request_channel_dbg
  719. (DMA_Device_t dev, const char *fileName, int lineNum)
  720. #else
  721. DMA_Handle_t dma_request_channel(DMA_Device_t dev)
  722. #endif
  723. {
  724. DMA_Handle_t handle;
  725. DMA_DeviceAttribute_t *devAttr;
  726. DMA_Channel_t *channel;
  727. int controllerIdx;
  728. int controllerIdx2;
  729. int channelIdx;
  730. if (down_interruptible(&gDMA.lock) < 0) {
  731. return -ERESTARTSYS;
  732. }
  733. if ((dev < 0) || (dev >= DMA_NUM_DEVICE_ENTRIES)) {
  734. handle = -ENODEV;
  735. goto out;
  736. }
  737. devAttr = &DMA_gDeviceAttribute[dev];
  738. #if (DMA_DEBUG_TRACK_RESERVATION)
  739. {
  740. char *s;
  741. s = strrchr(fileName, '/');
  742. if (s != NULL) {
  743. fileName = s + 1;
  744. }
  745. }
  746. #endif
  747. if ((devAttr->flags & DMA_DEVICE_FLAG_IN_USE) != 0) {
  748. /* This device has already been requested and not been freed */
  749. printk(KERN_ERR "%s: device %s is already requested\n",
  750. __func__, devAttr->name);
  751. handle = -EBUSY;
  752. goto out;
  753. }
  754. if ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) != 0) {
  755. /* This device has a dedicated channel. */
  756. channel =
  757. &gDMA.controller[devAttr->dedicatedController].
  758. channel[devAttr->dedicatedChannel];
  759. if ((channel->flags & DMA_CHANNEL_FLAG_IN_USE) != 0) {
  760. handle = -EBUSY;
  761. goto out;
  762. }
  763. channel->flags |= DMA_CHANNEL_FLAG_IN_USE;
  764. devAttr->flags |= DMA_DEVICE_FLAG_IN_USE;
  765. #if (DMA_DEBUG_TRACK_RESERVATION)
  766. channel->fileName = fileName;
  767. channel->lineNum = lineNum;
  768. #endif
  769. handle =
  770. MAKE_HANDLE(devAttr->dedicatedController,
  771. devAttr->dedicatedChannel);
  772. goto out;
  773. }
  774. /* This device needs to use one of the shared channels. */
  775. handle = DMA_INVALID_HANDLE;
  776. while (handle == DMA_INVALID_HANDLE) {
  777. /* Scan through the shared channels and see if one is available */
  778. for (controllerIdx2 = 0; controllerIdx2 < DMA_NUM_CONTROLLERS;
  779. controllerIdx2++) {
  780. /* Check to see if we should try on controller 1 first. */
  781. controllerIdx = controllerIdx2;
  782. if ((devAttr->
  783. flags & DMA_DEVICE_FLAG_ALLOC_DMA1_FIRST) != 0) {
  784. controllerIdx = 1 - controllerIdx;
  785. }
  786. /* See if the device is available on the controller being tested */
  787. if ((devAttr->
  788. flags & (DMA_DEVICE_FLAG_ON_DMA0 << controllerIdx))
  789. != 0) {
  790. for (channelIdx = 0;
  791. channelIdx < DMA_NUM_CHANNELS;
  792. channelIdx++) {
  793. channel =
  794. &gDMA.controller[controllerIdx].
  795. channel[channelIdx];
  796. if (((channel->
  797. flags &
  798. DMA_CHANNEL_FLAG_IS_DEDICATED) ==
  799. 0)
  800. &&
  801. ((channel->
  802. flags & DMA_CHANNEL_FLAG_IN_USE)
  803. == 0)) {
  804. if (((channel->
  805. flags &
  806. DMA_CHANNEL_FLAG_LARGE_FIFO)
  807. != 0)
  808. &&
  809. ((devAttr->
  810. flags &
  811. DMA_DEVICE_FLAG_ALLOW_LARGE_FIFO)
  812. == 0)) {
  813. /* This channel is a large fifo - don't tie it up */
  814. /* with devices that we don't want using it. */
  815. continue;
  816. }
  817. channel->flags |=
  818. DMA_CHANNEL_FLAG_IN_USE;
  819. channel->devType = dev;
  820. devAttr->flags |=
  821. DMA_DEVICE_FLAG_IN_USE;
  822. #if (DMA_DEBUG_TRACK_RESERVATION)
  823. channel->fileName = fileName;
  824. channel->lineNum = lineNum;
  825. #endif
  826. handle =
  827. MAKE_HANDLE(controllerIdx,
  828. channelIdx);
  829. /* Now that we've reserved the channel - we can go ahead and configure it */
  830. if (ConfigChannel(handle) != 0) {
  831. handle = -EIO;
  832. printk(KERN_ERR
  833. "dma_request_channel: ConfigChannel failed\n");
  834. }
  835. goto out;
  836. }
  837. }
  838. }
  839. }
  840. /* No channels are currently available. Let's wait for one to free up. */
  841. {
  842. DEFINE_WAIT(wait);
  843. prepare_to_wait(&gDMA.freeChannelQ, &wait,
  844. TASK_INTERRUPTIBLE);
  845. up(&gDMA.lock);
  846. schedule();
  847. finish_wait(&gDMA.freeChannelQ, &wait);
  848. if (signal_pending(current)) {
  849. /* We don't currently hold gDMA.lock, so we return directly */
  850. return -ERESTARTSYS;
  851. }
  852. }
  853. if (down_interruptible(&gDMA.lock)) {
  854. return -ERESTARTSYS;
  855. }
  856. }
  857. out:
  858. up(&gDMA.lock);
  859. return handle;
  860. }
  861. /* Create both _dbg and non _dbg functions for modules. */
  862. #if (DMA_DEBUG_TRACK_RESERVATION)
  863. #undef dma_request_channel
  864. DMA_Handle_t dma_request_channel(DMA_Device_t dev)
  865. {
  866. return dma_request_channel_dbg(dev, __FILE__, __LINE__);
  867. }
  868. EXPORT_SYMBOL(dma_request_channel_dbg);
  869. #endif
  870. EXPORT_SYMBOL(dma_request_channel);
  871. /****************************************************************************/
  872. /**
  873. * Frees a previously allocated DMA Handle.
  874. */
  875. /****************************************************************************/
  876. int dma_free_channel(DMA_Handle_t handle /* DMA handle. */
  877. ) {
  878. int rc = 0;
  879. DMA_Channel_t *channel;
  880. DMA_DeviceAttribute_t *devAttr;
  881. if (down_interruptible(&gDMA.lock) < 0) {
  882. return -ERESTARTSYS;
  883. }
  884. channel = HandleToChannel(handle);
  885. if (channel == NULL) {
  886. rc = -EINVAL;
  887. goto out;
  888. }
  889. devAttr = &DMA_gDeviceAttribute[channel->devType];
  890. if ((channel->flags & DMA_CHANNEL_FLAG_IS_DEDICATED) == 0) {
  891. channel->lastDevType = channel->devType;
  892. channel->devType = DMA_DEVICE_NONE;
  893. }
  894. channel->flags &= ~DMA_CHANNEL_FLAG_IN_USE;
  895. devAttr->flags &= ~DMA_DEVICE_FLAG_IN_USE;
  896. out:
  897. up(&gDMA.lock);
  898. wake_up_interruptible(&gDMA.freeChannelQ);
  899. return rc;
  900. }
  901. EXPORT_SYMBOL(dma_free_channel);
  902. /****************************************************************************/
  903. /**
  904. * Determines if a given device has been configured as using a shared
  905. * channel.
  906. *
  907. * @return
  908. * 0 Device uses a dedicated channel
  909. * > zero Device uses a shared channel
  910. * < zero Error code
  911. */
  912. /****************************************************************************/
  913. int dma_device_is_channel_shared(DMA_Device_t device /* Device to check. */
  914. ) {
  915. DMA_DeviceAttribute_t *devAttr;
  916. if (!IsDeviceValid(device)) {
  917. return -ENODEV;
  918. }
  919. devAttr = &DMA_gDeviceAttribute[device];
  920. return ((devAttr->flags & DMA_DEVICE_FLAG_IS_DEDICATED) == 0);
  921. }
  922. EXPORT_SYMBOL(dma_device_is_channel_shared);
  923. /****************************************************************************/
  924. /**
  925. * Allocates buffers for the descriptors. This is normally done automatically
  926. * but needs to be done explicitly when initiating a dma from interrupt
  927. * context.
  928. *
  929. * @return
  930. * 0 Descriptors were allocated successfully
  931. * -EINVAL Invalid device type for this kind of transfer
  932. * (i.e. the device is _MEM_TO_DEV and not _DEV_TO_MEM)
  933. * -ENOMEM Memory exhausted
  934. */
  935. /****************************************************************************/
  936. int dma_alloc_descriptors(DMA_Handle_t handle, /* DMA Handle */
  937. dmacHw_TRANSFER_TYPE_e transferType, /* Type of transfer being performed */
  938. dma_addr_t srcData, /* Place to get data to write to device */
  939. dma_addr_t dstData, /* Pointer to device data address */
  940. size_t numBytes /* Number of bytes to transfer to the device */
  941. ) {
  942. DMA_Channel_t *channel;
  943. DMA_DeviceAttribute_t *devAttr;
  944. int numDescriptors;
  945. size_t ringBytesRequired;
  946. int rc = 0;
  947. channel = HandleToChannel(handle);
  948. if (channel == NULL) {
  949. return -ENODEV;
  950. }
  951. devAttr = &DMA_gDeviceAttribute[channel->devType];
  952. if (devAttr->config.transferType != transferType) {
  953. return -EINVAL;
  954. }
  955. /* Figure out how many descriptors we need. */
  956. /* printk("srcData: 0x%08x dstData: 0x%08x, numBytes: %d\n", */
  957. /* srcData, dstData, numBytes); */
  958. numDescriptors = dmacHw_calculateDescriptorCount(&devAttr->config,
  959. (void *)srcData,
  960. (void *)dstData,
  961. numBytes);
  962. if (numDescriptors < 0) {
  963. printk(KERN_ERR "%s: dmacHw_calculateDescriptorCount failed\n",
  964. __func__);
  965. return -EINVAL;
  966. }
  967. /* Check to see if we can reuse the existing descriptor ring, or if we need to allocate */
  968. /* a new one. */
  969. ringBytesRequired = dmacHw_descriptorLen(numDescriptors);
  970. /* printk("ringBytesRequired: %d\n", ringBytesRequired); */
  971. if (ringBytesRequired > devAttr->ring.bytesAllocated) {
  972. /* Make sure that this code path is never taken from interrupt context. */
  973. /* It's OK for an interrupt to initiate a DMA transfer, but the descriptor */
  974. /* allocation needs to have already been done. */
  975. might_sleep();
  976. /* Free the old descriptor ring and allocate a new one. */
  977. dma_free_descriptor_ring(&devAttr->ring);
  978. /* And allocate a new one. */
  979. rc =
  980. dma_alloc_descriptor_ring(&devAttr->ring,
  981. numDescriptors);
  982. if (rc < 0) {
  983. printk(KERN_ERR
  984. "%s: dma_alloc_descriptor_ring(%d) failed\n",
  985. __func__, numDescriptors);
  986. return rc;
  987. }
  988. /* Setup the descriptor for this transfer */
  989. if (dmacHw_initDescriptor(devAttr->ring.virtAddr,
  990. devAttr->ring.physAddr,
  991. devAttr->ring.bytesAllocated,
  992. numDescriptors) < 0) {
  993. printk(KERN_ERR "%s: dmacHw_initDescriptor failed\n",
  994. __func__);
  995. return -EINVAL;
  996. }
  997. } else {
  998. /* We've already got enough ring buffer allocated. All we need to do is reset */
  999. /* any control information, just in case the previous DMA was stopped. */
  1000. dmacHw_resetDescriptorControl(devAttr->ring.virtAddr);
  1001. }
  1002. /* dma_alloc/free both set the prevSrc/DstData to 0. If they happen to be the same */
  1003. /* as last time, then we don't need to call setDataDescriptor again. */
  1004. if (dmacHw_setDataDescriptor(&devAttr->config,
  1005. devAttr->ring.virtAddr,
  1006. (void *)srcData,
  1007. (void *)dstData, numBytes) < 0) {
  1008. printk(KERN_ERR "%s: dmacHw_setDataDescriptor failed\n",
  1009. __func__);
  1010. return -EINVAL;
  1011. }
  1012. /* Remember the critical information for this transfer so that we can eliminate */
  1013. /* another call to dma_alloc_descriptors if the caller reuses the same buffers */
  1014. devAttr->prevSrcData = srcData;
  1015. devAttr->prevDstData = dstData;
  1016. devAttr->prevNumBytes = numBytes;
  1017. return 0;
  1018. }
  1019. EXPORT_SYMBOL(dma_alloc_descriptors);
  1020. /****************************************************************************/
  1021. /**
  1022. * Allocates and sets up descriptors for a double buffered circular buffer.
  1023. *
  1024. * This is primarily intended to be used for things like the ingress samples
  1025. * from a microphone.
  1026. *
  1027. * @return
  1028. * > 0 Number of descriptors actually allocated.
  1029. * -EINVAL Invalid device type for this kind of transfer
  1030. * (i.e. the device is _MEM_TO_DEV and not _DEV_TO_MEM)
  1031. * -ENOMEM Memory exhausted
  1032. */
  1033. /****************************************************************************/
  1034. int dma_alloc_double_dst_descriptors(DMA_Handle_t handle, /* DMA Handle */
  1035. dma_addr_t srcData, /* Physical address of source data */
  1036. dma_addr_t dstData1, /* Physical address of first destination buffer */
  1037. dma_addr_t dstData2, /* Physical address of second destination buffer */
  1038. size_t numBytes /* Number of bytes in each destination buffer */
  1039. ) {
  1040. DMA_Channel_t *channel;
  1041. DMA_DeviceAttribute_t *devAttr;
  1042. int numDst1Descriptors;
  1043. int numDst2Descriptors;
  1044. int numDescriptors;
  1045. size_t ringBytesRequired;
  1046. int rc = 0;
  1047. channel = HandleToChannel(handle);
  1048. if (channel == NULL) {
  1049. return -ENODEV;
  1050. }
  1051. devAttr = &DMA_gDeviceAttribute[channel->devType];
  1052. /* Figure out how many descriptors we need. */
  1053. /* printk("srcData: 0x%08x dstData: 0x%08x, numBytes: %d\n", */
  1054. /* srcData, dstData, numBytes); */
  1055. numDst1Descriptors =
  1056. dmacHw_calculateDescriptorCount(&devAttr->config, (void *)srcData,
  1057. (void *)dstData1, numBytes);
  1058. if (numDst1Descriptors < 0) {
  1059. return -EINVAL;
  1060. }
  1061. numDst2Descriptors =
  1062. dmacHw_calculateDescriptorCount(&devAttr->config, (void *)srcData,
  1063. (void *)dstData2, numBytes);
  1064. if (numDst2Descriptors < 0) {
  1065. return -EINVAL;
  1066. }
  1067. numDescriptors = numDst1Descriptors + numDst2Descriptors;
  1068. /* printk("numDescriptors: %d\n", numDescriptors); */
  1069. /* Check to see if we can reuse the existing descriptor ring, or if we need to allocate */
  1070. /* a new one. */
  1071. ringBytesRequired = dmacHw_descriptorLen(numDescriptors);
  1072. /* printk("ringBytesRequired: %d\n", ringBytesRequired); */
  1073. if (ringBytesRequired > devAttr->ring.bytesAllocated) {
  1074. /* Make sure that this code path is never taken from interrupt context. */
  1075. /* It's OK for an interrupt to initiate a DMA transfer, but the descriptor */
  1076. /* allocation needs to have already been done. */
  1077. might_sleep();
  1078. /* Free the old descriptor ring and allocate a new one. */
  1079. dma_free_descriptor_ring(&devAttr->ring);
  1080. /* And allocate a new one. */
  1081. rc =
  1082. dma_alloc_descriptor_ring(&devAttr->ring,
  1083. numDescriptors);
  1084. if (rc < 0) {
  1085. printk(KERN_ERR
  1086. "%s: dma_alloc_descriptor_ring(%d) failed\n",
  1087. __func__, ringBytesRequired);
  1088. return rc;
  1089. }
  1090. }
  1091. /* Setup the descriptor for this transfer. Since this function is used with */
  1092. /* CONTINUOUS DMA operations, we need to reinitialize every time, otherwise */
  1093. /* setDataDescriptor will keep trying to append onto the end. */
  1094. if (dmacHw_initDescriptor(devAttr->ring.virtAddr,
  1095. devAttr->ring.physAddr,
  1096. devAttr->ring.bytesAllocated,
  1097. numDescriptors) < 0) {
  1098. printk(KERN_ERR "%s: dmacHw_initDescriptor failed\n", __func__);
  1099. return -EINVAL;
  1100. }
  1101. /* dma_alloc/free both set the prevSrc/DstData to 0. If they happen to be the same */
  1102. /* as last time, then we don't need to call setDataDescriptor again. */
  1103. if (dmacHw_setDataDescriptor(&devAttr->config,
  1104. devAttr->ring.virtAddr,
  1105. (void *)srcData,
  1106. (void *)dstData1, numBytes) < 0) {
  1107. printk(KERN_ERR "%s: dmacHw_setDataDescriptor 1 failed\n",
  1108. __func__);
  1109. return -EINVAL;
  1110. }
  1111. if (dmacHw_setDataDescriptor(&devAttr->config,
  1112. devAttr->ring.virtAddr,
  1113. (void *)srcData,
  1114. (void *)dstData2, numBytes) < 0) {
  1115. printk(KERN_ERR "%s: dmacHw_setDataDescriptor 2 failed\n",
  1116. __func__);
  1117. return -EINVAL;
  1118. }
  1119. /* You should use dma_start_transfer rather than dma_transfer_xxx so we don't */
  1120. /* try to make the 'prev' variables right. */
  1121. devAttr->prevSrcData = 0;
  1122. devAttr->prevDstData = 0;
  1123. devAttr->prevNumBytes = 0;
  1124. return numDescriptors;
  1125. }
  1126. EXPORT_SYMBOL(dma_alloc_double_dst_descriptors);
  1127. /****************************************************************************/
  1128. /**
  1129. * Initiates a transfer when the descriptors have already been setup.
  1130. *
  1131. * This is a special case, and normally, the dma_transfer_xxx functions should
  1132. * be used.
  1133. *
  1134. * @return
  1135. * 0 Transfer was started successfully
  1136. * -ENODEV Invalid handle
  1137. */
  1138. /****************************************************************************/
  1139. int dma_start_transfer(DMA_Handle_t handle)
  1140. {
  1141. DMA_Channel_t *channel;
  1142. DMA_DeviceAttribute_t *devAttr;
  1143. channel = HandleToChannel(handle);
  1144. if (channel == NULL) {
  1145. return -ENODEV;
  1146. }
  1147. devAttr = &DMA_gDeviceAttribute[channel->devType];
  1148. dmacHw_initiateTransfer(channel->dmacHwHandle, &devAttr->config,
  1149. devAttr->ring.virtAddr);
  1150. /* Since we got this far, everything went successfully */
  1151. return 0;
  1152. }
  1153. EXPORT_SYMBOL(dma_start_transfer);
  1154. /****************************************************************************/
  1155. /**
  1156. * Stops a previously started DMA transfer.
  1157. *
  1158. * @return
  1159. * 0 Transfer was stopped successfully
  1160. * -ENODEV Invalid handle
  1161. */
  1162. /****************************************************************************/
  1163. int dma_stop_transfer(DMA_Handle_t handle)
  1164. {
  1165. DMA_Channel_t *channel;
  1166. channel = HandleToChannel(handle);
  1167. if (channel == NULL) {
  1168. return -ENODEV;
  1169. }
  1170. dmacHw_stopTransfer(channel->dmacHwHandle);
  1171. return 0;
  1172. }
  1173. EXPORT_SYMBOL(dma_stop_transfer);
  1174. /****************************************************************************/
  1175. /**
  1176. * Waits for a DMA to complete by polling. This function is only intended
  1177. * to be used for testing. Interrupts should be used for most DMA operations.
  1178. */
  1179. /****************************************************************************/
  1180. int dma_wait_transfer_done(DMA_Handle_t handle)
  1181. {
  1182. DMA_Channel_t *channel;
  1183. dmacHw_TRANSFER_STATUS_e status;
  1184. channel = HandleToChannel(handle);
  1185. if (channel == NULL) {
  1186. return -ENODEV;
  1187. }
  1188. while ((status =
  1189. dmacHw_transferCompleted(channel->dmacHwHandle)) ==
  1190. dmacHw_TRANSFER_STATUS_BUSY) {
  1191. ;
  1192. }
  1193. if (status == dmacHw_TRANSFER_STATUS_ERROR) {
  1194. printk(KERN_ERR "%s: DMA transfer failed\n", __func__);
  1195. return -EIO;
  1196. }
  1197. return 0;
  1198. }
  1199. EXPORT_SYMBOL(dma_wait_transfer_done);
  1200. /****************************************************************************/
  1201. /**
  1202. * Initiates a DMA, allocating the descriptors as required.
  1203. *
  1204. * @return
  1205. * 0 Transfer was started successfully
  1206. * -EINVAL Invalid device type for this kind of transfer
  1207. * (i.e. the device is _DEV_TO_MEM and not _MEM_TO_DEV)
  1208. */
  1209. /****************************************************************************/
  1210. int dma_transfer(DMA_Handle_t handle, /* DMA Handle */
  1211. dmacHw_TRANSFER_TYPE_e transferType, /* Type of transfer being performed */
  1212. dma_addr_t srcData, /* Place to get data to write to device */
  1213. dma_addr_t dstData, /* Pointer to device data address */
  1214. size_t numBytes /* Number of bytes to transfer to the device */
  1215. ) {
  1216. DMA_Channel_t *channel;
  1217. DMA_DeviceAttribute_t *devAttr;
  1218. int rc = 0;
  1219. channel = HandleToChannel(handle);
  1220. if (channel == NULL) {
  1221. return -ENODEV;
  1222. }
  1223. devAttr = &DMA_gDeviceAttribute[channel->devType];
  1224. if (devAttr->config.transferType != transferType) {
  1225. return -EINVAL;
  1226. }
  1227. /* We keep track of the information about the previous request for this */
  1228. /* device, and if the attributes match, then we can use the descriptors we setup */
  1229. /* the last time, and not have to reinitialize everything. */
  1230. {
  1231. rc =
  1232. dma_alloc_descriptors(handle, transferType, srcData,
  1233. dstData, numBytes);
  1234. if (rc != 0) {
  1235. return rc;
  1236. }
  1237. }
  1238. /* And kick off the transfer */
  1239. devAttr->numBytes = numBytes;
  1240. devAttr->transferStartTime = timer_get_tick_count();
  1241. dmacHw_initiateTransfer(channel->dmacHwHandle, &devAttr->config,
  1242. devAttr->ring.virtAddr);
  1243. /* Since we got this far, everything went successfully */
  1244. return 0;
  1245. }
  1246. EXPORT_SYMBOL(dma_transfer);
  1247. /****************************************************************************/
  1248. /**
  1249. * Set the callback function which will be called when a transfer completes.
  1250. * If a NULL callback function is set, then no callback will occur.
  1251. *
  1252. * @note @a devHandler will be called from IRQ context.
  1253. *
  1254. * @return
  1255. * 0 - Success
  1256. * -ENODEV - Device handed in is invalid.
  1257. */
  1258. /****************************************************************************/
  1259. int dma_set_device_handler(DMA_Device_t dev, /* Device to set the callback for. */
  1260. DMA_DeviceHandler_t devHandler, /* Function to call when the DMA completes */
  1261. void *userData /* Pointer which will be passed to devHandler. */
  1262. ) {
  1263. DMA_DeviceAttribute_t *devAttr;
  1264. unsigned long flags;
  1265. if (!IsDeviceValid(dev)) {
  1266. return -ENODEV;
  1267. }
  1268. devAttr = &DMA_gDeviceAttribute[dev];
  1269. local_irq_save(flags);
  1270. devAttr->userData = userData;
  1271. devAttr->devHandler = devHandler;
  1272. local_irq_restore(flags);
  1273. return 0;
  1274. }
  1275. EXPORT_SYMBOL(dma_set_device_handler);
  1276. /****************************************************************************/
  1277. /**
  1278. * Initializes a memory mapping structure
  1279. */
  1280. /****************************************************************************/
  1281. int dma_init_mem_map(DMA_MemMap_t *memMap)
  1282. {
  1283. memset(memMap, 0, sizeof(*memMap));
  1284. sema_init(&memMap->lock, 1);
  1285. return 0;
  1286. }
  1287. EXPORT_SYMBOL(dma_init_mem_map);
  1288. /****************************************************************************/
  1289. /**
  1290. * Releases any memory currently being held by a memory mapping structure.
  1291. */
  1292. /****************************************************************************/
  1293. int dma_term_mem_map(DMA_MemMap_t *memMap)
  1294. {
  1295. down(&memMap->lock); /* Just being paranoid */
  1296. /* Free up any allocated memory */
  1297. up(&memMap->lock);
  1298. memset(memMap, 0, sizeof(*memMap));
  1299. return 0;
  1300. }
  1301. EXPORT_SYMBOL(dma_term_mem_map);
  1302. /****************************************************************************/
  1303. /**
  1304. * Looks at a memory address and categorizes it.
  1305. *
  1306. * @return One of the values from the DMA_MemType_t enumeration.
  1307. */
  1308. /****************************************************************************/
  1309. DMA_MemType_t dma_mem_type(void *addr)
  1310. {
  1311. unsigned long addrVal = (unsigned long)addr;
  1312. if (addrVal >= CONSISTENT_BASE) {
  1313. /* NOTE: DMA virtual memory space starts at 0xFFxxxxxx */
  1314. /* dma_alloc_xxx pages are physically and virtually contiguous */
  1315. return DMA_MEM_TYPE_DMA;
  1316. }
  1317. /* Technically, we could add one more classification. Addresses between VMALLOC_END */
  1318. /* and the beginning of the DMA virtual address could be considered to be I/O space. */
  1319. /* Right now, nobody cares about this particular classification, so we ignore it. */
  1320. if (is_vmalloc_addr(addr)) {
  1321. /* Address comes from the vmalloc'd region. Pages are virtually */
  1322. /* contiguous but NOT physically contiguous */
  1323. return DMA_MEM_TYPE_VMALLOC;
  1324. }
  1325. if (addrVal >= PAGE_OFFSET) {
  1326. /* PAGE_OFFSET is typically 0xC0000000 */
  1327. /* kmalloc'd pages are physically contiguous */
  1328. return DMA_MEM_TYPE_KMALLOC;
  1329. }
  1330. return DMA_MEM_TYPE_USER;
  1331. }
  1332. EXPORT_SYMBOL(dma_mem_type);
  1333. /****************************************************************************/
  1334. /**
  1335. * Looks at a memory address and determines if we support DMA'ing to/from
  1336. * that type of memory.
  1337. *
  1338. * @return boolean -
  1339. * return value != 0 means dma supported
  1340. * return value == 0 means dma not supported
  1341. */
  1342. /****************************************************************************/
  1343. int dma_mem_supports_dma(void *addr)
  1344. {
  1345. DMA_MemType_t memType = dma_mem_type(addr);
  1346. return (memType == DMA_MEM_TYPE_DMA)
  1347. #if ALLOW_MAP_OF_KMALLOC_MEMORY
  1348. || (memType == DMA_MEM_TYPE_KMALLOC)
  1349. #endif
  1350. || (memType == DMA_MEM_TYPE_USER);
  1351. }
  1352. EXPORT_SYMBOL(dma_mem_supports_dma);
  1353. /****************************************************************************/
  1354. /**
  1355. * Maps in a memory region such that it can be used for performing a DMA.
  1356. *
  1357. * @return
  1358. */
  1359. /****************************************************************************/
  1360. int dma_map_start(DMA_MemMap_t *memMap, /* Stores state information about the map */
  1361. enum dma_data_direction dir /* Direction that the mapping will be going */
  1362. ) {
  1363. int rc;
  1364. down(&memMap->lock);
  1365. DMA_MAP_PRINT("memMap: %p\n", memMap);
  1366. if (memMap->inUse) {
  1367. printk(KERN_ERR "%s: memory map %p is already being used\n",
  1368. __func__, memMap);
  1369. rc = -EBUSY;
  1370. goto out;
  1371. }
  1372. memMap->inUse = 1;
  1373. memMap->dir = dir;
  1374. memMap->numRegionsUsed = 0;
  1375. rc = 0;
  1376. out:
  1377. DMA_MAP_PRINT("returning %d", rc);
  1378. up(&memMap->lock);
  1379. return rc;
  1380. }
  1381. EXPORT_SYMBOL(dma_map_start);
  1382. /****************************************************************************/
  1383. /**
  1384. * Adds a segment of memory to a memory map. Each segment is both
  1385. * physically and virtually contiguous.
  1386. *
  1387. * @return 0 on success, error code otherwise.
  1388. */
  1389. /****************************************************************************/
  1390. static int dma_map_add_segment(DMA_MemMap_t *memMap, /* Stores state information about the map */
  1391. DMA_Region_t *region, /* Region that the segment belongs to */
  1392. void *virtAddr, /* Virtual address of the segment being added */
  1393. dma_addr_t physAddr, /* Physical address of the segment being added */
  1394. size_t numBytes /* Number of bytes of the segment being added */
  1395. ) {
  1396. DMA_Segment_t *segment;
  1397. DMA_MAP_PRINT("memMap:%p va:%p pa:0x%x #:%d\n", memMap, virtAddr,
  1398. physAddr, numBytes);
  1399. /* Sanity check */
  1400. if (((unsigned long)virtAddr < (unsigned long)region->virtAddr)
  1401. || (((unsigned long)virtAddr + numBytes)) >
  1402. ((unsigned long)region->virtAddr + region->numBytes)) {
  1403. printk(KERN_ERR
  1404. "%s: virtAddr %p is outside region @ %p len: %d\n",
  1405. __func__, virtAddr, region->virtAddr, region->numBytes);
  1406. return -EINVAL;
  1407. }
  1408. if (region->numSegmentsUsed > 0) {
  1409. /* Check to see if this segment is physically contiguous with the previous one */
  1410. segment = &region->segment[region->numSegmentsUsed - 1];
  1411. if ((segment->physAddr + segment->numBytes) == physAddr) {
  1412. /* It is - just add on to the end */
  1413. DMA_MAP_PRINT("appending %d bytes to last segment\n",
  1414. numBytes);
  1415. segment->numBytes += numBytes;
  1416. return 0;
  1417. }
  1418. }
  1419. /* Reallocate to hold more segments, if required. */
  1420. if (region->numSegmentsUsed >= region->numSegmentsAllocated) {
  1421. DMA_Segment_t *newSegment;
  1422. size_t oldSize =
  1423. region->numSegmentsAllocated * sizeof(*newSegment);
  1424. int newAlloc = region->numSegmentsAllocated + 4;
  1425. size_t newSize = newAlloc * sizeof(*newSegment);
  1426. newSegment = kmalloc(newSize, GFP_KERNEL);
  1427. if (newSegment == NULL) {
  1428. return -ENOMEM;
  1429. }
  1430. memcpy(newSegment, region->segment, oldSize);
  1431. memset(&((uint8_t *) newSegment)[oldSize], 0,
  1432. newSize - oldSize);
  1433. kfree(region->segment);
  1434. region->numSegmentsAllocated = newAlloc;
  1435. region->segment = newSegment;
  1436. }
  1437. segment = &region->segment[region->numSegmentsUsed];
  1438. region->numSegmentsUsed++;
  1439. segment->virtAddr = virtAddr;
  1440. segment->physAddr = physAddr;
  1441. segment->numBytes = numBytes;
  1442. DMA_MAP_PRINT("returning success\n");
  1443. return 0;
  1444. }
  1445. /****************************************************************************/
  1446. /**
  1447. * Adds a region of memory to a memory map. Each region is virtually
  1448. * contiguous, but not necessarily physically contiguous.
  1449. *
  1450. * @return 0 on success, error code otherwise.
  1451. */
  1452. /****************************************************************************/
  1453. int dma_map_add_region(DMA_MemMap_t *memMap, /* Stores state information about the map */
  1454. void *mem, /* Virtual address that we want to get a map of */
  1455. size_t numBytes /* Number of bytes being mapped */
  1456. ) {
  1457. unsigned long addr = (unsigned long)mem;
  1458. unsigned int offset;
  1459. int rc = 0;
  1460. DMA_Region_t *region;
  1461. dma_addr_t physAddr;
  1462. down(&memMap->lock);
  1463. DMA_MAP_PRINT("memMap:%p va:%p #:%d\n", memMap, mem, numBytes);
  1464. if (!memMap->inUse) {
  1465. printk(KERN_ERR "%s: Make sure you call dma_map_start first\n",
  1466. __func__);
  1467. rc = -EINVAL;
  1468. goto out;
  1469. }
  1470. /* Reallocate to hold more regions. */
  1471. if (memMap->numRegionsUsed >= memMap->numRegionsAllocated) {
  1472. DMA_Region_t *newRegion;
  1473. size_t oldSize =
  1474. memMap->numRegionsAllocated * sizeof(*newRegion);
  1475. int newAlloc = memMap->numRegionsAllocated + 4;
  1476. size_t newSize = newAlloc * sizeof(*newRegion);
  1477. newRegion = kmalloc(newSize, GFP_KERNEL);
  1478. if (newRegion == NULL) {
  1479. rc = -ENOMEM;
  1480. goto out;
  1481. }
  1482. memcpy(newRegion, memMap->region, oldSize);
  1483. memset(&((uint8_t *) newRegion)[oldSize], 0, newSize - oldSize);
  1484. kfree(memMap->region);
  1485. memMap->numRegionsAllocated = newAlloc;
  1486. memMap->region = newRegion;
  1487. }
  1488. region = &memMap->region[memMap->numRegionsUsed];
  1489. memMap->numRegionsUsed++;
  1490. offset = addr & ~PAGE_MASK;
  1491. region->memType = dma_mem_type(mem);
  1492. region->virtAddr = mem;
  1493. region->numBytes = numBytes;
  1494. region->numSegmentsUsed = 0;
  1495. region->numLockedPages = 0;
  1496. region->lockedPages = NULL;
  1497. switch (region->memType) {
  1498. case DMA_MEM_TYPE_VMALLOC:
  1499. {
  1500. atomic_inc(&gDmaStatMemTypeVmalloc);
  1501. /* printk(KERN_ERR "%s: vmalloc'd pages are not supported\n", __func__); */
  1502. /* vmalloc'd pages are not physically contiguous */
  1503. rc = -EINVAL;
  1504. break;
  1505. }
  1506. case DMA_MEM_TYPE_KMALLOC:
  1507. {
  1508. atomic_inc(&gDmaStatMemTypeKmalloc);
  1509. /* kmalloc'd pages are physically contiguous, so they'll have exactly */
  1510. /* one segment */
  1511. #if ALLOW_MAP_OF_KMALLOC_MEMORY
  1512. physAddr =
  1513. dma_map_single(NULL, mem, numBytes, memMap->dir);
  1514. rc = dma_map_add_segment(memMap, region, mem, physAddr,
  1515. numBytes);
  1516. #else
  1517. rc = -EINVAL;
  1518. #endif
  1519. break;
  1520. }
  1521. case DMA_MEM_TYPE_DMA:
  1522. {
  1523. /* dma_alloc_xxx pages are physically contiguous */
  1524. atomic_inc(&gDmaStatMemTypeCoherent);
  1525. physAddr = (vmalloc_to_pfn(mem) << PAGE_SHIFT) + offset;
  1526. dma_sync_single_for_cpu(NULL, physAddr, numBytes,
  1527. memMap->dir);
  1528. rc = dma_map_add_segment(memMap, region, mem, physAddr,
  1529. numBytes);
  1530. break;
  1531. }
  1532. case DMA_MEM_TYPE_USER:
  1533. {
  1534. size_t firstPageOffset;
  1535. size_t firstPageSize;
  1536. struct page **pages;
  1537. struct task_struct *userTask;
  1538. atomic_inc(&gDmaStatMemTypeUser);
  1539. #if 1
  1540. /* If the pages are user pages, then the dma_mem_map_set_user_task function */
  1541. /* must have been previously called. */
  1542. if (memMap->userTask == NULL) {
  1543. printk(KERN_ERR
  1544. "%s: must call dma_mem_map_set_user_task when using user-mode memory\n",
  1545. __func__);
  1546. return -EINVAL;
  1547. }
  1548. /* User pages need to be locked. */
  1549. firstPageOffset =
  1550. (unsigned long)region->virtAddr & (PAGE_SIZE - 1);
  1551. firstPageSize = PAGE_SIZE - firstPageOffset;
  1552. region->numLockedPages = (firstPageOffset
  1553. + region->numBytes +
  1554. PAGE_SIZE - 1) / PAGE_SIZE;
  1555. pages =
  1556. kmalloc(region->numLockedPages *
  1557. sizeof(struct page *), GFP_KERNEL);
  1558. if (pages == NULL) {
  1559. region->numLockedPages = 0;
  1560. return -ENOMEM;
  1561. }
  1562. userTask = memMap->userTask;
  1563. down_read(&userTask->mm->mmap_sem);
  1564. rc = get_user_pages(userTask, /* task */
  1565. userTask->mm, /* mm */
  1566. (unsigned long)region->virtAddr, /* start */
  1567. region->numLockedPages, /* len */
  1568. memMap->dir == DMA_FROM_DEVICE, /* write */
  1569. 0, /* force */
  1570. pages, /* pages (array of pointers to page) */
  1571. NULL); /* vmas */
  1572. up_read(&userTask->mm->mmap_sem);
  1573. if (rc != region->numLockedPages) {
  1574. kfree(pages);
  1575. region->numLockedPages = 0;
  1576. if (rc >= 0) {
  1577. rc = -EINVAL;
  1578. }
  1579. } else {
  1580. uint8_t *virtAddr = region->virtAddr;
  1581. size_t bytesRemaining;
  1582. int pageIdx;
  1583. rc = 0; /* Since get_user_pages returns +ve number */
  1584. region->lockedPages = pages;
  1585. /* We've locked the user pages. Now we need to walk them and figure */
  1586. /* out the physical addresses. */
  1587. /* The first page may be partial */
  1588. dma_map_add_segment(memMap,
  1589. region,
  1590. virtAddr,
  1591. PFN_PHYS(page_to_pfn
  1592. (pages[0])) +
  1593. firstPageOffset,
  1594. firstPageSize);
  1595. virtAddr += firstPageSize;
  1596. bytesRemaining =
  1597. region->numBytes - firstPageSize;
  1598. for (pageIdx = 1;
  1599. pageIdx < region->numLockedPages;
  1600. pageIdx++) {
  1601. size_t bytesThisPage =
  1602. (bytesRemaining >
  1603. PAGE_SIZE ? PAGE_SIZE :
  1604. bytesRemaining);
  1605. DMA_MAP_PRINT
  1606. ("pageIdx:%d pages[pageIdx]=%p pfn=%u phys=%u\n",
  1607. pageIdx, pages[pageIdx],
  1608. page_to_pfn(pages[pageIdx]),
  1609. PFN_PHYS(page_to_pfn
  1610. (pages[pageIdx])));
  1611. dma_map_add_segment(memMap,
  1612. region,
  1613. virtAddr,
  1614. PFN_PHYS(page_to_pfn
  1615. (pages
  1616. [pageIdx])),
  1617. bytesThisPage);
  1618. virtAddr += bytesThisPage;
  1619. bytesRemaining -= bytesThisPage;
  1620. }
  1621. }
  1622. #else
  1623. printk(KERN_ERR
  1624. "%s: User mode pages are not yet supported\n",
  1625. __func__);
  1626. /* user pages are not physically contiguous */
  1627. rc = -EINVAL;
  1628. #endif
  1629. break;
  1630. }
  1631. default:
  1632. {
  1633. printk(KERN_ERR "%s: Unsupported memory type: %d\n",
  1634. __func__, region->memType);
  1635. rc = -EINVAL;
  1636. break;
  1637. }
  1638. }
  1639. if (rc != 0) {
  1640. memMap->numRegionsUsed--;
  1641. }
  1642. out:
  1643. DMA_MAP_PRINT("returning %d\n", rc);
  1644. up(&memMap->lock);
  1645. return rc;
  1646. }
  1647. EXPORT_SYMBOL(dma_map_add_segment);
  1648. /****************************************************************************/
  1649. /**
  1650. * Maps in a memory region such that it can be used for performing a DMA.
  1651. *
  1652. * @return 0 on success, error code otherwise.
  1653. */
  1654. /****************************************************************************/
  1655. int dma_map_mem(DMA_MemMap_t *memMap, /* Stores state information about the map */
  1656. void *mem, /* Virtual address that we want to get a map of */
  1657. size_t numBytes, /* Number of bytes being mapped */
  1658. enum dma_data_direction dir /* Direction that the mapping will be going */
  1659. ) {
  1660. int rc;
  1661. rc = dma_map_start(memMap, dir);
  1662. if (rc == 0) {
  1663. rc = dma_map_add_region(memMap, mem, numBytes);
  1664. if (rc < 0) {
  1665. /* Since the add fails, this function will fail, and the caller won't */
  1666. /* call unmap, so we need to do it here. */
  1667. dma_unmap(memMap, 0);
  1668. }
  1669. }
  1670. return rc;
  1671. }
  1672. EXPORT_SYMBOL(dma_map_mem);
  1673. /****************************************************************************/
  1674. /**
  1675. * Setup a descriptor ring for a given memory map.
  1676. *
  1677. * It is assumed that the descriptor ring has already been initialized, and
  1678. * this routine will only reallocate a new descriptor ring if the existing
  1679. * one is too small.
  1680. *
  1681. * @return 0 on success, error code otherwise.
  1682. */
  1683. /****************************************************************************/
  1684. int dma_map_create_descriptor_ring(DMA_Device_t dev, /* DMA device (where the ring is stored) */
  1685. DMA_MemMap_t *memMap, /* Memory map that will be used */
  1686. dma_addr_t devPhysAddr /* Physical address of device */
  1687. ) {
  1688. int rc;
  1689. int numDescriptors;
  1690. DMA_DeviceAttribute_t *devAttr;
  1691. DMA_Region_t *region;
  1692. DMA_Segment_t *segment;
  1693. dma_addr_t srcPhysAddr;
  1694. dma_addr_t dstPhysAddr;
  1695. int regionIdx;
  1696. int segmentIdx;
  1697. devAttr = &DMA_gDeviceAttribute[dev];
  1698. down(&memMap->lock);
  1699. /* Figure out how many descriptors we need */
  1700. numDescriptors = 0;
  1701. for (regionIdx = 0; regionIdx < memMap->numRegionsUsed; regionIdx++) {
  1702. region = &memMap->region[regionIdx];
  1703. for (segmentIdx = 0; segmentIdx < region->numSegmentsUsed;
  1704. segmentIdx++) {
  1705. segment = &region->segment[segmentIdx];
  1706. if (memMap->dir == DMA_TO_DEVICE) {
  1707. srcPhysAddr = segment->physAddr;
  1708. dstPhysAddr = devPhysAddr;
  1709. } else {
  1710. srcPhysAddr = devPhysAddr;
  1711. dstPhysAddr = segment->physAddr;
  1712. }
  1713. rc =
  1714. dma_calculate_descriptor_count(dev, srcPhysAddr,
  1715. dstPhysAddr,
  1716. segment->
  1717. numBytes);
  1718. if (rc < 0) {
  1719. printk(KERN_ERR
  1720. "%s: dma_calculate_descriptor_count failed: %d\n",
  1721. __func__, rc);
  1722. goto out;
  1723. }
  1724. numDescriptors += rc;
  1725. }
  1726. }
  1727. /* Adjust the size of the ring, if it isn't big enough */
  1728. if (numDescriptors > devAttr->ring.descriptorsAllocated) {
  1729. dma_free_descriptor_ring(&devAttr->ring);
  1730. rc =
  1731. dma_alloc_descriptor_ring(&devAttr->ring,
  1732. numDescriptors);
  1733. if (rc < 0) {
  1734. printk(KERN_ERR
  1735. "%s: dma_alloc_descriptor_ring failed: %d\n",
  1736. __func__, rc);
  1737. goto out;
  1738. }
  1739. } else {
  1740. rc =
  1741. dma_init_descriptor_ring(&devAttr->ring,
  1742. numDescriptors);
  1743. if (rc < 0) {
  1744. printk(KERN_ERR
  1745. "%s: dma_init_descriptor_ring failed: %d\n",
  1746. __func__, rc);
  1747. goto out;
  1748. }
  1749. }
  1750. /* Populate the descriptors */
  1751. for (regionIdx = 0; regionIdx < memMap->numRegionsUsed; regionIdx++) {
  1752. region = &memMap->region[regionIdx];
  1753. for (segmentIdx = 0; segmentIdx < region->numSegmentsUsed;
  1754. segmentIdx++) {
  1755. segment = &region->segment[segmentIdx];
  1756. if (memMap->dir == DMA_TO_DEVICE) {
  1757. srcPhysAddr = segment->physAddr;
  1758. dstPhysAddr = devPhysAddr;
  1759. } else {
  1760. srcPhysAddr = devPhysAddr;
  1761. dstPhysAddr = segment->physAddr;
  1762. }
  1763. rc =
  1764. dma_add_descriptors(&devAttr->ring, dev,
  1765. srcPhysAddr, dstPhysAddr,
  1766. segment->numBytes);
  1767. if (rc < 0) {
  1768. printk(KERN_ERR
  1769. "%s: dma_add_descriptors failed: %d\n",
  1770. __func__, rc);
  1771. goto out;
  1772. }
  1773. }
  1774. }
  1775. rc = 0;
  1776. out:
  1777. up(&memMap->lock);
  1778. return rc;
  1779. }
  1780. EXPORT_SYMBOL(dma_map_create_descriptor_ring);
  1781. /****************************************************************************/
  1782. /**
  1783. * Maps in a memory region such that it can be used for performing a DMA.
  1784. *
  1785. * @return
  1786. */
  1787. /****************************************************************************/
  1788. int dma_unmap(DMA_MemMap_t *memMap, /* Stores state information about the map */
  1789. int dirtied /* non-zero if any of the pages were modified */
  1790. ) {
  1791. int rc = 0;
  1792. int regionIdx;
  1793. int segmentIdx;
  1794. DMA_Region_t *region;
  1795. DMA_Segment_t *segment;
  1796. down(&memMap->lock);
  1797. for (regionIdx = 0; regionIdx < memMap->numRegionsUsed; regionIdx++) {
  1798. region = &memMap->region[regionIdx];
  1799. for (segmentIdx = 0; segmentIdx < region->numSegmentsUsed;
  1800. segmentIdx++) {
  1801. segment = &region->segment[segmentIdx];
  1802. switch (region->memType) {
  1803. case DMA_MEM_TYPE_VMALLOC:
  1804. {
  1805. printk(KERN_ERR
  1806. "%s: vmalloc'd pages are not yet supported\n",
  1807. __func__);
  1808. rc = -EINVAL;
  1809. goto out;
  1810. }
  1811. case DMA_MEM_TYPE_KMALLOC:
  1812. {
  1813. #if ALLOW_MAP_OF_KMALLOC_MEMORY
  1814. dma_unmap_single(NULL,
  1815. segment->physAddr,
  1816. segment->numBytes,
  1817. memMap->dir);
  1818. #endif
  1819. break;
  1820. }
  1821. case DMA_MEM_TYPE_DMA:
  1822. {
  1823. dma_sync_single_for_cpu(NULL,
  1824. segment->
  1825. physAddr,
  1826. segment->
  1827. numBytes,
  1828. memMap->dir);
  1829. break;
  1830. }
  1831. case DMA_MEM_TYPE_USER:
  1832. {
  1833. /* Nothing to do here. */
  1834. break;
  1835. }
  1836. default:
  1837. {
  1838. printk(KERN_ERR
  1839. "%s: Unsupported memory type: %d\n",
  1840. __func__, region->memType);
  1841. rc = -EINVAL;
  1842. goto out;
  1843. }
  1844. }
  1845. segment->virtAddr = NULL;
  1846. segment->physAddr = 0;
  1847. segment->numBytes = 0;
  1848. }
  1849. if (region->numLockedPages > 0) {
  1850. int pageIdx;
  1851. /* Some user pages were locked. We need to go and unlock them now. */
  1852. for (pageIdx = 0; pageIdx < region->numLockedPages;
  1853. pageIdx++) {
  1854. struct page *page =
  1855. region->lockedPages[pageIdx];
  1856. if (memMap->dir == DMA_FROM_DEVICE) {
  1857. SetPageDirty(page);
  1858. }
  1859. page_cache_release(page);
  1860. }
  1861. kfree(region->lockedPages);
  1862. region->numLockedPages = 0;
  1863. region->lockedPages = NULL;
  1864. }
  1865. region->memType = DMA_MEM_TYPE_NONE;
  1866. region->virtAddr = NULL;
  1867. region->numBytes = 0;
  1868. region->numSegmentsUsed = 0;
  1869. }
  1870. memMap->userTask = NULL;
  1871. memMap->numRegionsUsed = 0;
  1872. memMap->inUse = 0;
  1873. out:
  1874. up(&memMap->lock);
  1875. return rc;
  1876. }
  1877. EXPORT_SYMBOL(dma_unmap);