Skip to content

Commit 9176a30

Browse files
JeffyCNjoergroedel
authored andcommitted
iommu/rockchip: Use IOMMU device for dma mapping operations
Use the first registered IOMMU device for dma mapping operations, and drop the domain platform device. This is similar to exynos iommu driver. Signed-off-by: Jeffy Chen <[email protected]> Reviewed-by: Tomasz Figa <[email protected]> Reviewed-by: Robin Murphy <[email protected]> Signed-off-by: Joerg Roedel <[email protected]>
1 parent 8fa9eb3 commit 9176a30

File tree

1 file changed

+24
-61
lines changed

1 file changed

+24
-61
lines changed

drivers/iommu/rockchip-iommu.c

Lines changed: 24 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979

8080
struct rk_iommu_domain {
8181
struct list_head iommus;
82-
struct platform_device *pdev;
8382
u32 *dt; /* page directory table */
8483
dma_addr_t dt_dma;
8584
spinlock_t iommus_lock; /* lock for iommus list */
@@ -105,12 +104,14 @@ struct rk_iommu {
105104
struct iommu_domain *domain; /* domain to which iommu is attached */
106105
};
107106

107+
static struct device *dma_dev;
108+
108109
static inline void rk_table_flush(struct rk_iommu_domain *dom, dma_addr_t dma,
109110
unsigned int count)
110111
{
111112
size_t size = count * sizeof(u32); /* count of u32 entry */
112113

113-
dma_sync_single_for_device(&dom->pdev->dev, dma, size, DMA_TO_DEVICE);
114+
dma_sync_single_for_device(dma_dev, dma, size, DMA_TO_DEVICE);
114115
}
115116

116117
static struct rk_iommu_domain *to_rk_domain(struct iommu_domain *dom)
@@ -625,7 +626,6 @@ static void rk_iommu_zap_iova_first_last(struct rk_iommu_domain *rk_domain,
625626
static u32 *rk_dte_get_page_table(struct rk_iommu_domain *rk_domain,
626627
dma_addr_t iova)
627628
{
628-
struct device *dev = &rk_domain->pdev->dev;
629629
u32 *page_table, *dte_addr;
630630
u32 dte_index, dte;
631631
phys_addr_t pt_phys;
@@ -643,9 +643,9 @@ static u32 *rk_dte_get_page_table(struct rk_iommu_domain *rk_domain,
643643
if (!page_table)
644644
return ERR_PTR(-ENOMEM);
645645

646-
pt_dma = dma_map_single(dev, page_table, SPAGE_SIZE, DMA_TO_DEVICE);
647-
if (dma_mapping_error(dev, pt_dma)) {
648-
dev_err(dev, "DMA mapping error while allocating page table\n");
646+
pt_dma = dma_map_single(dma_dev, page_table, SPAGE_SIZE, DMA_TO_DEVICE);
647+
if (dma_mapping_error(dma_dev, pt_dma)) {
648+
dev_err(dma_dev, "DMA mapping error while allocating page table\n");
649649
free_page((unsigned long)page_table);
650650
return ERR_PTR(-ENOMEM);
651651
}
@@ -911,29 +911,20 @@ static void rk_iommu_detach_device(struct iommu_domain *domain,
911911
static struct iommu_domain *rk_iommu_domain_alloc(unsigned type)
912912
{
913913
struct rk_iommu_domain *rk_domain;
914-
struct platform_device *pdev;
915-
struct device *iommu_dev;
916914

917915
if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
918916
return NULL;
919917

920-
/* Register a pdev per domain, so DMA API can base on this *dev
921-
* even some virtual master doesn't have an iommu slave
922-
*/
923-
pdev = platform_device_register_simple("rk_iommu_domain",
924-
PLATFORM_DEVID_AUTO, NULL, 0);
925-
if (IS_ERR(pdev))
918+
if (!dma_dev)
926919
return NULL;
927920

928-
rk_domain = devm_kzalloc(&pdev->dev, sizeof(*rk_domain), GFP_KERNEL);
921+
rk_domain = devm_kzalloc(dma_dev, sizeof(*rk_domain), GFP_KERNEL);
929922
if (!rk_domain)
930-
goto err_unreg_pdev;
931-
932-
rk_domain->pdev = pdev;
923+
return NULL;
933924

934925
if (type == IOMMU_DOMAIN_DMA &&
935926
iommu_get_dma_cookie(&rk_domain->domain))
936-
goto err_unreg_pdev;
927+
return NULL;
937928

938929
/*
939930
* rk32xx iommus use a 2 level pagetable.
@@ -944,11 +935,10 @@ static struct iommu_domain *rk_iommu_domain_alloc(unsigned type)
944935
if (!rk_domain->dt)
945936
goto err_put_cookie;
946937

947-
iommu_dev = &pdev->dev;
948-
rk_domain->dt_dma = dma_map_single(iommu_dev, rk_domain->dt,
938+
rk_domain->dt_dma = dma_map_single(dma_dev, rk_domain->dt,
949939
SPAGE_SIZE, DMA_TO_DEVICE);
950-
if (dma_mapping_error(iommu_dev, rk_domain->dt_dma)) {
951-
dev_err(iommu_dev, "DMA map error for DT\n");
940+
if (dma_mapping_error(dma_dev, rk_domain->dt_dma)) {
941+
dev_err(dma_dev, "DMA map error for DT\n");
952942
goto err_free_dt;
953943
}
954944

@@ -969,8 +959,6 @@ static struct iommu_domain *rk_iommu_domain_alloc(unsigned type)
969959
err_put_cookie:
970960
if (type == IOMMU_DOMAIN_DMA)
971961
iommu_put_dma_cookie(&rk_domain->domain);
972-
err_unreg_pdev:
973-
platform_device_unregister(pdev);
974962

975963
return NULL;
976964
}
@@ -987,20 +975,18 @@ static void rk_iommu_domain_free(struct iommu_domain *domain)
987975
if (rk_dte_is_pt_valid(dte)) {
988976
phys_addr_t pt_phys = rk_dte_pt_address(dte);
989977
u32 *page_table = phys_to_virt(pt_phys);
990-
dma_unmap_single(&rk_domain->pdev->dev, pt_phys,
978+
dma_unmap_single(dma_dev, pt_phys,
991979
SPAGE_SIZE, DMA_TO_DEVICE);
992980
free_page((unsigned long)page_table);
993981
}
994982
}
995983

996-
dma_unmap_single(&rk_domain->pdev->dev, rk_domain->dt_dma,
984+
dma_unmap_single(dma_dev, rk_domain->dt_dma,
997985
SPAGE_SIZE, DMA_TO_DEVICE);
998986
free_page((unsigned long)rk_domain->dt);
999987

1000988
if (domain->type == IOMMU_DOMAIN_DMA)
1001989
iommu_put_dma_cookie(&rk_domain->domain);
1002-
1003-
platform_device_unregister(rk_domain->pdev);
1004990
}
1005991

1006992
static bool rk_iommu_is_dev_iommu_master(struct device *dev)
@@ -1123,30 +1109,6 @@ static const struct iommu_ops rk_iommu_ops = {
11231109
.pgsize_bitmap = RK_IOMMU_PGSIZE_BITMAP,
11241110
};
11251111

1126-
static int rk_iommu_domain_probe(struct platform_device *pdev)
1127-
{
1128-
struct device *dev = &pdev->dev;
1129-
1130-
dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms), GFP_KERNEL);
1131-
if (!dev->dma_parms)
1132-
return -ENOMEM;
1133-
1134-
/* Set dma_ops for dev, otherwise it would be dummy_dma_ops */
1135-
arch_setup_dma_ops(dev, 0, DMA_BIT_MASK(32), NULL, false);
1136-
1137-
dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
1138-
dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
1139-
1140-
return 0;
1141-
}
1142-
1143-
static struct platform_driver rk_iommu_domain_driver = {
1144-
.probe = rk_iommu_domain_probe,
1145-
.driver = {
1146-
.name = "rk_iommu_domain",
1147-
},
1148-
};
1149-
11501112
static int rk_iommu_probe(struct platform_device *pdev)
11511113
{
11521114
struct device *dev = &pdev->dev;
@@ -1220,6 +1182,14 @@ static int rk_iommu_probe(struct platform_device *pdev)
12201182
if (err)
12211183
goto err_remove_sysfs;
12221184

1185+
/*
1186+
* Use the first registered IOMMU device for domain to use with DMA
1187+
* API, since a domain might not physically correspond to a single
1188+
* IOMMU device..
1189+
*/
1190+
if (!dma_dev)
1191+
dma_dev = &pdev->dev;
1192+
12231193
return 0;
12241194
err_remove_sysfs:
12251195
iommu_device_sysfs_remove(&iommu->iommu);
@@ -1276,14 +1246,7 @@ static int __init rk_iommu_init(void)
12761246
if (ret)
12771247
return ret;
12781248

1279-
ret = platform_driver_register(&rk_iommu_domain_driver);
1280-
if (ret)
1281-
return ret;
1282-
1283-
ret = platform_driver_register(&rk_iommu_driver);
1284-
if (ret)
1285-
platform_driver_unregister(&rk_iommu_domain_driver);
1286-
return ret;
1249+
return platform_driver_register(&rk_iommu_driver);
12871250
}
12881251
subsys_initcall(rk_iommu_init);
12891252

0 commit comments

Comments
 (0)