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>
14 static int cxl_dma_set_mask(struct pci_dev
*pdev
, u64 dma_mask
)
16 if (dma_mask
< DMA_BIT_MASK(64)) {
17 pr_info("%s only 64bit DMA supported on CXL", __func__
);
21 *(pdev
->dev
.dma_mask
) = dma_mask
;
25 static int cxl_pci_probe_mode(struct pci_bus
*bus
)
27 return PCI_PROBE_NORMAL
;
30 static int cxl_setup_msi_irqs(struct pci_dev
*pdev
, int nvec
, int type
)
35 static void cxl_teardown_msi_irqs(struct pci_dev
*pdev
)
38 * MSI should never be set but need still need to provide this call
43 static bool cxl_pci_enable_device_hook(struct pci_dev
*dev
)
45 struct pci_controller
*phb
;
47 struct cxl_context
*ctx
;
49 phb
= pci_bus_to_host(dev
->bus
);
50 afu
= (struct cxl_afu
*)phb
->private_data
;
51 set_dma_ops(&dev
->dev
, &dma_direct_ops
);
52 set_dma_offset(&dev
->dev
, PAGE_OFFSET
);
55 * Allocate a context to do cxl things too. If we eventually do real
56 * DMA ops, we'll need a default context to attach them to
58 ctx
= cxl_dev_context_init(dev
);
61 dev
->dev
.archdata
.cxl_ctx
= ctx
;
63 return (cxl_afu_check_and_enable(afu
) == 0);
66 static void cxl_pci_disable_device(struct pci_dev
*dev
)
68 struct cxl_context
*ctx
= cxl_get_context(dev
);
71 if (ctx
->status
== STARTED
) {
72 dev_err(&dev
->dev
, "Default context started\n");
75 dev
->dev
.archdata
.cxl_ctx
= NULL
;
76 cxl_release_context(ctx
);
80 static resource_size_t
cxl_pci_window_alignment(struct pci_bus
*bus
,
86 static void cxl_pci_reset_secondary_bus(struct pci_dev
*dev
)
88 /* Should we do an AFU reset here ? */
91 static int cxl_pcie_cfg_record(u8 bus
, u8 devfn
)
93 return (bus
<< 8) + devfn
;
96 static unsigned long cxl_pcie_cfg_addr(struct pci_controller
* phb
,
97 u8 bus
, u8 devfn
, int offset
)
99 int record
= cxl_pcie_cfg_record(bus
, devfn
);
101 return (unsigned long)phb
->cfg_addr
+ ((unsigned long)phb
->cfg_data
* record
) + offset
;
105 static int cxl_pcie_config_info(struct pci_bus
*bus
, unsigned int devfn
,
107 volatile void __iomem
**ioaddr
,
108 u32
*mask
, int *shift
)
110 struct pci_controller
*phb
;
114 phb
= pci_bus_to_host(bus
);
116 return PCIBIOS_DEVICE_NOT_FOUND
;
117 afu
= (struct cxl_afu
*)phb
->private_data
;
119 if (cxl_pcie_cfg_record(bus
->number
, devfn
) > afu
->crs_num
)
120 return PCIBIOS_DEVICE_NOT_FOUND
;
121 if (offset
>= (unsigned long)phb
->cfg_data
)
122 return PCIBIOS_BAD_REGISTER_NUMBER
;
123 addr
= cxl_pcie_cfg_addr(phb
, bus
->number
, devfn
, offset
);
125 *ioaddr
= (void *)(addr
& ~0x3ULL
);
126 *shift
= ((addr
& 0x3) * 8);
141 static int cxl_pcie_read_config(struct pci_bus
*bus
, unsigned int devfn
,
142 int offset
, int len
, u32
*val
)
144 volatile void __iomem
*ioaddr
;
148 rc
= cxl_pcie_config_info(bus
, devfn
, offset
, len
, &ioaddr
,
153 /* Can only read 32 bits */
154 *val
= (in_le32(ioaddr
) >> shift
) & mask
;
155 return PCIBIOS_SUCCESSFUL
;
158 static int cxl_pcie_write_config(struct pci_bus
*bus
, unsigned int devfn
,
159 int offset
, int len
, u32 val
)
161 volatile void __iomem
*ioaddr
;
165 rc
= cxl_pcie_config_info(bus
, devfn
, offset
, len
, &ioaddr
,
170 /* Can only write 32 bits so do read-modify-write */
174 v
= (in_le32(ioaddr
) & ~mask
) || (val
& mask
);
177 return PCIBIOS_SUCCESSFUL
;
180 static struct pci_ops cxl_pcie_pci_ops
=
182 .read
= cxl_pcie_read_config
,
183 .write
= cxl_pcie_write_config
,
187 static struct pci_controller_ops cxl_pci_controller_ops
=
189 .probe_mode
= cxl_pci_probe_mode
,
190 .enable_device_hook
= cxl_pci_enable_device_hook
,
191 .disable_device
= cxl_pci_disable_device
,
192 .release_device
= cxl_pci_disable_device
,
193 .window_alignment
= cxl_pci_window_alignment
,
194 .reset_secondary_bus
= cxl_pci_reset_secondary_bus
,
195 .setup_msi_irqs
= cxl_setup_msi_irqs
,
196 .teardown_msi_irqs
= cxl_teardown_msi_irqs
,
197 .dma_set_mask
= cxl_dma_set_mask
,
200 int cxl_pci_vphb_add(struct cxl_afu
*afu
)
202 struct pci_dev
*phys_dev
;
203 struct pci_controller
*phb
, *phys_phb
;
205 phys_dev
= to_pci_dev(afu
->adapter
->dev
.parent
);
206 phys_phb
= pci_bus_to_host(phys_dev
->bus
);
208 /* Alloc and setup PHB data structure */
209 phb
= pcibios_alloc_controller(phys_phb
->dn
);
214 /* Setup parent in sysfs */
215 phb
->parent
= &phys_dev
->dev
;
217 /* Setup the PHB using arch provided callback */
218 phb
->ops
= &cxl_pcie_pci_ops
;
219 phb
->cfg_addr
= afu
->afu_desc_mmio
+ afu
->crs_offset
;
220 phb
->cfg_data
= (void *)(u64
)afu
->crs_len
;
221 phb
->private_data
= afu
;
222 phb
->controller_ops
= cxl_pci_controller_ops
;
225 pcibios_scan_phb(phb
);
226 if (phb
->bus
== NULL
)
229 /* Claim resources. This might need some rework as well depending
230 * whether we are doing probe-only or not, like assigning unassigned
233 pcibios_claim_one_bus(phb
->bus
);
235 /* Add probed PCI devices to the device model */
236 pci_bus_add_devices(phb
->bus
);
244 void cxl_pci_vphb_remove(struct cxl_afu
*afu
)
246 struct pci_controller
*phb
;
248 /* If there is no configuration record we won't have one of these */
249 if (!afu
|| !afu
->phb
)
254 pci_remove_root_bus(phb
->bus
);
257 struct cxl_afu
*cxl_pci_to_afu(struct pci_dev
*dev
)
259 struct pci_controller
*phb
;
261 phb
= pci_bus_to_host(dev
->bus
);
263 return (struct cxl_afu
*)phb
->private_data
;
265 EXPORT_SYMBOL_GPL(cxl_pci_to_afu
);
267 unsigned int cxl_pci_to_cfg_record(struct pci_dev
*dev
)
269 return cxl_pcie_cfg_record(dev
->bus
->number
, dev
->devfn
);
271 EXPORT_SYMBOL_GPL(cxl_pci_to_cfg_record
);