xref: /openbmc/linux/drivers/misc/cxl/vphb.c (revision 0d400f77c19e8d2606f8194846bcf18ebdc9df2a)
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;
476f7f0b3dSMichael Neuling 	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 
52*0d400f77SChristophe 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 
576f7f0b3dSMichael Neuling 	set_dma_ops(&dev->dev, &dma_direct_ops);
586f7f0b3dSMichael Neuling 	set_dma_offset(&dev->dev, PAGE_OFFSET);
596f7f0b3dSMichael Neuling 
606f7f0b3dSMichael Neuling 	/*
616f7f0b3dSMichael Neuling 	 * Allocate a context to do cxl things too.  If we eventually do real
626f7f0b3dSMichael Neuling 	 * DMA ops, we'll need a default context to attach them to
636f7f0b3dSMichael Neuling 	 */
646f7f0b3dSMichael Neuling 	ctx = cxl_dev_context_init(dev);
656f7f0b3dSMichael Neuling 	if (!ctx)
666f7f0b3dSMichael Neuling 		return false;
676f7f0b3dSMichael Neuling 	dev->dev.archdata.cxl_ctx = ctx;
686f7f0b3dSMichael Neuling 
695be587b1SFrederic Barrat 	return (cxl_ops->afu_check_and_enable(afu) == 0);
706f7f0b3dSMichael Neuling }
716f7f0b3dSMichael Neuling 
726f7f0b3dSMichael Neuling static void cxl_pci_disable_device(struct pci_dev *dev)
736f7f0b3dSMichael Neuling {
746f7f0b3dSMichael Neuling 	struct cxl_context *ctx = cxl_get_context(dev);
756f7f0b3dSMichael Neuling 
766f7f0b3dSMichael Neuling 	if (ctx) {
776f7f0b3dSMichael Neuling 		if (ctx->status == STARTED) {
786f7f0b3dSMichael Neuling 			dev_err(&dev->dev, "Default context started\n");
796f7f0b3dSMichael Neuling 			return;
806f7f0b3dSMichael Neuling 		}
81f67b4938SMichael Neuling 		dev->dev.archdata.cxl_ctx = NULL;
826f7f0b3dSMichael Neuling 		cxl_release_context(ctx);
836f7f0b3dSMichael Neuling 	}
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 
1026f7f0b3dSMichael Neuling static int cxl_pcie_config_info(struct pci_bus *bus, unsigned int devfn,
103d601ea91SFrederic Barrat 				struct cxl_afu **_afu, int *_record)
1046f7f0b3dSMichael Neuling {
1056f7f0b3dSMichael Neuling 	struct pci_controller *phb;
1066f7f0b3dSMichael Neuling 	struct cxl_afu *afu;
107d601ea91SFrederic Barrat 	int record;
1086f7f0b3dSMichael Neuling 
1096f7f0b3dSMichael Neuling 	phb = pci_bus_to_host(bus);
1106f7f0b3dSMichael Neuling 	if (phb == NULL)
1116f7f0b3dSMichael Neuling 		return PCIBIOS_DEVICE_NOT_FOUND;
112d601ea91SFrederic Barrat 
11314f21189SManinder Singh 	afu = (struct cxl_afu *)phb->private_data;
114d601ea91SFrederic Barrat 	record = cxl_pcie_cfg_record(bus->number, devfn);
115d601ea91SFrederic Barrat 	if (record > afu->crs_num)
1166f7f0b3dSMichael Neuling 		return PCIBIOS_DEVICE_NOT_FOUND;
1176f7f0b3dSMichael Neuling 
118d601ea91SFrederic Barrat 	*_afu = afu;
119d601ea91SFrederic Barrat 	*_record = record;
1206f7f0b3dSMichael Neuling 	return 0;
1216f7f0b3dSMichael Neuling }
1226f7f0b3dSMichael Neuling 
1236f7f0b3dSMichael Neuling static int cxl_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
1246f7f0b3dSMichael Neuling 				int offset, int len, u32 *val)
1256f7f0b3dSMichael Neuling {
126d601ea91SFrederic Barrat 	int rc, record;
127d601ea91SFrederic Barrat 	struct cxl_afu *afu;
128d601ea91SFrederic Barrat 	u8 val8;
129d601ea91SFrederic Barrat 	u16 val16;
130d601ea91SFrederic Barrat 	u32 val32;
1316f7f0b3dSMichael Neuling 
132d601ea91SFrederic Barrat 	rc = cxl_pcie_config_info(bus, devfn, &afu, &record);
1336f7f0b3dSMichael Neuling 	if (rc)
1346f7f0b3dSMichael Neuling 		return rc;
1356f7f0b3dSMichael Neuling 
136d601ea91SFrederic Barrat 	switch (len) {
137d601ea91SFrederic Barrat 	case 1:
138d601ea91SFrederic Barrat 		rc = cxl_ops->afu_cr_read8(afu, record, offset,	&val8);
139d601ea91SFrederic Barrat 		*val = val8;
140d601ea91SFrederic Barrat 		break;
141d601ea91SFrederic Barrat 	case 2:
142d601ea91SFrederic Barrat 		rc = cxl_ops->afu_cr_read16(afu, record, offset, &val16);
143d601ea91SFrederic Barrat 		*val = val16;
144d601ea91SFrederic Barrat 		break;
145d601ea91SFrederic Barrat 	case 4:
146d601ea91SFrederic Barrat 		rc = cxl_ops->afu_cr_read32(afu, record, offset, &val32);
147d601ea91SFrederic Barrat 		*val = val32;
148d601ea91SFrederic Barrat 		break;
149d601ea91SFrederic Barrat 	default:
150d601ea91SFrederic Barrat 		WARN_ON(1);
151d601ea91SFrederic Barrat 	}
152d601ea91SFrederic Barrat 
153d601ea91SFrederic Barrat 	if (rc)
1540b3f9c75SDaniel Axtens 		return PCIBIOS_DEVICE_NOT_FOUND;
1550b3f9c75SDaniel Axtens 
1566f7f0b3dSMichael Neuling 	return PCIBIOS_SUCCESSFUL;
1576f7f0b3dSMichael Neuling }
1586f7f0b3dSMichael Neuling 
1596f7f0b3dSMichael Neuling static int cxl_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
1606f7f0b3dSMichael Neuling 				 int offset, int len, u32 val)
1616f7f0b3dSMichael Neuling {
162d601ea91SFrederic Barrat 	int rc, record;
163d601ea91SFrederic Barrat 	struct cxl_afu *afu;
1646f7f0b3dSMichael Neuling 
165d601ea91SFrederic Barrat 	rc = cxl_pcie_config_info(bus, devfn, &afu, &record);
1666f7f0b3dSMichael Neuling 	if (rc)
1676f7f0b3dSMichael Neuling 		return rc;
1686f7f0b3dSMichael Neuling 
169d601ea91SFrederic Barrat 	switch (len) {
170d601ea91SFrederic Barrat 	case 1:
171d601ea91SFrederic Barrat 		rc = cxl_ops->afu_cr_write8(afu, record, offset, val & 0xff);
172d601ea91SFrederic Barrat 		break;
173d601ea91SFrederic Barrat 	case 2:
174d601ea91SFrederic Barrat 		rc = cxl_ops->afu_cr_write16(afu, record, offset, val & 0xffff);
175d601ea91SFrederic Barrat 		break;
176d601ea91SFrederic Barrat 	case 4:
177d601ea91SFrederic Barrat 		rc = cxl_ops->afu_cr_write32(afu, record, offset, val);
178d601ea91SFrederic Barrat 		break;
179d601ea91SFrederic Barrat 	default:
180d601ea91SFrederic Barrat 		WARN_ON(1);
181d601ea91SFrederic Barrat 	}
1820b3f9c75SDaniel Axtens 
183d601ea91SFrederic Barrat 	if (rc)
184d601ea91SFrederic Barrat 		return PCIBIOS_SET_FAILED;
1856f7f0b3dSMichael Neuling 
1866f7f0b3dSMichael Neuling 	return PCIBIOS_SUCCESSFUL;
1876f7f0b3dSMichael Neuling }
1886f7f0b3dSMichael Neuling 
1896f7f0b3dSMichael Neuling static struct pci_ops cxl_pcie_pci_ops =
1906f7f0b3dSMichael Neuling {
1916f7f0b3dSMichael Neuling 	.read = cxl_pcie_read_config,
1926f7f0b3dSMichael Neuling 	.write = cxl_pcie_write_config,
1936f7f0b3dSMichael Neuling };
1946f7f0b3dSMichael Neuling 
1956f7f0b3dSMichael Neuling 
1966f7f0b3dSMichael Neuling static struct pci_controller_ops cxl_pci_controller_ops =
1976f7f0b3dSMichael Neuling {
1986f7f0b3dSMichael Neuling 	.probe_mode = cxl_pci_probe_mode,
1996f7f0b3dSMichael Neuling 	.enable_device_hook = cxl_pci_enable_device_hook,
2006f7f0b3dSMichael Neuling 	.disable_device = cxl_pci_disable_device,
2016f7f0b3dSMichael Neuling 	.release_device = cxl_pci_disable_device,
2026f7f0b3dSMichael Neuling 	.window_alignment = cxl_pci_window_alignment,
2036f7f0b3dSMichael Neuling 	.reset_secondary_bus = cxl_pci_reset_secondary_bus,
2046f7f0b3dSMichael Neuling 	.setup_msi_irqs = cxl_setup_msi_irqs,
2056f7f0b3dSMichael Neuling 	.teardown_msi_irqs = cxl_teardown_msi_irqs,
2066f7f0b3dSMichael Neuling 	.dma_set_mask = cxl_dma_set_mask,
2076f7f0b3dSMichael Neuling };
2086f7f0b3dSMichael Neuling 
2096f7f0b3dSMichael Neuling int cxl_pci_vphb_add(struct cxl_afu *afu)
2106f7f0b3dSMichael Neuling {
2116f7f0b3dSMichael Neuling 	struct pci_dev *phys_dev;
2126f7f0b3dSMichael Neuling 	struct pci_controller *phb, *phys_phb;
213d601ea91SFrederic Barrat 	struct device_node *vphb_dn;
214d601ea91SFrederic Barrat 	struct device *parent;
2156f7f0b3dSMichael Neuling 
216d601ea91SFrederic Barrat 	if (cpu_has_feature(CPU_FTR_HVMODE)) {
2176f7f0b3dSMichael Neuling 		phys_dev = to_pci_dev(afu->adapter->dev.parent);
2186f7f0b3dSMichael Neuling 		phys_phb = pci_bus_to_host(phys_dev->bus);
219d601ea91SFrederic Barrat 		vphb_dn = phys_phb->dn;
220d601ea91SFrederic Barrat 		parent = &phys_dev->dev;
221d601ea91SFrederic Barrat 	} else {
222d601ea91SFrederic Barrat 		vphb_dn = afu->adapter->dev.parent->of_node;
223d601ea91SFrederic Barrat 		parent = afu->adapter->dev.parent;
224d601ea91SFrederic Barrat 	}
2256f7f0b3dSMichael Neuling 
2266f7f0b3dSMichael Neuling 	/* Alloc and setup PHB data structure */
227d601ea91SFrederic Barrat 	phb = pcibios_alloc_controller(vphb_dn);
2286f7f0b3dSMichael Neuling 	if (!phb)
2296f7f0b3dSMichael Neuling 		return -ENODEV;
2306f7f0b3dSMichael Neuling 
2316f7f0b3dSMichael Neuling 	/* Setup parent in sysfs */
232d601ea91SFrederic Barrat 	phb->parent = parent;
2336f7f0b3dSMichael Neuling 
2346f7f0b3dSMichael Neuling 	/* Setup the PHB using arch provided callback */
2356f7f0b3dSMichael Neuling 	phb->ops = &cxl_pcie_pci_ops;
236d601ea91SFrederic Barrat 	phb->cfg_addr = NULL;
237d601ea91SFrederic Barrat 	phb->cfg_data = 0;
2386f7f0b3dSMichael Neuling 	phb->private_data = afu;
2396f7f0b3dSMichael Neuling 	phb->controller_ops = cxl_pci_controller_ops;
2406f7f0b3dSMichael Neuling 
2416f7f0b3dSMichael Neuling 	/* Scan the bus */
2426f7f0b3dSMichael Neuling 	pcibios_scan_phb(phb);
2436f7f0b3dSMichael Neuling 	if (phb->bus == NULL)
2446f7f0b3dSMichael Neuling 		return -ENXIO;
2456f7f0b3dSMichael Neuling 
2466f7f0b3dSMichael Neuling 	/* Claim resources. This might need some rework as well depending
2476f7f0b3dSMichael Neuling 	 * whether we are doing probe-only or not, like assigning unassigned
2486f7f0b3dSMichael Neuling 	 * resources etc...
2496f7f0b3dSMichael Neuling 	 */
2506f7f0b3dSMichael Neuling 	pcibios_claim_one_bus(phb->bus);
2516f7f0b3dSMichael Neuling 
2526f7f0b3dSMichael Neuling 	/* Add probed PCI devices to the device model */
2536f7f0b3dSMichael Neuling 	pci_bus_add_devices(phb->bus);
2546f7f0b3dSMichael Neuling 
2556f7f0b3dSMichael Neuling 	afu->phb = phb;
2566f7f0b3dSMichael Neuling 
2576f7f0b3dSMichael Neuling 	return 0;
2586f7f0b3dSMichael Neuling }
2596f7f0b3dSMichael Neuling 
2606f7f0b3dSMichael Neuling void cxl_pci_vphb_remove(struct cxl_afu *afu)
2616f7f0b3dSMichael Neuling {
2626f7f0b3dSMichael Neuling 	struct pci_controller *phb;
2636f7f0b3dSMichael Neuling 
2646f7f0b3dSMichael Neuling 	/* If there is no configuration record we won't have one of these */
2656f7f0b3dSMichael Neuling 	if (!afu || !afu->phb)
2666f7f0b3dSMichael Neuling 		return;
2676f7f0b3dSMichael Neuling 
2686f7f0b3dSMichael Neuling 	phb = afu->phb;
2692e1a2556SAndrew Donnellan 	afu->phb = NULL;
2706f7f0b3dSMichael Neuling 
2716f7f0b3dSMichael Neuling 	pci_remove_root_bus(phb->bus);
2722e1a2556SAndrew Donnellan 	pcibios_free_controller(phb);
2736f7f0b3dSMichael Neuling }
2746f7f0b3dSMichael Neuling 
2756f7f0b3dSMichael Neuling struct cxl_afu *cxl_pci_to_afu(struct pci_dev *dev)
2766f7f0b3dSMichael Neuling {
2776f7f0b3dSMichael Neuling 	struct pci_controller *phb;
2786f7f0b3dSMichael Neuling 
2796f7f0b3dSMichael Neuling 	phb = pci_bus_to_host(dev->bus);
2806f7f0b3dSMichael Neuling 
2816f7f0b3dSMichael Neuling 	return (struct cxl_afu *)phb->private_data;
2826f7f0b3dSMichael Neuling }
2836f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_pci_to_afu);
2846f7f0b3dSMichael Neuling 
2856f7f0b3dSMichael Neuling unsigned int cxl_pci_to_cfg_record(struct pci_dev *dev)
2866f7f0b3dSMichael Neuling {
2876f7f0b3dSMichael Neuling 	return cxl_pcie_cfg_record(dev->bus->number, dev->devfn);
2886f7f0b3dSMichael Neuling }
2896f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_pci_to_cfg_record);
290