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