dma_v2.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. /*
  2. * Intel I/OAT DMA Linux driver
  3. * Copyright(c) 2004 - 2009 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 (versions >= 2), which
  24. * does asynchronous data movement and checksumming 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 <linux/workqueue.h>
  34. #include <linux/i7300_idle.h>
  35. #include "dma.h"
  36. #include "dma_v2.h"
  37. #include "registers.h"
  38. #include "hw.h"
  39. int ioat_ring_alloc_order = 8;
  40. module_param(ioat_ring_alloc_order, int, 0644);
  41. MODULE_PARM_DESC(ioat_ring_alloc_order,
  42. "ioat2+: allocate 2^n descriptors per channel (default: n=8)");
  43. static int ioat_ring_max_alloc_order = IOAT_MAX_ORDER;
  44. module_param(ioat_ring_max_alloc_order, int, 0644);
  45. MODULE_PARM_DESC(ioat_ring_max_alloc_order,
  46. "ioat2+: upper limit for dynamic ring resizing (default: n=16)");
  47. void __ioat2_issue_pending(struct ioat2_dma_chan *ioat)
  48. {
  49. void * __iomem reg_base = ioat->base.reg_base;
  50. ioat->pending = 0;
  51. ioat->dmacount += ioat2_ring_pending(ioat);;
  52. ioat->issued = ioat->head;
  53. /* make descriptor updates globally visible before notifying channel */
  54. wmb();
  55. writew(ioat->dmacount, reg_base + IOAT_CHAN_DMACOUNT_OFFSET);
  56. dev_dbg(to_dev(&ioat->base),
  57. "%s: head: %#x tail: %#x issued: %#x count: %#x\n",
  58. __func__, ioat->head, ioat->tail, ioat->issued, ioat->dmacount);
  59. }
  60. void ioat2_issue_pending(struct dma_chan *chan)
  61. {
  62. struct ioat2_dma_chan *ioat = to_ioat2_chan(chan);
  63. spin_lock_bh(&ioat->ring_lock);
  64. if (ioat->pending == 1)
  65. __ioat2_issue_pending(ioat);
  66. spin_unlock_bh(&ioat->ring_lock);
  67. }
  68. /**
  69. * ioat2_update_pending - log pending descriptors
  70. * @ioat: ioat2+ channel
  71. *
  72. * set pending to '1' unless pending is already set to '2', pending == 2
  73. * indicates that submission is temporarily blocked due to an in-flight
  74. * reset. If we are already above the ioat_pending_level threshold then
  75. * just issue pending.
  76. *
  77. * called with ring_lock held
  78. */
  79. static void ioat2_update_pending(struct ioat2_dma_chan *ioat)
  80. {
  81. if (unlikely(ioat->pending == 2))
  82. return;
  83. else if (ioat2_ring_pending(ioat) > ioat_pending_level)
  84. __ioat2_issue_pending(ioat);
  85. else
  86. ioat->pending = 1;
  87. }
  88. static void __ioat2_start_null_desc(struct ioat2_dma_chan *ioat)
  89. {
  90. struct ioat_ring_ent *desc;
  91. struct ioat_dma_descriptor *hw;
  92. int idx;
  93. if (ioat2_ring_space(ioat) < 1) {
  94. dev_err(to_dev(&ioat->base),
  95. "Unable to start null desc - ring full\n");
  96. return;
  97. }
  98. dev_dbg(to_dev(&ioat->base), "%s: head: %#x tail: %#x issued: %#x\n",
  99. __func__, ioat->head, ioat->tail, ioat->issued);
  100. idx = ioat2_desc_alloc(ioat, 1);
  101. desc = ioat2_get_ring_ent(ioat, idx);
  102. hw = desc->hw;
  103. hw->ctl = 0;
  104. hw->ctl_f.null = 1;
  105. hw->ctl_f.int_en = 1;
  106. hw->ctl_f.compl_write = 1;
  107. /* set size to non-zero value (channel returns error when size is 0) */
  108. hw->size = NULL_DESC_BUFFER_SIZE;
  109. hw->src_addr = 0;
  110. hw->dst_addr = 0;
  111. async_tx_ack(&desc->txd);
  112. ioat2_set_chainaddr(ioat, desc->txd.phys);
  113. dump_desc_dbg(ioat, desc);
  114. __ioat2_issue_pending(ioat);
  115. }
  116. static void ioat2_start_null_desc(struct ioat2_dma_chan *ioat)
  117. {
  118. spin_lock_bh(&ioat->ring_lock);
  119. __ioat2_start_null_desc(ioat);
  120. spin_unlock_bh(&ioat->ring_lock);
  121. }
  122. static void __cleanup(struct ioat2_dma_chan *ioat, unsigned long phys_complete)
  123. {
  124. struct ioat_chan_common *chan = &ioat->base;
  125. struct dma_async_tx_descriptor *tx;
  126. struct ioat_ring_ent *desc;
  127. bool seen_current = false;
  128. u16 active;
  129. int i;
  130. dev_dbg(to_dev(chan), "%s: head: %#x tail: %#x issued: %#x\n",
  131. __func__, ioat->head, ioat->tail, ioat->issued);
  132. active = ioat2_ring_active(ioat);
  133. for (i = 0; i < active && !seen_current; i++) {
  134. prefetch(ioat2_get_ring_ent(ioat, ioat->tail + i + 1));
  135. desc = ioat2_get_ring_ent(ioat, ioat->tail + i);
  136. tx = &desc->txd;
  137. dump_desc_dbg(ioat, desc);
  138. if (tx->cookie) {
  139. ioat_dma_unmap(chan, tx->flags, desc->len, desc->hw);
  140. chan->completed_cookie = tx->cookie;
  141. tx->cookie = 0;
  142. if (tx->callback) {
  143. tx->callback(tx->callback_param);
  144. tx->callback = NULL;
  145. }
  146. }
  147. if (tx->phys == phys_complete)
  148. seen_current = true;
  149. }
  150. ioat->tail += i;
  151. BUG_ON(!seen_current); /* no active descs have written a completion? */
  152. chan->last_completion = phys_complete;
  153. if (ioat->head == ioat->tail) {
  154. dev_dbg(to_dev(chan), "%s: cancel completion timeout\n",
  155. __func__);
  156. clear_bit(IOAT_COMPLETION_PENDING, &chan->state);
  157. mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
  158. }
  159. }
  160. /**
  161. * ioat2_cleanup - clean finished descriptors (advance tail pointer)
  162. * @chan: ioat channel to be cleaned up
  163. */
  164. static void ioat2_cleanup(struct ioat2_dma_chan *ioat)
  165. {
  166. struct ioat_chan_common *chan = &ioat->base;
  167. unsigned long phys_complete;
  168. prefetch(chan->completion);
  169. if (!spin_trylock_bh(&chan->cleanup_lock))
  170. return;
  171. if (!ioat_cleanup_preamble(chan, &phys_complete)) {
  172. spin_unlock_bh(&chan->cleanup_lock);
  173. return;
  174. }
  175. if (!spin_trylock_bh(&ioat->ring_lock)) {
  176. spin_unlock_bh(&chan->cleanup_lock);
  177. return;
  178. }
  179. __cleanup(ioat, phys_complete);
  180. spin_unlock_bh(&ioat->ring_lock);
  181. spin_unlock_bh(&chan->cleanup_lock);
  182. }
  183. static void ioat2_cleanup_tasklet(unsigned long data)
  184. {
  185. struct ioat2_dma_chan *ioat = (void *) data;
  186. ioat2_cleanup(ioat);
  187. writew(IOAT_CHANCTRL_RUN, ioat->base.reg_base + IOAT_CHANCTRL_OFFSET);
  188. }
  189. void __ioat2_restart_chan(struct ioat2_dma_chan *ioat)
  190. {
  191. struct ioat_chan_common *chan = &ioat->base;
  192. /* set the tail to be re-issued */
  193. ioat->issued = ioat->tail;
  194. ioat->dmacount = 0;
  195. set_bit(IOAT_COMPLETION_PENDING, &chan->state);
  196. mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
  197. dev_dbg(to_dev(chan),
  198. "%s: head: %#x tail: %#x issued: %#x count: %#x\n",
  199. __func__, ioat->head, ioat->tail, ioat->issued, ioat->dmacount);
  200. if (ioat2_ring_pending(ioat)) {
  201. struct ioat_ring_ent *desc;
  202. desc = ioat2_get_ring_ent(ioat, ioat->tail);
  203. ioat2_set_chainaddr(ioat, desc->txd.phys);
  204. __ioat2_issue_pending(ioat);
  205. } else
  206. __ioat2_start_null_desc(ioat);
  207. }
  208. static void ioat2_restart_channel(struct ioat2_dma_chan *ioat)
  209. {
  210. struct ioat_chan_common *chan = &ioat->base;
  211. unsigned long phys_complete;
  212. u32 status;
  213. status = ioat_chansts(chan);
  214. if (is_ioat_active(status) || is_ioat_idle(status))
  215. ioat_suspend(chan);
  216. while (is_ioat_active(status) || is_ioat_idle(status)) {
  217. status = ioat_chansts(chan);
  218. cpu_relax();
  219. }
  220. if (ioat_cleanup_preamble(chan, &phys_complete))
  221. __cleanup(ioat, phys_complete);
  222. __ioat2_restart_chan(ioat);
  223. }
  224. static void ioat2_timer_event(unsigned long data)
  225. {
  226. struct ioat2_dma_chan *ioat = (void *) data;
  227. struct ioat_chan_common *chan = &ioat->base;
  228. spin_lock_bh(&chan->cleanup_lock);
  229. if (test_bit(IOAT_COMPLETION_PENDING, &chan->state)) {
  230. unsigned long phys_complete;
  231. u64 status;
  232. spin_lock_bh(&ioat->ring_lock);
  233. status = ioat_chansts(chan);
  234. /* when halted due to errors check for channel
  235. * programming errors before advancing the completion state
  236. */
  237. if (is_ioat_halted(status)) {
  238. u32 chanerr;
  239. chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
  240. BUG_ON(is_ioat_bug(chanerr));
  241. }
  242. /* if we haven't made progress and we have already
  243. * acknowledged a pending completion once, then be more
  244. * forceful with a restart
  245. */
  246. if (ioat_cleanup_preamble(chan, &phys_complete))
  247. __cleanup(ioat, phys_complete);
  248. else if (test_bit(IOAT_COMPLETION_ACK, &chan->state))
  249. ioat2_restart_channel(ioat);
  250. else {
  251. set_bit(IOAT_COMPLETION_ACK, &chan->state);
  252. mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
  253. }
  254. spin_unlock_bh(&ioat->ring_lock);
  255. } else {
  256. u16 active;
  257. /* if the ring is idle, empty, and oversized try to step
  258. * down the size
  259. */
  260. spin_lock_bh(&ioat->ring_lock);
  261. active = ioat2_ring_active(ioat);
  262. if (active == 0 && ioat->alloc_order > ioat_get_alloc_order())
  263. reshape_ring(ioat, ioat->alloc_order-1);
  264. spin_unlock_bh(&ioat->ring_lock);
  265. /* keep shrinking until we get back to our minimum
  266. * default size
  267. */
  268. if (ioat->alloc_order > ioat_get_alloc_order())
  269. mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
  270. }
  271. spin_unlock_bh(&chan->cleanup_lock);
  272. }
  273. /**
  274. * ioat2_enumerate_channels - find and initialize the device's channels
  275. * @device: the device to be enumerated
  276. */
  277. int ioat2_enumerate_channels(struct ioatdma_device *device)
  278. {
  279. struct ioat2_dma_chan *ioat;
  280. struct device *dev = &device->pdev->dev;
  281. struct dma_device *dma = &device->common;
  282. u8 xfercap_log;
  283. int i;
  284. INIT_LIST_HEAD(&dma->channels);
  285. dma->chancnt = readb(device->reg_base + IOAT_CHANCNT_OFFSET);
  286. dma->chancnt &= 0x1f; /* bits [4:0] valid */
  287. if (dma->chancnt > ARRAY_SIZE(device->idx)) {
  288. dev_warn(dev, "(%d) exceeds max supported channels (%zu)\n",
  289. dma->chancnt, ARRAY_SIZE(device->idx));
  290. dma->chancnt = ARRAY_SIZE(device->idx);
  291. }
  292. xfercap_log = readb(device->reg_base + IOAT_XFERCAP_OFFSET);
  293. xfercap_log &= 0x1f; /* bits [4:0] valid */
  294. if (xfercap_log == 0)
  295. return 0;
  296. dev_dbg(dev, "%s: xfercap = %d\n", __func__, 1 << xfercap_log);
  297. /* FIXME which i/oat version is i7300? */
  298. #ifdef CONFIG_I7300_IDLE_IOAT_CHANNEL
  299. if (i7300_idle_platform_probe(NULL, NULL, 1) == 0)
  300. dma->chancnt--;
  301. #endif
  302. for (i = 0; i < dma->chancnt; i++) {
  303. ioat = devm_kzalloc(dev, sizeof(*ioat), GFP_KERNEL);
  304. if (!ioat)
  305. break;
  306. ioat_init_channel(device, &ioat->base, i,
  307. device->timer_fn,
  308. device->cleanup_tasklet,
  309. (unsigned long) ioat);
  310. ioat->xfercap_log = xfercap_log;
  311. spin_lock_init(&ioat->ring_lock);
  312. }
  313. dma->chancnt = i;
  314. return i;
  315. }
  316. static dma_cookie_t ioat2_tx_submit_unlock(struct dma_async_tx_descriptor *tx)
  317. {
  318. struct dma_chan *c = tx->chan;
  319. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  320. struct ioat_chan_common *chan = &ioat->base;
  321. dma_cookie_t cookie = c->cookie;
  322. cookie++;
  323. if (cookie < 0)
  324. cookie = 1;
  325. tx->cookie = cookie;
  326. c->cookie = cookie;
  327. dev_dbg(to_dev(&ioat->base), "%s: cookie: %d\n", __func__, cookie);
  328. if (!test_and_set_bit(IOAT_COMPLETION_PENDING, &chan->state))
  329. mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
  330. ioat2_update_pending(ioat);
  331. spin_unlock_bh(&ioat->ring_lock);
  332. return cookie;
  333. }
  334. static struct ioat_ring_ent *ioat2_alloc_ring_ent(struct dma_chan *chan, gfp_t flags)
  335. {
  336. struct ioat_dma_descriptor *hw;
  337. struct ioat_ring_ent *desc;
  338. struct ioatdma_device *dma;
  339. dma_addr_t phys;
  340. dma = to_ioatdma_device(chan->device);
  341. hw = pci_pool_alloc(dma->dma_pool, flags, &phys);
  342. if (!hw)
  343. return NULL;
  344. memset(hw, 0, sizeof(*hw));
  345. desc = kzalloc(sizeof(*desc), flags);
  346. if (!desc) {
  347. pci_pool_free(dma->dma_pool, hw, phys);
  348. return NULL;
  349. }
  350. dma_async_tx_descriptor_init(&desc->txd, chan);
  351. desc->txd.tx_submit = ioat2_tx_submit_unlock;
  352. desc->hw = hw;
  353. desc->txd.phys = phys;
  354. return desc;
  355. }
  356. static void ioat2_free_ring_ent(struct ioat_ring_ent *desc, struct dma_chan *chan)
  357. {
  358. struct ioatdma_device *dma;
  359. dma = to_ioatdma_device(chan->device);
  360. pci_pool_free(dma->dma_pool, desc->hw, desc->txd.phys);
  361. kfree(desc);
  362. }
  363. static struct ioat_ring_ent **ioat2_alloc_ring(struct dma_chan *c, int order, gfp_t flags)
  364. {
  365. struct ioat_ring_ent **ring;
  366. int descs = 1 << order;
  367. int i;
  368. if (order > ioat_get_max_alloc_order())
  369. return NULL;
  370. /* allocate the array to hold the software ring */
  371. ring = kcalloc(descs, sizeof(*ring), flags);
  372. if (!ring)
  373. return NULL;
  374. for (i = 0; i < descs; i++) {
  375. ring[i] = ioat2_alloc_ring_ent(c, flags);
  376. if (!ring[i]) {
  377. while (i--)
  378. ioat2_free_ring_ent(ring[i], c);
  379. kfree(ring);
  380. return NULL;
  381. }
  382. set_desc_id(ring[i], i);
  383. }
  384. /* link descs */
  385. for (i = 0; i < descs-1; i++) {
  386. struct ioat_ring_ent *next = ring[i+1];
  387. struct ioat_dma_descriptor *hw = ring[i]->hw;
  388. hw->next = next->txd.phys;
  389. }
  390. ring[i]->hw->next = ring[0]->txd.phys;
  391. return ring;
  392. }
  393. /* ioat2_alloc_chan_resources - allocate/initialize ioat2 descriptor ring
  394. * @chan: channel to be initialized
  395. */
  396. int ioat2_alloc_chan_resources(struct dma_chan *c)
  397. {
  398. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  399. struct ioat_chan_common *chan = &ioat->base;
  400. struct ioat_ring_ent **ring;
  401. u32 chanerr;
  402. int order;
  403. /* have we already been set up? */
  404. if (ioat->ring)
  405. return 1 << ioat->alloc_order;
  406. /* Setup register to interrupt and write completion status on error */
  407. writew(IOAT_CHANCTRL_RUN, chan->reg_base + IOAT_CHANCTRL_OFFSET);
  408. chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
  409. if (chanerr) {
  410. dev_err(to_dev(chan), "CHANERR = %x, clearing\n", chanerr);
  411. writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
  412. }
  413. /* allocate a completion writeback area */
  414. /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
  415. chan->completion = pci_pool_alloc(chan->device->completion_pool,
  416. GFP_KERNEL, &chan->completion_dma);
  417. if (!chan->completion)
  418. return -ENOMEM;
  419. memset(chan->completion, 0, sizeof(*chan->completion));
  420. writel(((u64) chan->completion_dma) & 0x00000000FFFFFFFF,
  421. chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
  422. writel(((u64) chan->completion_dma) >> 32,
  423. chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
  424. order = ioat_get_alloc_order();
  425. ring = ioat2_alloc_ring(c, order, GFP_KERNEL);
  426. if (!ring)
  427. return -ENOMEM;
  428. spin_lock_bh(&ioat->ring_lock);
  429. ioat->ring = ring;
  430. ioat->head = 0;
  431. ioat->issued = 0;
  432. ioat->tail = 0;
  433. ioat->pending = 0;
  434. ioat->alloc_order = order;
  435. spin_unlock_bh(&ioat->ring_lock);
  436. tasklet_enable(&chan->cleanup_task);
  437. ioat2_start_null_desc(ioat);
  438. return 1 << ioat->alloc_order;
  439. }
  440. bool reshape_ring(struct ioat2_dma_chan *ioat, int order)
  441. {
  442. /* reshape differs from normal ring allocation in that we want
  443. * to allocate a new software ring while only
  444. * extending/truncating the hardware ring
  445. */
  446. struct ioat_chan_common *chan = &ioat->base;
  447. struct dma_chan *c = &chan->common;
  448. const u16 curr_size = ioat2_ring_mask(ioat) + 1;
  449. const u16 active = ioat2_ring_active(ioat);
  450. const u16 new_size = 1 << order;
  451. struct ioat_ring_ent **ring;
  452. u16 i;
  453. if (order > ioat_get_max_alloc_order())
  454. return false;
  455. /* double check that we have at least 1 free descriptor */
  456. if (active == curr_size)
  457. return false;
  458. /* when shrinking, verify that we can hold the current active
  459. * set in the new ring
  460. */
  461. if (active >= new_size)
  462. return false;
  463. /* allocate the array to hold the software ring */
  464. ring = kcalloc(new_size, sizeof(*ring), GFP_NOWAIT);
  465. if (!ring)
  466. return false;
  467. /* allocate/trim descriptors as needed */
  468. if (new_size > curr_size) {
  469. /* copy current descriptors to the new ring */
  470. for (i = 0; i < curr_size; i++) {
  471. u16 curr_idx = (ioat->tail+i) & (curr_size-1);
  472. u16 new_idx = (ioat->tail+i) & (new_size-1);
  473. ring[new_idx] = ioat->ring[curr_idx];
  474. set_desc_id(ring[new_idx], new_idx);
  475. }
  476. /* add new descriptors to the ring */
  477. for (i = curr_size; i < new_size; i++) {
  478. u16 new_idx = (ioat->tail+i) & (new_size-1);
  479. ring[new_idx] = ioat2_alloc_ring_ent(c, GFP_NOWAIT);
  480. if (!ring[new_idx]) {
  481. while (i--) {
  482. u16 new_idx = (ioat->tail+i) & (new_size-1);
  483. ioat2_free_ring_ent(ring[new_idx], c);
  484. }
  485. kfree(ring);
  486. return false;
  487. }
  488. set_desc_id(ring[new_idx], new_idx);
  489. }
  490. /* hw link new descriptors */
  491. for (i = curr_size-1; i < new_size; i++) {
  492. u16 new_idx = (ioat->tail+i) & (new_size-1);
  493. struct ioat_ring_ent *next = ring[(new_idx+1) & (new_size-1)];
  494. struct ioat_dma_descriptor *hw = ring[new_idx]->hw;
  495. hw->next = next->txd.phys;
  496. }
  497. } else {
  498. struct ioat_dma_descriptor *hw;
  499. struct ioat_ring_ent *next;
  500. /* copy current descriptors to the new ring, dropping the
  501. * removed descriptors
  502. */
  503. for (i = 0; i < new_size; i++) {
  504. u16 curr_idx = (ioat->tail+i) & (curr_size-1);
  505. u16 new_idx = (ioat->tail+i) & (new_size-1);
  506. ring[new_idx] = ioat->ring[curr_idx];
  507. set_desc_id(ring[new_idx], new_idx);
  508. }
  509. /* free deleted descriptors */
  510. for (i = new_size; i < curr_size; i++) {
  511. struct ioat_ring_ent *ent;
  512. ent = ioat2_get_ring_ent(ioat, ioat->tail+i);
  513. ioat2_free_ring_ent(ent, c);
  514. }
  515. /* fix up hardware ring */
  516. hw = ring[(ioat->tail+new_size-1) & (new_size-1)]->hw;
  517. next = ring[(ioat->tail+new_size) & (new_size-1)];
  518. hw->next = next->txd.phys;
  519. }
  520. dev_dbg(to_dev(chan), "%s: allocated %d descriptors\n",
  521. __func__, new_size);
  522. kfree(ioat->ring);
  523. ioat->ring = ring;
  524. ioat->alloc_order = order;
  525. return true;
  526. }
  527. /**
  528. * ioat2_alloc_and_lock - common descriptor alloc boilerplate for ioat2,3 ops
  529. * @idx: gets starting descriptor index on successful allocation
  530. * @ioat: ioat2,3 channel (ring) to operate on
  531. * @num_descs: allocation length
  532. */
  533. int ioat2_alloc_and_lock(u16 *idx, struct ioat2_dma_chan *ioat, int num_descs)
  534. {
  535. struct ioat_chan_common *chan = &ioat->base;
  536. spin_lock_bh(&ioat->ring_lock);
  537. /* never allow the last descriptor to be consumed, we need at
  538. * least one free at all times to allow for on-the-fly ring
  539. * resizing.
  540. */
  541. while (unlikely(ioat2_ring_space(ioat) <= num_descs)) {
  542. if (reshape_ring(ioat, ioat->alloc_order + 1) &&
  543. ioat2_ring_space(ioat) > num_descs)
  544. break;
  545. if (printk_ratelimit())
  546. dev_dbg(to_dev(chan),
  547. "%s: ring full! num_descs: %d (%x:%x:%x)\n",
  548. __func__, num_descs, ioat->head, ioat->tail,
  549. ioat->issued);
  550. spin_unlock_bh(&ioat->ring_lock);
  551. /* progress reclaim in the allocation failure case we
  552. * may be called under bh_disabled so we need to trigger
  553. * the timer event directly
  554. */
  555. spin_lock_bh(&chan->cleanup_lock);
  556. if (jiffies > chan->timer.expires &&
  557. timer_pending(&chan->timer)) {
  558. struct ioatdma_device *device = chan->device;
  559. mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
  560. spin_unlock_bh(&chan->cleanup_lock);
  561. device->timer_fn((unsigned long) ioat);
  562. } else
  563. spin_unlock_bh(&chan->cleanup_lock);
  564. return -ENOMEM;
  565. }
  566. dev_dbg(to_dev(chan), "%s: num_descs: %d (%x:%x:%x)\n",
  567. __func__, num_descs, ioat->head, ioat->tail, ioat->issued);
  568. *idx = ioat2_desc_alloc(ioat, num_descs);
  569. return 0; /* with ioat->ring_lock held */
  570. }
  571. struct dma_async_tx_descriptor *
  572. ioat2_dma_prep_memcpy_lock(struct dma_chan *c, dma_addr_t dma_dest,
  573. dma_addr_t dma_src, size_t len, unsigned long flags)
  574. {
  575. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  576. struct ioat_dma_descriptor *hw;
  577. struct ioat_ring_ent *desc;
  578. dma_addr_t dst = dma_dest;
  579. dma_addr_t src = dma_src;
  580. size_t total_len = len;
  581. int num_descs;
  582. u16 idx;
  583. int i;
  584. num_descs = ioat2_xferlen_to_descs(ioat, len);
  585. if (likely(num_descs) &&
  586. ioat2_alloc_and_lock(&idx, ioat, num_descs) == 0)
  587. /* pass */;
  588. else
  589. return NULL;
  590. for (i = 0; i < num_descs; i++) {
  591. size_t copy = min_t(size_t, len, 1 << ioat->xfercap_log);
  592. desc = ioat2_get_ring_ent(ioat, idx + i);
  593. hw = desc->hw;
  594. hw->size = copy;
  595. hw->ctl = 0;
  596. hw->src_addr = src;
  597. hw->dst_addr = dst;
  598. len -= copy;
  599. dst += copy;
  600. src += copy;
  601. dump_desc_dbg(ioat, desc);
  602. }
  603. desc->txd.flags = flags;
  604. desc->len = total_len;
  605. hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
  606. hw->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
  607. hw->ctl_f.compl_write = 1;
  608. dump_desc_dbg(ioat, desc);
  609. /* we leave the channel locked to ensure in order submission */
  610. return &desc->txd;
  611. }
  612. /**
  613. * ioat2_free_chan_resources - release all the descriptors
  614. * @chan: the channel to be cleaned
  615. */
  616. void ioat2_free_chan_resources(struct dma_chan *c)
  617. {
  618. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  619. struct ioat_chan_common *chan = &ioat->base;
  620. struct ioatdma_device *device = chan->device;
  621. struct ioat_ring_ent *desc;
  622. const u16 total_descs = 1 << ioat->alloc_order;
  623. int descs;
  624. int i;
  625. /* Before freeing channel resources first check
  626. * if they have been previously allocated for this channel.
  627. */
  628. if (!ioat->ring)
  629. return;
  630. tasklet_disable(&chan->cleanup_task);
  631. del_timer_sync(&chan->timer);
  632. device->cleanup_tasklet((unsigned long) ioat);
  633. /* Delay 100ms after reset to allow internal DMA logic to quiesce
  634. * before removing DMA descriptor resources.
  635. */
  636. writeb(IOAT_CHANCMD_RESET,
  637. chan->reg_base + IOAT_CHANCMD_OFFSET(chan->device->version));
  638. mdelay(100);
  639. spin_lock_bh(&ioat->ring_lock);
  640. descs = ioat2_ring_space(ioat);
  641. dev_dbg(to_dev(chan), "freeing %d idle descriptors\n", descs);
  642. for (i = 0; i < descs; i++) {
  643. desc = ioat2_get_ring_ent(ioat, ioat->head + i);
  644. ioat2_free_ring_ent(desc, c);
  645. }
  646. if (descs < total_descs)
  647. dev_err(to_dev(chan), "Freeing %d in use descriptors!\n",
  648. total_descs - descs);
  649. for (i = 0; i < total_descs - descs; i++) {
  650. desc = ioat2_get_ring_ent(ioat, ioat->tail + i);
  651. dump_desc_dbg(ioat, desc);
  652. ioat2_free_ring_ent(desc, c);
  653. }
  654. kfree(ioat->ring);
  655. ioat->ring = NULL;
  656. ioat->alloc_order = 0;
  657. pci_pool_free(device->completion_pool, chan->completion,
  658. chan->completion_dma);
  659. spin_unlock_bh(&ioat->ring_lock);
  660. chan->last_completion = 0;
  661. chan->completion_dma = 0;
  662. ioat->pending = 0;
  663. ioat->dmacount = 0;
  664. }
  665. enum dma_status
  666. ioat2_is_complete(struct dma_chan *c, dma_cookie_t cookie,
  667. dma_cookie_t *done, dma_cookie_t *used)
  668. {
  669. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  670. struct ioatdma_device *device = ioat->base.device;
  671. if (ioat_is_complete(c, cookie, done, used) == DMA_SUCCESS)
  672. return DMA_SUCCESS;
  673. device->cleanup_tasklet((unsigned long) ioat);
  674. return ioat_is_complete(c, cookie, done, used);
  675. }
  676. static ssize_t ring_size_show(struct dma_chan *c, char *page)
  677. {
  678. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  679. return sprintf(page, "%d\n", (1 << ioat->alloc_order) & ~1);
  680. }
  681. static struct ioat_sysfs_entry ring_size_attr = __ATTR_RO(ring_size);
  682. static ssize_t ring_active_show(struct dma_chan *c, char *page)
  683. {
  684. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  685. /* ...taken outside the lock, no need to be precise */
  686. return sprintf(page, "%d\n", ioat2_ring_active(ioat));
  687. }
  688. static struct ioat_sysfs_entry ring_active_attr = __ATTR_RO(ring_active);
  689. static struct attribute *ioat2_attrs[] = {
  690. &ring_size_attr.attr,
  691. &ring_active_attr.attr,
  692. &ioat_cap_attr.attr,
  693. &ioat_version_attr.attr,
  694. NULL,
  695. };
  696. struct kobj_type ioat2_ktype = {
  697. .sysfs_ops = &ioat_sysfs_ops,
  698. .default_attrs = ioat2_attrs,
  699. };
  700. int __devinit ioat2_dma_probe(struct ioatdma_device *device, int dca)
  701. {
  702. struct pci_dev *pdev = device->pdev;
  703. struct dma_device *dma;
  704. struct dma_chan *c;
  705. struct ioat_chan_common *chan;
  706. int err;
  707. device->enumerate_channels = ioat2_enumerate_channels;
  708. device->cleanup_tasklet = ioat2_cleanup_tasklet;
  709. device->timer_fn = ioat2_timer_event;
  710. dma = &device->common;
  711. dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy_lock;
  712. dma->device_issue_pending = ioat2_issue_pending;
  713. dma->device_alloc_chan_resources = ioat2_alloc_chan_resources;
  714. dma->device_free_chan_resources = ioat2_free_chan_resources;
  715. dma->device_is_tx_complete = ioat2_is_complete;
  716. err = ioat_probe(device);
  717. if (err)
  718. return err;
  719. ioat_set_tcp_copy_break(2048);
  720. list_for_each_entry(c, &dma->channels, device_node) {
  721. chan = to_chan_common(c);
  722. writel(IOAT_DCACTRL_CMPL_WRITE_ENABLE | IOAT_DMA_DCA_ANY_CPU,
  723. chan->reg_base + IOAT_DCACTRL_OFFSET);
  724. }
  725. err = ioat_register(device);
  726. if (err)
  727. return err;
  728. ioat_kobject_add(device, &ioat2_ktype);
  729. if (dca)
  730. device->dca = ioat2_dca_init(pdev, device->reg_base);
  731. return err;
  732. }