ioatdma.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /*
  2. * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 2 of the License, or (at your option)
  7. * any later version.
  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., 59
  16. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called COPYING.
  20. */
  21. /*
  22. * This driver supports an Intel I/OAT DMA engine, which does asynchronous
  23. * copy operations.
  24. */
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/pci.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/dmaengine.h>
  30. #include <linux/delay.h>
  31. #include <linux/dma-mapping.h>
  32. #include "ioatdma.h"
  33. #include "ioatdma_registers.h"
  34. #include "ioatdma_hw.h"
  35. #define to_ioat_chan(chan) container_of(chan, struct ioat_dma_chan, common)
  36. #define to_ioat_device(dev) container_of(dev, struct ioat_device, common)
  37. #define to_ioat_desc(lh) container_of(lh, struct ioat_desc_sw, node)
  38. /* internal functions */
  39. static int __devinit ioat_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
  40. static void ioat_shutdown(struct pci_dev *pdev);
  41. static void __devexit ioat_remove(struct pci_dev *pdev);
  42. static int enumerate_dma_channels(struct ioat_device *device)
  43. {
  44. u8 xfercap_scale;
  45. u32 xfercap;
  46. int i;
  47. struct ioat_dma_chan *ioat_chan;
  48. device->common.chancnt = readb(device->reg_base + IOAT_CHANCNT_OFFSET);
  49. xfercap_scale = readb(device->reg_base + IOAT_XFERCAP_OFFSET);
  50. xfercap = (xfercap_scale == 0 ? -1 : (1UL << xfercap_scale));
  51. for (i = 0; i < device->common.chancnt; i++) {
  52. ioat_chan = kzalloc(sizeof(*ioat_chan), GFP_KERNEL);
  53. if (!ioat_chan) {
  54. device->common.chancnt = i;
  55. break;
  56. }
  57. ioat_chan->device = device;
  58. ioat_chan->reg_base = device->reg_base + (0x80 * (i + 1));
  59. ioat_chan->xfercap = xfercap;
  60. spin_lock_init(&ioat_chan->cleanup_lock);
  61. spin_lock_init(&ioat_chan->desc_lock);
  62. INIT_LIST_HEAD(&ioat_chan->free_desc);
  63. INIT_LIST_HEAD(&ioat_chan->used_desc);
  64. /* This should be made common somewhere in dmaengine.c */
  65. ioat_chan->common.device = &device->common;
  66. ioat_chan->common.client = NULL;
  67. list_add_tail(&ioat_chan->common.device_node,
  68. &device->common.channels);
  69. }
  70. return device->common.chancnt;
  71. }
  72. static struct ioat_desc_sw *ioat_dma_alloc_descriptor(
  73. struct ioat_dma_chan *ioat_chan,
  74. gfp_t flags)
  75. {
  76. struct ioat_dma_descriptor *desc;
  77. struct ioat_desc_sw *desc_sw;
  78. struct ioat_device *ioat_device;
  79. dma_addr_t phys;
  80. ioat_device = to_ioat_device(ioat_chan->common.device);
  81. desc = pci_pool_alloc(ioat_device->dma_pool, flags, &phys);
  82. if (unlikely(!desc))
  83. return NULL;
  84. desc_sw = kzalloc(sizeof(*desc_sw), flags);
  85. if (unlikely(!desc_sw)) {
  86. pci_pool_free(ioat_device->dma_pool, desc, phys);
  87. return NULL;
  88. }
  89. memset(desc, 0, sizeof(*desc));
  90. desc_sw->hw = desc;
  91. desc_sw->phys = phys;
  92. return desc_sw;
  93. }
  94. #define INITIAL_IOAT_DESC_COUNT 128
  95. static void ioat_start_null_desc(struct ioat_dma_chan *ioat_chan);
  96. /* returns the actual number of allocated descriptors */
  97. static int ioat_dma_alloc_chan_resources(struct dma_chan *chan)
  98. {
  99. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  100. struct ioat_desc_sw *desc = NULL;
  101. u16 chanctrl;
  102. u32 chanerr;
  103. int i;
  104. LIST_HEAD(tmp_list);
  105. /*
  106. * In-use bit automatically set by reading chanctrl
  107. * If 0, we got it, if 1, someone else did
  108. */
  109. chanctrl = readw(ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
  110. if (chanctrl & IOAT_CHANCTRL_CHANNEL_IN_USE)
  111. return -EBUSY;
  112. /* Setup register to interrupt and write completion status on error */
  113. chanctrl = IOAT_CHANCTRL_CHANNEL_IN_USE |
  114. IOAT_CHANCTRL_ERR_INT_EN |
  115. IOAT_CHANCTRL_ANY_ERR_ABORT_EN |
  116. IOAT_CHANCTRL_ERR_COMPLETION_EN;
  117. writew(chanctrl, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
  118. chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  119. if (chanerr) {
  120. printk("IOAT: CHANERR = %x, clearing\n", chanerr);
  121. writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  122. }
  123. /* Allocate descriptors */
  124. for (i = 0; i < INITIAL_IOAT_DESC_COUNT; i++) {
  125. desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_KERNEL);
  126. if (!desc) {
  127. printk(KERN_ERR "IOAT: Only %d initial descriptors\n", i);
  128. break;
  129. }
  130. list_add_tail(&desc->node, &tmp_list);
  131. }
  132. spin_lock_bh(&ioat_chan->desc_lock);
  133. list_splice(&tmp_list, &ioat_chan->free_desc);
  134. spin_unlock_bh(&ioat_chan->desc_lock);
  135. /* allocate a completion writeback area */
  136. /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
  137. ioat_chan->completion_virt =
  138. pci_pool_alloc(ioat_chan->device->completion_pool,
  139. GFP_KERNEL,
  140. &ioat_chan->completion_addr);
  141. memset(ioat_chan->completion_virt, 0,
  142. sizeof(*ioat_chan->completion_virt));
  143. writel(((u64) ioat_chan->completion_addr) & 0x00000000FFFFFFFF,
  144. ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
  145. writel(((u64) ioat_chan->completion_addr) >> 32,
  146. ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
  147. ioat_start_null_desc(ioat_chan);
  148. return i;
  149. }
  150. static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan);
  151. static void ioat_dma_free_chan_resources(struct dma_chan *chan)
  152. {
  153. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  154. struct ioat_device *ioat_device = to_ioat_device(chan->device);
  155. struct ioat_desc_sw *desc, *_desc;
  156. u16 chanctrl;
  157. int in_use_descs = 0;
  158. ioat_dma_memcpy_cleanup(ioat_chan);
  159. writeb(IOAT_CHANCMD_RESET, ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
  160. spin_lock_bh(&ioat_chan->desc_lock);
  161. list_for_each_entry_safe(desc, _desc, &ioat_chan->used_desc, node) {
  162. in_use_descs++;
  163. list_del(&desc->node);
  164. pci_pool_free(ioat_device->dma_pool, desc->hw, desc->phys);
  165. kfree(desc);
  166. }
  167. list_for_each_entry_safe(desc, _desc, &ioat_chan->free_desc, node) {
  168. list_del(&desc->node);
  169. pci_pool_free(ioat_device->dma_pool, desc->hw, desc->phys);
  170. kfree(desc);
  171. }
  172. spin_unlock_bh(&ioat_chan->desc_lock);
  173. pci_pool_free(ioat_device->completion_pool,
  174. ioat_chan->completion_virt,
  175. ioat_chan->completion_addr);
  176. /* one is ok since we left it on there on purpose */
  177. if (in_use_descs > 1)
  178. printk(KERN_ERR "IOAT: Freeing %d in use descriptors!\n",
  179. in_use_descs - 1);
  180. ioat_chan->last_completion = ioat_chan->completion_addr = 0;
  181. /* Tell hw the chan is free */
  182. chanctrl = readw(ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
  183. chanctrl &= ~IOAT_CHANCTRL_CHANNEL_IN_USE;
  184. writew(chanctrl, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
  185. }
  186. /**
  187. * do_ioat_dma_memcpy - actual function that initiates a IOAT DMA transaction
  188. * @ioat_chan: IOAT DMA channel handle
  189. * @dest: DMA destination address
  190. * @src: DMA source address
  191. * @len: transaction length in bytes
  192. */
  193. static dma_cookie_t do_ioat_dma_memcpy(struct ioat_dma_chan *ioat_chan,
  194. dma_addr_t dest,
  195. dma_addr_t src,
  196. size_t len)
  197. {
  198. struct ioat_desc_sw *first;
  199. struct ioat_desc_sw *prev;
  200. struct ioat_desc_sw *new;
  201. dma_cookie_t cookie;
  202. LIST_HEAD(new_chain);
  203. u32 copy;
  204. size_t orig_len;
  205. dma_addr_t orig_src, orig_dst;
  206. unsigned int desc_count = 0;
  207. unsigned int append = 0;
  208. if (!ioat_chan || !dest || !src)
  209. return -EFAULT;
  210. if (!len)
  211. return ioat_chan->common.cookie;
  212. orig_len = len;
  213. orig_src = src;
  214. orig_dst = dest;
  215. first = NULL;
  216. prev = NULL;
  217. spin_lock_bh(&ioat_chan->desc_lock);
  218. while (len) {
  219. if (!list_empty(&ioat_chan->free_desc)) {
  220. new = to_ioat_desc(ioat_chan->free_desc.next);
  221. list_del(&new->node);
  222. } else {
  223. /* try to get another desc */
  224. new = ioat_dma_alloc_descriptor(ioat_chan, GFP_ATOMIC);
  225. /* will this ever happen? */
  226. /* TODO add upper limit on these */
  227. BUG_ON(!new);
  228. }
  229. copy = min((u32) len, ioat_chan->xfercap);
  230. new->hw->size = copy;
  231. new->hw->ctl = 0;
  232. new->hw->src_addr = src;
  233. new->hw->dst_addr = dest;
  234. new->cookie = 0;
  235. /* chain together the physical address list for the HW */
  236. if (!first)
  237. first = new;
  238. else
  239. prev->hw->next = (u64) new->phys;
  240. prev = new;
  241. len -= copy;
  242. dest += copy;
  243. src += copy;
  244. list_add_tail(&new->node, &new_chain);
  245. desc_count++;
  246. }
  247. new->hw->ctl = IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
  248. new->hw->next = 0;
  249. /* cookie incr and addition to used_list must be atomic */
  250. cookie = ioat_chan->common.cookie;
  251. cookie++;
  252. if (cookie < 0)
  253. cookie = 1;
  254. ioat_chan->common.cookie = new->cookie = cookie;
  255. pci_unmap_addr_set(new, src, orig_src);
  256. pci_unmap_addr_set(new, dst, orig_dst);
  257. pci_unmap_len_set(new, src_len, orig_len);
  258. pci_unmap_len_set(new, dst_len, orig_len);
  259. /* write address into NextDescriptor field of last desc in chain */
  260. to_ioat_desc(ioat_chan->used_desc.prev)->hw->next = first->phys;
  261. list_splice_init(&new_chain, ioat_chan->used_desc.prev);
  262. ioat_chan->pending += desc_count;
  263. if (ioat_chan->pending >= 4) {
  264. append = 1;
  265. ioat_chan->pending = 0;
  266. }
  267. spin_unlock_bh(&ioat_chan->desc_lock);
  268. if (append)
  269. writeb(IOAT_CHANCMD_APPEND,
  270. ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
  271. return cookie;
  272. }
  273. /**
  274. * ioat_dma_memcpy_buf_to_buf - wrapper that takes src & dest bufs
  275. * @chan: IOAT DMA channel handle
  276. * @dest: DMA destination address
  277. * @src: DMA source address
  278. * @len: transaction length in bytes
  279. */
  280. static dma_cookie_t ioat_dma_memcpy_buf_to_buf(struct dma_chan *chan,
  281. void *dest,
  282. void *src,
  283. size_t len)
  284. {
  285. dma_addr_t dest_addr;
  286. dma_addr_t src_addr;
  287. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  288. dest_addr = pci_map_single(ioat_chan->device->pdev,
  289. dest, len, PCI_DMA_FROMDEVICE);
  290. src_addr = pci_map_single(ioat_chan->device->pdev,
  291. src, len, PCI_DMA_TODEVICE);
  292. return do_ioat_dma_memcpy(ioat_chan, dest_addr, src_addr, len);
  293. }
  294. /**
  295. * ioat_dma_memcpy_buf_to_pg - wrapper, copying from a buf to a page
  296. * @chan: IOAT DMA channel handle
  297. * @page: pointer to the page to copy to
  298. * @offset: offset into that page
  299. * @src: DMA source address
  300. * @len: transaction length in bytes
  301. */
  302. static dma_cookie_t ioat_dma_memcpy_buf_to_pg(struct dma_chan *chan,
  303. struct page *page,
  304. unsigned int offset,
  305. void *src,
  306. size_t len)
  307. {
  308. dma_addr_t dest_addr;
  309. dma_addr_t src_addr;
  310. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  311. dest_addr = pci_map_page(ioat_chan->device->pdev,
  312. page, offset, len, PCI_DMA_FROMDEVICE);
  313. src_addr = pci_map_single(ioat_chan->device->pdev,
  314. src, len, PCI_DMA_TODEVICE);
  315. return do_ioat_dma_memcpy(ioat_chan, dest_addr, src_addr, len);
  316. }
  317. /**
  318. * ioat_dma_memcpy_pg_to_pg - wrapper, copying between two pages
  319. * @chan: IOAT DMA channel handle
  320. * @dest_pg: pointer to the page to copy to
  321. * @dest_off: offset into that page
  322. * @src_pg: pointer to the page to copy from
  323. * @src_off: offset into that page
  324. * @len: transaction length in bytes. This is guaranteed not to make a copy
  325. * across a page boundary.
  326. */
  327. static dma_cookie_t ioat_dma_memcpy_pg_to_pg(struct dma_chan *chan,
  328. struct page *dest_pg,
  329. unsigned int dest_off,
  330. struct page *src_pg,
  331. unsigned int src_off,
  332. size_t len)
  333. {
  334. dma_addr_t dest_addr;
  335. dma_addr_t src_addr;
  336. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  337. dest_addr = pci_map_page(ioat_chan->device->pdev,
  338. dest_pg, dest_off, len, PCI_DMA_FROMDEVICE);
  339. src_addr = pci_map_page(ioat_chan->device->pdev,
  340. src_pg, src_off, len, PCI_DMA_TODEVICE);
  341. return do_ioat_dma_memcpy(ioat_chan, dest_addr, src_addr, len);
  342. }
  343. /**
  344. * ioat_dma_memcpy_issue_pending - push potentially unrecognized appended descriptors to hw
  345. * @chan: DMA channel handle
  346. */
  347. static void ioat_dma_memcpy_issue_pending(struct dma_chan *chan)
  348. {
  349. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  350. if (ioat_chan->pending != 0) {
  351. ioat_chan->pending = 0;
  352. writeb(IOAT_CHANCMD_APPEND,
  353. ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
  354. }
  355. }
  356. static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *chan)
  357. {
  358. unsigned long phys_complete;
  359. struct ioat_desc_sw *desc, *_desc;
  360. dma_cookie_t cookie = 0;
  361. prefetch(chan->completion_virt);
  362. if (!spin_trylock(&chan->cleanup_lock))
  363. return;
  364. /* The completion writeback can happen at any time,
  365. so reads by the driver need to be atomic operations
  366. The descriptor physical addresses are limited to 32-bits
  367. when the CPU can only do a 32-bit mov */
  368. #if (BITS_PER_LONG == 64)
  369. phys_complete =
  370. chan->completion_virt->full & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
  371. #else
  372. phys_complete = chan->completion_virt->low & IOAT_LOW_COMPLETION_MASK;
  373. #endif
  374. if ((chan->completion_virt->full & IOAT_CHANSTS_DMA_TRANSFER_STATUS) ==
  375. IOAT_CHANSTS_DMA_TRANSFER_STATUS_HALTED) {
  376. printk("IOAT: Channel halted, chanerr = %x\n",
  377. readl(chan->reg_base + IOAT_CHANERR_OFFSET));
  378. /* TODO do something to salvage the situation */
  379. }
  380. if (phys_complete == chan->last_completion) {
  381. spin_unlock(&chan->cleanup_lock);
  382. return;
  383. }
  384. spin_lock_bh(&chan->desc_lock);
  385. list_for_each_entry_safe(desc, _desc, &chan->used_desc, node) {
  386. /*
  387. * Incoming DMA requests may use multiple descriptors, due to
  388. * exceeding xfercap, perhaps. If so, only the last one will
  389. * have a cookie, and require unmapping.
  390. */
  391. if (desc->cookie) {
  392. cookie = desc->cookie;
  393. /* yes we are unmapping both _page and _single alloc'd
  394. regions with unmap_page. Is this *really* that bad?
  395. */
  396. pci_unmap_page(chan->device->pdev,
  397. pci_unmap_addr(desc, dst),
  398. pci_unmap_len(desc, dst_len),
  399. PCI_DMA_FROMDEVICE);
  400. pci_unmap_page(chan->device->pdev,
  401. pci_unmap_addr(desc, src),
  402. pci_unmap_len(desc, src_len),
  403. PCI_DMA_TODEVICE);
  404. }
  405. if (desc->phys != phys_complete) {
  406. /* a completed entry, but not the last, so cleanup */
  407. list_del(&desc->node);
  408. list_add_tail(&desc->node, &chan->free_desc);
  409. } else {
  410. /* last used desc. Do not remove, so we can append from
  411. it, but don't look at it next time, either */
  412. desc->cookie = 0;
  413. /* TODO check status bits? */
  414. break;
  415. }
  416. }
  417. spin_unlock_bh(&chan->desc_lock);
  418. chan->last_completion = phys_complete;
  419. if (cookie != 0)
  420. chan->completed_cookie = cookie;
  421. spin_unlock(&chan->cleanup_lock);
  422. }
  423. /**
  424. * ioat_dma_is_complete - poll the status of a IOAT DMA transaction
  425. * @chan: IOAT DMA channel handle
  426. * @cookie: DMA transaction identifier
  427. * @done: if not %NULL, updated with last completed transaction
  428. * @used: if not %NULL, updated with last used transaction
  429. */
  430. static enum dma_status ioat_dma_is_complete(struct dma_chan *chan,
  431. dma_cookie_t cookie,
  432. dma_cookie_t *done,
  433. dma_cookie_t *used)
  434. {
  435. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  436. dma_cookie_t last_used;
  437. dma_cookie_t last_complete;
  438. enum dma_status ret;
  439. last_used = chan->cookie;
  440. last_complete = ioat_chan->completed_cookie;
  441. if (done)
  442. *done= last_complete;
  443. if (used)
  444. *used = last_used;
  445. ret = dma_async_is_complete(cookie, last_complete, last_used);
  446. if (ret == DMA_SUCCESS)
  447. return ret;
  448. ioat_dma_memcpy_cleanup(ioat_chan);
  449. last_used = chan->cookie;
  450. last_complete = ioat_chan->completed_cookie;
  451. if (done)
  452. *done= last_complete;
  453. if (used)
  454. *used = last_used;
  455. return dma_async_is_complete(cookie, last_complete, last_used);
  456. }
  457. /* PCI API */
  458. static struct pci_device_id ioat_pci_tbl[] = {
  459. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT) },
  460. { 0, }
  461. };
  462. static struct pci_driver ioat_pci_driver = {
  463. .name = "ioatdma",
  464. .id_table = ioat_pci_tbl,
  465. .probe = ioat_probe,
  466. .shutdown = ioat_shutdown,
  467. .remove = __devexit_p(ioat_remove),
  468. };
  469. static irqreturn_t ioat_do_interrupt(int irq, void *data)
  470. {
  471. struct ioat_device *instance = data;
  472. unsigned long attnstatus;
  473. u8 intrctrl;
  474. intrctrl = readb(instance->reg_base + IOAT_INTRCTRL_OFFSET);
  475. if (!(intrctrl & IOAT_INTRCTRL_MASTER_INT_EN))
  476. return IRQ_NONE;
  477. if (!(intrctrl & IOAT_INTRCTRL_INT_STATUS)) {
  478. writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
  479. return IRQ_NONE;
  480. }
  481. attnstatus = readl(instance->reg_base + IOAT_ATTNSTATUS_OFFSET);
  482. printk(KERN_ERR "ioatdma error: interrupt! status %lx\n", attnstatus);
  483. writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
  484. return IRQ_HANDLED;
  485. }
  486. static void ioat_start_null_desc(struct ioat_dma_chan *ioat_chan)
  487. {
  488. struct ioat_desc_sw *desc;
  489. spin_lock_bh(&ioat_chan->desc_lock);
  490. if (!list_empty(&ioat_chan->free_desc)) {
  491. desc = to_ioat_desc(ioat_chan->free_desc.next);
  492. list_del(&desc->node);
  493. } else {
  494. /* try to get another desc */
  495. spin_unlock_bh(&ioat_chan->desc_lock);
  496. desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_KERNEL);
  497. spin_lock_bh(&ioat_chan->desc_lock);
  498. /* will this ever happen? */
  499. BUG_ON(!desc);
  500. }
  501. desc->hw->ctl = IOAT_DMA_DESCRIPTOR_NUL;
  502. desc->hw->next = 0;
  503. list_add_tail(&desc->node, &ioat_chan->used_desc);
  504. spin_unlock_bh(&ioat_chan->desc_lock);
  505. writel(((u64) desc->phys) & 0x00000000FFFFFFFF,
  506. ioat_chan->reg_base + IOAT_CHAINADDR_OFFSET_LOW);
  507. writel(((u64) desc->phys) >> 32,
  508. ioat_chan->reg_base + IOAT_CHAINADDR_OFFSET_HIGH);
  509. writeb(IOAT_CHANCMD_START, ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
  510. }
  511. /*
  512. * Perform a IOAT transaction to verify the HW works.
  513. */
  514. #define IOAT_TEST_SIZE 2000
  515. static int ioat_self_test(struct ioat_device *device)
  516. {
  517. int i;
  518. u8 *src;
  519. u8 *dest;
  520. struct dma_chan *dma_chan;
  521. dma_cookie_t cookie;
  522. int err = 0;
  523. src = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
  524. if (!src)
  525. return -ENOMEM;
  526. dest = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
  527. if (!dest) {
  528. kfree(src);
  529. return -ENOMEM;
  530. }
  531. /* Fill in src buffer */
  532. for (i = 0; i < IOAT_TEST_SIZE; i++)
  533. src[i] = (u8)i;
  534. /* Start copy, using first DMA channel */
  535. dma_chan = container_of(device->common.channels.next,
  536. struct dma_chan,
  537. device_node);
  538. if (ioat_dma_alloc_chan_resources(dma_chan) < 1) {
  539. err = -ENODEV;
  540. goto out;
  541. }
  542. cookie = ioat_dma_memcpy_buf_to_buf(dma_chan, dest, src, IOAT_TEST_SIZE);
  543. ioat_dma_memcpy_issue_pending(dma_chan);
  544. msleep(1);
  545. if (ioat_dma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
  546. printk(KERN_ERR "ioatdma: Self-test copy timed out, disabling\n");
  547. err = -ENODEV;
  548. goto free_resources;
  549. }
  550. if (memcmp(src, dest, IOAT_TEST_SIZE)) {
  551. printk(KERN_ERR "ioatdma: Self-test copy failed compare, disabling\n");
  552. err = -ENODEV;
  553. goto free_resources;
  554. }
  555. free_resources:
  556. ioat_dma_free_chan_resources(dma_chan);
  557. out:
  558. kfree(src);
  559. kfree(dest);
  560. return err;
  561. }
  562. static int __devinit ioat_probe(struct pci_dev *pdev,
  563. const struct pci_device_id *ent)
  564. {
  565. int err;
  566. unsigned long mmio_start, mmio_len;
  567. void __iomem *reg_base;
  568. struct ioat_device *device;
  569. err = pci_enable_device(pdev);
  570. if (err)
  571. goto err_enable_device;
  572. err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
  573. if (err)
  574. err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  575. if (err)
  576. goto err_set_dma_mask;
  577. err = pci_request_regions(pdev, ioat_pci_driver.name);
  578. if (err)
  579. goto err_request_regions;
  580. mmio_start = pci_resource_start(pdev, 0);
  581. mmio_len = pci_resource_len(pdev, 0);
  582. reg_base = ioremap(mmio_start, mmio_len);
  583. if (!reg_base) {
  584. err = -ENOMEM;
  585. goto err_ioremap;
  586. }
  587. device = kzalloc(sizeof(*device), GFP_KERNEL);
  588. if (!device) {
  589. err = -ENOMEM;
  590. goto err_kzalloc;
  591. }
  592. /* DMA coherent memory pool for DMA descriptor allocations */
  593. device->dma_pool = pci_pool_create("dma_desc_pool", pdev,
  594. sizeof(struct ioat_dma_descriptor), 64, 0);
  595. if (!device->dma_pool) {
  596. err = -ENOMEM;
  597. goto err_dma_pool;
  598. }
  599. device->completion_pool = pci_pool_create("completion_pool", pdev, sizeof(u64), SMP_CACHE_BYTES, SMP_CACHE_BYTES);
  600. if (!device->completion_pool) {
  601. err = -ENOMEM;
  602. goto err_completion_pool;
  603. }
  604. device->pdev = pdev;
  605. pci_set_drvdata(pdev, device);
  606. #ifdef CONFIG_PCI_MSI
  607. if (pci_enable_msi(pdev) == 0) {
  608. device->msi = 1;
  609. } else {
  610. device->msi = 0;
  611. }
  612. #endif
  613. err = request_irq(pdev->irq, &ioat_do_interrupt, IRQF_SHARED, "ioat",
  614. device);
  615. if (err)
  616. goto err_irq;
  617. device->reg_base = reg_base;
  618. writeb(IOAT_INTRCTRL_MASTER_INT_EN, device->reg_base + IOAT_INTRCTRL_OFFSET);
  619. pci_set_master(pdev);
  620. INIT_LIST_HEAD(&device->common.channels);
  621. enumerate_dma_channels(device);
  622. device->common.device_alloc_chan_resources = ioat_dma_alloc_chan_resources;
  623. device->common.device_free_chan_resources = ioat_dma_free_chan_resources;
  624. device->common.device_memcpy_buf_to_buf = ioat_dma_memcpy_buf_to_buf;
  625. device->common.device_memcpy_buf_to_pg = ioat_dma_memcpy_buf_to_pg;
  626. device->common.device_memcpy_pg_to_pg = ioat_dma_memcpy_pg_to_pg;
  627. device->common.device_memcpy_complete = ioat_dma_is_complete;
  628. device->common.device_memcpy_issue_pending = ioat_dma_memcpy_issue_pending;
  629. printk(KERN_INFO "Intel(R) I/OAT DMA Engine found, %d channels\n",
  630. device->common.chancnt);
  631. err = ioat_self_test(device);
  632. if (err)
  633. goto err_self_test;
  634. dma_async_device_register(&device->common);
  635. return 0;
  636. err_self_test:
  637. err_irq:
  638. pci_pool_destroy(device->completion_pool);
  639. err_completion_pool:
  640. pci_pool_destroy(device->dma_pool);
  641. err_dma_pool:
  642. kfree(device);
  643. err_kzalloc:
  644. iounmap(reg_base);
  645. err_ioremap:
  646. pci_release_regions(pdev);
  647. err_request_regions:
  648. err_set_dma_mask:
  649. pci_disable_device(pdev);
  650. err_enable_device:
  651. printk(KERN_ERR "Intel(R) I/OAT DMA Engine initialization failed\n");
  652. return err;
  653. }
  654. static void ioat_shutdown(struct pci_dev *pdev)
  655. {
  656. struct ioat_device *device;
  657. device = pci_get_drvdata(pdev);
  658. dma_async_device_unregister(&device->common);
  659. }
  660. static void __devexit ioat_remove(struct pci_dev *pdev)
  661. {
  662. struct ioat_device *device;
  663. struct dma_chan *chan, *_chan;
  664. struct ioat_dma_chan *ioat_chan;
  665. device = pci_get_drvdata(pdev);
  666. dma_async_device_unregister(&device->common);
  667. free_irq(device->pdev->irq, device);
  668. #ifdef CONFIG_PCI_MSI
  669. if (device->msi)
  670. pci_disable_msi(device->pdev);
  671. #endif
  672. pci_pool_destroy(device->dma_pool);
  673. pci_pool_destroy(device->completion_pool);
  674. iounmap(device->reg_base);
  675. pci_release_regions(pdev);
  676. pci_disable_device(pdev);
  677. list_for_each_entry_safe(chan, _chan, &device->common.channels, device_node) {
  678. ioat_chan = to_ioat_chan(chan);
  679. list_del(&chan->device_node);
  680. kfree(ioat_chan);
  681. }
  682. kfree(device);
  683. }
  684. /* MODULE API */
  685. MODULE_VERSION("1.9");
  686. MODULE_LICENSE("GPL");
  687. MODULE_AUTHOR("Intel Corporation");
  688. static int __init ioat_init_module(void)
  689. {
  690. /* it's currently unsafe to unload this module */
  691. /* if forced, worst case is that rmmod hangs */
  692. __unsafe(THIS_MODULE);
  693. return pci_register_driver(&ioat_pci_driver);
  694. }
  695. module_init(ioat_init_module);
  696. static void __exit ioat_exit_module(void)
  697. {
  698. pci_unregister_driver(&ioat_pci_driver);
  699. }
  700. module_exit(ioat_exit_module);