3cf2639fb06ae422029785a85bb4ebff4bc9a073
[cascardo/linux.git] / drivers / dma / ioat / dma.c
1 /*
2  * Intel I/OAT DMA Linux driver
3  * Copyright(c) 2004 - 2015 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  * The full GNU General Public License is included in this distribution in
15  * the file called "COPYING".
16  *
17  */
18
19 /*
20  * This driver supports an Intel I/OAT DMA engine, which does asynchronous
21  * copy operations.
22  */
23
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/pci.h>
28 #include <linux/interrupt.h>
29 #include <linux/dmaengine.h>
30 #include <linux/delay.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/workqueue.h>
33 #include <linux/prefetch.h>
34 #include "dma.h"
35 #include "registers.h"
36 #include "hw.h"
37
38 #include "../dmaengine.h"
39
40 int ioat_pending_level = 4;
41 module_param(ioat_pending_level, int, 0644);
42 MODULE_PARM_DESC(ioat_pending_level,
43                  "high-water mark for pushing ioat descriptors (default: 4)");
44
45 /**
46  * ioat_dma_do_interrupt - handler used for single vector interrupt mode
47  * @irq: interrupt id
48  * @data: interrupt data
49  */
50 static irqreturn_t ioat_dma_do_interrupt(int irq, void *data)
51 {
52         struct ioatdma_device *instance = data;
53         struct ioatdma_chan *ioat_chan;
54         unsigned long attnstatus;
55         int bit;
56         u8 intrctrl;
57
58         intrctrl = readb(instance->reg_base + IOAT_INTRCTRL_OFFSET);
59
60         if (!(intrctrl & IOAT_INTRCTRL_MASTER_INT_EN))
61                 return IRQ_NONE;
62
63         if (!(intrctrl & IOAT_INTRCTRL_INT_STATUS)) {
64                 writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
65                 return IRQ_NONE;
66         }
67
68         attnstatus = readl(instance->reg_base + IOAT_ATTNSTATUS_OFFSET);
69         for_each_set_bit(bit, &attnstatus, BITS_PER_LONG) {
70                 ioat_chan = ioat_chan_by_index(instance, bit);
71                 if (test_bit(IOAT_RUN, &ioat_chan->state))
72                         tasklet_schedule(&ioat_chan->cleanup_task);
73         }
74
75         writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
76         return IRQ_HANDLED;
77 }
78
79 /**
80  * ioat_dma_do_interrupt_msix - handler used for vector-per-channel interrupt mode
81  * @irq: interrupt id
82  * @data: interrupt data
83  */
84 static irqreturn_t ioat_dma_do_interrupt_msix(int irq, void *data)
85 {
86         struct ioatdma_chan *ioat_chan = data;
87
88         if (test_bit(IOAT_RUN, &ioat_chan->state))
89                 tasklet_schedule(&ioat_chan->cleanup_task);
90
91         return IRQ_HANDLED;
92 }
93
94 /* common channel initialization */
95 void
96 ioat_init_channel(struct ioatdma_device *ioat_dma,
97                   struct ioatdma_chan *ioat_chan, int idx)
98 {
99         struct dma_device *dma = &ioat_dma->dma_dev;
100         struct dma_chan *c = &ioat_chan->dma_chan;
101         unsigned long data = (unsigned long) c;
102
103         ioat_chan->ioat_dma = ioat_dma;
104         ioat_chan->reg_base = ioat_dma->reg_base + (0x80 * (idx + 1));
105         spin_lock_init(&ioat_chan->cleanup_lock);
106         ioat_chan->dma_chan.device = dma;
107         dma_cookie_init(&ioat_chan->dma_chan);
108         list_add_tail(&ioat_chan->dma_chan.device_node, &dma->channels);
109         ioat_dma->idx[idx] = ioat_chan;
110         init_timer(&ioat_chan->timer);
111         ioat_chan->timer.function = ioat_dma->timer_fn;
112         ioat_chan->timer.data = data;
113         tasklet_init(&ioat_chan->cleanup_task, ioat_dma->cleanup_fn, data);
114 }
115
116 void ioat_stop(struct ioatdma_chan *ioat_chan)
117 {
118         struct ioatdma_device *ioat_dma = ioat_chan->ioat_dma;
119         struct pci_dev *pdev = ioat_dma->pdev;
120         int chan_id = chan_num(ioat_chan);
121         struct msix_entry *msix;
122
123         /* 1/ stop irq from firing tasklets
124          * 2/ stop the tasklet from re-arming irqs
125          */
126         clear_bit(IOAT_RUN, &ioat_chan->state);
127
128         /* flush inflight interrupts */
129         switch (ioat_dma->irq_mode) {
130         case IOAT_MSIX:
131                 msix = &ioat_dma->msix_entries[chan_id];
132                 synchronize_irq(msix->vector);
133                 break;
134         case IOAT_MSI:
135         case IOAT_INTX:
136                 synchronize_irq(pdev->irq);
137                 break;
138         default:
139                 break;
140         }
141
142         /* flush inflight timers */
143         del_timer_sync(&ioat_chan->timer);
144
145         /* flush inflight tasklet runs */
146         tasklet_kill(&ioat_chan->cleanup_task);
147
148         /* final cleanup now that everything is quiesced and can't re-arm */
149         ioat_dma->cleanup_fn((unsigned long)&ioat_chan->dma_chan);
150 }
151
152 dma_addr_t ioat_get_current_completion(struct ioatdma_chan *ioat_chan)
153 {
154         dma_addr_t phys_complete;
155         u64 completion;
156
157         completion = *ioat_chan->completion;
158         phys_complete = ioat_chansts_to_addr(completion);
159
160         dev_dbg(to_dev(ioat_chan), "%s: phys_complete: %#llx\n", __func__,
161                 (unsigned long long) phys_complete);
162
163         if (is_ioat_halted(completion)) {
164                 u32 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
165
166                 dev_err(to_dev(ioat_chan), "Channel halted, chanerr = %x\n",
167                         chanerr);
168
169                 /* TODO do something to salvage the situation */
170         }
171
172         return phys_complete;
173 }
174
175 bool ioat_cleanup_preamble(struct ioatdma_chan *ioat_chan,
176                            dma_addr_t *phys_complete)
177 {
178         *phys_complete = ioat_get_current_completion(ioat_chan);
179         if (*phys_complete == ioat_chan->last_completion)
180                 return false;
181         clear_bit(IOAT_COMPLETION_ACK, &ioat_chan->state);
182         mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
183
184         return true;
185 }
186
187 enum dma_status
188 ioat_dma_tx_status(struct dma_chan *c, dma_cookie_t cookie,
189                    struct dma_tx_state *txstate)
190 {
191         struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
192         struct ioatdma_device *ioat_dma = ioat_chan->ioat_dma;
193         enum dma_status ret;
194
195         ret = dma_cookie_status(c, cookie, txstate);
196         if (ret == DMA_COMPLETE)
197                 return ret;
198
199         ioat_dma->cleanup_fn((unsigned long) c);
200
201         return dma_cookie_status(c, cookie, txstate);
202 }
203
204 /*
205  * Perform a IOAT transaction to verify the HW works.
206  */
207 #define IOAT_TEST_SIZE 2000
208
209 static void ioat_dma_test_callback(void *dma_async_param)
210 {
211         struct completion *cmp = dma_async_param;
212
213         complete(cmp);
214 }
215
216 /**
217  * ioat_dma_self_test - Perform a IOAT transaction to verify the HW works.
218  * @ioat_dma: dma device to be tested
219  */
220 int ioat_dma_self_test(struct ioatdma_device *ioat_dma)
221 {
222         int i;
223         u8 *src;
224         u8 *dest;
225         struct dma_device *dma = &ioat_dma->dma_dev;
226         struct device *dev = &ioat_dma->pdev->dev;
227         struct dma_chan *dma_chan;
228         struct dma_async_tx_descriptor *tx;
229         dma_addr_t dma_dest, dma_src;
230         dma_cookie_t cookie;
231         int err = 0;
232         struct completion cmp;
233         unsigned long tmo;
234         unsigned long flags;
235
236         src = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
237         if (!src)
238                 return -ENOMEM;
239         dest = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
240         if (!dest) {
241                 kfree(src);
242                 return -ENOMEM;
243         }
244
245         /* Fill in src buffer */
246         for (i = 0; i < IOAT_TEST_SIZE; i++)
247                 src[i] = (u8)i;
248
249         /* Start copy, using first DMA channel */
250         dma_chan = container_of(dma->channels.next, struct dma_chan,
251                                 device_node);
252         if (dma->device_alloc_chan_resources(dma_chan) < 1) {
253                 dev_err(dev, "selftest cannot allocate chan resource\n");
254                 err = -ENODEV;
255                 goto out;
256         }
257
258         dma_src = dma_map_single(dev, src, IOAT_TEST_SIZE, DMA_TO_DEVICE);
259         if (dma_mapping_error(dev, dma_src)) {
260                 dev_err(dev, "mapping src buffer failed\n");
261                 goto free_resources;
262         }
263         dma_dest = dma_map_single(dev, dest, IOAT_TEST_SIZE, DMA_FROM_DEVICE);
264         if (dma_mapping_error(dev, dma_dest)) {
265                 dev_err(dev, "mapping dest buffer failed\n");
266                 goto unmap_src;
267         }
268         flags = DMA_PREP_INTERRUPT;
269         tx = ioat_dma->dma_dev.device_prep_dma_memcpy(dma_chan, dma_dest,
270                                                       dma_src, IOAT_TEST_SIZE,
271                                                       flags);
272         if (!tx) {
273                 dev_err(dev, "Self-test prep failed, disabling\n");
274                 err = -ENODEV;
275                 goto unmap_dma;
276         }
277
278         async_tx_ack(tx);
279         init_completion(&cmp);
280         tx->callback = ioat_dma_test_callback;
281         tx->callback_param = &cmp;
282         cookie = tx->tx_submit(tx);
283         if (cookie < 0) {
284                 dev_err(dev, "Self-test setup failed, disabling\n");
285                 err = -ENODEV;
286                 goto unmap_dma;
287         }
288         dma->device_issue_pending(dma_chan);
289
290         tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
291
292         if (tmo == 0 ||
293             dma->device_tx_status(dma_chan, cookie, NULL)
294                                         != DMA_COMPLETE) {
295                 dev_err(dev, "Self-test copy timed out, disabling\n");
296                 err = -ENODEV;
297                 goto unmap_dma;
298         }
299         if (memcmp(src, dest, IOAT_TEST_SIZE)) {
300                 dev_err(dev, "Self-test copy failed compare, disabling\n");
301                 err = -ENODEV;
302                 goto free_resources;
303         }
304
305 unmap_dma:
306         dma_unmap_single(dev, dma_dest, IOAT_TEST_SIZE, DMA_FROM_DEVICE);
307 unmap_src:
308         dma_unmap_single(dev, dma_src, IOAT_TEST_SIZE, DMA_TO_DEVICE);
309 free_resources:
310         dma->device_free_chan_resources(dma_chan);
311 out:
312         kfree(src);
313         kfree(dest);
314         return err;
315 }
316
317 static char ioat_interrupt_style[32] = "msix";
318 module_param_string(ioat_interrupt_style, ioat_interrupt_style,
319                     sizeof(ioat_interrupt_style), 0644);
320 MODULE_PARM_DESC(ioat_interrupt_style,
321                  "set ioat interrupt style: msix (default), msi, intx");
322
323 /**
324  * ioat_dma_setup_interrupts - setup interrupt handler
325  * @ioat_dma: ioat dma device
326  */
327 int ioat_dma_setup_interrupts(struct ioatdma_device *ioat_dma)
328 {
329         struct ioatdma_chan *ioat_chan;
330         struct pci_dev *pdev = ioat_dma->pdev;
331         struct device *dev = &pdev->dev;
332         struct msix_entry *msix;
333         int i, j, msixcnt;
334         int err = -EINVAL;
335         u8 intrctrl = 0;
336
337         if (!strcmp(ioat_interrupt_style, "msix"))
338                 goto msix;
339         if (!strcmp(ioat_interrupt_style, "msi"))
340                 goto msi;
341         if (!strcmp(ioat_interrupt_style, "intx"))
342                 goto intx;
343         dev_err(dev, "invalid ioat_interrupt_style %s\n", ioat_interrupt_style);
344         goto err_no_irq;
345
346 msix:
347         /* The number of MSI-X vectors should equal the number of channels */
348         msixcnt = ioat_dma->dma_dev.chancnt;
349         for (i = 0; i < msixcnt; i++)
350                 ioat_dma->msix_entries[i].entry = i;
351
352         err = pci_enable_msix_exact(pdev, ioat_dma->msix_entries, msixcnt);
353         if (err)
354                 goto msi;
355
356         for (i = 0; i < msixcnt; i++) {
357                 msix = &ioat_dma->msix_entries[i];
358                 ioat_chan = ioat_chan_by_index(ioat_dma, i);
359                 err = devm_request_irq(dev, msix->vector,
360                                        ioat_dma_do_interrupt_msix, 0,
361                                        "ioat-msix", ioat_chan);
362                 if (err) {
363                         for (j = 0; j < i; j++) {
364                                 msix = &ioat_dma->msix_entries[j];
365                                 ioat_chan = ioat_chan_by_index(ioat_dma, j);
366                                 devm_free_irq(dev, msix->vector, ioat_chan);
367                         }
368                         goto msi;
369                 }
370         }
371         intrctrl |= IOAT_INTRCTRL_MSIX_VECTOR_CONTROL;
372         ioat_dma->irq_mode = IOAT_MSIX;
373         goto done;
374
375 msi:
376         err = pci_enable_msi(pdev);
377         if (err)
378                 goto intx;
379
380         err = devm_request_irq(dev, pdev->irq, ioat_dma_do_interrupt, 0,
381                                "ioat-msi", ioat_dma);
382         if (err) {
383                 pci_disable_msi(pdev);
384                 goto intx;
385         }
386         ioat_dma->irq_mode = IOAT_MSI;
387         goto done;
388
389 intx:
390         err = devm_request_irq(dev, pdev->irq, ioat_dma_do_interrupt,
391                                IRQF_SHARED, "ioat-intx", ioat_dma);
392         if (err)
393                 goto err_no_irq;
394
395         ioat_dma->irq_mode = IOAT_INTX;
396 done:
397         if (ioat_dma->intr_quirk)
398                 ioat_dma->intr_quirk(ioat_dma);
399         intrctrl |= IOAT_INTRCTRL_MASTER_INT_EN;
400         writeb(intrctrl, ioat_dma->reg_base + IOAT_INTRCTRL_OFFSET);
401         return 0;
402
403 err_no_irq:
404         /* Disable all interrupt generation */
405         writeb(0, ioat_dma->reg_base + IOAT_INTRCTRL_OFFSET);
406         ioat_dma->irq_mode = IOAT_NOIRQ;
407         dev_err(dev, "no usable interrupts\n");
408         return err;
409 }
410 EXPORT_SYMBOL(ioat_dma_setup_interrupts);
411
412 static void ioat_disable_interrupts(struct ioatdma_device *ioat_dma)
413 {
414         /* Disable all interrupt generation */
415         writeb(0, ioat_dma->reg_base + IOAT_INTRCTRL_OFFSET);
416 }
417
418 int ioat_probe(struct ioatdma_device *ioat_dma)
419 {
420         int err = -ENODEV;
421         struct dma_device *dma = &ioat_dma->dma_dev;
422         struct pci_dev *pdev = ioat_dma->pdev;
423         struct device *dev = &pdev->dev;
424
425         /* DMA coherent memory pool for DMA descriptor allocations */
426         ioat_dma->dma_pool = pci_pool_create("dma_desc_pool", pdev,
427                                              sizeof(struct ioat_dma_descriptor),
428                                              64, 0);
429         if (!ioat_dma->dma_pool) {
430                 err = -ENOMEM;
431                 goto err_dma_pool;
432         }
433
434         ioat_dma->completion_pool = pci_pool_create("completion_pool", pdev,
435                                                     sizeof(u64),
436                                                     SMP_CACHE_BYTES,
437                                                     SMP_CACHE_BYTES);
438
439         if (!ioat_dma->completion_pool) {
440                 err = -ENOMEM;
441                 goto err_completion_pool;
442         }
443
444         ioat_dma->enumerate_channels(ioat_dma);
445
446         dma_cap_set(DMA_MEMCPY, dma->cap_mask);
447         dma->dev = &pdev->dev;
448
449         if (!dma->chancnt) {
450                 dev_err(dev, "channel enumeration error\n");
451                 goto err_setup_interrupts;
452         }
453
454         err = ioat_dma_setup_interrupts(ioat_dma);
455         if (err)
456                 goto err_setup_interrupts;
457
458         err = ioat_dma->self_test(ioat_dma);
459         if (err)
460                 goto err_self_test;
461
462         return 0;
463
464 err_self_test:
465         ioat_disable_interrupts(ioat_dma);
466 err_setup_interrupts:
467         pci_pool_destroy(ioat_dma->completion_pool);
468 err_completion_pool:
469         pci_pool_destroy(ioat_dma->dma_pool);
470 err_dma_pool:
471         return err;
472 }
473
474 int ioat_register(struct ioatdma_device *ioat_dma)
475 {
476         int err = dma_async_device_register(&ioat_dma->dma_dev);
477
478         if (err) {
479                 ioat_disable_interrupts(ioat_dma);
480                 pci_pool_destroy(ioat_dma->completion_pool);
481                 pci_pool_destroy(ioat_dma->dma_pool);
482         }
483
484         return err;
485 }
486
487 static ssize_t cap_show(struct dma_chan *c, char *page)
488 {
489         struct dma_device *dma = c->device;
490
491         return sprintf(page, "copy%s%s%s%s%s\n",
492                        dma_has_cap(DMA_PQ, dma->cap_mask) ? " pq" : "",
493                        dma_has_cap(DMA_PQ_VAL, dma->cap_mask) ? " pq_val" : "",
494                        dma_has_cap(DMA_XOR, dma->cap_mask) ? " xor" : "",
495                        dma_has_cap(DMA_XOR_VAL, dma->cap_mask) ? " xor_val" : "",
496                        dma_has_cap(DMA_INTERRUPT, dma->cap_mask) ? " intr" : "");
497
498 }
499 struct ioat_sysfs_entry ioat_cap_attr = __ATTR_RO(cap);
500
501 static ssize_t version_show(struct dma_chan *c, char *page)
502 {
503         struct dma_device *dma = c->device;
504         struct ioatdma_device *ioat_dma = to_ioatdma_device(dma);
505
506         return sprintf(page, "%d.%d\n",
507                        ioat_dma->version >> 4, ioat_dma->version & 0xf);
508 }
509 struct ioat_sysfs_entry ioat_version_attr = __ATTR_RO(version);
510
511 static ssize_t
512 ioat_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
513 {
514         struct ioat_sysfs_entry *entry;
515         struct ioatdma_chan *ioat_chan;
516
517         entry = container_of(attr, struct ioat_sysfs_entry, attr);
518         ioat_chan = container_of(kobj, struct ioatdma_chan, kobj);
519
520         if (!entry->show)
521                 return -EIO;
522         return entry->show(&ioat_chan->dma_chan, page);
523 }
524
525 const struct sysfs_ops ioat_sysfs_ops = {
526         .show   = ioat_attr_show,
527 };
528
529 void ioat_kobject_add(struct ioatdma_device *ioat_dma, struct kobj_type *type)
530 {
531         struct dma_device *dma = &ioat_dma->dma_dev;
532         struct dma_chan *c;
533
534         list_for_each_entry(c, &dma->channels, device_node) {
535                 struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
536                 struct kobject *parent = &c->dev->device.kobj;
537                 int err;
538
539                 err = kobject_init_and_add(&ioat_chan->kobj, type,
540                                            parent, "quickdata");
541                 if (err) {
542                         dev_warn(to_dev(ioat_chan),
543                                  "sysfs init error (%d), continuing...\n", err);
544                         kobject_put(&ioat_chan->kobj);
545                         set_bit(IOAT_KOBJ_INIT_FAIL, &ioat_chan->state);
546                 }
547         }
548 }
549
550 void ioat_kobject_del(struct ioatdma_device *ioat_dma)
551 {
552         struct dma_device *dma = &ioat_dma->dma_dev;
553         struct dma_chan *c;
554
555         list_for_each_entry(c, &dma->channels, device_node) {
556                 struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
557
558                 if (!test_bit(IOAT_KOBJ_INIT_FAIL, &ioat_chan->state)) {
559                         kobject_del(&ioat_chan->kobj);
560                         kobject_put(&ioat_chan->kobj);
561                 }
562         }
563 }
564
565 void ioat_dma_remove(struct ioatdma_device *ioat_dma)
566 {
567         struct dma_device *dma = &ioat_dma->dma_dev;
568
569         ioat_disable_interrupts(ioat_dma);
570
571         ioat_kobject_del(ioat_dma);
572
573         dma_async_device_unregister(dma);
574
575         pci_pool_destroy(ioat_dma->dma_pool);
576         pci_pool_destroy(ioat_dma->completion_pool);
577
578         INIT_LIST_HEAD(&dma->channels);
579 }