Skip to content

Commit 2a6db71

Browse files
nipungupta87joergroedel
authored andcommitted
iommu/of: make of_pci_map_rid() available for other devices too
iommu-map property is also used by devices with fsl-mc. This patch moves the of_pci_map_rid to generic location, so that it can be used by other busses too. 'of_pci_map_rid' is renamed here to 'of_map_rid' and there is no functional change done in the API. Signed-off-by: Nipun Gupta <nipun.gupta@nxp.com> Reviewed-by: Rob Herring <robh@kernel.org> Reviewed-by: Robin Murphy <robin.murphy@arm.com> Acked-by: Bjorn Helgaas <bhelgaas@google.com> Signed-off-by: Joerg Roedel <jroedel@suse.de>
1 parent c9d8d66 commit 2a6db71

6 files changed

Lines changed: 117 additions & 117 deletions

File tree

‎drivers/iommu/of_iommu.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,8 @@ static int of_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
132132
struct of_phandle_args iommu_spec = { .args_count = 1 };
133133
int err;
134134

135-
err = of_pci_map_rid(info->np, alias, "iommu-map",
136-
"iommu-map-mask", &iommu_spec.np,
137-
iommu_spec.args);
135+
err = of_map_rid(info->np, alias, "iommu-map", "iommu-map-mask",
136+
&iommu_spec.np, iommu_spec.args);
138137
if (err)
139138
return err == -ENODEV ? NO_IOMMU : err;
140139

‎drivers/of/base.c‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,3 +2045,105 @@ int of_find_last_cache_level(unsigned int cpu)
20452045

20462046
return cache_level;
20472047
}
2048+
2049+
/**
2050+
* of_map_rid - Translate a requester ID through a downstream mapping.
2051+
* @np: root complex device node.
2052+
* @rid: device requester ID to map.
2053+
* @map_name: property name of the map to use.
2054+
* @map_mask_name: optional property name of the mask to use.
2055+
* @target: optional pointer to a target device node.
2056+
* @id_out: optional pointer to receive the translated ID.
2057+
*
2058+
* Given a device requester ID, look up the appropriate implementation-defined
2059+
* platform ID and/or the target device which receives transactions on that
2060+
* ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or
2061+
* @id_out may be NULL if only the other is required. If @target points to
2062+
* a non-NULL device node pointer, only entries targeting that node will be
2063+
* matched; if it points to a NULL value, it will receive the device node of
2064+
* the first matching target phandle, with a reference held.
2065+
*
2066+
* Return: 0 on success or a standard error code on failure.
2067+
*/
2068+
int of_map_rid(struct device_node *np, u32 rid,
2069+
const char *map_name, const char *map_mask_name,
2070+
struct device_node **target, u32 *id_out)
2071+
{
2072+
u32 map_mask, masked_rid;
2073+
int map_len;
2074+
const __be32 *map = NULL;
2075+
2076+
if (!np || !map_name || (!target && !id_out))
2077+
return -EINVAL;
2078+
2079+
map = of_get_property(np, map_name, &map_len);
2080+
if (!map) {
2081+
if (target)
2082+
return -ENODEV;
2083+
/* Otherwise, no map implies no translation */
2084+
*id_out = rid;
2085+
return 0;
2086+
}
2087+
2088+
if (!map_len || map_len % (4 * sizeof(*map))) {
2089+
pr_err("%pOF: Error: Bad %s length: %d\n", np,
2090+
map_name, map_len);
2091+
return -EINVAL;
2092+
}
2093+
2094+
/* The default is to select all bits. */
2095+
map_mask = 0xffffffff;
2096+
2097+
/*
2098+
* Can be overridden by "{iommu,msi}-map-mask" property.
2099+
* If of_property_read_u32() fails, the default is used.
2100+
*/
2101+
if (map_mask_name)
2102+
of_property_read_u32(np, map_mask_name, &map_mask);
2103+
2104+
masked_rid = map_mask & rid;
2105+
for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4) {
2106+
struct device_node *phandle_node;
2107+
u32 rid_base = be32_to_cpup(map + 0);
2108+
u32 phandle = be32_to_cpup(map + 1);
2109+
u32 out_base = be32_to_cpup(map + 2);
2110+
u32 rid_len = be32_to_cpup(map + 3);
2111+
2112+
if (rid_base & ~map_mask) {
2113+
pr_err("%pOF: Invalid %s translation - %s-mask (0x%x) ignores rid-base (0x%x)\n",
2114+
np, map_name, map_name,
2115+
map_mask, rid_base);
2116+
return -EFAULT;
2117+
}
2118+
2119+
if (masked_rid < rid_base || masked_rid >= rid_base + rid_len)
2120+
continue;
2121+
2122+
phandle_node = of_find_node_by_phandle(phandle);
2123+
if (!phandle_node)
2124+
return -ENODEV;
2125+
2126+
if (target) {
2127+
if (*target)
2128+
of_node_put(phandle_node);
2129+
else
2130+
*target = phandle_node;
2131+
2132+
if (*target != phandle_node)
2133+
continue;
2134+
}
2135+
2136+
if (id_out)
2137+
*id_out = masked_rid - rid_base + out_base;
2138+
2139+
pr_debug("%pOF: %s, using mask %08x, rid-base: %08x, out-base: %08x, length: %08x, rid: %08x -> %08x\n",
2140+
np, map_name, map_mask, rid_base, out_base,
2141+
rid_len, rid, masked_rid - rid_base + out_base);
2142+
return 0;
2143+
}
2144+
2145+
pr_err("%pOF: Invalid %s translation - no match for rid 0x%x on %pOF\n",
2146+
np, map_name, rid, target && *target ? *target : NULL);
2147+
return -EFAULT;
2148+
}
2149+
EXPORT_SYMBOL_GPL(of_map_rid);

‎drivers/of/irq.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <linux/module.h>
2323
#include <linux/of.h>
2424
#include <linux/of_irq.h>
25-
#include <linux/of_pci.h>
2625
#include <linux/string.h>
2726
#include <linux/slab.h>
2827

@@ -588,8 +587,8 @@ static u32 __of_msi_map_rid(struct device *dev, struct device_node **np,
588587
* "msi-map" property.
589588
*/
590589
for (parent_dev = dev; parent_dev; parent_dev = parent_dev->parent)
591-
if (!of_pci_map_rid(parent_dev->of_node, rid_in, "msi-map",
592-
"msi-map-mask", np, &rid_out))
590+
if (!of_map_rid(parent_dev->of_node, rid_in, "msi-map",
591+
"msi-map-mask", np, &rid_out))
593592
break;
594593
return rid_out;
595594
}

‎drivers/pci/of.c‎

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -355,107 +355,6 @@ int devm_of_pci_get_host_bridge_resources(struct device *dev,
355355
EXPORT_SYMBOL_GPL(devm_of_pci_get_host_bridge_resources);
356356
#endif /* CONFIG_OF_ADDRESS */
357357

358-
/**
359-
* of_pci_map_rid - Translate a requester ID through a downstream mapping.
360-
* @np: root complex device node.
361-
* @rid: PCI requester ID to map.
362-
* @map_name: property name of the map to use.
363-
* @map_mask_name: optional property name of the mask to use.
364-
* @target: optional pointer to a target device node.
365-
* @id_out: optional pointer to receive the translated ID.
366-
*
367-
* Given a PCI requester ID, look up the appropriate implementation-defined
368-
* platform ID and/or the target device which receives transactions on that
369-
* ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or
370-
* @id_out may be NULL if only the other is required. If @target points to
371-
* a non-NULL device node pointer, only entries targeting that node will be
372-
* matched; if it points to a NULL value, it will receive the device node of
373-
* the first matching target phandle, with a reference held.
374-
*
375-
* Return: 0 on success or a standard error code on failure.
376-
*/
377-
int of_pci_map_rid(struct device_node *np, u32 rid,
378-
const char *map_name, const char *map_mask_name,
379-
struct device_node **target, u32 *id_out)
380-
{
381-
u32 map_mask, masked_rid;
382-
int map_len;
383-
const __be32 *map = NULL;
384-
385-
if (!np || !map_name || (!target && !id_out))
386-
return -EINVAL;
387-
388-
map = of_get_property(np, map_name, &map_len);
389-
if (!map) {
390-
if (target)
391-
return -ENODEV;
392-
/* Otherwise, no map implies no translation */
393-
*id_out = rid;
394-
return 0;
395-
}
396-
397-
if (!map_len || map_len % (4 * sizeof(*map))) {
398-
pr_err("%pOF: Error: Bad %s length: %d\n", np,
399-
map_name, map_len);
400-
return -EINVAL;
401-
}
402-
403-
/* The default is to select all bits. */
404-
map_mask = 0xffffffff;
405-
406-
/*
407-
* Can be overridden by "{iommu,msi}-map-mask" property.
408-
* If of_property_read_u32() fails, the default is used.
409-
*/
410-
if (map_mask_name)
411-
of_property_read_u32(np, map_mask_name, &map_mask);
412-
413-
masked_rid = map_mask & rid;
414-
for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4) {
415-
struct device_node *phandle_node;
416-
u32 rid_base = be32_to_cpup(map + 0);
417-
u32 phandle = be32_to_cpup(map + 1);
418-
u32 out_base = be32_to_cpup(map + 2);
419-
u32 rid_len = be32_to_cpup(map + 3);
420-
421-
if (rid_base & ~map_mask) {
422-
pr_err("%pOF: Invalid %s translation - %s-mask (0x%x) ignores rid-base (0x%x)\n",
423-
np, map_name, map_name,
424-
map_mask, rid_base);
425-
return -EFAULT;
426-
}
427-
428-
if (masked_rid < rid_base || masked_rid >= rid_base + rid_len)
429-
continue;
430-
431-
phandle_node = of_find_node_by_phandle(phandle);
432-
if (!phandle_node)
433-
return -ENODEV;
434-
435-
if (target) {
436-
if (*target)
437-
of_node_put(phandle_node);
438-
else
439-
*target = phandle_node;
440-
441-
if (*target != phandle_node)
442-
continue;
443-
}
444-
445-
if (id_out)
446-
*id_out = masked_rid - rid_base + out_base;
447-
448-
pr_debug("%pOF: %s, using mask %08x, rid-base: %08x, out-base: %08x, length: %08x, rid: %08x -> %08x\n",
449-
np, map_name, map_mask, rid_base, out_base,
450-
rid_len, rid, masked_rid - rid_base + out_base);
451-
return 0;
452-
}
453-
454-
pr_err("%pOF: Invalid %s translation - no match for rid 0x%x on %pOF\n",
455-
np, map_name, rid, target && *target ? *target : NULL);
456-
return -EFAULT;
457-
}
458-
459358
#if IS_ENABLED(CONFIG_OF_IRQ)
460359
/**
461360
* of_irq_parse_pci - Resolve the interrupt for a PCI device

‎include/linux/of.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,10 @@ bool of_console_check(struct device_node *dn, char *name, int index);
550550

551551
extern int of_cpu_node_to_id(struct device_node *np);
552552

553+
int of_map_rid(struct device_node *np, u32 rid,
554+
const char *map_name, const char *map_mask_name,
555+
struct device_node **target, u32 *id_out);
556+
553557
#else /* CONFIG_OF */
554558

555559
static inline void of_core_init(void)
@@ -952,6 +956,13 @@ static inline int of_cpu_node_to_id(struct device_node *np)
952956
return -ENODEV;
953957
}
954958

959+
static inline int of_map_rid(struct device_node *np, u32 rid,
960+
const char *map_name, const char *map_mask_name,
961+
struct device_node **target, u32 *id_out)
962+
{
963+
return -EINVAL;
964+
}
965+
955966
#define of_match_ptr(_ptr) NULL
956967
#define of_match_node(_matches, _node) NULL
957968
#endif /* CONFIG_OF */

‎include/linux/of_pci.h‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ struct device_node *of_pci_find_child_device(struct device_node *parent,
1414
unsigned int devfn);
1515
int of_pci_get_devfn(struct device_node *np);
1616
void of_pci_check_probe_only(void);
17-
int of_pci_map_rid(struct device_node *np, u32 rid,
18-
const char *map_name, const char *map_mask_name,
19-
struct device_node **target, u32 *id_out);
2017
#else
2118
static inline struct device_node *of_pci_find_child_device(struct device_node *parent,
2219
unsigned int devfn)
@@ -29,13 +26,6 @@ static inline int of_pci_get_devfn(struct device_node *np)
2926
return -EINVAL;
3027
}
3128

32-
static inline int of_pci_map_rid(struct device_node *np, u32 rid,
33-
const char *map_name, const char *map_mask_name,
34-
struct device_node **target, u32 *id_out)
35-
{
36-
return -EINVAL;
37-
}
38-
3929
static inline void of_pci_check_probe_only(void) { }
4030
#endif
4131

0 commit comments

Comments
 (0)