xref: /linux/drivers/iommu/riscv/iommu-platform.c (revision fbf5df34a4dbcd09d433dd4f0916bf9b2ddb16de)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * RISC-V IOMMU as a platform device
4  *
5  * Copyright © 2023 FORTH-ICS/CARV
6  * Copyright © 2023-2024 Rivos Inc.
7  *
8  * Authors
9  *	Nick Kossifidis <mick@ics.forth.gr>
10  *	Tomasz Jeznach <tjeznach@rivosinc.com>
11  */
12 
13 #include <linux/acpi.h>
14 #include <linux/irqchip/riscv-imsic.h>
15 #include <linux/kernel.h>
16 #include <linux/msi.h>
17 #include <linux/of_irq.h>
18 #include <linux/of_platform.h>
19 #include <linux/platform_device.h>
20 
21 #include "iommu-bits.h"
22 #include "iommu.h"
23 
24 static void riscv_iommu_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
25 {
26 	struct device *dev = msi_desc_to_dev(desc);
27 	struct riscv_iommu_device *iommu = dev_get_drvdata(dev);
28 	u16 idx = desc->msi_index;
29 	u64 addr;
30 
31 	addr = ((u64)msg->address_hi << 32) | msg->address_lo;
32 
33 	if (addr != (addr & RISCV_IOMMU_MSI_CFG_TBL_ADDR)) {
34 		dev_err_once(dev,
35 			     "uh oh, the IOMMU can't send MSIs to 0x%llx, sending to 0x%llx instead\n",
36 			     addr, addr & RISCV_IOMMU_MSI_CFG_TBL_ADDR);
37 	}
38 
39 	addr &= RISCV_IOMMU_MSI_CFG_TBL_ADDR;
40 
41 	riscv_iommu_writeq(iommu, RISCV_IOMMU_REG_MSI_CFG_TBL_ADDR(idx), addr);
42 	riscv_iommu_writel(iommu, RISCV_IOMMU_REG_MSI_CFG_TBL_DATA(idx), msg->data);
43 	riscv_iommu_writel(iommu, RISCV_IOMMU_REG_MSI_CFG_TBL_CTRL(idx), 0);
44 }
45 
46 static int riscv_iommu_platform_probe(struct platform_device *pdev)
47 {
48 	enum riscv_iommu_igs_settings igs;
49 	struct device *dev = &pdev->dev;
50 	struct riscv_iommu_device *iommu = NULL;
51 	struct irq_domain *msi_domain;
52 	struct resource *res = NULL;
53 	int vec, ret;
54 
55 	iommu = devm_kzalloc(dev, sizeof(*iommu), GFP_KERNEL);
56 	if (!iommu)
57 		return -ENOMEM;
58 
59 	iommu->dev = dev;
60 	iommu->reg = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
61 	if (IS_ERR(iommu->reg))
62 		return dev_err_probe(dev, PTR_ERR(iommu->reg),
63 				     "could not map register region\n");
64 
65 	dev_set_drvdata(dev, iommu);
66 
67 	/* Check device reported capabilities / features. */
68 	iommu->caps = riscv_iommu_readq(iommu, RISCV_IOMMU_REG_CAPABILITIES);
69 	iommu->fctl = riscv_iommu_readl(iommu, RISCV_IOMMU_REG_FCTL);
70 
71 	iommu->irqs_count = RISCV_IOMMU_INTR_COUNT;
72 
73 	igs = FIELD_GET(RISCV_IOMMU_CAPABILITIES_IGS, iommu->caps);
74 	switch (igs) {
75 	case RISCV_IOMMU_CAPABILITIES_IGS_BOTH:
76 	case RISCV_IOMMU_CAPABILITIES_IGS_MSI:
77 		if (is_of_node(dev_fwnode(dev))) {
78 			of_msi_configure(dev, to_of_node(dev->fwnode));
79 		} else {
80 			msi_domain = irq_find_matching_fwnode(imsic_acpi_get_fwnode(dev),
81 							      DOMAIN_BUS_PLATFORM_MSI);
82 			dev_set_msi_domain(dev, msi_domain);
83 		}
84 
85 		if (!dev_get_msi_domain(dev)) {
86 			dev_warn(dev, "failed to find an MSI domain\n");
87 			goto msi_fail;
88 		}
89 
90 		ret = platform_device_msi_init_and_alloc_irqs(dev, iommu->irqs_count,
91 							      riscv_iommu_write_msi_msg);
92 		if (ret) {
93 			dev_warn(dev, "failed to allocate MSIs\n");
94 			goto msi_fail;
95 		}
96 
97 		for (vec = 0; vec < iommu->irqs_count; vec++)
98 			iommu->irqs[vec] = msi_get_virq(dev, vec);
99 
100 		/* Enable message-signaled interrupts, fctl.WSI */
101 		if (iommu->fctl & RISCV_IOMMU_FCTL_WSI) {
102 			iommu->fctl ^= RISCV_IOMMU_FCTL_WSI;
103 			riscv_iommu_writel(iommu, RISCV_IOMMU_REG_FCTL, iommu->fctl);
104 		}
105 
106 		dev_info(dev, "using MSIs\n");
107 		break;
108 
109 msi_fail:
110 		if (igs != RISCV_IOMMU_CAPABILITIES_IGS_BOTH) {
111 			return dev_err_probe(dev, -ENODEV,
112 					     "unable to use wire-signaled interrupts\n");
113 		}
114 
115 		fallthrough;
116 
117 	case RISCV_IOMMU_CAPABILITIES_IGS_WSI:
118 		ret = platform_irq_count(pdev);
119 		if (ret <= 0)
120 			return dev_err_probe(dev, -ENODEV,
121 					     "no IRQ resources provided\n");
122 
123 		iommu->irqs_count = ret;
124 
125 		if (iommu->irqs_count > RISCV_IOMMU_INTR_COUNT)
126 			iommu->irqs_count = RISCV_IOMMU_INTR_COUNT;
127 
128 		for (vec = 0; vec < iommu->irqs_count; vec++)
129 			iommu->irqs[vec] = platform_get_irq(pdev, vec);
130 
131 		/* Enable wire-signaled interrupts, fctl.WSI */
132 		if (!(iommu->fctl & RISCV_IOMMU_FCTL_WSI)) {
133 			iommu->fctl |= RISCV_IOMMU_FCTL_WSI;
134 			riscv_iommu_writel(iommu, RISCV_IOMMU_REG_FCTL, iommu->fctl);
135 		}
136 		dev_info(dev, "using wire-signaled interrupts\n");
137 		break;
138 	default:
139 		return dev_err_probe(dev, -ENODEV, "invalid IGS\n");
140 	}
141 
142 	return riscv_iommu_init(iommu);
143 };
144 
145 static void riscv_iommu_platform_remove(struct platform_device *pdev)
146 {
147 	struct riscv_iommu_device *iommu = dev_get_drvdata(&pdev->dev);
148 	bool msi = !(iommu->fctl & RISCV_IOMMU_FCTL_WSI);
149 
150 	riscv_iommu_remove(iommu);
151 
152 	if (msi)
153 		platform_device_msi_free_irqs_all(&pdev->dev);
154 };
155 
156 static void riscv_iommu_platform_shutdown(struct platform_device *pdev)
157 {
158 	riscv_iommu_disable(dev_get_drvdata(&pdev->dev));
159 };
160 
161 static const struct of_device_id riscv_iommu_of_match[] = {
162 	{.compatible = "riscv,iommu",},
163 	{},
164 };
165 
166 static const struct acpi_device_id riscv_iommu_acpi_match[] = {
167 	{ "RSCV0004", 0 },
168 	{}
169 };
170 MODULE_DEVICE_TABLE(acpi, riscv_iommu_acpi_match);
171 
172 static struct platform_driver riscv_iommu_platform_driver = {
173 	.probe = riscv_iommu_platform_probe,
174 	.remove = riscv_iommu_platform_remove,
175 	.shutdown = riscv_iommu_platform_shutdown,
176 	.driver = {
177 		.name = "riscv,iommu",
178 		.of_match_table = riscv_iommu_of_match,
179 		.suppress_bind_attrs = true,
180 		.acpi_match_table = riscv_iommu_acpi_match,
181 	},
182 };
183 
184 builtin_platform_driver(riscv_iommu_platform_driver);
185