summaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/ttm/tests/ttm_resource_test.c
blob: 67584058dadbca151996fb8f0dc99af83c0a5601 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// SPDX-License-Identifier: GPL-2.0 AND MIT
/*
 * Copyright © 2023 Intel Corporation
 */
#include <drm/ttm/ttm_resource.h>

#include "ttm_kunit_helpers.h"

#define RES_SIZE		SZ_4K
#define TTM_PRIV_DUMMY_REG	(TTM_NUM_MEM_TYPES - 1)

struct ttm_resource_test_case {
	const char *description;
	uint32_t mem_type;
	uint32_t flags;
};

struct ttm_resource_test_priv {
	struct ttm_test_devices *devs;
	struct ttm_buffer_object *bo;
	struct ttm_place *place;
};

static const struct ttm_resource_manager_func ttm_resource_manager_mock_funcs = { };

static int ttm_resource_test_init(struct kunit *test)
{
	struct ttm_resource_test_priv *priv;

	priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, priv);

	priv->devs = ttm_test_devices_all(test);
	KUNIT_ASSERT_NOT_NULL(test, priv->devs);

	test->priv = priv;

	return 0;
}

static void ttm_resource_test_fini(struct kunit *test)
{
	struct ttm_resource_test_priv *priv = test->priv;

	ttm_test_devices_put(test, priv->devs);
}

static void ttm_init_test_mocks(struct kunit *test,
				struct ttm_resource_test_priv *priv,
				uint32_t mem_type, uint32_t flags)
{
	size_t size = RES_SIZE;

	/* Make sure we have what we need for a good BO mock */
	KUNIT_ASSERT_NOT_NULL(test, priv->devs->ttm_dev);

	priv->bo = ttm_bo_kunit_init(test, priv->devs, size, NULL);
	priv->place = ttm_place_kunit_init(test, mem_type, flags);
}

static void ttm_init_test_manager(struct kunit *test,
				  struct ttm_resource_test_priv *priv,
				  uint32_t mem_type)
{
	struct ttm_device *ttm_dev = priv->devs->ttm_dev;
	struct ttm_resource_manager *man;
	size_t size = SZ_16K;

	man = kunit_kzalloc(test, sizeof(*man), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, man);

	man->use_tt = false;
	man->func = &ttm_resource_manager_mock_funcs;

	ttm_resource_manager_init(man, ttm_dev, size);
	ttm_set_driver_manager(ttm_dev, mem_type, man);
	ttm_resource_manager_set_used(man, true);
}

static const struct ttm_resource_test_case ttm_resource_cases[] = {
	{
		.description = "Init resource in TTM_PL_SYSTEM",
		.mem_type = TTM_PL_SYSTEM,
	},
	{
		.description = "Init resource in TTM_PL_VRAM",
		.mem_type = TTM_PL_VRAM,
	},
	{
		.description = "Init resource in a private placement",
		.mem_type = TTM_PRIV_DUMMY_REG,
	},
	{
		.description = "Init resource in TTM_PL_SYSTEM, set placement flags",
		.mem_type = TTM_PL_SYSTEM,
		.flags = TTM_PL_FLAG_TOPDOWN,
	},
};

static void ttm_resource_case_desc(const struct ttm_resource_test_case *t, char *desc)
{
	strscpy(desc, t->description, KUNIT_PARAM_DESC_SIZE);
}

KUNIT_ARRAY_PARAM(ttm_resource, ttm_resource_cases, ttm_resource_case_desc);

static void ttm_resource_init_basic(struct kunit *test)
{
	const struct ttm_resource_test_case *params = test->param_value;
	struct ttm_resource_test_priv *priv = test->priv;
	struct ttm_resource *res;
	struct ttm_buffer_object *bo;
	struct ttm_place *place;
	struct ttm_resource_manager *man;
	uint64_t expected_usage;

	ttm_init_test_mocks(test, priv, params->mem_type, params->flags);
	bo = priv->bo;
	place = priv->place;

	if (params->mem_type > TTM_PL_SYSTEM)
		ttm_init_test_manager(test, priv, params->mem_type);

	res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, res);

	man = ttm_manager_type(priv->devs->ttm_dev, place->mem_type);
	expected_usage = man->usage + RES_SIZE;

	KUNIT_ASSERT_TRUE(test, list_empty(&man->lru[bo->priority]));

	ttm_resource_init(bo, place, res);

	KUNIT_ASSERT_EQ(test, res->start, 0);
	KUNIT_ASSERT_EQ(test, res->size, RES_SIZE);
	KUNIT_ASSERT_EQ(test, res->mem_type, place->mem_type);
	KUNIT_ASSERT_EQ(test, res->placement, place->flags);
	KUNIT_ASSERT_PTR_EQ(test, res->bo, bo);

	KUNIT_ASSERT_NULL(test, res->bus.addr);
	KUNIT_ASSERT_EQ(test, res->bus.offset, 0);
	KUNIT_ASSERT_FALSE(test, res->bus.is_iomem);
	KUNIT_ASSERT_EQ(test, res->bus.caching, ttm_cached);
	KUNIT_ASSERT_EQ(test, man->usage, expected_usage);

	KUNIT_ASSERT_TRUE(test, list_is_singular(&man->lru[bo->priority]));

	ttm_resource_fini(man, res);
}

static void ttm_resource_init_pinned(struct kunit *test)
{
	struct ttm_resource_test_priv *priv = test->priv;
	struct ttm_resource *res;
	struct ttm_buffer_object *bo;
	struct ttm_place *place;
	struct ttm_resource_manager *man;

	ttm_init_test_mocks(test, priv, TTM_PL_SYSTEM, 0);
	bo = priv->bo;
	place = priv->place;

	man = ttm_manager_type(priv->devs->ttm_dev, place->mem_type);

	res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, res);
	KUNIT_ASSERT_TRUE(test, list_empty(&bo->bdev->pinned));

	dma_resv_lock(bo->base.resv, NULL);
	ttm_bo_pin(bo);
	ttm_resource_init(bo, place, res);
	KUNIT_ASSERT_TRUE(test, list_is_singular(&bo->bdev->pinned));

	ttm_bo_unpin(bo);
	ttm_resource_fini(man, res);
	dma_resv_unlock(bo->base.resv);

	KUNIT_ASSERT_TRUE(test, list_empty(&bo->bdev->pinned));
}

static void ttm_resource_fini_basic(struct kunit *test)
{
	struct ttm_resource_test_priv *priv = test->priv;
	struct ttm_resource *res;
	struct ttm_buffer_object *bo;
	struct ttm_place *place;
	struct ttm_resource_manager *man;

	ttm_init_test_mocks(test, priv, TTM_PL_SYSTEM, 0);
	bo = priv->bo;
	place = priv->place;

	man = ttm_manager_type(priv->devs->ttm_dev, place->mem_type);

	res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, res);

	ttm_resource_init(bo, place, res);
	ttm_resource_fini(man, res);

	KUNIT_ASSERT_TRUE(test, list_empty(&res->lru));
	KUNIT_ASSERT_EQ(test, man->usage, 0);
}

static void ttm_resource_manager_init_basic(struct kunit *test)
{
	struct ttm_resource_test_priv *priv = test->priv;
	struct ttm_resource_manager *man;
	size_t size = SZ_16K;

	man = kunit_kzalloc(test, sizeof(*man), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, man);

	ttm_resource_manager_init(man, priv->devs->ttm_dev, size);

	KUNIT_ASSERT_PTR_EQ(test, man->bdev, priv->devs->ttm_dev);
	KUNIT_ASSERT_EQ(test, man->size, size);
	KUNIT_ASSERT_EQ(test, man->usage, 0);
	KUNIT_ASSERT_NULL(test, man->move);
	KUNIT_ASSERT_NOT_NULL(test, &man->move_lock);

	for (int i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
		KUNIT_ASSERT_TRUE(test, list_empty(&man->lru[i]));
}

static void ttm_resource_manager_usage_basic(struct kunit *test)
{
	struct ttm_resource_test_priv *priv = test->priv;
	struct ttm_resource *res;
	struct ttm_buffer_object *bo;
	struct ttm_place *place;
	struct ttm_resource_manager *man;
	uint64_t actual_usage;

	ttm_init_test_mocks(test, priv, TTM_PL_SYSTEM, TTM_PL_FLAG_TOPDOWN);
	bo = priv->bo;
	place = priv->place;

	res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, res);

	man = ttm_manager_type(priv->devs->ttm_dev, place->mem_type);

	ttm_resource_init(bo, place, res);
	actual_usage = ttm_resource_manager_usage(man);

	KUNIT_ASSERT_EQ(test, actual_usage, RES_SIZE);

	ttm_resource_fini(man, res);
}

static void ttm_resource_manager_set_used_basic(struct kunit *test)
{
	struct ttm_resource_test_priv *priv = test->priv;
	struct ttm_resource_manager *man;

	man = ttm_manager_type(priv->devs->ttm_dev, TTM_PL_SYSTEM);
	KUNIT_ASSERT_TRUE(test, man->use_type);

	ttm_resource_manager_set_used(man, false);
	KUNIT_ASSERT_FALSE(test, man->use_type);
}

static void ttm_sys_man_alloc_basic(struct kunit *test)
{
	struct ttm_resource_test_priv *priv = test->priv;
	struct ttm_resource_manager *man;
	struct ttm_buffer_object *bo;
	struct ttm_place *place;
	struct ttm_resource *res;
	uint32_t mem_type = TTM_PL_SYSTEM;
	int ret;

	ttm_init_test_mocks(test, priv, mem_type, 0);
	bo = priv->bo;
	place = priv->place;

	man = ttm_manager_type(priv->devs->ttm_dev, mem_type);
	ret = man->func->alloc(man, bo, place, &res);

	KUNIT_ASSERT_EQ(test, ret, 0);
	KUNIT_ASSERT_EQ(test, res->size, RES_SIZE);
	KUNIT_ASSERT_EQ(test, res->mem_type, mem_type);
	KUNIT_ASSERT_PTR_EQ(test, res->bo, bo);

	ttm_resource_fini(man, res);
}

static void ttm_sys_man_free_basic(struct kunit *test)
{
	struct ttm_resource_test_priv *priv = test->priv;
	struct ttm_resource_manager *man;
	struct ttm_buffer_object *bo;
	struct ttm_place *place;
	struct ttm_resource *res;
	uint32_t mem_type = TTM_PL_SYSTEM;

	ttm_init_test_mocks(test, priv, mem_type, 0);
	bo = priv->bo;
	place = priv->place;

	res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, res);

	ttm_resource_alloc(bo, place, &res);

	man = ttm_manager_type(priv->devs->ttm_dev, mem_type);
	man->func->free(man, res);

	KUNIT_ASSERT_TRUE(test, list_empty(&man->lru[bo->priority]));
	KUNIT_ASSERT_EQ(test, man->usage, 0);
}

static struct kunit_case ttm_resource_test_cases[] = {
	KUNIT_CASE_PARAM(ttm_resource_init_basic, ttm_resource_gen_params),
	KUNIT_CASE(ttm_resource_init_pinned),
	KUNIT_CASE(ttm_resource_fini_basic),
	KUNIT_CASE(ttm_resource_manager_init_basic),
	KUNIT_CASE(ttm_resource_manager_usage_basic),
	KUNIT_CASE(ttm_resource_manager_set_used_basic),
	KUNIT_CASE(ttm_sys_man_alloc_basic),
	KUNIT_CASE(ttm_sys_man_free_basic),
	{}
};

static struct kunit_suite ttm_resource_test_suite = {
	.name = "ttm_resource",
	.init = ttm_resource_test_init,
	.exit = ttm_resource_test_fini,
	.test_cases = ttm_resource_test_cases,
};

kunit_test_suites(&ttm_resource_test_suite);

MODULE_LICENSE("GPL");