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> |
Mark Kettenis | 1551896 | 2023-01-21 20:27:53 +0100 | [diff] [blame] | 7 | #include <iommu.h> |
Mark Kettenis | 1551896 | 2023-01-21 20:27:53 +0100 | [diff] [blame] | 8 | #include <asm/io.h> |
Sughosh Ganu | 0c0ed2b | 2024-08-26 17:29:36 +0530 | [diff] [blame] | 9 | #include <asm/test.h> |
Mark Kettenis | 1551896 | 2023-01-21 20:27:53 +0100 | [diff] [blame] | 10 | #include <linux/sizes.h> |
| 11 | |
Mark Kettenis | 1551896 | 2023-01-21 20:27:53 +0100 | [diff] [blame] | 12 | static dma_addr_t sandbox_iommu_map(struct udevice *dev, void *addr, |
| 13 | size_t size) |
| 14 | { |
Mark Kettenis | 1551896 | 2023-01-21 20:27:53 +0100 | [diff] [blame] | 15 | phys_addr_t paddr, dva; |
| 16 | phys_size_t psize, off; |
| 17 | |
Sughosh Ganu | 0c0ed2b | 2024-08-26 17:29:36 +0530 | [diff] [blame] | 18 | paddr = ALIGN_DOWN(virt_to_phys(addr), SANDBOX_IOMMU_PAGE_SIZE); |
Mark Kettenis | 1551896 | 2023-01-21 20:27:53 +0100 | [diff] [blame] | 19 | off = virt_to_phys(addr) - paddr; |
Sughosh Ganu | 0c0ed2b | 2024-08-26 17:29:36 +0530 | [diff] [blame] | 20 | psize = ALIGN(size + off, SANDBOX_IOMMU_PAGE_SIZE); |
| 21 | dva = (phys_addr_t)SANDBOX_IOMMU_DVA_ADDR; |
Mark Kettenis | 1551896 | 2023-01-21 20:27:53 +0100 | [diff] [blame] | 22 | |
| 23 | return dva + off; |
| 24 | } |
| 25 | |
| 26 | static void sandbox_iommu_unmap(struct udevice *dev, dma_addr_t addr, |
| 27 | size_t size) |
| 28 | { |
Mark Kettenis | 1551896 | 2023-01-21 20:27:53 +0100 | [diff] [blame] | 29 | phys_addr_t dva; |
| 30 | phys_size_t psize; |
| 31 | |
Sughosh Ganu | 0c0ed2b | 2024-08-26 17:29:36 +0530 | [diff] [blame] | 32 | dva = ALIGN_DOWN(addr, SANDBOX_IOMMU_PAGE_SIZE); |
Mark Kettenis | 1551896 | 2023-01-21 20:27:53 +0100 | [diff] [blame] | 33 | psize = size + (addr - dva); |
Sughosh Ganu | 0c0ed2b | 2024-08-26 17:29:36 +0530 | [diff] [blame] | 34 | psize = ALIGN(psize, SANDBOX_IOMMU_PAGE_SIZE); |
Mark Kettenis | 1551896 | 2023-01-21 20:27:53 +0100 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | static struct iommu_ops sandbox_iommu_ops = { |
| 38 | .map = sandbox_iommu_map, |
| 39 | .unmap = sandbox_iommu_unmap, |
| 40 | }; |
| 41 | |
Mark Kettenis | 67748ee | 2021-10-23 16:58:02 +0200 | [diff] [blame] | 42 | static const struct udevice_id sandbox_iommu_ids[] = { |
| 43 | { .compatible = "sandbox,iommu" }, |
| 44 | { /* sentinel */ } |
| 45 | }; |
| 46 | |
| 47 | U_BOOT_DRIVER(sandbox_iommu) = { |
| 48 | .name = "sandbox_iommu", |
| 49 | .id = UCLASS_IOMMU, |
| 50 | .of_match = sandbox_iommu_ids, |
Mark Kettenis | 1551896 | 2023-01-21 20:27:53 +0100 | [diff] [blame] | 51 | .ops = &sandbox_iommu_ops, |
Mark Kettenis | 67748ee | 2021-10-23 16:58:02 +0200 | [diff] [blame] | 52 | }; |