Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 2 | /* |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 3 | * 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. |
linas | ae65a39 | 2005-11-03 18:42:26 -0600 | [diff] [blame] | 7 | * |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 8 | * Based on code from pci.c, chrp_pci.c and pSeries_pci.c |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 9 | */ |
| 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 Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 16 | |
| 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 Rothwell | bbeb3f4 | 2005-09-27 13:51:59 +1000 | [diff] [blame] | 25 | #include <asm/mpic.h> |
Stephen Rothwell | d387899 | 2005-09-28 02:50:25 +1000 | [diff] [blame] | 26 | #include <asm/ppc-pci.h> |
Benjamin Herrenschmidt | 68a6435 | 2006-11-13 09:27:39 +1100 | [diff] [blame] | 27 | #include <asm/eeh.h> |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 28 | |
| 29 | /* RTAS tokens */ |
| 30 | static int read_pci_config; |
| 31 | static int write_pci_config; |
| 32 | static int ibm_read_pci_config; |
| 33 | static int ibm_write_pci_config; |
| 34 | |
linas | ae65a39 | 2005-11-03 18:42:26 -0600 | [diff] [blame] | 35 | static inline int config_access_valid(struct pci_dn *dn, int where) |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 36 | { |
| 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 Vepstas | 7684b40 | 2005-11-03 18:55:19 -0600 | [diff] [blame] | 45 | int rtas_read_config(struct pci_dn *pdn, int where, int size, u32 *val) |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 46 | { |
| 47 | int returnval = -1; |
| 48 | unsigned long buid, addr; |
| 49 | int ret; |
| 50 | |
linas | ae65a39 | 2005-11-03 18:42:26 -0600 | [diff] [blame] | 51 | if (!pdn) |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 52 | return PCIBIOS_DEVICE_NOT_FOUND; |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 53 | if (!config_access_valid(pdn, where)) |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 54 | return PCIBIOS_BAD_REGISTER_NUMBER; |
Gavin Shan | 3409eb4 | 2014-10-01 17:07:52 +1000 | [diff] [blame] | 55 | #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 Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 60 | |
Michael Ellerman | 6f3d5d3 | 2006-08-16 22:04:14 +1000 | [diff] [blame] | 61 | addr = rtas_config_addr(pdn->busno, pdn->devfn, where); |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 62 | buid = pdn->phb->buid; |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 63 | if (buid) { |
| 64 | ret = rtas_call(ibm_read_pci_config, 4, 2, &returnval, |
linas | ae65a39 | 2005-11-03 18:42:26 -0600 | [diff] [blame] | 65 | addr, BUID_HI(buid), BUID_LO(buid), size); |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 66 | } 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 Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 74 | return PCIBIOS_SUCCESSFUL; |
| 75 | } |
| 76 | |
| 77 | static int rtas_pci_read_config(struct pci_bus *bus, |
| 78 | unsigned int devfn, |
| 79 | int where, int size, u32 *val) |
| 80 | { |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 81 | struct pci_dn *pdn; |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 82 | int ret; |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 83 | |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 84 | *val = 0xFFFFFFFF; |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 85 | |
Bryant G. Ly | f9df74d | 2017-08-29 08:11:51 -0500 | [diff] [blame] | 86 | pdn = pci_get_pdn_by_devfn(bus, devfn); |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 87 | |
Bryant G. Ly | f9df74d | 2017-08-29 08:11:51 -0500 | [diff] [blame] | 88 | /* Validity of pdn is checked in here */ |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 89 | ret = rtas_read_config(pdn, where, size, val); |
| 90 | if (*val == EEH_IO_ERROR_VALUE(size) && |
Gavin Shan | c6406d8 | 2015-03-17 16:15:08 +1100 | [diff] [blame] | 91 | eeh_dev_check_failure(pdn_to_eeh_dev(pdn))) |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 92 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 93 | |
| 94 | return ret; |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 95 | } |
| 96 | |
linas | ae65a39 | 2005-11-03 18:42:26 -0600 | [diff] [blame] | 97 | int rtas_write_config(struct pci_dn *pdn, int where, int size, u32 val) |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 98 | { |
| 99 | unsigned long buid, addr; |
| 100 | int ret; |
| 101 | |
linas | ae65a39 | 2005-11-03 18:42:26 -0600 | [diff] [blame] | 102 | if (!pdn) |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 103 | return PCIBIOS_DEVICE_NOT_FOUND; |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 104 | if (!config_access_valid(pdn, where)) |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 105 | return PCIBIOS_BAD_REGISTER_NUMBER; |
Gavin Shan | 3409eb4 | 2014-10-01 17:07:52 +1000 | [diff] [blame] | 106 | #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 Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 111 | |
Michael Ellerman | 6f3d5d3 | 2006-08-16 22:04:14 +1000 | [diff] [blame] | 112 | addr = rtas_config_addr(pdn->busno, pdn->devfn, where); |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 113 | buid = pdn->phb->buid; |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 114 | if (buid) { |
linas | ae65a39 | 2005-11-03 18:42:26 -0600 | [diff] [blame] | 115 | ret = rtas_call(ibm_write_pci_config, 5, 1, NULL, addr, |
| 116 | BUID_HI(buid), BUID_LO(buid), size, (ulong) val); |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 117 | } 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 | |
| 127 | static int rtas_pci_write_config(struct pci_bus *bus, |
| 128 | unsigned int devfn, |
| 129 | int where, int size, u32 val) |
| 130 | { |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 131 | struct pci_dn *pdn; |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 132 | |
Bryant G. Ly | f9df74d | 2017-08-29 08:11:51 -0500 | [diff] [blame] | 133 | pdn = pci_get_pdn_by_devfn(bus, devfn); |
Gavin Shan | d0914f5 | 2014-04-24 18:00:12 +1000 | [diff] [blame] | 134 | |
Bryant G. Ly | f9df74d | 2017-08-29 08:11:51 -0500 | [diff] [blame] | 135 | /* Validity of pdn is checked in here. */ |
Gavin Shan | 3409eb4 | 2014-10-01 17:07:52 +1000 | [diff] [blame] | 136 | return rtas_write_config(pdn, where, size, val); |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 137 | } |
| 138 | |
Michael Ellerman | 1c21a29 | 2008-05-08 14:27:19 +1000 | [diff] [blame] | 139 | static struct pci_ops rtas_pci_ops = { |
Nathan Lynch | 8674e0c | 2007-08-10 05:18:36 +1000 | [diff] [blame] | 140 | .read = rtas_pci_read_config, |
| 141 | .write = rtas_pci_write_config, |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 142 | }; |
| 143 | |
Michael Ellerman | 1c21a29 | 2008-05-08 14:27:19 +1000 | [diff] [blame] | 144 | static int is_python(struct device_node *dev) |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 145 | { |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 146 | const char *model = of_get_property(dev, "model", NULL); |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 147 | |
| 148 | if (model && strstr(model, "Python")) |
| 149 | return 1; |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 154 | static void python_countermeasures(struct device_node *dev) |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 155 | { |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 156 | struct resource registers; |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 157 | void __iomem *chip_regs; |
| 158 | volatile u32 val; |
| 159 | |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 160 | if (of_address_to_resource(dev, 0, ®isters)) { |
| 161 | printk(KERN_ERR "Can't get address for Python workarounds !\n"); |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 162 | return; |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 163 | } |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 164 | |
| 165 | /* Python's register file is 1 MB in size. */ |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 166 | chip_regs = ioremap(registers.start & ~(0xfffffUL), 0x100000); |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 167 | |
linas | ae65a39 | 2005-11-03 18:42:26 -0600 | [diff] [blame] | 168 | /* |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 169 | * 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 Shan | db38f29 | 2013-03-08 01:47:58 +0000 | [diff] [blame] | 191 | void __init init_pci_config_tokens(void) |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 192 | { |
| 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 Shan | db38f29 | 2013-03-08 01:47:58 +0000 | [diff] [blame] | 199 | unsigned long get_phb_buid(struct device_node *phb) |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 200 | { |
Benjamin Herrenschmidt | 6506e71 | 2006-11-11 17:25:06 +1100 | [diff] [blame] | 201 | struct resource r; |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 202 | |
Benjamin Herrenschmidt | 6506e71 | 2006-11-11 17:25:06 +1100 | [diff] [blame] | 203 | if (ibm_read_pci_config == -1) |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 204 | return 0; |
Benjamin Herrenschmidt | 6506e71 | 2006-11-11 17:25:06 +1100 | [diff] [blame] | 205 | if (of_address_to_resource(phb, 0, &r)) |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 206 | return 0; |
Benjamin Herrenschmidt | 6506e71 | 2006-11-11 17:25:06 +1100 | [diff] [blame] | 207 | return r.start; |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static int phb_set_bus_ranges(struct device_node *dev, |
| 211 | struct pci_controller *phb) |
| 212 | { |
Cedric Le Goater | cf05996 | 2013-09-23 14:17:54 +0200 | [diff] [blame] | 213 | const __be32 *bus_range; |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 214 | unsigned int len; |
| 215 | |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 216 | bus_range = of_get_property(dev, "bus-range", &len); |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 217 | if (bus_range == NULL || len < 2 * sizeof(int)) { |
| 218 | return 1; |
| 219 | } |
linas | ae65a39 | 2005-11-03 18:42:26 -0600 | [diff] [blame] | 220 | |
Cedric Le Goater | cf05996 | 2013-09-23 14:17:54 +0200 | [diff] [blame] | 221 | phb->first_busno = be32_to_cpu(bus_range[0]); |
| 222 | phb->last_busno = be32_to_cpu(bus_range[1]); |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 227 | int rtas_setup_phb(struct pci_controller *phb) |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 228 | { |
Stephen Rothwell | 44ef339 | 2007-12-10 14:33:21 +1100 | [diff] [blame] | 229 | struct device_node *dev = phb->dn; |
Benjamin Herrenschmidt | 4c9d280 | 2006-11-11 17:25:08 +1100 | [diff] [blame] | 230 | |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 231 | if (is_python(dev)) |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 232 | python_countermeasures(dev); |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 233 | |
| 234 | if (phb_set_bus_ranges(dev, phb)) |
| 235 | return 1; |
| 236 | |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 237 | phb->ops = &rtas_pci_ops; |
| 238 | phb->buid = get_phb_buid(dev); |
| 239 | |
| 240 | return 0; |
| 241 | } |