xref: /openbmc/linux/drivers/misc/cxl/vphb.c (revision 7d1647dc4ba0a61fec5381c1abb59dc886b6ef3c)
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;
51*7d1647dcSAndrew Donnellan 
52*7d1647dcSAndrew Donnellan 	if (!cxl_adapter_link_ok(afu->adapter)) {
53*7d1647dcSAndrew Donnellan 		dev_warn(&dev->dev, "%s: Device link is down, refusing to enable AFU\n", __func__);
54*7d1647dcSAndrew Donnellan 		return false;
55*7d1647dcSAndrew Donnellan 	}
56*7d1647dcSAndrew 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 
696f7f0b3dSMichael Neuling 	return (cxl_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 unsigned long cxl_pcie_cfg_addr(struct pci_controller* phb,
1036f7f0b3dSMichael Neuling 				       u8 bus, u8 devfn, int offset)
1046f7f0b3dSMichael Neuling {
1056f7f0b3dSMichael Neuling 	int record = cxl_pcie_cfg_record(bus, devfn);
1066f7f0b3dSMichael Neuling 
1076f7f0b3dSMichael Neuling 	return (unsigned long)phb->cfg_addr + ((unsigned long)phb->cfg_data * record) + offset;
1086f7f0b3dSMichael Neuling }
1096f7f0b3dSMichael Neuling 
1106f7f0b3dSMichael Neuling 
1116f7f0b3dSMichael Neuling static int cxl_pcie_config_info(struct pci_bus *bus, unsigned int devfn,
1126f7f0b3dSMichael Neuling 				int offset, int len,
1136f7f0b3dSMichael Neuling 				volatile void __iomem **ioaddr,
1146f7f0b3dSMichael Neuling 				u32 *mask, int *shift)
1156f7f0b3dSMichael Neuling {
1166f7f0b3dSMichael Neuling 	struct pci_controller *phb;
1176f7f0b3dSMichael Neuling 	struct cxl_afu *afu;
1186f7f0b3dSMichael Neuling 	unsigned long addr;
1196f7f0b3dSMichael Neuling 
1206f7f0b3dSMichael Neuling 	phb = pci_bus_to_host(bus);
1216f7f0b3dSMichael Neuling 	if (phb == NULL)
1226f7f0b3dSMichael Neuling 		return PCIBIOS_DEVICE_NOT_FOUND;
12314f21189SManinder Singh 	afu = (struct cxl_afu *)phb->private_data;
12414f21189SManinder Singh 
1256f7f0b3dSMichael Neuling 	if (cxl_pcie_cfg_record(bus->number, devfn) > afu->crs_num)
1266f7f0b3dSMichael Neuling 		return PCIBIOS_DEVICE_NOT_FOUND;
1276f7f0b3dSMichael Neuling 	if (offset >= (unsigned long)phb->cfg_data)
1286f7f0b3dSMichael Neuling 		return PCIBIOS_BAD_REGISTER_NUMBER;
1296f7f0b3dSMichael Neuling 	addr = cxl_pcie_cfg_addr(phb, bus->number, devfn, offset);
1306f7f0b3dSMichael Neuling 
1316f7f0b3dSMichael Neuling 	*ioaddr = (void *)(addr & ~0x3ULL);
1326f7f0b3dSMichael Neuling 	*shift = ((addr & 0x3) * 8);
1336f7f0b3dSMichael Neuling 	switch (len) {
1346f7f0b3dSMichael Neuling 	case 1:
1356f7f0b3dSMichael Neuling 		*mask = 0xff;
1366f7f0b3dSMichael Neuling 		break;
1376f7f0b3dSMichael Neuling 	case 2:
1386f7f0b3dSMichael Neuling 		*mask = 0xffff;
1396f7f0b3dSMichael Neuling 		break;
1406f7f0b3dSMichael Neuling 	default:
1416f7f0b3dSMichael Neuling 		*mask = 0xffffffff;
1426f7f0b3dSMichael Neuling 		break;
1436f7f0b3dSMichael Neuling 	}
1446f7f0b3dSMichael Neuling 	return 0;
1456f7f0b3dSMichael Neuling }
1466f7f0b3dSMichael Neuling 
1470b3f9c75SDaniel Axtens 
1480b3f9c75SDaniel Axtens static inline bool cxl_config_link_ok(struct pci_bus *bus)
1490b3f9c75SDaniel Axtens {
1500b3f9c75SDaniel Axtens 	struct pci_controller *phb;
1510b3f9c75SDaniel Axtens 	struct cxl_afu *afu;
1520b3f9c75SDaniel Axtens 
1530b3f9c75SDaniel Axtens 	/* Config space IO is based on phb->cfg_addr, which is based on
1540b3f9c75SDaniel Axtens 	 * afu_desc_mmio. This isn't safe to read/write when the link
1550b3f9c75SDaniel Axtens 	 * goes down, as EEH tears down MMIO space.
1560b3f9c75SDaniel Axtens 	 *
1570b3f9c75SDaniel Axtens 	 * Check if the link is OK before proceeding.
1580b3f9c75SDaniel Axtens 	 */
1590b3f9c75SDaniel Axtens 
1600b3f9c75SDaniel Axtens 	phb = pci_bus_to_host(bus);
1610b3f9c75SDaniel Axtens 	if (phb == NULL)
1620b3f9c75SDaniel Axtens 		return false;
1630b3f9c75SDaniel Axtens 	afu = (struct cxl_afu *)phb->private_data;
1640b3f9c75SDaniel Axtens 	return cxl_adapter_link_ok(afu->adapter);
1650b3f9c75SDaniel Axtens }
1660b3f9c75SDaniel Axtens 
1676f7f0b3dSMichael Neuling static int cxl_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
1686f7f0b3dSMichael Neuling 				int offset, int len, u32 *val)
1696f7f0b3dSMichael Neuling {
1706f7f0b3dSMichael Neuling 	volatile void __iomem *ioaddr;
1716f7f0b3dSMichael Neuling 	int shift, rc;
1726f7f0b3dSMichael Neuling 	u32 mask;
1736f7f0b3dSMichael Neuling 
1746f7f0b3dSMichael Neuling 	rc = cxl_pcie_config_info(bus, devfn, offset, len, &ioaddr,
1756f7f0b3dSMichael Neuling 				  &mask, &shift);
1766f7f0b3dSMichael Neuling 	if (rc)
1776f7f0b3dSMichael Neuling 		return rc;
1786f7f0b3dSMichael Neuling 
1790b3f9c75SDaniel Axtens 	if (!cxl_config_link_ok(bus))
1800b3f9c75SDaniel Axtens 		return PCIBIOS_DEVICE_NOT_FOUND;
1810b3f9c75SDaniel Axtens 
1826f7f0b3dSMichael Neuling 	/* Can only read 32 bits */
1836f7f0b3dSMichael Neuling 	*val = (in_le32(ioaddr) >> shift) & mask;
1846f7f0b3dSMichael Neuling 	return PCIBIOS_SUCCESSFUL;
1856f7f0b3dSMichael Neuling }
1866f7f0b3dSMichael Neuling 
1876f7f0b3dSMichael Neuling static int cxl_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
1886f7f0b3dSMichael Neuling 				 int offset, int len, u32 val)
1896f7f0b3dSMichael Neuling {
1906f7f0b3dSMichael Neuling 	volatile void __iomem *ioaddr;
1916f7f0b3dSMichael Neuling 	u32 v, mask;
1926f7f0b3dSMichael Neuling 	int shift, rc;
1936f7f0b3dSMichael Neuling 
1946f7f0b3dSMichael Neuling 	rc = cxl_pcie_config_info(bus, devfn, offset, len, &ioaddr,
1956f7f0b3dSMichael Neuling 				  &mask, &shift);
1966f7f0b3dSMichael Neuling 	if (rc)
1976f7f0b3dSMichael Neuling 		return rc;
1986f7f0b3dSMichael Neuling 
1990b3f9c75SDaniel Axtens 	if (!cxl_config_link_ok(bus))
2000b3f9c75SDaniel Axtens 		return PCIBIOS_DEVICE_NOT_FOUND;
2010b3f9c75SDaniel Axtens 
2026f7f0b3dSMichael Neuling 	/* Can only write 32 bits so do read-modify-write */
2036f7f0b3dSMichael Neuling 	mask <<= shift;
2046f7f0b3dSMichael Neuling 	val <<= shift;
2056f7f0b3dSMichael Neuling 
2066f7f0b3dSMichael Neuling 	v = (in_le32(ioaddr) & ~mask) || (val & mask);
2076f7f0b3dSMichael Neuling 
2086f7f0b3dSMichael Neuling 	out_le32(ioaddr, v);
2096f7f0b3dSMichael Neuling 	return PCIBIOS_SUCCESSFUL;
2106f7f0b3dSMichael Neuling }
2116f7f0b3dSMichael Neuling 
2126f7f0b3dSMichael Neuling static struct pci_ops cxl_pcie_pci_ops =
2136f7f0b3dSMichael Neuling {
2146f7f0b3dSMichael Neuling 	.read = cxl_pcie_read_config,
2156f7f0b3dSMichael Neuling 	.write = cxl_pcie_write_config,
2166f7f0b3dSMichael Neuling };
2176f7f0b3dSMichael Neuling 
2186f7f0b3dSMichael Neuling 
2196f7f0b3dSMichael Neuling static struct pci_controller_ops cxl_pci_controller_ops =
2206f7f0b3dSMichael Neuling {
2216f7f0b3dSMichael Neuling 	.probe_mode = cxl_pci_probe_mode,
2226f7f0b3dSMichael Neuling 	.enable_device_hook = cxl_pci_enable_device_hook,
2236f7f0b3dSMichael Neuling 	.disable_device = cxl_pci_disable_device,
2246f7f0b3dSMichael Neuling 	.release_device = cxl_pci_disable_device,
2256f7f0b3dSMichael Neuling 	.window_alignment = cxl_pci_window_alignment,
2266f7f0b3dSMichael Neuling 	.reset_secondary_bus = cxl_pci_reset_secondary_bus,
2276f7f0b3dSMichael Neuling 	.setup_msi_irqs = cxl_setup_msi_irqs,
2286f7f0b3dSMichael Neuling 	.teardown_msi_irqs = cxl_teardown_msi_irqs,
2296f7f0b3dSMichael Neuling 	.dma_set_mask = cxl_dma_set_mask,
2306f7f0b3dSMichael Neuling };
2316f7f0b3dSMichael Neuling 
2326f7f0b3dSMichael Neuling int cxl_pci_vphb_add(struct cxl_afu *afu)
2336f7f0b3dSMichael Neuling {
2346f7f0b3dSMichael Neuling 	struct pci_dev *phys_dev;
2356f7f0b3dSMichael Neuling 	struct pci_controller *phb, *phys_phb;
2366f7f0b3dSMichael Neuling 
2376f7f0b3dSMichael Neuling 	phys_dev = to_pci_dev(afu->adapter->dev.parent);
2386f7f0b3dSMichael Neuling 	phys_phb = pci_bus_to_host(phys_dev->bus);
2396f7f0b3dSMichael Neuling 
2406f7f0b3dSMichael Neuling 	/* Alloc and setup PHB data structure */
2416f7f0b3dSMichael Neuling 	phb = pcibios_alloc_controller(phys_phb->dn);
2426f7f0b3dSMichael Neuling 
2436f7f0b3dSMichael Neuling 	if (!phb)
2446f7f0b3dSMichael Neuling 		return -ENODEV;
2456f7f0b3dSMichael Neuling 
2466f7f0b3dSMichael Neuling 	/* Setup parent in sysfs */
2476f7f0b3dSMichael Neuling 	phb->parent = &phys_dev->dev;
2486f7f0b3dSMichael Neuling 
2496f7f0b3dSMichael Neuling 	/* Setup the PHB using arch provided callback */
2506f7f0b3dSMichael Neuling 	phb->ops = &cxl_pcie_pci_ops;
2516f7f0b3dSMichael Neuling 	phb->cfg_addr = afu->afu_desc_mmio + afu->crs_offset;
2526f7f0b3dSMichael Neuling 	phb->cfg_data = (void *)(u64)afu->crs_len;
2536f7f0b3dSMichael Neuling 	phb->private_data = afu;
2546f7f0b3dSMichael Neuling 	phb->controller_ops = cxl_pci_controller_ops;
2556f7f0b3dSMichael Neuling 
2566f7f0b3dSMichael Neuling 	/* Scan the bus */
2576f7f0b3dSMichael Neuling 	pcibios_scan_phb(phb);
2586f7f0b3dSMichael Neuling 	if (phb->bus == NULL)
2596f7f0b3dSMichael Neuling 		return -ENXIO;
2606f7f0b3dSMichael Neuling 
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 
2759e8df8a2SDaniel Axtens void cxl_pci_vphb_reconfigure(struct cxl_afu *afu)
2769e8df8a2SDaniel Axtens {
2779e8df8a2SDaniel Axtens 	/* When we are reconfigured, the AFU's MMIO space is unmapped
2789e8df8a2SDaniel Axtens 	 * and remapped. We need to reflect this in the PHB's view of
2799e8df8a2SDaniel Axtens 	 * the world.
2809e8df8a2SDaniel Axtens 	 */
2819e8df8a2SDaniel Axtens 	afu->phb->cfg_addr = afu->afu_desc_mmio + afu->crs_offset;
2829e8df8a2SDaniel Axtens }
2836f7f0b3dSMichael Neuling 
2846f7f0b3dSMichael Neuling void cxl_pci_vphb_remove(struct cxl_afu *afu)
2856f7f0b3dSMichael Neuling {
2866f7f0b3dSMichael Neuling 	struct pci_controller *phb;
2876f7f0b3dSMichael Neuling 
2886f7f0b3dSMichael Neuling 	/* If there is no configuration record we won't have one of these */
2896f7f0b3dSMichael Neuling 	if (!afu || !afu->phb)
2906f7f0b3dSMichael Neuling 		return;
2916f7f0b3dSMichael Neuling 
2926f7f0b3dSMichael Neuling 	phb = afu->phb;
2936f7f0b3dSMichael Neuling 
2946f7f0b3dSMichael Neuling 	pci_remove_root_bus(phb->bus);
2956f7f0b3dSMichael Neuling }
2966f7f0b3dSMichael Neuling 
2976f7f0b3dSMichael Neuling struct cxl_afu *cxl_pci_to_afu(struct pci_dev *dev)
2986f7f0b3dSMichael Neuling {
2996f7f0b3dSMichael Neuling 	struct pci_controller *phb;
3006f7f0b3dSMichael Neuling 
3016f7f0b3dSMichael Neuling 	phb = pci_bus_to_host(dev->bus);
3026f7f0b3dSMichael Neuling 
3036f7f0b3dSMichael Neuling 	return (struct cxl_afu *)phb->private_data;
3046f7f0b3dSMichael Neuling }
3056f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_pci_to_afu);
3066f7f0b3dSMichael Neuling 
3076f7f0b3dSMichael Neuling unsigned int cxl_pci_to_cfg_record(struct pci_dev *dev)
3086f7f0b3dSMichael Neuling {
3096f7f0b3dSMichael Neuling 	return cxl_pcie_cfg_record(dev->bus->number, dev->devfn);
3106f7f0b3dSMichael Neuling }
3116f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_pci_to_cfg_record);
312