Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * drm gem framebuffer helper functions
  4 *
  5 * Copyright (C) 2017 Noralf Trønnes
 
 
 
 
 
  6 */
  7
 
 
 
  8#include <linux/slab.h>
  9#include <linux/module.h>
 10
 11#include <drm/drm_damage_helper.h>
 12#include <drm/drm_drv.h>
 
 13#include <drm/drm_fourcc.h>
 14#include <drm/drm_framebuffer.h>
 15#include <drm/drm_gem.h>
 16#include <drm/drm_gem_framebuffer_helper.h>
 17#include <drm/drm_modeset_helper.h>
 18
 19#include "drm_internal.h"
 20
 21MODULE_IMPORT_NS("DMA_BUF");
 22
 23#define AFBC_HEADER_SIZE		16
 24#define AFBC_TH_LAYOUT_ALIGNMENT	8
 25#define AFBC_HDR_ALIGN			64
 26#define AFBC_SUPERBLOCK_PIXELS		256
 27#define AFBC_SUPERBLOCK_ALIGNMENT	128
 28#define AFBC_TH_BODY_START_ALIGNMENT	4096
 29
 30/**
 31 * DOC: overview
 32 *
 33 * This library provides helpers for drivers that don't subclass
 34 * &drm_framebuffer and use &drm_gem_object for their backing storage.
 35 *
 36 * Drivers without additional needs to validate framebuffers can simply use
 37 * drm_gem_fb_create() and everything is wired up automatically. Other drivers
 38 * can use all parts independently.
 39 */
 40
 41/**
 42 * drm_gem_fb_get_obj() - Get GEM object backing the framebuffer
 43 * @fb: Framebuffer
 44 * @plane: Plane index
 45 *
 46 * No additional reference is taken beyond the one that the &drm_frambuffer
 47 * already holds.
 48 *
 49 * Returns:
 50 * Pointer to &drm_gem_object for the given framebuffer and plane index or NULL
 51 * if it does not exist.
 52 */
 53struct drm_gem_object *drm_gem_fb_get_obj(struct drm_framebuffer *fb,
 54					  unsigned int plane)
 55{
 56	struct drm_device *dev = fb->dev;
 57
 58	if (drm_WARN_ON_ONCE(dev, plane >= ARRAY_SIZE(fb->obj)))
 59		return NULL;
 60	else if (drm_WARN_ON_ONCE(dev, !fb->obj[plane]))
 61		return NULL;
 62
 63	return fb->obj[plane];
 64}
 65EXPORT_SYMBOL_GPL(drm_gem_fb_get_obj);
 66
 67static int
 68drm_gem_fb_init(struct drm_device *dev,
 69		 struct drm_framebuffer *fb,
 70		 const struct drm_mode_fb_cmd2 *mode_cmd,
 71		 struct drm_gem_object **obj, unsigned int num_planes,
 72		 const struct drm_framebuffer_funcs *funcs)
 73{
 74	unsigned int i;
 75	int ret;
 
 
 
 
 76
 77	drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
 78
 79	for (i = 0; i < num_planes; i++)
 80		fb->obj[i] = obj[i];
 81
 82	ret = drm_framebuffer_init(dev, fb, funcs);
 83	if (ret)
 84		drm_err(dev, "Failed to init framebuffer: %d\n", ret);
 
 
 
 
 85
 86	return ret;
 87}
 88
 89/**
 90 * drm_gem_fb_destroy - Free GEM backed framebuffer
 91 * @fb: Framebuffer
 92 *
 93 * Frees a GEM backed framebuffer with its backing buffer(s) and the structure
 94 * itself. Drivers can use this as their &drm_framebuffer_funcs->destroy
 95 * callback.
 96 */
 97void drm_gem_fb_destroy(struct drm_framebuffer *fb)
 98{
 99	unsigned int i;
100
101	for (i = 0; i < fb->format->num_planes; i++)
102		drm_gem_object_put(fb->obj[i]);
103
104	drm_framebuffer_cleanup(fb);
105	kfree(fb);
106}
107EXPORT_SYMBOL(drm_gem_fb_destroy);
108
109/**
110 * drm_gem_fb_create_handle - Create handle for GEM backed framebuffer
111 * @fb: Framebuffer
112 * @file: DRM file to register the handle for
113 * @handle: Pointer to return the created handle
114 *
115 * This function creates a handle for the GEM object backing the framebuffer.
116 * Drivers can use this as their &drm_framebuffer_funcs->create_handle
117 * callback. The GETFB IOCTL calls into this callback.
118 *
119 * Returns:
120 * 0 on success or a negative error code on failure.
121 */
122int drm_gem_fb_create_handle(struct drm_framebuffer *fb, struct drm_file *file,
123			     unsigned int *handle)
124{
125	return drm_gem_handle_create(file, fb->obj[0], handle);
126}
127EXPORT_SYMBOL(drm_gem_fb_create_handle);
128
129/**
130 * drm_gem_fb_init_with_funcs() - Helper function for implementing
131 *				  &drm_mode_config_funcs.fb_create
132 *				  callback in cases when the driver
133 *				  allocates a subclass of
134 *				  struct drm_framebuffer
135 * @dev: DRM device
136 * @fb: framebuffer object
137 * @file: DRM file that holds the GEM handle(s) backing the framebuffer
138 * @mode_cmd: Metadata from the userspace framebuffer creation request
139 * @funcs: vtable to be used for the new framebuffer object
140 *
141 * This function can be used to set &drm_framebuffer_funcs for drivers that need
142 * custom framebuffer callbacks. Use drm_gem_fb_create() if you don't need to
143 * change &drm_framebuffer_funcs. The function does buffer size validation.
144 * The buffer size validation is for a general case, though, so users should
145 * pay attention to the checks being appropriate for them or, at least,
146 * non-conflicting.
147 *
148 * Returns:
149 * Zero or a negative error code.
150 */
151int drm_gem_fb_init_with_funcs(struct drm_device *dev,
152			       struct drm_framebuffer *fb,
153			       struct drm_file *file,
154			       const struct drm_mode_fb_cmd2 *mode_cmd,
155			       const struct drm_framebuffer_funcs *funcs)
156{
157	const struct drm_format_info *info;
158	struct drm_gem_object *objs[DRM_FORMAT_MAX_PLANES];
159	unsigned int i;
160	int ret;
161
162	info = drm_get_format_info(dev, mode_cmd);
163	if (!info) {
164		drm_dbg_kms(dev, "Failed to get FB format info\n");
165		return -EINVAL;
166	}
167
168	if (drm_drv_uses_atomic_modeset(dev) &&
169	    !drm_any_plane_has_format(dev, mode_cmd->pixel_format,
170				      mode_cmd->modifier[0])) {
171		drm_dbg_kms(dev, "Unsupported pixel format %p4cc / modifier 0x%llx\n",
172			    &mode_cmd->pixel_format, mode_cmd->modifier[0]);
173		return -EINVAL;
174	}
175
176	for (i = 0; i < info->num_planes; i++) {
177		unsigned int width = mode_cmd->width / (i ? info->hsub : 1);
178		unsigned int height = mode_cmd->height / (i ? info->vsub : 1);
179		unsigned int min_size;
180
181		objs[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
182		if (!objs[i]) {
183			drm_dbg_kms(dev, "Failed to lookup GEM object\n");
184			ret = -ENOENT;
185			goto err_gem_object_put;
186		}
187
188		min_size = (height - 1) * mode_cmd->pitches[i]
189			 + drm_format_info_min_pitch(info, i, width)
190			 + mode_cmd->offsets[i];
191
192		if (objs[i]->size < min_size) {
193			drm_dbg_kms(dev,
194				    "GEM object size (%zu) smaller than minimum size (%u) for plane %d\n",
195				    objs[i]->size, min_size, i);
196			drm_gem_object_put(objs[i]);
197			ret = -EINVAL;
198			goto err_gem_object_put;
199		}
200	}
201
202	ret = drm_gem_fb_init(dev, fb, mode_cmd, objs, i, funcs);
203	if (ret)
 
204		goto err_gem_object_put;
205
206	return 0;
207
208err_gem_object_put:
209	while (i > 0) {
210		--i;
211		drm_gem_object_put(objs[i]);
212	}
213	return ret;
214}
215EXPORT_SYMBOL_GPL(drm_gem_fb_init_with_funcs);
216
217/**
218 * drm_gem_fb_create_with_funcs() - Helper function for the
219 *                                  &drm_mode_config_funcs.fb_create
220 *                                  callback
221 * @dev: DRM device
222 * @file: DRM file that holds the GEM handle(s) backing the framebuffer
223 * @mode_cmd: Metadata from the userspace framebuffer creation request
224 * @funcs: vtable to be used for the new framebuffer object
225 *
226 * This function can be used to set &drm_framebuffer_funcs for drivers that need
227 * custom framebuffer callbacks. Use drm_gem_fb_create() if you don't need to
228 * change &drm_framebuffer_funcs. The function does buffer size validation.
229 *
230 * Returns:
231 * Pointer to a &drm_framebuffer on success or an error pointer on failure.
232 */
233struct drm_framebuffer *
234drm_gem_fb_create_with_funcs(struct drm_device *dev, struct drm_file *file,
235			     const struct drm_mode_fb_cmd2 *mode_cmd,
236			     const struct drm_framebuffer_funcs *funcs)
237{
238	struct drm_framebuffer *fb;
239	int ret;
240
241	fb = kzalloc(sizeof(*fb), GFP_KERNEL);
242	if (!fb)
243		return ERR_PTR(-ENOMEM);
244
245	ret = drm_gem_fb_init_with_funcs(dev, fb, file, mode_cmd, funcs);
246	if (ret) {
247		kfree(fb);
248		return ERR_PTR(ret);
249	}
250
251	return fb;
252}
253EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_funcs);
254
255static const struct drm_framebuffer_funcs drm_gem_fb_funcs = {
256	.destroy	= drm_gem_fb_destroy,
257	.create_handle	= drm_gem_fb_create_handle,
258};
259
260/**
261 * drm_gem_fb_create() - Helper function for the
262 *                       &drm_mode_config_funcs.fb_create callback
263 * @dev: DRM device
264 * @file: DRM file that holds the GEM handle(s) backing the framebuffer
265 * @mode_cmd: Metadata from the userspace framebuffer creation request
266 *
267 * This function creates a new framebuffer object described by
268 * &drm_mode_fb_cmd2. This description includes handles for the buffer(s)
269 * backing the framebuffer.
270 *
271 * If your hardware has special alignment or pitch requirements these should be
272 * checked before calling this function. The function does buffer size
273 * validation. Use drm_gem_fb_create_with_dirty() if you need framebuffer
274 * flushing.
275 *
276 * Drivers can use this as their &drm_mode_config_funcs.fb_create callback.
277 * The ADDFB2 IOCTL calls into this callback.
278 *
279 * Returns:
280 * Pointer to a &drm_framebuffer on success or an error pointer on failure.
281 */
282struct drm_framebuffer *
283drm_gem_fb_create(struct drm_device *dev, struct drm_file *file,
284		  const struct drm_mode_fb_cmd2 *mode_cmd)
285{
286	return drm_gem_fb_create_with_funcs(dev, file, mode_cmd,
287					    &drm_gem_fb_funcs);
288}
289EXPORT_SYMBOL_GPL(drm_gem_fb_create);
290
291static const struct drm_framebuffer_funcs drm_gem_fb_funcs_dirtyfb = {
292	.destroy	= drm_gem_fb_destroy,
293	.create_handle	= drm_gem_fb_create_handle,
294	.dirty		= drm_atomic_helper_dirtyfb,
295};
296
297/**
298 * drm_gem_fb_create_with_dirty() - Helper function for the
299 *                       &drm_mode_config_funcs.fb_create callback
300 * @dev: DRM device
301 * @file: DRM file that holds the GEM handle(s) backing the framebuffer
302 * @mode_cmd: Metadata from the userspace framebuffer creation request
303 *
304 * This function creates a new framebuffer object described by
305 * &drm_mode_fb_cmd2. This description includes handles for the buffer(s)
306 * backing the framebuffer. drm_atomic_helper_dirtyfb() is used for the dirty
307 * callback giving framebuffer flushing through the atomic machinery. Use
308 * drm_gem_fb_create() if you don't need the dirty callback.
309 * The function does buffer size validation.
310 *
311 * Drivers should also call drm_plane_enable_fb_damage_clips() on all planes
312 * to enable userspace to use damage clips also with the ATOMIC IOCTL.
313 *
314 * Drivers can use this as their &drm_mode_config_funcs.fb_create callback.
315 * The ADDFB2 IOCTL calls into this callback.
316 *
317 * Returns:
318 * Pointer to a &drm_framebuffer on success or an error pointer on failure.
319 */
320struct drm_framebuffer *
321drm_gem_fb_create_with_dirty(struct drm_device *dev, struct drm_file *file,
322			     const struct drm_mode_fb_cmd2 *mode_cmd)
323{
324	return drm_gem_fb_create_with_funcs(dev, file, mode_cmd,
325					    &drm_gem_fb_funcs_dirtyfb);
326}
327EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_dirty);
328
329/**
330 * drm_gem_fb_vmap - maps all framebuffer BOs into kernel address space
331 * @fb: the framebuffer
332 * @map: returns the mapping's address for each BO
333 * @data: returns the data address for each BO, can be NULL
334 *
335 * This function maps all buffer objects of the given framebuffer into
336 * kernel address space and stores them in struct iosys_map. If the
337 * mapping operation fails for one of the BOs, the function unmaps the
338 * already established mappings automatically.
339 *
340 * Callers that want to access a BO's stored data should pass @data.
341 * The argument returns the addresses of the data stored in each BO. This
342 * is different from @map if the framebuffer's offsets field is non-zero.
343 *
344 * Both, @map and @data, must each refer to arrays with at least
345 * fb->format->num_planes elements.
346 *
347 * See drm_gem_fb_vunmap() for unmapping.
348 *
349 * Returns:
350 * 0 on success, or a negative errno code otherwise.
351 */
352int drm_gem_fb_vmap(struct drm_framebuffer *fb, struct iosys_map *map,
353		    struct iosys_map *data)
354{
355	struct drm_gem_object *obj;
356	unsigned int i;
357	int ret;
358
359	for (i = 0; i < fb->format->num_planes; ++i) {
360		obj = drm_gem_fb_get_obj(fb, i);
361		if (!obj) {
362			ret = -EINVAL;
363			goto err_drm_gem_vunmap;
364		}
365		ret = drm_gem_vmap_unlocked(obj, &map[i]);
366		if (ret)
367			goto err_drm_gem_vunmap;
368	}
369
370	if (data) {
371		for (i = 0; i < fb->format->num_planes; ++i) {
372			memcpy(&data[i], &map[i], sizeof(data[i]));
373			if (iosys_map_is_null(&data[i]))
374				continue;
375			iosys_map_incr(&data[i], fb->offsets[i]);
376		}
377	}
378
379	return 0;
380
381err_drm_gem_vunmap:
382	while (i) {
383		--i;
384		obj = drm_gem_fb_get_obj(fb, i);
385		if (!obj)
386			continue;
387		drm_gem_vunmap_unlocked(obj, &map[i]);
388	}
389	return ret;
390}
391EXPORT_SYMBOL(drm_gem_fb_vmap);
392
393/**
394 * drm_gem_fb_vunmap - unmaps framebuffer BOs from kernel address space
395 * @fb: the framebuffer
396 * @map: mapping addresses as returned by drm_gem_fb_vmap()
397 *
398 * This function unmaps all buffer objects of the given framebuffer.
399 *
400 * See drm_gem_fb_vmap() for more information.
401 */
402void drm_gem_fb_vunmap(struct drm_framebuffer *fb, struct iosys_map *map)
403{
404	unsigned int i = fb->format->num_planes;
405	struct drm_gem_object *obj;
406
407	while (i) {
408		--i;
409		obj = drm_gem_fb_get_obj(fb, i);
410		if (!obj)
411			continue;
412		if (iosys_map_is_null(&map[i]))
413			continue;
414		drm_gem_vunmap_unlocked(obj, &map[i]);
415	}
416}
417EXPORT_SYMBOL(drm_gem_fb_vunmap);
418
419static void __drm_gem_fb_end_cpu_access(struct drm_framebuffer *fb, enum dma_data_direction dir,
420					unsigned int num_planes)
421{
422	struct dma_buf_attachment *import_attach;
423	struct drm_gem_object *obj;
424	int ret;
425
426	while (num_planes) {
427		--num_planes;
428		obj = drm_gem_fb_get_obj(fb, num_planes);
429		if (!obj)
430			continue;
431		import_attach = obj->import_attach;
432		if (!import_attach)
433			continue;
434		ret = dma_buf_end_cpu_access(import_attach->dmabuf, dir);
435		if (ret)
436			drm_err(fb->dev, "dma_buf_end_cpu_access(%u, %d) failed: %d\n",
437				ret, num_planes, dir);
438	}
439}
440
441/**
442 * drm_gem_fb_begin_cpu_access - prepares GEM buffer objects for CPU access
443 * @fb: the framebuffer
444 * @dir: access mode
445 *
446 * Prepares a framebuffer's GEM buffer objects for CPU access. This function
447 * must be called before accessing the BO data within the kernel. For imported
448 * BOs, the function calls dma_buf_begin_cpu_access().
449 *
450 * See drm_gem_fb_end_cpu_access() for signalling the end of CPU access.
451 *
452 * Returns:
453 * 0 on success, or a negative errno code otherwise.
454 */
455int drm_gem_fb_begin_cpu_access(struct drm_framebuffer *fb, enum dma_data_direction dir)
456{
457	struct dma_buf_attachment *import_attach;
458	struct drm_gem_object *obj;
459	unsigned int i;
460	int ret;
461
462	for (i = 0; i < fb->format->num_planes; ++i) {
463		obj = drm_gem_fb_get_obj(fb, i);
464		if (!obj) {
465			ret = -EINVAL;
466			goto err___drm_gem_fb_end_cpu_access;
467		}
468		import_attach = obj->import_attach;
469		if (!import_attach)
470			continue;
471		ret = dma_buf_begin_cpu_access(import_attach->dmabuf, dir);
472		if (ret)
473			goto err___drm_gem_fb_end_cpu_access;
474	}
475
476	return 0;
477
478err___drm_gem_fb_end_cpu_access:
479	__drm_gem_fb_end_cpu_access(fb, dir, i);
480	return ret;
481}
482EXPORT_SYMBOL(drm_gem_fb_begin_cpu_access);
483
484/**
485 * drm_gem_fb_end_cpu_access - signals end of CPU access to GEM buffer objects
486 * @fb: the framebuffer
487 * @dir: access mode
488 *
489 * Signals the end of CPU access to the given framebuffer's GEM buffer objects. This
490 * function must be paired with a corresponding call to drm_gem_fb_begin_cpu_access().
491 * For imported BOs, the function calls dma_buf_end_cpu_access().
492 *
493 * See also drm_gem_fb_begin_cpu_access().
494 */
495void drm_gem_fb_end_cpu_access(struct drm_framebuffer *fb, enum dma_data_direction dir)
496{
497	__drm_gem_fb_end_cpu_access(fb, dir, fb->format->num_planes);
498}
499EXPORT_SYMBOL(drm_gem_fb_end_cpu_access);
500
501// TODO Drop this function and replace by drm_format_info_bpp() once all
502// DRM_FORMAT_* provide proper block info in drivers/gpu/drm/drm_fourcc.c
503static __u32 drm_gem_afbc_get_bpp(struct drm_device *dev,
504				  const struct drm_mode_fb_cmd2 *mode_cmd)
505{
506	const struct drm_format_info *info;
507
508	info = drm_get_format_info(dev, mode_cmd);
509
510	switch (info->format) {
511	case DRM_FORMAT_YUV420_8BIT:
512		return 12;
513	case DRM_FORMAT_YUV420_10BIT:
514		return 15;
515	case DRM_FORMAT_VUY101010:
516		return 30;
517	default:
518		return drm_format_info_bpp(info, 0);
519	}
520}
521
522static int drm_gem_afbc_min_size(struct drm_device *dev,
523				 const struct drm_mode_fb_cmd2 *mode_cmd,
524				 struct drm_afbc_framebuffer *afbc_fb)
525{
526	__u32 n_blocks, w_alignment, h_alignment, hdr_alignment;
527	/* remove bpp when all users properly encode cpp in drm_format_info */
528	__u32 bpp;
529
530	switch (mode_cmd->modifier[0] & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK) {
531	case AFBC_FORMAT_MOD_BLOCK_SIZE_16x16:
532		afbc_fb->block_width = 16;
533		afbc_fb->block_height = 16;
534		break;
535	case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8:
536		afbc_fb->block_width = 32;
537		afbc_fb->block_height = 8;
538		break;
539	/* no user exists yet - fall through */
540	case AFBC_FORMAT_MOD_BLOCK_SIZE_64x4:
541	case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8_64x4:
542	default:
543		drm_dbg_kms(dev, "Invalid AFBC_FORMAT_MOD_BLOCK_SIZE: %lld.\n",
544			    mode_cmd->modifier[0]
545			    & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK);
546		return -EINVAL;
547	}
548
549	/* tiled header afbc */
550	w_alignment = afbc_fb->block_width;
551	h_alignment = afbc_fb->block_height;
552	hdr_alignment = AFBC_HDR_ALIGN;
553	if (mode_cmd->modifier[0] & AFBC_FORMAT_MOD_TILED) {
554		w_alignment *= AFBC_TH_LAYOUT_ALIGNMENT;
555		h_alignment *= AFBC_TH_LAYOUT_ALIGNMENT;
556		hdr_alignment = AFBC_TH_BODY_START_ALIGNMENT;
557	}
558
559	afbc_fb->aligned_width = ALIGN(mode_cmd->width, w_alignment);
560	afbc_fb->aligned_height = ALIGN(mode_cmd->height, h_alignment);
561	afbc_fb->offset = mode_cmd->offsets[0];
562
563	bpp = drm_gem_afbc_get_bpp(dev, mode_cmd);
564	if (!bpp) {
565		drm_dbg_kms(dev, "Invalid AFBC bpp value: %d\n", bpp);
566		return -EINVAL;
567	}
568
569	n_blocks = (afbc_fb->aligned_width * afbc_fb->aligned_height)
570		   / AFBC_SUPERBLOCK_PIXELS;
571	afbc_fb->afbc_size = ALIGN(n_blocks * AFBC_HEADER_SIZE, hdr_alignment);
572	afbc_fb->afbc_size += n_blocks * ALIGN(bpp * AFBC_SUPERBLOCK_PIXELS / 8,
573					       AFBC_SUPERBLOCK_ALIGNMENT);
574
575	return 0;
576}
 
577
578/**
579 * drm_gem_fb_afbc_init() - Helper function for drivers using afbc to
580 *			    fill and validate all the afbc-specific
581 *			    struct drm_afbc_framebuffer members
582 *
583 * @dev: DRM device
584 * @afbc_fb: afbc-specific framebuffer
585 * @mode_cmd: Metadata from the userspace framebuffer creation request
586 * @afbc_fb: afbc framebuffer
 
 
587 *
588 * This function can be used by drivers which support afbc to complete
589 * the preparation of struct drm_afbc_framebuffer. It must be called after
590 * allocating the said struct and calling drm_gem_fb_init_with_funcs().
591 * It is caller's responsibility to put afbc_fb->base.obj objects in case
592 * the call is unsuccessful.
593 *
594 * Returns:
595 * Zero on success or a negative error value on failure.
596 */
597int drm_gem_fb_afbc_init(struct drm_device *dev,
598			 const struct drm_mode_fb_cmd2 *mode_cmd,
599			 struct drm_afbc_framebuffer *afbc_fb)
600{
601	const struct drm_format_info *info;
602	struct drm_gem_object **objs;
603	int ret;
604
605	objs = afbc_fb->base.obj;
606	info = drm_get_format_info(dev, mode_cmd);
607	if (!info)
608		return -EINVAL;
609
610	ret = drm_gem_afbc_min_size(dev, mode_cmd, afbc_fb);
611	if (ret < 0)
612		return ret;
 
 
 
613
614	if (objs[0]->size < afbc_fb->afbc_size)
615		return -EINVAL;
616
617	return 0;
618}
619EXPORT_SYMBOL_GPL(drm_gem_fb_afbc_init);
v4.17
 
  1/*
  2 * drm gem framebuffer helper functions
  3 *
  4 * Copyright (C) 2017 Noralf Trønnes
  5 *
  6 * This program is free software; you can redistribute it and/or modify
  7 * it under the terms of the GNU General Public License as published by
  8 * the Free Software Foundation; either version 2 of the License, or
  9 * (at your option) any later version.
 10 */
 11
 12#include <linux/dma-buf.h>
 13#include <linux/dma-fence.h>
 14#include <linux/reservation.h>
 15#include <linux/slab.h>
 
 16
 17#include <drm/drmP.h>
 18#include <drm/drm_atomic.h>
 19#include <drm/drm_fb_helper.h>
 20#include <drm/drm_fourcc.h>
 21#include <drm/drm_framebuffer.h>
 22#include <drm/drm_gem.h>
 23#include <drm/drm_gem_framebuffer_helper.h>
 24#include <drm/drm_modeset_helper.h>
 25
 
 
 
 
 
 
 
 
 
 
 
 26/**
 27 * DOC: overview
 28 *
 29 * This library provides helpers for drivers that don't subclass
 30 * &drm_framebuffer and use &drm_gem_object for their backing storage.
 31 *
 32 * Drivers without additional needs to validate framebuffers can simply use
 33 * drm_gem_fb_create() and everything is wired up automatically. Other drivers
 34 * can use all parts independently.
 35 */
 36
 37/**
 38 * drm_gem_fb_get_obj() - Get GEM object backing the framebuffer
 39 * @fb: Framebuffer
 40 * @plane: Plane index
 41 *
 42 * No additional reference is taken beyond the one that the &drm_frambuffer
 43 * already holds.
 44 *
 45 * Returns:
 46 * Pointer to &drm_gem_object for the given framebuffer and plane index or NULL
 47 * if it does not exist.
 48 */
 49struct drm_gem_object *drm_gem_fb_get_obj(struct drm_framebuffer *fb,
 50					  unsigned int plane)
 51{
 52	if (plane >= 4)
 
 
 
 
 53		return NULL;
 54
 55	return fb->obj[plane];
 56}
 57EXPORT_SYMBOL_GPL(drm_gem_fb_get_obj);
 58
 59static struct drm_framebuffer *
 60drm_gem_fb_alloc(struct drm_device *dev,
 
 61		 const struct drm_mode_fb_cmd2 *mode_cmd,
 62		 struct drm_gem_object **obj, unsigned int num_planes,
 63		 const struct drm_framebuffer_funcs *funcs)
 64{
 65	struct drm_framebuffer *fb;
 66	int ret, i;
 67
 68	fb = kzalloc(sizeof(*fb), GFP_KERNEL);
 69	if (!fb)
 70		return ERR_PTR(-ENOMEM);
 71
 72	drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
 73
 74	for (i = 0; i < num_planes; i++)
 75		fb->obj[i] = obj[i];
 76
 77	ret = drm_framebuffer_init(dev, fb, funcs);
 78	if (ret) {
 79		DRM_DEV_ERROR(dev->dev, "Failed to init framebuffer: %d\n",
 80			      ret);
 81		kfree(fb);
 82		return ERR_PTR(ret);
 83	}
 84
 85	return fb;
 86}
 87
 88/**
 89 * drm_gem_fb_destroy - Free GEM backed framebuffer
 90 * @fb: Framebuffer
 91 *
 92 * Frees a GEM backed framebuffer with its backing buffer(s) and the structure
 93 * itself. Drivers can use this as their &drm_framebuffer_funcs->destroy
 94 * callback.
 95 */
 96void drm_gem_fb_destroy(struct drm_framebuffer *fb)
 97{
 98	int i;
 99
100	for (i = 0; i < 4; i++)
101		drm_gem_object_put_unlocked(fb->obj[i]);
102
103	drm_framebuffer_cleanup(fb);
104	kfree(fb);
105}
106EXPORT_SYMBOL(drm_gem_fb_destroy);
107
108/**
109 * drm_gem_fb_create_handle - Create handle for GEM backed framebuffer
110 * @fb: Framebuffer
111 * @file: DRM file to register the handle for
112 * @handle: Pointer to return the created handle
113 *
114 * This function creates a handle for the GEM object backing the framebuffer.
115 * Drivers can use this as their &drm_framebuffer_funcs->create_handle
116 * callback. The GETFB IOCTL calls into this callback.
117 *
118 * Returns:
119 * 0 on success or a negative error code on failure.
120 */
121int drm_gem_fb_create_handle(struct drm_framebuffer *fb, struct drm_file *file,
122			     unsigned int *handle)
123{
124	return drm_gem_handle_create(file, fb->obj[0], handle);
125}
126EXPORT_SYMBOL(drm_gem_fb_create_handle);
127
128/**
129 * drm_gem_fb_create_with_funcs() - Helper function for the
130 *                                  &drm_mode_config_funcs.fb_create
131 *                                  callback
 
 
132 * @dev: DRM device
 
133 * @file: DRM file that holds the GEM handle(s) backing the framebuffer
134 * @mode_cmd: Metadata from the userspace framebuffer creation request
135 * @funcs: vtable to be used for the new framebuffer object
136 *
137 * This can be used to set &drm_framebuffer_funcs for drivers that need the
138 * &drm_framebuffer_funcs.dirty callback. Use drm_gem_fb_create() if you don't
139 * need to change &drm_framebuffer_funcs.
140 * The function does buffer size validation.
 
 
141 *
142 * Returns:
143 * Pointer to a &drm_framebuffer on success or an error pointer on failure.
144 */
145struct drm_framebuffer *
146drm_gem_fb_create_with_funcs(struct drm_device *dev, struct drm_file *file,
147			     const struct drm_mode_fb_cmd2 *mode_cmd,
148			     const struct drm_framebuffer_funcs *funcs)
 
149{
150	const struct drm_format_info *info;
151	struct drm_gem_object *objs[4];
152	struct drm_framebuffer *fb;
153	int ret, i;
154
155	info = drm_get_format_info(dev, mode_cmd);
156	if (!info)
157		return ERR_PTR(-EINVAL);
 
 
 
 
 
 
 
 
 
 
158
159	for (i = 0; i < info->num_planes; i++) {
160		unsigned int width = mode_cmd->width / (i ? info->hsub : 1);
161		unsigned int height = mode_cmd->height / (i ? info->vsub : 1);
162		unsigned int min_size;
163
164		objs[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
165		if (!objs[i]) {
166			DRM_DEBUG_KMS("Failed to lookup GEM object\n");
167			ret = -ENOENT;
168			goto err_gem_object_put;
169		}
170
171		min_size = (height - 1) * mode_cmd->pitches[i]
172			 + width * info->cpp[i]
173			 + mode_cmd->offsets[i];
174
175		if (objs[i]->size < min_size) {
176			drm_gem_object_put_unlocked(objs[i]);
 
 
 
177			ret = -EINVAL;
178			goto err_gem_object_put;
179		}
180	}
181
182	fb = drm_gem_fb_alloc(dev, mode_cmd, objs, i, funcs);
183	if (IS_ERR(fb)) {
184		ret = PTR_ERR(fb);
185		goto err_gem_object_put;
 
 
 
 
 
 
 
186	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
188	return fb;
 
 
189
190err_gem_object_put:
191	for (i--; i >= 0; i--)
192		drm_gem_object_put_unlocked(objs[i]);
 
 
193
194	return ERR_PTR(ret);
195}
196EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_funcs);
197
198static const struct drm_framebuffer_funcs drm_gem_fb_funcs = {
199	.destroy	= drm_gem_fb_destroy,
200	.create_handle	= drm_gem_fb_create_handle,
201};
202
203/**
204 * drm_gem_fb_create() - Helper function for the
205 *                       &drm_mode_config_funcs.fb_create callback
206 * @dev: DRM device
207 * @file: DRM file that holds the GEM handle(s) backing the framebuffer
208 * @mode_cmd: Metadata from the userspace framebuffer creation request
209 *
210 * This function creates a new framebuffer object described by
211 * &drm_mode_fb_cmd2. This description includes handles for the buffer(s)
212 * backing the framebuffer.
213 *
214 * If your hardware has special alignment or pitch requirements these should be
215 * checked before calling this function. The function does buffer size
216 * validation. Use drm_gem_fb_create_with_funcs() if you need to set
217 * &drm_framebuffer_funcs.dirty.
218 *
219 * Drivers can use this as their &drm_mode_config_funcs.fb_create callback.
220 * The ADDFB2 IOCTL calls into this callback.
221 *
222 * Returns:
223 * Pointer to a &drm_framebuffer on success or an error pointer on failure.
224 */
225struct drm_framebuffer *
226drm_gem_fb_create(struct drm_device *dev, struct drm_file *file,
227		  const struct drm_mode_fb_cmd2 *mode_cmd)
228{
229	return drm_gem_fb_create_with_funcs(dev, file, mode_cmd,
230					    &drm_gem_fb_funcs);
231}
232EXPORT_SYMBOL_GPL(drm_gem_fb_create);
233
 
 
 
 
 
 
234/**
235 * drm_gem_fb_prepare_fb() - Prepare a GEM backed framebuffer
236 * @plane: Plane
237 * @state: Plane state the fence will be attached to
238 *
239 * This function prepares a GEM backed framebuffer for scanout by checking if
240 * the plane framebuffer has a DMA-BUF attached. If it does, it extracts the
241 * exclusive fence and attaches it to the plane state for the atomic helper to
242 * wait on. This function can be used as the &drm_plane_helper_funcs.prepare_fb
243 * callback.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244 *
245 * There is no need for &drm_plane_helper_funcs.cleanup_fb hook for simple
246 * gem based framebuffer drivers which have their buffers always pinned in
247 * memory.
248 */
249int drm_gem_fb_prepare_fb(struct drm_plane *plane,
250			  struct drm_plane_state *state)
251{
252	struct dma_buf *dma_buf;
253	struct dma_fence *fence;
254
255	if (plane->state->fb == state->fb || !state->fb)
256		return 0;
257
258	dma_buf = drm_gem_fb_get_obj(state->fb, 0)->dma_buf;
259	if (dma_buf) {
260		fence = reservation_object_get_excl_rcu(dma_buf->resv);
261		drm_atomic_set_fence_for_plane(state, fence);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
262	}
263
 
 
 
 
 
 
264	return 0;
265}
266EXPORT_SYMBOL_GPL(drm_gem_fb_prepare_fb);
267
268/**
269 * drm_gem_fbdev_fb_create - Create a GEM backed &drm_framebuffer for fbdev
270 *                           emulation
 
 
271 * @dev: DRM device
272 * @sizes: fbdev size description
273 * @pitch_align: Optional pitch alignment
274 * @obj: GEM object backing the framebuffer
275 * @funcs: Optional vtable to be used for the new framebuffer object when the
276 *         dirty callback is needed.
277 *
278 * This function creates a framebuffer from a &drm_fb_helper_surface_size
279 * description for use in the &drm_fb_helper_funcs.fb_probe callback.
 
 
 
280 *
281 * Returns:
282 * Pointer to a &drm_framebuffer on success or an error pointer on failure.
283 */
284struct drm_framebuffer *
285drm_gem_fbdev_fb_create(struct drm_device *dev,
286			struct drm_fb_helper_surface_size *sizes,
287			unsigned int pitch_align, struct drm_gem_object *obj,
288			const struct drm_framebuffer_funcs *funcs)
289{
290	struct drm_mode_fb_cmd2 mode_cmd = { 0 };
291
292	mode_cmd.width = sizes->surface_width;
293	mode_cmd.height = sizes->surface_height;
294	mode_cmd.pitches[0] = sizes->surface_width *
295			      DIV_ROUND_UP(sizes->surface_bpp, 8);
296	if (pitch_align)
297		mode_cmd.pitches[0] = roundup(mode_cmd.pitches[0],
298					      pitch_align);
299	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
300							sizes->surface_depth);
301	if (obj->size < mode_cmd.pitches[0] * mode_cmd.height)
302		return ERR_PTR(-EINVAL);
303
304	if (!funcs)
305		funcs = &drm_gem_fb_funcs;
306
307	return drm_gem_fb_alloc(dev, &mode_cmd, &obj, 1, funcs);
308}
309EXPORT_SYMBOL(drm_gem_fbdev_fb_create);