dma_v2.c 23 KB

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