blob: 6dbc73eb2ca25c3547519d7dc5c387611813ff25 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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 Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Arnd Bergmannc5a3c2e2005-06-23 09:43:23 +10009#include <linux/init.h>
10#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/pci.h>
13#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Olaf Heringc3b9d9a2007-03-22 23:14:07 +010015#include <asm/eeh.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/pci-bridge.h>
Stephen Rothwelld3878992005-09-28 02:50:25 +100017#include <asm/ppc-pci.h>
Alexey Kardashevskiy3be2df02018-12-19 19:52:19 +110018#include <asm/pci.h>
Anton Blanchard1217d342014-08-20 08:55:19 +100019#include "pseries.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Bryant G. Lydae72532017-11-09 08:00:34 -060021#ifdef CONFIG_PCI_IOV
Bryant G. Ly9a7f6b42018-01-05 10:45:51 -060022#define MAX_VFS_FOR_MAP_PE 256
23struct 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 Goater53137a92021-01-04 15:32:05 +010030static int pseries_send_map_pe(struct pci_dev *pdev, u16 num_vfs,
31 struct pe_map_bar_entry *vf_pe_array)
Bryant G. Ly9a7f6b42018-01-05 10:45:51 -060032{
33 struct pci_dn *pdn;
34 int rc;
35 unsigned long buid, addr;
Nathan Lynch08273c92023-02-10 12:42:08 -060036 int ibm_map_pes = rtas_function_token(RTAS_FN_IBM_OPEN_SRIOV_MAP_PE_NUMBER);
Bryant G. Ly9a7f6b42018-01-05 10:45:51 -060037
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 Goater53137a92021-01-04 15:32:05 +010062static void pseries_set_pe_num(struct pci_dev *pdev, u16 vf_index, __be16 pe_num)
Bryant G. Ly9a7f6b42018-01-05 10:45:51 -060063{
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 Goater53137a92021-01-04 15:32:05 +010076static int pseries_associate_pes(struct pci_dev *pdev, u16 num_vfs)
Bryant G. Ly9a7f6b42018-01-05 10:45:51 -060077{
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 Goater53137a92021-01-04 15:32:05 +0100120static int pseries_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
Bryant G. Ly9a7f6b42018-01-05 10:45:51 -0600121{
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 Goater53137a92021-01-04 15:32:05 +0100163static int pseries_pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
Bryant G. Lydae72532017-11-09 08:00:34 -0600164{
165 /* Allocate PCI data */
Oliver O'Halloran8cd6aac2019-08-21 16:26:54 +1000166 add_sriov_vf_pdns(pdev);
Bryant G. Ly9a7f6b42018-01-05 10:45:51 -0600167 return pseries_pci_sriov_enable(pdev, num_vfs);
Bryant G. Lydae72532017-11-09 08:00:34 -0600168}
169
Cédric Le Goater53137a92021-01-04 15:32:05 +0100170static int pseries_pcibios_sriov_disable(struct pci_dev *pdev)
Bryant G. Lydae72532017-11-09 08:00:34 -0600171{
Bryant G. Ly9a7f6b42018-01-05 10:45:51 -0600172 struct pci_dn *pdn;
173
174 pdn = pci_get_pdn(pdev);
175 /* Releasing pe_num_map */
176 kfree(pdn->pe_num_map);
Bryant G. Lydae72532017-11-09 08:00:34 -0600177 /* Release PCI data */
Oliver O'Halloran8cd6aac2019-08-21 16:26:54 +1000178 remove_sriov_vf_pdns(pdev);
Bryant G. Ly608c0d82017-11-09 08:00:35 -0600179 pci_vf_drivers_autoprobe(pdev, true);
Bryant G. Lydae72532017-11-09 08:00:34 -0600180 return 0;
181}
182#endif
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184static 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
197void __init pSeries_final_fixup(void)
198{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 pSeries_request_regions();
200
Sam Bobroffc44e4cc2019-08-16 14:48:10 +1000201 eeh_show_enabled();
Bryant G. Lydae72532017-11-09 08:00:34 -0600202
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 Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
209/*
210 * Assume the winbond 82c105 is the IDE controller on a
Olaf Hering0bcace32007-01-04 18:31:55 +0100211 * p610/p615/p630. We should probably be more careful in case
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 * someone tries to plug in a similar adapter.
213 */
214static void fixup_winbond_82c105(struct pci_dev* dev)
215{
Mika Westerberg09cc9002023-03-30 19:24:30 +0300216 struct resource *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 unsigned int reg;
218
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100219 if (!machine_is(pseries))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return;
221
222 printk("Using INTC for W82c105 IDE controller.\n");
223 pci_read_config_dword(dev, 0x40, &reg);
224 /* Enable LEGIRQ to use INTC instead of ISA interrupts */
225 pci_write_config_dword(dev, 0x40, reg | (1<<11));
226
Mika Westerberg09cc9002023-03-30 19:24:30 +0300227 pci_dev_for_each_resource(dev, r) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /* zap the 2nd function of the winbond chip */
Mika Westerberg09cc9002023-03-30 19:24:30 +0300229 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 Hering2d99c412007-02-10 21:38:37 +0100235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237}
238DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_WINBOND, PCI_DEVICE_ID_WINBOND_82C105,
239 fixup_winbond_82c105);
Kleber Sacilotto de Souzad82fb312013-05-03 12:43:12 +0000240
Frederic Barratc8754c52020-11-30 16:29:49 +0100241static 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 Souzad82fb312013-05-03 12:43:12 +0000260int pseries_root_bridge_prepare(struct pci_host_bridge *bridge)
261{
262 struct device_node *dn, *pdn;
263 struct pci_bus *bus;
Kleber Sacilotto de Souzab020cc62014-01-17 11:56:51 -0200264 u32 pcie_link_speed_stats[2];
265 int rc;
Kleber Sacilotto de Souzad82fb312013-05-03 12:43:12 +0000266
267 bus = bridge->bus;
268
Mauricio Faria de Oliveira2dd9c112016-08-11 17:25:40 -0300269 /* 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 Souzad82fb312013-05-03 12:43:12 +0000273 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 Souzab020cc62014-01-17 11:56:51 -0200278 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 Souzad82fb312013-05-03 12:43:12 +0000282 break;
283 }
284
285 of_node_put(pdn);
286
Kleber Sacilotto de Souzab020cc62014-01-17 11:56:51 -0200287 if (rc) {
Anton Blanchard7aa189c2014-11-03 08:18:06 +1100288 pr_debug("no ibm,pcie-link-speed-stats property\n");
Kleber Sacilotto de Souzad82fb312013-05-03 12:43:12 +0000289 return 0;
290 }
291
Frederic Barratc8754c52020-11-30 16:29:49 +0100292 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 Souzad82fb312013-05-03 12:43:12 +0000294 return 0;
295}