1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2015 - 2016 Cavium, Inc.
6 #include <linux/bitfield.h>
7 #include <linux/kernel.h>
8 #include <linux/init.h>
9 #include <linux/of_address.h>
10 #include <linux/of_pci.h>
11 #include <linux/pci-acpi.h>
12 #include <linux/pci-ecam.h>
13 #include <linux/platform_device.h>
16 #if defined(CONFIG_PCI_HOST_THUNDER_PEM) || (defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS))
18 #define PEM_CFG_WR 0x28
19 #define PEM_CFG_RD 0x30
21 struct thunder_pem_pci
{
23 void __iomem
*pem_reg_base
;
26 static int thunder_pem_bridge_read(struct pci_bus
*bus
, unsigned int devfn
,
27 int where
, int size
, u32
*val
)
29 u64 read_val
, tmp_val
;
30 struct pci_config_window
*cfg
= bus
->sysdata
;
31 struct thunder_pem_pci
*pem_pci
= (struct thunder_pem_pci
*)cfg
->priv
;
33 if (devfn
!= 0 || where
>= 2048) {
35 return PCIBIOS_DEVICE_NOT_FOUND
;
39 * 32-bit accesses only. Write the address to the low order
40 * bits of PEM_CFG_RD, then trigger the read by reading back.
41 * The config data lands in the upper 32-bits of PEM_CFG_RD.
43 read_val
= where
& ~3ull;
44 writeq(read_val
, pem_pci
->pem_reg_base
+ PEM_CFG_RD
);
45 read_val
= readq(pem_pci
->pem_reg_base
+ PEM_CFG_RD
);
49 * The config space contains some garbage, fix it up. Also
50 * synthesize an EA capability for the BAR used by MSI-X.
54 read_val
&= 0xffff00ff;
55 read_val
|= 0x00007000; /* Skip MSI CAP */
57 case 0x70: /* Express Cap */
59 * Change PME interrupt to vector 2 on T88 where it
60 * reads as 0, else leave it alone.
62 if (!(read_val
& (0x1f << 25)))
63 read_val
|= (2u << 25);
65 case 0xb0: /* MSI-X Cap */
66 /* TableSize=2 or 4, Next Cap is EA */
67 read_val
&= 0xc00000ff;
69 * If Express Cap(0x70) raw PME vector reads as 0 we are on
70 * T88 and TableSize is reported as 4, else TableSize
73 writeq(0x70, pem_pci
->pem_reg_base
+ PEM_CFG_RD
);
74 tmp_val
= readq(pem_pci
->pem_reg_base
+ PEM_CFG_RD
);
76 if (!(tmp_val
& (0x1f << 25)))
77 read_val
|= 0x0003bc00;
79 read_val
|= 0x0001bc00;
82 /* Table offset=0, BIR=0 */
83 read_val
= 0x00000000;
86 /* BPA offset=0xf0000, BIR=0 */
87 read_val
= 0x000f0000;
90 /* EA, 1 entry, no next Cap */
91 read_val
= 0x00010014;
95 read_val
= 0x00000000;
98 /* Entry BEI=0, PP=0x00, SP=0xff, ES=3 */
99 read_val
= 0x80ff0003;
102 read_val
= pem_pci
->ea_entry
[0];
105 read_val
= pem_pci
->ea_entry
[1];
108 read_val
= pem_pci
->ea_entry
[2];
113 read_val
>>= (8 * (where
& 3));
125 return PCIBIOS_SUCCESSFUL
;
128 static int thunder_pem_config_read(struct pci_bus
*bus
, unsigned int devfn
,
129 int where
, int size
, u32
*val
)
131 struct pci_config_window
*cfg
= bus
->sysdata
;
133 if (bus
->number
< cfg
->busr
.start
||
134 bus
->number
> cfg
->busr
.end
)
135 return PCIBIOS_DEVICE_NOT_FOUND
;
138 * The first device on the bus is the PEM PCIe bridge.
139 * Special case its config access.
141 if (bus
->number
== cfg
->busr
.start
)
142 return thunder_pem_bridge_read(bus
, devfn
, where
, size
, val
);
144 return pci_generic_config_read(bus
, devfn
, where
, size
, val
);
148 * Some of the w1c_bits below also include read-only or non-writable
149 * reserved bits, this makes the code simpler and is OK as the bits
150 * are not affected by writing zeros to them.
152 static u32
thunder_pem_bridge_w1c_bits(u64 where_aligned
)
156 switch (where_aligned
) {
157 case 0x04: /* Command/Status */
158 case 0x1c: /* Base and I/O Limit/Secondary Status */
159 w1c_bits
= 0xff000000;
161 case 0x44: /* Power Management Control and Status */
162 w1c_bits
= 0xfffffe00;
164 case 0x78: /* Device Control/Device Status */
165 case 0x80: /* Link Control/Link Status */
166 case 0x88: /* Slot Control/Slot Status */
167 case 0x90: /* Root Status */
168 case 0xa0: /* Link Control 2 Registers/Link Status 2 */
169 w1c_bits
= 0xffff0000;
171 case 0x104: /* Uncorrectable Error Status */
172 case 0x110: /* Correctable Error Status */
173 case 0x130: /* Error Status */
174 case 0x160: /* Link Control 4 */
175 w1c_bits
= 0xffffffff;
183 /* Some bits must be written to one so they appear to be read-only. */
184 static u32
thunder_pem_bridge_w1_bits(u64 where_aligned
)
188 switch (where_aligned
) {
189 case 0x1c: /* I/O Base / I/O Limit, Secondary Status */
190 /* Force 32-bit I/O addressing. */
193 case 0x24: /* Prefetchable Memory Base / Prefetchable Memory Limit */
194 /* Force 64-bit addressing */
195 w1_bits
= 0x00010001;
204 static int thunder_pem_bridge_write(struct pci_bus
*bus
, unsigned int devfn
,
205 int where
, int size
, u32 val
)
207 struct pci_config_window
*cfg
= bus
->sysdata
;
208 struct thunder_pem_pci
*pem_pci
= (struct thunder_pem_pci
*)cfg
->priv
;
209 u64 write_val
, read_val
;
210 u64 where_aligned
= where
& ~3ull;
214 if (devfn
!= 0 || where
>= 2048)
215 return PCIBIOS_DEVICE_NOT_FOUND
;
218 * 32-bit accesses only. If the write is for a size smaller
219 * than 32-bits, we must first read the 32-bit value and merge
220 * in the desired bits and then write the whole 32-bits back
225 writeq(where_aligned
, pem_pci
->pem_reg_base
+ PEM_CFG_RD
);
226 read_val
= readq(pem_pci
->pem_reg_base
+ PEM_CFG_RD
);
228 mask
= ~(0xff << (8 * (where
& 3)));
230 val
= (val
& 0xff) << (8 * (where
& 3));
231 val
|= (u32
)read_val
;
234 writeq(where_aligned
, pem_pci
->pem_reg_base
+ PEM_CFG_RD
);
235 read_val
= readq(pem_pci
->pem_reg_base
+ PEM_CFG_RD
);
237 mask
= ~(0xffff << (8 * (where
& 3)));
239 val
= (val
& 0xffff) << (8 * (where
& 3));
240 val
|= (u32
)read_val
;
247 * By expanding the write width to 32 bits, we may
248 * inadvertently hit some W1C bits that were not intended to
249 * be written. Calculate the mask that must be applied to the
250 * data to be written to avoid these cases.
253 u32 w1c_bits
= thunder_pem_bridge_w1c_bits(where
);
262 * Some bits must be read-only with value of one. Since the
263 * access method allows these to be cleared if a zero is
264 * written, force them to one before writing.
266 val
|= thunder_pem_bridge_w1_bits(where_aligned
);
269 * Low order bits are the config address, the high order 32
270 * bits are the data to be written.
272 write_val
= (((u64
)val
) << 32) | where_aligned
;
273 writeq(write_val
, pem_pci
->pem_reg_base
+ PEM_CFG_WR
);
274 return PCIBIOS_SUCCESSFUL
;
277 static int thunder_pem_config_write(struct pci_bus
*bus
, unsigned int devfn
,
278 int where
, int size
, u32 val
)
280 struct pci_config_window
*cfg
= bus
->sysdata
;
282 if (bus
->number
< cfg
->busr
.start
||
283 bus
->number
> cfg
->busr
.end
)
284 return PCIBIOS_DEVICE_NOT_FOUND
;
286 * The first device on the bus is the PEM PCIe bridge.
287 * Special case its config access.
289 if (bus
->number
== cfg
->busr
.start
)
290 return thunder_pem_bridge_write(bus
, devfn
, where
, size
, val
);
293 return pci_generic_config_write(bus
, devfn
, where
, size
, val
);
296 static int thunder_pem_init(struct device
*dev
, struct pci_config_window
*cfg
,
297 struct resource
*res_pem
)
299 struct thunder_pem_pci
*pem_pci
;
300 resource_size_t bar4_start
;
302 pem_pci
= devm_kzalloc(dev
, sizeof(*pem_pci
), GFP_KERNEL
);
306 pem_pci
->pem_reg_base
= devm_ioremap(dev
, res_pem
->start
, 0x10000);
307 if (!pem_pci
->pem_reg_base
)
311 * The MSI-X BAR for the PEM and AER interrupts is located at
312 * a fixed offset from the PEM register base. Generate a
313 * fragment of the synthesized Enhanced Allocation capability
314 * structure here for the BAR.
316 bar4_start
= res_pem
->start
+ 0xf00000;
317 pem_pci
->ea_entry
[0] = (u32
)bar4_start
| 2;
318 pem_pci
->ea_entry
[1] = (u32
)(res_pem
->end
- bar4_start
) & ~3u;
319 pem_pci
->ea_entry
[2] = (u32
)(bar4_start
>> 32);
325 #if defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS)
327 #define PEM_RES_BASE 0x87e0c0000000UL
328 #define PEM_NODE_MASK GENMASK(45, 44)
329 #define PEM_INDX_MASK GENMASK(26, 24)
330 #define PEM_MIN_DOM_IN_NODE 4
331 #define PEM_MAX_DOM_IN_NODE 10
333 static void thunder_pem_reserve_range(struct device
*dev
, int seg
,
336 resource_size_t start
= r
->start
, end
= r
->end
;
337 struct resource
*res
;
338 const char *regionid
;
340 regionid
= kasprintf(GFP_KERNEL
, "PEM RC:%d", seg
);
344 res
= request_mem_region(start
, end
- start
+ 1, regionid
);
346 res
->flags
&= ~IORESOURCE_BUSY
;
350 dev_info(dev
, "%pR %s reserved\n", r
,
351 res
? "has been" : "could not be");
354 static void thunder_pem_legacy_fw(struct acpi_pci_root
*root
,
355 struct resource
*res_pem
)
357 int node
= acpi_get_node(root
->device
->handle
);
360 if (node
== NUMA_NO_NODE
)
363 index
= root
->segment
- PEM_MIN_DOM_IN_NODE
;
364 index
-= node
* PEM_MAX_DOM_IN_NODE
;
365 res_pem
->start
= PEM_RES_BASE
| FIELD_PREP(PEM_NODE_MASK
, node
) |
366 FIELD_PREP(PEM_INDX_MASK
, index
);
367 res_pem
->flags
= IORESOURCE_MEM
;
370 static int thunder_pem_acpi_init(struct pci_config_window
*cfg
)
372 struct device
*dev
= cfg
->parent
;
373 struct acpi_device
*adev
= to_acpi_device(dev
);
374 struct acpi_pci_root
*root
= acpi_driver_data(adev
);
375 struct resource
*res_pem
;
378 res_pem
= devm_kzalloc(&adev
->dev
, sizeof(*res_pem
), GFP_KERNEL
);
382 ret
= acpi_get_rc_resources(dev
, "CAVA02B", root
->segment
, res_pem
);
385 * If we fail to gather resources it means that we run with old
386 * FW where we need to calculate PEM-specific resources manually.
389 thunder_pem_legacy_fw(root
, res_pem
);
391 * Reserve 64K size PEM specific resources. The full 16M range
392 * size is required for thunder_pem_init() call.
394 res_pem
->end
= res_pem
->start
+ SZ_64K
- 1;
395 thunder_pem_reserve_range(dev
, root
->segment
, res_pem
);
396 res_pem
->end
= res_pem
->start
+ SZ_16M
- 1;
398 /* Reserve PCI configuration space as well. */
399 thunder_pem_reserve_range(dev
, root
->segment
, &cfg
->res
);
402 return thunder_pem_init(dev
, cfg
, res_pem
);
405 struct pci_ecam_ops thunder_pem_ecam_ops
= {
407 .init
= thunder_pem_acpi_init
,
409 .map_bus
= pci_ecam_map_bus
,
410 .read
= thunder_pem_config_read
,
411 .write
= thunder_pem_config_write
,
417 #ifdef CONFIG_PCI_HOST_THUNDER_PEM
419 static int thunder_pem_platform_init(struct pci_config_window
*cfg
)
421 struct device
*dev
= cfg
->parent
;
422 struct platform_device
*pdev
= to_platform_device(dev
);
423 struct resource
*res_pem
;
429 * The second register range is the PEM bridge to the PCIe
430 * bus. It has a different config access method than those
431 * devices behind the bridge.
433 res_pem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
435 dev_err(dev
, "missing \"reg[1]\"property\n");
439 return thunder_pem_init(dev
, cfg
, res_pem
);
442 static struct pci_ecam_ops pci_thunder_pem_ops
= {
444 .init
= thunder_pem_platform_init
,
446 .map_bus
= pci_ecam_map_bus
,
447 .read
= thunder_pem_config_read
,
448 .write
= thunder_pem_config_write
,
452 static const struct of_device_id thunder_pem_of_match
[] = {
453 { .compatible
= "cavium,pci-host-thunder-pem" },
457 static int thunder_pem_probe(struct platform_device
*pdev
)
459 return pci_host_common_probe(pdev
, &pci_thunder_pem_ops
);
462 static struct platform_driver thunder_pem_driver
= {
464 .name
= KBUILD_MODNAME
,
465 .of_match_table
= thunder_pem_of_match
,
466 .suppress_bind_attrs
= true,
468 .probe
= thunder_pem_probe
,
470 builtin_platform_driver(thunder_pem_driver
);