ioat_dma.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*
  2. * Intel I/OAT DMA Linux driver
  3. * Copyright(c) 2004 - 2007 Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. */
  22. /*
  23. * This driver supports an Intel I/OAT DMA engine, which does asynchronous
  24. * copy operations.
  25. */
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/dmaengine.h>
  31. #include <linux/delay.h>
  32. #include <linux/dma-mapping.h>
  33. #include "ioatdma.h"
  34. #include "ioatdma_registers.h"
  35. #include "ioatdma_hw.h"
  36. #define INITIAL_IOAT_DESC_COUNT 128
  37. #define to_ioat_chan(chan) container_of(chan, struct ioat_dma_chan, common)
  38. #define to_ioatdma_device(dev) container_of(dev, struct ioatdma_device, common)
  39. #define to_ioat_desc(lh) container_of(lh, struct ioat_desc_sw, node)
  40. #define tx_to_ioat_desc(tx) container_of(tx, struct ioat_desc_sw, async_tx)
  41. /* internal functions */
  42. static void ioat_dma_start_null_desc(struct ioat_dma_chan *ioat_chan);
  43. static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan);
  44. static int ioat_dma_enumerate_channels(struct ioatdma_device *device)
  45. {
  46. u8 xfercap_scale;
  47. u32 xfercap;
  48. int i;
  49. struct ioat_dma_chan *ioat_chan;
  50. device->common.chancnt = readb(device->reg_base + IOAT_CHANCNT_OFFSET);
  51. xfercap_scale = readb(device->reg_base + IOAT_XFERCAP_OFFSET);
  52. xfercap = (xfercap_scale == 0 ? -1 : (1UL << xfercap_scale));
  53. for (i = 0; i < device->common.chancnt; i++) {
  54. ioat_chan = kzalloc(sizeof(*ioat_chan), GFP_KERNEL);
  55. if (!ioat_chan) {
  56. device->common.chancnt = i;
  57. break;
  58. }
  59. ioat_chan->device = device;
  60. ioat_chan->reg_base = device->reg_base + (0x80 * (i + 1));
  61. ioat_chan->xfercap = xfercap;
  62. spin_lock_init(&ioat_chan->cleanup_lock);
  63. spin_lock_init(&ioat_chan->desc_lock);
  64. INIT_LIST_HEAD(&ioat_chan->free_desc);
  65. INIT_LIST_HEAD(&ioat_chan->used_desc);
  66. /* This should be made common somewhere in dmaengine.c */
  67. ioat_chan->common.device = &device->common;
  68. list_add_tail(&ioat_chan->common.device_node,
  69. &device->common.channels);
  70. }
  71. return device->common.chancnt;
  72. }
  73. static void ioat_set_src(dma_addr_t addr,
  74. struct dma_async_tx_descriptor *tx,
  75. int index)
  76. {
  77. struct ioat_desc_sw *iter, *desc = tx_to_ioat_desc(tx);
  78. struct ioat_dma_chan *ioat_chan = to_ioat_chan(tx->chan);
  79. pci_unmap_addr_set(desc, src, addr);
  80. list_for_each_entry(iter, &desc->async_tx.tx_list, node) {
  81. iter->hw->src_addr = addr;
  82. addr += ioat_chan->xfercap;
  83. }
  84. }
  85. static void ioat_set_dest(dma_addr_t addr,
  86. struct dma_async_tx_descriptor *tx,
  87. int index)
  88. {
  89. struct ioat_desc_sw *iter, *desc = tx_to_ioat_desc(tx);
  90. struct ioat_dma_chan *ioat_chan = to_ioat_chan(tx->chan);
  91. pci_unmap_addr_set(desc, dst, addr);
  92. list_for_each_entry(iter, &desc->async_tx.tx_list, node) {
  93. iter->hw->dst_addr = addr;
  94. addr += ioat_chan->xfercap;
  95. }
  96. }
  97. static dma_cookie_t ioat_tx_submit(struct dma_async_tx_descriptor *tx)
  98. {
  99. struct ioat_dma_chan *ioat_chan = to_ioat_chan(tx->chan);
  100. struct ioat_desc_sw *desc = tx_to_ioat_desc(tx);
  101. int append = 0;
  102. dma_cookie_t cookie;
  103. struct ioat_desc_sw *group_start;
  104. group_start = list_entry(desc->async_tx.tx_list.next,
  105. struct ioat_desc_sw, node);
  106. spin_lock_bh(&ioat_chan->desc_lock);
  107. /* cookie incr and addition to used_list must be atomic */
  108. cookie = ioat_chan->common.cookie;
  109. cookie++;
  110. if (cookie < 0)
  111. cookie = 1;
  112. ioat_chan->common.cookie = desc->async_tx.cookie = cookie;
  113. /* write address into NextDescriptor field of last desc in chain */
  114. to_ioat_desc(ioat_chan->used_desc.prev)->hw->next =
  115. group_start->async_tx.phys;
  116. list_splice_init(&desc->async_tx.tx_list, ioat_chan->used_desc.prev);
  117. ioat_chan->pending += desc->tx_cnt;
  118. if (ioat_chan->pending >= 4) {
  119. append = 1;
  120. ioat_chan->pending = 0;
  121. }
  122. spin_unlock_bh(&ioat_chan->desc_lock);
  123. if (append)
  124. writeb(IOAT_CHANCMD_APPEND,
  125. ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
  126. return cookie;
  127. }
  128. static struct ioat_desc_sw *ioat_dma_alloc_descriptor(
  129. struct ioat_dma_chan *ioat_chan,
  130. gfp_t flags)
  131. {
  132. struct ioat_dma_descriptor *desc;
  133. struct ioat_desc_sw *desc_sw;
  134. struct ioatdma_device *ioatdma_device;
  135. dma_addr_t phys;
  136. ioatdma_device = to_ioatdma_device(ioat_chan->common.device);
  137. desc = pci_pool_alloc(ioatdma_device->dma_pool, flags, &phys);
  138. if (unlikely(!desc))
  139. return NULL;
  140. desc_sw = kzalloc(sizeof(*desc_sw), flags);
  141. if (unlikely(!desc_sw)) {
  142. pci_pool_free(ioatdma_device->dma_pool, desc, phys);
  143. return NULL;
  144. }
  145. memset(desc, 0, sizeof(*desc));
  146. dma_async_tx_descriptor_init(&desc_sw->async_tx, &ioat_chan->common);
  147. desc_sw->async_tx.tx_set_src = ioat_set_src;
  148. desc_sw->async_tx.tx_set_dest = ioat_set_dest;
  149. desc_sw->async_tx.tx_submit = ioat_tx_submit;
  150. INIT_LIST_HEAD(&desc_sw->async_tx.tx_list);
  151. desc_sw->hw = desc;
  152. desc_sw->async_tx.phys = phys;
  153. return desc_sw;
  154. }
  155. /* returns the actual number of allocated descriptors */
  156. static int ioat_dma_alloc_chan_resources(struct dma_chan *chan)
  157. {
  158. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  159. struct ioat_desc_sw *desc = NULL;
  160. u16 chanctrl;
  161. u32 chanerr;
  162. int i;
  163. LIST_HEAD(tmp_list);
  164. /* have we already been set up? */
  165. if (!list_empty(&ioat_chan->free_desc))
  166. return INITIAL_IOAT_DESC_COUNT;
  167. /* Setup register to interrupt and write completion status on error */
  168. chanctrl = IOAT_CHANCTRL_ERR_INT_EN |
  169. IOAT_CHANCTRL_ANY_ERR_ABORT_EN |
  170. IOAT_CHANCTRL_ERR_COMPLETION_EN;
  171. writew(chanctrl, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
  172. chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  173. if (chanerr) {
  174. dev_err(&ioat_chan->device->pdev->dev,
  175. "ioatdma: CHANERR = %x, clearing\n", chanerr);
  176. writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  177. }
  178. /* Allocate descriptors */
  179. for (i = 0; i < INITIAL_IOAT_DESC_COUNT; i++) {
  180. desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_KERNEL);
  181. if (!desc) {
  182. dev_err(&ioat_chan->device->pdev->dev,
  183. "ioatdma: Only %d initial descriptors\n", i);
  184. break;
  185. }
  186. list_add_tail(&desc->node, &tmp_list);
  187. }
  188. spin_lock_bh(&ioat_chan->desc_lock);
  189. list_splice(&tmp_list, &ioat_chan->free_desc);
  190. spin_unlock_bh(&ioat_chan->desc_lock);
  191. /* allocate a completion writeback area */
  192. /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
  193. ioat_chan->completion_virt =
  194. pci_pool_alloc(ioat_chan->device->completion_pool,
  195. GFP_KERNEL,
  196. &ioat_chan->completion_addr);
  197. memset(ioat_chan->completion_virt, 0,
  198. sizeof(*ioat_chan->completion_virt));
  199. writel(((u64) ioat_chan->completion_addr) & 0x00000000FFFFFFFF,
  200. ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
  201. writel(((u64) ioat_chan->completion_addr) >> 32,
  202. ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
  203. ioat_dma_start_null_desc(ioat_chan);
  204. return i;
  205. }
  206. static void ioat_dma_free_chan_resources(struct dma_chan *chan)
  207. {
  208. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  209. struct ioatdma_device *ioatdma_device = to_ioatdma_device(chan->device);
  210. struct ioat_desc_sw *desc, *_desc;
  211. int in_use_descs = 0;
  212. ioat_dma_memcpy_cleanup(ioat_chan);
  213. writeb(IOAT_CHANCMD_RESET, ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
  214. spin_lock_bh(&ioat_chan->desc_lock);
  215. list_for_each_entry_safe(desc, _desc, &ioat_chan->used_desc, node) {
  216. in_use_descs++;
  217. list_del(&desc->node);
  218. pci_pool_free(ioatdma_device->dma_pool, desc->hw,
  219. desc->async_tx.phys);
  220. kfree(desc);
  221. }
  222. list_for_each_entry_safe(desc, _desc, &ioat_chan->free_desc, node) {
  223. list_del(&desc->node);
  224. pci_pool_free(ioatdma_device->dma_pool, desc->hw,
  225. desc->async_tx.phys);
  226. kfree(desc);
  227. }
  228. spin_unlock_bh(&ioat_chan->desc_lock);
  229. pci_pool_free(ioatdma_device->completion_pool,
  230. ioat_chan->completion_virt,
  231. ioat_chan->completion_addr);
  232. /* one is ok since we left it on there on purpose */
  233. if (in_use_descs > 1)
  234. dev_err(&ioat_chan->device->pdev->dev,
  235. "ioatdma: Freeing %d in use descriptors!\n",
  236. in_use_descs - 1);
  237. ioat_chan->last_completion = ioat_chan->completion_addr = 0;
  238. }
  239. static struct dma_async_tx_descriptor *ioat_dma_prep_memcpy(
  240. struct dma_chan *chan,
  241. size_t len,
  242. int int_en)
  243. {
  244. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  245. struct ioat_desc_sw *first, *prev, *new;
  246. LIST_HEAD(new_chain);
  247. u32 copy;
  248. size_t orig_len;
  249. int desc_count = 0;
  250. if (!len)
  251. return NULL;
  252. orig_len = len;
  253. first = NULL;
  254. prev = NULL;
  255. spin_lock_bh(&ioat_chan->desc_lock);
  256. while (len) {
  257. if (!list_empty(&ioat_chan->free_desc)) {
  258. new = to_ioat_desc(ioat_chan->free_desc.next);
  259. list_del(&new->node);
  260. } else {
  261. /* try to get another desc */
  262. new = ioat_dma_alloc_descriptor(ioat_chan, GFP_ATOMIC);
  263. /* will this ever happen? */
  264. /* TODO add upper limit on these */
  265. BUG_ON(!new);
  266. }
  267. copy = min((u32) len, ioat_chan->xfercap);
  268. new->hw->size = copy;
  269. new->hw->ctl = 0;
  270. new->async_tx.cookie = 0;
  271. new->async_tx.ack = 1;
  272. /* chain together the physical address list for the HW */
  273. if (!first)
  274. first = new;
  275. else
  276. prev->hw->next = (u64) new->async_tx.phys;
  277. prev = new;
  278. len -= copy;
  279. list_add_tail(&new->node, &new_chain);
  280. desc_count++;
  281. }
  282. list_splice(&new_chain, &new->async_tx.tx_list);
  283. new->hw->ctl = IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
  284. new->hw->next = 0;
  285. new->tx_cnt = desc_count;
  286. new->async_tx.ack = 0; /* client is in control of this ack */
  287. new->async_tx.cookie = -EBUSY;
  288. pci_unmap_len_set(new, len, orig_len);
  289. spin_unlock_bh(&ioat_chan->desc_lock);
  290. return new ? &new->async_tx : NULL;
  291. }
  292. /**
  293. * ioat_dma_memcpy_issue_pending - push potentially unrecognized appended
  294. * descriptors to hw
  295. * @chan: DMA channel handle
  296. */
  297. static void ioat_dma_memcpy_issue_pending(struct dma_chan *chan)
  298. {
  299. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  300. if (ioat_chan->pending != 0) {
  301. ioat_chan->pending = 0;
  302. writeb(IOAT_CHANCMD_APPEND,
  303. ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
  304. }
  305. }
  306. static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan)
  307. {
  308. unsigned long phys_complete;
  309. struct ioat_desc_sw *desc, *_desc;
  310. dma_cookie_t cookie = 0;
  311. prefetch(ioat_chan->completion_virt);
  312. if (!spin_trylock(&ioat_chan->cleanup_lock))
  313. return;
  314. /* The completion writeback can happen at any time,
  315. so reads by the driver need to be atomic operations
  316. The descriptor physical addresses are limited to 32-bits
  317. when the CPU can only do a 32-bit mov */
  318. #if (BITS_PER_LONG == 64)
  319. phys_complete =
  320. ioat_chan->completion_virt->full & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
  321. #else
  322. phys_complete = ioat_chan->completion_virt->low & IOAT_LOW_COMPLETION_MASK;
  323. #endif
  324. if ((ioat_chan->completion_virt->full & IOAT_CHANSTS_DMA_TRANSFER_STATUS) ==
  325. IOAT_CHANSTS_DMA_TRANSFER_STATUS_HALTED) {
  326. dev_err(&ioat_chan->device->pdev->dev,
  327. "ioatdma: Channel halted, chanerr = %x\n",
  328. readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET));
  329. /* TODO do something to salvage the situation */
  330. }
  331. if (phys_complete == ioat_chan->last_completion) {
  332. spin_unlock(&ioat_chan->cleanup_lock);
  333. return;
  334. }
  335. spin_lock_bh(&ioat_chan->desc_lock);
  336. list_for_each_entry_safe(desc, _desc, &ioat_chan->used_desc, node) {
  337. /*
  338. * Incoming DMA requests may use multiple descriptors, due to
  339. * exceeding xfercap, perhaps. If so, only the last one will
  340. * have a cookie, and require unmapping.
  341. */
  342. if (desc->async_tx.cookie) {
  343. cookie = desc->async_tx.cookie;
  344. /*
  345. * yes we are unmapping both _page and _single alloc'd
  346. * regions with unmap_page. Is this *really* that bad?
  347. */
  348. pci_unmap_page(ioat_chan->device->pdev,
  349. pci_unmap_addr(desc, dst),
  350. pci_unmap_len(desc, len),
  351. PCI_DMA_FROMDEVICE);
  352. pci_unmap_page(ioat_chan->device->pdev,
  353. pci_unmap_addr(desc, src),
  354. pci_unmap_len(desc, len),
  355. PCI_DMA_TODEVICE);
  356. }
  357. if (desc->async_tx.phys != phys_complete) {
  358. /*
  359. * a completed entry, but not the last, so cleanup
  360. * if the client is done with the descriptor
  361. */
  362. if (desc->async_tx.ack) {
  363. list_del(&desc->node);
  364. list_add_tail(&desc->node,
  365. &ioat_chan->free_desc);
  366. } else
  367. desc->async_tx.cookie = 0;
  368. } else {
  369. /*
  370. * last used desc. Do not remove, so we can append from
  371. * it, but don't look at it next time, either
  372. */
  373. desc->async_tx.cookie = 0;
  374. /* TODO check status bits? */
  375. break;
  376. }
  377. }
  378. spin_unlock_bh(&ioat_chan->desc_lock);
  379. ioat_chan->last_completion = phys_complete;
  380. if (cookie != 0)
  381. ioat_chan->completed_cookie = cookie;
  382. spin_unlock(&ioat_chan->cleanup_lock);
  383. }
  384. static void ioat_dma_dependency_added(struct dma_chan *chan)
  385. {
  386. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  387. spin_lock_bh(&ioat_chan->desc_lock);
  388. if (ioat_chan->pending == 0) {
  389. spin_unlock_bh(&ioat_chan->desc_lock);
  390. ioat_dma_memcpy_cleanup(ioat_chan);
  391. } else
  392. spin_unlock_bh(&ioat_chan->desc_lock);
  393. }
  394. /**
  395. * ioat_dma_is_complete - poll the status of a IOAT DMA transaction
  396. * @chan: IOAT DMA channel handle
  397. * @cookie: DMA transaction identifier
  398. * @done: if not %NULL, updated with last completed transaction
  399. * @used: if not %NULL, updated with last used transaction
  400. */
  401. static enum dma_status ioat_dma_is_complete(struct dma_chan *chan,
  402. dma_cookie_t cookie,
  403. dma_cookie_t *done,
  404. dma_cookie_t *used)
  405. {
  406. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  407. dma_cookie_t last_used;
  408. dma_cookie_t last_complete;
  409. enum dma_status ret;
  410. last_used = chan->cookie;
  411. last_complete = ioat_chan->completed_cookie;
  412. if (done)
  413. *done = last_complete;
  414. if (used)
  415. *used = last_used;
  416. ret = dma_async_is_complete(cookie, last_complete, last_used);
  417. if (ret == DMA_SUCCESS)
  418. return ret;
  419. ioat_dma_memcpy_cleanup(ioat_chan);
  420. last_used = chan->cookie;
  421. last_complete = ioat_chan->completed_cookie;
  422. if (done)
  423. *done = last_complete;
  424. if (used)
  425. *used = last_used;
  426. return dma_async_is_complete(cookie, last_complete, last_used);
  427. }
  428. /* PCI API */
  429. static irqreturn_t ioat_do_interrupt(int irq, void *data)
  430. {
  431. struct ioatdma_device *instance = data;
  432. unsigned long attnstatus;
  433. u8 intrctrl;
  434. intrctrl = readb(instance->reg_base + IOAT_INTRCTRL_OFFSET);
  435. if (!(intrctrl & IOAT_INTRCTRL_MASTER_INT_EN))
  436. return IRQ_NONE;
  437. if (!(intrctrl & IOAT_INTRCTRL_INT_STATUS)) {
  438. writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
  439. return IRQ_NONE;
  440. }
  441. attnstatus = readl(instance->reg_base + IOAT_ATTNSTATUS_OFFSET);
  442. printk(KERN_ERR "ioatdma: interrupt! status %lx\n", attnstatus);
  443. writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
  444. return IRQ_HANDLED;
  445. }
  446. static void ioat_dma_start_null_desc(struct ioat_dma_chan *ioat_chan)
  447. {
  448. struct ioat_desc_sw *desc;
  449. spin_lock_bh(&ioat_chan->desc_lock);
  450. if (!list_empty(&ioat_chan->free_desc)) {
  451. desc = to_ioat_desc(ioat_chan->free_desc.next);
  452. list_del(&desc->node);
  453. } else {
  454. /* try to get another desc */
  455. spin_unlock_bh(&ioat_chan->desc_lock);
  456. desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_KERNEL);
  457. spin_lock_bh(&ioat_chan->desc_lock);
  458. /* will this ever happen? */
  459. BUG_ON(!desc);
  460. }
  461. desc->hw->ctl = IOAT_DMA_DESCRIPTOR_NUL;
  462. desc->hw->next = 0;
  463. desc->async_tx.ack = 1;
  464. list_add_tail(&desc->node, &ioat_chan->used_desc);
  465. spin_unlock_bh(&ioat_chan->desc_lock);
  466. writel(((u64) desc->async_tx.phys) & 0x00000000FFFFFFFF,
  467. ioat_chan->reg_base + IOAT_CHAINADDR_OFFSET_LOW);
  468. writel(((u64) desc->async_tx.phys) >> 32,
  469. ioat_chan->reg_base + IOAT_CHAINADDR_OFFSET_HIGH);
  470. writeb(IOAT_CHANCMD_START, ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
  471. }
  472. /*
  473. * Perform a IOAT transaction to verify the HW works.
  474. */
  475. #define IOAT_TEST_SIZE 2000
  476. static int ioat_self_test(struct ioatdma_device *device)
  477. {
  478. int i;
  479. u8 *src;
  480. u8 *dest;
  481. struct dma_chan *dma_chan;
  482. struct dma_async_tx_descriptor *tx;
  483. dma_addr_t addr;
  484. dma_cookie_t cookie;
  485. int err = 0;
  486. src = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
  487. if (!src)
  488. return -ENOMEM;
  489. dest = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
  490. if (!dest) {
  491. kfree(src);
  492. return -ENOMEM;
  493. }
  494. /* Fill in src buffer */
  495. for (i = 0; i < IOAT_TEST_SIZE; i++)
  496. src[i] = (u8)i;
  497. /* Start copy, using first DMA channel */
  498. dma_chan = container_of(device->common.channels.next,
  499. struct dma_chan,
  500. device_node);
  501. if (ioat_dma_alloc_chan_resources(dma_chan) < 1) {
  502. dev_err(&device->pdev->dev,
  503. "selftest cannot allocate chan resource\n");
  504. err = -ENODEV;
  505. goto out;
  506. }
  507. tx = ioat_dma_prep_memcpy(dma_chan, IOAT_TEST_SIZE, 0);
  508. async_tx_ack(tx);
  509. addr = dma_map_single(dma_chan->device->dev, src, IOAT_TEST_SIZE,
  510. DMA_TO_DEVICE);
  511. ioat_set_src(addr, tx, 0);
  512. addr = dma_map_single(dma_chan->device->dev, dest, IOAT_TEST_SIZE,
  513. DMA_FROM_DEVICE);
  514. ioat_set_dest(addr, tx, 0);
  515. cookie = ioat_tx_submit(tx);
  516. ioat_dma_memcpy_issue_pending(dma_chan);
  517. msleep(1);
  518. if (ioat_dma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
  519. dev_err(&device->pdev->dev,
  520. "ioatdma: Self-test copy timed out, disabling\n");
  521. err = -ENODEV;
  522. goto free_resources;
  523. }
  524. if (memcmp(src, dest, IOAT_TEST_SIZE)) {
  525. dev_err(&device->pdev->dev,
  526. "ioatdma: Self-test copy failed compare, disabling\n");
  527. err = -ENODEV;
  528. goto free_resources;
  529. }
  530. free_resources:
  531. ioat_dma_free_chan_resources(dma_chan);
  532. out:
  533. kfree(src);
  534. kfree(dest);
  535. return err;
  536. }
  537. struct ioatdma_device *ioat_dma_probe(struct pci_dev *pdev,
  538. void __iomem *iobase)
  539. {
  540. int err;
  541. struct ioatdma_device *device;
  542. device = kzalloc(sizeof(*device), GFP_KERNEL);
  543. if (!device) {
  544. err = -ENOMEM;
  545. goto err_kzalloc;
  546. }
  547. device->pdev = pdev;
  548. device->reg_base = iobase;
  549. device->version = readb(device->reg_base + IOAT_VER_OFFSET);
  550. /* DMA coherent memory pool for DMA descriptor allocations */
  551. device->dma_pool = pci_pool_create("dma_desc_pool", pdev,
  552. sizeof(struct ioat_dma_descriptor),
  553. 64, 0);
  554. if (!device->dma_pool) {
  555. err = -ENOMEM;
  556. goto err_dma_pool;
  557. }
  558. device->completion_pool = pci_pool_create("completion_pool", pdev,
  559. sizeof(u64), SMP_CACHE_BYTES,
  560. SMP_CACHE_BYTES);
  561. if (!device->completion_pool) {
  562. err = -ENOMEM;
  563. goto err_completion_pool;
  564. }
  565. INIT_LIST_HEAD(&device->common.channels);
  566. ioat_dma_enumerate_channels(device);
  567. dma_cap_set(DMA_MEMCPY, device->common.cap_mask);
  568. device->common.device_alloc_chan_resources =
  569. ioat_dma_alloc_chan_resources;
  570. device->common.device_free_chan_resources =
  571. ioat_dma_free_chan_resources;
  572. device->common.device_prep_dma_memcpy = ioat_dma_prep_memcpy;
  573. device->common.device_is_tx_complete = ioat_dma_is_complete;
  574. device->common.device_issue_pending = ioat_dma_memcpy_issue_pending;
  575. device->common.device_dependency_added = ioat_dma_dependency_added;
  576. device->common.dev = &pdev->dev;
  577. printk(KERN_INFO "ioatdma: Intel(R) I/OAT DMA Engine found,"
  578. " %d channels, device version 0x%02x\n",
  579. device->common.chancnt, device->version);
  580. pci_set_drvdata(pdev, device);
  581. err = request_irq(pdev->irq, &ioat_do_interrupt, IRQF_SHARED, "ioat",
  582. device);
  583. if (err)
  584. goto err_irq;
  585. writeb(IOAT_INTRCTRL_MASTER_INT_EN,
  586. device->reg_base + IOAT_INTRCTRL_OFFSET);
  587. pci_set_master(pdev);
  588. err = ioat_self_test(device);
  589. if (err)
  590. goto err_self_test;
  591. dma_async_device_register(&device->common);
  592. return device;
  593. err_self_test:
  594. free_irq(device->pdev->irq, device);
  595. err_irq:
  596. pci_pool_destroy(device->completion_pool);
  597. err_completion_pool:
  598. pci_pool_destroy(device->dma_pool);
  599. err_dma_pool:
  600. kfree(device);
  601. err_kzalloc:
  602. iounmap(iobase);
  603. printk(KERN_ERR
  604. "ioatdma: Intel(R) I/OAT DMA Engine initialization failed\n");
  605. return NULL;
  606. }
  607. void ioat_dma_remove(struct ioatdma_device *device)
  608. {
  609. struct dma_chan *chan, *_chan;
  610. struct ioat_dma_chan *ioat_chan;
  611. dma_async_device_unregister(&device->common);
  612. free_irq(device->pdev->irq, device);
  613. pci_pool_destroy(device->dma_pool);
  614. pci_pool_destroy(device->completion_pool);
  615. list_for_each_entry_safe(chan, _chan,
  616. &device->common.channels, device_node) {
  617. ioat_chan = to_ioat_chan(chan);
  618. list_del(&chan->device_node);
  619. kfree(ioat_chan);
  620. }
  621. kfree(device);
  622. }