dma_v3.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright(c) 2004 - 2009 Intel Corporation. All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms and conditions of the GNU General Public License,
  11. * version 2, as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * The full GNU General Public License is included in this distribution in
  23. * the file called "COPYING".
  24. *
  25. * BSD LICENSE
  26. *
  27. * Copyright(c) 2004-2009 Intel Corporation. All rights reserved.
  28. *
  29. * Redistribution and use in source and binary forms, with or without
  30. * modification, are permitted provided that the following conditions are met:
  31. *
  32. * * Redistributions of source code must retain the above copyright
  33. * notice, this list of conditions and the following disclaimer.
  34. * * Redistributions in binary form must reproduce the above copyright
  35. * notice, this list of conditions and the following disclaimer in
  36. * the documentation and/or other materials provided with the
  37. * distribution.
  38. * * Neither the name of Intel Corporation nor the names of its
  39. * contributors may be used to endorse or promote products derived
  40. * from this software without specific prior written permission.
  41. *
  42. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  43. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  44. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  45. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  46. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  47. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  48. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  49. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  50. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  51. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  52. * POSSIBILITY OF SUCH DAMAGE.
  53. */
  54. /*
  55. * Support routines for v3+ hardware
  56. */
  57. #include <linux/pci.h>
  58. #include <linux/dmaengine.h>
  59. #include <linux/dma-mapping.h>
  60. #include "registers.h"
  61. #include "hw.h"
  62. #include "dma.h"
  63. #include "dma_v2.h"
  64. static void ioat3_dma_unmap(struct ioat2_dma_chan *ioat,
  65. struct ioat_ring_ent *desc)
  66. {
  67. struct ioat_chan_common *chan = &ioat->base;
  68. struct pci_dev *pdev = chan->device->pdev;
  69. size_t len = desc->len;
  70. size_t offset = len - desc->hw->size;
  71. struct dma_async_tx_descriptor *tx = &desc->txd;
  72. enum dma_ctrl_flags flags = tx->flags;
  73. switch (desc->hw->ctl_f.op) {
  74. case IOAT_OP_COPY:
  75. ioat_dma_unmap(chan, flags, len, desc->hw);
  76. break;
  77. case IOAT_OP_FILL: {
  78. struct ioat_fill_descriptor *hw = desc->fill;
  79. if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP))
  80. ioat_unmap(pdev, hw->dst_addr - offset, len,
  81. PCI_DMA_FROMDEVICE, flags, 1);
  82. break;
  83. }
  84. default:
  85. dev_err(&pdev->dev, "%s: unknown op type: %#x\n",
  86. __func__, desc->hw->ctl_f.op);
  87. }
  88. }
  89. static void __cleanup(struct ioat2_dma_chan *ioat, unsigned long phys_complete)
  90. {
  91. struct ioat_chan_common *chan = &ioat->base;
  92. struct ioat_ring_ent *desc;
  93. bool seen_current = false;
  94. u16 active;
  95. int i;
  96. dev_dbg(to_dev(chan), "%s: head: %#x tail: %#x issued: %#x\n",
  97. __func__, ioat->head, ioat->tail, ioat->issued);
  98. active = ioat2_ring_active(ioat);
  99. for (i = 0; i < active && !seen_current; i++) {
  100. struct dma_async_tx_descriptor *tx;
  101. prefetch(ioat2_get_ring_ent(ioat, ioat->tail + i + 1));
  102. desc = ioat2_get_ring_ent(ioat, ioat->tail + i);
  103. dump_desc_dbg(ioat, desc);
  104. tx = &desc->txd;
  105. if (tx->cookie) {
  106. chan->completed_cookie = tx->cookie;
  107. ioat3_dma_unmap(ioat, desc);
  108. tx->cookie = 0;
  109. if (tx->callback) {
  110. tx->callback(tx->callback_param);
  111. tx->callback = NULL;
  112. }
  113. }
  114. if (tx->phys == phys_complete)
  115. seen_current = true;
  116. }
  117. ioat->tail += i;
  118. BUG_ON(!seen_current); /* no active descs have written a completion? */
  119. chan->last_completion = phys_complete;
  120. if (ioat->head == ioat->tail) {
  121. dev_dbg(to_dev(chan), "%s: cancel completion timeout\n",
  122. __func__);
  123. clear_bit(IOAT_COMPLETION_PENDING, &chan->state);
  124. mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
  125. }
  126. }
  127. static void ioat3_cleanup(struct ioat2_dma_chan *ioat)
  128. {
  129. struct ioat_chan_common *chan = &ioat->base;
  130. unsigned long phys_complete;
  131. prefetch(chan->completion);
  132. if (!spin_trylock_bh(&chan->cleanup_lock))
  133. return;
  134. if (!ioat_cleanup_preamble(chan, &phys_complete)) {
  135. spin_unlock_bh(&chan->cleanup_lock);
  136. return;
  137. }
  138. if (!spin_trylock_bh(&ioat->ring_lock)) {
  139. spin_unlock_bh(&chan->cleanup_lock);
  140. return;
  141. }
  142. __cleanup(ioat, phys_complete);
  143. spin_unlock_bh(&ioat->ring_lock);
  144. spin_unlock_bh(&chan->cleanup_lock);
  145. }
  146. static void ioat3_cleanup_tasklet(unsigned long data)
  147. {
  148. struct ioat2_dma_chan *ioat = (void *) data;
  149. ioat3_cleanup(ioat);
  150. writew(IOAT_CHANCTRL_RUN, ioat->base.reg_base + IOAT_CHANCTRL_OFFSET);
  151. }
  152. static void ioat3_restart_channel(struct ioat2_dma_chan *ioat)
  153. {
  154. struct ioat_chan_common *chan = &ioat->base;
  155. unsigned long phys_complete;
  156. u32 status;
  157. status = ioat_chansts(chan);
  158. if (is_ioat_active(status) || is_ioat_idle(status))
  159. ioat_suspend(chan);
  160. while (is_ioat_active(status) || is_ioat_idle(status)) {
  161. status = ioat_chansts(chan);
  162. cpu_relax();
  163. }
  164. if (ioat_cleanup_preamble(chan, &phys_complete))
  165. __cleanup(ioat, phys_complete);
  166. __ioat2_restart_chan(ioat);
  167. }
  168. static void ioat3_timer_event(unsigned long data)
  169. {
  170. struct ioat2_dma_chan *ioat = (void *) data;
  171. struct ioat_chan_common *chan = &ioat->base;
  172. spin_lock_bh(&chan->cleanup_lock);
  173. if (test_bit(IOAT_COMPLETION_PENDING, &chan->state)) {
  174. unsigned long phys_complete;
  175. u64 status;
  176. spin_lock_bh(&ioat->ring_lock);
  177. status = ioat_chansts(chan);
  178. /* when halted due to errors check for channel
  179. * programming errors before advancing the completion state
  180. */
  181. if (is_ioat_halted(status)) {
  182. u32 chanerr;
  183. chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
  184. BUG_ON(is_ioat_bug(chanerr));
  185. }
  186. /* if we haven't made progress and we have already
  187. * acknowledged a pending completion once, then be more
  188. * forceful with a restart
  189. */
  190. if (ioat_cleanup_preamble(chan, &phys_complete))
  191. __cleanup(ioat, phys_complete);
  192. else if (test_bit(IOAT_COMPLETION_ACK, &chan->state))
  193. ioat3_restart_channel(ioat);
  194. else {
  195. set_bit(IOAT_COMPLETION_ACK, &chan->state);
  196. mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
  197. }
  198. spin_unlock_bh(&ioat->ring_lock);
  199. } else {
  200. u16 active;
  201. /* if the ring is idle, empty, and oversized try to step
  202. * down the size
  203. */
  204. spin_lock_bh(&ioat->ring_lock);
  205. active = ioat2_ring_active(ioat);
  206. if (active == 0 && ioat->alloc_order > ioat_get_alloc_order())
  207. reshape_ring(ioat, ioat->alloc_order-1);
  208. spin_unlock_bh(&ioat->ring_lock);
  209. /* keep shrinking until we get back to our minimum
  210. * default size
  211. */
  212. if (ioat->alloc_order > ioat_get_alloc_order())
  213. mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
  214. }
  215. spin_unlock_bh(&chan->cleanup_lock);
  216. }
  217. static enum dma_status
  218. ioat3_is_complete(struct dma_chan *c, dma_cookie_t cookie,
  219. dma_cookie_t *done, dma_cookie_t *used)
  220. {
  221. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  222. if (ioat_is_complete(c, cookie, done, used) == DMA_SUCCESS)
  223. return DMA_SUCCESS;
  224. ioat3_cleanup(ioat);
  225. return ioat_is_complete(c, cookie, done, used);
  226. }
  227. static struct dma_async_tx_descriptor *
  228. ioat3_prep_memset_lock(struct dma_chan *c, dma_addr_t dest, int value,
  229. size_t len, unsigned long flags)
  230. {
  231. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  232. struct ioat_ring_ent *desc;
  233. size_t total_len = len;
  234. struct ioat_fill_descriptor *fill;
  235. int num_descs;
  236. u64 src_data = (0x0101010101010101ULL) * (value & 0xff);
  237. u16 idx;
  238. int i;
  239. num_descs = ioat2_xferlen_to_descs(ioat, len);
  240. if (likely(num_descs) &&
  241. ioat2_alloc_and_lock(&idx, ioat, num_descs) == 0)
  242. /* pass */;
  243. else
  244. return NULL;
  245. for (i = 0; i < num_descs; i++) {
  246. size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
  247. desc = ioat2_get_ring_ent(ioat, idx + i);
  248. fill = desc->fill;
  249. fill->size = xfer_size;
  250. fill->src_data = src_data;
  251. fill->dst_addr = dest;
  252. fill->ctl = 0;
  253. fill->ctl_f.op = IOAT_OP_FILL;
  254. len -= xfer_size;
  255. dest += xfer_size;
  256. dump_desc_dbg(ioat, desc);
  257. }
  258. desc->txd.flags = flags;
  259. desc->len = total_len;
  260. fill->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
  261. fill->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
  262. fill->ctl_f.compl_write = 1;
  263. dump_desc_dbg(ioat, desc);
  264. /* we leave the channel locked to ensure in order submission */
  265. return &desc->txd;
  266. }
  267. int __devinit ioat3_dma_probe(struct ioatdma_device *device, int dca)
  268. {
  269. struct pci_dev *pdev = device->pdev;
  270. struct dma_device *dma;
  271. struct dma_chan *c;
  272. struct ioat_chan_common *chan;
  273. int err;
  274. u16 dev_id;
  275. u32 cap;
  276. device->enumerate_channels = ioat2_enumerate_channels;
  277. device->cleanup_tasklet = ioat3_cleanup_tasklet;
  278. device->timer_fn = ioat3_timer_event;
  279. dma = &device->common;
  280. dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy_lock;
  281. dma->device_issue_pending = ioat2_issue_pending;
  282. dma->device_alloc_chan_resources = ioat2_alloc_chan_resources;
  283. dma->device_free_chan_resources = ioat2_free_chan_resources;
  284. dma->device_is_tx_complete = ioat3_is_complete;
  285. cap = readl(device->reg_base + IOAT_DMA_CAP_OFFSET);
  286. if (cap & IOAT_CAP_FILL_BLOCK) {
  287. dma_cap_set(DMA_MEMSET, dma->cap_mask);
  288. dma->device_prep_dma_memset = ioat3_prep_memset_lock;
  289. }
  290. /* -= IOAT ver.3 workarounds =- */
  291. /* Write CHANERRMSK_INT with 3E07h to mask out the errors
  292. * that can cause stability issues for IOAT ver.3
  293. */
  294. pci_write_config_dword(pdev, IOAT_PCI_CHANERRMASK_INT_OFFSET, 0x3e07);
  295. /* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
  296. * (workaround for spurious config parity error after restart)
  297. */
  298. pci_read_config_word(pdev, IOAT_PCI_DEVICE_ID_OFFSET, &dev_id);
  299. if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0)
  300. pci_write_config_dword(pdev, IOAT_PCI_DMAUNCERRSTS_OFFSET, 0x10);
  301. err = ioat_probe(device);
  302. if (err)
  303. return err;
  304. ioat_set_tcp_copy_break(262144);
  305. list_for_each_entry(c, &dma->channels, device_node) {
  306. chan = to_chan_common(c);
  307. writel(IOAT_DMA_DCA_ANY_CPU,
  308. chan->reg_base + IOAT_DCACTRL_OFFSET);
  309. }
  310. err = ioat_register(device);
  311. if (err)
  312. return err;
  313. if (dca)
  314. device->dca = ioat3_dca_init(pdev, device->reg_base);
  315. return 0;
  316. }