blob: c5eefec2185f911347be44cff53864cf194e6d74 [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>
Mark Kettenis15518962023-01-21 20:27:53 +01007#include <iommu.h>
Mark Kettenis15518962023-01-21 20:27:53 +01008#include <asm/io.h>
Sughosh Ganu0c0ed2b2024-08-26 17:29:36 +05309#include <asm/test.h>
Mark Kettenis15518962023-01-21 20:27:53 +010010#include <linux/sizes.h>
11
Mark Kettenis15518962023-01-21 20:27:53 +010012static dma_addr_t sandbox_iommu_map(struct udevice *dev, void *addr,
13 size_t size)
14{
Mark Kettenis15518962023-01-21 20:27:53 +010015 phys_addr_t paddr, dva;
16 phys_size_t psize, off;
17
Sughosh Ganu0c0ed2b2024-08-26 17:29:36 +053018 paddr = ALIGN_DOWN(virt_to_phys(addr), SANDBOX_IOMMU_PAGE_SIZE);
Mark Kettenis15518962023-01-21 20:27:53 +010019 off = virt_to_phys(addr) - paddr;
Sughosh Ganu0c0ed2b2024-08-26 17:29:36 +053020 psize = ALIGN(size + off, SANDBOX_IOMMU_PAGE_SIZE);
21 dva = (phys_addr_t)SANDBOX_IOMMU_DVA_ADDR;
Mark Kettenis15518962023-01-21 20:27:53 +010022
23 return dva + off;
24}
25
26static void sandbox_iommu_unmap(struct udevice *dev, dma_addr_t addr,
27 size_t size)
28{
Mark Kettenis15518962023-01-21 20:27:53 +010029 phys_addr_t dva;
30 phys_size_t psize;
31
Sughosh Ganu0c0ed2b2024-08-26 17:29:36 +053032 dva = ALIGN_DOWN(addr, SANDBOX_IOMMU_PAGE_SIZE);
Mark Kettenis15518962023-01-21 20:27:53 +010033 psize = size + (addr - dva);
Sughosh Ganu0c0ed2b2024-08-26 17:29:36 +053034 psize = ALIGN(psize, SANDBOX_IOMMU_PAGE_SIZE);
Mark Kettenis15518962023-01-21 20:27:53 +010035}
36
37static struct iommu_ops sandbox_iommu_ops = {
38 .map = sandbox_iommu_map,
39 .unmap = sandbox_iommu_unmap,
40};
41
Mark Kettenis67748ee2021-10-23 16:58:02 +020042static const struct udevice_id sandbox_iommu_ids[] = {
43 { .compatible = "sandbox,iommu" },
44 { /* sentinel */ }
45};
46
47U_BOOT_DRIVER(sandbox_iommu) = {
48 .name = "sandbox_iommu",
49 .id = UCLASS_IOMMU,
50 .of_match = sandbox_iommu_ids,
Mark Kettenis15518962023-01-21 20:27:53 +010051 .ops = &sandbox_iommu_ops,
Mark Kettenis67748ee2021-10-23 16:58:02 +020052};