1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright(c) 2021 Intel Corporation. All rights reserved.
3
4 #include <linux/platform_device.h>
5 #include <linux/memory_hotplug.h>
6 #include <linux/genalloc.h>
7 #include <linux/module.h>
8 #include <linux/mutex.h>
9 #include <linux/acpi.h>
10 #include <linux/pci.h>
11 #include <linux/mm.h>
12 #include <cxlmem.h>
13
14 #include "../watermark.h"
15 #include "mock.h"
16
17 static int interleave_arithmetic;
18 static bool extended_linear_cache;
19 static bool fail_autoassemble;
20
21 #define FAKE_QTG_ID 42
22
23 #define NR_CXL_HOST_BRIDGES 2
24 #define NR_CXL_SINGLE_HOST 1
25 #define NR_CXL_RCH 1
26 #define NR_CXL_ROOT_PORTS 2
27 #define NR_CXL_SWITCH_PORTS 2
28 #define NR_CXL_PORT_DECODERS 8
29 #define NR_BRIDGES (NR_CXL_HOST_BRIDGES + NR_CXL_SINGLE_HOST + NR_CXL_RCH)
30
31 #define MOCK_AUTO_REGION_SIZE_DEFAULT SZ_512M
32 static int mock_auto_region_size = MOCK_AUTO_REGION_SIZE_DEFAULT;
33
34 static struct platform_device *cxl_acpi;
35 static struct platform_device *cxl_host_bridge[NR_CXL_HOST_BRIDGES];
36 #define NR_MULTI_ROOT (NR_CXL_HOST_BRIDGES * NR_CXL_ROOT_PORTS)
37 static struct platform_device *cxl_root_port[NR_MULTI_ROOT];
38 static struct platform_device *cxl_switch_uport[NR_MULTI_ROOT];
39 #define NR_MEM_MULTI \
40 (NR_CXL_HOST_BRIDGES * NR_CXL_ROOT_PORTS * NR_CXL_SWITCH_PORTS)
41 static struct platform_device *cxl_switch_dport[NR_MEM_MULTI];
42
43 static struct platform_device *cxl_hb_single[NR_CXL_SINGLE_HOST];
44 static struct platform_device *cxl_root_single[NR_CXL_SINGLE_HOST];
45 static struct platform_device *cxl_swu_single[NR_CXL_SINGLE_HOST];
46 #define NR_MEM_SINGLE (NR_CXL_SINGLE_HOST * NR_CXL_SWITCH_PORTS)
47 static struct platform_device *cxl_swd_single[NR_MEM_SINGLE];
48
49 struct platform_device *cxl_mem[NR_MEM_MULTI];
50 struct platform_device *cxl_mem_single[NR_MEM_SINGLE];
51
52 static struct platform_device *cxl_rch[NR_CXL_RCH];
53 static struct platform_device *cxl_rcd[NR_CXL_RCH];
54
55 /*
56 * Decoder registry
57 *
58 * Record decoder programming so that the topology can be reconstructed
59 * after cxl_acpi unbind/bind. This allows a user-created region config
60 * to be replayed as if firmware had provided the region at enumeration
61 * time.
62 *
63 * Entries are keyed by a stable port identity (port->uport_dev) combined
64 * with the decoder id. Decoder state is saved at initialization and
65 * updated on commit and reset.
66 *
67 * On re-enumeration mock_init_hdm_decoder() consults this registry to
68 * restore enabled decoders. Disabled decoders are reinitialized to a
69 * clean default state rather than replaying stale programming.
70 */
71 static DEFINE_XARRAY(decoder_registry);
72
73 /*
74 * When set, decoder reset will not update the registry. This allows
75 * region destroy operations to reset live decoders without erasing
76 * the saved programming needed for replay after re-enumeration.
77 */
78 static bool decoder_reset_preserve_registry;
79
is_multi_bridge(struct device * dev)80 static inline bool is_multi_bridge(struct device *dev)
81 {
82 int i;
83
84 for (i = 0; i < ARRAY_SIZE(cxl_host_bridge); i++)
85 if (&cxl_host_bridge[i]->dev == dev)
86 return true;
87 return false;
88 }
89
is_single_bridge(struct device * dev)90 static inline bool is_single_bridge(struct device *dev)
91 {
92 int i;
93
94 for (i = 0; i < ARRAY_SIZE(cxl_hb_single); i++)
95 if (&cxl_hb_single[i]->dev == dev)
96 return true;
97 return false;
98 }
99
100 static struct acpi_device acpi0017_mock;
101 static struct acpi_device host_bridge[NR_BRIDGES] = {
102 [0] = {
103 .handle = &host_bridge[0],
104 .pnp.unique_id = "0",
105 },
106 [1] = {
107 .handle = &host_bridge[1],
108 .pnp.unique_id = "1",
109 },
110 [2] = {
111 .handle = &host_bridge[2],
112 .pnp.unique_id = "2",
113 },
114 [3] = {
115 .handle = &host_bridge[3],
116 .pnp.unique_id = "3",
117 },
118 };
119
is_mock_dev(struct device * dev)120 static bool is_mock_dev(struct device *dev)
121 {
122 int i;
123
124 for (i = 0; i < ARRAY_SIZE(cxl_mem); i++)
125 if (dev == &cxl_mem[i]->dev)
126 return true;
127 for (i = 0; i < ARRAY_SIZE(cxl_mem_single); i++)
128 if (dev == &cxl_mem_single[i]->dev)
129 return true;
130 for (i = 0; i < ARRAY_SIZE(cxl_rcd); i++)
131 if (dev == &cxl_rcd[i]->dev)
132 return true;
133 if (dev == &cxl_acpi->dev)
134 return true;
135 return false;
136 }
137
is_mock_adev(struct acpi_device * adev)138 static bool is_mock_adev(struct acpi_device *adev)
139 {
140 int i;
141
142 if (adev == &acpi0017_mock)
143 return true;
144
145 for (i = 0; i < ARRAY_SIZE(host_bridge); i++)
146 if (adev == &host_bridge[i])
147 return true;
148
149 return false;
150 }
151
152 static struct {
153 struct acpi_table_cedt cedt;
154 struct acpi_cedt_chbs chbs[NR_BRIDGES];
155 struct {
156 struct acpi_cedt_cfmws cfmws;
157 u32 target[1];
158 } cfmws0;
159 struct {
160 struct acpi_cedt_cfmws cfmws;
161 u32 target[2];
162 } cfmws1;
163 struct {
164 struct acpi_cedt_cfmws cfmws;
165 u32 target[1];
166 } cfmws2;
167 struct {
168 struct acpi_cedt_cfmws cfmws;
169 u32 target[2];
170 } cfmws3;
171 struct {
172 struct acpi_cedt_cfmws cfmws;
173 u32 target[1];
174 } cfmws4;
175 struct {
176 struct acpi_cedt_cfmws cfmws;
177 u32 target[1];
178 } cfmws5;
179 struct {
180 struct acpi_cedt_cfmws cfmws;
181 u32 target[1];
182 } cfmws6;
183 struct {
184 struct acpi_cedt_cfmws cfmws;
185 u32 target[2];
186 } cfmws7;
187 struct {
188 struct acpi_cedt_cfmws cfmws;
189 u32 target[3];
190 } cfmws8;
191 struct {
192 struct acpi_cedt_cxims cxims;
193 u64 xormap_list[2];
194 } cxims0;
195 } __packed mock_cedt = {
196 .cedt = {
197 .header = {
198 .signature = "CEDT",
199 .length = sizeof(mock_cedt),
200 .revision = 1,
201 },
202 },
203 .chbs[0] = {
204 .header = {
205 .type = ACPI_CEDT_TYPE_CHBS,
206 .length = sizeof(mock_cedt.chbs[0]),
207 },
208 .uid = 0,
209 .cxl_version = ACPI_CEDT_CHBS_VERSION_CXL20,
210 },
211 .chbs[1] = {
212 .header = {
213 .type = ACPI_CEDT_TYPE_CHBS,
214 .length = sizeof(mock_cedt.chbs[0]),
215 },
216 .uid = 1,
217 .cxl_version = ACPI_CEDT_CHBS_VERSION_CXL20,
218 },
219 .chbs[2] = {
220 .header = {
221 .type = ACPI_CEDT_TYPE_CHBS,
222 .length = sizeof(mock_cedt.chbs[0]),
223 },
224 .uid = 2,
225 .cxl_version = ACPI_CEDT_CHBS_VERSION_CXL20,
226 },
227 .chbs[3] = {
228 .header = {
229 .type = ACPI_CEDT_TYPE_CHBS,
230 .length = sizeof(mock_cedt.chbs[0]),
231 },
232 .uid = 3,
233 .cxl_version = ACPI_CEDT_CHBS_VERSION_CXL11,
234 },
235 .cfmws0 = {
236 .cfmws = {
237 .header = {
238 .type = ACPI_CEDT_TYPE_CFMWS,
239 .length = sizeof(mock_cedt.cfmws0),
240 },
241 .interleave_ways = 0,
242 .granularity = 4,
243 .restrictions = ACPI_CEDT_CFMWS_RESTRICT_HOSTONLYMEM |
244 ACPI_CEDT_CFMWS_RESTRICT_VOLATILE,
245 .qtg_id = FAKE_QTG_ID,
246 .window_size = SZ_256M * 4UL,
247 },
248 .target = { 0 },
249 },
250 .cfmws1 = {
251 .cfmws = {
252 .header = {
253 .type = ACPI_CEDT_TYPE_CFMWS,
254 .length = sizeof(mock_cedt.cfmws1),
255 },
256 .interleave_ways = 1,
257 .granularity = 4,
258 .restrictions = ACPI_CEDT_CFMWS_RESTRICT_HOSTONLYMEM |
259 ACPI_CEDT_CFMWS_RESTRICT_VOLATILE,
260 .qtg_id = FAKE_QTG_ID,
261 .window_size = SZ_256M * 8UL,
262 },
263 .target = { 0, 1, },
264 },
265 .cfmws2 = {
266 .cfmws = {
267 .header = {
268 .type = ACPI_CEDT_TYPE_CFMWS,
269 .length = sizeof(mock_cedt.cfmws2),
270 },
271 .interleave_ways = 0,
272 .granularity = 4,
273 .restrictions = ACPI_CEDT_CFMWS_RESTRICT_HOSTONLYMEM |
274 ACPI_CEDT_CFMWS_RESTRICT_PMEM,
275 .qtg_id = FAKE_QTG_ID,
276 .window_size = SZ_256M * 4UL,
277 },
278 .target = { 0 },
279 },
280 .cfmws3 = {
281 .cfmws = {
282 .header = {
283 .type = ACPI_CEDT_TYPE_CFMWS,
284 .length = sizeof(mock_cedt.cfmws3),
285 },
286 .interleave_ways = 1,
287 .granularity = 4,
288 .restrictions = ACPI_CEDT_CFMWS_RESTRICT_HOSTONLYMEM |
289 ACPI_CEDT_CFMWS_RESTRICT_PMEM,
290 .qtg_id = FAKE_QTG_ID,
291 .window_size = SZ_256M * 8UL,
292 },
293 .target = { 0, 1, },
294 },
295 .cfmws4 = {
296 .cfmws = {
297 .header = {
298 .type = ACPI_CEDT_TYPE_CFMWS,
299 .length = sizeof(mock_cedt.cfmws4),
300 },
301 .interleave_ways = 0,
302 .granularity = 4,
303 .restrictions = ACPI_CEDT_CFMWS_RESTRICT_HOSTONLYMEM |
304 ACPI_CEDT_CFMWS_RESTRICT_PMEM,
305 .qtg_id = FAKE_QTG_ID,
306 .window_size = SZ_256M * 4UL,
307 },
308 .target = { 2 },
309 },
310 .cfmws5 = {
311 .cfmws = {
312 .header = {
313 .type = ACPI_CEDT_TYPE_CFMWS,
314 .length = sizeof(mock_cedt.cfmws5),
315 },
316 .interleave_ways = 0,
317 .granularity = 4,
318 .restrictions = ACPI_CEDT_CFMWS_RESTRICT_HOSTONLYMEM |
319 ACPI_CEDT_CFMWS_RESTRICT_VOLATILE,
320 .qtg_id = FAKE_QTG_ID,
321 .window_size = SZ_256M,
322 },
323 .target = { 3 },
324 },
325 /* .cfmws6,7,8 use ACPI_CEDT_CFMWS_ARITHMETIC_XOR */
326 .cfmws6 = {
327 .cfmws = {
328 .header = {
329 .type = ACPI_CEDT_TYPE_CFMWS,
330 .length = sizeof(mock_cedt.cfmws6),
331 },
332 .interleave_arithmetic = ACPI_CEDT_CFMWS_ARITHMETIC_XOR,
333 .interleave_ways = 0,
334 .granularity = 4,
335 .restrictions = ACPI_CEDT_CFMWS_RESTRICT_HOSTONLYMEM |
336 ACPI_CEDT_CFMWS_RESTRICT_PMEM,
337 .qtg_id = FAKE_QTG_ID,
338 .window_size = SZ_256M * 8UL,
339 },
340 .target = { 0, },
341 },
342 .cfmws7 = {
343 .cfmws = {
344 .header = {
345 .type = ACPI_CEDT_TYPE_CFMWS,
346 .length = sizeof(mock_cedt.cfmws7),
347 },
348 .interleave_arithmetic = ACPI_CEDT_CFMWS_ARITHMETIC_XOR,
349 .interleave_ways = 1,
350 .granularity = 0,
351 .restrictions = ACPI_CEDT_CFMWS_RESTRICT_HOSTONLYMEM |
352 ACPI_CEDT_CFMWS_RESTRICT_PMEM,
353 .qtg_id = FAKE_QTG_ID,
354 .window_size = SZ_256M * 8UL,
355 },
356 .target = { 0, 1, },
357 },
358 .cfmws8 = {
359 .cfmws = {
360 .header = {
361 .type = ACPI_CEDT_TYPE_CFMWS,
362 .length = sizeof(mock_cedt.cfmws8),
363 },
364 .interleave_arithmetic = ACPI_CEDT_CFMWS_ARITHMETIC_XOR,
365 .interleave_ways = 8,
366 .granularity = 1,
367 .restrictions = ACPI_CEDT_CFMWS_RESTRICT_HOSTONLYMEM |
368 ACPI_CEDT_CFMWS_RESTRICT_PMEM,
369 .qtg_id = FAKE_QTG_ID,
370 .window_size = SZ_512M * 6UL,
371 },
372 .target = { 0, 1, 2, },
373 },
374 .cxims0 = {
375 .cxims = {
376 .header = {
377 .type = ACPI_CEDT_TYPE_CXIMS,
378 .length = sizeof(mock_cedt.cxims0),
379 },
380 .hbig = 0,
381 .nr_xormaps = 2,
382 },
383 .xormap_list = { 0x404100, 0x808200, },
384 },
385 };
386
387 struct acpi_cedt_cfmws *mock_cfmws[] = {
388 [0] = &mock_cedt.cfmws0.cfmws,
389 [1] = &mock_cedt.cfmws1.cfmws,
390 [2] = &mock_cedt.cfmws2.cfmws,
391 [3] = &mock_cedt.cfmws3.cfmws,
392 [4] = &mock_cedt.cfmws4.cfmws,
393 [5] = &mock_cedt.cfmws5.cfmws,
394 /* Modulo Math above, XOR Math below */
395 [6] = &mock_cedt.cfmws6.cfmws,
396 [7] = &mock_cedt.cfmws7.cfmws,
397 [8] = &mock_cedt.cfmws8.cfmws,
398 };
399
400 static int cfmws_start;
401 static int cfmws_end;
402 #define CFMWS_MOD_ARRAY_START 0
403 #define CFMWS_MOD_ARRAY_END 5
404 #define CFMWS_XOR_ARRAY_START 6
405 #define CFMWS_XOR_ARRAY_END 8
406
407 struct acpi_cedt_cxims *mock_cxims[1] = {
408 [0] = &mock_cedt.cxims0.cxims,
409 };
410
411 struct cxl_mock_res {
412 struct list_head list;
413 struct range range;
414 };
415
416 static LIST_HEAD(mock_res);
417 static DEFINE_MUTEX(mock_res_lock);
418 static struct gen_pool *cxl_mock_pool;
419
depopulate_all_mock_resources(void)420 static void depopulate_all_mock_resources(void)
421 {
422 struct cxl_mock_res *res, *_res;
423
424 mutex_lock(&mock_res_lock);
425 list_for_each_entry_safe(res, _res, &mock_res, list) {
426 gen_pool_free(cxl_mock_pool, res->range.start,
427 range_len(&res->range));
428 list_del(&res->list);
429 kfree(res);
430 }
431 mutex_unlock(&mock_res_lock);
432 }
433
alloc_mock_res(resource_size_t size,int align)434 static struct cxl_mock_res *alloc_mock_res(resource_size_t size, int align)
435 {
436 struct cxl_mock_res *res = kzalloc(sizeof(*res), GFP_KERNEL);
437 struct genpool_data_align data = {
438 .align = align,
439 };
440 unsigned long phys;
441
442 INIT_LIST_HEAD(&res->list);
443 phys = gen_pool_alloc_algo(cxl_mock_pool, size,
444 gen_pool_first_fit_align, &data);
445 if (!phys)
446 return NULL;
447
448 res->range = (struct range) {
449 .start = phys,
450 .end = phys + size - 1,
451 };
452 mutex_lock(&mock_res_lock);
453 list_add(&res->list, &mock_res);
454 mutex_unlock(&mock_res_lock);
455
456 return res;
457 }
458
459 /* Only update CFMWS0 as this is used by the auto region. */
cfmws_elc_update(struct acpi_cedt_cfmws * window,int index)460 static void cfmws_elc_update(struct acpi_cedt_cfmws *window, int index)
461 {
462 if (!extended_linear_cache)
463 return;
464
465 if (index != 0)
466 return;
467
468 /*
469 * The window size should be 2x of the CXL region size where half is
470 * DRAM and half is CXL
471 */
472 window->window_size = mock_auto_region_size * 2;
473 }
474
populate_cedt(void)475 static int populate_cedt(void)
476 {
477 struct cxl_mock_res *res;
478 int i;
479
480 for (i = 0; i < ARRAY_SIZE(mock_cedt.chbs); i++) {
481 struct acpi_cedt_chbs *chbs = &mock_cedt.chbs[i];
482 resource_size_t size;
483
484 if (chbs->cxl_version == ACPI_CEDT_CHBS_VERSION_CXL20)
485 size = ACPI_CEDT_CHBS_LENGTH_CXL20;
486 else
487 size = ACPI_CEDT_CHBS_LENGTH_CXL11;
488
489 res = alloc_mock_res(size, size);
490 if (!res)
491 return -ENOMEM;
492 chbs->base = res->range.start;
493 chbs->length = size;
494 }
495
496 for (i = cfmws_start; i <= cfmws_end; i++) {
497 struct acpi_cedt_cfmws *window = mock_cfmws[i];
498
499 cfmws_elc_update(window, i);
500 res = alloc_mock_res(window->window_size, SZ_256M);
501 if (!res)
502 return -ENOMEM;
503 window->base_hpa = res->range.start;
504 }
505
506 return 0;
507 }
508
509 static bool is_mock_port(struct device *dev);
510
511 /*
512 * WARNING, this hack assumes the format of 'struct cxl_cfmws_context'
513 * and 'struct cxl_chbs_context' share the property that the first
514 * struct member is a cxl_test device being probed by the cxl_acpi
515 * driver.
516 */
517 struct cxl_cedt_context {
518 struct device *dev;
519 };
520
mock_acpi_table_parse_cedt(enum acpi_cedt_type id,acpi_tbl_entry_handler_arg handler_arg,void * arg)521 static int mock_acpi_table_parse_cedt(enum acpi_cedt_type id,
522 acpi_tbl_entry_handler_arg handler_arg,
523 void *arg)
524 {
525 struct cxl_cedt_context *ctx = arg;
526 struct device *dev = ctx->dev;
527 union acpi_subtable_headers *h;
528 unsigned long end;
529 int i;
530
531 if (!is_mock_port(dev) && !is_mock_dev(dev))
532 return acpi_table_parse_cedt(id, handler_arg, arg);
533
534 if (id == ACPI_CEDT_TYPE_CHBS)
535 for (i = 0; i < ARRAY_SIZE(mock_cedt.chbs); i++) {
536 h = (union acpi_subtable_headers *)&mock_cedt.chbs[i];
537 end = (unsigned long)&mock_cedt.chbs[i + 1];
538 handler_arg(h, arg, end);
539 }
540
541 if (id == ACPI_CEDT_TYPE_CFMWS)
542 for (i = cfmws_start; i <= cfmws_end; i++) {
543 h = (union acpi_subtable_headers *) mock_cfmws[i];
544 end = (unsigned long) h + mock_cfmws[i]->header.length;
545 handler_arg(h, arg, end);
546 }
547
548 if (id == ACPI_CEDT_TYPE_CXIMS)
549 for (i = 0; i < ARRAY_SIZE(mock_cxims); i++) {
550 h = (union acpi_subtable_headers *)mock_cxims[i];
551 end = (unsigned long)h + mock_cxims[i]->header.length;
552 handler_arg(h, arg, end);
553 }
554
555 return 0;
556 }
557
is_mock_bridge(struct device * dev)558 static bool is_mock_bridge(struct device *dev)
559 {
560 int i;
561
562 for (i = 0; i < ARRAY_SIZE(cxl_host_bridge); i++)
563 if (dev == &cxl_host_bridge[i]->dev)
564 return true;
565 for (i = 0; i < ARRAY_SIZE(cxl_hb_single); i++)
566 if (dev == &cxl_hb_single[i]->dev)
567 return true;
568 for (i = 0; i < ARRAY_SIZE(cxl_rch); i++)
569 if (dev == &cxl_rch[i]->dev)
570 return true;
571
572 return false;
573 }
574
is_mock_port(struct device * dev)575 static bool is_mock_port(struct device *dev)
576 {
577 int i;
578
579 if (is_mock_bridge(dev))
580 return true;
581
582 for (i = 0; i < ARRAY_SIZE(cxl_root_port); i++)
583 if (dev == &cxl_root_port[i]->dev)
584 return true;
585
586 for (i = 0; i < ARRAY_SIZE(cxl_switch_uport); i++)
587 if (dev == &cxl_switch_uport[i]->dev)
588 return true;
589
590 for (i = 0; i < ARRAY_SIZE(cxl_switch_dport); i++)
591 if (dev == &cxl_switch_dport[i]->dev)
592 return true;
593
594 for (i = 0; i < ARRAY_SIZE(cxl_root_single); i++)
595 if (dev == &cxl_root_single[i]->dev)
596 return true;
597
598 for (i = 0; i < ARRAY_SIZE(cxl_swu_single); i++)
599 if (dev == &cxl_swu_single[i]->dev)
600 return true;
601
602 for (i = 0; i < ARRAY_SIZE(cxl_swd_single); i++)
603 if (dev == &cxl_swd_single[i]->dev)
604 return true;
605
606 if (is_cxl_memdev(dev))
607 return is_mock_dev(dev->parent);
608
609 return false;
610 }
611
host_bridge_index(struct acpi_device * adev)612 static int host_bridge_index(struct acpi_device *adev)
613 {
614 return adev - host_bridge;
615 }
616
find_host_bridge(acpi_handle handle)617 static struct acpi_device *find_host_bridge(acpi_handle handle)
618 {
619 int i;
620
621 for (i = 0; i < ARRAY_SIZE(host_bridge); i++)
622 if (handle == host_bridge[i].handle)
623 return &host_bridge[i];
624 return NULL;
625 }
626
627 static acpi_status
mock_acpi_evaluate_integer(acpi_handle handle,acpi_string pathname,struct acpi_object_list * arguments,unsigned long long * data)628 mock_acpi_evaluate_integer(acpi_handle handle, acpi_string pathname,
629 struct acpi_object_list *arguments,
630 unsigned long long *data)
631 {
632 struct acpi_device *adev = find_host_bridge(handle);
633
634 if (!adev || strcmp(pathname, METHOD_NAME__UID) != 0)
635 return acpi_evaluate_integer(handle, pathname, arguments, data);
636
637 *data = host_bridge_index(adev);
638 return AE_OK;
639 }
640
641 static int
mock_hmat_get_extended_linear_cache_size(struct resource * backing_res,int nid,resource_size_t * cache_size)642 mock_hmat_get_extended_linear_cache_size(struct resource *backing_res,
643 int nid, resource_size_t *cache_size)
644 {
645 struct acpi_cedt_cfmws *window = mock_cfmws[0];
646 struct resource cfmws0_res =
647 DEFINE_RES_MEM(window->base_hpa, window->window_size);
648
649 if (!extended_linear_cache ||
650 !resource_contains(&cfmws0_res, backing_res)) {
651 return hmat_get_extended_linear_cache_size(backing_res,
652 nid, cache_size);
653 }
654
655 *cache_size = mock_auto_region_size;
656
657 return 0;
658 }
659
660 static struct pci_bus mock_pci_bus[NR_BRIDGES];
661 static struct acpi_pci_root mock_pci_root[ARRAY_SIZE(mock_pci_bus)] = {
662 [0] = {
663 .bus = &mock_pci_bus[0],
664 },
665 [1] = {
666 .bus = &mock_pci_bus[1],
667 },
668 [2] = {
669 .bus = &mock_pci_bus[2],
670 },
671 [3] = {
672 .bus = &mock_pci_bus[3],
673 },
674
675 };
676
is_mock_bus(struct pci_bus * bus)677 static bool is_mock_bus(struct pci_bus *bus)
678 {
679 int i;
680
681 for (i = 0; i < ARRAY_SIZE(mock_pci_bus); i++)
682 if (bus == &mock_pci_bus[i])
683 return true;
684 return false;
685 }
686
mock_acpi_pci_find_root(acpi_handle handle)687 static struct acpi_pci_root *mock_acpi_pci_find_root(acpi_handle handle)
688 {
689 struct acpi_device *adev = find_host_bridge(handle);
690
691 if (!adev)
692 return acpi_pci_find_root(handle);
693 return &mock_pci_root[host_bridge_index(adev)];
694 }
695
mock_cxl_setup_hdm(struct cxl_port * port,struct cxl_endpoint_dvsec_info * info)696 static struct cxl_hdm *mock_cxl_setup_hdm(struct cxl_port *port,
697 struct cxl_endpoint_dvsec_info *info)
698 {
699 struct cxl_hdm *cxlhdm = devm_kzalloc(&port->dev, sizeof(*cxlhdm), GFP_KERNEL);
700 struct device *dev = &port->dev;
701
702 if (!cxlhdm)
703 return ERR_PTR(-ENOMEM);
704
705 cxlhdm->port = port;
706 cxlhdm->interleave_mask = ~0U;
707 cxlhdm->iw_cap_mask = ~0UL;
708 dev_set_drvdata(dev, cxlhdm);
709 return cxlhdm;
710 }
711
712 struct target_map_ctx {
713 u32 *target_map;
714 int index;
715 int target_count;
716 };
717
map_targets(struct device * dev,void * data)718 static int map_targets(struct device *dev, void *data)
719 {
720 struct platform_device *pdev = to_platform_device(dev);
721 struct target_map_ctx *ctx = data;
722
723 ctx->target_map[ctx->index++] = pdev->id;
724
725 if (ctx->index > ctx->target_count) {
726 dev_WARN_ONCE(dev, 1, "too many targets found?\n");
727 return -ENXIO;
728 }
729
730 return 0;
731 }
732
733 /*
734 * Build a stable registry key from the decoder's upstream port identity
735 * and decoder id.
736 *
737 * Decoder objects and cxl_port objects are reallocated on each enumeration,
738 * so their addresses cannot be used directly as replay keys. However,
739 * port->uport_dev is stable for a given topology across cxl_acpi unbind/bind
740 * in cxl_test, so use that as the port identity and pack the local decoder
741 * id into the low bits.
742 *
743 * The key is formed as:
744 * ((unsigned long)port->uport_dev << 4) | cxld->id
745 *
746 * The low bits hold the decoder id (which must fit in 4 bits) while
747 * the remaining bits identify the upstream port. This key is only used
748 * within cxl_test to locate saved decoder state during replay.
749 */
cxld_registry_index(struct cxl_decoder * cxld)750 static unsigned long cxld_registry_index(struct cxl_decoder *cxld)
751 {
752 struct cxl_port *port = to_cxl_port(cxld->dev.parent);
753
754 dev_WARN_ONCE(&port->dev, cxld->id >= 16,
755 "decoder id:%d out of range\n", cxld->id);
756 return (((unsigned long)port->uport_dev) << 4) | cxld->id;
757 }
758
759 struct cxl_test_decoder {
760 union {
761 struct cxl_switch_decoder cxlsd;
762 struct cxl_endpoint_decoder cxled;
763 };
764 struct range dpa_range;
765 };
766
cxld_registry_find(struct cxl_decoder * cxld)767 static struct cxl_test_decoder *cxld_registry_find(struct cxl_decoder *cxld)
768 {
769 return xa_load(&decoder_registry, cxld_registry_index(cxld));
770 }
771
772 #define dbg_cxld(port, msg, cxld) \
773 do { \
774 struct cxl_decoder *___d = (cxld); \
775 dev_dbg((port)->uport_dev, \
776 "decoder%d: %s range: %#llx-%#llx iw: %d ig: %d flags: %#lx\n", \
777 ___d->id, msg, ___d->hpa_range.start, \
778 ___d->hpa_range.end + 1, ___d->interleave_ways, \
779 ___d->interleave_granularity, ___d->flags); \
780 } while (0)
781
782 static int mock_decoder_commit(struct cxl_decoder *cxld);
783 static void mock_decoder_reset(struct cxl_decoder *cxld);
784 static void init_disabled_mock_decoder(struct cxl_decoder *cxld);
785
cxld_copy(struct cxl_decoder * a,struct cxl_decoder * b)786 static void cxld_copy(struct cxl_decoder *a, struct cxl_decoder *b)
787 {
788 a->id = b->id;
789 a->hpa_range = b->hpa_range;
790 a->interleave_ways = b->interleave_ways;
791 a->interleave_granularity = b->interleave_granularity;
792 a->target_type = b->target_type;
793 a->flags = b->flags;
794 a->commit = mock_decoder_commit;
795 a->reset = mock_decoder_reset;
796 }
797
798 /*
799 * Restore decoder programming saved in the registry.
800 *
801 * Only decoders that were saved enabled are restored. Disabled decoders
802 * are left in their default inactive state so that stale programming is
803 * not resurrected after topology replay.
804 *
805 * For endpoint decoders this also restores the DPA reservation needed
806 * to reconstruct committed mappings.
807 */
cxld_registry_restore(struct cxl_decoder * cxld,struct cxl_test_decoder * td)808 static int cxld_registry_restore(struct cxl_decoder *cxld,
809 struct cxl_test_decoder *td)
810 {
811 struct cxl_port *port = to_cxl_port(cxld->dev.parent);
812 int rc;
813
814 if (is_switch_decoder(&cxld->dev)) {
815 struct cxl_switch_decoder *cxlsd =
816 to_cxl_switch_decoder(&cxld->dev);
817
818 if (!(td->cxlsd.cxld.flags & CXL_DECODER_F_ENABLE))
819 return 0;
820
821 dbg_cxld(port, "restore", &td->cxlsd.cxld);
822 cxld_copy(cxld, &td->cxlsd.cxld);
823 WARN_ON(cxlsd->nr_targets != td->cxlsd.nr_targets);
824
825 /* Restore saved target intent; live dport binding happens later */
826 for (int i = 0; i < cxlsd->nr_targets; i++) {
827 cxlsd->target[i] = NULL;
828 cxld->target_map[i] = td->cxlsd.cxld.target_map[i];
829 }
830
831 port->commit_end = cxld->id;
832
833 } else {
834 struct cxl_endpoint_decoder *cxled =
835 to_cxl_endpoint_decoder(&cxld->dev);
836
837 if (!(td->cxled.cxld.flags & CXL_DECODER_F_ENABLE))
838 return 0;
839
840 dbg_cxld(port, "restore", &td->cxled.cxld);
841 cxld_copy(cxld, &td->cxled.cxld);
842 cxled->state = td->cxled.state;
843 cxled->skip = td->cxled.skip;
844 if (range_len(&td->dpa_range)) {
845 rc = devm_cxl_dpa_reserve(cxled, td->dpa_range.start,
846 range_len(&td->dpa_range),
847 td->cxled.skip);
848 if (rc) {
849 init_disabled_mock_decoder(cxld);
850 return rc;
851 }
852 }
853 port->commit_end = cxld->id;
854 }
855
856 return 0;
857 }
858
__cxld_registry_save(struct cxl_test_decoder * td,struct cxl_decoder * cxld)859 static void __cxld_registry_save(struct cxl_test_decoder *td,
860 struct cxl_decoder *cxld)
861 {
862 if (is_switch_decoder(&cxld->dev)) {
863 struct cxl_switch_decoder *cxlsd =
864 to_cxl_switch_decoder(&cxld->dev);
865
866 cxld_copy(&td->cxlsd.cxld, cxld);
867 td->cxlsd.nr_targets = cxlsd->nr_targets;
868
869 /* Save target port_id as a stable identify for the dport */
870 for (int i = 0; i < cxlsd->nr_targets; i++) {
871 struct cxl_dport *dport;
872
873 if (!cxlsd->target[i])
874 continue;
875
876 dport = cxlsd->target[i];
877 td->cxlsd.cxld.target_map[i] = dport->port_id;
878 }
879 } else {
880 struct cxl_endpoint_decoder *cxled =
881 to_cxl_endpoint_decoder(&cxld->dev);
882
883 cxld_copy(&td->cxled.cxld, cxld);
884 td->cxled.state = cxled->state;
885 td->cxled.skip = cxled->skip;
886
887 if (!(cxld->flags & CXL_DECODER_F_ENABLE)) {
888 td->dpa_range.start = 0;
889 td->dpa_range.end = -1;
890 } else if (cxled->dpa_res) {
891 td->dpa_range.start = cxled->dpa_res->start;
892 td->dpa_range.end = cxled->dpa_res->end;
893 } else {
894 td->dpa_range.start = 0;
895 td->dpa_range.end = -1;
896 }
897 }
898 }
899
cxld_registry_save(struct cxl_test_decoder * td,struct cxl_decoder * cxld)900 static void cxld_registry_save(struct cxl_test_decoder *td,
901 struct cxl_decoder *cxld)
902 {
903 struct cxl_port *port = to_cxl_port(cxld->dev.parent);
904
905 dbg_cxld(port, "save", cxld);
906 __cxld_registry_save(td, cxld);
907 }
908
cxld_registry_update(struct cxl_decoder * cxld)909 static void cxld_registry_update(struct cxl_decoder *cxld)
910 {
911 struct cxl_test_decoder *td = cxld_registry_find(cxld);
912 struct cxl_port *port = to_cxl_port(cxld->dev.parent);
913
914 if (WARN_ON_ONCE(!td))
915 return;
916
917 dbg_cxld(port, "update", cxld);
918 __cxld_registry_save(td, cxld);
919 }
920
mock_decoder_commit(struct cxl_decoder * cxld)921 static int mock_decoder_commit(struct cxl_decoder *cxld)
922 {
923 struct cxl_port *port = to_cxl_port(cxld->dev.parent);
924 int id = cxld->id;
925
926 if (cxld->flags & CXL_DECODER_F_ENABLE)
927 return 0;
928
929 dev_dbg(&port->dev, "%s commit\n", dev_name(&cxld->dev));
930 if (cxl_num_decoders_committed(port) != id) {
931 dev_dbg(&port->dev,
932 "%s: out of order commit, expected decoder%d.%d\n",
933 dev_name(&cxld->dev), port->id,
934 cxl_num_decoders_committed(port));
935 return -EBUSY;
936 }
937
938 port->commit_end++;
939 cxld->flags |= CXL_DECODER_F_ENABLE;
940 if (is_endpoint_decoder(&cxld->dev)) {
941 struct cxl_endpoint_decoder *cxled =
942 to_cxl_endpoint_decoder(&cxld->dev);
943
944 cxled->state = CXL_DECODER_STATE_AUTO;
945 }
946 cxld_registry_update(cxld);
947
948 return 0;
949 }
950
mock_decoder_reset(struct cxl_decoder * cxld)951 static void mock_decoder_reset(struct cxl_decoder *cxld)
952 {
953 struct cxl_port *port = to_cxl_port(cxld->dev.parent);
954 int id = cxld->id;
955
956 if ((cxld->flags & CXL_DECODER_F_ENABLE) == 0)
957 return;
958
959 dev_dbg(&port->dev, "%s reset\n", dev_name(&cxld->dev));
960 if (port->commit_end == id)
961 cxl_port_commit_reap(cxld);
962 else
963 dev_dbg(&port->dev,
964 "%s: out of order reset, expected decoder%d.%d\n",
965 dev_name(&cxld->dev), port->id, port->commit_end);
966 cxld->flags &= ~CXL_DECODER_F_ENABLE;
967
968 if (is_endpoint_decoder(&cxld->dev)) {
969 struct cxl_endpoint_decoder *cxled =
970 to_cxl_endpoint_decoder(&cxld->dev);
971
972 cxled->state = CXL_DECODER_STATE_MANUAL;
973 cxled->skip = 0;
974 }
975 if (decoder_reset_preserve_registry)
976 dev_dbg(port->uport_dev, "decoder%d: skip registry update\n",
977 cxld->id);
978 else
979 cxld_registry_update(cxld);
980 }
981
cxld_registry_new(struct cxl_decoder * cxld)982 static struct cxl_test_decoder *cxld_registry_new(struct cxl_decoder *cxld)
983 {
984 struct cxl_test_decoder *td __free(kfree) =
985 kzalloc(sizeof(*td), GFP_KERNEL);
986 unsigned long key = cxld_registry_index(cxld);
987
988 if (!td)
989 return NULL;
990
991 if (xa_insert(&decoder_registry, key, td, GFP_KERNEL)) {
992 WARN_ON(1);
993 return NULL;
994 }
995
996 cxld_registry_save(td, cxld);
997 return no_free_ptr(td);
998 }
999
init_disabled_mock_decoder(struct cxl_decoder * cxld)1000 static void init_disabled_mock_decoder(struct cxl_decoder *cxld)
1001 {
1002 cxld->hpa_range.start = 0;
1003 cxld->hpa_range.end = -1;
1004 cxld->interleave_ways = 1;
1005 cxld->interleave_granularity = 0;
1006 cxld->target_type = CXL_DECODER_HOSTONLYMEM;
1007 cxld->flags = 0;
1008 cxld->commit = mock_decoder_commit;
1009 cxld->reset = mock_decoder_reset;
1010
1011 if (is_switch_decoder(&cxld->dev)) {
1012 struct cxl_switch_decoder *cxlsd =
1013 to_cxl_switch_decoder(&cxld->dev);
1014
1015 for (int i = 0; i < cxlsd->nr_targets; i++) {
1016 cxlsd->target[i] = NULL;
1017 cxld->target_map[i] = 0;
1018 }
1019 } else {
1020 struct cxl_endpoint_decoder *cxled =
1021 to_cxl_endpoint_decoder(&cxld->dev);
1022
1023 cxled->state = CXL_DECODER_STATE_MANUAL;
1024 cxled->skip = 0;
1025 }
1026 }
1027
default_mock_decoder(struct cxl_decoder * cxld)1028 static void default_mock_decoder(struct cxl_decoder *cxld)
1029 {
1030 cxld->hpa_range = (struct range){
1031 .start = 0,
1032 .end = -1,
1033 };
1034
1035 cxld->interleave_ways = 1;
1036 cxld->interleave_granularity = 256;
1037 cxld->target_type = CXL_DECODER_HOSTONLYMEM;
1038 cxld->commit = mock_decoder_commit;
1039 cxld->reset = mock_decoder_reset;
1040
1041 WARN_ON_ONCE(!cxld_registry_new(cxld));
1042 }
1043
first_decoder(struct device * dev,const void * data)1044 static int first_decoder(struct device *dev, const void *data)
1045 {
1046 struct cxl_decoder *cxld;
1047
1048 if (!is_switch_decoder(dev))
1049 return 0;
1050 cxld = to_cxl_decoder(dev);
1051 if (cxld->id == 0)
1052 return 1;
1053 return 0;
1054 }
1055
1056 /*
1057 * Initialize a decoder during HDM enumeration.
1058 *
1059 * If a saved registry entry exists:
1060 * - enabled decoders are restored from the saved programming
1061 * - disabled decoders are initialized in a clean disabled state
1062 *
1063 * If no registry entry exists the decoder follows the normal mock
1064 * initialization path, including the special auto-region setup for
1065 * the first endpoints under host-bridge0.
1066 *
1067 * Returns true if decoder state was restored from the registry. In
1068 * that case the saved decode configuration (including target mapping)
1069 * has already been applied and the map_targets() is skipped.
1070 */
mock_init_hdm_decoder(struct cxl_decoder * cxld)1071 static bool mock_init_hdm_decoder(struct cxl_decoder *cxld)
1072 {
1073 struct acpi_cedt_cfmws *window = mock_cfmws[0];
1074 struct platform_device *pdev = NULL;
1075 struct cxl_endpoint_decoder *cxled;
1076 struct cxl_switch_decoder *cxlsd;
1077 struct cxl_port *port, *iter;
1078 struct cxl_test_decoder *td;
1079 struct cxl_memdev *cxlmd;
1080 struct cxl_dport *dport;
1081 struct device *dev;
1082 bool hb0 = false;
1083 u64 base;
1084 int i;
1085
1086 if (is_endpoint_decoder(&cxld->dev)) {
1087 cxled = to_cxl_endpoint_decoder(&cxld->dev);
1088 cxlmd = cxled_to_memdev(cxled);
1089 WARN_ON(!dev_is_platform(cxlmd->dev.parent));
1090 pdev = to_platform_device(cxlmd->dev.parent);
1091
1092 /* check is endpoint is attach to host-bridge0 */
1093 port = cxled_to_port(cxled);
1094 do {
1095 if (port->uport_dev == &cxl_host_bridge[0]->dev) {
1096 hb0 = true;
1097 break;
1098 }
1099 if (is_cxl_port(port->dev.parent))
1100 port = to_cxl_port(port->dev.parent);
1101 else
1102 port = NULL;
1103 } while (port);
1104 port = cxled_to_port(cxled);
1105 } else {
1106 port = to_cxl_port(cxld->dev.parent);
1107 }
1108
1109 td = cxld_registry_find(cxld);
1110 if (td) {
1111 bool enabled;
1112
1113 if (is_switch_decoder(&cxld->dev))
1114 enabled = td->cxlsd.cxld.flags & CXL_DECODER_F_ENABLE;
1115 else
1116 enabled = td->cxled.cxld.flags & CXL_DECODER_F_ENABLE;
1117
1118 if (enabled)
1119 return !cxld_registry_restore(cxld, td);
1120
1121 init_disabled_mock_decoder(cxld);
1122 return false;
1123 }
1124
1125 /*
1126 * The first decoder on the first 2 devices on the first switch
1127 * attached to host-bridge0 mock a fake / static RAM region. All
1128 * other decoders are default disabled. Given the round robin
1129 * assignment those devices are named cxl_mem.0, and cxl_mem.4.
1130 *
1131 * See 'cxl list -BMPu -m cxl_mem.0,cxl_mem.4'
1132 */
1133 if (!is_endpoint_decoder(&cxld->dev) || !hb0 || pdev->id % 4 ||
1134 pdev->id > 4 || cxld->id > 0) {
1135 default_mock_decoder(cxld);
1136 return false;
1137 }
1138
1139 /* Simulate missing cxl_mem.4 configuration */
1140 if (hb0 && pdev->id == 4 && cxld->id == 0 && fail_autoassemble) {
1141 default_mock_decoder(cxld);
1142 return false;
1143 }
1144
1145 base = window->base_hpa;
1146 if (extended_linear_cache)
1147 base += mock_auto_region_size;
1148 cxld->hpa_range = (struct range) {
1149 .start = base,
1150 .end = base + mock_auto_region_size - 1,
1151 };
1152
1153 cxld->interleave_ways = 2;
1154 eig_to_granularity(window->granularity, &cxld->interleave_granularity);
1155 cxld->target_type = CXL_DECODER_HOSTONLYMEM;
1156 cxld->flags = CXL_DECODER_F_ENABLE;
1157 cxled->state = CXL_DECODER_STATE_AUTO;
1158 port->commit_end = cxld->id;
1159 devm_cxl_dpa_reserve(cxled, 0,
1160 mock_auto_region_size / cxld->interleave_ways, 0);
1161 cxld->commit = mock_decoder_commit;
1162 cxld->reset = mock_decoder_reset;
1163
1164 WARN_ON_ONCE(!cxld_registry_new(cxld));
1165 /*
1166 * Now that endpoint decoder is set up, walk up the hierarchy
1167 * and setup the switch and root port decoders targeting @cxlmd.
1168 */
1169 iter = port;
1170 for (i = 0; i < 2; i++) {
1171 dport = iter->parent_dport;
1172 iter = dport->port;
1173 dev = device_find_child(&iter->dev, NULL, first_decoder);
1174 /*
1175 * Ancestor ports are guaranteed to be enumerated before
1176 * @port, and all ports have at least one decoder.
1177 */
1178 if (WARN_ON(!dev))
1179 continue;
1180
1181 cxlsd = to_cxl_switch_decoder(dev);
1182 if (i == 0) {
1183 /* put cxl_mem.4 second in the decode order */
1184 if (pdev->id == 4) {
1185 cxlsd->target[1] = dport;
1186 cxlsd->cxld.target_map[1] = dport->port_id;
1187 } else {
1188 cxlsd->target[0] = dport;
1189 cxlsd->cxld.target_map[0] = dport->port_id;
1190 }
1191 } else {
1192 cxlsd->target[0] = dport;
1193 cxlsd->cxld.target_map[0] = dport->port_id;
1194 }
1195 cxld = &cxlsd->cxld;
1196 cxld->target_type = CXL_DECODER_HOSTONLYMEM;
1197 cxld->flags = CXL_DECODER_F_ENABLE;
1198 iter->commit_end = 0;
1199 /*
1200 * Switch targets 2 endpoints, while host bridge targets
1201 * one root port
1202 */
1203 if (i == 0)
1204 cxld->interleave_ways = 2;
1205 else
1206 cxld->interleave_ways = 1;
1207 cxld->interleave_granularity = 4096;
1208 cxld->hpa_range = (struct range) {
1209 .start = base,
1210 .end = base + mock_auto_region_size - 1,
1211 };
1212 cxld->commit = mock_decoder_commit;
1213 cxld->reset = mock_decoder_reset;
1214
1215 cxld_registry_update(cxld);
1216 put_device(dev);
1217 }
1218
1219 return false;
1220 }
1221
mock_cxl_enumerate_decoders(struct cxl_hdm * cxlhdm,struct cxl_endpoint_dvsec_info * info)1222 static int mock_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm,
1223 struct cxl_endpoint_dvsec_info *info)
1224 {
1225 struct cxl_port *port = cxlhdm->port;
1226 struct cxl_port *parent_port = to_cxl_port(port->dev.parent);
1227 int target_count, i;
1228 bool restored;
1229
1230 if (is_cxl_endpoint(port))
1231 target_count = 0;
1232 else if (is_cxl_root(parent_port))
1233 target_count = NR_CXL_ROOT_PORTS;
1234 else
1235 target_count = NR_CXL_SWITCH_PORTS;
1236
1237 for (i = 0; i < NR_CXL_PORT_DECODERS; i++) {
1238 struct target_map_ctx ctx = {
1239 .target_count = target_count,
1240 };
1241 struct cxl_decoder *cxld;
1242 int rc;
1243
1244 if (target_count) {
1245 struct cxl_switch_decoder *cxlsd;
1246
1247 cxlsd = cxl_switch_decoder_alloc(port, target_count);
1248 if (IS_ERR(cxlsd)) {
1249 dev_warn(&port->dev,
1250 "Failed to allocate the decoder\n");
1251 return PTR_ERR(cxlsd);
1252 }
1253 cxld = &cxlsd->cxld;
1254 } else {
1255 struct cxl_endpoint_decoder *cxled;
1256
1257 cxled = cxl_endpoint_decoder_alloc(port);
1258
1259 if (IS_ERR(cxled)) {
1260 dev_warn(&port->dev,
1261 "Failed to allocate the decoder\n");
1262 return PTR_ERR(cxled);
1263 }
1264 cxld = &cxled->cxld;
1265 }
1266
1267 ctx.target_map = cxld->target_map;
1268 restored = mock_init_hdm_decoder(cxld);
1269 if (target_count && !restored) {
1270 rc = device_for_each_child(port->uport_dev, &ctx,
1271 map_targets);
1272 if (rc) {
1273 put_device(&cxld->dev);
1274 return rc;
1275 }
1276 }
1277
1278 rc = cxl_decoder_add_locked(cxld);
1279 if (rc) {
1280 put_device(&cxld->dev);
1281 dev_err(&port->dev, "Failed to add decoder\n");
1282 return rc;
1283 }
1284
1285 rc = cxl_decoder_autoremove(&port->dev, cxld);
1286 if (rc)
1287 return rc;
1288 dev_dbg(&cxld->dev, "Added to port %s\n", dev_name(&port->dev));
1289 }
1290
1291 return 0;
1292 }
1293
__mock_cxl_decoders_setup(struct cxl_port * port)1294 static int __mock_cxl_decoders_setup(struct cxl_port *port)
1295 {
1296 struct cxl_hdm *cxlhdm;
1297
1298 cxlhdm = mock_cxl_setup_hdm(port, NULL);
1299 if (IS_ERR(cxlhdm)) {
1300 if (PTR_ERR(cxlhdm) != -ENODEV)
1301 dev_err(&port->dev, "Failed to map HDM decoder capability\n");
1302 return PTR_ERR(cxlhdm);
1303 }
1304
1305 return mock_cxl_enumerate_decoders(cxlhdm, NULL);
1306 }
1307
mock_cxl_switch_port_decoders_setup(struct cxl_port * port)1308 static int mock_cxl_switch_port_decoders_setup(struct cxl_port *port)
1309 {
1310 if (is_cxl_root(port) || is_cxl_endpoint(port))
1311 return -EOPNOTSUPP;
1312
1313 return __mock_cxl_decoders_setup(port);
1314 }
1315
mock_cxl_endpoint_decoders_setup(struct cxl_port * port)1316 static int mock_cxl_endpoint_decoders_setup(struct cxl_port *port)
1317 {
1318 if (!is_cxl_endpoint(port))
1319 return -EOPNOTSUPP;
1320
1321 return __mock_cxl_decoders_setup(port);
1322 }
1323
get_port_array(struct cxl_port * port,struct platform_device *** port_array,int * port_array_size)1324 static int get_port_array(struct cxl_port *port,
1325 struct platform_device ***port_array,
1326 int *port_array_size)
1327 {
1328 struct platform_device **array;
1329 int array_size;
1330
1331 if (port->depth == 1) {
1332 if (is_multi_bridge(port->uport_dev)) {
1333 array_size = ARRAY_SIZE(cxl_root_port);
1334 array = cxl_root_port;
1335 } else if (is_single_bridge(port->uport_dev)) {
1336 array_size = ARRAY_SIZE(cxl_root_single);
1337 array = cxl_root_single;
1338 } else {
1339 dev_dbg(&port->dev, "%s: unknown bridge type\n",
1340 dev_name(port->uport_dev));
1341 return -ENXIO;
1342 }
1343 } else if (port->depth == 2) {
1344 struct cxl_port *parent = to_cxl_port(port->dev.parent);
1345
1346 if (is_multi_bridge(parent->uport_dev)) {
1347 array_size = ARRAY_SIZE(cxl_switch_dport);
1348 array = cxl_switch_dport;
1349 } else if (is_single_bridge(parent->uport_dev)) {
1350 array_size = ARRAY_SIZE(cxl_swd_single);
1351 array = cxl_swd_single;
1352 } else {
1353 dev_dbg(&port->dev, "%s: unknown bridge type\n",
1354 dev_name(port->uport_dev));
1355 return -ENXIO;
1356 }
1357 } else {
1358 dev_WARN_ONCE(&port->dev, 1, "unexpected depth %d\n",
1359 port->depth);
1360 return -ENXIO;
1361 }
1362
1363 *port_array = array;
1364 *port_array_size = array_size;
1365
1366 return 0;
1367 }
1368
mock_cxl_add_dport_by_dev(struct cxl_port * port,struct device * dport_dev)1369 static struct cxl_dport *mock_cxl_add_dport_by_dev(struct cxl_port *port,
1370 struct device *dport_dev)
1371 {
1372 struct platform_device **array;
1373 int rc, i, array_size;
1374
1375 rc = get_port_array(port, &array, &array_size);
1376 if (rc)
1377 return ERR_PTR(rc);
1378
1379 for (i = 0; i < array_size; i++) {
1380 struct platform_device *pdev = array[i];
1381
1382 if (pdev->dev.parent != port->uport_dev) {
1383 dev_dbg(&port->dev, "%s: mismatch parent %s\n",
1384 dev_name(port->uport_dev),
1385 dev_name(pdev->dev.parent));
1386 continue;
1387 }
1388
1389 if (&pdev->dev != dport_dev)
1390 continue;
1391
1392 return devm_cxl_add_dport(port, &pdev->dev, pdev->id,
1393 CXL_RESOURCE_NONE);
1394 }
1395
1396 return ERR_PTR(-ENODEV);
1397 }
1398
1399 /*
1400 * Faking the cxl_dpa_perf for the memdev when appropriate.
1401 */
dpa_perf_setup(struct cxl_port * endpoint,struct range * range,struct cxl_dpa_perf * dpa_perf)1402 static void dpa_perf_setup(struct cxl_port *endpoint, struct range *range,
1403 struct cxl_dpa_perf *dpa_perf)
1404 {
1405 dpa_perf->qos_class = FAKE_QTG_ID;
1406 dpa_perf->dpa_range = *range;
1407 for (int i = 0; i < ACCESS_COORDINATE_MAX; i++) {
1408 dpa_perf->coord[i].read_latency = 500;
1409 dpa_perf->coord[i].write_latency = 500;
1410 dpa_perf->coord[i].read_bandwidth = 1000;
1411 dpa_perf->coord[i].write_bandwidth = 1000;
1412 }
1413 }
1414
mock_cxl_endpoint_parse_cdat(struct cxl_port * port)1415 static void mock_cxl_endpoint_parse_cdat(struct cxl_port *port)
1416 {
1417 struct cxl_root *cxl_root __free(put_cxl_root) =
1418 find_cxl_root(port);
1419 struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport_dev);
1420 struct cxl_dev_state *cxlds = cxlmd->cxlds;
1421 struct access_coordinate ep_c[ACCESS_COORDINATE_MAX];
1422
1423 if (!cxl_root)
1424 return;
1425
1426 for (int i = 0; i < cxlds->nr_partitions; i++) {
1427 struct resource *res = &cxlds->part[i].res;
1428 struct cxl_dpa_perf *perf = &cxlds->part[i].perf;
1429 struct range range = {
1430 .start = res->start,
1431 .end = res->end,
1432 };
1433
1434 dpa_perf_setup(port, &range, perf);
1435 }
1436
1437 cxl_memdev_update_perf(cxlmd);
1438
1439 /*
1440 * This function is here to only test the topology iterator. It serves
1441 * no other purpose.
1442 */
1443 cxl_endpoint_get_perf_coordinates(port, ep_c);
1444 }
1445
1446 /*
1447 * Simulate that the first half of mock CXL Window 0 is "Soft Reserve" capacity
1448 */
mock_walk_hmem_resources(struct device * host,walk_hmem_fn fn)1449 static int mock_walk_hmem_resources(struct device *host, walk_hmem_fn fn)
1450 {
1451 struct acpi_cedt_cfmws *cfmws = mock_cfmws[0];
1452 struct resource window =
1453 DEFINE_RES_MEM(cfmws->base_hpa, cfmws->window_size / 2);
1454
1455 dev_dbg(host, "walk cxl_test resource: %pr\n", &window);
1456 return fn(host, 0, &window);
1457 }
1458
1459 /*
1460 * This should only be called by the dax_hmem case, treat mismatches (negative
1461 * result) as "fallback to base region_intersects()". Simulate that the first
1462 * half of mock CXL Window 0 is IORES_DESC_CXL capacity.
1463 */
mock_region_intersects(resource_size_t start,size_t size,unsigned long flags,unsigned long desc)1464 static int mock_region_intersects(resource_size_t start, size_t size,
1465 unsigned long flags, unsigned long desc)
1466 {
1467 struct resource res = DEFINE_RES_MEM(start, size);
1468 struct acpi_cedt_cfmws *cfmws = mock_cfmws[0];
1469 struct resource window =
1470 DEFINE_RES_MEM(cfmws->base_hpa, cfmws->window_size / 2);
1471
1472 if (resource_overlaps(&res, &window))
1473 return REGION_INTERSECTS;
1474 pr_debug("warning: no cxl_test CXL intersection for %pr\n", &res);
1475 return -1;
1476 }
1477
1478
1479 static int
mock_region_intersects_soft_reserve(resource_size_t start,size_t size)1480 mock_region_intersects_soft_reserve(resource_size_t start, size_t size)
1481 {
1482 struct resource res = DEFINE_RES_MEM(start, size);
1483 struct acpi_cedt_cfmws *cfmws = mock_cfmws[0];
1484 struct resource window =
1485 DEFINE_RES_MEM(cfmws->base_hpa, cfmws->window_size / 2);
1486
1487 if (resource_overlaps(&res, &window))
1488 return REGION_INTERSECTS;
1489 pr_debug("warning: no cxl_test soft reserve intersection for %pr\n", &res);
1490 return -1;
1491 }
1492
1493 static struct cxl_mock_ops cxl_mock_ops = {
1494 .is_mock_adev = is_mock_adev,
1495 .is_mock_bridge = is_mock_bridge,
1496 .is_mock_bus = is_mock_bus,
1497 .is_mock_port = is_mock_port,
1498 .is_mock_dev = is_mock_dev,
1499 .acpi_table_parse_cedt = mock_acpi_table_parse_cedt,
1500 .acpi_evaluate_integer = mock_acpi_evaluate_integer,
1501 .acpi_pci_find_root = mock_acpi_pci_find_root,
1502 .devm_cxl_switch_port_decoders_setup = mock_cxl_switch_port_decoders_setup,
1503 .devm_cxl_endpoint_decoders_setup = mock_cxl_endpoint_decoders_setup,
1504 .cxl_endpoint_parse_cdat = mock_cxl_endpoint_parse_cdat,
1505 .devm_cxl_add_dport_by_dev = mock_cxl_add_dport_by_dev,
1506 .hmat_get_extended_linear_cache_size =
1507 mock_hmat_get_extended_linear_cache_size,
1508 .walk_hmem_resources = mock_walk_hmem_resources,
1509 .region_intersects = mock_region_intersects,
1510 .region_intersects_soft_reserve = mock_region_intersects_soft_reserve,
1511 .list = LIST_HEAD_INIT(cxl_mock_ops.list),
1512 };
1513
mock_companion(struct acpi_device * adev,struct device * dev)1514 static void mock_companion(struct acpi_device *adev, struct device *dev)
1515 {
1516 device_initialize(&adev->dev);
1517 fwnode_init(&adev->fwnode, NULL);
1518 dev->fwnode = &adev->fwnode;
1519 adev->fwnode.dev = dev;
1520 }
1521
1522 #ifndef SZ_64G
1523 #define SZ_64G (SZ_32G * 2)
1524 #endif
1525
cxl_mock_platform_device_add(struct platform_device * pdev,struct platform_device ** ppdev)1526 static int cxl_mock_platform_device_add(struct platform_device *pdev,
1527 struct platform_device **ppdev)
1528 {
1529 int rc;
1530
1531 if (ppdev)
1532 *ppdev = pdev;
1533 rc = platform_device_add(pdev);
1534 if (rc) {
1535 platform_device_put(pdev);
1536 if (ppdev)
1537 *ppdev = NULL;
1538 }
1539
1540 return rc;
1541 }
1542
cxl_rch_topo_init(void)1543 static __init int cxl_rch_topo_init(void)
1544 {
1545 int rc, i;
1546
1547 for (i = 0; i < ARRAY_SIZE(cxl_rch); i++) {
1548 int idx = NR_CXL_HOST_BRIDGES + NR_CXL_SINGLE_HOST + i;
1549 struct acpi_device *adev = &host_bridge[idx];
1550 struct platform_device *pdev;
1551
1552 pdev = platform_device_alloc("cxl_host_bridge", idx);
1553 if (!pdev)
1554 goto err_bridge;
1555
1556 mock_companion(adev, &pdev->dev);
1557 rc = cxl_mock_platform_device_add(pdev, &cxl_rch[i]);
1558 if (rc)
1559 goto err_bridge;
1560
1561 mock_pci_bus[idx].bridge = &pdev->dev;
1562 rc = sysfs_create_link(&pdev->dev.kobj, &pdev->dev.kobj,
1563 "firmware_node");
1564 if (rc)
1565 goto err_bridge;
1566 }
1567
1568 return 0;
1569
1570 err_bridge:
1571 for (i = ARRAY_SIZE(cxl_rch) - 1; i >= 0; i--) {
1572 struct platform_device *pdev = cxl_rch[i];
1573
1574 if (!pdev)
1575 continue;
1576 sysfs_remove_link(&pdev->dev.kobj, "firmware_node");
1577 platform_device_unregister(cxl_rch[i]);
1578 }
1579
1580 return rc;
1581 }
1582
cxl_rch_topo_exit(void)1583 static void cxl_rch_topo_exit(void)
1584 {
1585 int i;
1586
1587 for (i = ARRAY_SIZE(cxl_rch) - 1; i >= 0; i--) {
1588 struct platform_device *pdev = cxl_rch[i];
1589
1590 if (!pdev)
1591 continue;
1592 sysfs_remove_link(&pdev->dev.kobj, "firmware_node");
1593 platform_device_unregister(cxl_rch[i]);
1594 }
1595 }
1596
cxl_single_topo_init(void)1597 static __init int cxl_single_topo_init(void)
1598 {
1599 int i, rc;
1600
1601 for (i = 0; i < ARRAY_SIZE(cxl_hb_single); i++) {
1602 struct acpi_device *adev =
1603 &host_bridge[NR_CXL_HOST_BRIDGES + i];
1604 struct platform_device *pdev;
1605
1606 pdev = platform_device_alloc("cxl_host_bridge",
1607 NR_CXL_HOST_BRIDGES + i);
1608 if (!pdev)
1609 goto err_bridge;
1610
1611 mock_companion(adev, &pdev->dev);
1612 rc = cxl_mock_platform_device_add(pdev, &cxl_hb_single[i]);
1613 if (rc)
1614 goto err_bridge;
1615
1616 mock_pci_bus[i + NR_CXL_HOST_BRIDGES].bridge = &pdev->dev;
1617 rc = sysfs_create_link(&pdev->dev.kobj, &pdev->dev.kobj,
1618 "physical_node");
1619 if (rc)
1620 goto err_bridge;
1621 }
1622
1623 for (i = 0; i < ARRAY_SIZE(cxl_root_single); i++) {
1624 struct platform_device *bridge =
1625 cxl_hb_single[i % ARRAY_SIZE(cxl_hb_single)];
1626 struct platform_device *pdev;
1627
1628 pdev = platform_device_alloc("cxl_root_port",
1629 NR_MULTI_ROOT + i);
1630 if (!pdev)
1631 goto err_port;
1632 pdev->dev.parent = &bridge->dev;
1633
1634 rc = cxl_mock_platform_device_add(pdev, &cxl_root_single[i]);
1635 if (rc)
1636 goto err_port;
1637 }
1638
1639 for (i = 0; i < ARRAY_SIZE(cxl_swu_single); i++) {
1640 struct platform_device *root_port = cxl_root_single[i];
1641 struct platform_device *pdev;
1642
1643 pdev = platform_device_alloc("cxl_switch_uport",
1644 NR_MULTI_ROOT + i);
1645 if (!pdev)
1646 goto err_uport;
1647 pdev->dev.parent = &root_port->dev;
1648
1649 rc = cxl_mock_platform_device_add(pdev, &cxl_swu_single[i]);
1650 if (rc)
1651 goto err_uport;
1652 }
1653
1654 for (i = 0; i < ARRAY_SIZE(cxl_swd_single); i++) {
1655 struct platform_device *uport =
1656 cxl_swu_single[i % ARRAY_SIZE(cxl_swu_single)];
1657 struct platform_device *pdev;
1658
1659 pdev = platform_device_alloc("cxl_switch_dport",
1660 i + NR_MEM_MULTI);
1661 if (!pdev)
1662 goto err_dport;
1663 pdev->dev.parent = &uport->dev;
1664
1665 rc = cxl_mock_platform_device_add(pdev, &cxl_swd_single[i]);
1666 if (rc)
1667 goto err_dport;
1668 }
1669
1670 return 0;
1671
1672 err_dport:
1673 for (i = ARRAY_SIZE(cxl_swd_single) - 1; i >= 0; i--)
1674 platform_device_unregister(cxl_swd_single[i]);
1675 err_uport:
1676 for (i = ARRAY_SIZE(cxl_swu_single) - 1; i >= 0; i--)
1677 platform_device_unregister(cxl_swu_single[i]);
1678 err_port:
1679 for (i = ARRAY_SIZE(cxl_root_single) - 1; i >= 0; i--)
1680 platform_device_unregister(cxl_root_single[i]);
1681 err_bridge:
1682 for (i = ARRAY_SIZE(cxl_hb_single) - 1; i >= 0; i--) {
1683 struct platform_device *pdev = cxl_hb_single[i];
1684
1685 if (!pdev)
1686 continue;
1687 sysfs_remove_link(&pdev->dev.kobj, "physical_node");
1688 platform_device_unregister(cxl_hb_single[i]);
1689 }
1690
1691 return rc;
1692 }
1693
cxl_single_topo_exit(void)1694 static void cxl_single_topo_exit(void)
1695 {
1696 int i;
1697
1698 for (i = ARRAY_SIZE(cxl_swd_single) - 1; i >= 0; i--)
1699 platform_device_unregister(cxl_swd_single[i]);
1700 for (i = ARRAY_SIZE(cxl_swu_single) - 1; i >= 0; i--)
1701 platform_device_unregister(cxl_swu_single[i]);
1702 for (i = ARRAY_SIZE(cxl_root_single) - 1; i >= 0; i--)
1703 platform_device_unregister(cxl_root_single[i]);
1704 for (i = ARRAY_SIZE(cxl_hb_single) - 1; i >= 0; i--) {
1705 struct platform_device *pdev = cxl_hb_single[i];
1706
1707 if (!pdev)
1708 continue;
1709 sysfs_remove_link(&pdev->dev.kobj, "physical_node");
1710 platform_device_unregister(cxl_hb_single[i]);
1711 }
1712 }
1713
cxl_mem_exit(void)1714 static void cxl_mem_exit(void)
1715 {
1716 int i;
1717
1718 for (i = ARRAY_SIZE(cxl_rcd) - 1; i >= 0; i--)
1719 platform_device_unregister(cxl_rcd[i]);
1720 for (i = ARRAY_SIZE(cxl_mem_single) - 1; i >= 0; i--)
1721 platform_device_unregister(cxl_mem_single[i]);
1722 for (i = ARRAY_SIZE(cxl_mem) - 1; i >= 0; i--)
1723 platform_device_unregister(cxl_mem[i]);
1724 }
1725
cxl_mem_init(void)1726 static int cxl_mem_init(void)
1727 {
1728 int i, rc;
1729
1730 for (i = 0; i < ARRAY_SIZE(cxl_mem); i++) {
1731 struct platform_device *dport = cxl_switch_dport[i];
1732 struct platform_device *pdev;
1733
1734 pdev = platform_device_alloc("cxl_mem", i);
1735 if (!pdev)
1736 goto err_mem;
1737 pdev->dev.parent = &dport->dev;
1738 set_dev_node(&pdev->dev, i % 2);
1739
1740 rc = cxl_mock_platform_device_add(pdev, &cxl_mem[i]);
1741 if (rc)
1742 goto err_mem;
1743 }
1744
1745 for (i = 0; i < ARRAY_SIZE(cxl_mem_single); i++) {
1746 struct platform_device *dport = cxl_swd_single[i];
1747 struct platform_device *pdev;
1748
1749 pdev = platform_device_alloc("cxl_mem", NR_MEM_MULTI + i);
1750 if (!pdev)
1751 goto err_single;
1752 pdev->dev.parent = &dport->dev;
1753 set_dev_node(&pdev->dev, i % 2);
1754
1755 rc = cxl_mock_platform_device_add(pdev, &cxl_mem_single[i]);
1756 if (rc)
1757 goto err_single;
1758 }
1759
1760 for (i = 0; i < ARRAY_SIZE(cxl_rcd); i++) {
1761 int idx = NR_MEM_MULTI + NR_MEM_SINGLE + i;
1762 struct platform_device *rch = cxl_rch[i];
1763 struct platform_device *pdev;
1764
1765 pdev = platform_device_alloc("cxl_rcd", idx);
1766 if (!pdev)
1767 goto err_rcd;
1768 pdev->dev.parent = &rch->dev;
1769 set_dev_node(&pdev->dev, i % 2);
1770
1771 rc = cxl_mock_platform_device_add(pdev, &cxl_rcd[i]);
1772 if (rc)
1773 goto err_rcd;
1774 }
1775
1776 return 0;
1777
1778 err_rcd:
1779 for (i = ARRAY_SIZE(cxl_rcd) - 1; i >= 0; i--)
1780 platform_device_unregister(cxl_rcd[i]);
1781 err_single:
1782 for (i = ARRAY_SIZE(cxl_mem_single) - 1; i >= 0; i--)
1783 platform_device_unregister(cxl_mem_single[i]);
1784 err_mem:
1785 for (i = ARRAY_SIZE(cxl_mem) - 1; i >= 0; i--)
1786 platform_device_unregister(cxl_mem[i]);
1787 return rc;
1788 }
1789
1790 static ssize_t
decoder_reset_preserve_registry_show(struct device * dev,struct device_attribute * attr,char * buf)1791 decoder_reset_preserve_registry_show(struct device *dev,
1792 struct device_attribute *attr, char *buf)
1793 {
1794 return sysfs_emit(buf, "%d\n", decoder_reset_preserve_registry);
1795 }
1796
1797 static ssize_t
decoder_reset_preserve_registry_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1798 decoder_reset_preserve_registry_store(struct device *dev,
1799 struct device_attribute *attr,
1800 const char *buf, size_t count)
1801 {
1802 int rc;
1803
1804 rc = kstrtobool(buf, &decoder_reset_preserve_registry);
1805 if (rc)
1806 return rc;
1807 return count;
1808 }
1809
1810 static DEVICE_ATTR_RW(decoder_reset_preserve_registry);
1811
1812 static struct attribute *cxl_acpi_attrs[] = {
1813 &dev_attr_decoder_reset_preserve_registry.attr, NULL
1814 };
1815 ATTRIBUTE_GROUPS(cxl_acpi);
1816
cxl_test_init(void)1817 static __init int cxl_test_init(void)
1818 {
1819 int rc, i;
1820 struct range mappable;
1821
1822 cxl_acpi_test();
1823 cxl_core_test();
1824 cxl_mem_test();
1825 cxl_pmem_test();
1826 cxl_port_test();
1827
1828 register_cxl_mock_ops(&cxl_mock_ops);
1829
1830 cxl_mock_pool = gen_pool_create(ilog2(SZ_2M), NUMA_NO_NODE);
1831 if (!cxl_mock_pool) {
1832 rc = -ENOMEM;
1833 goto err_gen_pool_create;
1834 }
1835 mappable = mhp_get_pluggable_range(true);
1836
1837 rc = gen_pool_add(cxl_mock_pool,
1838 min(iomem_resource.end + 1 - SZ_64G,
1839 mappable.end + 1 - SZ_64G),
1840 SZ_64G, NUMA_NO_NODE);
1841 if (rc)
1842 goto err_gen_pool_add;
1843
1844 if (interleave_arithmetic == 1) {
1845 cfmws_start = CFMWS_XOR_ARRAY_START;
1846 cfmws_end = CFMWS_XOR_ARRAY_END;
1847 } else {
1848 cfmws_start = CFMWS_MOD_ARRAY_START;
1849 cfmws_end = CFMWS_MOD_ARRAY_END;
1850 }
1851
1852 rc = populate_cedt();
1853 if (rc)
1854 goto err_populate;
1855
1856 for (i = 0; i < ARRAY_SIZE(cxl_host_bridge); i++) {
1857 struct acpi_device *adev = &host_bridge[i];
1858 struct platform_device *pdev;
1859
1860 pdev = platform_device_alloc("cxl_host_bridge", i);
1861 if (!pdev)
1862 goto err_bridge;
1863
1864 mock_companion(adev, &pdev->dev);
1865 rc = cxl_mock_platform_device_add(pdev, &cxl_host_bridge[i]);
1866 if (rc)
1867 goto err_bridge;
1868
1869 mock_pci_bus[i].bridge = &pdev->dev;
1870 rc = sysfs_create_link(&pdev->dev.kobj, &pdev->dev.kobj,
1871 "physical_node");
1872 if (rc)
1873 goto err_bridge;
1874 }
1875
1876 for (i = 0; i < ARRAY_SIZE(cxl_root_port); i++) {
1877 struct platform_device *bridge =
1878 cxl_host_bridge[i % ARRAY_SIZE(cxl_host_bridge)];
1879 struct platform_device *pdev;
1880
1881 pdev = platform_device_alloc("cxl_root_port", i);
1882 if (!pdev)
1883 goto err_port;
1884 pdev->dev.parent = &bridge->dev;
1885
1886 rc = cxl_mock_platform_device_add(pdev, &cxl_root_port[i]);
1887 if (rc)
1888 goto err_port;
1889 }
1890
1891 BUILD_BUG_ON(ARRAY_SIZE(cxl_switch_uport) != ARRAY_SIZE(cxl_root_port));
1892 for (i = 0; i < ARRAY_SIZE(cxl_switch_uport); i++) {
1893 struct platform_device *root_port = cxl_root_port[i];
1894 struct platform_device *pdev;
1895
1896 pdev = platform_device_alloc("cxl_switch_uport", i);
1897 if (!pdev)
1898 goto err_uport;
1899 pdev->dev.parent = &root_port->dev;
1900
1901 rc = cxl_mock_platform_device_add(pdev, &cxl_switch_uport[i]);
1902 if (rc)
1903 goto err_uport;
1904 }
1905
1906 for (i = 0; i < ARRAY_SIZE(cxl_switch_dport); i++) {
1907 struct platform_device *uport =
1908 cxl_switch_uport[i % ARRAY_SIZE(cxl_switch_uport)];
1909 struct platform_device *pdev;
1910
1911 pdev = platform_device_alloc("cxl_switch_dport", i);
1912 if (!pdev)
1913 goto err_dport;
1914 pdev->dev.parent = &uport->dev;
1915
1916 rc = cxl_mock_platform_device_add(pdev, &cxl_switch_dport[i]);
1917 if (rc)
1918 goto err_dport;
1919 }
1920
1921 rc = cxl_single_topo_init();
1922 if (rc)
1923 goto err_dport;
1924
1925 rc = cxl_rch_topo_init();
1926 if (rc)
1927 goto err_single;
1928
1929 cxl_acpi = platform_device_alloc("cxl_acpi", 0);
1930 if (!cxl_acpi)
1931 goto err_rch;
1932
1933 mock_companion(&acpi0017_mock, &cxl_acpi->dev);
1934 acpi0017_mock.dev.bus = &platform_bus_type;
1935 cxl_acpi->dev.groups = cxl_acpi_groups;
1936
1937 rc = cxl_mock_platform_device_add(cxl_acpi, NULL);
1938 if (rc)
1939 goto err_rch;
1940
1941 rc = cxl_mem_init();
1942 if (rc)
1943 goto err_root;
1944
1945 rc = hmem_test_init();
1946 if (rc)
1947 goto err_mem;
1948
1949 return 0;
1950
1951 err_mem:
1952 cxl_mem_exit();
1953 err_root:
1954 platform_device_put(cxl_acpi);
1955 err_rch:
1956 cxl_rch_topo_exit();
1957 err_single:
1958 cxl_single_topo_exit();
1959 err_dport:
1960 for (i = ARRAY_SIZE(cxl_switch_dport) - 1; i >= 0; i--)
1961 platform_device_unregister(cxl_switch_dport[i]);
1962 err_uport:
1963 for (i = ARRAY_SIZE(cxl_switch_uport) - 1; i >= 0; i--)
1964 platform_device_unregister(cxl_switch_uport[i]);
1965 err_port:
1966 for (i = ARRAY_SIZE(cxl_root_port) - 1; i >= 0; i--)
1967 platform_device_unregister(cxl_root_port[i]);
1968 err_bridge:
1969 for (i = ARRAY_SIZE(cxl_host_bridge) - 1; i >= 0; i--) {
1970 struct platform_device *pdev = cxl_host_bridge[i];
1971
1972 if (!pdev)
1973 continue;
1974 sysfs_remove_link(&pdev->dev.kobj, "physical_node");
1975 platform_device_unregister(cxl_host_bridge[i]);
1976 }
1977 err_populate:
1978 depopulate_all_mock_resources();
1979 err_gen_pool_add:
1980 gen_pool_destroy(cxl_mock_pool);
1981 err_gen_pool_create:
1982 unregister_cxl_mock_ops(&cxl_mock_ops);
1983 return rc;
1984 }
1985
free_decoder_registry(void)1986 static void free_decoder_registry(void)
1987 {
1988 unsigned long index;
1989 void *entry;
1990
1991 xa_for_each(&decoder_registry, index, entry) {
1992 xa_erase(&decoder_registry, index);
1993 kfree(entry);
1994 }
1995 }
1996
cxl_test_exit(void)1997 static __exit void cxl_test_exit(void)
1998 {
1999 int i;
2000
2001 hmem_test_exit();
2002 cxl_mem_exit();
2003 platform_device_unregister(cxl_acpi);
2004 cxl_rch_topo_exit();
2005 cxl_single_topo_exit();
2006 for (i = ARRAY_SIZE(cxl_switch_dport) - 1; i >= 0; i--)
2007 platform_device_unregister(cxl_switch_dport[i]);
2008 for (i = ARRAY_SIZE(cxl_switch_uport) - 1; i >= 0; i--)
2009 platform_device_unregister(cxl_switch_uport[i]);
2010 for (i = ARRAY_SIZE(cxl_root_port) - 1; i >= 0; i--)
2011 platform_device_unregister(cxl_root_port[i]);
2012 for (i = ARRAY_SIZE(cxl_host_bridge) - 1; i >= 0; i--) {
2013 struct platform_device *pdev = cxl_host_bridge[i];
2014
2015 if (!pdev)
2016 continue;
2017 sysfs_remove_link(&pdev->dev.kobj, "physical_node");
2018 platform_device_unregister(cxl_host_bridge[i]);
2019 }
2020 depopulate_all_mock_resources();
2021 gen_pool_destroy(cxl_mock_pool);
2022 unregister_cxl_mock_ops(&cxl_mock_ops);
2023 free_decoder_registry();
2024 xa_destroy(&decoder_registry);
2025 }
2026
2027 module_param(interleave_arithmetic, int, 0444);
2028 MODULE_PARM_DESC(interleave_arithmetic, "Modulo:0, XOR:1");
2029 module_param(extended_linear_cache, bool, 0444);
2030 MODULE_PARM_DESC(extended_linear_cache, "Enable extended linear cache support");
2031 module_param(fail_autoassemble, bool, 0444);
2032 MODULE_PARM_DESC(fail_autoassemble, "Simulate missing member of an auto-region");
2033 module_init(cxl_test_init);
2034 module_exit(cxl_test_exit);
2035 MODULE_LICENSE("GPL v2");
2036 MODULE_DESCRIPTION("cxl_test: setup module");
2037 MODULE_IMPORT_NS("ACPI");
2038 MODULE_IMPORT_NS("CXL");
2039