Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Copyright (C) 2001 Dave Engebretsen, IBM Corporation |
| 4 | * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM |
| 5 | * |
| 6 | * pSeries specific routines for PCI. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Arnd Bergmann | c5a3c2e | 2005-06-23 09:43:23 +1000 | [diff] [blame] | 9 | #include <linux/init.h> |
| 10 | #include <linux/ioport.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/pci.h> |
| 13 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
Olaf Hering | c3b9d9a | 2007-03-22 23:14:07 +0100 | [diff] [blame] | 15 | #include <asm/eeh.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <asm/pci-bridge.h> |
Stephen Rothwell | d387899 | 2005-09-28 02:50:25 +1000 | [diff] [blame] | 17 | #include <asm/ppc-pci.h> |
Alexey Kardashevskiy | 3be2df0 | 2018-12-19 19:52:19 +1100 | [diff] [blame] | 18 | #include <asm/pci.h> |
Anton Blanchard | 1217d34 | 2014-08-20 08:55:19 +1000 | [diff] [blame] | 19 | #include "pseries.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Bryant G. Ly | dae7253 | 2017-11-09 08:00:34 -0600 | [diff] [blame] | 21 | #ifdef CONFIG_PCI_IOV |
Bryant G. Ly | 9a7f6b4 | 2018-01-05 10:45:51 -0600 | [diff] [blame] | 22 | #define MAX_VFS_FOR_MAP_PE 256 |
| 23 | struct pe_map_bar_entry { |
| 24 | __be64 bar; /* Input: Virtual Function BAR */ |
| 25 | __be16 rid; /* Input: Virtual Function Router ID */ |
| 26 | __be16 pe_num; /* Output: Virtual Function PE Number */ |
| 27 | __be32 reserved; /* Reserved Space */ |
| 28 | }; |
| 29 | |
Cédric Le Goater | 53137a9 | 2021-01-04 15:32:05 +0100 | [diff] [blame] | 30 | static int pseries_send_map_pe(struct pci_dev *pdev, u16 num_vfs, |
| 31 | struct pe_map_bar_entry *vf_pe_array) |
Bryant G. Ly | 9a7f6b4 | 2018-01-05 10:45:51 -0600 | [diff] [blame] | 32 | { |
| 33 | struct pci_dn *pdn; |
| 34 | int rc; |
| 35 | unsigned long buid, addr; |
Nathan Lynch | 08273c9 | 2023-02-10 12:42:08 -0600 | [diff] [blame] | 36 | int ibm_map_pes = rtas_function_token(RTAS_FN_IBM_OPEN_SRIOV_MAP_PE_NUMBER); |
Bryant G. Ly | 9a7f6b4 | 2018-01-05 10:45:51 -0600 | [diff] [blame] | 37 | |
| 38 | if (ibm_map_pes == RTAS_UNKNOWN_SERVICE) |
| 39 | return -EINVAL; |
| 40 | |
| 41 | pdn = pci_get_pdn(pdev); |
| 42 | addr = rtas_config_addr(pdn->busno, pdn->devfn, 0); |
| 43 | buid = pdn->phb->buid; |
| 44 | spin_lock(&rtas_data_buf_lock); |
| 45 | memcpy(rtas_data_buf, vf_pe_array, |
| 46 | RTAS_DATA_BUF_SIZE); |
| 47 | rc = rtas_call(ibm_map_pes, 5, 1, NULL, addr, |
| 48 | BUID_HI(buid), BUID_LO(buid), |
| 49 | rtas_data_buf, |
| 50 | num_vfs * sizeof(struct pe_map_bar_entry)); |
| 51 | memcpy(vf_pe_array, rtas_data_buf, RTAS_DATA_BUF_SIZE); |
| 52 | spin_unlock(&rtas_data_buf_lock); |
| 53 | |
| 54 | if (rc) |
| 55 | dev_err(&pdev->dev, |
| 56 | "%s: Failed to associate pes PE#%lx, rc=%x\n", |
| 57 | __func__, addr, rc); |
| 58 | |
| 59 | return rc; |
| 60 | } |
| 61 | |
Cédric Le Goater | 53137a9 | 2021-01-04 15:32:05 +0100 | [diff] [blame] | 62 | static void pseries_set_pe_num(struct pci_dev *pdev, u16 vf_index, __be16 pe_num) |
Bryant G. Ly | 9a7f6b4 | 2018-01-05 10:45:51 -0600 | [diff] [blame] | 63 | { |
| 64 | struct pci_dn *pdn; |
| 65 | |
| 66 | pdn = pci_get_pdn(pdev); |
| 67 | pdn->pe_num_map[vf_index] = be16_to_cpu(pe_num); |
| 68 | dev_dbg(&pdev->dev, "VF %04x:%02x:%02x.%x associated with PE#%x\n", |
| 69 | pci_domain_nr(pdev->bus), |
| 70 | pdev->bus->number, |
| 71 | PCI_SLOT(pci_iov_virtfn_devfn(pdev, vf_index)), |
| 72 | PCI_FUNC(pci_iov_virtfn_devfn(pdev, vf_index)), |
| 73 | pdn->pe_num_map[vf_index]); |
| 74 | } |
| 75 | |
Cédric Le Goater | 53137a9 | 2021-01-04 15:32:05 +0100 | [diff] [blame] | 76 | static int pseries_associate_pes(struct pci_dev *pdev, u16 num_vfs) |
Bryant G. Ly | 9a7f6b4 | 2018-01-05 10:45:51 -0600 | [diff] [blame] | 77 | { |
| 78 | struct pci_dn *pdn; |
| 79 | int i, rc, vf_index; |
| 80 | struct pe_map_bar_entry *vf_pe_array; |
| 81 | struct resource *res; |
| 82 | u64 size; |
| 83 | |
| 84 | vf_pe_array = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL); |
| 85 | if (!vf_pe_array) |
| 86 | return -ENOMEM; |
| 87 | |
| 88 | pdn = pci_get_pdn(pdev); |
| 89 | /* create firmware structure to associate pes */ |
| 90 | for (vf_index = 0; vf_index < num_vfs; vf_index++) { |
| 91 | pdn->pe_num_map[vf_index] = IODA_INVALID_PE; |
| 92 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
| 93 | res = &pdev->resource[i + PCI_IOV_RESOURCES]; |
| 94 | if (!res->parent) |
| 95 | continue; |
| 96 | size = pcibios_iov_resource_alignment(pdev, i + |
| 97 | PCI_IOV_RESOURCES); |
| 98 | vf_pe_array[vf_index].bar = |
| 99 | cpu_to_be64(res->start + size * vf_index); |
| 100 | vf_pe_array[vf_index].rid = |
| 101 | cpu_to_be16((pci_iov_virtfn_bus(pdev, vf_index) |
| 102 | << 8) | pci_iov_virtfn_devfn(pdev, |
| 103 | vf_index)); |
| 104 | vf_pe_array[vf_index].pe_num = |
| 105 | cpu_to_be16(IODA_INVALID_PE); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | rc = pseries_send_map_pe(pdev, num_vfs, vf_pe_array); |
| 110 | /* Only zero is success */ |
| 111 | if (!rc) |
| 112 | for (vf_index = 0; vf_index < num_vfs; vf_index++) |
| 113 | pseries_set_pe_num(pdev, vf_index, |
| 114 | vf_pe_array[vf_index].pe_num); |
| 115 | |
| 116 | kfree(vf_pe_array); |
| 117 | return rc; |
| 118 | } |
| 119 | |
Cédric Le Goater | 53137a9 | 2021-01-04 15:32:05 +0100 | [diff] [blame] | 120 | static int pseries_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs) |
Bryant G. Ly | 9a7f6b4 | 2018-01-05 10:45:51 -0600 | [diff] [blame] | 121 | { |
| 122 | struct pci_dn *pdn; |
| 123 | int rc; |
| 124 | const int *max_vfs; |
| 125 | int max_config_vfs; |
| 126 | struct device_node *dn = pci_device_to_OF_node(pdev); |
| 127 | |
| 128 | max_vfs = of_get_property(dn, "ibm,number-of-configurable-vfs", NULL); |
| 129 | |
| 130 | if (!max_vfs) |
| 131 | return -EINVAL; |
| 132 | |
| 133 | /* First integer stores max config */ |
| 134 | max_config_vfs = of_read_number(&max_vfs[0], 1); |
| 135 | if (max_config_vfs < num_vfs && num_vfs > MAX_VFS_FOR_MAP_PE) { |
| 136 | dev_err(&pdev->dev, |
| 137 | "Num VFs %x > %x Configurable VFs\n", |
| 138 | num_vfs, (num_vfs > MAX_VFS_FOR_MAP_PE) ? |
| 139 | MAX_VFS_FOR_MAP_PE : max_config_vfs); |
| 140 | return -EINVAL; |
| 141 | } |
| 142 | |
| 143 | pdn = pci_get_pdn(pdev); |
| 144 | pdn->pe_num_map = kmalloc_array(num_vfs, |
| 145 | sizeof(*pdn->pe_num_map), |
| 146 | GFP_KERNEL); |
| 147 | if (!pdn->pe_num_map) |
| 148 | return -ENOMEM; |
| 149 | |
| 150 | rc = pseries_associate_pes(pdev, num_vfs); |
| 151 | |
| 152 | /* Anything other than zero is failure */ |
| 153 | if (rc) { |
| 154 | dev_err(&pdev->dev, "Failure to enable sriov: %x\n", rc); |
| 155 | kfree(pdn->pe_num_map); |
| 156 | } else { |
| 157 | pci_vf_drivers_autoprobe(pdev, false); |
| 158 | } |
| 159 | |
| 160 | return rc; |
| 161 | } |
| 162 | |
Cédric Le Goater | 53137a9 | 2021-01-04 15:32:05 +0100 | [diff] [blame] | 163 | static int pseries_pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs) |
Bryant G. Ly | dae7253 | 2017-11-09 08:00:34 -0600 | [diff] [blame] | 164 | { |
| 165 | /* Allocate PCI data */ |
Oliver O'Halloran | 8cd6aac | 2019-08-21 16:26:54 +1000 | [diff] [blame] | 166 | add_sriov_vf_pdns(pdev); |
Bryant G. Ly | 9a7f6b4 | 2018-01-05 10:45:51 -0600 | [diff] [blame] | 167 | return pseries_pci_sriov_enable(pdev, num_vfs); |
Bryant G. Ly | dae7253 | 2017-11-09 08:00:34 -0600 | [diff] [blame] | 168 | } |
| 169 | |
Cédric Le Goater | 53137a9 | 2021-01-04 15:32:05 +0100 | [diff] [blame] | 170 | static int pseries_pcibios_sriov_disable(struct pci_dev *pdev) |
Bryant G. Ly | dae7253 | 2017-11-09 08:00:34 -0600 | [diff] [blame] | 171 | { |
Bryant G. Ly | 9a7f6b4 | 2018-01-05 10:45:51 -0600 | [diff] [blame] | 172 | struct pci_dn *pdn; |
| 173 | |
| 174 | pdn = pci_get_pdn(pdev); |
| 175 | /* Releasing pe_num_map */ |
| 176 | kfree(pdn->pe_num_map); |
Bryant G. Ly | dae7253 | 2017-11-09 08:00:34 -0600 | [diff] [blame] | 177 | /* Release PCI data */ |
Oliver O'Halloran | 8cd6aac | 2019-08-21 16:26:54 +1000 | [diff] [blame] | 178 | remove_sriov_vf_pdns(pdev); |
Bryant G. Ly | 608c0d8 | 2017-11-09 08:00:35 -0600 | [diff] [blame] | 179 | pci_vf_drivers_autoprobe(pdev, true); |
Bryant G. Ly | dae7253 | 2017-11-09 08:00:34 -0600 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | #endif |
| 183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | static void __init pSeries_request_regions(void) |
| 185 | { |
| 186 | if (!isa_io_base) |
| 187 | return; |
| 188 | |
| 189 | request_region(0x20,0x20,"pic1"); |
| 190 | request_region(0xa0,0x20,"pic2"); |
| 191 | request_region(0x00,0x20,"dma1"); |
| 192 | request_region(0x40,0x20,"timer"); |
| 193 | request_region(0x80,0x10,"dma page reg"); |
| 194 | request_region(0xc0,0x20,"dma2"); |
| 195 | } |
| 196 | |
| 197 | void __init pSeries_final_fixup(void) |
| 198 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | pSeries_request_regions(); |
| 200 | |
Sam Bobroff | c44e4cc | 2019-08-16 14:48:10 +1000 | [diff] [blame] | 201 | eeh_show_enabled(); |
Bryant G. Ly | dae7253 | 2017-11-09 08:00:34 -0600 | [diff] [blame] | 202 | |
| 203 | #ifdef CONFIG_PCI_IOV |
| 204 | ppc_md.pcibios_sriov_enable = pseries_pcibios_sriov_enable; |
| 205 | ppc_md.pcibios_sriov_disable = pseries_pcibios_sriov_disable; |
| 206 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /* |
| 210 | * Assume the winbond 82c105 is the IDE controller on a |
Olaf Hering | 0bcace3 | 2007-01-04 18:31:55 +0100 | [diff] [blame] | 211 | * p610/p615/p630. We should probably be more careful in case |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | * someone tries to plug in a similar adapter. |
| 213 | */ |
| 214 | static void fixup_winbond_82c105(struct pci_dev* dev) |
| 215 | { |
Mika Westerberg | 09cc900 | 2023-03-30 19:24:30 +0300 | [diff] [blame] | 216 | struct resource *r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | unsigned int reg; |
| 218 | |
Benjamin Herrenschmidt | e822250 | 2006-03-28 23:15:54 +1100 | [diff] [blame] | 219 | if (!machine_is(pseries)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | return; |
| 221 | |
| 222 | printk("Using INTC for W82c105 IDE controller.\n"); |
| 223 | pci_read_config_dword(dev, 0x40, ®); |
| 224 | /* Enable LEGIRQ to use INTC instead of ISA interrupts */ |
| 225 | pci_write_config_dword(dev, 0x40, reg | (1<<11)); |
| 226 | |
Mika Westerberg | 09cc900 | 2023-03-30 19:24:30 +0300 | [diff] [blame] | 227 | pci_dev_for_each_resource(dev, r) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | /* zap the 2nd function of the winbond chip */ |
Mika Westerberg | 09cc900 | 2023-03-30 19:24:30 +0300 | [diff] [blame] | 229 | if (dev->bus->number == 0 && dev->devfn == 0x81 && |
| 230 | r->flags & IORESOURCE_IO) |
| 231 | r->flags &= ~IORESOURCE_IO; |
| 232 | if (r->start == 0 && r->end) { |
| 233 | r->flags = 0; |
| 234 | r->end = 0; |
Olaf Hering | 2d99c41 | 2007-02-10 21:38:37 +0100 | [diff] [blame] | 235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_WINBOND, PCI_DEVICE_ID_WINBOND_82C105, |
| 239 | fixup_winbond_82c105); |
Kleber Sacilotto de Souza | d82fb31 | 2013-05-03 12:43:12 +0000 | [diff] [blame] | 240 | |
Frederic Barrat | c8754c5 | 2020-11-30 16:29:49 +0100 | [diff] [blame] | 241 | static enum pci_bus_speed prop_to_pci_speed(u32 prop) |
| 242 | { |
| 243 | switch (prop) { |
| 244 | case 0x01: |
| 245 | return PCIE_SPEED_2_5GT; |
| 246 | case 0x02: |
| 247 | return PCIE_SPEED_5_0GT; |
| 248 | case 0x04: |
| 249 | return PCIE_SPEED_8_0GT; |
| 250 | case 0x08: |
| 251 | return PCIE_SPEED_16_0GT; |
| 252 | case 0x10: |
| 253 | return PCIE_SPEED_32_0GT; |
| 254 | default: |
| 255 | pr_debug("Unexpected PCI link speed property value\n"); |
| 256 | return PCI_SPEED_UNKNOWN; |
| 257 | } |
| 258 | } |
| 259 | |
Kleber Sacilotto de Souza | d82fb31 | 2013-05-03 12:43:12 +0000 | [diff] [blame] | 260 | int pseries_root_bridge_prepare(struct pci_host_bridge *bridge) |
| 261 | { |
| 262 | struct device_node *dn, *pdn; |
| 263 | struct pci_bus *bus; |
Kleber Sacilotto de Souza | b020cc6 | 2014-01-17 11:56:51 -0200 | [diff] [blame] | 264 | u32 pcie_link_speed_stats[2]; |
| 265 | int rc; |
Kleber Sacilotto de Souza | d82fb31 | 2013-05-03 12:43:12 +0000 | [diff] [blame] | 266 | |
| 267 | bus = bridge->bus; |
| 268 | |
Mauricio Faria de Oliveira | 2dd9c11 | 2016-08-11 17:25:40 -0300 | [diff] [blame] | 269 | /* Rely on the pcibios_free_controller_deferred() callback. */ |
| 270 | pci_set_host_bridge_release(bridge, pcibios_free_controller_deferred, |
| 271 | (void *) pci_bus_to_host(bus)); |
| 272 | |
Kleber Sacilotto de Souza | d82fb31 | 2013-05-03 12:43:12 +0000 | [diff] [blame] | 273 | dn = pcibios_get_phb_of_node(bus); |
| 274 | if (!dn) |
| 275 | return 0; |
| 276 | |
| 277 | for (pdn = dn; pdn != NULL; pdn = of_get_next_parent(pdn)) { |
Kleber Sacilotto de Souza | b020cc6 | 2014-01-17 11:56:51 -0200 | [diff] [blame] | 278 | rc = of_property_read_u32_array(pdn, |
| 279 | "ibm,pcie-link-speed-stats", |
| 280 | &pcie_link_speed_stats[0], 2); |
| 281 | if (!rc) |
Kleber Sacilotto de Souza | d82fb31 | 2013-05-03 12:43:12 +0000 | [diff] [blame] | 282 | break; |
| 283 | } |
| 284 | |
| 285 | of_node_put(pdn); |
| 286 | |
Kleber Sacilotto de Souza | b020cc6 | 2014-01-17 11:56:51 -0200 | [diff] [blame] | 287 | if (rc) { |
Anton Blanchard | 7aa189c | 2014-11-03 08:18:06 +1100 | [diff] [blame] | 288 | pr_debug("no ibm,pcie-link-speed-stats property\n"); |
Kleber Sacilotto de Souza | d82fb31 | 2013-05-03 12:43:12 +0000 | [diff] [blame] | 289 | return 0; |
| 290 | } |
| 291 | |
Frederic Barrat | c8754c5 | 2020-11-30 16:29:49 +0100 | [diff] [blame] | 292 | bus->max_bus_speed = prop_to_pci_speed(pcie_link_speed_stats[0]); |
| 293 | bus->cur_bus_speed = prop_to_pci_speed(pcie_link_speed_stats[1]); |
Kleber Sacilotto de Souza | d82fb31 | 2013-05-03 12:43:12 +0000 | [diff] [blame] | 294 | return 0; |
| 295 | } |