xref: /linux/drivers/cxl/acpi.c (revision f2d14bc4e437b8ed21e6890ae047a6ec47c030d9)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2021 Intel Corporation. All rights reserved. */
3 #include <linux/platform_device.h>
4 #include <linux/module.h>
5 #include <linux/device.h>
6 #include <linux/kernel.h>
7 #include <linux/acpi.h>
8 #include <linux/pci.h>
9 #include <asm/div64.h>
10 #include "cxlpci.h"
11 #include "cxl.h"
12 
13 #define CXL_RCRB_SIZE	SZ_8K
14 
15 struct cxl_cxims_data {
16 	int nr_maps;
17 	u64 xormaps[];
18 };
19 
20 /*
21  * Find a targets entry (n) in the host bridge interleave list.
22  * CXL Specification 3.0 Table 9-22
23  */
24 static int cxl_xor_calc_n(u64 hpa, struct cxl_cxims_data *cximsd, int iw,
25 			  int ig)
26 {
27 	int i = 0, n = 0;
28 	u8 eiw;
29 
30 	/* IW: 2,4,6,8,12,16 begin building 'n' using xormaps */
31 	if (iw != 3) {
32 		for (i = 0; i < cximsd->nr_maps; i++)
33 			n |= (hweight64(hpa & cximsd->xormaps[i]) & 1) << i;
34 	}
35 	/* IW: 3,6,12 add a modulo calculation to 'n' */
36 	if (!is_power_of_2(iw)) {
37 		if (ways_to_eiw(iw, &eiw))
38 			return -1;
39 		hpa &= GENMASK_ULL(51, eiw + ig);
40 		n |= do_div(hpa, 3) << i;
41 	}
42 	return n;
43 }
44 
45 static struct cxl_dport *cxl_hb_xor(struct cxl_root_decoder *cxlrd, int pos)
46 {
47 	struct cxl_cxims_data *cximsd = cxlrd->platform_data;
48 	struct cxl_switch_decoder *cxlsd = &cxlrd->cxlsd;
49 	struct cxl_decoder *cxld = &cxlsd->cxld;
50 	int ig = cxld->interleave_granularity;
51 	int iw = cxld->interleave_ways;
52 	int n = 0;
53 	u64 hpa;
54 
55 	if (dev_WARN_ONCE(&cxld->dev,
56 			  cxld->interleave_ways != cxlsd->nr_targets,
57 			  "misconfigured root decoder\n"))
58 		return NULL;
59 
60 	hpa = cxlrd->res->start + pos * ig;
61 
62 	/* Entry (n) is 0 for no interleave (iw == 1) */
63 	if (iw != 1)
64 		n = cxl_xor_calc_n(hpa, cximsd, iw, ig);
65 
66 	if (n < 0)
67 		return NULL;
68 
69 	return cxlrd->cxlsd.target[n];
70 }
71 
72 struct cxl_cxims_context {
73 	struct device *dev;
74 	struct cxl_root_decoder *cxlrd;
75 };
76 
77 static int cxl_parse_cxims(union acpi_subtable_headers *header, void *arg,
78 			   const unsigned long end)
79 {
80 	struct acpi_cedt_cxims *cxims = (struct acpi_cedt_cxims *)header;
81 	struct cxl_cxims_context *ctx = arg;
82 	struct cxl_root_decoder *cxlrd = ctx->cxlrd;
83 	struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
84 	struct device *dev = ctx->dev;
85 	struct cxl_cxims_data *cximsd;
86 	unsigned int hbig, nr_maps;
87 	int rc;
88 
89 	rc = eig_to_granularity(cxims->hbig, &hbig);
90 	if (rc)
91 		return rc;
92 
93 	/* Does this CXIMS entry apply to the given CXL Window? */
94 	if (hbig != cxld->interleave_granularity)
95 		return 0;
96 
97 	/* IW 1,3 do not use xormaps and skip this parsing entirely */
98 	if (is_power_of_2(cxld->interleave_ways))
99 		/* 2, 4, 8, 16 way */
100 		nr_maps = ilog2(cxld->interleave_ways);
101 	else
102 		/* 6, 12 way */
103 		nr_maps = ilog2(cxld->interleave_ways / 3);
104 
105 	if (cxims->nr_xormaps < nr_maps) {
106 		dev_dbg(dev, "CXIMS nr_xormaps[%d] expected[%d]\n",
107 			cxims->nr_xormaps, nr_maps);
108 		return -ENXIO;
109 	}
110 
111 	cximsd = devm_kzalloc(dev, struct_size(cximsd, xormaps, nr_maps),
112 			      GFP_KERNEL);
113 	if (!cximsd)
114 		return -ENOMEM;
115 	memcpy(cximsd->xormaps, cxims->xormap_list,
116 	       nr_maps * sizeof(*cximsd->xormaps));
117 	cximsd->nr_maps = nr_maps;
118 	cxlrd->platform_data = cximsd;
119 
120 	return 0;
121 }
122 
123 static unsigned long cfmws_to_decoder_flags(int restrictions)
124 {
125 	unsigned long flags = CXL_DECODER_F_ENABLE;
126 
127 	if (restrictions & ACPI_CEDT_CFMWS_RESTRICT_TYPE2)
128 		flags |= CXL_DECODER_F_TYPE2;
129 	if (restrictions & ACPI_CEDT_CFMWS_RESTRICT_TYPE3)
130 		flags |= CXL_DECODER_F_TYPE3;
131 	if (restrictions & ACPI_CEDT_CFMWS_RESTRICT_VOLATILE)
132 		flags |= CXL_DECODER_F_RAM;
133 	if (restrictions & ACPI_CEDT_CFMWS_RESTRICT_PMEM)
134 		flags |= CXL_DECODER_F_PMEM;
135 	if (restrictions & ACPI_CEDT_CFMWS_RESTRICT_FIXED)
136 		flags |= CXL_DECODER_F_LOCK;
137 
138 	return flags;
139 }
140 
141 static int cxl_acpi_cfmws_verify(struct device *dev,
142 				 struct acpi_cedt_cfmws *cfmws)
143 {
144 	int rc, expected_len;
145 	unsigned int ways;
146 
147 	if (cfmws->interleave_arithmetic != ACPI_CEDT_CFMWS_ARITHMETIC_MODULO &&
148 	    cfmws->interleave_arithmetic != ACPI_CEDT_CFMWS_ARITHMETIC_XOR) {
149 		dev_err(dev, "CFMWS Unknown Interleave Arithmetic: %d\n",
150 			cfmws->interleave_arithmetic);
151 		return -EINVAL;
152 	}
153 
154 	if (!IS_ALIGNED(cfmws->base_hpa, SZ_256M)) {
155 		dev_err(dev, "CFMWS Base HPA not 256MB aligned\n");
156 		return -EINVAL;
157 	}
158 
159 	if (!IS_ALIGNED(cfmws->window_size, SZ_256M)) {
160 		dev_err(dev, "CFMWS Window Size not 256MB aligned\n");
161 		return -EINVAL;
162 	}
163 
164 	rc = eiw_to_ways(cfmws->interleave_ways, &ways);
165 	if (rc) {
166 		dev_err(dev, "CFMWS Interleave Ways (%d) invalid\n",
167 			cfmws->interleave_ways);
168 		return -EINVAL;
169 	}
170 
171 	expected_len = struct_size(cfmws, interleave_targets, ways);
172 
173 	if (cfmws->header.length < expected_len) {
174 		dev_err(dev, "CFMWS length %d less than expected %d\n",
175 			cfmws->header.length, expected_len);
176 		return -EINVAL;
177 	}
178 
179 	if (cfmws->header.length > expected_len)
180 		dev_dbg(dev, "CFMWS length %d greater than expected %d\n",
181 			cfmws->header.length, expected_len);
182 
183 	return 0;
184 }
185 
186 /*
187  * Note, @dev must be the first member, see 'struct cxl_chbs_context'
188  * and mock_acpi_table_parse_cedt()
189  */
190 struct cxl_cfmws_context {
191 	struct device *dev;
192 	struct cxl_port *root_port;
193 	struct resource *cxl_res;
194 	int id;
195 };
196 
197 static int cxl_parse_cfmws(union acpi_subtable_headers *header, void *arg,
198 			   const unsigned long end)
199 {
200 	int target_map[CXL_DECODER_MAX_INTERLEAVE];
201 	struct cxl_cfmws_context *ctx = arg;
202 	struct cxl_port *root_port = ctx->root_port;
203 	struct resource *cxl_res = ctx->cxl_res;
204 	struct cxl_cxims_context cxims_ctx;
205 	struct cxl_root_decoder *cxlrd;
206 	struct device *dev = ctx->dev;
207 	struct acpi_cedt_cfmws *cfmws;
208 	cxl_calc_hb_fn cxl_calc_hb;
209 	struct cxl_decoder *cxld;
210 	unsigned int ways, i, ig;
211 	struct resource *res;
212 	int rc;
213 
214 	cfmws = (struct acpi_cedt_cfmws *) header;
215 
216 	rc = cxl_acpi_cfmws_verify(dev, cfmws);
217 	if (rc) {
218 		dev_err(dev, "CFMWS range %#llx-%#llx not registered\n",
219 			cfmws->base_hpa,
220 			cfmws->base_hpa + cfmws->window_size - 1);
221 		return 0;
222 	}
223 
224 	rc = eiw_to_ways(cfmws->interleave_ways, &ways);
225 	if (rc)
226 		return rc;
227 	rc = eig_to_granularity(cfmws->granularity, &ig);
228 	if (rc)
229 		return rc;
230 	for (i = 0; i < ways; i++)
231 		target_map[i] = cfmws->interleave_targets[i];
232 
233 	res = kzalloc(sizeof(*res), GFP_KERNEL);
234 	if (!res)
235 		return -ENOMEM;
236 
237 	res->name = kasprintf(GFP_KERNEL, "CXL Window %d", ctx->id++);
238 	if (!res->name)
239 		goto err_name;
240 
241 	res->start = cfmws->base_hpa;
242 	res->end = cfmws->base_hpa + cfmws->window_size - 1;
243 	res->flags = IORESOURCE_MEM;
244 
245 	/* add to the local resource tracking to establish a sort order */
246 	rc = insert_resource(cxl_res, res);
247 	if (rc)
248 		goto err_insert;
249 
250 	if (cfmws->interleave_arithmetic == ACPI_CEDT_CFMWS_ARITHMETIC_MODULO)
251 		cxl_calc_hb = cxl_hb_modulo;
252 	else
253 		cxl_calc_hb = cxl_hb_xor;
254 
255 	cxlrd = cxl_root_decoder_alloc(root_port, ways, cxl_calc_hb);
256 	if (IS_ERR(cxlrd))
257 		return 0;
258 
259 	cxld = &cxlrd->cxlsd.cxld;
260 	cxld->flags = cfmws_to_decoder_flags(cfmws->restrictions);
261 	cxld->target_type = CXL_DECODER_HOSTONLYMEM;
262 	cxld->hpa_range = (struct range) {
263 		.start = res->start,
264 		.end = res->end,
265 	};
266 	cxld->interleave_ways = ways;
267 	/*
268 	 * Minimize the x1 granularity to advertise support for any
269 	 * valid region granularity
270 	 */
271 	if (ways == 1)
272 		ig = CXL_DECODER_MIN_GRANULARITY;
273 	cxld->interleave_granularity = ig;
274 
275 	if (cfmws->interleave_arithmetic == ACPI_CEDT_CFMWS_ARITHMETIC_XOR) {
276 		if (ways != 1 && ways != 3) {
277 			cxims_ctx = (struct cxl_cxims_context) {
278 				.dev = dev,
279 				.cxlrd = cxlrd,
280 			};
281 			rc = acpi_table_parse_cedt(ACPI_CEDT_TYPE_CXIMS,
282 						   cxl_parse_cxims, &cxims_ctx);
283 			if (rc < 0)
284 				goto err_xormap;
285 			if (!cxlrd->platform_data) {
286 				dev_err(dev, "No CXIMS for HBIG %u\n", ig);
287 				rc = -EINVAL;
288 				goto err_xormap;
289 			}
290 		}
291 	}
292 	rc = cxl_decoder_add(cxld, target_map);
293 err_xormap:
294 	if (rc)
295 		put_device(&cxld->dev);
296 	else
297 		rc = cxl_decoder_autoremove(dev, cxld);
298 	if (rc) {
299 		dev_err(dev, "Failed to add decode range [%#llx - %#llx]\n",
300 			cxld->hpa_range.start, cxld->hpa_range.end);
301 		return 0;
302 	}
303 	dev_dbg(dev, "add: %s node: %d range [%#llx - %#llx]\n",
304 		dev_name(&cxld->dev),
305 		phys_to_target_node(cxld->hpa_range.start),
306 		cxld->hpa_range.start, cxld->hpa_range.end);
307 
308 	return 0;
309 
310 err_insert:
311 	kfree(res->name);
312 err_name:
313 	kfree(res);
314 	return -ENOMEM;
315 }
316 
317 __mock struct acpi_device *to_cxl_host_bridge(struct device *host,
318 					      struct device *dev)
319 {
320 	struct acpi_device *adev = to_acpi_device(dev);
321 
322 	if (!acpi_pci_find_root(adev->handle))
323 		return NULL;
324 
325 	if (strcmp(acpi_device_hid(adev), "ACPI0016") == 0)
326 		return adev;
327 	return NULL;
328 }
329 
330 /* Note, @dev is used by mock_acpi_table_parse_cedt() */
331 struct cxl_chbs_context {
332 	struct device *dev;
333 	unsigned long long uid;
334 	resource_size_t base;
335 	u32 cxl_version;
336 };
337 
338 static int cxl_get_chbs_iter(union acpi_subtable_headers *header, void *arg,
339 			     const unsigned long end)
340 {
341 	struct cxl_chbs_context *ctx = arg;
342 	struct acpi_cedt_chbs *chbs;
343 
344 	if (ctx->base != CXL_RESOURCE_NONE)
345 		return 0;
346 
347 	chbs = (struct acpi_cedt_chbs *) header;
348 
349 	if (ctx->uid != chbs->uid)
350 		return 0;
351 
352 	ctx->cxl_version = chbs->cxl_version;
353 	if (!chbs->base)
354 		return 0;
355 
356 	if (chbs->cxl_version == ACPI_CEDT_CHBS_VERSION_CXL11 &&
357 	    chbs->length != CXL_RCRB_SIZE)
358 		return 0;
359 
360 	ctx->base = chbs->base;
361 
362 	return 0;
363 }
364 
365 static int cxl_get_chbs(struct device *dev, struct acpi_device *hb,
366 			struct cxl_chbs_context *ctx)
367 {
368 	unsigned long long uid;
369 	int rc;
370 
371 	rc = acpi_evaluate_integer(hb->handle, METHOD_NAME__UID, NULL, &uid);
372 	if (rc != AE_OK) {
373 		dev_err(dev, "unable to retrieve _UID\n");
374 		return -ENOENT;
375 	}
376 
377 	dev_dbg(dev, "UID found: %lld\n", uid);
378 	*ctx = (struct cxl_chbs_context) {
379 		.dev = dev,
380 		.uid = uid,
381 		.base = CXL_RESOURCE_NONE,
382 		.cxl_version = UINT_MAX,
383 	};
384 
385 	acpi_table_parse_cedt(ACPI_CEDT_TYPE_CHBS, cxl_get_chbs_iter, ctx);
386 
387 	return 0;
388 }
389 
390 static int add_host_bridge_dport(struct device *match, void *arg)
391 {
392 	acpi_status rc;
393 	struct device *bridge;
394 	struct cxl_dport *dport;
395 	struct cxl_chbs_context ctx;
396 	struct acpi_pci_root *pci_root;
397 	struct cxl_port *root_port = arg;
398 	struct device *host = root_port->dev.parent;
399 	struct acpi_device *hb = to_cxl_host_bridge(host, match);
400 
401 	if (!hb)
402 		return 0;
403 
404 	rc = cxl_get_chbs(match, hb, &ctx);
405 	if (rc)
406 		return rc;
407 
408 	if (ctx.cxl_version == UINT_MAX) {
409 		dev_warn(match, "No CHBS found for Host Bridge (UID %lld)\n",
410 			 ctx.uid);
411 		return 0;
412 	}
413 
414 	if (ctx.base == CXL_RESOURCE_NONE) {
415 		dev_warn(match, "CHBS invalid for Host Bridge (UID %lld)\n",
416 			 ctx.uid);
417 		return 0;
418 	}
419 
420 	pci_root = acpi_pci_find_root(hb->handle);
421 	bridge = pci_root->bus->bridge;
422 
423 	/*
424 	 * In RCH mode, bind the component regs base to the dport. In
425 	 * VH mode it will be bound to the CXL host bridge's port
426 	 * object later in add_host_bridge_uport().
427 	 */
428 	if (ctx.cxl_version == ACPI_CEDT_CHBS_VERSION_CXL11) {
429 		dev_dbg(match, "RCRB found for UID %lld: %pa\n", ctx.uid,
430 			&ctx.base);
431 		dport = devm_cxl_add_rch_dport(root_port, bridge, ctx.uid,
432 					       ctx.base);
433 	} else {
434 		dport = devm_cxl_add_dport(root_port, bridge, ctx.uid,
435 					   CXL_RESOURCE_NONE);
436 	}
437 
438 	if (IS_ERR(dport))
439 		return PTR_ERR(dport);
440 
441 	return 0;
442 }
443 
444 /*
445  * A host bridge is a dport to a CFMWS decode and it is a uport to the
446  * dport (PCIe Root Ports) in the host bridge.
447  */
448 static int add_host_bridge_uport(struct device *match, void *arg)
449 {
450 	struct cxl_port *root_port = arg;
451 	struct device *host = root_port->dev.parent;
452 	struct acpi_device *hb = to_cxl_host_bridge(host, match);
453 	struct acpi_pci_root *pci_root;
454 	struct cxl_dport *dport;
455 	struct cxl_port *port;
456 	struct device *bridge;
457 	struct cxl_chbs_context ctx;
458 	resource_size_t component_reg_phys;
459 	int rc;
460 
461 	if (!hb)
462 		return 0;
463 
464 	pci_root = acpi_pci_find_root(hb->handle);
465 	bridge = pci_root->bus->bridge;
466 	dport = cxl_find_dport_by_dev(root_port, bridge);
467 	if (!dport) {
468 		dev_dbg(host, "host bridge expected and not found\n");
469 		return 0;
470 	}
471 
472 	if (dport->rch) {
473 		dev_info(bridge, "host supports CXL (restricted)\n");
474 		return 0;
475 	}
476 
477 	rc = cxl_get_chbs(match, hb, &ctx);
478 	if (rc)
479 		return rc;
480 
481 	if (ctx.cxl_version == ACPI_CEDT_CHBS_VERSION_CXL11) {
482 		dev_warn(bridge,
483 			 "CXL CHBS version mismatch, skip port registration\n");
484 		return 0;
485 	}
486 
487 	component_reg_phys = ctx.base;
488 	if (component_reg_phys != CXL_RESOURCE_NONE)
489 		dev_dbg(match, "CHBCR found for UID %lld: %pa\n",
490 			ctx.uid, &component_reg_phys);
491 
492 	rc = devm_cxl_register_pci_bus(host, bridge, pci_root->bus);
493 	if (rc)
494 		return rc;
495 
496 	port = devm_cxl_add_port(host, bridge, component_reg_phys, dport);
497 	if (IS_ERR(port))
498 		return PTR_ERR(port);
499 
500 	dev_info(bridge, "host supports CXL\n");
501 
502 	return 0;
503 }
504 
505 static int add_root_nvdimm_bridge(struct device *match, void *data)
506 {
507 	struct cxl_decoder *cxld;
508 	struct cxl_port *root_port = data;
509 	struct cxl_nvdimm_bridge *cxl_nvb;
510 	struct device *host = root_port->dev.parent;
511 
512 	if (!is_root_decoder(match))
513 		return 0;
514 
515 	cxld = to_cxl_decoder(match);
516 	if (!(cxld->flags & CXL_DECODER_F_PMEM))
517 		return 0;
518 
519 	cxl_nvb = devm_cxl_add_nvdimm_bridge(host, root_port);
520 	if (IS_ERR(cxl_nvb)) {
521 		dev_dbg(host, "failed to register pmem\n");
522 		return PTR_ERR(cxl_nvb);
523 	}
524 	dev_dbg(host, "%s: add: %s\n", dev_name(&root_port->dev),
525 		dev_name(&cxl_nvb->dev));
526 	return 1;
527 }
528 
529 static struct lock_class_key cxl_root_key;
530 
531 static void cxl_acpi_lock_reset_class(void *dev)
532 {
533 	device_lock_reset_class(dev);
534 }
535 
536 static void del_cxl_resource(struct resource *res)
537 {
538 	kfree(res->name);
539 	kfree(res);
540 }
541 
542 static void cxl_set_public_resource(struct resource *priv, struct resource *pub)
543 {
544 	priv->desc = (unsigned long) pub;
545 }
546 
547 static struct resource *cxl_get_public_resource(struct resource *priv)
548 {
549 	return (struct resource *) priv->desc;
550 }
551 
552 static void remove_cxl_resources(void *data)
553 {
554 	struct resource *res, *next, *cxl = data;
555 
556 	for (res = cxl->child; res; res = next) {
557 		struct resource *victim = cxl_get_public_resource(res);
558 
559 		next = res->sibling;
560 		remove_resource(res);
561 
562 		if (victim) {
563 			remove_resource(victim);
564 			kfree(victim);
565 		}
566 
567 		del_cxl_resource(res);
568 	}
569 }
570 
571 /**
572  * add_cxl_resources() - reflect CXL fixed memory windows in iomem_resource
573  * @cxl_res: A standalone resource tree where each CXL window is a sibling
574  *
575  * Walk each CXL window in @cxl_res and add it to iomem_resource potentially
576  * expanding its boundaries to ensure that any conflicting resources become
577  * children. If a window is expanded it may then conflict with a another window
578  * entry and require the window to be truncated or trimmed. Consider this
579  * situation:
580  *
581  * |-- "CXL Window 0" --||----- "CXL Window 1" -----|
582  * |--------------- "System RAM" -------------|
583  *
584  * ...where platform firmware has established as System RAM resource across 2
585  * windows, but has left some portion of window 1 for dynamic CXL region
586  * provisioning. In this case "Window 0" will span the entirety of the "System
587  * RAM" span, and "CXL Window 1" is truncated to the remaining tail past the end
588  * of that "System RAM" resource.
589  */
590 static int add_cxl_resources(struct resource *cxl_res)
591 {
592 	struct resource *res, *new, *next;
593 
594 	for (res = cxl_res->child; res; res = next) {
595 		new = kzalloc(sizeof(*new), GFP_KERNEL);
596 		if (!new)
597 			return -ENOMEM;
598 		new->name = res->name;
599 		new->start = res->start;
600 		new->end = res->end;
601 		new->flags = IORESOURCE_MEM;
602 		new->desc = IORES_DESC_CXL;
603 
604 		/*
605 		 * Record the public resource in the private cxl_res tree for
606 		 * later removal.
607 		 */
608 		cxl_set_public_resource(res, new);
609 
610 		insert_resource_expand_to_fit(&iomem_resource, new);
611 
612 		next = res->sibling;
613 		while (next && resource_overlaps(new, next)) {
614 			if (resource_contains(new, next)) {
615 				struct resource *_next = next->sibling;
616 
617 				remove_resource(next);
618 				del_cxl_resource(next);
619 				next = _next;
620 			} else
621 				next->start = new->end + 1;
622 		}
623 	}
624 	return 0;
625 }
626 
627 static int pair_cxl_resource(struct device *dev, void *data)
628 {
629 	struct resource *cxl_res = data;
630 	struct resource *p;
631 
632 	if (!is_root_decoder(dev))
633 		return 0;
634 
635 	for (p = cxl_res->child; p; p = p->sibling) {
636 		struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
637 		struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
638 		struct resource res = {
639 			.start = cxld->hpa_range.start,
640 			.end = cxld->hpa_range.end,
641 			.flags = IORESOURCE_MEM,
642 		};
643 
644 		if (resource_contains(p, &res)) {
645 			cxlrd->res = cxl_get_public_resource(p);
646 			break;
647 		}
648 	}
649 
650 	return 0;
651 }
652 
653 static int cxl_acpi_probe(struct platform_device *pdev)
654 {
655 	int rc;
656 	struct resource *cxl_res;
657 	struct cxl_port *root_port;
658 	struct device *host = &pdev->dev;
659 	struct acpi_device *adev = ACPI_COMPANION(host);
660 	struct cxl_cfmws_context ctx;
661 
662 	device_lock_set_class(&pdev->dev, &cxl_root_key);
663 	rc = devm_add_action_or_reset(&pdev->dev, cxl_acpi_lock_reset_class,
664 				      &pdev->dev);
665 	if (rc)
666 		return rc;
667 
668 	cxl_res = devm_kzalloc(host, sizeof(*cxl_res), GFP_KERNEL);
669 	if (!cxl_res)
670 		return -ENOMEM;
671 	cxl_res->name = "CXL mem";
672 	cxl_res->start = 0;
673 	cxl_res->end = -1;
674 	cxl_res->flags = IORESOURCE_MEM;
675 
676 	root_port = devm_cxl_add_port(host, host, CXL_RESOURCE_NONE, NULL);
677 	if (IS_ERR(root_port))
678 		return PTR_ERR(root_port);
679 
680 	rc = bus_for_each_dev(adev->dev.bus, NULL, root_port,
681 			      add_host_bridge_dport);
682 	if (rc < 0)
683 		return rc;
684 
685 	rc = devm_add_action_or_reset(host, remove_cxl_resources, cxl_res);
686 	if (rc)
687 		return rc;
688 
689 	ctx = (struct cxl_cfmws_context) {
690 		.dev = host,
691 		.root_port = root_port,
692 		.cxl_res = cxl_res,
693 	};
694 	rc = acpi_table_parse_cedt(ACPI_CEDT_TYPE_CFMWS, cxl_parse_cfmws, &ctx);
695 	if (rc < 0)
696 		return -ENXIO;
697 
698 	rc = add_cxl_resources(cxl_res);
699 	if (rc)
700 		return rc;
701 
702 	/*
703 	 * Populate the root decoders with their related iomem resource,
704 	 * if present
705 	 */
706 	device_for_each_child(&root_port->dev, cxl_res, pair_cxl_resource);
707 
708 	/*
709 	 * Root level scanned with host-bridge as dports, now scan host-bridges
710 	 * for their role as CXL uports to their CXL-capable PCIe Root Ports.
711 	 */
712 	rc = bus_for_each_dev(adev->dev.bus, NULL, root_port,
713 			      add_host_bridge_uport);
714 	if (rc < 0)
715 		return rc;
716 
717 	if (IS_ENABLED(CONFIG_CXL_PMEM))
718 		rc = device_for_each_child(&root_port->dev, root_port,
719 					   add_root_nvdimm_bridge);
720 	if (rc < 0)
721 		return rc;
722 
723 	/* In case PCI is scanned before ACPI re-trigger memdev attach */
724 	cxl_bus_rescan();
725 	return 0;
726 }
727 
728 static const struct acpi_device_id cxl_acpi_ids[] = {
729 	{ "ACPI0017" },
730 	{ },
731 };
732 MODULE_DEVICE_TABLE(acpi, cxl_acpi_ids);
733 
734 static const struct platform_device_id cxl_test_ids[] = {
735 	{ "cxl_acpi" },
736 	{ },
737 };
738 MODULE_DEVICE_TABLE(platform, cxl_test_ids);
739 
740 static struct platform_driver cxl_acpi_driver = {
741 	.probe = cxl_acpi_probe,
742 	.driver = {
743 		.name = KBUILD_MODNAME,
744 		.acpi_match_table = cxl_acpi_ids,
745 	},
746 	.id_table = cxl_test_ids,
747 };
748 
749 static int __init cxl_acpi_init(void)
750 {
751 	return platform_driver_register(&cxl_acpi_driver);
752 }
753 
754 static void __exit cxl_acpi_exit(void)
755 {
756 	platform_driver_unregister(&cxl_acpi_driver);
757 	cxl_bus_drain();
758 }
759 
760 /* load before dax_hmem sees 'Soft Reserved' CXL ranges */
761 subsys_initcall(cxl_acpi_init);
762 module_exit(cxl_acpi_exit);
763 MODULE_LICENSE("GPL v2");
764 MODULE_IMPORT_NS(CXL);
765 MODULE_IMPORT_NS(ACPI);
766