xref: /openbmc/linux/drivers/misc/cxl/vphb.c (revision 31af04cd)
1 /*
2  * Copyright 2014 IBM Corp.
3  *
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.
8  */
9 
10 #include <linux/pci.h>
11 #include <misc/cxl.h>
12 #include "cxl.h"
13 
14 static int cxl_pci_probe_mode(struct pci_bus *bus)
15 {
16 	return PCI_PROBE_NORMAL;
17 }
18 
19 static int cxl_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
20 {
21 	return -ENODEV;
22 }
23 
24 static void cxl_teardown_msi_irqs(struct pci_dev *pdev)
25 {
26 	/*
27 	 * MSI should never be set but need still need to provide this call
28 	 * back.
29 	 */
30 }
31 
32 static bool cxl_pci_enable_device_hook(struct pci_dev *dev)
33 {
34 	struct pci_controller *phb;
35 	struct cxl_afu *afu;
36 	struct cxl_context *ctx;
37 
38 	phb = pci_bus_to_host(dev->bus);
39 	afu = (struct cxl_afu *)phb->private_data;
40 
41 	if (!cxl_ops->link_ok(afu->adapter, afu)) {
42 		dev_warn(&dev->dev, "%s: Device link is down, refusing to enable AFU\n", __func__);
43 		return false;
44 	}
45 
46 	set_dma_ops(&dev->dev, &dma_nommu_ops);
47 	set_dma_offset(&dev->dev, PAGE_OFFSET);
48 
49 	/*
50 	 * Allocate a context to do cxl things too.  If we eventually do real
51 	 * DMA ops, we'll need a default context to attach them to
52 	 */
53 	ctx = cxl_dev_context_init(dev);
54 	if (IS_ERR(ctx))
55 		return false;
56 	dev->dev.archdata.cxl_ctx = ctx;
57 
58 	return (cxl_ops->afu_check_and_enable(afu) == 0);
59 }
60 
61 static void cxl_pci_disable_device(struct pci_dev *dev)
62 {
63 	struct cxl_context *ctx = cxl_get_context(dev);
64 
65 	if (ctx) {
66 		if (ctx->status == STARTED) {
67 			dev_err(&dev->dev, "Default context started\n");
68 			return;
69 		}
70 		dev->dev.archdata.cxl_ctx = NULL;
71 		cxl_release_context(ctx);
72 	}
73 }
74 
75 static resource_size_t cxl_pci_window_alignment(struct pci_bus *bus,
76 						unsigned long type)
77 {
78 	return 1;
79 }
80 
81 static void cxl_pci_reset_secondary_bus(struct pci_dev *dev)
82 {
83 	/* Should we do an AFU reset here ? */
84 }
85 
86 static int cxl_pcie_cfg_record(u8 bus, u8 devfn)
87 {
88 	return (bus << 8) + devfn;
89 }
90 
91 static inline struct cxl_afu *pci_bus_to_afu(struct pci_bus *bus)
92 {
93 	struct pci_controller *phb = bus ? pci_bus_to_host(bus) : NULL;
94 
95 	return phb ? phb->private_data : NULL;
96 }
97 
98 static void cxl_afu_configured_put(struct cxl_afu *afu)
99 {
100 	atomic_dec_if_positive(&afu->configured_state);
101 }
102 
103 static bool cxl_afu_configured_get(struct cxl_afu *afu)
104 {
105 	return atomic_inc_unless_negative(&afu->configured_state);
106 }
107 
108 static inline int cxl_pcie_config_info(struct pci_bus *bus, unsigned int devfn,
109 				       struct cxl_afu *afu, int *_record)
110 {
111 	int record;
112 
113 	record = cxl_pcie_cfg_record(bus->number, devfn);
114 	if (record > afu->crs_num)
115 		return PCIBIOS_DEVICE_NOT_FOUND;
116 
117 	*_record = record;
118 	return 0;
119 }
120 
121 static int cxl_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
122 				int offset, int len, u32 *val)
123 {
124 	int rc, record;
125 	struct cxl_afu *afu;
126 	u8 val8;
127 	u16 val16;
128 	u32 val32;
129 
130 	afu = pci_bus_to_afu(bus);
131 	/* Grab a reader lock on afu. */
132 	if (afu == NULL || !cxl_afu_configured_get(afu))
133 		return PCIBIOS_DEVICE_NOT_FOUND;
134 
135 	rc = cxl_pcie_config_info(bus, devfn, afu, &record);
136 	if (rc)
137 		goto out;
138 
139 	switch (len) {
140 	case 1:
141 		rc = cxl_ops->afu_cr_read8(afu, record, offset,	&val8);
142 		*val = val8;
143 		break;
144 	case 2:
145 		rc = cxl_ops->afu_cr_read16(afu, record, offset, &val16);
146 		*val = val16;
147 		break;
148 	case 4:
149 		rc = cxl_ops->afu_cr_read32(afu, record, offset, &val32);
150 		*val = val32;
151 		break;
152 	default:
153 		WARN_ON(1);
154 	}
155 
156 out:
157 	cxl_afu_configured_put(afu);
158 	return rc ? PCIBIOS_DEVICE_NOT_FOUND : PCIBIOS_SUCCESSFUL;
159 }
160 
161 static int cxl_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
162 				 int offset, int len, u32 val)
163 {
164 	int rc, record;
165 	struct cxl_afu *afu;
166 
167 	afu = pci_bus_to_afu(bus);
168 	/* Grab a reader lock on afu. */
169 	if (afu == NULL || !cxl_afu_configured_get(afu))
170 		return PCIBIOS_DEVICE_NOT_FOUND;
171 
172 	rc = cxl_pcie_config_info(bus, devfn, afu, &record);
173 	if (rc)
174 		goto out;
175 
176 	switch (len) {
177 	case 1:
178 		rc = cxl_ops->afu_cr_write8(afu, record, offset, val & 0xff);
179 		break;
180 	case 2:
181 		rc = cxl_ops->afu_cr_write16(afu, record, offset, val & 0xffff);
182 		break;
183 	case 4:
184 		rc = cxl_ops->afu_cr_write32(afu, record, offset, val);
185 		break;
186 	default:
187 		WARN_ON(1);
188 	}
189 
190 out:
191 	cxl_afu_configured_put(afu);
192 	return rc ? PCIBIOS_SET_FAILED : PCIBIOS_SUCCESSFUL;
193 }
194 
195 static struct pci_ops cxl_pcie_pci_ops =
196 {
197 	.read = cxl_pcie_read_config,
198 	.write = cxl_pcie_write_config,
199 };
200 
201 
202 static struct pci_controller_ops cxl_pci_controller_ops =
203 {
204 	.probe_mode = cxl_pci_probe_mode,
205 	.enable_device_hook = cxl_pci_enable_device_hook,
206 	.disable_device = cxl_pci_disable_device,
207 	.release_device = cxl_pci_disable_device,
208 	.window_alignment = cxl_pci_window_alignment,
209 	.reset_secondary_bus = cxl_pci_reset_secondary_bus,
210 	.setup_msi_irqs = cxl_setup_msi_irqs,
211 	.teardown_msi_irqs = cxl_teardown_msi_irqs,
212 };
213 
214 int cxl_pci_vphb_add(struct cxl_afu *afu)
215 {
216 	struct pci_controller *phb;
217 	struct device_node *vphb_dn;
218 	struct device *parent;
219 
220 	/*
221 	 * If there are no AFU configuration records we won't have anything to
222 	 * expose under the vPHB, so skip creating one, returning success since
223 	 * this is still a valid case. This will also opt us out of EEH
224 	 * handling since we won't have anything special to do if there are no
225 	 * kernel drivers attached to the vPHB, and EEH handling is not yet
226 	 * supported in the peer model.
227 	 */
228 	if (!afu->crs_num)
229 		return 0;
230 
231 	/* The parent device is the adapter. Reuse the device node of
232 	 * the adapter.
233 	 * We don't seem to care what device node is used for the vPHB,
234 	 * but tools such as lsvpd walk up the device parents looking
235 	 * for a valid location code, so we might as well show devices
236 	 * attached to the adapter as being located on that adapter.
237 	 */
238 	parent = afu->adapter->dev.parent;
239 	vphb_dn = parent->of_node;
240 
241 	/* Alloc and setup PHB data structure */
242 	phb = pcibios_alloc_controller(vphb_dn);
243 	if (!phb)
244 		return -ENODEV;
245 
246 	/* Setup parent in sysfs */
247 	phb->parent = parent;
248 
249 	/* Setup the PHB using arch provided callback */
250 	phb->ops = &cxl_pcie_pci_ops;
251 	phb->cfg_addr = NULL;
252 	phb->cfg_data = NULL;
253 	phb->private_data = afu;
254 	phb->controller_ops = cxl_pci_controller_ops;
255 
256 	/* Scan the bus */
257 	pcibios_scan_phb(phb);
258 	if (phb->bus == NULL)
259 		return -ENXIO;
260 
261 	/* Set release hook on root bus */
262 	pci_set_host_bridge_release(to_pci_host_bridge(phb->bus->bridge),
263 				    pcibios_free_controller_deferred,
264 				    (void *) phb);
265 
266 	/* Claim resources. This might need some rework as well depending
267 	 * whether we are doing probe-only or not, like assigning unassigned
268 	 * resources etc...
269 	 */
270 	pcibios_claim_one_bus(phb->bus);
271 
272 	/* Add probed PCI devices to the device model */
273 	pci_bus_add_devices(phb->bus);
274 
275 	afu->phb = phb;
276 
277 	return 0;
278 }
279 
280 void cxl_pci_vphb_remove(struct cxl_afu *afu)
281 {
282 	struct pci_controller *phb;
283 
284 	/* If there is no configuration record we won't have one of these */
285 	if (!afu || !afu->phb)
286 		return;
287 
288 	phb = afu->phb;
289 	afu->phb = NULL;
290 
291 	pci_remove_root_bus(phb->bus);
292 	/*
293 	 * We don't free phb here - that's handled by
294 	 * pcibios_free_controller_deferred()
295 	 */
296 }
297 
298 bool cxl_pci_is_vphb_device(struct pci_dev *dev)
299 {
300 	struct pci_controller *phb;
301 
302 	phb = pci_bus_to_host(dev->bus);
303 
304 	return (phb->ops == &cxl_pcie_pci_ops);
305 }
306 
307 struct cxl_afu *cxl_pci_to_afu(struct pci_dev *dev)
308 {
309 	struct pci_controller *phb;
310 
311 	phb = pci_bus_to_host(dev->bus);
312 
313 	return (struct cxl_afu *)phb->private_data;
314 }
315 EXPORT_SYMBOL_GPL(cxl_pci_to_afu);
316 
317 unsigned int cxl_pci_to_cfg_record(struct pci_dev *dev)
318 {
319 	return cxl_pcie_cfg_record(dev->bus->number, dev->devfn);
320 }
321 EXPORT_SYMBOL_GPL(cxl_pci_to_cfg_record);
322