Mark Kettenis | 67748ee | 2021-10-23 16:58:02 +0200 | [diff] [blame] | 1 | // 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 Kettenis | 1551896 | 2023-01-21 20:27:53 +0100 | [diff] [blame] | 11 | #include <malloc.h> |
Mark Kettenis | 67748ee | 2021-10-23 16:58:02 +0200 | [diff] [blame] | 12 | #include <test/test.h> |
| 13 | #include <test/ut.h> |
Mark Kettenis | 1551896 | 2023-01-21 20:27:53 +0100 | [diff] [blame] | 14 | #include <asm/io.h> |
Mark Kettenis | 67748ee | 2021-10-23 16:58:02 +0200 | [diff] [blame] | 15 | |
| 16 | static int dm_test_iommu(struct unit_test_state *uts) |
| 17 | { |
| 18 | struct udevice *dev; |
Mark Kettenis | 1551896 | 2023-01-21 20:27:53 +0100 | [diff] [blame] | 19 | dma_addr_t dva; |
| 20 | void *addr; |
Mark Kettenis | 67748ee | 2021-10-23 16:58:02 +0200 | [diff] [blame] | 21 | |
| 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 Kettenis | 1551896 | 2023-01-21 20:27:53 +0100 | [diff] [blame] | 29 | 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 Kettenis | 67748ee | 2021-10-23 16:58:02 +0200 | [diff] [blame] | 40 | return 0; |
| 41 | } |
Mark Kettenis | 67748ee | 2021-10-23 16:58:02 +0200 | [diff] [blame] | 42 | DM_TEST(dm_test_iommu, UT_TESTF_SCAN_FDT); |
Mark Kettenis | 1551896 | 2023-01-21 20:27:53 +0100 | [diff] [blame] | 43 | |
| 44 | static 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 | } |
| 70 | DM_TEST(dm_test_iommu_noiommu, UT_TESTF_SCAN_FDT); |
Mark Kettenis | 5dfd4ec | 2023-01-21 20:27:57 +0100 | [diff] [blame] | 71 | |
| 72 | static 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 | } |
| 85 | DM_TEST(dm_test_iommu_pci, UT_TESTF_SCAN_FDT); |
| 86 | |
| 87 | static 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 | } |
| 100 | DM_TEST(dm_test_iommu_pci_noiommu, UT_TESTF_SCAN_FDT); |