xref: /linux/drivers/dma/ioat/dca.c (revision 2bb129ebb23d2dfec3cd9c22dc7defd681cfcd58)
1584ec227SDan Williams /*
2584ec227SDan Williams  * Intel I/OAT DMA Linux driver
3584ec227SDan Williams  * Copyright(c) 2007 - 2009 Intel Corporation.
4584ec227SDan Williams  *
5584ec227SDan Williams  * This program is free software; you can redistribute it and/or modify it
6584ec227SDan Williams  * under the terms and conditions of the GNU General Public License,
7584ec227SDan Williams  * version 2, as published by the Free Software Foundation.
8584ec227SDan Williams  *
9584ec227SDan Williams  * This program is distributed in the hope that it will be useful, but WITHOUT
10584ec227SDan Williams  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11584ec227SDan Williams  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12584ec227SDan Williams  * more details.
13584ec227SDan Williams  *
14584ec227SDan Williams  * The full GNU General Public License is included in this distribution in
15584ec227SDan Williams  * the file called "COPYING".
16584ec227SDan Williams  *
17584ec227SDan Williams  */
18584ec227SDan Williams 
19584ec227SDan Williams #include <linux/kernel.h>
20584ec227SDan Williams #include <linux/pci.h>
21584ec227SDan Williams #include <linux/smp.h>
22584ec227SDan Williams #include <linux/interrupt.h>
23584ec227SDan Williams #include <linux/dca.h>
24584ec227SDan Williams 
25584ec227SDan Williams /* either a kernel change is needed, or we need something like this in kernel */
26584ec227SDan Williams #ifndef CONFIG_SMP
27584ec227SDan Williams #include <asm/smp.h>
28584ec227SDan Williams #undef cpu_physical_id
29584ec227SDan Williams #define cpu_physical_id(cpu) (cpuid_ebx(1) >> 24)
30584ec227SDan Williams #endif
31584ec227SDan Williams 
32584ec227SDan Williams #include "dma.h"
33584ec227SDan Williams #include "registers.h"
34584ec227SDan Williams 
35584ec227SDan Williams /*
36584ec227SDan Williams  * Bit 7 of a tag map entry is the "valid" bit, if it is set then bits 0:6
37584ec227SDan Williams  * contain the bit number of the APIC ID to map into the DCA tag.  If the valid
38584ec227SDan Williams  * bit is not set, then the value must be 0 or 1 and defines the bit in the tag.
39584ec227SDan Williams  */
40584ec227SDan Williams #define DCA_TAG_MAP_VALID 0x80
41584ec227SDan Williams 
42584ec227SDan Williams #define DCA3_TAG_MAP_BIT_TO_INV 0x80
43584ec227SDan Williams #define DCA3_TAG_MAP_BIT_TO_SEL 0x40
44584ec227SDan Williams #define DCA3_TAG_MAP_LITERAL_VAL 0x1
45584ec227SDan Williams 
46584ec227SDan Williams #define DCA_TAG_MAP_MASK 0xDF
47584ec227SDan Williams 
48584ec227SDan Williams /* expected tag map bytes for I/OAT ver.2 */
49584ec227SDan Williams #define DCA2_TAG_MAP_BYTE0 0x80
50584ec227SDan Williams #define DCA2_TAG_MAP_BYTE1 0x0
51584ec227SDan Williams #define DCA2_TAG_MAP_BYTE2 0x81
52584ec227SDan Williams #define DCA2_TAG_MAP_BYTE3 0x82
53584ec227SDan Williams #define DCA2_TAG_MAP_BYTE4 0x82
54584ec227SDan Williams 
55584ec227SDan Williams /* verify if tag map matches expected values */
56584ec227SDan Williams static inline int dca2_tag_map_valid(u8 *tag_map)
57584ec227SDan Williams {
58584ec227SDan Williams 	return ((tag_map[0] == DCA2_TAG_MAP_BYTE0) &&
59584ec227SDan Williams 		(tag_map[1] == DCA2_TAG_MAP_BYTE1) &&
60584ec227SDan Williams 		(tag_map[2] == DCA2_TAG_MAP_BYTE2) &&
61584ec227SDan Williams 		(tag_map[3] == DCA2_TAG_MAP_BYTE3) &&
62584ec227SDan Williams 		(tag_map[4] == DCA2_TAG_MAP_BYTE4));
63584ec227SDan Williams }
64584ec227SDan Williams 
65584ec227SDan Williams /*
66584ec227SDan Williams  * "Legacy" DCA systems do not implement the DCA register set in the
67584ec227SDan Williams  * I/OAT device.  Software needs direct support for their tag mappings.
68584ec227SDan Williams  */
69584ec227SDan Williams 
70584ec227SDan Williams #define APICID_BIT(x)		(DCA_TAG_MAP_VALID | (x))
71584ec227SDan Williams #define IOAT_TAG_MAP_LEN	8
72584ec227SDan Williams 
73584ec227SDan Williams /* pack PCI B/D/F into a u16 */
74584ec227SDan Williams static inline u16 dcaid_from_pcidev(struct pci_dev *pci)
75584ec227SDan Williams {
76584ec227SDan Williams 	return (pci->bus->number << 8) | pci->devfn;
77584ec227SDan Williams }
78584ec227SDan Williams 
79584ec227SDan Williams static int dca_enabled_in_bios(struct pci_dev *pdev)
80584ec227SDan Williams {
81584ec227SDan Williams 	/* CPUID level 9 returns DCA configuration */
82584ec227SDan Williams 	/* Bit 0 indicates DCA enabled by the BIOS */
83584ec227SDan Williams 	unsigned long cpuid_level_9;
84584ec227SDan Williams 	int res;
85584ec227SDan Williams 
86584ec227SDan Williams 	cpuid_level_9 = cpuid_eax(9);
87584ec227SDan Williams 	res = test_bit(0, &cpuid_level_9);
88584ec227SDan Williams 	if (!res)
89e22dde99SDan Williams 		dev_dbg(&pdev->dev, "DCA is disabled in BIOS\n");
90584ec227SDan Williams 
91584ec227SDan Williams 	return res;
92584ec227SDan Williams }
93584ec227SDan Williams 
94228c4f5cSDan Williams int system_has_dca_enabled(struct pci_dev *pdev)
95584ec227SDan Williams {
96584ec227SDan Williams 	if (boot_cpu_has(X86_FEATURE_DCA))
97584ec227SDan Williams 		return dca_enabled_in_bios(pdev);
98584ec227SDan Williams 
99e22dde99SDan Williams 	dev_dbg(&pdev->dev, "boot cpu doesn't have X86_FEATURE_DCA\n");
100584ec227SDan Williams 	return 0;
101584ec227SDan Williams }
102584ec227SDan Williams 
103584ec227SDan Williams struct ioat_dca_slot {
104584ec227SDan Williams 	struct pci_dev *pdev;	/* requester device */
105584ec227SDan Williams 	u16 rid;		/* requester id, as used by IOAT */
106584ec227SDan Williams };
107584ec227SDan Williams 
108584ec227SDan Williams #define IOAT_DCA_MAX_REQ 6
109584ec227SDan Williams #define IOAT3_DCA_MAX_REQ 2
110584ec227SDan Williams 
111584ec227SDan Williams struct ioat_dca_priv {
112584ec227SDan Williams 	void __iomem		*iobase;
113584ec227SDan Williams 	void __iomem		*dca_base;
114584ec227SDan Williams 	int			 max_requesters;
115584ec227SDan Williams 	int			 requester_count;
116584ec227SDan Williams 	u8			 tag_map[IOAT_TAG_MAP_LEN];
117584ec227SDan Williams 	struct ioat_dca_slot 	 req_slots[0];
118584ec227SDan Williams };
119584ec227SDan Williams 
120584ec227SDan Williams static int ioat_dca_dev_managed(struct dca_provider *dca,
121584ec227SDan Williams 				struct device *dev)
122584ec227SDan Williams {
123584ec227SDan Williams 	struct ioat_dca_priv *ioatdca = dca_priv(dca);
124584ec227SDan Williams 	struct pci_dev *pdev;
125584ec227SDan Williams 	int i;
126584ec227SDan Williams 
127584ec227SDan Williams 	pdev = to_pci_dev(dev);
128584ec227SDan Williams 	for (i = 0; i < ioatdca->max_requesters; i++) {
129584ec227SDan Williams 		if (ioatdca->req_slots[i].pdev == pdev)
130584ec227SDan Williams 			return 1;
131584ec227SDan Williams 	}
132584ec227SDan Williams 	return 0;
133584ec227SDan Williams }
134584ec227SDan Williams 
1353372de58SDave Jiang static int ioat_dca_add_requester(struct dca_provider *dca, struct device *dev)
136584ec227SDan Williams {
137584ec227SDan Williams 	struct ioat_dca_priv *ioatdca = dca_priv(dca);
138584ec227SDan Williams 	struct pci_dev *pdev;
139584ec227SDan Williams 	int i;
140584ec227SDan Williams 	u16 id;
141584ec227SDan Williams 	u16 global_req_table;
142584ec227SDan Williams 
143584ec227SDan Williams 	/* This implementation only supports PCI-Express */
1441fde2548SYijing Wang 	if (!dev_is_pci(dev))
145584ec227SDan Williams 		return -ENODEV;
146584ec227SDan Williams 	pdev = to_pci_dev(dev);
147584ec227SDan Williams 	id = dcaid_from_pcidev(pdev);
148584ec227SDan Williams 
149584ec227SDan Williams 	if (ioatdca->requester_count == ioatdca->max_requesters)
150584ec227SDan Williams 		return -ENODEV;
151584ec227SDan Williams 
152584ec227SDan Williams 	for (i = 0; i < ioatdca->max_requesters; i++) {
153584ec227SDan Williams 		if (ioatdca->req_slots[i].pdev == NULL) {
154584ec227SDan Williams 			/* found an empty slot */
155584ec227SDan Williams 			ioatdca->requester_count++;
156584ec227SDan Williams 			ioatdca->req_slots[i].pdev = pdev;
157584ec227SDan Williams 			ioatdca->req_slots[i].rid = id;
158584ec227SDan Williams 			global_req_table =
159584ec227SDan Williams 			      readw(ioatdca->dca_base + IOAT3_DCA_GREQID_OFFSET);
160584ec227SDan Williams 			writel(id | IOAT_DCA_GREQID_VALID,
161584ec227SDan Williams 			       ioatdca->iobase + global_req_table + (i * 4));
162584ec227SDan Williams 			return i;
163584ec227SDan Williams 		}
164584ec227SDan Williams 	}
165584ec227SDan Williams 	/* Error, ioatdma->requester_count is out of whack */
166584ec227SDan Williams 	return -EFAULT;
167584ec227SDan Williams }
168584ec227SDan Williams 
1693372de58SDave Jiang static int ioat_dca_remove_requester(struct dca_provider *dca,
170584ec227SDan Williams 				      struct device *dev)
171584ec227SDan Williams {
172584ec227SDan Williams 	struct ioat_dca_priv *ioatdca = dca_priv(dca);
173584ec227SDan Williams 	struct pci_dev *pdev;
174584ec227SDan Williams 	int i;
175584ec227SDan Williams 	u16 global_req_table;
176584ec227SDan Williams 
177584ec227SDan Williams 	/* This implementation only supports PCI-Express */
1781fde2548SYijing Wang 	if (!dev_is_pci(dev))
179584ec227SDan Williams 		return -ENODEV;
180584ec227SDan Williams 	pdev = to_pci_dev(dev);
181584ec227SDan Williams 
182584ec227SDan Williams 	for (i = 0; i < ioatdca->max_requesters; i++) {
183584ec227SDan Williams 		if (ioatdca->req_slots[i].pdev == pdev) {
184584ec227SDan Williams 			global_req_table =
185584ec227SDan Williams 			      readw(ioatdca->dca_base + IOAT3_DCA_GREQID_OFFSET);
186584ec227SDan Williams 			writel(0, ioatdca->iobase + global_req_table + (i * 4));
187584ec227SDan Williams 			ioatdca->req_slots[i].pdev = NULL;
188584ec227SDan Williams 			ioatdca->req_slots[i].rid = 0;
189584ec227SDan Williams 			ioatdca->requester_count--;
190584ec227SDan Williams 			return i;
191584ec227SDan Williams 		}
192584ec227SDan Williams 	}
193584ec227SDan Williams 	return -ENODEV;
194584ec227SDan Williams }
195584ec227SDan Williams 
1963372de58SDave Jiang static u8 ioat_dca_get_tag(struct dca_provider *dca,
197584ec227SDan Williams 			    struct device *dev,
198584ec227SDan Williams 			    int cpu)
199584ec227SDan Williams {
200584ec227SDan Williams 	u8 tag;
201584ec227SDan Williams 
202584ec227SDan Williams 	struct ioat_dca_priv *ioatdca = dca_priv(dca);
203584ec227SDan Williams 	int i, apic_id, bit, value;
204584ec227SDan Williams 	u8 entry;
205584ec227SDan Williams 
206584ec227SDan Williams 	tag = 0;
207584ec227SDan Williams 	apic_id = cpu_physical_id(cpu);
208584ec227SDan Williams 
209584ec227SDan Williams 	for (i = 0; i < IOAT_TAG_MAP_LEN; i++) {
210584ec227SDan Williams 		entry = ioatdca->tag_map[i];
211584ec227SDan Williams 		if (entry & DCA3_TAG_MAP_BIT_TO_SEL) {
212584ec227SDan Williams 			bit = entry &
213584ec227SDan Williams 				~(DCA3_TAG_MAP_BIT_TO_SEL | DCA3_TAG_MAP_BIT_TO_INV);
214584ec227SDan Williams 			value = (apic_id & (1 << bit)) ? 1 : 0;
215584ec227SDan Williams 		} else if (entry & DCA3_TAG_MAP_BIT_TO_INV) {
216584ec227SDan Williams 			bit = entry & ~DCA3_TAG_MAP_BIT_TO_INV;
217584ec227SDan Williams 			value = (apic_id & (1 << bit)) ? 0 : 1;
218584ec227SDan Williams 		} else {
219584ec227SDan Williams 			value = (entry & DCA3_TAG_MAP_LITERAL_VAL) ? 1 : 0;
220584ec227SDan Williams 		}
221584ec227SDan Williams 		tag |= (value << i);
222584ec227SDan Williams 	}
223584ec227SDan Williams 
224584ec227SDan Williams 	return tag;
225584ec227SDan Williams }
226584ec227SDan Williams 
227*2bb129ebSJulia Lawall static const struct dca_ops ioat_dca_ops = {
2283372de58SDave Jiang 	.add_requester		= ioat_dca_add_requester,
2293372de58SDave Jiang 	.remove_requester	= ioat_dca_remove_requester,
2303372de58SDave Jiang 	.get_tag		= ioat_dca_get_tag,
231584ec227SDan Williams 	.dev_managed		= ioat_dca_dev_managed,
232584ec227SDan Williams };
233584ec227SDan Williams 
2343372de58SDave Jiang static int ioat_dca_count_dca_slots(void *iobase, u16 dca_offset)
235584ec227SDan Williams {
236584ec227SDan Williams 	int slots = 0;
237584ec227SDan Williams 	u32 req;
238584ec227SDan Williams 	u16 global_req_table;
239584ec227SDan Williams 
240584ec227SDan Williams 	global_req_table = readw(iobase + dca_offset + IOAT3_DCA_GREQID_OFFSET);
241584ec227SDan Williams 	if (global_req_table == 0)
242584ec227SDan Williams 		return 0;
243584ec227SDan Williams 
244584ec227SDan Williams 	do {
245584ec227SDan Williams 		req = readl(iobase + global_req_table + (slots * sizeof(u32)));
246584ec227SDan Williams 		slots++;
247584ec227SDan Williams 	} while ((req & IOAT_DCA_GREQID_LASTID) == 0);
248584ec227SDan Williams 
249584ec227SDan Williams 	return slots;
250584ec227SDan Williams }
251584ec227SDan Williams 
25207bd34dbSAlexander Duyck static inline int dca3_tag_map_invalid(u8 *tag_map)
25307bd34dbSAlexander Duyck {
25407bd34dbSAlexander Duyck 	/*
25507bd34dbSAlexander Duyck 	 * If the tag map is not programmed by the BIOS the default is:
25607bd34dbSAlexander Duyck 	 * 0x80 0x80 0x80 0x80 0x80 0x00 0x00 0x00
25707bd34dbSAlexander Duyck 	 *
25807bd34dbSAlexander Duyck 	 * This an invalid map and will result in only 2 possible tags
25907bd34dbSAlexander Duyck 	 * 0x1F and 0x00.  0x00 is an invalid DCA tag so we know that
26007bd34dbSAlexander Duyck 	 * this entire definition is invalid.
26107bd34dbSAlexander Duyck 	 */
26207bd34dbSAlexander Duyck 	return ((tag_map[0] == DCA_TAG_MAP_VALID) &&
26307bd34dbSAlexander Duyck 		(tag_map[1] == DCA_TAG_MAP_VALID) &&
26407bd34dbSAlexander Duyck 		(tag_map[2] == DCA_TAG_MAP_VALID) &&
26507bd34dbSAlexander Duyck 		(tag_map[3] == DCA_TAG_MAP_VALID) &&
26607bd34dbSAlexander Duyck 		(tag_map[4] == DCA_TAG_MAP_VALID));
26707bd34dbSAlexander Duyck }
26807bd34dbSAlexander Duyck 
2693372de58SDave Jiang struct dca_provider *ioat_dca_init(struct pci_dev *pdev, void __iomem *iobase)
270584ec227SDan Williams {
271584ec227SDan Williams 	struct dca_provider *dca;
272584ec227SDan Williams 	struct ioat_dca_priv *ioatdca;
273584ec227SDan Williams 	int slots;
274584ec227SDan Williams 	int i;
275584ec227SDan Williams 	int err;
276584ec227SDan Williams 	u16 dca_offset;
277584ec227SDan Williams 	u16 csi_fsb_control;
278584ec227SDan Williams 	u16 pcie_control;
279584ec227SDan Williams 	u8 bit;
280584ec227SDan Williams 
281584ec227SDan Williams 	union {
282584ec227SDan Williams 		u64 full;
283584ec227SDan Williams 		struct {
284584ec227SDan Williams 			u32 low;
285584ec227SDan Williams 			u32 high;
286584ec227SDan Williams 		};
287584ec227SDan Williams 	} tag_map;
288584ec227SDan Williams 
289584ec227SDan Williams 	if (!system_has_dca_enabled(pdev))
290584ec227SDan Williams 		return NULL;
291584ec227SDan Williams 
292584ec227SDan Williams 	dca_offset = readw(iobase + IOAT_DCAOFFSET_OFFSET);
293584ec227SDan Williams 	if (dca_offset == 0)
294584ec227SDan Williams 		return NULL;
295584ec227SDan Williams 
2963372de58SDave Jiang 	slots = ioat_dca_count_dca_slots(iobase, dca_offset);
297584ec227SDan Williams 	if (slots == 0)
298584ec227SDan Williams 		return NULL;
299584ec227SDan Williams 
3003372de58SDave Jiang 	dca = alloc_dca_provider(&ioat_dca_ops,
301584ec227SDan Williams 				 sizeof(*ioatdca)
302584ec227SDan Williams 				      + (sizeof(struct ioat_dca_slot) * slots));
303584ec227SDan Williams 	if (!dca)
304584ec227SDan Williams 		return NULL;
305584ec227SDan Williams 
306584ec227SDan Williams 	ioatdca = dca_priv(dca);
307584ec227SDan Williams 	ioatdca->iobase = iobase;
308584ec227SDan Williams 	ioatdca->dca_base = iobase + dca_offset;
309584ec227SDan Williams 	ioatdca->max_requesters = slots;
310584ec227SDan Williams 
311584ec227SDan Williams 	/* some bios might not know to turn these on */
312584ec227SDan Williams 	csi_fsb_control = readw(ioatdca->dca_base + IOAT3_CSI_CONTROL_OFFSET);
313584ec227SDan Williams 	if ((csi_fsb_control & IOAT3_CSI_CONTROL_PREFETCH) == 0) {
314584ec227SDan Williams 		csi_fsb_control |= IOAT3_CSI_CONTROL_PREFETCH;
315584ec227SDan Williams 		writew(csi_fsb_control,
316584ec227SDan Williams 		       ioatdca->dca_base + IOAT3_CSI_CONTROL_OFFSET);
317584ec227SDan Williams 	}
318584ec227SDan Williams 	pcie_control = readw(ioatdca->dca_base + IOAT3_PCI_CONTROL_OFFSET);
319584ec227SDan Williams 	if ((pcie_control & IOAT3_PCI_CONTROL_MEMWR) == 0) {
320584ec227SDan Williams 		pcie_control |= IOAT3_PCI_CONTROL_MEMWR;
321584ec227SDan Williams 		writew(pcie_control,
322584ec227SDan Williams 		       ioatdca->dca_base + IOAT3_PCI_CONTROL_OFFSET);
323584ec227SDan Williams 	}
324584ec227SDan Williams 
325584ec227SDan Williams 
326584ec227SDan Williams 	/* TODO version, compatibility and configuration checks */
327584ec227SDan Williams 
328584ec227SDan Williams 	/* copy out the APIC to DCA tag map */
329584ec227SDan Williams 	tag_map.low =
330584ec227SDan Williams 		readl(ioatdca->dca_base + IOAT3_APICID_TAG_MAP_OFFSET_LOW);
331584ec227SDan Williams 	tag_map.high =
332584ec227SDan Williams 		readl(ioatdca->dca_base + IOAT3_APICID_TAG_MAP_OFFSET_HIGH);
333584ec227SDan Williams 	for (i = 0; i < 8; i++) {
334584ec227SDan Williams 		bit = tag_map.full >> (8 * i);
335584ec227SDan Williams 		ioatdca->tag_map[i] = bit & DCA_TAG_MAP_MASK;
336584ec227SDan Williams 	}
337584ec227SDan Williams 
33807bd34dbSAlexander Duyck 	if (dca3_tag_map_invalid(ioatdca->tag_map)) {
339f3c78f85SAlexander Duyck 		WARN_TAINT_ONCE(1, TAINT_FIRMWARE_WORKAROUND,
340f3c78f85SAlexander Duyck 				"%s %s: APICID_TAG_MAP set incorrectly by BIOS, disabling DCA\n",
341f3c78f85SAlexander Duyck 				dev_driver_string(&pdev->dev),
342f3c78f85SAlexander Duyck 				dev_name(&pdev->dev));
34307bd34dbSAlexander Duyck 		free_dca_provider(dca);
34407bd34dbSAlexander Duyck 		return NULL;
34507bd34dbSAlexander Duyck 	}
34607bd34dbSAlexander Duyck 
347584ec227SDan Williams 	err = register_dca_provider(dca, &pdev->dev);
348584ec227SDan Williams 	if (err) {
349584ec227SDan Williams 		free_dca_provider(dca);
350584ec227SDan Williams 		return NULL;
351584ec227SDan Williams 	}
352584ec227SDan Williams 
353584ec227SDan Williams 	return dca;
354584ec227SDan Williams }
355