mic_x100_dma.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /*
  2. * Intel MIC Platform Software Stack (MPSS)
  3. *
  4. * Copyright(c) 2014 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License, version 2, as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * The full GNU General Public License is included in this distribution in
  16. * the file called "COPYING".
  17. *
  18. * Intel MIC X100 DMA Driver.
  19. *
  20. * Adapted from IOAT dma driver.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/io.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/vmalloc.h>
  26. #include "mic_x100_dma.h"
  27. #define MIC_DMA_MAX_XFER_SIZE_CARD (1 * 1024 * 1024 -\
  28. MIC_DMA_ALIGN_BYTES)
  29. #define MIC_DMA_MAX_XFER_SIZE_HOST (1 * 1024 * 1024 >> 1)
  30. #define MIC_DMA_DESC_TYPE_SHIFT 60
  31. #define MIC_DMA_MEMCPY_LEN_SHIFT 46
  32. #define MIC_DMA_STAT_INTR_SHIFT 59
  33. /* high-water mark for pushing dma descriptors */
  34. static int mic_dma_pending_level = 4;
  35. /* Status descriptor is used to write a 64 bit value to a memory location */
  36. enum mic_dma_desc_format_type {
  37. MIC_DMA_MEMCPY = 1,
  38. MIC_DMA_STATUS,
  39. };
  40. static inline u32 mic_dma_hw_ring_inc(u32 val)
  41. {
  42. return (val + 1) % MIC_DMA_DESC_RX_SIZE;
  43. }
  44. static inline u32 mic_dma_hw_ring_dec(u32 val)
  45. {
  46. return val ? val - 1 : MIC_DMA_DESC_RX_SIZE - 1;
  47. }
  48. static inline void mic_dma_hw_ring_inc_head(struct mic_dma_chan *ch)
  49. {
  50. ch->head = mic_dma_hw_ring_inc(ch->head);
  51. }
  52. /* Prepare a memcpy desc */
  53. static inline void mic_dma_memcpy_desc(struct mic_dma_desc *desc,
  54. dma_addr_t src_phys, dma_addr_t dst_phys, u64 size)
  55. {
  56. u64 qw0, qw1;
  57. qw0 = src_phys;
  58. qw0 |= (size >> MIC_DMA_ALIGN_SHIFT) << MIC_DMA_MEMCPY_LEN_SHIFT;
  59. qw1 = MIC_DMA_MEMCPY;
  60. qw1 <<= MIC_DMA_DESC_TYPE_SHIFT;
  61. qw1 |= dst_phys;
  62. desc->qw0 = qw0;
  63. desc->qw1 = qw1;
  64. }
  65. /* Prepare a status desc. with @data to be written at @dst_phys */
  66. static inline void mic_dma_prep_status_desc(struct mic_dma_desc *desc, u64 data,
  67. dma_addr_t dst_phys, bool generate_intr)
  68. {
  69. u64 qw0, qw1;
  70. qw0 = data;
  71. qw1 = (u64) MIC_DMA_STATUS << MIC_DMA_DESC_TYPE_SHIFT | dst_phys;
  72. if (generate_intr)
  73. qw1 |= (1ULL << MIC_DMA_STAT_INTR_SHIFT);
  74. desc->qw0 = qw0;
  75. desc->qw1 = qw1;
  76. }
  77. static void mic_dma_cleanup(struct mic_dma_chan *ch)
  78. {
  79. struct dma_async_tx_descriptor *tx;
  80. u32 tail;
  81. u32 last_tail;
  82. spin_lock(&ch->cleanup_lock);
  83. tail = mic_dma_read_cmp_cnt(ch);
  84. /*
  85. * This is the barrier pair for smp_wmb() in fn.
  86. * mic_dma_tx_submit_unlock. It's required so that we read the
  87. * updated cookie value from tx->cookie.
  88. */
  89. smp_rmb();
  90. for (last_tail = ch->last_tail; tail != last_tail;) {
  91. tx = &ch->tx_array[last_tail];
  92. if (tx->cookie) {
  93. dma_cookie_complete(tx);
  94. if (tx->callback) {
  95. tx->callback(tx->callback_param);
  96. tx->callback = NULL;
  97. }
  98. }
  99. last_tail = mic_dma_hw_ring_inc(last_tail);
  100. }
  101. /* finish all completion callbacks before incrementing tail */
  102. smp_mb();
  103. ch->last_tail = last_tail;
  104. spin_unlock(&ch->cleanup_lock);
  105. }
  106. static u32 mic_dma_ring_count(u32 head, u32 tail)
  107. {
  108. u32 count;
  109. if (head >= tail)
  110. count = (tail - 0) + (MIC_DMA_DESC_RX_SIZE - head);
  111. else
  112. count = tail - head;
  113. return count - 1;
  114. }
  115. /* Returns the num. of free descriptors on success, -ENOMEM on failure */
  116. static int mic_dma_avail_desc_ring_space(struct mic_dma_chan *ch, int required)
  117. {
  118. struct device *dev = mic_dma_ch_to_device(ch);
  119. u32 count;
  120. count = mic_dma_ring_count(ch->head, ch->last_tail);
  121. if (count < required) {
  122. mic_dma_cleanup(ch);
  123. count = mic_dma_ring_count(ch->head, ch->last_tail);
  124. }
  125. if (count < required) {
  126. dev_dbg(dev, "Not enough desc space");
  127. dev_dbg(dev, "%s %d required=%u, avail=%u\n",
  128. __func__, __LINE__, required, count);
  129. return -ENOMEM;
  130. } else {
  131. return count;
  132. }
  133. }
  134. /* Program memcpy descriptors into the descriptor ring and update s/w head ptr*/
  135. static int mic_dma_prog_memcpy_desc(struct mic_dma_chan *ch, dma_addr_t src,
  136. dma_addr_t dst, size_t len)
  137. {
  138. size_t current_transfer_len;
  139. size_t max_xfer_size = to_mic_dma_dev(ch)->max_xfer_size;
  140. /* 3 is added to make sure we have enough space for status desc */
  141. int num_desc = len / max_xfer_size + 3;
  142. int ret;
  143. if (len % max_xfer_size)
  144. num_desc++;
  145. ret = mic_dma_avail_desc_ring_space(ch, num_desc);
  146. if (ret < 0)
  147. return ret;
  148. do {
  149. current_transfer_len = min(len, max_xfer_size);
  150. mic_dma_memcpy_desc(&ch->desc_ring[ch->head],
  151. src, dst, current_transfer_len);
  152. mic_dma_hw_ring_inc_head(ch);
  153. len -= current_transfer_len;
  154. dst = dst + current_transfer_len;
  155. src = src + current_transfer_len;
  156. } while (len > 0);
  157. return 0;
  158. }
  159. /* It's a h/w quirk and h/w needs 2 status descriptors for every status desc */
  160. static void mic_dma_prog_intr(struct mic_dma_chan *ch)
  161. {
  162. mic_dma_prep_status_desc(&ch->desc_ring[ch->head], 0,
  163. ch->status_dest_micpa, false);
  164. mic_dma_hw_ring_inc_head(ch);
  165. mic_dma_prep_status_desc(&ch->desc_ring[ch->head], 0,
  166. ch->status_dest_micpa, true);
  167. mic_dma_hw_ring_inc_head(ch);
  168. }
  169. /* Wrapper function to program memcpy descriptors/status descriptors */
  170. static int mic_dma_do_dma(struct mic_dma_chan *ch, int flags, dma_addr_t src,
  171. dma_addr_t dst, size_t len)
  172. {
  173. if (len && -ENOMEM == mic_dma_prog_memcpy_desc(ch, src, dst, len)) {
  174. return -ENOMEM;
  175. } else {
  176. /* 3 is the maximum number of status descriptors */
  177. int ret = mic_dma_avail_desc_ring_space(ch, 3);
  178. if (ret < 0)
  179. return ret;
  180. }
  181. /* Above mic_dma_prog_memcpy_desc() makes sure we have enough space */
  182. if (flags & DMA_PREP_FENCE) {
  183. mic_dma_prep_status_desc(&ch->desc_ring[ch->head], 0,
  184. ch->status_dest_micpa, false);
  185. mic_dma_hw_ring_inc_head(ch);
  186. }
  187. if (flags & DMA_PREP_INTERRUPT)
  188. mic_dma_prog_intr(ch);
  189. return 0;
  190. }
  191. static inline void mic_dma_issue_pending(struct dma_chan *ch)
  192. {
  193. struct mic_dma_chan *mic_ch = to_mic_dma_chan(ch);
  194. spin_lock(&mic_ch->issue_lock);
  195. /*
  196. * Write to head triggers h/w to act on the descriptors.
  197. * On MIC, writing the same head value twice causes
  198. * a h/w error. On second write, h/w assumes we filled
  199. * the entire ring & overwrote some of the descriptors.
  200. */
  201. if (mic_ch->issued == mic_ch->submitted)
  202. goto out;
  203. mic_ch->issued = mic_ch->submitted;
  204. /*
  205. * make descriptor updates visible before advancing head,
  206. * this is purposefully not smp_wmb() since we are also
  207. * publishing the descriptor updates to a dma device
  208. */
  209. wmb();
  210. mic_dma_write_reg(mic_ch, MIC_DMA_REG_DHPR, mic_ch->issued);
  211. out:
  212. spin_unlock(&mic_ch->issue_lock);
  213. }
  214. static inline void mic_dma_update_pending(struct mic_dma_chan *ch)
  215. {
  216. if (mic_dma_ring_count(ch->issued, ch->submitted)
  217. > mic_dma_pending_level)
  218. mic_dma_issue_pending(&ch->api_ch);
  219. }
  220. static dma_cookie_t mic_dma_tx_submit_unlock(struct dma_async_tx_descriptor *tx)
  221. {
  222. struct mic_dma_chan *mic_ch = to_mic_dma_chan(tx->chan);
  223. dma_cookie_t cookie;
  224. dma_cookie_assign(tx);
  225. cookie = tx->cookie;
  226. /*
  227. * We need an smp write barrier here because another CPU might see
  228. * an update to submitted and update h/w head even before we
  229. * assigned a cookie to this tx.
  230. */
  231. smp_wmb();
  232. mic_ch->submitted = mic_ch->head;
  233. spin_unlock(&mic_ch->prep_lock);
  234. mic_dma_update_pending(mic_ch);
  235. return cookie;
  236. }
  237. static inline struct dma_async_tx_descriptor *
  238. allocate_tx(struct mic_dma_chan *ch)
  239. {
  240. u32 idx = mic_dma_hw_ring_dec(ch->head);
  241. struct dma_async_tx_descriptor *tx = &ch->tx_array[idx];
  242. dma_async_tx_descriptor_init(tx, &ch->api_ch);
  243. tx->tx_submit = mic_dma_tx_submit_unlock;
  244. return tx;
  245. }
  246. /* Program a status descriptor with dst as address and value to be written */
  247. static struct dma_async_tx_descriptor *
  248. mic_dma_prep_status_lock(struct dma_chan *ch, dma_addr_t dst, u64 src_val,
  249. unsigned long flags)
  250. {
  251. struct mic_dma_chan *mic_ch = to_mic_dma_chan(ch);
  252. int result;
  253. spin_lock(&mic_ch->prep_lock);
  254. result = mic_dma_avail_desc_ring_space(mic_ch, 4);
  255. if (result < 0)
  256. goto error;
  257. mic_dma_prep_status_desc(&mic_ch->desc_ring[mic_ch->head], src_val, dst,
  258. false);
  259. mic_dma_hw_ring_inc_head(mic_ch);
  260. result = mic_dma_do_dma(mic_ch, flags, 0, 0, 0);
  261. if (result < 0)
  262. goto error;
  263. return allocate_tx(mic_ch);
  264. error:
  265. dev_err(mic_dma_ch_to_device(mic_ch),
  266. "Error enqueueing dma status descriptor, error=%d\n", result);
  267. spin_unlock(&mic_ch->prep_lock);
  268. return NULL;
  269. }
  270. /*
  271. * Prepare a memcpy descriptor to be added to the ring.
  272. * Note that the temporary descriptor adds an extra overhead of copying the
  273. * descriptor to ring. So, we copy directly to the descriptor ring
  274. */
  275. static struct dma_async_tx_descriptor *
  276. mic_dma_prep_memcpy_lock(struct dma_chan *ch, dma_addr_t dma_dest,
  277. dma_addr_t dma_src, size_t len, unsigned long flags)
  278. {
  279. struct mic_dma_chan *mic_ch = to_mic_dma_chan(ch);
  280. struct device *dev = mic_dma_ch_to_device(mic_ch);
  281. int result;
  282. if (!len && !flags)
  283. return NULL;
  284. spin_lock(&mic_ch->prep_lock);
  285. result = mic_dma_do_dma(mic_ch, flags, dma_src, dma_dest, len);
  286. if (result >= 0)
  287. return allocate_tx(mic_ch);
  288. dev_err(dev, "Error enqueueing dma, error=%d\n", result);
  289. spin_unlock(&mic_ch->prep_lock);
  290. return NULL;
  291. }
  292. static struct dma_async_tx_descriptor *
  293. mic_dma_prep_interrupt_lock(struct dma_chan *ch, unsigned long flags)
  294. {
  295. struct mic_dma_chan *mic_ch = to_mic_dma_chan(ch);
  296. int ret;
  297. spin_lock(&mic_ch->prep_lock);
  298. ret = mic_dma_do_dma(mic_ch, flags, 0, 0, 0);
  299. if (!ret)
  300. return allocate_tx(mic_ch);
  301. spin_unlock(&mic_ch->prep_lock);
  302. return NULL;
  303. }
  304. /* Return the status of the transaction */
  305. static enum dma_status
  306. mic_dma_tx_status(struct dma_chan *ch, dma_cookie_t cookie,
  307. struct dma_tx_state *txstate)
  308. {
  309. struct mic_dma_chan *mic_ch = to_mic_dma_chan(ch);
  310. if (DMA_COMPLETE != dma_cookie_status(ch, cookie, txstate))
  311. mic_dma_cleanup(mic_ch);
  312. return dma_cookie_status(ch, cookie, txstate);
  313. }
  314. static irqreturn_t mic_dma_thread_fn(int irq, void *data)
  315. {
  316. mic_dma_cleanup((struct mic_dma_chan *)data);
  317. return IRQ_HANDLED;
  318. }
  319. static irqreturn_t mic_dma_intr_handler(int irq, void *data)
  320. {
  321. struct mic_dma_chan *ch = ((struct mic_dma_chan *)data);
  322. mic_dma_ack_interrupt(ch);
  323. return IRQ_WAKE_THREAD;
  324. }
  325. static int mic_dma_alloc_desc_ring(struct mic_dma_chan *ch)
  326. {
  327. u64 desc_ring_size = MIC_DMA_DESC_RX_SIZE * sizeof(*ch->desc_ring);
  328. struct device *dev = &to_mbus_device(ch)->dev;
  329. desc_ring_size = ALIGN(desc_ring_size, MIC_DMA_ALIGN_BYTES);
  330. ch->desc_ring = kzalloc(desc_ring_size, GFP_KERNEL);
  331. if (!ch->desc_ring)
  332. return -ENOMEM;
  333. ch->desc_ring_micpa = dma_map_single(dev, ch->desc_ring,
  334. desc_ring_size, DMA_BIDIRECTIONAL);
  335. if (dma_mapping_error(dev, ch->desc_ring_micpa))
  336. goto map_error;
  337. ch->tx_array = vzalloc(MIC_DMA_DESC_RX_SIZE * sizeof(*ch->tx_array));
  338. if (!ch->tx_array)
  339. goto tx_error;
  340. return 0;
  341. tx_error:
  342. dma_unmap_single(dev, ch->desc_ring_micpa, desc_ring_size,
  343. DMA_BIDIRECTIONAL);
  344. map_error:
  345. kfree(ch->desc_ring);
  346. return -ENOMEM;
  347. }
  348. static void mic_dma_free_desc_ring(struct mic_dma_chan *ch)
  349. {
  350. u64 desc_ring_size = MIC_DMA_DESC_RX_SIZE * sizeof(*ch->desc_ring);
  351. vfree(ch->tx_array);
  352. desc_ring_size = ALIGN(desc_ring_size, MIC_DMA_ALIGN_BYTES);
  353. dma_unmap_single(&to_mbus_device(ch)->dev, ch->desc_ring_micpa,
  354. desc_ring_size, DMA_BIDIRECTIONAL);
  355. kfree(ch->desc_ring);
  356. ch->desc_ring = NULL;
  357. }
  358. static void mic_dma_free_status_dest(struct mic_dma_chan *ch)
  359. {
  360. dma_unmap_single(&to_mbus_device(ch)->dev, ch->status_dest_micpa,
  361. L1_CACHE_BYTES, DMA_BIDIRECTIONAL);
  362. kfree(ch->status_dest);
  363. }
  364. static int mic_dma_alloc_status_dest(struct mic_dma_chan *ch)
  365. {
  366. struct device *dev = &to_mbus_device(ch)->dev;
  367. ch->status_dest = kzalloc(L1_CACHE_BYTES, GFP_KERNEL);
  368. if (!ch->status_dest)
  369. return -ENOMEM;
  370. ch->status_dest_micpa = dma_map_single(dev, ch->status_dest,
  371. L1_CACHE_BYTES, DMA_BIDIRECTIONAL);
  372. if (dma_mapping_error(dev, ch->status_dest_micpa)) {
  373. kfree(ch->status_dest);
  374. ch->status_dest = NULL;
  375. return -ENOMEM;
  376. }
  377. return 0;
  378. }
  379. static int mic_dma_check_chan(struct mic_dma_chan *ch)
  380. {
  381. if (mic_dma_read_reg(ch, MIC_DMA_REG_DCHERR) ||
  382. mic_dma_read_reg(ch, MIC_DMA_REG_DSTAT) & MIC_DMA_CHAN_QUIESCE) {
  383. mic_dma_disable_chan(ch);
  384. mic_dma_chan_mask_intr(ch);
  385. dev_err(mic_dma_ch_to_device(ch),
  386. "%s %d error setting up mic dma chan %d\n",
  387. __func__, __LINE__, ch->ch_num);
  388. return -EBUSY;
  389. }
  390. return 0;
  391. }
  392. static int mic_dma_chan_setup(struct mic_dma_chan *ch)
  393. {
  394. if (MIC_DMA_CHAN_MIC == ch->owner)
  395. mic_dma_chan_set_owner(ch);
  396. mic_dma_disable_chan(ch);
  397. mic_dma_chan_mask_intr(ch);
  398. mic_dma_write_reg(ch, MIC_DMA_REG_DCHERRMSK, 0);
  399. mic_dma_chan_set_desc_ring(ch);
  400. ch->last_tail = mic_dma_read_reg(ch, MIC_DMA_REG_DTPR);
  401. ch->head = ch->last_tail;
  402. ch->issued = 0;
  403. mic_dma_chan_unmask_intr(ch);
  404. mic_dma_enable_chan(ch);
  405. return mic_dma_check_chan(ch);
  406. }
  407. static void mic_dma_chan_destroy(struct mic_dma_chan *ch)
  408. {
  409. mic_dma_disable_chan(ch);
  410. mic_dma_chan_mask_intr(ch);
  411. }
  412. static void mic_dma_unregister_dma_device(struct mic_dma_device *mic_dma_dev)
  413. {
  414. dma_async_device_unregister(&mic_dma_dev->dma_dev);
  415. }
  416. static int mic_dma_setup_irq(struct mic_dma_chan *ch)
  417. {
  418. ch->cookie =
  419. to_mbus_hw_ops(ch)->request_threaded_irq(to_mbus_device(ch),
  420. mic_dma_intr_handler, mic_dma_thread_fn,
  421. "mic dma_channel", ch, ch->ch_num);
  422. if (IS_ERR(ch->cookie))
  423. return IS_ERR(ch->cookie);
  424. return 0;
  425. }
  426. static inline void mic_dma_free_irq(struct mic_dma_chan *ch)
  427. {
  428. to_mbus_hw_ops(ch)->free_irq(to_mbus_device(ch), ch->cookie, ch);
  429. }
  430. static int mic_dma_chan_init(struct mic_dma_chan *ch)
  431. {
  432. int ret = mic_dma_alloc_desc_ring(ch);
  433. if (ret)
  434. goto ring_error;
  435. ret = mic_dma_alloc_status_dest(ch);
  436. if (ret)
  437. goto status_error;
  438. ret = mic_dma_chan_setup(ch);
  439. if (ret)
  440. goto chan_error;
  441. return ret;
  442. chan_error:
  443. mic_dma_free_status_dest(ch);
  444. status_error:
  445. mic_dma_free_desc_ring(ch);
  446. ring_error:
  447. return ret;
  448. }
  449. static int mic_dma_drain_chan(struct mic_dma_chan *ch)
  450. {
  451. struct dma_async_tx_descriptor *tx;
  452. int err = 0;
  453. dma_cookie_t cookie;
  454. tx = mic_dma_prep_memcpy_lock(&ch->api_ch, 0, 0, 0, DMA_PREP_FENCE);
  455. if (!tx) {
  456. err = -ENOMEM;
  457. goto error;
  458. }
  459. cookie = tx->tx_submit(tx);
  460. if (dma_submit_error(cookie))
  461. err = -ENOMEM;
  462. else
  463. err = dma_sync_wait(&ch->api_ch, cookie);
  464. if (err) {
  465. dev_err(mic_dma_ch_to_device(ch), "%s %d TO chan 0x%x\n",
  466. __func__, __LINE__, ch->ch_num);
  467. err = -EIO;
  468. }
  469. error:
  470. mic_dma_cleanup(ch);
  471. return err;
  472. }
  473. static inline void mic_dma_chan_uninit(struct mic_dma_chan *ch)
  474. {
  475. mic_dma_chan_destroy(ch);
  476. mic_dma_cleanup(ch);
  477. mic_dma_free_status_dest(ch);
  478. mic_dma_free_desc_ring(ch);
  479. }
  480. static int mic_dma_init(struct mic_dma_device *mic_dma_dev,
  481. enum mic_dma_chan_owner owner)
  482. {
  483. int i, first_chan = mic_dma_dev->start_ch;
  484. struct mic_dma_chan *ch;
  485. int ret;
  486. for (i = first_chan; i < first_chan + MIC_DMA_NUM_CHAN; i++) {
  487. unsigned long data;
  488. ch = &mic_dma_dev->mic_ch[i];
  489. data = (unsigned long)ch;
  490. ch->ch_num = i;
  491. ch->owner = owner;
  492. spin_lock_init(&ch->cleanup_lock);
  493. spin_lock_init(&ch->prep_lock);
  494. spin_lock_init(&ch->issue_lock);
  495. ret = mic_dma_setup_irq(ch);
  496. if (ret)
  497. goto error;
  498. }
  499. return 0;
  500. error:
  501. for (i = i - 1; i >= first_chan; i--)
  502. mic_dma_free_irq(ch);
  503. return ret;
  504. }
  505. static void mic_dma_uninit(struct mic_dma_device *mic_dma_dev)
  506. {
  507. int i, first_chan = mic_dma_dev->start_ch;
  508. struct mic_dma_chan *ch;
  509. for (i = first_chan; i < first_chan + MIC_DMA_NUM_CHAN; i++) {
  510. ch = &mic_dma_dev->mic_ch[i];
  511. mic_dma_free_irq(ch);
  512. }
  513. }
  514. static int mic_dma_alloc_chan_resources(struct dma_chan *ch)
  515. {
  516. int ret = mic_dma_chan_init(to_mic_dma_chan(ch));
  517. if (ret)
  518. return ret;
  519. return MIC_DMA_DESC_RX_SIZE;
  520. }
  521. static void mic_dma_free_chan_resources(struct dma_chan *ch)
  522. {
  523. struct mic_dma_chan *mic_ch = to_mic_dma_chan(ch);
  524. mic_dma_drain_chan(mic_ch);
  525. mic_dma_chan_uninit(mic_ch);
  526. }
  527. /* Set the fn. handlers and register the dma device with dma api */
  528. static int mic_dma_register_dma_device(struct mic_dma_device *mic_dma_dev,
  529. enum mic_dma_chan_owner owner)
  530. {
  531. int i, first_chan = mic_dma_dev->start_ch;
  532. dma_cap_zero(mic_dma_dev->dma_dev.cap_mask);
  533. /*
  534. * This dma engine is not capable of host memory to host memory
  535. * transfers
  536. */
  537. dma_cap_set(DMA_MEMCPY, mic_dma_dev->dma_dev.cap_mask);
  538. if (MIC_DMA_CHAN_HOST == owner)
  539. dma_cap_set(DMA_PRIVATE, mic_dma_dev->dma_dev.cap_mask);
  540. mic_dma_dev->dma_dev.device_alloc_chan_resources =
  541. mic_dma_alloc_chan_resources;
  542. mic_dma_dev->dma_dev.device_free_chan_resources =
  543. mic_dma_free_chan_resources;
  544. mic_dma_dev->dma_dev.device_tx_status = mic_dma_tx_status;
  545. mic_dma_dev->dma_dev.device_prep_dma_memcpy = mic_dma_prep_memcpy_lock;
  546. mic_dma_dev->dma_dev.device_prep_dma_imm_data =
  547. mic_dma_prep_status_lock;
  548. mic_dma_dev->dma_dev.device_prep_dma_interrupt =
  549. mic_dma_prep_interrupt_lock;
  550. mic_dma_dev->dma_dev.device_issue_pending = mic_dma_issue_pending;
  551. mic_dma_dev->dma_dev.copy_align = MIC_DMA_ALIGN_SHIFT;
  552. INIT_LIST_HEAD(&mic_dma_dev->dma_dev.channels);
  553. for (i = first_chan; i < first_chan + MIC_DMA_NUM_CHAN; i++) {
  554. mic_dma_dev->mic_ch[i].api_ch.device = &mic_dma_dev->dma_dev;
  555. dma_cookie_init(&mic_dma_dev->mic_ch[i].api_ch);
  556. list_add_tail(&mic_dma_dev->mic_ch[i].api_ch.device_node,
  557. &mic_dma_dev->dma_dev.channels);
  558. }
  559. return dma_async_device_register(&mic_dma_dev->dma_dev);
  560. }
  561. /*
  562. * Initializes dma channels and registers the dma device with the
  563. * dma engine api.
  564. */
  565. static struct mic_dma_device *mic_dma_dev_reg(struct mbus_device *mbdev,
  566. enum mic_dma_chan_owner owner)
  567. {
  568. struct mic_dma_device *mic_dma_dev;
  569. int ret;
  570. struct device *dev = &mbdev->dev;
  571. mic_dma_dev = kzalloc(sizeof(*mic_dma_dev), GFP_KERNEL);
  572. if (!mic_dma_dev) {
  573. ret = -ENOMEM;
  574. goto alloc_error;
  575. }
  576. mic_dma_dev->mbdev = mbdev;
  577. mic_dma_dev->dma_dev.dev = dev;
  578. mic_dma_dev->mmio = mbdev->mmio_va;
  579. if (MIC_DMA_CHAN_HOST == owner) {
  580. mic_dma_dev->start_ch = 0;
  581. mic_dma_dev->max_xfer_size = MIC_DMA_MAX_XFER_SIZE_HOST;
  582. } else {
  583. mic_dma_dev->start_ch = 4;
  584. mic_dma_dev->max_xfer_size = MIC_DMA_MAX_XFER_SIZE_CARD;
  585. }
  586. ret = mic_dma_init(mic_dma_dev, owner);
  587. if (ret)
  588. goto init_error;
  589. ret = mic_dma_register_dma_device(mic_dma_dev, owner);
  590. if (ret)
  591. goto reg_error;
  592. return mic_dma_dev;
  593. reg_error:
  594. mic_dma_uninit(mic_dma_dev);
  595. init_error:
  596. kfree(mic_dma_dev);
  597. mic_dma_dev = NULL;
  598. alloc_error:
  599. dev_err(dev, "Error at %s %d ret=%d\n", __func__, __LINE__, ret);
  600. return mic_dma_dev;
  601. }
  602. static void mic_dma_dev_unreg(struct mic_dma_device *mic_dma_dev)
  603. {
  604. mic_dma_unregister_dma_device(mic_dma_dev);
  605. mic_dma_uninit(mic_dma_dev);
  606. kfree(mic_dma_dev);
  607. }
  608. /* DEBUGFS CODE */
  609. static int mic_dma_reg_seq_show(struct seq_file *s, void *pos)
  610. {
  611. struct mic_dma_device *mic_dma_dev = s->private;
  612. int i, chan_num, first_chan = mic_dma_dev->start_ch;
  613. struct mic_dma_chan *ch;
  614. seq_printf(s, "SBOX_DCR: %#x\n",
  615. mic_dma_mmio_read(&mic_dma_dev->mic_ch[first_chan],
  616. MIC_DMA_SBOX_BASE + MIC_DMA_SBOX_DCR));
  617. seq_puts(s, "DMA Channel Registers\n");
  618. seq_printf(s, "%-10s| %-10s %-10s %-10s %-10s %-10s",
  619. "Channel", "DCAR", "DTPR", "DHPR", "DRAR_HI", "DRAR_LO");
  620. seq_printf(s, " %-11s %-14s %-10s\n", "DCHERR", "DCHERRMSK", "DSTAT");
  621. for (i = first_chan; i < first_chan + MIC_DMA_NUM_CHAN; i++) {
  622. ch = &mic_dma_dev->mic_ch[i];
  623. chan_num = ch->ch_num;
  624. seq_printf(s, "%-10i| %-#10x %-#10x %-#10x %-#10x",
  625. chan_num,
  626. mic_dma_read_reg(ch, MIC_DMA_REG_DCAR),
  627. mic_dma_read_reg(ch, MIC_DMA_REG_DTPR),
  628. mic_dma_read_reg(ch, MIC_DMA_REG_DHPR),
  629. mic_dma_read_reg(ch, MIC_DMA_REG_DRAR_HI));
  630. seq_printf(s, " %-#10x %-#10x %-#14x %-#10x\n",
  631. mic_dma_read_reg(ch, MIC_DMA_REG_DRAR_LO),
  632. mic_dma_read_reg(ch, MIC_DMA_REG_DCHERR),
  633. mic_dma_read_reg(ch, MIC_DMA_REG_DCHERRMSK),
  634. mic_dma_read_reg(ch, MIC_DMA_REG_DSTAT));
  635. }
  636. return 0;
  637. }
  638. static int mic_dma_reg_debug_open(struct inode *inode, struct file *file)
  639. {
  640. return single_open(file, mic_dma_reg_seq_show, inode->i_private);
  641. }
  642. static int mic_dma_reg_debug_release(struct inode *inode, struct file *file)
  643. {
  644. return single_release(inode, file);
  645. }
  646. static const struct file_operations mic_dma_reg_ops = {
  647. .owner = THIS_MODULE,
  648. .open = mic_dma_reg_debug_open,
  649. .read = seq_read,
  650. .llseek = seq_lseek,
  651. .release = mic_dma_reg_debug_release
  652. };
  653. /* Debugfs parent dir */
  654. static struct dentry *mic_dma_dbg;
  655. static int mic_dma_driver_probe(struct mbus_device *mbdev)
  656. {
  657. struct mic_dma_device *mic_dma_dev;
  658. enum mic_dma_chan_owner owner;
  659. if (MBUS_DEV_DMA_MIC == mbdev->id.device)
  660. owner = MIC_DMA_CHAN_MIC;
  661. else
  662. owner = MIC_DMA_CHAN_HOST;
  663. mic_dma_dev = mic_dma_dev_reg(mbdev, owner);
  664. dev_set_drvdata(&mbdev->dev, mic_dma_dev);
  665. if (mic_dma_dbg) {
  666. mic_dma_dev->dbg_dir = debugfs_create_dir(dev_name(&mbdev->dev),
  667. mic_dma_dbg);
  668. if (mic_dma_dev->dbg_dir)
  669. debugfs_create_file("mic_dma_reg", 0444,
  670. mic_dma_dev->dbg_dir, mic_dma_dev,
  671. &mic_dma_reg_ops);
  672. }
  673. return 0;
  674. }
  675. static void mic_dma_driver_remove(struct mbus_device *mbdev)
  676. {
  677. struct mic_dma_device *mic_dma_dev;
  678. mic_dma_dev = dev_get_drvdata(&mbdev->dev);
  679. debugfs_remove_recursive(mic_dma_dev->dbg_dir);
  680. mic_dma_dev_unreg(mic_dma_dev);
  681. }
  682. static struct mbus_device_id id_table[] = {
  683. {MBUS_DEV_DMA_MIC, MBUS_DEV_ANY_ID},
  684. {MBUS_DEV_DMA_HOST, MBUS_DEV_ANY_ID},
  685. {0},
  686. };
  687. static struct mbus_driver mic_dma_driver = {
  688. .driver.name = KBUILD_MODNAME,
  689. .driver.owner = THIS_MODULE,
  690. .id_table = id_table,
  691. .probe = mic_dma_driver_probe,
  692. .remove = mic_dma_driver_remove,
  693. };
  694. static int __init mic_x100_dma_init(void)
  695. {
  696. int rc = mbus_register_driver(&mic_dma_driver);
  697. if (rc)
  698. return rc;
  699. mic_dma_dbg = debugfs_create_dir(KBUILD_MODNAME, NULL);
  700. return 0;
  701. }
  702. static void __exit mic_x100_dma_exit(void)
  703. {
  704. debugfs_remove_recursive(mic_dma_dbg);
  705. mbus_unregister_driver(&mic_dma_driver);
  706. }
  707. module_init(mic_x100_dma_init);
  708. module_exit(mic_x100_dma_exit);
  709. MODULE_DEVICE_TABLE(mbus, id_table);
  710. MODULE_AUTHOR("Intel Corporation");
  711. MODULE_DESCRIPTION("Intel(R) MIC X100 DMA Driver");
  712. MODULE_LICENSE("GPL v2");