blob: ae5e43eaca48462e65ad5d09f8a0e0f655eb6a2a [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +10002/*
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +10003 * Copyright (C) 2001 Dave Engebretsen, IBM Corporation
4 * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
5 *
6 * RTAS specific routines for PCI.
linasae65a392005-11-03 18:42:26 -06007 *
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +10008 * Based on code from pci.c, chrp_pci.c and pSeries_pci.c
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +10009 */
10
11#include <linux/kernel.h>
12#include <linux/threads.h>
13#include <linux/pci.h>
14#include <linux/string.h>
15#include <linux/init.h>
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +100016
17#include <asm/io.h>
18#include <asm/pgtable.h>
19#include <asm/irq.h>
20#include <asm/prom.h>
21#include <asm/machdep.h>
22#include <asm/pci-bridge.h>
23#include <asm/iommu.h>
24#include <asm/rtas.h>
Stephen Rothwellbbeb3f42005-09-27 13:51:59 +100025#include <asm/mpic.h>
Stephen Rothwelld3878992005-09-28 02:50:25 +100026#include <asm/ppc-pci.h>
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +110027#include <asm/eeh.h>
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +100028
29/* RTAS tokens */
30static int read_pci_config;
31static int write_pci_config;
32static int ibm_read_pci_config;
33static int ibm_write_pci_config;
34
linasae65a392005-11-03 18:42:26 -060035static inline int config_access_valid(struct pci_dn *dn, int where)
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +100036{
37 if (where < 256)
38 return 1;
39 if (where < 4096 && dn->pci_ext_config_space)
40 return 1;
41
42 return 0;
43}
44
Linas Vepstas7684b402005-11-03 18:55:19 -060045int rtas_read_config(struct pci_dn *pdn, int where, int size, u32 *val)
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +100046{
47 int returnval = -1;
48 unsigned long buid, addr;
49 int ret;
50
linasae65a392005-11-03 18:42:26 -060051 if (!pdn)
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +100052 return PCIBIOS_DEVICE_NOT_FOUND;
Paul Mackerras16353172005-09-06 13:17:54 +100053 if (!config_access_valid(pdn, where))
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +100054 return PCIBIOS_BAD_REGISTER_NUMBER;
Gavin Shan3409eb42014-10-01 17:07:52 +100055#ifdef CONFIG_EEH
56 if (pdn->edev && pdn->edev->pe &&
57 (pdn->edev->pe->state & EEH_PE_CFG_BLOCKED))
58 return PCIBIOS_SET_FAILED;
59#endif
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +100060
Michael Ellerman6f3d5d32006-08-16 22:04:14 +100061 addr = rtas_config_addr(pdn->busno, pdn->devfn, where);
Paul Mackerras16353172005-09-06 13:17:54 +100062 buid = pdn->phb->buid;
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +100063 if (buid) {
64 ret = rtas_call(ibm_read_pci_config, 4, 2, &returnval,
linasae65a392005-11-03 18:42:26 -060065 addr, BUID_HI(buid), BUID_LO(buid), size);
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +100066 } else {
67 ret = rtas_call(read_pci_config, 2, 2, &returnval, addr, size);
68 }
69 *val = returnval;
70
71 if (ret)
72 return PCIBIOS_DEVICE_NOT_FOUND;
73
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +100074 return PCIBIOS_SUCCESSFUL;
75}
76
77static int rtas_pci_read_config(struct pci_bus *bus,
78 unsigned int devfn,
79 int where, int size, u32 *val)
80{
Gavin Shand0914f52014-04-24 18:00:12 +100081 struct pci_dn *pdn;
Gavin Shand0914f52014-04-24 18:00:12 +100082 int ret;
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +100083
Gavin Shand0914f52014-04-24 18:00:12 +100084 *val = 0xFFFFFFFF;
Paul Mackerras16353172005-09-06 13:17:54 +100085
Bryant G. Lyf9df74d2017-08-29 08:11:51 -050086 pdn = pci_get_pdn_by_devfn(bus, devfn);
Gavin Shand0914f52014-04-24 18:00:12 +100087
Bryant G. Lyf9df74d2017-08-29 08:11:51 -050088 /* Validity of pdn is checked in here */
Gavin Shand0914f52014-04-24 18:00:12 +100089 ret = rtas_read_config(pdn, where, size, val);
90 if (*val == EEH_IO_ERROR_VALUE(size) &&
Gavin Shanc6406d82015-03-17 16:15:08 +110091 eeh_dev_check_failure(pdn_to_eeh_dev(pdn)))
Gavin Shand0914f52014-04-24 18:00:12 +100092 return PCIBIOS_DEVICE_NOT_FOUND;
93
94 return ret;
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +100095}
96
linasae65a392005-11-03 18:42:26 -060097int rtas_write_config(struct pci_dn *pdn, int where, int size, u32 val)
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +100098{
99 unsigned long buid, addr;
100 int ret;
101
linasae65a392005-11-03 18:42:26 -0600102 if (!pdn)
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000103 return PCIBIOS_DEVICE_NOT_FOUND;
Paul Mackerras16353172005-09-06 13:17:54 +1000104 if (!config_access_valid(pdn, where))
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000105 return PCIBIOS_BAD_REGISTER_NUMBER;
Gavin Shan3409eb42014-10-01 17:07:52 +1000106#ifdef CONFIG_EEH
107 if (pdn->edev && pdn->edev->pe &&
108 (pdn->edev->pe->state & EEH_PE_CFG_BLOCKED))
109 return PCIBIOS_SET_FAILED;
110#endif
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000111
Michael Ellerman6f3d5d32006-08-16 22:04:14 +1000112 addr = rtas_config_addr(pdn->busno, pdn->devfn, where);
Paul Mackerras16353172005-09-06 13:17:54 +1000113 buid = pdn->phb->buid;
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000114 if (buid) {
linasae65a392005-11-03 18:42:26 -0600115 ret = rtas_call(ibm_write_pci_config, 5, 1, NULL, addr,
116 BUID_HI(buid), BUID_LO(buid), size, (ulong) val);
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000117 } else {
118 ret = rtas_call(write_pci_config, 3, 1, NULL, addr, size, (ulong)val);
119 }
120
121 if (ret)
122 return PCIBIOS_DEVICE_NOT_FOUND;
123
124 return PCIBIOS_SUCCESSFUL;
125}
126
127static int rtas_pci_write_config(struct pci_bus *bus,
128 unsigned int devfn,
129 int where, int size, u32 val)
130{
Gavin Shand0914f52014-04-24 18:00:12 +1000131 struct pci_dn *pdn;
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000132
Bryant G. Lyf9df74d2017-08-29 08:11:51 -0500133 pdn = pci_get_pdn_by_devfn(bus, devfn);
Gavin Shand0914f52014-04-24 18:00:12 +1000134
Bryant G. Lyf9df74d2017-08-29 08:11:51 -0500135 /* Validity of pdn is checked in here. */
Gavin Shan3409eb42014-10-01 17:07:52 +1000136 return rtas_write_config(pdn, where, size, val);
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000137}
138
Michael Ellerman1c21a292008-05-08 14:27:19 +1000139static struct pci_ops rtas_pci_ops = {
Nathan Lynch8674e0c2007-08-10 05:18:36 +1000140 .read = rtas_pci_read_config,
141 .write = rtas_pci_write_config,
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000142};
143
Michael Ellerman1c21a292008-05-08 14:27:19 +1000144static int is_python(struct device_node *dev)
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000145{
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000146 const char *model = of_get_property(dev, "model", NULL);
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000147
148 if (model && strstr(model, "Python"))
149 return 1;
150
151 return 0;
152}
153
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100154static void python_countermeasures(struct device_node *dev)
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000155{
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100156 struct resource registers;
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000157 void __iomem *chip_regs;
158 volatile u32 val;
159
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100160 if (of_address_to_resource(dev, 0, &registers)) {
161 printk(KERN_ERR "Can't get address for Python workarounds !\n");
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000162 return;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100163 }
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000164
165 /* Python's register file is 1 MB in size. */
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100166 chip_regs = ioremap(registers.start & ~(0xfffffUL), 0x100000);
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000167
linasae65a392005-11-03 18:42:26 -0600168 /*
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000169 * Firmware doesn't always clear this bit which is critical
170 * for good performance - Anton
171 */
172
173#define PRG_CL_RESET_VALID 0x00010000
174
175 val = in_be32(chip_regs + 0xf6030);
176 if (val & PRG_CL_RESET_VALID) {
177 printk(KERN_INFO "Python workaround: ");
178 val &= ~PRG_CL_RESET_VALID;
179 out_be32(chip_regs + 0xf6030, val);
180 /*
181 * We must read it back for changes to
182 * take effect
183 */
184 val = in_be32(chip_regs + 0xf6030);
185 printk("reg0: %x\n", val);
186 }
187
188 iounmap(chip_regs);
189}
190
Gavin Shandb38f292013-03-08 01:47:58 +0000191void __init init_pci_config_tokens(void)
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000192{
193 read_pci_config = rtas_token("read-pci-config");
194 write_pci_config = rtas_token("write-pci-config");
195 ibm_read_pci_config = rtas_token("ibm,read-pci-config");
196 ibm_write_pci_config = rtas_token("ibm,write-pci-config");
197}
198
Gavin Shandb38f292013-03-08 01:47:58 +0000199unsigned long get_phb_buid(struct device_node *phb)
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000200{
Benjamin Herrenschmidt6506e712006-11-11 17:25:06 +1100201 struct resource r;
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000202
Benjamin Herrenschmidt6506e712006-11-11 17:25:06 +1100203 if (ibm_read_pci_config == -1)
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000204 return 0;
Benjamin Herrenschmidt6506e712006-11-11 17:25:06 +1100205 if (of_address_to_resource(phb, 0, &r))
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000206 return 0;
Benjamin Herrenschmidt6506e712006-11-11 17:25:06 +1100207 return r.start;
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000208}
209
210static int phb_set_bus_ranges(struct device_node *dev,
211 struct pci_controller *phb)
212{
Cedric Le Goatercf059962013-09-23 14:17:54 +0200213 const __be32 *bus_range;
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000214 unsigned int len;
215
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000216 bus_range = of_get_property(dev, "bus-range", &len);
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000217 if (bus_range == NULL || len < 2 * sizeof(int)) {
218 return 1;
219 }
linasae65a392005-11-03 18:42:26 -0600220
Cedric Le Goatercf059962013-09-23 14:17:54 +0200221 phb->first_busno = be32_to_cpu(bus_range[0]);
222 phb->last_busno = be32_to_cpu(bus_range[1]);
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000223
224 return 0;
225}
226
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800227int rtas_setup_phb(struct pci_controller *phb)
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000228{
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100229 struct device_node *dev = phb->dn;
Benjamin Herrenschmidt4c9d2802006-11-11 17:25:08 +1100230
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000231 if (is_python(dev))
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100232 python_countermeasures(dev);
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000233
234 if (phb_set_bus_ranges(dev, phb))
235 return 1;
236
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +1000237 phb->ops = &rtas_pci_ops;
238 phb->buid = get_phb_buid(dev);
239
240 return 0;
241}