dma.c 62 KB

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