iommu: Support mappings/reservations in reserved-memory regions
Subsystems (6)
v8 cover iommu: Support mappings/reservations in reserved-memory regions
From: Thierry Reding <[email protected]>
Hi,
This version has several fixes over the previous v6/v7, which can be found here:
https://lore.kernel.org/all/[email protected]/
An example is included in the DT bindings, but here is an extract of what I've used to test this:
reserved-memory {
#address-cells = <2>;
#size-cells = <2>;
ranges;
/*
* Creates an identity mapping for the framebuffer that
* the firmware has setup to scan out a bootsplash from.
*/
fb: framebuffer@92cb2000 {
reg = <0x0 0x92cb2000 0x0 0x00800000>;
iommu-addresses = <&dc0 0x0 0x92cb2000 0x0 0x00800000>;
};
/*
* Creates a reservation in the IOVA space to prevent
* any buffers from being mapped to that region. Note
* that on Tegra the range is actually quite different
* from this, but it would conflict with the display
* driver that I tested this against, so this is just
* a dummy region for testing.
*/
adsp: reservation-adsp {
iommu-addresses = <&dc0 0x0 0x90000000 0x0 0x00010000>;
};
};
host1x@50000000 {
dc@54200000 {
memory-region = <&fb>, <&adsp>;
};
};This is abbreviated a little to focus on the essentials. Note also that the ADSP reservation is not actually used on this device and the driver for this doesn't exist yet, but I wanted to include this variant for testing, because we'll want to use these bindings for the reservation use-case as well at some point.
Adding Alyssa and Janne who have in the past tried to make these bindings work on Apple M1. Also adding Sameer from the Tegra audio team to look at the ADSP reservation and double-check that this is suitable for our needs.
Thierry
Navneet Kumar (1):
iommu/tegra-smmu: Support managed domains
Thierry Reding (4):
dt-bindings: reserved-memory: Document iommu-addresses
iommu: Implement of_iommu_get_resv_regions()
iommu: dma: Use of_iommu_get_resv_regions() iommu/tegra-smmu: Add support for reserved regions
.../reserved-memory/reserved-memory.yaml | 70 +++++++++++++++ drivers/iommu/dma-iommu.c | 3 + drivers/iommu/of_iommu.c | 85 +++++++++++++++++++ drivers/iommu/tegra-smmu.c | 85 +++++++++++++++---- include/linux/of_iommu.h | 8 ++ 5 files changed, 234 insertions(+), 17 deletions(-)
Signature · 1 line
2.37.2
PATCH v8 1/5 dt-bindings: reserved-memory: Document iommu-addresses
From: Thierry Reding <[email protected]>
This adds the "iommu-addresses" property to reserved-memory nodes, which allow describing the interaction of memory regions with IOMMUs. Two use- cases are supported:
1. Static mappings can be described by pairing the "iommu-addresses" property with a "reg" property. This is mostly useful for adopting firmware-allocated buffers via identity mappings. One common use- case where this is required is if early firmware or bootloaders have set up a bootsplash framebuffer that a display controller is actively scanning out from during the operating system boot process.
2. If an "iommu-addresses" property exists without a "reg" property, the reserved-memory node describes an IOVA reservation. Such memory regions are excluded from the IOVA space available to operating system drivers and can be used for regions that must not be used to map arbitrary buffers.
Each mapping or reservation is tied to a specific device via a phandle to the device's device tree node. This allows a reserved-memory region to be reused across multiple devices.
---
Changes in v8:
- include validation warning fixes that had crept into an unrelated patch
Changes in v7:
- keep reserved-memory.txt to avoid broken references
Changes in v6:
- add device phandle to iommu-addresses property in examples
- remove now unused dt-bindings/reserved-memory.h header
.../reserved-memory/reserved-memory.yaml | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+)
Documentation/devicetree/bindings/reserved-memory/reserved-memory.yaml +70 -0
@@ -52,6 +52,30 @@ properties: Address and Length pairs. Specifies regions of memory that are acceptable to allocate from. + iommu-addresses: + $ref: /schemas/types.yaml#/definitions/phandle-array + description: > + A list of phandle and specifier pairs that describe static IO virtual + address space mappings and carveouts associated with a given reserved + memory region. The phandle in the first cell refers to the device for + which the mapping or carveout is to be created. + + The specifier consists of an address/size pair and denotes the IO + virtual address range of the region for the given device. The exact + format depends on the values of the "#address-cells" and "#size-cells" + properties of the device referenced via the phandle. + + When used in combination with a "reg" property, an IOVA mapping is to + be established for this memory region. One example where this can be + useful is to create an identity mapping for physical memory that the + firmware has configured some hardware to access (such as a bootsplash + framebuffer). + + If no "reg" property is specified, the "iommu-addresses" property + defines carveout regions in the IOVA space for the given device. This + can be useful if a certain memory region should not be mapped through + the IOMMU. + no-map: type: boolean description: >
@@ -97,4 +121,50 @@ oneOf: additionalProperties: true +examples: + - | + / { + compatible = "foo"; + model = "foo"; + + #address-cells = <2>; + #size-cells = <2>; + + reserved-memory { + #address-cells = <2>; + #size-cells = <2>; + ranges; + + adsp_resv: reservation-adsp { + /* + * Restrict IOVA mappings for ADSP buffers to the 512 MiB region + * from 0x40000000 - 0x5fffffff. Anything outside is reserved by + * the ADSP for I/O memory and private memory allocations. + */ + iommu-addresses = <&adsp 0x0 0x00000000 0x00 0x40000000>, + <&adsp 0x0 0x60000000 0xff 0xa0000000>; + }; + + fb: framebuffer@90000000 { + reg = <0x0 0x90000000 0x0 0x00800000>; + iommu-addresses = <&dc0 0x0 0x90000000 0x0 0x00800000>; + }; + }; + + bus@0 { + #address-cells = <1>; + #size-cells = <1>; + ranges = <0x0 0x0 0x0 0x40000000>; + + adsp: adsp@2990000 { + reg = <0x2990000 0x2000>; + memory-region = <&adsp_resv>; + }; + + dc0: display@15200000 { + reg = <0x15200000 0x10000>; + memory-region = <&fb>; + }; + }; + }; ...
Signature · 1 line
2.37.2
Re: [PATCH v8 1/5] dt-bindings: reserved-memory: Document iommu-addresses
Quoted 32 lines · Thierry Reding
From: Thierry Reding <[email protected]>
This adds the "iommu-addresses" property to reserved-memory nodes, which allow describing the interaction of memory regions with IOMMUs. Two use- cases are supported:
1. Static mappings can be described by pairing the "iommu-addresses" property with a "reg" property. This is mostly useful for adopting firmware-allocated buffers via identity mappings. One common use- case where this is required is if early firmware or bootloaders have set up a bootsplash framebuffer that a display controller is actively scanning out from during the operating system boot process.
2. If an "iommu-addresses" property exists without a "reg" property, the reserved-memory node describes an IOVA reservation. Such memory regions are excluded from the IOVA space available to operating system drivers and can be used for regions that must not be used to map arbitrary buffers.
Each mapping or reservation is tied to a specific device via a phandle to the device's device tree node. This allows a reserved-memory region to be reused across multiple devices.
Signed-off-by Thierry Reding <[email protected]>---
Changes in v8:
- include validation warning fixes that had crept into an unrelated patchChanges in v7:
- keep reserved-memory.txt to avoid broken references
Quoted 6 lines
Changes in v6:
- add device phandle to iommu-addresses property in examples
- remove now unuseddt-bindings/reserved-memory.hheader.../reserved-memory/reserved-memory.yaml | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+)
Thanks for being persistent with this. It looks good to me.
I really don't like new common bindings with only 1 user, so I hope the Asahi folks chime in here. Or really anyone else look at it.
Rob
Re: [PATCH v8 1/5] dt-bindings: reserved-memory: Document iommu-addresses
Quoted 40 lines · Rob Herring
Quoted 32 lines · Thierry Reding
From: Thierry Reding <[email protected]>
This adds the "iommu-addresses" property to reserved-memory nodes, which allow describing the interaction of memory regions with IOMMUs. Two use- cases are supported:
1. Static mappings can be described by pairing the "iommu-addresses" property with a "reg" property. This is mostly useful for adopting firmware-allocated buffers via identity mappings. One common use- case where this is required is if early firmware or bootloaders have set up a bootsplash framebuffer that a display controller is actively scanning out from during the operating system boot process.
2. If an "iommu-addresses" property exists without a "reg" property, the reserved-memory node describes an IOVA reservation. Such memory regions are excluded from the IOVA space available to operating system drivers and can be used for regions that must not be used to map arbitrary buffers.
Each mapping or reservation is tied to a specific device via a phandle to the device's device tree node. This allows a reserved-memory region to be reused across multiple devices.
Signed-off-by Thierry Reding <[email protected]>---
Changes in v8:
- include validation warning fixes that had crept into an unrelated patchChanges in v7:
- keep reserved-memory.txt to avoid broken referencesQuoted 6 lines
Changes in v6:
- add device phandle to iommu-addresses property in examples
- remove now unuseddt-bindings/reserved-memory.hheader.../reserved-memory/reserved-memory.yaml | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+)
Quoted 6 lines
Thanks for being persistent with this. It looks good to me.
Reviewed-by Rob Herring <[email protected]>I really don't like new common bindings with only 1 user, so I hope the Asahi folks chime in here. Or really anyone else look at it.
The Asahi WIP display controller/processor driver uses this binding with static non 1:1 mappings. The binding is sufficient for our needs our needs.
Janne
Re: [PATCH v8 1/5] dt-bindings: reserved-memory: Document iommu-addresses
In reply to Thierry RedingQuoted 20 lines · Thierry Reding
From: Thierry Reding <[email protected]>
This adds the "iommu-addresses" property to reserved-memory nodes, which allow describing the interaction of memory regions with IOMMUs. Two use- cases are supported:
1. Static mappings can be described by pairing the "iommu-addresses" property with a "reg" property. This is mostly useful for adopting firmware-allocated buffers via identity mappings. One common use- case where this is required is if early firmware or bootloaders have set up a bootsplash framebuffer that a display controller is actively scanning out from during the operating system boot process.
2. If an "iommu-addresses" property exists without a "reg" property, the reserved-memory node describes an IOVA reservation. Such memory regions are excluded from the IOVA space available to operating system drivers and can be used for regions that must not be used to map arbitrary buffers.
Quoted 3 lines
Each mapping or reservation is tied to a specific device via a phandle to the device's device tree node. This allows a reserved-memory region to be reused across multiple devices.
This seems sufficiently robust to me, just one possibly-relevant point which I'll get to in the following patch...
Quoted 101 lines
Signed-off-by Thierry Reding <[email protected]>---
Changes in v8:
- include validation warning fixes that had crept into an unrelated patchChanges in v7:
- keep reserved-memory.txt to avoid broken referencesChanges in v6:
- add device phandle to iommu-addresses property in examples
- remove now unuseddt-bindings/reserved-memory.hheader.../reserved-memory/reserved-memory.yaml | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+)Documentation/devicetree/bindings/reserved-memory/reserved-memory.yaml +70
-0@@ -52,6 +52,30 @@ properties: Address and Length pairs. Specifies regions of memory that are acceptable to allocate from. + iommu-addresses: + $ref: /schemas/types.yaml#/definitions/phandle-array + description: > + A list of phandle and specifier pairs that describe static IO virtual + address space mappings and carveouts associated with a given reserved + memory region. The phandle in the first cell refers to the device for + which the mapping or carveout is to be created. + + The specifier consists of an address/size pair and denotes the IO + virtual address range of the region for the given device. The exact + format depends on the values of the "#address-cells" and "#size-cells" + properties of the device referenced via the phandle. + + When used in combination with a "reg" property, an IOVA mapping is to + be established for this memory region. One example where this can be + useful is to create an identity mapping for physical memory that the + firmware has configured some hardware to access (such as a bootsplash + framebuffer). + + If no "reg" property is specified, the "iommu-addresses" property + defines carveout regions in the IOVA space for the given device. This + can be useful if a certain memory region should not be mapped through + the IOMMU. + no-map: type: boolean description: >@@ -97,4 +121,50 @@ oneOf: additionalProperties: true +examples: + - | + / { + compatible = "foo"; + model = "foo"; + + #address-cells = <2>; + #size-cells = <2>; + + reserved-memory { + #address-cells = <2>; + #size-cells = <2>; + ranges; + + adsp_resv: reservation-adsp { + /* + * Restrict IOVA mappings for ADSP buffers to the 512 MiB region + * from 0x40000000 - 0x5fffffff. Anything outside is reserved by + * the ADSP for I/O memory and private memory allocations. + */ + iommu-addresses = <&adsp 0x0 0x00000000 0x00 0x40000000>, + <&adsp 0x0 0x60000000 0xff 0xa0000000>; + }; + + fb: framebuffer@90000000 { + reg = <0x0 0x90000000 0x0 0x00800000>; + iommu-addresses = <&dc0 0x0 0x90000000 0x0 0x00800000>; + }; + }; + + bus@0 { + #address-cells = <1>; + #size-cells = <1>; + ranges = <0x0 0x0 0x0 0x40000000>; + + adsp: adsp@2990000 { + reg = <0x2990000 0x2000>; + memory-region = <&adsp_resv>; + }; + + dc0: display@15200000 { + reg = <0x15200000 0x10000>; + memory-region = <&fb>; + }; + }; + };
PATCH v8 2/5 iommu: Implement of_iommu_get_resv_regions()
In reply to Thierry RedingFrom: Thierry Reding <[email protected]>
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.
---
Changes in v8:
- cleanup set-but-unused variables
Changes in v6:
- remove reference to now unused dt-bindings/reserved-memory.h include
Changes in v5:
- update for new "iommu-addresses" device tree bindings
Changes in v4:
- fix build failure on !CONFIG_OF_ADDRESS
Changes in v3:
- change "active" property to identity mapping flag that is part of the memory region specifier (as defined by #memory-region-cells) to allow per-reference flags to be used
Changes in v2:
- use "active" property to determine whether direct mappings are needed
drivers/iommu/of_iommu.c | 85 ++++++++++++++++++++++++++++++++++++++++ include/linux/of_iommu.h | 8 ++++ 2 files changed, 93 insertions(+)
drivers/iommu/of_iommu.c +85 -0
@@ -11,6 +11,7 @@#include <linux/module.h> #include <linux/msi.h> #include <linux/of.h> +#include <linux/of_address.h> #include <linux/of_iommu.h> #include <linux/of_pci.h> #include <linux/pci.h>
@@ -172,3 +173,87 @@ const struct iommu_ops *of_iommu_configure(struct device *dev,return ops; } + +/** +* of_iommu_get_resv_regions - reserved region driver helper for device tree +* @dev: device for which to get reserved regions +* @list: reserved region list +* +* IOMMU drivers can use this to implement their .get_resv_regions() callback +* for memory regions attached to a device tree node. See the reserved-memory +* device tree bindings on how to use these: +* +* Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt +*/ +void of_iommu_get_resv_regions (struct device *dev,struct list_head *list) +{ +#if IS_ENABLED (CONFIG_OF_ADDRESS ) +struct of_phandle_iterator it; +int err; + +of_for_each_phandle (&it, err, dev->of_node,"memory-region" ,NULL ,0 ) { +struct iommu_resv_region *region; +struct resource res; +const __be32 *maps; +int size; + +memset (&res,0 ,sizeof (res)); + +/* +* The "reg" property is optional and can be omitted by reserved-memory regions +* that represent reservations in the IOVA space, which are regions that should +* not be mapped. +*/ +if (of_find_property (it.node,"reg" ,NULL )) { + err =of_address_to_resource (it.node,0 , &res); +if (err <0 ) { +dev_err (dev,"failed to parse memory region %pOF: %d\n" , + it.node, err); +continue ; + } + } + + maps =of_get_property (it.node,"iommu-addresses" , &size); +if (maps) { +const __be32 *end = maps + size /sizeof (__be32); +struct device_node *np; +u32 phandle; +int na, ns; + +while (maps < end) { +phys_addr_t start; +size_t length; + + phandle =be32_to_cpup (maps++); + np =of_find_node_by_phandle (phandle); + na =of_n_addr_cells (np); + ns =of_n_size_cells (np); + + start =of_translate_dma_address (np, maps); + length =of_read_number (maps + na, ns); + +if (np == dev->of_node) { +int prot =IOMMU_READ |IOMMU_WRITE ; +enum iommu_resv_type type; + +/* +* IOMMU regions without an associated physical region +* cannot be mapped and are simply reservations. +*/ +if (res.end > res.start) + type =IOMMU_RESV_DIRECT_RELAXABLE ; +else + type =IOMMU_RESV_RESERVED ; + + region =iommu_alloc_resv_region (start, length, prot, type); +if (region) +list_add_tail (®ion->list, list); + } + + maps += na + ns; + } + } + } +#endif +} +EXPORT_SYMBOL (of_iommu_get_resv_regions);
include/linux/of_iommu.h +8 -0
@@ -12,6 +12,9 @@ extern const struct iommu_ops *of_iommu_configure(struct device *dev,struct device_node *master_np, const u32 *id); +extern void of_iommu_get_resv_regions (struct device *dev, +struct list_head *list); +#else static inline const struct iommu_ops *of_iommu_configure(struct device *dev,
@@ -21,6 +24,11 @@ static inline const struct iommu_ops *of_iommu_configure(struct device *dev,return NULL ; } +static inline void of_iommu_get_resv_regions (struct device *dev, +struct list_head *list) +{ +} +#endif /* CONFIG_OF_IOMMU */ #endif /* __OF_IOMMU_H */
Signature · 1 line
2.37.2
Robin Murphy
maintainerRe: [PATCH v8 2/5] iommu: Implement of_iommu_get_resv_regions()
Quoted 85 lines · Thierry Reding
From: Thierry Reding <[email protected]>
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 <[email protected]>Reviewed-by Rob Herring <[email protected]>Signed-off-by Thierry Reding <[email protected]>---
Changes in v8:
- cleanup set-but-unused variablesChanges in v6:
- remove reference to now unuseddt-bindings/reserved-memory.hincludeChanges in v5:
- update for new "iommu-addresses" device tree bindingsChanges in v4:
- fix build failure on !CONFIG_OF_ADDRESSChanges in v3:
- change "active" property to identity mapping flag that is part of the memory region specifier (as defined by #memory-region-cells) to allow per-reference flags to be usedChanges in v2:
- use "active" property to determine whether direct mappings are neededdrivers/iommu/of_iommu.c | 85 ++++++++++++++++++++++++++++++++++++++++ include/linux/of_iommu.h | 8 ++++ 2 files changed, 93 insertions(+)drivers/iommu/of_iommu.c +33
-0@@ -11,6 +11,7 @@#include <linux/module.h> #include <linux/msi.h> #include <linux/of.h> +#include <linux/of_address.h> #include <linux/of_iommu.h> #include <linux/of_pci.h> #include <linux/pci.h> @@ -172,3 +173,87 @@ const struct iommu_ops *of_iommu_configure(struct device *dev,return ops; } + +/** +* of_iommu_get_resv_regions - reserved region driver helper for device tree +* @dev: device for which to get reserved regions +* @list: reserved region list +* +* IOMMU drivers can use this to implement their .get_resv_regions() callback +* for memory regions attached to a device tree node. See the reserved-memory +* device tree bindings on how to use these: +* +* Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt +*/ +void of_iommu_get_resv_regions (struct device *dev,struct list_head *list) +{ +#if IS_ENABLED (CONFIG_OF_ADDRESS ) +struct of_phandle_iterator it; +int err; + +of_for_each_phandle (&it, err, dev->of_node,"memory-region" ,NULL ,0 ) { +struct iommu_resv_region *region; +struct resource res; +const __be32 *maps; +int size; + +memset (&res,0 ,sizeof (res)); + +/* +* The "reg" property is optional and can be omitted by reserved-memory regions +* that represent reservations in the IOVA space, which are regions that should +* not be mapped. +*/ +if (of_find_property (it.node,"reg" ,NULL )) {
drivers/iommu/of_iommu.c const struct iommu_ops *of_iommu_configure(struct device *dev, · line 217
Quoted 10 lines
drivers/iommu/of_iommu.c +10
-0+ err =of_address_to_resource (it.node,0 , &res); +if (err <0 ) { +dev_err (dev,"failed to parse memory region %pOF: %d\n" , + it.node, err); +continue ; + } + } + + maps =of_get_property (it.node,"iommu-addresses" , &size); +if (maps) {
Nit: "if (!maps) continue;" and save some indentation.
Quoted 6 lines
drivers/iommu/of_iommu.c +6
-0+const __be32 *end = maps + size /sizeof (__be32); +struct device_node *np; +u32 phandle; +int na, ns; + +while (maps < end) {
drivers/iommu/of_iommu.c const struct iommu_ops *of_iommu_configure(struct device *dev, · line 233
Quoted 10 lines
drivers/iommu/of_iommu.c +10
-0+phys_addr_t start; +size_t length; + + phandle =be32_to_cpup (maps++); + np =of_find_node_by_phandle (phandle); + na =of_n_addr_cells (np); + ns =of_n_size_cells (np); + + start =of_translate_dma_address (np, maps); + length =of_read_number (maps + na, ns);
Nit: these could go inside the if condition.
drivers/iommu/of_iommu.c const struct iommu_ops *of_iommu_configure(struct device *dev, · line 244
Quoted 11 lines
drivers/iommu/of_iommu.c +11
-0+ +if (np == dev->of_node) { +int prot =IOMMU_READ |IOMMU_WRITE ; +enum iommu_resv_type type; + +/* +* IOMMU regions without an associated physical region +* cannot be mapped and are simply reservations. +*/ +if (res.end > res.start) + type =IOMMU_RESV_DIRECT_RELAXABLE ;
There may be reservations that have a PA but are expected to live beyond boot-time handover, like device firmware or a shared-memory communication buffer which the kernel driver can't reconfigure, or some kind of black hole that needs a PA because it's also "no-map" for the CPUs. Those are not relaxable. Might it be reasonable to expect to infer this from the compatible, or should we have an additional explicit flag to distinguish ephemeral boot-time mappings from permanent ones?
Furthermore, we should only use IOMMU_RESV_DIRECT (in either form) if start and length actually match res here; if not then we should warn that we're reserving the IOVA space but not actually honouring the specified mapping (we'd need a new resv_region type for arbitrary translations).
Thanks,
Robin.
Quoted 41 lines
drivers/iommu/of_iommu.c +15
-0+ else + type =IOMMU_RESV_RESERVED ; + + region =iommu_alloc_resv_region (start, length, prot, type); +if (region) + list_add_tail(®ion->list, list); + } + + maps += na + ns; + } + } + } +#endif +} +EXPORT_SYMBOL (of_iommu_get_resv_regions);include/linux/of_iommu.h +8
-0@@ -12,6 +12,9 @@ extern const struct iommu_ops *of_iommu_configure(struct device *dev,struct device_node *master_np, const u32 *id); +extern void of_iommu_get_resv_regions (struct device *dev, +struct list_head *list); +#else static inline const struct iommu_ops *of_iommu_configure(struct device *dev,@@ -21,6 +24,11 @@ static inline const struct iommu_ops *of_iommu_configure(struct device *dev,return NULL ; } +static inline void of_iommu_get_resv_regions (struct device *dev, +struct list_head *list) +{ +} +#endif /* CONFIG_OF_IOMMU */ #endif /* __OF_IOMMU_H */
Re: [PATCH v8 2/5] iommu: Implement of_iommu_get_resv_regions()
Quoted 117 lines · Robin Murphy
Quoted 84 lines · Thierry Reding
From: Thierry Reding <[email protected]>
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 <[email protected]>Reviewed-by Rob Herring <[email protected]>Signed-off-by Thierry Reding <[email protected]>---
Changes in v8:
- cleanup set-but-unused variablesChanges in v6:
- remove reference to now unuseddt-bindings/reserved-memory.hincludeChanges in v5:
- update for new "iommu-addresses" device tree bindingsChanges in v4:
- fix build failure on !CONFIG_OF_ADDRESSChanges in v3:
- change "active" property to identity mapping flag that is part of the memory region specifier (as defined by #memory-region-cells) to allow per-reference flags to be usedChanges in v2:
- use "active" property to determine whether direct mappings are neededdrivers/iommu/of_iommu.c | 85 ++++++++++++++++++++++++++++++++++++++++ include/linux/of_iommu.h | 8 ++++ 2 files changed, 93 insertions(+)drivers/iommu/of_iommu.c +33
-0@@ -11,6 +11,7 @@#include <linux/module.h> #include <linux/msi.h> #include <linux/of.h> +#include <linux/of_address.h> #include <linux/of_iommu.h> #include <linux/of_pci.h> #include <linux/pci.h> @@ -172,3 +173,87 @@ const struct iommu_ops *of_iommu_configure(struct device *dev,return ops; } + +/** +* of_iommu_get_resv_regions - reserved region driver helper for device tree +* @dev: device for which to get reserved regions +* @list: reserved region list +* +* IOMMU drivers can use this to implement their .get_resv_regions() callback +* for memory regions attached to a device tree node. See the reserved-memory +* device tree bindings on how to use these: +* +* Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt +*/ +void of_iommu_get_resv_regions (struct device *dev,struct list_head *list) +{ +#if IS_ENABLED (CONFIG_OF_ADDRESS ) +struct of_phandle_iterator it; +int err; + +of_for_each_phandle (&it, err, dev->of_node,"memory-region" ,NULL ,0 ) { +struct iommu_resv_region *region; +struct resource res; +const __be32 *maps; +int size; + +memset (&res,0 ,sizeof (res)); + +/* +* The "reg" property is optional and can be omitted by reserved-memory regions +* that represent reservations in the IOVA space, which are regions that should +* not be mapped. +*/ +if (of_find_property (it.node,"reg" ,NULL )) {drivers/iommu/of_iommu.c const struct iommu_ops *of_iommu_configure(struct device *dev, · line 216
Quoted 10 lines
drivers/iommu/of_iommu.c +10
-0+ err =of_address_to_resource (it.node,0 , &res); +if (err <0 ) { +dev_err (dev,"failed to parse memory region %pOF: %d\n" , + it.node, err); +continue ; + } + } + + maps =of_get_property (it.node,"iommu-addresses" , &size); +if (maps) {Nit: "if (!maps) continue;" and save some indentation.
Quoted 6 lines
drivers/iommu/of_iommu.c +6
-0+const __be32 *end = maps + size /sizeof (__be32); +struct device_node *np; +u32 phandle; +int na, ns; + +while (maps < end) {drivers/iommu/of_iommu.c const struct iommu_ops *of_iommu_configure(struct device *dev, · line 233
Quoted 10 lines
drivers/iommu/of_iommu.c +10
-0+phys_addr_t start; +size_t length; + + phandle =be32_to_cpup (maps++); + np =of_find_node_by_phandle (phandle); + na =of_n_addr_cells (np); + ns =of_n_size_cells (np); + + start =of_translate_dma_address (np, maps); + length =of_read_number (maps + na, ns);Nit: these could go inside the if condition.
Quoted 3 lines
drivers/iommu/of_iommu.c const struct iommu_ops *of_iommu_configure(struct device *dev, · line 236
Quoted 3 lines
drivers/iommu/of_iommu.c +3
-0+ +if (np == dev->of_node) { +int prot =IOMMU_READ |IOMMU_WRITE ;
would it be reasonable to infer IOMMU_CACHE here from "dma-coherrent"?
Quoted 9 lines
drivers/iommu/of_iommu.c const struct iommu_ops *of_iommu_configure(struct device *dev, · line 244
Quoted 8 lines
drivers/iommu/of_iommu.c +8
-0+enum iommu_resv_type type; + +/* +* IOMMU regions without an associated physical region +* cannot be mapped and are simply reservations. +*/ +if (res.end > res.start) + type =IOMMU_RESV_DIRECT_RELAXABLE ;
Quoted 7 lines
There may be reservations that have a PA but are expected to live beyond boot-time handover, like device firmware or a shared-memory communication buffer which the kernel driver can't reconfigure, or some kind of black hole that needs a PA because it's also "no-map" for the CPUs. Those are not relaxable. Might it be reasonable to expect to infer this from the compatible, or should we have an additional explicit flag to distinguish ephemeral boot-time mappings from permanent ones?
From which compatible? the device's? That's not possible for the display processor on apple silicon machines. They will carry mappings for their firmware's data and heap and the boot framebuffer. That are however not direct mappings so it doesn't apply directly to IOMMU_RESV_DIRECT_RELAXABLE. There are probably other ways we could identify the boot framebuffer but so far I'm not even convinced that we want to reuse it.
Quoted 5 lines
Furthermore, we should only use
IOMMU_RESV_DIRECT(in either form) if start
and length actually match res here; if not then we should warn that we're reserving the IOVA space but not actually honouring the specified mapping (we'd need a newresv_regiontype for arbitrary translations).
This would be needed for Asahi's dcp driver. So far I worked around this problem by direct reserved-memory parsing from apple-dart. Probably best to postpone the extension until it's needed.
Thanks,
Janne
Robin Murphy
maintainerRe: [PATCH v8 2/5] iommu: Implement of_iommu_get_resv_regions()
Quoted 118 lines · Janne Grunau
Quoted 117 lines · Robin Murphy
Quoted 84 lines · Thierry Reding
From: Thierry Reding <[email protected]>
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 <[email protected]>Reviewed-by Rob Herring <[email protected]>Signed-off-by Thierry Reding <[email protected]>---
Changes in v8:
- cleanup set-but-unused variablesChanges in v6:
- remove reference to now unuseddt-bindings/reserved-memory.hincludeChanges in v5:
- update for new "iommu-addresses" device tree bindingsChanges in v4:
- fix build failure on !CONFIG_OF_ADDRESSChanges in v3:
- change "active" property to identity mapping flag that is part of the memory region specifier (as defined by #memory-region-cells) to allow per-reference flags to be usedChanges in v2:
- use "active" property to determine whether direct mappings are neededdrivers/iommu/of_iommu.c | 85 ++++++++++++++++++++++++++++++++++++++++ include/linux/of_iommu.h | 8 ++++ 2 files changed, 93 insertions(+)drivers/iommu/of_iommu.c +33
-0@@ -11,6 +11,7 @@#include <linux/module.h> #include <linux/msi.h> #include <linux/of.h> +#include <linux/of_address.h> #include <linux/of_iommu.h> #include <linux/of_pci.h> #include <linux/pci.h> @@ -172,3 +173,87 @@ const struct iommu_ops *of_iommu_configure(struct device *dev,return ops; } + +/** +* of_iommu_get_resv_regions - reserved region driver helper for device tree +* @dev: device for which to get reserved regions +* @list: reserved region list +* +* IOMMU drivers can use this to implement their .get_resv_regions() callback +* for memory regions attached to a device tree node. See the reserved-memory +* device tree bindings on how to use these: +* +* Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt +*/ +void of_iommu_get_resv_regions (struct device *dev,struct list_head *list) +{ +#if IS_ENABLED (CONFIG_OF_ADDRESS ) +struct of_phandle_iterator it; +int err; + +of_for_each_phandle (&it, err, dev->of_node,"memory-region" ,NULL ,0 ) { +struct iommu_resv_region *region; +struct resource res; +const __be32 *maps; +int size; + +memset (&res,0 ,sizeof (res)); + +/* +* The "reg" property is optional and can be omitted by reserved-memory regions +* that represent reservations in the IOVA space, which are regions that should +* not be mapped. +*/ +if (of_find_property (it.node,"reg" ,NULL )) {drivers/iommu/of_iommu.c const struct iommu_ops *of_iommu_configure(struct device *dev, · line 216
Quoted 10 lines
drivers/iommu/of_iommu.c +10
-0+ err =of_address_to_resource (it.node,0 , &res); +if (err <0 ) { +dev_err (dev,"failed to parse memory region %pOF: %d\n" , + it.node, err); +continue ; + } + } + + maps =of_get_property (it.node,"iommu-addresses" , &size); +if (maps) {Nit: "if (!maps) continue;" and save some indentation.
Quoted 6 lines
drivers/iommu/of_iommu.c +6
-0+const __be32 *end = maps + size /sizeof (__be32); +struct device_node *np; +u32 phandle; +int na, ns; + +while (maps < end) {drivers/iommu/of_iommu.c const struct iommu_ops *of_iommu_configure(struct device *dev, · line 233
Quoted 10 lines
drivers/iommu/of_iommu.c +10
-0+phys_addr_t start; +size_t length; + + phandle =be32_to_cpup (maps++); + np =of_find_node_by_phandle (phandle); + na =of_n_addr_cells (np); + ns =of_n_size_cells (np); + + start =of_translate_dma_address (np, maps); + length =of_read_number (maps + na, ns);Nit: these could go inside the if condition.
Quoted 5 lines
Quoted 3 lines
drivers/iommu/of_iommu.c const struct iommu_ops *of_iommu_configure(struct device *dev, · line 236
Quoted 3 lines
drivers/iommu/of_iommu.c +3
-0+ +if (np == dev->of_node) { +int prot =IOMMU_READ |IOMMU_WRITE ;would it be reasonable to infer
IOMMU_CACHEhere from "dma-coherrent"?
Hmm, good point, it really depends on what the device wants - even if it is coherent, we don't necessarily know how it intends to use any particular reservation; allowing MSI writes or similar to allocate in a system cache wouldn't go too well, for instance.
Empirically, making the wrong assumption in this area can lead to people preferring to spend a year being unfairly rude on Twitter instead of providing timely productive feedback :(
Quoted 17 lines
Quoted 9 lines
drivers/iommu/of_iommu.c const struct iommu_ops *of_iommu_configure(struct device *dev, · line 244
Quoted 8 lines
drivers/iommu/of_iommu.c +8
-0+enum iommu_resv_type type; + +/* +* IOMMU regions without an associated physical region +* cannot be mapped and are simply reservations. +*/ +if (res.end > res.start) + type =IOMMU_RESV_DIRECT_RELAXABLE ;Quoted 7 lines
There may be reservations that have a PA but are expected to live beyond boot-time handover, like device firmware or a shared-memory communication buffer which the kernel driver can't reconfigure, or some kind of black hole that needs a PA because it's also "no-map" for the CPUs. Those are not relaxable. Might it be reasonable to expect to infer this from the compatible, or should we have an additional explicit flag to distinguish ephemeral boot-time mappings from permanent ones?
Quoted 7 lines
From which compatible? the device's? That's not possible for the display
processor on apple silicon machines. They will carry mappings for their firmware's data and heap and the boot framebuffer. That are however not direct mappings so it doesn't apply directly toIOMMU_RESV_DIRECT_RELAXABLE. There are probably other ways we could identify the boot framebuffer but so far I'm not even convinced that we want to reuse it.
I mean the compatibles of the reserved-memory nodes themselves. Semantically that should be the perfect way to identify their individual purposes, but in practice does mean that we might end up with a big match table to say that e.g. "apple,framebuffer" is relaxable and wants IOMMU_CACHE, and so on, and old kernels still have to have some default behaviour for new things they don't understand. It's not *too* far off the general situation for drivers, so if we expect these to be fairly rare, maybe that's OK? Particularly if it might be feasible to have some semi-generic compatibles to encapsulate the most common behaviours? I'm inclined to defer to Rob and Frank on this one.
Quoted 9 lines
Quoted 5 lines
Furthermore, we should only use
IOMMU_RESV_DIRECT(in either form) if start
and length actually match res here; if not then we should warn that we're reserving the IOVA space but not actually honouring the specified mapping (we'd need a newresv_regiontype for arbitrary translations).This would be needed for Asahi's dcp driver. So far I worked around this problem by direct reserved-memory parsing from apple-dart. Probably best to postpone the extension until it's needed.
Indeed, as long as the binding is good from the outset, we can fill in support for the more specialised cases as and when.
Thanks,
Robin.
PATCH v8 3/5 iommu: dma: Use of_iommu_get_resv_regions()
In reply to Thierry RedingFrom: Thierry Reding <[email protected]>
For device tree nodes, use the standard of_iommu_get_resv_regions() implementation to obtain the reserved memory regions associated with a device.
--- drivers/iommu/dma-iommu.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/iommu/dma-iommu.c +3 -0
@@ -24,6 +24,7 @@#include <linux/memremap.h> #include <linux/mm.h> #include <linux/mutex.h> +#include <linux/of_iommu.h> #include <linux/pci.h> #include <linux/scatterlist.h> #include <linux/spinlock.h>
@@ -390,6 +391,8 @@ void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list)if (!is_of_node (dev_iommu_fwspec_get (dev)->iommu_fwnode))iort_iommu_get_resv_regions (dev, list); +if (dev->of_node) +of_iommu_get_resv_regions (dev, list); }EXPORT_SYMBOL (iommu_dma_get_resv_regions);
Signature · 1 line
2.37.2
PATCH v8 4/5 iommu/tegra-smmu: Add support for reserved regions
In reply to Thierry RedingFrom: Thierry Reding <[email protected]>
The Tegra DRM driver currently uses the IOMMU API explicitly. This means that it has fine-grained control over when exactly the translation through the IOMMU is enabled. This currently happens after the driver probes, so the driver is in a DMA quiesced state when the IOMMU translation is enabled.
During the transition of the Tegra DRM driver to use the DMA API instead of the IOMMU API explicitly, it was observed that on certain platforms the display controllers were still actively fetching from memory. When a DMA IOMMU domain is created as part of the DMA/IOMMU API setup during boot, the IOMMU translation for the display controllers can be enabled a significant amount of time before the driver has had a chance to reset the hardware into a sane state. This causes the SMMU to detect faults on the addresses that the display controller is trying to fetch.
To avoid this, and as a byproduct paving the way for seamless transition of display from the bootloader to the kernel, add support for reserved regions in the Tegra SMMU driver. This is implemented using the standard reserved memory device tree bindings, which let us describe regions of memory which the kernel is forbidden from using for regular allocations. The Tegra SMMU driver will parse the nodes associated with each device via the "memory-region" property and return reserved regions that the IOMMU core will then create direct mappings for prior to attaching the IOMMU domains to the devices. This ensures that a 1:1 mapping is in place when IOMMU translation starts and prevents the SMMU from detecting any faults.
--- drivers/iommu/tegra-smmu.c | 50 +++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-)
drivers/iommu/tegra-smmu.c +49 -1
@@ -10,6 +10,7 @@#include <linux/kernel.h> #include <linux/of.h> #include <linux/of_device.h> +#include <linux/of_iommu.h> #include <linux/pci.h> #include <linux/platform_device.h> #include <linux/slab.h>
@@ -471,6 +472,7 @@ static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu,tegra_smmu_free_asid (smmu, as->id);dma_unmap_page (smmu->dev, as->pd_dma,SMMU_SIZE_PD ,DMA_TO_DEVICE ); + as->pd_dma =0 ; as->smmu =NULL ;
@@ -534,6 +536,38 @@ static void tegra_smmu_set_pde(struct tegra_smmu_as *as, unsigned long iova,struct tegra_smmu *smmu = as->smmu;u32 *pd =page_address (as->pd);unsigned long offset = pd_index *sizeof (*pd); +bool unmap = false; + +/* +* XXX Move this outside of this function. Perhaps add a struct +* iommu_domain parameter to ->{get,put}_resv_regions() so that +* the mapping can be done there. +* +* The problem here is that as->smmu is only known once we attach +* the domain to a device (because then we look up the right SMMU +* instance via the dev->archdata.iommu pointer). When the direct +* mappings are created for reserved regions, the domain has not +* been attached to a device yet, so we don't know. We currently +* fix that up in ->apply_resv_regions() because that is the first +* time where we have access to a struct device that will be used +* with the IOMMU domain. However, that's asymmetric and doesn't +* take care of the page directory mapping either, so we need to +* come up with something better. +*/ +if (WARN_ON_ONCE (as->pd_dma ==0 )) { + as->pd_dma =dma_map_page (smmu->dev, as->pd,0 ,SMMU_SIZE_PD , +DMA_TO_DEVICE ); +if (dma_mapping_error (smmu->dev, as->pd_dma)) +return ; + +if (!smmu_dma_addr_valid (smmu, as->pd_dma)) { +dma_unmap_page (smmu->dev, as->pd_dma,SMMU_SIZE_PD , +DMA_TO_DEVICE ); +return ; + } + + unmap = true; + }/* Set the page directory entry first */ pd[pd_index] = value;
@@ -546,6 +580,12 @@ static void tegra_smmu_set_pde(struct tegra_smmu_as *as, unsigned long iova,smmu_flush_ptc (smmu, as->pd_dma, offset);smmu_flush_tlb_section (smmu, as->id, iova);smmu_flush (smmu); + +if (unmap) { +dma_unmap_page (smmu->dev, as->pd_dma,SMMU_SIZE_PD , +DMA_TO_DEVICE ); + as->pd_dma =0 ; + } }static u32 *tegra_smmu_pte_offset (struct page *pt_page,unsigned long iova)
@@ -846,7 +886,6 @@ static struct iommu_device *tegra_smmu_probe_device(struct device *dev) smmu =tegra_smmu_find (args.np);if (smmu) { err =tegra_smmu_configure (smmu, dev, &args); -if (err <0 ) {of_node_put (args.np);return ERR_PTR (err);
@@ -864,6 +903,13 @@ static struct iommu_device *tegra_smmu_probe_device(struct device *dev)return &smmu->iommu; } +static void tegra_smmu_release_device (struct device *dev) +{ +struct tegra_smmu *smmu =dev_iommu_priv_get (dev); + +put_device (smmu->dev); +} +static const struct tegra_smmu_group_soc *tegra_smmu_find_group (struct tegra_smmu *smmu,unsigned int swgroup) {
@@ -964,7 +1010,9 @@ static int tegra_smmu_of_xlate(struct device *dev,static const struct iommu_ops tegra_smmu_ops = { .domain_alloc = tegra_smmu_domain_alloc, .probe_device = tegra_smmu_probe_device, + .release_device = tegra_smmu_release_device, .device_group = tegra_smmu_device_group, + .get_resv_regions = of_iommu_get_resv_regions, .of_xlate = tegra_smmu_of_xlate, .pgsize_bitmap =SZ_4K , .default_domain_ops = &(const struct iommu_domain_ops ) {
Signature · 1 line
2.37.2
PATCH v8 5/5 iommu/tegra-smmu: Support managed domains
In reply to Thierry RedingFrom: Navneet Kumar <[email protected]>
Allow creating identity and DMA API compatible IOMMU domains. When creating a DMA API compatible domain, make sure to also create the required cookie.
---
Changes in v5:
- remove DMA cookie initialization that's now no longer needed
drivers/iommu/tegra-smmu.c | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-)
drivers/iommu/tegra-smmu.c +20 -17
@@ -16,6 +16,7 @@#include <linux/slab.h> #include <linux/spinlock.h> #include <linux/dma-mapping.h> +#include <linux/dma-iommu.h> #include <soc/tegra/ahb.h> #include <soc/tegra/mc.h>
@@ -277,7 +278,9 @@ static struct iommu_domain *tegra_smmu_domain_alloc(unsigned type) {struct tegra_smmu_as *as; -if (type !=IOMMU_DOMAIN_UNMANAGED ) +if (type !=IOMMU_DOMAIN_UNMANAGED && + type !=IOMMU_DOMAIN_DMA && + type !=IOMMU_DOMAIN_IDENTITY )return NULL ; as =kzalloc (sizeof (*as),GFP_KERNEL );
@@ -287,25 +290,16 @@ static struct iommu_domain *tegra_smmu_domain_alloc(unsigned type) as->attr =SMMU_PD_READABLE |SMMU_PD_WRITABLE |SMMU_PD_NONSECURE ; as->pd =alloc_page (GFP_KERNEL | __GFP_DMA | __GFP_ZERO); -if (!as->pd) { -kfree (as); -return NULL ; - } +if (!as->pd) + goto free_as; as->count =kcalloc (SMMU_NUM_PDE ,sizeof (u32),GFP_KERNEL ); -if (!as->count) { -__free_page (as->pd); -kfree (as); -return NULL ; - } +if (!as->count) + goto free_pd_range; as->pts =kcalloc (SMMU_NUM_PDE ,sizeof (*as->pts),GFP_KERNEL ); -if (!as->pts) { -kfree (as->count); -__free_page (as->pd); -kfree (as); -return NULL ; - } +if (!as->pts) + goto free_pts;spin_lock_init (&as->lock);
@@ -315,6 +309,15 @@ static struct iommu_domain *tegra_smmu_domain_alloc(unsigned type) as->domain.geometry.force_aperture = true;return &as->domain; + +free_pts: +kfree (as->pts); +free_pd_range: +__free_page (as->pd); +free_as: +kfree (as); + +return NULL ; }static void tegra_smmu_domain_free (struct iommu_domain *domain)
@@ -1012,7 +1015,7 @@ static const struct iommu_ops tegra_smmu_ops = { .probe_device = tegra_smmu_probe_device, .release_device = tegra_smmu_release_device, .device_group = tegra_smmu_device_group, - .get_resv_regions = of_iommu_get_resv_regions, + .get_resv_regions = iommu_dma_get_resv_regions, .of_xlate = tegra_smmu_of_xlate, .pgsize_bitmap = SZ_4K, .default_domain_ops = &(const struct iommu_domain_ops ) {
Signature · 1 line
2.37.2