1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2019 Arm Ltd.
3  *
4  * Based on msm_gem_freedreno.c:
5  * Copyright (C) 2016 Red Hat
6  * Author: Rob Clark <robdclark@gmail.com>
7  */
8 
9 #include <linux/list.h>
10 
11 #include <drm/drm_device.h>
12 #include <drm/drm_gem_shmem_helper.h>
13 
14 #include "panfrost_device.h"
15 #include "panfrost_gem.h"
16 #include "panfrost_mmu.h"
17 
18 static unsigned long
19 panfrost_gem_shrinker_count(struct shrinker *shrinker, struct shrink_control *sc)
20 {
21 	struct panfrost_device *pfdev =
22 		container_of(shrinker, struct panfrost_device, shrinker);
23 	struct drm_gem_shmem_object *shmem;
24 	unsigned long count = 0;
25 
26 	if (!mutex_trylock(&pfdev->shrinker_lock))
27 		return 0;
28 
29 	list_for_each_entry(shmem, &pfdev->shrinker_list, madv_list) {
30 		if (drm_gem_shmem_is_purgeable(shmem))
31 			count += shmem->base.size >> PAGE_SHIFT;
32 	}
33 
34 	mutex_unlock(&pfdev->shrinker_lock);
35 
36 	return count;
37 }
38 
39 static bool panfrost_gem_purge(struct drm_gem_object *obj)
40 {
41 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
42 	struct panfrost_gem_object *bo = to_panfrost_bo(obj);
43 	bool ret = false;
44 
45 	if (atomic_read(&bo->gpu_usecount))
46 		return false;
47 
48 	if (!mutex_trylock(&bo->mappings.lock))
49 		return false;
50 
51 	if (!mutex_trylock(&shmem->pages_lock))
52 		goto unlock_mappings;
53 
54 	panfrost_gem_teardown_mappings_locked(bo);
55 	drm_gem_shmem_purge_locked(&bo->base);
56 	ret = true;
57 
58 	mutex_unlock(&shmem->pages_lock);
59 
60 unlock_mappings:
61 	mutex_unlock(&bo->mappings.lock);
62 	return ret;
63 }
64 
65 static unsigned long
66 panfrost_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
67 {
68 	struct panfrost_device *pfdev =
69 		container_of(shrinker, struct panfrost_device, shrinker);
70 	struct drm_gem_shmem_object *shmem, *tmp;
71 	unsigned long freed = 0;
72 
73 	if (!mutex_trylock(&pfdev->shrinker_lock))
74 		return SHRINK_STOP;
75 
76 	list_for_each_entry_safe(shmem, tmp, &pfdev->shrinker_list, madv_list) {
77 		if (freed >= sc->nr_to_scan)
78 			break;
79 		if (drm_gem_shmem_is_purgeable(shmem) &&
80 		    panfrost_gem_purge(&shmem->base)) {
81 			freed += shmem->base.size >> PAGE_SHIFT;
82 			list_del_init(&shmem->madv_list);
83 		}
84 	}
85 
86 	mutex_unlock(&pfdev->shrinker_lock);
87 
88 	if (freed > 0)
89 		pr_info_ratelimited("Purging %lu bytes\n", freed << PAGE_SHIFT);
90 
91 	return freed;
92 }
93 
94 /**
95  * panfrost_gem_shrinker_init - Initialize panfrost shrinker
96  * @dev: DRM device
97  *
98  * This function registers and sets up the panfrost shrinker.
99  */
100 void panfrost_gem_shrinker_init(struct drm_device *dev)
101 {
102 	struct panfrost_device *pfdev = dev->dev_private;
103 	pfdev->shrinker.count_objects = panfrost_gem_shrinker_count;
104 	pfdev->shrinker.scan_objects = panfrost_gem_shrinker_scan;
105 	pfdev->shrinker.seeks = DEFAULT_SEEKS;
106 	WARN_ON(register_shrinker(&pfdev->shrinker, "drm-panfrost"));
107 }
108 
109 /**
110  * panfrost_gem_shrinker_cleanup - Clean up panfrost shrinker
111  * @dev: DRM device
112  *
113  * This function unregisters the panfrost shrinker.
114  */
115 void panfrost_gem_shrinker_cleanup(struct drm_device *dev)
116 {
117 	struct panfrost_device *pfdev = dev->dev_private;
118 
119 	if (pfdev->shrinker.nr_deferred) {
120 		unregister_shrinker(&pfdev->shrinker);
121 	}
122 }
123