2 * Intel I/OAT DMA Linux driver
3 * Copyright(c) 2007 Intel Corporation.
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.
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
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.
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
23 #include <linux/kernel.h>
24 #include <linux/pci.h>
25 #include <linux/smp.h>
26 #include <linux/interrupt.h>
27 #include <linux/dca.h>
29 /* either a kernel change is needed, or we need something like this in kernel */
32 #undef cpu_physical_id
33 #define cpu_physical_id(cpu) (cpuid_ebx(1) >> 24)
37 #include "ioatdma_registers.h"
40 * Bit 7 of a tag map entry is the "valid" bit, if it is set then bits 0:6
41 * contain the bit number of the APIC ID to map into the DCA tag. If the valid
42 * bit is not set, then the value must be 0 or 1 and defines the bit in the tag.
44 #define DCA_TAG_MAP_VALID 0x80
46 #define DCA3_TAG_MAP_BIT_TO_INV 0x80
47 #define DCA3_TAG_MAP_BIT_TO_SEL 0x40
48 #define DCA3_TAG_MAP_LITERAL_VAL 0x1
50 #define DCA_TAG_MAP_MASK 0xDF
53 * "Legacy" DCA systems do not implement the DCA register set in the
54 * I/OAT device. Software needs direct support for their tag mappings.
57 #define APICID_BIT(x) (DCA_TAG_MAP_VALID | (x))
58 #define IOAT_TAG_MAP_LEN 8
60 static u8 ioat_tag_map_BNB[IOAT_TAG_MAP_LEN] = {
61 1, APICID_BIT(1), APICID_BIT(2), APICID_BIT(2), };
62 static u8 ioat_tag_map_SCNB[IOAT_TAG_MAP_LEN] = {
63 1, APICID_BIT(1), APICID_BIT(2), APICID_BIT(2), };
64 static u8 ioat_tag_map_CNB[IOAT_TAG_MAP_LEN] = {
65 1, APICID_BIT(1), APICID_BIT(3), APICID_BIT(4), APICID_BIT(2), };
66 static u8 ioat_tag_map_UNISYS[IOAT_TAG_MAP_LEN] = { 0 };
68 /* pack PCI B/D/F into a u16 */
69 static inline u16 dcaid_from_pcidev(struct pci_dev *pci)
71 return (pci->bus->number << 8) | pci->devfn;
74 static int dca_enabled_in_bios(struct pci_dev *pdev)
76 /* CPUID level 9 returns DCA configuration */
77 /* Bit 0 indicates DCA enabled by the BIOS */
78 unsigned long cpuid_level_9;
81 cpuid_level_9 = cpuid_eax(9);
82 res = test_bit(0, &cpuid_level_9);
84 dev_err(&pdev->dev, "DCA is disabled in BIOS\n");
89 static int system_has_dca_enabled(struct pci_dev *pdev)
91 if (boot_cpu_has(X86_FEATURE_DCA))
92 return dca_enabled_in_bios(pdev);
94 dev_err(&pdev->dev, "boot cpu doesn't have X86_FEATURE_DCA\n");
98 struct ioat_dca_slot {
99 struct pci_dev *pdev; /* requester device */
100 u16 rid; /* requester id, as used by IOAT */
103 #define IOAT_DCA_MAX_REQ 6
104 #define IOAT3_DCA_MAX_REQ 2
106 struct ioat_dca_priv {
107 void __iomem *iobase;
108 void __iomem *dca_base;
111 u8 tag_map[IOAT_TAG_MAP_LEN];
112 struct ioat_dca_slot req_slots[0];
115 /* 5000 series chipset DCA Port Requester ID Table Entry Format
116 * [15:8] PCI-Express Bus Number
117 * [7:3] PCI-Express Device Number
118 * [2:0] PCI-Express Function Number
120 * 5000 series chipset DCA control register format
122 * [0] Ignore Function Number
125 static int ioat_dca_add_requester(struct dca_provider *dca, struct device *dev)
127 struct ioat_dca_priv *ioatdca = dca_priv(dca);
128 struct pci_dev *pdev;
132 /* This implementation only supports PCI-Express */
133 if (dev->bus != &pci_bus_type)
135 pdev = to_pci_dev(dev);
136 id = dcaid_from_pcidev(pdev);
138 if (ioatdca->requester_count == ioatdca->max_requesters)
141 for (i = 0; i < ioatdca->max_requesters; i++) {
142 if (ioatdca->req_slots[i].pdev == NULL) {
143 /* found an empty slot */
144 ioatdca->requester_count++;
145 ioatdca->req_slots[i].pdev = pdev;
146 ioatdca->req_slots[i].rid = id;
147 writew(id, ioatdca->dca_base + (i * 4));
148 /* make sure the ignore function bit is off */
149 writeb(0, ioatdca->dca_base + (i * 4) + 2);
153 /* Error, ioatdma->requester_count is out of whack */
157 static int ioat_dca_remove_requester(struct dca_provider *dca,
160 struct ioat_dca_priv *ioatdca = dca_priv(dca);
161 struct pci_dev *pdev;
164 /* This implementation only supports PCI-Express */
165 if (dev->bus != &pci_bus_type)
167 pdev = to_pci_dev(dev);
169 for (i = 0; i < ioatdca->max_requesters; i++) {
170 if (ioatdca->req_slots[i].pdev == pdev) {
171 writew(0, ioatdca->dca_base + (i * 4));
172 ioatdca->req_slots[i].pdev = NULL;
173 ioatdca->req_slots[i].rid = 0;
174 ioatdca->requester_count--;
181 static u8 ioat_dca_get_tag(struct dca_provider *dca,
185 struct ioat_dca_priv *ioatdca = dca_priv(dca);
186 int i, apic_id, bit, value;
190 apic_id = cpu_physical_id(cpu);
192 for (i = 0; i < IOAT_TAG_MAP_LEN; i++) {
193 entry = ioatdca->tag_map[i];
194 if (entry & DCA_TAG_MAP_VALID) {
195 bit = entry & ~DCA_TAG_MAP_VALID;
196 value = (apic_id & (1 << bit)) ? 1 : 0;
198 value = entry ? 1 : 0;
205 static int ioat_dca_dev_managed(struct dca_provider *dca,
208 struct ioat_dca_priv *ioatdca = dca_priv(dca);
209 struct pci_dev *pdev;
212 pdev = to_pci_dev(dev);
213 for (i = 0; i < ioatdca->max_requesters; i++) {
214 if (ioatdca->req_slots[i].pdev == pdev)
220 static struct dca_ops ioat_dca_ops = {
221 .add_requester = ioat_dca_add_requester,
222 .remove_requester = ioat_dca_remove_requester,
223 .get_tag = ioat_dca_get_tag,
224 .dev_managed = ioat_dca_dev_managed,
228 struct dca_provider *ioat_dca_init(struct pci_dev *pdev, void __iomem *iobase)
230 struct dca_provider *dca;
231 struct ioat_dca_priv *ioatdca;
238 if (!system_has_dca_enabled(pdev))
241 /* I/OAT v1 systems must have a known tag_map to support DCA */
242 switch (pdev->vendor) {
243 case PCI_VENDOR_ID_INTEL:
244 switch (pdev->device) {
245 case PCI_DEVICE_ID_INTEL_IOAT:
246 tag_map = ioat_tag_map_BNB;
248 case PCI_DEVICE_ID_INTEL_IOAT_CNB:
249 tag_map = ioat_tag_map_CNB;
251 case PCI_DEVICE_ID_INTEL_IOAT_SCNB:
252 tag_map = ioat_tag_map_SCNB;
256 case PCI_VENDOR_ID_UNISYS:
257 switch (pdev->device) {
258 case PCI_DEVICE_ID_UNISYS_DMA_DIRECTOR:
259 tag_map = ioat_tag_map_UNISYS;
267 version = readb(iobase + IOAT_VER_OFFSET);
268 if (version == IOAT_VER_3_0)
269 max_requesters = IOAT3_DCA_MAX_REQ;
271 max_requesters = IOAT_DCA_MAX_REQ;
273 dca = alloc_dca_provider(&ioat_dca_ops,
275 (sizeof(struct ioat_dca_slot) * max_requesters));
279 ioatdca = dca_priv(dca);
280 ioatdca->max_requesters = max_requesters;
281 ioatdca->dca_base = iobase + 0x54;
283 /* copy over the APIC ID to DCA tag mapping */
284 for (i = 0; i < IOAT_TAG_MAP_LEN; i++)
285 ioatdca->tag_map[i] = tag_map[i];
287 err = register_dca_provider(dca, &pdev->dev);
289 free_dca_provider(dca);
297 static int ioat2_dca_add_requester(struct dca_provider *dca, struct device *dev)
299 struct ioat_dca_priv *ioatdca = dca_priv(dca);
300 struct pci_dev *pdev;
303 u16 global_req_table;
305 /* This implementation only supports PCI-Express */
306 if (dev->bus != &pci_bus_type)
308 pdev = to_pci_dev(dev);
309 id = dcaid_from_pcidev(pdev);
311 if (ioatdca->requester_count == ioatdca->max_requesters)
314 for (i = 0; i < ioatdca->max_requesters; i++) {
315 if (ioatdca->req_slots[i].pdev == NULL) {
316 /* found an empty slot */
317 ioatdca->requester_count++;
318 ioatdca->req_slots[i].pdev = pdev;
319 ioatdca->req_slots[i].rid = id;
321 readw(ioatdca->dca_base + IOAT_DCA_GREQID_OFFSET);
322 writel(id | IOAT_DCA_GREQID_VALID,
323 ioatdca->iobase + global_req_table + (i * 4));
327 /* Error, ioatdma->requester_count is out of whack */
331 static int ioat2_dca_remove_requester(struct dca_provider *dca,
334 struct ioat_dca_priv *ioatdca = dca_priv(dca);
335 struct pci_dev *pdev;
337 u16 global_req_table;
339 /* This implementation only supports PCI-Express */
340 if (dev->bus != &pci_bus_type)
342 pdev = to_pci_dev(dev);
344 for (i = 0; i < ioatdca->max_requesters; i++) {
345 if (ioatdca->req_slots[i].pdev == pdev) {
347 readw(ioatdca->dca_base + IOAT_DCA_GREQID_OFFSET);
348 writel(0, ioatdca->iobase + global_req_table + (i * 4));
349 ioatdca->req_slots[i].pdev = NULL;
350 ioatdca->req_slots[i].rid = 0;
351 ioatdca->requester_count--;
358 static u8 ioat2_dca_get_tag(struct dca_provider *dca,
364 tag = ioat_dca_get_tag(dca, dev, cpu);
369 static struct dca_ops ioat2_dca_ops = {
370 .add_requester = ioat2_dca_add_requester,
371 .remove_requester = ioat2_dca_remove_requester,
372 .get_tag = ioat2_dca_get_tag,
373 .dev_managed = ioat_dca_dev_managed,
376 static int ioat2_dca_count_dca_slots(void __iomem *iobase, u16 dca_offset)
380 u16 global_req_table;
382 global_req_table = readw(iobase + dca_offset + IOAT_DCA_GREQID_OFFSET);
383 if (global_req_table == 0)
386 req = readl(iobase + global_req_table + (slots * sizeof(u32)));
388 } while ((req & IOAT_DCA_GREQID_LASTID) == 0);
393 struct dca_provider *ioat2_dca_init(struct pci_dev *pdev, void __iomem *iobase)
395 struct dca_provider *dca;
396 struct ioat_dca_priv *ioatdca;
406 if (!system_has_dca_enabled(pdev))
409 dca_offset = readw(iobase + IOAT_DCAOFFSET_OFFSET);
413 slots = ioat2_dca_count_dca_slots(iobase, dca_offset);
417 dca = alloc_dca_provider(&ioat2_dca_ops,
419 + (sizeof(struct ioat_dca_slot) * slots));
423 ioatdca = dca_priv(dca);
424 ioatdca->iobase = iobase;
425 ioatdca->dca_base = iobase + dca_offset;
426 ioatdca->max_requesters = slots;
428 /* some bios might not know to turn these on */
429 csi_fsb_control = readw(ioatdca->dca_base + IOAT_FSB_CAP_ENABLE_OFFSET);
430 if ((csi_fsb_control & IOAT_FSB_CAP_ENABLE_PREFETCH) == 0) {
431 csi_fsb_control |= IOAT_FSB_CAP_ENABLE_PREFETCH;
432 writew(csi_fsb_control,
433 ioatdca->dca_base + IOAT_FSB_CAP_ENABLE_OFFSET);
435 pcie_control = readw(ioatdca->dca_base + IOAT_PCI_CAP_ENABLE_OFFSET);
436 if ((pcie_control & IOAT_PCI_CAP_ENABLE_MEMWR) == 0) {
437 pcie_control |= IOAT_PCI_CAP_ENABLE_MEMWR;
439 ioatdca->dca_base + IOAT_PCI_CAP_ENABLE_OFFSET);
443 /* TODO version, compatibility and configuration checks */
445 /* copy out the APIC to DCA tag map */
446 tag_map = readl(ioatdca->dca_base + IOAT_APICID_TAG_MAP_OFFSET);
447 for (i = 0; i < 5; i++) {
448 bit = (tag_map >> (4 * i)) & 0x0f;
450 ioatdca->tag_map[i] = bit | DCA_TAG_MAP_VALID;
452 ioatdca->tag_map[i] = 0;
455 err = register_dca_provider(dca, &pdev->dev);
457 free_dca_provider(dca);
464 static int ioat3_dca_add_requester(struct dca_provider *dca, struct device *dev)
466 struct ioat_dca_priv *ioatdca = dca_priv(dca);
467 struct pci_dev *pdev;
470 u16 global_req_table;
472 /* This implementation only supports PCI-Express */
473 if (dev->bus != &pci_bus_type)
475 pdev = to_pci_dev(dev);
476 id = dcaid_from_pcidev(pdev);
478 if (ioatdca->requester_count == ioatdca->max_requesters)
481 for (i = 0; i < ioatdca->max_requesters; i++) {
482 if (ioatdca->req_slots[i].pdev == NULL) {
483 /* found an empty slot */
484 ioatdca->requester_count++;
485 ioatdca->req_slots[i].pdev = pdev;
486 ioatdca->req_slots[i].rid = id;
488 readw(ioatdca->dca_base + IOAT3_DCA_GREQID_OFFSET);
489 writel(id | IOAT_DCA_GREQID_VALID,
490 ioatdca->iobase + global_req_table + (i * 4));
494 /* Error, ioatdma->requester_count is out of whack */
498 static int ioat3_dca_remove_requester(struct dca_provider *dca,
501 struct ioat_dca_priv *ioatdca = dca_priv(dca);
502 struct pci_dev *pdev;
504 u16 global_req_table;
506 /* This implementation only supports PCI-Express */
507 if (dev->bus != &pci_bus_type)
509 pdev = to_pci_dev(dev);
511 for (i = 0; i < ioatdca->max_requesters; i++) {
512 if (ioatdca->req_slots[i].pdev == pdev) {
514 readw(ioatdca->dca_base + IOAT3_DCA_GREQID_OFFSET);
515 writel(0, ioatdca->iobase + global_req_table + (i * 4));
516 ioatdca->req_slots[i].pdev = NULL;
517 ioatdca->req_slots[i].rid = 0;
518 ioatdca->requester_count--;
525 static u8 ioat3_dca_get_tag(struct dca_provider *dca,
531 struct ioat_dca_priv *ioatdca = dca_priv(dca);
532 int i, apic_id, bit, value;
536 apic_id = cpu_physical_id(cpu);
538 for (i = 0; i < IOAT_TAG_MAP_LEN; i++) {
539 entry = ioatdca->tag_map[i];
540 if (entry & DCA3_TAG_MAP_BIT_TO_SEL) {
542 ~(DCA3_TAG_MAP_BIT_TO_SEL | DCA3_TAG_MAP_BIT_TO_INV);
543 value = (apic_id & (1 << bit)) ? 1 : 0;
544 } else if (entry & DCA3_TAG_MAP_BIT_TO_INV) {
545 bit = entry & ~DCA3_TAG_MAP_BIT_TO_INV;
546 value = (apic_id & (1 << bit)) ? 0 : 1;
548 value = (entry & DCA3_TAG_MAP_LITERAL_VAL) ? 1 : 0;
556 static struct dca_ops ioat3_dca_ops = {
557 .add_requester = ioat3_dca_add_requester,
558 .remove_requester = ioat3_dca_remove_requester,
559 .get_tag = ioat3_dca_get_tag,
560 .dev_managed = ioat_dca_dev_managed,
563 static int ioat3_dca_count_dca_slots(void *iobase, u16 dca_offset)
567 u16 global_req_table;
569 global_req_table = readw(iobase + dca_offset + IOAT3_DCA_GREQID_OFFSET);
570 if (global_req_table == 0)
574 req = readl(iobase + global_req_table + (slots * sizeof(u32)));
576 } while ((req & IOAT_DCA_GREQID_LASTID) == 0);
581 struct dca_provider *ioat3_dca_init(struct pci_dev *pdev, void __iomem *iobase)
583 struct dca_provider *dca;
584 struct ioat_dca_priv *ioatdca;
601 if (!system_has_dca_enabled(pdev))
604 dca_offset = readw(iobase + IOAT_DCAOFFSET_OFFSET);
608 slots = ioat3_dca_count_dca_slots(iobase, dca_offset);
612 dca = alloc_dca_provider(&ioat3_dca_ops,
614 + (sizeof(struct ioat_dca_slot) * slots));
618 ioatdca = dca_priv(dca);
619 ioatdca->iobase = iobase;
620 ioatdca->dca_base = iobase + dca_offset;
621 ioatdca->max_requesters = slots;
623 /* some bios might not know to turn these on */
624 csi_fsb_control = readw(ioatdca->dca_base + IOAT3_CSI_CONTROL_OFFSET);
625 if ((csi_fsb_control & IOAT3_CSI_CONTROL_PREFETCH) == 0) {
626 csi_fsb_control |= IOAT3_CSI_CONTROL_PREFETCH;
627 writew(csi_fsb_control,
628 ioatdca->dca_base + IOAT3_CSI_CONTROL_OFFSET);
630 pcie_control = readw(ioatdca->dca_base + IOAT3_PCI_CONTROL_OFFSET);
631 if ((pcie_control & IOAT3_PCI_CONTROL_MEMWR) == 0) {
632 pcie_control |= IOAT3_PCI_CONTROL_MEMWR;
634 ioatdca->dca_base + IOAT3_PCI_CONTROL_OFFSET);
638 /* TODO version, compatibility and configuration checks */
640 /* copy out the APIC to DCA tag map */
642 readl(ioatdca->dca_base + IOAT3_APICID_TAG_MAP_OFFSET_LOW);
644 readl(ioatdca->dca_base + IOAT3_APICID_TAG_MAP_OFFSET_HIGH);
645 for (i = 0; i < 8; i++) {
646 bit = tag_map.full >> (8 * i);
647 ioatdca->tag_map[i] = bit & DCA_TAG_MAP_MASK;
650 err = register_dca_provider(dca, &pdev->dev);
652 free_dca_provider(dca);