1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright(c) 2021 Intel Corporation. All rights reserved.
4 #include <linux/platform_device.h>
5 #include <linux/genalloc.h>
6 #include <linux/module.h>
7 #include <linux/mutex.h>
8 #include <linux/acpi.h>
13 #include "../watermark.h"
16 static int interleave_arithmetic;
18 #define NR_CXL_HOST_BRIDGES 2
19 #define NR_CXL_SINGLE_HOST 1
21 #define NR_CXL_ROOT_PORTS 2
22 #define NR_CXL_SWITCH_PORTS 2
23 #define NR_CXL_PORT_DECODERS 8
24 #define NR_BRIDGES (NR_CXL_HOST_BRIDGES + NR_CXL_SINGLE_HOST + NR_CXL_RCH)
26 static struct platform_device *cxl_acpi;
27 static struct platform_device *cxl_host_bridge[NR_CXL_HOST_BRIDGES];
28 #define NR_MULTI_ROOT (NR_CXL_HOST_BRIDGES * NR_CXL_ROOT_PORTS)
29 static struct platform_device *cxl_root_port[NR_MULTI_ROOT];
30 static struct platform_device *cxl_switch_uport[NR_MULTI_ROOT];
31 #define NR_MEM_MULTI \
32 (NR_CXL_HOST_BRIDGES * NR_CXL_ROOT_PORTS * NR_CXL_SWITCH_PORTS)
33 static struct platform_device *cxl_switch_dport[NR_MEM_MULTI];
35 static struct platform_device *cxl_hb_single[NR_CXL_SINGLE_HOST];
36 static struct platform_device *cxl_root_single[NR_CXL_SINGLE_HOST];
37 static struct platform_device *cxl_swu_single[NR_CXL_SINGLE_HOST];
38 #define NR_MEM_SINGLE (NR_CXL_SINGLE_HOST * NR_CXL_SWITCH_PORTS)
39 static struct platform_device *cxl_swd_single[NR_MEM_SINGLE];
41 struct platform_device *cxl_mem[NR_MEM_MULTI];
42 struct platform_device *cxl_mem_single[NR_MEM_SINGLE];
44 static struct platform_device *cxl_rch[NR_CXL_RCH];
45 static struct platform_device *cxl_rcd[NR_CXL_RCH];
47 static inline bool is_multi_bridge(struct device *dev)
51 for (i = 0; i < ARRAY_SIZE(cxl_host_bridge); i++)
52 if (&cxl_host_bridge[i]->dev == dev)
57 static inline bool is_single_bridge(struct device *dev)
61 for (i = 0; i < ARRAY_SIZE(cxl_hb_single); i++)
62 if (&cxl_hb_single[i]->dev == dev)
67 static struct acpi_device acpi0017_mock;
68 static struct acpi_device host_bridge[NR_BRIDGES] = {
70 .handle = &host_bridge[0],
73 .handle = &host_bridge[1],
76 .handle = &host_bridge[2],
79 .handle = &host_bridge[3],
83 static bool is_mock_dev(struct device *dev)
87 for (i = 0; i < ARRAY_SIZE(cxl_mem); i++)
88 if (dev == &cxl_mem[i]->dev)
90 for (i = 0; i < ARRAY_SIZE(cxl_mem_single); i++)
91 if (dev == &cxl_mem_single[i]->dev)
93 for (i = 0; i < ARRAY_SIZE(cxl_rcd); i++)
94 if (dev == &cxl_rcd[i]->dev)
96 if (dev == &cxl_acpi->dev)
101 static bool is_mock_adev(struct acpi_device *adev)
105 if (adev == &acpi0017_mock)
108 for (i = 0; i < ARRAY_SIZE(host_bridge); i++)
109 if (adev == &host_bridge[i])
116 struct acpi_table_cedt cedt;
117 struct acpi_cedt_chbs chbs[NR_BRIDGES];
119 struct acpi_cedt_cfmws cfmws;
123 struct acpi_cedt_cfmws cfmws;
127 struct acpi_cedt_cfmws cfmws;
131 struct acpi_cedt_cfmws cfmws;
135 struct acpi_cedt_cfmws cfmws;
139 struct acpi_cedt_cfmws cfmws;
143 struct acpi_cedt_cfmws cfmws;
147 struct acpi_cedt_cfmws cfmws;
151 struct acpi_cedt_cfmws cfmws;
155 struct acpi_cedt_cxims cxims;
158 } __packed mock_cedt = {
162 .length = sizeof(mock_cedt),
168 .type = ACPI_CEDT_TYPE_CHBS,
169 .length = sizeof(mock_cedt.chbs[0]),
172 .cxl_version = ACPI_CEDT_CHBS_VERSION_CXL20,
176 .type = ACPI_CEDT_TYPE_CHBS,
177 .length = sizeof(mock_cedt.chbs[0]),
180 .cxl_version = ACPI_CEDT_CHBS_VERSION_CXL20,
184 .type = ACPI_CEDT_TYPE_CHBS,
185 .length = sizeof(mock_cedt.chbs[0]),
188 .cxl_version = ACPI_CEDT_CHBS_VERSION_CXL20,
192 .type = ACPI_CEDT_TYPE_CHBS,
193 .length = sizeof(mock_cedt.chbs[0]),
196 .cxl_version = ACPI_CEDT_CHBS_VERSION_CXL11,
201 .type = ACPI_CEDT_TYPE_CFMWS,
202 .length = sizeof(mock_cedt.cfmws0),
204 .interleave_ways = 0,
206 .restrictions = ACPI_CEDT_CFMWS_RESTRICT_TYPE3 |
207 ACPI_CEDT_CFMWS_RESTRICT_VOLATILE,
209 .window_size = SZ_256M * 4UL,
216 .type = ACPI_CEDT_TYPE_CFMWS,
217 .length = sizeof(mock_cedt.cfmws1),
219 .interleave_ways = 1,
221 .restrictions = ACPI_CEDT_CFMWS_RESTRICT_TYPE3 |
222 ACPI_CEDT_CFMWS_RESTRICT_VOLATILE,
224 .window_size = SZ_256M * 8UL,
231 .type = ACPI_CEDT_TYPE_CFMWS,
232 .length = sizeof(mock_cedt.cfmws2),
234 .interleave_ways = 0,
236 .restrictions = ACPI_CEDT_CFMWS_RESTRICT_TYPE3 |
237 ACPI_CEDT_CFMWS_RESTRICT_PMEM,
239 .window_size = SZ_256M * 4UL,
246 .type = ACPI_CEDT_TYPE_CFMWS,
247 .length = sizeof(mock_cedt.cfmws3),
249 .interleave_ways = 1,
251 .restrictions = ACPI_CEDT_CFMWS_RESTRICT_TYPE3 |
252 ACPI_CEDT_CFMWS_RESTRICT_PMEM,
254 .window_size = SZ_256M * 8UL,
261 .type = ACPI_CEDT_TYPE_CFMWS,
262 .length = sizeof(mock_cedt.cfmws4),
264 .interleave_ways = 0,
266 .restrictions = ACPI_CEDT_CFMWS_RESTRICT_TYPE3 |
267 ACPI_CEDT_CFMWS_RESTRICT_PMEM,
269 .window_size = SZ_256M * 4UL,
276 .type = ACPI_CEDT_TYPE_CFMWS,
277 .length = sizeof(mock_cedt.cfmws5),
279 .interleave_ways = 0,
281 .restrictions = ACPI_CEDT_CFMWS_RESTRICT_TYPE3 |
282 ACPI_CEDT_CFMWS_RESTRICT_VOLATILE,
284 .window_size = SZ_256M,
288 /* .cfmws6,7,8 use ACPI_CEDT_CFMWS_ARITHMETIC_XOR */
292 .type = ACPI_CEDT_TYPE_CFMWS,
293 .length = sizeof(mock_cedt.cfmws6),
295 .interleave_arithmetic = ACPI_CEDT_CFMWS_ARITHMETIC_XOR,
296 .interleave_ways = 0,
298 .restrictions = ACPI_CEDT_CFMWS_RESTRICT_TYPE3 |
299 ACPI_CEDT_CFMWS_RESTRICT_PMEM,
301 .window_size = SZ_256M * 8UL,
308 .type = ACPI_CEDT_TYPE_CFMWS,
309 .length = sizeof(mock_cedt.cfmws7),
311 .interleave_arithmetic = ACPI_CEDT_CFMWS_ARITHMETIC_XOR,
312 .interleave_ways = 1,
314 .restrictions = ACPI_CEDT_CFMWS_RESTRICT_TYPE3 |
315 ACPI_CEDT_CFMWS_RESTRICT_PMEM,
317 .window_size = SZ_256M * 8UL,
324 .type = ACPI_CEDT_TYPE_CFMWS,
325 .length = sizeof(mock_cedt.cfmws8),
327 .interleave_arithmetic = ACPI_CEDT_CFMWS_ARITHMETIC_XOR,
328 .interleave_ways = 2,
330 .restrictions = ACPI_CEDT_CFMWS_RESTRICT_TYPE3 |
331 ACPI_CEDT_CFMWS_RESTRICT_PMEM,
333 .window_size = SZ_256M * 16UL,
335 .target = { 0, 1, 0, 1, },
340 .type = ACPI_CEDT_TYPE_CXIMS,
341 .length = sizeof(mock_cedt.cxims0),
346 .xormap_list = { 0x404100, 0x808200, },
350 struct acpi_cedt_cfmws *mock_cfmws[] = {
351 [0] = &mock_cedt.cfmws0.cfmws,
352 [1] = &mock_cedt.cfmws1.cfmws,
353 [2] = &mock_cedt.cfmws2.cfmws,
354 [3] = &mock_cedt.cfmws3.cfmws,
355 [4] = &mock_cedt.cfmws4.cfmws,
356 [5] = &mock_cedt.cfmws5.cfmws,
357 /* Modulo Math above, XOR Math below */
358 [6] = &mock_cedt.cfmws6.cfmws,
359 [7] = &mock_cedt.cfmws7.cfmws,
360 [8] = &mock_cedt.cfmws8.cfmws,
363 static int cfmws_start;
364 static int cfmws_end;
365 #define CFMWS_MOD_ARRAY_START 0
366 #define CFMWS_MOD_ARRAY_END 5
367 #define CFMWS_XOR_ARRAY_START 6
368 #define CFMWS_XOR_ARRAY_END 8
370 struct acpi_cedt_cxims *mock_cxims[1] = {
371 [0] = &mock_cedt.cxims0.cxims,
374 struct cxl_mock_res {
375 struct list_head list;
379 static LIST_HEAD(mock_res);
380 static DEFINE_MUTEX(mock_res_lock);
381 static struct gen_pool *cxl_mock_pool;
383 static void depopulate_all_mock_resources(void)
385 struct cxl_mock_res *res, *_res;
387 mutex_lock(&mock_res_lock);
388 list_for_each_entry_safe(res, _res, &mock_res, list) {
389 gen_pool_free(cxl_mock_pool, res->range.start,
390 range_len(&res->range));
391 list_del(&res->list);
394 mutex_unlock(&mock_res_lock);
397 static struct cxl_mock_res *alloc_mock_res(resource_size_t size, int align)
399 struct cxl_mock_res *res = kzalloc(sizeof(*res), GFP_KERNEL);
400 struct genpool_data_align data = {
405 INIT_LIST_HEAD(&res->list);
406 phys = gen_pool_alloc_algo(cxl_mock_pool, size,
407 gen_pool_first_fit_align, &data);
411 res->range = (struct range) {
413 .end = phys + size - 1,
415 mutex_lock(&mock_res_lock);
416 list_add(&res->list, &mock_res);
417 mutex_unlock(&mock_res_lock);
422 static int populate_cedt(void)
424 struct cxl_mock_res *res;
427 for (i = 0; i < ARRAY_SIZE(mock_cedt.chbs); i++) {
428 struct acpi_cedt_chbs *chbs = &mock_cedt.chbs[i];
429 resource_size_t size;
431 if (chbs->cxl_version == ACPI_CEDT_CHBS_VERSION_CXL20)
432 size = ACPI_CEDT_CHBS_LENGTH_CXL20;
434 size = ACPI_CEDT_CHBS_LENGTH_CXL11;
436 res = alloc_mock_res(size, size);
439 chbs->base = res->range.start;
443 for (i = cfmws_start; i <= cfmws_end; i++) {
444 struct acpi_cedt_cfmws *window = mock_cfmws[i];
446 res = alloc_mock_res(window->window_size, SZ_256M);
449 window->base_hpa = res->range.start;
455 static bool is_mock_port(struct device *dev);
458 * WARNING, this hack assumes the format of 'struct cxl_cfmws_context'
459 * and 'struct cxl_chbs_context' share the property that the first
460 * struct member is a cxl_test device being probed by the cxl_acpi
463 struct cxl_cedt_context {
467 static int mock_acpi_table_parse_cedt(enum acpi_cedt_type id,
468 acpi_tbl_entry_handler_arg handler_arg,
471 struct cxl_cedt_context *ctx = arg;
472 struct device *dev = ctx->dev;
473 union acpi_subtable_headers *h;
477 if (!is_mock_port(dev) && !is_mock_dev(dev))
478 return acpi_table_parse_cedt(id, handler_arg, arg);
480 if (id == ACPI_CEDT_TYPE_CHBS)
481 for (i = 0; i < ARRAY_SIZE(mock_cedt.chbs); i++) {
482 h = (union acpi_subtable_headers *)&mock_cedt.chbs[i];
483 end = (unsigned long)&mock_cedt.chbs[i + 1];
484 handler_arg(h, arg, end);
487 if (id == ACPI_CEDT_TYPE_CFMWS)
488 for (i = cfmws_start; i <= cfmws_end; i++) {
489 h = (union acpi_subtable_headers *) mock_cfmws[i];
490 end = (unsigned long) h + mock_cfmws[i]->header.length;
491 handler_arg(h, arg, end);
494 if (id == ACPI_CEDT_TYPE_CXIMS)
495 for (i = 0; i < ARRAY_SIZE(mock_cxims); i++) {
496 h = (union acpi_subtable_headers *)mock_cxims[i];
497 end = (unsigned long)h + mock_cxims[i]->header.length;
498 handler_arg(h, arg, end);
504 static bool is_mock_bridge(struct device *dev)
508 for (i = 0; i < ARRAY_SIZE(cxl_host_bridge); i++)
509 if (dev == &cxl_host_bridge[i]->dev)
511 for (i = 0; i < ARRAY_SIZE(cxl_hb_single); i++)
512 if (dev == &cxl_hb_single[i]->dev)
514 for (i = 0; i < ARRAY_SIZE(cxl_rch); i++)
515 if (dev == &cxl_rch[i]->dev)
521 static bool is_mock_port(struct device *dev)
525 if (is_mock_bridge(dev))
528 for (i = 0; i < ARRAY_SIZE(cxl_root_port); i++)
529 if (dev == &cxl_root_port[i]->dev)
532 for (i = 0; i < ARRAY_SIZE(cxl_switch_uport); i++)
533 if (dev == &cxl_switch_uport[i]->dev)
536 for (i = 0; i < ARRAY_SIZE(cxl_switch_dport); i++)
537 if (dev == &cxl_switch_dport[i]->dev)
540 for (i = 0; i < ARRAY_SIZE(cxl_root_single); i++)
541 if (dev == &cxl_root_single[i]->dev)
544 for (i = 0; i < ARRAY_SIZE(cxl_swu_single); i++)
545 if (dev == &cxl_swu_single[i]->dev)
548 for (i = 0; i < ARRAY_SIZE(cxl_swd_single); i++)
549 if (dev == &cxl_swd_single[i]->dev)
552 if (is_cxl_memdev(dev))
553 return is_mock_dev(dev->parent);
558 static int host_bridge_index(struct acpi_device *adev)
560 return adev - host_bridge;
563 static struct acpi_device *find_host_bridge(acpi_handle handle)
567 for (i = 0; i < ARRAY_SIZE(host_bridge); i++)
568 if (handle == host_bridge[i].handle)
569 return &host_bridge[i];
574 mock_acpi_evaluate_integer(acpi_handle handle, acpi_string pathname,
575 struct acpi_object_list *arguments,
576 unsigned long long *data)
578 struct acpi_device *adev = find_host_bridge(handle);
580 if (!adev || strcmp(pathname, METHOD_NAME__UID) != 0)
581 return acpi_evaluate_integer(handle, pathname, arguments, data);
583 *data = host_bridge_index(adev);
587 static struct pci_bus mock_pci_bus[NR_BRIDGES];
588 static struct acpi_pci_root mock_pci_root[ARRAY_SIZE(mock_pci_bus)] = {
590 .bus = &mock_pci_bus[0],
593 .bus = &mock_pci_bus[1],
596 .bus = &mock_pci_bus[2],
599 .bus = &mock_pci_bus[3],
604 static bool is_mock_bus(struct pci_bus *bus)
608 for (i = 0; i < ARRAY_SIZE(mock_pci_bus); i++)
609 if (bus == &mock_pci_bus[i])
614 static struct acpi_pci_root *mock_acpi_pci_find_root(acpi_handle handle)
616 struct acpi_device *adev = find_host_bridge(handle);
619 return acpi_pci_find_root(handle);
620 return &mock_pci_root[host_bridge_index(adev)];
623 static struct cxl_hdm *mock_cxl_setup_hdm(struct cxl_port *port,
624 struct cxl_endpoint_dvsec_info *info)
626 struct cxl_hdm *cxlhdm = devm_kzalloc(&port->dev, sizeof(*cxlhdm), GFP_KERNEL);
629 return ERR_PTR(-ENOMEM);
635 static int mock_cxl_add_passthrough_decoder(struct cxl_port *port)
637 dev_err(&port->dev, "unexpected passthrough decoder for cxl_test\n");
642 struct target_map_ctx {
648 static int map_targets(struct device *dev, void *data)
650 struct platform_device *pdev = to_platform_device(dev);
651 struct target_map_ctx *ctx = data;
653 ctx->target_map[ctx->index++] = pdev->id;
655 if (ctx->index > ctx->target_count) {
656 dev_WARN_ONCE(dev, 1, "too many targets found?\n");
663 static int mock_decoder_commit(struct cxl_decoder *cxld)
665 struct cxl_port *port = to_cxl_port(cxld->dev.parent);
668 if (cxld->flags & CXL_DECODER_F_ENABLE)
671 dev_dbg(&port->dev, "%s commit\n", dev_name(&cxld->dev));
672 if (port->commit_end + 1 != id) {
674 "%s: out of order commit, expected decoder%d.%d\n",
675 dev_name(&cxld->dev), port->id, port->commit_end + 1);
680 cxld->flags |= CXL_DECODER_F_ENABLE;
685 static int mock_decoder_reset(struct cxl_decoder *cxld)
687 struct cxl_port *port = to_cxl_port(cxld->dev.parent);
690 if ((cxld->flags & CXL_DECODER_F_ENABLE) == 0)
693 dev_dbg(&port->dev, "%s reset\n", dev_name(&cxld->dev));
694 if (port->commit_end != id) {
696 "%s: out of order reset, expected decoder%d.%d\n",
697 dev_name(&cxld->dev), port->id, port->commit_end);
702 cxld->flags &= ~CXL_DECODER_F_ENABLE;
707 static void default_mock_decoder(struct cxl_decoder *cxld)
709 cxld->hpa_range = (struct range){
714 cxld->interleave_ways = 1;
715 cxld->interleave_granularity = 256;
716 cxld->target_type = CXL_DECODER_HOSTONLYMEM;
717 cxld->commit = mock_decoder_commit;
718 cxld->reset = mock_decoder_reset;
721 static int first_decoder(struct device *dev, void *data)
723 struct cxl_decoder *cxld;
725 if (!is_switch_decoder(dev))
727 cxld = to_cxl_decoder(dev);
733 static void mock_init_hdm_decoder(struct cxl_decoder *cxld)
735 struct acpi_cedt_cfmws *window = mock_cfmws[0];
736 struct platform_device *pdev = NULL;
737 struct cxl_endpoint_decoder *cxled;
738 struct cxl_switch_decoder *cxlsd;
739 struct cxl_port *port, *iter;
740 const int size = SZ_512M;
741 struct cxl_memdev *cxlmd;
742 struct cxl_dport *dport;
748 if (is_endpoint_decoder(&cxld->dev)) {
749 cxled = to_cxl_endpoint_decoder(&cxld->dev);
750 cxlmd = cxled_to_memdev(cxled);
751 WARN_ON(!dev_is_platform(cxlmd->dev.parent));
752 pdev = to_platform_device(cxlmd->dev.parent);
754 /* check is endpoint is attach to host-bridge0 */
755 port = cxled_to_port(cxled);
757 if (port->uport_dev == &cxl_host_bridge[0]->dev) {
761 if (is_cxl_port(port->dev.parent))
762 port = to_cxl_port(port->dev.parent);
766 port = cxled_to_port(cxled);
770 * The first decoder on the first 2 devices on the first switch
771 * attached to host-bridge0 mock a fake / static RAM region. All
772 * other decoders are default disabled. Given the round robin
773 * assignment those devices are named cxl_mem.0, and cxl_mem.4.
775 * See 'cxl list -BMPu -m cxl_mem.0,cxl_mem.4'
777 if (!hb0 || pdev->id % 4 || pdev->id > 4 || cxld->id > 0) {
778 default_mock_decoder(cxld);
782 base = window->base_hpa;
783 cxld->hpa_range = (struct range) {
785 .end = base + size - 1,
788 cxld->interleave_ways = 2;
789 eig_to_granularity(window->granularity, &cxld->interleave_granularity);
790 cxld->target_type = CXL_DECODER_HOSTONLYMEM;
791 cxld->flags = CXL_DECODER_F_ENABLE;
792 cxled->state = CXL_DECODER_STATE_AUTO;
793 port->commit_end = cxld->id;
794 devm_cxl_dpa_reserve(cxled, 0, size / cxld->interleave_ways, 0);
795 cxld->commit = mock_decoder_commit;
796 cxld->reset = mock_decoder_reset;
799 * Now that endpoint decoder is set up, walk up the hierarchy
800 * and setup the switch and root port decoders targeting @cxlmd.
803 for (i = 0; i < 2; i++) {
804 dport = iter->parent_dport;
806 dev = device_find_child(&iter->dev, NULL, first_decoder);
808 * Ancestor ports are guaranteed to be enumerated before
809 * @port, and all ports have at least one decoder.
813 cxlsd = to_cxl_switch_decoder(dev);
815 /* put cxl_mem.4 second in the decode order */
817 cxlsd->target[1] = dport;
819 cxlsd->target[0] = dport;
821 cxlsd->target[0] = dport;
823 cxld->target_type = CXL_DECODER_HOSTONLYMEM;
824 cxld->flags = CXL_DECODER_F_ENABLE;
825 iter->commit_end = 0;
827 * Switch targets 2 endpoints, while host bridge targets
831 cxld->interleave_ways = 2;
833 cxld->interleave_ways = 1;
834 cxld->interleave_granularity = 256;
835 cxld->hpa_range = (struct range) {
837 .end = base + size - 1,
843 static int mock_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm,
844 struct cxl_endpoint_dvsec_info *info)
846 struct cxl_port *port = cxlhdm->port;
847 struct cxl_port *parent_port = to_cxl_port(port->dev.parent);
850 if (is_cxl_endpoint(port))
852 else if (is_cxl_root(parent_port))
853 target_count = NR_CXL_ROOT_PORTS;
855 target_count = NR_CXL_SWITCH_PORTS;
857 for (i = 0; i < NR_CXL_PORT_DECODERS; i++) {
858 int target_map[CXL_DECODER_MAX_INTERLEAVE] = { 0 };
859 struct target_map_ctx ctx = {
860 .target_map = target_map,
861 .target_count = target_count,
863 struct cxl_decoder *cxld;
867 struct cxl_switch_decoder *cxlsd;
869 cxlsd = cxl_switch_decoder_alloc(port, target_count);
872 "Failed to allocate the decoder\n");
873 return PTR_ERR(cxlsd);
877 struct cxl_endpoint_decoder *cxled;
879 cxled = cxl_endpoint_decoder_alloc(port);
883 "Failed to allocate the decoder\n");
884 return PTR_ERR(cxled);
889 mock_init_hdm_decoder(cxld);
892 rc = device_for_each_child(port->uport_dev, &ctx,
895 put_device(&cxld->dev);
900 rc = cxl_decoder_add_locked(cxld, target_map);
902 put_device(&cxld->dev);
903 dev_err(&port->dev, "Failed to add decoder\n");
907 rc = cxl_decoder_autoremove(&port->dev, cxld);
910 dev_dbg(&cxld->dev, "Added to port %s\n", dev_name(&port->dev));
916 static int mock_cxl_port_enumerate_dports(struct cxl_port *port)
918 struct platform_device **array;
921 if (port->depth == 1) {
922 if (is_multi_bridge(port->uport_dev)) {
923 array_size = ARRAY_SIZE(cxl_root_port);
924 array = cxl_root_port;
925 } else if (is_single_bridge(port->uport_dev)) {
926 array_size = ARRAY_SIZE(cxl_root_single);
927 array = cxl_root_single;
929 dev_dbg(&port->dev, "%s: unknown bridge type\n",
930 dev_name(port->uport_dev));
933 } else if (port->depth == 2) {
934 struct cxl_port *parent = to_cxl_port(port->dev.parent);
936 if (is_multi_bridge(parent->uport_dev)) {
937 array_size = ARRAY_SIZE(cxl_switch_dport);
938 array = cxl_switch_dport;
939 } else if (is_single_bridge(parent->uport_dev)) {
940 array_size = ARRAY_SIZE(cxl_swd_single);
941 array = cxl_swd_single;
943 dev_dbg(&port->dev, "%s: unknown bridge type\n",
944 dev_name(port->uport_dev));
948 dev_WARN_ONCE(&port->dev, 1, "unexpected depth %d\n",
953 for (i = 0; i < array_size; i++) {
954 struct platform_device *pdev = array[i];
955 struct cxl_dport *dport;
957 if (pdev->dev.parent != port->uport_dev) {
958 dev_dbg(&port->dev, "%s: mismatch parent %s\n",
959 dev_name(port->uport_dev),
960 dev_name(pdev->dev.parent));
964 dport = devm_cxl_add_dport(port, &pdev->dev, pdev->id,
968 return PTR_ERR(dport);
974 static struct cxl_mock_ops cxl_mock_ops = {
975 .is_mock_adev = is_mock_adev,
976 .is_mock_bridge = is_mock_bridge,
977 .is_mock_bus = is_mock_bus,
978 .is_mock_port = is_mock_port,
979 .is_mock_dev = is_mock_dev,
980 .acpi_table_parse_cedt = mock_acpi_table_parse_cedt,
981 .acpi_evaluate_integer = mock_acpi_evaluate_integer,
982 .acpi_pci_find_root = mock_acpi_pci_find_root,
983 .devm_cxl_port_enumerate_dports = mock_cxl_port_enumerate_dports,
984 .devm_cxl_setup_hdm = mock_cxl_setup_hdm,
985 .devm_cxl_add_passthrough_decoder = mock_cxl_add_passthrough_decoder,
986 .devm_cxl_enumerate_decoders = mock_cxl_enumerate_decoders,
987 .list = LIST_HEAD_INIT(cxl_mock_ops.list),
990 static void mock_companion(struct acpi_device *adev, struct device *dev)
992 device_initialize(&adev->dev);
993 fwnode_init(&adev->fwnode, NULL);
994 dev->fwnode = &adev->fwnode;
995 adev->fwnode.dev = dev;
999 #define SZ_64G (SZ_32G * 2)
1003 #define SZ_512G (SZ_64G * 8)
1006 static __init int cxl_rch_init(void)
1010 for (i = 0; i < ARRAY_SIZE(cxl_rch); i++) {
1011 int idx = NR_CXL_HOST_BRIDGES + NR_CXL_SINGLE_HOST + i;
1012 struct acpi_device *adev = &host_bridge[idx];
1013 struct platform_device *pdev;
1015 pdev = platform_device_alloc("cxl_host_bridge", idx);
1019 mock_companion(adev, &pdev->dev);
1020 rc = platform_device_add(pdev);
1022 platform_device_put(pdev);
1027 mock_pci_bus[idx].bridge = &pdev->dev;
1028 rc = sysfs_create_link(&pdev->dev.kobj, &pdev->dev.kobj,
1034 for (i = 0; i < ARRAY_SIZE(cxl_rcd); i++) {
1035 int idx = NR_MEM_MULTI + NR_MEM_SINGLE + i;
1036 struct platform_device *rch = cxl_rch[i];
1037 struct platform_device *pdev;
1039 pdev = platform_device_alloc("cxl_rcd", idx);
1042 pdev->dev.parent = &rch->dev;
1043 set_dev_node(&pdev->dev, i % 2);
1045 rc = platform_device_add(pdev);
1047 platform_device_put(pdev);
1056 for (i = ARRAY_SIZE(cxl_rcd) - 1; i >= 0; i--)
1057 platform_device_unregister(cxl_rcd[i]);
1059 for (i = ARRAY_SIZE(cxl_rch) - 1; i >= 0; i--) {
1060 struct platform_device *pdev = cxl_rch[i];
1064 sysfs_remove_link(&pdev->dev.kobj, "firmware_node");
1065 platform_device_unregister(cxl_rch[i]);
1071 static void cxl_rch_exit(void)
1075 for (i = ARRAY_SIZE(cxl_rcd) - 1; i >= 0; i--)
1076 platform_device_unregister(cxl_rcd[i]);
1077 for (i = ARRAY_SIZE(cxl_rch) - 1; i >= 0; i--) {
1078 struct platform_device *pdev = cxl_rch[i];
1082 sysfs_remove_link(&pdev->dev.kobj, "firmware_node");
1083 platform_device_unregister(cxl_rch[i]);
1087 static __init int cxl_single_init(void)
1091 for (i = 0; i < ARRAY_SIZE(cxl_hb_single); i++) {
1092 struct acpi_device *adev =
1093 &host_bridge[NR_CXL_HOST_BRIDGES + i];
1094 struct platform_device *pdev;
1096 pdev = platform_device_alloc("cxl_host_bridge",
1097 NR_CXL_HOST_BRIDGES + i);
1101 mock_companion(adev, &pdev->dev);
1102 rc = platform_device_add(pdev);
1104 platform_device_put(pdev);
1108 cxl_hb_single[i] = pdev;
1109 mock_pci_bus[i + NR_CXL_HOST_BRIDGES].bridge = &pdev->dev;
1110 rc = sysfs_create_link(&pdev->dev.kobj, &pdev->dev.kobj,
1116 for (i = 0; i < ARRAY_SIZE(cxl_root_single); i++) {
1117 struct platform_device *bridge =
1118 cxl_hb_single[i % ARRAY_SIZE(cxl_hb_single)];
1119 struct platform_device *pdev;
1121 pdev = platform_device_alloc("cxl_root_port",
1125 pdev->dev.parent = &bridge->dev;
1127 rc = platform_device_add(pdev);
1129 platform_device_put(pdev);
1132 cxl_root_single[i] = pdev;
1135 for (i = 0; i < ARRAY_SIZE(cxl_swu_single); i++) {
1136 struct platform_device *root_port = cxl_root_single[i];
1137 struct platform_device *pdev;
1139 pdev = platform_device_alloc("cxl_switch_uport",
1143 pdev->dev.parent = &root_port->dev;
1145 rc = platform_device_add(pdev);
1147 platform_device_put(pdev);
1150 cxl_swu_single[i] = pdev;
1153 for (i = 0; i < ARRAY_SIZE(cxl_swd_single); i++) {
1154 struct platform_device *uport =
1155 cxl_swu_single[i % ARRAY_SIZE(cxl_swu_single)];
1156 struct platform_device *pdev;
1158 pdev = platform_device_alloc("cxl_switch_dport",
1162 pdev->dev.parent = &uport->dev;
1164 rc = platform_device_add(pdev);
1166 platform_device_put(pdev);
1169 cxl_swd_single[i] = pdev;
1172 for (i = 0; i < ARRAY_SIZE(cxl_mem_single); i++) {
1173 struct platform_device *dport = cxl_swd_single[i];
1174 struct platform_device *pdev;
1176 pdev = platform_device_alloc("cxl_mem", NR_MEM_MULTI + i);
1179 pdev->dev.parent = &dport->dev;
1180 set_dev_node(&pdev->dev, i % 2);
1182 rc = platform_device_add(pdev);
1184 platform_device_put(pdev);
1187 cxl_mem_single[i] = pdev;
1193 for (i = ARRAY_SIZE(cxl_mem_single) - 1; i >= 0; i--)
1194 platform_device_unregister(cxl_mem_single[i]);
1196 for (i = ARRAY_SIZE(cxl_swd_single) - 1; i >= 0; i--)
1197 platform_device_unregister(cxl_swd_single[i]);
1199 for (i = ARRAY_SIZE(cxl_swu_single) - 1; i >= 0; i--)
1200 platform_device_unregister(cxl_swu_single[i]);
1202 for (i = ARRAY_SIZE(cxl_root_single) - 1; i >= 0; i--)
1203 platform_device_unregister(cxl_root_single[i]);
1205 for (i = ARRAY_SIZE(cxl_hb_single) - 1; i >= 0; i--) {
1206 struct platform_device *pdev = cxl_hb_single[i];
1210 sysfs_remove_link(&pdev->dev.kobj, "physical_node");
1211 platform_device_unregister(cxl_hb_single[i]);
1217 static void cxl_single_exit(void)
1221 for (i = ARRAY_SIZE(cxl_mem_single) - 1; i >= 0; i--)
1222 platform_device_unregister(cxl_mem_single[i]);
1223 for (i = ARRAY_SIZE(cxl_swd_single) - 1; i >= 0; i--)
1224 platform_device_unregister(cxl_swd_single[i]);
1225 for (i = ARRAY_SIZE(cxl_swu_single) - 1; i >= 0; i--)
1226 platform_device_unregister(cxl_swu_single[i]);
1227 for (i = ARRAY_SIZE(cxl_root_single) - 1; i >= 0; i--)
1228 platform_device_unregister(cxl_root_single[i]);
1229 for (i = ARRAY_SIZE(cxl_hb_single) - 1; i >= 0; i--) {
1230 struct platform_device *pdev = cxl_hb_single[i];
1234 sysfs_remove_link(&pdev->dev.kobj, "physical_node");
1235 platform_device_unregister(cxl_hb_single[i]);
1239 static __init int cxl_test_init(void)
1249 register_cxl_mock_ops(&cxl_mock_ops);
1251 cxl_mock_pool = gen_pool_create(ilog2(SZ_2M), NUMA_NO_NODE);
1252 if (!cxl_mock_pool) {
1254 goto err_gen_pool_create;
1257 rc = gen_pool_add(cxl_mock_pool, iomem_resource.end + 1 - SZ_64G,
1258 SZ_64G, NUMA_NO_NODE);
1260 goto err_gen_pool_add;
1262 if (interleave_arithmetic == 1) {
1263 cfmws_start = CFMWS_XOR_ARRAY_START;
1264 cfmws_end = CFMWS_XOR_ARRAY_END;
1266 cfmws_start = CFMWS_MOD_ARRAY_START;
1267 cfmws_end = CFMWS_MOD_ARRAY_END;
1270 rc = populate_cedt();
1274 for (i = 0; i < ARRAY_SIZE(cxl_host_bridge); i++) {
1275 struct acpi_device *adev = &host_bridge[i];
1276 struct platform_device *pdev;
1278 pdev = platform_device_alloc("cxl_host_bridge", i);
1282 mock_companion(adev, &pdev->dev);
1283 rc = platform_device_add(pdev);
1285 platform_device_put(pdev);
1289 cxl_host_bridge[i] = pdev;
1290 mock_pci_bus[i].bridge = &pdev->dev;
1291 rc = sysfs_create_link(&pdev->dev.kobj, &pdev->dev.kobj,
1297 for (i = 0; i < ARRAY_SIZE(cxl_root_port); i++) {
1298 struct platform_device *bridge =
1299 cxl_host_bridge[i % ARRAY_SIZE(cxl_host_bridge)];
1300 struct platform_device *pdev;
1302 pdev = platform_device_alloc("cxl_root_port", i);
1305 pdev->dev.parent = &bridge->dev;
1307 rc = platform_device_add(pdev);
1309 platform_device_put(pdev);
1312 cxl_root_port[i] = pdev;
1315 BUILD_BUG_ON(ARRAY_SIZE(cxl_switch_uport) != ARRAY_SIZE(cxl_root_port));
1316 for (i = 0; i < ARRAY_SIZE(cxl_switch_uport); i++) {
1317 struct platform_device *root_port = cxl_root_port[i];
1318 struct platform_device *pdev;
1320 pdev = platform_device_alloc("cxl_switch_uport", i);
1323 pdev->dev.parent = &root_port->dev;
1325 rc = platform_device_add(pdev);
1327 platform_device_put(pdev);
1330 cxl_switch_uport[i] = pdev;
1333 for (i = 0; i < ARRAY_SIZE(cxl_switch_dport); i++) {
1334 struct platform_device *uport =
1335 cxl_switch_uport[i % ARRAY_SIZE(cxl_switch_uport)];
1336 struct platform_device *pdev;
1338 pdev = platform_device_alloc("cxl_switch_dport", i);
1341 pdev->dev.parent = &uport->dev;
1343 rc = platform_device_add(pdev);
1345 platform_device_put(pdev);
1348 cxl_switch_dport[i] = pdev;
1351 for (i = 0; i < ARRAY_SIZE(cxl_mem); i++) {
1352 struct platform_device *dport = cxl_switch_dport[i];
1353 struct platform_device *pdev;
1355 pdev = platform_device_alloc("cxl_mem", i);
1358 pdev->dev.parent = &dport->dev;
1359 set_dev_node(&pdev->dev, i % 2);
1361 rc = platform_device_add(pdev);
1363 platform_device_put(pdev);
1369 rc = cxl_single_init();
1373 rc = cxl_rch_init();
1377 cxl_acpi = platform_device_alloc("cxl_acpi", 0);
1381 mock_companion(&acpi0017_mock, &cxl_acpi->dev);
1382 acpi0017_mock.dev.bus = &platform_bus_type;
1384 rc = platform_device_add(cxl_acpi);
1391 platform_device_put(cxl_acpi);
1397 for (i = ARRAY_SIZE(cxl_mem) - 1; i >= 0; i--)
1398 platform_device_unregister(cxl_mem[i]);
1400 for (i = ARRAY_SIZE(cxl_switch_dport) - 1; i >= 0; i--)
1401 platform_device_unregister(cxl_switch_dport[i]);
1403 for (i = ARRAY_SIZE(cxl_switch_uport) - 1; i >= 0; i--)
1404 platform_device_unregister(cxl_switch_uport[i]);
1406 for (i = ARRAY_SIZE(cxl_root_port) - 1; i >= 0; i--)
1407 platform_device_unregister(cxl_root_port[i]);
1409 for (i = ARRAY_SIZE(cxl_host_bridge) - 1; i >= 0; i--) {
1410 struct platform_device *pdev = cxl_host_bridge[i];
1414 sysfs_remove_link(&pdev->dev.kobj, "physical_node");
1415 platform_device_unregister(cxl_host_bridge[i]);
1418 depopulate_all_mock_resources();
1420 gen_pool_destroy(cxl_mock_pool);
1421 err_gen_pool_create:
1422 unregister_cxl_mock_ops(&cxl_mock_ops);
1426 static __exit void cxl_test_exit(void)
1430 platform_device_unregister(cxl_acpi);
1433 for (i = ARRAY_SIZE(cxl_mem) - 1; i >= 0; i--)
1434 platform_device_unregister(cxl_mem[i]);
1435 for (i = ARRAY_SIZE(cxl_switch_dport) - 1; i >= 0; i--)
1436 platform_device_unregister(cxl_switch_dport[i]);
1437 for (i = ARRAY_SIZE(cxl_switch_uport) - 1; i >= 0; i--)
1438 platform_device_unregister(cxl_switch_uport[i]);
1439 for (i = ARRAY_SIZE(cxl_root_port) - 1; i >= 0; i--)
1440 platform_device_unregister(cxl_root_port[i]);
1441 for (i = ARRAY_SIZE(cxl_host_bridge) - 1; i >= 0; i--) {
1442 struct platform_device *pdev = cxl_host_bridge[i];
1446 sysfs_remove_link(&pdev->dev.kobj, "physical_node");
1447 platform_device_unregister(cxl_host_bridge[i]);
1449 depopulate_all_mock_resources();
1450 gen_pool_destroy(cxl_mock_pool);
1451 unregister_cxl_mock_ops(&cxl_mock_ops);
1454 module_param(interleave_arithmetic, int, 0444);
1455 MODULE_PARM_DESC(interleave_arithmetic, "Modulo:0, XOR:1");
1456 module_init(cxl_test_init);
1457 module_exit(cxl_test_exit);
1458 MODULE_LICENSE("GPL v2");
1459 MODULE_IMPORT_NS(ACPI);
1460 MODULE_IMPORT_NS(CXL);