dma_v2.c 24 KB

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