857 lines · 24.0 KB · C
RawDownload
/* * Copyright (c) 2016 Intel Corporation * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that * the above copyright notice appear in all copies and that both that copyright * notice and this permission notice appear in supporting documentation, and * that the name of the copyright holders not be used in advertising or * publicity pertaining to distribution of the software without specific, * written prior permission.  The copyright holders make no representations * about the suitability of this software for any purpose.  It is provided "as * is" without express or implied warranty. * * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THIS SOFTWARE. */ #include <linux/export.h>#include <drm/drmP.h>#include <drm/drm_auth.h>#include <drm/drm_framebuffer.h> #include "drm_crtc_internal.h" /** * DOC: overview * * Frame buffers are abstract memory objects that provide a source of pixels to * scanout to a CRTC. Applications explicitly request the creation of frame * buffers through the DRM_IOCTL_MODE_ADDFB(2) ioctls and receive an opaque * handle that can be passed to the KMS CRTC control, plane configuration and * page flip functions. * * Frame buffers rely on the underlying memory manager for allocating backing * storage. When creating a frame buffer applications pass a memory handle * (or a list of memory handles for multi-planar formats) through the * struct &drm_mode_fb_cmd2 argument. For drivers using GEM as their userspace * buffer management interface this would be a GEM handle.  Drivers are however * free to use their own backing storage object handles, e.g. vmwgfx directly * exposes special TTM handles to userspace and so expects TTM handles in the * create ioctl and not GEM handles. * * Framebuffers are tracked with struct &drm_framebuffer. They are published * using drm_framebuffer_init() - after calling that function userspace can use * and access the framebuffer object. The helper function * drm_helper_mode_fill_fb_struct() can be used to pre-fill the required * metadata fields. * * The lifetime of a drm framebuffer is controlled with a reference count, * drivers can grab additional references with drm_framebuffer_reference() and * drop them again with drm_framebuffer_unreference(). For driver-private * framebuffers for which the last reference is never dropped (e.g. for the * fbdev framebuffer when the struct struct &drm_framebuffer is embedded into * the fbdev helper struct) drivers can manually clean up a framebuffer at * module unload time with drm_framebuffer_unregister_private(). But doing this * is not recommended, and it's better to have a normal free-standing struct * &drm_framebuffer. */ int drm_framebuffer_check_src_coords(uint32_t src_x, uint32_t src_y,				     uint32_t src_w, uint32_t src_h,				     const struct drm_framebuffer *fb){	unsigned int fb_width, fb_height; 	fb_width = fb->width << 16;	fb_height = fb->height << 16; 	/* Make sure source coordinates are inside the fb. */	if (src_w > fb_width ||	    src_x > fb_width - src_w ||	    src_h > fb_height ||	    src_y > fb_height - src_h) {		DRM_DEBUG_KMS("Invalid source coordinates "			      "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",			      src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,			      src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,			      src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,			      src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);		return -ENOSPC;	} 	return 0;} /** * drm_mode_addfb - add an FB to the graphics configuration * @dev: drm device for the ioctl * @data: data pointer for the ioctl * @file_priv: drm file for the ioctl call * * Add a new FB to the specified CRTC, given a user request. This is the * original addfb ioctl which only supported RGB formats. * * Called by the user via ioctl. * * Returns: * Zero on success, negative errno on failure. */int drm_mode_addfb(struct drm_device *dev,		   void *data, struct drm_file *file_priv){	struct drm_mode_fb_cmd *or = data;	struct drm_mode_fb_cmd2 r = {};	int ret; 	/* convert to new format and call new ioctl */	r.fb_id = or->fb_id;	r.width = or->width;	r.height = or->height;	r.pitches[0] = or->pitch;	r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);	r.handles[0] = or->handle; 	ret = drm_mode_addfb2(dev, &r, file_priv);	if (ret)		return ret; 	or->fb_id = r.fb_id; 	return 0;} static int format_check(const struct drm_mode_fb_cmd2 *r){	uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;	char *format_name; 	switch (format) {	case DRM_FORMAT_C8:	case DRM_FORMAT_RGB332:	case DRM_FORMAT_BGR233:	case DRM_FORMAT_XRGB4444:	case DRM_FORMAT_XBGR4444:	case DRM_FORMAT_RGBX4444:	case DRM_FORMAT_BGRX4444:	case DRM_FORMAT_ARGB4444:	case DRM_FORMAT_ABGR4444:	case DRM_FORMAT_RGBA4444:	case DRM_FORMAT_BGRA4444:	case DRM_FORMAT_XRGB1555:	case DRM_FORMAT_XBGR1555:	case DRM_FORMAT_RGBX5551:	case DRM_FORMAT_BGRX5551:	case DRM_FORMAT_ARGB1555:	case DRM_FORMAT_ABGR1555:	case DRM_FORMAT_RGBA5551:	case DRM_FORMAT_BGRA5551:	case DRM_FORMAT_RGB565:	case DRM_FORMAT_BGR565:	case DRM_FORMAT_RGB888:	case DRM_FORMAT_BGR888:	case DRM_FORMAT_XRGB8888:	case DRM_FORMAT_XBGR8888:	case DRM_FORMAT_RGBX8888:	case DRM_FORMAT_BGRX8888:	case DRM_FORMAT_ARGB8888:	case DRM_FORMAT_ABGR8888:	case DRM_FORMAT_RGBA8888:	case DRM_FORMAT_BGRA8888:	case DRM_FORMAT_XRGB2101010:	case DRM_FORMAT_XBGR2101010:	case DRM_FORMAT_RGBX1010102:	case DRM_FORMAT_BGRX1010102:	case DRM_FORMAT_ARGB2101010:	case DRM_FORMAT_ABGR2101010:	case DRM_FORMAT_RGBA1010102:	case DRM_FORMAT_BGRA1010102:	case DRM_FORMAT_YUYV:	case DRM_FORMAT_YVYU:	case DRM_FORMAT_UYVY:	case DRM_FORMAT_VYUY:	case DRM_FORMAT_AYUV:	case DRM_FORMAT_NV12:	case DRM_FORMAT_NV21:	case DRM_FORMAT_NV16:	case DRM_FORMAT_NV61:	case DRM_FORMAT_NV24:	case DRM_FORMAT_NV42:	case DRM_FORMAT_YUV410:	case DRM_FORMAT_YVU410:	case DRM_FORMAT_YUV411:	case DRM_FORMAT_YVU411:	case DRM_FORMAT_YUV420:	case DRM_FORMAT_YVU420:	case DRM_FORMAT_YUV422:	case DRM_FORMAT_YVU422:	case DRM_FORMAT_YUV444:	case DRM_FORMAT_YVU444:		return 0;	default:		format_name = drm_get_format_name(r->pixel_format);		DRM_DEBUG_KMS("invalid pixel format %s\n", format_name);		kfree(format_name);		return -EINVAL;	}} static int framebuffer_check(const struct drm_mode_fb_cmd2 *r){	int ret, hsub, vsub, num_planes, i; 	ret = format_check(r);	if (ret) {		char *format_name = drm_get_format_name(r->pixel_format);		DRM_DEBUG_KMS("bad framebuffer format %s\n", format_name);		kfree(format_name);		return ret;	} 	hsub = drm_format_horz_chroma_subsampling(r->pixel_format);	vsub = drm_format_vert_chroma_subsampling(r->pixel_format);	num_planes = drm_format_num_planes(r->pixel_format); 	if (r->width == 0 || r->width % hsub) {		DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);		return -EINVAL;	} 	if (r->height == 0 || r->height % vsub) {		DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);		return -EINVAL;	} 	for (i = 0; i < num_planes; i++) {		unsigned int width = r->width / (i != 0 ? hsub : 1);		unsigned int height = r->height / (i != 0 ? vsub : 1);		unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i); 		if (!r->handles[i]) {			DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);			return -EINVAL;		} 		if ((uint64_t) width * cpp > UINT_MAX)			return -ERANGE; 		if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)			return -ERANGE; 		if (r->pitches[i] < width * cpp) {			DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);			return -EINVAL;		} 		if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {			DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",				      r->modifier[i], i);			return -EINVAL;		} 		/* modifier specific checks: */		switch (r->modifier[i]) {		case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:			/* NOTE: the pitch restriction may be lifted later if it turns			 * out that no hw has this restriction:			 */			if (r->pixel_format != DRM_FORMAT_NV12 ||					width % 128 || height % 32 ||					r->pitches[i] % 128) {				DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);				return -EINVAL;			}			break; 		default:			break;		}	} 	for (i = num_planes; i < 4; i++) {		if (r->modifier[i]) {			DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);			return -EINVAL;		} 		/* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */		if (!(r->flags & DRM_MODE_FB_MODIFIERS))			continue; 		if (r->handles[i]) {			DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);			return -EINVAL;		} 		if (r->pitches[i]) {			DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);			return -EINVAL;		} 		if (r->offsets[i]) {			DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);			return -EINVAL;		}	} 	return 0;} struct drm_framebuffer *drm_internal_framebuffer_create(struct drm_device *dev,				const struct drm_mode_fb_cmd2 *r,				struct drm_file *file_priv){	struct drm_mode_config *config = &dev->mode_config;	struct drm_framebuffer *fb;	int ret; 	if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {		DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);		return ERR_PTR(-EINVAL);	} 	if ((config->min_width > r->width) || (r->width > config->max_width)) {		DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",			  r->width, config->min_width, config->max_width);		return ERR_PTR(-EINVAL);	}	if ((config->min_height > r->height) || (r->height > config->max_height)) {		DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",			  r->height, config->min_height, config->max_height);		return ERR_PTR(-EINVAL);	} 	if (r->flags & DRM_MODE_FB_MODIFIERS &&	    !dev->mode_config.allow_fb_modifiers) {		DRM_DEBUG_KMS("driver does not support fb modifiers\n");		return ERR_PTR(-EINVAL);	} 	ret = framebuffer_check(r);	if (ret)		return ERR_PTR(ret); 	fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);	if (IS_ERR(fb)) {		DRM_DEBUG_KMS("could not create framebuffer\n");		return fb;	} 	return fb;} /** * drm_mode_addfb2 - add an FB to the graphics configuration * @dev: drm device for the ioctl * @data: data pointer for the ioctl * @file_priv: drm file for the ioctl call * * Add a new FB to the specified CRTC, given a user request with format. This is * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers * and uses fourcc codes as pixel format specifiers. * * Called by the user via ioctl. * * Returns: * Zero on success, negative errno on failure. */int drm_mode_addfb2(struct drm_device *dev,		    void *data, struct drm_file *file_priv){	struct drm_mode_fb_cmd2 *r = data;	struct drm_framebuffer *fb; 	if (!drm_core_check_feature(dev, DRIVER_MODESET))		return -EINVAL; 	fb = drm_internal_framebuffer_create(dev, r, file_priv);	if (IS_ERR(fb))		return PTR_ERR(fb); 	DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);	r->fb_id = fb->base.id; 	/* Transfer ownership to the filp for reaping on close */	mutex_lock(&file_priv->fbs_lock);	list_add(&fb->filp_head, &file_priv->fbs);	mutex_unlock(&file_priv->fbs_lock); 	return 0;} struct drm_mode_rmfb_work {	struct work_struct work;	struct list_head fbs;}; static void drm_mode_rmfb_work_fn(struct work_struct *w){	struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work); 	while (!list_empty(&arg->fbs)) {		struct drm_framebuffer *fb =			list_first_entry(&arg->fbs, typeof(*fb), filp_head); 		list_del_init(&fb->filp_head);		drm_framebuffer_remove(fb);	}} /** * drm_mode_rmfb - remove an FB from the configuration * @dev: drm device for the ioctl * @data: data pointer for the ioctl * @file_priv: drm file for the ioctl call * * Remove the FB specified by the user. * * Called by the user via ioctl. * * Returns: * Zero on success, negative errno on failure. */int drm_mode_rmfb(struct drm_device *dev,		   void *data, struct drm_file *file_priv){	struct drm_framebuffer *fb = NULL;	struct drm_framebuffer *fbl = NULL;	uint32_t *id = data;	int found = 0; 	if (!drm_core_check_feature(dev, DRIVER_MODESET))		return -EINVAL; 	fb = drm_framebuffer_lookup(dev, *id);	if (!fb)		return -ENOENT; 	mutex_lock(&file_priv->fbs_lock);	list_for_each_entry(fbl, &file_priv->fbs, filp_head)		if (fb == fbl)			found = 1;	if (!found) {		mutex_unlock(&file_priv->fbs_lock);		goto fail_unref;	} 	list_del_init(&fb->filp_head);	mutex_unlock(&file_priv->fbs_lock); 	/* drop the reference we picked up in framebuffer lookup */	drm_framebuffer_unreference(fb); 	/*	 * we now own the reference that was stored in the fbs list	 *	 * drm_framebuffer_remove may fail with -EINTR on pending signals,	 * so run this in a separate stack as there's no way to correctly	 * handle this after the fb is already removed from the lookup table.	 */	if (drm_framebuffer_read_refcount(fb) > 1) {		struct drm_mode_rmfb_work arg; 		INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);		INIT_LIST_HEAD(&arg.fbs);		list_add_tail(&fb->filp_head, &arg.fbs); 		schedule_work(&arg.work);		flush_work(&arg.work);		destroy_work_on_stack(&arg.work);	} else		drm_framebuffer_unreference(fb); 	return 0; fail_unref:	drm_framebuffer_unreference(fb);	return -ENOENT;} /** * drm_mode_getfb - get FB info * @dev: drm device for the ioctl * @data: data pointer for the ioctl * @file_priv: drm file for the ioctl call * * Lookup the FB given its ID and return info about it. * * Called by the user via ioctl. * * Returns: * Zero on success, negative errno on failure. */int drm_mode_getfb(struct drm_device *dev,		   void *data, struct drm_file *file_priv){	struct drm_mode_fb_cmd *r = data;	struct drm_framebuffer *fb;	int ret; 	if (!drm_core_check_feature(dev, DRIVER_MODESET))		return -EINVAL; 	fb = drm_framebuffer_lookup(dev, r->fb_id);	if (!fb)

Lines 1–500 of 857.