mic_x100_dma.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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 (-ENOMEM == mic_dma_prog_memcpy_desc(ch, src, dst, len))
  174. return -ENOMEM;
  175. /* Above mic_dma_prog_memcpy_desc() makes sure we have enough space */
  176. if (flags & DMA_PREP_FENCE) {
  177. mic_dma_prep_status_desc(&ch->desc_ring[ch->head], 0,
  178. ch->status_dest_micpa, false);
  179. mic_dma_hw_ring_inc_head(ch);
  180. }
  181. if (flags & DMA_PREP_INTERRUPT)
  182. mic_dma_prog_intr(ch);
  183. return 0;
  184. }
  185. static inline void mic_dma_issue_pending(struct dma_chan *ch)
  186. {
  187. struct mic_dma_chan *mic_ch = to_mic_dma_chan(ch);
  188. spin_lock(&mic_ch->issue_lock);
  189. /*
  190. * Write to head triggers h/w to act on the descriptors.
  191. * On MIC, writing the same head value twice causes
  192. * a h/w error. On second write, h/w assumes we filled
  193. * the entire ring & overwrote some of the descriptors.
  194. */
  195. if (mic_ch->issued == mic_ch->submitted)
  196. goto out;
  197. mic_ch->issued = mic_ch->submitted;
  198. /*
  199. * make descriptor updates visible before advancing head,
  200. * this is purposefully not smp_wmb() since we are also
  201. * publishing the descriptor updates to a dma device
  202. */
  203. wmb();
  204. mic_dma_write_reg(mic_ch, MIC_DMA_REG_DHPR, mic_ch->issued);
  205. out:
  206. spin_unlock(&mic_ch->issue_lock);
  207. }
  208. static inline void mic_dma_update_pending(struct mic_dma_chan *ch)
  209. {
  210. if (mic_dma_ring_count(ch->issued, ch->submitted)
  211. > mic_dma_pending_level)
  212. mic_dma_issue_pending(&ch->api_ch);
  213. }
  214. static dma_cookie_t mic_dma_tx_submit_unlock(struct dma_async_tx_descriptor *tx)
  215. {
  216. struct mic_dma_chan *mic_ch = to_mic_dma_chan(tx->chan);
  217. dma_cookie_t cookie;
  218. dma_cookie_assign(tx);
  219. cookie = tx->cookie;
  220. /*
  221. * We need an smp write barrier here because another CPU might see
  222. * an update to submitted and update h/w head even before we
  223. * assigned a cookie to this tx.
  224. */
  225. smp_wmb();
  226. mic_ch->submitted = mic_ch->head;
  227. spin_unlock(&mic_ch->prep_lock);
  228. mic_dma_update_pending(mic_ch);
  229. return cookie;
  230. }
  231. static inline struct dma_async_tx_descriptor *
  232. allocate_tx(struct mic_dma_chan *ch)
  233. {
  234. u32 idx = mic_dma_hw_ring_dec(ch->head);
  235. struct dma_async_tx_descriptor *tx = &ch->tx_array[idx];
  236. dma_async_tx_descriptor_init(tx, &ch->api_ch);
  237. tx->tx_submit = mic_dma_tx_submit_unlock;
  238. return tx;
  239. }
  240. /*
  241. * Prepare a memcpy descriptor to be added to the ring.
  242. * Note that the temporary descriptor adds an extra overhead of copying the
  243. * descriptor to ring. So, we copy directly to the descriptor ring
  244. */
  245. static struct dma_async_tx_descriptor *
  246. mic_dma_prep_memcpy_lock(struct dma_chan *ch, dma_addr_t dma_dest,
  247. dma_addr_t dma_src, size_t len, unsigned long flags)
  248. {
  249. struct mic_dma_chan *mic_ch = to_mic_dma_chan(ch);
  250. struct device *dev = mic_dma_ch_to_device(mic_ch);
  251. int result;
  252. if (!len && !flags)
  253. return NULL;
  254. spin_lock(&mic_ch->prep_lock);
  255. result = mic_dma_do_dma(mic_ch, flags, dma_src, dma_dest, len);
  256. if (result >= 0)
  257. return allocate_tx(mic_ch);
  258. dev_err(dev, "Error enqueueing dma, error=%d\n", result);
  259. spin_unlock(&mic_ch->prep_lock);
  260. return NULL;
  261. }
  262. static struct dma_async_tx_descriptor *
  263. mic_dma_prep_interrupt_lock(struct dma_chan *ch, unsigned long flags)
  264. {
  265. struct mic_dma_chan *mic_ch = to_mic_dma_chan(ch);
  266. int ret;
  267. spin_lock(&mic_ch->prep_lock);
  268. ret = mic_dma_do_dma(mic_ch, flags, 0, 0, 0);
  269. if (!ret)
  270. return allocate_tx(mic_ch);
  271. spin_unlock(&mic_ch->prep_lock);
  272. return NULL;
  273. }
  274. /* Return the status of the transaction */
  275. static enum dma_status
  276. mic_dma_tx_status(struct dma_chan *ch, dma_cookie_t cookie,
  277. struct dma_tx_state *txstate)
  278. {
  279. struct mic_dma_chan *mic_ch = to_mic_dma_chan(ch);
  280. if (DMA_COMPLETE != dma_cookie_status(ch, cookie, txstate))
  281. mic_dma_cleanup(mic_ch);
  282. return dma_cookie_status(ch, cookie, txstate);
  283. }
  284. static irqreturn_t mic_dma_thread_fn(int irq, void *data)
  285. {
  286. mic_dma_cleanup((struct mic_dma_chan *)data);
  287. return IRQ_HANDLED;
  288. }
  289. static irqreturn_t mic_dma_intr_handler(int irq, void *data)
  290. {
  291. struct mic_dma_chan *ch = ((struct mic_dma_chan *)data);
  292. mic_dma_ack_interrupt(ch);
  293. return IRQ_WAKE_THREAD;
  294. }
  295. static int mic_dma_alloc_desc_ring(struct mic_dma_chan *ch)
  296. {
  297. u64 desc_ring_size = MIC_DMA_DESC_RX_SIZE * sizeof(*ch->desc_ring);
  298. struct device *dev = &to_mbus_device(ch)->dev;
  299. desc_ring_size = ALIGN(desc_ring_size, MIC_DMA_ALIGN_BYTES);
  300. ch->desc_ring = kzalloc(desc_ring_size, GFP_KERNEL);
  301. if (!ch->desc_ring)
  302. return -ENOMEM;
  303. ch->desc_ring_micpa = dma_map_single(dev, ch->desc_ring,
  304. desc_ring_size, DMA_BIDIRECTIONAL);
  305. if (dma_mapping_error(dev, ch->desc_ring_micpa))
  306. goto map_error;
  307. ch->tx_array = vzalloc(MIC_DMA_DESC_RX_SIZE * sizeof(*ch->tx_array));
  308. if (!ch->tx_array)
  309. goto tx_error;
  310. return 0;
  311. tx_error:
  312. dma_unmap_single(dev, ch->desc_ring_micpa, desc_ring_size,
  313. DMA_BIDIRECTIONAL);
  314. map_error:
  315. kfree(ch->desc_ring);
  316. return -ENOMEM;
  317. }
  318. static void mic_dma_free_desc_ring(struct mic_dma_chan *ch)
  319. {
  320. u64 desc_ring_size = MIC_DMA_DESC_RX_SIZE * sizeof(*ch->desc_ring);
  321. vfree(ch->tx_array);
  322. desc_ring_size = ALIGN(desc_ring_size, MIC_DMA_ALIGN_BYTES);
  323. dma_unmap_single(&to_mbus_device(ch)->dev, ch->desc_ring_micpa,
  324. desc_ring_size, DMA_BIDIRECTIONAL);
  325. kfree(ch->desc_ring);
  326. ch->desc_ring = NULL;
  327. }
  328. static void mic_dma_free_status_dest(struct mic_dma_chan *ch)
  329. {
  330. dma_unmap_single(&to_mbus_device(ch)->dev, ch->status_dest_micpa,
  331. L1_CACHE_BYTES, DMA_BIDIRECTIONAL);
  332. kfree(ch->status_dest);
  333. }
  334. static int mic_dma_alloc_status_dest(struct mic_dma_chan *ch)
  335. {
  336. struct device *dev = &to_mbus_device(ch)->dev;
  337. ch->status_dest = kzalloc(L1_CACHE_BYTES, GFP_KERNEL);
  338. if (!ch->status_dest)
  339. return -ENOMEM;
  340. ch->status_dest_micpa = dma_map_single(dev, ch->status_dest,
  341. L1_CACHE_BYTES, DMA_BIDIRECTIONAL);
  342. if (dma_mapping_error(dev, ch->status_dest_micpa)) {
  343. kfree(ch->status_dest);
  344. ch->status_dest = NULL;
  345. return -ENOMEM;
  346. }
  347. return 0;
  348. }
  349. static int mic_dma_check_chan(struct mic_dma_chan *ch)
  350. {
  351. if (mic_dma_read_reg(ch, MIC_DMA_REG_DCHERR) ||
  352. mic_dma_read_reg(ch, MIC_DMA_REG_DSTAT) & MIC_DMA_CHAN_QUIESCE) {
  353. mic_dma_disable_chan(ch);
  354. mic_dma_chan_mask_intr(ch);
  355. dev_err(mic_dma_ch_to_device(ch),
  356. "%s %d error setting up mic dma chan %d\n",
  357. __func__, __LINE__, ch->ch_num);
  358. return -EBUSY;
  359. }
  360. return 0;
  361. }
  362. static int mic_dma_chan_setup(struct mic_dma_chan *ch)
  363. {
  364. if (MIC_DMA_CHAN_MIC == ch->owner)
  365. mic_dma_chan_set_owner(ch);
  366. mic_dma_disable_chan(ch);
  367. mic_dma_chan_mask_intr(ch);
  368. mic_dma_write_reg(ch, MIC_DMA_REG_DCHERRMSK, 0);
  369. mic_dma_chan_set_desc_ring(ch);
  370. ch->last_tail = mic_dma_read_reg(ch, MIC_DMA_REG_DTPR);
  371. ch->head = ch->last_tail;
  372. ch->issued = 0;
  373. mic_dma_chan_unmask_intr(ch);
  374. mic_dma_enable_chan(ch);
  375. return mic_dma_check_chan(ch);
  376. }
  377. static void mic_dma_chan_destroy(struct mic_dma_chan *ch)
  378. {
  379. mic_dma_disable_chan(ch);
  380. mic_dma_chan_mask_intr(ch);
  381. }
  382. static void mic_dma_unregister_dma_device(struct mic_dma_device *mic_dma_dev)
  383. {
  384. dma_async_device_unregister(&mic_dma_dev->dma_dev);
  385. }
  386. static int mic_dma_setup_irq(struct mic_dma_chan *ch)
  387. {
  388. ch->cookie =
  389. to_mbus_hw_ops(ch)->request_threaded_irq(to_mbus_device(ch),
  390. mic_dma_intr_handler, mic_dma_thread_fn,
  391. "mic dma_channel", ch, ch->ch_num);
  392. if (IS_ERR(ch->cookie))
  393. return IS_ERR(ch->cookie);
  394. return 0;
  395. }
  396. static inline void mic_dma_free_irq(struct mic_dma_chan *ch)
  397. {
  398. to_mbus_hw_ops(ch)->free_irq(to_mbus_device(ch), ch->cookie, ch);
  399. }
  400. static int mic_dma_chan_init(struct mic_dma_chan *ch)
  401. {
  402. int ret = mic_dma_alloc_desc_ring(ch);
  403. if (ret)
  404. goto ring_error;
  405. ret = mic_dma_alloc_status_dest(ch);
  406. if (ret)
  407. goto status_error;
  408. ret = mic_dma_chan_setup(ch);
  409. if (ret)
  410. goto chan_error;
  411. return ret;
  412. chan_error:
  413. mic_dma_free_status_dest(ch);
  414. status_error:
  415. mic_dma_free_desc_ring(ch);
  416. ring_error:
  417. return ret;
  418. }
  419. static int mic_dma_drain_chan(struct mic_dma_chan *ch)
  420. {
  421. struct dma_async_tx_descriptor *tx;
  422. int err = 0;
  423. dma_cookie_t cookie;
  424. tx = mic_dma_prep_memcpy_lock(&ch->api_ch, 0, 0, 0, DMA_PREP_FENCE);
  425. if (!tx) {
  426. err = -ENOMEM;
  427. goto error;
  428. }
  429. cookie = tx->tx_submit(tx);
  430. if (dma_submit_error(cookie))
  431. err = -ENOMEM;
  432. else
  433. err = dma_sync_wait(&ch->api_ch, cookie);
  434. if (err) {
  435. dev_err(mic_dma_ch_to_device(ch), "%s %d TO chan 0x%x\n",
  436. __func__, __LINE__, ch->ch_num);
  437. err = -EIO;
  438. }
  439. error:
  440. mic_dma_cleanup(ch);
  441. return err;
  442. }
  443. static inline void mic_dma_chan_uninit(struct mic_dma_chan *ch)
  444. {
  445. mic_dma_chan_destroy(ch);
  446. mic_dma_cleanup(ch);
  447. mic_dma_free_status_dest(ch);
  448. mic_dma_free_desc_ring(ch);
  449. }
  450. static int mic_dma_init(struct mic_dma_device *mic_dma_dev,
  451. enum mic_dma_chan_owner owner)
  452. {
  453. int i, first_chan = mic_dma_dev->start_ch;
  454. struct mic_dma_chan *ch;
  455. int ret;
  456. for (i = first_chan; i < first_chan + MIC_DMA_NUM_CHAN; i++) {
  457. unsigned long data;
  458. ch = &mic_dma_dev->mic_ch[i];
  459. data = (unsigned long)ch;
  460. ch->ch_num = i;
  461. ch->owner = owner;
  462. spin_lock_init(&ch->cleanup_lock);
  463. spin_lock_init(&ch->prep_lock);
  464. spin_lock_init(&ch->issue_lock);
  465. ret = mic_dma_setup_irq(ch);
  466. if (ret)
  467. goto error;
  468. }
  469. return 0;
  470. error:
  471. for (i = i - 1; i >= first_chan; i--)
  472. mic_dma_free_irq(ch);
  473. return ret;
  474. }
  475. static void mic_dma_uninit(struct mic_dma_device *mic_dma_dev)
  476. {
  477. int i, first_chan = mic_dma_dev->start_ch;
  478. struct mic_dma_chan *ch;
  479. for (i = first_chan; i < first_chan + MIC_DMA_NUM_CHAN; i++) {
  480. ch = &mic_dma_dev->mic_ch[i];
  481. mic_dma_free_irq(ch);
  482. }
  483. }
  484. static int mic_dma_alloc_chan_resources(struct dma_chan *ch)
  485. {
  486. int ret = mic_dma_chan_init(to_mic_dma_chan(ch));
  487. if (ret)
  488. return ret;
  489. return MIC_DMA_DESC_RX_SIZE;
  490. }
  491. static void mic_dma_free_chan_resources(struct dma_chan *ch)
  492. {
  493. struct mic_dma_chan *mic_ch = to_mic_dma_chan(ch);
  494. mic_dma_drain_chan(mic_ch);
  495. mic_dma_chan_uninit(mic_ch);
  496. }
  497. /* Set the fn. handlers and register the dma device with dma api */
  498. static int mic_dma_register_dma_device(struct mic_dma_device *mic_dma_dev,
  499. enum mic_dma_chan_owner owner)
  500. {
  501. int i, first_chan = mic_dma_dev->start_ch;
  502. dma_cap_zero(mic_dma_dev->dma_dev.cap_mask);
  503. /*
  504. * This dma engine is not capable of host memory to host memory
  505. * transfers
  506. */
  507. dma_cap_set(DMA_MEMCPY, mic_dma_dev->dma_dev.cap_mask);
  508. if (MIC_DMA_CHAN_HOST == owner)
  509. dma_cap_set(DMA_PRIVATE, mic_dma_dev->dma_dev.cap_mask);
  510. mic_dma_dev->dma_dev.device_alloc_chan_resources =
  511. mic_dma_alloc_chan_resources;
  512. mic_dma_dev->dma_dev.device_free_chan_resources =
  513. mic_dma_free_chan_resources;
  514. mic_dma_dev->dma_dev.device_tx_status = mic_dma_tx_status;
  515. mic_dma_dev->dma_dev.device_prep_dma_memcpy = mic_dma_prep_memcpy_lock;
  516. mic_dma_dev->dma_dev.device_prep_dma_interrupt =
  517. mic_dma_prep_interrupt_lock;
  518. mic_dma_dev->dma_dev.device_issue_pending = mic_dma_issue_pending;
  519. mic_dma_dev->dma_dev.copy_align = MIC_DMA_ALIGN_SHIFT;
  520. INIT_LIST_HEAD(&mic_dma_dev->dma_dev.channels);
  521. for (i = first_chan; i < first_chan + MIC_DMA_NUM_CHAN; i++) {
  522. mic_dma_dev->mic_ch[i].api_ch.device = &mic_dma_dev->dma_dev;
  523. dma_cookie_init(&mic_dma_dev->mic_ch[i].api_ch);
  524. list_add_tail(&mic_dma_dev->mic_ch[i].api_ch.device_node,
  525. &mic_dma_dev->dma_dev.channels);
  526. }
  527. return dma_async_device_register(&mic_dma_dev->dma_dev);
  528. }
  529. /*
  530. * Initializes dma channels and registers the dma device with the
  531. * dma engine api.
  532. */
  533. static struct mic_dma_device *mic_dma_dev_reg(struct mbus_device *mbdev,
  534. enum mic_dma_chan_owner owner)
  535. {
  536. struct mic_dma_device *mic_dma_dev;
  537. int ret;
  538. struct device *dev = &mbdev->dev;
  539. mic_dma_dev = kzalloc(sizeof(*mic_dma_dev), GFP_KERNEL);
  540. if (!mic_dma_dev) {
  541. ret = -ENOMEM;
  542. goto alloc_error;
  543. }
  544. mic_dma_dev->mbdev = mbdev;
  545. mic_dma_dev->dma_dev.dev = dev;
  546. mic_dma_dev->mmio = mbdev->mmio_va;
  547. if (MIC_DMA_CHAN_HOST == owner) {
  548. mic_dma_dev->start_ch = 0;
  549. mic_dma_dev->max_xfer_size = MIC_DMA_MAX_XFER_SIZE_HOST;
  550. } else {
  551. mic_dma_dev->start_ch = 4;
  552. mic_dma_dev->max_xfer_size = MIC_DMA_MAX_XFER_SIZE_CARD;
  553. }
  554. ret = mic_dma_init(mic_dma_dev, owner);
  555. if (ret)
  556. goto init_error;
  557. ret = mic_dma_register_dma_device(mic_dma_dev, owner);
  558. if (ret)
  559. goto reg_error;
  560. return mic_dma_dev;
  561. reg_error:
  562. mic_dma_uninit(mic_dma_dev);
  563. init_error:
  564. kfree(mic_dma_dev);
  565. mic_dma_dev = NULL;
  566. alloc_error:
  567. dev_err(dev, "Error at %s %d ret=%d\n", __func__, __LINE__, ret);
  568. return mic_dma_dev;
  569. }
  570. static void mic_dma_dev_unreg(struct mic_dma_device *mic_dma_dev)
  571. {
  572. mic_dma_unregister_dma_device(mic_dma_dev);
  573. mic_dma_uninit(mic_dma_dev);
  574. kfree(mic_dma_dev);
  575. }
  576. /* DEBUGFS CODE */
  577. static int mic_dma_reg_seq_show(struct seq_file *s, void *pos)
  578. {
  579. struct mic_dma_device *mic_dma_dev = s->private;
  580. int i, chan_num, first_chan = mic_dma_dev->start_ch;
  581. struct mic_dma_chan *ch;
  582. seq_printf(s, "SBOX_DCR: %#x\n",
  583. mic_dma_mmio_read(&mic_dma_dev->mic_ch[first_chan],
  584. MIC_DMA_SBOX_BASE + MIC_DMA_SBOX_DCR));
  585. seq_puts(s, "DMA Channel Registers\n");
  586. seq_printf(s, "%-10s| %-10s %-10s %-10s %-10s %-10s",
  587. "Channel", "DCAR", "DTPR", "DHPR", "DRAR_HI", "DRAR_LO");
  588. seq_printf(s, " %-11s %-14s %-10s\n", "DCHERR", "DCHERRMSK", "DSTAT");
  589. for (i = first_chan; i < first_chan + MIC_DMA_NUM_CHAN; i++) {
  590. ch = &mic_dma_dev->mic_ch[i];
  591. chan_num = ch->ch_num;
  592. seq_printf(s, "%-10i| %-#10x %-#10x %-#10x %-#10x",
  593. chan_num,
  594. mic_dma_read_reg(ch, MIC_DMA_REG_DCAR),
  595. mic_dma_read_reg(ch, MIC_DMA_REG_DTPR),
  596. mic_dma_read_reg(ch, MIC_DMA_REG_DHPR),
  597. mic_dma_read_reg(ch, MIC_DMA_REG_DRAR_HI));
  598. seq_printf(s, " %-#10x %-#10x %-#14x %-#10x\n",
  599. mic_dma_read_reg(ch, MIC_DMA_REG_DRAR_LO),
  600. mic_dma_read_reg(ch, MIC_DMA_REG_DCHERR),
  601. mic_dma_read_reg(ch, MIC_DMA_REG_DCHERRMSK),
  602. mic_dma_read_reg(ch, MIC_DMA_REG_DSTAT));
  603. }
  604. return 0;
  605. }
  606. static int mic_dma_reg_debug_open(struct inode *inode, struct file *file)
  607. {
  608. return single_open(file, mic_dma_reg_seq_show, inode->i_private);
  609. }
  610. static int mic_dma_reg_debug_release(struct inode *inode, struct file *file)
  611. {
  612. return single_release(inode, file);
  613. }
  614. static const struct file_operations mic_dma_reg_ops = {
  615. .owner = THIS_MODULE,
  616. .open = mic_dma_reg_debug_open,
  617. .read = seq_read,
  618. .llseek = seq_lseek,
  619. .release = mic_dma_reg_debug_release
  620. };
  621. /* Debugfs parent dir */
  622. static struct dentry *mic_dma_dbg;
  623. static int mic_dma_driver_probe(struct mbus_device *mbdev)
  624. {
  625. struct mic_dma_device *mic_dma_dev;
  626. enum mic_dma_chan_owner owner;
  627. if (MBUS_DEV_DMA_MIC == mbdev->id.device)
  628. owner = MIC_DMA_CHAN_MIC;
  629. else
  630. owner = MIC_DMA_CHAN_HOST;
  631. mic_dma_dev = mic_dma_dev_reg(mbdev, owner);
  632. dev_set_drvdata(&mbdev->dev, mic_dma_dev);
  633. if (mic_dma_dbg) {
  634. mic_dma_dev->dbg_dir = debugfs_create_dir(dev_name(&mbdev->dev),
  635. mic_dma_dbg);
  636. if (mic_dma_dev->dbg_dir)
  637. debugfs_create_file("mic_dma_reg", 0444,
  638. mic_dma_dev->dbg_dir, mic_dma_dev,
  639. &mic_dma_reg_ops);
  640. }
  641. return 0;
  642. }
  643. static void mic_dma_driver_remove(struct mbus_device *mbdev)
  644. {
  645. struct mic_dma_device *mic_dma_dev;
  646. mic_dma_dev = dev_get_drvdata(&mbdev->dev);
  647. debugfs_remove_recursive(mic_dma_dev->dbg_dir);
  648. mic_dma_dev_unreg(mic_dma_dev);
  649. }
  650. static struct mbus_device_id id_table[] = {
  651. {MBUS_DEV_DMA_MIC, MBUS_DEV_ANY_ID},
  652. {MBUS_DEV_DMA_HOST, MBUS_DEV_ANY_ID},
  653. {0},
  654. };
  655. static struct mbus_driver mic_dma_driver = {
  656. .driver.name = KBUILD_MODNAME,
  657. .driver.owner = THIS_MODULE,
  658. .id_table = id_table,
  659. .probe = mic_dma_driver_probe,
  660. .remove = mic_dma_driver_remove,
  661. };
  662. static int __init mic_x100_dma_init(void)
  663. {
  664. int rc = mbus_register_driver(&mic_dma_driver);
  665. if (rc)
  666. return rc;
  667. mic_dma_dbg = debugfs_create_dir(KBUILD_MODNAME, NULL);
  668. return 0;
  669. }
  670. static void __exit mic_x100_dma_exit(void)
  671. {
  672. debugfs_remove_recursive(mic_dma_dbg);
  673. mbus_unregister_driver(&mic_dma_driver);
  674. }
  675. module_init(mic_x100_dma_init);
  676. module_exit(mic_x100_dma_exit);
  677. MODULE_DEVICE_TABLE(mbus, id_table);
  678. MODULE_AUTHOR("Intel Corporation");
  679. MODULE_DESCRIPTION("Intel(R) MIC X100 DMA Driver");
  680. MODULE_LICENSE("GPL v2");