aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/vkms/vkms_config.c
blob: db8be054f6f4b5523b5b69c50955411795c3d5d6 (plain) (blame)
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// SPDX-License-Identifier: GPL-2.0+

#include <linux/slab.h>

#include <drm/drm_print.h>
#include <drm/drm_debugfs.h>
#include <kunit/visibility.h>

#include "vkms_config.h"

struct vkms_config *vkms_config_create(const char *dev_name)
{
	struct vkms_config *config;

	config = kzalloc(sizeof(*config), GFP_KERNEL);
	if (!config)
		return ERR_PTR(-ENOMEM);

	config->dev_name = kstrdup_const(dev_name, GFP_KERNEL);
	if (!config->dev_name) {
		kfree(config);
		return ERR_PTR(-ENOMEM);
	}

	INIT_LIST_HEAD(&config->planes);
	INIT_LIST_HEAD(&config->crtcs);
	INIT_LIST_HEAD(&config->encoders);

	return config;
}
EXPORT_SYMBOL_IF_KUNIT(vkms_config_create);

struct vkms_config *vkms_config_default_create(bool enable_cursor,
					       bool enable_writeback,
					       bool enable_overlay)
{
	struct vkms_config *config;
	struct vkms_config_plane *plane_cfg;
	struct vkms_config_crtc *crtc_cfg;
	struct vkms_config_encoder *encoder_cfg;
	int n;

	config = vkms_config_create(DEFAULT_DEVICE_NAME);
	if (IS_ERR(config))
		return config;

	plane_cfg = vkms_config_create_plane(config);
	if (IS_ERR(plane_cfg))
		goto err_alloc;
	vkms_config_plane_set_type(plane_cfg, DRM_PLANE_TYPE_PRIMARY);

	crtc_cfg = vkms_config_create_crtc(config);
	if (IS_ERR(crtc_cfg))
		goto err_alloc;
	vkms_config_crtc_set_writeback(crtc_cfg, enable_writeback);

	if (vkms_config_plane_attach_crtc(plane_cfg, crtc_cfg))
		goto err_alloc;

	if (enable_overlay) {
		for (n = 0; n < NUM_OVERLAY_PLANES; n++) {
			plane_cfg = vkms_config_create_plane(config);
			if (IS_ERR(plane_cfg))
				goto err_alloc;

			vkms_config_plane_set_type(plane_cfg,
						   DRM_PLANE_TYPE_OVERLAY);

			if (vkms_config_plane_attach_crtc(plane_cfg, crtc_cfg))
				goto err_alloc;
		}
	}

	if (enable_cursor) {
		plane_cfg = vkms_config_create_plane(config);
		if (IS_ERR(plane_cfg))
			goto err_alloc;

		vkms_config_plane_set_type(plane_cfg, DRM_PLANE_TYPE_CURSOR);

		if (vkms_config_plane_attach_crtc(plane_cfg, crtc_cfg))
			goto err_alloc;
	}

	encoder_cfg = vkms_config_create_encoder(config);
	if (IS_ERR(encoder_cfg))
		goto err_alloc;

	return config;

err_alloc:
	vkms_config_destroy(config);
	return ERR_PTR(-ENOMEM);
}
EXPORT_SYMBOL_IF_KUNIT(vkms_config_default_create);

void vkms_config_destroy(struct vkms_config *config)
{
	struct vkms_config_plane *plane_cfg, *plane_tmp;
	struct vkms_config_crtc *crtc_cfg, *crtc_tmp;
	struct vkms_config_encoder *encoder_cfg, *encoder_tmp;

	list_for_each_entry_safe(plane_cfg, plane_tmp, &config->planes, link)
		vkms_config_destroy_plane(plane_cfg);

	list_for_each_entry_safe(crtc_cfg, crtc_tmp, &config->crtcs, link)
		vkms_config_destroy_crtc(config, crtc_cfg);

	list_for_each_entry_safe(encoder_cfg, encoder_tmp, &config->encoders, link)
		vkms_config_destroy_encoder(config, encoder_cfg);

	kfree_const(config->dev_name);
	kfree(config);
}
EXPORT_SYMBOL_IF_KUNIT(vkms_config_destroy);

static bool valid_plane_number(const struct vkms_config *config)
{
	struct drm_device *dev = config->dev ? &config->dev->drm : NULL;
	size_t n_planes;

	n_planes = list_count_nodes((struct list_head *)&config->planes);
	if (n_planes <= 0 || n_planes >= 32) {
		drm_info(dev, "The number of planes must be between 1 and 31\n");
		return false;
	}

	return true;
}

static bool valid_planes_for_crtc(const struct vkms_config *config,
				  struct vkms_config_crtc *crtc_cfg)
{
	struct drm_device *dev = config->dev ? &config->dev->drm : NULL;
	struct vkms_config_plane *plane_cfg;
	bool has_primary_plane = false;
	bool has_cursor_plane = false;

	vkms_config_for_each_plane(config, plane_cfg) {
		struct vkms_config_crtc *possible_crtc;
		unsigned long idx = 0;
		enum drm_plane_type type;

		type = vkms_config_plane_get_type(plane_cfg);

		vkms_config_plane_for_each_possible_crtc(plane_cfg, idx, possible_crtc) {
			if (possible_crtc != crtc_cfg)
				continue;

			if (type == DRM_PLANE_TYPE_PRIMARY) {
				if (has_primary_plane) {
					drm_info(dev, "Multiple primary planes\n");
					return false;
				}

				has_primary_plane = true;
			} else if (type == DRM_PLANE_TYPE_CURSOR) {
				if (has_cursor_plane) {
					drm_info(dev, "Multiple cursor planes\n");
					return false;
				}

				has_cursor_plane = true;
			}
		}
	}

	if (!has_primary_plane) {
		drm_info(dev, "Primary plane not found\n");
		return false;
	}

	return true;
}

static bool valid_plane_possible_crtcs(const struct vkms_config *config)
{
	struct drm_device *dev = config->dev ? &config->dev->drm : NULL;
	struct vkms_config_plane *plane_cfg;

	vkms_config_for_each_plane(config, plane_cfg) {
		if (xa_empty(&plane_cfg->possible_crtcs)) {
			drm_info(dev, "All planes must have at least one possible CRTC\n");
			return false;
		}
	}

	return true;
}

static bool valid_crtc_number(const struct vkms_config *config)
{
	struct drm_device *dev = config->dev ? &config->dev->drm : NULL;
	size_t n_crtcs;

	n_crtcs = list_count_nodes((struct list_head *)&config->crtcs);
	if (n_crtcs <= 0 || n_crtcs >= 32) {
		drm_info(dev, "The number of CRTCs must be between 1 and 31\n");
		return false;
	}

	return true;
}

static bool valid_encoder_number(const struct vkms_config *config)
{
	struct drm_device *dev = config->dev ? &config->dev->drm : NULL;
	size_t n_encoders;

	n_encoders = list_count_nodes((struct list_head *)&config->encoders);
	if (n_encoders <= 0 || n_encoders >= 32) {
		drm_info(dev, "The number of encoders must be between 1 and 31\n");
		return false;
	}

	return true;
}

bool vkms_config_is_valid(const struct vkms_config *config)
{
	struct vkms_config_crtc *crtc_cfg;

	if (!valid_plane_number(config))
		return false;

	if (!valid_crtc_number(config))
		return false;

	if (!valid_encoder_number(config))
		return false;

	if (!valid_plane_possible_crtcs(config))
		return false;

	vkms_config_for_each_crtc(config, crtc_cfg) {
		if (!valid_planes_for_crtc(config, crtc_cfg))
			return false;
	}

	return true;
}
EXPORT_SYMBOL_IF_KUNIT(vkms_config_is_valid);

static int vkms_config_show(struct seq_file *m, void *data)
{
	struct drm_debugfs_entry *entry = m->private;
	struct drm_device *dev = entry->dev;
	struct vkms_device *vkmsdev = drm_device_to_vkms_device(dev);
	const char *dev_name;
	struct vkms_config_plane *plane_cfg;
	struct vkms_config_crtc *crtc_cfg;
	struct vkms_config_encoder *encoder_cfg;

	dev_name = vkms_config_get_device_name((struct vkms_config *)vkmsdev->config);
	seq_printf(m, "dev_name=%s\n", dev_name);

	vkms_config_for_each_plane(vkmsdev->config, plane_cfg) {
		seq_puts(m, "plane:\n");
		seq_printf(m, "\ttype=%d\n",
			   vkms_config_plane_get_type(plane_cfg));
	}

	vkms_config_for_each_crtc(vkmsdev->config, crtc_cfg) {
		seq_puts(m, "crtc:\n");
		seq_printf(m, "\twriteback=%d\n",
			   vkms_config_crtc_get_writeback(crtc_cfg));
	}

	vkms_config_for_each_encoder(vkmsdev->config, encoder_cfg)
		seq_puts(m, "encoder\n");

	return 0;
}

static const struct drm_debugfs_info vkms_config_debugfs_list[] = {
	{ "vkms_config", vkms_config_show, 0 },
};

void vkms_config_register_debugfs(struct vkms_device *vkms_device)
{
	drm_debugfs_add_files(&vkms_device->drm, vkms_config_debugfs_list,
			      ARRAY_SIZE(vkms_config_debugfs_list));
}

struct vkms_config_plane *vkms_config_create_plane(struct vkms_config *config)
{
	struct vkms_config_plane *plane_cfg;

	plane_cfg = kzalloc(sizeof(*plane_cfg), GFP_KERNEL);
	if (!plane_cfg)
		return ERR_PTR(-ENOMEM);

	plane_cfg->config = config;
	vkms_config_plane_set_type(plane_cfg, DRM_PLANE_TYPE_OVERLAY);
	xa_init_flags(&plane_cfg->possible_crtcs, XA_FLAGS_ALLOC);

	list_add_tail(&plane_cfg->link, &config->planes);

	return plane_cfg;
}
EXPORT_SYMBOL_IF_KUNIT(vkms_config_create_plane);

void vkms_config_destroy_plane(struct vkms_config_plane *plane_cfg)
{
	xa_destroy(&plane_cfg->possible_crtcs);
	list_del(&plane_cfg->link);
	kfree(plane_cfg);
}
EXPORT_SYMBOL_IF_KUNIT(vkms_config_destroy_plane);

int __must_check vkms_config_plane_attach_crtc(struct vkms_config_plane *plane_cfg,
					       struct vkms_config_crtc *crtc_cfg)
{
	struct vkms_config_crtc *possible_crtc;
	unsigned long idx = 0;
	u32 crtc_idx = 0;

	if (plane_cfg->config != crtc_cfg->config)
		return -EINVAL;

	vkms_config_plane_for_each_possible_crtc(plane_cfg, idx, possible_crtc) {
		if (possible_crtc == crtc_cfg)
			return -EEXIST;
	}

	return xa_alloc(&plane_cfg->possible_crtcs, &crtc_idx, crtc_cfg,
			xa_limit_32b, GFP_KERNEL);
}
EXPORT_SYMBOL_IF_KUNIT(vkms_config_plane_attach_crtc);

void vkms_config_plane_detach_crtc(struct vkms_config_plane *plane_cfg,
				   struct vkms_config_crtc *crtc_cfg)
{
	struct vkms_config_crtc *possible_crtc;
	unsigned long idx = 0;

	vkms_config_plane_for_each_possible_crtc(plane_cfg, idx, possible_crtc) {
		if (possible_crtc == crtc_cfg)
			xa_erase(&plane_cfg->possible_crtcs, idx);
	}
}
EXPORT_SYMBOL_IF_KUNIT(vkms_config_plane_detach_crtc);

struct vkms_config_crtc *vkms_config_create_crtc(struct vkms_config *config)
{
	struct vkms_config_crtc *crtc_cfg;

	crtc_cfg = kzalloc(sizeof(*crtc_cfg), GFP_KERNEL);
	if (!crtc_cfg)
		return ERR_PTR(-ENOMEM);

	crtc_cfg->config = config;
	vkms_config_crtc_set_writeback(crtc_cfg, false);

	list_add_tail(&crtc_cfg->link, &config->crtcs);

	return crtc_cfg;
}
EXPORT_SYMBOL_IF_KUNIT(vkms_config_create_crtc);

void vkms_config_destroy_crtc(struct vkms_config *config,
			      struct vkms_config_crtc *crtc_cfg)
{
	struct vkms_config_plane *plane_cfg;

	vkms_config_for_each_plane(config, plane_cfg)
		vkms_config_plane_detach_crtc(plane_cfg, crtc_cfg);

	list_del(&crtc_cfg->link);
	kfree(crtc_cfg);
}
EXPORT_SYMBOL_IF_KUNIT(vkms_config_destroy_crtc);

/**
 * vkms_config_crtc_get_plane() - Return the first attached plane to a CRTC with
 * the specific type
 * @config: Configuration containing the CRTC and the plane
 * @crtc_cfg: Only find planes attached to this CRTC
 * @type: Plane type to search
 *
 * Returns:
 * The first plane found attached to @crtc_cfg with the type @type.
 */
static struct vkms_config_plane *vkms_config_crtc_get_plane(const struct vkms_config *config,
							    struct vkms_config_crtc *crtc_cfg,
							    enum drm_plane_type type)
{
	struct vkms_config_plane *plane_cfg;
	struct vkms_config_crtc *possible_crtc;
	enum drm_plane_type current_type;
	unsigned long idx = 0;

	vkms_config_for_each_plane(config, plane_cfg) {
		current_type = vkms_config_plane_get_type(plane_cfg);

		vkms_config_plane_for_each_possible_crtc(plane_cfg, idx, possible_crtc) {
			if (possible_crtc == crtc_cfg && current_type == type)
				return plane_cfg;
		}
	}

	return NULL;
}

struct vkms_config_plane *vkms_config_crtc_primary_plane(const struct vkms_config *config,
							 struct vkms_config_crtc *crtc_cfg)
{
	return vkms_config_crtc_get_plane(config, crtc_cfg, DRM_PLANE_TYPE_PRIMARY);
}
EXPORT_SYMBOL_IF_KUNIT(vkms_config_crtc_primary_plane);

struct vkms_config_plane *vkms_config_crtc_cursor_plane(const struct vkms_config *config,
							struct vkms_config_crtc *crtc_cfg)
{
	return vkms_config_crtc_get_plane(config, crtc_cfg, DRM_PLANE_TYPE_CURSOR);
}
EXPORT_SYMBOL_IF_KUNIT(vkms_config_crtc_cursor_plane);

struct vkms_config_encoder *vkms_config_create_encoder(struct vkms_config *config)
{
	struct vkms_config_encoder *encoder_cfg;

	encoder_cfg = kzalloc(sizeof(*encoder_cfg), GFP_KERNEL);
	if (!encoder_cfg)
		return ERR_PTR(-ENOMEM);

	encoder_cfg->config = config;
	list_add_tail(&encoder_cfg->link, &config->encoders);

	return encoder_cfg;
}
EXPORT_SYMBOL_IF_KUNIT(vkms_config_create_encoder);

void vkms_config_destroy_encoder(struct vkms_config *config,
				 struct vkms_config_encoder *encoder_cfg)
{
	list_del(&encoder_cfg->link);
	kfree(encoder_cfg);
}
EXPORT_SYMBOL_IF_KUNIT(vkms_config_destroy_encoder);