Skip to content

Commit 5fd577c

Browse files
JeffyCNjoergroedel
authored andcommitted
iommu/rockchip: Use OF_IOMMU to attach devices automatically
Converts the rockchip-iommu driver to use the OF_IOMMU infrastructure, which allows attaching master devices to their IOMMUs automatically according to DT properties. Signed-off-by: Jeffy Chen <[email protected]> Reviewed-by: Robin Murphy <[email protected]> Signed-off-by: Joerg Roedel <[email protected]>
1 parent 9176a30 commit 5fd577c

File tree

1 file changed

+40
-95
lines changed

1 file changed

+40
-95
lines changed

drivers/iommu/rockchip-iommu.c

Lines changed: 40 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/mm.h>
2020
#include <linux/module.h>
2121
#include <linux/of.h>
22+
#include <linux/of_iommu.h>
2223
#include <linux/of_platform.h>
2324
#include <linux/platform_device.h>
2425
#include <linux/slab.h>
@@ -104,6 +105,10 @@ struct rk_iommu {
104105
struct iommu_domain *domain; /* domain to which iommu is attached */
105106
};
106107

108+
struct rk_iommudata {
109+
struct rk_iommu *iommu;
110+
};
111+
107112
static struct device *dma_dev;
108113

109114
static inline void rk_table_flush(struct rk_iommu_domain *dom, dma_addr_t dma,
@@ -807,18 +812,9 @@ static size_t rk_iommu_unmap(struct iommu_domain *domain, unsigned long _iova,
807812

808813
static struct rk_iommu *rk_iommu_from_dev(struct device *dev)
809814
{
810-
struct iommu_group *group;
811-
struct device *iommu_dev;
812-
struct rk_iommu *rk_iommu;
815+
struct rk_iommudata *data = dev->archdata.iommu;
813816

814-
group = iommu_group_get(dev);
815-
if (!group)
816-
return NULL;
817-
iommu_dev = iommu_group_get_iommudata(group);
818-
rk_iommu = dev_get_drvdata(iommu_dev);
819-
iommu_group_put(group);
820-
821-
return rk_iommu;
817+
return data ? data->iommu : NULL;
822818
}
823819

824820
static int rk_iommu_attach_device(struct iommu_domain *domain,
@@ -989,110 +985,53 @@ static void rk_iommu_domain_free(struct iommu_domain *domain)
989985
iommu_put_dma_cookie(&rk_domain->domain);
990986
}
991987

992-
static bool rk_iommu_is_dev_iommu_master(struct device *dev)
993-
{
994-
struct device_node *np = dev->of_node;
995-
int ret;
996-
997-
/*
998-
* An iommu master has an iommus property containing a list of phandles
999-
* to iommu nodes, each with an #iommu-cells property with value 0.
1000-
*/
1001-
ret = of_count_phandle_with_args(np, "iommus", "#iommu-cells");
1002-
return (ret > 0);
1003-
}
1004-
1005-
static int rk_iommu_group_set_iommudata(struct iommu_group *group,
1006-
struct device *dev)
988+
static int rk_iommu_add_device(struct device *dev)
1007989
{
1008-
struct device_node *np = dev->of_node;
1009-
struct platform_device *pd;
1010-
int ret;
1011-
struct of_phandle_args args;
990+
struct iommu_group *group;
991+
struct rk_iommu *iommu;
1012992

1013-
/*
1014-
* An iommu master has an iommus property containing a list of phandles
1015-
* to iommu nodes, each with an #iommu-cells property with value 0.
1016-
*/
1017-
ret = of_parse_phandle_with_args(np, "iommus", "#iommu-cells", 0,
1018-
&args);
1019-
if (ret) {
1020-
dev_err(dev, "of_parse_phandle_with_args(%pOF) => %d\n",
1021-
np, ret);
1022-
return ret;
1023-
}
1024-
if (args.args_count != 0) {
1025-
dev_err(dev, "incorrect number of iommu params found for %pOF (found %d, expected 0)\n",
1026-
args.np, args.args_count);
1027-
return -EINVAL;
1028-
}
993+
iommu = rk_iommu_from_dev(dev);
994+
if (!iommu)
995+
return -ENODEV;
1029996

1030-
pd = of_find_device_by_node(args.np);
1031-
of_node_put(args.np);
1032-
if (!pd) {
1033-
dev_err(dev, "iommu %pOF not found\n", args.np);
1034-
return -EPROBE_DEFER;
1035-
}
997+
group = iommu_group_get_for_dev(dev);
998+
if (IS_ERR(group))
999+
return PTR_ERR(group);
1000+
iommu_group_put(group);
10361001

1037-
/* TODO(djkurtz): handle multiple slave iommus for a single master */
1038-
iommu_group_set_iommudata(group, &pd->dev, NULL);
1002+
iommu_device_link(&iommu->iommu, dev);
10391003

10401004
return 0;
10411005
}
10421006

1043-
static int rk_iommu_add_device(struct device *dev)
1007+
static void rk_iommu_remove_device(struct device *dev)
10441008
{
1045-
struct iommu_group *group;
10461009
struct rk_iommu *iommu;
1047-
int ret;
1048-
1049-
if (!rk_iommu_is_dev_iommu_master(dev))
1050-
return -ENODEV;
1051-
1052-
group = iommu_group_get(dev);
1053-
if (!group) {
1054-
group = iommu_group_alloc();
1055-
if (IS_ERR(group)) {
1056-
dev_err(dev, "Failed to allocate IOMMU group\n");
1057-
return PTR_ERR(group);
1058-
}
1059-
}
1060-
1061-
ret = iommu_group_add_device(group, dev);
1062-
if (ret)
1063-
goto err_put_group;
1064-
1065-
ret = rk_iommu_group_set_iommudata(group, dev);
1066-
if (ret)
1067-
goto err_remove_device;
10681010

10691011
iommu = rk_iommu_from_dev(dev);
1070-
if (iommu)
1071-
iommu_device_link(&iommu->iommu, dev);
10721012

1073-
iommu_group_put(group);
1074-
1075-
return 0;
1076-
1077-
err_remove_device:
1013+
iommu_device_unlink(&iommu->iommu, dev);
10781014
iommu_group_remove_device(dev);
1079-
err_put_group:
1080-
iommu_group_put(group);
1081-
return ret;
10821015
}
10831016

1084-
static void rk_iommu_remove_device(struct device *dev)
1017+
static int rk_iommu_of_xlate(struct device *dev,
1018+
struct of_phandle_args *args)
10851019
{
1086-
struct rk_iommu *iommu;
1020+
struct platform_device *iommu_dev;
1021+
struct rk_iommudata *data;
10871022

1088-
if (!rk_iommu_is_dev_iommu_master(dev))
1089-
return;
1023+
data = devm_kzalloc(dma_dev, sizeof(*data), GFP_KERNEL);
1024+
if (!data)
1025+
return -ENOMEM;
10901026

1091-
iommu = rk_iommu_from_dev(dev);
1092-
if (iommu)
1093-
iommu_device_unlink(&iommu->iommu, dev);
1027+
iommu_dev = of_find_device_by_node(args->np);
10941028

1095-
iommu_group_remove_device(dev);
1029+
data->iommu = platform_get_drvdata(iommu_dev);
1030+
dev->archdata.iommu = data;
1031+
1032+
of_dev_put(iommu_dev);
1033+
1034+
return 0;
10961035
}
10971036

10981037
static const struct iommu_ops rk_iommu_ops = {
@@ -1106,7 +1045,9 @@ static const struct iommu_ops rk_iommu_ops = {
11061045
.add_device = rk_iommu_add_device,
11071046
.remove_device = rk_iommu_remove_device,
11081047
.iova_to_phys = rk_iommu_iova_to_phys,
1048+
.device_group = generic_device_group,
11091049
.pgsize_bitmap = RK_IOMMU_PGSIZE_BITMAP,
1050+
.of_xlate = rk_iommu_of_xlate,
11101051
};
11111052

11121053
static int rk_iommu_probe(struct platform_device *pdev)
@@ -1178,6 +1119,8 @@ static int rk_iommu_probe(struct platform_device *pdev)
11781119
goto err_unprepare_clocks;
11791120

11801121
iommu_device_set_ops(&iommu->iommu, &rk_iommu_ops);
1122+
iommu_device_set_fwnode(&iommu->iommu, &dev->of_node->fwnode);
1123+
11811124
err = iommu_device_register(&iommu->iommu);
11821125
if (err)
11831126
goto err_remove_sysfs;
@@ -1250,6 +1193,8 @@ static int __init rk_iommu_init(void)
12501193
}
12511194
subsys_initcall(rk_iommu_init);
12521195

1196+
IOMMU_OF_DECLARE(rk_iommu_of, "rockchip,iommu");
1197+
12531198
MODULE_DESCRIPTION("IOMMU API for Rockchip");
12541199
MODULE_AUTHOR("Simon Xue <[email protected]> and Daniel Kurtz <[email protected]>");
12551200
MODULE_ALIAS("platform:rockchip-iommu");

0 commit comments

Comments
 (0)