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
@@ -359,57 +362,10 @@ static const struct iommu_flush_ops mmu_tlb_ops = {
359362 .tlb_flush_leaf = mmu_tlb_flush_leaf ,
360363};
361364
362- int panfrost_mmu_pgtable_alloc (struct panfrost_file_priv * priv )
363- {
364- struct panfrost_mmu * mmu = & priv -> mmu ;
365- struct panfrost_device * pfdev = priv -> pfdev ;
366-
367- INIT_LIST_HEAD (& mmu -> list );
368- mmu -> as = -1 ;
369-
370- mmu -> pgtbl_cfg = (struct io_pgtable_cfg ) {
371- .pgsize_bitmap = SZ_4K | SZ_2M ,
372- .ias = FIELD_GET (0xff , pfdev -> features .mmu_features ),
373- .oas = FIELD_GET (0xff00 , pfdev -> features .mmu_features ),
374- .coherent_walk = pfdev -> coherent ,
375- .tlb = & mmu_tlb_ops ,
376- .iommu_dev = pfdev -> dev ,
377- };
378-
379- mmu -> pgtbl_ops = alloc_io_pgtable_ops (ARM_MALI_LPAE , & mmu -> pgtbl_cfg ,
380- priv );
381- if (!mmu -> pgtbl_ops )
382- return - EINVAL ;
383-
384- return 0 ;
385- }
386-
387- void panfrost_mmu_pgtable_free (struct panfrost_file_priv * priv )
388- {
389- struct panfrost_device * pfdev = priv -> pfdev ;
390- struct panfrost_mmu * mmu = & priv -> mmu ;
391-
392- spin_lock (& pfdev -> as_lock );
393- if (mmu -> as >= 0 ) {
394- pm_runtime_get_noresume (pfdev -> dev );
395- if (pm_runtime_active (pfdev -> dev ))
396- panfrost_mmu_disable (pfdev , mmu -> as );
397- pm_runtime_put_autosuspend (pfdev -> dev );
398-
399- clear_bit (mmu -> as , & pfdev -> as_alloc_mask );
400- clear_bit (mmu -> as , & pfdev -> as_in_use_mask );
401- list_del (& mmu -> list );
402- }
403- spin_unlock (& pfdev -> as_lock );
404-
405- free_io_pgtable_ops (mmu -> pgtbl_ops );
406- }
407-
408365static struct panfrost_gem_mapping *
409366addr_to_mapping (struct panfrost_device * pfdev , int as , u64 addr )
410367{
411368 struct panfrost_gem_mapping * mapping = NULL ;
412- struct panfrost_file_priv * priv ;
413369 struct drm_mm_node * node ;
414370 u64 offset = addr >> PAGE_SHIFT ;
415371 struct panfrost_mmu * mmu ;
@@ -422,11 +378,10 @@ addr_to_mapping(struct panfrost_device *pfdev, int as, u64 addr)
422378 goto out ;
423379
424380found_mmu :
425- priv = container_of (mmu , struct panfrost_file_priv , mmu );
426381
427- spin_lock (& priv -> mm_lock );
382+ spin_lock (& mmu -> mm_lock );
428383
429- drm_mm_for_each_node (node , & priv -> mm ) {
384+ drm_mm_for_each_node (node , & mmu -> mm ) {
430385 if (offset >= node -> start &&
431386 offset < (node -> start + node -> size )) {
432387 mapping = drm_mm_node_to_panfrost_mapping (node );
@@ -436,7 +391,7 @@ addr_to_mapping(struct panfrost_device *pfdev, int as, u64 addr)
436391 }
437392 }
438393
439- spin_unlock (& priv -> mm_lock );
394+ spin_unlock (& mmu -> mm_lock );
440395out :
441396 spin_unlock (& pfdev -> as_lock );
442397 return mapping ;
@@ -549,6 +504,107 @@ static int panfrost_mmu_map_fault_addr(struct panfrost_device *pfdev, int as,
549504 return ret ;
550505}
551506
507+ static void panfrost_mmu_release_ctx (struct kref * kref )
508+ {
509+ struct panfrost_mmu * mmu = container_of (kref , struct panfrost_mmu ,
510+ refcount );
511+ struct panfrost_device * pfdev = mmu -> pfdev ;
512+
513+ spin_lock (& pfdev -> as_lock );
514+ if (mmu -> as >= 0 ) {
515+ pm_runtime_get_noresume (pfdev -> dev );
516+ if (pm_runtime_active (pfdev -> dev ))
517+ panfrost_mmu_disable (pfdev , mmu -> as );
518+ pm_runtime_put_autosuspend (pfdev -> dev );
519+
520+ clear_bit (mmu -> as , & pfdev -> as_alloc_mask );
521+ clear_bit (mmu -> as , & pfdev -> as_in_use_mask );
522+ list_del (& mmu -> list );
523+ }
524+ spin_unlock (& pfdev -> as_lock );
525+
526+ free_io_pgtable_ops (mmu -> pgtbl_ops );
527+ drm_mm_takedown (& mmu -> mm );
528+ kfree (mmu );
529+ }
530+
531+ void panfrost_mmu_ctx_put (struct panfrost_mmu * mmu )
532+ {
533+ kref_put (& mmu -> refcount , panfrost_mmu_release_ctx );
534+ }
535+
536+ struct panfrost_mmu * panfrost_mmu_ctx_get (struct panfrost_mmu * mmu )
537+ {
538+ kref_get (& mmu -> refcount );
539+
540+ return mmu ;
541+ }
542+
543+ #define PFN_4G (SZ_4G >> PAGE_SHIFT)
544+ #define PFN_4G_MASK (PFN_4G - 1)
545+ #define PFN_16M (SZ_16M >> PAGE_SHIFT)
546+
547+ static void panfrost_drm_mm_color_adjust (const struct drm_mm_node * node ,
548+ unsigned long color ,
549+ u64 * start , u64 * end )
550+ {
551+ /* Executable buffers can't start or end on a 4GB boundary */
552+ if (!(color & PANFROST_BO_NOEXEC )) {
553+ u64 next_seg ;
554+
555+ if ((* start & PFN_4G_MASK ) == 0 )
556+ (* start )++ ;
557+
558+ if ((* end & PFN_4G_MASK ) == 0 )
559+ (* end )-- ;
560+
561+ next_seg = ALIGN (* start , PFN_4G );
562+ if (next_seg - * start <= PFN_16M )
563+ * start = next_seg + 1 ;
564+
565+ * end = min (* end , ALIGN (* start , PFN_4G ) - 1 );
566+ }
567+ }
568+
569+ struct panfrost_mmu * panfrost_mmu_ctx_create (struct panfrost_device * pfdev )
570+ {
571+ struct panfrost_mmu * mmu ;
572+
573+ mmu = kzalloc (sizeof (* mmu ), GFP_KERNEL );
574+ if (!mmu )
575+ return ERR_PTR (- ENOMEM );
576+
577+ mmu -> pfdev = pfdev ;
578+ spin_lock_init (& mmu -> mm_lock );
579+
580+ /* 4G enough for now. can be 48-bit */
581+ drm_mm_init (& mmu -> mm , SZ_32M >> PAGE_SHIFT , (SZ_4G - SZ_32M ) >> PAGE_SHIFT );
582+ mmu -> mm .color_adjust = panfrost_drm_mm_color_adjust ;
583+
584+ INIT_LIST_HEAD (& mmu -> list );
585+ mmu -> as = -1 ;
586+
587+ mmu -> pgtbl_cfg = (struct io_pgtable_cfg ) {
588+ .pgsize_bitmap = SZ_4K | SZ_2M ,
589+ .ias = FIELD_GET (0xff , pfdev -> features .mmu_features ),
590+ .oas = FIELD_GET (0xff00 , pfdev -> features .mmu_features ),
591+ .coherent_walk = pfdev -> coherent ,
592+ .tlb = & mmu_tlb_ops ,
593+ .iommu_dev = pfdev -> dev ,
594+ };
595+
596+ mmu -> pgtbl_ops = alloc_io_pgtable_ops (ARM_MALI_LPAE , & mmu -> pgtbl_cfg ,
597+ mmu );
598+ if (!mmu -> pgtbl_ops ) {
599+ kfree (mmu );
600+ return ERR_PTR (- EINVAL );
601+ }
602+
603+ kref_init (& mmu -> refcount );
604+
605+ return mmu ;
606+ }
607+
552608static const char * access_type_name (struct panfrost_device * pfdev ,
553609 u32 fault_status )
554610{
0 commit comments