xref: /openbmc/linux/drivers/misc/cxl/vphb.c (revision 14f21189df33bc972455d6a0ed875aa68718d7fc)
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;
516f7f0b3dSMichael Neuling 	set_dma_ops(&dev->dev, &dma_direct_ops);
526f7f0b3dSMichael Neuling 	set_dma_offset(&dev->dev, PAGE_OFFSET);
536f7f0b3dSMichael Neuling 
546f7f0b3dSMichael Neuling 	/*
556f7f0b3dSMichael Neuling 	 * Allocate a context to do cxl things too.  If we eventually do real
566f7f0b3dSMichael Neuling 	 * DMA ops, we'll need a default context to attach them to
576f7f0b3dSMichael Neuling 	 */
586f7f0b3dSMichael Neuling 	ctx = cxl_dev_context_init(dev);
596f7f0b3dSMichael Neuling 	if (!ctx)
606f7f0b3dSMichael Neuling 		return false;
616f7f0b3dSMichael Neuling 	dev->dev.archdata.cxl_ctx = ctx;
626f7f0b3dSMichael Neuling 
636f7f0b3dSMichael Neuling 	return (cxl_afu_check_and_enable(afu) == 0);
646f7f0b3dSMichael Neuling }
656f7f0b3dSMichael Neuling 
666f7f0b3dSMichael Neuling static void cxl_pci_disable_device(struct pci_dev *dev)
676f7f0b3dSMichael Neuling {
686f7f0b3dSMichael Neuling 	struct cxl_context *ctx = cxl_get_context(dev);
696f7f0b3dSMichael Neuling 
706f7f0b3dSMichael Neuling 	if (ctx) {
716f7f0b3dSMichael Neuling 		if (ctx->status == STARTED) {
726f7f0b3dSMichael Neuling 			dev_err(&dev->dev, "Default context started\n");
736f7f0b3dSMichael Neuling 			return;
746f7f0b3dSMichael Neuling 		}
75f67b4938SMichael Neuling 		dev->dev.archdata.cxl_ctx = NULL;
766f7f0b3dSMichael Neuling 		cxl_release_context(ctx);
776f7f0b3dSMichael Neuling 	}
786f7f0b3dSMichael Neuling }
796f7f0b3dSMichael Neuling 
806f7f0b3dSMichael Neuling static resource_size_t cxl_pci_window_alignment(struct pci_bus *bus,
816f7f0b3dSMichael Neuling 						unsigned long type)
826f7f0b3dSMichael Neuling {
836f7f0b3dSMichael Neuling 	return 1;
846f7f0b3dSMichael Neuling }
856f7f0b3dSMichael Neuling 
866f7f0b3dSMichael Neuling static void cxl_pci_reset_secondary_bus(struct pci_dev *dev)
876f7f0b3dSMichael Neuling {
886f7f0b3dSMichael Neuling 	/* Should we do an AFU reset here ? */
896f7f0b3dSMichael Neuling }
906f7f0b3dSMichael Neuling 
916f7f0b3dSMichael Neuling static int cxl_pcie_cfg_record(u8 bus, u8 devfn)
926f7f0b3dSMichael Neuling {
936f7f0b3dSMichael Neuling 	return (bus << 8) + devfn;
946f7f0b3dSMichael Neuling }
956f7f0b3dSMichael Neuling 
966f7f0b3dSMichael Neuling static unsigned long cxl_pcie_cfg_addr(struct pci_controller* phb,
976f7f0b3dSMichael Neuling 				       u8 bus, u8 devfn, int offset)
986f7f0b3dSMichael Neuling {
996f7f0b3dSMichael Neuling 	int record = cxl_pcie_cfg_record(bus, devfn);
1006f7f0b3dSMichael Neuling 
1016f7f0b3dSMichael Neuling 	return (unsigned long)phb->cfg_addr + ((unsigned long)phb->cfg_data * record) + offset;
1026f7f0b3dSMichael Neuling }
1036f7f0b3dSMichael Neuling 
1046f7f0b3dSMichael Neuling 
1056f7f0b3dSMichael Neuling static int cxl_pcie_config_info(struct pci_bus *bus, unsigned int devfn,
1066f7f0b3dSMichael Neuling 				int offset, int len,
1076f7f0b3dSMichael Neuling 				volatile void __iomem **ioaddr,
1086f7f0b3dSMichael Neuling 				u32 *mask, int *shift)
1096f7f0b3dSMichael Neuling {
1106f7f0b3dSMichael Neuling 	struct pci_controller *phb;
1116f7f0b3dSMichael Neuling 	struct cxl_afu *afu;
1126f7f0b3dSMichael Neuling 	unsigned long addr;
1136f7f0b3dSMichael Neuling 
1146f7f0b3dSMichael Neuling 	phb = pci_bus_to_host(bus);
1156f7f0b3dSMichael Neuling 	if (phb == NULL)
1166f7f0b3dSMichael Neuling 		return PCIBIOS_DEVICE_NOT_FOUND;
117*14f21189SManinder Singh 	afu = (struct cxl_afu *)phb->private_data;
118*14f21189SManinder Singh 
1196f7f0b3dSMichael Neuling 	if (cxl_pcie_cfg_record(bus->number, devfn) > afu->crs_num)
1206f7f0b3dSMichael Neuling 		return PCIBIOS_DEVICE_NOT_FOUND;
1216f7f0b3dSMichael Neuling 	if (offset >= (unsigned long)phb->cfg_data)
1226f7f0b3dSMichael Neuling 		return PCIBIOS_BAD_REGISTER_NUMBER;
1236f7f0b3dSMichael Neuling 	addr = cxl_pcie_cfg_addr(phb, bus->number, devfn, offset);
1246f7f0b3dSMichael Neuling 
1256f7f0b3dSMichael Neuling 	*ioaddr = (void *)(addr & ~0x3ULL);
1266f7f0b3dSMichael Neuling 	*shift = ((addr & 0x3) * 8);
1276f7f0b3dSMichael Neuling 	switch (len) {
1286f7f0b3dSMichael Neuling 	case 1:
1296f7f0b3dSMichael Neuling 		*mask = 0xff;
1306f7f0b3dSMichael Neuling 		break;
1316f7f0b3dSMichael Neuling 	case 2:
1326f7f0b3dSMichael Neuling 		*mask = 0xffff;
1336f7f0b3dSMichael Neuling 		break;
1346f7f0b3dSMichael Neuling 	default:
1356f7f0b3dSMichael Neuling 		*mask = 0xffffffff;
1366f7f0b3dSMichael Neuling 		break;
1376f7f0b3dSMichael Neuling 	}
1386f7f0b3dSMichael Neuling 	return 0;
1396f7f0b3dSMichael Neuling }
1406f7f0b3dSMichael Neuling 
1416f7f0b3dSMichael Neuling static int cxl_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
1426f7f0b3dSMichael Neuling 				int offset, int len, u32 *val)
1436f7f0b3dSMichael Neuling {
1446f7f0b3dSMichael Neuling 	volatile void __iomem *ioaddr;
1456f7f0b3dSMichael Neuling 	int shift, rc;
1466f7f0b3dSMichael Neuling 	u32 mask;
1476f7f0b3dSMichael Neuling 
1486f7f0b3dSMichael Neuling 	rc = cxl_pcie_config_info(bus, devfn, offset, len, &ioaddr,
1496f7f0b3dSMichael Neuling 				  &mask, &shift);
1506f7f0b3dSMichael Neuling 	if (rc)
1516f7f0b3dSMichael Neuling 		return rc;
1526f7f0b3dSMichael Neuling 
1536f7f0b3dSMichael Neuling 	/* Can only read 32 bits */
1546f7f0b3dSMichael Neuling 	*val = (in_le32(ioaddr) >> shift) & mask;
1556f7f0b3dSMichael Neuling 	return PCIBIOS_SUCCESSFUL;
1566f7f0b3dSMichael Neuling }
1576f7f0b3dSMichael Neuling 
1586f7f0b3dSMichael Neuling static int cxl_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
1596f7f0b3dSMichael Neuling 				 int offset, int len, u32 val)
1606f7f0b3dSMichael Neuling {
1616f7f0b3dSMichael Neuling 	volatile void __iomem *ioaddr;
1626f7f0b3dSMichael Neuling 	u32 v, mask;
1636f7f0b3dSMichael Neuling 	int shift, rc;
1646f7f0b3dSMichael Neuling 
1656f7f0b3dSMichael Neuling 	rc = cxl_pcie_config_info(bus, devfn, offset, len, &ioaddr,
1666f7f0b3dSMichael Neuling 				  &mask, &shift);
1676f7f0b3dSMichael Neuling 	if (rc)
1686f7f0b3dSMichael Neuling 		return rc;
1696f7f0b3dSMichael Neuling 
1706f7f0b3dSMichael Neuling 	/* Can only write 32 bits so do read-modify-write */
1716f7f0b3dSMichael Neuling 	mask <<= shift;
1726f7f0b3dSMichael Neuling 	val <<= shift;
1736f7f0b3dSMichael Neuling 
1746f7f0b3dSMichael Neuling 	v = (in_le32(ioaddr) & ~mask) || (val & mask);
1756f7f0b3dSMichael Neuling 
1766f7f0b3dSMichael Neuling 	out_le32(ioaddr, v);
1776f7f0b3dSMichael Neuling 	return PCIBIOS_SUCCESSFUL;
1786f7f0b3dSMichael Neuling }
1796f7f0b3dSMichael Neuling 
1806f7f0b3dSMichael Neuling static struct pci_ops cxl_pcie_pci_ops =
1816f7f0b3dSMichael Neuling {
1826f7f0b3dSMichael Neuling 	.read = cxl_pcie_read_config,
1836f7f0b3dSMichael Neuling 	.write = cxl_pcie_write_config,
1846f7f0b3dSMichael Neuling };
1856f7f0b3dSMichael Neuling 
1866f7f0b3dSMichael Neuling 
1876f7f0b3dSMichael Neuling static struct pci_controller_ops cxl_pci_controller_ops =
1886f7f0b3dSMichael Neuling {
1896f7f0b3dSMichael Neuling 	.probe_mode = cxl_pci_probe_mode,
1906f7f0b3dSMichael Neuling 	.enable_device_hook = cxl_pci_enable_device_hook,
1916f7f0b3dSMichael Neuling 	.disable_device = cxl_pci_disable_device,
1926f7f0b3dSMichael Neuling 	.release_device = cxl_pci_disable_device,
1936f7f0b3dSMichael Neuling 	.window_alignment = cxl_pci_window_alignment,
1946f7f0b3dSMichael Neuling 	.reset_secondary_bus = cxl_pci_reset_secondary_bus,
1956f7f0b3dSMichael Neuling 	.setup_msi_irqs = cxl_setup_msi_irqs,
1966f7f0b3dSMichael Neuling 	.teardown_msi_irqs = cxl_teardown_msi_irqs,
1976f7f0b3dSMichael Neuling 	.dma_set_mask = cxl_dma_set_mask,
1986f7f0b3dSMichael Neuling };
1996f7f0b3dSMichael Neuling 
2006f7f0b3dSMichael Neuling int cxl_pci_vphb_add(struct cxl_afu *afu)
2016f7f0b3dSMichael Neuling {
2026f7f0b3dSMichael Neuling 	struct pci_dev *phys_dev;
2036f7f0b3dSMichael Neuling 	struct pci_controller *phb, *phys_phb;
2046f7f0b3dSMichael Neuling 
2056f7f0b3dSMichael Neuling 	phys_dev = to_pci_dev(afu->adapter->dev.parent);
2066f7f0b3dSMichael Neuling 	phys_phb = pci_bus_to_host(phys_dev->bus);
2076f7f0b3dSMichael Neuling 
2086f7f0b3dSMichael Neuling 	/* Alloc and setup PHB data structure */
2096f7f0b3dSMichael Neuling 	phb = pcibios_alloc_controller(phys_phb->dn);
2106f7f0b3dSMichael Neuling 
2116f7f0b3dSMichael Neuling 	if (!phb)
2126f7f0b3dSMichael Neuling 		return -ENODEV;
2136f7f0b3dSMichael Neuling 
2146f7f0b3dSMichael Neuling 	/* Setup parent in sysfs */
2156f7f0b3dSMichael Neuling 	phb->parent = &phys_dev->dev;
2166f7f0b3dSMichael Neuling 
2176f7f0b3dSMichael Neuling 	/* Setup the PHB using arch provided callback */
2186f7f0b3dSMichael Neuling 	phb->ops = &cxl_pcie_pci_ops;
2196f7f0b3dSMichael Neuling 	phb->cfg_addr = afu->afu_desc_mmio + afu->crs_offset;
2206f7f0b3dSMichael Neuling 	phb->cfg_data = (void *)(u64)afu->crs_len;
2216f7f0b3dSMichael Neuling 	phb->private_data = afu;
2226f7f0b3dSMichael Neuling 	phb->controller_ops = cxl_pci_controller_ops;
2236f7f0b3dSMichael Neuling 
2246f7f0b3dSMichael Neuling 	/* Scan the bus */
2256f7f0b3dSMichael Neuling 	pcibios_scan_phb(phb);
2266f7f0b3dSMichael Neuling 	if (phb->bus == NULL)
2276f7f0b3dSMichael Neuling 		return -ENXIO;
2286f7f0b3dSMichael Neuling 
2296f7f0b3dSMichael Neuling 	/* Claim resources. This might need some rework as well depending
2306f7f0b3dSMichael Neuling 	 * whether we are doing probe-only or not, like assigning unassigned
2316f7f0b3dSMichael Neuling 	 * resources etc...
2326f7f0b3dSMichael Neuling 	 */
2336f7f0b3dSMichael Neuling 	pcibios_claim_one_bus(phb->bus);
2346f7f0b3dSMichael Neuling 
2356f7f0b3dSMichael Neuling 	/* Add probed PCI devices to the device model */
2366f7f0b3dSMichael Neuling 	pci_bus_add_devices(phb->bus);
2376f7f0b3dSMichael Neuling 
2386f7f0b3dSMichael Neuling 	afu->phb = phb;
2396f7f0b3dSMichael Neuling 
2406f7f0b3dSMichael Neuling 	return 0;
2416f7f0b3dSMichael Neuling }
2426f7f0b3dSMichael Neuling 
2436f7f0b3dSMichael Neuling 
2446f7f0b3dSMichael Neuling void cxl_pci_vphb_remove(struct cxl_afu *afu)
2456f7f0b3dSMichael Neuling {
2466f7f0b3dSMichael Neuling 	struct pci_controller *phb;
2476f7f0b3dSMichael Neuling 
2486f7f0b3dSMichael Neuling 	/* If there is no configuration record we won't have one of these */
2496f7f0b3dSMichael Neuling 	if (!afu || !afu->phb)
2506f7f0b3dSMichael Neuling 		return;
2516f7f0b3dSMichael Neuling 
2526f7f0b3dSMichael Neuling 	phb = afu->phb;
2536f7f0b3dSMichael Neuling 
2546f7f0b3dSMichael Neuling 	pci_remove_root_bus(phb->bus);
2556f7f0b3dSMichael Neuling }
2566f7f0b3dSMichael Neuling 
2576f7f0b3dSMichael Neuling struct cxl_afu *cxl_pci_to_afu(struct pci_dev *dev)
2586f7f0b3dSMichael Neuling {
2596f7f0b3dSMichael Neuling 	struct pci_controller *phb;
2606f7f0b3dSMichael Neuling 
2616f7f0b3dSMichael Neuling 	phb = pci_bus_to_host(dev->bus);
2626f7f0b3dSMichael Neuling 
2636f7f0b3dSMichael Neuling 	return (struct cxl_afu *)phb->private_data;
2646f7f0b3dSMichael Neuling }
2656f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_pci_to_afu);
2666f7f0b3dSMichael Neuling 
2676f7f0b3dSMichael Neuling unsigned int cxl_pci_to_cfg_record(struct pci_dev *dev)
2686f7f0b3dSMichael Neuling {
2696f7f0b3dSMichael Neuling 	return cxl_pcie_cfg_record(dev->bus->number, dev->devfn);
2706f7f0b3dSMichael Neuling }
2716f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_pci_to_cfg_record);
272