xref: /openbmc/linux/drivers/misc/cxl/pci.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2f204e0b8SIan Munsie /*
3f204e0b8SIan Munsie  * Copyright 2014 IBM Corp.
4f204e0b8SIan Munsie  */
5f204e0b8SIan Munsie 
6f204e0b8SIan Munsie #include <linux/pci_regs.h>
7f204e0b8SIan Munsie #include <linux/pci_ids.h>
8f204e0b8SIan Munsie #include <linux/device.h>
9f204e0b8SIan Munsie #include <linux/module.h>
10f204e0b8SIan Munsie #include <linux/kernel.h>
11f204e0b8SIan Munsie #include <linux/slab.h>
12f204e0b8SIan Munsie #include <linux/sort.h>
13f204e0b8SIan Munsie #include <linux/pci.h>
14f204e0b8SIan Munsie #include <linux/of.h>
15f204e0b8SIan Munsie #include <linux/delay.h>
16f204e0b8SIan Munsie #include <asm/opal.h>
17f204e0b8SIan Munsie #include <asm/msi_bitmap.h>
18f204e0b8SIan Munsie #include <asm/pnv-pci.h>
1962fa19d4SRyan Grimm #include <asm/io.h>
20aa14138aSPhilippe Bergheaud #include <asm/reg.h>
21f204e0b8SIan Munsie 
22f204e0b8SIan Munsie #include "cxl.h"
239e8df8a2SDaniel Axtens #include <misc/cxl.h>
24f204e0b8SIan Munsie 
25f204e0b8SIan Munsie 
26f204e0b8SIan Munsie #define CXL_PCI_VSEC_ID	0x1280
27f204e0b8SIan Munsie #define CXL_VSEC_MIN_SIZE 0x80
28f204e0b8SIan Munsie 
29f204e0b8SIan Munsie #define CXL_READ_VSEC_LENGTH(dev, vsec, dest)			\
30f204e0b8SIan Munsie 	{							\
31f204e0b8SIan Munsie 		pci_read_config_word(dev, vsec + 0x6, dest);	\
32f204e0b8SIan Munsie 		*dest >>= 4;					\
33f204e0b8SIan Munsie 	}
34f204e0b8SIan Munsie #define CXL_READ_VSEC_NAFUS(dev, vsec, dest) \
35f204e0b8SIan Munsie 	pci_read_config_byte(dev, vsec + 0x8, dest)
36f204e0b8SIan Munsie 
37f204e0b8SIan Munsie #define CXL_READ_VSEC_STATUS(dev, vsec, dest) \
38f204e0b8SIan Munsie 	pci_read_config_byte(dev, vsec + 0x9, dest)
39f204e0b8SIan Munsie #define CXL_STATUS_SECOND_PORT  0x80
40f204e0b8SIan Munsie #define CXL_STATUS_MSI_X_FULL   0x40
41f204e0b8SIan Munsie #define CXL_STATUS_MSI_X_SINGLE 0x20
42f204e0b8SIan Munsie #define CXL_STATUS_FLASH_RW     0x08
43f204e0b8SIan Munsie #define CXL_STATUS_FLASH_RO     0x04
44f204e0b8SIan Munsie #define CXL_STATUS_LOADABLE_AFU 0x02
45f204e0b8SIan Munsie #define CXL_STATUS_LOADABLE_PSL 0x01
46f204e0b8SIan Munsie /* If we see these features we won't try to use the card */
47f204e0b8SIan Munsie #define CXL_UNSUPPORTED_FEATURES \
48f204e0b8SIan Munsie 	(CXL_STATUS_MSI_X_FULL | CXL_STATUS_MSI_X_SINGLE)
49f204e0b8SIan Munsie 
50f204e0b8SIan Munsie #define CXL_READ_VSEC_MODE_CONTROL(dev, vsec, dest) \
51f204e0b8SIan Munsie 	pci_read_config_byte(dev, vsec + 0xa, dest)
52f204e0b8SIan Munsie #define CXL_WRITE_VSEC_MODE_CONTROL(dev, vsec, val) \
53f204e0b8SIan Munsie 	pci_write_config_byte(dev, vsec + 0xa, val)
54f204e0b8SIan Munsie #define CXL_VSEC_PROTOCOL_MASK   0xe0
55f204e0b8SIan Munsie #define CXL_VSEC_PROTOCOL_1024TB 0x80
56f204e0b8SIan Munsie #define CXL_VSEC_PROTOCOL_512TB  0x40
57f24be42aSChristophe Lombard #define CXL_VSEC_PROTOCOL_256TB  0x20 /* Power 8/9 uses this */
58f204e0b8SIan Munsie #define CXL_VSEC_PROTOCOL_ENABLE 0x01
59f204e0b8SIan Munsie 
60f204e0b8SIan Munsie #define CXL_READ_VSEC_PSL_REVISION(dev, vsec, dest) \
61f204e0b8SIan Munsie 	pci_read_config_word(dev, vsec + 0xc, dest)
62f204e0b8SIan Munsie #define CXL_READ_VSEC_CAIA_MINOR(dev, vsec, dest) \
63f204e0b8SIan Munsie 	pci_read_config_byte(dev, vsec + 0xe, dest)
64f204e0b8SIan Munsie #define CXL_READ_VSEC_CAIA_MAJOR(dev, vsec, dest) \
65f204e0b8SIan Munsie 	pci_read_config_byte(dev, vsec + 0xf, dest)
66f204e0b8SIan Munsie #define CXL_READ_VSEC_BASE_IMAGE(dev, vsec, dest) \
67f204e0b8SIan Munsie 	pci_read_config_word(dev, vsec + 0x10, dest)
68f204e0b8SIan Munsie 
69f204e0b8SIan Munsie #define CXL_READ_VSEC_IMAGE_STATE(dev, vsec, dest) \
70f204e0b8SIan Munsie 	pci_read_config_byte(dev, vsec + 0x13, dest)
71f204e0b8SIan Munsie #define CXL_WRITE_VSEC_IMAGE_STATE(dev, vsec, val) \
72f204e0b8SIan Munsie 	pci_write_config_byte(dev, vsec + 0x13, val)
73f204e0b8SIan Munsie #define CXL_VSEC_USER_IMAGE_LOADED 0x80 /* RO */
74f204e0b8SIan Munsie #define CXL_VSEC_PERST_LOADS_IMAGE 0x20 /* RW */
75f204e0b8SIan Munsie #define CXL_VSEC_PERST_SELECT_USER 0x10 /* RW */
76f204e0b8SIan Munsie 
77f204e0b8SIan Munsie #define CXL_READ_VSEC_AFU_DESC_OFF(dev, vsec, dest) \
78f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x20, dest)
79f204e0b8SIan Munsie #define CXL_READ_VSEC_AFU_DESC_SIZE(dev, vsec, dest) \
80f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x24, dest)
81f204e0b8SIan Munsie #define CXL_READ_VSEC_PS_OFF(dev, vsec, dest) \
82f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x28, dest)
83f204e0b8SIan Munsie #define CXL_READ_VSEC_PS_SIZE(dev, vsec, dest) \
84f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x2c, dest)
85f204e0b8SIan Munsie 
86f204e0b8SIan Munsie 
87f204e0b8SIan Munsie /* This works a little different than the p1/p2 register accesses to make it
88f204e0b8SIan Munsie  * easier to pull out individual fields */
89cbffa3a5SChristophe Lombard #define AFUD_READ(afu, off)		in_be64(afu->native->afu_desc_mmio + off)
90cbffa3a5SChristophe Lombard #define AFUD_READ_LE(afu, off)		in_le64(afu->native->afu_desc_mmio + off)
91f204e0b8SIan Munsie #define EXTRACT_PPC_BIT(val, bit)	(!!(val & PPC_BIT(bit)))
92f204e0b8SIan Munsie #define EXTRACT_PPC_BITS(val, bs, be)	((val & PPC_BITMASK(bs, be)) >> PPC_BITLSHIFT(be))
93f204e0b8SIan Munsie 
94f204e0b8SIan Munsie #define AFUD_READ_INFO(afu)		AFUD_READ(afu, 0x0)
95f204e0b8SIan Munsie #define   AFUD_NUM_INTS_PER_PROC(val)	EXTRACT_PPC_BITS(val,  0, 15)
96f204e0b8SIan Munsie #define   AFUD_NUM_PROCS(val)		EXTRACT_PPC_BITS(val, 16, 31)
97f204e0b8SIan Munsie #define   AFUD_NUM_CRS(val)		EXTRACT_PPC_BITS(val, 32, 47)
98f204e0b8SIan Munsie #define   AFUD_MULTIMODE(val)		EXTRACT_PPC_BIT(val, 48)
99f204e0b8SIan Munsie #define   AFUD_PUSH_BLOCK_TRANSFER(val)	EXTRACT_PPC_BIT(val, 55)
100f204e0b8SIan Munsie #define   AFUD_DEDICATED_PROCESS(val)	EXTRACT_PPC_BIT(val, 59)
101f204e0b8SIan Munsie #define   AFUD_AFU_DIRECTED(val)	EXTRACT_PPC_BIT(val, 61)
102f204e0b8SIan Munsie #define   AFUD_TIME_SLICED(val)		EXTRACT_PPC_BIT(val, 63)
103f204e0b8SIan Munsie #define AFUD_READ_CR(afu)		AFUD_READ(afu, 0x20)
104f204e0b8SIan Munsie #define   AFUD_CR_LEN(val)		EXTRACT_PPC_BITS(val, 8, 63)
105f204e0b8SIan Munsie #define AFUD_READ_CR_OFF(afu)		AFUD_READ(afu, 0x28)
106f204e0b8SIan Munsie #define AFUD_READ_PPPSA(afu)		AFUD_READ(afu, 0x30)
107f204e0b8SIan Munsie #define   AFUD_PPPSA_PP(val)		EXTRACT_PPC_BIT(val, 6)
108f204e0b8SIan Munsie #define   AFUD_PPPSA_PSA(val)		EXTRACT_PPC_BIT(val, 7)
109f204e0b8SIan Munsie #define   AFUD_PPPSA_LEN(val)		EXTRACT_PPC_BITS(val, 8, 63)
110f204e0b8SIan Munsie #define AFUD_READ_PPPSA_OFF(afu)	AFUD_READ(afu, 0x38)
111f204e0b8SIan Munsie #define AFUD_READ_EB(afu)		AFUD_READ(afu, 0x40)
112f204e0b8SIan Munsie #define   AFUD_EB_LEN(val)		EXTRACT_PPC_BITS(val, 8, 63)
113f204e0b8SIan Munsie #define AFUD_READ_EB_OFF(afu)		AFUD_READ(afu, 0x48)
114f204e0b8SIan Munsie 
115f47f966fSVaishali Thakkar static const struct pci_device_id cxl_pci_tbl[] = {
116f204e0b8SIan Munsie 	{ PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x0477), },
117f204e0b8SIan Munsie 	{ PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x044b), },
118f204e0b8SIan Munsie 	{ PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x04cf), },
11968adb7bfSUma Krishnan 	{ PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x0601), },
12041e20d95SMatthew R. Ochs 	{ PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x0623), },
12141e20d95SMatthew R. Ochs 	{ PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x0628), },
122f204e0b8SIan Munsie 	{ }
123f204e0b8SIan Munsie };
124f204e0b8SIan Munsie MODULE_DEVICE_TABLE(pci, cxl_pci_tbl);
125f204e0b8SIan Munsie 
126f204e0b8SIan Munsie 
127f204e0b8SIan Munsie /*
128f204e0b8SIan Munsie  * Mostly using these wrappers to avoid confusion:
129f204e0b8SIan Munsie  * priv 1 is BAR2, while priv 2 is BAR0
130f204e0b8SIan Munsie  */
p1_base(struct pci_dev * dev)131f204e0b8SIan Munsie static inline resource_size_t p1_base(struct pci_dev *dev)
132f204e0b8SIan Munsie {
133f204e0b8SIan Munsie 	return pci_resource_start(dev, 2);
134f204e0b8SIan Munsie }
135f204e0b8SIan Munsie 
p1_size(struct pci_dev * dev)136f204e0b8SIan Munsie static inline resource_size_t p1_size(struct pci_dev *dev)
137f204e0b8SIan Munsie {
138f204e0b8SIan Munsie 	return pci_resource_len(dev, 2);
139f204e0b8SIan Munsie }
140f204e0b8SIan Munsie 
p2_base(struct pci_dev * dev)141f204e0b8SIan Munsie static inline resource_size_t p2_base(struct pci_dev *dev)
142f204e0b8SIan Munsie {
143f204e0b8SIan Munsie 	return pci_resource_start(dev, 0);
144f204e0b8SIan Munsie }
145f204e0b8SIan Munsie 
p2_size(struct pci_dev * dev)146f204e0b8SIan Munsie static inline resource_size_t p2_size(struct pci_dev *dev)
147f204e0b8SIan Munsie {
148f204e0b8SIan Munsie 	return pci_resource_len(dev, 0);
149f204e0b8SIan Munsie }
150f204e0b8SIan Munsie 
find_cxl_vsec(struct pci_dev * dev)151f204e0b8SIan Munsie static int find_cxl_vsec(struct pci_dev *dev)
152f204e0b8SIan Munsie {
153*0e1cd3d9SXiongfeng Wang 	return pci_find_vsec_capability(dev, PCI_VENDOR_ID_IBM, CXL_PCI_VSEC_ID);
154f204e0b8SIan Munsie }
155f204e0b8SIan Munsie 
dump_cxl_config_space(struct pci_dev * dev)156f204e0b8SIan Munsie static void dump_cxl_config_space(struct pci_dev *dev)
157f204e0b8SIan Munsie {
158f204e0b8SIan Munsie 	int vsec;
159f204e0b8SIan Munsie 	u32 val;
160f204e0b8SIan Munsie 
161f204e0b8SIan Munsie 	dev_info(&dev->dev, "dump_cxl_config_space\n");
162f204e0b8SIan Munsie 
163f204e0b8SIan Munsie 	pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &val);
164f204e0b8SIan Munsie 	dev_info(&dev->dev, "BAR0: %#.8x\n", val);
165f204e0b8SIan Munsie 	pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &val);
166f204e0b8SIan Munsie 	dev_info(&dev->dev, "BAR1: %#.8x\n", val);
167f204e0b8SIan Munsie 	pci_read_config_dword(dev, PCI_BASE_ADDRESS_2, &val);
168f204e0b8SIan Munsie 	dev_info(&dev->dev, "BAR2: %#.8x\n", val);
169f204e0b8SIan Munsie 	pci_read_config_dword(dev, PCI_BASE_ADDRESS_3, &val);
170f204e0b8SIan Munsie 	dev_info(&dev->dev, "BAR3: %#.8x\n", val);
171f204e0b8SIan Munsie 	pci_read_config_dword(dev, PCI_BASE_ADDRESS_4, &val);
172f204e0b8SIan Munsie 	dev_info(&dev->dev, "BAR4: %#.8x\n", val);
173f204e0b8SIan Munsie 	pci_read_config_dword(dev, PCI_BASE_ADDRESS_5, &val);
174f204e0b8SIan Munsie 	dev_info(&dev->dev, "BAR5: %#.8x\n", val);
175f204e0b8SIan Munsie 
176f204e0b8SIan Munsie 	dev_info(&dev->dev, "p1 regs: %#llx, len: %#llx\n",
177f204e0b8SIan Munsie 		p1_base(dev), p1_size(dev));
178f204e0b8SIan Munsie 	dev_info(&dev->dev, "p2 regs: %#llx, len: %#llx\n",
179f2931069SMichael Neuling 		p2_base(dev), p2_size(dev));
180f204e0b8SIan Munsie 	dev_info(&dev->dev, "BAR 4/5: %#llx, len: %#llx\n",
181f204e0b8SIan Munsie 		pci_resource_start(dev, 4), pci_resource_len(dev, 4));
182f204e0b8SIan Munsie 
183f204e0b8SIan Munsie 	if (!(vsec = find_cxl_vsec(dev)))
184f204e0b8SIan Munsie 		return;
185f204e0b8SIan Munsie 
186f204e0b8SIan Munsie #define show_reg(name, what) \
187f204e0b8SIan Munsie 	dev_info(&dev->dev, "cxl vsec: %30s: %#x\n", name, what)
188f204e0b8SIan Munsie 
189f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x0, &val);
190f204e0b8SIan Munsie 	show_reg("Cap ID", (val >> 0) & 0xffff);
191f204e0b8SIan Munsie 	show_reg("Cap Ver", (val >> 16) & 0xf);
192f204e0b8SIan Munsie 	show_reg("Next Cap Ptr", (val >> 20) & 0xfff);
193f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x4, &val);
194f204e0b8SIan Munsie 	show_reg("VSEC ID", (val >> 0) & 0xffff);
195f204e0b8SIan Munsie 	show_reg("VSEC Rev", (val >> 16) & 0xf);
196f204e0b8SIan Munsie 	show_reg("VSEC Length",	(val >> 20) & 0xfff);
197f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x8, &val);
198f204e0b8SIan Munsie 	show_reg("Num AFUs", (val >> 0) & 0xff);
199f204e0b8SIan Munsie 	show_reg("Status", (val >> 8) & 0xff);
200f204e0b8SIan Munsie 	show_reg("Mode Control", (val >> 16) & 0xff);
201f204e0b8SIan Munsie 	show_reg("Reserved", (val >> 24) & 0xff);
202f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0xc, &val);
203f204e0b8SIan Munsie 	show_reg("PSL Rev", (val >> 0) & 0xffff);
204f204e0b8SIan Munsie 	show_reg("CAIA Ver", (val >> 16) & 0xffff);
205f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x10, &val);
206f204e0b8SIan Munsie 	show_reg("Base Image Rev", (val >> 0) & 0xffff);
207f204e0b8SIan Munsie 	show_reg("Reserved", (val >> 16) & 0x0fff);
208f204e0b8SIan Munsie 	show_reg("Image Control", (val >> 28) & 0x3);
209f204e0b8SIan Munsie 	show_reg("Reserved", (val >> 30) & 0x1);
210f204e0b8SIan Munsie 	show_reg("Image Loaded", (val >> 31) & 0x1);
211f204e0b8SIan Munsie 
212f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x14, &val);
213f204e0b8SIan Munsie 	show_reg("Reserved", val);
214f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x18, &val);
215f204e0b8SIan Munsie 	show_reg("Reserved", val);
216f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x1c, &val);
217f204e0b8SIan Munsie 	show_reg("Reserved", val);
218f204e0b8SIan Munsie 
219f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x20, &val);
220f204e0b8SIan Munsie 	show_reg("AFU Descriptor Offset", val);
221f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x24, &val);
222f204e0b8SIan Munsie 	show_reg("AFU Descriptor Size", val);
223f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x28, &val);
224f204e0b8SIan Munsie 	show_reg("Problem State Offset", val);
225f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x2c, &val);
226f204e0b8SIan Munsie 	show_reg("Problem State Size", val);
227f204e0b8SIan Munsie 
228f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x30, &val);
229f204e0b8SIan Munsie 	show_reg("Reserved", val);
230f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x34, &val);
231f204e0b8SIan Munsie 	show_reg("Reserved", val);
232f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x38, &val);
233f204e0b8SIan Munsie 	show_reg("Reserved", val);
234f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x3c, &val);
235f204e0b8SIan Munsie 	show_reg("Reserved", val);
236f204e0b8SIan Munsie 
237f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x40, &val);
238f204e0b8SIan Munsie 	show_reg("PSL Programming Port", val);
239f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x44, &val);
240f204e0b8SIan Munsie 	show_reg("PSL Programming Control", val);
241f204e0b8SIan Munsie 
242f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x48, &val);
243f204e0b8SIan Munsie 	show_reg("Reserved", val);
244f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x4c, &val);
245f204e0b8SIan Munsie 	show_reg("Reserved", val);
246f204e0b8SIan Munsie 
247f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x50, &val);
248f204e0b8SIan Munsie 	show_reg("Flash Address Register", val);
249f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x54, &val);
250f204e0b8SIan Munsie 	show_reg("Flash Size Register", val);
251f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x58, &val);
252f204e0b8SIan Munsie 	show_reg("Flash Status/Control Register", val);
253f204e0b8SIan Munsie 	pci_read_config_dword(dev, vsec + 0x58, &val);
254f204e0b8SIan Munsie 	show_reg("Flash Data Port", val);
255f204e0b8SIan Munsie 
256f204e0b8SIan Munsie #undef show_reg
257f204e0b8SIan Munsie }
258f204e0b8SIan Munsie 
dump_afu_descriptor(struct cxl_afu * afu)259f204e0b8SIan Munsie static void dump_afu_descriptor(struct cxl_afu *afu)
260f204e0b8SIan Munsie {
261bfcdc8ffSMichael Neuling 	u64 val, afu_cr_num, afu_cr_off, afu_cr_len;
262bfcdc8ffSMichael Neuling 	int i;
263f204e0b8SIan Munsie 
264f204e0b8SIan Munsie #define show_reg(name, what) \
265f204e0b8SIan Munsie 	dev_info(&afu->dev, "afu desc: %30s: %#llx\n", name, what)
266f204e0b8SIan Munsie 
267f204e0b8SIan Munsie 	val = AFUD_READ_INFO(afu);
268f204e0b8SIan Munsie 	show_reg("num_ints_per_process", AFUD_NUM_INTS_PER_PROC(val));
269f204e0b8SIan Munsie 	show_reg("num_of_processes", AFUD_NUM_PROCS(val));
270f204e0b8SIan Munsie 	show_reg("num_of_afu_CRs", AFUD_NUM_CRS(val));
271f204e0b8SIan Munsie 	show_reg("req_prog_mode", val & 0xffffULL);
272bfcdc8ffSMichael Neuling 	afu_cr_num = AFUD_NUM_CRS(val);
273f204e0b8SIan Munsie 
274f204e0b8SIan Munsie 	val = AFUD_READ(afu, 0x8);
275f204e0b8SIan Munsie 	show_reg("Reserved", val);
276f204e0b8SIan Munsie 	val = AFUD_READ(afu, 0x10);
277f204e0b8SIan Munsie 	show_reg("Reserved", val);
278f204e0b8SIan Munsie 	val = AFUD_READ(afu, 0x18);
279f204e0b8SIan Munsie 	show_reg("Reserved", val);
280f204e0b8SIan Munsie 
281f204e0b8SIan Munsie 	val = AFUD_READ_CR(afu);
282f204e0b8SIan Munsie 	show_reg("Reserved", (val >> (63-7)) & 0xff);
283f204e0b8SIan Munsie 	show_reg("AFU_CR_len", AFUD_CR_LEN(val));
284bfcdc8ffSMichael Neuling 	afu_cr_len = AFUD_CR_LEN(val) * 256;
285f204e0b8SIan Munsie 
286f204e0b8SIan Munsie 	val = AFUD_READ_CR_OFF(afu);
287bfcdc8ffSMichael Neuling 	afu_cr_off = val;
288f204e0b8SIan Munsie 	show_reg("AFU_CR_offset", val);
289f204e0b8SIan Munsie 
290f204e0b8SIan Munsie 	val = AFUD_READ_PPPSA(afu);
291f204e0b8SIan Munsie 	show_reg("PerProcessPSA_control", (val >> (63-7)) & 0xff);
292f204e0b8SIan Munsie 	show_reg("PerProcessPSA Length", AFUD_PPPSA_LEN(val));
293f204e0b8SIan Munsie 
294f204e0b8SIan Munsie 	val = AFUD_READ_PPPSA_OFF(afu);
295f204e0b8SIan Munsie 	show_reg("PerProcessPSA_offset", val);
296f204e0b8SIan Munsie 
297f204e0b8SIan Munsie 	val = AFUD_READ_EB(afu);
298f204e0b8SIan Munsie 	show_reg("Reserved", (val >> (63-7)) & 0xff);
299f204e0b8SIan Munsie 	show_reg("AFU_EB_len", AFUD_EB_LEN(val));
300f204e0b8SIan Munsie 
301f204e0b8SIan Munsie 	val = AFUD_READ_EB_OFF(afu);
302f204e0b8SIan Munsie 	show_reg("AFU_EB_offset", val);
303f204e0b8SIan Munsie 
304bfcdc8ffSMichael Neuling 	for (i = 0; i < afu_cr_num; i++) {
305bfcdc8ffSMichael Neuling 		val = AFUD_READ_LE(afu, afu_cr_off + i * afu_cr_len);
306bfcdc8ffSMichael Neuling 		show_reg("CR Vendor", val & 0xffff);
307bfcdc8ffSMichael Neuling 		show_reg("CR Device", (val >> 16) & 0xffff);
308bfcdc8ffSMichael Neuling 	}
309f204e0b8SIan Munsie #undef show_reg
310f204e0b8SIan Munsie }
311f204e0b8SIan Munsie 
312abd1d99bSChristophe Lombard #define P8_CAPP_UNIT0_ID 0xBA
313abd1d99bSChristophe Lombard #define P8_CAPP_UNIT1_ID 0XBE
314f24be42aSChristophe Lombard #define P9_CAPP_UNIT0_ID 0xC0
315f24be42aSChristophe Lombard #define P9_CAPP_UNIT1_ID 0xE0
316aa14138aSPhilippe Bergheaud 
get_phb_index(struct device_node * np,u32 * phb_index)317f24be42aSChristophe Lombard static int get_phb_index(struct device_node *np, u32 *phb_index)
318aa14138aSPhilippe Bergheaud {
319f24be42aSChristophe Lombard 	if (of_property_read_u32(np, "ibm,phb-index", phb_index))
320f24be42aSChristophe Lombard 		return -ENODEV;
321aa14138aSPhilippe Bergheaud 	return 0;
322f24be42aSChristophe Lombard }
323aa14138aSPhilippe Bergheaud 
get_capp_unit_id(struct device_node * np,u32 phb_index)324f24be42aSChristophe Lombard static u64 get_capp_unit_id(struct device_node *np, u32 phb_index)
325f24be42aSChristophe Lombard {
326aa14138aSPhilippe Bergheaud 	/*
327abd1d99bSChristophe Lombard 	 * POWER 8:
328abd1d99bSChristophe Lombard 	 *  - For chips other than POWER8NVL, we only have CAPP 0,
329aa14138aSPhilippe Bergheaud 	 *    irrespective of which PHB is used.
330abd1d99bSChristophe Lombard 	 *  - For POWER8NVL, assume CAPP 0 is attached to PHB0 and
331aa14138aSPhilippe Bergheaud 	 *    CAPP 1 is attached to PHB1.
332aa14138aSPhilippe Bergheaud 	 */
333abd1d99bSChristophe Lombard 	if (cxl_is_power8()) {
334abd1d99bSChristophe Lombard 		if (!pvr_version_is(PVR_POWER8NVL))
335abd1d99bSChristophe Lombard 			return P8_CAPP_UNIT0_ID;
336aa14138aSPhilippe Bergheaud 
337aa14138aSPhilippe Bergheaud 		if (phb_index == 0)
338abd1d99bSChristophe Lombard 			return P8_CAPP_UNIT0_ID;
339aa14138aSPhilippe Bergheaud 
340aa14138aSPhilippe Bergheaud 		if (phb_index == 1)
341abd1d99bSChristophe Lombard 			return P8_CAPP_UNIT1_ID;
342abd1d99bSChristophe Lombard 	}
343aa14138aSPhilippe Bergheaud 
344f24be42aSChristophe Lombard 	/*
345f24be42aSChristophe Lombard 	 * POWER 9:
346f24be42aSChristophe Lombard 	 *   PEC0 (PHB0). Capp ID = CAPP0 (0b1100_0000)
347f24be42aSChristophe Lombard 	 *   PEC1 (PHB1 - PHB2). No capi mode
348f24be42aSChristophe Lombard 	 *   PEC2 (PHB3 - PHB4 - PHB5): Capi mode on PHB3 only. Capp ID = CAPP1 (0b1110_0000)
349f24be42aSChristophe Lombard 	 */
350f24be42aSChristophe Lombard 	if (cxl_is_power9()) {
351f24be42aSChristophe Lombard 		if (phb_index == 0)
352f24be42aSChristophe Lombard 			return P9_CAPP_UNIT0_ID;
353f24be42aSChristophe Lombard 
354f24be42aSChristophe Lombard 		if (phb_index == 3)
355f24be42aSChristophe Lombard 			return P9_CAPP_UNIT1_ID;
356f24be42aSChristophe Lombard 	}
357aa14138aSPhilippe Bergheaud 
358aa14138aSPhilippe Bergheaud 	return 0;
359aa14138aSPhilippe Bergheaud }
360aa14138aSPhilippe Bergheaud 
cxl_calc_capp_routing(struct pci_dev * dev,u64 * chipid,u32 * phb_index,u64 * capp_unit_id)3613ced8d73SChristophe Lombard int cxl_calc_capp_routing(struct pci_dev *dev, u64 *chipid,
362f24be42aSChristophe Lombard 			     u32 *phb_index, u64 *capp_unit_id)
363f204e0b8SIan Munsie {
364f24be42aSChristophe Lombard 	int rc;
365f204e0b8SIan Munsie 	struct device_node *np;
366f204e0b8SIan Munsie 	const __be32 *prop;
367f204e0b8SIan Munsie 
3686f963ec2SRyan Grimm 	if (!(np = pnv_pci_get_phb_node(dev)))
369f204e0b8SIan Munsie 		return -ENODEV;
370f204e0b8SIan Munsie 
371f204e0b8SIan Munsie 	while (np && !(prop = of_get_property(np, "ibm,chip-id", NULL)))
372f204e0b8SIan Munsie 		np = of_get_next_parent(np);
373f204e0b8SIan Munsie 	if (!np)
374f204e0b8SIan Munsie 		return -ENODEV;
375f24be42aSChristophe Lombard 
3766d382616SFrederic Barrat 	*chipid = be32_to_cpup(prop);
377f24be42aSChristophe Lombard 
378f24be42aSChristophe Lombard 	rc = get_phb_index(np, phb_index);
379f24be42aSChristophe Lombard 	if (rc) {
380f24be42aSChristophe Lombard 		pr_err("cxl: invalid phb index\n");
3811d09697fSMiaoqian Lin 		of_node_put(np);
382f24be42aSChristophe Lombard 		return rc;
383f24be42aSChristophe Lombard 	}
384f24be42aSChristophe Lombard 
385f24be42aSChristophe Lombard 	*capp_unit_id = get_capp_unit_id(np, *phb_index);
386f204e0b8SIan Munsie 	of_node_put(np);
3876d382616SFrederic Barrat 	if (!*capp_unit_id) {
38840ac790dSFrederic Barrat 		pr_err("cxl: No capp unit found for PHB[%lld,%d]. Make sure the adapter is on a capi-compatible slot\n",
38940ac790dSFrederic Barrat 		       *chipid, *phb_index);
390aa14138aSPhilippe Bergheaud 		return -ENODEV;
391aa14138aSPhilippe Bergheaud 	}
392f204e0b8SIan Munsie 
3936d382616SFrederic Barrat 	return 0;
3946d382616SFrederic Barrat }
3956d382616SFrederic Barrat 
3969dbcbfa1SPhilippe Bergheaud static DEFINE_MUTEX(indications_mutex);
3979dbcbfa1SPhilippe Bergheaud 
get_phb_indications(struct pci_dev * dev,u64 * capiind,u64 * asnind,u64 * nbwind)3989dbcbfa1SPhilippe Bergheaud static int get_phb_indications(struct pci_dev *dev, u64 *capiind, u64 *asnind,
3999dbcbfa1SPhilippe Bergheaud 			       u64 *nbwind)
4009dbcbfa1SPhilippe Bergheaud {
4019dbcbfa1SPhilippe Bergheaud 	static u64 nbw, asn, capi = 0;
4029dbcbfa1SPhilippe Bergheaud 	struct device_node *np;
4039dbcbfa1SPhilippe Bergheaud 	const __be32 *prop;
4049dbcbfa1SPhilippe Bergheaud 
4059dbcbfa1SPhilippe Bergheaud 	mutex_lock(&indications_mutex);
4069dbcbfa1SPhilippe Bergheaud 	if (!capi) {
4079dbcbfa1SPhilippe Bergheaud 		if (!(np = pnv_pci_get_phb_node(dev))) {
4089dbcbfa1SPhilippe Bergheaud 			mutex_unlock(&indications_mutex);
4099dbcbfa1SPhilippe Bergheaud 			return -ENODEV;
4109dbcbfa1SPhilippe Bergheaud 		}
4119dbcbfa1SPhilippe Bergheaud 
4129dbcbfa1SPhilippe Bergheaud 		prop = of_get_property(np, "ibm,phb-indications", NULL);
4139dbcbfa1SPhilippe Bergheaud 		if (!prop) {
4149dbcbfa1SPhilippe Bergheaud 			nbw = 0x0300UL; /* legacy values */
4159dbcbfa1SPhilippe Bergheaud 			asn = 0x0400UL;
4169dbcbfa1SPhilippe Bergheaud 			capi = 0x0200UL;
4179dbcbfa1SPhilippe Bergheaud 		} else {
4189dbcbfa1SPhilippe Bergheaud 			nbw = (u64)be32_to_cpu(prop[2]);
4199dbcbfa1SPhilippe Bergheaud 			asn = (u64)be32_to_cpu(prop[1]);
4209dbcbfa1SPhilippe Bergheaud 			capi = (u64)be32_to_cpu(prop[0]);
4219dbcbfa1SPhilippe Bergheaud 		}
4229dbcbfa1SPhilippe Bergheaud 		of_node_put(np);
4239dbcbfa1SPhilippe Bergheaud 	}
4249dbcbfa1SPhilippe Bergheaud 	*capiind = capi;
4259dbcbfa1SPhilippe Bergheaud 	*asnind = asn;
4269dbcbfa1SPhilippe Bergheaud 	*nbwind = nbw;
4279dbcbfa1SPhilippe Bergheaud 	mutex_unlock(&indications_mutex);
4289dbcbfa1SPhilippe Bergheaud 	return 0;
4299dbcbfa1SPhilippe Bergheaud }
4309dbcbfa1SPhilippe Bergheaud 
cxl_get_xsl9_dsnctl(struct pci_dev * dev,u64 capp_unit_id,u64 * reg)4319dbcbfa1SPhilippe Bergheaud int cxl_get_xsl9_dsnctl(struct pci_dev *dev, u64 capp_unit_id, u64 *reg)
4326d382616SFrederic Barrat {
4333ced8d73SChristophe Lombard 	u64 xsl_dsnctl;
4349dbcbfa1SPhilippe Bergheaud 	u64 capiind, asnind, nbwind;
435f24be42aSChristophe Lombard 
436f24be42aSChristophe Lombard 	/*
437f24be42aSChristophe Lombard 	 * CAPI Identifier bits [0:7]
438f24be42aSChristophe Lombard 	 * bit 61:60 MSI bits --> 0
439f24be42aSChristophe Lombard 	 * bit 59 TVT selector --> 0
440f24be42aSChristophe Lombard 	 */
4419dbcbfa1SPhilippe Bergheaud 	if (get_phb_indications(dev, &capiind, &asnind, &nbwind))
4429dbcbfa1SPhilippe Bergheaud 		return -ENODEV;
443f24be42aSChristophe Lombard 
444f24be42aSChristophe Lombard 	/*
445f24be42aSChristophe Lombard 	 * Tell XSL where to route data to.
446f24be42aSChristophe Lombard 	 * The field chipid should match the PHB CAPI_CMPM register
447f24be42aSChristophe Lombard 	 */
4489dbcbfa1SPhilippe Bergheaud 	xsl_dsnctl = (capiind << (63-15)); /* Bit 57 */
449f24be42aSChristophe Lombard 	xsl_dsnctl |= (capp_unit_id << (63-15));
450f24be42aSChristophe Lombard 
451f24be42aSChristophe Lombard 	/* nMMU_ID Defaults to: b’000001001’*/
452f24be42aSChristophe Lombard 	xsl_dsnctl |= ((u64)0x09 << (63-28));
453f24be42aSChristophe Lombard 
454f24be42aSChristophe Lombard 	/*
455f24be42aSChristophe Lombard 	 * Used to identify CAPI packets which should be sorted into
456f24be42aSChristophe Lombard 	 * the Non-Blocking queues by the PHB. This field should match
457f24be42aSChristophe Lombard 	 * the PHB PBL_NBW_CMPM register
458f24be42aSChristophe Lombard 	 * nbwind=0x03, bits [57:58], must include capi indicator.
459f24be42aSChristophe Lombard 	 * Not supported on P9 DD1.
460f24be42aSChristophe Lombard 	 */
4619dbcbfa1SPhilippe Bergheaud 	xsl_dsnctl |= (nbwind << (63-55));
462f24be42aSChristophe Lombard 
463f24be42aSChristophe Lombard 	/*
464f24be42aSChristophe Lombard 	 * Upper 16b address bits of ASB_Notify messages sent to the
465f24be42aSChristophe Lombard 	 * system. Need to match the PHB’s ASN Compare/Mask Register.
466f24be42aSChristophe Lombard 	 * Not supported on P9 DD1.
467f24be42aSChristophe Lombard 	 */
4689dbcbfa1SPhilippe Bergheaud 	xsl_dsnctl |= asnind;
469f24be42aSChristophe Lombard 
4703ced8d73SChristophe Lombard 	*reg = xsl_dsnctl;
4713ced8d73SChristophe Lombard 	return 0;
4723ced8d73SChristophe Lombard }
4733ced8d73SChristophe Lombard 
init_implementation_adapter_regs_psl9(struct cxl * adapter,struct pci_dev * dev)4743ced8d73SChristophe Lombard static int init_implementation_adapter_regs_psl9(struct cxl *adapter,
4753ced8d73SChristophe Lombard 						 struct pci_dev *dev)
4763ced8d73SChristophe Lombard {
4773ced8d73SChristophe Lombard 	u64 xsl_dsnctl, psl_fircntl;
4783ced8d73SChristophe Lombard 	u64 chipid;
4793ced8d73SChristophe Lombard 	u32 phb_index;
4803ced8d73SChristophe Lombard 	u64 capp_unit_id;
48194322ed8SVaibhav Jain 	u64 psl_debug;
4823ced8d73SChristophe Lombard 	int rc;
4833ced8d73SChristophe Lombard 
4843ced8d73SChristophe Lombard 	rc = cxl_calc_capp_routing(dev, &chipid, &phb_index, &capp_unit_id);
4853ced8d73SChristophe Lombard 	if (rc)
4863ced8d73SChristophe Lombard 		return rc;
4873ced8d73SChristophe Lombard 
4889dbcbfa1SPhilippe Bergheaud 	rc = cxl_get_xsl9_dsnctl(dev, capp_unit_id, &xsl_dsnctl);
4893ced8d73SChristophe Lombard 	if (rc)
4903ced8d73SChristophe Lombard 		return rc;
4913ced8d73SChristophe Lombard 
492f24be42aSChristophe Lombard 	cxl_p1_write(adapter, CXL_XSL9_DSNCTL, xsl_dsnctl);
493f24be42aSChristophe Lombard 
494f24be42aSChristophe Lombard 	/* Set fir_cntl to recommended value for production env */
495f24be42aSChristophe Lombard 	psl_fircntl = (0x2ULL << (63-3)); /* ce_report */
496f24be42aSChristophe Lombard 	psl_fircntl |= (0x1ULL << (63-6)); /* FIR_report */
497f24be42aSChristophe Lombard 	psl_fircntl |= 0x1ULL; /* ce_thresh */
498f24be42aSChristophe Lombard 	cxl_p1_write(adapter, CXL_PSL9_FIR_CNTL, psl_fircntl);
499f24be42aSChristophe Lombard 
50056328743SChristophe Lombard 	/* Setup the PSL to transmit packets on the PCIe before the
5019a6d2022SVaibhav Jain 	 * CAPP is enabled. Make sure that CAPP virtual machines are disabled
502f24be42aSChristophe Lombard 	 */
5039a6d2022SVaibhav Jain 	cxl_p1_write(adapter, CXL_PSL9_DSNDCTL, 0x0001001000012A10ULL);
504f24be42aSChristophe Lombard 
505f24be42aSChristophe Lombard 	/*
506f24be42aSChristophe Lombard 	 * A response to an ASB_Notify request is returned by the
507f24be42aSChristophe Lombard 	 * system as an MMIO write to the address defined in
50856328743SChristophe Lombard 	 * the PSL_TNR_ADDR register.
50956328743SChristophe Lombard 	 * keep the Reset Value: 0x00020000E0000000
510f24be42aSChristophe Lombard 	 */
511f24be42aSChristophe Lombard 
51256328743SChristophe Lombard 	/* Enable XSL rty limit */
51356328743SChristophe Lombard 	cxl_p1_write(adapter, CXL_XSL9_DEF, 0x51F8000000000005ULL);
514f24be42aSChristophe Lombard 
51556328743SChristophe Lombard 	/* Change XSL_INV dummy read threshold */
51656328743SChristophe Lombard 	cxl_p1_write(adapter, CXL_XSL9_INV, 0x0000040007FFC200ULL);
517f24be42aSChristophe Lombard 
51856328743SChristophe Lombard 	if (phb_index == 3) {
51956328743SChristophe Lombard 		/* disable machines 31-47 and 20-27 for DMA */
52056328743SChristophe Lombard 		cxl_p1_write(adapter, CXL_PSL9_APCDEDTYPE, 0x40000FF3FFFF0000ULL);
52156328743SChristophe Lombard 	}
52256328743SChristophe Lombard 
52356328743SChristophe Lombard 	/* Snoop machines */
52456328743SChristophe Lombard 	cxl_p1_write(adapter, CXL_PSL9_APCDEDALLOC, 0x800F000200000000ULL);
52556328743SChristophe Lombard 
52603ebb419SVaibhav Jain 	/* Enable NORST and DD2 features */
52703ebb419SVaibhav Jain 	cxl_p1_write(adapter, CXL_PSL9_DEBUG, 0xC000000000000000ULL);
528f24be42aSChristophe Lombard 
52994322ed8SVaibhav Jain 	/*
53094322ed8SVaibhav Jain 	 * Check if PSL has data-cache. We need to flush adapter datacache
53194322ed8SVaibhav Jain 	 * when as its about to be removed.
53294322ed8SVaibhav Jain 	 */
53394322ed8SVaibhav Jain 	psl_debug = cxl_p1_read(adapter, CXL_PSL9_DEBUG);
53494322ed8SVaibhav Jain 	if (psl_debug & CXL_PSL_DEBUG_CDC) {
53594322ed8SVaibhav Jain 		dev_dbg(&dev->dev, "No data-cache present\n");
53694322ed8SVaibhav Jain 		adapter->native->no_data_cache = true;
53794322ed8SVaibhav Jain 	}
53894322ed8SVaibhav Jain 
539f24be42aSChristophe Lombard 	return 0;
540f24be42aSChristophe Lombard }
541f24be42aSChristophe Lombard 
init_implementation_adapter_regs_psl8(struct cxl * adapter,struct pci_dev * dev)54264663f37SChristophe Lombard static int init_implementation_adapter_regs_psl8(struct cxl *adapter, struct pci_dev *dev)
5436d382616SFrederic Barrat {
544c6d2ee09SFrederic Barrat 	u64 psl_dsnctl, psl_fircntl;
5456d382616SFrederic Barrat 	u64 chipid;
546f24be42aSChristophe Lombard 	u32 phb_index;
5476d382616SFrederic Barrat 	u64 capp_unit_id;
5486d382616SFrederic Barrat 	int rc;
5496d382616SFrederic Barrat 
5503ced8d73SChristophe Lombard 	rc = cxl_calc_capp_routing(dev, &chipid, &phb_index, &capp_unit_id);
5516d382616SFrederic Barrat 	if (rc)
5526d382616SFrederic Barrat 		return rc;
5536d382616SFrederic Barrat 
5544aec6ec0SFrederic Barrat 	psl_dsnctl = 0x0000900000000000ULL; /* pteupd ttype, scdone */
5554aec6ec0SFrederic Barrat 	psl_dsnctl |= (0x2ULL << (63-38)); /* MMIO hang pulse: 256 us */
556f204e0b8SIan Munsie 	/* Tell PSL where to route data to */
5574aec6ec0SFrederic Barrat 	psl_dsnctl |= (chipid << (63-5));
558aa14138aSPhilippe Bergheaud 	psl_dsnctl |= (capp_unit_id << (63-13));
559aa14138aSPhilippe Bergheaud 
560f204e0b8SIan Munsie 	cxl_p1_write(adapter, CXL_PSL_DSNDCTL, psl_dsnctl);
561f204e0b8SIan Munsie 	cxl_p1_write(adapter, CXL_PSL_RESLCKTO, 0x20000000200ULL);
562f204e0b8SIan Munsie 	/* snoop write mask */
563f204e0b8SIan Munsie 	cxl_p1_write(adapter, CXL_PSL_SNWRALLOC, 0x00000000FFFFFFFFULL);
564c6d2ee09SFrederic Barrat 	/* set fir_cntl to recommended value for production env */
565c6d2ee09SFrederic Barrat 	psl_fircntl = (0x2ULL << (63-3)); /* ce_report */
566c6d2ee09SFrederic Barrat 	psl_fircntl |= (0x1ULL << (63-6)); /* FIR_report */
567c6d2ee09SFrederic Barrat 	psl_fircntl |= 0x1ULL; /* ce_thresh */
568c6d2ee09SFrederic Barrat 	cxl_p1_write(adapter, CXL_PSL_FIR_CNTL, psl_fircntl);
569f204e0b8SIan Munsie 	/* for debugging with trace arrays */
570f204e0b8SIan Munsie 	cxl_p1_write(adapter, CXL_PSL_TRACE, 0x0000FF7C00000000ULL);
571f204e0b8SIan Munsie 
572f204e0b8SIan Munsie 	return 0;
573f204e0b8SIan Munsie }
574f204e0b8SIan Munsie 
575f3988ca4SFrederic Barrat /* PSL */
5766d382616SFrederic Barrat #define TBSYNC_CAL(n) (((u64)n & 0x7) << (63-3))
577390fd592SPhilippe Bergheaud #define TBSYNC_CNT(n) (((u64)n & 0x7) << (63-6))
5786d382616SFrederic Barrat /* For the PSL this is a multiple for 0 < n <= 7: */
5796d382616SFrederic Barrat #define PSL_2048_250MHZ_CYCLES 1
5806d382616SFrederic Barrat 
write_timebase_ctrl_psl8(struct cxl * adapter)58164663f37SChristophe Lombard static void write_timebase_ctrl_psl8(struct cxl *adapter)
5826d382616SFrederic Barrat {
5836d382616SFrederic Barrat 	cxl_p1_write(adapter, CXL_PSL_TB_CTLSTAT,
5846d382616SFrederic Barrat 		     TBSYNC_CNT(2 * PSL_2048_250MHZ_CYCLES));
5856d382616SFrederic Barrat }
5866d382616SFrederic Barrat 
timebase_read_psl9(struct cxl * adapter)587f24be42aSChristophe Lombard static u64 timebase_read_psl9(struct cxl *adapter)
588f24be42aSChristophe Lombard {
589f24be42aSChristophe Lombard 	return cxl_p1_read(adapter, CXL_PSL9_Timebase);
590f24be42aSChristophe Lombard }
591f24be42aSChristophe Lombard 
timebase_read_psl8(struct cxl * adapter)59264663f37SChristophe Lombard static u64 timebase_read_psl8(struct cxl *adapter)
5936d382616SFrederic Barrat {
5946d382616SFrederic Barrat 	return cxl_p1_read(adapter, CXL_PSL_Timebase);
5956d382616SFrederic Barrat }
5966d382616SFrederic Barrat 
cxl_setup_psl_timebase(struct cxl * adapter,struct pci_dev * dev)597e009a7e8SFrederic Barrat static void cxl_setup_psl_timebase(struct cxl *adapter, struct pci_dev *dev)
598390fd592SPhilippe Bergheaud {
599390fd592SPhilippe Bergheaud 	struct device_node *np;
600390fd592SPhilippe Bergheaud 
601e009a7e8SFrederic Barrat 	adapter->psl_timebase_synced = false;
602e009a7e8SFrederic Barrat 
603390fd592SPhilippe Bergheaud 	if (!(np = pnv_pci_get_phb_node(dev)))
604e009a7e8SFrederic Barrat 		return;
605390fd592SPhilippe Bergheaud 
606390fd592SPhilippe Bergheaud 	/* Do not fail when CAPP timebase sync is not supported by OPAL */
607390fd592SPhilippe Bergheaud 	of_node_get(np);
608390fd592SPhilippe Bergheaud 	if (! of_get_property(np, "ibm,capp-timebase-sync", NULL)) {
609390fd592SPhilippe Bergheaud 		of_node_put(np);
610e009a7e8SFrederic Barrat 		dev_info(&dev->dev, "PSL timebase inactive: OPAL support missing\n");
611e009a7e8SFrederic Barrat 		return;
612390fd592SPhilippe Bergheaud 	}
613390fd592SPhilippe Bergheaud 	of_node_put(np);
614390fd592SPhilippe Bergheaud 
615390fd592SPhilippe Bergheaud 	/*
616390fd592SPhilippe Bergheaud 	 * Setup PSL Timebase Control and Status register
617390fd592SPhilippe Bergheaud 	 * with the recommended Timebase Sync Count value
618390fd592SPhilippe Bergheaud 	 */
61902b63b42SVaibhav Jain 	if (adapter->native->sl_ops->write_timebase_ctrl)
6206d382616SFrederic Barrat 		adapter->native->sl_ops->write_timebase_ctrl(adapter);
621390fd592SPhilippe Bergheaud 
622390fd592SPhilippe Bergheaud 	/* Enable PSL Timebase */
623390fd592SPhilippe Bergheaud 	cxl_p1_write(adapter, CXL_PSL_Control, 0x0000000000000000);
624390fd592SPhilippe Bergheaud 	cxl_p1_write(adapter, CXL_PSL_Control, CXL_PSL_Control_tb);
625390fd592SPhilippe Bergheaud 
626e009a7e8SFrederic Barrat 	return;
627390fd592SPhilippe Bergheaud }
628390fd592SPhilippe Bergheaud 
init_implementation_afu_regs_psl9(struct cxl_afu * afu)629f24be42aSChristophe Lombard static int init_implementation_afu_regs_psl9(struct cxl_afu *afu)
630f24be42aSChristophe Lombard {
631f24be42aSChristophe Lombard 	return 0;
632f24be42aSChristophe Lombard }
633f24be42aSChristophe Lombard 
init_implementation_afu_regs_psl8(struct cxl_afu * afu)63464663f37SChristophe Lombard static int init_implementation_afu_regs_psl8(struct cxl_afu *afu)
635f204e0b8SIan Munsie {
636f204e0b8SIan Munsie 	/* read/write masks for this slice */
637f204e0b8SIan Munsie 	cxl_p1n_write(afu, CXL_PSL_APCALLOC_A, 0xFFFFFFFEFEFEFEFEULL);
638f204e0b8SIan Munsie 	/* APC read/write masks for this slice */
639f204e0b8SIan Munsie 	cxl_p1n_write(afu, CXL_PSL_COALLOC_A, 0xFF000000FEFEFEFEULL);
640f204e0b8SIan Munsie 	/* for debugging with trace arrays */
641f204e0b8SIan Munsie 	cxl_p1n_write(afu, CXL_PSL_SLICE_TRACE, 0x0000FFFF00000000ULL);
642d6a6af2cSIan Munsie 	cxl_p1n_write(afu, CXL_PSL_RXCTL_A, CXL_PSL_RXCTL_AFUHP_4S);
643f204e0b8SIan Munsie 
644f204e0b8SIan Munsie 	return 0;
645f204e0b8SIan Munsie }
646f204e0b8SIan Munsie 
cxl_pci_setup_irq(struct cxl * adapter,unsigned int hwirq,unsigned int virq)6472b04cf31SFrederic Barrat int cxl_pci_setup_irq(struct cxl *adapter, unsigned int hwirq,
648f204e0b8SIan Munsie 		unsigned int virq)
649f204e0b8SIan Munsie {
650f204e0b8SIan Munsie 	struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
651f204e0b8SIan Munsie 
652f204e0b8SIan Munsie 	return pnv_cxl_ioda_msi_setup(dev, hwirq, virq);
653f204e0b8SIan Munsie }
654f204e0b8SIan Munsie 
cxl_update_image_control(struct cxl * adapter)6554beb5421SRyan Grimm int cxl_update_image_control(struct cxl *adapter)
6564beb5421SRyan Grimm {
6574beb5421SRyan Grimm 	struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
6584beb5421SRyan Grimm 	int rc;
6594beb5421SRyan Grimm 	int vsec;
6604beb5421SRyan Grimm 	u8 image_state;
6614beb5421SRyan Grimm 
6624beb5421SRyan Grimm 	if (!(vsec = find_cxl_vsec(dev))) {
6634beb5421SRyan Grimm 		dev_err(&dev->dev, "ABORTING: CXL VSEC not found!\n");
6644beb5421SRyan Grimm 		return -ENODEV;
6654beb5421SRyan Grimm 	}
6664beb5421SRyan Grimm 
6674beb5421SRyan Grimm 	if ((rc = CXL_READ_VSEC_IMAGE_STATE(dev, vsec, &image_state))) {
6684beb5421SRyan Grimm 		dev_err(&dev->dev, "failed to read image state: %i\n", rc);
6694beb5421SRyan Grimm 		return rc;
6704beb5421SRyan Grimm 	}
6714beb5421SRyan Grimm 
6724beb5421SRyan Grimm 	if (adapter->perst_loads_image)
6734beb5421SRyan Grimm 		image_state |= CXL_VSEC_PERST_LOADS_IMAGE;
6744beb5421SRyan Grimm 	else
6754beb5421SRyan Grimm 		image_state &= ~CXL_VSEC_PERST_LOADS_IMAGE;
6764beb5421SRyan Grimm 
6774beb5421SRyan Grimm 	if (adapter->perst_select_user)
6784beb5421SRyan Grimm 		image_state |= CXL_VSEC_PERST_SELECT_USER;
6794beb5421SRyan Grimm 	else
6804beb5421SRyan Grimm 		image_state &= ~CXL_VSEC_PERST_SELECT_USER;
6814beb5421SRyan Grimm 
6824beb5421SRyan Grimm 	if ((rc = CXL_WRITE_VSEC_IMAGE_STATE(dev, vsec, image_state))) {
6834beb5421SRyan Grimm 		dev_err(&dev->dev, "failed to update image control: %i\n", rc);
6844beb5421SRyan Grimm 		return rc;
6854beb5421SRyan Grimm 	}
6864beb5421SRyan Grimm 
6874beb5421SRyan Grimm 	return 0;
6884beb5421SRyan Grimm }
6894beb5421SRyan Grimm 
cxl_pci_alloc_one_irq(struct cxl * adapter)6902b04cf31SFrederic Barrat int cxl_pci_alloc_one_irq(struct cxl *adapter)
691f204e0b8SIan Munsie {
692f204e0b8SIan Munsie 	struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
693f204e0b8SIan Munsie 
694f204e0b8SIan Munsie 	return pnv_cxl_alloc_hwirqs(dev, 1);
695f204e0b8SIan Munsie }
696f204e0b8SIan Munsie 
cxl_pci_release_one_irq(struct cxl * adapter,int hwirq)6972b04cf31SFrederic Barrat void cxl_pci_release_one_irq(struct cxl *adapter, int hwirq)
698f204e0b8SIan Munsie {
699f204e0b8SIan Munsie 	struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
700f204e0b8SIan Munsie 
701f204e0b8SIan Munsie 	return pnv_cxl_release_hwirqs(dev, hwirq, 1);
702f204e0b8SIan Munsie }
703f204e0b8SIan Munsie 
cxl_pci_alloc_irq_ranges(struct cxl_irq_ranges * irqs,struct cxl * adapter,unsigned int num)7042b04cf31SFrederic Barrat int cxl_pci_alloc_irq_ranges(struct cxl_irq_ranges *irqs,
7052b04cf31SFrederic Barrat 			struct cxl *adapter, unsigned int num)
706f204e0b8SIan Munsie {
707f204e0b8SIan Munsie 	struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
708f204e0b8SIan Munsie 
709f204e0b8SIan Munsie 	return pnv_cxl_alloc_hwirq_ranges(irqs, dev, num);
710f204e0b8SIan Munsie }
711f204e0b8SIan Munsie 
cxl_pci_release_irq_ranges(struct cxl_irq_ranges * irqs,struct cxl * adapter)7122b04cf31SFrederic Barrat void cxl_pci_release_irq_ranges(struct cxl_irq_ranges *irqs,
7132b04cf31SFrederic Barrat 				struct cxl *adapter)
714f204e0b8SIan Munsie {
715f204e0b8SIan Munsie 	struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
716f204e0b8SIan Munsie 
717f204e0b8SIan Munsie 	pnv_cxl_release_hwirq_ranges(irqs, dev);
718f204e0b8SIan Munsie }
719f204e0b8SIan Munsie 
setup_cxl_bars(struct pci_dev * dev)720f204e0b8SIan Munsie static int setup_cxl_bars(struct pci_dev *dev)
721f204e0b8SIan Munsie {
722f204e0b8SIan Munsie 	/* Safety check in case we get backported to < 3.17 without M64 */
723f204e0b8SIan Munsie 	if ((p1_base(dev) < 0x100000000ULL) ||
724f204e0b8SIan Munsie 	    (p2_base(dev) < 0x100000000ULL)) {
725f204e0b8SIan Munsie 		dev_err(&dev->dev, "ABORTING: M32 BAR assignment incompatible with CXL\n");
726f204e0b8SIan Munsie 		return -ENODEV;
727f204e0b8SIan Munsie 	}
728f204e0b8SIan Munsie 
729f204e0b8SIan Munsie 	/*
730f204e0b8SIan Munsie 	 * BAR 4/5 has a special meaning for CXL and must be programmed with a
731f204e0b8SIan Munsie 	 * special value corresponding to the CXL protocol address range.
732f24be42aSChristophe Lombard 	 * For POWER 8/9 that means bits 48:49 must be set to 10
733f204e0b8SIan Munsie 	 */
734f204e0b8SIan Munsie 	pci_write_config_dword(dev, PCI_BASE_ADDRESS_4, 0x00000000);
735f204e0b8SIan Munsie 	pci_write_config_dword(dev, PCI_BASE_ADDRESS_5, 0x00020000);
736f204e0b8SIan Munsie 
737f204e0b8SIan Munsie 	return 0;
738f204e0b8SIan Munsie }
739f204e0b8SIan Munsie 
74029fea8aaSAlastair D'Silva /* pciex node: ibm,opal-m64-window = <0x3d058 0x0 0x3d058 0x0 0x8 0x0>; */
switch_card_to_cxl(struct pci_dev * dev)74129fea8aaSAlastair D'Silva static int switch_card_to_cxl(struct pci_dev *dev)
742b0b5e591SAndrew Donnellan {
74329fea8aaSAlastair D'Silva 	int vsec;
744f204e0b8SIan Munsie 	u8 val;
745f204e0b8SIan Munsie 	int rc;
746f204e0b8SIan Munsie 
74729fea8aaSAlastair D'Silva 	dev_info(&dev->dev, "switch card to CXL\n");
74829fea8aaSAlastair D'Silva 
74929fea8aaSAlastair D'Silva 	if (!(vsec = find_cxl_vsec(dev))) {
75029fea8aaSAlastair D'Silva 		dev_err(&dev->dev, "ABORTING: CXL VSEC not found!\n");
75129fea8aaSAlastair D'Silva 		return -ENODEV;
752b0b5e591SAndrew Donnellan 	}
753f204e0b8SIan Munsie 
75429fea8aaSAlastair D'Silva 	if ((rc = CXL_READ_VSEC_MODE_CONTROL(dev, vsec, &val))) {
75529fea8aaSAlastair D'Silva 		dev_err(&dev->dev, "failed to read current mode control: %i", rc);
75629fea8aaSAlastair D'Silva 		return rc;
757b0b5e591SAndrew Donnellan 	}
758b0b5e591SAndrew Donnellan 	val &= ~CXL_VSEC_PROTOCOL_MASK;
759b0b5e591SAndrew Donnellan 	val |= CXL_VSEC_PROTOCOL_256TB | CXL_VSEC_PROTOCOL_ENABLE;
76029fea8aaSAlastair D'Silva 	if ((rc = CXL_WRITE_VSEC_MODE_CONTROL(dev, vsec, val))) {
76129fea8aaSAlastair D'Silva 		dev_err(&dev->dev, "failed to enable CXL protocol: %i", rc);
76229fea8aaSAlastair D'Silva 		return rc;
763b0b5e591SAndrew Donnellan 	}
764b0b5e591SAndrew Donnellan 	/*
76529fea8aaSAlastair D'Silva 	 * The CAIA spec (v0.12 11.6 Bi-modal Device Support) states
76629fea8aaSAlastair D'Silva 	 * we must wait 100ms after this mode switch before touching
76729fea8aaSAlastair D'Silva 	 * PCIe config space.
768b0b5e591SAndrew Donnellan 	 */
769b0b5e591SAndrew Donnellan 	msleep(100);
770b0b5e591SAndrew Donnellan 
771f204e0b8SIan Munsie 	return 0;
772f204e0b8SIan Munsie }
773f204e0b8SIan Munsie 
pci_map_slice_regs(struct cxl_afu * afu,struct cxl * adapter,struct pci_dev * dev)7742b04cf31SFrederic Barrat static int pci_map_slice_regs(struct cxl_afu *afu, struct cxl *adapter, struct pci_dev *dev)
775f204e0b8SIan Munsie {
776f204e0b8SIan Munsie 	u64 p1n_base, p2n_base, afu_desc;
777f204e0b8SIan Munsie 	const u64 p1n_size = 0x100;
778f204e0b8SIan Munsie 	const u64 p2n_size = 0x1000;
779f204e0b8SIan Munsie 
780f204e0b8SIan Munsie 	p1n_base = p1_base(dev) + 0x10000 + (afu->slice * p1n_size);
781f204e0b8SIan Munsie 	p2n_base = p2_base(dev) + (afu->slice * p2n_size);
782cbffa3a5SChristophe Lombard 	afu->psn_phys = p2_base(dev) + (adapter->native->ps_off + (afu->slice * adapter->ps_size));
783cbffa3a5SChristophe Lombard 	afu_desc = p2_base(dev) + adapter->native->afu_desc_off + (afu->slice * adapter->native->afu_desc_size);
784f204e0b8SIan Munsie 
785cbffa3a5SChristophe Lombard 	if (!(afu->native->p1n_mmio = ioremap(p1n_base, p1n_size)))
786f204e0b8SIan Munsie 		goto err;
787f204e0b8SIan Munsie 	if (!(afu->p2n_mmio = ioremap(p2n_base, p2n_size)))
788f204e0b8SIan Munsie 		goto err1;
789f204e0b8SIan Munsie 	if (afu_desc) {
790cbffa3a5SChristophe Lombard 		if (!(afu->native->afu_desc_mmio = ioremap(afu_desc, adapter->native->afu_desc_size)))
791f204e0b8SIan Munsie 			goto err2;
792f204e0b8SIan Munsie 	}
793f204e0b8SIan Munsie 
794f204e0b8SIan Munsie 	return 0;
795f204e0b8SIan Munsie err2:
796f204e0b8SIan Munsie 	iounmap(afu->p2n_mmio);
797f204e0b8SIan Munsie err1:
798cbffa3a5SChristophe Lombard 	iounmap(afu->native->p1n_mmio);
799f204e0b8SIan Munsie err:
800f204e0b8SIan Munsie 	dev_err(&afu->dev, "Error mapping AFU MMIO regions\n");
801f204e0b8SIan Munsie 	return -ENOMEM;
802f204e0b8SIan Munsie }
803f204e0b8SIan Munsie 
pci_unmap_slice_regs(struct cxl_afu * afu)8042b04cf31SFrederic Barrat static void pci_unmap_slice_regs(struct cxl_afu *afu)
805f204e0b8SIan Munsie {
806575e6986SDaniel Axtens 	if (afu->p2n_mmio) {
807f204e0b8SIan Munsie 		iounmap(afu->p2n_mmio);
808575e6986SDaniel Axtens 		afu->p2n_mmio = NULL;
809575e6986SDaniel Axtens 	}
810cbffa3a5SChristophe Lombard 	if (afu->native->p1n_mmio) {
811cbffa3a5SChristophe Lombard 		iounmap(afu->native->p1n_mmio);
812cbffa3a5SChristophe Lombard 		afu->native->p1n_mmio = NULL;
813575e6986SDaniel Axtens 	}
814cbffa3a5SChristophe Lombard 	if (afu->native->afu_desc_mmio) {
815cbffa3a5SChristophe Lombard 		iounmap(afu->native->afu_desc_mmio);
816cbffa3a5SChristophe Lombard 		afu->native->afu_desc_mmio = NULL;
817575e6986SDaniel Axtens 	}
818f204e0b8SIan Munsie }
819f204e0b8SIan Munsie 
cxl_pci_release_afu(struct device * dev)8202b04cf31SFrederic Barrat void cxl_pci_release_afu(struct device *dev)
821f204e0b8SIan Munsie {
822f204e0b8SIan Munsie 	struct cxl_afu *afu = to_cxl_afu(dev);
823f204e0b8SIan Munsie 
8242b04cf31SFrederic Barrat 	pr_devel("%s\n", __func__);
825f204e0b8SIan Munsie 
826bd664f89SJohannes Thumshirn 	idr_destroy(&afu->contexts_idr);
82705155772SDaniel Axtens 	cxl_release_spa(afu);
82805155772SDaniel Axtens 
829cbffa3a5SChristophe Lombard 	kfree(afu->native);
830f204e0b8SIan Munsie 	kfree(afu);
831f204e0b8SIan Munsie }
832f204e0b8SIan Munsie 
833f204e0b8SIan Munsie /* Expects AFU struct to have recently been zeroed out */
cxl_read_afu_descriptor(struct cxl_afu * afu)834f204e0b8SIan Munsie static int cxl_read_afu_descriptor(struct cxl_afu *afu)
835f204e0b8SIan Munsie {
836f204e0b8SIan Munsie 	u64 val;
837f204e0b8SIan Munsie 
838f204e0b8SIan Munsie 	val = AFUD_READ_INFO(afu);
839f204e0b8SIan Munsie 	afu->pp_irqs = AFUD_NUM_INTS_PER_PROC(val);
840f204e0b8SIan Munsie 	afu->max_procs_virtualised = AFUD_NUM_PROCS(val);
841b087e619SIan Munsie 	afu->crs_num = AFUD_NUM_CRS(val);
842f204e0b8SIan Munsie 
843f204e0b8SIan Munsie 	if (AFUD_AFU_DIRECTED(val))
844f204e0b8SIan Munsie 		afu->modes_supported |= CXL_MODE_DIRECTED;
845f204e0b8SIan Munsie 	if (AFUD_DEDICATED_PROCESS(val))
846f204e0b8SIan Munsie 		afu->modes_supported |= CXL_MODE_DEDICATED;
847f204e0b8SIan Munsie 	if (AFUD_TIME_SLICED(val))
848f204e0b8SIan Munsie 		afu->modes_supported |= CXL_MODE_TIME_SLICED;
849f204e0b8SIan Munsie 
850f204e0b8SIan Munsie 	val = AFUD_READ_PPPSA(afu);
851f204e0b8SIan Munsie 	afu->pp_size = AFUD_PPPSA_LEN(val) * 4096;
852f204e0b8SIan Munsie 	afu->psa = AFUD_PPPSA_PSA(val);
853f204e0b8SIan Munsie 	if ((afu->pp_psa = AFUD_PPPSA_PP(val)))
854cbffa3a5SChristophe Lombard 		afu->native->pp_offset = AFUD_READ_PPPSA_OFF(afu);
855f204e0b8SIan Munsie 
856b087e619SIan Munsie 	val = AFUD_READ_CR(afu);
857b087e619SIan Munsie 	afu->crs_len = AFUD_CR_LEN(val) * 256;
858b087e619SIan Munsie 	afu->crs_offset = AFUD_READ_CR_OFF(afu);
859b087e619SIan Munsie 
860e36f6fe1SVaibhav Jain 
861e36f6fe1SVaibhav Jain 	/* eb_len is in multiple of 4K */
862e36f6fe1SVaibhav Jain 	afu->eb_len = AFUD_EB_LEN(AFUD_READ_EB(afu)) * 4096;
863e36f6fe1SVaibhav Jain 	afu->eb_offset = AFUD_READ_EB_OFF(afu);
864e36f6fe1SVaibhav Jain 
865e36f6fe1SVaibhav Jain 	/* eb_off is 4K aligned so lower 12 bits are always zero */
866e36f6fe1SVaibhav Jain 	if (EXTRACT_PPC_BITS(afu->eb_offset, 0, 11) != 0) {
867e36f6fe1SVaibhav Jain 		dev_warn(&afu->dev,
868e36f6fe1SVaibhav Jain 			 "Invalid AFU error buffer offset %Lx\n",
869e36f6fe1SVaibhav Jain 			 afu->eb_offset);
870e36f6fe1SVaibhav Jain 		dev_info(&afu->dev,
871e36f6fe1SVaibhav Jain 			 "Ignoring AFU error buffer in the descriptor\n");
872e36f6fe1SVaibhav Jain 		/* indicate that no afu buffer exists */
873e36f6fe1SVaibhav Jain 		afu->eb_len = 0;
874e36f6fe1SVaibhav Jain 	}
875e36f6fe1SVaibhav Jain 
876f204e0b8SIan Munsie 	return 0;
877f204e0b8SIan Munsie }
878f204e0b8SIan Munsie 
cxl_afu_descriptor_looks_ok(struct cxl_afu * afu)879f204e0b8SIan Munsie static int cxl_afu_descriptor_looks_ok(struct cxl_afu *afu)
880f204e0b8SIan Munsie {
8815be587b1SFrederic Barrat 	int i, rc;
8825be587b1SFrederic Barrat 	u32 val;
8833d5be039SIan Munsie 
884f204e0b8SIan Munsie 	if (afu->psa && afu->adapter->ps_size <
885cbffa3a5SChristophe Lombard 			(afu->native->pp_offset + afu->pp_size*afu->max_procs_virtualised)) {
886f204e0b8SIan Munsie 		dev_err(&afu->dev, "per-process PSA can't fit inside the PSA!\n");
887f204e0b8SIan Munsie 		return -ENODEV;
888f204e0b8SIan Munsie 	}
889f204e0b8SIan Munsie 
890f204e0b8SIan Munsie 	if (afu->pp_psa && (afu->pp_size < PAGE_SIZE))
891abd1d99bSChristophe Lombard 		dev_warn(&afu->dev, "AFU uses pp_size(%#016llx) < PAGE_SIZE per-process PSA!\n", afu->pp_size);
892f204e0b8SIan Munsie 
8933d5be039SIan Munsie 	for (i = 0; i < afu->crs_num; i++) {
8945be587b1SFrederic Barrat 		rc = cxl_ops->afu_cr_read32(afu, i, 0, &val);
8955be587b1SFrederic Barrat 		if (rc || val == 0) {
8963d5be039SIan Munsie 			dev_err(&afu->dev, "ABORTING: AFU configuration record %i is invalid\n", i);
8973d5be039SIan Munsie 			return -EINVAL;
8983d5be039SIan Munsie 		}
8993d5be039SIan Munsie 	}
9003d5be039SIan Munsie 
90149e9c99fSIan Munsie 	if ((afu->modes_supported & ~CXL_MODE_DEDICATED) && afu->max_procs_virtualised == 0) {
90249e9c99fSIan Munsie 		/*
90349e9c99fSIan Munsie 		 * We could also check this for the dedicated process model
90449e9c99fSIan Munsie 		 * since the architecture indicates it should be set to 1, but
90549e9c99fSIan Munsie 		 * in that case we ignore the value and I'd rather not risk
90649e9c99fSIan Munsie 		 * breaking any existing dedicated process AFUs that left it as
90749e9c99fSIan Munsie 		 * 0 (not that I'm aware of any). It is clearly an error for an
90849e9c99fSIan Munsie 		 * AFU directed AFU to set this to 0, and would have previously
90949e9c99fSIan Munsie 		 * triggered a bug resulting in the maximum not being enforced
91049e9c99fSIan Munsie 		 * at all since idr_alloc treats 0 as no maximum.
91149e9c99fSIan Munsie 		 */
91249e9c99fSIan Munsie 		dev_err(&afu->dev, "AFU does not support any processes\n");
91349e9c99fSIan Munsie 		return -EINVAL;
91449e9c99fSIan Munsie 	}
91549e9c99fSIan Munsie 
916f204e0b8SIan Munsie 	return 0;
917f204e0b8SIan Munsie }
918f204e0b8SIan Munsie 
sanitise_afu_regs_psl9(struct cxl_afu * afu)919f24be42aSChristophe Lombard static int sanitise_afu_regs_psl9(struct cxl_afu *afu)
920f24be42aSChristophe Lombard {
921f24be42aSChristophe Lombard 	u64 reg;
922f24be42aSChristophe Lombard 
923f24be42aSChristophe Lombard 	/*
924f24be42aSChristophe Lombard 	 * Clear out any regs that contain either an IVTE or address or may be
925f24be42aSChristophe Lombard 	 * waiting on an acknowledgment to try to be a bit safer as we bring
926f24be42aSChristophe Lombard 	 * it online
927f24be42aSChristophe Lombard 	 */
928f24be42aSChristophe Lombard 	reg = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
929f24be42aSChristophe Lombard 	if ((reg & CXL_AFU_Cntl_An_ES_MASK) != CXL_AFU_Cntl_An_ES_Disabled) {
930f24be42aSChristophe Lombard 		dev_warn(&afu->dev, "WARNING: AFU was not disabled: %#016llx\n", reg);
931f24be42aSChristophe Lombard 		if (cxl_ops->afu_reset(afu))
932f24be42aSChristophe Lombard 			return -EIO;
933f24be42aSChristophe Lombard 		if (cxl_afu_disable(afu))
934f24be42aSChristophe Lombard 			return -EIO;
935f24be42aSChristophe Lombard 		if (cxl_psl_purge(afu))
936f24be42aSChristophe Lombard 			return -EIO;
937f24be42aSChristophe Lombard 	}
938f24be42aSChristophe Lombard 	cxl_p1n_write(afu, CXL_PSL_SPAP_An, 0x0000000000000000);
939f24be42aSChristophe Lombard 	cxl_p1n_write(afu, CXL_PSL_AMBAR_An, 0x0000000000000000);
940f24be42aSChristophe Lombard 	reg = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
941f24be42aSChristophe Lombard 	if (reg) {
942f24be42aSChristophe Lombard 		dev_warn(&afu->dev, "AFU had pending DSISR: %#016llx\n", reg);
943f24be42aSChristophe Lombard 		if (reg & CXL_PSL9_DSISR_An_TF)
944f24be42aSChristophe Lombard 			cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
945f24be42aSChristophe Lombard 		else
946f24be42aSChristophe Lombard 			cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
947f24be42aSChristophe Lombard 	}
948f24be42aSChristophe Lombard 	if (afu->adapter->native->sl_ops->register_serr_irq) {
949f24be42aSChristophe Lombard 		reg = cxl_p1n_read(afu, CXL_PSL_SERR_An);
950f24be42aSChristophe Lombard 		if (reg) {
951f24be42aSChristophe Lombard 			if (reg & ~0x000000007fffffff)
952f24be42aSChristophe Lombard 				dev_warn(&afu->dev, "AFU had pending SERR: %#016llx\n", reg);
953f24be42aSChristophe Lombard 			cxl_p1n_write(afu, CXL_PSL_SERR_An, reg & ~0xffff);
954f24be42aSChristophe Lombard 		}
955f24be42aSChristophe Lombard 	}
956f24be42aSChristophe Lombard 	reg = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
957f24be42aSChristophe Lombard 	if (reg) {
958f24be42aSChristophe Lombard 		dev_warn(&afu->dev, "AFU had pending error status: %#016llx\n", reg);
959f24be42aSChristophe Lombard 		cxl_p2n_write(afu, CXL_PSL_ErrStat_An, reg);
960f24be42aSChristophe Lombard 	}
961f24be42aSChristophe Lombard 
962f24be42aSChristophe Lombard 	return 0;
963f24be42aSChristophe Lombard }
964f24be42aSChristophe Lombard 
sanitise_afu_regs_psl8(struct cxl_afu * afu)96564663f37SChristophe Lombard static int sanitise_afu_regs_psl8(struct cxl_afu *afu)
966f204e0b8SIan Munsie {
967f204e0b8SIan Munsie 	u64 reg;
968f204e0b8SIan Munsie 
969f204e0b8SIan Munsie 	/*
970f204e0b8SIan Munsie 	 * Clear out any regs that contain either an IVTE or address or may be
971f204e0b8SIan Munsie 	 * waiting on an acknowledgement to try to be a bit safer as we bring
972f204e0b8SIan Munsie 	 * it online
973f204e0b8SIan Munsie 	 */
974f204e0b8SIan Munsie 	reg = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
975f204e0b8SIan Munsie 	if ((reg & CXL_AFU_Cntl_An_ES_MASK) != CXL_AFU_Cntl_An_ES_Disabled) {
976de369538SRasmus Villemoes 		dev_warn(&afu->dev, "WARNING: AFU was not disabled: %#016llx\n", reg);
9775be587b1SFrederic Barrat 		if (cxl_ops->afu_reset(afu))
978f204e0b8SIan Munsie 			return -EIO;
979f204e0b8SIan Munsie 		if (cxl_afu_disable(afu))
980f204e0b8SIan Munsie 			return -EIO;
981f204e0b8SIan Munsie 		if (cxl_psl_purge(afu))
982f204e0b8SIan Munsie 			return -EIO;
983f204e0b8SIan Munsie 	}
984f204e0b8SIan Munsie 	cxl_p1n_write(afu, CXL_PSL_SPAP_An, 0x0000000000000000);
985f204e0b8SIan Munsie 	cxl_p1n_write(afu, CXL_PSL_IVTE_Limit_An, 0x0000000000000000);
986f204e0b8SIan Munsie 	cxl_p1n_write(afu, CXL_PSL_IVTE_Offset_An, 0x0000000000000000);
987f204e0b8SIan Munsie 	cxl_p1n_write(afu, CXL_PSL_AMBAR_An, 0x0000000000000000);
988f204e0b8SIan Munsie 	cxl_p1n_write(afu, CXL_PSL_SPOffset_An, 0x0000000000000000);
989f204e0b8SIan Munsie 	cxl_p1n_write(afu, CXL_HAURP_An, 0x0000000000000000);
990f204e0b8SIan Munsie 	cxl_p2n_write(afu, CXL_CSRP_An, 0x0000000000000000);
991f204e0b8SIan Munsie 	cxl_p2n_write(afu, CXL_AURP1_An, 0x0000000000000000);
992f204e0b8SIan Munsie 	cxl_p2n_write(afu, CXL_AURP0_An, 0x0000000000000000);
993f204e0b8SIan Munsie 	cxl_p2n_write(afu, CXL_SSTP1_An, 0x0000000000000000);
994f204e0b8SIan Munsie 	cxl_p2n_write(afu, CXL_SSTP0_An, 0x0000000000000000);
995f204e0b8SIan Munsie 	reg = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
996f204e0b8SIan Munsie 	if (reg) {
997de369538SRasmus Villemoes 		dev_warn(&afu->dev, "AFU had pending DSISR: %#016llx\n", reg);
998f204e0b8SIan Munsie 		if (reg & CXL_PSL_DSISR_TRANS)
999f204e0b8SIan Munsie 			cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
1000f204e0b8SIan Munsie 		else
1001f204e0b8SIan Munsie 			cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
1002f204e0b8SIan Munsie 	}
10036d382616SFrederic Barrat 	if (afu->adapter->native->sl_ops->register_serr_irq) {
1004f204e0b8SIan Munsie 		reg = cxl_p1n_read(afu, CXL_PSL_SERR_An);
1005f204e0b8SIan Munsie 		if (reg) {
1006f204e0b8SIan Munsie 			if (reg & ~0xffff)
1007de369538SRasmus Villemoes 				dev_warn(&afu->dev, "AFU had pending SERR: %#016llx\n", reg);
1008f204e0b8SIan Munsie 			cxl_p1n_write(afu, CXL_PSL_SERR_An, reg & ~0xffff);
1009f204e0b8SIan Munsie 		}
10106d382616SFrederic Barrat 	}
1011f204e0b8SIan Munsie 	reg = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
1012f204e0b8SIan Munsie 	if (reg) {
1013de369538SRasmus Villemoes 		dev_warn(&afu->dev, "AFU had pending error status: %#016llx\n", reg);
1014f204e0b8SIan Munsie 		cxl_p2n_write(afu, CXL_PSL_ErrStat_An, reg);
1015f204e0b8SIan Munsie 	}
1016f204e0b8SIan Munsie 
1017f204e0b8SIan Munsie 	return 0;
1018f204e0b8SIan Munsie }
1019f204e0b8SIan Munsie 
1020e36f6fe1SVaibhav Jain #define ERR_BUFF_MAX_COPY_SIZE PAGE_SIZE
1021e36f6fe1SVaibhav Jain /*
1022e36f6fe1SVaibhav Jain  * afu_eb_read:
1023e36f6fe1SVaibhav Jain  * Called from sysfs and reads the afu error info buffer. The h/w only supports
1024e36f6fe1SVaibhav Jain  * 4/8 bytes aligned access. So in case the requested offset/count arent 8 byte
1025e36f6fe1SVaibhav Jain  * aligned the function uses a bounce buffer which can be max PAGE_SIZE.
1026e36f6fe1SVaibhav Jain  */
cxl_pci_afu_read_err_buffer(struct cxl_afu * afu,char * buf,loff_t off,size_t count)10272b04cf31SFrederic Barrat ssize_t cxl_pci_afu_read_err_buffer(struct cxl_afu *afu, char *buf,
1028e36f6fe1SVaibhav Jain 				loff_t off, size_t count)
1029e36f6fe1SVaibhav Jain {
1030e36f6fe1SVaibhav Jain 	loff_t aligned_start, aligned_end;
1031e36f6fe1SVaibhav Jain 	size_t aligned_length;
1032e36f6fe1SVaibhav Jain 	void *tbuf;
1033cbffa3a5SChristophe Lombard 	const void __iomem *ebuf = afu->native->afu_desc_mmio + afu->eb_offset;
1034e36f6fe1SVaibhav Jain 
1035e36f6fe1SVaibhav Jain 	if (count == 0 || off < 0 || (size_t)off >= afu->eb_len)
1036e36f6fe1SVaibhav Jain 		return 0;
1037e36f6fe1SVaibhav Jain 
1038e36f6fe1SVaibhav Jain 	/* calculate aligned read window */
1039e36f6fe1SVaibhav Jain 	count = min((size_t)(afu->eb_len - off), count);
1040e36f6fe1SVaibhav Jain 	aligned_start = round_down(off, 8);
1041e36f6fe1SVaibhav Jain 	aligned_end = round_up(off + count, 8);
1042e36f6fe1SVaibhav Jain 	aligned_length = aligned_end - aligned_start;
1043e36f6fe1SVaibhav Jain 
1044e36f6fe1SVaibhav Jain 	/* max we can copy in one read is PAGE_SIZE */
1045e36f6fe1SVaibhav Jain 	if (aligned_length > ERR_BUFF_MAX_COPY_SIZE) {
1046e36f6fe1SVaibhav Jain 		aligned_length = ERR_BUFF_MAX_COPY_SIZE;
1047e36f6fe1SVaibhav Jain 		count = ERR_BUFF_MAX_COPY_SIZE - (off & 0x7);
1048e36f6fe1SVaibhav Jain 	}
1049e36f6fe1SVaibhav Jain 
1050e36f6fe1SVaibhav Jain 	/* use bounce buffer for copy */
10510ee931c4SMichal Hocko 	tbuf = (void *)__get_free_page(GFP_KERNEL);
1052e36f6fe1SVaibhav Jain 	if (!tbuf)
1053e36f6fe1SVaibhav Jain 		return -ENOMEM;
1054e36f6fe1SVaibhav Jain 
1055e36f6fe1SVaibhav Jain 	/* perform aligned read from the mmio region */
1056e36f6fe1SVaibhav Jain 	memcpy_fromio(tbuf, ebuf + aligned_start, aligned_length);
1057e36f6fe1SVaibhav Jain 	memcpy(buf, tbuf + (off & 0x7), count);
1058e36f6fe1SVaibhav Jain 
1059e36f6fe1SVaibhav Jain 	free_page((unsigned long)tbuf);
1060e36f6fe1SVaibhav Jain 
1061e36f6fe1SVaibhav Jain 	return count;
1062e36f6fe1SVaibhav Jain }
1063e36f6fe1SVaibhav Jain 
pci_configure_afu(struct cxl_afu * afu,struct cxl * adapter,struct pci_dev * dev)10642b04cf31SFrederic Barrat static int pci_configure_afu(struct cxl_afu *afu, struct cxl *adapter, struct pci_dev *dev)
1065f204e0b8SIan Munsie {
1066f204e0b8SIan Munsie 	int rc;
1067f204e0b8SIan Munsie 
10682b04cf31SFrederic Barrat 	if ((rc = pci_map_slice_regs(afu, adapter, dev)))
1069d76427b0SDaniel Axtens 		return rc;
1070f204e0b8SIan Munsie 
1071bdd2e715SChristophe Lombard 	if (adapter->native->sl_ops->sanitise_afu_regs) {
1072bdd2e715SChristophe Lombard 		rc = adapter->native->sl_ops->sanitise_afu_regs(afu);
1073bdd2e715SChristophe Lombard 		if (rc)
1074d76427b0SDaniel Axtens 			goto err1;
1075bdd2e715SChristophe Lombard 	}
1076f204e0b8SIan Munsie 
1077f204e0b8SIan Munsie 	/* We need to reset the AFU before we can read the AFU descriptor */
10785be587b1SFrederic Barrat 	if ((rc = cxl_ops->afu_reset(afu)))
1079d76427b0SDaniel Axtens 		goto err1;
1080f204e0b8SIan Munsie 
1081f204e0b8SIan Munsie 	if (cxl_verbose)
1082f204e0b8SIan Munsie 		dump_afu_descriptor(afu);
1083f204e0b8SIan Munsie 
1084f204e0b8SIan Munsie 	if ((rc = cxl_read_afu_descriptor(afu)))
1085d76427b0SDaniel Axtens 		goto err1;
1086f204e0b8SIan Munsie 
1087f204e0b8SIan Munsie 	if ((rc = cxl_afu_descriptor_looks_ok(afu)))
1088d76427b0SDaniel Axtens 		goto err1;
1089f204e0b8SIan Munsie 
10906d382616SFrederic Barrat 	if (adapter->native->sl_ops->afu_regs_init)
10916d382616SFrederic Barrat 		if ((rc = adapter->native->sl_ops->afu_regs_init(afu)))
1092d76427b0SDaniel Axtens 			goto err1;
1093f204e0b8SIan Munsie 
10946d382616SFrederic Barrat 	if (adapter->native->sl_ops->register_serr_irq)
10956d382616SFrederic Barrat 		if ((rc = adapter->native->sl_ops->register_serr_irq(afu)))
1096d76427b0SDaniel Axtens 			goto err1;
1097f204e0b8SIan Munsie 
10982b04cf31SFrederic Barrat 	if ((rc = cxl_native_register_psl_irq(afu)))
1099d76427b0SDaniel Axtens 		goto err2;
1100d76427b0SDaniel Axtens 
1101171ed0fcSAndrew Donnellan 	atomic_set(&afu->configured_state, 0);
1102d76427b0SDaniel Axtens 	return 0;
1103d76427b0SDaniel Axtens 
1104d76427b0SDaniel Axtens err2:
11056d382616SFrederic Barrat 	if (adapter->native->sl_ops->release_serr_irq)
11066d382616SFrederic Barrat 		adapter->native->sl_ops->release_serr_irq(afu);
1107d76427b0SDaniel Axtens err1:
11082b04cf31SFrederic Barrat 	pci_unmap_slice_regs(afu);
1109d76427b0SDaniel Axtens 	return rc;
1110d76427b0SDaniel Axtens }
1111d76427b0SDaniel Axtens 
pci_deconfigure_afu(struct cxl_afu * afu)11122b04cf31SFrederic Barrat static void pci_deconfigure_afu(struct cxl_afu *afu)
1113d76427b0SDaniel Axtens {
1114171ed0fcSAndrew Donnellan 	/*
1115171ed0fcSAndrew Donnellan 	 * It's okay to deconfigure when AFU is already locked, otherwise wait
1116171ed0fcSAndrew Donnellan 	 * until there are no readers
1117171ed0fcSAndrew Donnellan 	 */
1118171ed0fcSAndrew Donnellan 	if (atomic_read(&afu->configured_state) != -1) {
1119171ed0fcSAndrew Donnellan 		while (atomic_cmpxchg(&afu->configured_state, 0, -1) != -1)
1120171ed0fcSAndrew Donnellan 			schedule();
1121171ed0fcSAndrew Donnellan 	}
11222b04cf31SFrederic Barrat 	cxl_native_release_psl_irq(afu);
11236d382616SFrederic Barrat 	if (afu->adapter->native->sl_ops->release_serr_irq)
11246d382616SFrederic Barrat 		afu->adapter->native->sl_ops->release_serr_irq(afu);
11252b04cf31SFrederic Barrat 	pci_unmap_slice_regs(afu);
1126d76427b0SDaniel Axtens }
1127d76427b0SDaniel Axtens 
pci_init_afu(struct cxl * adapter,int slice,struct pci_dev * dev)11282b04cf31SFrederic Barrat static int pci_init_afu(struct cxl *adapter, int slice, struct pci_dev *dev)
1129d76427b0SDaniel Axtens {
1130d76427b0SDaniel Axtens 	struct cxl_afu *afu;
1131cbffa3a5SChristophe Lombard 	int rc = -ENOMEM;
1132d76427b0SDaniel Axtens 
1133d76427b0SDaniel Axtens 	afu = cxl_alloc_afu(adapter, slice);
1134d76427b0SDaniel Axtens 	if (!afu)
1135d76427b0SDaniel Axtens 		return -ENOMEM;
1136d76427b0SDaniel Axtens 
1137cbffa3a5SChristophe Lombard 	afu->native = kzalloc(sizeof(struct cxl_afu_native), GFP_KERNEL);
1138cbffa3a5SChristophe Lombard 	if (!afu->native)
1139cbffa3a5SChristophe Lombard 		goto err_free_afu;
1140cbffa3a5SChristophe Lombard 
1141cbffa3a5SChristophe Lombard 	mutex_init(&afu->native->spa_mutex);
1142cbffa3a5SChristophe Lombard 
1143d76427b0SDaniel Axtens 	rc = dev_set_name(&afu->dev, "afu%i.%i", adapter->adapter_num, slice);
1144d76427b0SDaniel Axtens 	if (rc)
1145cbffa3a5SChristophe Lombard 		goto err_free_native;
1146d76427b0SDaniel Axtens 
11472b04cf31SFrederic Barrat 	rc = pci_configure_afu(afu, adapter, dev);
1148d76427b0SDaniel Axtens 	if (rc)
1149cbffa3a5SChristophe Lombard 		goto err_free_native;
1150f204e0b8SIan Munsie 
1151f204e0b8SIan Munsie 	/* Don't care if this fails */
1152f204e0b8SIan Munsie 	cxl_debugfs_afu_add(afu);
1153f204e0b8SIan Munsie 
1154f204e0b8SIan Munsie 	/*
1155f204e0b8SIan Munsie 	 * After we call this function we must not free the afu directly, even
1156f204e0b8SIan Munsie 	 * if it returns an error!
1157f204e0b8SIan Munsie 	 */
1158f204e0b8SIan Munsie 	if ((rc = cxl_register_afu(afu)))
11598bf03f55SYang Yingliang 		goto err_put_dev;
1160f204e0b8SIan Munsie 
1161f204e0b8SIan Munsie 	if ((rc = cxl_sysfs_afu_add(afu)))
11628bf03f55SYang Yingliang 		goto err_del_dev;
1163f204e0b8SIan Munsie 
1164f204e0b8SIan Munsie 	adapter->afu[afu->slice] = afu;
1165f204e0b8SIan Munsie 
11666f7f0b3dSMichael Neuling 	if ((rc = cxl_pci_vphb_add(afu)))
11676f7f0b3dSMichael Neuling 		dev_info(&afu->dev, "Can't register vPHB\n");
11686f7f0b3dSMichael Neuling 
1169f204e0b8SIan Munsie 	return 0;
1170f204e0b8SIan Munsie 
11718bf03f55SYang Yingliang err_del_dev:
11728bf03f55SYang Yingliang 	device_del(&afu->dev);
11738bf03f55SYang Yingliang err_put_dev:
11742b04cf31SFrederic Barrat 	pci_deconfigure_afu(afu);
1175f204e0b8SIan Munsie 	cxl_debugfs_afu_remove(afu);
11768bf03f55SYang Yingliang 	put_device(&afu->dev);
1177d76427b0SDaniel Axtens 	return rc;
1178d76427b0SDaniel Axtens 
1179cbffa3a5SChristophe Lombard err_free_native:
1180cbffa3a5SChristophe Lombard 	kfree(afu->native);
1181cbffa3a5SChristophe Lombard err_free_afu:
1182f204e0b8SIan Munsie 	kfree(afu);
1183f204e0b8SIan Munsie 	return rc;
1184d76427b0SDaniel Axtens 
1185f204e0b8SIan Munsie }
1186f204e0b8SIan Munsie 
cxl_pci_remove_afu(struct cxl_afu * afu)11872b04cf31SFrederic Barrat static void cxl_pci_remove_afu(struct cxl_afu *afu)
1188f204e0b8SIan Munsie {
11892b04cf31SFrederic Barrat 	pr_devel("%s\n", __func__);
1190f204e0b8SIan Munsie 
1191f204e0b8SIan Munsie 	if (!afu)
1192f204e0b8SIan Munsie 		return;
1193f204e0b8SIan Munsie 
1194d601ea91SFrederic Barrat 	cxl_pci_vphb_remove(afu);
1195f204e0b8SIan Munsie 	cxl_sysfs_afu_remove(afu);
1196f204e0b8SIan Munsie 	cxl_debugfs_afu_remove(afu);
1197f204e0b8SIan Munsie 
1198f204e0b8SIan Munsie 	spin_lock(&afu->adapter->afu_list_lock);
1199f204e0b8SIan Munsie 	afu->adapter->afu[afu->slice] = NULL;
1200f204e0b8SIan Munsie 	spin_unlock(&afu->adapter->afu_list_lock);
1201f204e0b8SIan Munsie 
1202f204e0b8SIan Munsie 	cxl_context_detach_all(afu);
12035be587b1SFrederic Barrat 	cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
1204f204e0b8SIan Munsie 
12052b04cf31SFrederic Barrat 	pci_deconfigure_afu(afu);
1206f204e0b8SIan Munsie 	device_unregister(&afu->dev);
1207f204e0b8SIan Munsie }
1208f204e0b8SIan Munsie 
cxl_pci_reset(struct cxl * adapter)12092b04cf31SFrederic Barrat int cxl_pci_reset(struct cxl *adapter)
121062fa19d4SRyan Grimm {
121162fa19d4SRyan Grimm 	struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
121262fa19d4SRyan Grimm 	int rc;
121362fa19d4SRyan Grimm 
121413e68d8bSDaniel Axtens 	if (adapter->perst_same_image) {
121513e68d8bSDaniel Axtens 		dev_warn(&dev->dev,
121613e68d8bSDaniel Axtens 			 "cxl: refusing to reset/reflash when perst_reloads_same_image is set.\n");
121713e68d8bSDaniel Axtens 		return -EINVAL;
121813e68d8bSDaniel Axtens 	}
121913e68d8bSDaniel Axtens 
122062fa19d4SRyan Grimm 	dev_info(&dev->dev, "CXL reset\n");
122162fa19d4SRyan Grimm 
1222abd1d99bSChristophe Lombard 	/*
1223abd1d99bSChristophe Lombard 	 * The adapter is about to be reset, so ignore errors.
1224abd1d99bSChristophe Lombard 	 */
1225aaa2245eSFrederic Barrat 	cxl_data_cache_flush(adapter);
1226aaa2245eSFrederic Barrat 
122762fa19d4SRyan Grimm 	/* pcie_warm_reset requests a fundamental pci reset which includes a
122862fa19d4SRyan Grimm 	 * PERST assert/deassert.  PERST triggers a loading of the image
122962fa19d4SRyan Grimm 	 * if "user" or "factory" is selected in sysfs */
123062fa19d4SRyan Grimm 	if ((rc = pci_set_pcie_reset_state(dev, pcie_warm_reset))) {
123162fa19d4SRyan Grimm 		dev_err(&dev->dev, "cxl: pcie_warm_reset failed\n");
123262fa19d4SRyan Grimm 		return rc;
123362fa19d4SRyan Grimm 	}
123462fa19d4SRyan Grimm 
123562fa19d4SRyan Grimm 	return rc;
123662fa19d4SRyan Grimm }
1237f204e0b8SIan Munsie 
cxl_map_adapter_regs(struct cxl * adapter,struct pci_dev * dev)1238f204e0b8SIan Munsie static int cxl_map_adapter_regs(struct cxl *adapter, struct pci_dev *dev)
1239f204e0b8SIan Munsie {
1240f204e0b8SIan Munsie 	if (pci_request_region(dev, 2, "priv 2 regs"))
1241f204e0b8SIan Munsie 		goto err1;
1242f204e0b8SIan Munsie 	if (pci_request_region(dev, 0, "priv 1 regs"))
1243f204e0b8SIan Munsie 		goto err2;
1244f204e0b8SIan Munsie 
1245de369538SRasmus Villemoes 	pr_devel("cxl_map_adapter_regs: p1: %#016llx %#llx, p2: %#016llx %#llx",
1246f204e0b8SIan Munsie 			p1_base(dev), p1_size(dev), p2_base(dev), p2_size(dev));
1247f204e0b8SIan Munsie 
1248cbffa3a5SChristophe Lombard 	if (!(adapter->native->p1_mmio = ioremap(p1_base(dev), p1_size(dev))))
1249f204e0b8SIan Munsie 		goto err3;
1250f204e0b8SIan Munsie 
1251cbffa3a5SChristophe Lombard 	if (!(adapter->native->p2_mmio = ioremap(p2_base(dev), p2_size(dev))))
1252f204e0b8SIan Munsie 		goto err4;
1253f204e0b8SIan Munsie 
1254f204e0b8SIan Munsie 	return 0;
1255f204e0b8SIan Munsie 
1256f204e0b8SIan Munsie err4:
1257cbffa3a5SChristophe Lombard 	iounmap(adapter->native->p1_mmio);
1258cbffa3a5SChristophe Lombard 	adapter->native->p1_mmio = NULL;
1259f204e0b8SIan Munsie err3:
1260f204e0b8SIan Munsie 	pci_release_region(dev, 0);
1261f204e0b8SIan Munsie err2:
1262f204e0b8SIan Munsie 	pci_release_region(dev, 2);
1263f204e0b8SIan Munsie err1:
1264f204e0b8SIan Munsie 	return -ENOMEM;
1265f204e0b8SIan Munsie }
1266f204e0b8SIan Munsie 
cxl_unmap_adapter_regs(struct cxl * adapter)1267f204e0b8SIan Munsie static void cxl_unmap_adapter_regs(struct cxl *adapter)
1268f204e0b8SIan Munsie {
1269cbffa3a5SChristophe Lombard 	if (adapter->native->p1_mmio) {
1270cbffa3a5SChristophe Lombard 		iounmap(adapter->native->p1_mmio);
1271cbffa3a5SChristophe Lombard 		adapter->native->p1_mmio = NULL;
1272575e6986SDaniel Axtens 		pci_release_region(to_pci_dev(adapter->dev.parent), 2);
1273575e6986SDaniel Axtens 	}
1274cbffa3a5SChristophe Lombard 	if (adapter->native->p2_mmio) {
1275cbffa3a5SChristophe Lombard 		iounmap(adapter->native->p2_mmio);
1276cbffa3a5SChristophe Lombard 		adapter->native->p2_mmio = NULL;
1277575e6986SDaniel Axtens 		pci_release_region(to_pci_dev(adapter->dev.parent), 0);
1278575e6986SDaniel Axtens 	}
1279f204e0b8SIan Munsie }
1280f204e0b8SIan Munsie 
cxl_read_vsec(struct cxl * adapter,struct pci_dev * dev)1281f204e0b8SIan Munsie static int cxl_read_vsec(struct cxl *adapter, struct pci_dev *dev)
1282f204e0b8SIan Munsie {
1283f204e0b8SIan Munsie 	int vsec;
1284f204e0b8SIan Munsie 	u32 afu_desc_off, afu_desc_size;
1285f204e0b8SIan Munsie 	u32 ps_off, ps_size;
1286f204e0b8SIan Munsie 	u16 vseclen;
1287f204e0b8SIan Munsie 	u8 image_state;
1288f204e0b8SIan Munsie 
1289f204e0b8SIan Munsie 	if (!(vsec = find_cxl_vsec(dev))) {
1290bee30c70SIan Munsie 		dev_err(&dev->dev, "ABORTING: CXL VSEC not found!\n");
1291f204e0b8SIan Munsie 		return -ENODEV;
1292f204e0b8SIan Munsie 	}
1293f204e0b8SIan Munsie 
1294f204e0b8SIan Munsie 	CXL_READ_VSEC_LENGTH(dev, vsec, &vseclen);
1295f204e0b8SIan Munsie 	if (vseclen < CXL_VSEC_MIN_SIZE) {
1296bee30c70SIan Munsie 		dev_err(&dev->dev, "ABORTING: CXL VSEC too short\n");
1297f204e0b8SIan Munsie 		return -EINVAL;
1298f204e0b8SIan Munsie 	}
1299f204e0b8SIan Munsie 
1300f204e0b8SIan Munsie 	CXL_READ_VSEC_STATUS(dev, vsec, &adapter->vsec_status);
1301f204e0b8SIan Munsie 	CXL_READ_VSEC_PSL_REVISION(dev, vsec, &adapter->psl_rev);
1302f204e0b8SIan Munsie 	CXL_READ_VSEC_CAIA_MAJOR(dev, vsec, &adapter->caia_major);
1303f204e0b8SIan Munsie 	CXL_READ_VSEC_CAIA_MINOR(dev, vsec, &adapter->caia_minor);
1304f204e0b8SIan Munsie 	CXL_READ_VSEC_BASE_IMAGE(dev, vsec, &adapter->base_image);
1305f204e0b8SIan Munsie 	CXL_READ_VSEC_IMAGE_STATE(dev, vsec, &image_state);
1306f204e0b8SIan Munsie 	adapter->user_image_loaded = !!(image_state & CXL_VSEC_USER_IMAGE_LOADED);
13074beb5421SRyan Grimm 	adapter->perst_select_user = !!(image_state & CXL_VSEC_USER_IMAGE_LOADED);
1308aba81433SChristophe Lombard 	adapter->perst_loads_image = !!(image_state & CXL_VSEC_PERST_LOADS_IMAGE);
1309f204e0b8SIan Munsie 
1310f204e0b8SIan Munsie 	CXL_READ_VSEC_NAFUS(dev, vsec, &adapter->slices);
1311f204e0b8SIan Munsie 	CXL_READ_VSEC_AFU_DESC_OFF(dev, vsec, &afu_desc_off);
1312f204e0b8SIan Munsie 	CXL_READ_VSEC_AFU_DESC_SIZE(dev, vsec, &afu_desc_size);
1313f204e0b8SIan Munsie 	CXL_READ_VSEC_PS_OFF(dev, vsec, &ps_off);
1314f204e0b8SIan Munsie 	CXL_READ_VSEC_PS_SIZE(dev, vsec, &ps_size);
1315f204e0b8SIan Munsie 
1316f204e0b8SIan Munsie 	/* Convert everything to bytes, because there is NO WAY I'd look at the
1317f204e0b8SIan Munsie 	 * code a month later and forget what units these are in ;-) */
1318cbffa3a5SChristophe Lombard 	adapter->native->ps_off = ps_off * 64 * 1024;
1319f204e0b8SIan Munsie 	adapter->ps_size = ps_size * 64 * 1024;
1320cbffa3a5SChristophe Lombard 	adapter->native->afu_desc_off = afu_desc_off * 64 * 1024;
1321cbffa3a5SChristophe Lombard 	adapter->native->afu_desc_size = afu_desc_size * 64 * 1024;
1322f204e0b8SIan Munsie 
1323f204e0b8SIan Munsie 	/* Total IRQs - 1 PSL ERROR - #AFU*(1 slice error + 1 DSI) */
1324f204e0b8SIan Munsie 	adapter->user_irqs = pnv_cxl_get_irq_count(dev) - 1 - 2*adapter->slices;
1325f204e0b8SIan Munsie 
1326f204e0b8SIan Munsie 	return 0;
1327f204e0b8SIan Munsie }
1328f204e0b8SIan Munsie 
1329d79e6801SPhilippe Bergheaud /*
1330d79e6801SPhilippe Bergheaud  * Workaround a PCIe Host Bridge defect on some cards, that can cause
1331d79e6801SPhilippe Bergheaud  * malformed Transaction Layer Packet (TLP) errors to be erroneously
1332d79e6801SPhilippe Bergheaud  * reported. Mask this error in the Uncorrectable Error Mask Register.
1333d79e6801SPhilippe Bergheaud  *
1334d79e6801SPhilippe Bergheaud  * The upper nibble of the PSL revision is used to distinguish between
1335d79e6801SPhilippe Bergheaud  * different cards. The affected ones have it set to 0.
1336d79e6801SPhilippe Bergheaud  */
cxl_fixup_malformed_tlp(struct cxl * adapter,struct pci_dev * dev)1337d79e6801SPhilippe Bergheaud static void cxl_fixup_malformed_tlp(struct cxl *adapter, struct pci_dev *dev)
1338d79e6801SPhilippe Bergheaud {
1339d79e6801SPhilippe Bergheaud 	int aer;
1340d79e6801SPhilippe Bergheaud 	u32 data;
1341d79e6801SPhilippe Bergheaud 
1342d79e6801SPhilippe Bergheaud 	if (adapter->psl_rev & 0xf000)
1343d79e6801SPhilippe Bergheaud 		return;
1344d79e6801SPhilippe Bergheaud 	if (!(aer = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR)))
1345d79e6801SPhilippe Bergheaud 		return;
1346d79e6801SPhilippe Bergheaud 	pci_read_config_dword(dev, aer + PCI_ERR_UNCOR_MASK, &data);
1347d79e6801SPhilippe Bergheaud 	if (data & PCI_ERR_UNC_MALF_TLP)
1348d79e6801SPhilippe Bergheaud 		if (data & PCI_ERR_UNC_INTN)
1349d79e6801SPhilippe Bergheaud 			return;
1350d79e6801SPhilippe Bergheaud 	data |= PCI_ERR_UNC_MALF_TLP;
1351d79e6801SPhilippe Bergheaud 	data |= PCI_ERR_UNC_INTN;
1352d79e6801SPhilippe Bergheaud 	pci_write_config_dword(dev, aer + PCI_ERR_UNCOR_MASK, data);
1353d79e6801SPhilippe Bergheaud }
1354d79e6801SPhilippe Bergheaud 
cxl_compatible_caia_version(struct cxl * adapter)1355abd1d99bSChristophe Lombard static bool cxl_compatible_caia_version(struct cxl *adapter)
1356abd1d99bSChristophe Lombard {
1357abd1d99bSChristophe Lombard 	if (cxl_is_power8() && (adapter->caia_major == 1))
1358abd1d99bSChristophe Lombard 		return true;
1359abd1d99bSChristophe Lombard 
1360f24be42aSChristophe Lombard 	if (cxl_is_power9() && (adapter->caia_major == 2))
1361f24be42aSChristophe Lombard 		return true;
1362f24be42aSChristophe Lombard 
1363abd1d99bSChristophe Lombard 	return false;
1364abd1d99bSChristophe Lombard }
1365abd1d99bSChristophe Lombard 
cxl_vsec_looks_ok(struct cxl * adapter,struct pci_dev * dev)1366f204e0b8SIan Munsie static int cxl_vsec_looks_ok(struct cxl *adapter, struct pci_dev *dev)
1367f204e0b8SIan Munsie {
1368f204e0b8SIan Munsie 	if (adapter->vsec_status & CXL_STATUS_SECOND_PORT)
1369f204e0b8SIan Munsie 		return -EBUSY;
1370f204e0b8SIan Munsie 
1371f204e0b8SIan Munsie 	if (adapter->vsec_status & CXL_UNSUPPORTED_FEATURES) {
1372bee30c70SIan Munsie 		dev_err(&dev->dev, "ABORTING: CXL requires unsupported features\n");
1373f204e0b8SIan Munsie 		return -EINVAL;
1374f204e0b8SIan Munsie 	}
1375f204e0b8SIan Munsie 
1376abd1d99bSChristophe Lombard 	if (!cxl_compatible_caia_version(adapter)) {
1377abd1d99bSChristophe Lombard 		dev_info(&dev->dev, "Ignoring card. PSL type is not supported (caia version: %d)\n",
1378abd1d99bSChristophe Lombard 			 adapter->caia_major);
1379abd1d99bSChristophe Lombard 		return -ENODEV;
1380abd1d99bSChristophe Lombard 	}
1381abd1d99bSChristophe Lombard 
1382f204e0b8SIan Munsie 	if (!adapter->slices) {
1383f204e0b8SIan Munsie 		/* Once we support dynamic reprogramming we can use the card if
1384f204e0b8SIan Munsie 		 * it supports loadable AFUs */
1385bee30c70SIan Munsie 		dev_err(&dev->dev, "ABORTING: Device has no AFUs\n");
1386f204e0b8SIan Munsie 		return -EINVAL;
1387f204e0b8SIan Munsie 	}
1388f204e0b8SIan Munsie 
1389cbffa3a5SChristophe Lombard 	if (!adapter->native->afu_desc_off || !adapter->native->afu_desc_size) {
1390bee30c70SIan Munsie 		dev_err(&dev->dev, "ABORTING: VSEC shows no AFU descriptors\n");
1391f204e0b8SIan Munsie 		return -EINVAL;
1392f204e0b8SIan Munsie 	}
1393f204e0b8SIan Munsie 
1394cbffa3a5SChristophe Lombard 	if (adapter->ps_size > p2_size(dev) - adapter->native->ps_off) {
1395bee30c70SIan Munsie 		dev_err(&dev->dev, "ABORTING: Problem state size larger than "
1396f204e0b8SIan Munsie 				   "available in BAR2: 0x%llx > 0x%llx\n",
1397cbffa3a5SChristophe Lombard 			 adapter->ps_size, p2_size(dev) - adapter->native->ps_off);
1398f204e0b8SIan Munsie 		return -EINVAL;
1399f204e0b8SIan Munsie 	}
1400f204e0b8SIan Munsie 
1401f204e0b8SIan Munsie 	return 0;
1402f204e0b8SIan Munsie }
1403f204e0b8SIan Munsie 
cxl_pci_read_adapter_vpd(struct cxl * adapter,void * buf,size_t len)1404d601ea91SFrederic Barrat ssize_t cxl_pci_read_adapter_vpd(struct cxl *adapter, void *buf, size_t len)
1405d601ea91SFrederic Barrat {
1406d601ea91SFrederic Barrat 	return pci_read_vpd(to_pci_dev(adapter->dev.parent), 0, len, buf);
1407d601ea91SFrederic Barrat }
1408d601ea91SFrederic Barrat 
cxl_release_adapter(struct device * dev)1409f204e0b8SIan Munsie static void cxl_release_adapter(struct device *dev)
1410f204e0b8SIan Munsie {
1411f204e0b8SIan Munsie 	struct cxl *adapter = to_cxl_adapter(dev);
1412f204e0b8SIan Munsie 
1413f204e0b8SIan Munsie 	pr_devel("cxl_release_adapter\n");
1414f204e0b8SIan Munsie 
1415c044c415SDaniel Axtens 	cxl_remove_adapter_nr(adapter);
1416c044c415SDaniel Axtens 
1417cbffa3a5SChristophe Lombard 	kfree(adapter->native);
1418f204e0b8SIan Munsie 	kfree(adapter);
1419f204e0b8SIan Munsie }
1420f204e0b8SIan Munsie 
1421390fd592SPhilippe Bergheaud #define CXL_PSL_ErrIVTE_tberror (0x1ull << (63-31))
1422390fd592SPhilippe Bergheaud 
sanitise_adapter_regs(struct cxl * adapter)1423f204e0b8SIan Munsie static int sanitise_adapter_regs(struct cxl *adapter)
1424f204e0b8SIan Munsie {
1425bdd2e715SChristophe Lombard 	int rc = 0;
1426bdd2e715SChristophe Lombard 
1427390fd592SPhilippe Bergheaud 	/* Clear PSL tberror bit by writing 1 to it */
1428390fd592SPhilippe Bergheaud 	cxl_p1_write(adapter, CXL_PSL_ErrIVTE, CXL_PSL_ErrIVTE_tberror);
1429bdd2e715SChristophe Lombard 
1430f24be42aSChristophe Lombard 	if (adapter->native->sl_ops->invalidate_all) {
1431f24be42aSChristophe Lombard 		/* do not invalidate ERAT entries when not reloading on PERST */
1432f24be42aSChristophe Lombard 		if (cxl_is_power9() && (adapter->perst_loads_image))
1433f24be42aSChristophe Lombard 			return 0;
1434bdd2e715SChristophe Lombard 		rc = adapter->native->sl_ops->invalidate_all(adapter);
1435f24be42aSChristophe Lombard 	}
1436bdd2e715SChristophe Lombard 
1437bdd2e715SChristophe Lombard 	return rc;
1438f204e0b8SIan Munsie }
1439f204e0b8SIan Munsie 
1440c044c415SDaniel Axtens /* This should contain *only* operations that can safely be done in
1441c044c415SDaniel Axtens  * both creation and recovery.
1442c044c415SDaniel Axtens  */
cxl_configure_adapter(struct cxl * adapter,struct pci_dev * dev)1443c044c415SDaniel Axtens static int cxl_configure_adapter(struct cxl *adapter, struct pci_dev *dev)
1444f204e0b8SIan Munsie {
1445f204e0b8SIan Munsie 	int rc;
1446f204e0b8SIan Munsie 
1447c044c415SDaniel Axtens 	adapter->dev.parent = &dev->dev;
1448c044c415SDaniel Axtens 	adapter->dev.release = cxl_release_adapter;
1449c044c415SDaniel Axtens 	pci_set_drvdata(dev, adapter);
1450f204e0b8SIan Munsie 
1451c044c415SDaniel Axtens 	rc = pci_enable_device(dev);
1452c044c415SDaniel Axtens 	if (rc) {
1453c044c415SDaniel Axtens 		dev_err(&dev->dev, "pci_enable_device failed: %i\n", rc);
1454c044c415SDaniel Axtens 		return rc;
1455c044c415SDaniel Axtens 	}
1456f204e0b8SIan Munsie 
1457bee30c70SIan Munsie 	if ((rc = cxl_read_vsec(adapter, dev)))
1458c044c415SDaniel Axtens 		return rc;
1459bee30c70SIan Munsie 
1460bee30c70SIan Munsie 	if ((rc = cxl_vsec_looks_ok(adapter, dev)))
1461c044c415SDaniel Axtens 	        return rc;
1462bee30c70SIan Munsie 
1463d79e6801SPhilippe Bergheaud 	cxl_fixup_malformed_tlp(adapter, dev);
1464d79e6801SPhilippe Bergheaud 
1465bee30c70SIan Munsie 	if ((rc = setup_cxl_bars(dev)))
1466c044c415SDaniel Axtens 		return rc;
1467bee30c70SIan Munsie 
146829fea8aaSAlastair D'Silva 	if ((rc = switch_card_to_cxl(dev)))
1469c044c415SDaniel Axtens 		return rc;
1470f204e0b8SIan Munsie 
14714beb5421SRyan Grimm 	if ((rc = cxl_update_image_control(adapter)))
1472c044c415SDaniel Axtens 		return rc;
14734beb5421SRyan Grimm 
1474f204e0b8SIan Munsie 	if ((rc = cxl_map_adapter_regs(adapter, dev)))
1475c044c415SDaniel Axtens 		return rc;
1476f204e0b8SIan Munsie 
1477f204e0b8SIan Munsie 	if ((rc = sanitise_adapter_regs(adapter)))
1478c044c415SDaniel Axtens 		goto err;
1479f204e0b8SIan Munsie 
14806d382616SFrederic Barrat 	if ((rc = adapter->native->sl_ops->adapter_regs_init(adapter, dev)))
1481c044c415SDaniel Axtens 		goto err;
1482f204e0b8SIan Munsie 
148348b3adf3SIan Munsie 	/* Required for devices using CAPP DMA mode, harmless for others */
148448b3adf3SIan Munsie 	pci_set_master(dev);
148548b3adf3SIan Munsie 
1486497a0790SPhilippe Bergheaud 	adapter->tunneled_ops_supported = false;
1487497a0790SPhilippe Bergheaud 
1488497a0790SPhilippe Bergheaud 	if (cxl_is_power9()) {
1489401dca8cSPhilippe Bergheaud 		if (pnv_pci_set_tunnel_bar(dev, 0x00020000E0000000ull, 1))
1490401dca8cSPhilippe Bergheaud 			dev_info(&dev->dev, "Tunneled operations unsupported\n");
1491497a0790SPhilippe Bergheaud 		else
1492497a0790SPhilippe Bergheaud 			adapter->tunneled_ops_supported = true;
1493497a0790SPhilippe Bergheaud 	}
1494401dca8cSPhilippe Bergheaud 
1495b385c9e9SIan Munsie 	if ((rc = pnv_phb_to_cxl_mode(dev, adapter->native->sl_ops->capi_mode)))
1496c044c415SDaniel Axtens 		goto err;
1497f204e0b8SIan Munsie 
14981212aa1cSRyan Grimm 	/* If recovery happened, the last step is to turn on snooping.
14991212aa1cSRyan Grimm 	 * In the non-recovery case this has no effect */
1500c044c415SDaniel Axtens 	if ((rc = pnv_phb_to_cxl_mode(dev, OPAL_PHB_CAPI_MODE_SNOOP_ON)))
1501c044c415SDaniel Axtens 		goto err;
15021212aa1cSRyan Grimm 
1503e009a7e8SFrederic Barrat 	/* Ignore error, adapter init is not dependant on timebase sync */
1504e009a7e8SFrederic Barrat 	cxl_setup_psl_timebase(adapter, dev);
1505390fd592SPhilippe Bergheaud 
15062b04cf31SFrederic Barrat 	if ((rc = cxl_native_register_psl_err_irq(adapter)))
1507c044c415SDaniel Axtens 		goto err;
1508c044c415SDaniel Axtens 
1509c044c415SDaniel Axtens 	return 0;
1510c044c415SDaniel Axtens 
1511c044c415SDaniel Axtens err:
1512c044c415SDaniel Axtens 	cxl_unmap_adapter_regs(adapter);
1513c044c415SDaniel Axtens 	return rc;
1514c044c415SDaniel Axtens 
1515c044c415SDaniel Axtens }
1516c044c415SDaniel Axtens 
cxl_deconfigure_adapter(struct cxl * adapter)1517c044c415SDaniel Axtens static void cxl_deconfigure_adapter(struct cxl *adapter)
1518c044c415SDaniel Axtens {
1519c044c415SDaniel Axtens 	struct pci_dev *pdev = to_pci_dev(adapter->dev.parent);
1520c044c415SDaniel Axtens 
1521401dca8cSPhilippe Bergheaud 	if (cxl_is_power9())
1522401dca8cSPhilippe Bergheaud 		pnv_pci_set_tunnel_bar(pdev, 0x00020000E0000000ull, 0);
1523401dca8cSPhilippe Bergheaud 
15242b04cf31SFrederic Barrat 	cxl_native_release_psl_err_irq(adapter);
1525c044c415SDaniel Axtens 	cxl_unmap_adapter_regs(adapter);
1526c044c415SDaniel Axtens 
1527c044c415SDaniel Axtens 	pci_disable_device(pdev);
1528c044c415SDaniel Axtens }
1529c044c415SDaniel Axtens 
cxl_stop_trace_psl9(struct cxl * adapter)1530cbb55eebSVaibhav Jain static void cxl_stop_trace_psl9(struct cxl *adapter)
1531cbb55eebSVaibhav Jain {
1532cbb55eebSVaibhav Jain 	int traceid;
1533cbb55eebSVaibhav Jain 	u64 trace_state, trace_mask;
1534cbb55eebSVaibhav Jain 	struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
1535cbb55eebSVaibhav Jain 
1536cbb55eebSVaibhav Jain 	/* read each tracearray state and issue mmio to stop them is needed */
1537cbb55eebSVaibhav Jain 	for (traceid = 0; traceid <= CXL_PSL9_TRACEID_MAX; ++traceid) {
1538cbb55eebSVaibhav Jain 		trace_state = cxl_p1_read(adapter, CXL_PSL9_CTCCFG);
1539cbb55eebSVaibhav Jain 		trace_mask = (0x3ULL << (62 - traceid * 2));
1540cbb55eebSVaibhav Jain 		trace_state = (trace_state & trace_mask) >> (62 - traceid * 2);
1541cbb55eebSVaibhav Jain 		dev_dbg(&dev->dev, "cxl: Traceid-%d trace_state=0x%0llX\n",
1542cbb55eebSVaibhav Jain 			traceid, trace_state);
1543cbb55eebSVaibhav Jain 
1544cbb55eebSVaibhav Jain 		/* issue mmio if the trace array isn't in FIN state */
1545cbb55eebSVaibhav Jain 		if (trace_state != CXL_PSL9_TRACESTATE_FIN)
1546cbb55eebSVaibhav Jain 			cxl_p1_write(adapter, CXL_PSL9_TRACECFG,
1547cbb55eebSVaibhav Jain 				     0x8400000000000000ULL | traceid);
1548cbb55eebSVaibhav Jain 	}
1549cbb55eebSVaibhav Jain }
1550cbb55eebSVaibhav Jain 
cxl_stop_trace_psl8(struct cxl * adapter)1551cbb55eebSVaibhav Jain static void cxl_stop_trace_psl8(struct cxl *adapter)
1552cbb55eebSVaibhav Jain {
1553cbb55eebSVaibhav Jain 	int slice;
1554cbb55eebSVaibhav Jain 
1555cbb55eebSVaibhav Jain 	/* Stop the trace */
1556cbb55eebSVaibhav Jain 	cxl_p1_write(adapter, CXL_PSL_TRACE, 0x8000000000000017LL);
1557cbb55eebSVaibhav Jain 
1558cbb55eebSVaibhav Jain 	/* Stop the slice traces */
1559cbb55eebSVaibhav Jain 	spin_lock(&adapter->afu_list_lock);
1560cbb55eebSVaibhav Jain 	for (slice = 0; slice < adapter->slices; slice++) {
1561cbb55eebSVaibhav Jain 		if (adapter->afu[slice])
1562cbb55eebSVaibhav Jain 			cxl_p1n_write(adapter->afu[slice], CXL_PSL_SLICE_TRACE,
1563cbb55eebSVaibhav Jain 				      0x8000000000000000LL);
1564cbb55eebSVaibhav Jain 	}
1565cbb55eebSVaibhav Jain 	spin_unlock(&adapter->afu_list_lock);
1566cbb55eebSVaibhav Jain }
1567cbb55eebSVaibhav Jain 
1568f24be42aSChristophe Lombard static const struct cxl_service_layer_ops psl9_ops = {
1569f24be42aSChristophe Lombard 	.adapter_regs_init = init_implementation_adapter_regs_psl9,
1570f24be42aSChristophe Lombard 	.invalidate_all = cxl_invalidate_all_psl9,
1571f24be42aSChristophe Lombard 	.afu_regs_init = init_implementation_afu_regs_psl9,
1572f24be42aSChristophe Lombard 	.sanitise_afu_regs = sanitise_afu_regs_psl9,
15736d382616SFrederic Barrat 	.register_serr_irq = cxl_native_register_serr_irq,
15746d382616SFrederic Barrat 	.release_serr_irq = cxl_native_release_serr_irq,
1575f24be42aSChristophe Lombard 	.handle_interrupt = cxl_irq_psl9,
1576f24be42aSChristophe Lombard 	.fail_irq = cxl_fail_irq_psl,
1577f24be42aSChristophe Lombard 	.activate_dedicated_process = cxl_activate_dedicated_process_psl9,
1578f24be42aSChristophe Lombard 	.attach_afu_directed = cxl_attach_afu_directed_psl9,
1579f24be42aSChristophe Lombard 	.attach_dedicated_process = cxl_attach_dedicated_process_psl9,
1580f24be42aSChristophe Lombard 	.update_dedicated_ivtes = cxl_update_dedicated_ivtes_psl9,
1581f24be42aSChristophe Lombard 	.debugfs_add_adapter_regs = cxl_debugfs_add_adapter_regs_psl9,
1582f24be42aSChristophe Lombard 	.debugfs_add_afu_regs = cxl_debugfs_add_afu_regs_psl9,
1583f24be42aSChristophe Lombard 	.psl_irq_dump_registers = cxl_native_irq_dump_regs_psl9,
1584990f19aeSVaibhav Jain 	.err_irq_dump_registers = cxl_native_err_irq_dump_regs_psl9,
1585f24be42aSChristophe Lombard 	.debugfs_stop_trace = cxl_stop_trace_psl9,
1586f24be42aSChristophe Lombard 	.timebase_read = timebase_read_psl9,
1587f24be42aSChristophe Lombard 	.capi_mode = OPAL_PHB_CAPI_MODE_CAPI,
1588f24be42aSChristophe Lombard 	.needs_reset_before_disable = true,
1589f24be42aSChristophe Lombard };
1590f24be42aSChristophe Lombard 
159164663f37SChristophe Lombard static const struct cxl_service_layer_ops psl8_ops = {
159264663f37SChristophe Lombard 	.adapter_regs_init = init_implementation_adapter_regs_psl8,
159364663f37SChristophe Lombard 	.invalidate_all = cxl_invalidate_all_psl8,
159464663f37SChristophe Lombard 	.afu_regs_init = init_implementation_afu_regs_psl8,
159564663f37SChristophe Lombard 	.sanitise_afu_regs = sanitise_afu_regs_psl8,
15966d382616SFrederic Barrat 	.register_serr_irq = cxl_native_register_serr_irq,
15976d382616SFrederic Barrat 	.release_serr_irq = cxl_native_release_serr_irq,
159864663f37SChristophe Lombard 	.handle_interrupt = cxl_irq_psl8,
1599bdd2e715SChristophe Lombard 	.fail_irq = cxl_fail_irq_psl,
160064663f37SChristophe Lombard 	.activate_dedicated_process = cxl_activate_dedicated_process_psl8,
160164663f37SChristophe Lombard 	.attach_afu_directed = cxl_attach_afu_directed_psl8,
160264663f37SChristophe Lombard 	.attach_dedicated_process = cxl_attach_dedicated_process_psl8,
160364663f37SChristophe Lombard 	.update_dedicated_ivtes = cxl_update_dedicated_ivtes_psl8,
160464663f37SChristophe Lombard 	.debugfs_add_adapter_regs = cxl_debugfs_add_adapter_regs_psl8,
160564663f37SChristophe Lombard 	.debugfs_add_afu_regs = cxl_debugfs_add_afu_regs_psl8,
160664663f37SChristophe Lombard 	.psl_irq_dump_registers = cxl_native_irq_dump_regs_psl8,
1607990f19aeSVaibhav Jain 	.err_irq_dump_registers = cxl_native_err_irq_dump_regs_psl8,
160864663f37SChristophe Lombard 	.debugfs_stop_trace = cxl_stop_trace_psl8,
160964663f37SChristophe Lombard 	.write_timebase_ctrl = write_timebase_ctrl_psl8,
161064663f37SChristophe Lombard 	.timebase_read = timebase_read_psl8,
1611b385c9e9SIan Munsie 	.capi_mode = OPAL_PHB_CAPI_MODE_CAPI,
16125e7823c9SIan Munsie 	.needs_reset_before_disable = true,
16136d382616SFrederic Barrat };
16146d382616SFrederic Barrat 
set_sl_ops(struct cxl * adapter,struct pci_dev * dev)16156d382616SFrederic Barrat static void set_sl_ops(struct cxl *adapter, struct pci_dev *dev)
16166d382616SFrederic Barrat {
1617abd1d99bSChristophe Lombard 	if (cxl_is_power8()) {
161864663f37SChristophe Lombard 		dev_info(&dev->dev, "Device uses a PSL8\n");
161964663f37SChristophe Lombard 		adapter->native->sl_ops = &psl8_ops;
1620f24be42aSChristophe Lombard 	} else {
1621f24be42aSChristophe Lombard 		dev_info(&dev->dev, "Device uses a PSL9\n");
1622f24be42aSChristophe Lombard 		adapter->native->sl_ops = &psl9_ops;
16236d382616SFrederic Barrat 	}
16246d382616SFrederic Barrat }
16256d382616SFrederic Barrat 
16266d382616SFrederic Barrat 
cxl_pci_init_adapter(struct pci_dev * dev)16272b04cf31SFrederic Barrat static struct cxl *cxl_pci_init_adapter(struct pci_dev *dev)
1628c044c415SDaniel Axtens {
1629c044c415SDaniel Axtens 	struct cxl *adapter;
1630c044c415SDaniel Axtens 	int rc;
1631c044c415SDaniel Axtens 
1632c044c415SDaniel Axtens 	adapter = cxl_alloc_adapter();
1633c044c415SDaniel Axtens 	if (!adapter)
1634c044c415SDaniel Axtens 		return ERR_PTR(-ENOMEM);
1635c044c415SDaniel Axtens 
1636cbffa3a5SChristophe Lombard 	adapter->native = kzalloc(sizeof(struct cxl_native), GFP_KERNEL);
1637cbffa3a5SChristophe Lombard 	if (!adapter->native) {
1638cbffa3a5SChristophe Lombard 		rc = -ENOMEM;
1639cbffa3a5SChristophe Lombard 		goto err_release;
1640cbffa3a5SChristophe Lombard 	}
1641cbffa3a5SChristophe Lombard 
16426d382616SFrederic Barrat 	set_sl_ops(adapter, dev);
16436d382616SFrederic Barrat 
1644c044c415SDaniel Axtens 	/* Set defaults for parameters which need to persist over
1645c044c415SDaniel Axtens 	 * configure/reconfigure
1646c044c415SDaniel Axtens 	 */
1647c044c415SDaniel Axtens 	adapter->perst_loads_image = true;
164813e68d8bSDaniel Axtens 	adapter->perst_same_image = false;
1649c044c415SDaniel Axtens 
1650c044c415SDaniel Axtens 	rc = cxl_configure_adapter(adapter, dev);
1651c044c415SDaniel Axtens 	if (rc) {
1652c044c415SDaniel Axtens 		pci_disable_device(dev);
1653cbffa3a5SChristophe Lombard 		goto err_release;
1654c044c415SDaniel Axtens 	}
1655f204e0b8SIan Munsie 
1656f204e0b8SIan Munsie 	/* Don't care if this one fails: */
1657f204e0b8SIan Munsie 	cxl_debugfs_adapter_add(adapter);
1658f204e0b8SIan Munsie 
1659f204e0b8SIan Munsie 	/*
1660f204e0b8SIan Munsie 	 * After we call this function we must not free the adapter directly,
1661f204e0b8SIan Munsie 	 * even if it returns an error!
1662f204e0b8SIan Munsie 	 */
1663f204e0b8SIan Munsie 	if ((rc = cxl_register_adapter(adapter)))
16648bf03f55SYang Yingliang 		goto err_put_dev;
1665f204e0b8SIan Munsie 
1666f204e0b8SIan Munsie 	if ((rc = cxl_sysfs_adapter_add(adapter)))
16678bf03f55SYang Yingliang 		goto err_del_dev;
1668f204e0b8SIan Munsie 
1669ea9a26d1SVaibhav Jain 	/* Release the context lock as adapter is configured */
1670ea9a26d1SVaibhav Jain 	cxl_adapter_context_unlock(adapter);
1671ea9a26d1SVaibhav Jain 
1672f204e0b8SIan Munsie 	return adapter;
1673f204e0b8SIan Munsie 
16748bf03f55SYang Yingliang err_del_dev:
16758bf03f55SYang Yingliang 	device_del(&adapter->dev);
16768bf03f55SYang Yingliang err_put_dev:
1677c044c415SDaniel Axtens 	/* This should mirror cxl_remove_adapter, except without the
1678c044c415SDaniel Axtens 	 * sysfs parts
1679c044c415SDaniel Axtens 	 */
1680f204e0b8SIan Munsie 	cxl_debugfs_adapter_remove(adapter);
1681c044c415SDaniel Axtens 	cxl_deconfigure_adapter(adapter);
16828bf03f55SYang Yingliang 	put_device(&adapter->dev);
1683f204e0b8SIan Munsie 	return ERR_PTR(rc);
1684cbffa3a5SChristophe Lombard 
1685cbffa3a5SChristophe Lombard err_release:
1686cbffa3a5SChristophe Lombard 	cxl_release_adapter(&adapter->dev);
1687cbffa3a5SChristophe Lombard 	return ERR_PTR(rc);
1688f204e0b8SIan Munsie }
1689f204e0b8SIan Munsie 
cxl_pci_remove_adapter(struct cxl * adapter)16902b04cf31SFrederic Barrat static void cxl_pci_remove_adapter(struct cxl *adapter)
1691f204e0b8SIan Munsie {
1692c044c415SDaniel Axtens 	pr_devel("cxl_remove_adapter\n");
1693f204e0b8SIan Munsie 
1694f204e0b8SIan Munsie 	cxl_sysfs_adapter_remove(adapter);
1695f204e0b8SIan Munsie 	cxl_debugfs_adapter_remove(adapter);
1696c044c415SDaniel Axtens 
1697f24be42aSChristophe Lombard 	/*
1698f24be42aSChristophe Lombard 	 * Flush adapter datacache as its about to be removed.
1699f24be42aSChristophe Lombard 	 */
1700d7b1946cSVaibhav Jain 	cxl_data_cache_flush(adapter);
1701d7b1946cSVaibhav Jain 
1702c044c415SDaniel Axtens 	cxl_deconfigure_adapter(adapter);
1703f204e0b8SIan Munsie 
1704f204e0b8SIan Munsie 	device_unregister(&adapter->dev);
1705f204e0b8SIan Munsie }
1706f204e0b8SIan Munsie 
17073b3dcd61SPhilippe Bergheaud #define CXL_MAX_PCIEX_PARENT 2
17083b3dcd61SPhilippe Bergheaud 
cxl_slot_is_switched(struct pci_dev * dev)17093ced8d73SChristophe Lombard int cxl_slot_is_switched(struct pci_dev *dev)
17103b3dcd61SPhilippe Bergheaud {
17113b3dcd61SPhilippe Bergheaud 	struct device_node *np;
17123b3dcd61SPhilippe Bergheaud 	int depth = 0;
17133b3dcd61SPhilippe Bergheaud 
17143b3dcd61SPhilippe Bergheaud 	if (!(np = pci_device_to_OF_node(dev))) {
17153b3dcd61SPhilippe Bergheaud 		pr_err("cxl: np = NULL\n");
17163b3dcd61SPhilippe Bergheaud 		return -ENODEV;
17173b3dcd61SPhilippe Bergheaud 	}
17183b3dcd61SPhilippe Bergheaud 	of_node_get(np);
17193b3dcd61SPhilippe Bergheaud 	while (np) {
17203b3dcd61SPhilippe Bergheaud 		np = of_get_next_parent(np);
1721d2db0979SRob Herring 		if (!of_node_is_type(np, "pciex"))
17223b3dcd61SPhilippe Bergheaud 			break;
17233b3dcd61SPhilippe Bergheaud 		depth++;
17243b3dcd61SPhilippe Bergheaud 	}
17253b3dcd61SPhilippe Bergheaud 	of_node_put(np);
17263b3dcd61SPhilippe Bergheaud 	return (depth > CXL_MAX_PCIEX_PARENT);
17273b3dcd61SPhilippe Bergheaud }
17283b3dcd61SPhilippe Bergheaud 
cxl_probe(struct pci_dev * dev,const struct pci_device_id * id)1729f204e0b8SIan Munsie static int cxl_probe(struct pci_dev *dev, const struct pci_device_id *id)
1730f204e0b8SIan Munsie {
1731f204e0b8SIan Munsie 	struct cxl *adapter;
1732f204e0b8SIan Munsie 	int slice;
1733f204e0b8SIan Munsie 	int rc;
1734f204e0b8SIan Munsie 
173517eb3eefSVaibhav Jain 	if (cxl_pci_is_vphb_device(dev)) {
173617eb3eefSVaibhav Jain 		dev_dbg(&dev->dev, "cxl_init_adapter: Ignoring cxl vphb device\n");
173717eb3eefSVaibhav Jain 		return -ENODEV;
173817eb3eefSVaibhav Jain 	}
173917eb3eefSVaibhav Jain 
17403b3dcd61SPhilippe Bergheaud 	if (cxl_slot_is_switched(dev)) {
17413b3dcd61SPhilippe Bergheaud 		dev_info(&dev->dev, "Ignoring card on incompatible PCI slot\n");
17423b3dcd61SPhilippe Bergheaud 		return -ENODEV;
17433b3dcd61SPhilippe Bergheaud 	}
17443b3dcd61SPhilippe Bergheaud 
1745f24be42aSChristophe Lombard 	if (cxl_is_power9() && !radix_enabled()) {
1746f24be42aSChristophe Lombard 		dev_info(&dev->dev, "Only Radix mode supported\n");
1747f24be42aSChristophe Lombard 		return -ENODEV;
1748f24be42aSChristophe Lombard 	}
1749f24be42aSChristophe Lombard 
1750f204e0b8SIan Munsie 	if (cxl_verbose)
1751f204e0b8SIan Munsie 		dump_cxl_config_space(dev);
1752f204e0b8SIan Munsie 
17532b04cf31SFrederic Barrat 	adapter = cxl_pci_init_adapter(dev);
1754f204e0b8SIan Munsie 	if (IS_ERR(adapter)) {
1755f204e0b8SIan Munsie 		dev_err(&dev->dev, "cxl_init_adapter failed: %li\n", PTR_ERR(adapter));
1756f204e0b8SIan Munsie 		return PTR_ERR(adapter);
1757f204e0b8SIan Munsie 	}
1758f204e0b8SIan Munsie 
1759f204e0b8SIan Munsie 	for (slice = 0; slice < adapter->slices; slice++) {
17602b04cf31SFrederic Barrat 		if ((rc = pci_init_afu(adapter, slice, dev))) {
1761f204e0b8SIan Munsie 			dev_err(&dev->dev, "AFU %i failed to initialise: %i\n", slice, rc);
1762d76427b0SDaniel Axtens 			continue;
1763d76427b0SDaniel Axtens 		}
1764d76427b0SDaniel Axtens 
1765d76427b0SDaniel Axtens 		rc = cxl_afu_select_best_mode(adapter->afu[slice]);
1766d76427b0SDaniel Axtens 		if (rc)
1767d76427b0SDaniel Axtens 			dev_err(&dev->dev, "AFU %i failed to start: %i\n", slice, rc);
1768f204e0b8SIan Munsie 	}
1769f204e0b8SIan Munsie 
1770f204e0b8SIan Munsie 	return 0;
1771f204e0b8SIan Munsie }
1772f204e0b8SIan Munsie 
cxl_remove(struct pci_dev * dev)1773f204e0b8SIan Munsie static void cxl_remove(struct pci_dev *dev)
1774f204e0b8SIan Munsie {
1775f204e0b8SIan Munsie 	struct cxl *adapter = pci_get_drvdata(dev);
17766f7f0b3dSMichael Neuling 	struct cxl_afu *afu;
17776f7f0b3dSMichael Neuling 	int i;
1778f204e0b8SIan Munsie 
1779f204e0b8SIan Munsie 	/*
1780f204e0b8SIan Munsie 	 * Lock to prevent someone grabbing a ref through the adapter list as
1781f204e0b8SIan Munsie 	 * we are removing it
1782f204e0b8SIan Munsie 	 */
17836f7f0b3dSMichael Neuling 	for (i = 0; i < adapter->slices; i++) {
17846f7f0b3dSMichael Neuling 		afu = adapter->afu[i];
17852b04cf31SFrederic Barrat 		cxl_pci_remove_afu(afu);
17866f7f0b3dSMichael Neuling 	}
17872b04cf31SFrederic Barrat 	cxl_pci_remove_adapter(adapter);
1788f204e0b8SIan Munsie }
1789f204e0b8SIan Munsie 
cxl_vphb_error_detected(struct cxl_afu * afu,pci_channel_state_t state)17909e8df8a2SDaniel Axtens static pci_ers_result_t cxl_vphb_error_detected(struct cxl_afu *afu,
17919e8df8a2SDaniel Axtens 						pci_channel_state_t state)
17929e8df8a2SDaniel Axtens {
17939e8df8a2SDaniel Axtens 	struct pci_dev *afu_dev;
17944e59b754SBjorn Helgaas 	struct pci_driver *afu_drv;
17954e59b754SBjorn Helgaas 	const struct pci_error_handlers *err_handler;
17969e8df8a2SDaniel Axtens 	pci_ers_result_t result = PCI_ERS_RESULT_NEED_RESET;
17979e8df8a2SDaniel Axtens 	pci_ers_result_t afu_result = PCI_ERS_RESULT_NEED_RESET;
17989e8df8a2SDaniel Axtens 
17999e8df8a2SDaniel Axtens 	/* There should only be one entry, but go through the list
18009e8df8a2SDaniel Axtens 	 * anyway
18019e8df8a2SDaniel Axtens 	 */
1802edeb304fSVaibhav Jain 	if (afu == NULL || afu->phb == NULL)
180312841f87SVaibhav Jain 		return result;
180412841f87SVaibhav Jain 
18059e8df8a2SDaniel Axtens 	list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
180616bd44e5SUwe Kleine-König 		afu_drv = to_pci_driver(afu_dev->dev.driver);
18074e59b754SBjorn Helgaas 		if (!afu_drv)
18089e8df8a2SDaniel Axtens 			continue;
18099e8df8a2SDaniel Axtens 
18109e8df8a2SDaniel Axtens 		afu_dev->error_state = state;
18119e8df8a2SDaniel Axtens 
18124e59b754SBjorn Helgaas 		err_handler = afu_drv->err_handler;
18134e59b754SBjorn Helgaas 		if (err_handler)
18144e59b754SBjorn Helgaas 			afu_result = err_handler->error_detected(afu_dev,
18159e8df8a2SDaniel Axtens 								 state);
18169e8df8a2SDaniel Axtens 		/* Disconnect trumps all, NONE trumps NEED_RESET */
18179e8df8a2SDaniel Axtens 		if (afu_result == PCI_ERS_RESULT_DISCONNECT)
18189e8df8a2SDaniel Axtens 			result = PCI_ERS_RESULT_DISCONNECT;
18199e8df8a2SDaniel Axtens 		else if ((afu_result == PCI_ERS_RESULT_NONE) &&
18209e8df8a2SDaniel Axtens 			 (result == PCI_ERS_RESULT_NEED_RESET))
18219e8df8a2SDaniel Axtens 			result = PCI_ERS_RESULT_NONE;
18229e8df8a2SDaniel Axtens 	}
18239e8df8a2SDaniel Axtens 	return result;
18249e8df8a2SDaniel Axtens }
18259e8df8a2SDaniel Axtens 
cxl_pci_error_detected(struct pci_dev * pdev,pci_channel_state_t state)18269e8df8a2SDaniel Axtens static pci_ers_result_t cxl_pci_error_detected(struct pci_dev *pdev,
18279e8df8a2SDaniel Axtens 					       pci_channel_state_t state)
18289e8df8a2SDaniel Axtens {
18299e8df8a2SDaniel Axtens 	struct cxl *adapter = pci_get_drvdata(pdev);
18309e8df8a2SDaniel Axtens 	struct cxl_afu *afu;
1831edeb304fSVaibhav Jain 	pci_ers_result_t result = PCI_ERS_RESULT_NEED_RESET;
1832edeb304fSVaibhav Jain 	pci_ers_result_t afu_result = PCI_ERS_RESULT_NEED_RESET;
18339e8df8a2SDaniel Axtens 	int i;
18349e8df8a2SDaniel Axtens 
18359e8df8a2SDaniel Axtens 	/* At this point, we could still have an interrupt pending.
18369e8df8a2SDaniel Axtens 	 * Let's try to get them out of the way before they do
18379e8df8a2SDaniel Axtens 	 * anything we don't like.
18389e8df8a2SDaniel Axtens 	 */
18399e8df8a2SDaniel Axtens 	schedule();
18409e8df8a2SDaniel Axtens 
18419e8df8a2SDaniel Axtens 	/* If we're permanently dead, give up. */
18429e8df8a2SDaniel Axtens 	if (state == pci_channel_io_perm_failure) {
1843edeb304fSVaibhav Jain 		spin_lock(&adapter->afu_list_lock);
18449e8df8a2SDaniel Axtens 		for (i = 0; i < adapter->slices; i++) {
18459e8df8a2SDaniel Axtens 			afu = adapter->afu[i];
184607f5ab60SVaibhav Jain 			/*
184707f5ab60SVaibhav Jain 			 * Tell the AFU drivers; but we don't care what they
184807f5ab60SVaibhav Jain 			 * say, we're going away.
184907f5ab60SVaibhav Jain 			 */
18509e8df8a2SDaniel Axtens 			cxl_vphb_error_detected(afu, state);
18519e8df8a2SDaniel Axtens 		}
1852edeb304fSVaibhav Jain 		spin_unlock(&adapter->afu_list_lock);
18539e8df8a2SDaniel Axtens 		return PCI_ERS_RESULT_DISCONNECT;
18549e8df8a2SDaniel Axtens 	}
18559e8df8a2SDaniel Axtens 
18569e8df8a2SDaniel Axtens 	/* Are we reflashing?
18579e8df8a2SDaniel Axtens 	 *
18589e8df8a2SDaniel Axtens 	 * If we reflash, we could come back as something entirely
18599e8df8a2SDaniel Axtens 	 * different, including a non-CAPI card. As such, by default
18609e8df8a2SDaniel Axtens 	 * we don't participate in the process. We'll be unbound and
18619e8df8a2SDaniel Axtens 	 * the slot re-probed. (TODO: check EEH doesn't blindly rebind
18629e8df8a2SDaniel Axtens 	 * us!)
18639e8df8a2SDaniel Axtens 	 *
18649e8df8a2SDaniel Axtens 	 * However, this isn't the entire story: for reliablity
18659e8df8a2SDaniel Axtens 	 * reasons, we usually want to reflash the FPGA on PERST in
18669e8df8a2SDaniel Axtens 	 * order to get back to a more reliable known-good state.
18679e8df8a2SDaniel Axtens 	 *
18689e8df8a2SDaniel Axtens 	 * This causes us a bit of a problem: if we reflash we can't
18699e8df8a2SDaniel Axtens 	 * trust that we'll come back the same - we could have a new
18709e8df8a2SDaniel Axtens 	 * image and been PERSTed in order to load that
18719e8df8a2SDaniel Axtens 	 * image. However, most of the time we actually *will* come
18729e8df8a2SDaniel Axtens 	 * back the same - for example a regular EEH event.
18739e8df8a2SDaniel Axtens 	 *
18749e8df8a2SDaniel Axtens 	 * Therefore, we allow the user to assert that the image is
18759e8df8a2SDaniel Axtens 	 * indeed the same and that we should continue on into EEH
18769e8df8a2SDaniel Axtens 	 * anyway.
18779e8df8a2SDaniel Axtens 	 */
18789e8df8a2SDaniel Axtens 	if (adapter->perst_loads_image && !adapter->perst_same_image) {
18799e8df8a2SDaniel Axtens 		/* TODO take the PHB out of CXL mode */
18809e8df8a2SDaniel Axtens 		dev_info(&pdev->dev, "reflashing, so opting out of EEH!\n");
18819e8df8a2SDaniel Axtens 		return PCI_ERS_RESULT_NONE;
18829e8df8a2SDaniel Axtens 	}
18839e8df8a2SDaniel Axtens 
18849e8df8a2SDaniel Axtens 	/*
18859e8df8a2SDaniel Axtens 	 * At this point, we want to try to recover.  We'll always
18869e8df8a2SDaniel Axtens 	 * need a complete slot reset: we don't trust any other reset.
18879e8df8a2SDaniel Axtens 	 *
18889e8df8a2SDaniel Axtens 	 * Now, we go through each AFU:
18899e8df8a2SDaniel Axtens 	 *  - We send the driver, if bound, an error_detected callback.
18909e8df8a2SDaniel Axtens 	 *    We expect it to clean up, but it can also tell us to give
18919e8df8a2SDaniel Axtens 	 *    up and permanently detach the card. To simplify things, if
18929e8df8a2SDaniel Axtens 	 *    any bound AFU driver doesn't support EEH, we give up on EEH.
18939e8df8a2SDaniel Axtens 	 *
18949e8df8a2SDaniel Axtens 	 *  - We detach all contexts associated with the AFU. This
18959e8df8a2SDaniel Axtens 	 *    does not free them, but puts them into a CLOSED state
18969e8df8a2SDaniel Axtens 	 *    which causes any the associated files to return useful
18979e8df8a2SDaniel Axtens 	 *    errors to userland. It also unmaps, but does not free,
18989e8df8a2SDaniel Axtens 	 *    any IRQs.
18999e8df8a2SDaniel Axtens 	 *
19009e8df8a2SDaniel Axtens 	 *  - We clean up our side: releasing and unmapping resources we hold
19019e8df8a2SDaniel Axtens 	 *    so we can wire them up again when the hardware comes back up.
19029e8df8a2SDaniel Axtens 	 *
19039e8df8a2SDaniel Axtens 	 * Driver authors should note:
19049e8df8a2SDaniel Axtens 	 *
19059e8df8a2SDaniel Axtens 	 *  - Any contexts you create in your kernel driver (except
19069e8df8a2SDaniel Axtens 	 *    those associated with anonymous file descriptors) are
19079e8df8a2SDaniel Axtens 	 *    your responsibility to free and recreate. Likewise with
19089e8df8a2SDaniel Axtens 	 *    any attached resources.
19099e8df8a2SDaniel Axtens 	 *
19109e8df8a2SDaniel Axtens 	 *  - We will take responsibility for re-initialising the
19119e8df8a2SDaniel Axtens 	 *    device context (the one set up for you in
19129e8df8a2SDaniel Axtens 	 *    cxl_pci_enable_device_hook and accessed through
19139e8df8a2SDaniel Axtens 	 *    cxl_get_context). If you've attached IRQs or other
19149e8df8a2SDaniel Axtens 	 *    resources to it, they remains yours to free.
19159e8df8a2SDaniel Axtens 	 *
19169e8df8a2SDaniel Axtens 	 * You can call the same functions to release resources as you
19179e8df8a2SDaniel Axtens 	 * normally would: we make sure that these functions continue
19189e8df8a2SDaniel Axtens 	 * to work when the hardware is down.
19199e8df8a2SDaniel Axtens 	 *
19209e8df8a2SDaniel Axtens 	 * Two examples:
19219e8df8a2SDaniel Axtens 	 *
19229e8df8a2SDaniel Axtens 	 * 1) If you normally free all your resources at the end of
19239e8df8a2SDaniel Axtens 	 *    each request, or if you use anonymous FDs, your
19249e8df8a2SDaniel Axtens 	 *    error_detected callback can simply set a flag to tell
19259e8df8a2SDaniel Axtens 	 *    your driver not to start any new calls. You can then
19269e8df8a2SDaniel Axtens 	 *    clear the flag in the resume callback.
19279e8df8a2SDaniel Axtens 	 *
19289e8df8a2SDaniel Axtens 	 * 2) If you normally allocate your resources on startup:
19299e8df8a2SDaniel Axtens 	 *     * Set a flag in error_detected as above.
19309e8df8a2SDaniel Axtens 	 *     * Let CXL detach your contexts.
19319e8df8a2SDaniel Axtens 	 *     * In slot_reset, free the old resources and allocate new ones.
19329e8df8a2SDaniel Axtens 	 *     * In resume, clear the flag to allow things to start.
19339e8df8a2SDaniel Axtens 	 */
1934edeb304fSVaibhav Jain 
1935edeb304fSVaibhav Jain 	/* Make sure no one else changes the afu list */
1936edeb304fSVaibhav Jain 	spin_lock(&adapter->afu_list_lock);
1937edeb304fSVaibhav Jain 
19389e8df8a2SDaniel Axtens 	for (i = 0; i < adapter->slices; i++) {
19399e8df8a2SDaniel Axtens 		afu = adapter->afu[i];
19409e8df8a2SDaniel Axtens 
1941edeb304fSVaibhav Jain 		if (afu == NULL)
1942edeb304fSVaibhav Jain 			continue;
19439e8df8a2SDaniel Axtens 
1944edeb304fSVaibhav Jain 		afu_result = cxl_vphb_error_detected(afu, state);
19459e8df8a2SDaniel Axtens 		cxl_context_detach_all(afu);
19465be587b1SFrederic Barrat 		cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
19472b04cf31SFrederic Barrat 		pci_deconfigure_afu(afu);
19484f58f0bfSVaibhav Jain 
19494f58f0bfSVaibhav Jain 		/* Disconnect trumps all, NONE trumps NEED_RESET */
19504f58f0bfSVaibhav Jain 		if (afu_result == PCI_ERS_RESULT_DISCONNECT)
19514f58f0bfSVaibhav Jain 			result = PCI_ERS_RESULT_DISCONNECT;
19524f58f0bfSVaibhav Jain 		else if ((afu_result == PCI_ERS_RESULT_NONE) &&
19534f58f0bfSVaibhav Jain 			 (result == PCI_ERS_RESULT_NEED_RESET))
19544f58f0bfSVaibhav Jain 			result = PCI_ERS_RESULT_NONE;
19559e8df8a2SDaniel Axtens 	}
1956edeb304fSVaibhav Jain 	spin_unlock(&adapter->afu_list_lock);
1957ea9a26d1SVaibhav Jain 
1958ea9a26d1SVaibhav Jain 	/* should take the context lock here */
1959ea9a26d1SVaibhav Jain 	if (cxl_adapter_context_lock(adapter) != 0)
1960ea9a26d1SVaibhav Jain 		dev_warn(&adapter->dev,
1961ea9a26d1SVaibhav Jain 			 "Couldn't take context lock with %d active-contexts\n",
1962ea9a26d1SVaibhav Jain 			 atomic_read(&adapter->contexts_num));
1963ea9a26d1SVaibhav Jain 
19649e8df8a2SDaniel Axtens 	cxl_deconfigure_adapter(adapter);
19659e8df8a2SDaniel Axtens 
19669e8df8a2SDaniel Axtens 	return result;
19679e8df8a2SDaniel Axtens }
19689e8df8a2SDaniel Axtens 
cxl_pci_slot_reset(struct pci_dev * pdev)19699e8df8a2SDaniel Axtens static pci_ers_result_t cxl_pci_slot_reset(struct pci_dev *pdev)
19709e8df8a2SDaniel Axtens {
19719e8df8a2SDaniel Axtens 	struct cxl *adapter = pci_get_drvdata(pdev);
19729e8df8a2SDaniel Axtens 	struct cxl_afu *afu;
19739e8df8a2SDaniel Axtens 	struct cxl_context *ctx;
19749e8df8a2SDaniel Axtens 	struct pci_dev *afu_dev;
19754e59b754SBjorn Helgaas 	struct pci_driver *afu_drv;
19764e59b754SBjorn Helgaas 	const struct pci_error_handlers *err_handler;
19779e8df8a2SDaniel Axtens 	pci_ers_result_t afu_result = PCI_ERS_RESULT_RECOVERED;
19789e8df8a2SDaniel Axtens 	pci_ers_result_t result = PCI_ERS_RESULT_RECOVERED;
19799e8df8a2SDaniel Axtens 	int i;
19809e8df8a2SDaniel Axtens 
19819e8df8a2SDaniel Axtens 	if (cxl_configure_adapter(adapter, pdev))
19829e8df8a2SDaniel Axtens 		goto err;
19839e8df8a2SDaniel Axtens 
1984ea9a26d1SVaibhav Jain 	/*
1985ea9a26d1SVaibhav Jain 	 * Unlock context activation for the adapter. Ideally this should be
1986ea9a26d1SVaibhav Jain 	 * done in cxl_pci_resume but cxlflash module tries to activate the
1987ea9a26d1SVaibhav Jain 	 * master context as part of slot_reset callback.
1988ea9a26d1SVaibhav Jain 	 */
1989ea9a26d1SVaibhav Jain 	cxl_adapter_context_unlock(adapter);
1990ea9a26d1SVaibhav Jain 
1991edeb304fSVaibhav Jain 	spin_lock(&adapter->afu_list_lock);
19929e8df8a2SDaniel Axtens 	for (i = 0; i < adapter->slices; i++) {
19939e8df8a2SDaniel Axtens 		afu = adapter->afu[i];
19949e8df8a2SDaniel Axtens 
1995edeb304fSVaibhav Jain 		if (afu == NULL)
1996edeb304fSVaibhav Jain 			continue;
1997edeb304fSVaibhav Jain 
19982b04cf31SFrederic Barrat 		if (pci_configure_afu(afu, adapter, pdev))
1999edeb304fSVaibhav Jain 			goto err_unlock;
20009e8df8a2SDaniel Axtens 
20019e8df8a2SDaniel Axtens 		if (cxl_afu_select_best_mode(afu))
2002edeb304fSVaibhav Jain 			goto err_unlock;
20039e8df8a2SDaniel Axtens 
200412841f87SVaibhav Jain 		if (afu->phb == NULL)
200512841f87SVaibhav Jain 			continue;
200612841f87SVaibhav Jain 
20079e8df8a2SDaniel Axtens 		list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
20089e8df8a2SDaniel Axtens 			/* Reset the device context.
20099e8df8a2SDaniel Axtens 			 * TODO: make this less disruptive
20109e8df8a2SDaniel Axtens 			 */
20119e8df8a2SDaniel Axtens 			ctx = cxl_get_context(afu_dev);
20129e8df8a2SDaniel Axtens 
20139e8df8a2SDaniel Axtens 			if (ctx && cxl_release_context(ctx))
2014edeb304fSVaibhav Jain 				goto err_unlock;
20159e8df8a2SDaniel Axtens 
20169e8df8a2SDaniel Axtens 			ctx = cxl_dev_context_init(afu_dev);
2017bb81733dSChristophe Jaillet 			if (IS_ERR(ctx))
2018edeb304fSVaibhav Jain 				goto err_unlock;
20199e8df8a2SDaniel Axtens 
20209e8df8a2SDaniel Axtens 			afu_dev->dev.archdata.cxl_ctx = ctx;
20219e8df8a2SDaniel Axtens 
20225be587b1SFrederic Barrat 			if (cxl_ops->afu_check_and_enable(afu))
2023edeb304fSVaibhav Jain 				goto err_unlock;
20249e8df8a2SDaniel Axtens 
20259e8df8a2SDaniel Axtens 			afu_dev->error_state = pci_channel_io_normal;
20269e8df8a2SDaniel Axtens 
20279e8df8a2SDaniel Axtens 			/* If there's a driver attached, allow it to
20289e8df8a2SDaniel Axtens 			 * chime in on recovery. Drivers should check
20299e8df8a2SDaniel Axtens 			 * if everything has come back OK, but
20309e8df8a2SDaniel Axtens 			 * shouldn't start new work until we call
20319e8df8a2SDaniel Axtens 			 * their resume function.
20329e8df8a2SDaniel Axtens 			 */
203316bd44e5SUwe Kleine-König 			afu_drv = to_pci_driver(afu_dev->dev.driver);
20344e59b754SBjorn Helgaas 			if (!afu_drv)
20359e8df8a2SDaniel Axtens 				continue;
20369e8df8a2SDaniel Axtens 
20374e59b754SBjorn Helgaas 			err_handler = afu_drv->err_handler;
20384e59b754SBjorn Helgaas 			if (err_handler && err_handler->slot_reset)
20394e59b754SBjorn Helgaas 				afu_result = err_handler->slot_reset(afu_dev);
20409e8df8a2SDaniel Axtens 
20419e8df8a2SDaniel Axtens 			if (afu_result == PCI_ERS_RESULT_DISCONNECT)
20429e8df8a2SDaniel Axtens 				result = PCI_ERS_RESULT_DISCONNECT;
20439e8df8a2SDaniel Axtens 		}
20449e8df8a2SDaniel Axtens 	}
2045edeb304fSVaibhav Jain 
2046edeb304fSVaibhav Jain 	spin_unlock(&adapter->afu_list_lock);
20479e8df8a2SDaniel Axtens 	return result;
20489e8df8a2SDaniel Axtens 
2049edeb304fSVaibhav Jain err_unlock:
2050edeb304fSVaibhav Jain 	spin_unlock(&adapter->afu_list_lock);
2051edeb304fSVaibhav Jain 
20529e8df8a2SDaniel Axtens err:
20539e8df8a2SDaniel Axtens 	/* All the bits that happen in both error_detected and cxl_remove
20549e8df8a2SDaniel Axtens 	 * should be idempotent, so we don't need to worry about leaving a mix
20559e8df8a2SDaniel Axtens 	 * of unconfigured and reconfigured resources.
20569e8df8a2SDaniel Axtens 	 */
20579e8df8a2SDaniel Axtens 	dev_err(&pdev->dev, "EEH recovery failed. Asking to be disconnected.\n");
20589e8df8a2SDaniel Axtens 	return PCI_ERS_RESULT_DISCONNECT;
20599e8df8a2SDaniel Axtens }
20609e8df8a2SDaniel Axtens 
cxl_pci_resume(struct pci_dev * pdev)20619e8df8a2SDaniel Axtens static void cxl_pci_resume(struct pci_dev *pdev)
20629e8df8a2SDaniel Axtens {
20639e8df8a2SDaniel Axtens 	struct cxl *adapter = pci_get_drvdata(pdev);
20649e8df8a2SDaniel Axtens 	struct cxl_afu *afu;
20659e8df8a2SDaniel Axtens 	struct pci_dev *afu_dev;
20664e59b754SBjorn Helgaas 	struct pci_driver *afu_drv;
20674e59b754SBjorn Helgaas 	const struct pci_error_handlers *err_handler;
20689e8df8a2SDaniel Axtens 	int i;
20699e8df8a2SDaniel Axtens 
20709e8df8a2SDaniel Axtens 	/* Everything is back now. Drivers should restart work now.
20719e8df8a2SDaniel Axtens 	 * This is not the place to be checking if everything came back up
20729e8df8a2SDaniel Axtens 	 * properly, because there's no return value: do that in slot_reset.
20739e8df8a2SDaniel Axtens 	 */
2074edeb304fSVaibhav Jain 	spin_lock(&adapter->afu_list_lock);
20759e8df8a2SDaniel Axtens 	for (i = 0; i < adapter->slices; i++) {
20769e8df8a2SDaniel Axtens 		afu = adapter->afu[i];
20779e8df8a2SDaniel Axtens 
2078edeb304fSVaibhav Jain 		if (afu == NULL || afu->phb == NULL)
207912841f87SVaibhav Jain 			continue;
208012841f87SVaibhav Jain 
20819e8df8a2SDaniel Axtens 		list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
208216bd44e5SUwe Kleine-König 			afu_drv = to_pci_driver(afu_dev->dev.driver);
20834e59b754SBjorn Helgaas 			if (!afu_drv)
20844e59b754SBjorn Helgaas 				continue;
20854e59b754SBjorn Helgaas 
20864e59b754SBjorn Helgaas 			err_handler = afu_drv->err_handler;
20874e59b754SBjorn Helgaas 			if (err_handler && err_handler->resume)
20884e59b754SBjorn Helgaas 				err_handler->resume(afu_dev);
20899e8df8a2SDaniel Axtens 		}
20909e8df8a2SDaniel Axtens 	}
2091edeb304fSVaibhav Jain 	spin_unlock(&adapter->afu_list_lock);
20929e8df8a2SDaniel Axtens }
20939e8df8a2SDaniel Axtens 
20949e8df8a2SDaniel Axtens static const struct pci_error_handlers cxl_err_handler = {
20959e8df8a2SDaniel Axtens 	.error_detected = cxl_pci_error_detected,
20969e8df8a2SDaniel Axtens 	.slot_reset = cxl_pci_slot_reset,
20979e8df8a2SDaniel Axtens 	.resume = cxl_pci_resume,
20989e8df8a2SDaniel Axtens };
20999e8df8a2SDaniel Axtens 
2100f204e0b8SIan Munsie struct pci_driver cxl_pci_driver = {
2101f204e0b8SIan Munsie 	.name = "cxl-pci",
2102f204e0b8SIan Munsie 	.id_table = cxl_pci_tbl,
2103f204e0b8SIan Munsie 	.probe = cxl_probe,
2104f204e0b8SIan Munsie 	.remove = cxl_remove,
2105aa70775eSMichael Neuling 	.shutdown = cxl_remove,
21069e8df8a2SDaniel Axtens 	.err_handler = &cxl_err_handler,
2107f204e0b8SIan Munsie };
2108