dma_v2.c 25 KB

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