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