2 * Copyright 2014 IBM Corp.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
10 #include <linux/pci.h>
12 #include <asm/pnv-pci.h>
15 static int cxl_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
17 if (dma_mask < DMA_BIT_MASK(64)) {
18 pr_info("%s only 64bit DMA supported on CXL", __func__);
22 *(pdev->dev.dma_mask) = dma_mask;
26 static int cxl_pci_probe_mode(struct pci_bus *bus)
28 return PCI_PROBE_NORMAL;
31 static int cxl_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
36 static void cxl_teardown_msi_irqs(struct pci_dev *pdev)
39 * MSI should never be set but need still need to provide this call
44 static bool cxl_pci_enable_device_hook(struct pci_dev *dev)
46 struct pci_controller *phb;
49 phb = pci_bus_to_host(dev->bus);
50 afu = (struct cxl_afu *)phb->private_data;
52 if (!cxl_ops->link_ok(afu->adapter, afu)) {
53 dev_warn(&dev->dev, "%s: Device link is down, refusing to enable AFU\n", __func__);
57 set_dma_ops(&dev->dev, &dma_direct_ops);
58 set_dma_offset(&dev->dev, PAGE_OFFSET);
60 return _cxl_pci_associate_default_context(dev, afu);
63 static resource_size_t cxl_pci_window_alignment(struct pci_bus *bus,
69 static void cxl_pci_reset_secondary_bus(struct pci_dev *dev)
71 /* Should we do an AFU reset here ? */
74 static int cxl_pcie_cfg_record(u8 bus, u8 devfn)
76 return (bus << 8) + devfn;
79 static inline struct cxl_afu *pci_bus_to_afu(struct pci_bus *bus)
81 struct pci_controller *phb = bus ? pci_bus_to_host(bus) : NULL;
83 return phb ? phb->private_data : NULL;
86 static inline int cxl_pcie_config_info(struct pci_bus *bus, unsigned int devfn,
87 struct cxl_afu *afu, int *_record)
91 record = cxl_pcie_cfg_record(bus->number, devfn);
92 if (record > afu->crs_num)
93 return PCIBIOS_DEVICE_NOT_FOUND;
99 static int cxl_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
100 int offset, int len, u32 *val)
108 afu = pci_bus_to_afu(bus);
109 /* Grab a reader lock on afu. */
110 if (afu == NULL || !down_read_trylock(&afu->configured_rwsem))
111 return PCIBIOS_DEVICE_NOT_FOUND;
113 rc = cxl_pcie_config_info(bus, devfn, afu, &record);
119 rc = cxl_ops->afu_cr_read8(afu, record, offset, &val8);
123 rc = cxl_ops->afu_cr_read16(afu, record, offset, &val16);
127 rc = cxl_ops->afu_cr_read32(afu, record, offset, &val32);
135 up_read(&afu->configured_rwsem);
136 return rc ? PCIBIOS_DEVICE_NOT_FOUND : PCIBIOS_SUCCESSFUL;
139 static int cxl_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
140 int offset, int len, u32 val)
145 afu = pci_bus_to_afu(bus);
146 /* Grab a reader lock on afu. */
147 if (afu == NULL || !down_read_trylock(&afu->configured_rwsem))
148 return PCIBIOS_DEVICE_NOT_FOUND;
150 rc = cxl_pcie_config_info(bus, devfn, afu, &record);
156 rc = cxl_ops->afu_cr_write8(afu, record, offset, val & 0xff);
159 rc = cxl_ops->afu_cr_write16(afu, record, offset, val & 0xffff);
162 rc = cxl_ops->afu_cr_write32(afu, record, offset, val);
169 up_read(&afu->configured_rwsem);
170 return rc ? PCIBIOS_SET_FAILED : PCIBIOS_SUCCESSFUL;
173 static struct pci_ops cxl_pcie_pci_ops =
175 .read = cxl_pcie_read_config,
176 .write = cxl_pcie_write_config,
180 static struct pci_controller_ops cxl_pci_controller_ops =
182 .probe_mode = cxl_pci_probe_mode,
183 .enable_device_hook = cxl_pci_enable_device_hook,
184 .disable_device = _cxl_pci_disable_device,
185 .release_device = _cxl_pci_disable_device,
186 .window_alignment = cxl_pci_window_alignment,
187 .reset_secondary_bus = cxl_pci_reset_secondary_bus,
188 .setup_msi_irqs = cxl_setup_msi_irqs,
189 .teardown_msi_irqs = cxl_teardown_msi_irqs,
190 .dma_set_mask = cxl_dma_set_mask,
193 int cxl_pci_vphb_add(struct cxl_afu *afu)
195 struct pci_controller *phb;
196 struct device_node *vphb_dn;
197 struct device *parent;
200 * If there are no AFU configuration records we won't have anything to
201 * expose under the vPHB, so skip creating one, returning success since
202 * this is still a valid case. This will also opt us out of EEH
203 * handling since we won't have anything special to do if there are no
204 * kernel drivers attached to the vPHB, and EEH handling is not yet
205 * supported in the peer model.
210 /* The parent device is the adapter. Reuse the device node of
212 * We don't seem to care what device node is used for the vPHB,
213 * but tools such as lsvpd walk up the device parents looking
214 * for a valid location code, so we might as well show devices
215 * attached to the adapter as being located on that adapter.
217 parent = afu->adapter->dev.parent;
218 vphb_dn = parent->of_node;
220 /* Alloc and setup PHB data structure */
221 phb = pcibios_alloc_controller(vphb_dn);
225 /* Setup parent in sysfs */
226 phb->parent = parent;
228 /* Setup the PHB using arch provided callback */
229 phb->ops = &cxl_pcie_pci_ops;
230 phb->cfg_addr = NULL;
231 phb->cfg_data = NULL;
232 phb->private_data = afu;
233 phb->controller_ops = cxl_pci_controller_ops;
236 pcibios_scan_phb(phb);
237 if (phb->bus == NULL)
240 /* Set release hook on root bus */
241 pci_set_host_bridge_release(to_pci_host_bridge(phb->bus->bridge),
242 pcibios_free_controller_deferred,
245 /* Claim resources. This might need some rework as well depending
246 * whether we are doing probe-only or not, like assigning unassigned
249 pcibios_claim_one_bus(phb->bus);
251 /* Add probed PCI devices to the device model */
252 pci_bus_add_devices(phb->bus);
259 void cxl_pci_vphb_remove(struct cxl_afu *afu)
261 struct pci_controller *phb;
263 /* If there is no configuration record we won't have one of these */
264 if (!afu || !afu->phb)
270 pci_remove_root_bus(phb->bus);
272 * We don't free phb here - that's handled by
273 * pcibios_free_controller_deferred()
277 static bool _cxl_pci_is_vphb_device(struct pci_controller *phb)
279 return (phb->ops == &cxl_pcie_pci_ops);
282 bool cxl_pci_is_vphb_device(struct pci_dev *dev)
284 struct pci_controller *phb;
286 phb = pci_bus_to_host(dev->bus);
288 return _cxl_pci_is_vphb_device(phb);
291 struct cxl_afu *cxl_pci_to_afu(struct pci_dev *dev)
293 struct pci_controller *phb;
295 phb = pci_bus_to_host(dev->bus);
297 if (_cxl_pci_is_vphb_device(phb))
298 return (struct cxl_afu *)phb->private_data;
300 if (pnv_pci_on_cxl_phb(dev))
301 return pnv_cxl_phb_to_afu(phb);
303 return ERR_PTR(-ENODEV);
305 EXPORT_SYMBOL_GPL(cxl_pci_to_afu);
307 unsigned int cxl_pci_to_cfg_record(struct pci_dev *dev)
309 return cxl_pcie_cfg_record(dev->bus->number, dev->devfn);
311 EXPORT_SYMBOL_GPL(cxl_pci_to_cfg_record);