Re: [RFC PATCH 0/3] Introduce iommu-map-masked for platform devices

From: Charan Teja Kalla

Date: Fri Oct 10 2025 - 15:54:08 EST




On 10/9/2025 11:55 PM, Dmitry Baryshkov wrote:
>>> I really don't like the idea of extending the #iommu-cells. The ARM SMMU
>>> has only one cell, which is correct even for our platforms. The fact
>>> that we need to identify different IOMMU SIDs (and handle them in a
>>> differnt ways) is internal to the video device (and several other
>>> devices). There is nothing to be handled on the ARM SMMU side.
>> Huh? So if you prefer not to change anything, are you suggesting this series
>> doesn't need to exist at all? Now I'm thoroughly confused...
> Hmm. We need changes, but I don't feel like adding the FUNCTION_ID to
> #iommu-cells is the best idea.
>
>> If you want to use SMR masks, then you absolutely need #iommu-cells = 2,
>> because that is the SMMU binding for using SMR masks. It would definitely
> I'm sorry. Yes, we have #iommu-cells = <2>.
>
>> not be OK to have some magic property trying to smuggle
>> IOMMU-driver-specific data contrary to what the IOMMU node itself says. As
>> for iommu-map, I don't see what would be objectionable about improving the
>> parsing to respect a real #iommu-cells value rather than hard-coding an
>> assumption. Yes, we'd probably need to forbid entries with length > 1
>> targeting IOMMUs with #iommu-cells > 1, since the notion of a linear
> This will break e.g. PCIe on Qualcomm platforms:
>
> iommu-map = <0x0 &apps_smmu 0x1400 0x1>,
> <0x100 &apps_smmu 0x1401 0x1>;
>
>
> But this seems unlogical anyway wrt. apps_smmu having #iommu-cells =
> <2>. It depends on ARM SMMU ignoring the second cell when it's not
> present.
>
>> relationship between the input ID and the output specifier falls apart when
>> the specifier is complex, but that seems simple enough to implement and
>> document (even if it's too fiddly to describe in the schema itself), and
>> still certainly no worse than having another property that *is* just
>> iommu-map with implicit length = 1.
>>
>> And if you want individual StreamIDs for logical functions to be attachable
>> to distinct contexts then those functions absolutely must be visible to the
>> IOMMU layer and the SMMU driver as independent devices with their own unique
>> properties, which means either they come that way from the DT as of_platform
>> devices in the first place, or you implement a full bus_type abstraction

I don't want to dilute what Dmitry is saying here, but the below is what
i can make out of Robin comments, please CMIW:

iommu {
#iommu-cells = <2>;
}

video {
iommu = <iommu sid1 mask1>, <iommu sid2 mask2>;
#iommu-map-cells = 2; /* does it look weird to define here, even if
it is SMMU property? */
iommu-map = <0 smmu sid3 mask3>,
<0 smmu sid4 mask4>;
};

video driver calls of_dma_configure_id(, id) for each of the logical
functionality, id is the index(which can be treated as SMMU identifier
for logical function). Multiple smmu identifiers for a logical function
can be represented as bitmap indices in 'id'...

The sample code below, based on #iommu-map-cells defined in video{}
node, on top of this RFC patch.
-------------------------------8888-------------------------------------
diff --git a/drivers/iommu/of_iommu.c b/drivers/iommu/of_iommu.c
index 2363de8f2fd6..ed5568278e2a 100644
--- a/drivers/iommu/of_iommu.c
+++ b/drivers/iommu/of_iommu.c
@@ -41,27 +41,24 @@ static int of_iommu_xlate(struct device *dev,
return ret;
}

-static int of_iommu_map_id(const __be32 *map, u32 id,
+static int of_iommu_map_id(const __be32 *map, u32 idx, u32 id, u32
cell_count,
struct device *dev, void *data)
{
struct device_node *phandle_node;
struct of_phandle_args *iommu_spec = data;
- u32 id_base = be32_to_cpup(map + 0);
u32 phandle = be32_to_cpup(map + 1);
- u32 master_id0 = be32_to_cpup(map + 2);
- u32 master_id1 = be32_to_cpup(map + 3);
- int err;
+ int err, i;

phandle_node = of_find_node_by_phandle(phandle);
if (!phandle_node)
return -ENODEV;

- if (id != id_base)
+ if (!test_bit(idx, (unsigned long*)&id))
return -EAGAIN;

iommu_spec->np = phandle_node;
- iommu_spec->args[0] = master_id0;
- iommu_spec->args[1] = master_id1;
+ for (i = 0; i <= cell_count; ++i)
+ iommu_spec->args[i] = be32_to_cpup(map + 2 + i);

err = of_iommu_xlate(dev, iommu_spec);
of_node_put(iommu_spec->np);
@@ -76,7 +73,7 @@ static int of_iommu_configure_map_id_and_mask(struct
device_node *master_np,
struct of_phandle_args iommu_spec = { .args_count = 2 };

return of_map_id_and_mask(master_np, *id,
- "iommu-map-masked", NULL,
+ "iommu-map", "iommu-map-mask",
&iommu_spec.np, NULL,
dev, (void *)&iommu_spec, of_iommu_map_id);
}
@@ -86,10 +83,14 @@ static int of_iommu_configure_dev_id(struct
device_node *master_np,
const u32 *id)
{
struct of_phandle_args iommu_spec = { .args_count = 1 };
+ u32 cell_count;
int err;
- bool iommu_map_masked = !!of_find_property(master_np,
"iommu-map-masked", NULL);

- if (iommu_map_masked)
+ err = of_property_read_u32(master_np, "iommu-map-cells", &cell_count);
+ if (err)
+ return err;
+
+ if (cell_count > 1)
return of_iommu_configure_map_id_and_mask(master_np, dev, id);

err = of_map_id(master_np, *id, "iommu-map",
diff --git a/drivers/of/base.c b/drivers/of/base.c
index bb11125e9624..c42d77beed52 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -2065,12 +2065,15 @@ int of_map_id_and_mask(const struct device_node
*np, u32 id,
const char *map_name, const char *map_mask_name,
struct device_node **target, u32 *id_out,
struct device *dev, void *data,
- int (*fn)(const __be32 *map, u32 id, struct device *dev, void
*data))
+ int (*fn)(const __be32 *map, u32 idx, u32 id,
+ u32 cell_count, struct device *dev, void *data))
{
+ const char *cells_prop_name __free(kfree);
u32 map_mask, masked_id;
+ u32 cell_count;
int map_len;
- int ret;
const __be32 *map = NULL;
+ int ret, list_count, idx;

if (!np || !map_name || (!target && !id_out))
return -EINVAL;
@@ -2084,7 +2087,16 @@ int of_map_id_and_mask(const struct device_node
*np, u32 id,
return 0;
}

- if (!map_len || map_len % (4 * sizeof(*map))) {
+ cells_prop_name = kasprintf(GFP_KERNEL, "#%s-cells", map_name);
+ ret = of_property_read_u32(np, cells_prop_name, &cell_count);
+ if (ret == -EINVAL) // property doesn't defined
+ cell_count = 1;
+ else
+ return ret;
+
+ /* syntax: [base iommu cell0 <cell1 ...celln> len] */
+ list_count = 2 + cell_count + 1;
+ if (!map_len || map_len % (list_count * sizeof(*map))) {
pr_err("%pOF: Error: Bad %s length: %d\n", np,
map_name, map_len);
return -EINVAL;
@@ -2101,12 +2113,12 @@ int of_map_id_and_mask(const struct device_node
*np, u32 id,
of_property_read_u32(np, map_mask_name, &map_mask);

masked_id = map_mask & id;
- for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4) {
+ for (idx = 0; map_len > 0; map_len -= list_count * sizeof(*map), map
+= list_count, idx++) {
struct device_node *phandle_node;
u32 id_base = be32_to_cpup(map + 0);
u32 phandle = be32_to_cpup(map + 1);
u32 out_base = be32_to_cpup(map + 2);
- u32 id_len = be32_to_cpup(map + 3);
+ u32 id_len = be32_to_cpup(map + cell_count - 1);

if (id_base & ~map_mask) {
pr_err("%pOF: Invalid %s translation - %s-mask (0x%x) ignores
id-base (0x%x)\n",
@@ -2115,8 +2127,14 @@ int of_map_id_and_mask(const struct device_node
*np, u32 id,
return -EFAULT;
}

- if (fn) {
- ret = fn(map, id, dev, data);
+ /* for >1 cell count, avoid the linear translation. It is
+ * expected that custom mapping managed in cb fn().
+ */
+ if (cell_count > 2) {
+ if (!fn)
+ return -EINVAL;
+ /* list of required indices is mentioned in id */
+ ret = fn(map, idx, id, cell_count, dev, data);
if (ret != -EAGAIN)
break;
continue;
diff --git a/include/linux/of.h b/include/linux/of.h
index 7f3890ab26d5..b7f38a43ffc7 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -464,7 +464,8 @@ int of_map_id_and_mask(const struct device_node *np,
u32 id,
const char *map_name, const char *map_mask_name,
struct device_node **target, u32 *id_out,
struct device *dev, void *data,
- int (*fn)(const __be32 *map, u32 id, struct device *dev, void *data));
+ int (*fn)(const __be32 *map, u32 id, u32 idx, u32 cell_count,
+ struct device *dev, void *data));

phys_addr_t of_dma_get_max_cpu_address(struct device_node *np);

------------------------------------------------------------------------

Thanks,
Charan