xref: /openbmc/linux/drivers/misc/cxl/vphb.c (revision 2d9d6f6c9e8ae9490a8df8727f7e00310a5efb5f)
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>
12317f5ef1SIan Munsie #include <asm/pnv-pci.h>
136f7f0b3dSMichael Neuling #include "cxl.h"
146f7f0b3dSMichael Neuling 
156f7f0b3dSMichael Neuling static int cxl_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
166f7f0b3dSMichael Neuling {
176f7f0b3dSMichael Neuling 	if (dma_mask < DMA_BIT_MASK(64)) {
186f7f0b3dSMichael Neuling 		pr_info("%s only 64bit DMA supported on CXL", __func__);
196f7f0b3dSMichael Neuling 		return -EIO;
206f7f0b3dSMichael Neuling 	}
216f7f0b3dSMichael Neuling 
226f7f0b3dSMichael Neuling 	*(pdev->dev.dma_mask) = dma_mask;
236f7f0b3dSMichael Neuling 	return 0;
246f7f0b3dSMichael Neuling }
256f7f0b3dSMichael Neuling 
266f7f0b3dSMichael Neuling static int cxl_pci_probe_mode(struct pci_bus *bus)
276f7f0b3dSMichael Neuling {
286f7f0b3dSMichael Neuling 	return PCI_PROBE_NORMAL;
296f7f0b3dSMichael Neuling }
306f7f0b3dSMichael Neuling 
316f7f0b3dSMichael Neuling static int cxl_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
326f7f0b3dSMichael Neuling {
336f7f0b3dSMichael Neuling 	return -ENODEV;
346f7f0b3dSMichael Neuling }
356f7f0b3dSMichael Neuling 
366f7f0b3dSMichael Neuling static void cxl_teardown_msi_irqs(struct pci_dev *pdev)
376f7f0b3dSMichael Neuling {
386f7f0b3dSMichael Neuling 	/*
396f7f0b3dSMichael Neuling 	 * MSI should never be set but need still need to provide this call
406f7f0b3dSMichael Neuling 	 * back.
416f7f0b3dSMichael Neuling 	 */
426f7f0b3dSMichael Neuling }
436f7f0b3dSMichael Neuling 
446f7f0b3dSMichael Neuling static bool cxl_pci_enable_device_hook(struct pci_dev *dev)
456f7f0b3dSMichael Neuling {
466f7f0b3dSMichael Neuling 	struct pci_controller *phb;
476f7f0b3dSMichael Neuling 	struct cxl_afu *afu;
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 
57*2d9d6f6cSChristoph Hellwig 	set_dma_ops(&dev->dev, &dma_nommu_ops);
586f7f0b3dSMichael Neuling 	set_dma_offset(&dev->dev, PAGE_OFFSET);
596f7f0b3dSMichael Neuling 
60a19bd79eSIan Munsie 	return _cxl_pci_associate_default_context(dev, afu);
616f7f0b3dSMichael Neuling }
626f7f0b3dSMichael Neuling 
636f7f0b3dSMichael Neuling static resource_size_t cxl_pci_window_alignment(struct pci_bus *bus,
646f7f0b3dSMichael Neuling 						unsigned long type)
656f7f0b3dSMichael Neuling {
666f7f0b3dSMichael Neuling 	return 1;
676f7f0b3dSMichael Neuling }
686f7f0b3dSMichael Neuling 
696f7f0b3dSMichael Neuling static void cxl_pci_reset_secondary_bus(struct pci_dev *dev)
706f7f0b3dSMichael Neuling {
716f7f0b3dSMichael Neuling 	/* Should we do an AFU reset here ? */
726f7f0b3dSMichael Neuling }
736f7f0b3dSMichael Neuling 
746f7f0b3dSMichael Neuling static int cxl_pcie_cfg_record(u8 bus, u8 devfn)
756f7f0b3dSMichael Neuling {
766f7f0b3dSMichael Neuling 	return (bus << 8) + devfn;
776f7f0b3dSMichael Neuling }
786f7f0b3dSMichael Neuling 
7914a3ae34SAndrew Donnellan static inline struct cxl_afu *pci_bus_to_afu(struct pci_bus *bus)
806f7f0b3dSMichael Neuling {
8114a3ae34SAndrew Donnellan 	struct pci_controller *phb = bus ? pci_bus_to_host(bus) : NULL;
8214a3ae34SAndrew Donnellan 
8314a3ae34SAndrew Donnellan 	return phb ? phb->private_data : NULL;
8414a3ae34SAndrew Donnellan }
8514a3ae34SAndrew Donnellan 
86171ed0fcSAndrew Donnellan static void cxl_afu_configured_put(struct cxl_afu *afu)
87171ed0fcSAndrew Donnellan {
88171ed0fcSAndrew Donnellan 	atomic_dec_if_positive(&afu->configured_state);
89171ed0fcSAndrew Donnellan }
90171ed0fcSAndrew Donnellan 
91171ed0fcSAndrew Donnellan static bool cxl_afu_configured_get(struct cxl_afu *afu)
92171ed0fcSAndrew Donnellan {
93171ed0fcSAndrew Donnellan 	return atomic_inc_unless_negative(&afu->configured_state);
94171ed0fcSAndrew Donnellan }
95171ed0fcSAndrew Donnellan 
9614a3ae34SAndrew Donnellan static inline int cxl_pcie_config_info(struct pci_bus *bus, unsigned int devfn,
9714a3ae34SAndrew Donnellan 				       struct cxl_afu *afu, int *_record)
9814a3ae34SAndrew Donnellan {
99d601ea91SFrederic Barrat 	int record;
1006f7f0b3dSMichael Neuling 
101d601ea91SFrederic Barrat 	record = cxl_pcie_cfg_record(bus->number, devfn);
102d601ea91SFrederic Barrat 	if (record > afu->crs_num)
1036f7f0b3dSMichael Neuling 		return PCIBIOS_DEVICE_NOT_FOUND;
1046f7f0b3dSMichael Neuling 
105d601ea91SFrederic Barrat 	*_record = record;
1066f7f0b3dSMichael Neuling 	return 0;
1076f7f0b3dSMichael Neuling }
1086f7f0b3dSMichael Neuling 
1096f7f0b3dSMichael Neuling static int cxl_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
1106f7f0b3dSMichael Neuling 				int offset, int len, u32 *val)
1116f7f0b3dSMichael Neuling {
112d601ea91SFrederic Barrat 	int rc, record;
113d601ea91SFrederic Barrat 	struct cxl_afu *afu;
114d601ea91SFrederic Barrat 	u8 val8;
115d601ea91SFrederic Barrat 	u16 val16;
116d601ea91SFrederic Barrat 	u32 val32;
1176f7f0b3dSMichael Neuling 
11814a3ae34SAndrew Donnellan 	afu = pci_bus_to_afu(bus);
11914a3ae34SAndrew Donnellan 	/* Grab a reader lock on afu. */
120171ed0fcSAndrew Donnellan 	if (afu == NULL || !cxl_afu_configured_get(afu))
12114a3ae34SAndrew Donnellan 		return PCIBIOS_DEVICE_NOT_FOUND;
12214a3ae34SAndrew Donnellan 
12314a3ae34SAndrew Donnellan 	rc = cxl_pcie_config_info(bus, devfn, afu, &record);
1246f7f0b3dSMichael Neuling 	if (rc)
12514a3ae34SAndrew Donnellan 		goto out;
1266f7f0b3dSMichael Neuling 
127d601ea91SFrederic Barrat 	switch (len) {
128d601ea91SFrederic Barrat 	case 1:
129d601ea91SFrederic Barrat 		rc = cxl_ops->afu_cr_read8(afu, record, offset,	&val8);
130d601ea91SFrederic Barrat 		*val = val8;
131d601ea91SFrederic Barrat 		break;
132d601ea91SFrederic Barrat 	case 2:
133d601ea91SFrederic Barrat 		rc = cxl_ops->afu_cr_read16(afu, record, offset, &val16);
134d601ea91SFrederic Barrat 		*val = val16;
135d601ea91SFrederic Barrat 		break;
136d601ea91SFrederic Barrat 	case 4:
137d601ea91SFrederic Barrat 		rc = cxl_ops->afu_cr_read32(afu, record, offset, &val32);
138d601ea91SFrederic Barrat 		*val = val32;
139d601ea91SFrederic Barrat 		break;
140d601ea91SFrederic Barrat 	default:
141d601ea91SFrederic Barrat 		WARN_ON(1);
142d601ea91SFrederic Barrat 	}
143d601ea91SFrederic Barrat 
14414a3ae34SAndrew Donnellan out:
145171ed0fcSAndrew Donnellan 	cxl_afu_configured_put(afu);
14614a3ae34SAndrew Donnellan 	return rc ? PCIBIOS_DEVICE_NOT_FOUND : PCIBIOS_SUCCESSFUL;
1476f7f0b3dSMichael Neuling }
1486f7f0b3dSMichael Neuling 
1496f7f0b3dSMichael Neuling static int cxl_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
1506f7f0b3dSMichael Neuling 				 int offset, int len, u32 val)
1516f7f0b3dSMichael Neuling {
152d601ea91SFrederic Barrat 	int rc, record;
153d601ea91SFrederic Barrat 	struct cxl_afu *afu;
1546f7f0b3dSMichael Neuling 
15514a3ae34SAndrew Donnellan 	afu = pci_bus_to_afu(bus);
15614a3ae34SAndrew Donnellan 	/* Grab a reader lock on afu. */
157171ed0fcSAndrew Donnellan 	if (afu == NULL || !cxl_afu_configured_get(afu))
15814a3ae34SAndrew Donnellan 		return PCIBIOS_DEVICE_NOT_FOUND;
15914a3ae34SAndrew Donnellan 
16014a3ae34SAndrew Donnellan 	rc = cxl_pcie_config_info(bus, devfn, afu, &record);
1616f7f0b3dSMichael Neuling 	if (rc)
16214a3ae34SAndrew Donnellan 		goto out;
1636f7f0b3dSMichael Neuling 
164d601ea91SFrederic Barrat 	switch (len) {
165d601ea91SFrederic Barrat 	case 1:
166d601ea91SFrederic Barrat 		rc = cxl_ops->afu_cr_write8(afu, record, offset, val & 0xff);
167d601ea91SFrederic Barrat 		break;
168d601ea91SFrederic Barrat 	case 2:
169d601ea91SFrederic Barrat 		rc = cxl_ops->afu_cr_write16(afu, record, offset, val & 0xffff);
170d601ea91SFrederic Barrat 		break;
171d601ea91SFrederic Barrat 	case 4:
172d601ea91SFrederic Barrat 		rc = cxl_ops->afu_cr_write32(afu, record, offset, val);
173d601ea91SFrederic Barrat 		break;
174d601ea91SFrederic Barrat 	default:
175d601ea91SFrederic Barrat 		WARN_ON(1);
176d601ea91SFrederic Barrat 	}
1770b3f9c75SDaniel Axtens 
17814a3ae34SAndrew Donnellan out:
179171ed0fcSAndrew Donnellan 	cxl_afu_configured_put(afu);
18014a3ae34SAndrew Donnellan 	return rc ? PCIBIOS_SET_FAILED : PCIBIOS_SUCCESSFUL;
1816f7f0b3dSMichael Neuling }
1826f7f0b3dSMichael Neuling 
1836f7f0b3dSMichael Neuling static struct pci_ops cxl_pcie_pci_ops =
1846f7f0b3dSMichael Neuling {
1856f7f0b3dSMichael Neuling 	.read = cxl_pcie_read_config,
1866f7f0b3dSMichael Neuling 	.write = cxl_pcie_write_config,
1876f7f0b3dSMichael Neuling };
1886f7f0b3dSMichael Neuling 
1896f7f0b3dSMichael Neuling 
1906f7f0b3dSMichael Neuling static struct pci_controller_ops cxl_pci_controller_ops =
1916f7f0b3dSMichael Neuling {
1926f7f0b3dSMichael Neuling 	.probe_mode = cxl_pci_probe_mode,
1936f7f0b3dSMichael Neuling 	.enable_device_hook = cxl_pci_enable_device_hook,
194a19bd79eSIan Munsie 	.disable_device = _cxl_pci_disable_device,
195a19bd79eSIan Munsie 	.release_device = _cxl_pci_disable_device,
1966f7f0b3dSMichael Neuling 	.window_alignment = cxl_pci_window_alignment,
1976f7f0b3dSMichael Neuling 	.reset_secondary_bus = cxl_pci_reset_secondary_bus,
1986f7f0b3dSMichael Neuling 	.setup_msi_irqs = cxl_setup_msi_irqs,
1996f7f0b3dSMichael Neuling 	.teardown_msi_irqs = cxl_teardown_msi_irqs,
2006f7f0b3dSMichael Neuling 	.dma_set_mask = cxl_dma_set_mask,
2016f7f0b3dSMichael Neuling };
2026f7f0b3dSMichael Neuling 
2036f7f0b3dSMichael Neuling int cxl_pci_vphb_add(struct cxl_afu *afu)
2046f7f0b3dSMichael Neuling {
205a4307390SFrederic Barrat 	struct pci_controller *phb;
206d601ea91SFrederic Barrat 	struct device_node *vphb_dn;
207d601ea91SFrederic Barrat 	struct device *parent;
2086f7f0b3dSMichael Neuling 
209e4f5fc00SIan Munsie 	/*
210e4f5fc00SIan Munsie 	 * If there are no AFU configuration records we won't have anything to
211e4f5fc00SIan Munsie 	 * expose under the vPHB, so skip creating one, returning success since
212e4f5fc00SIan Munsie 	 * this is still a valid case. This will also opt us out of EEH
213e4f5fc00SIan Munsie 	 * handling since we won't have anything special to do if there are no
214e4f5fc00SIan Munsie 	 * kernel drivers attached to the vPHB, and EEH handling is not yet
215e4f5fc00SIan Munsie 	 * supported in the peer model.
216e4f5fc00SIan Munsie 	 */
217e4f5fc00SIan Munsie 	if (!afu->crs_num)
218e4f5fc00SIan Munsie 		return 0;
219e4f5fc00SIan Munsie 
220a4307390SFrederic Barrat 	/* The parent device is the adapter. Reuse the device node of
221a4307390SFrederic Barrat 	 * the adapter.
222a4307390SFrederic Barrat 	 * We don't seem to care what device node is used for the vPHB,
223a4307390SFrederic Barrat 	 * but tools such as lsvpd walk up the device parents looking
224a4307390SFrederic Barrat 	 * for a valid location code, so we might as well show devices
225a4307390SFrederic Barrat 	 * attached to the adapter as being located on that adapter.
226a4307390SFrederic Barrat 	 */
227d601ea91SFrederic Barrat 	parent = afu->adapter->dev.parent;
228a4307390SFrederic Barrat 	vphb_dn = parent->of_node;
2296f7f0b3dSMichael Neuling 
2306f7f0b3dSMichael Neuling 	/* Alloc and setup PHB data structure */
231d601ea91SFrederic Barrat 	phb = pcibios_alloc_controller(vphb_dn);
2326f7f0b3dSMichael Neuling 	if (!phb)
2336f7f0b3dSMichael Neuling 		return -ENODEV;
2346f7f0b3dSMichael Neuling 
2356f7f0b3dSMichael Neuling 	/* Setup parent in sysfs */
236d601ea91SFrederic Barrat 	phb->parent = parent;
2376f7f0b3dSMichael Neuling 
2386f7f0b3dSMichael Neuling 	/* Setup the PHB using arch provided callback */
2396f7f0b3dSMichael Neuling 	phb->ops = &cxl_pcie_pci_ops;
240d601ea91SFrederic Barrat 	phb->cfg_addr = NULL;
2416fd40f19SAndrew Donnellan 	phb->cfg_data = NULL;
2426f7f0b3dSMichael Neuling 	phb->private_data = afu;
2436f7f0b3dSMichael Neuling 	phb->controller_ops = cxl_pci_controller_ops;
2446f7f0b3dSMichael Neuling 
2456f7f0b3dSMichael Neuling 	/* Scan the bus */
2466f7f0b3dSMichael Neuling 	pcibios_scan_phb(phb);
2476f7f0b3dSMichael Neuling 	if (phb->bus == NULL)
2486f7f0b3dSMichael Neuling 		return -ENXIO;
2496f7f0b3dSMichael Neuling 
2506f38a8b9SAndrew Donnellan 	/* Set release hook on root bus */
2516f38a8b9SAndrew Donnellan 	pci_set_host_bridge_release(to_pci_host_bridge(phb->bus->bridge),
2526f38a8b9SAndrew Donnellan 				    pcibios_free_controller_deferred,
2536f38a8b9SAndrew Donnellan 				    (void *) phb);
2546f38a8b9SAndrew Donnellan 
2556f7f0b3dSMichael Neuling 	/* Claim resources. This might need some rework as well depending
2566f7f0b3dSMichael Neuling 	 * whether we are doing probe-only or not, like assigning unassigned
2576f7f0b3dSMichael Neuling 	 * resources etc...
2586f7f0b3dSMichael Neuling 	 */
2596f7f0b3dSMichael Neuling 	pcibios_claim_one_bus(phb->bus);
2606f7f0b3dSMichael Neuling 
2616f7f0b3dSMichael Neuling 	/* Add probed PCI devices to the device model */
2626f7f0b3dSMichael Neuling 	pci_bus_add_devices(phb->bus);
2636f7f0b3dSMichael Neuling 
2646f7f0b3dSMichael Neuling 	afu->phb = phb;
2656f7f0b3dSMichael Neuling 
2666f7f0b3dSMichael Neuling 	return 0;
2676f7f0b3dSMichael Neuling }
2686f7f0b3dSMichael Neuling 
2696f7f0b3dSMichael Neuling void cxl_pci_vphb_remove(struct cxl_afu *afu)
2706f7f0b3dSMichael Neuling {
2716f7f0b3dSMichael Neuling 	struct pci_controller *phb;
2726f7f0b3dSMichael Neuling 
2736f7f0b3dSMichael Neuling 	/* If there is no configuration record we won't have one of these */
2746f7f0b3dSMichael Neuling 	if (!afu || !afu->phb)
2756f7f0b3dSMichael Neuling 		return;
2766f7f0b3dSMichael Neuling 
2776f7f0b3dSMichael Neuling 	phb = afu->phb;
2782e1a2556SAndrew Donnellan 	afu->phb = NULL;
2796f7f0b3dSMichael Neuling 
2806f7f0b3dSMichael Neuling 	pci_remove_root_bus(phb->bus);
2816f38a8b9SAndrew Donnellan 	/*
2826f38a8b9SAndrew Donnellan 	 * We don't free phb here - that's handled by
2836f38a8b9SAndrew Donnellan 	 * pcibios_free_controller_deferred()
2846f38a8b9SAndrew Donnellan 	 */
2856f7f0b3dSMichael Neuling }
2866f7f0b3dSMichael Neuling 
287317f5ef1SIan Munsie static bool _cxl_pci_is_vphb_device(struct pci_controller *phb)
288317f5ef1SIan Munsie {
289317f5ef1SIan Munsie 	return (phb->ops == &cxl_pcie_pci_ops);
290317f5ef1SIan Munsie }
291317f5ef1SIan Munsie 
29217eb3eefSVaibhav Jain bool cxl_pci_is_vphb_device(struct pci_dev *dev)
29317eb3eefSVaibhav Jain {
29417eb3eefSVaibhav Jain 	struct pci_controller *phb;
29517eb3eefSVaibhav Jain 
29617eb3eefSVaibhav Jain 	phb = pci_bus_to_host(dev->bus);
29717eb3eefSVaibhav Jain 
298317f5ef1SIan Munsie 	return _cxl_pci_is_vphb_device(phb);
29917eb3eefSVaibhav Jain }
30017eb3eefSVaibhav Jain 
3016f7f0b3dSMichael Neuling struct cxl_afu *cxl_pci_to_afu(struct pci_dev *dev)
3026f7f0b3dSMichael Neuling {
3036f7f0b3dSMichael Neuling 	struct pci_controller *phb;
3046f7f0b3dSMichael Neuling 
3056f7f0b3dSMichael Neuling 	phb = pci_bus_to_host(dev->bus);
3066f7f0b3dSMichael Neuling 
307317f5ef1SIan Munsie 	if (_cxl_pci_is_vphb_device(phb))
3086f7f0b3dSMichael Neuling 		return (struct cxl_afu *)phb->private_data;
309317f5ef1SIan Munsie 
310317f5ef1SIan Munsie 	if (pnv_pci_on_cxl_phb(dev))
311317f5ef1SIan Munsie 		return pnv_cxl_phb_to_afu(phb);
312317f5ef1SIan Munsie 
313317f5ef1SIan Munsie 	return ERR_PTR(-ENODEV);
3146f7f0b3dSMichael Neuling }
3156f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_pci_to_afu);
3166f7f0b3dSMichael Neuling 
3176f7f0b3dSMichael Neuling unsigned int cxl_pci_to_cfg_record(struct pci_dev *dev)
3186f7f0b3dSMichael Neuling {
3196f7f0b3dSMichael Neuling 	return cxl_pcie_cfg_record(dev->bus->number, dev->devfn);
3206f7f0b3dSMichael Neuling }
3216f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_pci_to_cfg_record);
322