xref: /openbmc/linux/drivers/misc/cxl/vphb.c (revision 2874c5fd284268364ece81a7bd936f3c8168e567)
1*2874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
26f7f0b3dSMichael Neuling /*
36f7f0b3dSMichael Neuling  * Copyright 2014 IBM Corp.
46f7f0b3dSMichael Neuling  */
56f7f0b3dSMichael Neuling 
66f7f0b3dSMichael Neuling #include <linux/pci.h>
76f7f0b3dSMichael Neuling #include <misc/cxl.h>
86f7f0b3dSMichael Neuling #include "cxl.h"
96f7f0b3dSMichael Neuling 
106f7f0b3dSMichael Neuling static int cxl_pci_probe_mode(struct pci_bus *bus)
116f7f0b3dSMichael Neuling {
126f7f0b3dSMichael Neuling 	return PCI_PROBE_NORMAL;
136f7f0b3dSMichael Neuling }
146f7f0b3dSMichael Neuling 
156f7f0b3dSMichael Neuling static int cxl_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
166f7f0b3dSMichael Neuling {
176f7f0b3dSMichael Neuling 	return -ENODEV;
186f7f0b3dSMichael Neuling }
196f7f0b3dSMichael Neuling 
206f7f0b3dSMichael Neuling static void cxl_teardown_msi_irqs(struct pci_dev *pdev)
216f7f0b3dSMichael Neuling {
226f7f0b3dSMichael Neuling 	/*
236f7f0b3dSMichael Neuling 	 * MSI should never be set but need still need to provide this call
246f7f0b3dSMichael Neuling 	 * back.
256f7f0b3dSMichael Neuling 	 */
266f7f0b3dSMichael Neuling }
276f7f0b3dSMichael Neuling 
286f7f0b3dSMichael Neuling static bool cxl_pci_enable_device_hook(struct pci_dev *dev)
296f7f0b3dSMichael Neuling {
306f7f0b3dSMichael Neuling 	struct pci_controller *phb;
316f7f0b3dSMichael Neuling 	struct cxl_afu *afu;
32f18a4e1dSFrederic Barrat 	struct cxl_context *ctx;
336f7f0b3dSMichael Neuling 
346f7f0b3dSMichael Neuling 	phb = pci_bus_to_host(dev->bus);
356f7f0b3dSMichael Neuling 	afu = (struct cxl_afu *)phb->private_data;
367d1647dcSAndrew Donnellan 
370d400f77SChristophe Lombard 	if (!cxl_ops->link_ok(afu->adapter, afu)) {
387d1647dcSAndrew Donnellan 		dev_warn(&dev->dev, "%s: Device link is down, refusing to enable AFU\n", __func__);
397d1647dcSAndrew Donnellan 		return false;
407d1647dcSAndrew Donnellan 	}
417d1647dcSAndrew Donnellan 
420617fc0cSChristoph Hellwig 	dev->dev.archdata.dma_offset = PAGE_OFFSET;
436f7f0b3dSMichael Neuling 
44f18a4e1dSFrederic Barrat 	/*
45f18a4e1dSFrederic Barrat 	 * Allocate a context to do cxl things too.  If we eventually do real
46f18a4e1dSFrederic Barrat 	 * DMA ops, we'll need a default context to attach them to
47f18a4e1dSFrederic Barrat 	 */
48f18a4e1dSFrederic Barrat 	ctx = cxl_dev_context_init(dev);
49f18a4e1dSFrederic Barrat 	if (IS_ERR(ctx))
50f18a4e1dSFrederic Barrat 		return false;
51f18a4e1dSFrederic Barrat 	dev->dev.archdata.cxl_ctx = ctx;
52f18a4e1dSFrederic Barrat 
53f18a4e1dSFrederic Barrat 	return (cxl_ops->afu_check_and_enable(afu) == 0);
54f18a4e1dSFrederic Barrat }
55f18a4e1dSFrederic Barrat 
56f18a4e1dSFrederic Barrat static void cxl_pci_disable_device(struct pci_dev *dev)
57f18a4e1dSFrederic Barrat {
58f18a4e1dSFrederic Barrat 	struct cxl_context *ctx = cxl_get_context(dev);
59f18a4e1dSFrederic Barrat 
60f18a4e1dSFrederic Barrat 	if (ctx) {
61f18a4e1dSFrederic Barrat 		if (ctx->status == STARTED) {
62f18a4e1dSFrederic Barrat 			dev_err(&dev->dev, "Default context started\n");
63f18a4e1dSFrederic Barrat 			return;
64f18a4e1dSFrederic Barrat 		}
65f18a4e1dSFrederic Barrat 		dev->dev.archdata.cxl_ctx = NULL;
66f18a4e1dSFrederic Barrat 		cxl_release_context(ctx);
67f18a4e1dSFrederic Barrat 	}
686f7f0b3dSMichael Neuling }
696f7f0b3dSMichael Neuling 
706f7f0b3dSMichael Neuling static resource_size_t cxl_pci_window_alignment(struct pci_bus *bus,
716f7f0b3dSMichael Neuling 						unsigned long type)
726f7f0b3dSMichael Neuling {
736f7f0b3dSMichael Neuling 	return 1;
746f7f0b3dSMichael Neuling }
756f7f0b3dSMichael Neuling 
766f7f0b3dSMichael Neuling static void cxl_pci_reset_secondary_bus(struct pci_dev *dev)
776f7f0b3dSMichael Neuling {
786f7f0b3dSMichael Neuling 	/* Should we do an AFU reset here ? */
796f7f0b3dSMichael Neuling }
806f7f0b3dSMichael Neuling 
816f7f0b3dSMichael Neuling static int cxl_pcie_cfg_record(u8 bus, u8 devfn)
826f7f0b3dSMichael Neuling {
836f7f0b3dSMichael Neuling 	return (bus << 8) + devfn;
846f7f0b3dSMichael Neuling }
856f7f0b3dSMichael Neuling 
8614a3ae34SAndrew Donnellan static inline struct cxl_afu *pci_bus_to_afu(struct pci_bus *bus)
876f7f0b3dSMichael Neuling {
8814a3ae34SAndrew Donnellan 	struct pci_controller *phb = bus ? pci_bus_to_host(bus) : NULL;
8914a3ae34SAndrew Donnellan 
9014a3ae34SAndrew Donnellan 	return phb ? phb->private_data : NULL;
9114a3ae34SAndrew Donnellan }
9214a3ae34SAndrew Donnellan 
93171ed0fcSAndrew Donnellan static void cxl_afu_configured_put(struct cxl_afu *afu)
94171ed0fcSAndrew Donnellan {
95171ed0fcSAndrew Donnellan 	atomic_dec_if_positive(&afu->configured_state);
96171ed0fcSAndrew Donnellan }
97171ed0fcSAndrew Donnellan 
98171ed0fcSAndrew Donnellan static bool cxl_afu_configured_get(struct cxl_afu *afu)
99171ed0fcSAndrew Donnellan {
100171ed0fcSAndrew Donnellan 	return atomic_inc_unless_negative(&afu->configured_state);
101171ed0fcSAndrew Donnellan }
102171ed0fcSAndrew Donnellan 
10314a3ae34SAndrew Donnellan static inline int cxl_pcie_config_info(struct pci_bus *bus, unsigned int devfn,
10414a3ae34SAndrew Donnellan 				       struct cxl_afu *afu, int *_record)
10514a3ae34SAndrew Donnellan {
106d601ea91SFrederic Barrat 	int record;
1076f7f0b3dSMichael Neuling 
108d601ea91SFrederic Barrat 	record = cxl_pcie_cfg_record(bus->number, devfn);
109d601ea91SFrederic Barrat 	if (record > afu->crs_num)
1106f7f0b3dSMichael Neuling 		return PCIBIOS_DEVICE_NOT_FOUND;
1116f7f0b3dSMichael Neuling 
112d601ea91SFrederic Barrat 	*_record = record;
1136f7f0b3dSMichael Neuling 	return 0;
1146f7f0b3dSMichael Neuling }
1156f7f0b3dSMichael Neuling 
1166f7f0b3dSMichael Neuling static int cxl_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
1176f7f0b3dSMichael Neuling 				int offset, int len, u32 *val)
1186f7f0b3dSMichael Neuling {
119d601ea91SFrederic Barrat 	int rc, record;
120d601ea91SFrederic Barrat 	struct cxl_afu *afu;
121d601ea91SFrederic Barrat 	u8 val8;
122d601ea91SFrederic Barrat 	u16 val16;
123d601ea91SFrederic Barrat 	u32 val32;
1246f7f0b3dSMichael Neuling 
12514a3ae34SAndrew Donnellan 	afu = pci_bus_to_afu(bus);
12614a3ae34SAndrew Donnellan 	/* Grab a reader lock on afu. */
127171ed0fcSAndrew Donnellan 	if (afu == NULL || !cxl_afu_configured_get(afu))
12814a3ae34SAndrew Donnellan 		return PCIBIOS_DEVICE_NOT_FOUND;
12914a3ae34SAndrew Donnellan 
13014a3ae34SAndrew Donnellan 	rc = cxl_pcie_config_info(bus, devfn, afu, &record);
1316f7f0b3dSMichael Neuling 	if (rc)
13214a3ae34SAndrew Donnellan 		goto out;
1336f7f0b3dSMichael Neuling 
134d601ea91SFrederic Barrat 	switch (len) {
135d601ea91SFrederic Barrat 	case 1:
136d601ea91SFrederic Barrat 		rc = cxl_ops->afu_cr_read8(afu, record, offset,	&val8);
137d601ea91SFrederic Barrat 		*val = val8;
138d601ea91SFrederic Barrat 		break;
139d601ea91SFrederic Barrat 	case 2:
140d601ea91SFrederic Barrat 		rc = cxl_ops->afu_cr_read16(afu, record, offset, &val16);
141d601ea91SFrederic Barrat 		*val = val16;
142d601ea91SFrederic Barrat 		break;
143d601ea91SFrederic Barrat 	case 4:
144d601ea91SFrederic Barrat 		rc = cxl_ops->afu_cr_read32(afu, record, offset, &val32);
145d601ea91SFrederic Barrat 		*val = val32;
146d601ea91SFrederic Barrat 		break;
147d601ea91SFrederic Barrat 	default:
148d601ea91SFrederic Barrat 		WARN_ON(1);
149d601ea91SFrederic Barrat 	}
150d601ea91SFrederic Barrat 
15114a3ae34SAndrew Donnellan out:
152171ed0fcSAndrew Donnellan 	cxl_afu_configured_put(afu);
15314a3ae34SAndrew Donnellan 	return rc ? PCIBIOS_DEVICE_NOT_FOUND : PCIBIOS_SUCCESSFUL;
1546f7f0b3dSMichael Neuling }
1556f7f0b3dSMichael Neuling 
1566f7f0b3dSMichael Neuling static int cxl_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
1576f7f0b3dSMichael Neuling 				 int offset, int len, u32 val)
1586f7f0b3dSMichael Neuling {
159d601ea91SFrederic Barrat 	int rc, record;
160d601ea91SFrederic Barrat 	struct cxl_afu *afu;
1616f7f0b3dSMichael Neuling 
16214a3ae34SAndrew Donnellan 	afu = pci_bus_to_afu(bus);
16314a3ae34SAndrew Donnellan 	/* Grab a reader lock on afu. */
164171ed0fcSAndrew Donnellan 	if (afu == NULL || !cxl_afu_configured_get(afu))
16514a3ae34SAndrew Donnellan 		return PCIBIOS_DEVICE_NOT_FOUND;
16614a3ae34SAndrew Donnellan 
16714a3ae34SAndrew Donnellan 	rc = cxl_pcie_config_info(bus, devfn, afu, &record);
1686f7f0b3dSMichael Neuling 	if (rc)
16914a3ae34SAndrew Donnellan 		goto out;
1706f7f0b3dSMichael Neuling 
171d601ea91SFrederic Barrat 	switch (len) {
172d601ea91SFrederic Barrat 	case 1:
173d601ea91SFrederic Barrat 		rc = cxl_ops->afu_cr_write8(afu, record, offset, val & 0xff);
174d601ea91SFrederic Barrat 		break;
175d601ea91SFrederic Barrat 	case 2:
176d601ea91SFrederic Barrat 		rc = cxl_ops->afu_cr_write16(afu, record, offset, val & 0xffff);
177d601ea91SFrederic Barrat 		break;
178d601ea91SFrederic Barrat 	case 4:
179d601ea91SFrederic Barrat 		rc = cxl_ops->afu_cr_write32(afu, record, offset, val);
180d601ea91SFrederic Barrat 		break;
181d601ea91SFrederic Barrat 	default:
182d601ea91SFrederic Barrat 		WARN_ON(1);
183d601ea91SFrederic Barrat 	}
1840b3f9c75SDaniel Axtens 
18514a3ae34SAndrew Donnellan out:
186171ed0fcSAndrew Donnellan 	cxl_afu_configured_put(afu);
18714a3ae34SAndrew Donnellan 	return rc ? PCIBIOS_SET_FAILED : PCIBIOS_SUCCESSFUL;
1886f7f0b3dSMichael Neuling }
1896f7f0b3dSMichael Neuling 
1906f7f0b3dSMichael Neuling static struct pci_ops cxl_pcie_pci_ops =
1916f7f0b3dSMichael Neuling {
1926f7f0b3dSMichael Neuling 	.read = cxl_pcie_read_config,
1936f7f0b3dSMichael Neuling 	.write = cxl_pcie_write_config,
1946f7f0b3dSMichael Neuling };
1956f7f0b3dSMichael Neuling 
1966f7f0b3dSMichael Neuling 
1976f7f0b3dSMichael Neuling static struct pci_controller_ops cxl_pci_controller_ops =
1986f7f0b3dSMichael Neuling {
1996f7f0b3dSMichael Neuling 	.probe_mode = cxl_pci_probe_mode,
2006f7f0b3dSMichael Neuling 	.enable_device_hook = cxl_pci_enable_device_hook,
201f18a4e1dSFrederic Barrat 	.disable_device = cxl_pci_disable_device,
202f18a4e1dSFrederic Barrat 	.release_device = cxl_pci_disable_device,
2036f7f0b3dSMichael Neuling 	.window_alignment = cxl_pci_window_alignment,
2046f7f0b3dSMichael Neuling 	.reset_secondary_bus = cxl_pci_reset_secondary_bus,
2056f7f0b3dSMichael Neuling 	.setup_msi_irqs = cxl_setup_msi_irqs,
2066f7f0b3dSMichael Neuling 	.teardown_msi_irqs = cxl_teardown_msi_irqs,
2076f7f0b3dSMichael Neuling };
2086f7f0b3dSMichael Neuling 
2096f7f0b3dSMichael Neuling int cxl_pci_vphb_add(struct cxl_afu *afu)
2106f7f0b3dSMichael Neuling {
211a4307390SFrederic Barrat 	struct pci_controller *phb;
212d601ea91SFrederic Barrat 	struct device_node *vphb_dn;
213d601ea91SFrederic Barrat 	struct device *parent;
2146f7f0b3dSMichael Neuling 
215e4f5fc00SIan Munsie 	/*
216e4f5fc00SIan Munsie 	 * If there are no AFU configuration records we won't have anything to
217e4f5fc00SIan Munsie 	 * expose under the vPHB, so skip creating one, returning success since
218e4f5fc00SIan Munsie 	 * this is still a valid case. This will also opt us out of EEH
219e4f5fc00SIan Munsie 	 * handling since we won't have anything special to do if there are no
220e4f5fc00SIan Munsie 	 * kernel drivers attached to the vPHB, and EEH handling is not yet
221e4f5fc00SIan Munsie 	 * supported in the peer model.
222e4f5fc00SIan Munsie 	 */
223e4f5fc00SIan Munsie 	if (!afu->crs_num)
224e4f5fc00SIan Munsie 		return 0;
225e4f5fc00SIan Munsie 
226a4307390SFrederic Barrat 	/* The parent device is the adapter. Reuse the device node of
227a4307390SFrederic Barrat 	 * the adapter.
228a4307390SFrederic Barrat 	 * We don't seem to care what device node is used for the vPHB,
229a4307390SFrederic Barrat 	 * but tools such as lsvpd walk up the device parents looking
230a4307390SFrederic Barrat 	 * for a valid location code, so we might as well show devices
231a4307390SFrederic Barrat 	 * attached to the adapter as being located on that adapter.
232a4307390SFrederic Barrat 	 */
233d601ea91SFrederic Barrat 	parent = afu->adapter->dev.parent;
234a4307390SFrederic Barrat 	vphb_dn = parent->of_node;
2356f7f0b3dSMichael Neuling 
2366f7f0b3dSMichael Neuling 	/* Alloc and setup PHB data structure */
237d601ea91SFrederic Barrat 	phb = pcibios_alloc_controller(vphb_dn);
2386f7f0b3dSMichael Neuling 	if (!phb)
2396f7f0b3dSMichael Neuling 		return -ENODEV;
2406f7f0b3dSMichael Neuling 
2416f7f0b3dSMichael Neuling 	/* Setup parent in sysfs */
242d601ea91SFrederic Barrat 	phb->parent = parent;
2436f7f0b3dSMichael Neuling 
2446f7f0b3dSMichael Neuling 	/* Setup the PHB using arch provided callback */
2456f7f0b3dSMichael Neuling 	phb->ops = &cxl_pcie_pci_ops;
246d601ea91SFrederic Barrat 	phb->cfg_addr = NULL;
2476fd40f19SAndrew Donnellan 	phb->cfg_data = NULL;
2486f7f0b3dSMichael Neuling 	phb->private_data = afu;
2496f7f0b3dSMichael Neuling 	phb->controller_ops = cxl_pci_controller_ops;
2506f7f0b3dSMichael Neuling 
2516f7f0b3dSMichael Neuling 	/* Scan the bus */
2526f7f0b3dSMichael Neuling 	pcibios_scan_phb(phb);
2536f7f0b3dSMichael Neuling 	if (phb->bus == NULL)
2546f7f0b3dSMichael Neuling 		return -ENXIO;
2556f7f0b3dSMichael Neuling 
2566f38a8b9SAndrew Donnellan 	/* Set release hook on root bus */
2576f38a8b9SAndrew Donnellan 	pci_set_host_bridge_release(to_pci_host_bridge(phb->bus->bridge),
2586f38a8b9SAndrew Donnellan 				    pcibios_free_controller_deferred,
2596f38a8b9SAndrew Donnellan 				    (void *) phb);
2606f38a8b9SAndrew Donnellan 
2616f7f0b3dSMichael Neuling 	/* Claim resources. This might need some rework as well depending
2626f7f0b3dSMichael Neuling 	 * whether we are doing probe-only or not, like assigning unassigned
2636f7f0b3dSMichael Neuling 	 * resources etc...
2646f7f0b3dSMichael Neuling 	 */
2656f7f0b3dSMichael Neuling 	pcibios_claim_one_bus(phb->bus);
2666f7f0b3dSMichael Neuling 
2676f7f0b3dSMichael Neuling 	/* Add probed PCI devices to the device model */
2686f7f0b3dSMichael Neuling 	pci_bus_add_devices(phb->bus);
2696f7f0b3dSMichael Neuling 
2706f7f0b3dSMichael Neuling 	afu->phb = phb;
2716f7f0b3dSMichael Neuling 
2726f7f0b3dSMichael Neuling 	return 0;
2736f7f0b3dSMichael Neuling }
2746f7f0b3dSMichael Neuling 
2756f7f0b3dSMichael Neuling void cxl_pci_vphb_remove(struct cxl_afu *afu)
2766f7f0b3dSMichael Neuling {
2776f7f0b3dSMichael Neuling 	struct pci_controller *phb;
2786f7f0b3dSMichael Neuling 
2796f7f0b3dSMichael Neuling 	/* If there is no configuration record we won't have one of these */
2806f7f0b3dSMichael Neuling 	if (!afu || !afu->phb)
2816f7f0b3dSMichael Neuling 		return;
2826f7f0b3dSMichael Neuling 
2836f7f0b3dSMichael Neuling 	phb = afu->phb;
2842e1a2556SAndrew Donnellan 	afu->phb = NULL;
2856f7f0b3dSMichael Neuling 
2866f7f0b3dSMichael Neuling 	pci_remove_root_bus(phb->bus);
2876f38a8b9SAndrew Donnellan 	/*
2886f38a8b9SAndrew Donnellan 	 * We don't free phb here - that's handled by
2896f38a8b9SAndrew Donnellan 	 * pcibios_free_controller_deferred()
2906f38a8b9SAndrew Donnellan 	 */
2916f7f0b3dSMichael Neuling }
2926f7f0b3dSMichael Neuling 
29317eb3eefSVaibhav Jain bool cxl_pci_is_vphb_device(struct pci_dev *dev)
29417eb3eefSVaibhav Jain {
29517eb3eefSVaibhav Jain 	struct pci_controller *phb;
29617eb3eefSVaibhav Jain 
29717eb3eefSVaibhav Jain 	phb = pci_bus_to_host(dev->bus);
29817eb3eefSVaibhav Jain 
299c8d43cf0SAlastair D'Silva 	return (phb->ops == &cxl_pcie_pci_ops);
30017eb3eefSVaibhav Jain }
30117eb3eefSVaibhav Jain 
3026f7f0b3dSMichael Neuling struct cxl_afu *cxl_pci_to_afu(struct pci_dev *dev)
3036f7f0b3dSMichael Neuling {
3046f7f0b3dSMichael Neuling 	struct pci_controller *phb;
3056f7f0b3dSMichael Neuling 
3066f7f0b3dSMichael Neuling 	phb = pci_bus_to_host(dev->bus);
3076f7f0b3dSMichael Neuling 
3086f7f0b3dSMichael Neuling 	return (struct cxl_afu *)phb->private_data;
3096f7f0b3dSMichael Neuling }
3106f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_pci_to_afu);
3116f7f0b3dSMichael Neuling 
3126f7f0b3dSMichael Neuling unsigned int cxl_pci_to_cfg_record(struct pci_dev *dev)
3136f7f0b3dSMichael Neuling {
3146f7f0b3dSMichael Neuling 	return cxl_pcie_cfg_record(dev->bus->number, dev->devfn);
3156f7f0b3dSMichael Neuling }
3166f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_pci_to_cfg_record);
317