11// SPDX-License-Identifier: GPL-2.0
22/* Copyright 2019 Linaro, Ltd, Rob Herring <robh@kernel.org> */
3+
4+ #include <drm/panfrost_drm.h>
5+
36#include <linux/atomic.h>
47#include <linux/bitfield.h>
58#include <linux/delay.h>
@@ -337,7 +340,7 @@ static void mmu_tlb_inv_context_s1(void *cookie)
337340
338341static void mmu_tlb_sync_context (void * cookie )
339342{
340- //struct panfrost_device *pfdev = cookie;
343+ //struct panfrost_mmu *mmu = cookie;
341344 // TODO: Wait 1000 GPU cycles for HW_ISSUE_6367/T60X
342345}
343346
@@ -352,57 +355,10 @@ static const struct iommu_flush_ops mmu_tlb_ops = {
352355 .tlb_flush_walk = mmu_tlb_flush_walk ,
353356};
354357
355- int panfrost_mmu_pgtable_alloc (struct panfrost_file_priv * priv )
356- {
357- struct panfrost_mmu * mmu = & priv -> mmu ;
358- struct panfrost_device * pfdev = priv -> pfdev ;
359-
360- INIT_LIST_HEAD (& mmu -> list );
361- mmu -> as = -1 ;
362-
363- mmu -> pgtbl_cfg = (struct io_pgtable_cfg ) {
364- .pgsize_bitmap = SZ_4K | SZ_2M ,
365- .ias = FIELD_GET (0xff , pfdev -> features .mmu_features ),
366- .oas = FIELD_GET (0xff00 , pfdev -> features .mmu_features ),
367- .coherent_walk = pfdev -> coherent ,
368- .tlb = & mmu_tlb_ops ,
369- .iommu_dev = pfdev -> dev ,
370- };
371-
372- mmu -> pgtbl_ops = alloc_io_pgtable_ops (ARM_MALI_LPAE , & mmu -> pgtbl_cfg ,
373- priv );
374- if (!mmu -> pgtbl_ops )
375- return - EINVAL ;
376-
377- return 0 ;
378- }
379-
380- void panfrost_mmu_pgtable_free (struct panfrost_file_priv * priv )
381- {
382- struct panfrost_device * pfdev = priv -> pfdev ;
383- struct panfrost_mmu * mmu = & priv -> mmu ;
384-
385- spin_lock (& pfdev -> as_lock );
386- if (mmu -> as >= 0 ) {
387- pm_runtime_get_noresume (pfdev -> dev );
388- if (pm_runtime_active (pfdev -> dev ))
389- panfrost_mmu_disable (pfdev , mmu -> as );
390- pm_runtime_put_autosuspend (pfdev -> dev );
391-
392- clear_bit (mmu -> as , & pfdev -> as_alloc_mask );
393- clear_bit (mmu -> as , & pfdev -> as_in_use_mask );
394- list_del (& mmu -> list );
395- }
396- spin_unlock (& pfdev -> as_lock );
397-
398- free_io_pgtable_ops (mmu -> pgtbl_ops );
399- }
400-
401358static struct panfrost_gem_mapping *
402359addr_to_mapping (struct panfrost_device * pfdev , int as , u64 addr )
403360{
404361 struct panfrost_gem_mapping * mapping = NULL ;
405- struct panfrost_file_priv * priv ;
406362 struct drm_mm_node * node ;
407363 u64 offset = addr >> PAGE_SHIFT ;
408364 struct panfrost_mmu * mmu ;
@@ -415,11 +371,10 @@ addr_to_mapping(struct panfrost_device *pfdev, int as, u64 addr)
415371 goto out ;
416372
417373found_mmu :
418- priv = container_of (mmu , struct panfrost_file_priv , mmu );
419374
420- spin_lock (& priv -> mm_lock );
375+ spin_lock (& mmu -> mm_lock );
421376
422- drm_mm_for_each_node (node , & priv -> mm ) {
377+ drm_mm_for_each_node (node , & mmu -> mm ) {
423378 if (offset >= node -> start &&
424379 offset < (node -> start + node -> size )) {
425380 mapping = drm_mm_node_to_panfrost_mapping (node );
@@ -429,7 +384,7 @@ addr_to_mapping(struct panfrost_device *pfdev, int as, u64 addr)
429384 }
430385 }
431386
432- spin_unlock (& priv -> mm_lock );
387+ spin_unlock (& mmu -> mm_lock );
433388out :
434389 spin_unlock (& pfdev -> as_lock );
435390 return mapping ;
@@ -542,6 +497,107 @@ static int panfrost_mmu_map_fault_addr(struct panfrost_device *pfdev, int as,
542497 return ret ;
543498}
544499
500+ static void panfrost_mmu_release_ctx (struct kref * kref )
501+ {
502+ struct panfrost_mmu * mmu = container_of (kref , struct panfrost_mmu ,
503+ refcount );
504+ struct panfrost_device * pfdev = mmu -> pfdev ;
505+
506+ spin_lock (& pfdev -> as_lock );
507+ if (mmu -> as >= 0 ) {
508+ pm_runtime_get_noresume (pfdev -> dev );
509+ if (pm_runtime_active (pfdev -> dev ))
510+ panfrost_mmu_disable (pfdev , mmu -> as );
511+ pm_runtime_put_autosuspend (pfdev -> dev );
512+
513+ clear_bit (mmu -> as , & pfdev -> as_alloc_mask );
514+ clear_bit (mmu -> as , & pfdev -> as_in_use_mask );
515+ list_del (& mmu -> list );
516+ }
517+ spin_unlock (& pfdev -> as_lock );
518+
519+ free_io_pgtable_ops (mmu -> pgtbl_ops );
520+ drm_mm_takedown (& mmu -> mm );
521+ kfree (mmu );
522+ }
523+
524+ void panfrost_mmu_ctx_put (struct panfrost_mmu * mmu )
525+ {
526+ kref_put (& mmu -> refcount , panfrost_mmu_release_ctx );
527+ }
528+
529+ struct panfrost_mmu * panfrost_mmu_ctx_get (struct panfrost_mmu * mmu )
530+ {
531+ kref_get (& mmu -> refcount );
532+
533+ return mmu ;
534+ }
535+
536+ #define PFN_4G (SZ_4G >> PAGE_SHIFT)
537+ #define PFN_4G_MASK (PFN_4G - 1)
538+ #define PFN_16M (SZ_16M >> PAGE_SHIFT)
539+
540+ static void panfrost_drm_mm_color_adjust (const struct drm_mm_node * node ,
541+ unsigned long color ,
542+ u64 * start , u64 * end )
543+ {
544+ /* Executable buffers can't start or end on a 4GB boundary */
545+ if (!(color & PANFROST_BO_NOEXEC )) {
546+ u64 next_seg ;
547+
548+ if ((* start & PFN_4G_MASK ) == 0 )
549+ (* start )++ ;
550+
551+ if ((* end & PFN_4G_MASK ) == 0 )
552+ (* end )-- ;
553+
554+ next_seg = ALIGN (* start , PFN_4G );
555+ if (next_seg - * start <= PFN_16M )
556+ * start = next_seg + 1 ;
557+
558+ * end = min (* end , ALIGN (* start , PFN_4G ) - 1 );
559+ }
560+ }
561+
562+ struct panfrost_mmu * panfrost_mmu_ctx_create (struct panfrost_device * pfdev )
563+ {
564+ struct panfrost_mmu * mmu ;
565+
566+ mmu = kzalloc (sizeof (* mmu ), GFP_KERNEL );
567+ if (!mmu )
568+ return ERR_PTR (- ENOMEM );
569+
570+ mmu -> pfdev = pfdev ;
571+ spin_lock_init (& mmu -> mm_lock );
572+
573+ /* 4G enough for now. can be 48-bit */
574+ drm_mm_init (& mmu -> mm , SZ_32M >> PAGE_SHIFT , (SZ_4G - SZ_32M ) >> PAGE_SHIFT );
575+ mmu -> mm .color_adjust = panfrost_drm_mm_color_adjust ;
576+
577+ INIT_LIST_HEAD (& mmu -> list );
578+ mmu -> as = -1 ;
579+
580+ mmu -> pgtbl_cfg = (struct io_pgtable_cfg ) {
581+ .pgsize_bitmap = SZ_4K | SZ_2M ,
582+ .ias = FIELD_GET (0xff , pfdev -> features .mmu_features ),
583+ .oas = FIELD_GET (0xff00 , pfdev -> features .mmu_features ),
584+ .coherent_walk = pfdev -> coherent ,
585+ .tlb = & mmu_tlb_ops ,
586+ .iommu_dev = pfdev -> dev ,
587+ };
588+
589+ mmu -> pgtbl_ops = alloc_io_pgtable_ops (ARM_MALI_LPAE , & mmu -> pgtbl_cfg ,
590+ mmu );
591+ if (!mmu -> pgtbl_ops ) {
592+ kfree (mmu );
593+ return ERR_PTR (- EINVAL );
594+ }
595+
596+ kref_init (& mmu -> refcount );
597+
598+ return mmu ;
599+ }
600+
545601static const char * access_type_name (struct panfrost_device * pfdev ,
546602 u32 fault_status )
547603{
0 commit comments