hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/of/unittest.c
....@@ -5,13 +5,15 @@
55
66 #define pr_fmt(fmt) "### dt-test ### " fmt
77
8
-#include <linux/bootmem.h>
8
+#include <linux/memblock.h>
99 #include <linux/clk.h>
10
+#include <linux/dma-direct.h> /* to test phys_to_dma/dma_to_phys */
1011 #include <linux/err.h>
1112 #include <linux/errno.h>
1213 #include <linux/hashtable.h>
1314 #include <linux/libfdt.h>
1415 #include <linux/of.h>
16
+#include <linux/of_address.h>
1517 #include <linux/of_fdt.h>
1618 #include <linux/of_irq.h>
1719 #include <linux/of_platform.h>
....@@ -23,6 +25,7 @@
2325
2426 #include <linux/i2c.h>
2527 #include <linux/i2c-mux.h>
28
+#include <linux/gpio/driver.h>
2629
2730 #include <linux/bitops.h>
2831
....@@ -44,6 +47,20 @@
4447 } \
4548 failed; \
4649 })
50
+
51
+/*
52
+ * Expected message may have a message level other than KERN_INFO.
53
+ * Print the expected message only if the current loglevel will allow
54
+ * the actual message to print.
55
+ *
56
+ * Do not use EXPECT_BEGIN() or EXPECT_END() for messages generated by
57
+ * pr_debug().
58
+ */
59
+#define EXPECT_BEGIN(level, fmt, ...) \
60
+ printk(level pr_fmt("EXPECT \\ : ") fmt, ##__VA_ARGS__)
61
+
62
+#define EXPECT_END(level, fmt, ...) \
63
+ printk(level pr_fmt("EXPECT / : ") fmt, ##__VA_ARGS__)
4764
4865 static void __init of_unittest_find_node_by_name(void)
4966 {
....@@ -299,6 +316,10 @@
299316
300317 of_unittest_printf_one(np, "%pOF", full_name);
301318 of_unittest_printf_one(np, "%pOFf", full_name);
319
+ of_unittest_printf_one(np, "%pOFn", "dev");
320
+ of_unittest_printf_one(np, "%2pOFn", "dev");
321
+ of_unittest_printf_one(np, "%5pOFn", " dev");
322
+ of_unittest_printf_one(np, "%pOFnc", "dev:test-sub-device");
302323 of_unittest_printf_one(np, "%pOFp", phandle_str);
303324 of_unittest_printf_one(np, "%pOFP", "dev@100");
304325 of_unittest_printf_one(np, "ABC %pOFP ABC", "ABC dev@100 ABC");
....@@ -340,7 +361,7 @@
340361 }
341362
342363 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
343
- if (WARN_ON(!nh))
364
+ if (!nh)
344365 return;
345366
346367 nh->np = np;
....@@ -439,29 +460,77 @@
439460
440461 /* Check for missing cells property */
441462 memset(&args, 0, sizeof(args));
463
+
464
+ EXPECT_BEGIN(KERN_INFO,
465
+ "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
466
+
442467 rc = of_parse_phandle_with_args(np, "phandle-list",
443468 "#phandle-cells-missing", 0, &args);
469
+
470
+ EXPECT_END(KERN_INFO,
471
+ "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
472
+
444473 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
474
+
475
+ EXPECT_BEGIN(KERN_INFO,
476
+ "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
477
+
445478 rc = of_count_phandle_with_args(np, "phandle-list",
446479 "#phandle-cells-missing");
480
+
481
+ EXPECT_END(KERN_INFO,
482
+ "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
483
+
447484 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
448485
449486 /* Check for bad phandle in list */
450487 memset(&args, 0, sizeof(args));
488
+
489
+ EXPECT_BEGIN(KERN_INFO,
490
+ "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
491
+
451492 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
452493 "#phandle-cells", 0, &args);
494
+
495
+ EXPECT_END(KERN_INFO,
496
+ "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
497
+
453498 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
499
+
500
+ EXPECT_BEGIN(KERN_INFO,
501
+ "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
502
+
454503 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
455504 "#phandle-cells");
505
+
506
+ EXPECT_END(KERN_INFO,
507
+ "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
508
+
456509 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
457510
458511 /* Check for incorrectly formed argument list */
459512 memset(&args, 0, sizeof(args));
513
+
514
+ EXPECT_BEGIN(KERN_INFO,
515
+ "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found -1");
516
+
460517 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
461518 "#phandle-cells", 1, &args);
519
+
520
+ EXPECT_END(KERN_INFO,
521
+ "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found -1");
522
+
462523 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
524
+
525
+ EXPECT_BEGIN(KERN_INFO,
526
+ "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found -1");
527
+
463528 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
464529 "#phandle-cells");
530
+
531
+ EXPECT_END(KERN_INFO,
532
+ "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found -1");
533
+
465534 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
466535 }
467536
....@@ -572,20 +641,41 @@
572641
573642 /* Check for missing cells,map,mask property */
574643 memset(&args, 0, sizeof(args));
644
+
645
+ EXPECT_BEGIN(KERN_INFO,
646
+ "OF: /testcase-data/phandle-tests/consumer-b: could not get #phandle-missing-cells for /testcase-data/phandle-tests/provider1");
647
+
575648 rc = of_parse_phandle_with_args_map(np, "phandle-list",
576649 "phandle-missing", 0, &args);
650
+ EXPECT_END(KERN_INFO,
651
+ "OF: /testcase-data/phandle-tests/consumer-b: could not get #phandle-missing-cells for /testcase-data/phandle-tests/provider1");
652
+
577653 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
578654
579655 /* Check for bad phandle in list */
580656 memset(&args, 0, sizeof(args));
657
+
658
+ EXPECT_BEGIN(KERN_INFO,
659
+ "OF: /testcase-data/phandle-tests/consumer-b: could not find phandle");
660
+
581661 rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-phandle",
582662 "phandle", 0, &args);
663
+ EXPECT_END(KERN_INFO,
664
+ "OF: /testcase-data/phandle-tests/consumer-b: could not find phandle");
665
+
583666 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
584667
585668 /* Check for incorrectly formed argument list */
586669 memset(&args, 0, sizeof(args));
670
+
671
+ EXPECT_BEGIN(KERN_INFO,
672
+ "OF: /testcase-data/phandle-tests/consumer-b: #phandle-cells = 2 found -1");
673
+
587674 rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-args",
588675 "phandle", 1, &args);
676
+ EXPECT_END(KERN_INFO,
677
+ "OF: /testcase-data/phandle-tests/consumer-b: #phandle-cells = 2 found -1");
678
+
589679 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
590680 }
591681
....@@ -777,6 +867,137 @@
777867 of_node_put(n2);
778868 of_node_put(n21);
779869 #endif
870
+}
871
+
872
+static void __init of_unittest_dma_get_max_cpu_address(void)
873
+{
874
+ struct device_node *np;
875
+ phys_addr_t cpu_addr;
876
+
877
+ if (!IS_ENABLED(CONFIG_OF_ADDRESS))
878
+ return;
879
+
880
+ np = of_find_node_by_path("/testcase-data/address-tests");
881
+ if (!np) {
882
+ pr_err("missing testcase data\n");
883
+ return;
884
+ }
885
+
886
+ cpu_addr = of_dma_get_max_cpu_address(np);
887
+ unittest(cpu_addr == 0x4fffffff,
888
+ "of_dma_get_max_cpu_address: wrong CPU addr %pad (expecting %x)\n",
889
+ &cpu_addr, 0x4fffffff);
890
+}
891
+
892
+static void __init of_unittest_dma_ranges_one(const char *path,
893
+ u64 expect_dma_addr, u64 expect_paddr)
894
+{
895
+#ifdef CONFIG_HAS_DMA
896
+ struct device_node *np;
897
+ const struct bus_dma_region *map = NULL;
898
+ int rc;
899
+
900
+ np = of_find_node_by_path(path);
901
+ if (!np) {
902
+ pr_err("missing testcase data\n");
903
+ return;
904
+ }
905
+
906
+ rc = of_dma_get_range(np, &map);
907
+
908
+ unittest(!rc, "of_dma_get_range failed on node %pOF rc=%i\n", np, rc);
909
+
910
+ if (!rc) {
911
+ phys_addr_t paddr;
912
+ dma_addr_t dma_addr;
913
+ struct device *dev_bogus;
914
+
915
+ dev_bogus = kzalloc(sizeof(struct device), GFP_KERNEL);
916
+ if (!dev_bogus) {
917
+ unittest(0, "kzalloc() failed\n");
918
+ kfree(map);
919
+ return;
920
+ }
921
+
922
+ dev_bogus->dma_range_map = map;
923
+ paddr = dma_to_phys(dev_bogus, expect_dma_addr);
924
+ dma_addr = phys_to_dma(dev_bogus, expect_paddr);
925
+
926
+ unittest(paddr == expect_paddr,
927
+ "of_dma_get_range: wrong phys addr %pap (expecting %llx) on node %pOF\n",
928
+ &paddr, expect_paddr, np);
929
+ unittest(dma_addr == expect_dma_addr,
930
+ "of_dma_get_range: wrong DMA addr %pad (expecting %llx) on node %pOF\n",
931
+ &dma_addr, expect_dma_addr, np);
932
+
933
+ kfree(map);
934
+ kfree(dev_bogus);
935
+ }
936
+ of_node_put(np);
937
+#endif
938
+}
939
+
940
+static void __init of_unittest_parse_dma_ranges(void)
941
+{
942
+ of_unittest_dma_ranges_one("/testcase-data/address-tests/device@70000000",
943
+ 0x0, 0x20000000);
944
+ if (IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT))
945
+ of_unittest_dma_ranges_one("/testcase-data/address-tests/bus@80000000/device@1000",
946
+ 0x100000000, 0x20000000);
947
+ of_unittest_dma_ranges_one("/testcase-data/address-tests/pci@90000000",
948
+ 0x80000000, 0x20000000);
949
+}
950
+
951
+static void __init of_unittest_pci_dma_ranges(void)
952
+{
953
+ struct device_node *np;
954
+ struct of_pci_range range;
955
+ struct of_pci_range_parser parser;
956
+ int i = 0;
957
+
958
+ if (!IS_ENABLED(CONFIG_PCI))
959
+ return;
960
+
961
+ np = of_find_node_by_path("/testcase-data/address-tests/pci@90000000");
962
+ if (!np) {
963
+ pr_err("missing testcase data\n");
964
+ return;
965
+ }
966
+
967
+ if (of_pci_dma_range_parser_init(&parser, np)) {
968
+ pr_err("missing dma-ranges property\n");
969
+ return;
970
+ }
971
+
972
+ /*
973
+ * Get the dma-ranges from the device tree
974
+ */
975
+ for_each_of_pci_range(&parser, &range) {
976
+ if (!i) {
977
+ unittest(range.size == 0x10000000,
978
+ "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
979
+ np, range.size);
980
+ unittest(range.cpu_addr == 0x20000000,
981
+ "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
982
+ range.cpu_addr, np);
983
+ unittest(range.pci_addr == 0x80000000,
984
+ "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
985
+ range.pci_addr, np);
986
+ } else {
987
+ unittest(range.size == 0x10000000,
988
+ "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
989
+ np, range.size);
990
+ unittest(range.cpu_addr == 0x40000000,
991
+ "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
992
+ range.cpu_addr, np);
993
+ unittest(range.pci_addr == 0xc0000000,
994
+ "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
995
+ range.pci_addr, np);
996
+ }
997
+ i++;
998
+ }
999
+
1000
+ of_node_put(np);
7801001 }
7811002
7821003 static void __init of_unittest_parse_interrupts(void)
....@@ -1031,7 +1252,15 @@
10311252 np = of_find_node_by_path("/testcase-data/testcase-device2");
10321253 pdev = of_find_device_by_node(np);
10331254 unittest(pdev, "device 2 creation failed\n");
1255
+
1256
+ EXPECT_BEGIN(KERN_INFO,
1257
+ "platform testcase-data:testcase-device2: IRQ index 0 not found");
1258
+
10341259 irq = platform_get_irq(pdev, 0);
1260
+
1261
+ EXPECT_END(KERN_INFO,
1262
+ "platform testcase-data:testcase-device2: IRQ index 0 not found");
1263
+
10351264 unittest(irq < 0 && irq != -EPROBE_DEFER,
10361265 "device parsing error failed - %d\n", irq);
10371266 }
....@@ -1044,8 +1273,10 @@
10441273 test_bus = platform_device_register_full(&test_bus_info);
10451274 rc = PTR_ERR_OR_ZERO(test_bus);
10461275 unittest(!rc, "testbus registration failed; rc=%i\n", rc);
1047
- if (rc)
1276
+ if (rc) {
1277
+ of_node_put(np);
10481278 return;
1279
+ }
10491280 test_bus->dev.of_node = np;
10501281
10511282 /*
....@@ -1119,15 +1350,22 @@
11191350 for (prop = np->properties; prop != NULL; prop = save_next) {
11201351 save_next = prop->next;
11211352 ret = of_add_property(dup, prop);
1122
- if (ret)
1353
+ if (ret) {
1354
+ if (ret == -EEXIST && !strcmp(prop->name, "name"))
1355
+ continue;
11231356 pr_err("unittest internal error: unable to add testdata property %pOF/%s",
11241357 np, prop->name);
1358
+ }
11251359 }
11261360 }
11271361
11281362 /**
11291363 * attach_node_and_children - attaches nodes
1130
- * and its children to live tree
1364
+ * and its children to live tree.
1365
+ * CAUTION: misleading function name - if node @np already exists in
1366
+ * the live tree then children of @np are *not* attached to the live
1367
+ * tree. This works for the current test devicetree nodes because such
1368
+ * nodes do not have child nodes.
11311369 *
11321370 * @np: Node to attach to live tree
11331371 */
....@@ -1197,12 +1435,9 @@
11971435
11981436 /* creating copy */
11991437 unittest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
1200
-
1201
- if (!unittest_data) {
1202
- pr_warn("%s: Failed to allocate memory for unittest_data; "
1203
- "not running tests\n", __func__);
1438
+ if (!unittest_data)
12041439 return -ENOMEM;
1205
- }
1440
+
12061441 of_fdt_unflatten_tree(unittest_data, NULL, &unittest_data_node);
12071442 if (!unittest_data_node) {
12081443 pr_warn("%s: No tree to attach; not running tests\n", __func__);
....@@ -1232,6 +1467,9 @@
12321467 return 0;
12331468 }
12341469
1470
+ EXPECT_BEGIN(KERN_INFO,
1471
+ "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
1472
+
12351473 /* attach the sub-tree to live tree */
12361474 np = unittest_data_node->child;
12371475 while (np) {
....@@ -1241,6 +1479,9 @@
12411479 attach_node_and_children(np);
12421480 np = next;
12431481 }
1482
+
1483
+ EXPECT_END(KERN_INFO,
1484
+ "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
12441485
12451486 of_overlay_mutex_unlock();
12461487
....@@ -1316,6 +1557,249 @@
13161557 platform_device_put(pdev);
13171558 return pdev != NULL;
13181559 }
1560
+
1561
+#ifdef CONFIG_OF_GPIO
1562
+
1563
+struct unittest_gpio_dev {
1564
+ struct gpio_chip chip;
1565
+};
1566
+
1567
+static int unittest_gpio_chip_request_count;
1568
+static int unittest_gpio_probe_count;
1569
+static int unittest_gpio_probe_pass_count;
1570
+
1571
+static int unittest_gpio_chip_request(struct gpio_chip *chip, unsigned int offset)
1572
+{
1573
+ unittest_gpio_chip_request_count++;
1574
+
1575
+ pr_debug("%s(): %s %d %d\n", __func__, chip->label, offset,
1576
+ unittest_gpio_chip_request_count);
1577
+ return 0;
1578
+}
1579
+
1580
+static int unittest_gpio_probe(struct platform_device *pdev)
1581
+{
1582
+ struct unittest_gpio_dev *devptr;
1583
+ int ret;
1584
+
1585
+ unittest_gpio_probe_count++;
1586
+
1587
+ devptr = kzalloc(sizeof(*devptr), GFP_KERNEL);
1588
+ if (!devptr)
1589
+ return -ENOMEM;
1590
+
1591
+ platform_set_drvdata(pdev, devptr);
1592
+
1593
+ devptr->chip.of_node = pdev->dev.of_node;
1594
+ devptr->chip.label = "of-unittest-gpio";
1595
+ devptr->chip.base = -1; /* dynamic allocation */
1596
+ devptr->chip.ngpio = 5;
1597
+ devptr->chip.request = unittest_gpio_chip_request;
1598
+
1599
+ ret = gpiochip_add_data(&devptr->chip, NULL);
1600
+
1601
+ unittest(!ret,
1602
+ "gpiochip_add_data() for node @%pOF failed, ret = %d\n", devptr->chip.of_node, ret);
1603
+
1604
+ if (!ret)
1605
+ unittest_gpio_probe_pass_count++;
1606
+ return ret;
1607
+}
1608
+
1609
+static int unittest_gpio_remove(struct platform_device *pdev)
1610
+{
1611
+ struct unittest_gpio_dev *gdev = platform_get_drvdata(pdev);
1612
+ struct device *dev = &pdev->dev;
1613
+ struct device_node *np = pdev->dev.of_node;
1614
+
1615
+ dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1616
+
1617
+ if (!gdev)
1618
+ return -EINVAL;
1619
+
1620
+ if (gdev->chip.base != -1)
1621
+ gpiochip_remove(&gdev->chip);
1622
+
1623
+ platform_set_drvdata(pdev, NULL);
1624
+ kfree(gdev);
1625
+
1626
+ return 0;
1627
+}
1628
+
1629
+static const struct of_device_id unittest_gpio_id[] = {
1630
+ { .compatible = "unittest-gpio", },
1631
+ {}
1632
+};
1633
+
1634
+static struct platform_driver unittest_gpio_driver = {
1635
+ .probe = unittest_gpio_probe,
1636
+ .remove = unittest_gpio_remove,
1637
+ .driver = {
1638
+ .name = "unittest-gpio",
1639
+ .of_match_table = of_match_ptr(unittest_gpio_id),
1640
+ },
1641
+};
1642
+
1643
+static void __init of_unittest_overlay_gpio(void)
1644
+{
1645
+ int chip_request_count;
1646
+ int probe_pass_count;
1647
+ int ret;
1648
+
1649
+ /*
1650
+ * tests: apply overlays before registering driver
1651
+ * Similar to installing a driver as a module, the
1652
+ * driver is registered after applying the overlays.
1653
+ *
1654
+ * The overlays are applied by overlay_data_apply()
1655
+ * instead of of_unittest_apply_overlay() so that they
1656
+ * will not be tracked. Thus they will not be removed
1657
+ * by of_unittest_destroy_tracked_overlays().
1658
+ *
1659
+ * - apply overlay_gpio_01
1660
+ * - apply overlay_gpio_02a
1661
+ * - apply overlay_gpio_02b
1662
+ * - register driver
1663
+ *
1664
+ * register driver will result in
1665
+ * - probe and processing gpio hog for overlay_gpio_01
1666
+ * - probe for overlay_gpio_02a
1667
+ * - processing gpio for overlay_gpio_02b
1668
+ */
1669
+
1670
+ probe_pass_count = unittest_gpio_probe_pass_count;
1671
+ chip_request_count = unittest_gpio_chip_request_count;
1672
+
1673
+ /*
1674
+ * overlay_gpio_01 contains gpio node and child gpio hog node
1675
+ * overlay_gpio_02a contains gpio node
1676
+ * overlay_gpio_02b contains child gpio hog node
1677
+ */
1678
+
1679
+ unittest(overlay_data_apply("overlay_gpio_01", NULL),
1680
+ "Adding overlay 'overlay_gpio_01' failed\n");
1681
+
1682
+ unittest(overlay_data_apply("overlay_gpio_02a", NULL),
1683
+ "Adding overlay 'overlay_gpio_02a' failed\n");
1684
+
1685
+ unittest(overlay_data_apply("overlay_gpio_02b", NULL),
1686
+ "Adding overlay 'overlay_gpio_02b' failed\n");
1687
+
1688
+ /*
1689
+ * messages are the result of the probes, after the
1690
+ * driver is registered
1691
+ */
1692
+
1693
+ EXPECT_BEGIN(KERN_INFO,
1694
+ "gpio-<<int>> (line-B-input): hogged as input\n");
1695
+
1696
+ EXPECT_BEGIN(KERN_INFO,
1697
+ "gpio-<<int>> (line-A-input): hogged as input\n");
1698
+
1699
+ ret = platform_driver_register(&unittest_gpio_driver);
1700
+ if (unittest(ret == 0, "could not register unittest gpio driver\n"))
1701
+ return;
1702
+
1703
+ EXPECT_END(KERN_INFO,
1704
+ "gpio-<<int>> (line-A-input): hogged as input\n");
1705
+ EXPECT_END(KERN_INFO,
1706
+ "gpio-<<int>> (line-B-input): hogged as input\n");
1707
+
1708
+ unittest(probe_pass_count + 2 == unittest_gpio_probe_pass_count,
1709
+ "unittest_gpio_probe() failed or not called\n");
1710
+
1711
+ unittest(chip_request_count + 2 == unittest_gpio_chip_request_count,
1712
+ "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1713
+ unittest_gpio_chip_request_count - chip_request_count);
1714
+
1715
+ /*
1716
+ * tests: apply overlays after registering driver
1717
+ *
1718
+ * Similar to a driver built-in to the kernel, the
1719
+ * driver is registered before applying the overlays.
1720
+ *
1721
+ * overlay_gpio_03 contains gpio node and child gpio hog node
1722
+ *
1723
+ * - apply overlay_gpio_03
1724
+ *
1725
+ * apply overlay will result in
1726
+ * - probe and processing gpio hog.
1727
+ */
1728
+
1729
+ probe_pass_count = unittest_gpio_probe_pass_count;
1730
+ chip_request_count = unittest_gpio_chip_request_count;
1731
+
1732
+ EXPECT_BEGIN(KERN_INFO,
1733
+ "gpio-<<int>> (line-D-input): hogged as input\n");
1734
+
1735
+ /* overlay_gpio_03 contains gpio node and child gpio hog node */
1736
+
1737
+ unittest(overlay_data_apply("overlay_gpio_03", NULL),
1738
+ "Adding overlay 'overlay_gpio_03' failed\n");
1739
+
1740
+ EXPECT_END(KERN_INFO,
1741
+ "gpio-<<int>> (line-D-input): hogged as input\n");
1742
+
1743
+ unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
1744
+ "unittest_gpio_probe() failed or not called\n");
1745
+
1746
+ unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
1747
+ "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1748
+ unittest_gpio_chip_request_count - chip_request_count);
1749
+
1750
+ /*
1751
+ * overlay_gpio_04a contains gpio node
1752
+ *
1753
+ * - apply overlay_gpio_04a
1754
+ *
1755
+ * apply the overlay will result in
1756
+ * - probe for overlay_gpio_04a
1757
+ */
1758
+
1759
+ probe_pass_count = unittest_gpio_probe_pass_count;
1760
+ chip_request_count = unittest_gpio_chip_request_count;
1761
+
1762
+ /* overlay_gpio_04a contains gpio node */
1763
+
1764
+ unittest(overlay_data_apply("overlay_gpio_04a", NULL),
1765
+ "Adding overlay 'overlay_gpio_04a' failed\n");
1766
+
1767
+ unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
1768
+ "unittest_gpio_probe() failed or not called\n");
1769
+
1770
+ /*
1771
+ * overlay_gpio_04b contains child gpio hog node
1772
+ *
1773
+ * - apply overlay_gpio_04b
1774
+ *
1775
+ * apply the overlay will result in
1776
+ * - processing gpio for overlay_gpio_04b
1777
+ */
1778
+
1779
+ EXPECT_BEGIN(KERN_INFO,
1780
+ "gpio-<<int>> (line-C-input): hogged as input\n");
1781
+
1782
+ /* overlay_gpio_04b contains child gpio hog node */
1783
+
1784
+ unittest(overlay_data_apply("overlay_gpio_04b", NULL),
1785
+ "Adding overlay 'overlay_gpio_04b' failed\n");
1786
+
1787
+ EXPECT_END(KERN_INFO,
1788
+ "gpio-<<int>> (line-C-input): hogged as input\n");
1789
+
1790
+ unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
1791
+ "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1792
+ unittest_gpio_chip_request_count - chip_request_count);
1793
+}
1794
+
1795
+#else
1796
+
1797
+static void __init of_unittest_overlay_gpio(void)
1798
+{
1799
+ /* skip tests */
1800
+}
1801
+
1802
+#endif
13191803
13201804 #if IS_BUILTIN(CONFIG_I2C)
13211805
....@@ -1418,10 +1902,18 @@
14181902
14191903 static const char *bus_path = "/testcase-data/overlay-node/test-bus";
14201904
1421
-/* it is guaranteed that overlay ids are assigned in sequence */
1905
+/* FIXME: it is NOT guaranteed that overlay ids are assigned in sequence */
1906
+
14221907 #define MAX_UNITTEST_OVERLAYS 256
14231908 static unsigned long overlay_id_bits[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS)];
14241909 static int overlay_first_id = -1;
1910
+
1911
+static long of_unittest_overlay_tracked(int id)
1912
+{
1913
+ if (WARN_ON(id >= MAX_UNITTEST_OVERLAYS))
1914
+ return 0;
1915
+ return overlay_id_bits[BIT_WORD(id)] & BIT_MASK(id);
1916
+}
14251917
14261918 static void of_unittest_track_overlay(int id)
14271919 {
....@@ -1429,8 +1921,8 @@
14291921 overlay_first_id = id;
14301922 id -= overlay_first_id;
14311923
1432
- /* we shouldn't need that many */
1433
- BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1924
+ if (WARN_ON(id >= MAX_UNITTEST_OVERLAYS))
1925
+ return;
14341926 overlay_id_bits[BIT_WORD(id)] |= BIT_MASK(id);
14351927 }
14361928
....@@ -1439,7 +1931,8 @@
14391931 if (overlay_first_id < 0)
14401932 return;
14411933 id -= overlay_first_id;
1442
- BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1934
+ if (WARN_ON(id >= MAX_UNITTEST_OVERLAYS))
1935
+ return;
14431936 overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
14441937 }
14451938
....@@ -1455,7 +1948,7 @@
14551948 defers = 0;
14561949 /* remove in reverse order */
14571950 for (id = MAX_UNITTEST_OVERLAYS - 1; id >= 0; id--) {
1458
- if (!(overlay_id_bits[BIT_WORD(id)] & BIT_MASK(id)))
1951
+ if (!of_unittest_overlay_tracked(id))
14591952 continue;
14601953
14611954 ovcs_id = id + overlay_first_id;
....@@ -1472,13 +1965,12 @@
14721965 continue;
14731966 }
14741967
1475
- overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1968
+ of_unittest_untrack_overlay(id);
14761969 }
14771970 } while (defers > 0);
14781971 }
14791972
1480
-static int __init of_unittest_apply_overlay(int overlay_nr, int unittest_nr,
1481
- int *overlay_id)
1973
+static int __init of_unittest_apply_overlay(int overlay_nr, int *overlay_id)
14821974 {
14831975 const char *overlay_name;
14841976
....@@ -1511,7 +2003,7 @@
15112003 }
15122004
15132005 ovcs_id = 0;
1514
- ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id);
2006
+ ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
15152007 if (ret != 0) {
15162008 /* of_unittest_apply_overlay already called unittest() */
15172009 return ret;
....@@ -1534,7 +2026,7 @@
15342026 int unittest_nr, int before, int after,
15352027 enum overlay_type ovtype)
15362028 {
1537
- int ret, ovcs_id;
2029
+ int ret, ovcs_id, save_id;
15382030
15392031 /* unittest device must be in before state */
15402032 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
....@@ -1547,7 +2039,7 @@
15472039
15482040 /* apply the overlay */
15492041 ovcs_id = 0;
1550
- ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id);
2042
+ ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
15512043 if (ret != 0) {
15522044 /* of_unittest_apply_overlay already called unittest() */
15532045 return ret;
....@@ -1562,6 +2054,7 @@
15622054 return -EINVAL;
15632055 }
15642056
2057
+ save_id = ovcs_id;
15652058 ret = of_overlay_remove(&ovcs_id);
15662059 if (ret != 0) {
15672060 unittest(0, "%s failed to be destroyed @\"%s\"\n",
....@@ -1569,6 +2062,7 @@
15692062 unittest_path(unittest_nr, ovtype));
15702063 return ret;
15712064 }
2065
+ of_unittest_untrack_overlay(save_id);
15722066
15732067 /* unittest device must be again in before state */
15742068 if (of_unittest_device_exists(unittest_nr, PDEV_OVERLAY) != before) {
....@@ -1585,8 +2079,18 @@
15852079 /* test activation of device */
15862080 static void __init of_unittest_overlay_0(void)
15872081 {
2082
+ int ret;
2083
+
2084
+ EXPECT_BEGIN(KERN_INFO,
2085
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
2086
+
15882087 /* device should enable */
1589
- if (of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY))
2088
+ ret = of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
2089
+
2090
+ EXPECT_END(KERN_INFO,
2091
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
2092
+
2093
+ if (ret)
15902094 return;
15912095
15922096 unittest(1, "overlay test %d passed\n", 0);
....@@ -1595,28 +2099,58 @@
15952099 /* test deactivation of device */
15962100 static void __init of_unittest_overlay_1(void)
15972101 {
2102
+ int ret;
2103
+
2104
+ EXPECT_BEGIN(KERN_INFO,
2105
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
2106
+
15982107 /* device should disable */
1599
- if (of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY))
2108
+ ret = of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
2109
+
2110
+ EXPECT_END(KERN_INFO,
2111
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
2112
+
2113
+ if (ret)
16002114 return;
16012115
16022116 unittest(1, "overlay test %d passed\n", 1);
2117
+
16032118 }
16042119
16052120 /* test activation of device */
16062121 static void __init of_unittest_overlay_2(void)
16072122 {
1608
- /* device should enable */
1609
- if (of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY))
1610
- return;
2123
+ int ret;
16112124
2125
+ EXPECT_BEGIN(KERN_INFO,
2126
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
2127
+
2128
+ /* device should enable */
2129
+ ret = of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
2130
+
2131
+ EXPECT_END(KERN_INFO,
2132
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
2133
+
2134
+ if (ret)
2135
+ return;
16122136 unittest(1, "overlay test %d passed\n", 2);
16132137 }
16142138
16152139 /* test deactivation of device */
16162140 static void __init of_unittest_overlay_3(void)
16172141 {
2142
+ int ret;
2143
+
2144
+ EXPECT_BEGIN(KERN_INFO,
2145
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
2146
+
16182147 /* device should disable */
1619
- if (of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY))
2148
+ ret = of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
2149
+
2150
+ EXPECT_END(KERN_INFO,
2151
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
2152
+
2153
+ if (ret)
16202154 return;
16212155
16222156 unittest(1, "overlay test %d passed\n", 3);
....@@ -1635,8 +2169,18 @@
16352169 /* test overlay apply/revert sequence */
16362170 static void __init of_unittest_overlay_5(void)
16372171 {
2172
+ int ret;
2173
+
2174
+ EXPECT_BEGIN(KERN_INFO,
2175
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
2176
+
16382177 /* device should disable */
1639
- if (of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY))
2178
+ ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
2179
+
2180
+ EXPECT_END(KERN_INFO,
2181
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
2182
+
2183
+ if (ret)
16402184 return;
16412185
16422186 unittest(1, "overlay test %d passed\n", 5);
....@@ -1649,6 +2193,8 @@
16492193 int overlay_nr = 6, unittest_nr = 6;
16502194 int before = 0, after = 1;
16512195 const char *overlay_name;
2196
+
2197
+ int ret;
16522198
16532199 /* unittest device must be in before state */
16542200 for (i = 0; i < 2; i++) {
....@@ -1664,18 +2210,41 @@
16642210 }
16652211
16662212 /* apply the overlays */
1667
- for (i = 0; i < 2; i++) {
16682213
1669
- overlay_name = overlay_name_from_nr(overlay_nr + i);
2214
+ EXPECT_BEGIN(KERN_INFO,
2215
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
16702216
1671
- if (!overlay_data_apply(overlay_name, &ovcs_id)) {
1672
- unittest(0, "could not apply overlay \"%s\"\n",
1673
- overlay_name);
2217
+ overlay_name = overlay_name_from_nr(overlay_nr + 0);
2218
+
2219
+ ret = overlay_data_apply(overlay_name, &ovcs_id);
2220
+
2221
+ if (!ret) {
2222
+ unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
16742223 return;
1675
- }
1676
- ov_id[i] = ovcs_id;
1677
- of_unittest_track_overlay(ov_id[i]);
16782224 }
2225
+ ov_id[0] = ovcs_id;
2226
+ of_unittest_track_overlay(ov_id[0]);
2227
+
2228
+ EXPECT_END(KERN_INFO,
2229
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
2230
+
2231
+ EXPECT_BEGIN(KERN_INFO,
2232
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
2233
+
2234
+ overlay_name = overlay_name_from_nr(overlay_nr + 1);
2235
+
2236
+ ret = overlay_data_apply(overlay_name, &ovcs_id);
2237
+
2238
+ if (!ret) {
2239
+ unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2240
+ return;
2241
+ }
2242
+ ov_id[1] = ovcs_id;
2243
+ of_unittest_track_overlay(ov_id[1]);
2244
+
2245
+ EXPECT_END(KERN_INFO,
2246
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
2247
+
16792248
16802249 for (i = 0; i < 2; i++) {
16812250 /* unittest device must be in after state */
....@@ -1716,6 +2285,7 @@
17162285 }
17172286
17182287 unittest(1, "overlay test %d passed\n", 6);
2288
+
17192289 }
17202290
17212291 /* test overlay application in sequence */
....@@ -1724,26 +2294,65 @@
17242294 int i, ov_id[2], ovcs_id;
17252295 int overlay_nr = 8, unittest_nr = 8;
17262296 const char *overlay_name;
2297
+ int ret;
17272298
17282299 /* we don't care about device state in this test */
17292300
2301
+ EXPECT_BEGIN(KERN_INFO,
2302
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
2303
+
2304
+ overlay_name = overlay_name_from_nr(overlay_nr + 0);
2305
+
2306
+ ret = overlay_data_apply(overlay_name, &ovcs_id);
2307
+ if (!ret)
2308
+ unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2309
+
2310
+ EXPECT_END(KERN_INFO,
2311
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
2312
+
2313
+ if (!ret)
2314
+ return;
2315
+
2316
+ ov_id[0] = ovcs_id;
2317
+ of_unittest_track_overlay(ov_id[0]);
2318
+
2319
+ overlay_name = overlay_name_from_nr(overlay_nr + 1);
2320
+
2321
+ EXPECT_BEGIN(KERN_INFO,
2322
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
2323
+
17302324 /* apply the overlays */
1731
- for (i = 0; i < 2; i++) {
2325
+ ret = overlay_data_apply(overlay_name, &ovcs_id);
17322326
1733
- overlay_name = overlay_name_from_nr(overlay_nr + i);
2327
+ EXPECT_END(KERN_INFO,
2328
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
17342329
1735
- if (!overlay_data_apply(overlay_name, &ovcs_id)) {
1736
- unittest(0, "could not apply overlay \"%s\"\n",
1737
- overlay_name);
1738
- return;
1739
- }
1740
- ov_id[i] = ovcs_id;
1741
- of_unittest_track_overlay(ov_id[i]);
2330
+ if (!ret) {
2331
+ unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2332
+ return;
17422333 }
2334
+
2335
+ ov_id[1] = ovcs_id;
2336
+ of_unittest_track_overlay(ov_id[1]);
17432337
17442338 /* now try to remove first overlay (it should fail) */
17452339 ovcs_id = ov_id[0];
1746
- if (!of_overlay_remove(&ovcs_id)) {
2340
+
2341
+ EXPECT_BEGIN(KERN_INFO,
2342
+ "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
2343
+
2344
+ EXPECT_BEGIN(KERN_INFO,
2345
+ "OF: overlay: overlay #6 is not topmost");
2346
+
2347
+ ret = of_overlay_remove(&ovcs_id);
2348
+
2349
+ EXPECT_END(KERN_INFO,
2350
+ "OF: overlay: overlay #6 is not topmost");
2351
+
2352
+ EXPECT_END(KERN_INFO,
2353
+ "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
2354
+
2355
+ if (!ret) {
17472356 unittest(0, "%s was destroyed @\"%s\"\n",
17482357 overlay_name_from_nr(overlay_nr + 0),
17492358 unittest_path(unittest_nr,
....@@ -1775,6 +2384,7 @@
17752384
17762385 /* device should disable */
17772386 ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
2387
+
17782388 if (unittest(ret == 0,
17792389 "overlay test %d failed; overlay application\n", 10))
17802390 return;
....@@ -1798,6 +2408,7 @@
17982408 /* device should disable */
17992409 ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
18002410 PDEV_OVERLAY);
2411
+
18012412 unittest(ret == 0, "overlay test %d failed; overlay apply\n", 11);
18022413 }
18032414
....@@ -1845,10 +2456,8 @@
18452456 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
18462457
18472458 std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
1848
- if (!std) {
1849
- dev_err(dev, "Failed to allocate unittest i2c data\n");
2459
+ if (!std)
18502460 return -ENOMEM;
1851
- }
18522461
18532462 /* link them together */
18542463 std->pdev = pdev;
....@@ -1951,7 +2560,7 @@
19512560 {
19522561 int i, nchans;
19532562 struct device *dev = &client->dev;
1954
- struct i2c_adapter *adap = to_i2c_adapter(dev->parent);
2563
+ struct i2c_adapter *adap = client->adapter;
19552564 struct device_node *np = client->dev.of_node, *child;
19562565 struct i2c_mux_core *muxc;
19572566 u32 reg, max_reg;
....@@ -2030,12 +2639,21 @@
20302639 return ret;
20312640
20322641 ret = platform_driver_register(&unittest_i2c_bus_driver);
2642
+
20332643 if (unittest(ret == 0,
20342644 "could not register unittest i2c bus driver\n"))
20352645 return ret;
20362646
20372647 #if IS_BUILTIN(CONFIG_I2C_MUX)
2648
+
2649
+ EXPECT_BEGIN(KERN_INFO,
2650
+ "i2c i2c-1: Added multiplexed i2c bus 2");
2651
+
20382652 ret = i2c_add_driver(&unittest_i2c_mux_driver);
2653
+
2654
+ EXPECT_END(KERN_INFO,
2655
+ "i2c i2c-1: Added multiplexed i2c bus 2");
2656
+
20392657 if (unittest(ret == 0,
20402658 "could not register unittest i2c mux driver\n"))
20412659 return ret;
....@@ -2055,8 +2673,18 @@
20552673
20562674 static void __init of_unittest_overlay_i2c_12(void)
20572675 {
2676
+ int ret;
2677
+
20582678 /* device should enable */
2059
- if (of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY))
2679
+ EXPECT_BEGIN(KERN_INFO,
2680
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
2681
+
2682
+ ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
2683
+
2684
+ EXPECT_END(KERN_INFO,
2685
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
2686
+
2687
+ if (ret)
20602688 return;
20612689
20622690 unittest(1, "overlay test %d passed\n", 12);
....@@ -2065,8 +2693,18 @@
20652693 /* test deactivation of device */
20662694 static void __init of_unittest_overlay_i2c_13(void)
20672695 {
2696
+ int ret;
2697
+
2698
+ EXPECT_BEGIN(KERN_INFO,
2699
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
2700
+
20682701 /* device should disable */
2069
- if (of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY))
2702
+ ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
2703
+
2704
+ EXPECT_END(KERN_INFO,
2705
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
2706
+
2707
+ if (ret)
20702708 return;
20712709
20722710 unittest(1, "overlay test %d passed\n", 13);
....@@ -2079,8 +2717,18 @@
20792717
20802718 static void __init of_unittest_overlay_i2c_15(void)
20812719 {
2720
+ int ret;
2721
+
20822722 /* device should enable */
2083
- if (of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY))
2723
+ EXPECT_BEGIN(KERN_INFO,
2724
+ "i2c i2c-1: Added multiplexed i2c bus 3");
2725
+
2726
+ ret = of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY);
2727
+
2728
+ EXPECT_END(KERN_INFO,
2729
+ "i2c i2c-1: Added multiplexed i2c bus 3");
2730
+
2731
+ if (ret)
20842732 return;
20852733
20862734 unittest(1, "overlay test %d passed\n", 15);
....@@ -2152,6 +2800,8 @@
21522800 of_unittest_overlay_i2c_cleanup();
21532801 #endif
21542802
2803
+ of_unittest_overlay_gpio();
2804
+
21552805 of_unittest_destroy_tracked_overlays();
21562806
21572807 out:
....@@ -2205,10 +2855,18 @@
22052855 OVERLAY_INFO_EXTERN(overlay_12);
22062856 OVERLAY_INFO_EXTERN(overlay_13);
22072857 OVERLAY_INFO_EXTERN(overlay_15);
2858
+OVERLAY_INFO_EXTERN(overlay_gpio_01);
2859
+OVERLAY_INFO_EXTERN(overlay_gpio_02a);
2860
+OVERLAY_INFO_EXTERN(overlay_gpio_02b);
2861
+OVERLAY_INFO_EXTERN(overlay_gpio_03);
2862
+OVERLAY_INFO_EXTERN(overlay_gpio_04a);
2863
+OVERLAY_INFO_EXTERN(overlay_gpio_04b);
2864
+OVERLAY_INFO_EXTERN(overlay_bad_add_dup_node);
2865
+OVERLAY_INFO_EXTERN(overlay_bad_add_dup_prop);
22082866 OVERLAY_INFO_EXTERN(overlay_bad_phandle);
22092867 OVERLAY_INFO_EXTERN(overlay_bad_symbol);
22102868
2211
-/* order of entries is hard-coded into users of overlays[] */
2869
+/* entries found by name */
22122870 static struct overlay_info overlays[] = {
22132871 OVERLAY_INFO(overlay_base, -9999),
22142872 OVERLAY_INFO(overlay, 0),
....@@ -2227,16 +2885,31 @@
22272885 OVERLAY_INFO(overlay_12, 0),
22282886 OVERLAY_INFO(overlay_13, 0),
22292887 OVERLAY_INFO(overlay_15, 0),
2888
+ OVERLAY_INFO(overlay_gpio_01, 0),
2889
+ OVERLAY_INFO(overlay_gpio_02a, 0),
2890
+ OVERLAY_INFO(overlay_gpio_02b, 0),
2891
+ OVERLAY_INFO(overlay_gpio_03, 0),
2892
+ OVERLAY_INFO(overlay_gpio_04a, 0),
2893
+ OVERLAY_INFO(overlay_gpio_04b, 0),
2894
+ OVERLAY_INFO(overlay_bad_add_dup_node, -EINVAL),
2895
+ OVERLAY_INFO(overlay_bad_add_dup_prop, -EINVAL),
22302896 OVERLAY_INFO(overlay_bad_phandle, -EINVAL),
22312897 OVERLAY_INFO(overlay_bad_symbol, -EINVAL),
2232
- {}
2898
+ /* end marker */
2899
+ {.dtb_begin = NULL, .dtb_end = NULL, .expected_result = 0, .name = NULL}
22332900 };
22342901
22352902 static struct device_node *overlay_base_root;
22362903
22372904 static void * __init dt_alloc_memory(u64 size, u64 align)
22382905 {
2239
- return memblock_virt_alloc(size, align);
2906
+ void *ptr = memblock_alloc(size, align);
2907
+
2908
+ if (!ptr)
2909
+ panic("%s: Failed to allocate %llu bytes align=0x%llx\n",
2910
+ __func__, size, align);
2911
+
2912
+ return ptr;
22402913 }
22412914
22422915 /*
....@@ -2259,6 +2932,19 @@
22592932 u32 data_size;
22602933 void *new_fdt;
22612934 u32 size;
2935
+ int found = 0;
2936
+ const char *overlay_name = "overlay_base";
2937
+
2938
+ for (info = overlays; info && info->name; info++) {
2939
+ if (!strcmp(overlay_name, info->name)) {
2940
+ found = 1;
2941
+ break;
2942
+ }
2943
+ }
2944
+ if (!found) {
2945
+ pr_err("no overlay data for %s\n", overlay_name);
2946
+ return;
2947
+ }
22622948
22632949 info = &overlays[0];
22642950
....@@ -2306,11 +2992,10 @@
23062992 {
23072993 struct overlay_info *info;
23082994 int found = 0;
2309
- int k;
23102995 int ret;
23112996 u32 size;
23122997
2313
- for (k = 0, info = overlays; info && info->name; info++, k++) {
2998
+ for (info = overlays; info && info->name; info++) {
23142999 if (!strcmp(overlay_name, info->name)) {
23153000 found = 1;
23163001 break;
....@@ -2357,6 +3042,7 @@
23573042 struct device_node *overlay_base_symbols;
23583043 struct device_node **pprev;
23593044 struct property *prop;
3045
+ int ret;
23603046
23613047 if (!overlay_base_root) {
23623048 unittest(0, "overlay_base_root not initialized\n");
....@@ -2383,7 +3069,7 @@
23833069 */
23843070 pprev = &overlay_base_root->child;
23853071 for (np = overlay_base_root->child; np; np = np->sibling) {
2386
- if (!of_node_cmp(np->name, "__local_fixups__")) {
3072
+ if (of_node_name_eq(np, "__local_fixups__")) {
23873073 *pprev = np->sibling;
23883074 break;
23893075 }
....@@ -2396,7 +3082,7 @@
23963082 /* will have to graft properties from node into live tree */
23973083 pprev = &overlay_base_root->child;
23983084 for (np = overlay_base_root->child; np; np = np->sibling) {
2399
- if (!of_node_cmp(np->name, "__symbols__")) {
3085
+ if (of_node_name_eq(np, "__symbols__")) {
24003086 overlay_base_symbols = np;
24013087 *pprev = np->sibling;
24023088 break;
....@@ -2405,11 +3091,14 @@
24053091 }
24063092 }
24073093
2408
- for (np = overlay_base_root->child; np; np = np->sibling) {
2409
- if (of_get_child_by_name(of_root, np->name)) {
2410
- unittest(0, "illegal node name in overlay_base %s",
2411
- np->name);
2412
- return;
3094
+ for_each_child_of_node(overlay_base_root, np) {
3095
+ struct device_node *base_child;
3096
+ for_each_child_of_node(of_root, base_child) {
3097
+ if (!strcmp(np->full_name, base_child->full_name)) {
3098
+ unittest(0, "illegal node name in overlay_base %pOFn",
3099
+ np);
3100
+ return;
3101
+ }
24133102 }
24143103 }
24153104
....@@ -2471,8 +3160,85 @@
24713160
24723161 /* now do the normal overlay usage test */
24733162
2474
- unittest(overlay_data_apply("overlay", NULL),
2475
- "Adding overlay 'overlay' failed\n");
3163
+ EXPECT_BEGIN(KERN_ERR,
3164
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
3165
+ EXPECT_BEGIN(KERN_ERR,
3166
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
3167
+ EXPECT_BEGIN(KERN_ERR,
3168
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
3169
+ EXPECT_BEGIN(KERN_ERR,
3170
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
3171
+ EXPECT_BEGIN(KERN_ERR,
3172
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
3173
+ EXPECT_BEGIN(KERN_ERR,
3174
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
3175
+ EXPECT_BEGIN(KERN_ERR,
3176
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
3177
+ EXPECT_BEGIN(KERN_ERR,
3178
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
3179
+ EXPECT_BEGIN(KERN_ERR,
3180
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
3181
+ EXPECT_BEGIN(KERN_ERR,
3182
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
3183
+ EXPECT_BEGIN(KERN_ERR,
3184
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
3185
+
3186
+ ret = overlay_data_apply("overlay", NULL);
3187
+
3188
+ EXPECT_END(KERN_ERR,
3189
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
3190
+ EXPECT_END(KERN_ERR,
3191
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
3192
+ EXPECT_END(KERN_ERR,
3193
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
3194
+ EXPECT_END(KERN_ERR,
3195
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
3196
+ EXPECT_END(KERN_ERR,
3197
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
3198
+ EXPECT_END(KERN_ERR,
3199
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
3200
+ EXPECT_END(KERN_ERR,
3201
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
3202
+ EXPECT_END(KERN_ERR,
3203
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
3204
+ EXPECT_END(KERN_ERR,
3205
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
3206
+ EXPECT_END(KERN_ERR,
3207
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
3208
+ EXPECT_END(KERN_ERR,
3209
+ "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
3210
+
3211
+ unittest(ret, "Adding overlay 'overlay' failed\n");
3212
+
3213
+ EXPECT_BEGIN(KERN_ERR,
3214
+ "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
3215
+ EXPECT_BEGIN(KERN_ERR,
3216
+ "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
3217
+
3218
+ unittest(overlay_data_apply("overlay_bad_add_dup_node", NULL),
3219
+ "Adding overlay 'overlay_bad_add_dup_node' failed\n");
3220
+
3221
+ EXPECT_END(KERN_ERR,
3222
+ "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
3223
+ EXPECT_END(KERN_ERR,
3224
+ "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
3225
+
3226
+ EXPECT_BEGIN(KERN_ERR,
3227
+ "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
3228
+ EXPECT_BEGIN(KERN_ERR,
3229
+ "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
3230
+ EXPECT_BEGIN(KERN_ERR,
3231
+ "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
3232
+
3233
+ unittest(overlay_data_apply("overlay_bad_add_dup_prop", NULL),
3234
+ "Adding overlay 'overlay_bad_add_dup_prop' failed\n");
3235
+
3236
+ EXPECT_END(KERN_ERR,
3237
+ "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
3238
+ EXPECT_END(KERN_ERR,
3239
+ "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
3240
+ EXPECT_END(KERN_ERR,
3241
+ "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
24763242
24773243 unittest(overlay_data_apply("overlay_bad_phandle", NULL),
24783244 "Adding overlay 'overlay_bad_phandle' failed\n");
....@@ -2497,7 +3263,13 @@
24973263 struct device_node *np;
24983264 int res;
24993265
3266
+ pr_info("start of unittest - you will see error messages\n");
3267
+
25003268 /* adding data for unittest */
3269
+
3270
+ if (IS_ENABLED(CONFIG_UML))
3271
+ unittest_unflatten_overlay_base();
3272
+
25013273 res = unittest_data_add();
25023274 if (res)
25033275 return res;
....@@ -2511,7 +3283,6 @@
25113283 }
25123284 of_node_put(np);
25133285
2514
- pr_info("start of unittest - you will see error messages\n");
25153286 of_unittest_check_tree_linkage();
25163287 of_unittest_check_phandles();
25173288 of_unittest_find_node_by_name();
....@@ -2524,6 +3295,9 @@
25243295 of_unittest_changeset();
25253296 of_unittest_parse_interrupts();
25263297 of_unittest_parse_interrupts_extended();
3298
+ of_unittest_dma_get_max_cpu_address();
3299
+ of_unittest_parse_dma_ranges();
3300
+ of_unittest_pci_dma_ranges();
25273301 of_unittest_match_node();
25283302 of_unittest_platform_populate();
25293303 of_unittest_overlay();