Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iommu: Implement of_iommu_get_resv_regions()

This is an implementation that IOMMU drivers can use to obtain reserved
memory regions from a device tree node. It uses the reserved-memory DT
bindings to find the regions associated with a given device. If these
regions are marked accordingly, identity mappings will be created for
them in the IOMMU domain that the devices will be attached to.

Cc: Frank Rowand <frowand.list@gmail.com>
Cc: devicetree@vger.kernel.org
Reviewed-by: Rob Herring <robh@kernel.org>
Acked-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Thierry Reding <treding@nvidia.com>
Link: https://lore.kernel.org/r/20230120174251.4004100-4-thierry.reding@gmail.com
Signed-off-by: Joerg Roedel <jroedel@suse.de>

authored by

Thierry Reding and committed by
Joerg Roedel
a5bf3cfc af0d8135

+102
+94
drivers/iommu/of_iommu.c
··· 10 10 #include <linux/limits.h> 11 11 #include <linux/module.h> 12 12 #include <linux/of.h> 13 + #include <linux/of_address.h> 13 14 #include <linux/of_iommu.h> 14 15 #include <linux/of_pci.h> 15 16 #include <linux/pci.h> ··· 172 171 173 172 return ops; 174 173 } 174 + 175 + static enum iommu_resv_type iommu_resv_region_get_type(struct device *dev, struct resource *phys, 176 + phys_addr_t start, size_t length) 177 + { 178 + phys_addr_t end = start + length - 1; 179 + 180 + /* 181 + * IOMMU regions without an associated physical region cannot be 182 + * mapped and are simply reservations. 183 + */ 184 + if (phys->start >= phys->end) 185 + return IOMMU_RESV_RESERVED; 186 + 187 + /* may be IOMMU_RESV_DIRECT_RELAXABLE for certain cases */ 188 + if (start == phys->start && end == phys->end) 189 + return IOMMU_RESV_DIRECT; 190 + 191 + dev_warn(dev, "treating non-direct mapping [%pr] -> [%pap-%pap] as reservation\n", &phys, 192 + &start, &end); 193 + return IOMMU_RESV_RESERVED; 194 + } 195 + 196 + /** 197 + * of_iommu_get_resv_regions - reserved region driver helper for device tree 198 + * @dev: device for which to get reserved regions 199 + * @list: reserved region list 200 + * 201 + * IOMMU drivers can use this to implement their .get_resv_regions() callback 202 + * for memory regions attached to a device tree node. See the reserved-memory 203 + * device tree bindings on how to use these: 204 + * 205 + * Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt 206 + */ 207 + void of_iommu_get_resv_regions(struct device *dev, struct list_head *list) 208 + { 209 + #if IS_ENABLED(CONFIG_OF_ADDRESS) 210 + struct of_phandle_iterator it; 211 + int err; 212 + 213 + of_for_each_phandle(&it, err, dev->of_node, "memory-region", NULL, 0) { 214 + const __be32 *maps, *end; 215 + struct resource phys; 216 + int size; 217 + 218 + memset(&phys, 0, sizeof(phys)); 219 + 220 + /* 221 + * The "reg" property is optional and can be omitted by reserved-memory regions 222 + * that represent reservations in the IOVA space, which are regions that should 223 + * not be mapped. 224 + */ 225 + if (of_find_property(it.node, "reg", NULL)) { 226 + err = of_address_to_resource(it.node, 0, &phys); 227 + if (err < 0) { 228 + dev_err(dev, "failed to parse memory region %pOF: %d\n", 229 + it.node, err); 230 + continue; 231 + } 232 + } 233 + 234 + maps = of_get_property(it.node, "iommu-addresses", &size); 235 + if (!maps) 236 + continue; 237 + 238 + end = maps + size / sizeof(__be32); 239 + 240 + while (maps < end) { 241 + struct device_node *np; 242 + u32 phandle; 243 + 244 + phandle = be32_to_cpup(maps++); 245 + np = of_find_node_by_phandle(phandle); 246 + 247 + if (np == dev->of_node) { 248 + int prot = IOMMU_READ | IOMMU_WRITE; 249 + struct iommu_resv_region *region; 250 + enum iommu_resv_type type; 251 + phys_addr_t iova; 252 + size_t length; 253 + 254 + maps = of_translate_dma_region(np, maps, &iova, &length); 255 + type = iommu_resv_region_get_type(dev, &phys, iova, length); 256 + 257 + region = iommu_alloc_resv_region(iova, length, prot, type, 258 + GFP_KERNEL); 259 + if (region) 260 + list_add_tail(&region->list, list); 261 + } 262 + } 263 + } 264 + #endif 265 + } 266 + EXPORT_SYMBOL(of_iommu_get_resv_regions);
+8
include/linux/of_iommu.h
··· 12 12 struct device_node *master_np, 13 13 const u32 *id); 14 14 15 + extern void of_iommu_get_resv_regions(struct device *dev, 16 + struct list_head *list); 17 + 15 18 #else 16 19 17 20 static inline const struct iommu_ops *of_iommu_configure(struct device *dev, ··· 22 19 const u32 *id) 23 20 { 24 21 return NULL; 22 + } 23 + 24 + static inline void of_iommu_get_resv_regions(struct device *dev, 25 + struct list_head *list) 26 + { 25 27 } 26 28 27 29 #endif /* CONFIG_OF_IOMMU */