blob: b5ae56c211e6edfd6b1f821f00a872381b309e58 [file] [log] [blame]
Dan Williams5cbafa62009-08-26 13:01:44 -07001/*
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/*
24 * This driver supports an Intel I/OAT DMA engine (versions >= 2), which
25 * does asynchronous data movement and checksumming operations.
26 */
27
28#include <linux/init.h>
29#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Dan Williams5cbafa62009-08-26 13:01:44 -070031#include <linux/pci.h>
32#include <linux/interrupt.h>
33#include <linux/dmaengine.h>
34#include <linux/delay.h>
35#include <linux/dma-mapping.h>
36#include <linux/workqueue.h>
37#include <linux/i7300_idle.h>
38#include "dma.h"
39#include "dma_v2.h"
40#include "registers.h"
41#include "hw.h"
42
Dan Williamsbf40a682009-09-08 17:42:55 -070043int ioat_ring_alloc_order = 8;
Dan Williams5cbafa62009-08-26 13:01:44 -070044module_param(ioat_ring_alloc_order, int, 0644);
45MODULE_PARM_DESC(ioat_ring_alloc_order,
Dan Williams376ec372009-09-16 15:16:50 -070046 "ioat2+: allocate 2^n descriptors per channel"
47 " (default: 8 max: 16)");
Dan Williamsa3092182009-09-08 12:02:01 -070048static int ioat_ring_max_alloc_order = IOAT_MAX_ORDER;
49module_param(ioat_ring_max_alloc_order, int, 0644);
50MODULE_PARM_DESC(ioat_ring_max_alloc_order,
Dan Williams376ec372009-09-16 15:16:50 -070051 "ioat2+: upper limit for ring size (default: 16)");
Dan Williams5cbafa62009-08-26 13:01:44 -070052
Dan Williamsb094ad32009-09-08 17:42:57 -070053void __ioat2_issue_pending(struct ioat2_dma_chan *ioat)
Dan Williams5cbafa62009-08-26 13:01:44 -070054{
Dan Williams281befa2010-03-03 11:47:43 -070055 struct ioat_chan_common *chan = &ioat->base;
Dan Williams5cbafa62009-08-26 13:01:44 -070056
Dan Williams376ec372009-09-16 15:16:50 -070057 ioat->dmacount += ioat2_ring_pending(ioat);
Dan Williams5cbafa62009-08-26 13:01:44 -070058 ioat->issued = ioat->head;
59 /* make descriptor updates globally visible before notifying channel */
60 wmb();
Dan Williams281befa2010-03-03 11:47:43 -070061 writew(ioat->dmacount, chan->reg_base + IOAT_CHAN_DMACOUNT_OFFSET);
62 dev_dbg(to_dev(chan),
Dan Williams6df91832009-09-08 12:00:55 -070063 "%s: head: %#x tail: %#x issued: %#x count: %#x\n",
64 __func__, ioat->head, ioat->tail, ioat->issued, ioat->dmacount);
Dan Williams5cbafa62009-08-26 13:01:44 -070065}
66
Dan Williams281befa2010-03-03 11:47:43 -070067void ioat2_issue_pending(struct dma_chan *c)
Dan Williams5cbafa62009-08-26 13:01:44 -070068{
Dan Williams281befa2010-03-03 11:47:43 -070069 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
Dan Williams5cbafa62009-08-26 13:01:44 -070070
Dan Williams281befa2010-03-03 11:47:43 -070071 if (ioat2_ring_pending(ioat)) {
72 spin_lock_bh(&ioat->ring_lock);
Dan Williams5cbafa62009-08-26 13:01:44 -070073 __ioat2_issue_pending(ioat);
Dan Williams281befa2010-03-03 11:47:43 -070074 spin_unlock_bh(&ioat->ring_lock);
75 }
Dan Williams5cbafa62009-08-26 13:01:44 -070076}
77
78/**
79 * ioat2_update_pending - log pending descriptors
80 * @ioat: ioat2+ channel
81 *
Dan Williams281befa2010-03-03 11:47:43 -070082 * Check if the number of unsubmitted descriptors has exceeded the
83 * watermark. Called with ring_lock held
Dan Williams5cbafa62009-08-26 13:01:44 -070084 */
85static void ioat2_update_pending(struct ioat2_dma_chan *ioat)
86{
Dan Williams281befa2010-03-03 11:47:43 -070087 if (ioat2_ring_pending(ioat) > ioat_pending_level)
Dan Williams5cbafa62009-08-26 13:01:44 -070088 __ioat2_issue_pending(ioat);
Dan Williams5cbafa62009-08-26 13:01:44 -070089}
90
91static void __ioat2_start_null_desc(struct ioat2_dma_chan *ioat)
92{
Dan Williams5cbafa62009-08-26 13:01:44 -070093 struct ioat_ring_ent *desc;
94 struct ioat_dma_descriptor *hw;
95 int idx;
96
97 if (ioat2_ring_space(ioat) < 1) {
98 dev_err(to_dev(&ioat->base),
99 "Unable to start null desc - ring full\n");
100 return;
101 }
102
Dan Williams6df91832009-09-08 12:00:55 -0700103 dev_dbg(to_dev(&ioat->base), "%s: head: %#x tail: %#x issued: %#x\n",
104 __func__, ioat->head, ioat->tail, ioat->issued);
Dan Williams5cbafa62009-08-26 13:01:44 -0700105 idx = ioat2_desc_alloc(ioat, 1);
106 desc = ioat2_get_ring_ent(ioat, idx);
107
108 hw = desc->hw;
109 hw->ctl = 0;
110 hw->ctl_f.null = 1;
111 hw->ctl_f.int_en = 1;
112 hw->ctl_f.compl_write = 1;
113 /* set size to non-zero value (channel returns error when size is 0) */
114 hw->size = NULL_DESC_BUFFER_SIZE;
115 hw->src_addr = 0;
116 hw->dst_addr = 0;
117 async_tx_ack(&desc->txd);
Dan Williams09c8a5b2009-09-08 12:01:49 -0700118 ioat2_set_chainaddr(ioat, desc->txd.phys);
Dan Williams6df91832009-09-08 12:00:55 -0700119 dump_desc_dbg(ioat, desc);
Dan Williams5cbafa62009-08-26 13:01:44 -0700120 __ioat2_issue_pending(ioat);
121}
122
123static void ioat2_start_null_desc(struct ioat2_dma_chan *ioat)
124{
125 spin_lock_bh(&ioat->ring_lock);
126 __ioat2_start_null_desc(ioat);
127 spin_unlock_bh(&ioat->ring_lock);
128}
129
Dan Williams09c8a5b2009-09-08 12:01:49 -0700130static void __cleanup(struct ioat2_dma_chan *ioat, unsigned long phys_complete)
Dan Williams5cbafa62009-08-26 13:01:44 -0700131{
132 struct ioat_chan_common *chan = &ioat->base;
Dan Williams09c8a5b2009-09-08 12:01:49 -0700133 struct dma_async_tx_descriptor *tx;
Dan Williams5cbafa62009-08-26 13:01:44 -0700134 struct ioat_ring_ent *desc;
135 bool seen_current = false;
136 u16 active;
137 int i;
Dan Williams5cbafa62009-08-26 13:01:44 -0700138
Dan Williams6df91832009-09-08 12:00:55 -0700139 dev_dbg(to_dev(chan), "%s: head: %#x tail: %#x issued: %#x\n",
140 __func__, ioat->head, ioat->tail, ioat->issued);
141
Dan Williams5cbafa62009-08-26 13:01:44 -0700142 active = ioat2_ring_active(ioat);
143 for (i = 0; i < active && !seen_current; i++) {
144 prefetch(ioat2_get_ring_ent(ioat, ioat->tail + i + 1));
145 desc = ioat2_get_ring_ent(ioat, ioat->tail + i);
146 tx = &desc->txd;
Dan Williams6df91832009-09-08 12:00:55 -0700147 dump_desc_dbg(ioat, desc);
Dan Williams5cbafa62009-08-26 13:01:44 -0700148 if (tx->cookie) {
149 ioat_dma_unmap(chan, tx->flags, desc->len, desc->hw);
150 chan->completed_cookie = tx->cookie;
151 tx->cookie = 0;
152 if (tx->callback) {
153 tx->callback(tx->callback_param);
154 tx->callback = NULL;
155 }
156 }
157
158 if (tx->phys == phys_complete)
159 seen_current = true;
160 }
161 ioat->tail += i;
Dan Williamsaa75db02010-03-03 21:21:10 -0700162 BUG_ON(active && !seen_current); /* no active descs have written a completion? */
Dan Williams5cbafa62009-08-26 13:01:44 -0700163
164 chan->last_completion = phys_complete;
Dan Williams09c8a5b2009-09-08 12:01:49 -0700165 if (ioat->head == ioat->tail) {
166 dev_dbg(to_dev(chan), "%s: cancel completion timeout\n",
167 __func__);
168 clear_bit(IOAT_COMPLETION_PENDING, &chan->state);
Dan Williamsa3092182009-09-08 12:02:01 -0700169 mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
Dan Williams09c8a5b2009-09-08 12:01:49 -0700170 }
171}
Dan Williams5cbafa62009-08-26 13:01:44 -0700172
Dan Williams09c8a5b2009-09-08 12:01:49 -0700173/**
174 * ioat2_cleanup - clean finished descriptors (advance tail pointer)
175 * @chan: ioat channel to be cleaned up
176 */
177static void ioat2_cleanup(struct ioat2_dma_chan *ioat)
178{
179 struct ioat_chan_common *chan = &ioat->base;
180 unsigned long phys_complete;
181
182 prefetch(chan->completion);
183
184 if (!spin_trylock_bh(&chan->cleanup_lock))
185 return;
186
187 if (!ioat_cleanup_preamble(chan, &phys_complete)) {
188 spin_unlock_bh(&chan->cleanup_lock);
189 return;
190 }
191
192 if (!spin_trylock_bh(&ioat->ring_lock)) {
193 spin_unlock_bh(&chan->cleanup_lock);
194 return;
195 }
196
197 __cleanup(ioat, phys_complete);
198
199 spin_unlock_bh(&ioat->ring_lock);
Dan Williams5cbafa62009-08-26 13:01:44 -0700200 spin_unlock_bh(&chan->cleanup_lock);
201}
202
Dan Williamsaa4d72a2010-03-03 21:21:13 -0700203void ioat2_cleanup_event(unsigned long data)
Dan Williams5cbafa62009-08-26 13:01:44 -0700204{
Dan Williamsaa4d72a2010-03-03 21:21:13 -0700205 struct ioat2_dma_chan *ioat = to_ioat2_chan((void *) data);
Dan Williams5cbafa62009-08-26 13:01:44 -0700206
207 ioat2_cleanup(ioat);
Dan Williamsf6ab95b2009-09-08 12:01:21 -0700208 writew(IOAT_CHANCTRL_RUN, ioat->base.reg_base + IOAT_CHANCTRL_OFFSET);
Dan Williams5cbafa62009-08-26 13:01:44 -0700209}
210
Dan Williamsbf40a682009-09-08 17:42:55 -0700211void __ioat2_restart_chan(struct ioat2_dma_chan *ioat)
Dan Williams09c8a5b2009-09-08 12:01:49 -0700212{
213 struct ioat_chan_common *chan = &ioat->base;
214
215 /* set the tail to be re-issued */
216 ioat->issued = ioat->tail;
217 ioat->dmacount = 0;
218 set_bit(IOAT_COMPLETION_PENDING, &chan->state);
219 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
220
221 dev_dbg(to_dev(chan),
222 "%s: head: %#x tail: %#x issued: %#x count: %#x\n",
223 __func__, ioat->head, ioat->tail, ioat->issued, ioat->dmacount);
224
225 if (ioat2_ring_pending(ioat)) {
226 struct ioat_ring_ent *desc;
227
228 desc = ioat2_get_ring_ent(ioat, ioat->tail);
229 ioat2_set_chainaddr(ioat, desc->txd.phys);
230 __ioat2_issue_pending(ioat);
231 } else
232 __ioat2_start_null_desc(ioat);
233}
234
Dan Williamsa6d52d72009-12-19 15:36:02 -0700235int ioat2_quiesce(struct ioat_chan_common *chan, unsigned long tmo)
Dan Williams09c8a5b2009-09-08 12:01:49 -0700236{
Dan Williamsa6d52d72009-12-19 15:36:02 -0700237 unsigned long end = jiffies + tmo;
238 int err = 0;
Dan Williams09c8a5b2009-09-08 12:01:49 -0700239 u32 status;
240
241 status = ioat_chansts(chan);
242 if (is_ioat_active(status) || is_ioat_idle(status))
243 ioat_suspend(chan);
244 while (is_ioat_active(status) || is_ioat_idle(status)) {
Dan Williams7e55a702010-01-13 13:33:12 -0700245 if (tmo && time_after(jiffies, end)) {
Dan Williamsa6d52d72009-12-19 15:36:02 -0700246 err = -ETIMEDOUT;
247 break;
248 }
Dan Williams09c8a5b2009-09-08 12:01:49 -0700249 status = ioat_chansts(chan);
250 cpu_relax();
251 }
252
Dan Williamsa6d52d72009-12-19 15:36:02 -0700253 return err;
254}
255
256int ioat2_reset_sync(struct ioat_chan_common *chan, unsigned long tmo)
257{
258 unsigned long end = jiffies + tmo;
259 int err = 0;
260
261 ioat_reset(chan);
262 while (ioat_reset_pending(chan)) {
263 if (end && time_after(jiffies, end)) {
264 err = -ETIMEDOUT;
265 break;
266 }
267 cpu_relax();
268 }
269
270 return err;
271}
272
273static void ioat2_restart_channel(struct ioat2_dma_chan *ioat)
274{
275 struct ioat_chan_common *chan = &ioat->base;
276 unsigned long phys_complete;
277
278 ioat2_quiesce(chan, 0);
Dan Williams09c8a5b2009-09-08 12:01:49 -0700279 if (ioat_cleanup_preamble(chan, &phys_complete))
280 __cleanup(ioat, phys_complete);
281
Dan Williamsbf40a682009-09-08 17:42:55 -0700282 __ioat2_restart_chan(ioat);
Dan Williams09c8a5b2009-09-08 12:01:49 -0700283}
284
Dan Williamse3232712009-09-08 17:43:02 -0700285void ioat2_timer_event(unsigned long data)
Dan Williams09c8a5b2009-09-08 12:01:49 -0700286{
Dan Williamsaa4d72a2010-03-03 21:21:13 -0700287 struct ioat2_dma_chan *ioat = to_ioat2_chan((void *) data);
Dan Williams09c8a5b2009-09-08 12:01:49 -0700288 struct ioat_chan_common *chan = &ioat->base;
289
290 spin_lock_bh(&chan->cleanup_lock);
291 if (test_bit(IOAT_COMPLETION_PENDING, &chan->state)) {
292 unsigned long phys_complete;
293 u64 status;
294
295 spin_lock_bh(&ioat->ring_lock);
296 status = ioat_chansts(chan);
297
298 /* when halted due to errors check for channel
299 * programming errors before advancing the completion state
300 */
301 if (is_ioat_halted(status)) {
302 u32 chanerr;
303
304 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
Dan Williamsb57014d2009-11-19 17:10:07 -0700305 dev_err(to_dev(chan), "%s: Channel halted (%x)\n",
306 __func__, chanerr);
Dan Williams09c8a5b2009-09-08 12:01:49 -0700307 BUG_ON(is_ioat_bug(chanerr));
308 }
309
310 /* if we haven't made progress and we have already
311 * acknowledged a pending completion once, then be more
312 * forceful with a restart
313 */
314 if (ioat_cleanup_preamble(chan, &phys_complete))
315 __cleanup(ioat, phys_complete);
316 else if (test_bit(IOAT_COMPLETION_ACK, &chan->state))
317 ioat2_restart_channel(ioat);
318 else {
319 set_bit(IOAT_COMPLETION_ACK, &chan->state);
320 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
321 }
322 spin_unlock_bh(&ioat->ring_lock);
Dan Williamsa3092182009-09-08 12:02:01 -0700323 } else {
324 u16 active;
325
326 /* if the ring is idle, empty, and oversized try to step
327 * down the size
328 */
329 spin_lock_bh(&ioat->ring_lock);
330 active = ioat2_ring_active(ioat);
331 if (active == 0 && ioat->alloc_order > ioat_get_alloc_order())
332 reshape_ring(ioat, ioat->alloc_order-1);
333 spin_unlock_bh(&ioat->ring_lock);
334
335 /* keep shrinking until we get back to our minimum
336 * default size
337 */
338 if (ioat->alloc_order > ioat_get_alloc_order())
339 mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
Dan Williams09c8a5b2009-09-08 12:01:49 -0700340 }
341 spin_unlock_bh(&chan->cleanup_lock);
342}
343
Dan Williamsa6d52d72009-12-19 15:36:02 -0700344static int ioat2_reset_hw(struct ioat_chan_common *chan)
345{
346 /* throw away whatever the channel was doing and get it initialized */
347 u32 chanerr;
348
349 ioat2_quiesce(chan, msecs_to_jiffies(100));
350
351 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
352 writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
353
354 return ioat2_reset_sync(chan, msecs_to_jiffies(200));
355}
356
Dan Williams5cbafa62009-08-26 13:01:44 -0700357/**
358 * ioat2_enumerate_channels - find and initialize the device's channels
359 * @device: the device to be enumerated
360 */
Dan Williamsbf40a682009-09-08 17:42:55 -0700361int ioat2_enumerate_channels(struct ioatdma_device *device)
Dan Williams5cbafa62009-08-26 13:01:44 -0700362{
363 struct ioat2_dma_chan *ioat;
364 struct device *dev = &device->pdev->dev;
365 struct dma_device *dma = &device->common;
366 u8 xfercap_log;
367 int i;
368
369 INIT_LIST_HEAD(&dma->channels);
370 dma->chancnt = readb(device->reg_base + IOAT_CHANCNT_OFFSET);
Dan Williamsbb320782009-09-08 12:01:14 -0700371 dma->chancnt &= 0x1f; /* bits [4:0] valid */
372 if (dma->chancnt > ARRAY_SIZE(device->idx)) {
373 dev_warn(dev, "(%d) exceeds max supported channels (%zu)\n",
374 dma->chancnt, ARRAY_SIZE(device->idx));
375 dma->chancnt = ARRAY_SIZE(device->idx);
376 }
Dan Williams5cbafa62009-08-26 13:01:44 -0700377 xfercap_log = readb(device->reg_base + IOAT_XFERCAP_OFFSET);
Dan Williamsbb320782009-09-08 12:01:14 -0700378 xfercap_log &= 0x1f; /* bits [4:0] valid */
Dan Williams5cbafa62009-08-26 13:01:44 -0700379 if (xfercap_log == 0)
380 return 0;
Dan Williams6df91832009-09-08 12:00:55 -0700381 dev_dbg(dev, "%s: xfercap = %d\n", __func__, 1 << xfercap_log);
Dan Williams5cbafa62009-08-26 13:01:44 -0700382
383 /* FIXME which i/oat version is i7300? */
384#ifdef CONFIG_I7300_IDLE_IOAT_CHANNEL
385 if (i7300_idle_platform_probe(NULL, NULL, 1) == 0)
386 dma->chancnt--;
387#endif
388 for (i = 0; i < dma->chancnt; i++) {
389 ioat = devm_kzalloc(dev, sizeof(*ioat), GFP_KERNEL);
390 if (!ioat)
391 break;
392
Dan Williamsaa4d72a2010-03-03 21:21:13 -0700393 ioat_init_channel(device, &ioat->base, i);
Dan Williams5cbafa62009-08-26 13:01:44 -0700394 ioat->xfercap_log = xfercap_log;
395 spin_lock_init(&ioat->ring_lock);
Dan Williamsa6d52d72009-12-19 15:36:02 -0700396 if (device->reset_hw(&ioat->base)) {
397 i = 0;
398 break;
399 }
Dan Williams5cbafa62009-08-26 13:01:44 -0700400 }
401 dma->chancnt = i;
402 return i;
403}
404
405static dma_cookie_t ioat2_tx_submit_unlock(struct dma_async_tx_descriptor *tx)
406{
407 struct dma_chan *c = tx->chan;
408 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
Dan Williams09c8a5b2009-09-08 12:01:49 -0700409 struct ioat_chan_common *chan = &ioat->base;
Dan Williams5cbafa62009-08-26 13:01:44 -0700410 dma_cookie_t cookie = c->cookie;
411
412 cookie++;
413 if (cookie < 0)
414 cookie = 1;
415 tx->cookie = cookie;
416 c->cookie = cookie;
Dan Williams6df91832009-09-08 12:00:55 -0700417 dev_dbg(to_dev(&ioat->base), "%s: cookie: %d\n", __func__, cookie);
418
Dan Williams09c8a5b2009-09-08 12:01:49 -0700419 if (!test_and_set_bit(IOAT_COMPLETION_PENDING, &chan->state))
420 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
Dan Williams5cbafa62009-08-26 13:01:44 -0700421 ioat2_update_pending(ioat);
422 spin_unlock_bh(&ioat->ring_lock);
423
424 return cookie;
425}
426
Dan Williamsa3092182009-09-08 12:02:01 -0700427static struct ioat_ring_ent *ioat2_alloc_ring_ent(struct dma_chan *chan, gfp_t flags)
Dan Williams5cbafa62009-08-26 13:01:44 -0700428{
429 struct ioat_dma_descriptor *hw;
430 struct ioat_ring_ent *desc;
431 struct ioatdma_device *dma;
432 dma_addr_t phys;
433
434 dma = to_ioatdma_device(chan->device);
Dan Williamsa3092182009-09-08 12:02:01 -0700435 hw = pci_pool_alloc(dma->dma_pool, flags, &phys);
Dan Williams5cbafa62009-08-26 13:01:44 -0700436 if (!hw)
437 return NULL;
438 memset(hw, 0, sizeof(*hw));
439
Dan Williams162b96e2009-09-08 17:53:04 -0700440 desc = kmem_cache_alloc(ioat2_cache, flags);
Dan Williams5cbafa62009-08-26 13:01:44 -0700441 if (!desc) {
442 pci_pool_free(dma->dma_pool, hw, phys);
443 return NULL;
444 }
Dan Williams162b96e2009-09-08 17:53:04 -0700445 memset(desc, 0, sizeof(*desc));
Dan Williams5cbafa62009-08-26 13:01:44 -0700446
447 dma_async_tx_descriptor_init(&desc->txd, chan);
448 desc->txd.tx_submit = ioat2_tx_submit_unlock;
449 desc->hw = hw;
450 desc->txd.phys = phys;
451 return desc;
452}
453
454static void ioat2_free_ring_ent(struct ioat_ring_ent *desc, struct dma_chan *chan)
455{
456 struct ioatdma_device *dma;
457
458 dma = to_ioatdma_device(chan->device);
459 pci_pool_free(dma->dma_pool, desc->hw, desc->txd.phys);
Dan Williams162b96e2009-09-08 17:53:04 -0700460 kmem_cache_free(ioat2_cache, desc);
Dan Williams5cbafa62009-08-26 13:01:44 -0700461}
462
Dan Williamsa3092182009-09-08 12:02:01 -0700463static struct ioat_ring_ent **ioat2_alloc_ring(struct dma_chan *c, int order, gfp_t flags)
464{
465 struct ioat_ring_ent **ring;
466 int descs = 1 << order;
467 int i;
468
469 if (order > ioat_get_max_alloc_order())
470 return NULL;
471
472 /* allocate the array to hold the software ring */
473 ring = kcalloc(descs, sizeof(*ring), flags);
474 if (!ring)
475 return NULL;
476 for (i = 0; i < descs; i++) {
477 ring[i] = ioat2_alloc_ring_ent(c, flags);
478 if (!ring[i]) {
479 while (i--)
480 ioat2_free_ring_ent(ring[i], c);
481 kfree(ring);
482 return NULL;
483 }
484 set_desc_id(ring[i], i);
485 }
486
487 /* link descs */
488 for (i = 0; i < descs-1; i++) {
489 struct ioat_ring_ent *next = ring[i+1];
490 struct ioat_dma_descriptor *hw = ring[i]->hw;
491
492 hw->next = next->txd.phys;
493 }
494 ring[i]->hw->next = ring[0]->txd.phys;
495
496 return ring;
497}
498
Dan Williams5cbafa62009-08-26 13:01:44 -0700499/* ioat2_alloc_chan_resources - allocate/initialize ioat2 descriptor ring
500 * @chan: channel to be initialized
501 */
Dan Williamsbf40a682009-09-08 17:42:55 -0700502int ioat2_alloc_chan_resources(struct dma_chan *c)
Dan Williams5cbafa62009-08-26 13:01:44 -0700503{
504 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
505 struct ioat_chan_common *chan = &ioat->base;
506 struct ioat_ring_ent **ring;
Dan Williamsa3092182009-09-08 12:02:01 -0700507 int order;
Dan Williams5cbafa62009-08-26 13:01:44 -0700508
509 /* have we already been set up? */
510 if (ioat->ring)
511 return 1 << ioat->alloc_order;
512
513 /* Setup register to interrupt and write completion status on error */
Dan Williamsf6ab95b2009-09-08 12:01:21 -0700514 writew(IOAT_CHANCTRL_RUN, chan->reg_base + IOAT_CHANCTRL_OFFSET);
Dan Williams5cbafa62009-08-26 13:01:44 -0700515
Dan Williams5cbafa62009-08-26 13:01:44 -0700516 /* allocate a completion writeback area */
517 /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700518 chan->completion = pci_pool_alloc(chan->device->completion_pool,
519 GFP_KERNEL, &chan->completion_dma);
520 if (!chan->completion)
Dan Williams5cbafa62009-08-26 13:01:44 -0700521 return -ENOMEM;
522
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700523 memset(chan->completion, 0, sizeof(*chan->completion));
524 writel(((u64) chan->completion_dma) & 0x00000000FFFFFFFF,
Dan Williams5cbafa62009-08-26 13:01:44 -0700525 chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700526 writel(((u64) chan->completion_dma) >> 32,
Dan Williams5cbafa62009-08-26 13:01:44 -0700527 chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
528
Dan Williamsa3092182009-09-08 12:02:01 -0700529 order = ioat_get_alloc_order();
530 ring = ioat2_alloc_ring(c, order, GFP_KERNEL);
Dan Williams5cbafa62009-08-26 13:01:44 -0700531 if (!ring)
532 return -ENOMEM;
Dan Williams5cbafa62009-08-26 13:01:44 -0700533
534 spin_lock_bh(&ioat->ring_lock);
535 ioat->ring = ring;
536 ioat->head = 0;
537 ioat->issued = 0;
538 ioat->tail = 0;
Dan Williamsa3092182009-09-08 12:02:01 -0700539 ioat->alloc_order = order;
Dan Williams5cbafa62009-08-26 13:01:44 -0700540 spin_unlock_bh(&ioat->ring_lock);
541
542 tasklet_enable(&chan->cleanup_task);
543 ioat2_start_null_desc(ioat);
544
Dan Williamsa3092182009-09-08 12:02:01 -0700545 return 1 << ioat->alloc_order;
546}
547
Dan Williamsbf40a682009-09-08 17:42:55 -0700548bool reshape_ring(struct ioat2_dma_chan *ioat, int order)
Dan Williamsa3092182009-09-08 12:02:01 -0700549{
550 /* reshape differs from normal ring allocation in that we want
551 * to allocate a new software ring while only
552 * extending/truncating the hardware ring
553 */
554 struct ioat_chan_common *chan = &ioat->base;
555 struct dma_chan *c = &chan->common;
556 const u16 curr_size = ioat2_ring_mask(ioat) + 1;
557 const u16 active = ioat2_ring_active(ioat);
558 const u16 new_size = 1 << order;
559 struct ioat_ring_ent **ring;
560 u16 i;
561
562 if (order > ioat_get_max_alloc_order())
563 return false;
564
565 /* double check that we have at least 1 free descriptor */
566 if (active == curr_size)
567 return false;
568
569 /* when shrinking, verify that we can hold the current active
570 * set in the new ring
571 */
572 if (active >= new_size)
573 return false;
574
575 /* allocate the array to hold the software ring */
576 ring = kcalloc(new_size, sizeof(*ring), GFP_NOWAIT);
577 if (!ring)
578 return false;
579
580 /* allocate/trim descriptors as needed */
581 if (new_size > curr_size) {
582 /* copy current descriptors to the new ring */
583 for (i = 0; i < curr_size; i++) {
584 u16 curr_idx = (ioat->tail+i) & (curr_size-1);
585 u16 new_idx = (ioat->tail+i) & (new_size-1);
586
587 ring[new_idx] = ioat->ring[curr_idx];
588 set_desc_id(ring[new_idx], new_idx);
589 }
590
591 /* add new descriptors to the ring */
592 for (i = curr_size; i < new_size; i++) {
593 u16 new_idx = (ioat->tail+i) & (new_size-1);
594
595 ring[new_idx] = ioat2_alloc_ring_ent(c, GFP_NOWAIT);
596 if (!ring[new_idx]) {
597 while (i--) {
598 u16 new_idx = (ioat->tail+i) & (new_size-1);
599
600 ioat2_free_ring_ent(ring[new_idx], c);
601 }
602 kfree(ring);
603 return false;
604 }
605 set_desc_id(ring[new_idx], new_idx);
606 }
607
608 /* hw link new descriptors */
609 for (i = curr_size-1; i < new_size; i++) {
610 u16 new_idx = (ioat->tail+i) & (new_size-1);
611 struct ioat_ring_ent *next = ring[(new_idx+1) & (new_size-1)];
612 struct ioat_dma_descriptor *hw = ring[new_idx]->hw;
613
614 hw->next = next->txd.phys;
615 }
616 } else {
617 struct ioat_dma_descriptor *hw;
618 struct ioat_ring_ent *next;
619
620 /* copy current descriptors to the new ring, dropping the
621 * removed descriptors
622 */
623 for (i = 0; i < new_size; i++) {
624 u16 curr_idx = (ioat->tail+i) & (curr_size-1);
625 u16 new_idx = (ioat->tail+i) & (new_size-1);
626
627 ring[new_idx] = ioat->ring[curr_idx];
628 set_desc_id(ring[new_idx], new_idx);
629 }
630
631 /* free deleted descriptors */
632 for (i = new_size; i < curr_size; i++) {
633 struct ioat_ring_ent *ent;
634
635 ent = ioat2_get_ring_ent(ioat, ioat->tail+i);
636 ioat2_free_ring_ent(ent, c);
637 }
638
639 /* fix up hardware ring */
640 hw = ring[(ioat->tail+new_size-1) & (new_size-1)]->hw;
641 next = ring[(ioat->tail+new_size) & (new_size-1)];
642 hw->next = next->txd.phys;
643 }
644
645 dev_dbg(to_dev(chan), "%s: allocated %d descriptors\n",
646 __func__, new_size);
647
648 kfree(ioat->ring);
649 ioat->ring = ring;
650 ioat->alloc_order = order;
651
652 return true;
Dan Williams5cbafa62009-08-26 13:01:44 -0700653}
654
655/**
656 * ioat2_alloc_and_lock - common descriptor alloc boilerplate for ioat2,3 ops
657 * @idx: gets starting descriptor index on successful allocation
658 * @ioat: ioat2,3 channel (ring) to operate on
659 * @num_descs: allocation length
660 */
Dan Williamsbf40a682009-09-08 17:42:55 -0700661int ioat2_alloc_and_lock(u16 *idx, struct ioat2_dma_chan *ioat, int num_descs)
Dan Williams5cbafa62009-08-26 13:01:44 -0700662{
663 struct ioat_chan_common *chan = &ioat->base;
664
665 spin_lock_bh(&ioat->ring_lock);
Dan Williamsa3092182009-09-08 12:02:01 -0700666 /* never allow the last descriptor to be consumed, we need at
667 * least one free at all times to allow for on-the-fly ring
668 * resizing.
669 */
670 while (unlikely(ioat2_ring_space(ioat) <= num_descs)) {
671 if (reshape_ring(ioat, ioat->alloc_order + 1) &&
672 ioat2_ring_space(ioat) > num_descs)
673 break;
674
Dan Williams5cbafa62009-08-26 13:01:44 -0700675 if (printk_ratelimit())
676 dev_dbg(to_dev(chan),
677 "%s: ring full! num_descs: %d (%x:%x:%x)\n",
678 __func__, num_descs, ioat->head, ioat->tail,
679 ioat->issued);
680 spin_unlock_bh(&ioat->ring_lock);
681
Dan Williams09c8a5b2009-09-08 12:01:49 -0700682 /* progress reclaim in the allocation failure case we
683 * may be called under bh_disabled so we need to trigger
684 * the timer event directly
685 */
686 spin_lock_bh(&chan->cleanup_lock);
687 if (jiffies > chan->timer.expires &&
688 timer_pending(&chan->timer)) {
Dan Williamsbf40a682009-09-08 17:42:55 -0700689 struct ioatdma_device *device = chan->device;
690
Dan Williams09c8a5b2009-09-08 12:01:49 -0700691 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
692 spin_unlock_bh(&chan->cleanup_lock);
Dan Williamsaa4d72a2010-03-03 21:21:13 -0700693 device->timer_fn((unsigned long) &chan->common);
Dan Williams09c8a5b2009-09-08 12:01:49 -0700694 } else
695 spin_unlock_bh(&chan->cleanup_lock);
Dan Williams5cbafa62009-08-26 13:01:44 -0700696 return -ENOMEM;
697 }
698
699 dev_dbg(to_dev(chan), "%s: num_descs: %d (%x:%x:%x)\n",
700 __func__, num_descs, ioat->head, ioat->tail, ioat->issued);
701
702 *idx = ioat2_desc_alloc(ioat, num_descs);
703 return 0; /* with ioat->ring_lock held */
704}
705
Dan Williamsbf40a682009-09-08 17:42:55 -0700706struct dma_async_tx_descriptor *
Dan Williams5cbafa62009-08-26 13:01:44 -0700707ioat2_dma_prep_memcpy_lock(struct dma_chan *c, dma_addr_t dma_dest,
708 dma_addr_t dma_src, size_t len, unsigned long flags)
709{
710 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
711 struct ioat_dma_descriptor *hw;
712 struct ioat_ring_ent *desc;
713 dma_addr_t dst = dma_dest;
714 dma_addr_t src = dma_src;
715 size_t total_len = len;
716 int num_descs;
717 u16 idx;
718 int i;
719
720 num_descs = ioat2_xferlen_to_descs(ioat, len);
721 if (likely(num_descs) &&
722 ioat2_alloc_and_lock(&idx, ioat, num_descs) == 0)
723 /* pass */;
724 else
725 return NULL;
Andrew Mortonf477f5b2009-09-21 09:17:58 -0700726 i = 0;
727 do {
Dan Williams5cbafa62009-08-26 13:01:44 -0700728 size_t copy = min_t(size_t, len, 1 << ioat->xfercap_log);
729
730 desc = ioat2_get_ring_ent(ioat, idx + i);
731 hw = desc->hw;
732
733 hw->size = copy;
734 hw->ctl = 0;
735 hw->src_addr = src;
736 hw->dst_addr = dst;
737
738 len -= copy;
739 dst += copy;
740 src += copy;
Dan Williams6df91832009-09-08 12:00:55 -0700741 dump_desc_dbg(ioat, desc);
Andrew Mortonf477f5b2009-09-21 09:17:58 -0700742 } while (++i < num_descs);
Dan Williams5cbafa62009-08-26 13:01:44 -0700743
744 desc->txd.flags = flags;
745 desc->len = total_len;
746 hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
Dan Williams128f2d52009-09-08 17:42:53 -0700747 hw->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
Dan Williams5cbafa62009-08-26 13:01:44 -0700748 hw->ctl_f.compl_write = 1;
Dan Williams6df91832009-09-08 12:00:55 -0700749 dump_desc_dbg(ioat, desc);
Dan Williams5cbafa62009-08-26 13:01:44 -0700750 /* we leave the channel locked to ensure in order submission */
751
752 return &desc->txd;
753}
754
755/**
756 * ioat2_free_chan_resources - release all the descriptors
757 * @chan: the channel to be cleaned
758 */
Dan Williamsbf40a682009-09-08 17:42:55 -0700759void ioat2_free_chan_resources(struct dma_chan *c)
Dan Williams5cbafa62009-08-26 13:01:44 -0700760{
761 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
762 struct ioat_chan_common *chan = &ioat->base;
Dan Williamsbf40a682009-09-08 17:42:55 -0700763 struct ioatdma_device *device = chan->device;
Dan Williams5cbafa62009-08-26 13:01:44 -0700764 struct ioat_ring_ent *desc;
765 const u16 total_descs = 1 << ioat->alloc_order;
766 int descs;
767 int i;
768
769 /* Before freeing channel resources first check
770 * if they have been previously allocated for this channel.
771 */
772 if (!ioat->ring)
773 return;
774
775 tasklet_disable(&chan->cleanup_task);
Dan Williams09c8a5b2009-09-08 12:01:49 -0700776 del_timer_sync(&chan->timer);
Dan Williamsaa4d72a2010-03-03 21:21:13 -0700777 device->cleanup_fn((unsigned long) c);
Dan Williamsa6d52d72009-12-19 15:36:02 -0700778 device->reset_hw(chan);
Dan Williams5cbafa62009-08-26 13:01:44 -0700779
780 spin_lock_bh(&ioat->ring_lock);
781 descs = ioat2_ring_space(ioat);
Dan Williams6df91832009-09-08 12:00:55 -0700782 dev_dbg(to_dev(chan), "freeing %d idle descriptors\n", descs);
Dan Williams5cbafa62009-08-26 13:01:44 -0700783 for (i = 0; i < descs; i++) {
784 desc = ioat2_get_ring_ent(ioat, ioat->head + i);
785 ioat2_free_ring_ent(desc, c);
786 }
787
788 if (descs < total_descs)
789 dev_err(to_dev(chan), "Freeing %d in use descriptors!\n",
790 total_descs - descs);
791
792 for (i = 0; i < total_descs - descs; i++) {
793 desc = ioat2_get_ring_ent(ioat, ioat->tail + i);
Dan Williams6df91832009-09-08 12:00:55 -0700794 dump_desc_dbg(ioat, desc);
Dan Williams5cbafa62009-08-26 13:01:44 -0700795 ioat2_free_ring_ent(desc, c);
796 }
797
798 kfree(ioat->ring);
799 ioat->ring = NULL;
800 ioat->alloc_order = 0;
Dan Williamsbf40a682009-09-08 17:42:55 -0700801 pci_pool_free(device->completion_pool, chan->completion,
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700802 chan->completion_dma);
Dan Williams5cbafa62009-08-26 13:01:44 -0700803 spin_unlock_bh(&ioat->ring_lock);
804
805 chan->last_completion = 0;
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700806 chan->completion_dma = 0;
Dan Williams5cbafa62009-08-26 13:01:44 -0700807 ioat->dmacount = 0;
Dan Williams5cbafa62009-08-26 13:01:44 -0700808}
809
Dan Williams5669e312009-09-08 17:42:56 -0700810static ssize_t ring_size_show(struct dma_chan *c, char *page)
811{
812 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
813
814 return sprintf(page, "%d\n", (1 << ioat->alloc_order) & ~1);
815}
816static struct ioat_sysfs_entry ring_size_attr = __ATTR_RO(ring_size);
817
818static ssize_t ring_active_show(struct dma_chan *c, char *page)
819{
820 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
821
822 /* ...taken outside the lock, no need to be precise */
823 return sprintf(page, "%d\n", ioat2_ring_active(ioat));
824}
825static struct ioat_sysfs_entry ring_active_attr = __ATTR_RO(ring_active);
826
827static struct attribute *ioat2_attrs[] = {
828 &ring_size_attr.attr,
829 &ring_active_attr.attr,
830 &ioat_cap_attr.attr,
831 &ioat_version_attr.attr,
832 NULL,
833};
834
835struct kobj_type ioat2_ktype = {
836 .sysfs_ops = &ioat_sysfs_ops,
837 .default_attrs = ioat2_attrs,
838};
839
Dan Williams345d8522009-09-08 12:01:30 -0700840int __devinit ioat2_dma_probe(struct ioatdma_device *device, int dca)
Dan Williams5cbafa62009-08-26 13:01:44 -0700841{
842 struct pci_dev *pdev = device->pdev;
843 struct dma_device *dma;
844 struct dma_chan *c;
845 struct ioat_chan_common *chan;
846 int err;
847
848 device->enumerate_channels = ioat2_enumerate_channels;
Dan Williamsa6d52d72009-12-19 15:36:02 -0700849 device->reset_hw = ioat2_reset_hw;
Dan Williamsaa4d72a2010-03-03 21:21:13 -0700850 device->cleanup_fn = ioat2_cleanup_event;
Dan Williamsbf40a682009-09-08 17:42:55 -0700851 device->timer_fn = ioat2_timer_event;
Dan Williams9de6fc72009-09-08 17:42:58 -0700852 device->self_test = ioat_dma_self_test;
Dan Williams5cbafa62009-08-26 13:01:44 -0700853 dma = &device->common;
854 dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy_lock;
855 dma->device_issue_pending = ioat2_issue_pending;
856 dma->device_alloc_chan_resources = ioat2_alloc_chan_resources;
857 dma->device_free_chan_resources = ioat2_free_chan_resources;
Dan Williamsaa4d72a2010-03-03 21:21:13 -0700858 dma->device_is_tx_complete = ioat_is_dma_complete;
Dan Williams5cbafa62009-08-26 13:01:44 -0700859
860 err = ioat_probe(device);
861 if (err)
862 return err;
863 ioat_set_tcp_copy_break(2048);
864
865 list_for_each_entry(c, &dma->channels, device_node) {
866 chan = to_chan_common(c);
867 writel(IOAT_DCACTRL_CMPL_WRITE_ENABLE | IOAT_DMA_DCA_ANY_CPU,
868 chan->reg_base + IOAT_DCACTRL_OFFSET);
869 }
870
871 err = ioat_register(device);
872 if (err)
873 return err;
Dan Williams5669e312009-09-08 17:42:56 -0700874
875 ioat_kobject_add(device, &ioat2_ktype);
876
Dan Williams5cbafa62009-08-26 13:01:44 -0700877 if (dca)
878 device->dca = ioat2_dca_init(pdev, device->reg_base);
879
Dan Williams5cbafa62009-08-26 13:01:44 -0700880 return err;
881}