mic_x100_dma.c 21 KB

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