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