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_nommu_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 void cxl_afu_configured_put(struct cxl_afu
*afu
)
88 atomic_dec_if_positive(&afu
->configured_state
);
91 static bool cxl_afu_configured_get(struct cxl_afu
*afu
)
93 return atomic_inc_unless_negative(&afu
->configured_state
);
96 static inline int cxl_pcie_config_info(struct pci_bus
*bus
, unsigned int devfn
,
97 struct cxl_afu
*afu
, int *_record
)
101 record
= cxl_pcie_cfg_record(bus
->number
, devfn
);
102 if (record
> afu
->crs_num
)
103 return PCIBIOS_DEVICE_NOT_FOUND
;
109 static int cxl_pcie_read_config(struct pci_bus
*bus
, unsigned int devfn
,
110 int offset
, int len
, u32
*val
)
118 afu
= pci_bus_to_afu(bus
);
119 /* Grab a reader lock on afu. */
120 if (afu
== NULL
|| !cxl_afu_configured_get(afu
))
121 return PCIBIOS_DEVICE_NOT_FOUND
;
123 rc
= cxl_pcie_config_info(bus
, devfn
, afu
, &record
);
129 rc
= cxl_ops
->afu_cr_read8(afu
, record
, offset
, &val8
);
133 rc
= cxl_ops
->afu_cr_read16(afu
, record
, offset
, &val16
);
137 rc
= cxl_ops
->afu_cr_read32(afu
, record
, offset
, &val32
);
145 cxl_afu_configured_put(afu
);
146 return rc
? PCIBIOS_DEVICE_NOT_FOUND
: PCIBIOS_SUCCESSFUL
;
149 static int cxl_pcie_write_config(struct pci_bus
*bus
, unsigned int devfn
,
150 int offset
, int len
, u32 val
)
155 afu
= pci_bus_to_afu(bus
);
156 /* Grab a reader lock on afu. */
157 if (afu
== NULL
|| !cxl_afu_configured_get(afu
))
158 return PCIBIOS_DEVICE_NOT_FOUND
;
160 rc
= cxl_pcie_config_info(bus
, devfn
, afu
, &record
);
166 rc
= cxl_ops
->afu_cr_write8(afu
, record
, offset
, val
& 0xff);
169 rc
= cxl_ops
->afu_cr_write16(afu
, record
, offset
, val
& 0xffff);
172 rc
= cxl_ops
->afu_cr_write32(afu
, record
, offset
, val
);
179 cxl_afu_configured_put(afu
);
180 return rc
? PCIBIOS_SET_FAILED
: PCIBIOS_SUCCESSFUL
;
183 static struct pci_ops cxl_pcie_pci_ops
=
185 .read
= cxl_pcie_read_config
,
186 .write
= cxl_pcie_write_config
,
190 static struct pci_controller_ops cxl_pci_controller_ops
=
192 .probe_mode
= cxl_pci_probe_mode
,
193 .enable_device_hook
= cxl_pci_enable_device_hook
,
194 .disable_device
= _cxl_pci_disable_device
,
195 .release_device
= _cxl_pci_disable_device
,
196 .window_alignment
= cxl_pci_window_alignment
,
197 .reset_secondary_bus
= cxl_pci_reset_secondary_bus
,
198 .setup_msi_irqs
= cxl_setup_msi_irqs
,
199 .teardown_msi_irqs
= cxl_teardown_msi_irqs
,
200 .dma_set_mask
= cxl_dma_set_mask
,
203 int cxl_pci_vphb_add(struct cxl_afu
*afu
)
205 struct pci_controller
*phb
;
206 struct device_node
*vphb_dn
;
207 struct device
*parent
;
210 * If there are no AFU configuration records we won't have anything to
211 * expose under the vPHB, so skip creating one, returning success since
212 * this is still a valid case. This will also opt us out of EEH
213 * handling since we won't have anything special to do if there are no
214 * kernel drivers attached to the vPHB, and EEH handling is not yet
215 * supported in the peer model.
220 /* The parent device is the adapter. Reuse the device node of
222 * We don't seem to care what device node is used for the vPHB,
223 * but tools such as lsvpd walk up the device parents looking
224 * for a valid location code, so we might as well show devices
225 * attached to the adapter as being located on that adapter.
227 parent
= afu
->adapter
->dev
.parent
;
228 vphb_dn
= parent
->of_node
;
230 /* Alloc and setup PHB data structure */
231 phb
= pcibios_alloc_controller(vphb_dn
);
235 /* Setup parent in sysfs */
236 phb
->parent
= parent
;
238 /* Setup the PHB using arch provided callback */
239 phb
->ops
= &cxl_pcie_pci_ops
;
240 phb
->cfg_addr
= NULL
;
241 phb
->cfg_data
= NULL
;
242 phb
->private_data
= afu
;
243 phb
->controller_ops
= cxl_pci_controller_ops
;
246 pcibios_scan_phb(phb
);
247 if (phb
->bus
== NULL
)
250 /* Set release hook on root bus */
251 pci_set_host_bridge_release(to_pci_host_bridge(phb
->bus
->bridge
),
252 pcibios_free_controller_deferred
,
255 /* Claim resources. This might need some rework as well depending
256 * whether we are doing probe-only or not, like assigning unassigned
259 pcibios_claim_one_bus(phb
->bus
);
261 /* Add probed PCI devices to the device model */
262 pci_bus_add_devices(phb
->bus
);
269 void cxl_pci_vphb_remove(struct cxl_afu
*afu
)
271 struct pci_controller
*phb
;
273 /* If there is no configuration record we won't have one of these */
274 if (!afu
|| !afu
->phb
)
280 pci_remove_root_bus(phb
->bus
);
282 * We don't free phb here - that's handled by
283 * pcibios_free_controller_deferred()
287 static bool _cxl_pci_is_vphb_device(struct pci_controller
*phb
)
289 return (phb
->ops
== &cxl_pcie_pci_ops
);
292 bool cxl_pci_is_vphb_device(struct pci_dev
*dev
)
294 struct pci_controller
*phb
;
296 phb
= pci_bus_to_host(dev
->bus
);
298 return _cxl_pci_is_vphb_device(phb
);
301 struct cxl_afu
*cxl_pci_to_afu(struct pci_dev
*dev
)
303 struct pci_controller
*phb
;
305 phb
= pci_bus_to_host(dev
->bus
);
307 if (_cxl_pci_is_vphb_device(phb
))
308 return (struct cxl_afu
*)phb
->private_data
;
310 if (pnv_pci_on_cxl_phb(dev
))
311 return pnv_cxl_phb_to_afu(phb
);
313 return ERR_PTR(-ENODEV
);
315 EXPORT_SYMBOL_GPL(cxl_pci_to_afu
);
317 unsigned int cxl_pci_to_cfg_record(struct pci_dev
*dev
)
319 return cxl_pcie_cfg_record(dev
->bus
->number
, dev
->devfn
);
321 EXPORT_SYMBOL_GPL(cxl_pci_to_cfg_record
);