dma.c 62 KB

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