Linux Audio

Check our new training course

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