Subversion Repositories Kolibri OS

Rev

Rev 6937 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2006-2008 Intel Corporation
  3.  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
  4.  * Copyright (c) 2008 Red Hat Inc.
  5.  *
  6.  * DRM core CRTC related functions
  7.  *
  8.  * Permission to use, copy, modify, distribute, and sell this software and its
  9.  * documentation for any purpose is hereby granted without fee, provided that
  10.  * the above copyright notice appear in all copies and that both that copyright
  11.  * notice and this permission notice appear in supporting documentation, and
  12.  * that the name of the copyright holders not be used in advertising or
  13.  * publicity pertaining to distribution of the software without specific,
  14.  * written prior permission.  The copyright holders make no representations
  15.  * about the suitability of this software for any purpose.  It is provided "as
  16.  * is" without express or implied warranty.
  17.  *
  18.  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  19.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  20.  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  21.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  22.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  23.  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  24.  * OF THIS SOFTWARE.
  25.  *
  26.  * Authors:
  27.  *      Keith Packard
  28.  *      Eric Anholt <eric@anholt.net>
  29.  *      Dave Airlie <airlied@linux.ie>
  30.  *      Jesse Barnes <jesse.barnes@intel.com>
  31.  */
  32. #include <linux/ctype.h>
  33. #include <linux/list.h>
  34. #include <linux/slab.h>
  35. #include <linux/export.h>
  36. #include <drm/drmP.h>
  37. #include <drm/drm_crtc.h>
  38. #include <drm/drm_edid.h>
  39. #include <drm/drm_fourcc.h>
  40. #include <drm/drm_modeset_lock.h>
  41. #include <drm/drm_atomic.h>
  42.  
  43. #include "drm_crtc_internal.h"
  44. #include "drm_internal.h"
  45.  
  46. static struct drm_framebuffer *
  47. internal_framebuffer_create(struct drm_device *dev,
  48.                             const struct drm_mode_fb_cmd2 *r,
  49.                             struct drm_file *file_priv);
  50.  
  51. /* Avoid boilerplate.  I'm tired of typing. */
  52. #define DRM_ENUM_NAME_FN(fnname, list)                          \
  53.         const char *fnname(int val)                             \
  54.         {                                                       \
  55.                 int i;                                          \
  56.                 for (i = 0; i < ARRAY_SIZE(list); i++) {        \
  57.                         if (list[i].type == val)                \
  58.                                 return list[i].name;            \
  59.                 }                                               \
  60.                 return "(unknown)";                             \
  61.         }
  62.  
  63. /*
  64.  * Global properties
  65.  */
  66. static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
  67.         { DRM_MODE_DPMS_ON, "On" },
  68.         { DRM_MODE_DPMS_STANDBY, "Standby" },
  69.         { DRM_MODE_DPMS_SUSPEND, "Suspend" },
  70.         { DRM_MODE_DPMS_OFF, "Off" }
  71. };
  72.  
  73. DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
  74.  
  75. static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
  76.         { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
  77.         { DRM_PLANE_TYPE_PRIMARY, "Primary" },
  78.         { DRM_PLANE_TYPE_CURSOR, "Cursor" },
  79. };
  80.  
  81. /*
  82.  * Optional properties
  83.  */
  84. static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
  85.         { DRM_MODE_SCALE_NONE, "None" },
  86.         { DRM_MODE_SCALE_FULLSCREEN, "Full" },
  87.         { DRM_MODE_SCALE_CENTER, "Center" },
  88.         { DRM_MODE_SCALE_ASPECT, "Full aspect" },
  89. };
  90.  
  91. static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
  92.         { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
  93.         { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
  94.         { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
  95. };
  96.  
  97. /*
  98.  * Non-global properties, but "required" for certain connectors.
  99.  */
  100. static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
  101.         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  102.         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
  103.         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
  104. };
  105.  
  106. DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
  107.  
  108. static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
  109.         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
  110.         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
  111.         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
  112. };
  113.  
  114. DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
  115.                  drm_dvi_i_subconnector_enum_list)
  116.  
  117. static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
  118.         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  119.         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  120.         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
  121.         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  122.         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
  123. };
  124.  
  125. DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
  126.  
  127. static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
  128.         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
  129.         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  130.         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
  131.         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  132.         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
  133. };
  134.  
  135. DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
  136.                  drm_tv_subconnector_enum_list)
  137.  
  138. static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
  139.         { DRM_MODE_DIRTY_OFF,      "Off"      },
  140.         { DRM_MODE_DIRTY_ON,       "On"       },
  141.         { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
  142. };
  143.  
  144. struct drm_conn_prop_enum_list {
  145.         int type;
  146.         const char *name;
  147.         struct ida ida;
  148. };
  149.  
  150. /*
  151.  * Connector and encoder types.
  152.  */
  153. static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
  154.         { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
  155.         { DRM_MODE_CONNECTOR_VGA, "VGA" },
  156.         { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
  157.         { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
  158.         { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
  159.         { DRM_MODE_CONNECTOR_Composite, "Composite" },
  160.         { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
  161.         { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
  162.         { DRM_MODE_CONNECTOR_Component, "Component" },
  163.         { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
  164.         { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
  165.         { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
  166.         { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
  167.         { DRM_MODE_CONNECTOR_TV, "TV" },
  168.         { DRM_MODE_CONNECTOR_eDP, "eDP" },
  169.         { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
  170.         { DRM_MODE_CONNECTOR_DSI, "DSI" },
  171. };
  172.  
  173. static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
  174.         { DRM_MODE_ENCODER_NONE, "None" },
  175.         { DRM_MODE_ENCODER_DAC, "DAC" },
  176.         { DRM_MODE_ENCODER_TMDS, "TMDS" },
  177.         { DRM_MODE_ENCODER_LVDS, "LVDS" },
  178.         { DRM_MODE_ENCODER_TVDAC, "TV" },
  179.         { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
  180.         { DRM_MODE_ENCODER_DSI, "DSI" },
  181.         { DRM_MODE_ENCODER_DPMST, "DP MST" },
  182. };
  183.  
  184. static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
  185.         { SubPixelUnknown, "Unknown" },
  186.         { SubPixelHorizontalRGB, "Horizontal RGB" },
  187.         { SubPixelHorizontalBGR, "Horizontal BGR" },
  188.         { SubPixelVerticalRGB, "Vertical RGB" },
  189.         { SubPixelVerticalBGR, "Vertical BGR" },
  190.         { SubPixelNone, "None" },
  191. };
  192.  
  193. void drm_connector_ida_init(void)
  194. {
  195.         int i;
  196.  
  197.         for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  198.                 ida_init(&drm_connector_enum_list[i].ida);
  199. }
  200.  
  201. void drm_connector_ida_destroy(void)
  202. {
  203.         int i;
  204.  
  205.         for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  206.                 ida_destroy(&drm_connector_enum_list[i].ida);
  207. }
  208.  
  209. /**
  210.  * drm_get_connector_status_name - return a string for connector status
  211.  * @status: connector status to compute name of
  212.  *
  213.  * In contrast to the other drm_get_*_name functions this one here returns a
  214.  * const pointer and hence is threadsafe.
  215.  */
  216. const char *drm_get_connector_status_name(enum drm_connector_status status)
  217. {
  218.         if (status == connector_status_connected)
  219.                 return "connected";
  220.         else if (status == connector_status_disconnected)
  221.                 return "disconnected";
  222.         else
  223.                 return "unknown";
  224. }
  225. EXPORT_SYMBOL(drm_get_connector_status_name);
  226.  
  227. /**
  228.  * drm_get_subpixel_order_name - return a string for a given subpixel enum
  229.  * @order: enum of subpixel_order
  230.  *
  231.  * Note you could abuse this and return something out of bounds, but that
  232.  * would be a caller error.  No unscrubbed user data should make it here.
  233.  */
  234. const char *drm_get_subpixel_order_name(enum subpixel_order order)
  235. {
  236.         return drm_subpixel_enum_list[order].name;
  237. }
  238. EXPORT_SYMBOL(drm_get_subpixel_order_name);
  239.  
  240. static char printable_char(int c)
  241. {
  242.         return isascii(c) && isprint(c) ? c : '?';
  243. }
  244.  
  245. /**
  246.  * drm_get_format_name - return a string for drm fourcc format
  247.  * @format: format to compute name of
  248.  *
  249.  * Note that the buffer used by this function is globally shared and owned by
  250.  * the function itself.
  251.  *
  252.  * FIXME: This isn't really multithreading safe.
  253.  */
  254. const char *drm_get_format_name(uint32_t format)
  255. {
  256.         static char buf[32];
  257.  
  258.         snprintf(buf, sizeof(buf),
  259.                  "%c%c%c%c %s-endian (0x%08x)",
  260.                  printable_char(format & 0xff),
  261.                  printable_char((format >> 8) & 0xff),
  262.                  printable_char((format >> 16) & 0xff),
  263.                  printable_char((format >> 24) & 0x7f),
  264.                  format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
  265.                  format);
  266.  
  267.         return buf;
  268. }
  269. EXPORT_SYMBOL(drm_get_format_name);
  270.  
  271. /*
  272.  * Internal function to assign a slot in the object idr and optionally
  273.  * register the object into the idr.
  274.  */
  275. static int drm_mode_object_get_reg(struct drm_device *dev,
  276.                                    struct drm_mode_object *obj,
  277.                                    uint32_t obj_type,
  278.                                    bool register_obj)
  279. {
  280.         int ret;
  281.  
  282.         mutex_lock(&dev->mode_config.idr_mutex);
  283.         ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
  284.         if (ret >= 0) {
  285.                 /*
  286.                  * Set up the object linking under the protection of the idr
  287.                  * lock so that other users can't see inconsistent state.
  288.                  */
  289.                 obj->id = ret;
  290.                 obj->type = obj_type;
  291.         }
  292.         mutex_unlock(&dev->mode_config.idr_mutex);
  293.  
  294.         return ret < 0 ? ret : 0;
  295. }
  296.  
  297. /**
  298.  * drm_mode_object_get - allocate a new modeset identifier
  299.  * @dev: DRM device
  300.  * @obj: object pointer, used to generate unique ID
  301.  * @obj_type: object type
  302.  *
  303.  * Create a unique identifier based on @ptr in @dev's identifier space.  Used
  304.  * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
  305.  * modeset identifiers are _not_ reference counted. Hence don't use this for
  306.  * reference counted modeset objects like framebuffers.
  307.  *
  308.  * Returns:
  309.  * Zero on success, error code on failure.
  310.  */
  311. int drm_mode_object_get(struct drm_device *dev,
  312.                         struct drm_mode_object *obj, uint32_t obj_type)
  313. {
  314.         return drm_mode_object_get_reg(dev, obj, obj_type, true);
  315. }
  316.  
  317. static void drm_mode_object_register(struct drm_device *dev,
  318.                                      struct drm_mode_object *obj)
  319. {
  320.         mutex_lock(&dev->mode_config.idr_mutex);
  321.         idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
  322.         mutex_unlock(&dev->mode_config.idr_mutex);
  323. }
  324.  
  325. /**
  326.  * drm_mode_object_put - free a modeset identifer
  327.  * @dev: DRM device
  328.  * @object: object to free
  329.  *
  330.  * Free @id from @dev's unique identifier pool. Note that despite the _get
  331.  * postfix modeset identifiers are _not_ reference counted. Hence don't use this
  332.  * for reference counted modeset objects like framebuffers.
  333.  */
  334. void drm_mode_object_put(struct drm_device *dev,
  335.                          struct drm_mode_object *object)
  336. {
  337.         mutex_lock(&dev->mode_config.idr_mutex);
  338.         idr_remove(&dev->mode_config.crtc_idr, object->id);
  339.         mutex_unlock(&dev->mode_config.idr_mutex);
  340. }
  341.  
  342. static struct drm_mode_object *_object_find(struct drm_device *dev,
  343.                 uint32_t id, uint32_t type)
  344. {
  345.         struct drm_mode_object *obj = NULL;
  346.  
  347.         mutex_lock(&dev->mode_config.idr_mutex);
  348.         obj = idr_find(&dev->mode_config.crtc_idr, id);
  349.         if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
  350.                 obj = NULL;
  351.         if (obj && obj->id != id)
  352.                 obj = NULL;
  353.         /* don't leak out unref'd fb's */
  354.         if (obj &&
  355.             (obj->type == DRM_MODE_OBJECT_FB ||
  356.              obj->type == DRM_MODE_OBJECT_BLOB))
  357.                 obj = NULL;
  358.         mutex_unlock(&dev->mode_config.idr_mutex);
  359.  
  360.         return obj;
  361. }
  362.  
  363. /**
  364.  * drm_mode_object_find - look up a drm object with static lifetime
  365.  * @dev: drm device
  366.  * @id: id of the mode object
  367.  * @type: type of the mode object
  368.  *
  369.  * Note that framebuffers cannot be looked up with this functions - since those
  370.  * are reference counted, they need special treatment.  Even with
  371.  * DRM_MODE_OBJECT_ANY (although that will simply return NULL
  372.  * rather than WARN_ON()).
  373.  */
  374. struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
  375.                 uint32_t id, uint32_t type)
  376. {
  377.         struct drm_mode_object *obj = NULL;
  378.  
  379.         /* Framebuffers are reference counted and need their own lookup
  380.          * function.*/
  381.         WARN_ON(type == DRM_MODE_OBJECT_FB || type == DRM_MODE_OBJECT_BLOB);
  382.         obj = _object_find(dev, id, type);
  383.         return obj;
  384. }
  385. EXPORT_SYMBOL(drm_mode_object_find);
  386.  
  387. /**
  388.  * drm_framebuffer_init - initialize a framebuffer
  389.  * @dev: DRM device
  390.  * @fb: framebuffer to be initialized
  391.  * @funcs: ... with these functions
  392.  *
  393.  * Allocates an ID for the framebuffer's parent mode object, sets its mode
  394.  * functions & device file and adds it to the master fd list.
  395.  *
  396.  * IMPORTANT:
  397.  * This functions publishes the fb and makes it available for concurrent access
  398.  * by other users. Which means by this point the fb _must_ be fully set up -
  399.  * since all the fb attributes are invariant over its lifetime, no further
  400.  * locking but only correct reference counting is required.
  401.  *
  402.  * Returns:
  403.  * Zero on success, error code on failure.
  404.  */
  405. int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
  406.                          const struct drm_framebuffer_funcs *funcs)
  407. {
  408.         int ret;
  409.  
  410.         mutex_lock(&dev->mode_config.fb_lock);
  411.         kref_init(&fb->refcount);
  412.         INIT_LIST_HEAD(&fb->filp_head);
  413.         fb->dev = dev;
  414.         fb->funcs = funcs;
  415.  
  416.         ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
  417.         if (ret)
  418.                 goto out;
  419.  
  420.         dev->mode_config.num_fb++;
  421.         list_add(&fb->head, &dev->mode_config.fb_list);
  422. out:
  423.         mutex_unlock(&dev->mode_config.fb_lock);
  424.  
  425.         return ret;
  426. }
  427. EXPORT_SYMBOL(drm_framebuffer_init);
  428.  
  429. /* dev->mode_config.fb_lock must be held! */
  430. static void __drm_framebuffer_unregister(struct drm_device *dev,
  431.                                          struct drm_framebuffer *fb)
  432. {
  433.         drm_mode_object_put(dev, &fb->base);
  434.  
  435.         fb->base.id = 0;
  436. }
  437.  
  438. static void drm_framebuffer_free(struct kref *kref)
  439. {
  440.         struct drm_framebuffer *fb =
  441.                         container_of(kref, struct drm_framebuffer, refcount);
  442.         struct drm_device *dev = fb->dev;
  443.  
  444.         /*
  445.          * The lookup idr holds a weak reference, which has not necessarily been
  446.          * removed at this point. Check for that.
  447.          */
  448.         mutex_lock(&dev->mode_config.fb_lock);
  449.         if (fb->base.id) {
  450.                 /* Mark fb as reaped and drop idr ref. */
  451.                 __drm_framebuffer_unregister(dev, fb);
  452.         }
  453.         mutex_unlock(&dev->mode_config.fb_lock);
  454.  
  455.         fb->funcs->destroy(fb);
  456. }
  457.  
  458. static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
  459.                                                         uint32_t id)
  460. {
  461.         struct drm_mode_object *obj = NULL;
  462.         struct drm_framebuffer *fb;
  463.  
  464.         mutex_lock(&dev->mode_config.idr_mutex);
  465.         obj = idr_find(&dev->mode_config.crtc_idr, id);
  466.         if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
  467.                 fb = NULL;
  468.         else
  469.                 fb = obj_to_fb(obj);
  470.         mutex_unlock(&dev->mode_config.idr_mutex);
  471.  
  472.         return fb;
  473. }
  474.  
  475. /**
  476.  * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
  477.  * @dev: drm device
  478.  * @id: id of the fb object
  479.  *
  480.  * If successful, this grabs an additional reference to the framebuffer -
  481.  * callers need to make sure to eventually unreference the returned framebuffer
  482.  * again, using @drm_framebuffer_unreference.
  483.  */
  484. struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
  485.                                                uint32_t id)
  486. {
  487.         struct drm_framebuffer *fb;
  488.  
  489.         mutex_lock(&dev->mode_config.fb_lock);
  490.         fb = __drm_framebuffer_lookup(dev, id);
  491.         if (fb) {
  492.                 if (!kref_get_unless_zero(&fb->refcount))
  493.                         fb = NULL;
  494.         }
  495.         mutex_unlock(&dev->mode_config.fb_lock);
  496.  
  497.         return fb;
  498. }
  499. EXPORT_SYMBOL(drm_framebuffer_lookup);
  500.  
  501. /**
  502.  * drm_framebuffer_unreference - unref a framebuffer
  503.  * @fb: framebuffer to unref
  504.  *
  505.  * This functions decrements the fb's refcount and frees it if it drops to zero.
  506.  */
  507. void drm_framebuffer_unreference(struct drm_framebuffer *fb)
  508. {
  509.         DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
  510.         kref_put(&fb->refcount, drm_framebuffer_free);
  511. }
  512. EXPORT_SYMBOL(drm_framebuffer_unreference);
  513.  
  514. /**
  515.  * drm_framebuffer_reference - incr the fb refcnt
  516.  * @fb: framebuffer
  517.  *
  518.  * This functions increments the fb's refcount.
  519.  */
  520. void drm_framebuffer_reference(struct drm_framebuffer *fb)
  521. {
  522.         DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
  523.         kref_get(&fb->refcount);
  524. }
  525. EXPORT_SYMBOL(drm_framebuffer_reference);
  526.  
  527. /**
  528.  * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
  529.  * @fb: fb to unregister
  530.  *
  531.  * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
  532.  * those used for fbdev. Note that the caller must hold a reference of it's own,
  533.  * i.e. the object may not be destroyed through this call (since it'll lead to a
  534.  * locking inversion).
  535.  */
  536. void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
  537. {
  538.         struct drm_device *dev;
  539.  
  540.         if (!fb)
  541.                 return;
  542.  
  543.         dev = fb->dev;
  544.  
  545.         mutex_lock(&dev->mode_config.fb_lock);
  546.         /* Mark fb as reaped and drop idr ref. */
  547.         __drm_framebuffer_unregister(dev, fb);
  548.         mutex_unlock(&dev->mode_config.fb_lock);
  549. }
  550. EXPORT_SYMBOL(drm_framebuffer_unregister_private);
  551.  
  552. /**
  553.  * drm_framebuffer_cleanup - remove a framebuffer object
  554.  * @fb: framebuffer to remove
  555.  *
  556.  * Cleanup framebuffer. This function is intended to be used from the drivers
  557.  * ->destroy callback. It can also be used to clean up driver private
  558.  *  framebuffers embedded into a larger structure.
  559.  *
  560.  * Note that this function does not remove the fb from active usuage - if it is
  561.  * still used anywhere, hilarity can ensue since userspace could call getfb on
  562.  * the id and get back -EINVAL. Obviously no concern at driver unload time.
  563.  *
  564.  * Also, the framebuffer will not be removed from the lookup idr - for
  565.  * user-created framebuffers this will happen in in the rmfb ioctl. For
  566.  * driver-private objects (e.g. for fbdev) drivers need to explicitly call
  567.  * drm_framebuffer_unregister_private.
  568.  */
  569. void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
  570. {
  571.         struct drm_device *dev = fb->dev;
  572.  
  573.         mutex_lock(&dev->mode_config.fb_lock);
  574.         list_del(&fb->head);
  575.         dev->mode_config.num_fb--;
  576.         mutex_unlock(&dev->mode_config.fb_lock);
  577. }
  578. EXPORT_SYMBOL(drm_framebuffer_cleanup);
  579.  
  580. /**
  581.  * drm_framebuffer_remove - remove and unreference a framebuffer object
  582.  * @fb: framebuffer to remove
  583.  *
  584.  * Scans all the CRTCs and planes in @dev's mode_config.  If they're
  585.  * using @fb, removes it, setting it to NULL. Then drops the reference to the
  586.  * passed-in framebuffer. Might take the modeset locks.
  587.  *
  588.  * Note that this function optimizes the cleanup away if the caller holds the
  589.  * last reference to the framebuffer. It is also guaranteed to not take the
  590.  * modeset locks in this case.
  591.  */
  592. void drm_framebuffer_remove(struct drm_framebuffer *fb)
  593. {
  594.         struct drm_device *dev;
  595.         struct drm_crtc *crtc;
  596.         struct drm_plane *plane;
  597.         struct drm_mode_set set;
  598.         int ret;
  599.  
  600.         if (!fb)
  601.                 return;
  602.  
  603.         dev = fb->dev;
  604.  
  605.         WARN_ON(!list_empty(&fb->filp_head));
  606.  
  607.         /*
  608.          * drm ABI mandates that we remove any deleted framebuffers from active
  609.          * useage. But since most sane clients only remove framebuffers they no
  610.          * longer need, try to optimize this away.
  611.          *
  612.          * Since we're holding a reference ourselves, observing a refcount of 1
  613.          * means that we're the last holder and can skip it. Also, the refcount
  614.          * can never increase from 1 again, so we don't need any barriers or
  615.          * locks.
  616.          *
  617.          * Note that userspace could try to race with use and instate a new
  618.          * usage _after_ we've cleared all current ones. End result will be an
  619.          * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
  620.          * in this manner.
  621.          */
  622.         if (atomic_read(&fb->refcount.refcount) > 1) {
  623.                 drm_modeset_lock_all(dev);
  624.                 /* remove from any CRTC */
  625.                 drm_for_each_crtc(crtc, dev) {
  626.                         if (crtc->primary->fb == fb) {
  627.                                 /* should turn off the crtc */
  628.                                 memset(&set, 0, sizeof(struct drm_mode_set));
  629.                                 set.crtc = crtc;
  630.                                 set.fb = NULL;
  631.                                 ret = drm_mode_set_config_internal(&set);
  632.                                 if (ret)
  633.                                         DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
  634.                         }
  635.                 }
  636.  
  637.                 drm_for_each_plane(plane, dev) {
  638.                         if (plane->fb == fb)
  639.                                 drm_plane_force_disable(plane);
  640.                 }
  641.                 drm_modeset_unlock_all(dev);
  642.         }
  643.  
  644.         drm_framebuffer_unreference(fb);
  645. }
  646. EXPORT_SYMBOL(drm_framebuffer_remove);
  647.  
  648. DEFINE_WW_CLASS(crtc_ww_class);
  649.  
  650. static unsigned int drm_num_crtcs(struct drm_device *dev)
  651. {
  652.         unsigned int num = 0;
  653.         struct drm_crtc *tmp;
  654.  
  655.         drm_for_each_crtc(tmp, dev) {
  656.                 num++;
  657.         }
  658.  
  659.         return num;
  660. }
  661.  
  662. /**
  663.  * drm_crtc_init_with_planes - Initialise a new CRTC object with
  664.  *    specified primary and cursor planes.
  665.  * @dev: DRM device
  666.  * @crtc: CRTC object to init
  667.  * @primary: Primary plane for CRTC
  668.  * @cursor: Cursor plane for CRTC
  669.  * @funcs: callbacks for the new CRTC
  670.  * @name: printf style format string for the CRTC name, or NULL for default name
  671.  *
  672.  * Inits a new object created as base part of a driver crtc object.
  673.  *
  674.  * Returns:
  675.  * Zero on success, error code on failure.
  676.  */
  677. int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
  678.                               struct drm_plane *primary,
  679.                               struct drm_plane *cursor,
  680.                               const struct drm_crtc_funcs *funcs,
  681.                               const char *name, ...)
  682. {
  683.         struct drm_mode_config *config = &dev->mode_config;
  684.         int ret;
  685.  
  686.         WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
  687.         WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
  688.  
  689.         crtc->dev = dev;
  690.         crtc->funcs = funcs;
  691.  
  692.         drm_modeset_lock_init(&crtc->mutex);
  693.         ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
  694.         if (ret)
  695.                 return ret;
  696.  
  697.         if (name) {
  698.                 va_list ap;
  699.  
  700.                 va_start(ap, name);
  701.                 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
  702.                 va_end(ap);
  703.         } else {
  704.                 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
  705.                                        drm_num_crtcs(dev));
  706.         }
  707.         if (!crtc->name) {
  708.                 drm_mode_object_put(dev, &crtc->base);
  709.                 return -ENOMEM;
  710.         }
  711.  
  712.         crtc->base.properties = &crtc->properties;
  713.  
  714.         list_add_tail(&crtc->head, &config->crtc_list);
  715.         config->num_crtc++;
  716.  
  717.         crtc->primary = primary;
  718.         crtc->cursor = cursor;
  719.         if (primary)
  720.                 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
  721.         if (cursor)
  722.                 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
  723.  
  724.         if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  725.                 drm_object_attach_property(&crtc->base, config->prop_active, 0);
  726.                 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
  727.         }
  728.  
  729.         return 0;
  730. }
  731. EXPORT_SYMBOL(drm_crtc_init_with_planes);
  732.  
  733. /**
  734.  * drm_crtc_cleanup - Clean up the core crtc usage
  735.  * @crtc: CRTC to cleanup
  736.  *
  737.  * This function cleans up @crtc and removes it from the DRM mode setting
  738.  * core. Note that the function does *not* free the crtc structure itself,
  739.  * this is the responsibility of the caller.
  740.  */
  741. void drm_crtc_cleanup(struct drm_crtc *crtc)
  742. {
  743.         struct drm_device *dev = crtc->dev;
  744.  
  745.         kfree(crtc->gamma_store);
  746.         crtc->gamma_store = NULL;
  747.  
  748.         drm_modeset_lock_fini(&crtc->mutex);
  749.  
  750.         drm_mode_object_put(dev, &crtc->base);
  751.         list_del(&crtc->head);
  752.         dev->mode_config.num_crtc--;
  753.  
  754.         WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
  755.         if (crtc->state && crtc->funcs->atomic_destroy_state)
  756.                 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
  757.  
  758.         kfree(crtc->name);
  759.  
  760.         memset(crtc, 0, sizeof(*crtc));
  761. }
  762. EXPORT_SYMBOL(drm_crtc_cleanup);
  763.  
  764. /**
  765.  * drm_crtc_index - find the index of a registered CRTC
  766.  * @crtc: CRTC to find index for
  767.  *
  768.  * Given a registered CRTC, return the index of that CRTC within a DRM
  769.  * device's list of CRTCs.
  770.  */
  771. unsigned int drm_crtc_index(struct drm_crtc *crtc)
  772. {
  773.         unsigned int index = 0;
  774.         struct drm_crtc *tmp;
  775.  
  776.         drm_for_each_crtc(tmp, crtc->dev) {
  777.                 if (tmp == crtc)
  778.                         return index;
  779.  
  780.                 index++;
  781.         }
  782.  
  783.         BUG();
  784. }
  785. EXPORT_SYMBOL(drm_crtc_index);
  786.  
  787. /*
  788.  * drm_mode_remove - remove and free a mode
  789.  * @connector: connector list to modify
  790.  * @mode: mode to remove
  791.  *
  792.  * Remove @mode from @connector's mode list, then free it.
  793.  */
  794. static void drm_mode_remove(struct drm_connector *connector,
  795.                             struct drm_display_mode *mode)
  796. {
  797.         list_del(&mode->head);
  798.         drm_mode_destroy(connector->dev, mode);
  799. }
  800.  
  801. /**
  802.  * drm_display_info_set_bus_formats - set the supported bus formats
  803.  * @info: display info to store bus formats in
  804.  * @formats: array containing the supported bus formats
  805.  * @num_formats: the number of entries in the fmts array
  806.  *
  807.  * Store the supported bus formats in display info structure.
  808.  * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
  809.  * a full list of available formats.
  810.  */
  811. int drm_display_info_set_bus_formats(struct drm_display_info *info,
  812.                                      const u32 *formats,
  813.                                      unsigned int num_formats)
  814. {
  815.         u32 *fmts = NULL;
  816.  
  817.         if (!formats && num_formats)
  818.                 return -EINVAL;
  819.  
  820.         if (formats && num_formats) {
  821.                 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
  822.                                GFP_KERNEL);
  823.                 if (!fmts)
  824.                         return -ENOMEM;
  825.         }
  826.  
  827.         kfree(info->bus_formats);
  828.         info->bus_formats = fmts;
  829.         info->num_bus_formats = num_formats;
  830.  
  831.         return 0;
  832. }
  833. EXPORT_SYMBOL(drm_display_info_set_bus_formats);
  834.  
  835. /**
  836.  * drm_connector_get_cmdline_mode - reads the user's cmdline mode
  837.  * @connector: connector to quwery
  838.  *
  839.  * The kernel supports per-connector configration of its consoles through
  840.  * use of the video= parameter. This function parses that option and
  841.  * extracts the user's specified mode (or enable/disable status) for a
  842.  * particular connector. This is typically only used during the early fbdev
  843.  * setup.
  844.  */
  845. static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
  846. {
  847.         struct drm_cmdline_mode *mode = &connector->cmdline_mode;
  848.         char *option = NULL;
  849.  
  850.         if (fb_get_options(connector->name, &option))
  851.                 return;
  852.  
  853.         if (!drm_mode_parse_command_line_for_connector(option,
  854.                                                        connector,
  855.                                                        mode))
  856.                 return;
  857.  
  858.         if (mode->force) {
  859.                 const char *s;
  860.  
  861.                 switch (mode->force) {
  862.                 case DRM_FORCE_OFF:
  863.                         s = "OFF";
  864.                         break;
  865.                 case DRM_FORCE_ON_DIGITAL:
  866.                         s = "ON - dig";
  867.                         break;
  868.                 default:
  869.                 case DRM_FORCE_ON:
  870.                         s = "ON";
  871.                         break;
  872.                 }
  873.  
  874.                 DRM_INFO("forcing %s connector %s\n", connector->name, s);
  875.                 connector->force = mode->force;
  876.         }
  877.  
  878.         DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
  879.                       connector->name,
  880.                       mode->xres, mode->yres,
  881.                       mode->refresh_specified ? mode->refresh : 60,
  882.                       mode->rb ? " reduced blanking" : "",
  883.                       mode->margins ? " with margins" : "",
  884.                       mode->interlace ?  " interlaced" : "");
  885. }
  886.  
  887. /**
  888.  * drm_connector_init - Init a preallocated connector
  889.  * @dev: DRM device
  890.  * @connector: the connector to init
  891.  * @funcs: callbacks for this connector
  892.  * @connector_type: user visible type of the connector
  893.  *
  894.  * Initialises a preallocated connector. Connectors should be
  895.  * subclassed as part of driver connector objects.
  896.  *
  897.  * Returns:
  898.  * Zero on success, error code on failure.
  899.  */
  900. int drm_connector_init(struct drm_device *dev,
  901.                        struct drm_connector *connector,
  902.                        const struct drm_connector_funcs *funcs,
  903.                        int connector_type)
  904. {
  905.         struct drm_mode_config *config = &dev->mode_config;
  906.         int ret;
  907.         struct ida *connector_ida =
  908.                 &drm_connector_enum_list[connector_type].ida;
  909.  
  910.         drm_modeset_lock_all(dev);
  911.  
  912.         ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
  913.         if (ret)
  914.                 goto out_unlock;
  915.  
  916.         connector->base.properties = &connector->properties;
  917.         connector->dev = dev;
  918.         connector->funcs = funcs;
  919.  
  920.         connector->connector_id = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
  921.         if (connector->connector_id < 0) {
  922.                 ret = connector->connector_id;
  923.                 goto out_put;
  924.         }
  925.  
  926.         connector->connector_type = connector_type;
  927.         connector->connector_type_id =
  928.                 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
  929.         if (connector->connector_type_id < 0) {
  930.                 ret = connector->connector_type_id;
  931.                 goto out_put_id;
  932.         }
  933.         connector->name =
  934.                 kasprintf(GFP_KERNEL, "%s-%d",
  935.                           drm_connector_enum_list[connector_type].name,
  936.                           connector->connector_type_id);
  937.         if (!connector->name) {
  938.                 ret = -ENOMEM;
  939.                 goto out_put_type_id;
  940.         }
  941.  
  942.         INIT_LIST_HEAD(&connector->probed_modes);
  943.         INIT_LIST_HEAD(&connector->modes);
  944.         connector->edid_blob_ptr = NULL;
  945.         connector->status = connector_status_unknown;
  946.  
  947.         drm_connector_get_cmdline_mode(connector);
  948.  
  949.         /* We should add connectors at the end to avoid upsetting the connector
  950.          * index too much. */
  951.         list_add_tail(&connector->head, &config->connector_list);
  952.         config->num_connector++;
  953.  
  954.         if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
  955.                 drm_object_attach_property(&connector->base,
  956.                                               config->edid_property,
  957.                                               0);
  958.  
  959.         drm_object_attach_property(&connector->base,
  960.                                       config->dpms_property, 0);
  961.  
  962.         if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  963.                 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
  964.         }
  965.  
  966.         connector->debugfs_entry = NULL;
  967. out_put_type_id:
  968.         if (ret)
  969.                 ida_remove(connector_ida, connector->connector_type_id);
  970. out_put_id:
  971.         if (ret)
  972.                 ida_remove(&config->connector_ida, connector->connector_id);
  973. out_put:
  974.         if (ret)
  975.                 drm_mode_object_put(dev, &connector->base);
  976.  
  977. out_unlock:
  978.         drm_modeset_unlock_all(dev);
  979.  
  980.         return ret;
  981. }
  982. EXPORT_SYMBOL(drm_connector_init);
  983.  
  984. /**
  985.  * drm_connector_cleanup - cleans up an initialised connector
  986.  * @connector: connector to cleanup
  987.  *
  988.  * Cleans up the connector but doesn't free the object.
  989.  */
  990. void drm_connector_cleanup(struct drm_connector *connector)
  991. {
  992.         struct drm_device *dev = connector->dev;
  993.         struct drm_display_mode *mode, *t;
  994.  
  995.         if (connector->tile_group) {
  996.                 drm_mode_put_tile_group(dev, connector->tile_group);
  997.                 connector->tile_group = NULL;
  998.         }
  999.  
  1000.         list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
  1001.                 drm_mode_remove(connector, mode);
  1002.  
  1003.         list_for_each_entry_safe(mode, t, &connector->modes, head)
  1004.                 drm_mode_remove(connector, mode);
  1005.  
  1006.         ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
  1007.                    connector->connector_type_id);
  1008.  
  1009.         ida_remove(&dev->mode_config.connector_ida,
  1010.                    connector->connector_id);
  1011.  
  1012.         kfree(connector->display_info.bus_formats);
  1013.         drm_mode_object_put(dev, &connector->base);
  1014.         kfree(connector->name);
  1015.         connector->name = NULL;
  1016.         list_del(&connector->head);
  1017.         dev->mode_config.num_connector--;
  1018.  
  1019.         WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
  1020.         if (connector->state && connector->funcs->atomic_destroy_state)
  1021.                 connector->funcs->atomic_destroy_state(connector,
  1022.                                                        connector->state);
  1023.  
  1024.         memset(connector, 0, sizeof(*connector));
  1025. }
  1026. EXPORT_SYMBOL(drm_connector_cleanup);
  1027.  
  1028. /**
  1029.  * drm_connector_register - register a connector
  1030.  * @connector: the connector to register
  1031.  *
  1032.  * Register userspace interfaces for a connector
  1033.  *
  1034.  * Returns:
  1035.  * Zero on success, error code on failure.
  1036.  */
  1037. int drm_connector_register(struct drm_connector *connector)
  1038. {
  1039.         int ret;
  1040.  
  1041.         drm_mode_object_register(connector->dev, &connector->base);
  1042.  
  1043.         ret = drm_sysfs_connector_add(connector);
  1044.         if (ret)
  1045.                 return ret;
  1046.  
  1047.         ret = drm_debugfs_connector_add(connector);
  1048.         if (ret) {
  1049.                 drm_sysfs_connector_remove(connector);
  1050.                 return ret;
  1051.         }
  1052.  
  1053.         return 0;
  1054. }
  1055. EXPORT_SYMBOL(drm_connector_register);
  1056.  
  1057. /**
  1058.  * drm_connector_unregister - unregister a connector
  1059.  * @connector: the connector to unregister
  1060.  *
  1061.  * Unregister userspace interfaces for a connector
  1062.  */
  1063. void drm_connector_unregister(struct drm_connector *connector)
  1064. {
  1065.         drm_sysfs_connector_remove(connector);
  1066.         drm_debugfs_connector_remove(connector);
  1067. }
  1068. EXPORT_SYMBOL(drm_connector_unregister);
  1069.  
  1070.  
  1071. /**
  1072.  * drm_connector_unplug_all - unregister connector userspace interfaces
  1073.  * @dev: drm device
  1074.  *
  1075.  * This function unregisters all connector userspace interfaces in sysfs. Should
  1076.  * be call when the device is disconnected, e.g. from an usb driver's
  1077.  * ->disconnect callback.
  1078.  */
  1079. void drm_connector_unplug_all(struct drm_device *dev)
  1080. {
  1081.         struct drm_connector *connector;
  1082.  
  1083.         /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
  1084.         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  1085.                 drm_connector_unregister(connector);
  1086.  
  1087. }
  1088. EXPORT_SYMBOL(drm_connector_unplug_all);
  1089.  
  1090. /**
  1091.  * drm_encoder_init - Init a preallocated encoder
  1092.  * @dev: drm device
  1093.  * @encoder: the encoder to init
  1094.  * @funcs: callbacks for this encoder
  1095.  * @encoder_type: user visible type of the encoder
  1096.  * @name: printf style format string for the encoder name, or NULL for default name
  1097.  *
  1098.  * Initialises a preallocated encoder. Encoder should be
  1099.  * subclassed as part of driver encoder objects.
  1100.  *
  1101.  * Returns:
  1102.  * Zero on success, error code on failure.
  1103.  */
  1104. int drm_encoder_init(struct drm_device *dev,
  1105.                       struct drm_encoder *encoder,
  1106.                       const struct drm_encoder_funcs *funcs,
  1107.                       int encoder_type, const char *name, ...)
  1108. {
  1109.         int ret;
  1110.  
  1111.         drm_modeset_lock_all(dev);
  1112.  
  1113.         ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
  1114.         if (ret)
  1115.                 goto out_unlock;
  1116.  
  1117.         encoder->dev = dev;
  1118.         encoder->encoder_type = encoder_type;
  1119.         encoder->funcs = funcs;
  1120.         if (name) {
  1121.                 va_list ap;
  1122.  
  1123.                 va_start(ap, name);
  1124.                 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
  1125.                 va_end(ap);
  1126.         } else {
  1127.                 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
  1128.                                           drm_encoder_enum_list[encoder_type].name,
  1129.                                           encoder->base.id);
  1130.         }
  1131.         if (!encoder->name) {
  1132.                 ret = -ENOMEM;
  1133.                 goto out_put;
  1134.         }
  1135.  
  1136.         list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
  1137.         dev->mode_config.num_encoder++;
  1138.  
  1139. out_put:
  1140.         if (ret)
  1141.                 drm_mode_object_put(dev, &encoder->base);
  1142.  
  1143. out_unlock:
  1144.         drm_modeset_unlock_all(dev);
  1145.  
  1146.         return ret;
  1147. }
  1148. EXPORT_SYMBOL(drm_encoder_init);
  1149.  
  1150. /**
  1151.  * drm_encoder_index - find the index of a registered encoder
  1152.  * @encoder: encoder to find index for
  1153.  *
  1154.  * Given a registered encoder, return the index of that encoder within a DRM
  1155.  * device's list of encoders.
  1156.  */
  1157. unsigned int drm_encoder_index(struct drm_encoder *encoder)
  1158. {
  1159.         unsigned int index = 0;
  1160.         struct drm_encoder *tmp;
  1161.  
  1162.         drm_for_each_encoder(tmp, encoder->dev) {
  1163.                 if (tmp == encoder)
  1164.                         return index;
  1165.  
  1166.                 index++;
  1167.         }
  1168.  
  1169.         BUG();
  1170. }
  1171. EXPORT_SYMBOL(drm_encoder_index);
  1172.  
  1173. /**
  1174.  * drm_encoder_cleanup - cleans up an initialised encoder
  1175.  * @encoder: encoder to cleanup
  1176.  *
  1177.  * Cleans up the encoder but doesn't free the object.
  1178.  */
  1179. void drm_encoder_cleanup(struct drm_encoder *encoder)
  1180. {
  1181.         struct drm_device *dev = encoder->dev;
  1182.  
  1183.         drm_modeset_lock_all(dev);
  1184.         drm_mode_object_put(dev, &encoder->base);
  1185.         kfree(encoder->name);
  1186.         list_del(&encoder->head);
  1187.         dev->mode_config.num_encoder--;
  1188.         drm_modeset_unlock_all(dev);
  1189.  
  1190.         memset(encoder, 0, sizeof(*encoder));
  1191. }
  1192. EXPORT_SYMBOL(drm_encoder_cleanup);
  1193.  
  1194. static unsigned int drm_num_planes(struct drm_device *dev)
  1195. {
  1196.         unsigned int num = 0;
  1197.         struct drm_plane *tmp;
  1198.  
  1199.         drm_for_each_plane(tmp, dev) {
  1200.                 num++;
  1201.         }
  1202.  
  1203.         return num;
  1204. }
  1205.  
  1206. /**
  1207.  * drm_universal_plane_init - Initialize a new universal plane object
  1208.  * @dev: DRM device
  1209.  * @plane: plane object to init
  1210.  * @possible_crtcs: bitmask of possible CRTCs
  1211.  * @funcs: callbacks for the new plane
  1212.  * @formats: array of supported formats (%DRM_FORMAT_*)
  1213.  * @format_count: number of elements in @formats
  1214.  * @type: type of plane (overlay, primary, cursor)
  1215.  * @name: printf style format string for the plane name, or NULL for default name
  1216.  *
  1217.  * Initializes a plane object of type @type.
  1218.  *
  1219.  * Returns:
  1220.  * Zero on success, error code on failure.
  1221.  */
  1222. int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
  1223.                              unsigned long possible_crtcs,
  1224.                              const struct drm_plane_funcs *funcs,
  1225.                              const uint32_t *formats, unsigned int format_count,
  1226.                              enum drm_plane_type type,
  1227.                              const char *name, ...)
  1228. {
  1229.         struct drm_mode_config *config = &dev->mode_config;
  1230.         int ret;
  1231.  
  1232.         ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
  1233.         if (ret)
  1234.                 return ret;
  1235.  
  1236.         drm_modeset_lock_init(&plane->mutex);
  1237.  
  1238.         plane->base.properties = &plane->properties;
  1239.         plane->dev = dev;
  1240.         plane->funcs = funcs;
  1241.         plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
  1242.                                             GFP_KERNEL);
  1243.         if (!plane->format_types) {
  1244.                 DRM_DEBUG_KMS("out of memory when allocating plane\n");
  1245.                 drm_mode_object_put(dev, &plane->base);
  1246.                 return -ENOMEM;
  1247.         }
  1248.  
  1249.         if (name) {
  1250.                 va_list ap;
  1251.  
  1252.                 va_start(ap, name);
  1253.                 plane->name = kvasprintf(GFP_KERNEL, name, ap);
  1254.                 va_end(ap);
  1255.         } else {
  1256.                 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
  1257.                                         drm_num_planes(dev));
  1258.         }
  1259.         if (!plane->name) {
  1260.                 kfree(plane->format_types);
  1261.                 drm_mode_object_put(dev, &plane->base);
  1262.                 return -ENOMEM;
  1263.         }
  1264.  
  1265.         memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
  1266.         plane->format_count = format_count;
  1267.         plane->possible_crtcs = possible_crtcs;
  1268.         plane->type = type;
  1269.  
  1270.         list_add_tail(&plane->head, &config->plane_list);
  1271.         config->num_total_plane++;
  1272.         if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  1273.                 config->num_overlay_plane++;
  1274.  
  1275.         drm_object_attach_property(&plane->base,
  1276.                                    config->plane_type_property,
  1277.                                    plane->type);
  1278.  
  1279.         if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  1280.                 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
  1281.                 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
  1282.                 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
  1283.                 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
  1284.                 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
  1285.                 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
  1286.                 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
  1287.                 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
  1288.                 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
  1289.                 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
  1290.         }
  1291.  
  1292.         return 0;
  1293. }
  1294. EXPORT_SYMBOL(drm_universal_plane_init);
  1295.  
  1296. /**
  1297.  * drm_plane_init - Initialize a legacy plane
  1298.  * @dev: DRM device
  1299.  * @plane: plane object to init
  1300.  * @possible_crtcs: bitmask of possible CRTCs
  1301.  * @funcs: callbacks for the new plane
  1302.  * @formats: array of supported formats (%DRM_FORMAT_*)
  1303.  * @format_count: number of elements in @formats
  1304.  * @is_primary: plane type (primary vs overlay)
  1305.  *
  1306.  * Legacy API to initialize a DRM plane.
  1307.  *
  1308.  * New drivers should call drm_universal_plane_init() instead.
  1309.  *
  1310.  * Returns:
  1311.  * Zero on success, error code on failure.
  1312.  */
  1313. int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
  1314.                    unsigned long possible_crtcs,
  1315.                    const struct drm_plane_funcs *funcs,
  1316.                    const uint32_t *formats, unsigned int format_count,
  1317.                    bool is_primary)
  1318. {
  1319.         enum drm_plane_type type;
  1320.  
  1321.         type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
  1322.         return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
  1323.                                         formats, format_count, type, NULL);
  1324. }
  1325. EXPORT_SYMBOL(drm_plane_init);
  1326.  
  1327. /**
  1328.  * drm_plane_cleanup - Clean up the core plane usage
  1329.  * @plane: plane to cleanup
  1330.  *
  1331.  * This function cleans up @plane and removes it from the DRM mode setting
  1332.  * core. Note that the function does *not* free the plane structure itself,
  1333.  * this is the responsibility of the caller.
  1334.  */
  1335. void drm_plane_cleanup(struct drm_plane *plane)
  1336. {
  1337.         struct drm_device *dev = plane->dev;
  1338.  
  1339.         drm_modeset_lock_all(dev);
  1340.         kfree(plane->format_types);
  1341.         drm_mode_object_put(dev, &plane->base);
  1342.  
  1343.         BUG_ON(list_empty(&plane->head));
  1344.  
  1345.         list_del(&plane->head);
  1346.         dev->mode_config.num_total_plane--;
  1347.         if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  1348.                 dev->mode_config.num_overlay_plane--;
  1349.         drm_modeset_unlock_all(dev);
  1350.  
  1351.         WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
  1352.         if (plane->state && plane->funcs->atomic_destroy_state)
  1353.                 plane->funcs->atomic_destroy_state(plane, plane->state);
  1354.  
  1355.         kfree(plane->name);
  1356.  
  1357.         memset(plane, 0, sizeof(*plane));
  1358. }
  1359. EXPORT_SYMBOL(drm_plane_cleanup);
  1360.  
  1361. /**
  1362.  * drm_plane_index - find the index of a registered plane
  1363.  * @plane: plane to find index for
  1364.  *
  1365.  * Given a registered plane, return the index of that CRTC within a DRM
  1366.  * device's list of planes.
  1367.  */
  1368. unsigned int drm_plane_index(struct drm_plane *plane)
  1369. {
  1370.         unsigned int index = 0;
  1371.         struct drm_plane *tmp;
  1372.  
  1373.         drm_for_each_plane(tmp, plane->dev) {
  1374.                 if (tmp == plane)
  1375.                         return index;
  1376.  
  1377.                 index++;
  1378.         }
  1379.  
  1380.         BUG();
  1381. }
  1382. EXPORT_SYMBOL(drm_plane_index);
  1383.  
  1384. /**
  1385.  * drm_plane_from_index - find the registered plane at an index
  1386.  * @dev: DRM device
  1387.  * @idx: index of registered plane to find for
  1388.  *
  1389.  * Given a plane index, return the registered plane from DRM device's
  1390.  * list of planes with matching index.
  1391.  */
  1392. struct drm_plane *
  1393. drm_plane_from_index(struct drm_device *dev, int idx)
  1394. {
  1395.         struct drm_plane *plane;
  1396.         unsigned int i = 0;
  1397.  
  1398.         drm_for_each_plane(plane, dev) {
  1399.                 if (i == idx)
  1400.                         return plane;
  1401.                 i++;
  1402.         }
  1403.         return NULL;
  1404. }
  1405. EXPORT_SYMBOL(drm_plane_from_index);
  1406.  
  1407. /**
  1408.  * drm_plane_force_disable - Forcibly disable a plane
  1409.  * @plane: plane to disable
  1410.  *
  1411.  * Forces the plane to be disabled.
  1412.  *
  1413.  * Used when the plane's current framebuffer is destroyed,
  1414.  * and when restoring fbdev mode.
  1415.  */
  1416. void drm_plane_force_disable(struct drm_plane *plane)
  1417. {
  1418.         int ret;
  1419.  
  1420.         if (!plane->fb)
  1421.                 return;
  1422.  
  1423.         plane->old_fb = plane->fb;
  1424.         ret = plane->funcs->disable_plane(plane);
  1425.         if (ret) {
  1426.                 DRM_ERROR("failed to disable plane with busy fb\n");
  1427.                 plane->old_fb = NULL;
  1428.                 return;
  1429.         }
  1430.         /* disconnect the plane from the fb and crtc: */
  1431.         drm_framebuffer_unreference(plane->old_fb);
  1432.         plane->old_fb = NULL;
  1433.         plane->fb = NULL;
  1434.         plane->crtc = NULL;
  1435. }
  1436. EXPORT_SYMBOL(drm_plane_force_disable);
  1437.  
  1438. static int drm_mode_create_standard_properties(struct drm_device *dev)
  1439. {
  1440.         struct drm_property *prop;
  1441.  
  1442.         /*
  1443.          * Standard properties (apply to all connectors)
  1444.          */
  1445.         prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
  1446.                                    DRM_MODE_PROP_IMMUTABLE,
  1447.                                    "EDID", 0);
  1448.         if (!prop)
  1449.                 return -ENOMEM;
  1450.         dev->mode_config.edid_property = prop;
  1451.  
  1452.         prop = drm_property_create_enum(dev, 0,
  1453.                                    "DPMS", drm_dpms_enum_list,
  1454.                                    ARRAY_SIZE(drm_dpms_enum_list));
  1455.         if (!prop)
  1456.                 return -ENOMEM;
  1457.         dev->mode_config.dpms_property = prop;
  1458.  
  1459.         prop = drm_property_create(dev,
  1460.                                    DRM_MODE_PROP_BLOB |
  1461.                                    DRM_MODE_PROP_IMMUTABLE,
  1462.                                    "PATH", 0);
  1463.         if (!prop)
  1464.                 return -ENOMEM;
  1465.         dev->mode_config.path_property = prop;
  1466.  
  1467.         prop = drm_property_create(dev,
  1468.                                    DRM_MODE_PROP_BLOB |
  1469.                                    DRM_MODE_PROP_IMMUTABLE,
  1470.                                    "TILE", 0);
  1471.         if (!prop)
  1472.                 return -ENOMEM;
  1473.         dev->mode_config.tile_property = prop;
  1474.  
  1475.         prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1476.                                         "type", drm_plane_type_enum_list,
  1477.                                         ARRAY_SIZE(drm_plane_type_enum_list));
  1478.         if (!prop)
  1479.                 return -ENOMEM;
  1480.         dev->mode_config.plane_type_property = prop;
  1481.  
  1482.         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1483.                         "SRC_X", 0, UINT_MAX);
  1484.         if (!prop)
  1485.                 return -ENOMEM;
  1486.         dev->mode_config.prop_src_x = prop;
  1487.  
  1488.         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1489.                         "SRC_Y", 0, UINT_MAX);
  1490.         if (!prop)
  1491.                 return -ENOMEM;
  1492.         dev->mode_config.prop_src_y = prop;
  1493.  
  1494.         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1495.                         "SRC_W", 0, UINT_MAX);
  1496.         if (!prop)
  1497.                 return -ENOMEM;
  1498.         dev->mode_config.prop_src_w = prop;
  1499.  
  1500.         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1501.                         "SRC_H", 0, UINT_MAX);
  1502.         if (!prop)
  1503.                 return -ENOMEM;
  1504.         dev->mode_config.prop_src_h = prop;
  1505.  
  1506.         prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
  1507.                         "CRTC_X", INT_MIN, INT_MAX);
  1508.         if (!prop)
  1509.                 return -ENOMEM;
  1510.         dev->mode_config.prop_crtc_x = prop;
  1511.  
  1512.         prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
  1513.                         "CRTC_Y", INT_MIN, INT_MAX);
  1514.         if (!prop)
  1515.                 return -ENOMEM;
  1516.         dev->mode_config.prop_crtc_y = prop;
  1517.  
  1518.         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1519.                         "CRTC_W", 0, INT_MAX);
  1520.         if (!prop)
  1521.                 return -ENOMEM;
  1522.         dev->mode_config.prop_crtc_w = prop;
  1523.  
  1524.         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1525.                         "CRTC_H", 0, INT_MAX);
  1526.         if (!prop)
  1527.                 return -ENOMEM;
  1528.         dev->mode_config.prop_crtc_h = prop;
  1529.  
  1530.         prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
  1531.                         "FB_ID", DRM_MODE_OBJECT_FB);
  1532.         if (!prop)
  1533.                 return -ENOMEM;
  1534.         dev->mode_config.prop_fb_id = prop;
  1535.  
  1536.         prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
  1537.                         "CRTC_ID", DRM_MODE_OBJECT_CRTC);
  1538.         if (!prop)
  1539.                 return -ENOMEM;
  1540.         dev->mode_config.prop_crtc_id = prop;
  1541.  
  1542.         prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
  1543.                         "ACTIVE");
  1544.         if (!prop)
  1545.                 return -ENOMEM;
  1546.         dev->mode_config.prop_active = prop;
  1547.  
  1548.         prop = drm_property_create(dev,
  1549.                         DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
  1550.                         "MODE_ID", 0);
  1551.         if (!prop)
  1552.                 return -ENOMEM;
  1553.         dev->mode_config.prop_mode_id = prop;
  1554.  
  1555.         prop = drm_property_create(dev,
  1556.                         DRM_MODE_PROP_BLOB,
  1557.                         "DEGAMMA_LUT", 0);
  1558.         if (!prop)
  1559.                 return -ENOMEM;
  1560.         dev->mode_config.degamma_lut_property = prop;
  1561.  
  1562.         prop = drm_property_create_range(dev,
  1563.                         DRM_MODE_PROP_IMMUTABLE,
  1564.                         "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
  1565.         if (!prop)
  1566.                 return -ENOMEM;
  1567.         dev->mode_config.degamma_lut_size_property = prop;
  1568.  
  1569.         prop = drm_property_create(dev,
  1570.                         DRM_MODE_PROP_BLOB,
  1571.                         "CTM", 0);
  1572.         if (!prop)
  1573.                 return -ENOMEM;
  1574.         dev->mode_config.ctm_property = prop;
  1575.  
  1576.         prop = drm_property_create(dev,
  1577.                         DRM_MODE_PROP_BLOB,
  1578.                         "GAMMA_LUT", 0);
  1579.         if (!prop)
  1580.                 return -ENOMEM;
  1581.         dev->mode_config.gamma_lut_property = prop;
  1582.  
  1583.         prop = drm_property_create_range(dev,
  1584.                         DRM_MODE_PROP_IMMUTABLE,
  1585.                         "GAMMA_LUT_SIZE", 0, UINT_MAX);
  1586.         if (!prop)
  1587.                 return -ENOMEM;
  1588.         dev->mode_config.gamma_lut_size_property = prop;
  1589.  
  1590.         return 0;
  1591. }
  1592.  
  1593. /**
  1594.  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
  1595.  * @dev: DRM device
  1596.  *
  1597.  * Called by a driver the first time a DVI-I connector is made.
  1598.  */
  1599. int drm_mode_create_dvi_i_properties(struct drm_device *dev)
  1600. {
  1601.         struct drm_property *dvi_i_selector;
  1602.         struct drm_property *dvi_i_subconnector;
  1603.  
  1604.         if (dev->mode_config.dvi_i_select_subconnector_property)
  1605.                 return 0;
  1606.  
  1607.         dvi_i_selector =
  1608.                 drm_property_create_enum(dev, 0,
  1609.                                     "select subconnector",
  1610.                                     drm_dvi_i_select_enum_list,
  1611.                                     ARRAY_SIZE(drm_dvi_i_select_enum_list));
  1612.         dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
  1613.  
  1614.         dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1615.                                     "subconnector",
  1616.                                     drm_dvi_i_subconnector_enum_list,
  1617.                                     ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
  1618.         dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
  1619.  
  1620.         return 0;
  1621. }
  1622. EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
  1623.  
  1624. /**
  1625.  * drm_create_tv_properties - create TV specific connector properties
  1626.  * @dev: DRM device
  1627.  * @num_modes: number of different TV formats (modes) supported
  1628.  * @modes: array of pointers to strings containing name of each format
  1629.  *
  1630.  * Called by a driver's TV initialization routine, this function creates
  1631.  * the TV specific connector properties for a given device.  Caller is
  1632.  * responsible for allocating a list of format names and passing them to
  1633.  * this routine.
  1634.  */
  1635. int drm_mode_create_tv_properties(struct drm_device *dev,
  1636.                                   unsigned int num_modes,
  1637.                                   const char * const modes[])
  1638. {
  1639.         struct drm_property *tv_selector;
  1640.         struct drm_property *tv_subconnector;
  1641.         unsigned int i;
  1642.  
  1643.         if (dev->mode_config.tv_select_subconnector_property)
  1644.                 return 0;
  1645.  
  1646.         /*
  1647.          * Basic connector properties
  1648.          */
  1649.         tv_selector = drm_property_create_enum(dev, 0,
  1650.                                           "select subconnector",
  1651.                                           drm_tv_select_enum_list,
  1652.                                           ARRAY_SIZE(drm_tv_select_enum_list));
  1653.         if (!tv_selector)
  1654.                 goto nomem;
  1655.  
  1656.         dev->mode_config.tv_select_subconnector_property = tv_selector;
  1657.  
  1658.         tv_subconnector =
  1659.                 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1660.                                     "subconnector",
  1661.                                     drm_tv_subconnector_enum_list,
  1662.                                     ARRAY_SIZE(drm_tv_subconnector_enum_list));
  1663.         if (!tv_subconnector)
  1664.                 goto nomem;
  1665.         dev->mode_config.tv_subconnector_property = tv_subconnector;
  1666.  
  1667.         /*
  1668.          * Other, TV specific properties: margins & TV modes.
  1669.          */
  1670.         dev->mode_config.tv_left_margin_property =
  1671.                 drm_property_create_range(dev, 0, "left margin", 0, 100);
  1672.         if (!dev->mode_config.tv_left_margin_property)
  1673.                 goto nomem;
  1674.  
  1675.         dev->mode_config.tv_right_margin_property =
  1676.                 drm_property_create_range(dev, 0, "right margin", 0, 100);
  1677.         if (!dev->mode_config.tv_right_margin_property)
  1678.                 goto nomem;
  1679.  
  1680.         dev->mode_config.tv_top_margin_property =
  1681.                 drm_property_create_range(dev, 0, "top margin", 0, 100);
  1682.         if (!dev->mode_config.tv_top_margin_property)
  1683.                 goto nomem;
  1684.  
  1685.         dev->mode_config.tv_bottom_margin_property =
  1686.                 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
  1687.         if (!dev->mode_config.tv_bottom_margin_property)
  1688.                 goto nomem;
  1689.  
  1690.         dev->mode_config.tv_mode_property =
  1691.                 drm_property_create(dev, DRM_MODE_PROP_ENUM,
  1692.                                     "mode", num_modes);
  1693.         if (!dev->mode_config.tv_mode_property)
  1694.                 goto nomem;
  1695.  
  1696.         for (i = 0; i < num_modes; i++)
  1697.                 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
  1698.                                       i, modes[i]);
  1699.  
  1700.         dev->mode_config.tv_brightness_property =
  1701.                 drm_property_create_range(dev, 0, "brightness", 0, 100);
  1702.         if (!dev->mode_config.tv_brightness_property)
  1703.                 goto nomem;
  1704.  
  1705.         dev->mode_config.tv_contrast_property =
  1706.                 drm_property_create_range(dev, 0, "contrast", 0, 100);
  1707.         if (!dev->mode_config.tv_contrast_property)
  1708.                 goto nomem;
  1709.  
  1710.         dev->mode_config.tv_flicker_reduction_property =
  1711.                 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
  1712.         if (!dev->mode_config.tv_flicker_reduction_property)
  1713.                 goto nomem;
  1714.  
  1715.         dev->mode_config.tv_overscan_property =
  1716.                 drm_property_create_range(dev, 0, "overscan", 0, 100);
  1717.         if (!dev->mode_config.tv_overscan_property)
  1718.                 goto nomem;
  1719.  
  1720.         dev->mode_config.tv_saturation_property =
  1721.                 drm_property_create_range(dev, 0, "saturation", 0, 100);
  1722.         if (!dev->mode_config.tv_saturation_property)
  1723.                 goto nomem;
  1724.  
  1725.         dev->mode_config.tv_hue_property =
  1726.                 drm_property_create_range(dev, 0, "hue", 0, 100);
  1727.         if (!dev->mode_config.tv_hue_property)
  1728.                 goto nomem;
  1729.  
  1730.         return 0;
  1731. nomem:
  1732.         return -ENOMEM;
  1733. }
  1734. EXPORT_SYMBOL(drm_mode_create_tv_properties);
  1735.  
  1736. /**
  1737.  * drm_mode_create_scaling_mode_property - create scaling mode property
  1738.  * @dev: DRM device
  1739.  *
  1740.  * Called by a driver the first time it's needed, must be attached to desired
  1741.  * connectors.
  1742.  */
  1743. int drm_mode_create_scaling_mode_property(struct drm_device *dev)
  1744. {
  1745.         struct drm_property *scaling_mode;
  1746.  
  1747.         if (dev->mode_config.scaling_mode_property)
  1748.                 return 0;
  1749.  
  1750.         scaling_mode =
  1751.                 drm_property_create_enum(dev, 0, "scaling mode",
  1752.                                 drm_scaling_mode_enum_list,
  1753.                                     ARRAY_SIZE(drm_scaling_mode_enum_list));
  1754.  
  1755.         dev->mode_config.scaling_mode_property = scaling_mode;
  1756.  
  1757.         return 0;
  1758. }
  1759. EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
  1760.  
  1761. /**
  1762.  * drm_mode_create_aspect_ratio_property - create aspect ratio property
  1763.  * @dev: DRM device
  1764.  *
  1765.  * Called by a driver the first time it's needed, must be attached to desired
  1766.  * connectors.
  1767.  *
  1768.  * Returns:
  1769.  * Zero on success, negative errno on failure.
  1770.  */
  1771. int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
  1772. {
  1773.         if (dev->mode_config.aspect_ratio_property)
  1774.                 return 0;
  1775.  
  1776.         dev->mode_config.aspect_ratio_property =
  1777.                 drm_property_create_enum(dev, 0, "aspect ratio",
  1778.                                 drm_aspect_ratio_enum_list,
  1779.                                 ARRAY_SIZE(drm_aspect_ratio_enum_list));
  1780.  
  1781.         if (dev->mode_config.aspect_ratio_property == NULL)
  1782.                 return -ENOMEM;
  1783.  
  1784.         return 0;
  1785. }
  1786. EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
  1787.  
  1788. /**
  1789.  * drm_mode_create_dirty_property - create dirty property
  1790.  * @dev: DRM device
  1791.  *
  1792.  * Called by a driver the first time it's needed, must be attached to desired
  1793.  * connectors.
  1794.  */
  1795. int drm_mode_create_dirty_info_property(struct drm_device *dev)
  1796. {
  1797.         struct drm_property *dirty_info;
  1798.  
  1799.         if (dev->mode_config.dirty_info_property)
  1800.                 return 0;
  1801.  
  1802.         dirty_info =
  1803.                 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1804.                                     "dirty",
  1805.                                     drm_dirty_info_enum_list,
  1806.                                     ARRAY_SIZE(drm_dirty_info_enum_list));
  1807.         dev->mode_config.dirty_info_property = dirty_info;
  1808.  
  1809.         return 0;
  1810. }
  1811. EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
  1812.  
  1813. /**
  1814.  * drm_mode_create_suggested_offset_properties - create suggests offset properties
  1815.  * @dev: DRM device
  1816.  *
  1817.  * Create the the suggested x/y offset property for connectors.
  1818.  */
  1819. int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
  1820. {
  1821.         if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
  1822.                 return 0;
  1823.  
  1824.         dev->mode_config.suggested_x_property =
  1825.                 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
  1826.  
  1827.         dev->mode_config.suggested_y_property =
  1828.                 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
  1829.  
  1830.         if (dev->mode_config.suggested_x_property == NULL ||
  1831.             dev->mode_config.suggested_y_property == NULL)
  1832.                 return -ENOMEM;
  1833.         return 0;
  1834. }
  1835. EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
  1836.  
  1837. #if 0
  1838. /**
  1839.  * drm_mode_getresources - get graphics configuration
  1840.  * @dev: drm device for the ioctl
  1841.  * @data: data pointer for the ioctl
  1842.  * @file_priv: drm file for the ioctl call
  1843.  *
  1844.  * Construct a set of configuration description structures and return
  1845.  * them to the user, including CRTC, connector and framebuffer configuration.
  1846.  *
  1847.  * Called by the user via ioctl.
  1848.  *
  1849.  * Returns:
  1850.  * Zero on success, negative errno on failure.
  1851.  */
  1852. int drm_mode_getresources(struct drm_device *dev, void *data,
  1853.                           struct drm_file *file_priv)
  1854. {
  1855.         struct drm_mode_card_res *card_res = data;
  1856.         struct list_head *lh;
  1857.         struct drm_framebuffer *fb;
  1858.         struct drm_connector *connector;
  1859.         struct drm_crtc *crtc;
  1860.         struct drm_encoder *encoder;
  1861.         int ret = 0;
  1862.         int connector_count = 0;
  1863.         int crtc_count = 0;
  1864.         int fb_count = 0;
  1865.         int encoder_count = 0;
  1866.         int copied = 0;
  1867.         uint32_t __user *fb_id;
  1868.         uint32_t __user *crtc_id;
  1869.         uint32_t __user *connector_id;
  1870.         uint32_t __user *encoder_id;
  1871.  
  1872.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1873.                 return -EINVAL;
  1874.  
  1875.  
  1876.         mutex_lock(&file_priv->fbs_lock);
  1877.         /*
  1878.          * For the non-control nodes we need to limit the list of resources
  1879.          * by IDs in the group list for this node
  1880.          */
  1881.         list_for_each(lh, &file_priv->fbs)
  1882.                 fb_count++;
  1883.  
  1884.         /* handle this in 4 parts */
  1885.         /* FBs */
  1886.         if (card_res->count_fbs >= fb_count) {
  1887.                 copied = 0;
  1888.                 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
  1889.                 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
  1890.                         if (put_user(fb->base.id, fb_id + copied)) {
  1891.                                 mutex_unlock(&file_priv->fbs_lock);
  1892.                                 return -EFAULT;
  1893.                         }
  1894.                         copied++;
  1895.                 }
  1896.         }
  1897.         card_res->count_fbs = fb_count;
  1898.         mutex_unlock(&file_priv->fbs_lock);
  1899.  
  1900.         /* mode_config.mutex protects the connector list against e.g. DP MST
  1901.          * connector hot-adding. CRTC/Plane lists are invariant. */
  1902.         mutex_lock(&dev->mode_config.mutex);
  1903.         drm_for_each_crtc(crtc, dev)
  1904.                 crtc_count++;
  1905.  
  1906.         drm_for_each_connector(connector, dev)
  1907.                 connector_count++;
  1908.  
  1909.         drm_for_each_encoder(encoder, dev)
  1910.                 encoder_count++;
  1911.  
  1912.         card_res->max_height = dev->mode_config.max_height;
  1913.         card_res->min_height = dev->mode_config.min_height;
  1914.         card_res->max_width = dev->mode_config.max_width;
  1915.         card_res->min_width = dev->mode_config.min_width;
  1916.  
  1917.         /* CRTCs */
  1918.         if (card_res->count_crtcs >= crtc_count) {
  1919.                 copied = 0;
  1920.                 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
  1921.                 drm_for_each_crtc(crtc, dev) {
  1922.                         DRM_DEBUG_KMS("[CRTC:%d:%s]\n",
  1923.                                       crtc->base.id, crtc->name);
  1924.                         if (put_user(crtc->base.id, crtc_id + copied)) {
  1925.                                 ret = -EFAULT;
  1926.                                 goto out;
  1927.                         }
  1928.                         copied++;
  1929.                 }
  1930.         }
  1931.         card_res->count_crtcs = crtc_count;
  1932.  
  1933.         /* Encoders */
  1934.         if (card_res->count_encoders >= encoder_count) {
  1935.                 copied = 0;
  1936.                 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
  1937.                 drm_for_each_encoder(encoder, dev) {
  1938.                         DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
  1939.                                         encoder->name);
  1940.                         if (put_user(encoder->base.id, encoder_id +
  1941.                                      copied)) {
  1942.                                 ret = -EFAULT;
  1943.                                 goto out;
  1944.                         }
  1945.                         copied++;
  1946.                 }
  1947.         }
  1948.         card_res->count_encoders = encoder_count;
  1949.  
  1950.         /* Connectors */
  1951.         if (card_res->count_connectors >= connector_count) {
  1952.                 copied = 0;
  1953.                 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
  1954.                 drm_for_each_connector(connector, dev) {
  1955.                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1956.                                 connector->base.id,
  1957.                                 connector->name);
  1958.                         if (put_user(connector->base.id,
  1959.                                      connector_id + copied)) {
  1960.                                 ret = -EFAULT;
  1961.                                 goto out;
  1962.                         }
  1963.                         copied++;
  1964.                 }
  1965.         }
  1966.         card_res->count_connectors = connector_count;
  1967.  
  1968.         DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
  1969.                   card_res->count_connectors, card_res->count_encoders);
  1970.  
  1971. out:
  1972.         mutex_unlock(&dev->mode_config.mutex);
  1973.         return ret;
  1974. }
  1975.  
  1976. /**
  1977.  * drm_mode_getcrtc - get CRTC configuration
  1978.  * @dev: drm device for the ioctl
  1979.  * @data: data pointer for the ioctl
  1980.  * @file_priv: drm file for the ioctl call
  1981.  *
  1982.  * Construct a CRTC configuration structure to return to the user.
  1983.  *
  1984.  * Called by the user via ioctl.
  1985.  *
  1986.  * Returns:
  1987.  * Zero on success, negative errno on failure.
  1988.  */
  1989. int drm_mode_getcrtc(struct drm_device *dev,
  1990.                      void *data, struct drm_file *file_priv)
  1991. {
  1992.         struct drm_mode_crtc *crtc_resp = data;
  1993.         struct drm_crtc *crtc;
  1994.  
  1995.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1996.                 return -EINVAL;
  1997.  
  1998.         crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
  1999.         if (!crtc)
  2000.                 return -ENOENT;
  2001.  
  2002.         drm_modeset_lock_crtc(crtc, crtc->primary);
  2003.         crtc_resp->gamma_size = crtc->gamma_size;
  2004.         if (crtc->primary->fb)
  2005.                 crtc_resp->fb_id = crtc->primary->fb->base.id;
  2006.         else
  2007.                 crtc_resp->fb_id = 0;
  2008.  
  2009.         if (crtc->state) {
  2010.                 crtc_resp->x = crtc->primary->state->src_x >> 16;
  2011.                 crtc_resp->y = crtc->primary->state->src_y >> 16;
  2012.                 if (crtc->state->enable) {
  2013.                         drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
  2014.                         crtc_resp->mode_valid = 1;
  2015.  
  2016.                 } else {
  2017.                         crtc_resp->mode_valid = 0;
  2018.                 }
  2019.         } else {
  2020.                 crtc_resp->x = crtc->x;
  2021.                 crtc_resp->y = crtc->y;
  2022.                 if (crtc->enabled) {
  2023.                         drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
  2024.                         crtc_resp->mode_valid = 1;
  2025.  
  2026.                 } else {
  2027.                         crtc_resp->mode_valid = 0;
  2028.                 }
  2029.         }
  2030.         drm_modeset_unlock_crtc(crtc);
  2031.  
  2032.         return 0;
  2033. }
  2034.  
  2035. static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
  2036.                                          const struct drm_file *file_priv)
  2037. {
  2038.         /*
  2039.          * If user-space hasn't configured the driver to expose the stereo 3D
  2040.          * modes, don't expose them.
  2041.          */
  2042.         if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
  2043.                 return false;
  2044.  
  2045.         return true;
  2046. }
  2047.  
  2048. static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
  2049. {
  2050.         /* For atomic drivers only state objects are synchronously updated and
  2051.          * protected by modeset locks, so check those first. */
  2052.         if (connector->state)
  2053.                 return connector->state->best_encoder;
  2054.         return connector->encoder;
  2055. }
  2056.  
  2057. /* helper for getconnector and getproperties ioctls */
  2058. static int get_properties(struct drm_mode_object *obj, bool atomic,
  2059.                 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
  2060.                 uint32_t *arg_count_props)
  2061. {
  2062.         int props_count;
  2063.         int i, ret, copied;
  2064.  
  2065.         props_count = obj->properties->count;
  2066.         if (!atomic)
  2067.                 props_count -= obj->properties->atomic_count;
  2068.  
  2069.         if ((*arg_count_props >= props_count) && props_count) {
  2070.                 for (i = 0, copied = 0; copied < props_count; i++) {
  2071.                         struct drm_property *prop = obj->properties->properties[i];
  2072.                         uint64_t val;
  2073.  
  2074.                         if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
  2075.                                 continue;
  2076.  
  2077.                         ret = drm_object_property_get_value(obj, prop, &val);
  2078.                         if (ret)
  2079.                                 return ret;
  2080.  
  2081.                         if (put_user(prop->base.id, prop_ptr + copied))
  2082.                                 return -EFAULT;
  2083.  
  2084.                         if (put_user(val, prop_values + copied))
  2085.                                 return -EFAULT;
  2086.  
  2087.                         copied++;
  2088.                 }
  2089.         }
  2090.         *arg_count_props = props_count;
  2091.  
  2092.         return 0;
  2093. }
  2094.  
  2095. /**
  2096.  * drm_mode_getconnector - get connector configuration
  2097.  * @dev: drm device for the ioctl
  2098.  * @data: data pointer for the ioctl
  2099.  * @file_priv: drm file for the ioctl call
  2100.  *
  2101.  * Construct a connector configuration structure to return to the user.
  2102.  *
  2103.  * Called by the user via ioctl.
  2104.  *
  2105.  * Returns:
  2106.  * Zero on success, negative errno on failure.
  2107.  */
  2108. int drm_mode_getconnector(struct drm_device *dev, void *data,
  2109.                           struct drm_file *file_priv)
  2110. {
  2111.         struct drm_mode_get_connector *out_resp = data;
  2112.         struct drm_connector *connector;
  2113.         struct drm_encoder *encoder;
  2114.         struct drm_display_mode *mode;
  2115.         int mode_count = 0;
  2116.         int encoders_count = 0;
  2117.         int ret = 0;
  2118.         int copied = 0;
  2119.         int i;
  2120.         struct drm_mode_modeinfo u_mode;
  2121.         struct drm_mode_modeinfo __user *mode_ptr;
  2122.         uint32_t __user *encoder_ptr;
  2123.  
  2124.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2125.                 return -EINVAL;
  2126.  
  2127.         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  2128.  
  2129.         DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
  2130.  
  2131.         mutex_lock(&dev->mode_config.mutex);
  2132.  
  2133.         connector = drm_connector_find(dev, out_resp->connector_id);
  2134.         if (!connector) {
  2135.                 ret = -ENOENT;
  2136.                 goto out_unlock;
  2137.         }
  2138.  
  2139.         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
  2140.                 if (connector->encoder_ids[i] != 0)
  2141.                         encoders_count++;
  2142.  
  2143.         if (out_resp->count_modes == 0) {
  2144.                 connector->funcs->fill_modes(connector,
  2145.                                              dev->mode_config.max_width,
  2146.                                              dev->mode_config.max_height);
  2147.         }
  2148.  
  2149.         /* delayed so we get modes regardless of pre-fill_modes state */
  2150.         list_for_each_entry(mode, &connector->modes, head)
  2151.                 if (drm_mode_expose_to_userspace(mode, file_priv))
  2152.                         mode_count++;
  2153.  
  2154.         out_resp->connector_id = connector->base.id;
  2155.         out_resp->connector_type = connector->connector_type;
  2156.         out_resp->connector_type_id = connector->connector_type_id;
  2157.         out_resp->mm_width = connector->display_info.width_mm;
  2158.         out_resp->mm_height = connector->display_info.height_mm;
  2159.         out_resp->subpixel = connector->display_info.subpixel_order;
  2160.         out_resp->connection = connector->status;
  2161.  
  2162.         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  2163.         encoder = drm_connector_get_encoder(connector);
  2164.         if (encoder)
  2165.                 out_resp->encoder_id = encoder->base.id;
  2166.         else
  2167.                 out_resp->encoder_id = 0;
  2168.  
  2169.         /*
  2170.          * This ioctl is called twice, once to determine how much space is
  2171.          * needed, and the 2nd time to fill it.
  2172.          */
  2173.         if ((out_resp->count_modes >= mode_count) && mode_count) {
  2174.                 copied = 0;
  2175.                 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
  2176.                 list_for_each_entry(mode, &connector->modes, head) {
  2177.                         if (!drm_mode_expose_to_userspace(mode, file_priv))
  2178.                                 continue;
  2179.  
  2180.                         drm_mode_convert_to_umode(&u_mode, mode);
  2181.                         if (copy_to_user(mode_ptr + copied,
  2182.                                          &u_mode, sizeof(u_mode))) {
  2183.                                 ret = -EFAULT;
  2184.                                 goto out;
  2185.                         }
  2186.                         copied++;
  2187.                 }
  2188.         }
  2189.         out_resp->count_modes = mode_count;
  2190.  
  2191.         ret = get_properties(&connector->base, file_priv->atomic,
  2192.                         (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
  2193.                         (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
  2194.                         &out_resp->count_props);
  2195.         if (ret)
  2196.                 goto out;
  2197.  
  2198.         if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  2199.                 copied = 0;
  2200.                 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
  2201.                 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  2202.                         if (connector->encoder_ids[i] != 0) {
  2203.                                 if (put_user(connector->encoder_ids[i],
  2204.                                              encoder_ptr + copied)) {
  2205.                                         ret = -EFAULT;
  2206.                                         goto out;
  2207.                                 }
  2208.                                 copied++;
  2209.                         }
  2210.                 }
  2211.         }
  2212.         out_resp->count_encoders = encoders_count;
  2213.  
  2214. out:
  2215.         drm_modeset_unlock(&dev->mode_config.connection_mutex);
  2216.  
  2217. out_unlock:
  2218.         mutex_unlock(&dev->mode_config.mutex);
  2219.  
  2220.         return ret;
  2221. }
  2222.  
  2223. static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
  2224. {
  2225.         struct drm_connector *connector;
  2226.         struct drm_device *dev = encoder->dev;
  2227.         bool uses_atomic = false;
  2228.  
  2229.         /* For atomic drivers only state objects are synchronously updated and
  2230.          * protected by modeset locks, so check those first. */
  2231.         drm_for_each_connector(connector, dev) {
  2232.                 if (!connector->state)
  2233.                         continue;
  2234.  
  2235.                 uses_atomic = true;
  2236.  
  2237.                 if (connector->state->best_encoder != encoder)
  2238.                         continue;
  2239.  
  2240.                 return connector->state->crtc;
  2241.         }
  2242.  
  2243.         /* Don't return stale data (e.g. pending async disable). */
  2244.         if (uses_atomic)
  2245.                 return NULL;
  2246.  
  2247.         return encoder->crtc;
  2248. }
  2249.  
  2250. /**
  2251.  * drm_mode_getencoder - get encoder configuration
  2252.  * @dev: drm device for the ioctl
  2253.  * @data: data pointer for the ioctl
  2254.  * @file_priv: drm file for the ioctl call
  2255.  *
  2256.  * Construct a encoder configuration structure to return to the user.
  2257.  *
  2258.  * Called by the user via ioctl.
  2259.  *
  2260.  * Returns:
  2261.  * Zero on success, negative errno on failure.
  2262.  */
  2263. int drm_mode_getencoder(struct drm_device *dev, void *data,
  2264.                         struct drm_file *file_priv)
  2265. {
  2266.         struct drm_mode_get_encoder *enc_resp = data;
  2267.         struct drm_encoder *encoder;
  2268.         struct drm_crtc *crtc;
  2269.  
  2270.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2271.                 return -EINVAL;
  2272.  
  2273.         encoder = drm_encoder_find(dev, enc_resp->encoder_id);
  2274.         if (!encoder)
  2275.                 return -ENOENT;
  2276.  
  2277.         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  2278.         crtc = drm_encoder_get_crtc(encoder);
  2279.         if (crtc)
  2280.                 enc_resp->crtc_id = crtc->base.id;
  2281.         else
  2282.                 enc_resp->crtc_id = 0;
  2283.         drm_modeset_unlock(&dev->mode_config.connection_mutex);
  2284.  
  2285.         enc_resp->encoder_type = encoder->encoder_type;
  2286.         enc_resp->encoder_id = encoder->base.id;
  2287.         enc_resp->possible_crtcs = encoder->possible_crtcs;
  2288.         enc_resp->possible_clones = encoder->possible_clones;
  2289.  
  2290.         return 0;
  2291. }
  2292.  
  2293. /**
  2294.  * drm_mode_getplane_res - enumerate all plane resources
  2295.  * @dev: DRM device
  2296.  * @data: ioctl data
  2297.  * @file_priv: DRM file info
  2298.  *
  2299.  * Construct a list of plane ids to return to the user.
  2300.  *
  2301.  * Called by the user via ioctl.
  2302.  *
  2303.  * Returns:
  2304.  * Zero on success, negative errno on failure.
  2305.  */
  2306. int drm_mode_getplane_res(struct drm_device *dev, void *data,
  2307.                           struct drm_file *file_priv)
  2308. {
  2309.         struct drm_mode_get_plane_res *plane_resp = data;
  2310.         struct drm_mode_config *config;
  2311.         struct drm_plane *plane;
  2312.         uint32_t __user *plane_ptr;
  2313.         int copied = 0;
  2314.         unsigned num_planes;
  2315.  
  2316.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2317.                 return -EINVAL;
  2318.  
  2319.         config = &dev->mode_config;
  2320.  
  2321.         if (file_priv->universal_planes)
  2322.                 num_planes = config->num_total_plane;
  2323.         else
  2324.                 num_planes = config->num_overlay_plane;
  2325.  
  2326.         /*
  2327.          * This ioctl is called twice, once to determine how much space is
  2328.          * needed, and the 2nd time to fill it.
  2329.          */
  2330.         if (num_planes &&
  2331.             (plane_resp->count_planes >= num_planes)) {
  2332.                 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
  2333.  
  2334.                 /* Plane lists are invariant, no locking needed. */
  2335.                 drm_for_each_plane(plane, dev) {
  2336.                         /*
  2337.                          * Unless userspace set the 'universal planes'
  2338.                          * capability bit, only advertise overlays.
  2339.                          */
  2340.                         if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
  2341.                             !file_priv->universal_planes)
  2342.                                 continue;
  2343.  
  2344.                         if (put_user(plane->base.id, plane_ptr + copied))
  2345.                                 return -EFAULT;
  2346.                         copied++;
  2347.                 }
  2348.         }
  2349.         plane_resp->count_planes = num_planes;
  2350.  
  2351.         return 0;
  2352. }
  2353.  
  2354. /**
  2355.  * drm_mode_getplane - get plane configuration
  2356.  * @dev: DRM device
  2357.  * @data: ioctl data
  2358.  * @file_priv: DRM file info
  2359.  *
  2360.  * Construct a plane configuration structure to return to the user.
  2361.  *
  2362.  * Called by the user via ioctl.
  2363.  *
  2364.  * Returns:
  2365.  * Zero on success, negative errno on failure.
  2366.  */
  2367. int drm_mode_getplane(struct drm_device *dev, void *data,
  2368.                       struct drm_file *file_priv)
  2369. {
  2370.         struct drm_mode_get_plane *plane_resp = data;
  2371.         struct drm_plane *plane;
  2372.         uint32_t __user *format_ptr;
  2373.  
  2374.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2375.                 return -EINVAL;
  2376.  
  2377.         plane = drm_plane_find(dev, plane_resp->plane_id);
  2378.         if (!plane)
  2379.                 return -ENOENT;
  2380.  
  2381.         drm_modeset_lock(&plane->mutex, NULL);
  2382.         if (plane->crtc)
  2383.                 plane_resp->crtc_id = plane->crtc->base.id;
  2384.         else
  2385.                 plane_resp->crtc_id = 0;
  2386.  
  2387.         if (plane->fb)
  2388.                 plane_resp->fb_id = plane->fb->base.id;
  2389.         else
  2390.                 plane_resp->fb_id = 0;
  2391.         drm_modeset_unlock(&plane->mutex);
  2392.  
  2393.         plane_resp->plane_id = plane->base.id;
  2394.         plane_resp->possible_crtcs = plane->possible_crtcs;
  2395.         plane_resp->gamma_size = 0;
  2396.  
  2397.         /*
  2398.          * This ioctl is called twice, once to determine how much space is
  2399.          * needed, and the 2nd time to fill it.
  2400.          */
  2401.         if (plane->format_count &&
  2402.             (plane_resp->count_format_types >= plane->format_count)) {
  2403.                 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
  2404.                 if (copy_to_user(format_ptr,
  2405.                                  plane->format_types,
  2406.                                  sizeof(uint32_t) * plane->format_count)) {
  2407.                         return -EFAULT;
  2408.                 }
  2409.         }
  2410.         plane_resp->count_format_types = plane->format_count;
  2411.  
  2412.         return 0;
  2413. }
  2414. #endif
  2415.  
  2416. /**
  2417.  * drm_plane_check_pixel_format - Check if the plane supports the pixel format
  2418.  * @plane: plane to check for format support
  2419.  * @format: the pixel format
  2420.  *
  2421.  * Returns:
  2422.  * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
  2423.  * otherwise.
  2424.  */
  2425. int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
  2426. {
  2427.         unsigned int i;
  2428.  
  2429.         for (i = 0; i < plane->format_count; i++) {
  2430.                 if (format == plane->format_types[i])
  2431.                         return 0;
  2432.         }
  2433.  
  2434.         return -EINVAL;
  2435. }
  2436.  
  2437. static int check_src_coords(uint32_t src_x, uint32_t src_y,
  2438.                             uint32_t src_w, uint32_t src_h,
  2439.                             const struct drm_framebuffer *fb)
  2440. {
  2441.         unsigned int fb_width, fb_height;
  2442.  
  2443.         fb_width = fb->width << 16;
  2444.         fb_height = fb->height << 16;
  2445.  
  2446.         /* Make sure source coordinates are inside the fb. */
  2447.         if (src_w > fb_width ||
  2448.             src_x > fb_width - src_w ||
  2449.             src_h > fb_height ||
  2450.             src_y > fb_height - src_h) {
  2451.                 DRM_DEBUG_KMS("Invalid source coordinates "
  2452.                               "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
  2453.                               src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
  2454.                               src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
  2455.                               src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
  2456.                               src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
  2457.                 return -ENOSPC;
  2458.         }
  2459.  
  2460.         return 0;
  2461. }
  2462.  
  2463. /*
  2464.  * setplane_internal - setplane handler for internal callers
  2465.  *
  2466.  * Note that we assume an extra reference has already been taken on fb.  If the
  2467.  * update fails, this reference will be dropped before return; if it succeeds,
  2468.  * the previous framebuffer (if any) will be unreferenced instead.
  2469.  *
  2470.  * src_{x,y,w,h} are provided in 16.16 fixed point format
  2471.  */
  2472. static int __setplane_internal(struct drm_plane *plane,
  2473.                                struct drm_crtc *crtc,
  2474.                                struct drm_framebuffer *fb,
  2475.                                int32_t crtc_x, int32_t crtc_y,
  2476.                                uint32_t crtc_w, uint32_t crtc_h,
  2477.                                /* src_{x,y,w,h} values are 16.16 fixed point */
  2478.                                uint32_t src_x, uint32_t src_y,
  2479.                                uint32_t src_w, uint32_t src_h)
  2480. {
  2481.         int ret = 0;
  2482.  
  2483.         /* No fb means shut it down */
  2484.         if (!fb) {
  2485.                 plane->old_fb = plane->fb;
  2486.                 ret = plane->funcs->disable_plane(plane);
  2487.                 if (!ret) {
  2488.                         plane->crtc = NULL;
  2489.                         plane->fb = NULL;
  2490.                 } else {
  2491.                         plane->old_fb = NULL;
  2492.                 }
  2493.                 goto out;
  2494.         }
  2495.  
  2496.         /* Check whether this plane is usable on this CRTC */
  2497.         if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
  2498.                 DRM_DEBUG_KMS("Invalid crtc for plane\n");
  2499.                 ret = -EINVAL;
  2500.                 goto out;
  2501.         }
  2502.  
  2503.         /* Check whether this plane supports the fb pixel format. */
  2504.         ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
  2505.         if (ret) {
  2506.                 DRM_DEBUG_KMS("Invalid pixel format %s\n",
  2507.                               drm_get_format_name(fb->pixel_format));
  2508.                 goto out;
  2509.         }
  2510.  
  2511.         /* Give drivers some help against integer overflows */
  2512.         if (crtc_w > INT_MAX ||
  2513.             crtc_x > INT_MAX - (int32_t) crtc_w ||
  2514.             crtc_h > INT_MAX ||
  2515.             crtc_y > INT_MAX - (int32_t) crtc_h) {
  2516.                 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
  2517.                               crtc_w, crtc_h, crtc_x, crtc_y);
  2518.                 ret = -ERANGE;
  2519.                 goto out;
  2520.         }
  2521.  
  2522.         ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
  2523.         if (ret)
  2524.                 goto out;
  2525.  
  2526.         plane->old_fb = plane->fb;
  2527.         ret = plane->funcs->update_plane(plane, crtc, fb,
  2528.                                          crtc_x, crtc_y, crtc_w, crtc_h,
  2529.                                          src_x, src_y, src_w, src_h);
  2530.         if (!ret) {
  2531.                 plane->crtc = crtc;
  2532.                 plane->fb = fb;
  2533.                 fb = NULL;
  2534.         } else {
  2535.                 plane->old_fb = NULL;
  2536.         }
  2537.  
  2538. out:
  2539.         if (fb)
  2540.                 drm_framebuffer_unreference(fb);
  2541.         if (plane->old_fb)
  2542.                 drm_framebuffer_unreference(plane->old_fb);
  2543.         plane->old_fb = NULL;
  2544.  
  2545.         return ret;
  2546. }
  2547.  
  2548. static int setplane_internal(struct drm_plane *plane,
  2549.                              struct drm_crtc *crtc,
  2550.                              struct drm_framebuffer *fb,
  2551.                              int32_t crtc_x, int32_t crtc_y,
  2552.                              uint32_t crtc_w, uint32_t crtc_h,
  2553.                              /* src_{x,y,w,h} values are 16.16 fixed point */
  2554.                              uint32_t src_x, uint32_t src_y,
  2555.                              uint32_t src_w, uint32_t src_h)
  2556. {
  2557.         int ret;
  2558.  
  2559.         drm_modeset_lock_all(plane->dev);
  2560.         ret = __setplane_internal(plane, crtc, fb,
  2561.                                   crtc_x, crtc_y, crtc_w, crtc_h,
  2562.                                   src_x, src_y, src_w, src_h);
  2563.         drm_modeset_unlock_all(plane->dev);
  2564.  
  2565.         return ret;
  2566. }
  2567.  
  2568. /**
  2569.  * drm_mode_setplane - configure a plane's configuration
  2570.  * @dev: DRM device
  2571.  * @data: ioctl data*
  2572.  * @file_priv: DRM file info
  2573.  *
  2574.  * Set plane configuration, including placement, fb, scaling, and other factors.
  2575.  * Or pass a NULL fb to disable (planes may be disabled without providing a
  2576.  * valid crtc).
  2577.  *
  2578.  * Returns:
  2579.  * Zero on success, negative errno on failure.
  2580.  */
  2581. int drm_mode_setplane(struct drm_device *dev, void *data,
  2582.                       struct drm_file *file_priv)
  2583. {
  2584.         struct drm_mode_set_plane *plane_req = data;
  2585.         struct drm_plane *plane;
  2586.         struct drm_crtc *crtc = NULL;
  2587.         struct drm_framebuffer *fb = NULL;
  2588.  
  2589.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2590.                 return -EINVAL;
  2591.  
  2592.         /*
  2593.          * First, find the plane, crtc, and fb objects.  If not available,
  2594.          * we don't bother to call the driver.
  2595.          */
  2596.         plane = drm_plane_find(dev, plane_req->plane_id);
  2597.         if (!plane) {
  2598.                 DRM_DEBUG_KMS("Unknown plane ID %d\n",
  2599.                               plane_req->plane_id);
  2600.                 return -ENOENT;
  2601.         }
  2602.  
  2603.         if (plane_req->fb_id) {
  2604.                 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
  2605.                 if (!fb) {
  2606.                         DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
  2607.                                       plane_req->fb_id);
  2608.                         return -ENOENT;
  2609.                 }
  2610.  
  2611.                 crtc = drm_crtc_find(dev, plane_req->crtc_id);
  2612.                 if (!crtc) {
  2613.                         DRM_DEBUG_KMS("Unknown crtc ID %d\n",
  2614.                                       plane_req->crtc_id);
  2615.                         return -ENOENT;
  2616.                 }
  2617.         }
  2618.  
  2619.         /*
  2620.          * setplane_internal will take care of deref'ing either the old or new
  2621.          * framebuffer depending on success.
  2622.          */
  2623.         return setplane_internal(plane, crtc, fb,
  2624.                                  plane_req->crtc_x, plane_req->crtc_y,
  2625.                                  plane_req->crtc_w, plane_req->crtc_h,
  2626.                                  plane_req->src_x, plane_req->src_y,
  2627.                                  plane_req->src_w, plane_req->src_h);
  2628. }
  2629.  
  2630. /**
  2631.  * drm_mode_set_config_internal - helper to call ->set_config
  2632.  * @set: modeset config to set
  2633.  *
  2634.  * This is a little helper to wrap internal calls to the ->set_config driver
  2635.  * interface. The only thing it adds is correct refcounting dance.
  2636.  *
  2637.  * Returns:
  2638.  * Zero on success, negative errno on failure.
  2639.  */
  2640. int drm_mode_set_config_internal(struct drm_mode_set *set)
  2641. {
  2642.         struct drm_crtc *crtc = set->crtc;
  2643.         struct drm_framebuffer *fb;
  2644.         struct drm_crtc *tmp;
  2645.         int ret;
  2646.  
  2647.         /*
  2648.          * NOTE: ->set_config can also disable other crtcs (if we steal all
  2649.          * connectors from it), hence we need to refcount the fbs across all
  2650.          * crtcs. Atomic modeset will have saner semantics ...
  2651.          */
  2652.         drm_for_each_crtc(tmp, crtc->dev)
  2653.                 tmp->primary->old_fb = tmp->primary->fb;
  2654.  
  2655.         fb = set->fb;
  2656.  
  2657.         ret = crtc->funcs->set_config(set);
  2658.         if (ret == 0) {
  2659.                 crtc->primary->crtc = crtc;
  2660.                 crtc->primary->fb = fb;
  2661.         }
  2662.  
  2663.         drm_for_each_crtc(tmp, crtc->dev) {
  2664.                 if (tmp->primary->fb)
  2665.                         drm_framebuffer_reference(tmp->primary->fb);
  2666.                 if (tmp->primary->old_fb)
  2667.                         drm_framebuffer_unreference(tmp->primary->old_fb);
  2668.                 tmp->primary->old_fb = NULL;
  2669.         }
  2670.  
  2671.         return ret;
  2672. }
  2673. EXPORT_SYMBOL(drm_mode_set_config_internal);
  2674.  
  2675. /**
  2676.  * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
  2677.  * @mode: mode to query
  2678.  * @hdisplay: hdisplay value to fill in
  2679.  * @vdisplay: vdisplay value to fill in
  2680.  *
  2681.  * The vdisplay value will be doubled if the specified mode is a stereo mode of
  2682.  * the appropriate layout.
  2683.  */
  2684. void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
  2685.                             int *hdisplay, int *vdisplay)
  2686. {
  2687.         struct drm_display_mode adjusted;
  2688.  
  2689.         drm_mode_copy(&adjusted, mode);
  2690.         drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
  2691.         *hdisplay = adjusted.crtc_hdisplay;
  2692.         *vdisplay = adjusted.crtc_vdisplay;
  2693. }
  2694. EXPORT_SYMBOL(drm_crtc_get_hv_timing);
  2695.  
  2696. /**
  2697.  * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
  2698.  *     CRTC viewport
  2699.  * @crtc: CRTC that framebuffer will be displayed on
  2700.  * @x: x panning
  2701.  * @y: y panning
  2702.  * @mode: mode that framebuffer will be displayed under
  2703.  * @fb: framebuffer to check size of
  2704.  */
  2705. int drm_crtc_check_viewport(const struct drm_crtc *crtc,
  2706.                             int x, int y,
  2707.                             const struct drm_display_mode *mode,
  2708.                             const struct drm_framebuffer *fb)
  2709.  
  2710. {
  2711.         int hdisplay, vdisplay;
  2712.  
  2713.         drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  2714.  
  2715.         if (crtc->state &&
  2716.             crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
  2717.                                               BIT(DRM_ROTATE_270)))
  2718.                 swap(hdisplay, vdisplay);
  2719.  
  2720.         return check_src_coords(x << 16, y << 16,
  2721.                                 hdisplay << 16, vdisplay << 16, fb);
  2722. }
  2723. EXPORT_SYMBOL(drm_crtc_check_viewport);
  2724.  
  2725. #if 0
  2726. /**
  2727.  * drm_mode_setcrtc - set CRTC configuration
  2728.  * @dev: drm device for the ioctl
  2729.  * @data: data pointer for the ioctl
  2730.  * @file_priv: drm file for the ioctl call
  2731.  *
  2732.  * Build a new CRTC configuration based on user request.
  2733.  *
  2734.  * Called by the user via ioctl.
  2735.  *
  2736.  * Returns:
  2737.  * Zero on success, negative errno on failure.
  2738.  */
  2739. int drm_mode_setcrtc(struct drm_device *dev, void *data,
  2740.                      struct drm_file *file_priv)
  2741. {
  2742.         struct drm_mode_config *config = &dev->mode_config;
  2743.         struct drm_mode_crtc *crtc_req = data;
  2744.         struct drm_crtc *crtc;
  2745.         struct drm_connector **connector_set = NULL, *connector;
  2746.         struct drm_framebuffer *fb = NULL;
  2747.         struct drm_display_mode *mode = NULL;
  2748.         struct drm_mode_set set;
  2749.         uint32_t __user *set_connectors_ptr;
  2750.         int ret;
  2751.         int i;
  2752.  
  2753.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2754.                 return -EINVAL;
  2755.  
  2756.         /*
  2757.          * Universal plane src offsets are only 16.16, prevent havoc for
  2758.          * drivers using universal plane code internally.
  2759.          */
  2760.         if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
  2761.                 return -ERANGE;
  2762.  
  2763.         drm_modeset_lock_all(dev);
  2764.         crtc = drm_crtc_find(dev, crtc_req->crtc_id);
  2765.         if (!crtc) {
  2766.                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
  2767.                 ret = -ENOENT;
  2768.                 goto out;
  2769.         }
  2770.         DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  2771.  
  2772.         if (crtc_req->mode_valid) {
  2773.                 /* If we have a mode we need a framebuffer. */
  2774.                 /* If we pass -1, set the mode with the currently bound fb */
  2775.                 if (crtc_req->fb_id == -1) {
  2776.                         if (!crtc->primary->fb) {
  2777.                                 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
  2778.                                 ret = -EINVAL;
  2779.                                 goto out;
  2780.                         }
  2781.                         fb = crtc->primary->fb;
  2782.                         /* Make refcounting symmetric with the lookup path. */
  2783.                         drm_framebuffer_reference(fb);
  2784.                 } else {
  2785.                         fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
  2786.                         if (!fb) {
  2787.                                 DRM_DEBUG_KMS("Unknown FB ID%d\n",
  2788.                                                 crtc_req->fb_id);
  2789.                                 ret = -ENOENT;
  2790.                                 goto out;
  2791.                         }
  2792.                 }
  2793.  
  2794.                 mode = drm_mode_create(dev);
  2795.                 if (!mode) {
  2796.                         ret = -ENOMEM;
  2797.                         goto out;
  2798.                 }
  2799.  
  2800.                 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
  2801.                 if (ret) {
  2802.                         DRM_DEBUG_KMS("Invalid mode\n");
  2803.                         goto out;
  2804.                 }
  2805.  
  2806.                 /*
  2807.                  * Check whether the primary plane supports the fb pixel format.
  2808.                  * Drivers not implementing the universal planes API use a
  2809.                  * default formats list provided by the DRM core which doesn't
  2810.                  * match real hardware capabilities. Skip the check in that
  2811.                  * case.
  2812.                  */
  2813.                 if (!crtc->primary->format_default) {
  2814.                         ret = drm_plane_check_pixel_format(crtc->primary,
  2815.                                                            fb->pixel_format);
  2816.                         if (ret) {
  2817.                                 DRM_DEBUG_KMS("Invalid pixel format %s\n",
  2818.                                         drm_get_format_name(fb->pixel_format));
  2819.                                 goto out;
  2820.                         }
  2821.                 }
  2822.  
  2823.                 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
  2824.                                               mode, fb);
  2825.                 if (ret)
  2826.                         goto out;
  2827.  
  2828.         }
  2829.  
  2830.         if (crtc_req->count_connectors == 0 && mode) {
  2831.                 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
  2832.                 ret = -EINVAL;
  2833.                 goto out;
  2834.         }
  2835.  
  2836.         if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  2837.                 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
  2838.                           crtc_req->count_connectors);
  2839.                 ret = -EINVAL;
  2840.                 goto out;
  2841.         }
  2842.  
  2843.         if (crtc_req->count_connectors > 0) {
  2844.                 u32 out_id;
  2845.  
  2846.                 /* Avoid unbounded kernel memory allocation */
  2847.                 if (crtc_req->count_connectors > config->num_connector) {
  2848.                         ret = -EINVAL;
  2849.                         goto out;
  2850.                 }
  2851.  
  2852.                 connector_set = kmalloc_array(crtc_req->count_connectors,
  2853.                                               sizeof(struct drm_connector *),
  2854.                                               GFP_KERNEL);
  2855.                 if (!connector_set) {
  2856.                         ret = -ENOMEM;
  2857.                         goto out;
  2858.                 }
  2859.  
  2860.                 for (i = 0; i < crtc_req->count_connectors; i++) {
  2861.                         set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
  2862.                         if (get_user(out_id, &set_connectors_ptr[i])) {
  2863.                                 ret = -EFAULT;
  2864.                                 goto out;
  2865.                         }
  2866.  
  2867.                         connector = drm_connector_find(dev, out_id);
  2868.                         if (!connector) {
  2869.                                 DRM_DEBUG_KMS("Connector id %d unknown\n",
  2870.                                                 out_id);
  2871.                                 ret = -ENOENT;
  2872.                                 goto out;
  2873.                         }
  2874.                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  2875.                                         connector->base.id,
  2876.                                         connector->name);
  2877.  
  2878.                         connector_set[i] = connector;
  2879.                 }
  2880.         }
  2881.  
  2882.         set.crtc = crtc;
  2883.         set.x = crtc_req->x;
  2884.         set.y = crtc_req->y;
  2885.         set.mode = mode;
  2886.         set.connectors = connector_set;
  2887.         set.num_connectors = crtc_req->count_connectors;
  2888.         set.fb = fb;
  2889.         ret = drm_mode_set_config_internal(&set);
  2890.  
  2891. out:
  2892.         if (fb)
  2893.                 drm_framebuffer_unreference(fb);
  2894.  
  2895.         kfree(connector_set);
  2896.         drm_mode_destroy(dev, mode);
  2897.         drm_modeset_unlock_all(dev);
  2898.         return ret;
  2899. }
  2900.  
  2901. static int drm_mode_cursor_common(struct drm_device *dev,
  2902.                                   struct drm_mode_cursor2 *req,
  2903.                                   struct drm_file *file_priv)
  2904. {
  2905.         struct drm_crtc *crtc;
  2906.         int ret = 0;
  2907.  
  2908.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2909.                 return -EINVAL;
  2910.  
  2911.         if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
  2912.                 return -EINVAL;
  2913.  
  2914.         crtc = drm_crtc_find(dev, req->crtc_id);
  2915.         if (!crtc) {
  2916.                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  2917.                 return -ENOENT;
  2918.         }
  2919.  
  2920.         /*
  2921.          * If this crtc has a universal cursor plane, call that plane's update
  2922.          * handler rather than using legacy cursor handlers.
  2923.          */
  2924.         drm_modeset_lock_crtc(crtc, crtc