1494N/A/*
1494N/A * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
1494N/A */
1494N/A
1494N/A/*
1494N/A * Copyright (c) 2006-2008, 2013, Intel Corporation
1494N/A * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
1494N/A * Copyright (c) 2008 Red Hat Inc.
1494N/A *
1494N/A * DRM core CRTC related functions
1494N/A *
1494N/A * Permission to use, copy, modify, distribute, and sell this software and its
1494N/A * documentation for any purpose is hereby granted without fee, provided that
1494N/A * the above copyright notice appear in all copies and that both that copyright
1494N/A * notice and this permission notice appear in supporting documentation, and
1494N/A * that the name of the copyright holders not be used in advertising or
1494N/A * publicity pertaining to distribution of the software without specific,
1494N/A * written prior permission. The copyright holders make no representations
1494N/A * about the suitability of this software for any purpose. It is provided "as
1494N/A * is" without express or implied warranty.
1494N/A *
1494N/A * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
1494N/A * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
1494N/A * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
1494N/A * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
1494N/A * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
1494N/A * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
1494N/A * OF THIS SOFTWARE.
1494N/A *
1494N/A * Authors:
1494N/A * Keith Packard
1494N/A * Eric Anholt <eric@anholt.net>
1494N/A * Dave Airlie <airlied@linux.ie>
1494N/A * Jesse Barnes <jesse.barnes@intel.com>
1494N/A */
1494N/A
1494N/A#include "drm.h"
1494N/A#include "drmP.h"
1494N/A#include "drm_crtc.h"
1494N/A#include "drm_edid.h"
1494N/A#include "drm_fourcc.h"
1494N/A
1494N/A/**
1494N/A * drm_modeset_lock_all - take all modeset locks
1494N/A * @dev: drm device
1494N/A *
1494N/A * This function takes all modeset locks, suitable where a more fine-grained
1494N/A * scheme isn't (yet) implemented.
1494N/A */
1494N/Avoid drm_modeset_lock_all(struct drm_device *dev)
1494N/A{
1494N/A struct drm_crtc *crtc;
1494N/A
1494N/A mutex_lock(&dev->mode_config.mutex);
1494N/A
1494N/A list_for_each_entry(crtc, struct drm_crtc, &dev->mode_config.crtc_list, head)
1494N/A mutex_lock(&crtc->mutex);
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_modeset_unlock_all - drop all modeset locks
1494N/A * @dev: device
1494N/A */
1494N/Avoid drm_modeset_unlock_all(struct drm_device *dev)
1494N/A{
1494N/A struct drm_crtc *crtc;
1494N/A
1494N/A list_for_each_entry(crtc, struct drm_crtc, &dev->mode_config.crtc_list, head)
1494N/A mutex_unlock(&crtc->mutex);
1494N/A
1494N/A mutex_unlock(&dev->mode_config.mutex);
1494N/A}
1494N/A
1494N/A/* Avoid boilerplate. I'm tired of typing. */
1494N/A#define DRM_ENUM_NAME_FN(fnname, list) \
1494N/A const char *fnname(int val) \
1494N/A { \
1494N/A int i; \
1494N/A for (i = 0; i < ARRAY_SIZE(list); i++) { \
1494N/A if (list[i].type == val) \
1494N/A return list[i].name; \
1494N/A } \
1494N/A return "(unknown)"; \
1494N/A }
1494N/A
1494N/A/*
1494N/A * Global properties
1494N/A */
1494N/Astatic const struct drm_prop_enum_list drm_dpms_enum_list[] =
1494N/A{ { DRM_MODE_DPMS_ON, "On" },
1494N/A { DRM_MODE_DPMS_STANDBY, "Standby" },
1494N/A { DRM_MODE_DPMS_SUSPEND, "Suspend" },
1494N/A { DRM_MODE_DPMS_OFF, "Off" }
1494N/A};
1494N/A
1494N/ADRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
1494N/A
1494N/A/*
1494N/A * Optional properties
1494N/A */
1494N/Astatic const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
1494N/A{
1494N/A { DRM_MODE_SCALE_NONE, "None" },
1494N/A { DRM_MODE_SCALE_FULLSCREEN, "Full" },
1494N/A { DRM_MODE_SCALE_CENTER, "Center" },
1494N/A { DRM_MODE_SCALE_ASPECT, "Full aspect" },
1494N/A};
1494N/A
1494N/Astatic const struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
1494N/A{
1494N/A { DRM_MODE_DITHERING_OFF, "Off" },
1494N/A { DRM_MODE_DITHERING_ON, "On" },
1494N/A { DRM_MODE_DITHERING_AUTO, "Automatic" },
1494N/A};
1494N/A
1494N/A/*
1494N/A * Non-global properties, but "required" for certain connectors.
1494N/A */
1494N/Astatic const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
1494N/A{
1494N/A { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
1494N/A { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
1494N/A { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
1494N/A};
1494N/A
1494N/ADRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
1494N/A
1494N/Astatic const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
1494N/A{
1494N/A { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
1494N/A { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
1494N/A { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
1494N/A};
1494N/A
1494N/ADRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
1494N/A drm_dvi_i_subconnector_enum_list)
1494N/A
1494N/Astatic const struct drm_prop_enum_list drm_tv_select_enum_list[] =
1494N/A{
1494N/A { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
1494N/A { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
1494N/A { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
1494N/A { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
1494N/A { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
1494N/A};
1494N/A
1494N/ADRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
1494N/A
1494N/Astatic const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
1494N/A{
1494N/A { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
1494N/A { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
1494N/A { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
1494N/A { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
1494N/A { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
1494N/A};
1494N/A
1494N/ADRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
1494N/A drm_tv_subconnector_enum_list)
1494N/A
1494N/Astatic const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
1494N/A { DRM_MODE_DIRTY_OFF, "Off" },
1494N/A { DRM_MODE_DIRTY_ON, "On" },
1494N/A { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
1494N/A};
1494N/A
1494N/Astruct drm_conn_prop_enum_list {
1494N/A int type;
1494N/A const char *name;
1494N/A int count;
1494N/A};
1494N/A
1494N/A/*
1494N/A * Connector and encoder types.
1494N/A */
1494N/Astatic struct drm_conn_prop_enum_list drm_connector_enum_list[] =
1494N/A{ { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
1494N/A { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
1494N/A { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
1494N/A { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
1494N/A { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
1494N/A { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
1494N/A { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
1494N/A { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
1494N/A { DRM_MODE_CONNECTOR_Component, "Component", 0 },
1494N/A { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
1494N/A { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
1494N/A { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
1494N/A { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
1494N/A { DRM_MODE_CONNECTOR_TV, "TV", 0 },
1494N/A { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
1494N/A { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
1494N/A};
1494N/A
1494N/Astatic const struct drm_prop_enum_list drm_encoder_enum_list[] =
1494N/A{ { DRM_MODE_ENCODER_NONE, "None" },
1494N/A { DRM_MODE_ENCODER_DAC, "DAC" },
1494N/A { DRM_MODE_ENCODER_TMDS, "TMDS" },
1494N/A { DRM_MODE_ENCODER_LVDS, "LVDS" },
1494N/A { DRM_MODE_ENCODER_TVDAC, "TV" },
1494N/A { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
1494N/A};
1494N/A
1494N/Aconst char *drm_get_encoder_name(const struct drm_encoder *encoder)
1494N/A{
1494N/A static char buf[32];
1494N/A
1494N/A (void) snprintf(buf, 32, "%s-%d",
1494N/A drm_encoder_enum_list[encoder->encoder_type].name,
1494N/A encoder->base.id);
1494N/A return buf;
1494N/A}
1494N/A
1494N/Aconst char *drm_get_connector_name(const struct drm_connector *connector)
1494N/A{
1494N/A static char buf[32];
1494N/A
1494N/A (void) snprintf(buf, 32, "%s-%d",
1494N/A drm_connector_enum_list[connector->connector_type].name,
1494N/A connector->connector_type_id);
1494N/A return buf;
1494N/A}
1494N/A
1494N/Aconst char *drm_get_connector_status_name(enum drm_connector_status status)
1494N/A{
1494N/A if (status == connector_status_connected)
1494N/A return "connected";
1494N/A else if (status == connector_status_disconnected)
1494N/A return "disconnected";
1494N/A else
1494N/A return "unknown";
1494N/A}
1494N/Astatic char printable_char(int c)
1494N/A{
1494N/A return (char)(c);
1494N/A}
1494N/A
1494N/Aconst char *drm_get_format_name(uint32_t format)
1494N/A{
1494N/A static char buf[32];
1494N/A
1494N/A (void) snprintf(buf, sizeof(buf),
1494N/A "%c%c%c%c %s-endian (0x%08x)",
1494N/A printable_char(format & 0xff),
1494N/A printable_char((format >> 8) & 0xff),
1494N/A printable_char((format >> 16) & 0xff),
1494N/A printable_char((format >> 24) & 0x7f),
1494N/A format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
1494N/A format);
1494N/A
1494N/A return buf;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_mode_object_get - allocate a new modeset identifier
1494N/A * @dev: DRM device
1494N/A * @obj: object pointer, used to generate unique ID
1494N/A * @obj_type: object type
1494N/A *
1494N/A * Create a unique identifier based on @ptr in @dev's identifier space. Used
1494N/A * for tracking modes, CRTCs and connectors.
1494N/A *
1494N/A * RETURNS:
1494N/A * New unique (relative to other objects in @dev) integer identifier for the
1494N/A * object.
1494N/A */
1494N/Astatic int drm_mode_object_get(struct drm_device *dev,
1494N/A struct drm_mode_object *obj, uint32_t obj_type)
1494N/A{
1494N/A int new_id = 0;
1494N/A int ret;
1494N/A
1494N/Aagain:
1494N/A if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
1494N/A DRM_ERROR("Ran out memory getting a mode number\n");
1494N/A return -ENOMEM;
1494N/A }
1494N/A
1494N/A mutex_lock(&dev->mode_config.idr_mutex);
1494N/A ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
1494N/A
1494N/A if (!ret) {
1494N/A /*
1494N/A * Set up the object linking under the protection of the idr
1494N/A * lock so that other users can't see inconsistent state.
1494N/A */
1494N/A obj->id = new_id;
1494N/A obj->type = obj_type;
1494N/A }
1494N/A mutex_unlock(&dev->mode_config.idr_mutex);
1494N/A
1494N/A if (ret == -EAGAIN)
1494N/A goto again;
1494N/A return ret;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_mode_object_put - free an identifer
1494N/A * @dev: DRM device
1494N/A * @id: ID to free
1494N/A *
1494N/A *
1494N/A * Free @id from @dev's unique identifier pool.
1494N/A */
1494N/Astatic void drm_mode_object_put(struct drm_device *dev,
1494N/A struct drm_mode_object *object)
1494N/A{
1494N/A mutex_lock(&dev->mode_config.idr_mutex);
1494N/A (void) idr_remove(&dev->mode_config.crtc_idr, object->id);
1494N/A mutex_unlock(&dev->mode_config.idr_mutex);
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_mode_object_find - look up a drm object with static lifetime
1494N/A * @dev: drm device
1494N/A * @id: id of the mode object
1494N/A * @type: type of the mode object
1494N/A *
1494N/A * Note that framebuffers cannot be looked up with this functions - since those
1494N/A * are reference counted, they need special treatment.
1494N/A */
1494N/Astruct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
1494N/A uint32_t id, uint32_t type)
1494N/A{
1494N/A struct drm_mode_object *obj = NULL;
1494N/A
1494N/A /* Framebuffers are reference counted and need their own lookup
1494N/A * function.*/
1494N/A WARN_ON(type == DRM_MODE_OBJECT_FB);
1494N/A
1494N/A mutex_lock(&dev->mode_config.idr_mutex);
1494N/A obj = idr_find(&dev->mode_config.crtc_idr, id);
1494N/A if (!obj || (obj->type != type) || (obj->id != id))
1494N/A obj = NULL;
1494N/A mutex_unlock(&dev->mode_config.idr_mutex);
1494N/A
1494N/A return obj;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_framebuffer_init - initialize a framebuffer
1494N/A * @dev: DRM device
1494N/A *
1494N/A *
1494N/A * Allocates an ID for the framebuffer's parent mode object, sets its mode
1494N/A * functions & device file and adds it to the master fd list.
1494N/A *
1494N/A * IMPORTANT:
1494N/A * This functions publishes the fb and makes it available for concurrent access
1494N/A * by other users. Which means by this point the fb _must_ be fully set up -
1494N/A * since all the fb attributes are invariant over its lifetime, no further
1494N/A * locking but only correct reference counting is required.
1494N/A *
1494N/A * RETURNS:
1494N/A * Zero on success, error code on failure.
1494N/A */
1494N/Aint drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
1494N/A const struct drm_framebuffer_funcs *funcs)
1494N/A{
1494N/A int ret;
1494N/A
1494N/A mutex_lock(&dev->mode_config.fb_lock);
1494N/A kref_init(&fb->refcount);
1494N/A INIT_LIST_HEAD(&fb->filp_head);
1494N/A fb->dev = dev;
1494N/A fb->funcs = funcs;
1494N/A
1494N/A ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
1494N/A if (ret)
1494N/A goto out;
1494N/A
1494N/A /* Grab the idr reference. */
1494N/A drm_framebuffer_reference(fb);
1494N/A
1494N/A dev->mode_config.num_fb++;
1494N/A list_add(&fb->head, &dev->mode_config.fb_list, (caddr_t)fb);
1494N/Aout:
1494N/A mutex_unlock(&dev->mode_config.fb_lock);
1494N/A
1494N/A return 0;
1494N/A}
1494N/A
1494N/Astatic void drm_framebuffer_free(struct kref *kref)
1494N/A{
1494N/A struct drm_framebuffer *fb =
1494N/A container_of(kref, struct drm_framebuffer, refcount);
1494N/A fb->funcs->destroy(fb);
1494N/A}
1494N/A
1494N/Astatic struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
1494N/A uint32_t id)
1494N/A{
1494N/A struct drm_mode_object *obj = NULL;
1494N/A struct drm_framebuffer *fb;
1494N/A
1494N/A mutex_lock(&dev->mode_config.idr_mutex);
1494N/A obj = idr_find(&dev->mode_config.crtc_idr, id);
1494N/A if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
1494N/A fb = NULL;
1494N/A else
1494N/A fb = obj_to_fb(obj);
1494N/A mutex_unlock(&dev->mode_config.idr_mutex);
1494N/A
1494N/A return fb;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
1494N/A * @dev: drm device
1494N/A * @id: id of the fb object
1494N/A *
1494N/A * If successful, this grabs an additional reference to the framebuffer -
1494N/A * callers need to make sure to eventually unreference the returned framebuffer
1494N/A * again.
1494N/A */
1494N/Astruct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
1494N/A uint32_t id)
1494N/A{
1494N/A struct drm_framebuffer *fb;
1494N/A
1494N/A mutex_lock(&dev->mode_config.fb_lock);
1494N/A
1494N/A fb = __drm_framebuffer_lookup(dev, id);
1494N/A if (fb)
1494N/A kref_get(&fb->refcount);
1494N/A
1494N/A mutex_unlock(&dev->mode_config.fb_lock);
1494N/A
1494N/A return fb;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_framebuffer_unreference - unref a framebuffer
1494N/A *
1494N/A */
1494N/Avoid drm_framebuffer_unreference(struct drm_framebuffer *fb)
1494N/A{
1494N/A DRM_DEBUG("FB ID: %d\n", fb->base.id);
1494N/A kref_put(&fb->refcount, drm_framebuffer_free);
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_framebuffer_reference - incr the fb refcnt
1494N/A */
1494N/Avoid drm_framebuffer_reference(struct drm_framebuffer *fb)
1494N/A{
1494N/A DRM_DEBUG("FB ID: %d\n", fb->base.id);
1494N/A kref_get(&fb->refcount);
1494N/A}
1494N/A
1494N/A/* LINTED E_FUNC_ARG_UNUSED */
1494N/Astatic void drm_framebuffer_free_bug(struct kref *kref)
1494N/A{
1494N/A BUG();
1494N/A}
1494N/A
1494N/Astatic void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
1494N/A{
1494N/A DRM_DEBUG("FB ID: %d\n", fb->base.id);
1494N/A kref_put(&fb->refcount, drm_framebuffer_free_bug);
1494N/A}
1494N/A
1494N/A/* dev->mode_config.fb_lock must be held! */
1494N/Astatic void __drm_framebuffer_unregister(struct drm_device *dev,
1494N/A struct drm_framebuffer *fb)
1494N/A{
1494N/A mutex_lock(&dev->mode_config.idr_mutex);
1494N/A (void) idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
1494N/A mutex_unlock(&dev->mode_config.idr_mutex);
1494N/A
1494N/A fb->base.id = 0;
1494N/A
1494N/A __drm_framebuffer_unreference(fb);
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
1494N/A * @fb: fb to unregister
1494N/A *
1494N/A * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
1494N/A * those used for fbdev. Note that the caller must hold a reference of it's own,
1494N/A * i.e. the object may not be destroyed through this call (since it'll lead to a
1494N/A * locking inversion).
1494N/A */
1494N/Avoid drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
1494N/A{
1494N/A struct drm_device *dev = fb->dev;
1494N/A
1494N/A mutex_lock(&dev->mode_config.fb_lock);
1494N/A /* Mark fb as reaped and drop idr ref. */
1494N/A __drm_framebuffer_unregister(dev, fb);
1494N/A mutex_unlock(&dev->mode_config.fb_lock);
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_framebuffer_cleanup - remove a framebuffer object
1494N/A * @fb: framebuffer to remove
1494N/A *
1494N/A *
1494N/A * Cleanup references to a user-created framebuffer. This function is intended
1494N/A * to be used from the drivers ->destroy callback.
1494N/A *
1494N/A * Note that this function does not remove the fb from active usuage - if it is
1494N/A * still used anywhere, hilarity can ensue since userspace could call getfb on
1494N/A * the id and get back -EINVAL. Obviously no concern at driver unload time.
1494N/A *
1494N/A * Also, the framebuffer will not be removed from the lookup idr - for
1494N/A * user-created framebuffers this will happen in in the rmfb ioctl. For
1494N/A * driver-private objects (e.g. for fbdev) drivers need to explicitly call
1494N/A * drm_framebuffer_unregister_private.
1494N/A */
1494N/Avoid drm_framebuffer_cleanup(struct drm_framebuffer *fb)
1494N/A{
1494N/A struct drm_device *dev = fb->dev;
1494N/A mutex_lock(&dev->mode_config.fb_lock);
1494N/A list_del(&fb->head);
1494N/A dev->mode_config.num_fb--;
1494N/A mutex_unlock(&dev->mode_config.fb_lock);
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_framebuffer_remove - remove and unreference a framebuffer object
1494N/A * @fb: framebuffer to remove
1494N/A *
1494N/A * using @fb, removes it, setting it to NULL. Then drops the reference to the
1494N/A * passed-in framebuffer. Might take the modeset locks.
1494N/A *
1494N/A * Note that this function optimizes the cleanup away if the caller holds the
1494N/A * last reference to the framebuffer. It is also guaranteed to not take the
1494N/A * modeset locks in this case.
1494N/A */
1494N/Avoid drm_framebuffer_remove(struct drm_framebuffer *fb)
1494N/A{
1494N/A struct drm_device *dev = fb->dev;
1494N/A struct drm_crtc *crtc;
1494N/A struct drm_plane *plane;
1494N/A struct drm_mode_set set;
1494N/A int ret;
1494N/A
1494N/A WARN_ON(!list_empty(&fb->filp_head));
1494N/A
1494N/A /*
1494N/A * drm ABI mandates that we remove any deleted framebuffers from active
1494N/A * useage. But since most sane clients only remove framebuffers they no
1494N/A * longer need, try to optimize this away.
1494N/A *
1494N/A * Since we're holding a reference ourselves, observing a refcount of 1
1494N/A * means that we're the last holder and can skip it. Also, the refcount
1494N/A * can never increase from 1 again, so we don't need any barriers or
1494N/A * locks.
1494N/A *
1494N/A * Note that userspace could try to race with use and instate a new
1494N/A * usage _after_ we've cleared all current ones. End result will be an
1494N/A * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
1494N/A * in this manner.
1494N/A */
1494N/A if (atomic_read(&fb->refcount.refcount) > 1) {
1494N/A drm_modeset_lock_all(dev);
1494N/A /* remove from any CRTC */
1494N/A list_for_each_entry(crtc, struct drm_crtc, &dev->mode_config.crtc_list, head) {
1494N/A if (crtc->fb == fb) {
1494N/A /* should turn off the crtc */
1494N/A (void) memset(&set, 0, sizeof(struct drm_mode_set));
1494N/A set.crtc = crtc;
1494N/A set.fb = NULL;
1494N/A ret = drm_mode_set_config_internal(&set);
1494N/A if (ret)
1494N/A DRM_ERROR("failed to reset crtc %p when fb was deleted\n", (void *)crtc);
1494N/A }
1494N/A }
1494N/A list_for_each_entry(plane, struct drm_plane, &dev->mode_config.plane_list, head) {
1494N/A if (plane->fb == fb)
1494N/A drm_plane_force_disable(plane);
1494N/A }
1494N/A drm_modeset_unlock_all(dev);
1494N/A }
1494N/A
1494N/A drm_framebuffer_unreference(fb);
1494N/A}
1494N/A
1494N/A
1494N/A/**
1494N/A * drm_crtc_init - Initialise a new CRTC object
1494N/A * @dev: DRM device
1494N/A * @crtc: CRTC object to init
1494N/A * @funcs: callbacks for the new CRTC
1494N/A *
1494N/A *
1494N/A * Inits a new object created as base part of an driver crtc object.
1494N/A */
1494N/Aint drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
1494N/A const struct drm_crtc_funcs *funcs)
1494N/A{
1494N/A int ret;
1494N/A
1494N/A crtc->dev = dev;
1494N/A crtc->funcs = funcs;
1494N/A crtc->invert_dimensions = false;
1494N/A
1494N/A drm_modeset_lock_all(dev);
1494N/A mutex_init(&crtc->mutex, NULL, MUTEX_DRIVER, NULL);
1494N/A mutex_lock(&crtc->mutex);
1494N/A
1494N/A ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
1494N/A if (ret)
1494N/A goto out;
1494N/A
1494N/A crtc->base.properties = &crtc->properties;
1494N/A
1494N/A list_add_tail(&crtc->head, &dev->mode_config.crtc_list, (caddr_t)crtc);
1494N/A dev->mode_config.num_crtc++;
1494N/A
1494N/A out:
1494N/A drm_modeset_unlock_all(dev);
1494N/A
1494N/A return ret;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_crtc_cleanup - Cleans up the core crtc usage.
1494N/A * @crtc: CRTC to cleanup
1494N/A *
1494N/A *
1494N/A * Cleanup @crtc. Removes from drm modesetting space
1494N/A * does NOT free object, caller does that.
1494N/A */
1494N/Avoid drm_crtc_cleanup(struct drm_crtc *crtc)
1494N/A{
1494N/A struct drm_device *dev = crtc->dev;
1494N/A
1494N/A if (crtc->gamma_store) {
1494N/A kfree(crtc->gamma_store, crtc->gamma_size * sizeof(uint16_t) * 3);
1494N/A crtc->gamma_store = NULL;
1494N/A }
1494N/A
1494N/A drm_mode_object_put(dev, &crtc->base);
1494N/A list_del(&crtc->head);
1494N/A dev->mode_config.num_crtc--;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_mode_probed_add - add a mode to a connector's probed mode list
1494N/A * @connector: connector the new mode
1494N/A * @mode: mode data
1494N/A *
1494N/A *
1494N/A * Add @mode to @connector's mode list for later use.
1494N/A */
1494N/Avoid drm_mode_probed_add(struct drm_connector *connector,
1494N/A struct drm_display_mode *mode)
1494N/A{
1494N/A list_add_tail(&mode->head, &connector->probed_modes, (caddr_t)mode);
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_mode_remove - remove and free a mode
1494N/A * @connector: connector list to modify
1494N/A * @mode: mode to remove
1494N/A *
1494N/A *
1494N/A * Remove @mode from @connector's mode list, then free it.
1494N/A */
1494N/Avoid drm_mode_remove(struct drm_connector *connector,
1494N/A struct drm_display_mode *mode)
1494N/A{
1494N/A list_del(&mode->head);
1494N/A drm_mode_destroy(connector->dev, mode);
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_connector_init - Init a preallocated connector
1494N/A * @dev: DRM device
1494N/A * @connector: the connector to init
1494N/A * @funcs: callbacks for this connector
1494N/A * @name: user visible name of the connector
1494N/A *
1494N/A *
1494N/A * Initialises a preallocated connector. Connectors should be
1494N/A * subclassed as part of driver connector objects.
1494N/A *
1494N/A * RETURNS:
1494N/A * Zero on success, error code on failure.
1494N/A */
1494N/Aint drm_connector_init(struct drm_device *dev,
1494N/A struct drm_connector *connector,
1494N/A const struct drm_connector_funcs *funcs,
1494N/A int connector_type)
1494N/A{
1494N/A int ret;
1494N/A
1494N/A drm_modeset_lock_all(dev);
1494N/A
1494N/A ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
1494N/A if (ret)
1494N/A goto out;
1494N/A
1494N/A connector->base.properties = &connector->properties;
1494N/A connector->dev = dev;
1494N/A connector->funcs = funcs;
1494N/A connector->connector_type = connector_type;
1494N/A connector->connector_type_id =
1494N/A ++drm_connector_enum_list[connector_type].count; /* TODO */
1494N/A INIT_LIST_HEAD(&connector->probed_modes);
1494N/A INIT_LIST_HEAD(&connector->modes);
1494N/A connector->edid_blob_ptr = NULL;
1494N/A connector->status = connector_status_unknown;
1494N/A
1494N/A list_add_tail(&connector->head, &dev->mode_config.connector_list, (caddr_t)connector);
1494N/A dev->mode_config.num_connector++;
1494N/A
1494N/A if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
1494N/A drm_object_attach_property(&connector->base,
1494N/A dev->mode_config.edid_property,
1494N/A 0);
1494N/A
1494N/A drm_object_attach_property(&connector->base,
1494N/A dev->mode_config.dpms_property, 0);
1494N/A
1494N/A out:
1494N/A drm_modeset_unlock_all(dev);
1494N/A
1494N/A return ret;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_connector_cleanup - cleans up an initialised connector
1494N/A * @connector: connector to cleanup
1494N/A *
1494N/A *
1494N/A * Cleans up the connector but doesn't free the object.
1494N/A */
1494N/Avoid drm_connector_cleanup(struct drm_connector *connector)
1494N/A{
1494N/A struct drm_device *dev = connector->dev;
1494N/A struct drm_display_mode *mode, *t;
1494N/A
1494N/A list_for_each_entry_safe(mode, t, struct drm_display_mode, &connector->probed_modes, head)
1494N/A drm_mode_remove(connector, mode);
1494N/A
1494N/A list_for_each_entry_safe(mode, t, struct drm_display_mode, &connector->modes, head)
1494N/A drm_mode_remove(connector, mode);
1494N/A
1494N/A drm_mode_object_put(dev, &connector->base);
1494N/A list_del(&connector->head);
1494N/A dev->mode_config.num_connector--;
1494N/A}
1494N/A
1494N/A/* LINTED E_FUNC_ARG_UNUSED */
1494N/Avoid drm_connector_unplug_all(struct drm_device *dev)
1494N/A{
1494N/A /*
1494N/A struct drm_connector *connector;
1494N/A */
1494N/A /* taking the mode config mutex ends up in a clash with sysfs */
1494N/A /*
1494N/A list_for_each_entry(connector, struct drm_connector, &dev->mode_config.connector_list, head)
1494N/A drm_sysfs_connector_remove(connector);
1494N/A */
1494N/A}
1494N/A
1494N/Aint drm_encoder_init(struct drm_device *dev,
1494N/A struct drm_encoder *encoder,
1494N/A const struct drm_encoder_funcs *funcs,
1494N/A int encoder_type)
1494N/A{
1494N/A int ret;
1494N/A drm_modeset_lock_all(dev);
1494N/A
1494N/A ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1494N/A if (ret)
1494N/A goto out;
1494N/A
1494N/A encoder->dev = dev;
1494N/A encoder->encoder_type = encoder_type;
1494N/A encoder->funcs = funcs;
1494N/A
1494N/A list_add_tail(&encoder->head, &dev->mode_config.encoder_list, (caddr_t)encoder);
1494N/A dev->mode_config.num_encoder++;
1494N/A
1494N/A out:
1494N/A drm_modeset_unlock_all(dev);
1494N/A
1494N/A return ret;
1494N/A}
1494N/A
1494N/Avoid drm_encoder_cleanup(struct drm_encoder *encoder)
1494N/A{
1494N/A struct drm_device *dev = encoder->dev;
1494N/A drm_modeset_lock_all(dev);
1494N/A drm_mode_object_put(dev, &encoder->base);
1494N/A list_del(&encoder->head);
1494N/A dev->mode_config.num_encoder--;
1494N/A drm_modeset_unlock_all(dev);
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_plane_init - Initialise a new plane object
1494N/A * @dev: DRM device
1494N/A * @plane: plane object to init
1494N/A * @possible_crtcs: bitmask of possible CRTCs
1494N/A * @funcs: callbacks for the new plane
1494N/A * @formats: array of supported formats (%DRM_FORMAT_*)
1494N/A * @format_count: number of elements in @formats
1494N/A * @priv: plane is private (hidden from userspace)?
1494N/A *
1494N/A * Inits a new object created as base part of a driver plane object.
1494N/A *
1494N/A * RETURNS:
1494N/A * Zero on success, error code on failure.
1494N/A */
1494N/Aint drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1494N/A unsigned long possible_crtcs,
1494N/A const struct drm_plane_funcs *funcs,
1494N/A const uint32_t *formats, uint32_t format_count,
1494N/A bool priv)
1494N/A{
1494N/A int ret;
1494N/A
1494N/A drm_modeset_lock_all(dev);
1494N/A
1494N/A ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1494N/A if (ret)
1494N/A goto out;
1494N/A
1494N/A plane->base.properties = &plane->properties;
1494N/A plane->dev = dev;
1494N/A plane->funcs = funcs;
1494N/A plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1494N/A GFP_KERNEL);
1494N/A if (!plane->format_types) {
1494N/A DRM_DEBUG_KMS("out of memory when allocating plane\n");
1494N/A drm_mode_object_put(dev, &plane->base);
1494N/A ret = -ENOMEM;
1494N/A goto out;
1494N/A }
1494N/A
1494N/A (void) memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1494N/A plane->format_count = format_count;
1494N/A plane->possible_crtcs = (uint32_t) possible_crtcs;
1494N/A
1494N/A /* private planes are not exposed to userspace, but depending on
1494N/A * display hardware, might be convenient to allow sharing programming
1494N/A * for the scanout engine with the crtc implementation.
1494N/A */
1494N/A if (!priv) {
1494N/A list_add_tail(&plane->head, &dev->mode_config.plane_list, (caddr_t)plane);
1494N/A dev->mode_config.num_plane++;
1494N/A } else {
1494N/A INIT_LIST_HEAD(&plane->head);
1494N/A }
1494N/A
1494N/A out:
1494N/A drm_modeset_unlock_all(dev);
1494N/A
1494N/A return ret;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_plane_cleanup - Clean up the core plane usage
1494N/A * @plane: plane to cleanup
1494N/A *
1494N/A * This function cleans up @plane and removes it from the DRM mode setting
1494N/A * core. Note that the function does *not* free the plane structure itself,
1494N/A * this is the responsibility of the caller.
1494N/A */
1494N/Avoid drm_plane_cleanup(struct drm_plane *plane)
1494N/A{
1494N/A struct drm_device *dev = plane->dev;
1494N/A
1494N/A drm_modeset_lock_all(dev);
1494N/A kfree(plane->format_types, sizeof(uint32_t) * plane->format_count);
1494N/A drm_mode_object_put(dev, &plane->base);
1494N/A /* if not added to a list, it must be a private plane */
1494N/A if (!list_empty(&plane->head)) {
1494N/A list_del(&plane->head);
1494N/A dev->mode_config.num_plane--;
1494N/A }
1494N/A drm_modeset_unlock_all(dev);
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_plane_force_disable - Forcibly disable a plane
1494N/A * @plane: plane to disable
1494N/A *
1494N/A * Forces the plane to be disabled.
1494N/A *
1494N/A * Used when the plane's current framebuffer is destroyed,
1494N/A * and when restoring fbdev mode.
1494N/A */
1494N/Avoid drm_plane_force_disable(struct drm_plane *plane)
1494N/A{
1494N/A int ret;
1494N/A
1494N/A if (!plane->fb)
1494N/A return;
1494N/A
1494N/A ret = plane->funcs->disable_plane(plane);
1494N/A if (ret)
1494N/A DRM_ERROR("failed to disable plane with busy fb\n");
1494N/A /* disconnect the plane from the fb and crtc: */
1494N/A __drm_framebuffer_unreference(plane->fb);
1494N/A plane->fb = NULL;
1494N/A plane->crtc = NULL;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_mode_create - create a new display mode
1494N/A * @dev: DRM device
1494N/A *
1494N/A *
1494N/A * Create a new drm_display_mode, give it an ID, and return it.
1494N/A *
1494N/A * RETURNS:
1494N/A * Pointer to new mode on success, NULL on error.
1494N/A */
1494N/Astruct drm_display_mode *drm_mode_create(struct drm_device *dev)
1494N/A{
1494N/A struct drm_display_mode *nmode;
1494N/A
1494N/A nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
1494N/A if (!nmode)
1494N/A return NULL;
1494N/A
1494N/A if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
1494N/A kfree(nmode, sizeof(struct drm_display_mode));
1494N/A return NULL;
1494N/A }
1494N/A
1494N/A return nmode;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_mode_destroy - remove a mode
1494N/A * @dev: DRM device
1494N/A * @mode: mode to remove
1494N/A *
1494N/A *
1494N/A * Free @mode's unique identifier, then free it.
1494N/A */
1494N/Avoid drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
1494N/A{
1494N/A if (!mode)
1494N/A return;
1494N/A
1494N/A drm_mode_object_put(dev, &mode->base);
1494N/A
1494N/A kfree(mode, sizeof(struct drm_display_mode));
1494N/A}
1494N/A
1494N/Astatic int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1494N/A{
1494N/A struct drm_property *edid;
1494N/A struct drm_property *dpms;
1494N/A
1494N/A /*
1494N/A * Standard properties (apply to all connectors)
1494N/A */
1494N/A edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1494N/A DRM_MODE_PROP_IMMUTABLE,
1494N/A "EDID", 0);
1494N/A dev->mode_config.edid_property = edid;
1494N/A
1494N/A dpms = drm_property_create_enum(dev, 0,
1494N/A "DPMS", drm_dpms_enum_list,
1494N/A ARRAY_SIZE(drm_dpms_enum_list));
1494N/A dev->mode_config.dpms_property = dpms;
1494N/A
1494N/A return 0;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1494N/A * @dev: DRM device
1494N/A *
1494N/A * Called by a driver the first time a DVI-I connector is made.
1494N/A */
1494N/Aint drm_mode_create_dvi_i_properties(struct drm_device *dev)
1494N/A{
1494N/A struct drm_property *dvi_i_selector;
1494N/A struct drm_property *dvi_i_subconnector;
1494N/A
1494N/A if (dev->mode_config.dvi_i_select_subconnector_property)
1494N/A return 0;
1494N/A
1494N/A dvi_i_selector =
1494N/A drm_property_create_enum(dev, 0,
1494N/A "select subconnector",
1494N/A drm_dvi_i_select_enum_list,
1494N/A ARRAY_SIZE(drm_dvi_i_select_enum_list));
1494N/A dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1494N/A
1494N/A dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1494N/A "subconnector",
1494N/A drm_dvi_i_subconnector_enum_list,
1494N/A ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1494N/A dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1494N/A
1494N/A return 0;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_create_tv_properties - create TV specific connector properties
1494N/A * @dev: DRM device
1494N/A * @num_modes: number of different TV formats (modes) supported
1494N/A * @modes: array of pointers to strings containing name of each format
1494N/A *
1494N/A * Called by a driver's TV initialization routine, this function creates
1494N/A * the TV specific connector properties for a given device. Caller is
1494N/A * responsible for allocating a list of format names and passing them to
1494N/A * this routine.
1494N/A */
1494N/Aint drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1494N/A char *modes[])
1494N/A{
1494N/A struct drm_property *tv_selector;
1494N/A struct drm_property *tv_subconnector;
1494N/A int i;
1494N/A
1494N/A if (dev->mode_config.tv_select_subconnector_property)
1494N/A return 0;
1494N/A
1494N/A /*
1494N/A * Basic connector properties
1494N/A */
1494N/A tv_selector = drm_property_create_enum(dev, 0,
1494N/A "select subconnector",
1494N/A drm_tv_select_enum_list,
1494N/A ARRAY_SIZE(drm_tv_select_enum_list));
1494N/A dev->mode_config.tv_select_subconnector_property = tv_selector;
1494N/A
1494N/A tv_subconnector =
1494N/A drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1494N/A "subconnector",
1494N/A drm_tv_subconnector_enum_list,
1494N/A ARRAY_SIZE(drm_tv_subconnector_enum_list));
1494N/A dev->mode_config.tv_subconnector_property = tv_subconnector;
1494N/A
1494N/A /*
1494N/A * Other, TV specific properties: margins & TV modes.
1494N/A */
1494N/A dev->mode_config.tv_left_margin_property =
1494N/A drm_property_create_range(dev, 0, "left margin", 0, 100);
1494N/A
1494N/A dev->mode_config.tv_right_margin_property =
1494N/A drm_property_create_range(dev, 0, "right margin", 0, 100);
1494N/A
1494N/A dev->mode_config.tv_top_margin_property =
1494N/A drm_property_create_range(dev, 0, "top margin", 0, 100);
1494N/A
1494N/A dev->mode_config.tv_bottom_margin_property =
1494N/A drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1494N/A
1494N/A dev->mode_config.tv_mode_property =
1494N/A drm_property_create(dev, DRM_MODE_PROP_ENUM,
1494N/A "mode", num_modes);
1494N/A for (i = 0; i < num_modes; i++)
1494N/A (void) drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1494N/A i, modes[i]);
1494N/A
1494N/A dev->mode_config.tv_brightness_property =
1494N/A drm_property_create_range(dev, 0, "brightness", 0, 100);
1494N/A
1494N/A dev->mode_config.tv_contrast_property =
1494N/A drm_property_create_range(dev, 0, "contrast", 0, 100);
1494N/A
1494N/A dev->mode_config.tv_flicker_reduction_property =
1494N/A drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1494N/A
1494N/A dev->mode_config.tv_overscan_property =
1494N/A drm_property_create_range(dev, 0, "overscan", 0, 100);
1494N/A
1494N/A dev->mode_config.tv_saturation_property =
1494N/A drm_property_create_range(dev, 0, "saturation", 0, 100);
1494N/A
1494N/A dev->mode_config.tv_hue_property =
1494N/A drm_property_create_range(dev, 0, "hue", 0, 100);
1494N/A
1494N/A return 0;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_mode_create_scaling_mode_property - create scaling mode property
1494N/A * @dev: DRM device
1494N/A *
1494N/A * Called by a driver the first time it's needed, must be attached to desired
1494N/A * connectors.
1494N/A */
1494N/Aint drm_mode_create_scaling_mode_property(struct drm_device *dev)
1494N/A{
1494N/A struct drm_property *scaling_mode;
1494N/A
1494N/A if (dev->mode_config.scaling_mode_property)
1494N/A return 0;
1494N/A
1494N/A scaling_mode =
1494N/A drm_property_create_enum(dev, 0, "scaling mode",
1494N/A drm_scaling_mode_enum_list,
1494N/A ARRAY_SIZE(drm_scaling_mode_enum_list));
1494N/A
1494N/A dev->mode_config.scaling_mode_property = scaling_mode;
1494N/A
1494N/A return 0;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_mode_create_dithering_property - create dithering property
1494N/A * @dev: DRM device
1494N/A *
1494N/A * Called by a driver the first time it's needed, must be attached to desired
1494N/A * connectors.
1494N/A */
1494N/Aint drm_mode_create_dithering_property(struct drm_device *dev)
1494N/A{
1494N/A struct drm_property *dithering_mode;
1494N/A
1494N/A if (dev->mode_config.dithering_mode_property)
1494N/A return 0;
1494N/A
1494N/A dithering_mode =
1494N/A drm_property_create_enum(dev, 0, "dithering",
1494N/A drm_dithering_mode_enum_list,
1494N/A ARRAY_SIZE(drm_dithering_mode_enum_list));
1494N/A dev->mode_config.dithering_mode_property = dithering_mode;
1494N/A
1494N/A return 0;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_mode_create_dirty_property - create dirty property
1494N/A * @dev: DRM device
1494N/A *
1494N/A * Called by a driver the first time it's needed, must be attached to desired
1494N/A * connectors.
1494N/A */
1494N/Aint drm_mode_create_dirty_info_property(struct drm_device *dev)
1494N/A{
1494N/A struct drm_property *dirty_info;
1494N/A
1494N/A if (dev->mode_config.dirty_info_property)
1494N/A return 0;
1494N/A
1494N/A dirty_info =
1494N/A drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1494N/A "dirty",
1494N/A drm_dirty_info_enum_list,
1494N/A ARRAY_SIZE(drm_dirty_info_enum_list));
1494N/A dev->mode_config.dirty_info_property = dirty_info;
1494N/A
1494N/A return 0;
1494N/A}
1494N/A
1494N/A
1494N/Astatic int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1494N/A{
1494N/A uint32_t total_objects = 0;
1494N/A
1494N/A total_objects += dev->mode_config.num_crtc;
1494N/A total_objects += dev->mode_config.num_connector;
1494N/A total_objects += dev->mode_config.num_encoder;
1494N/A
1494N/A group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1494N/A if (!group->id_list)
1494N/A return -ENOMEM;
1494N/A
1494N/A group->num_crtcs = 0;
1494N/A group->num_connectors = 0;
1494N/A group->num_encoders = 0;
1494N/A return 0;
1494N/A}
1494N/A
1494N/Aint drm_mode_group_init_legacy_group(struct drm_device *dev,
1494N/A struct drm_mode_group *group)
1494N/A{
1494N/A struct drm_crtc *crtc;
1494N/A struct drm_encoder *encoder;
1494N/A struct drm_connector *connector;
1494N/A int ret;
1494N/A
1494N/A if ((ret = drm_mode_group_init(dev, group)))
1494N/A return ret;
1494N/A
1494N/A list_for_each_entry(crtc, struct drm_crtc, &dev->mode_config.crtc_list, head)
1494N/A group->id_list[group->num_crtcs++] = crtc->base.id;
1494N/A
1494N/A list_for_each_entry(encoder, struct drm_encoder, &dev->mode_config.encoder_list, head)
1494N/A group->id_list[group->num_crtcs + group->num_encoders++] =
1494N/A encoder->base.id;
1494N/A
1494N/A list_for_each_entry(connector, struct drm_connector, &dev->mode_config.connector_list, head)
1494N/A group->id_list[group->num_crtcs + group->num_encoders +
1494N/A group->num_connectors++] = connector->base.id;
1494N/A
1494N/A return 0;
1494N/A}
1494N/A
1494N/A
1494N/A/**
1494N/A * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1494N/A * @out: drm_mode_modeinfo struct to return to the user
1494N/A * @in: drm_display_mode to use
1494N/A *
1494N/A *
1494N/A * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1494N/A * the user.
1494N/A */
1494N/Astatic void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1494N/A const struct drm_display_mode *in)
1494N/A{
1494N/A if (in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1494N/A in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1494N/A in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1494N/A in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1494N/A in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX)
1494N/A DRM_ERROR("timing values too large for mode info\n");
1494N/A
1494N/A out->clock = in->clock;
1494N/A out->hdisplay = in->hdisplay;
1494N/A out->hsync_start = in->hsync_start;
1494N/A out->hsync_end = in->hsync_end;
1494N/A out->htotal = in->htotal;
1494N/A out->hskew = in->hskew;
1494N/A out->vdisplay = in->vdisplay;
1494N/A out->vsync_start = in->vsync_start;
1494N/A out->vsync_end = in->vsync_end;
1494N/A out->vtotal = in->vtotal;
1494N/A out->vscan = in->vscan;
1494N/A out->vrefresh = in->vrefresh;
1494N/A out->flags = in->flags;
1494N/A out->type = in->type;
1494N/A (void) strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1494N/A out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1494N/A * @out: drm_display_mode to return to the user
1494N/A * @in: drm_mode_modeinfo to use
1494N/A *
1494N/A *
1494N/A * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1494N/A * the caller.
1494N/A */
1494N/Astatic int drm_crtc_convert_umode(struct drm_display_mode *out,
1494N/A const struct drm_mode_modeinfo *in)
1494N/A{
1494N/A if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1494N/A return -ERANGE;
1494N/A
1494N/A out->clock = in->clock;
1494N/A out->hdisplay = in->hdisplay;
1494N/A out->hsync_start = in->hsync_start;
1494N/A out->hsync_end = in->hsync_end;
1494N/A out->htotal = in->htotal;
1494N/A out->hskew = in->hskew;
1494N/A out->vdisplay = in->vdisplay;
1494N/A out->vsync_start = in->vsync_start;
1494N/A out->vsync_end = in->vsync_end;
1494N/A out->vtotal = in->vtotal;
1494N/A out->vscan = in->vscan;
1494N/A out->vrefresh = in->vrefresh;
1494N/A out->flags = in->flags;
1494N/A out->type = in->type;
1494N/A (void) strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1494N/A out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1494N/A
1494N/A return 0;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_mode_getresources - get graphics configuration
1494N/A * @inode: inode from the ioctl
1494N/A * @filp: file * from the ioctl
1494N/A * @cmd: cmd from ioctl
1494N/A * @arg: arg from ioctl
1494N/A *
1494N/A *
1494N/A * Construct a set of configuration description structures and return
1494N/A * them to the user, including CRTC, connector and framebuffer configuration.
1494N/A *
1494N/A * Called by the user via ioctl.
1494N/A *
1494N/A * RETURNS:
1494N/A * Zero on success, errno on failure.
1494N/A */
1494N/A/* LINTED E_FUNC_ARG_UNUSED */
1494N/Aint drm_mode_getresources(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_mode_card_res *card_res = data;
1494N/A struct list_head *lh;
1494N/A struct drm_framebuffer *fb;
1494N/A struct drm_connector *connector;
1494N/A struct drm_crtc *crtc;
1494N/A struct drm_encoder *encoder;
1494N/A int ret = 0;
1494N/A int connector_count = 0;
1494N/A int crtc_count = 0;
1494N/A int fb_count = 0;
1494N/A int encoder_count = 0;
1494N/A int copied = 0, i;
1494N/A uint32_t __user *fb_id;
1494N/A uint32_t __user *crtc_id;
1494N/A uint32_t __user *connector_id;
1494N/A uint32_t __user *encoder_id;
1494N/A struct drm_mode_group *mode_group;
1494N/A
1494N/A if (!drm_core_check_feature(dev, DRIVER_MODESET))
1494N/A return -EINVAL;
1494N/A
1494N/A mutex_lock(&file->fbs_lock);
1494N/A
1494N/A /*
1494N/A * For the non-control nodes we need to limit the list of resources
1494N/A * by IDs in the group list for this node
1494N/A */
1494N/A list_for_each(lh, &file->fbs)
1494N/A fb_count++;
1494N/A
1494N/A /* handle this in 4 parts */
1494N/A /* FBs */
1494N/A if (card_res->count_fbs >= fb_count) {
1494N/A copied = 0;
1494N/A fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1494N/A list_for_each_entry(fb, struct drm_framebuffer, &file->fbs, filp_head) {
1494N/A if (put_user(fb->base.id, fb_id + copied)) {
1494N/A mutex_unlock(&file->fbs_lock);
1494N/A return -EFAULT;
1494N/A }
1494N/A copied++;
1494N/A }
1494N/A }
1494N/A card_res->count_fbs = fb_count;
1494N/A mutex_unlock(&file->fbs_lock);
1494N/A
1494N/A drm_modeset_lock_all(dev);
1494N/A
1494N/A mode_group = &file->master->minor->mode_group;
1494N/A if (file->master->minor->type == DRM_MINOR_CONTROL) {
1494N/A
1494N/A list_for_each(lh, &dev->mode_config.crtc_list)
1494N/A crtc_count++;
1494N/A
1494N/A list_for_each(lh, &dev->mode_config.connector_list)
1494N/A connector_count++;
1494N/A
1494N/A list_for_each(lh, &dev->mode_config.encoder_list)
1494N/A encoder_count++;
1494N/A } else {
1494N/A
1494N/A crtc_count = mode_group->num_crtcs;
1494N/A connector_count = mode_group->num_connectors;
1494N/A encoder_count = mode_group->num_encoders;
1494N/A }
1494N/A
1494N/A card_res->max_height = dev->mode_config.max_height;
1494N/A card_res->min_height = dev->mode_config.min_height;
1494N/A card_res->max_width = dev->mode_config.max_width;
1494N/A card_res->min_width = dev->mode_config.min_width;
1494N/A
1494N/A /* CRTCs */
1494N/A if (card_res->count_crtcs >= crtc_count) {
1494N/A copied = 0;
1494N/A crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1494N/A if (file->master->minor->type == DRM_MINOR_CONTROL) {
1494N/A list_for_each_entry(crtc, struct drm_crtc, &dev->mode_config.crtc_list,
1494N/A head) {
1494N/A DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1494N/A if (put_user(crtc->base.id, crtc_id + copied)) {
1494N/A ret = -EFAULT;
1494N/A goto out;
1494N/A }
1494N/A copied++;
1494N/A }
1494N/A } else {
1494N/A for (i = 0; i < mode_group->num_crtcs; i++) {
1494N/A if (put_user(mode_group->id_list[i],
1494N/A crtc_id + copied)) {
1494N/A ret = -EFAULT;
1494N/A goto out;
1494N/A }
1494N/A copied++;
1494N/A }
1494N/A }
1494N/A }
1494N/A card_res->count_crtcs = crtc_count;
1494N/A
1494N/A /* Encoders */
1494N/A if (card_res->count_encoders >= encoder_count) {
1494N/A copied = 0;
1494N/A encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1494N/A if (file->master->minor->type == DRM_MINOR_CONTROL) {
1494N/A list_for_each_entry(encoder, struct drm_encoder,
1494N/A &dev->mode_config.encoder_list,
1494N/A head) {
1494N/A DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1494N/A drm_get_encoder_name(encoder));
1494N/A if (put_user(encoder->base.id, encoder_id +
1494N/A copied)) {
1494N/A ret = -EFAULT;
1494N/A goto out;
1494N/A }
1494N/A copied++;
1494N/A }
1494N/A } else {
1494N/A for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1494N/A if (put_user(mode_group->id_list[i],
1494N/A encoder_id + copied)) {
1494N/A ret = -EFAULT;
1494N/A goto out;
1494N/A }
1494N/A copied++;
1494N/A }
1494N/A
1494N/A }
1494N/A }
1494N/A card_res->count_encoders = encoder_count;
1494N/A
1494N/A /* Connectors */
1494N/A if (card_res->count_connectors >= connector_count) {
1494N/A copied = 0;
1494N/A connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1494N/A if (file->master->minor->type == DRM_MINOR_CONTROL) {
1494N/A list_for_each_entry(connector, struct drm_connector,
1494N/A &dev->mode_config.connector_list,
1494N/A head) {
1494N/A DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1494N/A connector->base.id,
1494N/A drm_get_connector_name(connector));
1494N/A if (put_user(connector->base.id,
1494N/A connector_id + copied)) {
1494N/A ret = -EFAULT;
1494N/A goto out;
1494N/A }
1494N/A copied++;
1494N/A }
1494N/A } else {
1494N/A int start = mode_group->num_crtcs +
1494N/A mode_group->num_encoders;
1494N/A for (i = start; i < start + mode_group->num_connectors; i++) {
1494N/A if (put_user(mode_group->id_list[i],
1494N/A connector_id + copied)) {
1494N/A ret = -EFAULT;
1494N/A goto out;
1494N/A }
1494N/A copied++;
1494N/A }
1494N/A }
1494N/A }
1494N/A card_res->count_connectors = connector_count;
1494N/A
1494N/A DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1494N/A card_res->count_connectors, card_res->count_encoders);
1494N/A
1494N/Aout:
1494N/A drm_modeset_unlock_all(dev);
1494N/A return ret;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_mode_getcrtc - get CRTC configuration
1494N/A * @inode: inode from the ioctl
1494N/A * @filp: file * from the ioctl
1494N/A * @cmd: cmd from ioctl
1494N/A * @arg: arg from ioctl
1494N/A *
1494N/A *
1494N/A * Construct a CRTC configuration structure to return to the user.
1494N/A *
1494N/A * Called by the user via ioctl.
1494N/A *
1494N/A * RETURNS:
1494N/A * Zero on success, errno on failure.
1494N/A */
1494N/A/* LINTED E_FUNC_ARG_UNUSED */
1494N/Aint drm_mode_getcrtc(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_mode_crtc *crtc_resp = data;
1494N/A struct drm_crtc *crtc;
1494N/A struct drm_mode_object *obj;
1494N/A int ret = 0;
1494N/A
1494N/A if (!drm_core_check_feature(dev, DRIVER_MODESET))
1494N/A return -EINVAL;
1494N/A
1494N/A drm_modeset_lock_all(dev);
1494N/A
1494N/A obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1494N/A DRM_MODE_OBJECT_CRTC);
1494N/A if (!obj) {
1494N/A ret = -EINVAL;
1494N/A goto out;
1494N/A }
1494N/A crtc = obj_to_crtc(obj);
1494N/A
1494N/A crtc_resp->x = crtc->x;
1494N/A crtc_resp->y = crtc->y;
1494N/A crtc_resp->gamma_size = crtc->gamma_size;
1494N/A if (crtc->fb)
1494N/A crtc_resp->fb_id = crtc->fb->base.id;
1494N/A else
1494N/A crtc_resp->fb_id = 0;
1494N/A
1494N/A if (crtc->enabled) {
1494N/A
1494N/A drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1494N/A crtc_resp->mode_valid = 1;
1494N/A
1494N/A } else {
1494N/A crtc_resp->mode_valid = 0;
1494N/A }
1494N/A
1494N/Aout:
1494N/A drm_modeset_unlock_all(dev);
1494N/A return ret;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_mode_getconnector - get connector configuration
1494N/A * @inode: inode from the ioctl
1494N/A * @filp: file * from the ioctl
1494N/A * @cmd: cmd from ioctl
1494N/A * @arg: arg from ioctl
1494N/A *
1494N/A *
1494N/A * Construct a connector configuration structure to return to the user.
1494N/A *
1494N/A * Called by the user via ioctl.
1494N/A *
1494N/A * RETURNS:
1494N/A * Zero on success, errno on failure.
1494N/A */
1494N/A/* LINTED E_FUNC_ARG_UNUSED */
1494N/Aint drm_mode_getconnector(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_mode_get_connector *out_resp = data;
1494N/A struct drm_mode_object *obj;
1494N/A struct drm_connector *connector;
1494N/A struct drm_display_mode *mode;
1494N/A int mode_count = 0;
1494N/A int props_count = 0;
1494N/A int encoders_count = 0;
1494N/A int ret = 0;
1494N/A int copied = 0;
1494N/A int i;
1494N/A struct drm_mode_modeinfo u_mode;
1494N/A struct drm_mode_modeinfo __user *mode_ptr;
1494N/A uint32_t __user *prop_ptr;
1494N/A uint64_t __user *prop_values;
1494N/A uint32_t __user *encoder_ptr;
1494N/A
1494N/A if (!drm_core_check_feature(dev, DRIVER_MODESET))
1494N/A return -EINVAL;
1494N/A
1494N/A (void) memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1494N/A
1494N/A DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1494N/A
1494N/A mutex_lock(&dev->mode_config.mutex);
1494N/A
1494N/A obj = drm_mode_object_find(dev, out_resp->connector_id,
1494N/A DRM_MODE_OBJECT_CONNECTOR);
1494N/A if (!obj) {
1494N/A ret = -EINVAL;
1494N/A goto out;
1494N/A }
1494N/A connector = obj_to_connector(obj);
1494N/A
1494N/A props_count = connector->properties.count;
1494N/A
1494N/A for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1494N/A if (connector->encoder_ids[i] != 0) {
1494N/A encoders_count++;
1494N/A }
1494N/A }
1494N/A
1494N/A if (out_resp->count_modes == 0) {
1494N/A connector->funcs->fill_modes(connector,
1494N/A dev->mode_config.max_width,
1494N/A dev->mode_config.max_height);
1494N/A }
1494N/A
1494N/A /* delayed so we get modes regardless of pre-fill_modes state */
1494N/A list_for_each_entry(mode, struct drm_display_mode, &connector->modes, head)
1494N/A mode_count++;
1494N/A
1494N/A out_resp->connector_id = connector->base.id;
1494N/A out_resp->connector_type = connector->connector_type;
1494N/A out_resp->connector_type_id = connector->connector_type_id;
1494N/A out_resp->mm_width = connector->display_info.width_mm;
1494N/A out_resp->mm_height = connector->display_info.height_mm;
1494N/A out_resp->subpixel = connector->display_info.subpixel_order;
1494N/A out_resp->connection = connector->status;
1494N/A if (connector->encoder)
1494N/A out_resp->encoder_id = connector->encoder->base.id;
1494N/A else
1494N/A out_resp->encoder_id = 0;
1494N/A
1494N/A /*
1494N/A * This ioctl is called twice, once to determine how much space is
1494N/A * needed, and the 2nd time to fill it.
1494N/A */
1494N/A if ((out_resp->count_modes >= mode_count) && mode_count) {
1494N/A copied = 0;
1494N/A mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1494N/A list_for_each_entry(mode, struct drm_display_mode, &connector->modes, head) {
1494N/A drm_crtc_convert_to_umode(&u_mode, mode);
1494N/A if (DRM_COPY_TO_USER(mode_ptr + copied,
1494N/A &u_mode, sizeof(u_mode))) {
1494N/A ret = -EFAULT;
1494N/A goto out;
1494N/A }
1494N/A copied++;
1494N/A }
1494N/A }
1494N/A out_resp->count_modes = mode_count;
1494N/A
1494N/A if ((out_resp->count_props >= props_count) && props_count) {
1494N/A copied = 0;
1494N/A prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1494N/A prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1494N/A for (i = 0; i < connector->properties.count; i++) {
1494N/A if (put_user(connector->properties.ids[i],
1494N/A prop_ptr + copied)) {
1494N/A ret = -EFAULT;
1494N/A goto out;
1494N/A }
1494N/A
1494N/A if (put_user(connector->properties.values[i],
1494N/A prop_values + copied)) {
1494N/A ret = -EFAULT;
1494N/A goto out;
1494N/A }
1494N/A copied++;
1494N/A }
1494N/A }
1494N/A out_resp->count_props = props_count;
1494N/A
1494N/A if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1494N/A copied = 0;
1494N/A encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
1494N/A for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1494N/A if (connector->encoder_ids[i] != 0) {
1494N/A if (put_user(connector->encoder_ids[i],
1494N/A encoder_ptr + copied)) {
1494N/A ret = -EFAULT;
1494N/A goto out;
1494N/A }
1494N/A copied++;
1494N/A }
1494N/A }
1494N/A }
1494N/A out_resp->count_encoders = encoders_count;
1494N/A
1494N/Aout:
1494N/A mutex_unlock(&dev->mode_config.mutex);
1494N/A return ret;
1494N/A}
1494N/A
1494N/A/* LINTED E_FUNC_ARG_UNUSED */
1494N/Aint drm_mode_getencoder(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_mode_get_encoder *enc_resp = data;
1494N/A struct drm_mode_object *obj;
1494N/A struct drm_encoder *encoder;
1494N/A int ret = 0;
1494N/A
1494N/A if (!drm_core_check_feature(dev, DRIVER_MODESET))
1494N/A return -EINVAL;
1494N/A
1494N/A drm_modeset_lock_all(dev);
1494N/A obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1494N/A DRM_MODE_OBJECT_ENCODER);
1494N/A if (!obj) {
1494N/A ret = -EINVAL;
1494N/A goto out;
1494N/A }
1494N/A encoder = obj_to_encoder(obj);
1494N/A
1494N/A if (encoder->crtc)
1494N/A enc_resp->crtc_id = encoder->crtc->base.id;
1494N/A else
1494N/A enc_resp->crtc_id = 0;
1494N/A enc_resp->encoder_type = encoder->encoder_type;
1494N/A enc_resp->encoder_id = encoder->base.id;
1494N/A enc_resp->possible_crtcs = encoder->possible_crtcs;
1494N/A enc_resp->possible_clones = encoder->possible_clones;
1494N/A
1494N/Aout:
1494N/A drm_modeset_unlock_all(dev);
1494N/A return ret;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_mode_getplane_res - get plane info
1494N/A * @dev: DRM device
1494N/A * @data: ioctl data
1494N/A * @file_priv: DRM file info
1494N/A *
1494N/A *
1494N/A * Return an plane count and set of IDs.
1494N/A */
1494N/A/* LINTED E_FUNC_ARG_UNUSED */
1494N/Aint drm_mode_getplane_res(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_mode_get_plane_res *plane_resp = data;
1494N/A struct drm_mode_config *config;
1494N/A struct drm_plane *plane;
1494N/A uint32_t __user *plane_ptr;
1494N/A int copied = 0, ret = 0;
1494N/A
1494N/A if (!drm_core_check_feature(dev, DRIVER_MODESET))
1494N/A return -EINVAL;
1494N/A
1494N/A drm_modeset_lock_all(dev);
1494N/A config = &dev->mode_config;
1494N/A
1494N/A /*
1494N/A * This ioctl is called twice, once to determine how much space is
1494N/A * needed, and the 2nd time to fill it.
1494N/A */
1494N/A if (config->num_plane &&
1494N/A (plane_resp->count_planes >= config->num_plane)) {
1494N/A plane_ptr = (uint32_t *)(unsigned long)plane_resp->plane_id_ptr;
1494N/A
1494N/A list_for_each_entry(plane, struct drm_plane, &config->plane_list, head) {
1494N/A if (put_user(plane->base.id, plane_ptr + copied)) {
1494N/A ret = -EFAULT;
1494N/A goto out;
1494N/A }
1494N/A copied++;
1494N/A }
1494N/A }
1494N/A plane_resp->count_planes = config->num_plane;
1494N/A
1494N/Aout:
1494N/A drm_modeset_unlock_all(dev);
1494N/A return ret;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_mode_getplane - get plane info
1494N/A * @dev: DRM device
1494N/A * @data: ioctl data
1494N/A * @file_priv: DRM file info
1494N/A *
1494N/A *
1494N/A * Return plane info, including formats supported, gamma size, any
1494N/A * current fb, etc.
1494N/A */
1494N/A/* LINTED E_FUNC_ARG_UNUSED */
1494N/Aint drm_mode_getplane(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_mode_get_plane *plane_resp = data;
1494N/A struct drm_mode_object *obj;
1494N/A struct drm_plane *plane;
1494N/A uint32_t *format_ptr;
1494N/A int ret = 0;
1494N/A
1494N/A if (!drm_core_check_feature(dev, DRIVER_MODESET))
1494N/A return -EINVAL;
1494N/A
1494N/A drm_modeset_lock_all(dev);
1494N/A obj = drm_mode_object_find(dev, plane_resp->plane_id,
1494N/A DRM_MODE_OBJECT_PLANE);
1494N/A if (!obj) {
1494N/A ret = -ENOENT;
1494N/A goto out;
1494N/A }
1494N/A plane = obj_to_plane(obj);
1494N/A
1494N/A if (plane->crtc)
1494N/A plane_resp->crtc_id = plane->crtc->base.id;
1494N/A else
1494N/A plane_resp->crtc_id = 0;
1494N/A
1494N/A if (plane->fb)
1494N/A plane_resp->fb_id = plane->fb->base.id;
1494N/A else
1494N/A plane_resp->fb_id = 0;
1494N/A
1494N/A plane_resp->plane_id = plane->base.id;
1494N/A plane_resp->possible_crtcs = plane->possible_crtcs;
1494N/A plane_resp->gamma_size = 0;
1494N/A
1494N/A /*
1494N/A * This ioctl is called twice, once to determine how much space is
1494N/A * needed, and the 2nd time to fill it.
1494N/A */
1494N/A if (plane->format_count &&
1494N/A (plane_resp->count_format_types >= plane->format_count)) {
1494N/A format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
1494N/A if (DRM_COPY_TO_USER(format_ptr,
1494N/A plane->format_types,
1494N/A sizeof(uint32_t) * plane->format_count)) {
1494N/A ret = -EFAULT;
1494N/A goto out;
1494N/A }
1494N/A }
1494N/A plane_resp->count_format_types = plane->format_count;
1494N/A
1494N/Aout:
1494N/A drm_modeset_unlock_all(dev);
1494N/A return ret;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_mode_setplane - set up or tear down an plane
1494N/A * @dev: DRM device
1494N/A * @data: ioctl data*
1494N/A * @file_prive: DRM file info
1494N/A *
1494N/A *
1494N/A * Set plane info, including placement, fb, scaling, and other factors.
1494N/A * Or pass a NULL fb to disable.
1494N/A */
1494N/A/* LINTED E_FUNC_ARG_UNUSED */
1494N/Aint drm_mode_setplane(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_mode_set_plane *plane_req = data;
1494N/A struct drm_mode_object *obj;
1494N/A struct drm_plane *plane;
1494N/A struct drm_crtc *crtc;
1494N/A struct drm_framebuffer *fb = NULL, *old_fb = NULL;
1494N/A int ret = 0;
1494N/A unsigned int fb_width, fb_height;
1494N/A int i;
1494N/A
1494N/A if (!drm_core_check_feature(dev, DRIVER_MODESET))
1494N/A return -EINVAL;
1494N/A
1494N/A /*
1494N/A * First, find the plane, crtc, and fb objects. If not available,
1494N/A * we don't bother to call the driver.
1494N/A */
1494N/A obj = drm_mode_object_find(dev, plane_req->plane_id,
1494N/A DRM_MODE_OBJECT_PLANE);
1494N/A if (!obj) {
1494N/A DRM_DEBUG_KMS("Unknown plane ID %d\n",
1494N/A plane_req->plane_id);
1494N/A return -ENOENT;
1494N/A }
1494N/A plane = obj_to_plane(obj);
1494N/A
1494N/A /* No fb means shut it down */
1494N/A if (!plane_req->fb_id) {
1494N/A drm_modeset_lock_all(dev);
1494N/A old_fb = plane->fb;
1494N/A plane->funcs->disable_plane(plane);
1494N/A plane->crtc = NULL;
1494N/A plane->fb = NULL;
1494N/A drm_modeset_unlock_all(dev);
1494N/A goto out;
1494N/A }
1494N/A
1494N/A obj = drm_mode_object_find(dev, plane_req->crtc_id,
1494N/A DRM_MODE_OBJECT_CRTC);
1494N/A if (!obj) {
1494N/A DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1494N/A plane_req->crtc_id);
1494N/A ret = -ENOENT;
1494N/A goto out;
1494N/A }
1494N/A crtc = obj_to_crtc(obj);
1494N/A
1494N/A fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1494N/A if (!fb) {
1494N/A DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1494N/A plane_req->fb_id);
1494N/A ret = -ENOENT;
1494N/A goto out;
1494N/A }
1494N/A
1494N/A /* Check whether this plane supports the fb pixel format. */
1494N/A for (i = 0; i < plane->format_count; i++)
1494N/A if (fb->pixel_format == plane->format_types[i])
1494N/A break;
1494N/A if (i == plane->format_count) {
1494N/A DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1494N/A ret = -EINVAL;
1494N/A goto out;
1494N/A }
1494N/A
1494N/A fb_width = fb->width << 16;
1494N/A fb_height = fb->height << 16;
1494N/A
1494N/A /* Make sure source coordinates are inside the fb. */
1494N/A if (plane_req->src_w > fb_width ||
1494N/A plane_req->src_x > fb_width - plane_req->src_w ||
1494N/A plane_req->src_h > fb_height ||
1494N/A plane_req->src_y > fb_height - plane_req->src_h) {
1494N/A DRM_DEBUG_KMS("Invalid source coordinates "
1494N/A "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1494N/A plane_req->src_w >> 16,
1494N/A ((plane_req->src_w & 0xffff) * 15625) >> 10,
1494N/A plane_req->src_h >> 16,
1494N/A ((plane_req->src_h & 0xffff) * 15625) >> 10,
1494N/A plane_req->src_x >> 16,
1494N/A ((plane_req->src_x & 0xffff) * 15625) >> 10,
1494N/A plane_req->src_y >> 16,
1494N/A ((plane_req->src_y & 0xffff) * 15625) >> 10);
1494N/A ret = -ENOSPC;
1494N/A goto out;
1494N/A }
1494N/A
1494N/A /* Give drivers some help against integer overflows */
1494N/A if (plane_req->crtc_w > INT_MAX ||
1494N/A plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1494N/A plane_req->crtc_h > INT_MAX ||
1494N/A plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1494N/A DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1494N/A plane_req->crtc_w, plane_req->crtc_h,
1494N/A plane_req->crtc_x, plane_req->crtc_y);
1494N/A ret = -ERANGE;
1494N/A goto out;
1494N/A }
1494N/A
1494N/A drm_modeset_lock_all(dev);
1494N/A ret = plane->funcs->update_plane(plane, crtc, fb,
1494N/A plane_req->crtc_x, plane_req->crtc_y,
1494N/A plane_req->crtc_w, plane_req->crtc_h,
1494N/A plane_req->src_x, plane_req->src_y,
1494N/A plane_req->src_w, plane_req->src_h);
1494N/A if (!ret) {
1494N/A old_fb = plane->fb;
1494N/A plane->crtc = crtc;
1494N/A plane->fb = fb;
1494N/A fb = NULL;
1494N/A }
1494N/A drm_modeset_unlock_all(dev);
1494N/A
1494N/Aout:
1494N/A if (fb)
1494N/A drm_framebuffer_unreference(fb);
1494N/A if (old_fb)
1494N/A drm_framebuffer_unreference(old_fb);
1494N/A return ret;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_mode_set_config_internal - helper to call ->set_config
1494N/A * @set: modeset config to set
1494N/A *
1494N/A * This is a little helper to wrap internal calls to the ->set_config driver
1494N/A * interface. The only thing it adds is correct refcounting dance.
1494N/A */
1494N/Aint drm_mode_set_config_internal(struct drm_mode_set *set)
1494N/A{
1494N/A struct drm_crtc *crtc = set->crtc;
1494N/A struct drm_framebuffer *fb;
1494N/A struct drm_crtc *tmp;
1494N/A int ret;
1494N/A
1494N/A /*
1494N/A * NOTE: ->set_config can also disable other crtcs (if we steal all
1494N/A * connectors from it), hence we need to refcount the fbs across all
1494N/A * crtcs. Atomic modeset will have saner semantics ...
1494N/A */
1494N/A list_for_each_entry(tmp, struct drm_crtc, &crtc->dev->mode_config.crtc_list, head)
1494N/A tmp->old_fb = tmp->fb;
1494N/A
1494N/A fb = set->fb;
1494N/A ret = crtc->funcs->set_config(set);
1494N/A if (ret == 0) {
1494N/A /* crtc->fb must be updated by ->set_config, enforces this. */
1494N/A if (fb != crtc->fb)
1494N/A DRM_ERROR("fb 0x%lx != crtc->fb 0x%lx", (uintptr_t)fb, (uintptr_t)crtc->fb);
1494N/A }
1494N/A
1494N/A list_for_each_entry(tmp, struct drm_crtc, &crtc->dev->mode_config.crtc_list, head) {
1494N/A if (tmp->fb)
1494N/A drm_framebuffer_reference(tmp->fb);
1494N/A if (tmp->old_fb)
1494N/A drm_framebuffer_unreference(tmp->old_fb);
1494N/A }
1494N/A
1494N/A return ret;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_mode_setcrtc - set CRTC configuration
1494N/A * @inode: inode from the ioctl
1494N/A * @filp: file * from the ioctl
1494N/A * @cmd: cmd from ioctl
1494N/A * @arg: arg from ioctl
1494N/A *
1494N/A *
1494N/A * Build a new CRTC configuration based on user request.
1494N/A *
1494N/A * Called by the user via ioctl.
1494N/A *
1494N/A * RETURNS:
1494N/A * Zero on success, errno on failure.
1494N/A */
1494N/A/* LINTED E_FUNC_ARG_UNUSED */
1494N/Aint drm_mode_setcrtc(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_mode_config *config = &dev->mode_config;
1494N/A struct drm_mode_crtc *crtc_req = data;
1494N/A struct drm_mode_object *obj;
1494N/A struct drm_crtc *crtc;
1494N/A struct drm_connector **connector_set = NULL, *connector;
1494N/A struct drm_framebuffer *fb = NULL;
1494N/A struct drm_display_mode *mode = NULL;
1494N/A struct drm_mode_set set;
1494N/A uint32_t __user *set_connectors_ptr;
1494N/A int ret;
1494N/A int i;
1494N/A
1494N/A if (!drm_core_check_feature(dev, DRIVER_MODESET))
1494N/A return -EINVAL;
1494N/A
1494N/A /* For some reason crtc x/y offsets are signed internally. */
1494N/A if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1494N/A return -ERANGE;
1494N/A
1494N/A drm_modeset_lock_all(dev);
1494N/A obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1494N/A DRM_MODE_OBJECT_CRTC);
1494N/A if (!obj) {
1494N/A DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1494N/A ret = -EINVAL;
1494N/A goto out;
1494N/A }
1494N/A crtc = obj_to_crtc(obj);
1494N/A DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1494N/A
1494N/A if (crtc_req->mode_valid) {
1494N/A int hdisplay, vdisplay;
1494N/A /* If we have a mode we need a framebuffer. */
1494N/A /* If we pass -1, set the mode with the currently bound fb */
1494N/A /* LINTED */
1494N/A if (crtc_req->fb_id == -1) {
1494N/A if (!crtc->fb) {
1494N/A DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1494N/A ret = -EINVAL;
1494N/A goto out;
1494N/A }
1494N/A fb = crtc->fb;
1494N/A /* Make refcounting symmetric with the lookup path. */
1494N/A drm_framebuffer_reference(fb);
1494N/A } else {
1494N/A fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
1494N/A if (!fb) {
1494N/A DRM_DEBUG_KMS("Unknown FB ID%d\n",
1494N/A crtc_req->fb_id);
1494N/A ret = -EINVAL;
1494N/A goto out;
1494N/A }
1494N/A }
1494N/A
1494N/A mode = drm_mode_create(dev);
1494N/A if (!mode) {
1494N/A ret = -ENOMEM;
1494N/A goto out;
1494N/A }
1494N/A
1494N/A ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
1494N/A if (ret) {
1494N/A DRM_DEBUG_KMS("Invalid mode\n");
1494N/A goto out;
1494N/A }
1494N/A
1494N/A drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1494N/A
1494N/A hdisplay = mode->hdisplay;
1494N/A vdisplay = mode->vdisplay;
1494N/A
1494N/A if (crtc->invert_dimensions)
1494N/A swap(hdisplay, vdisplay);
1494N/A
1494N/A if (hdisplay > fb->width ||
1494N/A vdisplay > fb->height ||
1494N/A crtc_req->x > fb->width - hdisplay ||
1494N/A crtc_req->y > fb->height - vdisplay) {
1494N/A DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
1494N/A fb->width, fb->height,
1494N/A hdisplay, vdisplay, crtc_req->x, crtc_req->y,
1494N/A crtc->invert_dimensions ? " (inverted)" : "");
1494N/A ret = -ENOSPC;
1494N/A goto out;
1494N/A }
1494N/A }
1494N/A
1494N/A if (crtc_req->count_connectors == 0 && mode) {
1494N/A DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1494N/A ret = -EINVAL;
1494N/A goto out;
1494N/A }
1494N/A
1494N/A if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
1494N/A DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1494N/A crtc_req->count_connectors);
1494N/A ret = -EINVAL;
1494N/A goto out;
1494N/A }
1494N/A
1494N/A if (crtc_req->count_connectors > 0) {
1494N/A u32 out_id;
1494N/A
1494N/A /* Avoid unbounded kernel memory allocation */
1494N/A if (crtc_req->count_connectors > config->num_connector) {
1494N/A ret = -EINVAL;
1494N/A goto out;
1494N/A }
1494N/A
1494N/A connector_set = kmalloc(crtc_req->count_connectors *
1494N/A sizeof(struct drm_connector *),
1494N/A GFP_KERNEL);
1494N/A if (!connector_set) {
1494N/A ret = -ENOMEM;
1494N/A goto out;
1494N/A }
1494N/A
1494N/A for (i = 0; i < crtc_req->count_connectors; i++) {
1494N/A set_connectors_ptr = (uint32_t *)(unsigned long)crtc_req->set_connectors_ptr;
1494N/A if (get_user(out_id, &set_connectors_ptr[i])) {
1494N/A ret = -EFAULT;
1494N/A goto out;
1494N/A }
1494N/A
1494N/A obj = drm_mode_object_find(dev, out_id,
1494N/A DRM_MODE_OBJECT_CONNECTOR);
1494N/A if (!obj) {
1494N/A DRM_DEBUG_KMS("Connector id %d unknown\n",
1494N/A out_id);
1494N/A ret = -EINVAL;
1494N/A goto out;
1494N/A }
1494N/A connector = obj_to_connector(obj);
1494N/A DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1494N/A connector->base.id,
1494N/A drm_get_connector_name(connector));
1494N/A
1494N/A connector_set[i] = connector;
1494N/A }
1494N/A }
1494N/A
1494N/A set.crtc = crtc;
1494N/A set.x = crtc_req->x;
1494N/A set.y = crtc_req->y;
1494N/A set.mode = mode;
1494N/A set.connectors = connector_set;
1494N/A set.num_connectors = crtc_req->count_connectors;
1494N/A set.fb = fb;
1494N/A ret = drm_mode_set_config_internal(&set);
1494N/A
1494N/Aout:
1494N/A if (fb)
1494N/A drm_framebuffer_unreference(fb);
1494N/A kfree(connector_set, crtc_req->count_connectors * sizeof(struct drm_connector *));
1494N/A drm_mode_destroy(dev, mode);
1494N/A drm_modeset_unlock_all(dev);
1494N/A return ret;
1494N/A}
1494N/A
1494N/Astatic int drm_mode_cursor_common(struct drm_device *dev,
1494N/A struct drm_mode_cursor2 *req,
1494N/A struct drm_file *file_priv)
1494N/A{
1494N/A struct drm_mode_object *obj;
1494N/A struct drm_crtc *crtc;
1494N/A int ret = 0;
1494N/A
1494N/A if (!drm_core_check_feature(dev, DRIVER_MODESET))
1494N/A return -EINVAL;
1494N/A
1494N/A if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
1494N/A return -EINVAL;
1494N/A
1494N/A obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
1494N/A if (!obj) {
1494N/A DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
1494N/A return -EINVAL;
1494N/A }
1494N/A crtc = obj_to_crtc(obj);
1494N/A
1494N/A mutex_lock(&crtc->mutex);
1494N/A if (req->flags & DRM_MODE_CURSOR_BO) {
1494N/A if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
1494N/A ret = -ENXIO;
1494N/A goto out;
1494N/A }
1494N/A /* Turns off the cursor if handle is 0 */
1494N/A if (crtc->funcs->cursor_set2)
1494N/A ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
1494N/A req->width, req->height, req->hot_x, req->hot_y);
1494N/A else
1494N/A ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1494N/A req->width, req->height);
1494N/A }
1494N/A
1494N/A if (req->flags & DRM_MODE_CURSOR_MOVE) {
1494N/A if (crtc->funcs->cursor_move) {
1494N/A ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1494N/A } else {
1494N/A ret = -EFAULT;
1494N/A goto out;
1494N/A }
1494N/A }
1494N/Aout:
1494N/A mutex_unlock(&crtc->mutex);
1494N/A
1494N/A return ret;
1494N/A
1494N/A}
1494N/A
1494N/A/* LINTED E_FUNC_ARG_UNUSED */
1494N/Aint drm_mode_cursor_ioctl(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_mode_cursor *req = data;
1494N/A struct drm_mode_cursor2 new_req;
1494N/A
1494N/A (void) memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
1494N/A new_req.hot_x = new_req.hot_y = 0;
1494N/A
1494N/A return drm_mode_cursor_common(dev, &new_req, file);
1494N/A}
1494N/A
1494N/A/* LINTED E_FUNC_ARG_UNUSED */
1494N/Aint drm_mode_cursor2_ioctl(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_mode_cursor2 *req = data;
1494N/A return drm_mode_cursor_common(dev, req, file);
1494N/A}
1494N/A
1494N/A/* Original addfb only supported RGB formats, so figure out which one */
1494N/Auint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
1494N/A{
1494N/A uint32_t fmt;
1494N/A
1494N/A switch (bpp) {
1494N/A case 8:
1494N/A fmt = DRM_FORMAT_C8;
1494N/A break;
1494N/A case 16:
1494N/A if (depth == 15)
1494N/A fmt = DRM_FORMAT_XRGB1555;
1494N/A else
1494N/A fmt = DRM_FORMAT_RGB565;
1494N/A break;
1494N/A case 24:
1494N/A fmt = DRM_FORMAT_RGB888;
1494N/A break;
1494N/A case 32:
1494N/A if (depth == 24)
1494N/A fmt = DRM_FORMAT_XRGB8888;
1494N/A else if (depth == 30)
1494N/A fmt = DRM_FORMAT_XRGB2101010;
1494N/A else
1494N/A fmt = DRM_FORMAT_ARGB8888;
1494N/A break;
1494N/A default:
1494N/A DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
1494N/A fmt = DRM_FORMAT_XRGB8888;
1494N/A break;
1494N/A }
1494N/A
1494N/A return fmt;
1494N/A}
1494N/A/**
1494N/A * drm_mode_addfb - add an FB to the graphics configuration
1494N/A * @inode: inode from the ioctl
1494N/A * @filp: file * from the ioctl
1494N/A * @cmd: cmd from ioctl
1494N/A * @arg: arg from ioctl
1494N/A *
1494N/A *
1494N/A * Add a new FB to the specified CRTC, given a user request.
1494N/A *
1494N/A * Called by the user via ioctl.
1494N/A *
1494N/A * RETURNS:
1494N/A * Zero on success, errno on failure.
1494N/A */
1494N/A/* LINTED E_FUNC_ARG_UNUSED */
1494N/Aint drm_mode_addfb(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_mode_fb_cmd *or = data;
1494N/A struct drm_mode_fb_cmd2 r;
1494N/A struct drm_mode_config *config = &dev->mode_config;
1494N/A struct drm_framebuffer *fb;
1494N/A int ret = 0;
1494N/A
1494N/A (void) memset(&r, 0, sizeof(struct drm_mode_fb_cmd2));
1494N/A
1494N/A /* Use new struct with format internally */
1494N/A r.fb_id = or->fb_id;
1494N/A r.width = or->width;
1494N/A r.height = or->height;
1494N/A r.pitches[0] = or->pitch;
1494N/A r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
1494N/A r.handles[0] = or->handle;
1494N/A
1494N/A if (!drm_core_check_feature(dev, DRIVER_MODESET))
1494N/A return -EINVAL;
1494N/A
1494N/A if ((config->min_width > r.width) || (r.width > config->max_width))
1494N/A return -EINVAL;
1494N/A
1494N/A if ((config->min_height > r.height) || (r.height > config->max_height))
1494N/A return -EINVAL;
1494N/A
1494N/A fb = dev->mode_config.funcs->fb_create(dev, file, &r);
1494N/A if (fb == NULL) {
1494N/A DRM_DEBUG_KMS("could not create framebuffer\n");
1494N/A return -ENOMEM;
1494N/A }
1494N/A
1494N/A mutex_lock(&file->fbs_lock);
1494N/A or->fb_id = fb->base.id;
1494N/A list_add(&fb->filp_head, &file->fbs, (caddr_t)fb);
1494N/A DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
1494N/A mutex_unlock(&file->fbs_lock);
1494N/A
1494N/A return ret;
1494N/A}
1494N/A
1494N/Astatic int format_check(const struct drm_mode_fb_cmd2 *r)
1494N/A{
1494N/A uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
1494N/A
1494N/A switch (format) {
1494N/A case DRM_FORMAT_C8:
1494N/A case DRM_FORMAT_RGB332:
1494N/A case DRM_FORMAT_BGR233:
1494N/A case DRM_FORMAT_XRGB4444:
1494N/A case DRM_FORMAT_XBGR4444:
1494N/A case DRM_FORMAT_RGBX4444:
1494N/A case DRM_FORMAT_BGRX4444:
1494N/A case DRM_FORMAT_ARGB4444:
1494N/A case DRM_FORMAT_ABGR4444:
1494N/A case DRM_FORMAT_RGBA4444:
1494N/A case DRM_FORMAT_BGRA4444:
1494N/A case DRM_FORMAT_XRGB1555:
1494N/A case DRM_FORMAT_XBGR1555:
1494N/A case DRM_FORMAT_RGBX5551:
1494N/A case DRM_FORMAT_BGRX5551:
1494N/A case DRM_FORMAT_ARGB1555:
1494N/A case DRM_FORMAT_ABGR1555:
1494N/A case DRM_FORMAT_RGBA5551:
1494N/A case DRM_FORMAT_BGRA5551:
1494N/A case DRM_FORMAT_RGB565:
1494N/A case DRM_FORMAT_BGR565:
1494N/A case DRM_FORMAT_RGB888:
1494N/A case DRM_FORMAT_BGR888:
1494N/A case DRM_FORMAT_XRGB8888:
1494N/A case DRM_FORMAT_XBGR8888:
1494N/A case DRM_FORMAT_RGBX8888:
1494N/A case DRM_FORMAT_BGRX8888:
1494N/A case DRM_FORMAT_ARGB8888:
1494N/A case DRM_FORMAT_ABGR8888:
1494N/A case DRM_FORMAT_RGBA8888:
1494N/A case DRM_FORMAT_BGRA8888:
1494N/A case DRM_FORMAT_XRGB2101010:
1494N/A case DRM_FORMAT_XBGR2101010:
1494N/A case DRM_FORMAT_RGBX1010102:
1494N/A case DRM_FORMAT_BGRX1010102:
1494N/A case DRM_FORMAT_ARGB2101010:
1494N/A case DRM_FORMAT_ABGR2101010:
1494N/A case DRM_FORMAT_RGBA1010102:
1494N/A case DRM_FORMAT_BGRA1010102:
1494N/A case DRM_FORMAT_YUYV:
1494N/A case DRM_FORMAT_YVYU:
1494N/A case DRM_FORMAT_UYVY:
1494N/A case DRM_FORMAT_VYUY:
1494N/A case DRM_FORMAT_AYUV:
1494N/A case DRM_FORMAT_NV12:
1494N/A case DRM_FORMAT_NV21:
1494N/A case DRM_FORMAT_NV16:
1494N/A case DRM_FORMAT_NV61:
1494N/A case DRM_FORMAT_NV24:
1494N/A case DRM_FORMAT_NV42:
1494N/A case DRM_FORMAT_YUV410:
1494N/A case DRM_FORMAT_YVU410:
1494N/A case DRM_FORMAT_YUV411:
1494N/A case DRM_FORMAT_YVU411:
1494N/A case DRM_FORMAT_YUV420:
1494N/A case DRM_FORMAT_YVU420:
1494N/A case DRM_FORMAT_YUV422:
1494N/A case DRM_FORMAT_YVU422:
1494N/A case DRM_FORMAT_YUV444:
1494N/A case DRM_FORMAT_YVU444:
1494N/A return 0;
1494N/A default:
1494N/A return -EINVAL;
1494N/A }
1494N/A}
1494N/A
1494N/Astatic int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
1494N/A{
1494N/A int ret, hsub, vsub, num_planes, i;
1494N/A
1494N/A ret = format_check(r);
1494N/A if (ret) {
1494N/A DRM_DEBUG_KMS("bad framebuffer format %s\n",
1494N/A drm_get_format_name(r->pixel_format));
1494N/A return ret;
1494N/A }
1494N/A
1494N/A hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
1494N/A vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
1494N/A num_planes = drm_format_num_planes(r->pixel_format);
1494N/A
1494N/A if (r->width == 0 || r->width % hsub) {
1494N/A DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
1494N/A return -EINVAL;
1494N/A }
1494N/A
1494N/A if (r->height == 0 || r->height % vsub) {
1494N/A DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
1494N/A return -EINVAL;
1494N/A }
1494N/A
1494N/A for (i = 0; i < num_planes; i++) {
1494N/A unsigned int width = r->width / (i != 0 ? hsub : 1);
1494N/A unsigned int height = r->height / (i != 0 ? vsub : 1);
1494N/A unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
1494N/A
1494N/A if (!r->handles[i]) {
1494N/A DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
1494N/A return -EINVAL;
1494N/A }
1494N/A
1494N/A if ((uint64_t) width * cpp > UINT_MAX)
1494N/A return -ERANGE;
1494N/A
1494N/A if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
1494N/A return -ERANGE;
1494N/A
1494N/A if (r->pitches[i] < width * cpp) {
1494N/A DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
1494N/A return -EINVAL;
1494N/A }
1494N/A }
1494N/A
1494N/A return 0;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_mode_addfb2 - add an FB to the graphics configuration
1494N/A * @inode: inode from the ioctl
1494N/A * @filp: file * from the ioctl
1494N/A * @arg: arg from ioctl
1494N/A *
1494N/A * Add a new FB to the specified CRTC, given a user request with format.
1494N/A *
1494N/A * Called by the user via ioctl.
1494N/A *
1494N/A * RETURNS:
1494N/A * Zero on success, errno on failure.
1494N/A */
1494N/A/* LINTED E_FUNC_ARG_UNUSED */
1494N/Aint drm_mode_addfb2(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_mode_fb_cmd2 *r = data;
1494N/A struct drm_mode_config *config = &dev->mode_config;
1494N/A struct drm_framebuffer *fb;
1494N/A int ret;
1494N/A
1494N/A if (!drm_core_check_feature(dev, DRIVER_MODESET))
1494N/A return -EINVAL;
1494N/A
1494N/A if (r->flags & ~DRM_MODE_FB_INTERLACED) {
1494N/A DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
1494N/A return -EINVAL;
1494N/A }
1494N/A
1494N/A if ((config->min_width > r->width) || (r->width > config->max_width)) {
1494N/A DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
1494N/A r->width, config->min_width, config->max_width);
1494N/A return -EINVAL;
1494N/A }
1494N/A if ((config->min_height > r->height) || (r->height > config->max_height)) {
1494N/A DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
1494N/A r->height, config->min_height, config->max_height);
1494N/A return -EINVAL;
1494N/A }
1494N/A
1494N/A ret = framebuffer_check(r);
1494N/A if (ret)
1494N/A return ret;
1494N/A
1494N/A fb = dev->mode_config.funcs->fb_create(dev, file, r);
1494N/A if (fb == NULL) {
1494N/A DRM_DEBUG_KMS("could not create framebuffer\n");
1494N/A return -ENOMEM;
1494N/A }
1494N/A
1494N/A mutex_lock(&file->fbs_lock);
1494N/A r->fb_id = fb->base.id;
1494N/A list_add(&fb->filp_head, &file->fbs, (caddr_t)fb);
1494N/A DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
1494N/A mutex_unlock(&file->fbs_lock);
1494N/A
1494N/A return ret;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_mode_rmfb - remove an FB from the configuration
1494N/A * @inode: inode from the ioctl
1494N/A * @filp: file * from the ioctl
1494N/A * @arg: arg from ioctl
1494N/A *
1494N/A * Remove the FB specified by the user.
1494N/A *
1494N/A * Called by the user via ioctl.
1494N/A *
1494N/A * RETURNS:
1494N/A * Zero on success, errno on failure.
1494N/A */
1494N/A/* LINTED */
1494N/Aint drm_mode_rmfb(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_framebuffer *fb = NULL;
1494N/A struct drm_framebuffer *fbl = NULL;
1494N/A uint32_t *id = data;
1494N/A int found = 0;
1494N/A
1494N/A if (!drm_core_check_feature(dev, DRIVER_MODESET))
1494N/A return -EINVAL;
1494N/A
1494N/A mutex_lock(&file->fbs_lock);
1494N/A mutex_lock(&dev->mode_config.fb_lock);
1494N/A fb = __drm_framebuffer_lookup(dev, *id);
1494N/A if (!fb)
1494N/A goto fail_lookup;
1494N/A
1494N/A list_for_each_entry(fbl, struct drm_framebuffer, &file->fbs, filp_head)
1494N/A if (fb == fbl)
1494N/A found = 1;
1494N/A
1494N/A if (!found)
1494N/A goto fail_lookup;
1494N/A
1494N/A /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
1494N/A __drm_framebuffer_unregister(dev, fb);
1494N/A
1494N/A list_del_init(&fb->filp_head);
1494N/A mutex_unlock(&dev->mode_config.fb_lock);
1494N/A mutex_unlock(&file->fbs_lock);
1494N/A
1494N/A drm_framebuffer_remove(fb);
1494N/A return 0;
1494N/A
1494N/Afail_lookup:
1494N/A mutex_unlock(&dev->mode_config.fb_lock);
1494N/A mutex_unlock(&file->fbs_lock);
1494N/A
1494N/A return -EINVAL;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_mode_getfb - get FB info
1494N/A * @inode: inode from the ioctl
1494N/A * @filp: file * from the ioctl
1494N/A * @arg: arg from ioctl
1494N/A *
1494N/A * Lookup the FB given its ID and return info about it.
1494N/A *
1494N/A * Called by the user via ioctl.
1494N/A *
1494N/A * RETURNS:
1494N/A * Zero on success, errno on failure.
1494N/A */
1494N/A/* LINTED */
1494N/Aint drm_mode_getfb(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_mode_fb_cmd *r = data;
1494N/A struct drm_framebuffer *fb;
1494N/A int ret;
1494N/A
1494N/A if (!drm_core_check_feature(dev, DRIVER_MODESET))
1494N/A return -EINVAL;
1494N/A
1494N/A fb = drm_framebuffer_lookup(dev, r->fb_id);
1494N/A if (!fb)
1494N/A return -EINVAL;
1494N/A
1494N/A r->height = fb->height;
1494N/A r->width = fb->width;
1494N/A r->depth = fb->depth;
1494N/A r->bpp = fb->bits_per_pixel;
1494N/A r->pitch = fb->pitches[0];
1494N/A if (fb->funcs->create_handle)
1494N/A ret = fb->funcs->create_handle(fb, file, &r->handle);
1494N/A else
1494N/A ret = -ENODEV;
1494N/A
1494N/A drm_framebuffer_unreference(fb);
1494N/A
1494N/A return ret;
1494N/A}
1494N/A
1494N/A/* LINTED */
1494N/Aint drm_mode_dirtyfb_ioctl(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_clip_rect __user *clips_ptr;
1494N/A struct drm_clip_rect *clips = NULL;
1494N/A struct drm_mode_fb_dirty_cmd *r = data;
1494N/A struct drm_framebuffer *fb;
1494N/A unsigned flags;
1494N/A int num_clips;
1494N/A int ret;
1494N/A
1494N/A if (!drm_core_check_feature(dev, DRIVER_MODESET))
1494N/A return -EINVAL;
1494N/A
1494N/A fb = drm_framebuffer_lookup(dev, r->fb_id);
1494N/A if (!fb)
1494N/A return -EINVAL;
1494N/A
1494N/A num_clips = r->num_clips;
1494N/A clips_ptr = (struct drm_clip_rect *)(unsigned long)r->clips_ptr;
1494N/A
1494N/A if (!num_clips != !clips_ptr) {
1494N/A ret = -EINVAL;
1494N/A goto out_err1;
1494N/A }
1494N/A
1494N/A flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
1494N/A
1494N/A /* If userspace annotates copy, clips must come in pairs */
1494N/A if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
1494N/A ret = -EINVAL;
1494N/A goto out_err1;
1494N/A }
1494N/A
1494N/A if (num_clips && clips_ptr) {
1494N/A if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
1494N/A ret = -EINVAL;
1494N/A goto out_err1;
1494N/A }
1494N/A clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
1494N/A if (!clips) {
1494N/A ret = -ENOMEM;
1494N/A goto out_err1;
1494N/A }
1494N/A
1494N/A ret = copy_from_user(clips, clips_ptr,
1494N/A num_clips * sizeof(*clips));
1494N/A if (ret) {
1494N/A ret = -EFAULT;
1494N/A goto out_err2;
1494N/A }
1494N/A }
1494N/A
1494N/A if (fb->funcs->dirty) {
1494N/A drm_modeset_lock_all(dev);
1494N/A ret = fb->funcs->dirty(fb, file, flags, r->color,
1494N/A clips, num_clips);
1494N/A drm_modeset_unlock_all(dev);
1494N/A } else {
1494N/A ret = -ENOSYS;
1494N/A }
1494N/A
1494N/Aout_err2:
1494N/A if (clips)
1494N/A kfree(clips, num_clips * sizeof(*clips));
1494N/Aout_err1:
1494N/A drm_framebuffer_unreference(fb);
1494N/A return ret;
1494N/A}
1494N/A
1494N/A
1494N/A/**
1494N/A * drm_fb_release - remove and free the FBs on this file
1494N/A * @filp: file * from the ioctl
1494N/A *
1494N/A *
1494N/A * Destroy all the FBs associated with @filp.
1494N/A *
1494N/A * Called by the user via ioctl.
1494N/A *
1494N/A * RETURNS:
1494N/A * Zero on success, errno on failure.
1494N/A */
1494N/Avoid drm_fb_release(struct drm_file *priv)
1494N/A{
1494N/A struct drm_device *dev = priv->minor->dev;
1494N/A struct drm_framebuffer *fb, *tfb;
1494N/A
1494N/A mutex_lock(&priv->fbs_lock);
1494N/A list_for_each_entry_safe(fb, tfb, struct drm_framebuffer, &priv->fbs, filp_head) {
1494N/A
1494N/A mutex_lock(&dev->mode_config.fb_lock);
1494N/A /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
1494N/A __drm_framebuffer_unregister(dev, fb);
1494N/A mutex_unlock(&dev->mode_config.fb_lock);
1494N/A
1494N/A list_del_init(&fb->filp_head);
1494N/A
1494N/A /* This will also drop the fpriv->fbs reference. */
1494N/A drm_framebuffer_remove(fb);
1494N/A }
1494N/A mutex_unlock(&priv->fbs_lock);
1494N/A}
1494N/A
1494N/Astruct drm_property *drm_property_create(struct drm_device *dev, int flags,
1494N/A const char *name, int num_values)
1494N/A{
1494N/A struct drm_property *property = NULL;
1494N/A int ret;
1494N/A
1494N/A property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
1494N/A if (!property)
1494N/A return NULL;
1494N/A
1494N/A if (num_values) {
1494N/A property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
1494N/A if (!property->values)
1494N/A goto fail;
1494N/A }
1494N/A
1494N/A ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
1494N/A if (ret)
1494N/A goto fail;
1494N/A
1494N/A property->flags = flags;
1494N/A property->num_values = num_values;
1494N/A INIT_LIST_HEAD(&property->enum_blob_list);
1494N/A
1494N/A if (name) {
1494N/A (void) strncpy(property->name, name, DRM_PROP_NAME_LEN);
1494N/A property->name[DRM_PROP_NAME_LEN-1] = '\0';
1494N/A }
1494N/A
1494N/A list_add_tail(&property->head, &dev->mode_config.property_list, (caddr_t)property);
1494N/A return property;
1494N/Afail:
1494N/A kfree(property->values, sizeof(uint64_t)*num_values);
1494N/A kfree(property, sizeof(struct drm_property));
1494N/A return NULL;
1494N/A}
1494N/Astruct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
1494N/A const char *name,
1494N/A const struct drm_prop_enum_list *props,
1494N/A int num_values)
1494N/A{
1494N/A struct drm_property *property;
1494N/A int i, ret;
1494N/A
1494N/A flags |= DRM_MODE_PROP_ENUM;
1494N/A
1494N/A property = drm_property_create(dev, flags, name, num_values);
1494N/A if (!property)
1494N/A return NULL;
1494N/A
1494N/A for (i = 0; i < num_values; i++) {
1494N/A ret = drm_property_add_enum(property, i,
1494N/A props[i].type,
1494N/A props[i].name);
1494N/A if (ret) {
1494N/A drm_property_destroy(dev, property);
1494N/A return NULL;
1494N/A }
1494N/A }
1494N/A
1494N/A return property;
1494N/A}
1494N/Astruct drm_property *drm_property_create_bitmask(struct drm_device *dev,
1494N/A int flags, const char *name,
1494N/A const struct drm_prop_enum_list *props,
1494N/A int num_values)
1494N/A{
1494N/A struct drm_property *property;
1494N/A int i, ret;
1494N/A
1494N/A flags |= DRM_MODE_PROP_BITMASK;
1494N/A
1494N/A property = drm_property_create(dev, flags, name, num_values);
1494N/A if (!property)
1494N/A return NULL;
1494N/A
1494N/A for (i = 0; i < num_values; i++) {
1494N/A ret = drm_property_add_enum(property, i,
1494N/A props[i].type,
1494N/A props[i].name);
1494N/A if (ret) {
1494N/A drm_property_destroy(dev, property);
1494N/A return NULL;
1494N/A }
1494N/A }
1494N/A
1494N/A return property;
1494N/A}
1494N/A
1494N/Astruct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
1494N/A const char *name,
1494N/A uint64_t min, uint64_t max)
1494N/A{
1494N/A struct drm_property *property;
1494N/A
1494N/A flags |= DRM_MODE_PROP_RANGE;
1494N/A
1494N/A property = drm_property_create(dev, flags, name, 2);
1494N/A if (!property)
1494N/A return NULL;
1494N/A
1494N/A property->values[0] = min;
1494N/A property->values[1] = max;
1494N/A
1494N/A return property;
1494N/A}
1494N/A
1494N/Aint drm_property_add_enum(struct drm_property *property, int index,
1494N/A uint64_t value, const char *name)
1494N/A{
1494N/A struct drm_property_enum *prop_enum;
1494N/A
1494N/A if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
1494N/A return -EINVAL;
1494N/A
1494N/A /*
1494N/A * Bitmask enum properties have the additional constraint of values
1494N/A * from 0 to 63
1494N/A */
1494N/A if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
1494N/A return -EINVAL;
1494N/A
1494N/A if (!list_empty(&property->enum_blob_list)) {
1494N/A list_for_each_entry(prop_enum, struct drm_property_enum, &property->enum_blob_list, head) {
1494N/A if (prop_enum->value == value) {
1494N/A (void) strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1494N/A prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1494N/A return 0;
1494N/A }
1494N/A }
1494N/A }
1494N/A
1494N/A prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
1494N/A if (!prop_enum)
1494N/A return -ENOMEM;
1494N/A
1494N/A (void) strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1494N/A prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1494N/A prop_enum->value = value;
1494N/A
1494N/A property->values[index] = value;
1494N/A list_add_tail(&prop_enum->head, &property->enum_blob_list, (caddr_t)prop_enum);
1494N/A return 0;
1494N/A}
1494N/A
1494N/Avoid drm_property_destroy(struct drm_device *dev, struct drm_property *property)
1494N/A{
1494N/A struct drm_property_enum *prop_enum, *pt;
1494N/A
1494N/A list_for_each_entry_safe(prop_enum, pt, struct drm_property_enum, &property->enum_blob_list, head) {
1494N/A list_del(&prop_enum->head);
1494N/A kfree(prop_enum, sizeof(struct drm_property_enum));
1494N/A }
1494N/A
1494N/A if (property->num_values)
1494N/A kfree(property->values, sizeof(uint64_t) * property->num_values);
1494N/A drm_mode_object_put(dev, &property->base);
1494N/A list_del(&property->head);
1494N/A kfree(property, sizeof(struct drm_property));
1494N/A}
1494N/A
1494N/A
1494N/Avoid drm_object_attach_property(struct drm_mode_object *obj,
1494N/A struct drm_property *property,
1494N/A uint64_t init_val)
1494N/A{
1494N/A int count = obj->properties->count;
1494N/A
1494N/A if (count == DRM_OBJECT_MAX_PROPERTY) {
1494N/A DRM_ERROR("Failed to attach object property (type: 0x%x). Please "
1494N/A "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
1494N/A "you see this message on the same object type.\n",
1494N/A obj->type);
1494N/A return;
1494N/A }
1494N/A
1494N/A obj->properties->ids[count] = property->base.id;
1494N/A obj->properties->values[count] = init_val;
1494N/A obj->properties->count++;
1494N/A}
1494N/A
1494N/Aint drm_object_property_set_value(struct drm_mode_object *obj,
1494N/A struct drm_property *property, uint64_t val)
1494N/A{
1494N/A int i;
1494N/A
1494N/A for (i = 0; i < obj->properties->count; i++) {
1494N/A if (obj->properties->ids[i] == property->base.id) {
1494N/A obj->properties->values[i] = val;
1494N/A return 0;
1494N/A }
1494N/A }
1494N/A
1494N/A return -EINVAL;
1494N/A}
1494N/A
1494N/Aint drm_object_property_get_value(struct drm_mode_object *obj,
1494N/A struct drm_property *property, uint64_t *val)
1494N/A{
1494N/A int i;
1494N/A
1494N/A for (i = 0; i < obj->properties->count; i++) {
1494N/A if (obj->properties->ids[i] == property->base.id) {
1494N/A *val = obj->properties->values[i];
1494N/A return 0;
1494N/A }
1494N/A }
1494N/A
1494N/A return -EINVAL;
1494N/A}
1494N/A
1494N/A/* LINTED */
1494N/Aint drm_mode_getproperty_ioctl(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_mode_object *obj;
1494N/A struct drm_mode_get_property *out_resp = data;
1494N/A struct drm_property *property;
1494N/A int enum_count = 0;
1494N/A int blob_count = 0;
1494N/A int value_count = 0;
1494N/A int ret = 0, i;
1494N/A int copied;
1494N/A struct drm_property_enum *prop_enum;
1494N/A struct drm_mode_property_enum __user *enum_ptr;
1494N/A struct drm_property_blob *prop_blob;
1494N/A uint32_t *blob_id_ptr;
1494N/A uint64_t *values_ptr;
1494N/A uint32_t *blob_length_ptr;
1494N/A
1494N/A if (!drm_core_check_feature(dev, DRIVER_MODESET))
1494N/A return -EINVAL;
1494N/A
1494N/A drm_modeset_lock_all(dev);
1494N/A obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
1494N/A if (!obj) {
1494N/A ret = -EINVAL;
1494N/A goto done;
1494N/A }
1494N/A property = obj_to_property(obj);
1494N/A
1494N/A if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
1494N/A list_for_each_entry(prop_enum, struct drm_property_enum, &property->enum_blob_list, head)
1494N/A enum_count++;
1494N/A } else if (property->flags & DRM_MODE_PROP_BLOB) {
1494N/A list_for_each_entry(prop_blob, struct drm_property_blob, &property->enum_blob_list, head)
1494N/A blob_count++;
1494N/A }
1494N/A
1494N/A value_count = property->num_values;
1494N/A
1494N/A (void) strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
1494N/A out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
1494N/A out_resp->flags = property->flags;
1494N/A
1494N/A if ((out_resp->count_values >= value_count) && value_count) {
1494N/A values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
1494N/A for (i = 0; i < value_count; i++) {
1494N/A if (DRM_COPY_TO_USER(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
1494N/A ret = -EFAULT;
1494N/A goto done;
1494N/A }
1494N/A }
1494N/A }
1494N/A out_resp->count_values = value_count;
1494N/A
1494N/A if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
1494N/A if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
1494N/A copied = 0;
1494N/A enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
1494N/A list_for_each_entry(prop_enum, struct drm_property_enum, &property->enum_blob_list, head) {
1494N/A
1494N/A if (DRM_COPY_TO_USER(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
1494N/A ret = -EFAULT;
1494N/A goto done;
1494N/A }
1494N/A
1494N/A if (DRM_COPY_TO_USER(&enum_ptr[copied].name,
1494N/A &prop_enum->name, DRM_PROP_NAME_LEN)) {
1494N/A ret = -EFAULT;
1494N/A goto done;
1494N/A }
1494N/A copied++;
1494N/A }
1494N/A }
1494N/A out_resp->count_enum_blobs = enum_count;
1494N/A }
1494N/A
1494N/A if (property->flags & DRM_MODE_PROP_BLOB) {
1494N/A if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
1494N/A copied = 0;
1494N/A blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
1494N/A blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
1494N/A
1494N/A list_for_each_entry(prop_blob, struct drm_property_blob, &property->enum_blob_list, head) {
1494N/A if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
1494N/A ret = -EFAULT;
1494N/A goto done;
1494N/A }
1494N/A
1494N/A if (put_user(prop_blob->length, blob_length_ptr + copied)) {
1494N/A ret = -EFAULT;
1494N/A goto done;
1494N/A }
1494N/A
1494N/A copied++;
1494N/A }
1494N/A }
1494N/A out_resp->count_enum_blobs = blob_count;
1494N/A }
1494N/Adone:
1494N/A drm_modeset_unlock_all(dev);
1494N/A return ret;
1494N/A}
1494N/A
1494N/Astatic struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
1494N/A void *data)
1494N/A{
1494N/A struct drm_property_blob *blob;
1494N/A int ret;
1494N/A
1494N/A if (!length || !data)
1494N/A return NULL;
1494N/A
1494N/A blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
1494N/A if (!blob)
1494N/A return NULL;
1494N/A
1494N/A ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
1494N/A if (ret) {
1494N/A kfree(blob, sizeof(struct drm_property_blob)+length);
1494N/A return NULL;
1494N/A }
1494N/A
1494N/A blob->length = length;
1494N/A
1494N/A (void) memcpy(blob->data, data, length);
1494N/A
1494N/A list_add_tail(&blob->head, &dev->mode_config.property_blob_list, (caddr_t)blob);
1494N/A return blob;
1494N/A}
1494N/A
1494N/Astatic void drm_property_destroy_blob(struct drm_device *dev,
1494N/A struct drm_property_blob *blob)
1494N/A{
1494N/A drm_mode_object_put(dev, &blob->base);
1494N/A list_del(&blob->head);
1494N/A kfree(blob, sizeof(struct drm_property_blob) + blob->length);
1494N/A}
1494N/A
1494N/A/* LINTED */
1494N/Aint drm_mode_getblob_ioctl(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_mode_object *obj;
1494N/A struct drm_mode_get_blob *out_resp = data;
1494N/A struct drm_property_blob *blob;
1494N/A int ret = 0;
1494N/A void *blob_ptr;
1494N/A
1494N/A if (!drm_core_check_feature(dev, DRIVER_MODESET))
1494N/A return -EINVAL;
1494N/A
1494N/A drm_modeset_lock_all(dev);
1494N/A obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
1494N/A if (!obj) {
1494N/A ret = -EINVAL;
1494N/A goto done;
1494N/A }
1494N/A blob = obj_to_blob(obj);
1494N/A
1494N/A if (out_resp->length == blob->length) {
1494N/A blob_ptr = (void *)(unsigned long)out_resp->data;
1494N/A if (DRM_COPY_TO_USER(blob_ptr, blob->data, blob->length)){
1494N/A ret = -EFAULT;
1494N/A goto done;
1494N/A }
1494N/A }
1494N/A out_resp->length = blob->length;
1494N/A
1494N/Adone:
1494N/A drm_modeset_unlock_all(dev);
1494N/A return ret;
1494N/A}
1494N/A
1494N/Aint drm_mode_connector_update_edid_property(struct drm_connector *connector,
1494N/A struct edid *edid)
1494N/A{
1494N/A struct drm_device *dev = connector->dev;
1494N/A int ret, size;
1494N/A
1494N/A if (connector->edid_blob_ptr)
1494N/A drm_property_destroy_blob(dev, connector->edid_blob_ptr);
1494N/A
1494N/A /* Delete edid, when there is none. */
1494N/A if (!edid) {
1494N/A connector->edid_blob_ptr = NULL;
1494N/A ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
1494N/A return ret;
1494N/A }
1494N/A
1494N/A size = EDID_LENGTH * (1 + edid->extensions);
1494N/A connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
1494N/A size, edid);
1494N/A if (!connector->edid_blob_ptr)
1494N/A return -EINVAL;
1494N/A
1494N/A ret = drm_object_property_set_value(&connector->base,
1494N/A dev->mode_config.edid_property,
1494N/A connector->edid_blob_ptr->base.id);
1494N/A
1494N/A return ret;
1494N/A}
1494N/A
1494N/Astatic bool drm_property_change_is_valid(struct drm_property *property,
1494N/A uint64_t value)
1494N/A{
1494N/A if (property->flags & DRM_MODE_PROP_IMMUTABLE)
1494N/A return false;
1494N/A if (property->flags & DRM_MODE_PROP_RANGE) {
1494N/A if (value < property->values[0] || value > property->values[1])
1494N/A return false;
1494N/A return true;
1494N/A } else if (property->flags & DRM_MODE_PROP_BITMASK) {
1494N/A int i;
1494N/A uint64_t valid_mask = 0;
1494N/A for (i = 0; i < property->num_values; i++)
1494N/A valid_mask |= (1ULL << property->values[i]);
1494N/A return !(value & ~valid_mask);
1494N/A } else if (property->flags & DRM_MODE_PROP_BLOB) {
1494N/A /* Only the driver knows */
1494N/A return true;
1494N/A } else {
1494N/A int i;
1494N/A for (i = 0; i < property->num_values; i++)
1494N/A if (property->values[i] == value)
1494N/A return true;
1494N/A return false;
1494N/A }
1494N/A}
1494N/A
1494N/Aint drm_mode_connector_property_set_ioctl(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_mode_connector_set_property *conn_set_prop = data;
1494N/A struct drm_mode_obj_set_property obj_set_prop = {
1494N/A .value = conn_set_prop->value,
1494N/A .prop_id = conn_set_prop->prop_id,
1494N/A .obj_id = conn_set_prop->connector_id,
1494N/A .obj_type = DRM_MODE_OBJECT_CONNECTOR
1494N/A };
1494N/A
1494N/A /* It does all the locking and checking we need */
1494N/A return drm_mode_obj_set_property_ioctl(dev_id, dev, &obj_set_prop, file, ioctl_mode, credp);
1494N/A}
1494N/A
1494N/Astatic int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
1494N/A struct drm_property *property,
1494N/A uint64_t value)
1494N/A{
1494N/A int ret = -EINVAL;
1494N/A struct drm_connector *connector = obj_to_connector(obj);
1494N/A
1494N/A /* Do DPMS ourselves */
1494N/A if (property == connector->dev->mode_config.dpms_property) {
1494N/A if (connector->funcs->dpms)
1494N/A (*connector->funcs->dpms)(connector, (int)value);
1494N/A ret = 0;
1494N/A } else if (connector->funcs->set_property)
1494N/A ret = connector->funcs->set_property(connector, property, value);
1494N/A
1494N/A /* store the property value if successful */
1494N/A if (!ret)
1494N/A ret = drm_object_property_set_value(&connector->base, property, value);
1494N/A return ret;
1494N/A}
1494N/A
1494N/Astatic int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
1494N/A struct drm_property *property,
1494N/A uint64_t value)
1494N/A{
1494N/A int ret = -EINVAL;
1494N/A struct drm_crtc *crtc = obj_to_crtc(obj);
1494N/A
1494N/A if (crtc->funcs->set_property)
1494N/A ret = crtc->funcs->set_property(crtc, property, value);
1494N/A if (!ret)
1494N/A ret = drm_object_property_set_value(obj, property, value);
1494N/A
1494N/A return ret;
1494N/A}
1494N/A
1494N/Astatic int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
1494N/A struct drm_property *property,
1494N/A uint64_t value)
1494N/A{
1494N/A int ret = -EINVAL;
1494N/A struct drm_plane *plane = obj_to_plane(obj);
1494N/A
1494N/A if (plane->funcs->set_property)
1494N/A ret = plane->funcs->set_property(plane, property, value);
1494N/A if (!ret)
1494N/A ret = drm_object_property_set_value(obj, property, value);
1494N/A
1494N/A return ret;
1494N/A}
1494N/A
1494N/A/* LINTED */
1494N/Aint drm_mode_obj_get_properties_ioctl(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_mode_obj_get_properties *arg = data;
1494N/A struct drm_mode_object *obj;
1494N/A int ret = 0;
1494N/A int i;
1494N/A int copied = 0;
1494N/A int props_count = 0;
1494N/A uint32_t __user *props_ptr;
1494N/A uint64_t __user *prop_values_ptr;
1494N/A
1494N/A if (!drm_core_check_feature(dev, DRIVER_MODESET))
1494N/A return -EINVAL;
1494N/A
1494N/A drm_modeset_lock_all(dev);
1494N/A
1494N/A obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
1494N/A if (!obj) {
1494N/A ret = -EINVAL;
1494N/A goto out;
1494N/A }
1494N/A if (!obj->properties) {
1494N/A ret = -EINVAL;
1494N/A goto out;
1494N/A }
1494N/A
1494N/A props_count = obj->properties->count;
1494N/A
1494N/A /* This ioctl is called twice, once to determine how much space is
1494N/A * needed, and the 2nd time to fill it. */
1494N/A if ((arg->count_props >= props_count) && props_count) {
1494N/A copied = 0;
1494N/A props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1494N/A prop_values_ptr = (uint64_t __user *)(unsigned long)
1494N/A (arg->prop_values_ptr);
1494N/A for (i = 0; i < props_count; i++) {
1494N/A if (put_user(obj->properties->ids[i],
1494N/A props_ptr + copied)) {
1494N/A ret = -EFAULT;
1494N/A goto out;
1494N/A }
1494N/A if (put_user(obj->properties->values[i],
1494N/A prop_values_ptr + copied)) {
1494N/A ret = -EFAULT;
1494N/A goto out;
1494N/A }
1494N/A copied++;
1494N/A }
1494N/A }
1494N/A arg->count_props = props_count;
1494N/Aout:
1494N/A drm_modeset_unlock_all(dev);
1494N/A return ret;
1494N/A}
1494N/A
1494N/A/* LINTED */
1494N/Aint drm_mode_obj_set_property_ioctl(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_mode_obj_set_property *arg = data;
1494N/A struct drm_mode_object *arg_obj;
1494N/A struct drm_mode_object *prop_obj;
1494N/A struct drm_property *property;
1494N/A int ret = -EINVAL;
1494N/A int i;
1494N/A
1494N/A if (!drm_core_check_feature(dev, DRIVER_MODESET))
1494N/A return -EINVAL;
1494N/A
1494N/A drm_modeset_lock_all(dev);
1494N/A
1494N/A arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
1494N/A if (!arg_obj)
1494N/A goto out;
1494N/A if (!arg_obj->properties)
1494N/A goto out;
1494N/A
1494N/A for (i = 0; i < arg_obj->properties->count; i++)
1494N/A if (arg_obj->properties->ids[i] == arg->prop_id)
1494N/A break;
1494N/A
1494N/A if (i == arg_obj->properties->count)
1494N/A goto out;
1494N/A
1494N/A prop_obj = drm_mode_object_find(dev, arg->prop_id,
1494N/A DRM_MODE_OBJECT_PROPERTY);
1494N/A if (!prop_obj)
1494N/A goto out;
1494N/A property = obj_to_property(prop_obj);
1494N/A
1494N/A if (!drm_property_change_is_valid(property, arg->value))
1494N/A goto out;
1494N/A
1494N/A switch (arg_obj->type) {
1494N/A case DRM_MODE_OBJECT_CONNECTOR:
1494N/A ret = drm_mode_connector_set_obj_prop(arg_obj, property,
1494N/A arg->value);
1494N/A break;
1494N/A case DRM_MODE_OBJECT_CRTC:
1494N/A ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
1494N/A break;
1494N/A case DRM_MODE_OBJECT_PLANE:
1494N/A ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
1494N/A break;
1494N/A }
1494N/A
1494N/Aout:
1494N/A drm_modeset_unlock_all(dev);
1494N/A return ret;
1494N/A}
1494N/A
1494N/Aint drm_mode_connector_attach_encoder(struct drm_connector *connector,
1494N/A struct drm_encoder *encoder)
1494N/A{
1494N/A int i;
1494N/A
1494N/A for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1494N/A if (connector->encoder_ids[i] == 0) {
1494N/A connector->encoder_ids[i] = encoder->base.id;
1494N/A return 0;
1494N/A }
1494N/A }
1494N/A return -ENOMEM;
1494N/A}
1494N/A
1494N/Avoid drm_mode_connector_detach_encoder(struct drm_connector *connector,
1494N/A struct drm_encoder *encoder)
1494N/A{
1494N/A int i;
1494N/A for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1494N/A if (connector->encoder_ids[i] == encoder->base.id) {
1494N/A connector->encoder_ids[i] = 0;
1494N/A if (connector->encoder == encoder)
1494N/A connector->encoder = NULL;
1494N/A break;
1494N/A }
1494N/A }
1494N/A}
1494N/A
1494N/Aint drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
1494N/A int gamma_size)
1494N/A{
1494N/A crtc->gamma_size = gamma_size;
1494N/A
1494N/A crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
1494N/A if (!crtc->gamma_store) {
1494N/A crtc->gamma_size = 0;
1494N/A return -ENOMEM;
1494N/A }
1494N/A
1494N/A return 0;
1494N/A}
1494N/A
1494N/A/* LINTED */
1494N/Aint drm_mode_gamma_set_ioctl(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_mode_crtc_lut *crtc_lut = data;
1494N/A struct drm_mode_object *obj;
1494N/A struct drm_crtc *crtc;
1494N/A void *r_base, *g_base, *b_base;
1494N/A int size;
1494N/A int ret = 0;
1494N/A
1494N/A if (!drm_core_check_feature(dev, DRIVER_MODESET))
1494N/A return -EINVAL;
1494N/A
1494N/A drm_modeset_lock_all(dev);
1494N/A obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
1494N/A if (!obj) {
1494N/A ret = -EINVAL;
1494N/A goto out;
1494N/A }
1494N/A crtc = obj_to_crtc(obj);
1494N/A
1494N/A if (crtc->funcs->gamma_set == NULL) {
1494N/A ret = -ENOSYS;
1494N/A goto out;
1494N/A }
1494N/A
1494N/A /* memcpy into gamma store */
1494N/A if (crtc_lut->gamma_size != crtc->gamma_size) {
1494N/A ret = -EINVAL;
1494N/A goto out;
1494N/A }
1494N/A
1494N/A size = crtc_lut->gamma_size * (sizeof(uint16_t));
1494N/A r_base = crtc->gamma_store;
1494N/A if (DRM_COPY_FROM_USER(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
1494N/A ret = -EFAULT;
1494N/A goto out;
1494N/A }
1494N/A
1494N/A g_base = (void *)((uintptr_t)r_base + size);
1494N/A if (DRM_COPY_FROM_USER(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
1494N/A ret = -EFAULT;
1494N/A goto out;
1494N/A }
1494N/A
1494N/A b_base = (void *)((uintptr_t)g_base + size);
1494N/A if (DRM_COPY_FROM_USER(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
1494N/A ret = -EFAULT;
1494N/A goto out;
1494N/A }
1494N/A
1494N/A crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
1494N/A
1494N/Aout:
1494N/A drm_modeset_unlock_all(dev);
1494N/A return ret;
1494N/A
1494N/A}
1494N/A
1494N/A/* LINTED */
1494N/Aint drm_mode_gamma_get_ioctl(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_mode_crtc_lut *crtc_lut = data;
1494N/A struct drm_mode_object *obj;
1494N/A struct drm_crtc *crtc;
1494N/A void *r_base, *g_base, *b_base;
1494N/A int size;
1494N/A int ret = 0;
1494N/A
1494N/A if (!drm_core_check_feature(dev, DRIVER_MODESET))
1494N/A return -EINVAL;
1494N/A
1494N/A drm_modeset_lock_all(dev);
1494N/A obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
1494N/A if (!obj) {
1494N/A ret = -EINVAL;
1494N/A goto out;
1494N/A }
1494N/A crtc = obj_to_crtc(obj);
1494N/A
1494N/A /* memcpy into gamma store */
1494N/A if (crtc_lut->gamma_size != crtc->gamma_size) {
1494N/A ret = -EINVAL;
1494N/A goto out;
1494N/A }
1494N/A
1494N/A size = crtc_lut->gamma_size * (sizeof(uint16_t));
1494N/A r_base = crtc->gamma_store;
1494N/A if (DRM_COPY_TO_USER((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
1494N/A ret = -EFAULT;
1494N/A goto out;
1494N/A }
1494N/A
1494N/A g_base =(void *)((long)r_base + size);
1494N/A if (DRM_COPY_TO_USER((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
1494N/A ret = -EFAULT;
1494N/A goto out;
1494N/A }
1494N/A
1494N/A b_base = (void *)((long)g_base + size);
1494N/A if (DRM_COPY_TO_USER((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
1494N/A ret = -EFAULT;
1494N/A goto out;
1494N/A }
1494N/Aout:
1494N/A drm_modeset_unlock_all(dev);
1494N/A return ret;
1494N/A}
1494N/A
1494N/A/* LINTED */
1494N/Aint drm_mode_page_flip_ioctl(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_mode_crtc_page_flip *page_flip = data;
1494N/A struct drm_mode_object *obj;
1494N/A struct drm_crtc *crtc;
1494N/A struct drm_framebuffer *fb = NULL, *old_fb = NULL;
1494N/A struct drm_pending_vblank_event *e = NULL;
1494N/A unsigned long flags;
1494N/A int hdisplay, vdisplay;
1494N/A int ret = -EINVAL;
1494N/A
1494N/A if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
1494N/A page_flip->reserved != 0)
1494N/A return -EINVAL;
1494N/A
1494N/A obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
1494N/A if (!obj)
1494N/A return -EINVAL;
1494N/A crtc = obj_to_crtc(obj);
1494N/A
1494N/A mutex_lock(&crtc->mutex);
1494N/A if (crtc->fb == NULL) {
1494N/A /* The framebuffer is currently unbound, presumably
1494N/A * due to a hotplug event, that userspace has not
1494N/A * yet discovered.
1494N/A */
1494N/A ret = -EBUSY;
1494N/A goto out;
1494N/A }
1494N/A
1494N/A if (crtc->funcs->page_flip == NULL)
1494N/A goto out;
1494N/A
1494N/A fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
1494N/A if (!fb)
1494N/A goto out;
1494N/A
1494N/A hdisplay = crtc->mode.hdisplay;
1494N/A vdisplay = crtc->mode.vdisplay;
1494N/A
1494N/A if (crtc->invert_dimensions)
1494N/A swap(hdisplay, vdisplay);
1494N/A
1494N/A if (hdisplay > fb->width ||
1494N/A vdisplay > fb->height ||
1494N/A crtc->x > fb->width - hdisplay ||
1494N/A crtc->y > fb->height - vdisplay) {
1494N/A DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
1494N/A fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
1494N/A crtc->invert_dimensions ? " (inverted)" : "");
1494N/A ret = -ENOSPC;
1494N/A goto out;
1494N/A }
1494N/A
1494N/A if (crtc->fb->pixel_format != fb->pixel_format) {
1494N/A DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
1494N/A ret = -EINVAL;
1494N/A goto out;
1494N/A }
1494N/A
1494N/A if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1494N/A ret = -ENOMEM;
1494N/A spin_lock_irqsave(&dev->event_lock, flags);
1494N/A if (file->event_space < sizeof e->event) {
1494N/A spin_unlock_irqrestore(&dev->event_lock, flags);
1494N/A goto out;
1494N/A }
1494N/A file->event_space -= sizeof e->event;
1494N/A spin_unlock_irqrestore(&dev->event_lock, flags);
1494N/A
1494N/A e = kzalloc(sizeof *e, GFP_KERNEL);
1494N/A if (e == NULL) {
1494N/A spin_lock_irqsave(&dev->event_lock, flags);
1494N/A file->event_space += sizeof e->event;
1494N/A spin_unlock_irqrestore(&dev->event_lock, flags);
1494N/A goto out;
1494N/A }
1494N/A
1494N/A e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1494N/A e->event.base.length = sizeof e->event;
1494N/A e->event.user_data = page_flip->user_data;
1494N/A e->base.event = &e->event.base;
1494N/A e->base.file_priv = file;
1494N/A e->base.destroy =
1494N/A (void (*) (void *, size_t)) kfree;
1494N/A }
1494N/A
1494N/A old_fb = crtc->fb;
1494N/A ret = crtc->funcs->page_flip(crtc, fb, e);
1494N/A if (ret) {
1494N/A if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1494N/A spin_lock_irqsave(&dev->event_lock, flags);
1494N/A file->event_space += sizeof e->event;
1494N/A spin_unlock_irqrestore(&dev->event_lock, flags);
1494N/A kfree(e, sizeof(*e));
1494N/A }
1494N/A /* Keep the old fb, don't unref it. */
1494N/A old_fb = NULL;
1494N/A } else {
1494N/A /*
1494N/A * Warn if the driver hasn't properly updated the crtc->fb
1494N/A * field to reflect that the new framebuffer is now used.
1494N/A * Failing to do so will screw with the reference counting
1494N/A * on framebuffers.
1494N/A */
1494N/A WARN_ON(crtc->fb != fb);
1494N/A /* Unref only the old framebuffer. */
1494N/A fb = NULL;
1494N/A }
1494N/A
1494N/Aout:
1494N/A if (fb)
1494N/A drm_framebuffer_unreference(fb);
1494N/A if (old_fb)
1494N/A drm_framebuffer_unreference(old_fb);
1494N/A mutex_unlock(&crtc->mutex);
1494N/A
1494N/A return ret;
1494N/A}
1494N/A
1494N/Avoid drm_mode_config_reset(struct drm_device *dev)
1494N/A{
1494N/A struct drm_crtc *crtc;
1494N/A struct drm_encoder *encoder;
1494N/A struct drm_connector *connector;
1494N/A
1494N/A list_for_each_entry(crtc, struct drm_crtc, &dev->mode_config.crtc_list, head)
1494N/A if (crtc->funcs->reset)
1494N/A crtc->funcs->reset(crtc);
1494N/A
1494N/A list_for_each_entry(encoder, struct drm_encoder, &dev->mode_config.encoder_list, head)
1494N/A if (encoder->funcs->reset)
1494N/A encoder->funcs->reset(encoder);
1494N/A
1494N/A list_for_each_entry(connector, struct drm_connector, &dev->mode_config.connector_list, head)
1494N/A if (connector->funcs->reset)
1494N/A connector->funcs->reset(connector);
1494N/A}
1494N/A
1494N/A/* LINTED E_FUNC_ARG_UNUSED */
1494N/Aint drm_mode_create_dumb_ioctl(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_mode_create_dumb *args = data;
1494N/A
1494N/A if (!dev->driver->dumb_create)
1494N/A return -ENOSYS;
1494N/A return dev->driver->dumb_create(file, dev, args);
1494N/A}
1494N/A
1494N/A/* LINTED E_FUNC_ARG_UNUSED */
1494N/Aint drm_mode_mmap_dumb_ioctl(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_mode_map_dumb *args = data;
1494N/A
1494N/A /* call driver ioctl to get mmap offset */
1494N/A if (!dev->driver->dumb_map_offset)
1494N/A return -ENOSYS;
1494N/A
1494N/A return dev->driver->dumb_map_offset(file, dev, args->handle, &args->offset);
1494N/A}
1494N/A
1494N/A/* LINTED E_FUNC_ARG_UNUSED */
1494N/Aint drm_mode_destroy_dumb_ioctl(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_mode_destroy_dumb *args = data;
1494N/A
1494N/A if (!dev->driver->dumb_destroy)
1494N/A return -ENOSYS;
1494N/A
1494N/A return dev->driver->dumb_destroy(file, dev, args->handle);
1494N/A}
1494N/A
1494N/A/*
1494N/A * Just need to support RGB formats here for compat with code that doesn't
1494N/A * use pixel formats directly yet.
1494N/A */
1494N/Avoid drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
1494N/A int *bpp)
1494N/A{
1494N/A switch (format) {
1494N/A case DRM_FORMAT_C8:
1494N/A case DRM_FORMAT_RGB332:
1494N/A case DRM_FORMAT_BGR233:
1494N/A *depth = 8;
1494N/A *bpp = 8;
1494N/A break;
1494N/A case DRM_FORMAT_XRGB1555:
1494N/A case DRM_FORMAT_XBGR1555:
1494N/A case DRM_FORMAT_RGBX5551:
1494N/A case DRM_FORMAT_BGRX5551:
1494N/A case DRM_FORMAT_ARGB1555:
1494N/A case DRM_FORMAT_ABGR1555:
1494N/A case DRM_FORMAT_RGBA5551:
1494N/A case DRM_FORMAT_BGRA5551:
1494N/A *depth = 15;
1494N/A *bpp = 16;
1494N/A break;
1494N/A case DRM_FORMAT_RGB565:
1494N/A case DRM_FORMAT_BGR565:
1494N/A *depth = 16;
1494N/A *bpp = 16;
1494N/A break;
1494N/A case DRM_FORMAT_RGB888:
1494N/A case DRM_FORMAT_BGR888:
1494N/A *depth = 24;
1494N/A *bpp = 24;
1494N/A break;
1494N/A case DRM_FORMAT_XRGB8888:
1494N/A case DRM_FORMAT_XBGR8888:
1494N/A case DRM_FORMAT_RGBX8888:
1494N/A case DRM_FORMAT_BGRX8888:
1494N/A *depth = 24;
1494N/A *bpp = 32;
1494N/A break;
1494N/A case DRM_FORMAT_XRGB2101010:
1494N/A case DRM_FORMAT_XBGR2101010:
1494N/A case DRM_FORMAT_RGBX1010102:
1494N/A case DRM_FORMAT_BGRX1010102:
1494N/A case DRM_FORMAT_ARGB2101010:
1494N/A case DRM_FORMAT_ABGR2101010:
1494N/A case DRM_FORMAT_RGBA1010102:
1494N/A case DRM_FORMAT_BGRA1010102:
1494N/A *depth = 30;
1494N/A *bpp = 32;
1494N/A break;
1494N/A case DRM_FORMAT_ARGB8888:
1494N/A case DRM_FORMAT_ABGR8888:
1494N/A case DRM_FORMAT_RGBA8888:
1494N/A case DRM_FORMAT_BGRA8888:
1494N/A *depth = 32;
1494N/A *bpp = 32;
1494N/A break;
1494N/A default:
1494N/A DRM_DEBUG_KMS("unsupported pixel format\n");
1494N/A *depth = 0;
1494N/A *bpp = 0;
1494N/A break;
1494N/A }
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_format_num_planes - get the number of planes for format
1494N/A * @format: pixel format (DRM_FORMAT_*)
1494N/A *
1494N/A * RETURNS:
1494N/A * The number of planes used by the specified pixel format.
1494N/A */
1494N/Aint drm_format_num_planes(uint32_t format)
1494N/A{
1494N/A switch (format) {
1494N/A case DRM_FORMAT_YUV410:
1494N/A case DRM_FORMAT_YVU410:
1494N/A case DRM_FORMAT_YUV411:
1494N/A case DRM_FORMAT_YVU411:
1494N/A case DRM_FORMAT_YUV420:
1494N/A case DRM_FORMAT_YVU420:
1494N/A case DRM_FORMAT_YUV422:
1494N/A case DRM_FORMAT_YVU422:
1494N/A case DRM_FORMAT_YUV444:
1494N/A case DRM_FORMAT_YVU444:
1494N/A return 3;
1494N/A case DRM_FORMAT_NV12:
1494N/A case DRM_FORMAT_NV21:
1494N/A case DRM_FORMAT_NV16:
1494N/A case DRM_FORMAT_NV61:
1494N/A case DRM_FORMAT_NV24:
1494N/A case DRM_FORMAT_NV42:
1494N/A return 2;
1494N/A default:
1494N/A return 1;
1494N/A }
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_format_plane_cpp - determine the bytes per pixel value
1494N/A * @format: pixel format (DRM_FORMAT_*)
1494N/A * @plane: plane index
1494N/A *
1494N/A * RETURNS:
1494N/A * The bytes per pixel value for the specified plane.
1494N/A */
1494N/Aint drm_format_plane_cpp(uint32_t format, int plane)
1494N/A{
1494N/A unsigned int depth;
1494N/A int bpp;
1494N/A
1494N/A if (plane >= drm_format_num_planes(format))
1494N/A return 0;
1494N/A
1494N/A switch (format) {
1494N/A case DRM_FORMAT_YUYV:
1494N/A case DRM_FORMAT_YVYU:
1494N/A case DRM_FORMAT_UYVY:
1494N/A case DRM_FORMAT_VYUY:
1494N/A return 2;
1494N/A case DRM_FORMAT_NV12:
1494N/A case DRM_FORMAT_NV21:
1494N/A case DRM_FORMAT_NV16:
1494N/A case DRM_FORMAT_NV61:
1494N/A case DRM_FORMAT_NV24:
1494N/A case DRM_FORMAT_NV42:
1494N/A return plane ? 2 : 1;
1494N/A case DRM_FORMAT_YUV410:
1494N/A case DRM_FORMAT_YVU410:
1494N/A case DRM_FORMAT_YUV411:
1494N/A case DRM_FORMAT_YVU411:
1494N/A case DRM_FORMAT_YUV420:
1494N/A case DRM_FORMAT_YVU420:
1494N/A case DRM_FORMAT_YUV422:
1494N/A case DRM_FORMAT_YVU422:
1494N/A case DRM_FORMAT_YUV444:
1494N/A case DRM_FORMAT_YVU444:
1494N/A return 1;
1494N/A default:
1494N/A drm_fb_get_bpp_depth(format, &depth, &bpp);
1494N/A return bpp >> 3;
1494N/A }
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
1494N/A * @format: pixel format (DRM_FORMAT_*)
1494N/A *
1494N/A * RETURNS:
1494N/A * The horizontal chroma subsampling factor for the
1494N/A * specified pixel format.
1494N/A */
1494N/Aint drm_format_horz_chroma_subsampling(uint32_t format)
1494N/A{
1494N/A switch (format) {
1494N/A case DRM_FORMAT_YUV411:
1494N/A case DRM_FORMAT_YVU411:
1494N/A case DRM_FORMAT_YUV410:
1494N/A case DRM_FORMAT_YVU410:
1494N/A return 4;
1494N/A case DRM_FORMAT_YUYV:
1494N/A case DRM_FORMAT_YVYU:
1494N/A case DRM_FORMAT_UYVY:
1494N/A case DRM_FORMAT_VYUY:
1494N/A case DRM_FORMAT_NV12:
1494N/A case DRM_FORMAT_NV21:
1494N/A case DRM_FORMAT_NV16:
1494N/A case DRM_FORMAT_NV61:
1494N/A case DRM_FORMAT_YUV422:
1494N/A case DRM_FORMAT_YVU422:
1494N/A case DRM_FORMAT_YUV420:
1494N/A case DRM_FORMAT_YVU420:
1494N/A return 2;
1494N/A default:
1494N/A return 1;
1494N/A }
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
1494N/A * @format: pixel format (DRM_FORMAT_*)
1494N/A *
1494N/A * RETURNS:
1494N/A * The vertical chroma subsampling factor for the
1494N/A * specified pixel format.
1494N/A */
1494N/Aint drm_format_vert_chroma_subsampling(uint32_t format)
1494N/A{
1494N/A switch (format) {
1494N/A case DRM_FORMAT_YUV410:
1494N/A case DRM_FORMAT_YVU410:
1494N/A return 4;
1494N/A case DRM_FORMAT_YUV420:
1494N/A case DRM_FORMAT_YVU420:
1494N/A case DRM_FORMAT_NV12:
1494N/A case DRM_FORMAT_NV21:
1494N/A return 2;
1494N/A default:
1494N/A return 1;
1494N/A }
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_mode_config_init - initialize DRM mode_configuration structure
1494N/A * @dev: DRM device
1494N/A *
1494N/A * Initialize @dev's mode_config structure, used for tracking the graphics
1494N/A * configuration of @dev.
1494N/A *
1494N/A * Since this initializes the modeset locks, no locking is possible. Which is no
1494N/A * problem, since this should happen single threaded at init time. It is the
1494N/A * driver's problem to ensure this guarantee.
1494N/A *
1494N/A */
1494N/Avoid drm_mode_config_init(struct drm_device *dev)
1494N/A{
1494N/A mutex_init(&dev->mode_config.mutex, NULL, MUTEX_DRIVER, NULL);
1494N/A mutex_init(&dev->mode_config.idr_mutex, NULL, MUTEX_DRIVER, NULL);
1494N/A mutex_init(&dev->mode_config.fb_lock, NULL, MUTEX_DRIVER, NULL);
1494N/A INIT_LIST_HEAD(&dev->mode_config.fb_list);
1494N/A INIT_LIST_HEAD(&dev->mode_config.crtc_list);
1494N/A INIT_LIST_HEAD(&dev->mode_config.connector_list);
1494N/A INIT_LIST_HEAD(&dev->mode_config.encoder_list);
1494N/A INIT_LIST_HEAD(&dev->mode_config.property_list);
1494N/A INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
1494N/A INIT_LIST_HEAD(&dev->mode_config.plane_list);
1494N/A idr_init(&dev->mode_config.crtc_idr);
1494N/A
1494N/A drm_modeset_lock_all(dev);
1494N/A (void) drm_mode_create_standard_connector_properties(dev);
1494N/A drm_modeset_unlock_all(dev);
1494N/A
1494N/A /* Just to be sure */
1494N/A dev->mode_config.num_fb = 0;
1494N/A dev->mode_config.num_connector = 0;
1494N/A dev->mode_config.num_crtc = 0;
1494N/A dev->mode_config.num_encoder = 0;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_mode_config_cleanup - free up DRM mode_config info
1494N/A * @dev: DRM device
1494N/A *
1494N/A * Free up all the connectors and CRTCs associated with this DRM device, then
1494N/A * free up the framebuffers and associated buffer objects.
1494N/A *
1494N/A * Note that since this /should/ happen single-threaded at driver/device
1494N/A * teardown time, no locking is required. It's the driver's job to ensure that
1494N/A * this guarantee actually holds true.
1494N/A *
1494N/A * FIXME: cleanup any dangling user buffer objects too
1494N/A */
1494N/Avoid drm_mode_config_cleanup(struct drm_device *dev)
1494N/A{
1494N/A struct drm_connector *connector, *ot;
1494N/A struct drm_crtc *crtc, *ct;
1494N/A struct drm_encoder *encoder, *enct;
1494N/A struct drm_framebuffer *fb, *fbt;
1494N/A struct drm_property *property, *pt;
1494N/A struct drm_property_blob *blob, *bt;
1494N/A struct drm_plane *plane, *plt;
1494N/A
1494N/A list_for_each_entry_safe(encoder, enct, struct drm_encoder, &dev->mode_config.encoder_list,
1494N/A head) {
1494N/A encoder->funcs->destroy(encoder);
1494N/A }
1494N/A
1494N/A list_for_each_entry_safe(connector, ot, struct drm_connector,
1494N/A &dev->mode_config.connector_list, head) {
1494N/A connector->funcs->destroy(connector);
1494N/A }
1494N/A
1494N/A list_for_each_entry_safe(property, pt, struct drm_property, &dev->mode_config.property_list,
1494N/A head) {
1494N/A drm_property_destroy(dev, property);
1494N/A }
1494N/A
1494N/A list_for_each_entry_safe(blob, bt, struct drm_property_blob, &dev->mode_config.property_blob_list,
1494N/A head) {
1494N/A drm_property_destroy_blob(dev, blob);
1494N/A }
1494N/A
1494N/A /*
1494N/A * Single-threaded teardown context, so it's not required to grab the
1494N/A * fb_lock to protect against concurrent fb_list access. Contrary, it
1494N/A * would actually deadlock with the drm_framebuffer_cleanup function.
1494N/A *
1494N/A * Also, if there are any framebuffers left, that's a driver leak now,
1494N/A * so politely WARN about this.
1494N/A */
1494N/A WARN_ON(!list_empty(&dev->mode_config.fb_list));
1494N/A list_for_each_entry_safe(fb, fbt, struct drm_framebuffer, &dev->mode_config.fb_list, head) {
1494N/A drm_framebuffer_remove(fb);
1494N/A }
1494N/A
1494N/A list_for_each_entry_safe(plane, plt, struct drm_plane, &dev->mode_config.plane_list,
1494N/A head) {
1494N/A plane->funcs->destroy(plane);
1494N/A }
1494N/A
1494N/A list_for_each_entry_safe(crtc, ct, struct drm_crtc, &dev->mode_config.crtc_list, head) {
1494N/A crtc->funcs->destroy(crtc);
1494N/A }
1494N/A idr_remove_all(&dev->mode_config.crtc_idr);
1494N/A idr_destroy(&dev->mode_config.crtc_idr);
1494N/A}