1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 2015 - 2016 Cavium, Inc.
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #include <linux/bitfield.h>
7*4882a593Smuzhiyun #include <linux/kernel.h>
8*4882a593Smuzhiyun #include <linux/init.h>
9*4882a593Smuzhiyun #include <linux/pci.h>
10*4882a593Smuzhiyun #include <linux/of_address.h>
11*4882a593Smuzhiyun #include <linux/of_pci.h>
12*4882a593Smuzhiyun #include <linux/pci-acpi.h>
13*4882a593Smuzhiyun #include <linux/pci-ecam.h>
14*4882a593Smuzhiyun #include <linux/platform_device.h>
15*4882a593Smuzhiyun #include <linux/io-64-nonatomic-lo-hi.h>
16*4882a593Smuzhiyun #include "../pci.h"
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #if defined(CONFIG_PCI_HOST_THUNDER_PEM) || (defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS))
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #define PEM_CFG_WR 0x28
21*4882a593Smuzhiyun #define PEM_CFG_RD 0x30
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun struct thunder_pem_pci {
24*4882a593Smuzhiyun u32 ea_entry[3];
25*4882a593Smuzhiyun void __iomem *pem_reg_base;
26*4882a593Smuzhiyun };
27*4882a593Smuzhiyun
thunder_pem_bridge_read(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 * val)28*4882a593Smuzhiyun static int thunder_pem_bridge_read(struct pci_bus *bus, unsigned int devfn,
29*4882a593Smuzhiyun int where, int size, u32 *val)
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun u64 read_val, tmp_val;
32*4882a593Smuzhiyun struct pci_config_window *cfg = bus->sysdata;
33*4882a593Smuzhiyun struct thunder_pem_pci *pem_pci = (struct thunder_pem_pci *)cfg->priv;
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun if (devfn != 0 || where >= 2048) {
36*4882a593Smuzhiyun *val = ~0;
37*4882a593Smuzhiyun return PCIBIOS_DEVICE_NOT_FOUND;
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun /*
41*4882a593Smuzhiyun * 32-bit accesses only. Write the address to the low order
42*4882a593Smuzhiyun * bits of PEM_CFG_RD, then trigger the read by reading back.
43*4882a593Smuzhiyun * The config data lands in the upper 32-bits of PEM_CFG_RD.
44*4882a593Smuzhiyun */
45*4882a593Smuzhiyun read_val = where & ~3ull;
46*4882a593Smuzhiyun writeq(read_val, pem_pci->pem_reg_base + PEM_CFG_RD);
47*4882a593Smuzhiyun read_val = readq(pem_pci->pem_reg_base + PEM_CFG_RD);
48*4882a593Smuzhiyun read_val >>= 32;
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun /*
51*4882a593Smuzhiyun * The config space contains some garbage, fix it up. Also
52*4882a593Smuzhiyun * synthesize an EA capability for the BAR used by MSI-X.
53*4882a593Smuzhiyun */
54*4882a593Smuzhiyun switch (where & ~3) {
55*4882a593Smuzhiyun case 0x40:
56*4882a593Smuzhiyun read_val &= 0xffff00ff;
57*4882a593Smuzhiyun read_val |= 0x00007000; /* Skip MSI CAP */
58*4882a593Smuzhiyun break;
59*4882a593Smuzhiyun case 0x70: /* Express Cap */
60*4882a593Smuzhiyun /*
61*4882a593Smuzhiyun * Change PME interrupt to vector 2 on T88 where it
62*4882a593Smuzhiyun * reads as 0, else leave it alone.
63*4882a593Smuzhiyun */
64*4882a593Smuzhiyun if (!(read_val & (0x1f << 25)))
65*4882a593Smuzhiyun read_val |= (2u << 25);
66*4882a593Smuzhiyun break;
67*4882a593Smuzhiyun case 0xb0: /* MSI-X Cap */
68*4882a593Smuzhiyun /* TableSize=2 or 4, Next Cap is EA */
69*4882a593Smuzhiyun read_val &= 0xc00000ff;
70*4882a593Smuzhiyun /*
71*4882a593Smuzhiyun * If Express Cap(0x70) raw PME vector reads as 0 we are on
72*4882a593Smuzhiyun * T88 and TableSize is reported as 4, else TableSize
73*4882a593Smuzhiyun * is 2.
74*4882a593Smuzhiyun */
75*4882a593Smuzhiyun writeq(0x70, pem_pci->pem_reg_base + PEM_CFG_RD);
76*4882a593Smuzhiyun tmp_val = readq(pem_pci->pem_reg_base + PEM_CFG_RD);
77*4882a593Smuzhiyun tmp_val >>= 32;
78*4882a593Smuzhiyun if (!(tmp_val & (0x1f << 25)))
79*4882a593Smuzhiyun read_val |= 0x0003bc00;
80*4882a593Smuzhiyun else
81*4882a593Smuzhiyun read_val |= 0x0001bc00;
82*4882a593Smuzhiyun break;
83*4882a593Smuzhiyun case 0xb4:
84*4882a593Smuzhiyun /* Table offset=0, BIR=0 */
85*4882a593Smuzhiyun read_val = 0x00000000;
86*4882a593Smuzhiyun break;
87*4882a593Smuzhiyun case 0xb8:
88*4882a593Smuzhiyun /* BPA offset=0xf0000, BIR=0 */
89*4882a593Smuzhiyun read_val = 0x000f0000;
90*4882a593Smuzhiyun break;
91*4882a593Smuzhiyun case 0xbc:
92*4882a593Smuzhiyun /* EA, 1 entry, no next Cap */
93*4882a593Smuzhiyun read_val = 0x00010014;
94*4882a593Smuzhiyun break;
95*4882a593Smuzhiyun case 0xc0:
96*4882a593Smuzhiyun /* DW2 for type-1 */
97*4882a593Smuzhiyun read_val = 0x00000000;
98*4882a593Smuzhiyun break;
99*4882a593Smuzhiyun case 0xc4:
100*4882a593Smuzhiyun /* Entry BEI=0, PP=0x00, SP=0xff, ES=3 */
101*4882a593Smuzhiyun read_val = 0x80ff0003;
102*4882a593Smuzhiyun break;
103*4882a593Smuzhiyun case 0xc8:
104*4882a593Smuzhiyun read_val = pem_pci->ea_entry[0];
105*4882a593Smuzhiyun break;
106*4882a593Smuzhiyun case 0xcc:
107*4882a593Smuzhiyun read_val = pem_pci->ea_entry[1];
108*4882a593Smuzhiyun break;
109*4882a593Smuzhiyun case 0xd0:
110*4882a593Smuzhiyun read_val = pem_pci->ea_entry[2];
111*4882a593Smuzhiyun break;
112*4882a593Smuzhiyun default:
113*4882a593Smuzhiyun break;
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun read_val >>= (8 * (where & 3));
116*4882a593Smuzhiyun switch (size) {
117*4882a593Smuzhiyun case 1:
118*4882a593Smuzhiyun read_val &= 0xff;
119*4882a593Smuzhiyun break;
120*4882a593Smuzhiyun case 2:
121*4882a593Smuzhiyun read_val &= 0xffff;
122*4882a593Smuzhiyun break;
123*4882a593Smuzhiyun default:
124*4882a593Smuzhiyun break;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun *val = read_val;
127*4882a593Smuzhiyun return PCIBIOS_SUCCESSFUL;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun
thunder_pem_config_read(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 * val)130*4882a593Smuzhiyun static int thunder_pem_config_read(struct pci_bus *bus, unsigned int devfn,
131*4882a593Smuzhiyun int where, int size, u32 *val)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun struct pci_config_window *cfg = bus->sysdata;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun if (bus->number < cfg->busr.start ||
136*4882a593Smuzhiyun bus->number > cfg->busr.end)
137*4882a593Smuzhiyun return PCIBIOS_DEVICE_NOT_FOUND;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun /*
140*4882a593Smuzhiyun * The first device on the bus is the PEM PCIe bridge.
141*4882a593Smuzhiyun * Special case its config access.
142*4882a593Smuzhiyun */
143*4882a593Smuzhiyun if (bus->number == cfg->busr.start)
144*4882a593Smuzhiyun return thunder_pem_bridge_read(bus, devfn, where, size, val);
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun return pci_generic_config_read(bus, devfn, where, size, val);
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun /*
150*4882a593Smuzhiyun * Some of the w1c_bits below also include read-only or non-writable
151*4882a593Smuzhiyun * reserved bits, this makes the code simpler and is OK as the bits
152*4882a593Smuzhiyun * are not affected by writing zeros to them.
153*4882a593Smuzhiyun */
thunder_pem_bridge_w1c_bits(u64 where_aligned)154*4882a593Smuzhiyun static u32 thunder_pem_bridge_w1c_bits(u64 where_aligned)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun u32 w1c_bits = 0;
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun switch (where_aligned) {
159*4882a593Smuzhiyun case 0x04: /* Command/Status */
160*4882a593Smuzhiyun case 0x1c: /* Base and I/O Limit/Secondary Status */
161*4882a593Smuzhiyun w1c_bits = 0xff000000;
162*4882a593Smuzhiyun break;
163*4882a593Smuzhiyun case 0x44: /* Power Management Control and Status */
164*4882a593Smuzhiyun w1c_bits = 0xfffffe00;
165*4882a593Smuzhiyun break;
166*4882a593Smuzhiyun case 0x78: /* Device Control/Device Status */
167*4882a593Smuzhiyun case 0x80: /* Link Control/Link Status */
168*4882a593Smuzhiyun case 0x88: /* Slot Control/Slot Status */
169*4882a593Smuzhiyun case 0x90: /* Root Status */
170*4882a593Smuzhiyun case 0xa0: /* Link Control 2 Registers/Link Status 2 */
171*4882a593Smuzhiyun w1c_bits = 0xffff0000;
172*4882a593Smuzhiyun break;
173*4882a593Smuzhiyun case 0x104: /* Uncorrectable Error Status */
174*4882a593Smuzhiyun case 0x110: /* Correctable Error Status */
175*4882a593Smuzhiyun case 0x130: /* Error Status */
176*4882a593Smuzhiyun case 0x160: /* Link Control 4 */
177*4882a593Smuzhiyun w1c_bits = 0xffffffff;
178*4882a593Smuzhiyun break;
179*4882a593Smuzhiyun default:
180*4882a593Smuzhiyun break;
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun return w1c_bits;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun /* Some bits must be written to one so they appear to be read-only. */
thunder_pem_bridge_w1_bits(u64 where_aligned)186*4882a593Smuzhiyun static u32 thunder_pem_bridge_w1_bits(u64 where_aligned)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun u32 w1_bits;
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun switch (where_aligned) {
191*4882a593Smuzhiyun case 0x1c: /* I/O Base / I/O Limit, Secondary Status */
192*4882a593Smuzhiyun /* Force 32-bit I/O addressing. */
193*4882a593Smuzhiyun w1_bits = 0x0101;
194*4882a593Smuzhiyun break;
195*4882a593Smuzhiyun case 0x24: /* Prefetchable Memory Base / Prefetchable Memory Limit */
196*4882a593Smuzhiyun /* Force 64-bit addressing */
197*4882a593Smuzhiyun w1_bits = 0x00010001;
198*4882a593Smuzhiyun break;
199*4882a593Smuzhiyun default:
200*4882a593Smuzhiyun w1_bits = 0;
201*4882a593Smuzhiyun break;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun return w1_bits;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun
thunder_pem_bridge_write(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 val)206*4882a593Smuzhiyun static int thunder_pem_bridge_write(struct pci_bus *bus, unsigned int devfn,
207*4882a593Smuzhiyun int where, int size, u32 val)
208*4882a593Smuzhiyun {
209*4882a593Smuzhiyun struct pci_config_window *cfg = bus->sysdata;
210*4882a593Smuzhiyun struct thunder_pem_pci *pem_pci = (struct thunder_pem_pci *)cfg->priv;
211*4882a593Smuzhiyun u64 write_val, read_val;
212*4882a593Smuzhiyun u64 where_aligned = where & ~3ull;
213*4882a593Smuzhiyun u32 mask = 0;
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun if (devfn != 0 || where >= 2048)
217*4882a593Smuzhiyun return PCIBIOS_DEVICE_NOT_FOUND;
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun /*
220*4882a593Smuzhiyun * 32-bit accesses only. If the write is for a size smaller
221*4882a593Smuzhiyun * than 32-bits, we must first read the 32-bit value and merge
222*4882a593Smuzhiyun * in the desired bits and then write the whole 32-bits back
223*4882a593Smuzhiyun * out.
224*4882a593Smuzhiyun */
225*4882a593Smuzhiyun switch (size) {
226*4882a593Smuzhiyun case 1:
227*4882a593Smuzhiyun writeq(where_aligned, pem_pci->pem_reg_base + PEM_CFG_RD);
228*4882a593Smuzhiyun read_val = readq(pem_pci->pem_reg_base + PEM_CFG_RD);
229*4882a593Smuzhiyun read_val >>= 32;
230*4882a593Smuzhiyun mask = ~(0xff << (8 * (where & 3)));
231*4882a593Smuzhiyun read_val &= mask;
232*4882a593Smuzhiyun val = (val & 0xff) << (8 * (where & 3));
233*4882a593Smuzhiyun val |= (u32)read_val;
234*4882a593Smuzhiyun break;
235*4882a593Smuzhiyun case 2:
236*4882a593Smuzhiyun writeq(where_aligned, pem_pci->pem_reg_base + PEM_CFG_RD);
237*4882a593Smuzhiyun read_val = readq(pem_pci->pem_reg_base + PEM_CFG_RD);
238*4882a593Smuzhiyun read_val >>= 32;
239*4882a593Smuzhiyun mask = ~(0xffff << (8 * (where & 3)));
240*4882a593Smuzhiyun read_val &= mask;
241*4882a593Smuzhiyun val = (val & 0xffff) << (8 * (where & 3));
242*4882a593Smuzhiyun val |= (u32)read_val;
243*4882a593Smuzhiyun break;
244*4882a593Smuzhiyun default:
245*4882a593Smuzhiyun break;
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun /*
249*4882a593Smuzhiyun * By expanding the write width to 32 bits, we may
250*4882a593Smuzhiyun * inadvertently hit some W1C bits that were not intended to
251*4882a593Smuzhiyun * be written. Calculate the mask that must be applied to the
252*4882a593Smuzhiyun * data to be written to avoid these cases.
253*4882a593Smuzhiyun */
254*4882a593Smuzhiyun if (mask) {
255*4882a593Smuzhiyun u32 w1c_bits = thunder_pem_bridge_w1c_bits(where);
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun if (w1c_bits) {
258*4882a593Smuzhiyun mask &= w1c_bits;
259*4882a593Smuzhiyun val &= ~mask;
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun /*
264*4882a593Smuzhiyun * Some bits must be read-only with value of one. Since the
265*4882a593Smuzhiyun * access method allows these to be cleared if a zero is
266*4882a593Smuzhiyun * written, force them to one before writing.
267*4882a593Smuzhiyun */
268*4882a593Smuzhiyun val |= thunder_pem_bridge_w1_bits(where_aligned);
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun /*
271*4882a593Smuzhiyun * Low order bits are the config address, the high order 32
272*4882a593Smuzhiyun * bits are the data to be written.
273*4882a593Smuzhiyun */
274*4882a593Smuzhiyun write_val = (((u64)val) << 32) | where_aligned;
275*4882a593Smuzhiyun writeq(write_val, pem_pci->pem_reg_base + PEM_CFG_WR);
276*4882a593Smuzhiyun return PCIBIOS_SUCCESSFUL;
277*4882a593Smuzhiyun }
278*4882a593Smuzhiyun
thunder_pem_config_write(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 val)279*4882a593Smuzhiyun static int thunder_pem_config_write(struct pci_bus *bus, unsigned int devfn,
280*4882a593Smuzhiyun int where, int size, u32 val)
281*4882a593Smuzhiyun {
282*4882a593Smuzhiyun struct pci_config_window *cfg = bus->sysdata;
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun if (bus->number < cfg->busr.start ||
285*4882a593Smuzhiyun bus->number > cfg->busr.end)
286*4882a593Smuzhiyun return PCIBIOS_DEVICE_NOT_FOUND;
287*4882a593Smuzhiyun /*
288*4882a593Smuzhiyun * The first device on the bus is the PEM PCIe bridge.
289*4882a593Smuzhiyun * Special case its config access.
290*4882a593Smuzhiyun */
291*4882a593Smuzhiyun if (bus->number == cfg->busr.start)
292*4882a593Smuzhiyun return thunder_pem_bridge_write(bus, devfn, where, size, val);
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun return pci_generic_config_write(bus, devfn, where, size, val);
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun
thunder_pem_init(struct device * dev,struct pci_config_window * cfg,struct resource * res_pem)298*4882a593Smuzhiyun static int thunder_pem_init(struct device *dev, struct pci_config_window *cfg,
299*4882a593Smuzhiyun struct resource *res_pem)
300*4882a593Smuzhiyun {
301*4882a593Smuzhiyun struct thunder_pem_pci *pem_pci;
302*4882a593Smuzhiyun resource_size_t bar4_start;
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun pem_pci = devm_kzalloc(dev, sizeof(*pem_pci), GFP_KERNEL);
305*4882a593Smuzhiyun if (!pem_pci)
306*4882a593Smuzhiyun return -ENOMEM;
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun pem_pci->pem_reg_base = devm_ioremap(dev, res_pem->start, 0x10000);
309*4882a593Smuzhiyun if (!pem_pci->pem_reg_base)
310*4882a593Smuzhiyun return -ENOMEM;
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun /*
313*4882a593Smuzhiyun * The MSI-X BAR for the PEM and AER interrupts is located at
314*4882a593Smuzhiyun * a fixed offset from the PEM register base. Generate a
315*4882a593Smuzhiyun * fragment of the synthesized Enhanced Allocation capability
316*4882a593Smuzhiyun * structure here for the BAR.
317*4882a593Smuzhiyun */
318*4882a593Smuzhiyun bar4_start = res_pem->start + 0xf00000;
319*4882a593Smuzhiyun pem_pci->ea_entry[0] = lower_32_bits(bar4_start) | 2;
320*4882a593Smuzhiyun pem_pci->ea_entry[1] = lower_32_bits(res_pem->end - bar4_start) & ~3u;
321*4882a593Smuzhiyun pem_pci->ea_entry[2] = upper_32_bits(bar4_start);
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun cfg->priv = pem_pci;
324*4882a593Smuzhiyun return 0;
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun #if defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS)
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun #define PEM_RES_BASE 0x87e0c0000000ULL
330*4882a593Smuzhiyun #define PEM_NODE_MASK GENMASK_ULL(45, 44)
331*4882a593Smuzhiyun #define PEM_INDX_MASK GENMASK_ULL(26, 24)
332*4882a593Smuzhiyun #define PEM_MIN_DOM_IN_NODE 4
333*4882a593Smuzhiyun #define PEM_MAX_DOM_IN_NODE 10
334*4882a593Smuzhiyun
thunder_pem_reserve_range(struct device * dev,int seg,struct resource * r)335*4882a593Smuzhiyun static void thunder_pem_reserve_range(struct device *dev, int seg,
336*4882a593Smuzhiyun struct resource *r)
337*4882a593Smuzhiyun {
338*4882a593Smuzhiyun resource_size_t start = r->start, end = r->end;
339*4882a593Smuzhiyun struct resource *res;
340*4882a593Smuzhiyun const char *regionid;
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun regionid = kasprintf(GFP_KERNEL, "PEM RC:%d", seg);
343*4882a593Smuzhiyun if (!regionid)
344*4882a593Smuzhiyun return;
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun res = request_mem_region(start, end - start + 1, regionid);
347*4882a593Smuzhiyun if (res)
348*4882a593Smuzhiyun res->flags &= ~IORESOURCE_BUSY;
349*4882a593Smuzhiyun else
350*4882a593Smuzhiyun kfree(regionid);
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun dev_info(dev, "%pR %s reserved\n", r,
353*4882a593Smuzhiyun res ? "has been" : "could not be");
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun
thunder_pem_legacy_fw(struct acpi_pci_root * root,struct resource * res_pem)356*4882a593Smuzhiyun static void thunder_pem_legacy_fw(struct acpi_pci_root *root,
357*4882a593Smuzhiyun struct resource *res_pem)
358*4882a593Smuzhiyun {
359*4882a593Smuzhiyun int node = acpi_get_node(root->device->handle);
360*4882a593Smuzhiyun int index;
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun if (node == NUMA_NO_NODE)
363*4882a593Smuzhiyun node = 0;
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun index = root->segment - PEM_MIN_DOM_IN_NODE;
366*4882a593Smuzhiyun index -= node * PEM_MAX_DOM_IN_NODE;
367*4882a593Smuzhiyun res_pem->start = PEM_RES_BASE | FIELD_PREP(PEM_NODE_MASK, node) |
368*4882a593Smuzhiyun FIELD_PREP(PEM_INDX_MASK, index);
369*4882a593Smuzhiyun res_pem->flags = IORESOURCE_MEM;
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun
thunder_pem_acpi_init(struct pci_config_window * cfg)372*4882a593Smuzhiyun static int thunder_pem_acpi_init(struct pci_config_window *cfg)
373*4882a593Smuzhiyun {
374*4882a593Smuzhiyun struct device *dev = cfg->parent;
375*4882a593Smuzhiyun struct acpi_device *adev = to_acpi_device(dev);
376*4882a593Smuzhiyun struct acpi_pci_root *root = acpi_driver_data(adev);
377*4882a593Smuzhiyun struct resource *res_pem;
378*4882a593Smuzhiyun int ret;
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun res_pem = devm_kzalloc(&adev->dev, sizeof(*res_pem), GFP_KERNEL);
381*4882a593Smuzhiyun if (!res_pem)
382*4882a593Smuzhiyun return -ENOMEM;
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun ret = acpi_get_rc_resources(dev, "CAVA02B", root->segment, res_pem);
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun /*
387*4882a593Smuzhiyun * If we fail to gather resources it means that we run with old
388*4882a593Smuzhiyun * FW where we need to calculate PEM-specific resources manually.
389*4882a593Smuzhiyun */
390*4882a593Smuzhiyun if (ret) {
391*4882a593Smuzhiyun thunder_pem_legacy_fw(root, res_pem);
392*4882a593Smuzhiyun /*
393*4882a593Smuzhiyun * Reserve 64K size PEM specific resources. The full 16M range
394*4882a593Smuzhiyun * size is required for thunder_pem_init() call.
395*4882a593Smuzhiyun */
396*4882a593Smuzhiyun res_pem->end = res_pem->start + SZ_64K - 1;
397*4882a593Smuzhiyun thunder_pem_reserve_range(dev, root->segment, res_pem);
398*4882a593Smuzhiyun res_pem->end = res_pem->start + SZ_16M - 1;
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun /* Reserve PCI configuration space as well. */
401*4882a593Smuzhiyun thunder_pem_reserve_range(dev, root->segment, &cfg->res);
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun return thunder_pem_init(dev, cfg, res_pem);
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun const struct pci_ecam_ops thunder_pem_ecam_ops = {
408*4882a593Smuzhiyun .bus_shift = 24,
409*4882a593Smuzhiyun .init = thunder_pem_acpi_init,
410*4882a593Smuzhiyun .pci_ops = {
411*4882a593Smuzhiyun .map_bus = pci_ecam_map_bus,
412*4882a593Smuzhiyun .read = thunder_pem_config_read,
413*4882a593Smuzhiyun .write = thunder_pem_config_write,
414*4882a593Smuzhiyun }
415*4882a593Smuzhiyun };
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun #endif
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun #ifdef CONFIG_PCI_HOST_THUNDER_PEM
420*4882a593Smuzhiyun
thunder_pem_platform_init(struct pci_config_window * cfg)421*4882a593Smuzhiyun static int thunder_pem_platform_init(struct pci_config_window *cfg)
422*4882a593Smuzhiyun {
423*4882a593Smuzhiyun struct device *dev = cfg->parent;
424*4882a593Smuzhiyun struct platform_device *pdev = to_platform_device(dev);
425*4882a593Smuzhiyun struct resource *res_pem;
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun if (!dev->of_node)
428*4882a593Smuzhiyun return -EINVAL;
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun /*
431*4882a593Smuzhiyun * The second register range is the PEM bridge to the PCIe
432*4882a593Smuzhiyun * bus. It has a different config access method than those
433*4882a593Smuzhiyun * devices behind the bridge.
434*4882a593Smuzhiyun */
435*4882a593Smuzhiyun res_pem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
436*4882a593Smuzhiyun if (!res_pem) {
437*4882a593Smuzhiyun dev_err(dev, "missing \"reg[1]\"property\n");
438*4882a593Smuzhiyun return -EINVAL;
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun return thunder_pem_init(dev, cfg, res_pem);
442*4882a593Smuzhiyun }
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun static const struct pci_ecam_ops pci_thunder_pem_ops = {
445*4882a593Smuzhiyun .bus_shift = 24,
446*4882a593Smuzhiyun .init = thunder_pem_platform_init,
447*4882a593Smuzhiyun .pci_ops = {
448*4882a593Smuzhiyun .map_bus = pci_ecam_map_bus,
449*4882a593Smuzhiyun .read = thunder_pem_config_read,
450*4882a593Smuzhiyun .write = thunder_pem_config_write,
451*4882a593Smuzhiyun }
452*4882a593Smuzhiyun };
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun static const struct of_device_id thunder_pem_of_match[] = {
455*4882a593Smuzhiyun {
456*4882a593Smuzhiyun .compatible = "cavium,pci-host-thunder-pem",
457*4882a593Smuzhiyun .data = &pci_thunder_pem_ops,
458*4882a593Smuzhiyun },
459*4882a593Smuzhiyun { },
460*4882a593Smuzhiyun };
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun static struct platform_driver thunder_pem_driver = {
463*4882a593Smuzhiyun .driver = {
464*4882a593Smuzhiyun .name = KBUILD_MODNAME,
465*4882a593Smuzhiyun .of_match_table = thunder_pem_of_match,
466*4882a593Smuzhiyun .suppress_bind_attrs = true,
467*4882a593Smuzhiyun },
468*4882a593Smuzhiyun .probe = pci_host_common_probe,
469*4882a593Smuzhiyun };
470*4882a593Smuzhiyun builtin_platform_driver(thunder_pem_driver);
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun #endif
473*4882a593Smuzhiyun #endif
474