blob: 62d38f1214ac7fee52b6adc1330456b095ffa3eb [file] [log] [blame]
Mark Kettenisfb574622021-10-23 16:58:02 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2021 Mark Kettenis <kettenis@openbsd.org>
4 */
5
6#include <common.h>
7#include <dm.h>
8#include <dm/test.h>
9#include <dm/uclass-internal.h>
10#include <iommu.h>
Mark Kettenis49a1a4b2023-01-21 20:27:53 +010011#include <malloc.h>
Mark Kettenisfb574622021-10-23 16:58:02 +020012#include <test/test.h>
13#include <test/ut.h>
Mark Kettenis49a1a4b2023-01-21 20:27:53 +010014#include <asm/io.h>
Mark Kettenisfb574622021-10-23 16:58:02 +020015
16static int dm_test_iommu(struct unit_test_state *uts)
17{
18 struct udevice *dev;
Mark Kettenis49a1a4b2023-01-21 20:27:53 +010019 dma_addr_t dva;
20 void *addr;
Mark Kettenisfb574622021-10-23 16:58:02 +020021
22 ut_assertok(uclass_find_device(UCLASS_IOMMU, 0, &dev));
23 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
24
25 /* Probing USB probes the IOMMU through the "iommus" property */
26 ut_assertok(uclass_probe_all(UCLASS_USB));
27 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
28
Mark Kettenis49a1a4b2023-01-21 20:27:53 +010029 addr = malloc(256);
30 ut_assert(addr);
31
32 ut_assertok(uclass_find_device(UCLASS_USB, 0, &dev));
33 dva = dev_iommu_dma_map(dev, addr, 256);
34 ut_assert(dva >= 0x89abc000 && dva < 0x89ac00000);
35
36 dev_iommu_dma_unmap(dev, dva, 256);
37
38 free(addr);
39
Mark Kettenisfb574622021-10-23 16:58:02 +020040 return 0;
41}
Mark Kettenisfb574622021-10-23 16:58:02 +020042DM_TEST(dm_test_iommu, UT_TESTF_SCAN_FDT);
Mark Kettenis49a1a4b2023-01-21 20:27:53 +010043
44static int dm_test_iommu_noiommu(struct unit_test_state *uts)
45{
46 struct udevice *dev;
47 dma_addr_t dva;
48 void *addr;
49
50 ut_assertok(uclass_find_device(UCLASS_IOMMU, 0, &dev));
51 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
52
53 /* Probing ethernet should not probe the IOMMU */
54 ut_assertok(uclass_probe_all(UCLASS_ETH));
55 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
56
57 addr = malloc(256);
58 ut_assert(addr);
59
60 ut_assertok(uclass_find_device(UCLASS_ETH, 0, &dev));
61 dva = dev_iommu_dma_map(dev, addr, 256);
62 ut_assert(dva == virt_to_phys(addr));
63
64 dev_iommu_dma_unmap(dev, dva, 256);
65
66 free(addr);
67
68 return 0;
69}
70DM_TEST(dm_test_iommu_noiommu, UT_TESTF_SCAN_FDT);
Mark Kettenis08386da2023-01-21 20:27:57 +010071
72static int dm_test_iommu_pci(struct unit_test_state *uts)
73{
74 struct udevice *dev;
75
76 ut_assertok(uclass_find_device(UCLASS_IOMMU, 0, &dev));
77 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
78
79 /* Probing P2SB probes the IOMMU through the "iommu-map" property */
80 ut_assertok(uclass_probe_all(UCLASS_P2SB));
81 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
82
83 return 0;
84}
85DM_TEST(dm_test_iommu_pci, UT_TESTF_SCAN_FDT);
86
87static int dm_test_iommu_pci_noiommu(struct unit_test_state *uts)
88{
89 struct udevice *dev;
90
91 ut_assertok(uclass_find_device(UCLASS_IOMMU, 0, &dev));
92 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
93
94 /* Probing PMC should not probe the IOMMU */
95 ut_assertok(uclass_probe_all(UCLASS_ACPI_PMC));
96 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
97
98 return 0;
99}
100DM_TEST(dm_test_iommu_pci_noiommu, UT_TESTF_SCAN_FDT);