summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c
blob: b0142341e22357d46fb8ccbb939a160288a0fc3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright (C) 2019 Arm Ltd.
 *
 * Based on msm_gem_freedreno.c:
 * Copyright (C) 2016 Red Hat
 * Author: Rob Clark <robdclark@gmail.com>
 */

#include <linux/list.h>

#include <drm/drm_device.h>
#include <drm/drm_gem_shmem_helper.h>

#include "panfrost_device.h"
#include "panfrost_gem.h"
#include "panfrost_mmu.h"

static unsigned long
panfrost_gem_shrinker_count(struct shrinker *shrinker, struct shrink_control *sc)
{
	struct panfrost_device *pfdev =
		container_of(shrinker, struct panfrost_device, shrinker);
	struct drm_gem_shmem_object *shmem;
	unsigned long count = 0;

	if (!mutex_trylock(&pfdev->shrinker_lock))
		return 0;

	list_for_each_entry(shmem, &pfdev->shrinker_list, madv_list) {
		if (drm_gem_shmem_is_purgeable(shmem))
			count += shmem->base.size >> PAGE_SHIFT;
	}

	mutex_unlock(&pfdev->shrinker_lock);

	return count;
}

static bool panfrost_gem_purge(struct drm_gem_object *obj)
{
	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
	struct panfrost_gem_object *bo = to_panfrost_bo(obj);
	bool ret = false;

	if (atomic_read(&bo->gpu_usecount))
		return false;

	if (!mutex_trylock(&bo->mappings.lock))
		return false;

	if (!mutex_trylock(&shmem->pages_lock))
		goto unlock_mappings;

	panfrost_gem_teardown_mappings_locked(bo);
	drm_gem_shmem_purge_locked(&bo->base);
	ret = true;

	mutex_unlock(&shmem->pages_lock);

unlock_mappings:
	mutex_unlock(&bo->mappings.lock);
	return ret;
}

static unsigned long
panfrost_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
{
	struct panfrost_device *pfdev =
		container_of(shrinker, struct panfrost_device, shrinker);
	struct drm_gem_shmem_object *shmem, *tmp;
	unsigned long freed = 0;

	if (!mutex_trylock(&pfdev->shrinker_lock))
		return SHRINK_STOP;

	list_for_each_entry_safe(shmem, tmp, &pfdev->shrinker_list, madv_list) {
		if (freed >= sc->nr_to_scan)
			break;
		if (drm_gem_shmem_is_purgeable(shmem) &&
		    panfrost_gem_purge(&shmem->base)) {
			freed += shmem->base.size >> PAGE_SHIFT;
			list_del_init(&shmem->madv_list);
		}
	}

	mutex_unlock(&pfdev->shrinker_lock);

	if (freed > 0)
		pr_info_ratelimited("Purging %lu bytes\n", freed << PAGE_SHIFT);

	return freed;
}

/**
 * panfrost_gem_shrinker_init - Initialize panfrost shrinker
 * @dev: DRM device
 *
 * This function registers and sets up the panfrost shrinker.
 */
void panfrost_gem_shrinker_init(struct drm_device *dev)
{
	struct panfrost_device *pfdev = dev->dev_private;
	pfdev->shrinker.count_objects = panfrost_gem_shrinker_count;
	pfdev->shrinker.scan_objects = panfrost_gem_shrinker_scan;
	pfdev->shrinker.seeks = DEFAULT_SEEKS;
	WARN_ON(register_shrinker(&pfdev->shrinker));
}

/**
 * panfrost_gem_shrinker_cleanup - Clean up panfrost shrinker
 * @dev: DRM device
 *
 * This function unregisters the panfrost shrinker.
 */
void panfrost_gem_shrinker_cleanup(struct drm_device *dev)
{
	struct panfrost_device *pfdev = dev->dev_private;

	if (pfdev->shrinker.nr_deferred) {
		unregister_shrinker(&pfdev->shrinker);
	}
}