mic_x100_dma.c 21 KB

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