aboutsummaryrefslogtreecommitdiff
path: root/test/dm/iommu.c
blob: 62d38f1214ac7fee52b6adc1330456b095ffa3eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2021 Mark Kettenis <kettenis@openbsd.org>
 */

#include <common.h>
#include <dm.h>
#include <dm/test.h>
#include <dm/uclass-internal.h>
#include <iommu.h>
#include <malloc.h>
#include <test/test.h>
#include <test/ut.h>
#include <asm/io.h>

static int dm_test_iommu(struct unit_test_state *uts)
{
	struct udevice *dev;
	dma_addr_t dva;
	void *addr;

	ut_assertok(uclass_find_device(UCLASS_IOMMU, 0, &dev));
	ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));

	/* Probing USB probes the IOMMU through the "iommus" property */
	ut_assertok(uclass_probe_all(UCLASS_USB));
	ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);

	addr = malloc(256);
	ut_assert(addr);

	ut_assertok(uclass_find_device(UCLASS_USB, 0, &dev));
	dva = dev_iommu_dma_map(dev, addr, 256);
	ut_assert(dva >= 0x89abc000 && dva < 0x89ac00000);

	dev_iommu_dma_unmap(dev, dva, 256);

	free(addr);

	return 0;
}
DM_TEST(dm_test_iommu, UT_TESTF_SCAN_FDT);

static int dm_test_iommu_noiommu(struct unit_test_state *uts)
{
	struct udevice *dev;
	dma_addr_t dva;
	void *addr;

	ut_assertok(uclass_find_device(UCLASS_IOMMU, 0, &dev));
	ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));

	/* Probing ethernet should not probe the IOMMU */
	ut_assertok(uclass_probe_all(UCLASS_ETH));
	ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));

	addr = malloc(256);
	ut_assert(addr);

	ut_assertok(uclass_find_device(UCLASS_ETH, 0, &dev));
	dva = dev_iommu_dma_map(dev, addr, 256);
	ut_assert(dva == virt_to_phys(addr));

	dev_iommu_dma_unmap(dev, dva, 256);

	free(addr);

	return 0;
}
DM_TEST(dm_test_iommu_noiommu, UT_TESTF_SCAN_FDT);

static int dm_test_iommu_pci(struct unit_test_state *uts)
{
	struct udevice *dev;

	ut_assertok(uclass_find_device(UCLASS_IOMMU, 0, &dev));
	ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));

	/* Probing P2SB probes the IOMMU through the "iommu-map" property */
	ut_assertok(uclass_probe_all(UCLASS_P2SB));
	ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);

	return 0;
}
DM_TEST(dm_test_iommu_pci, UT_TESTF_SCAN_FDT);

static int dm_test_iommu_pci_noiommu(struct unit_test_state *uts)
{
	struct udevice *dev;

	ut_assertok(uclass_find_device(UCLASS_IOMMU, 0, &dev));
	ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));

	/* Probing PMC should not probe the IOMMU */
	ut_assertok(uclass_probe_all(UCLASS_ACPI_PMC));
	ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));

	return 0;
}
DM_TEST(dm_test_iommu_pci_noiommu, UT_TESTF_SCAN_FDT);