xref: /openbmc/linux/arch/powerpc/kernel/rtas_pci.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2d3d2176aSDavid Gibson /*
3d3d2176aSDavid Gibson  * Copyright (C) 2001 Dave Engebretsen, IBM Corporation
4d3d2176aSDavid Gibson  * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
5d3d2176aSDavid Gibson  *
6d3d2176aSDavid Gibson  * RTAS specific routines for PCI.
7d3d2176aSDavid Gibson  *
8d3d2176aSDavid Gibson  * Based on code from pci.c, chrp_pci.c and pSeries_pci.c
9d3d2176aSDavid Gibson  */
10d3d2176aSDavid Gibson 
11d3d2176aSDavid Gibson #include <linux/kernel.h>
12d3d2176aSDavid Gibson #include <linux/threads.h>
13d3d2176aSDavid Gibson #include <linux/pci.h>
14d3d2176aSDavid Gibson #include <linux/string.h>
15d3d2176aSDavid Gibson #include <linux/init.h>
1665fddcfcSMike Rapoport #include <linux/pgtable.h>
17e6f6390aSChristophe Leroy #include <linux/of_address.h>
18e6f6390aSChristophe Leroy #include <linux/of_fdt.h>
19d3d2176aSDavid Gibson 
20d3d2176aSDavid Gibson #include <asm/io.h>
21d3d2176aSDavid Gibson #include <asm/irq.h>
22d3d2176aSDavid Gibson #include <asm/machdep.h>
23d3d2176aSDavid Gibson #include <asm/pci-bridge.h>
24d3d2176aSDavid Gibson #include <asm/iommu.h>
25d3d2176aSDavid Gibson #include <asm/rtas.h>
26d3d2176aSDavid Gibson #include <asm/mpic.h>
27d3d2176aSDavid Gibson #include <asm/ppc-pci.h>
2868a64357SBenjamin Herrenschmidt #include <asm/eeh.h>
29d3d2176aSDavid Gibson 
30d3d2176aSDavid Gibson /* RTAS tokens */
31d3d2176aSDavid Gibson static int read_pci_config;
32d3d2176aSDavid Gibson static int write_pci_config;
33d3d2176aSDavid Gibson static int ibm_read_pci_config;
34d3d2176aSDavid Gibson static int ibm_write_pci_config;
35d3d2176aSDavid Gibson 
config_access_valid(struct pci_dn * dn,int where)36d3d2176aSDavid Gibson static inline int config_access_valid(struct pci_dn *dn, int where)
37d3d2176aSDavid Gibson {
38d3d2176aSDavid Gibson 	if (where < 256)
39d3d2176aSDavid Gibson 		return 1;
40d3d2176aSDavid Gibson 	if (where < 4096 && dn->pci_ext_config_space)
41d3d2176aSDavid Gibson 		return 1;
42d3d2176aSDavid Gibson 
43d3d2176aSDavid Gibson 	return 0;
44d3d2176aSDavid Gibson }
45d3d2176aSDavid Gibson 
rtas_read_config(struct pci_dn * pdn,int where,int size,u32 * val)467684b40cSLinas Vepstas int rtas_read_config(struct pci_dn *pdn, int where, int size, u32 *val)
47d3d2176aSDavid Gibson {
48d3d2176aSDavid Gibson 	int returnval = -1;
49d3d2176aSDavid Gibson 	unsigned long buid, addr;
50d3d2176aSDavid Gibson 	int ret;
51d3d2176aSDavid Gibson 
52d3d2176aSDavid Gibson 	if (!pdn)
53d3d2176aSDavid Gibson 		return PCIBIOS_DEVICE_NOT_FOUND;
54d3d2176aSDavid Gibson 	if (!config_access_valid(pdn, where))
55d3d2176aSDavid Gibson 		return PCIBIOS_BAD_REGISTER_NUMBER;
563409eb4eSGavin Shan #ifdef CONFIG_EEH
573409eb4eSGavin Shan 	if (pdn->edev && pdn->edev->pe &&
583409eb4eSGavin Shan 	    (pdn->edev->pe->state & EEH_PE_CFG_BLOCKED))
593409eb4eSGavin Shan 		return PCIBIOS_SET_FAILED;
603409eb4eSGavin Shan #endif
61d3d2176aSDavid Gibson 
626f3d5d3cSMichael Ellerman 	addr = rtas_config_addr(pdn->busno, pdn->devfn, where);
63d3d2176aSDavid Gibson 	buid = pdn->phb->buid;
64d3d2176aSDavid Gibson 	if (buid) {
65d3d2176aSDavid Gibson 		ret = rtas_call(ibm_read_pci_config, 4, 2, &returnval,
66d3d2176aSDavid Gibson 				addr, BUID_HI(buid), BUID_LO(buid), size);
67d3d2176aSDavid Gibson 	} else {
68d3d2176aSDavid Gibson 		ret = rtas_call(read_pci_config, 2, 2, &returnval, addr, size);
69d3d2176aSDavid Gibson 	}
70d3d2176aSDavid Gibson 	*val = returnval;
71d3d2176aSDavid Gibson 
72d3d2176aSDavid Gibson 	if (ret)
73d3d2176aSDavid Gibson 		return PCIBIOS_DEVICE_NOT_FOUND;
74d3d2176aSDavid Gibson 
75d3d2176aSDavid Gibson 	return PCIBIOS_SUCCESSFUL;
76d3d2176aSDavid Gibson }
77d3d2176aSDavid Gibson 
rtas_pci_read_config(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 * val)78d3d2176aSDavid Gibson static int rtas_pci_read_config(struct pci_bus *bus,
79d3d2176aSDavid Gibson 				unsigned int devfn,
80d3d2176aSDavid Gibson 				int where, int size, u32 *val)
81d3d2176aSDavid Gibson {
82d0914f50SGavin Shan 	struct pci_dn *pdn;
83d0914f50SGavin Shan 	int ret;
84d3d2176aSDavid Gibson 
85d0914f50SGavin Shan 	*val = 0xFFFFFFFF;
86d3d2176aSDavid Gibson 
87f9df74dfSBryant G. Ly 	pdn = pci_get_pdn_by_devfn(bus, devfn);
88d0914f50SGavin Shan 
89f9df74dfSBryant G. Ly 	/* Validity of pdn is checked in here */
90d0914f50SGavin Shan 	ret = rtas_read_config(pdn, where, size, val);
91d0914f50SGavin Shan 	if (*val == EEH_IO_ERROR_VALUE(size) &&
92c6406d8fSGavin Shan 	    eeh_dev_check_failure(pdn_to_eeh_dev(pdn)))
93d0914f50SGavin Shan 		return PCIBIOS_DEVICE_NOT_FOUND;
94d0914f50SGavin Shan 
95d0914f50SGavin Shan 	return ret;
96d3d2176aSDavid Gibson }
97d3d2176aSDavid Gibson 
rtas_write_config(struct pci_dn * pdn,int where,int size,u32 val)98d3d2176aSDavid Gibson int rtas_write_config(struct pci_dn *pdn, int where, int size, u32 val)
99d3d2176aSDavid Gibson {
100d3d2176aSDavid Gibson 	unsigned long buid, addr;
101d3d2176aSDavid Gibson 	int ret;
102d3d2176aSDavid Gibson 
103d3d2176aSDavid Gibson 	if (!pdn)
104d3d2176aSDavid Gibson 		return PCIBIOS_DEVICE_NOT_FOUND;
105d3d2176aSDavid Gibson 	if (!config_access_valid(pdn, where))
106d3d2176aSDavid Gibson 		return PCIBIOS_BAD_REGISTER_NUMBER;
1073409eb4eSGavin Shan #ifdef CONFIG_EEH
1083409eb4eSGavin Shan 	if (pdn->edev && pdn->edev->pe &&
1093409eb4eSGavin Shan 	    (pdn->edev->pe->state & EEH_PE_CFG_BLOCKED))
1103409eb4eSGavin Shan 		return PCIBIOS_SET_FAILED;
1113409eb4eSGavin Shan #endif
112d3d2176aSDavid Gibson 
1136f3d5d3cSMichael Ellerman 	addr = rtas_config_addr(pdn->busno, pdn->devfn, where);
114d3d2176aSDavid Gibson 	buid = pdn->phb->buid;
115d3d2176aSDavid Gibson 	if (buid) {
116d3d2176aSDavid Gibson 		ret = rtas_call(ibm_write_pci_config, 5, 1, NULL, addr,
117d3d2176aSDavid Gibson 			BUID_HI(buid), BUID_LO(buid), size, (ulong) val);
118d3d2176aSDavid Gibson 	} else {
119d3d2176aSDavid Gibson 		ret = rtas_call(write_pci_config, 3, 1, NULL, addr, size, (ulong)val);
120d3d2176aSDavid Gibson 	}
121d3d2176aSDavid Gibson 
122d3d2176aSDavid Gibson 	if (ret)
123d3d2176aSDavid Gibson 		return PCIBIOS_DEVICE_NOT_FOUND;
124d3d2176aSDavid Gibson 
125d3d2176aSDavid Gibson 	return PCIBIOS_SUCCESSFUL;
126d3d2176aSDavid Gibson }
127d3d2176aSDavid Gibson 
rtas_pci_write_config(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 val)128d3d2176aSDavid Gibson static int rtas_pci_write_config(struct pci_bus *bus,
129d3d2176aSDavid Gibson 				 unsigned int devfn,
130d3d2176aSDavid Gibson 				 int where, int size, u32 val)
131d3d2176aSDavid Gibson {
132d0914f50SGavin Shan 	struct pci_dn *pdn;
133d3d2176aSDavid Gibson 
134f9df74dfSBryant G. Ly 	pdn = pci_get_pdn_by_devfn(bus, devfn);
135d0914f50SGavin Shan 
136f9df74dfSBryant G. Ly 	/* Validity of pdn is checked in here. */
1373409eb4eSGavin Shan 	return rtas_write_config(pdn, where, size, val);
138d3d2176aSDavid Gibson }
139d3d2176aSDavid Gibson 
1401c21a293SMichael Ellerman static struct pci_ops rtas_pci_ops = {
1418674e0c9SNathan Lynch 	.read = rtas_pci_read_config,
1428674e0c9SNathan Lynch 	.write = rtas_pci_write_config,
143d3d2176aSDavid Gibson };
144d3d2176aSDavid Gibson 
is_python(struct device_node * dev)1451c21a293SMichael Ellerman static int is_python(struct device_node *dev)
146d3d2176aSDavid Gibson {
147e2eb6392SStephen Rothwell 	const char *model = of_get_property(dev, "model", NULL);
148d3d2176aSDavid Gibson 
149d3d2176aSDavid Gibson 	if (model && strstr(model, "Python"))
150d3d2176aSDavid Gibson 		return 1;
151d3d2176aSDavid Gibson 
152d3d2176aSDavid Gibson 	return 0;
153d3d2176aSDavid Gibson }
154d3d2176aSDavid Gibson 
python_countermeasures(struct device_node * dev)155cc5d0189SBenjamin Herrenschmidt static void python_countermeasures(struct device_node *dev)
156d3d2176aSDavid Gibson {
157cc5d0189SBenjamin Herrenschmidt 	struct resource registers;
158d3d2176aSDavid Gibson 	void __iomem *chip_regs;
159d3d2176aSDavid Gibson 	volatile u32 val;
160d3d2176aSDavid Gibson 
161cc5d0189SBenjamin Herrenschmidt 	if (of_address_to_resource(dev, 0, &registers)) {
162cc5d0189SBenjamin Herrenschmidt 		printk(KERN_ERR "Can't get address for Python workarounds !\n");
163d3d2176aSDavid Gibson 		return;
164cc5d0189SBenjamin Herrenschmidt 	}
165d3d2176aSDavid Gibson 
166d3d2176aSDavid Gibson 	/* Python's register file is 1 MB in size. */
167cc5d0189SBenjamin Herrenschmidt 	chip_regs = ioremap(registers.start & ~(0xfffffUL), 0x100000);
168d3d2176aSDavid Gibson 
169d3d2176aSDavid Gibson 	/*
170d3d2176aSDavid Gibson 	 * Firmware doesn't always clear this bit which is critical
171d3d2176aSDavid Gibson 	 * for good performance - Anton
172d3d2176aSDavid Gibson 	 */
173d3d2176aSDavid Gibson 
174d3d2176aSDavid Gibson #define PRG_CL_RESET_VALID 0x00010000
175d3d2176aSDavid Gibson 
176d3d2176aSDavid Gibson 	val = in_be32(chip_regs + 0xf6030);
177d3d2176aSDavid Gibson 	if (val & PRG_CL_RESET_VALID) {
178d3d2176aSDavid Gibson 		printk(KERN_INFO "Python workaround: ");
179d3d2176aSDavid Gibson 		val &= ~PRG_CL_RESET_VALID;
180d3d2176aSDavid Gibson 		out_be32(chip_regs + 0xf6030, val);
181d3d2176aSDavid Gibson 		/*
182d3d2176aSDavid Gibson 		 * We must read it back for changes to
183d3d2176aSDavid Gibson 		 * take effect
184d3d2176aSDavid Gibson 		 */
185d3d2176aSDavid Gibson 		val = in_be32(chip_regs + 0xf6030);
186d3d2176aSDavid Gibson 		printk("reg0: %x\n", val);
187d3d2176aSDavid Gibson 	}
188d3d2176aSDavid Gibson 
189d3d2176aSDavid Gibson 	iounmap(chip_regs);
190d3d2176aSDavid Gibson }
191d3d2176aSDavid Gibson 
init_pci_config_tokens(void)192d3d2176aSDavid Gibson void __init init_pci_config_tokens(void)
193d3d2176aSDavid Gibson {
194*08273c9fSNathan Lynch 	read_pci_config = rtas_function_token(RTAS_FN_READ_PCI_CONFIG);
195*08273c9fSNathan Lynch 	write_pci_config = rtas_function_token(RTAS_FN_WRITE_PCI_CONFIG);
196*08273c9fSNathan Lynch 	ibm_read_pci_config = rtas_function_token(RTAS_FN_IBM_READ_PCI_CONFIG);
197*08273c9fSNathan Lynch 	ibm_write_pci_config = rtas_function_token(RTAS_FN_IBM_WRITE_PCI_CONFIG);
198d3d2176aSDavid Gibson }
199d3d2176aSDavid Gibson 
get_phb_buid(struct device_node * phb)200cad5cef6SGreg Kroah-Hartman unsigned long get_phb_buid(struct device_node *phb)
201d3d2176aSDavid Gibson {
2026506e710SBenjamin Herrenschmidt 	struct resource r;
203d3d2176aSDavid Gibson 
2046506e710SBenjamin Herrenschmidt 	if (ibm_read_pci_config == -1)
205d3d2176aSDavid Gibson 		return 0;
2066506e710SBenjamin Herrenschmidt 	if (of_address_to_resource(phb, 0, &r))
207d3d2176aSDavid Gibson 		return 0;
2086506e710SBenjamin Herrenschmidt 	return r.start;
209d3d2176aSDavid Gibson }
210d3d2176aSDavid Gibson 
phb_set_bus_ranges(struct device_node * dev,struct pci_controller * phb)211d3d2176aSDavid Gibson static int phb_set_bus_ranges(struct device_node *dev,
212d3d2176aSDavid Gibson 			      struct pci_controller *phb)
213d3d2176aSDavid Gibson {
214cf059965SCedric Le Goater 	const __be32 *bus_range;
215d3d2176aSDavid Gibson 	unsigned int len;
216d3d2176aSDavid Gibson 
217e2eb6392SStephen Rothwell 	bus_range = of_get_property(dev, "bus-range", &len);
218d3d2176aSDavid Gibson 	if (bus_range == NULL || len < 2 * sizeof(int)) {
219d3d2176aSDavid Gibson 		return 1;
220d3d2176aSDavid Gibson  	}
221d3d2176aSDavid Gibson 
222cf059965SCedric Le Goater 	phb->first_busno = be32_to_cpu(bus_range[0]);
223cf059965SCedric Le Goater 	phb->last_busno  = be32_to_cpu(bus_range[1]);
224d3d2176aSDavid Gibson 
225d3d2176aSDavid Gibson 	return 0;
226d3d2176aSDavid Gibson }
227d3d2176aSDavid Gibson 
rtas_setup_phb(struct pci_controller * phb)228cad5cef6SGreg Kroah-Hartman int rtas_setup_phb(struct pci_controller *phb)
229d3d2176aSDavid Gibson {
23044ef3390SStephen Rothwell 	struct device_node *dev = phb->dn;
2314c9d2800SBenjamin Herrenschmidt 
232d3d2176aSDavid Gibson 	if (is_python(dev))
233cc5d0189SBenjamin Herrenschmidt 		python_countermeasures(dev);
234d3d2176aSDavid Gibson 
235d3d2176aSDavid Gibson 	if (phb_set_bus_ranges(dev, phb))
236d3d2176aSDavid Gibson 		return 1;
237d3d2176aSDavid Gibson 
238d3d2176aSDavid Gibson 	phb->ops = &rtas_pci_ops;
239d3d2176aSDavid Gibson 	phb->buid = get_phb_buid(dev);
240d3d2176aSDavid Gibson 
241d3d2176aSDavid Gibson 	return 0;
242d3d2176aSDavid Gibson }
243