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 
43 	if (!mutex_trylock(&shmem->pages_lock))
44 		return false;
45 
46 	panfrost_mmu_unmap(to_panfrost_bo(obj));
47 	drm_gem_shmem_purge_locked(obj);
48 
49 	mutex_unlock(&shmem->pages_lock);
50 	return true;
51 }
52 
53 static unsigned long
54 panfrost_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
55 {
56 	struct panfrost_device *pfdev =
57 		container_of(shrinker, struct panfrost_device, shrinker);
58 	struct drm_gem_shmem_object *shmem, *tmp;
59 	unsigned long freed = 0;
60 
61 	if (!mutex_trylock(&pfdev->shrinker_lock))
62 		return SHRINK_STOP;
63 
64 	list_for_each_entry_safe(shmem, tmp, &pfdev->shrinker_list, madv_list) {
65 		if (freed >= sc->nr_to_scan)
66 			break;
67 		if (drm_gem_shmem_is_purgeable(shmem) &&
68 		    panfrost_gem_purge(&shmem->base)) {
69 			freed += shmem->base.size >> PAGE_SHIFT;
70 			list_del_init(&shmem->madv_list);
71 		}
72 	}
73 
74 	mutex_unlock(&pfdev->shrinker_lock);
75 
76 	if (freed > 0)
77 		pr_info_ratelimited("Purging %lu bytes\n", freed << PAGE_SHIFT);
78 
79 	return freed;
80 }
81 
82 /**
83  * panfrost_gem_shrinker_init - Initialize panfrost shrinker
84  * @dev: DRM device
85  *
86  * This function registers and sets up the panfrost shrinker.
87  */
88 void panfrost_gem_shrinker_init(struct drm_device *dev)
89 {
90 	struct panfrost_device *pfdev = dev->dev_private;
91 	pfdev->shrinker.count_objects = panfrost_gem_shrinker_count;
92 	pfdev->shrinker.scan_objects = panfrost_gem_shrinker_scan;
93 	pfdev->shrinker.seeks = DEFAULT_SEEKS;
94 	WARN_ON(register_shrinker(&pfdev->shrinker));
95 }
96 
97 /**
98  * panfrost_gem_shrinker_cleanup - Clean up panfrost shrinker
99  * @dev: DRM device
100  *
101  * This function unregisters the panfrost shrinker.
102  */
103 void panfrost_gem_shrinker_cleanup(struct drm_device *dev)
104 {
105 	struct panfrost_device *pfdev = dev->dev_private;
106 
107 	if (pfdev->shrinker.nr_deferred) {
108 		unregister_shrinker(&pfdev->shrinker);
109 	}
110 }
111