dma_v2.c 24 KB

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