2828#include <linux/of_platform.h>
2929#include <linux/pci.h>
3030#include <linux/platform_device.h>
31+ #include <linux/pm_runtime.h>
3132#include <linux/slab.h>
3233#include <linux/swab.h>
3334#include <linux/types.h>
@@ -576,11 +577,13 @@ static void apple_dart_domain_flush_tlb(struct apple_dart_domain *domain)
576577 for (j = 0 ; j < BITS_TO_LONGS (stream_map .dart -> num_streams ); j ++ )
577578 stream_map .sidmap [j ] = atomic_long_read (& domain_stream_map -> sidmap [j ]);
578579
580+ WARN_ON (pm_runtime_get_sync (stream_map .dart -> dev ) < 0 );
579581
580582 if (stream_map .dart -> locked )
581583 apple_dart_hw_sync_locked (& stream_map );
582584
583585 stream_map .dart -> hw -> invalidate_tlb (& stream_map );
586+ pm_runtime_put (stream_map .dart -> dev );
584587 }
585588}
586589
@@ -818,16 +821,19 @@ static int apple_dart_attach_dev(struct iommu_domain *domain,
818821 domain -> type != IOMMU_DOMAIN_UNMANAGED )
819822 return - EINVAL ;
820823
824+ for_each_stream_map (i , cfg , stream_map )
825+ WARN_ON (pm_runtime_get_sync (stream_map -> dart -> dev ) < 0 );
826+
821827 ret = apple_dart_finalize_domain (domain , dev , cfg );
822828 if (ret )
823- return ret ;
829+ goto err ;
824830
825831 switch (domain -> type ) {
826832 case IOMMU_DOMAIN_DMA :
827833 case IOMMU_DOMAIN_UNMANAGED :
828834 ret = apple_dart_domain_add_streams (dart_domain , cfg );
829835 if (ret )
830- return ret ;
836+ goto err ;
831837
832838 for_each_stream_map (i , cfg , stream_map )
833839 apple_dart_setup_translation (dart_domain , stream_map );
@@ -842,6 +848,9 @@ static int apple_dart_attach_dev(struct iommu_domain *domain,
842848 break ;
843849 }
844850
851+ err :
852+ for_each_stream_map (i , cfg , stream_map )
853+ pm_runtime_put (stream_map -> dart -> dev );
845854 return ret ;
846855}
847856
@@ -1253,6 +1262,14 @@ static int apple_dart_probe(struct platform_device *pdev)
12531262 if (ret )
12541263 return ret ;
12551264
1265+ pm_runtime_get_noresume (dev );
1266+ pm_runtime_set_active (dev );
1267+ pm_runtime_irq_safe (dev );
1268+
1269+ ret = devm_pm_runtime_enable (dev );
1270+ if (ret )
1271+ goto err_clk_disable ;
1272+
12561273 dart_params [0 ] = readl (dart -> regs + DART_PARAMS1 );
12571274 dart_params [1 ] = readl (dart -> regs + DART_PARAMS2 );
12581275 dart -> pgsize = 1 << FIELD_GET (DART_PARAMS1_PAGE_SHIFT , dart_params [0 ]);
@@ -1307,6 +1324,8 @@ static int apple_dart_probe(struct platform_device *pdev)
13071324 if (ret )
13081325 goto err_sysfs_remove ;
13091326
1327+ pm_runtime_put (dev );
1328+
13101329 dev_info (
13111330 & pdev -> dev ,
13121331 "DART [pagesize %x, %d streams, bypass support: %d, bypass forced: %d, locked: %d] initialized\n" ,
@@ -1318,6 +1337,7 @@ static int apple_dart_probe(struct platform_device *pdev)
13181337err_free_irq :
13191338 free_irq (dart -> irq , dart );
13201339err_clk_disable :
1340+ pm_runtime_put (dev );
13211341 clk_bulk_disable_unprepare (dart -> num_clks , dart -> clks );
13221342
13231343 return ret ;
@@ -1455,7 +1475,7 @@ static __maybe_unused int apple_dart_resume(struct device *dev)
14551475 return 0 ;
14561476}
14571477
1458- DEFINE_SIMPLE_DEV_PM_OPS (apple_dart_pm_ops , apple_dart_suspend , apple_dart_resume );
1478+ DEFINE_RUNTIME_DEV_PM_OPS (apple_dart_pm_ops , apple_dart_suspend , apple_dart_resume , NULL );
14591479
14601480static const struct of_device_id apple_dart_of_match [] = {
14611481 { .compatible = "apple,t8103-dart" , .data = & apple_dart_hw_t8103 },
@@ -1470,7 +1490,7 @@ static struct platform_driver apple_dart_driver = {
14701490 .name = "apple-dart" ,
14711491 .of_match_table = apple_dart_of_match ,
14721492 .suppress_bind_attrs = true,
1473- .pm = pm_sleep_ptr (& apple_dart_pm_ops ),
1493+ .pm = pm_ptr (& apple_dart_pm_ops ),
14741494 },
14751495 .probe = apple_dart_probe ,
14761496 .remove_new = apple_dart_remove ,
0 commit comments