blob: c5dd917f00cc9ebbe728f27c6a50aa38c5820786 [file] [log] [blame]
Mark Kettenis67748ee2021-10-23 16:58:02 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2021 Mark Kettenis <kettenis@openbsd.org>
4 */
5
Mark Kettenis67748ee2021-10-23 16:58:02 +02006#include <dm.h>
7#include <dm/test.h>
8#include <dm/uclass-internal.h>
9#include <iommu.h>
Mark Kettenis15518962023-01-21 20:27:53 +010010#include <malloc.h>
Mark Kettenis67748ee2021-10-23 16:58:02 +020011#include <test/test.h>
12#include <test/ut.h>
Mark Kettenis15518962023-01-21 20:27:53 +010013#include <asm/io.h>
Mark Kettenis67748ee2021-10-23 16:58:02 +020014
15static int dm_test_iommu(struct unit_test_state *uts)
16{
17 struct udevice *dev;
Mark Kettenis15518962023-01-21 20:27:53 +010018 dma_addr_t dva;
19 void *addr;
Mark Kettenis67748ee2021-10-23 16:58:02 +020020
21 ut_assertok(uclass_find_device(UCLASS_IOMMU, 0, &dev));
22 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
23
24 /* Probing USB probes the IOMMU through the "iommus" property */
25 ut_assertok(uclass_probe_all(UCLASS_USB));
26 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
27
Mark Kettenis15518962023-01-21 20:27:53 +010028 addr = malloc(256);
29 ut_assert(addr);
30
31 ut_assertok(uclass_find_device(UCLASS_USB, 0, &dev));
32 dva = dev_iommu_dma_map(dev, addr, 256);
33 ut_assert(dva >= 0x89abc000 && dva < 0x89ac00000);
34
35 dev_iommu_dma_unmap(dev, dva, 256);
36
37 free(addr);
38
Mark Kettenis67748ee2021-10-23 16:58:02 +020039 return 0;
40}
Simon Glass1a92f832024-08-22 07:57:48 -060041DM_TEST(dm_test_iommu, UTF_SCAN_FDT);
Mark Kettenis15518962023-01-21 20:27:53 +010042
43static int dm_test_iommu_noiommu(struct unit_test_state *uts)
44{
45 struct udevice *dev;
46 dma_addr_t dva;
47 void *addr;
48
49 ut_assertok(uclass_find_device(UCLASS_IOMMU, 0, &dev));
50 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
51
52 /* Probing ethernet should not probe the IOMMU */
53 ut_assertok(uclass_probe_all(UCLASS_ETH));
54 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
55
56 addr = malloc(256);
57 ut_assert(addr);
58
59 ut_assertok(uclass_find_device(UCLASS_ETH, 0, &dev));
60 dva = dev_iommu_dma_map(dev, addr, 256);
61 ut_assert(dva == virt_to_phys(addr));
62
63 dev_iommu_dma_unmap(dev, dva, 256);
64
65 free(addr);
66
67 return 0;
68}
Simon Glass1a92f832024-08-22 07:57:48 -060069DM_TEST(dm_test_iommu_noiommu, UTF_SCAN_FDT);
Mark Kettenis5dfd4ec2023-01-21 20:27:57 +010070
71static int dm_test_iommu_pci(struct unit_test_state *uts)
72{
73 struct udevice *dev;
74
75 ut_assertok(uclass_find_device(UCLASS_IOMMU, 0, &dev));
76 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
77
78 /* Probing P2SB probes the IOMMU through the "iommu-map" property */
79 ut_assertok(uclass_probe_all(UCLASS_P2SB));
80 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
81
82 return 0;
83}
Simon Glass1a92f832024-08-22 07:57:48 -060084DM_TEST(dm_test_iommu_pci, UTF_SCAN_FDT);
Mark Kettenis5dfd4ec2023-01-21 20:27:57 +010085
86static int dm_test_iommu_pci_noiommu(struct unit_test_state *uts)
87{
88 struct udevice *dev;
89
90 ut_assertok(uclass_find_device(UCLASS_IOMMU, 0, &dev));
91 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
92
93 /* Probing PMC should not probe the IOMMU */
94 ut_assertok(uclass_probe_all(UCLASS_ACPI_PMC));
95 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
96
97 return 0;
98}
Simon Glass1a92f832024-08-22 07:57:48 -060099DM_TEST(dm_test_iommu_pci_noiommu, UTF_SCAN_FDT);