dmaengine: ioatdma: remove ioat1 specific code
[firefly-linux-kernel-4.4.55.git] / drivers / dma / ioat / dca.c
1 /*
2  * Intel I/OAT DMA Linux driver
3  * Copyright(c) 2007 - 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  * The full GNU General Public License is included in this distribution in
15  * the file called "COPYING".
16  *
17  */
18
19 #include <linux/kernel.h>
20 #include <linux/pci.h>
21 #include <linux/smp.h>
22 #include <linux/interrupt.h>
23 #include <linux/dca.h>
24
25 /* either a kernel change is needed, or we need something like this in kernel */
26 #ifndef CONFIG_SMP
27 #include <asm/smp.h>
28 #undef cpu_physical_id
29 #define cpu_physical_id(cpu) (cpuid_ebx(1) >> 24)
30 #endif
31
32 #include "dma.h"
33 #include "registers.h"
34 #include "dma_v2.h"
35
36 /*
37  * Bit 7 of a tag map entry is the "valid" bit, if it is set then bits 0:6
38  * contain the bit number of the APIC ID to map into the DCA tag.  If the valid
39  * bit is not set, then the value must be 0 or 1 and defines the bit in the tag.
40  */
41 #define DCA_TAG_MAP_VALID 0x80
42
43 #define DCA3_TAG_MAP_BIT_TO_INV 0x80
44 #define DCA3_TAG_MAP_BIT_TO_SEL 0x40
45 #define DCA3_TAG_MAP_LITERAL_VAL 0x1
46
47 #define DCA_TAG_MAP_MASK 0xDF
48
49 /* expected tag map bytes for I/OAT ver.2 */
50 #define DCA2_TAG_MAP_BYTE0 0x80
51 #define DCA2_TAG_MAP_BYTE1 0x0
52 #define DCA2_TAG_MAP_BYTE2 0x81
53 #define DCA2_TAG_MAP_BYTE3 0x82
54 #define DCA2_TAG_MAP_BYTE4 0x82
55
56 /* verify if tag map matches expected values */
57 static inline int dca2_tag_map_valid(u8 *tag_map)
58 {
59         return ((tag_map[0] == DCA2_TAG_MAP_BYTE0) &&
60                 (tag_map[1] == DCA2_TAG_MAP_BYTE1) &&
61                 (tag_map[2] == DCA2_TAG_MAP_BYTE2) &&
62                 (tag_map[3] == DCA2_TAG_MAP_BYTE3) &&
63                 (tag_map[4] == DCA2_TAG_MAP_BYTE4));
64 }
65
66 /*
67  * "Legacy" DCA systems do not implement the DCA register set in the
68  * I/OAT device.  Software needs direct support for their tag mappings.
69  */
70
71 #define APICID_BIT(x)           (DCA_TAG_MAP_VALID | (x))
72 #define IOAT_TAG_MAP_LEN        8
73
74 /* pack PCI B/D/F into a u16 */
75 static inline u16 dcaid_from_pcidev(struct pci_dev *pci)
76 {
77         return (pci->bus->number << 8) | pci->devfn;
78 }
79
80 static int dca_enabled_in_bios(struct pci_dev *pdev)
81 {
82         /* CPUID level 9 returns DCA configuration */
83         /* Bit 0 indicates DCA enabled by the BIOS */
84         unsigned long cpuid_level_9;
85         int res;
86
87         cpuid_level_9 = cpuid_eax(9);
88         res = test_bit(0, &cpuid_level_9);
89         if (!res)
90                 dev_dbg(&pdev->dev, "DCA is disabled in BIOS\n");
91
92         return res;
93 }
94
95 int system_has_dca_enabled(struct pci_dev *pdev)
96 {
97         if (boot_cpu_has(X86_FEATURE_DCA))
98                 return dca_enabled_in_bios(pdev);
99
100         dev_dbg(&pdev->dev, "boot cpu doesn't have X86_FEATURE_DCA\n");
101         return 0;
102 }
103
104 struct ioat_dca_slot {
105         struct pci_dev *pdev;   /* requester device */
106         u16 rid;                /* requester id, as used by IOAT */
107 };
108
109 #define IOAT_DCA_MAX_REQ 6
110 #define IOAT3_DCA_MAX_REQ 2
111
112 struct ioat_dca_priv {
113         void __iomem            *iobase;
114         void __iomem            *dca_base;
115         int                      max_requesters;
116         int                      requester_count;
117         u8                       tag_map[IOAT_TAG_MAP_LEN];
118         struct ioat_dca_slot     req_slots[0];
119 };
120
121 static u8 ioat_dca_get_tag(struct dca_provider *dca,
122                            struct device *dev,
123                            int cpu)
124 {
125         struct ioat_dca_priv *ioatdca = dca_priv(dca);
126         int i, apic_id, bit, value;
127         u8 entry, tag;
128
129         tag = 0;
130         apic_id = cpu_physical_id(cpu);
131
132         for (i = 0; i < IOAT_TAG_MAP_LEN; i++) {
133                 entry = ioatdca->tag_map[i];
134                 if (entry & DCA_TAG_MAP_VALID) {
135                         bit = entry & ~DCA_TAG_MAP_VALID;
136                         value = (apic_id & (1 << bit)) ? 1 : 0;
137                 } else {
138                         value = entry ? 1 : 0;
139                 }
140                 tag |= (value << i);
141         }
142         return tag;
143 }
144
145 static int ioat_dca_dev_managed(struct dca_provider *dca,
146                                 struct device *dev)
147 {
148         struct ioat_dca_priv *ioatdca = dca_priv(dca);
149         struct pci_dev *pdev;
150         int i;
151
152         pdev = to_pci_dev(dev);
153         for (i = 0; i < ioatdca->max_requesters; i++) {
154                 if (ioatdca->req_slots[i].pdev == pdev)
155                         return 1;
156         }
157         return 0;
158 }
159
160 static int ioat2_dca_add_requester(struct dca_provider *dca, struct device *dev)
161 {
162         struct ioat_dca_priv *ioatdca = dca_priv(dca);
163         struct pci_dev *pdev;
164         int i;
165         u16 id;
166         u16 global_req_table;
167
168         /* This implementation only supports PCI-Express */
169         if (!dev_is_pci(dev))
170                 return -ENODEV;
171         pdev = to_pci_dev(dev);
172         id = dcaid_from_pcidev(pdev);
173
174         if (ioatdca->requester_count == ioatdca->max_requesters)
175                 return -ENODEV;
176
177         for (i = 0; i < ioatdca->max_requesters; i++) {
178                 if (ioatdca->req_slots[i].pdev == NULL) {
179                         /* found an empty slot */
180                         ioatdca->requester_count++;
181                         ioatdca->req_slots[i].pdev = pdev;
182                         ioatdca->req_slots[i].rid = id;
183                         global_req_table =
184                               readw(ioatdca->dca_base + IOAT_DCA_GREQID_OFFSET);
185                         writel(id | IOAT_DCA_GREQID_VALID,
186                                ioatdca->iobase + global_req_table + (i * 4));
187                         return i;
188                 }
189         }
190         /* Error, ioatdma->requester_count is out of whack */
191         return -EFAULT;
192 }
193
194 static int ioat2_dca_remove_requester(struct dca_provider *dca,
195                                       struct device *dev)
196 {
197         struct ioat_dca_priv *ioatdca = dca_priv(dca);
198         struct pci_dev *pdev;
199         int i;
200         u16 global_req_table;
201
202         /* This implementation only supports PCI-Express */
203         if (!dev_is_pci(dev))
204                 return -ENODEV;
205         pdev = to_pci_dev(dev);
206
207         for (i = 0; i < ioatdca->max_requesters; i++) {
208                 if (ioatdca->req_slots[i].pdev == pdev) {
209                         global_req_table =
210                               readw(ioatdca->dca_base + IOAT_DCA_GREQID_OFFSET);
211                         writel(0, ioatdca->iobase + global_req_table + (i * 4));
212                         ioatdca->req_slots[i].pdev = NULL;
213                         ioatdca->req_slots[i].rid = 0;
214                         ioatdca->requester_count--;
215                         return i;
216                 }
217         }
218         return -ENODEV;
219 }
220
221 static u8 ioat2_dca_get_tag(struct dca_provider *dca,
222                             struct device *dev,
223                             int cpu)
224 {
225         u8 tag;
226
227         tag = ioat_dca_get_tag(dca, dev, cpu);
228         tag = (~tag) & 0x1F;
229         return tag;
230 }
231
232 static struct dca_ops ioat2_dca_ops = {
233         .add_requester          = ioat2_dca_add_requester,
234         .remove_requester       = ioat2_dca_remove_requester,
235         .get_tag                = ioat2_dca_get_tag,
236         .dev_managed            = ioat_dca_dev_managed,
237 };
238
239 static int ioat2_dca_count_dca_slots(void __iomem *iobase, u16 dca_offset)
240 {
241         int slots = 0;
242         u32 req;
243         u16 global_req_table;
244
245         global_req_table = readw(iobase + dca_offset + IOAT_DCA_GREQID_OFFSET);
246         if (global_req_table == 0)
247                 return 0;
248         do {
249                 req = readl(iobase + global_req_table + (slots * sizeof(u32)));
250                 slots++;
251         } while ((req & IOAT_DCA_GREQID_LASTID) == 0);
252
253         return slots;
254 }
255
256 struct dca_provider *ioat2_dca_init(struct pci_dev *pdev, void __iomem *iobase)
257 {
258         struct dca_provider *dca;
259         struct ioat_dca_priv *ioatdca;
260         int slots;
261         int i;
262         int err;
263         u32 tag_map;
264         u16 dca_offset;
265         u16 csi_fsb_control;
266         u16 pcie_control;
267         u8 bit;
268
269         if (!system_has_dca_enabled(pdev))
270                 return NULL;
271
272         dca_offset = readw(iobase + IOAT_DCAOFFSET_OFFSET);
273         if (dca_offset == 0)
274                 return NULL;
275
276         slots = ioat2_dca_count_dca_slots(iobase, dca_offset);
277         if (slots == 0)
278                 return NULL;
279
280         dca = alloc_dca_provider(&ioat2_dca_ops,
281                                  sizeof(*ioatdca)
282                                       + (sizeof(struct ioat_dca_slot) * slots));
283         if (!dca)
284                 return NULL;
285
286         ioatdca = dca_priv(dca);
287         ioatdca->iobase = iobase;
288         ioatdca->dca_base = iobase + dca_offset;
289         ioatdca->max_requesters = slots;
290
291         /* some bios might not know to turn these on */
292         csi_fsb_control = readw(ioatdca->dca_base + IOAT_FSB_CAP_ENABLE_OFFSET);
293         if ((csi_fsb_control & IOAT_FSB_CAP_ENABLE_PREFETCH) == 0) {
294                 csi_fsb_control |= IOAT_FSB_CAP_ENABLE_PREFETCH;
295                 writew(csi_fsb_control,
296                        ioatdca->dca_base + IOAT_FSB_CAP_ENABLE_OFFSET);
297         }
298         pcie_control = readw(ioatdca->dca_base + IOAT_PCI_CAP_ENABLE_OFFSET);
299         if ((pcie_control & IOAT_PCI_CAP_ENABLE_MEMWR) == 0) {
300                 pcie_control |= IOAT_PCI_CAP_ENABLE_MEMWR;
301                 writew(pcie_control,
302                        ioatdca->dca_base + IOAT_PCI_CAP_ENABLE_OFFSET);
303         }
304
305
306         /* TODO version, compatibility and configuration checks */
307
308         /* copy out the APIC to DCA tag map */
309         tag_map = readl(ioatdca->dca_base + IOAT_APICID_TAG_MAP_OFFSET);
310         for (i = 0; i < 5; i++) {
311                 bit = (tag_map >> (4 * i)) & 0x0f;
312                 if (bit < 8)
313                         ioatdca->tag_map[i] = bit | DCA_TAG_MAP_VALID;
314                 else
315                         ioatdca->tag_map[i] = 0;
316         }
317
318         if (!dca2_tag_map_valid(ioatdca->tag_map)) {
319                 WARN_TAINT_ONCE(1, TAINT_FIRMWARE_WORKAROUND,
320                                 "%s %s: APICID_TAG_MAP set incorrectly by BIOS, disabling DCA\n",
321                                 dev_driver_string(&pdev->dev),
322                                 dev_name(&pdev->dev));
323                 free_dca_provider(dca);
324                 return NULL;
325         }
326
327         err = register_dca_provider(dca, &pdev->dev);
328         if (err) {
329                 free_dca_provider(dca);
330                 return NULL;
331         }
332
333         return dca;
334 }
335
336 static int ioat3_dca_add_requester(struct dca_provider *dca, struct device *dev)
337 {
338         struct ioat_dca_priv *ioatdca = dca_priv(dca);
339         struct pci_dev *pdev;
340         int i;
341         u16 id;
342         u16 global_req_table;
343
344         /* This implementation only supports PCI-Express */
345         if (!dev_is_pci(dev))
346                 return -ENODEV;
347         pdev = to_pci_dev(dev);
348         id = dcaid_from_pcidev(pdev);
349
350         if (ioatdca->requester_count == ioatdca->max_requesters)
351                 return -ENODEV;
352
353         for (i = 0; i < ioatdca->max_requesters; i++) {
354                 if (ioatdca->req_slots[i].pdev == NULL) {
355                         /* found an empty slot */
356                         ioatdca->requester_count++;
357                         ioatdca->req_slots[i].pdev = pdev;
358                         ioatdca->req_slots[i].rid = id;
359                         global_req_table =
360                               readw(ioatdca->dca_base + IOAT3_DCA_GREQID_OFFSET);
361                         writel(id | IOAT_DCA_GREQID_VALID,
362                                ioatdca->iobase + global_req_table + (i * 4));
363                         return i;
364                 }
365         }
366         /* Error, ioatdma->requester_count is out of whack */
367         return -EFAULT;
368 }
369
370 static int ioat3_dca_remove_requester(struct dca_provider *dca,
371                                       struct device *dev)
372 {
373         struct ioat_dca_priv *ioatdca = dca_priv(dca);
374         struct pci_dev *pdev;
375         int i;
376         u16 global_req_table;
377
378         /* This implementation only supports PCI-Express */
379         if (!dev_is_pci(dev))
380                 return -ENODEV;
381         pdev = to_pci_dev(dev);
382
383         for (i = 0; i < ioatdca->max_requesters; i++) {
384                 if (ioatdca->req_slots[i].pdev == pdev) {
385                         global_req_table =
386                               readw(ioatdca->dca_base + IOAT3_DCA_GREQID_OFFSET);
387                         writel(0, ioatdca->iobase + global_req_table + (i * 4));
388                         ioatdca->req_slots[i].pdev = NULL;
389                         ioatdca->req_slots[i].rid = 0;
390                         ioatdca->requester_count--;
391                         return i;
392                 }
393         }
394         return -ENODEV;
395 }
396
397 static u8 ioat3_dca_get_tag(struct dca_provider *dca,
398                             struct device *dev,
399                             int cpu)
400 {
401         u8 tag;
402
403         struct ioat_dca_priv *ioatdca = dca_priv(dca);
404         int i, apic_id, bit, value;
405         u8 entry;
406
407         tag = 0;
408         apic_id = cpu_physical_id(cpu);
409
410         for (i = 0; i < IOAT_TAG_MAP_LEN; i++) {
411                 entry = ioatdca->tag_map[i];
412                 if (entry & DCA3_TAG_MAP_BIT_TO_SEL) {
413                         bit = entry &
414                                 ~(DCA3_TAG_MAP_BIT_TO_SEL | DCA3_TAG_MAP_BIT_TO_INV);
415                         value = (apic_id & (1 << bit)) ? 1 : 0;
416                 } else if (entry & DCA3_TAG_MAP_BIT_TO_INV) {
417                         bit = entry & ~DCA3_TAG_MAP_BIT_TO_INV;
418                         value = (apic_id & (1 << bit)) ? 0 : 1;
419                 } else {
420                         value = (entry & DCA3_TAG_MAP_LITERAL_VAL) ? 1 : 0;
421                 }
422                 tag |= (value << i);
423         }
424
425         return tag;
426 }
427
428 static struct dca_ops ioat3_dca_ops = {
429         .add_requester          = ioat3_dca_add_requester,
430         .remove_requester       = ioat3_dca_remove_requester,
431         .get_tag                = ioat3_dca_get_tag,
432         .dev_managed            = ioat_dca_dev_managed,
433 };
434
435 static int ioat3_dca_count_dca_slots(void *iobase, u16 dca_offset)
436 {
437         int slots = 0;
438         u32 req;
439         u16 global_req_table;
440
441         global_req_table = readw(iobase + dca_offset + IOAT3_DCA_GREQID_OFFSET);
442         if (global_req_table == 0)
443                 return 0;
444
445         do {
446                 req = readl(iobase + global_req_table + (slots * sizeof(u32)));
447                 slots++;
448         } while ((req & IOAT_DCA_GREQID_LASTID) == 0);
449
450         return slots;
451 }
452
453 static inline int dca3_tag_map_invalid(u8 *tag_map)
454 {
455         /*
456          * If the tag map is not programmed by the BIOS the default is:
457          * 0x80 0x80 0x80 0x80 0x80 0x00 0x00 0x00
458          *
459          * This an invalid map and will result in only 2 possible tags
460          * 0x1F and 0x00.  0x00 is an invalid DCA tag so we know that
461          * this entire definition is invalid.
462          */
463         return ((tag_map[0] == DCA_TAG_MAP_VALID) &&
464                 (tag_map[1] == DCA_TAG_MAP_VALID) &&
465                 (tag_map[2] == DCA_TAG_MAP_VALID) &&
466                 (tag_map[3] == DCA_TAG_MAP_VALID) &&
467                 (tag_map[4] == DCA_TAG_MAP_VALID));
468 }
469
470 struct dca_provider *ioat3_dca_init(struct pci_dev *pdev, void __iomem *iobase)
471 {
472         struct dca_provider *dca;
473         struct ioat_dca_priv *ioatdca;
474         int slots;
475         int i;
476         int err;
477         u16 dca_offset;
478         u16 csi_fsb_control;
479         u16 pcie_control;
480         u8 bit;
481
482         union {
483                 u64 full;
484                 struct {
485                         u32 low;
486                         u32 high;
487                 };
488         } tag_map;
489
490         if (!system_has_dca_enabled(pdev))
491                 return NULL;
492
493         dca_offset = readw(iobase + IOAT_DCAOFFSET_OFFSET);
494         if (dca_offset == 0)
495                 return NULL;
496
497         slots = ioat3_dca_count_dca_slots(iobase, dca_offset);
498         if (slots == 0)
499                 return NULL;
500
501         dca = alloc_dca_provider(&ioat3_dca_ops,
502                                  sizeof(*ioatdca)
503                                       + (sizeof(struct ioat_dca_slot) * slots));
504         if (!dca)
505                 return NULL;
506
507         ioatdca = dca_priv(dca);
508         ioatdca->iobase = iobase;
509         ioatdca->dca_base = iobase + dca_offset;
510         ioatdca->max_requesters = slots;
511
512         /* some bios might not know to turn these on */
513         csi_fsb_control = readw(ioatdca->dca_base + IOAT3_CSI_CONTROL_OFFSET);
514         if ((csi_fsb_control & IOAT3_CSI_CONTROL_PREFETCH) == 0) {
515                 csi_fsb_control |= IOAT3_CSI_CONTROL_PREFETCH;
516                 writew(csi_fsb_control,
517                        ioatdca->dca_base + IOAT3_CSI_CONTROL_OFFSET);
518         }
519         pcie_control = readw(ioatdca->dca_base + IOAT3_PCI_CONTROL_OFFSET);
520         if ((pcie_control & IOAT3_PCI_CONTROL_MEMWR) == 0) {
521                 pcie_control |= IOAT3_PCI_CONTROL_MEMWR;
522                 writew(pcie_control,
523                        ioatdca->dca_base + IOAT3_PCI_CONTROL_OFFSET);
524         }
525
526
527         /* TODO version, compatibility and configuration checks */
528
529         /* copy out the APIC to DCA tag map */
530         tag_map.low =
531                 readl(ioatdca->dca_base + IOAT3_APICID_TAG_MAP_OFFSET_LOW);
532         tag_map.high =
533                 readl(ioatdca->dca_base + IOAT3_APICID_TAG_MAP_OFFSET_HIGH);
534         for (i = 0; i < 8; i++) {
535                 bit = tag_map.full >> (8 * i);
536                 ioatdca->tag_map[i] = bit & DCA_TAG_MAP_MASK;
537         }
538
539         if (dca3_tag_map_invalid(ioatdca->tag_map)) {
540                 WARN_TAINT_ONCE(1, TAINT_FIRMWARE_WORKAROUND,
541                                 "%s %s: APICID_TAG_MAP set incorrectly by BIOS, disabling DCA\n",
542                                 dev_driver_string(&pdev->dev),
543                                 dev_name(&pdev->dev));
544                 free_dca_provider(dca);
545                 return NULL;
546         }
547
548         err = register_dca_provider(dca, &pdev->dev);
549         if (err) {
550                 free_dca_provider(dca);
551                 return NULL;
552         }
553
554         return dca;
555 }