xref: /linux/drivers/misc/cxl/of.c (revision 35fc26541f79071c14f314951c0c7604852c8839)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright 2015 IBM Corp.
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/platform_device.h>
9 #include <linux/slab.h>
10 #include <linux/of_address.h>
11 #include <linux/of_platform.h>
12 
13 #include "cxl.h"
14 
15 
16 static const __be32 *read_prop_string(const struct device_node *np,
17 				const char *prop_name)
18 {
19 	const __be32 *prop;
20 
21 	prop = of_get_property(np, prop_name, NULL);
22 	return prop;
23 }
24 
25 static const __be32 *read_prop_dword(const struct device_node *np,
26 				const char *prop_name, u32 *val)
27 {
28 	const __be32 *prop;
29 
30 	prop = of_get_property(np, prop_name, NULL);
31 	if (prop)
32 		*val = be32_to_cpu(prop[0]);
33 	return prop;
34 }
35 
36 static const __be64 *read_prop64_dword(const struct device_node *np,
37 				const char *prop_name, u64 *val)
38 {
39 	const __be64 *prop;
40 
41 	prop = of_get_property(np, prop_name, NULL);
42 	if (prop)
43 		*val = be64_to_cpu(prop[0]);
44 	return prop;
45 }
46 
47 
48 static int read_handle(struct device_node *np, u64 *handle)
49 {
50 	const __be32 *prop;
51 	u64 size;
52 
53 	/* Get address and size of the node */
54 	prop = of_get_address(np, 0, &size, NULL);
55 	if (size)
56 		return -EINVAL;
57 
58 	/* Helper to read a big number; size is in cells (not bytes) */
59 	*handle = of_read_number(prop, of_n_addr_cells(np));
60 	return 0;
61 }
62 
63 static int read_phys_addr(struct device_node *np, char *prop_name,
64 			struct cxl_afu *afu)
65 {
66 	int i, len, entry_size, naddr, nsize, type;
67 	u64 addr, size;
68 	const __be32 *prop;
69 
70 	naddr = of_n_addr_cells(np);
71 	nsize = of_n_size_cells(np);
72 
73 	prop = of_get_property(np, prop_name, &len);
74 	if (prop) {
75 		entry_size = naddr + nsize;
76 		for (i = 0; i < (len / 4); i += entry_size, prop += entry_size) {
77 			type = be32_to_cpu(prop[0]);
78 			addr = of_read_number(prop, naddr);
79 			size = of_read_number(&prop[naddr], nsize);
80 			switch (type) {
81 			case 0: /* unit address */
82 				afu->guest->handle = addr;
83 				break;
84 			case 1: /* p2 area */
85 				afu->guest->p2n_phys += addr;
86 				afu->guest->p2n_size = size;
87 				break;
88 			case 2: /* problem state area */
89 				afu->psn_phys += addr;
90 				afu->adapter->ps_size = size;
91 				break;
92 			default:
93 				pr_err("Invalid address type %d found in %s property of AFU\n",
94 					type, prop_name);
95 				return -EINVAL;
96 			}
97 		}
98 	}
99 	return 0;
100 }
101 
102 static int read_vpd(struct cxl *adapter, struct cxl_afu *afu)
103 {
104 	char vpd[256];
105 	int rc;
106 	size_t len = sizeof(vpd);
107 
108 	memset(vpd, 0, len);
109 
110 	if (adapter)
111 		rc = cxl_guest_read_adapter_vpd(adapter, vpd, len);
112 	else
113 		rc = cxl_guest_read_afu_vpd(afu, vpd, len);
114 
115 	if (rc > 0) {
116 		cxl_dump_debug_buffer(vpd, rc);
117 		rc = 0;
118 	}
119 	return rc;
120 }
121 
122 int cxl_of_read_afu_handle(struct cxl_afu *afu, struct device_node *afu_np)
123 {
124 	if (read_handle(afu_np, &afu->guest->handle))
125 		return -EINVAL;
126 	pr_devel("AFU handle: 0x%.16llx\n", afu->guest->handle);
127 
128 	return 0;
129 }
130 
131 int cxl_of_read_afu_properties(struct cxl_afu *afu, struct device_node *np)
132 {
133 	int i, rc;
134 	const __be32 *prop;
135 	u16 device_id, vendor_id;
136 	u32 val = 0, class_code;
137 
138 	/* Properties are read in the same order as listed in PAPR */
139 
140 	rc = read_phys_addr(np, "reg", afu);
141 	if (rc)
142 		return rc;
143 
144 	rc = read_phys_addr(np, "assigned-addresses", afu);
145 	if (rc)
146 		return rc;
147 
148 	if (afu->psn_phys == 0)
149 		afu->psa = false;
150 	else
151 		afu->psa = true;
152 
153 	read_prop_dword(np, "ibm,#processes", &afu->max_procs_virtualised);
154 
155 	if (cxl_verbose)
156 		read_vpd(NULL, afu);
157 
158 	read_prop_dword(np, "ibm,max-ints-per-process", &afu->guest->max_ints);
159 	afu->irqs_max = afu->guest->max_ints;
160 
161 	prop = read_prop_dword(np, "ibm,min-ints-per-process", &afu->pp_irqs);
162 	if (prop) {
163 		/* One extra interrupt for the PSL interrupt is already
164 		 * included. Remove it now to keep only AFU interrupts and
165 		 * match the native case.
166 		 */
167 		afu->pp_irqs--;
168 	}
169 
170 	read_prop64_dword(np, "ibm,error-buffer-size", &afu->eb_len);
171 	afu->eb_offset = 0;
172 
173 	read_prop64_dword(np, "ibm,config-record-size", &afu->crs_len);
174 	afu->crs_offset = 0;
175 
176 	read_prop_dword(np, "ibm,#config-records", &afu->crs_num);
177 
178 	if (cxl_verbose) {
179 		for (i = 0; i < afu->crs_num; i++) {
180 			rc = cxl_ops->afu_cr_read16(afu, i, PCI_DEVICE_ID,
181 						&device_id);
182 			if (!rc)
183 				pr_info("record %d - device-id: %#x\n",
184 					i, device_id);
185 			rc = cxl_ops->afu_cr_read16(afu, i, PCI_VENDOR_ID,
186 						&vendor_id);
187 			if (!rc)
188 				pr_info("record %d - vendor-id: %#x\n",
189 					i, vendor_id);
190 			rc = cxl_ops->afu_cr_read32(afu, i, PCI_CLASS_REVISION,
191 						&class_code);
192 			if (!rc) {
193 				class_code >>= 8;
194 				pr_info("record %d - class-code: %#x\n",
195 					i, class_code);
196 			}
197 		}
198 	}
199 	/*
200 	 * if "ibm,process-mmio" doesn't exist then per-process mmio is
201 	 * not supported
202 	 */
203 	val = 0;
204 	prop = read_prop_dword(np, "ibm,process-mmio", &val);
205 	if (prop && val == 1)
206 		afu->pp_psa = true;
207 	else
208 		afu->pp_psa = false;
209 
210 	prop = read_prop_dword(np, "ibm,function-error-interrupt", &val);
211 	if (prop)
212 		afu->serr_hwirq = val;
213 
214 	pr_devel("AFU handle: %#llx\n", afu->guest->handle);
215 	pr_devel("p2n_phys: %#llx (size %#llx)\n",
216 		afu->guest->p2n_phys, afu->guest->p2n_size);
217 	pr_devel("psn_phys: %#llx (size %#llx)\n",
218 		afu->psn_phys, afu->adapter->ps_size);
219 	pr_devel("Max number of processes virtualised=%i\n",
220 		afu->max_procs_virtualised);
221 	pr_devel("Per-process irqs min=%i, max=%i\n", afu->pp_irqs,
222 		 afu->irqs_max);
223 	pr_devel("Slice error interrupt=%#lx\n", afu->serr_hwirq);
224 
225 	return 0;
226 }
227 
228 static int read_adapter_irq_config(struct cxl *adapter, struct device_node *np)
229 {
230 	const __be32 *ranges;
231 	int len, nranges, i;
232 	struct irq_avail *cur;
233 
234 	ranges = of_get_property(np, "interrupt-ranges", &len);
235 	if (ranges == NULL || len < (2 * sizeof(int)))
236 		return -EINVAL;
237 
238 	/*
239 	 * encoded array of two cells per entry, each cell encoded as
240 	 * with encode-int
241 	 */
242 	nranges = len / (2 * sizeof(int));
243 	if (nranges == 0 || (nranges * 2 * sizeof(int)) != len)
244 		return -EINVAL;
245 
246 	adapter->guest->irq_avail = kcalloc(nranges, sizeof(struct irq_avail),
247 					    GFP_KERNEL);
248 	if (adapter->guest->irq_avail == NULL)
249 		return -ENOMEM;
250 
251 	adapter->guest->irq_base_offset = be32_to_cpu(ranges[0]);
252 	for (i = 0; i < nranges; i++) {
253 		cur = &adapter->guest->irq_avail[i];
254 		cur->offset = be32_to_cpu(ranges[i * 2]);
255 		cur->range  = be32_to_cpu(ranges[i * 2 + 1]);
256 		cur->bitmap = bitmap_zalloc(cur->range, GFP_KERNEL);
257 		if (cur->bitmap == NULL)
258 			goto err;
259 		if (cur->offset < adapter->guest->irq_base_offset)
260 			adapter->guest->irq_base_offset = cur->offset;
261 		if (cxl_verbose)
262 			pr_info("available IRQ range: %#lx-%#lx (%lu)\n",
263 				cur->offset, cur->offset + cur->range - 1,
264 				cur->range);
265 	}
266 	adapter->guest->irq_nranges = nranges;
267 	spin_lock_init(&adapter->guest->irq_alloc_lock);
268 
269 	return 0;
270 err:
271 	for (i--; i >= 0; i--) {
272 		cur = &adapter->guest->irq_avail[i];
273 		bitmap_free(cur->bitmap);
274 	}
275 	kfree(adapter->guest->irq_avail);
276 	adapter->guest->irq_avail = NULL;
277 	return -ENOMEM;
278 }
279 
280 int cxl_of_read_adapter_handle(struct cxl *adapter, struct device_node *np)
281 {
282 	if (read_handle(np, &adapter->guest->handle))
283 		return -EINVAL;
284 	pr_devel("Adapter handle: 0x%.16llx\n", adapter->guest->handle);
285 
286 	return 0;
287 }
288 
289 int cxl_of_read_adapter_properties(struct cxl *adapter, struct device_node *np)
290 {
291 	int rc;
292 	const __be32 *prop;
293 	u32 val = 0;
294 
295 	/* Properties are read in the same order as listed in PAPR */
296 
297 	if ((rc = read_adapter_irq_config(adapter, np)))
298 		return rc;
299 
300 	prop = read_prop_dword(np, "ibm,caia-version", &val);
301 	if (prop) {
302 		adapter->caia_major = (val & 0xFF00) >> 8;
303 		adapter->caia_minor = val & 0xFF;
304 	}
305 
306 	prop = read_prop_dword(np, "ibm,psl-revision", &val);
307 	if (prop)
308 		adapter->psl_rev = val;
309 
310 	prop = read_prop_string(np, "status");
311 	if (prop) {
312 		adapter->guest->status = kasprintf(GFP_KERNEL, "%s", (char *) prop);
313 		if (adapter->guest->status == NULL)
314 			return -ENOMEM;
315 	}
316 
317 	prop = read_prop_dword(np, "vendor-id", &val);
318 	if (prop)
319 		adapter->guest->vendor = val;
320 
321 	prop = read_prop_dword(np, "device-id", &val);
322 	if (prop)
323 		adapter->guest->device = val;
324 
325 	prop = read_prop_dword(np, "subsystem-vendor-id", &val);
326 	if (prop)
327 		adapter->guest->subsystem_vendor = val;
328 
329 	prop = read_prop_dword(np, "subsystem-id", &val);
330 	if (prop)
331 		adapter->guest->subsystem = val;
332 
333 	if (cxl_verbose)
334 		read_vpd(adapter, NULL);
335 
336 	return 0;
337 }
338 
339 static void cxl_of_remove(struct platform_device *pdev)
340 {
341 	struct cxl *adapter;
342 	int afu;
343 
344 	adapter = dev_get_drvdata(&pdev->dev);
345 	for (afu = 0; afu < adapter->slices; afu++)
346 		cxl_guest_remove_afu(adapter->afu[afu]);
347 
348 	cxl_guest_remove_adapter(adapter);
349 }
350 
351 static void cxl_of_shutdown(struct platform_device *pdev)
352 {
353 	cxl_of_remove(pdev);
354 }
355 
356 int cxl_of_probe(struct platform_device *pdev)
357 {
358 	struct device_node *np = NULL;
359 	struct device_node *afu_np = NULL;
360 	struct cxl *adapter = NULL;
361 	int ret;
362 	int slice = 0, slice_ok = 0;
363 
364 	pr_devel("in %s\n", __func__);
365 
366 	np = pdev->dev.of_node;
367 	if (np == NULL)
368 		return -ENODEV;
369 
370 	/* init adapter */
371 	adapter = cxl_guest_init_adapter(np, pdev);
372 	if (IS_ERR(adapter)) {
373 		dev_err(&pdev->dev, "guest_init_adapter failed: %li\n", PTR_ERR(adapter));
374 		return PTR_ERR(adapter);
375 	}
376 
377 	/* init afu */
378 	for_each_child_of_node(np, afu_np) {
379 		if ((ret = cxl_guest_init_afu(adapter, slice, afu_np)))
380 			dev_err(&pdev->dev, "AFU %i failed to initialise: %i\n",
381 				slice, ret);
382 		else
383 			slice_ok++;
384 		slice++;
385 	}
386 
387 	if (slice_ok == 0) {
388 		dev_info(&pdev->dev, "No active AFU");
389 		adapter->slices = 0;
390 	}
391 
392 	return 0;
393 }
394 
395 static const struct of_device_id cxl_of_match[] = {
396 	{ .compatible = "ibm,coherent-platform-facility",},
397 	{},
398 };
399 MODULE_DEVICE_TABLE(of, cxl_of_match);
400 
401 struct platform_driver cxl_of_driver = {
402 	.driver = {
403 		.name = "cxl_of",
404 		.of_match_table = cxl_of_match,
405 		.owner = THIS_MODULE
406 	},
407 	.probe = cxl_of_probe,
408 	.remove_new = cxl_of_remove,
409 	.shutdown = cxl_of_shutdown,
410 };
411