xref: /openbmc/linux/drivers/misc/cxl/vphb.c (revision f18a4e1d973bc69a50419eb8918f458ea89c6c3f)
16f7f0b3dSMichael Neuling /*
26f7f0b3dSMichael Neuling  * Copyright 2014 IBM Corp.
36f7f0b3dSMichael Neuling  *
46f7f0b3dSMichael Neuling  * This program is free software; you can redistribute it and/or
56f7f0b3dSMichael Neuling  * modify it under the terms of the GNU General Public License
66f7f0b3dSMichael Neuling  * as published by the Free Software Foundation; either version
76f7f0b3dSMichael Neuling  * 2 of the License, or (at your option) any later version.
86f7f0b3dSMichael Neuling  */
96f7f0b3dSMichael Neuling 
106f7f0b3dSMichael Neuling #include <linux/pci.h>
116f7f0b3dSMichael Neuling #include <misc/cxl.h>
126f7f0b3dSMichael Neuling #include "cxl.h"
136f7f0b3dSMichael Neuling 
146f7f0b3dSMichael Neuling static int cxl_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
156f7f0b3dSMichael Neuling {
166f7f0b3dSMichael Neuling 	if (dma_mask < DMA_BIT_MASK(64)) {
176f7f0b3dSMichael Neuling 		pr_info("%s only 64bit DMA supported on CXL", __func__);
186f7f0b3dSMichael Neuling 		return -EIO;
196f7f0b3dSMichael Neuling 	}
206f7f0b3dSMichael Neuling 
216f7f0b3dSMichael Neuling 	*(pdev->dev.dma_mask) = dma_mask;
226f7f0b3dSMichael Neuling 	return 0;
236f7f0b3dSMichael Neuling }
246f7f0b3dSMichael Neuling 
256f7f0b3dSMichael Neuling static int cxl_pci_probe_mode(struct pci_bus *bus)
266f7f0b3dSMichael Neuling {
276f7f0b3dSMichael Neuling 	return PCI_PROBE_NORMAL;
286f7f0b3dSMichael Neuling }
296f7f0b3dSMichael Neuling 
306f7f0b3dSMichael Neuling static int cxl_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
316f7f0b3dSMichael Neuling {
326f7f0b3dSMichael Neuling 	return -ENODEV;
336f7f0b3dSMichael Neuling }
346f7f0b3dSMichael Neuling 
356f7f0b3dSMichael Neuling static void cxl_teardown_msi_irqs(struct pci_dev *pdev)
366f7f0b3dSMichael Neuling {
376f7f0b3dSMichael Neuling 	/*
386f7f0b3dSMichael Neuling 	 * MSI should never be set but need still need to provide this call
396f7f0b3dSMichael Neuling 	 * back.
406f7f0b3dSMichael Neuling 	 */
416f7f0b3dSMichael Neuling }
426f7f0b3dSMichael Neuling 
436f7f0b3dSMichael Neuling static bool cxl_pci_enable_device_hook(struct pci_dev *dev)
446f7f0b3dSMichael Neuling {
456f7f0b3dSMichael Neuling 	struct pci_controller *phb;
466f7f0b3dSMichael Neuling 	struct cxl_afu *afu;
47*f18a4e1dSFrederic Barrat 	struct cxl_context *ctx;
486f7f0b3dSMichael Neuling 
496f7f0b3dSMichael Neuling 	phb = pci_bus_to_host(dev->bus);
506f7f0b3dSMichael Neuling 	afu = (struct cxl_afu *)phb->private_data;
517d1647dcSAndrew Donnellan 
520d400f77SChristophe Lombard 	if (!cxl_ops->link_ok(afu->adapter, afu)) {
537d1647dcSAndrew Donnellan 		dev_warn(&dev->dev, "%s: Device link is down, refusing to enable AFU\n", __func__);
547d1647dcSAndrew Donnellan 		return false;
557d1647dcSAndrew Donnellan 	}
567d1647dcSAndrew Donnellan 
572d9d6f6cSChristoph Hellwig 	set_dma_ops(&dev->dev, &dma_nommu_ops);
586f7f0b3dSMichael Neuling 	set_dma_offset(&dev->dev, PAGE_OFFSET);
596f7f0b3dSMichael Neuling 
60*f18a4e1dSFrederic Barrat 	/*
61*f18a4e1dSFrederic Barrat 	 * Allocate a context to do cxl things too.  If we eventually do real
62*f18a4e1dSFrederic Barrat 	 * DMA ops, we'll need a default context to attach them to
63*f18a4e1dSFrederic Barrat 	 */
64*f18a4e1dSFrederic Barrat 	ctx = cxl_dev_context_init(dev);
65*f18a4e1dSFrederic Barrat 	if (IS_ERR(ctx))
66*f18a4e1dSFrederic Barrat 		return false;
67*f18a4e1dSFrederic Barrat 	dev->dev.archdata.cxl_ctx = ctx;
68*f18a4e1dSFrederic Barrat 
69*f18a4e1dSFrederic Barrat 	return (cxl_ops->afu_check_and_enable(afu) == 0);
70*f18a4e1dSFrederic Barrat }
71*f18a4e1dSFrederic Barrat 
72*f18a4e1dSFrederic Barrat static void cxl_pci_disable_device(struct pci_dev *dev)
73*f18a4e1dSFrederic Barrat {
74*f18a4e1dSFrederic Barrat 	struct cxl_context *ctx = cxl_get_context(dev);
75*f18a4e1dSFrederic Barrat 
76*f18a4e1dSFrederic Barrat 	if (ctx) {
77*f18a4e1dSFrederic Barrat 		if (ctx->status == STARTED) {
78*f18a4e1dSFrederic Barrat 			dev_err(&dev->dev, "Default context started\n");
79*f18a4e1dSFrederic Barrat 			return;
80*f18a4e1dSFrederic Barrat 		}
81*f18a4e1dSFrederic Barrat 		dev->dev.archdata.cxl_ctx = NULL;
82*f18a4e1dSFrederic Barrat 		cxl_release_context(ctx);
83*f18a4e1dSFrederic Barrat 	}
846f7f0b3dSMichael Neuling }
856f7f0b3dSMichael Neuling 
866f7f0b3dSMichael Neuling static resource_size_t cxl_pci_window_alignment(struct pci_bus *bus,
876f7f0b3dSMichael Neuling 						unsigned long type)
886f7f0b3dSMichael Neuling {
896f7f0b3dSMichael Neuling 	return 1;
906f7f0b3dSMichael Neuling }
916f7f0b3dSMichael Neuling 
926f7f0b3dSMichael Neuling static void cxl_pci_reset_secondary_bus(struct pci_dev *dev)
936f7f0b3dSMichael Neuling {
946f7f0b3dSMichael Neuling 	/* Should we do an AFU reset here ? */
956f7f0b3dSMichael Neuling }
966f7f0b3dSMichael Neuling 
976f7f0b3dSMichael Neuling static int cxl_pcie_cfg_record(u8 bus, u8 devfn)
986f7f0b3dSMichael Neuling {
996f7f0b3dSMichael Neuling 	return (bus << 8) + devfn;
1006f7f0b3dSMichael Neuling }
1016f7f0b3dSMichael Neuling 
10214a3ae34SAndrew Donnellan static inline struct cxl_afu *pci_bus_to_afu(struct pci_bus *bus)
1036f7f0b3dSMichael Neuling {
10414a3ae34SAndrew Donnellan 	struct pci_controller *phb = bus ? pci_bus_to_host(bus) : NULL;
10514a3ae34SAndrew Donnellan 
10614a3ae34SAndrew Donnellan 	return phb ? phb->private_data : NULL;
10714a3ae34SAndrew Donnellan }
10814a3ae34SAndrew Donnellan 
109171ed0fcSAndrew Donnellan static void cxl_afu_configured_put(struct cxl_afu *afu)
110171ed0fcSAndrew Donnellan {
111171ed0fcSAndrew Donnellan 	atomic_dec_if_positive(&afu->configured_state);
112171ed0fcSAndrew Donnellan }
113171ed0fcSAndrew Donnellan 
114171ed0fcSAndrew Donnellan static bool cxl_afu_configured_get(struct cxl_afu *afu)
115171ed0fcSAndrew Donnellan {
116171ed0fcSAndrew Donnellan 	return atomic_inc_unless_negative(&afu->configured_state);
117171ed0fcSAndrew Donnellan }
118171ed0fcSAndrew Donnellan 
11914a3ae34SAndrew Donnellan static inline int cxl_pcie_config_info(struct pci_bus *bus, unsigned int devfn,
12014a3ae34SAndrew Donnellan 				       struct cxl_afu *afu, int *_record)
12114a3ae34SAndrew Donnellan {
122d601ea91SFrederic Barrat 	int record;
1236f7f0b3dSMichael Neuling 
124d601ea91SFrederic Barrat 	record = cxl_pcie_cfg_record(bus->number, devfn);
125d601ea91SFrederic Barrat 	if (record > afu->crs_num)
1266f7f0b3dSMichael Neuling 		return PCIBIOS_DEVICE_NOT_FOUND;
1276f7f0b3dSMichael Neuling 
128d601ea91SFrederic Barrat 	*_record = record;
1296f7f0b3dSMichael Neuling 	return 0;
1306f7f0b3dSMichael Neuling }
1316f7f0b3dSMichael Neuling 
1326f7f0b3dSMichael Neuling static int cxl_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
1336f7f0b3dSMichael Neuling 				int offset, int len, u32 *val)
1346f7f0b3dSMichael Neuling {
135d601ea91SFrederic Barrat 	int rc, record;
136d601ea91SFrederic Barrat 	struct cxl_afu *afu;
137d601ea91SFrederic Barrat 	u8 val8;
138d601ea91SFrederic Barrat 	u16 val16;
139d601ea91SFrederic Barrat 	u32 val32;
1406f7f0b3dSMichael Neuling 
14114a3ae34SAndrew Donnellan 	afu = pci_bus_to_afu(bus);
14214a3ae34SAndrew Donnellan 	/* Grab a reader lock on afu. */
143171ed0fcSAndrew Donnellan 	if (afu == NULL || !cxl_afu_configured_get(afu))
14414a3ae34SAndrew Donnellan 		return PCIBIOS_DEVICE_NOT_FOUND;
14514a3ae34SAndrew Donnellan 
14614a3ae34SAndrew Donnellan 	rc = cxl_pcie_config_info(bus, devfn, afu, &record);
1476f7f0b3dSMichael Neuling 	if (rc)
14814a3ae34SAndrew Donnellan 		goto out;
1496f7f0b3dSMichael Neuling 
150d601ea91SFrederic Barrat 	switch (len) {
151d601ea91SFrederic Barrat 	case 1:
152d601ea91SFrederic Barrat 		rc = cxl_ops->afu_cr_read8(afu, record, offset,	&val8);
153d601ea91SFrederic Barrat 		*val = val8;
154d601ea91SFrederic Barrat 		break;
155d601ea91SFrederic Barrat 	case 2:
156d601ea91SFrederic Barrat 		rc = cxl_ops->afu_cr_read16(afu, record, offset, &val16);
157d601ea91SFrederic Barrat 		*val = val16;
158d601ea91SFrederic Barrat 		break;
159d601ea91SFrederic Barrat 	case 4:
160d601ea91SFrederic Barrat 		rc = cxl_ops->afu_cr_read32(afu, record, offset, &val32);
161d601ea91SFrederic Barrat 		*val = val32;
162d601ea91SFrederic Barrat 		break;
163d601ea91SFrederic Barrat 	default:
164d601ea91SFrederic Barrat 		WARN_ON(1);
165d601ea91SFrederic Barrat 	}
166d601ea91SFrederic Barrat 
16714a3ae34SAndrew Donnellan out:
168171ed0fcSAndrew Donnellan 	cxl_afu_configured_put(afu);
16914a3ae34SAndrew Donnellan 	return rc ? PCIBIOS_DEVICE_NOT_FOUND : PCIBIOS_SUCCESSFUL;
1706f7f0b3dSMichael Neuling }
1716f7f0b3dSMichael Neuling 
1726f7f0b3dSMichael Neuling static int cxl_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
1736f7f0b3dSMichael Neuling 				 int offset, int len, u32 val)
1746f7f0b3dSMichael Neuling {
175d601ea91SFrederic Barrat 	int rc, record;
176d601ea91SFrederic Barrat 	struct cxl_afu *afu;
1776f7f0b3dSMichael Neuling 
17814a3ae34SAndrew Donnellan 	afu = pci_bus_to_afu(bus);
17914a3ae34SAndrew Donnellan 	/* Grab a reader lock on afu. */
180171ed0fcSAndrew Donnellan 	if (afu == NULL || !cxl_afu_configured_get(afu))
18114a3ae34SAndrew Donnellan 		return PCIBIOS_DEVICE_NOT_FOUND;
18214a3ae34SAndrew Donnellan 
18314a3ae34SAndrew Donnellan 	rc = cxl_pcie_config_info(bus, devfn, afu, &record);
1846f7f0b3dSMichael Neuling 	if (rc)
18514a3ae34SAndrew Donnellan 		goto out;
1866f7f0b3dSMichael Neuling 
187d601ea91SFrederic Barrat 	switch (len) {
188d601ea91SFrederic Barrat 	case 1:
189d601ea91SFrederic Barrat 		rc = cxl_ops->afu_cr_write8(afu, record, offset, val & 0xff);
190d601ea91SFrederic Barrat 		break;
191d601ea91SFrederic Barrat 	case 2:
192d601ea91SFrederic Barrat 		rc = cxl_ops->afu_cr_write16(afu, record, offset, val & 0xffff);
193d601ea91SFrederic Barrat 		break;
194d601ea91SFrederic Barrat 	case 4:
195d601ea91SFrederic Barrat 		rc = cxl_ops->afu_cr_write32(afu, record, offset, val);
196d601ea91SFrederic Barrat 		break;
197d601ea91SFrederic Barrat 	default:
198d601ea91SFrederic Barrat 		WARN_ON(1);
199d601ea91SFrederic Barrat 	}
2000b3f9c75SDaniel Axtens 
20114a3ae34SAndrew Donnellan out:
202171ed0fcSAndrew Donnellan 	cxl_afu_configured_put(afu);
20314a3ae34SAndrew Donnellan 	return rc ? PCIBIOS_SET_FAILED : PCIBIOS_SUCCESSFUL;
2046f7f0b3dSMichael Neuling }
2056f7f0b3dSMichael Neuling 
2066f7f0b3dSMichael Neuling static struct pci_ops cxl_pcie_pci_ops =
2076f7f0b3dSMichael Neuling {
2086f7f0b3dSMichael Neuling 	.read = cxl_pcie_read_config,
2096f7f0b3dSMichael Neuling 	.write = cxl_pcie_write_config,
2106f7f0b3dSMichael Neuling };
2116f7f0b3dSMichael Neuling 
2126f7f0b3dSMichael Neuling 
2136f7f0b3dSMichael Neuling static struct pci_controller_ops cxl_pci_controller_ops =
2146f7f0b3dSMichael Neuling {
2156f7f0b3dSMichael Neuling 	.probe_mode = cxl_pci_probe_mode,
2166f7f0b3dSMichael Neuling 	.enable_device_hook = cxl_pci_enable_device_hook,
217*f18a4e1dSFrederic Barrat 	.disable_device = cxl_pci_disable_device,
218*f18a4e1dSFrederic Barrat 	.release_device = cxl_pci_disable_device,
2196f7f0b3dSMichael Neuling 	.window_alignment = cxl_pci_window_alignment,
2206f7f0b3dSMichael Neuling 	.reset_secondary_bus = cxl_pci_reset_secondary_bus,
2216f7f0b3dSMichael Neuling 	.setup_msi_irqs = cxl_setup_msi_irqs,
2226f7f0b3dSMichael Neuling 	.teardown_msi_irqs = cxl_teardown_msi_irqs,
2236f7f0b3dSMichael Neuling 	.dma_set_mask = cxl_dma_set_mask,
2246f7f0b3dSMichael Neuling };
2256f7f0b3dSMichael Neuling 
2266f7f0b3dSMichael Neuling int cxl_pci_vphb_add(struct cxl_afu *afu)
2276f7f0b3dSMichael Neuling {
228a4307390SFrederic Barrat 	struct pci_controller *phb;
229d601ea91SFrederic Barrat 	struct device_node *vphb_dn;
230d601ea91SFrederic Barrat 	struct device *parent;
2316f7f0b3dSMichael Neuling 
232e4f5fc00SIan Munsie 	/*
233e4f5fc00SIan Munsie 	 * If there are no AFU configuration records we won't have anything to
234e4f5fc00SIan Munsie 	 * expose under the vPHB, so skip creating one, returning success since
235e4f5fc00SIan Munsie 	 * this is still a valid case. This will also opt us out of EEH
236e4f5fc00SIan Munsie 	 * handling since we won't have anything special to do if there are no
237e4f5fc00SIan Munsie 	 * kernel drivers attached to the vPHB, and EEH handling is not yet
238e4f5fc00SIan Munsie 	 * supported in the peer model.
239e4f5fc00SIan Munsie 	 */
240e4f5fc00SIan Munsie 	if (!afu->crs_num)
241e4f5fc00SIan Munsie 		return 0;
242e4f5fc00SIan Munsie 
243a4307390SFrederic Barrat 	/* The parent device is the adapter. Reuse the device node of
244a4307390SFrederic Barrat 	 * the adapter.
245a4307390SFrederic Barrat 	 * We don't seem to care what device node is used for the vPHB,
246a4307390SFrederic Barrat 	 * but tools such as lsvpd walk up the device parents looking
247a4307390SFrederic Barrat 	 * for a valid location code, so we might as well show devices
248a4307390SFrederic Barrat 	 * attached to the adapter as being located on that adapter.
249a4307390SFrederic Barrat 	 */
250d601ea91SFrederic Barrat 	parent = afu->adapter->dev.parent;
251a4307390SFrederic Barrat 	vphb_dn = parent->of_node;
2526f7f0b3dSMichael Neuling 
2536f7f0b3dSMichael Neuling 	/* Alloc and setup PHB data structure */
254d601ea91SFrederic Barrat 	phb = pcibios_alloc_controller(vphb_dn);
2556f7f0b3dSMichael Neuling 	if (!phb)
2566f7f0b3dSMichael Neuling 		return -ENODEV;
2576f7f0b3dSMichael Neuling 
2586f7f0b3dSMichael Neuling 	/* Setup parent in sysfs */
259d601ea91SFrederic Barrat 	phb->parent = parent;
2606f7f0b3dSMichael Neuling 
2616f7f0b3dSMichael Neuling 	/* Setup the PHB using arch provided callback */
2626f7f0b3dSMichael Neuling 	phb->ops = &cxl_pcie_pci_ops;
263d601ea91SFrederic Barrat 	phb->cfg_addr = NULL;
2646fd40f19SAndrew Donnellan 	phb->cfg_data = NULL;
2656f7f0b3dSMichael Neuling 	phb->private_data = afu;
2666f7f0b3dSMichael Neuling 	phb->controller_ops = cxl_pci_controller_ops;
2676f7f0b3dSMichael Neuling 
2686f7f0b3dSMichael Neuling 	/* Scan the bus */
2696f7f0b3dSMichael Neuling 	pcibios_scan_phb(phb);
2706f7f0b3dSMichael Neuling 	if (phb->bus == NULL)
2716f7f0b3dSMichael Neuling 		return -ENXIO;
2726f7f0b3dSMichael Neuling 
2736f38a8b9SAndrew Donnellan 	/* Set release hook on root bus */
2746f38a8b9SAndrew Donnellan 	pci_set_host_bridge_release(to_pci_host_bridge(phb->bus->bridge),
2756f38a8b9SAndrew Donnellan 				    pcibios_free_controller_deferred,
2766f38a8b9SAndrew Donnellan 				    (void *) phb);
2776f38a8b9SAndrew Donnellan 
2786f7f0b3dSMichael Neuling 	/* Claim resources. This might need some rework as well depending
2796f7f0b3dSMichael Neuling 	 * whether we are doing probe-only or not, like assigning unassigned
2806f7f0b3dSMichael Neuling 	 * resources etc...
2816f7f0b3dSMichael Neuling 	 */
2826f7f0b3dSMichael Neuling 	pcibios_claim_one_bus(phb->bus);
2836f7f0b3dSMichael Neuling 
2846f7f0b3dSMichael Neuling 	/* Add probed PCI devices to the device model */
2856f7f0b3dSMichael Neuling 	pci_bus_add_devices(phb->bus);
2866f7f0b3dSMichael Neuling 
2876f7f0b3dSMichael Neuling 	afu->phb = phb;
2886f7f0b3dSMichael Neuling 
2896f7f0b3dSMichael Neuling 	return 0;
2906f7f0b3dSMichael Neuling }
2916f7f0b3dSMichael Neuling 
2926f7f0b3dSMichael Neuling void cxl_pci_vphb_remove(struct cxl_afu *afu)
2936f7f0b3dSMichael Neuling {
2946f7f0b3dSMichael Neuling 	struct pci_controller *phb;
2956f7f0b3dSMichael Neuling 
2966f7f0b3dSMichael Neuling 	/* If there is no configuration record we won't have one of these */
2976f7f0b3dSMichael Neuling 	if (!afu || !afu->phb)
2986f7f0b3dSMichael Neuling 		return;
2996f7f0b3dSMichael Neuling 
3006f7f0b3dSMichael Neuling 	phb = afu->phb;
3012e1a2556SAndrew Donnellan 	afu->phb = NULL;
3026f7f0b3dSMichael Neuling 
3036f7f0b3dSMichael Neuling 	pci_remove_root_bus(phb->bus);
3046f38a8b9SAndrew Donnellan 	/*
3056f38a8b9SAndrew Donnellan 	 * We don't free phb here - that's handled by
3066f38a8b9SAndrew Donnellan 	 * pcibios_free_controller_deferred()
3076f38a8b9SAndrew Donnellan 	 */
3086f7f0b3dSMichael Neuling }
3096f7f0b3dSMichael Neuling 
31017eb3eefSVaibhav Jain bool cxl_pci_is_vphb_device(struct pci_dev *dev)
31117eb3eefSVaibhav Jain {
31217eb3eefSVaibhav Jain 	struct pci_controller *phb;
31317eb3eefSVaibhav Jain 
31417eb3eefSVaibhav Jain 	phb = pci_bus_to_host(dev->bus);
31517eb3eefSVaibhav Jain 
316c8d43cf0SAlastair D'Silva 	return (phb->ops == &cxl_pcie_pci_ops);
31717eb3eefSVaibhav Jain }
31817eb3eefSVaibhav Jain 
3196f7f0b3dSMichael Neuling struct cxl_afu *cxl_pci_to_afu(struct pci_dev *dev)
3206f7f0b3dSMichael Neuling {
3216f7f0b3dSMichael Neuling 	struct pci_controller *phb;
3226f7f0b3dSMichael Neuling 
3236f7f0b3dSMichael Neuling 	phb = pci_bus_to_host(dev->bus);
3246f7f0b3dSMichael Neuling 
3256f7f0b3dSMichael Neuling 	return (struct cxl_afu *)phb->private_data;
3266f7f0b3dSMichael Neuling }
3276f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_pci_to_afu);
3286f7f0b3dSMichael Neuling 
3296f7f0b3dSMichael Neuling unsigned int cxl_pci_to_cfg_record(struct pci_dev *dev)
3306f7f0b3dSMichael Neuling {
3316f7f0b3dSMichael Neuling 	return cxl_pcie_cfg_record(dev->bus->number, dev->devfn);
3326f7f0b3dSMichael Neuling }
3336f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_pci_to_cfg_record);
334