Subversion Repositories Kolibri OS

Rev

Rev 6296 | Rev 6937 | Go to most recent revision | 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.                             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.         mutex_lock(&dev->mode_config.idr_mutex);
  434.         idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
  435.         mutex_unlock(&dev->mode_config.idr_mutex);
  436.  
  437.         fb->base.id = 0;
  438. }
  439.  
  440. static void drm_framebuffer_free(struct kref *kref)
  441. {
  442.         struct drm_framebuffer *fb =
  443.                         container_of(kref, struct drm_framebuffer, refcount);
  444.         struct drm_device *dev = fb->dev;
  445.  
  446.         /*
  447.          * The lookup idr holds a weak reference, which has not necessarily been
  448.          * removed at this point. Check for that.
  449.          */
  450.         mutex_lock(&dev->mode_config.fb_lock);
  451.         if (fb->base.id) {
  452.                 /* Mark fb as reaped and drop idr ref. */
  453.                 __drm_framebuffer_unregister(dev, fb);
  454.         }
  455.         mutex_unlock(&dev->mode_config.fb_lock);
  456.  
  457.         fb->funcs->destroy(fb);
  458. }
  459.  
  460. static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
  461.                                                         uint32_t id)
  462. {
  463.         struct drm_mode_object *obj = NULL;
  464.         struct drm_framebuffer *fb;
  465.  
  466.         mutex_lock(&dev->mode_config.idr_mutex);
  467.         obj = idr_find(&dev->mode_config.crtc_idr, id);
  468.         if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
  469.                 fb = NULL;
  470.         else
  471.                 fb = obj_to_fb(obj);
  472.         mutex_unlock(&dev->mode_config.idr_mutex);
  473.  
  474.         return fb;
  475. }
  476.  
  477. /**
  478.  * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
  479.  * @dev: drm device
  480.  * @id: id of the fb object
  481.  *
  482.  * If successful, this grabs an additional reference to the framebuffer -
  483.  * callers need to make sure to eventually unreference the returned framebuffer
  484.  * again, using @drm_framebuffer_unreference.
  485.  */
  486. struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
  487.                                                uint32_t id)
  488. {
  489.         struct drm_framebuffer *fb;
  490.  
  491.         mutex_lock(&dev->mode_config.fb_lock);
  492.         fb = __drm_framebuffer_lookup(dev, id);
  493.         if (fb) {
  494.                 if (!kref_get_unless_zero(&fb->refcount))
  495.                         fb = NULL;
  496.         }
  497.         mutex_unlock(&dev->mode_config.fb_lock);
  498.  
  499.         return fb;
  500. }
  501. EXPORT_SYMBOL(drm_framebuffer_lookup);
  502.  
  503. /**
  504.  * drm_framebuffer_unreference - unref a framebuffer
  505.  * @fb: framebuffer to unref
  506.  *
  507.  * This functions decrements the fb's refcount and frees it if it drops to zero.
  508.  */
  509. void drm_framebuffer_unreference(struct drm_framebuffer *fb)
  510. {
  511.         DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
  512.         kref_put(&fb->refcount, drm_framebuffer_free);
  513. }
  514. EXPORT_SYMBOL(drm_framebuffer_unreference);
  515.  
  516. /**
  517.  * drm_framebuffer_reference - incr the fb refcnt
  518.  * @fb: framebuffer
  519.  *
  520.  * This functions increments the fb's refcount.
  521.  */
  522. void drm_framebuffer_reference(struct drm_framebuffer *fb)
  523. {
  524.         DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
  525.         kref_get(&fb->refcount);
  526. }
  527. EXPORT_SYMBOL(drm_framebuffer_reference);
  528.  
  529. /**
  530.  * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
  531.  * @fb: fb to unregister
  532.  *
  533.  * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
  534.  * those used for fbdev. Note that the caller must hold a reference of it's own,
  535.  * i.e. the object may not be destroyed through this call (since it'll lead to a
  536.  * locking inversion).
  537.  */
  538. void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
  539. {
  540.         struct drm_device *dev;
  541.  
  542.         if (!fb)
  543.                 return;
  544.  
  545.         dev = fb->dev;
  546.  
  547.         mutex_lock(&dev->mode_config.fb_lock);
  548.         /* Mark fb as reaped and drop idr ref. */
  549.         __drm_framebuffer_unregister(dev, fb);
  550.         mutex_unlock(&dev->mode_config.fb_lock);
  551. }
  552. EXPORT_SYMBOL(drm_framebuffer_unregister_private);
  553.  
  554. /**
  555.  * drm_framebuffer_cleanup - remove a framebuffer object
  556.  * @fb: framebuffer to remove
  557.  *
  558.  * Cleanup framebuffer. This function is intended to be used from the drivers
  559.  * ->destroy callback. It can also be used to clean up driver private
  560.  *  framebuffers embedded into a larger structure.
  561.  *
  562.  * Note that this function does not remove the fb from active usuage - if it is
  563.  * still used anywhere, hilarity can ensue since userspace could call getfb on
  564.  * the id and get back -EINVAL. Obviously no concern at driver unload time.
  565.  *
  566.  * Also, the framebuffer will not be removed from the lookup idr - for
  567.  * user-created framebuffers this will happen in in the rmfb ioctl. For
  568.  * driver-private objects (e.g. for fbdev) drivers need to explicitly call
  569.  * drm_framebuffer_unregister_private.
  570.  */
  571. void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
  572. {
  573.         struct drm_device *dev = fb->dev;
  574.  
  575.         mutex_lock(&dev->mode_config.fb_lock);
  576.         list_del(&fb->head);
  577.         dev->mode_config.num_fb--;
  578.         mutex_unlock(&dev->mode_config.fb_lock);
  579. }
  580. EXPORT_SYMBOL(drm_framebuffer_cleanup);
  581.  
  582. /**
  583.  * drm_framebuffer_remove - remove and unreference a framebuffer object
  584.  * @fb: framebuffer to remove
  585.  *
  586.  * Scans all the CRTCs and planes in @dev's mode_config.  If they're
  587.  * using @fb, removes it, setting it to NULL. Then drops the reference to the
  588.  * passed-in framebuffer. Might take the modeset locks.
  589.  *
  590.  * Note that this function optimizes the cleanup away if the caller holds the
  591.  * last reference to the framebuffer. It is also guaranteed to not take the
  592.  * modeset locks in this case.
  593.  */
  594. void drm_framebuffer_remove(struct drm_framebuffer *fb)
  595. {
  596.         struct drm_device *dev;
  597.         struct drm_crtc *crtc;
  598.         struct drm_plane *plane;
  599.         struct drm_mode_set set;
  600.         int ret;
  601.  
  602.         if (!fb)
  603.                 return;
  604.  
  605.         dev = fb->dev;
  606.  
  607.         WARN_ON(!list_empty(&fb->filp_head));
  608.  
  609.         /*
  610.          * drm ABI mandates that we remove any deleted framebuffers from active
  611.          * useage. But since most sane clients only remove framebuffers they no
  612.          * longer need, try to optimize this away.
  613.          *
  614.          * Since we're holding a reference ourselves, observing a refcount of 1
  615.          * means that we're the last holder and can skip it. Also, the refcount
  616.          * can never increase from 1 again, so we don't need any barriers or
  617.          * locks.
  618.          *
  619.          * Note that userspace could try to race with use and instate a new
  620.          * usage _after_ we've cleared all current ones. End result will be an
  621.          * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
  622.          * in this manner.
  623.          */
  624.         if (atomic_read(&fb->refcount.refcount) > 1) {
  625.                 drm_modeset_lock_all(dev);
  626.                 /* remove from any CRTC */
  627.                 drm_for_each_crtc(crtc, dev) {
  628.                         if (crtc->primary->fb == fb) {
  629.                                 /* should turn off the crtc */
  630.                                 memset(&set, 0, sizeof(struct drm_mode_set));
  631.                                 set.crtc = crtc;
  632.                                 set.fb = NULL;
  633.                                 ret = drm_mode_set_config_internal(&set);
  634.                                 if (ret)
  635.                                         DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
  636.                         }
  637.                 }
  638.  
  639.                 drm_for_each_plane(plane, dev) {
  640.                         if (plane->fb == fb)
  641.                                 drm_plane_force_disable(plane);
  642.                 }
  643.                 drm_modeset_unlock_all(dev);
  644.         }
  645.  
  646.         drm_framebuffer_unreference(fb);
  647. }
  648. EXPORT_SYMBOL(drm_framebuffer_remove);
  649.  
  650. DEFINE_WW_CLASS(crtc_ww_class);
  651.  
  652. /**
  653.  * drm_crtc_init_with_planes - Initialise a new CRTC object with
  654.  *    specified primary and cursor planes.
  655.  * @dev: DRM device
  656.  * @crtc: CRTC object to init
  657.  * @primary: Primary plane for CRTC
  658.  * @cursor: Cursor plane for CRTC
  659.  * @funcs: callbacks for the new CRTC
  660.  *
  661.  * Inits a new object created as base part of a driver crtc object.
  662.  *
  663.  * Returns:
  664.  * Zero on success, error code on failure.
  665.  */
  666. int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
  667.                               struct drm_plane *primary,
  668.                               struct drm_plane *cursor,
  669.                               const struct drm_crtc_funcs *funcs)
  670. {
  671.         struct drm_mode_config *config = &dev->mode_config;
  672.         int ret;
  673.  
  674.         WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
  675.         WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
  676.  
  677.         crtc->dev = dev;
  678.         crtc->funcs = funcs;
  679.  
  680.         drm_modeset_lock_init(&crtc->mutex);
  681.         ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
  682.         if (ret)
  683.                 return ret;
  684.  
  685.         crtc->base.properties = &crtc->properties;
  686.  
  687.         list_add_tail(&crtc->head, &config->crtc_list);
  688.         config->num_crtc++;
  689.  
  690.         crtc->primary = primary;
  691.         crtc->cursor = cursor;
  692.         if (primary)
  693.                 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
  694.         if (cursor)
  695.                 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
  696.  
  697.         if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  698.                 drm_object_attach_property(&crtc->base, config->prop_active, 0);
  699.                 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
  700.         }
  701.  
  702.         return 0;
  703. }
  704. EXPORT_SYMBOL(drm_crtc_init_with_planes);
  705.  
  706. /**
  707.  * drm_crtc_cleanup - Clean up the core crtc usage
  708.  * @crtc: CRTC to cleanup
  709.  *
  710.  * This function cleans up @crtc and removes it from the DRM mode setting
  711.  * core. Note that the function does *not* free the crtc structure itself,
  712.  * this is the responsibility of the caller.
  713.  */
  714. void drm_crtc_cleanup(struct drm_crtc *crtc)
  715. {
  716.         struct drm_device *dev = crtc->dev;
  717.  
  718.         kfree(crtc->gamma_store);
  719.         crtc->gamma_store = NULL;
  720.  
  721.         drm_modeset_lock_fini(&crtc->mutex);
  722.  
  723.         drm_mode_object_put(dev, &crtc->base);
  724.         list_del(&crtc->head);
  725.         dev->mode_config.num_crtc--;
  726.  
  727.         WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
  728.         if (crtc->state && crtc->funcs->atomic_destroy_state)
  729.                 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
  730.  
  731.         memset(crtc, 0, sizeof(*crtc));
  732. }
  733. EXPORT_SYMBOL(drm_crtc_cleanup);
  734.  
  735. /**
  736.  * drm_crtc_index - find the index of a registered CRTC
  737.  * @crtc: CRTC to find index for
  738.  *
  739.  * Given a registered CRTC, return the index of that CRTC within a DRM
  740.  * device's list of CRTCs.
  741.  */
  742. unsigned int drm_crtc_index(struct drm_crtc *crtc)
  743. {
  744.         unsigned int index = 0;
  745.         struct drm_crtc *tmp;
  746.  
  747.         drm_for_each_crtc(tmp, crtc->dev) {
  748.                 if (tmp == crtc)
  749.                         return index;
  750.  
  751.                 index++;
  752.         }
  753.  
  754.         BUG();
  755. }
  756. EXPORT_SYMBOL(drm_crtc_index);
  757.  
  758. /*
  759.  * drm_mode_remove - remove and free a mode
  760.  * @connector: connector list to modify
  761.  * @mode: mode to remove
  762.  *
  763.  * Remove @mode from @connector's mode list, then free it.
  764.  */
  765. static void drm_mode_remove(struct drm_connector *connector,
  766.                             struct drm_display_mode *mode)
  767. {
  768.         list_del(&mode->head);
  769.         drm_mode_destroy(connector->dev, mode);
  770. }
  771.  
  772. /**
  773.  * drm_display_info_set_bus_formats - set the supported bus formats
  774.  * @info: display info to store bus formats in
  775.  * @formats: array containing the supported bus formats
  776.  * @num_formats: the number of entries in the fmts array
  777.  *
  778.  * Store the supported bus formats in display info structure.
  779.  * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
  780.  * a full list of available formats.
  781.  */
  782. int drm_display_info_set_bus_formats(struct drm_display_info *info,
  783.                                      const u32 *formats,
  784.                                      unsigned int num_formats)
  785. {
  786.         u32 *fmts = NULL;
  787.  
  788.         if (!formats && num_formats)
  789.                 return -EINVAL;
  790.  
  791.         if (formats && num_formats) {
  792.                 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
  793.                                GFP_KERNEL);
  794.                 if (!fmts)
  795.                         return -ENOMEM;
  796.         }
  797.  
  798.         kfree(info->bus_formats);
  799.         info->bus_formats = fmts;
  800.         info->num_bus_formats = num_formats;
  801.  
  802.         return 0;
  803. }
  804. EXPORT_SYMBOL(drm_display_info_set_bus_formats);
  805.  
  806. /**
  807.  * drm_connector_get_cmdline_mode - reads the user's cmdline mode
  808.  * @connector: connector to quwery
  809.  *
  810.  * The kernel supports per-connector configration of its consoles through
  811.  * use of the video= parameter. This function parses that option and
  812.  * extracts the user's specified mode (or enable/disable status) for a
  813.  * particular connector. This is typically only used during the early fbdev
  814.  * setup.
  815.  */
  816. static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
  817. {
  818.         struct drm_cmdline_mode *mode = &connector->cmdline_mode;
  819.         char *option = NULL;
  820.  
  821.         if (fb_get_options(connector->name, &option))
  822.                 return;
  823.  
  824.         if (!drm_mode_parse_command_line_for_connector(option,
  825.                                                        connector,
  826.                                                        mode))
  827.                 return;
  828.  
  829.         if (mode->force) {
  830.                 const char *s;
  831.  
  832.                 switch (mode->force) {
  833.                 case DRM_FORCE_OFF:
  834.                         s = "OFF";
  835.                         break;
  836.                 case DRM_FORCE_ON_DIGITAL:
  837.                         s = "ON - dig";
  838.                         break;
  839.                 default:
  840.                 case DRM_FORCE_ON:
  841.                         s = "ON";
  842.                         break;
  843.                 }
  844.  
  845.                 DRM_INFO("forcing %s connector %s\n", connector->name, s);
  846.                 connector->force = mode->force;
  847.         }
  848.  
  849.         DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
  850.                       connector->name,
  851.                       mode->xres, mode->yres,
  852.                       mode->refresh_specified ? mode->refresh : 60,
  853.                       mode->rb ? " reduced blanking" : "",
  854.                       mode->margins ? " with margins" : "",
  855.                       mode->interlace ?  " interlaced" : "");
  856. }
  857.  
  858. /**
  859.  * drm_connector_init - Init a preallocated connector
  860.  * @dev: DRM device
  861.  * @connector: the connector to init
  862.  * @funcs: callbacks for this connector
  863.  * @connector_type: user visible type of the connector
  864.  *
  865.  * Initialises a preallocated connector. Connectors should be
  866.  * subclassed as part of driver connector objects.
  867.  *
  868.  * Returns:
  869.  * Zero on success, error code on failure.
  870.  */
  871. int drm_connector_init(struct drm_device *dev,
  872.                        struct drm_connector *connector,
  873.                        const struct drm_connector_funcs *funcs,
  874.                        int connector_type)
  875. {
  876.         struct drm_mode_config *config = &dev->mode_config;
  877.         int ret;
  878.         struct ida *connector_ida =
  879.                 &drm_connector_enum_list[connector_type].ida;
  880.  
  881.         drm_modeset_lock_all(dev);
  882.  
  883.         ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
  884.         if (ret)
  885.                 goto out_unlock;
  886.  
  887.         connector->base.properties = &connector->properties;
  888.         connector->dev = dev;
  889.         connector->funcs = funcs;
  890.         connector->connector_type = connector_type;
  891.         connector->connector_type_id =
  892.                 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
  893.         if (connector->connector_type_id < 0) {
  894.                 ret = connector->connector_type_id;
  895.                 goto out_put;
  896.         }
  897.         connector->name =
  898.                 kasprintf(GFP_KERNEL, "%s-%d",
  899.                           drm_connector_enum_list[connector_type].name,
  900.                           connector->connector_type_id);
  901.         if (!connector->name) {
  902.                 ret = -ENOMEM;
  903.                 goto out_put;
  904.         }
  905.  
  906.         INIT_LIST_HEAD(&connector->probed_modes);
  907.         INIT_LIST_HEAD(&connector->modes);
  908.         connector->edid_blob_ptr = NULL;
  909.         connector->status = connector_status_unknown;
  910.  
  911.         drm_connector_get_cmdline_mode(connector);
  912.  
  913.         /* We should add connectors at the end to avoid upsetting the connector
  914.          * index too much. */
  915.         list_add_tail(&connector->head, &config->connector_list);
  916.         config->num_connector++;
  917.  
  918.         if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
  919.                 drm_object_attach_property(&connector->base,
  920.                                               config->edid_property,
  921.                                               0);
  922.  
  923.         drm_object_attach_property(&connector->base,
  924.                                       config->dpms_property, 0);
  925.  
  926.         if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  927.                 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
  928.         }
  929.  
  930.         connector->debugfs_entry = NULL;
  931.  
  932. out_put:
  933.         if (ret)
  934.                 drm_mode_object_put(dev, &connector->base);
  935.  
  936. out_unlock:
  937.         drm_modeset_unlock_all(dev);
  938.  
  939.         return ret;
  940. }
  941. EXPORT_SYMBOL(drm_connector_init);
  942.  
  943. /**
  944.  * drm_connector_cleanup - cleans up an initialised connector
  945.  * @connector: connector to cleanup
  946.  *
  947.  * Cleans up the connector but doesn't free the object.
  948.  */
  949. void drm_connector_cleanup(struct drm_connector *connector)
  950. {
  951.         struct drm_device *dev = connector->dev;
  952.         struct drm_display_mode *mode, *t;
  953.  
  954.         if (connector->tile_group) {
  955.                 drm_mode_put_tile_group(dev, connector->tile_group);
  956.                 connector->tile_group = NULL;
  957.         }
  958.  
  959.         list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
  960.                 drm_mode_remove(connector, mode);
  961.  
  962.         list_for_each_entry_safe(mode, t, &connector->modes, head)
  963.                 drm_mode_remove(connector, mode);
  964.  
  965.         ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
  966.                    connector->connector_type_id);
  967.  
  968.         kfree(connector->display_info.bus_formats);
  969.         drm_mode_object_put(dev, &connector->base);
  970.         kfree(connector->name);
  971.         connector->name = NULL;
  972.         list_del(&connector->head);
  973.         dev->mode_config.num_connector--;
  974.  
  975.         WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
  976.         if (connector->state && connector->funcs->atomic_destroy_state)
  977.                 connector->funcs->atomic_destroy_state(connector,
  978.                                                        connector->state);
  979.  
  980.         memset(connector, 0, sizeof(*connector));
  981. }
  982. EXPORT_SYMBOL(drm_connector_cleanup);
  983.  
  984. /**
  985.  * drm_connector_index - find the index of a registered connector
  986.  * @connector: connector to find index for
  987.  *
  988.  * Given a registered connector, return the index of that connector within a DRM
  989.  * device's list of connectors.
  990.  */
  991. unsigned int drm_connector_index(struct drm_connector *connector)
  992. {
  993.         unsigned int index = 0;
  994.         struct drm_connector *tmp;
  995.         struct drm_mode_config *config = &connector->dev->mode_config;
  996.  
  997.         WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  998.  
  999.         drm_for_each_connector(tmp, connector->dev) {
  1000.                 if (tmp == connector)
  1001.                         return index;
  1002.  
  1003.                 index++;
  1004.         }
  1005.  
  1006.         BUG();
  1007. }
  1008. EXPORT_SYMBOL(drm_connector_index);
  1009.  
  1010. /**
  1011.  * drm_connector_register - register a connector
  1012.  * @connector: the connector to register
  1013.  *
  1014.  * Register userspace interfaces for a connector
  1015.  *
  1016.  * Returns:
  1017.  * Zero on success, error code on failure.
  1018.  */
  1019. int drm_connector_register(struct drm_connector *connector)
  1020. {
  1021.         int ret;
  1022.  
  1023.         drm_mode_object_register(connector->dev, &connector->base);
  1024.  
  1025.         ret = drm_sysfs_connector_add(connector);
  1026.         if (ret)
  1027.                 return ret;
  1028.  
  1029.         ret = drm_debugfs_connector_add(connector);
  1030.         if (ret) {
  1031.                 drm_sysfs_connector_remove(connector);
  1032.                 return ret;
  1033.         }
  1034.  
  1035.         return 0;
  1036. }
  1037. EXPORT_SYMBOL(drm_connector_register);
  1038.  
  1039. /**
  1040.  * drm_connector_unregister - unregister a connector
  1041.  * @connector: the connector to unregister
  1042.  *
  1043.  * Unregister userspace interfaces for a connector
  1044.  */
  1045. void drm_connector_unregister(struct drm_connector *connector)
  1046. {
  1047.         drm_sysfs_connector_remove(connector);
  1048.         drm_debugfs_connector_remove(connector);
  1049. }
  1050. EXPORT_SYMBOL(drm_connector_unregister);
  1051.  
  1052.  
  1053. /**
  1054.  * drm_connector_unplug_all - unregister connector userspace interfaces
  1055.  * @dev: drm device
  1056.  *
  1057.  * This function unregisters all connector userspace interfaces in sysfs. Should
  1058.  * be call when the device is disconnected, e.g. from an usb driver's
  1059.  * ->disconnect callback.
  1060.  */
  1061. void drm_connector_unplug_all(struct drm_device *dev)
  1062. {
  1063.         struct drm_connector *connector;
  1064.  
  1065.         /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
  1066.         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  1067.                 drm_connector_unregister(connector);
  1068.  
  1069. }
  1070. EXPORT_SYMBOL(drm_connector_unplug_all);
  1071.  
  1072. /**
  1073.  * drm_encoder_init - Init a preallocated encoder
  1074.  * @dev: drm device
  1075.  * @encoder: the encoder to init
  1076.  * @funcs: callbacks for this encoder
  1077.  * @encoder_type: user visible type of the encoder
  1078.  *
  1079.  * Initialises a preallocated encoder. Encoder should be
  1080.  * subclassed as part of driver encoder objects.
  1081.  *
  1082.  * Returns:
  1083.  * Zero on success, error code on failure.
  1084.  */
  1085. int drm_encoder_init(struct drm_device *dev,
  1086.                       struct drm_encoder *encoder,
  1087.                       const struct drm_encoder_funcs *funcs,
  1088.                       int encoder_type)
  1089. {
  1090.         int ret;
  1091.  
  1092.         drm_modeset_lock_all(dev);
  1093.  
  1094.         ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
  1095.         if (ret)
  1096.                 goto out_unlock;
  1097.  
  1098.         encoder->dev = dev;
  1099.         encoder->encoder_type = encoder_type;
  1100.         encoder->funcs = funcs;
  1101.         encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
  1102.                                   drm_encoder_enum_list[encoder_type].name,
  1103.                                   encoder->base.id);
  1104.         if (!encoder->name) {
  1105.                 ret = -ENOMEM;
  1106.                 goto out_put;
  1107.         }
  1108.  
  1109.         list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
  1110.         dev->mode_config.num_encoder++;
  1111.  
  1112. out_put:
  1113.         if (ret)
  1114.                 drm_mode_object_put(dev, &encoder->base);
  1115.  
  1116. out_unlock:
  1117.         drm_modeset_unlock_all(dev);
  1118.  
  1119.         return ret;
  1120. }
  1121. EXPORT_SYMBOL(drm_encoder_init);
  1122.  
  1123. /**
  1124.  * drm_encoder_cleanup - cleans up an initialised encoder
  1125.  * @encoder: encoder to cleanup
  1126.  *
  1127.  * Cleans up the encoder but doesn't free the object.
  1128.  */
  1129. void drm_encoder_cleanup(struct drm_encoder *encoder)
  1130. {
  1131.         struct drm_device *dev = encoder->dev;
  1132.  
  1133.         drm_modeset_lock_all(dev);
  1134.         drm_mode_object_put(dev, &encoder->base);
  1135.         kfree(encoder->name);
  1136.         list_del(&encoder->head);
  1137.         dev->mode_config.num_encoder--;
  1138.         drm_modeset_unlock_all(dev);
  1139.  
  1140.         memset(encoder, 0, sizeof(*encoder));
  1141. }
  1142. EXPORT_SYMBOL(drm_encoder_cleanup);
  1143.  
  1144. /**
  1145.  * drm_universal_plane_init - Initialize a new universal plane object
  1146.  * @dev: DRM device
  1147.  * @plane: plane object to init
  1148.  * @possible_crtcs: bitmask of possible CRTCs
  1149.  * @funcs: callbacks for the new plane
  1150.  * @formats: array of supported formats (%DRM_FORMAT_*)
  1151.  * @format_count: number of elements in @formats
  1152.  * @type: type of plane (overlay, primary, cursor)
  1153.  *
  1154.  * Initializes a plane object of type @type.
  1155.  *
  1156.  * Returns:
  1157.  * Zero on success, error code on failure.
  1158.  */
  1159. int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
  1160.                              unsigned long possible_crtcs,
  1161.                              const struct drm_plane_funcs *funcs,
  1162.                              const uint32_t *formats, unsigned int format_count,
  1163.                              enum drm_plane_type type)
  1164. {
  1165.         struct drm_mode_config *config = &dev->mode_config;
  1166.         int ret;
  1167.  
  1168.         ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
  1169.         if (ret)
  1170.                 return ret;
  1171.  
  1172.         drm_modeset_lock_init(&plane->mutex);
  1173.  
  1174.         plane->base.properties = &plane->properties;
  1175.         plane->dev = dev;
  1176.         plane->funcs = funcs;
  1177.         plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
  1178.                                             GFP_KERNEL);
  1179.         if (!plane->format_types) {
  1180.                 DRM_DEBUG_KMS("out of memory when allocating plane\n");
  1181.                 drm_mode_object_put(dev, &plane->base);
  1182.                 return -ENOMEM;
  1183.         }
  1184.  
  1185.         memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
  1186.         plane->format_count = format_count;
  1187.         plane->possible_crtcs = possible_crtcs;
  1188.         plane->type = type;
  1189.  
  1190.         list_add_tail(&plane->head, &config->plane_list);
  1191.         config->num_total_plane++;
  1192.         if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  1193.                 config->num_overlay_plane++;
  1194.  
  1195.         drm_object_attach_property(&plane->base,
  1196.                                    config->plane_type_property,
  1197.                                    plane->type);
  1198.  
  1199.         if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  1200.                 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
  1201.                 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
  1202.                 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
  1203.                 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
  1204.                 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
  1205.                 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
  1206.                 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
  1207.                 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
  1208.                 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
  1209.                 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
  1210.         }
  1211.  
  1212.         return 0;
  1213. }
  1214. EXPORT_SYMBOL(drm_universal_plane_init);
  1215.  
  1216. /**
  1217.  * drm_plane_init - Initialize a legacy plane
  1218.  * @dev: DRM device
  1219.  * @plane: plane object to init
  1220.  * @possible_crtcs: bitmask of possible CRTCs
  1221.  * @funcs: callbacks for the new plane
  1222.  * @formats: array of supported formats (%DRM_FORMAT_*)
  1223.  * @format_count: number of elements in @formats
  1224.  * @is_primary: plane type (primary vs overlay)
  1225.  *
  1226.  * Legacy API to initialize a DRM plane.
  1227.  *
  1228.  * New drivers should call drm_universal_plane_init() instead.
  1229.  *
  1230.  * Returns:
  1231.  * Zero on success, error code on failure.
  1232.  */
  1233. int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
  1234.                    unsigned long possible_crtcs,
  1235.                    const struct drm_plane_funcs *funcs,
  1236.                    const uint32_t *formats, unsigned int format_count,
  1237.                    bool is_primary)
  1238. {
  1239.         enum drm_plane_type type;
  1240.  
  1241.         type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
  1242.         return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
  1243.                                         formats, format_count, type);
  1244. }
  1245. EXPORT_SYMBOL(drm_plane_init);
  1246.  
  1247. /**
  1248.  * drm_plane_cleanup - Clean up the core plane usage
  1249.  * @plane: plane to cleanup
  1250.  *
  1251.  * This function cleans up @plane and removes it from the DRM mode setting
  1252.  * core. Note that the function does *not* free the plane structure itself,
  1253.  * this is the responsibility of the caller.
  1254.  */
  1255. void drm_plane_cleanup(struct drm_plane *plane)
  1256. {
  1257.         struct drm_device *dev = plane->dev;
  1258.  
  1259.         drm_modeset_lock_all(dev);
  1260.         kfree(plane->format_types);
  1261.         drm_mode_object_put(dev, &plane->base);
  1262.  
  1263.         BUG_ON(list_empty(&plane->head));
  1264.  
  1265.         list_del(&plane->head);
  1266.         dev->mode_config.num_total_plane--;
  1267.         if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  1268.                 dev->mode_config.num_overlay_plane--;
  1269.         drm_modeset_unlock_all(dev);
  1270.  
  1271.         WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
  1272.         if (plane->state && plane->funcs->atomic_destroy_state)
  1273.                 plane->funcs->atomic_destroy_state(plane, plane->state);
  1274.  
  1275.         memset(plane, 0, sizeof(*plane));
  1276. }
  1277. EXPORT_SYMBOL(drm_plane_cleanup);
  1278.  
  1279. /**
  1280.  * drm_plane_index - find the index of a registered plane
  1281.  * @plane: plane to find index for
  1282.  *
  1283.  * Given a registered plane, return the index of that CRTC within a DRM
  1284.  * device's list of planes.
  1285.  */
  1286. unsigned int drm_plane_index(struct drm_plane *plane)
  1287. {
  1288.         unsigned int index = 0;
  1289.         struct drm_plane *tmp;
  1290.  
  1291.         drm_for_each_plane(tmp, plane->dev) {
  1292.                 if (tmp == plane)
  1293.                         return index;
  1294.  
  1295.                 index++;
  1296.         }
  1297.  
  1298.         BUG();
  1299. }
  1300. EXPORT_SYMBOL(drm_plane_index);
  1301.  
  1302. /**
  1303.  * drm_plane_from_index - find the registered plane at an index
  1304.  * @dev: DRM device
  1305.  * @idx: index of registered plane to find for
  1306.  *
  1307.  * Given a plane index, return the registered plane from DRM device's
  1308.  * list of planes with matching index.
  1309.  */
  1310. struct drm_plane *
  1311. drm_plane_from_index(struct drm_device *dev, int idx)
  1312. {
  1313.         struct drm_plane *plane;
  1314.         unsigned int i = 0;
  1315.  
  1316.         drm_for_each_plane(plane, dev) {
  1317.                 if (i == idx)
  1318.                         return plane;
  1319.                 i++;
  1320.         }
  1321.         return NULL;
  1322. }
  1323. EXPORT_SYMBOL(drm_plane_from_index);
  1324.  
  1325. /**
  1326.  * drm_plane_force_disable - Forcibly disable a plane
  1327.  * @plane: plane to disable
  1328.  *
  1329.  * Forces the plane to be disabled.
  1330.  *
  1331.  * Used when the plane's current framebuffer is destroyed,
  1332.  * and when restoring fbdev mode.
  1333.  */
  1334. void drm_plane_force_disable(struct drm_plane *plane)
  1335. {
  1336.         int ret;
  1337.  
  1338.         if (!plane->fb)
  1339.                 return;
  1340.  
  1341.         plane->old_fb = plane->fb;
  1342.         ret = plane->funcs->disable_plane(plane);
  1343.         if (ret) {
  1344.                 DRM_ERROR("failed to disable plane with busy fb\n");
  1345.                 plane->old_fb = NULL;
  1346.                 return;
  1347.         }
  1348.         /* disconnect the plane from the fb and crtc: */
  1349.         drm_framebuffer_unreference(plane->old_fb);
  1350.         plane->old_fb = NULL;
  1351.         plane->fb = NULL;
  1352.         plane->crtc = NULL;
  1353. }
  1354. EXPORT_SYMBOL(drm_plane_force_disable);
  1355.  
  1356. static int drm_mode_create_standard_properties(struct drm_device *dev)
  1357. {
  1358.         struct drm_property *prop;
  1359.  
  1360.         /*
  1361.          * Standard properties (apply to all connectors)
  1362.          */
  1363.         prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
  1364.                                    DRM_MODE_PROP_IMMUTABLE,
  1365.                                    "EDID", 0);
  1366.         if (!prop)
  1367.                 return -ENOMEM;
  1368.         dev->mode_config.edid_property = prop;
  1369.  
  1370.         prop = drm_property_create_enum(dev, 0,
  1371.                                    "DPMS", drm_dpms_enum_list,
  1372.                                    ARRAY_SIZE(drm_dpms_enum_list));
  1373.         if (!prop)
  1374.                 return -ENOMEM;
  1375.         dev->mode_config.dpms_property = prop;
  1376.  
  1377.         prop = drm_property_create(dev,
  1378.                                    DRM_MODE_PROP_BLOB |
  1379.                                    DRM_MODE_PROP_IMMUTABLE,
  1380.                                    "PATH", 0);
  1381.         if (!prop)
  1382.                 return -ENOMEM;
  1383.         dev->mode_config.path_property = prop;
  1384.  
  1385.         prop = drm_property_create(dev,
  1386.                                    DRM_MODE_PROP_BLOB |
  1387.                                    DRM_MODE_PROP_IMMUTABLE,
  1388.                                    "TILE", 0);
  1389.         if (!prop)
  1390.                 return -ENOMEM;
  1391.         dev->mode_config.tile_property = prop;
  1392.  
  1393.         prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1394.                                         "type", drm_plane_type_enum_list,
  1395.                                         ARRAY_SIZE(drm_plane_type_enum_list));
  1396.         if (!prop)
  1397.                 return -ENOMEM;
  1398.         dev->mode_config.plane_type_property = prop;
  1399.  
  1400.         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1401.                         "SRC_X", 0, UINT_MAX);
  1402.         if (!prop)
  1403.                 return -ENOMEM;
  1404.         dev->mode_config.prop_src_x = prop;
  1405.  
  1406.         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1407.                         "SRC_Y", 0, UINT_MAX);
  1408.         if (!prop)
  1409.                 return -ENOMEM;
  1410.         dev->mode_config.prop_src_y = prop;
  1411.  
  1412.         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1413.                         "SRC_W", 0, UINT_MAX);
  1414.         if (!prop)
  1415.                 return -ENOMEM;
  1416.         dev->mode_config.prop_src_w = prop;
  1417.  
  1418.         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1419.                         "SRC_H", 0, UINT_MAX);
  1420.         if (!prop)
  1421.                 return -ENOMEM;
  1422.         dev->mode_config.prop_src_h = prop;
  1423.  
  1424.         prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
  1425.                         "CRTC_X", INT_MIN, INT_MAX);
  1426.         if (!prop)
  1427.                 return -ENOMEM;
  1428.         dev->mode_config.prop_crtc_x = prop;
  1429.  
  1430.         prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
  1431.                         "CRTC_Y", INT_MIN, INT_MAX);
  1432.         if (!prop)
  1433.                 return -ENOMEM;
  1434.         dev->mode_config.prop_crtc_y = prop;
  1435.  
  1436.         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1437.                         "CRTC_W", 0, INT_MAX);
  1438.         if (!prop)
  1439.                 return -ENOMEM;
  1440.         dev->mode_config.prop_crtc_w = prop;
  1441.  
  1442.         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1443.                         "CRTC_H", 0, INT_MAX);
  1444.         if (!prop)
  1445.                 return -ENOMEM;
  1446.         dev->mode_config.prop_crtc_h = prop;
  1447.  
  1448.         prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
  1449.                         "FB_ID", DRM_MODE_OBJECT_FB);
  1450.         if (!prop)
  1451.                 return -ENOMEM;
  1452.         dev->mode_config.prop_fb_id = prop;
  1453.  
  1454.         prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
  1455.                         "CRTC_ID", DRM_MODE_OBJECT_CRTC);
  1456.         if (!prop)
  1457.                 return -ENOMEM;
  1458.         dev->mode_config.prop_crtc_id = prop;
  1459.  
  1460.         prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
  1461.                         "ACTIVE");
  1462.         if (!prop)
  1463.                 return -ENOMEM;
  1464.         dev->mode_config.prop_active = prop;
  1465.  
  1466.         prop = drm_property_create(dev,
  1467.                         DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
  1468.                         "MODE_ID", 0);
  1469.         if (!prop)
  1470.                 return -ENOMEM;
  1471.         dev->mode_config.prop_mode_id = prop;
  1472.  
  1473.         return 0;
  1474. }
  1475.  
  1476. /**
  1477.  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
  1478.  * @dev: DRM device
  1479.  *
  1480.  * Called by a driver the first time a DVI-I connector is made.
  1481.  */
  1482. int drm_mode_create_dvi_i_properties(struct drm_device *dev)
  1483. {
  1484.         struct drm_property *dvi_i_selector;
  1485.         struct drm_property *dvi_i_subconnector;
  1486.  
  1487.         if (dev->mode_config.dvi_i_select_subconnector_property)
  1488.                 return 0;
  1489.  
  1490.         dvi_i_selector =
  1491.                 drm_property_create_enum(dev, 0,
  1492.                                     "select subconnector",
  1493.                                     drm_dvi_i_select_enum_list,
  1494.                                     ARRAY_SIZE(drm_dvi_i_select_enum_list));
  1495.         dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
  1496.  
  1497.         dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1498.                                     "subconnector",
  1499.                                     drm_dvi_i_subconnector_enum_list,
  1500.                                     ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
  1501.         dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
  1502.  
  1503.         return 0;
  1504. }
  1505. EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
  1506.  
  1507. /**
  1508.  * drm_create_tv_properties - create TV specific connector properties
  1509.  * @dev: DRM device
  1510.  * @num_modes: number of different TV formats (modes) supported
  1511.  * @modes: array of pointers to strings containing name of each format
  1512.  *
  1513.  * Called by a driver's TV initialization routine, this function creates
  1514.  * the TV specific connector properties for a given device.  Caller is
  1515.  * responsible for allocating a list of format names and passing them to
  1516.  * this routine.
  1517.  */
  1518. int drm_mode_create_tv_properties(struct drm_device *dev,
  1519.                                   unsigned int num_modes,
  1520.                                   const char * const modes[])
  1521. {
  1522.         struct drm_property *tv_selector;
  1523.         struct drm_property *tv_subconnector;
  1524.         unsigned int i;
  1525.  
  1526.         if (dev->mode_config.tv_select_subconnector_property)
  1527.                 return 0;
  1528.  
  1529.         /*
  1530.          * Basic connector properties
  1531.          */
  1532.         tv_selector = drm_property_create_enum(dev, 0,
  1533.                                           "select subconnector",
  1534.                                           drm_tv_select_enum_list,
  1535.                                           ARRAY_SIZE(drm_tv_select_enum_list));
  1536.         if (!tv_selector)
  1537.                 goto nomem;
  1538.  
  1539.         dev->mode_config.tv_select_subconnector_property = tv_selector;
  1540.  
  1541.         tv_subconnector =
  1542.                 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1543.                                     "subconnector",
  1544.                                     drm_tv_subconnector_enum_list,
  1545.                                     ARRAY_SIZE(drm_tv_subconnector_enum_list));
  1546.         if (!tv_subconnector)
  1547.                 goto nomem;
  1548.         dev->mode_config.tv_subconnector_property = tv_subconnector;
  1549.  
  1550.         /*
  1551.          * Other, TV specific properties: margins & TV modes.
  1552.          */
  1553.         dev->mode_config.tv_left_margin_property =
  1554.                 drm_property_create_range(dev, 0, "left margin", 0, 100);
  1555.         if (!dev->mode_config.tv_left_margin_property)
  1556.                 goto nomem;
  1557.  
  1558.         dev->mode_config.tv_right_margin_property =
  1559.                 drm_property_create_range(dev, 0, "right margin", 0, 100);
  1560.         if (!dev->mode_config.tv_right_margin_property)
  1561.                 goto nomem;
  1562.  
  1563.         dev->mode_config.tv_top_margin_property =
  1564.                 drm_property_create_range(dev, 0, "top margin", 0, 100);
  1565.         if (!dev->mode_config.tv_top_margin_property)
  1566.                 goto nomem;
  1567.  
  1568.         dev->mode_config.tv_bottom_margin_property =
  1569.                 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
  1570.         if (!dev->mode_config.tv_bottom_margin_property)
  1571.                 goto nomem;
  1572.  
  1573.         dev->mode_config.tv_mode_property =
  1574.                 drm_property_create(dev, DRM_MODE_PROP_ENUM,
  1575.                                     "mode", num_modes);
  1576.         if (!dev->mode_config.tv_mode_property)
  1577.                 goto nomem;
  1578.  
  1579.         for (i = 0; i < num_modes; i++)
  1580.                 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
  1581.                                       i, modes[i]);
  1582.  
  1583.         dev->mode_config.tv_brightness_property =
  1584.                 drm_property_create_range(dev, 0, "brightness", 0, 100);
  1585.         if (!dev->mode_config.tv_brightness_property)
  1586.                 goto nomem;
  1587.  
  1588.         dev->mode_config.tv_contrast_property =
  1589.                 drm_property_create_range(dev, 0, "contrast", 0, 100);
  1590.         if (!dev->mode_config.tv_contrast_property)
  1591.                 goto nomem;
  1592.  
  1593.         dev->mode_config.tv_flicker_reduction_property =
  1594.                 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
  1595.         if (!dev->mode_config.tv_flicker_reduction_property)
  1596.                 goto nomem;
  1597.  
  1598.         dev->mode_config.tv_overscan_property =
  1599.                 drm_property_create_range(dev, 0, "overscan", 0, 100);
  1600.         if (!dev->mode_config.tv_overscan_property)
  1601.                 goto nomem;
  1602.  
  1603.         dev->mode_config.tv_saturation_property =
  1604.                 drm_property_create_range(dev, 0, "saturation", 0, 100);
  1605.         if (!dev->mode_config.tv_saturation_property)
  1606.                 goto nomem;
  1607.  
  1608.         dev->mode_config.tv_hue_property =
  1609.                 drm_property_create_range(dev, 0, "hue", 0, 100);
  1610.         if (!dev->mode_config.tv_hue_property)
  1611.                 goto nomem;
  1612.  
  1613.         return 0;
  1614. nomem:
  1615.         return -ENOMEM;
  1616. }
  1617. EXPORT_SYMBOL(drm_mode_create_tv_properties);
  1618.  
  1619. /**
  1620.  * drm_mode_create_scaling_mode_property - create scaling mode property
  1621.  * @dev: DRM device
  1622.  *
  1623.  * Called by a driver the first time it's needed, must be attached to desired
  1624.  * connectors.
  1625.  */
  1626. int drm_mode_create_scaling_mode_property(struct drm_device *dev)
  1627. {
  1628.         struct drm_property *scaling_mode;
  1629.  
  1630.         if (dev->mode_config.scaling_mode_property)
  1631.                 return 0;
  1632.  
  1633.         scaling_mode =
  1634.                 drm_property_create_enum(dev, 0, "scaling mode",
  1635.                                 drm_scaling_mode_enum_list,
  1636.                                     ARRAY_SIZE(drm_scaling_mode_enum_list));
  1637.  
  1638.         dev->mode_config.scaling_mode_property = scaling_mode;
  1639.  
  1640.         return 0;
  1641. }
  1642. EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
  1643.  
  1644. /**
  1645.  * drm_mode_create_aspect_ratio_property - create aspect ratio property
  1646.  * @dev: DRM device
  1647.  *
  1648.  * Called by a driver the first time it's needed, must be attached to desired
  1649.  * connectors.
  1650.  *
  1651.  * Returns:
  1652.  * Zero on success, negative errno on failure.
  1653.  */
  1654. int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
  1655. {
  1656.         if (dev->mode_config.aspect_ratio_property)
  1657.                 return 0;
  1658.  
  1659.         dev->mode_config.aspect_ratio_property =
  1660.                 drm_property_create_enum(dev, 0, "aspect ratio",
  1661.                                 drm_aspect_ratio_enum_list,
  1662.                                 ARRAY_SIZE(drm_aspect_ratio_enum_list));
  1663.  
  1664.         if (dev->mode_config.aspect_ratio_property == NULL)
  1665.                 return -ENOMEM;
  1666.  
  1667.         return 0;
  1668. }
  1669. EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
  1670.  
  1671. /**
  1672.  * drm_mode_create_dirty_property - create dirty property
  1673.  * @dev: DRM device
  1674.  *
  1675.  * Called by a driver the first time it's needed, must be attached to desired
  1676.  * connectors.
  1677.  */
  1678. int drm_mode_create_dirty_info_property(struct drm_device *dev)
  1679. {
  1680.         struct drm_property *dirty_info;
  1681.  
  1682.         if (dev->mode_config.dirty_info_property)
  1683.                 return 0;
  1684.  
  1685.         dirty_info =
  1686.                 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1687.                                     "dirty",
  1688.                                     drm_dirty_info_enum_list,
  1689.                                     ARRAY_SIZE(drm_dirty_info_enum_list));
  1690.         dev->mode_config.dirty_info_property = dirty_info;
  1691.  
  1692.         return 0;
  1693. }
  1694. EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
  1695.  
  1696. /**
  1697.  * drm_mode_create_suggested_offset_properties - create suggests offset properties
  1698.  * @dev: DRM device
  1699.  *
  1700.  * Create the the suggested x/y offset property for connectors.
  1701.  */
  1702. int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
  1703. {
  1704.         if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
  1705.                 return 0;
  1706.  
  1707.         dev->mode_config.suggested_x_property =
  1708.                 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
  1709.  
  1710.         dev->mode_config.suggested_y_property =
  1711.                 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
  1712.  
  1713.         if (dev->mode_config.suggested_x_property == NULL ||
  1714.             dev->mode_config.suggested_y_property == NULL)
  1715.                 return -ENOMEM;
  1716.         return 0;
  1717. }
  1718. EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
  1719.  
  1720. #if 0
  1721. /**
  1722.  * drm_mode_getresources - get graphics configuration
  1723.  * @dev: drm device for the ioctl
  1724.  * @data: data pointer for the ioctl
  1725.  * @file_priv: drm file for the ioctl call
  1726.  *
  1727.  * Construct a set of configuration description structures and return
  1728.  * them to the user, including CRTC, connector and framebuffer configuration.
  1729.  *
  1730.  * Called by the user via ioctl.
  1731.  *
  1732.  * Returns:
  1733.  * Zero on success, negative errno on failure.
  1734.  */
  1735. int drm_mode_getresources(struct drm_device *dev, void *data,
  1736.                           struct drm_file *file_priv)
  1737. {
  1738.         struct drm_mode_card_res *card_res = data;
  1739.         struct list_head *lh;
  1740.         struct drm_framebuffer *fb;
  1741.         struct drm_connector *connector;
  1742.         struct drm_crtc *crtc;
  1743.         struct drm_encoder *encoder;
  1744.         int ret = 0;
  1745.         int connector_count = 0;
  1746.         int crtc_count = 0;
  1747.         int fb_count = 0;
  1748.         int encoder_count = 0;
  1749.         int copied = 0;
  1750.         uint32_t __user *fb_id;
  1751.         uint32_t __user *crtc_id;
  1752.         uint32_t __user *connector_id;
  1753.         uint32_t __user *encoder_id;
  1754.  
  1755.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1756.                 return -EINVAL;
  1757.  
  1758.  
  1759.         mutex_lock(&file_priv->fbs_lock);
  1760.         /*
  1761.          * For the non-control nodes we need to limit the list of resources
  1762.          * by IDs in the group list for this node
  1763.          */
  1764.         list_for_each(lh, &file_priv->fbs)
  1765.                 fb_count++;
  1766.  
  1767.         /* handle this in 4 parts */
  1768.         /* FBs */
  1769.         if (card_res->count_fbs >= fb_count) {
  1770.                 copied = 0;
  1771.                 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
  1772.                 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
  1773.                         if (put_user(fb->base.id, fb_id + copied)) {
  1774.                                 mutex_unlock(&file_priv->fbs_lock);
  1775.                                 return -EFAULT;
  1776.                         }
  1777.                         copied++;
  1778.                 }
  1779.         }
  1780.         card_res->count_fbs = fb_count;
  1781.         mutex_unlock(&file_priv->fbs_lock);
  1782.  
  1783.         /* mode_config.mutex protects the connector list against e.g. DP MST
  1784.          * connector hot-adding. CRTC/Plane lists are invariant. */
  1785.         mutex_lock(&dev->mode_config.mutex);
  1786.         drm_for_each_crtc(crtc, dev)
  1787.                 crtc_count++;
  1788.  
  1789.         drm_for_each_connector(connector, dev)
  1790.                 connector_count++;
  1791.  
  1792.         drm_for_each_encoder(encoder, dev)
  1793.                 encoder_count++;
  1794.  
  1795.         card_res->max_height = dev->mode_config.max_height;
  1796.         card_res->min_height = dev->mode_config.min_height;
  1797.         card_res->max_width = dev->mode_config.max_width;
  1798.         card_res->min_width = dev->mode_config.min_width;
  1799.  
  1800.         /* CRTCs */
  1801.         if (card_res->count_crtcs >= crtc_count) {
  1802.                 copied = 0;
  1803.                 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
  1804.                 drm_for_each_crtc(crtc, dev) {
  1805.                         DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  1806.                         if (put_user(crtc->base.id, crtc_id + copied)) {
  1807.                                 ret = -EFAULT;
  1808.                                 goto out;
  1809.                         }
  1810.                         copied++;
  1811.                 }
  1812.         }
  1813.         card_res->count_crtcs = crtc_count;
  1814.  
  1815.         /* Encoders */
  1816.         if (card_res->count_encoders >= encoder_count) {
  1817.                 copied = 0;
  1818.                 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
  1819.                 drm_for_each_encoder(encoder, dev) {
  1820.                         DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
  1821.                                         encoder->name);
  1822.                         if (put_user(encoder->base.id, encoder_id +
  1823.                                      copied)) {
  1824.                                 ret = -EFAULT;
  1825.                                 goto out;
  1826.                         }
  1827.                         copied++;
  1828.                 }
  1829.         }
  1830.         card_res->count_encoders = encoder_count;
  1831.  
  1832.         /* Connectors */
  1833.         if (card_res->count_connectors >= connector_count) {
  1834.                 copied = 0;
  1835.                 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
  1836.                 drm_for_each_connector(connector, dev) {
  1837.                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1838.                                 connector->base.id,
  1839.                                 connector->name);
  1840.                         if (put_user(connector->base.id,
  1841.                                      connector_id + copied)) {
  1842.                                 ret = -EFAULT;
  1843.                                 goto out;
  1844.                         }
  1845.                         copied++;
  1846.                 }
  1847.         }
  1848.         card_res->count_connectors = connector_count;
  1849.  
  1850.         DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
  1851.                   card_res->count_connectors, card_res->count_encoders);
  1852.  
  1853. out:
  1854.         mutex_unlock(&dev->mode_config.mutex);
  1855.         return ret;
  1856. }
  1857.  
  1858. /**
  1859.  * drm_mode_getcrtc - get CRTC configuration
  1860.  * @dev: drm device for the ioctl
  1861.  * @data: data pointer for the ioctl
  1862.  * @file_priv: drm file for the ioctl call
  1863.  *
  1864.  * Construct a CRTC configuration structure to return to the user.
  1865.  *
  1866.  * Called by the user via ioctl.
  1867.  *
  1868.  * Returns:
  1869.  * Zero on success, negative errno on failure.
  1870.  */
  1871. int drm_mode_getcrtc(struct drm_device *dev,
  1872.                      void *data, struct drm_file *file_priv)
  1873. {
  1874.         struct drm_mode_crtc *crtc_resp = data;
  1875.         struct drm_crtc *crtc;
  1876.  
  1877.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1878.                 return -EINVAL;
  1879.  
  1880.         crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
  1881.         if (!crtc)
  1882.                 return -ENOENT;
  1883.  
  1884.         drm_modeset_lock_crtc(crtc, crtc->primary);
  1885.         crtc_resp->gamma_size = crtc->gamma_size;
  1886.         if (crtc->primary->fb)
  1887.                 crtc_resp->fb_id = crtc->primary->fb->base.id;
  1888.         else
  1889.                 crtc_resp->fb_id = 0;
  1890.  
  1891.         if (crtc->state) {
  1892.                 crtc_resp->x = crtc->primary->state->src_x >> 16;
  1893.                 crtc_resp->y = crtc->primary->state->src_y >> 16;
  1894.                 if (crtc->state->enable) {
  1895.                         drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
  1896.                         crtc_resp->mode_valid = 1;
  1897.  
  1898.                 } else {
  1899.                         crtc_resp->mode_valid = 0;
  1900.                 }
  1901.         } else {
  1902.                 crtc_resp->x = crtc->x;
  1903.                 crtc_resp->y = crtc->y;
  1904.                 if (crtc->enabled) {
  1905.                         drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
  1906.                         crtc_resp->mode_valid = 1;
  1907.  
  1908.                 } else {
  1909.                         crtc_resp->mode_valid = 0;
  1910.                 }
  1911.         }
  1912.         drm_modeset_unlock_crtc(crtc);
  1913.  
  1914.         return 0;
  1915. }
  1916.  
  1917. static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
  1918.                                          const struct drm_file *file_priv)
  1919. {
  1920.         /*
  1921.          * If user-space hasn't configured the driver to expose the stereo 3D
  1922.          * modes, don't expose them.
  1923.          */
  1924.         if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
  1925.                 return false;
  1926.  
  1927.         return true;
  1928. }
  1929.  
  1930. static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
  1931. {
  1932.         /* For atomic drivers only state objects are synchronously updated and
  1933.          * protected by modeset locks, so check those first. */
  1934.         if (connector->state)
  1935.                 return connector->state->best_encoder;
  1936.         return connector->encoder;
  1937. }
  1938.  
  1939. /* helper for getconnector and getproperties ioctls */
  1940. static int get_properties(struct drm_mode_object *obj, bool atomic,
  1941.                 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
  1942.                 uint32_t *arg_count_props)
  1943. {
  1944.         int props_count;
  1945.         int i, ret, copied;
  1946.  
  1947.         props_count = obj->properties->count;
  1948.         if (!atomic)
  1949.                 props_count -= obj->properties->atomic_count;
  1950.  
  1951.         if ((*arg_count_props >= props_count) && props_count) {
  1952.                 for (i = 0, copied = 0; copied < props_count; i++) {
  1953.                         struct drm_property *prop = obj->properties->properties[i];
  1954.                         uint64_t val;
  1955.  
  1956.                         if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
  1957.                                 continue;
  1958.  
  1959.                         ret = drm_object_property_get_value(obj, prop, &val);
  1960.                         if (ret)
  1961.                                 return ret;
  1962.  
  1963.                         if (put_user(prop->base.id, prop_ptr + copied))
  1964.                                 return -EFAULT;
  1965.  
  1966.                         if (put_user(val, prop_values + copied))
  1967.                                 return -EFAULT;
  1968.  
  1969.                         copied++;
  1970.                 }
  1971.         }
  1972.         *arg_count_props = props_count;
  1973.  
  1974.         return 0;
  1975. }
  1976.  
  1977. /**
  1978.  * drm_mode_getconnector - get connector configuration
  1979.  * @dev: drm device for the ioctl
  1980.  * @data: data pointer for the ioctl
  1981.  * @file_priv: drm file for the ioctl call
  1982.  *
  1983.  * Construct a connector configuration structure to return to the user.
  1984.  *
  1985.  * Called by the user via ioctl.
  1986.  *
  1987.  * Returns:
  1988.  * Zero on success, negative errno on failure.
  1989.  */
  1990. int drm_mode_getconnector(struct drm_device *dev, void *data,
  1991.                           struct drm_file *file_priv)
  1992. {
  1993.         struct drm_mode_get_connector *out_resp = data;
  1994.         struct drm_connector *connector;
  1995.         struct drm_encoder *encoder;
  1996.         struct drm_display_mode *mode;
  1997.         int mode_count = 0;
  1998.         int encoders_count = 0;
  1999.         int ret = 0;
  2000.         int copied = 0;
  2001.         int i;
  2002.         struct drm_mode_modeinfo u_mode;
  2003.         struct drm_mode_modeinfo __user *mode_ptr;
  2004.         uint32_t __user *encoder_ptr;
  2005.  
  2006.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2007.                 return -EINVAL;
  2008.  
  2009.         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  2010.  
  2011.         DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
  2012.  
  2013.         mutex_lock(&dev->mode_config.mutex);
  2014.  
  2015.         connector = drm_connector_find(dev, out_resp->connector_id);
  2016.         if (!connector) {
  2017.                 ret = -ENOENT;
  2018.                 goto out_unlock;
  2019.         }
  2020.  
  2021.         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
  2022.                 if (connector->encoder_ids[i] != 0)
  2023.                         encoders_count++;
  2024.  
  2025.         if (out_resp->count_modes == 0) {
  2026.                 connector->funcs->fill_modes(connector,
  2027.                                              dev->mode_config.max_width,
  2028.                                              dev->mode_config.max_height);
  2029.         }
  2030.  
  2031.         /* delayed so we get modes regardless of pre-fill_modes state */
  2032.         list_for_each_entry(mode, &connector->modes, head)
  2033.                 if (drm_mode_expose_to_userspace(mode, file_priv))
  2034.                         mode_count++;
  2035.  
  2036.         out_resp->connector_id = connector->base.id;
  2037.         out_resp->connector_type = connector->connector_type;
  2038.         out_resp->connector_type_id = connector->connector_type_id;
  2039.         out_resp->mm_width = connector->display_info.width_mm;
  2040.         out_resp->mm_height = connector->display_info.height_mm;
  2041.         out_resp->subpixel = connector->display_info.subpixel_order;
  2042.         out_resp->connection = connector->status;
  2043.  
  2044.         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  2045.         encoder = drm_connector_get_encoder(connector);
  2046.         if (encoder)
  2047.                 out_resp->encoder_id = encoder->base.id;
  2048.         else
  2049.                 out_resp->encoder_id = 0;
  2050.  
  2051.         /*
  2052.          * This ioctl is called twice, once to determine how much space is
  2053.          * needed, and the 2nd time to fill it.
  2054.          */
  2055.         if ((out_resp->count_modes >= mode_count) && mode_count) {
  2056.                 copied = 0;
  2057.                 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
  2058.                 list_for_each_entry(mode, &connector->modes, head) {
  2059.                         if (!drm_mode_expose_to_userspace(mode, file_priv))
  2060.                                 continue;
  2061.  
  2062.                         drm_mode_convert_to_umode(&u_mode, mode);
  2063.                         if (copy_to_user(mode_ptr + copied,
  2064.                                          &u_mode, sizeof(u_mode))) {
  2065.                                 ret = -EFAULT;
  2066.                                 goto out;
  2067.                         }
  2068.                         copied++;
  2069.                 }
  2070.         }
  2071.         out_resp->count_modes = mode_count;
  2072.  
  2073.         ret = get_properties(&connector->base, file_priv->atomic,
  2074.                         (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
  2075.                         (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
  2076.                         &out_resp->count_props);
  2077.         if (ret)
  2078.                 goto out;
  2079.  
  2080.         if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  2081.                 copied = 0;
  2082.                 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
  2083.                 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  2084.                         if (connector->encoder_ids[i] != 0) {
  2085.                                 if (put_user(connector->encoder_ids[i],
  2086.                                              encoder_ptr + copied)) {
  2087.                                         ret = -EFAULT;
  2088.                                         goto out;
  2089.                                 }
  2090.                                 copied++;
  2091.                         }
  2092.                 }
  2093.         }
  2094.         out_resp->count_encoders = encoders_count;
  2095.  
  2096. out:
  2097.         drm_modeset_unlock(&dev->mode_config.connection_mutex);
  2098.  
  2099. out_unlock:
  2100.         mutex_unlock(&dev->mode_config.mutex);
  2101.  
  2102.         return ret;
  2103. }
  2104.  
  2105. static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
  2106. {
  2107.         struct drm_connector *connector;
  2108.         struct drm_device *dev = encoder->dev;
  2109.         bool uses_atomic = false;
  2110.  
  2111.         /* For atomic drivers only state objects are synchronously updated and
  2112.          * protected by modeset locks, so check those first. */
  2113.         drm_for_each_connector(connector, dev) {
  2114.                 if (!connector->state)
  2115.                         continue;
  2116.  
  2117.                 uses_atomic = true;
  2118.  
  2119.                 if (connector->state->best_encoder != encoder)
  2120.                         continue;
  2121.  
  2122.                 return connector->state->crtc;
  2123.         }
  2124.  
  2125.         /* Don't return stale data (e.g. pending async disable). */
  2126.         if (uses_atomic)
  2127.                 return NULL;
  2128.  
  2129.         return encoder->crtc;
  2130. }
  2131.  
  2132. /**
  2133.  * drm_mode_getencoder - get encoder configuration
  2134.  * @dev: drm device for the ioctl
  2135.  * @data: data pointer for the ioctl
  2136.  * @file_priv: drm file for the ioctl call
  2137.  *
  2138.  * Construct a encoder configuration structure to return to the user.
  2139.  *
  2140.  * Called by the user via ioctl.
  2141.  *
  2142.  * Returns:
  2143.  * Zero on success, negative errno on failure.
  2144.  */
  2145. int drm_mode_getencoder(struct drm_device *dev, void *data,
  2146.                         struct drm_file *file_priv)
  2147. {
  2148.         struct drm_mode_get_encoder *enc_resp = data;
  2149.         struct drm_encoder *encoder;
  2150.         struct drm_crtc *crtc;
  2151.  
  2152.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2153.                 return -EINVAL;
  2154.  
  2155.         encoder = drm_encoder_find(dev, enc_resp->encoder_id);
  2156.         if (!encoder)
  2157.                 return -ENOENT;
  2158.  
  2159.         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  2160.         crtc = drm_encoder_get_crtc(encoder);
  2161.         if (crtc)
  2162.                 enc_resp->crtc_id = crtc->base.id;
  2163.         else
  2164.                 enc_resp->crtc_id = 0;
  2165.         drm_modeset_unlock(&dev->mode_config.connection_mutex);
  2166.  
  2167.         enc_resp->encoder_type = encoder->encoder_type;
  2168.         enc_resp->encoder_id = encoder->base.id;
  2169.         enc_resp->possible_crtcs = encoder->possible_crtcs;
  2170.         enc_resp->possible_clones = encoder->possible_clones;
  2171.  
  2172.         return 0;
  2173. }
  2174.  
  2175. /**
  2176.  * drm_mode_getplane_res - enumerate all plane resources
  2177.  * @dev: DRM device
  2178.  * @data: ioctl data
  2179.  * @file_priv: DRM file info
  2180.  *
  2181.  * Construct a list of plane ids to return to the user.
  2182.  *
  2183.  * Called by the user via ioctl.
  2184.  *
  2185.  * Returns:
  2186.  * Zero on success, negative errno on failure.
  2187.  */
  2188. int drm_mode_getplane_res(struct drm_device *dev, void *data,
  2189.                           struct drm_file *file_priv)
  2190. {
  2191.         struct drm_mode_get_plane_res *plane_resp = data;
  2192.         struct drm_mode_config *config;
  2193.         struct drm_plane *plane;
  2194.         uint32_t __user *plane_ptr;
  2195.         int copied = 0;
  2196.         unsigned num_planes;
  2197.  
  2198.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2199.                 return -EINVAL;
  2200.  
  2201.         config = &dev->mode_config;
  2202.  
  2203.         if (file_priv->universal_planes)
  2204.                 num_planes = config->num_total_plane;
  2205.         else
  2206.                 num_planes = config->num_overlay_plane;
  2207.  
  2208.         /*
  2209.          * This ioctl is called twice, once to determine how much space is
  2210.          * needed, and the 2nd time to fill it.
  2211.          */
  2212.         if (num_planes &&
  2213.             (plane_resp->count_planes >= num_planes)) {
  2214.                 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
  2215.  
  2216.                 /* Plane lists are invariant, no locking needed. */
  2217.                 drm_for_each_plane(plane, dev) {
  2218.                         /*
  2219.                          * Unless userspace set the 'universal planes'
  2220.                          * capability bit, only advertise overlays.
  2221.                          */
  2222.                         if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
  2223.                             !file_priv->universal_planes)
  2224.                                 continue;
  2225.  
  2226.                         if (put_user(plane->base.id, plane_ptr + copied))
  2227.                                 return -EFAULT;
  2228.                         copied++;
  2229.                 }
  2230.         }
  2231.         plane_resp->count_planes = num_planes;
  2232.  
  2233.         return 0;
  2234. }
  2235.  
  2236. /**
  2237.  * drm_mode_getplane - get plane configuration
  2238.  * @dev: DRM device
  2239.  * @data: ioctl data
  2240.  * @file_priv: DRM file info
  2241.  *
  2242.  * Construct a plane configuration structure to return to the user.
  2243.  *
  2244.  * Called by the user via ioctl.
  2245.  *
  2246.  * Returns:
  2247.  * Zero on success, negative errno on failure.
  2248.  */
  2249. int drm_mode_getplane(struct drm_device *dev, void *data,
  2250.                       struct drm_file *file_priv)
  2251. {
  2252.         struct drm_mode_get_plane *plane_resp = data;
  2253.         struct drm_plane *plane;
  2254.         uint32_t __user *format_ptr;
  2255.  
  2256.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2257.                 return -EINVAL;
  2258.  
  2259.         plane = drm_plane_find(dev, plane_resp->plane_id);
  2260.         if (!plane)
  2261.                 return -ENOENT;
  2262.  
  2263.         drm_modeset_lock(&plane->mutex, NULL);
  2264.         if (plane->crtc)
  2265.                 plane_resp->crtc_id = plane->crtc->base.id;
  2266.         else
  2267.                 plane_resp->crtc_id = 0;
  2268.  
  2269.         if (plane->fb)
  2270.                 plane_resp->fb_id = plane->fb->base.id;
  2271.         else
  2272.                 plane_resp->fb_id = 0;
  2273.         drm_modeset_unlock(&plane->mutex);
  2274.  
  2275.         plane_resp->plane_id = plane->base.id;
  2276.         plane_resp->possible_crtcs = plane->possible_crtcs;
  2277.         plane_resp->gamma_size = 0;
  2278.  
  2279.         /*
  2280.          * This ioctl is called twice, once to determine how much space is
  2281.          * needed, and the 2nd time to fill it.
  2282.          */
  2283.         if (plane->format_count &&
  2284.             (plane_resp->count_format_types >= plane->format_count)) {
  2285.                 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
  2286.                 if (copy_to_user(format_ptr,
  2287.                                  plane->format_types,
  2288.                                  sizeof(uint32_t) * plane->format_count)) {
  2289.                         return -EFAULT;
  2290.                 }
  2291.         }
  2292.         plane_resp->count_format_types = plane->format_count;
  2293.  
  2294.         return 0;
  2295. }
  2296. #endif
  2297.  
  2298. /**
  2299.  * drm_plane_check_pixel_format - Check if the plane supports the pixel format
  2300.  * @plane: plane to check for format support
  2301.  * @format: the pixel format
  2302.  *
  2303.  * Returns:
  2304.  * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
  2305.  * otherwise.
  2306.  */
  2307. int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
  2308. {
  2309.         unsigned int i;
  2310.  
  2311.         for (i = 0; i < plane->format_count; i++) {
  2312.                 if (format == plane->format_types[i])
  2313.                         return 0;
  2314.         }
  2315.  
  2316.         return -EINVAL;
  2317. }
  2318.  
  2319. static int check_src_coords(uint32_t src_x, uint32_t src_y,
  2320.                             uint32_t src_w, uint32_t src_h,
  2321.                             const struct drm_framebuffer *fb)
  2322. {
  2323.         unsigned int fb_width, fb_height;
  2324.  
  2325.         fb_width = fb->width << 16;
  2326.         fb_height = fb->height << 16;
  2327.  
  2328.         /* Make sure source coordinates are inside the fb. */
  2329.         if (src_w > fb_width ||
  2330.             src_x > fb_width - src_w ||
  2331.             src_h > fb_height ||
  2332.             src_y > fb_height - src_h) {
  2333.                 DRM_DEBUG_KMS("Invalid source coordinates "
  2334.                               "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
  2335.                               src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
  2336.                               src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
  2337.                               src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
  2338.                               src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
  2339.                 return -ENOSPC;
  2340.         }
  2341.  
  2342.         return 0;
  2343. }
  2344.  
  2345. /*
  2346.  * setplane_internal - setplane handler for internal callers
  2347.  *
  2348.  * Note that we assume an extra reference has already been taken on fb.  If the
  2349.  * update fails, this reference will be dropped before return; if it succeeds,
  2350.  * the previous framebuffer (if any) will be unreferenced instead.
  2351.  *
  2352.  * src_{x,y,w,h} are provided in 16.16 fixed point format
  2353.  */
  2354. static int __setplane_internal(struct drm_plane *plane,
  2355.                                struct drm_crtc *crtc,
  2356.                                struct drm_framebuffer *fb,
  2357.                                int32_t crtc_x, int32_t crtc_y,
  2358.                                uint32_t crtc_w, uint32_t crtc_h,
  2359.                                /* src_{x,y,w,h} values are 16.16 fixed point */
  2360.                                uint32_t src_x, uint32_t src_y,
  2361.                                uint32_t src_w, uint32_t src_h)
  2362. {
  2363.         int ret = 0;
  2364.  
  2365.         /* No fb means shut it down */
  2366.         if (!fb) {
  2367.                 plane->old_fb = plane->fb;
  2368.                 ret = plane->funcs->disable_plane(plane);
  2369.                 if (!ret) {
  2370.                         plane->crtc = NULL;
  2371.                         plane->fb = NULL;
  2372.                 } else {
  2373.                         plane->old_fb = NULL;
  2374.                 }
  2375.                 goto out;
  2376.         }
  2377.  
  2378.         /* Check whether this plane is usable on this CRTC */
  2379.         if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
  2380.                 DRM_DEBUG_KMS("Invalid crtc for plane\n");
  2381.                 ret = -EINVAL;
  2382.                 goto out;
  2383.         }
  2384.  
  2385.         /* Check whether this plane supports the fb pixel format. */
  2386.         ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
  2387.         if (ret) {
  2388.                 DRM_DEBUG_KMS("Invalid pixel format %s\n",
  2389.                               drm_get_format_name(fb->pixel_format));
  2390.                 goto out;
  2391.         }
  2392.  
  2393.         /* Give drivers some help against integer overflows */
  2394.         if (crtc_w > INT_MAX ||
  2395.             crtc_x > INT_MAX - (int32_t) crtc_w ||
  2396.             crtc_h > INT_MAX ||
  2397.             crtc_y > INT_MAX - (int32_t) crtc_h) {
  2398.                 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
  2399.                               crtc_w, crtc_h, crtc_x, crtc_y);
  2400.                 ret = -ERANGE;
  2401.                 goto out;
  2402.         }
  2403.  
  2404.         ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
  2405.         if (ret)
  2406.                 goto out;
  2407.  
  2408.         plane->old_fb = plane->fb;
  2409.         ret = plane->funcs->update_plane(plane, crtc, fb,
  2410.                                          crtc_x, crtc_y, crtc_w, crtc_h,
  2411.                                          src_x, src_y, src_w, src_h);
  2412.         if (!ret) {
  2413.                 plane->crtc = crtc;
  2414.                 plane->fb = fb;
  2415.                 fb = NULL;
  2416.         } else {
  2417.                 plane->old_fb = NULL;
  2418.         }
  2419.  
  2420. out:
  2421.         if (fb)
  2422.                 drm_framebuffer_unreference(fb);
  2423.         if (plane->old_fb)
  2424.                 drm_framebuffer_unreference(plane->old_fb);
  2425.         plane->old_fb = NULL;
  2426.  
  2427.         return ret;
  2428. }
  2429.  
  2430. static int setplane_internal(struct drm_plane *plane,
  2431.                              struct drm_crtc *crtc,
  2432.                              struct drm_framebuffer *fb,
  2433.                              int32_t crtc_x, int32_t crtc_y,
  2434.                              uint32_t crtc_w, uint32_t crtc_h,
  2435.                              /* src_{x,y,w,h} values are 16.16 fixed point */
  2436.                              uint32_t src_x, uint32_t src_y,
  2437.                              uint32_t src_w, uint32_t src_h)
  2438. {
  2439.         int ret;
  2440.  
  2441.         drm_modeset_lock_all(plane->dev);
  2442.         ret = __setplane_internal(plane, crtc, fb,
  2443.                                   crtc_x, crtc_y, crtc_w, crtc_h,
  2444.                                   src_x, src_y, src_w, src_h);
  2445.         drm_modeset_unlock_all(plane->dev);
  2446.  
  2447.         return ret;
  2448. }
  2449.  
  2450. /**
  2451.  * drm_mode_setplane - configure a plane's configuration
  2452.  * @dev: DRM device
  2453.  * @data: ioctl data*
  2454.  * @file_priv: DRM file info
  2455.  *
  2456.  * Set plane configuration, including placement, fb, scaling, and other factors.
  2457.  * Or pass a NULL fb to disable (planes may be disabled without providing a
  2458.  * valid crtc).
  2459.  *
  2460.  * Returns:
  2461.  * Zero on success, negative errno on failure.
  2462.  */
  2463. int drm_mode_setplane(struct drm_device *dev, void *data,
  2464.                       struct drm_file *file_priv)
  2465. {
  2466.         struct drm_mode_set_plane *plane_req = data;
  2467.         struct drm_plane *plane;
  2468.         struct drm_crtc *crtc = NULL;
  2469.         struct drm_framebuffer *fb = NULL;
  2470.  
  2471.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2472.                 return -EINVAL;
  2473.  
  2474.         /*
  2475.          * First, find the plane, crtc, and fb objects.  If not available,
  2476.          * we don't bother to call the driver.
  2477.          */
  2478.         plane = drm_plane_find(dev, plane_req->plane_id);
  2479.         if (!plane) {
  2480.                 DRM_DEBUG_KMS("Unknown plane ID %d\n",
  2481.                               plane_req->plane_id);
  2482.                 return -ENOENT;
  2483.         }
  2484.  
  2485.         if (plane_req->fb_id) {
  2486.                 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
  2487.                 if (!fb) {
  2488.                         DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
  2489.                                       plane_req->fb_id);
  2490.                         return -ENOENT;
  2491.                 }
  2492.  
  2493.                 crtc = drm_crtc_find(dev, plane_req->crtc_id);
  2494.                 if (!crtc) {
  2495.                         DRM_DEBUG_KMS("Unknown crtc ID %d\n",
  2496.                                       plane_req->crtc_id);
  2497.                         return -ENOENT;
  2498.                 }
  2499.         }
  2500.  
  2501.         /*
  2502.          * setplane_internal will take care of deref'ing either the old or new
  2503.          * framebuffer depending on success.
  2504.          */
  2505.         return setplane_internal(plane, crtc, fb,
  2506.                                  plane_req->crtc_x, plane_req->crtc_y,
  2507.                                  plane_req->crtc_w, plane_req->crtc_h,
  2508.                                  plane_req->src_x, plane_req->src_y,
  2509.                                  plane_req->src_w, plane_req->src_h);
  2510. }
  2511.  
  2512. /**
  2513.  * drm_mode_set_config_internal - helper to call ->set_config
  2514.  * @set: modeset config to set
  2515.  *
  2516.  * This is a little helper to wrap internal calls to the ->set_config driver
  2517.  * interface. The only thing it adds is correct refcounting dance.
  2518.  *
  2519.  * Returns:
  2520.  * Zero on success, negative errno on failure.
  2521.  */
  2522. int drm_mode_set_config_internal(struct drm_mode_set *set)
  2523. {
  2524.         struct drm_crtc *crtc = set->crtc;
  2525.         struct drm_framebuffer *fb;
  2526.         struct drm_crtc *tmp;
  2527.         int ret;
  2528.  
  2529.         /*
  2530.          * NOTE: ->set_config can also disable other crtcs (if we steal all
  2531.          * connectors from it), hence we need to refcount the fbs across all
  2532.          * crtcs. Atomic modeset will have saner semantics ...
  2533.          */
  2534.         drm_for_each_crtc(tmp, crtc->dev)
  2535.                 tmp->primary->old_fb = tmp->primary->fb;
  2536.  
  2537.         fb = set->fb;
  2538.  
  2539.         ret = crtc->funcs->set_config(set);
  2540.         if (ret == 0) {
  2541.                 crtc->primary->crtc = crtc;
  2542.                 crtc->primary->fb = fb;
  2543.         }
  2544.  
  2545.         drm_for_each_crtc(tmp, crtc->dev) {
  2546.                 if (tmp->primary->fb)
  2547.                         drm_framebuffer_reference(tmp->primary->fb);
  2548.                 if (tmp->primary->old_fb)
  2549.                         drm_framebuffer_unreference(tmp->primary->old_fb);
  2550.                 tmp->primary->old_fb = NULL;
  2551.         }
  2552.  
  2553.         return ret;
  2554. }
  2555. EXPORT_SYMBOL(drm_mode_set_config_internal);
  2556.  
  2557. /**
  2558.  * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
  2559.  * @mode: mode to query
  2560.  * @hdisplay: hdisplay value to fill in
  2561.  * @vdisplay: vdisplay value to fill in
  2562.  *
  2563.  * The vdisplay value will be doubled if the specified mode is a stereo mode of
  2564.  * the appropriate layout.
  2565.  */
  2566. void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
  2567.                             int *hdisplay, int *vdisplay)
  2568. {
  2569.         struct drm_display_mode adjusted;
  2570.  
  2571.         drm_mode_copy(&adjusted, mode);
  2572.         drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
  2573.         *hdisplay = adjusted.crtc_hdisplay;
  2574.         *vdisplay = adjusted.crtc_vdisplay;
  2575. }
  2576. EXPORT_SYMBOL(drm_crtc_get_hv_timing);
  2577.  
  2578. /**
  2579.  * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
  2580.  *     CRTC viewport
  2581.  * @crtc: CRTC that framebuffer will be displayed on
  2582.  * @x: x panning
  2583.  * @y: y panning
  2584.  * @mode: mode that framebuffer will be displayed under
  2585.  * @fb: framebuffer to check size of
  2586.  */
  2587. int drm_crtc_check_viewport(const struct drm_crtc *crtc,
  2588.                             int x, int y,
  2589.                             const struct drm_display_mode *mode,
  2590.                             const struct drm_framebuffer *fb)
  2591.  
  2592. {
  2593.         int hdisplay, vdisplay;
  2594.  
  2595.         drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  2596.  
  2597.         if (crtc->state &&
  2598.             crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
  2599.                                               BIT(DRM_ROTATE_270)))
  2600.                 swap(hdisplay, vdisplay);
  2601.  
  2602.         return check_src_coords(x << 16, y << 16,
  2603.                                 hdisplay << 16, vdisplay << 16, fb);
  2604. }
  2605. EXPORT_SYMBOL(drm_crtc_check_viewport);
  2606.  
  2607. #if 0
  2608. /**
  2609.  * drm_mode_setcrtc - set CRTC configuration
  2610.  * @dev: drm device for the ioctl
  2611.  * @data: data pointer for the ioctl
  2612.  * @file_priv: drm file for the ioctl call
  2613.  *
  2614.  * Build a new CRTC configuration based on user request.
  2615.  *
  2616.  * Called by the user via ioctl.
  2617.  *
  2618.  * Returns:
  2619.  * Zero on success, negative errno on failure.
  2620.  */
  2621. int drm_mode_setcrtc(struct drm_device *dev, void *data,
  2622.                      struct drm_file *file_priv)
  2623. {
  2624.         struct drm_mode_config *config = &dev->mode_config;
  2625.         struct drm_mode_crtc *crtc_req = data;
  2626.         struct drm_crtc *crtc;
  2627.         struct drm_connector **connector_set = NULL, *connector;
  2628.         struct drm_framebuffer *fb = NULL;
  2629.         struct drm_display_mode *mode = NULL;
  2630.         struct drm_mode_set set;
  2631.         uint32_t __user *set_connectors_ptr;
  2632.         int ret;
  2633.         int i;
  2634.  
  2635.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2636.                 return -EINVAL;
  2637.  
  2638.         /*
  2639.          * Universal plane src offsets are only 16.16, prevent havoc for
  2640.          * drivers using universal plane code internally.
  2641.          */
  2642.         if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
  2643.                 return -ERANGE;
  2644.  
  2645.         drm_modeset_lock_all(dev);
  2646.         crtc = drm_crtc_find(dev, crtc_req->crtc_id);
  2647.         if (!crtc) {
  2648.                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
  2649.                 ret = -ENOENT;
  2650.                 goto out;
  2651.         }
  2652.         DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  2653.  
  2654.         if (crtc_req->mode_valid) {
  2655.                 /* If we have a mode we need a framebuffer. */
  2656.                 /* If we pass -1, set the mode with the currently bound fb */
  2657.                 if (crtc_req->fb_id == -1) {
  2658.                         if (!crtc->primary->fb) {
  2659.                                 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
  2660.                                 ret = -EINVAL;
  2661.                                 goto out;
  2662.                         }
  2663.                         fb = crtc->primary->fb;
  2664.                         /* Make refcounting symmetric with the lookup path. */
  2665.                         drm_framebuffer_reference(fb);
  2666.                 } else {
  2667.                         fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
  2668.                         if (!fb) {
  2669.                                 DRM_DEBUG_KMS("Unknown FB ID%d\n",
  2670.                                                 crtc_req->fb_id);
  2671.                                 ret = -ENOENT;
  2672.                                 goto out;
  2673.                         }
  2674.                 }
  2675.  
  2676.                 mode = drm_mode_create(dev);
  2677.                 if (!mode) {
  2678.                         ret = -ENOMEM;
  2679.                         goto out;
  2680.                 }
  2681.  
  2682.                 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
  2683.                 if (ret) {
  2684.                         DRM_DEBUG_KMS("Invalid mode\n");
  2685.                         goto out;
  2686.                 }
  2687.  
  2688.                 /*
  2689.                  * Check whether the primary plane supports the fb pixel format.
  2690.                  * Drivers not implementing the universal planes API use a
  2691.                  * default formats list provided by the DRM core which doesn't
  2692.                  * match real hardware capabilities. Skip the check in that
  2693.                  * case.
  2694.                  */
  2695.                 if (!crtc->primary->format_default) {
  2696.                         ret = drm_plane_check_pixel_format(crtc->primary,
  2697.                                                            fb->pixel_format);
  2698.                         if (ret) {
  2699.                                 DRM_DEBUG_KMS("Invalid pixel format %s\n",
  2700.                                         drm_get_format_name(fb->pixel_format));
  2701.                                 goto out;
  2702.                         }
  2703.                 }
  2704.  
  2705.                 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
  2706.                                               mode, fb);
  2707.                 if (ret)
  2708.                         goto out;
  2709.  
  2710.         }
  2711.  
  2712.         if (crtc_req->count_connectors == 0 && mode) {
  2713.                 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
  2714.                 ret = -EINVAL;
  2715.                 goto out;
  2716.         }
  2717.  
  2718.         if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  2719.                 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
  2720.                           crtc_req->count_connectors);
  2721.                 ret = -EINVAL;
  2722.                 goto out;
  2723.         }
  2724.  
  2725.         if (crtc_req->count_connectors > 0) {
  2726.                 u32 out_id;
  2727.  
  2728.                 /* Avoid unbounded kernel memory allocation */
  2729.                 if (crtc_req->count_connectors > config->num_connector) {
  2730.                         ret = -EINVAL;
  2731.                         goto out;
  2732.                 }
  2733.  
  2734.                 connector_set = kmalloc_array(crtc_req->count_connectors,
  2735.                                               sizeof(struct drm_connector *),
  2736.                                               GFP_KERNEL);
  2737.                 if (!connector_set) {
  2738.                         ret = -ENOMEM;
  2739.                         goto out;
  2740.                 }
  2741.  
  2742.                 for (i = 0; i < crtc_req->count_connectors; i++) {
  2743.                         set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
  2744.                         if (get_user(out_id, &set_connectors_ptr[i])) {
  2745.                                 ret = -EFAULT;
  2746.                                 goto out;
  2747.                         }
  2748.  
  2749.                         connector = drm_connector_find(dev, out_id);
  2750.                         if (!connector) {
  2751.                                 DRM_DEBUG_KMS("Connector id %d unknown\n",
  2752.                                                 out_id);
  2753.                                 ret = -ENOENT;
  2754.                                 goto out;
  2755.                         }
  2756.                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  2757.                                         connector->base.id,
  2758.                                         connector->name);
  2759.  
  2760.                         connector_set[i] = connector;
  2761.                 }
  2762.         }
  2763.  
  2764.         set.crtc = crtc;
  2765.         set.x = crtc_req->x;
  2766.         set.y = crtc_req->y;
  2767.         set.mode = mode;
  2768.         set.connectors = connector_set;
  2769.         set.num_connectors = crtc_req->count_connectors;
  2770.         set.fb = fb;
  2771.         ret = drm_mode_set_config_internal(&set);
  2772.  
  2773. out:
  2774.         if (fb)
  2775.                 drm_framebuffer_unreference(fb);
  2776.  
  2777.         kfree(connector_set);
  2778.         drm_mode_destroy(dev, mode);
  2779.         drm_modeset_unlock_all(dev);
  2780.         return ret;
  2781. }
  2782.  
  2783. static int drm_mode_cursor_common(struct drm_device *dev,
  2784.                                   struct drm_mode_cursor2 *req,
  2785.                                   struct drm_file *file_priv)
  2786. {
  2787.         struct drm_crtc *crtc;
  2788.         int ret = 0;
  2789.  
  2790.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2791.                 return -EINVAL;
  2792.  
  2793.         if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
  2794.                 return -EINVAL;
  2795.  
  2796.         crtc = drm_crtc_find(dev, req->crtc_id);
  2797.         if (!crtc) {
  2798.                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  2799.                 return -ENOENT;
  2800.         }
  2801.  
  2802.         /*
  2803.          * If this crtc has a universal cursor plane, call that plane's update
  2804.          * handler rather than using legacy cursor handlers.
  2805.          */
  2806.         drm_modeset_lock_crtc(crtc, crtc->cursor);
  2807.         if (crtc->cursor) {
  2808.                 ret = drm_mode_cursor_universal(crtc, req, file_priv);
  2809.                 goto out;
  2810.         }
  2811.  
  2812.         if (req->flags & DRM_MODE_CURSOR_BO) {
  2813.                 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
  2814.                         ret = -ENXIO;
  2815.                         goto out;
  2816.                 }
  2817.                 /* Turns off the cursor if handle is 0 */
  2818.                 if (crtc->funcs->cursor_set2)
  2819.                         ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
  2820.                                                       req->width, req->height, req->hot_x, req->hot_y);
  2821.                 else
  2822.                         ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  2823.                                                       req->width, req->height);
  2824.         }
  2825.  
  2826.         if (req->flags & DRM_MODE_CURSOR_MOVE) {
  2827.                 if (crtc->funcs->cursor_move) {
  2828.                         ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  2829.                 } else {
  2830.                         ret = -EFAULT;
  2831.                         goto out;
  2832.                 }
  2833.         }
  2834. out:
  2835.         drm_modeset_unlock_crtc(crtc);
  2836.  
  2837.         return ret;
  2838.  
  2839. }
  2840.  
  2841.  
  2842. /**
  2843.  * drm_mode_cursor_ioctl - set CRTC's cursor configuration
  2844.  * @dev: drm device for the ioctl
  2845.  * @data: data pointer for the ioctl
  2846.  * @file_priv: drm file for the ioctl call
  2847.  *
  2848.  * Set the cursor configuration based on user request.
  2849.  *
  2850.  * Called by the user via ioctl.
  2851.  *
  2852.  * Returns:
  2853.  * Zero on success, negative errno on failure.
  2854.  */
  2855. int drm_mode_cursor_ioctl(struct drm_device *dev,
  2856.                           void *data, struct drm_file *file_priv)
  2857. {
  2858.         struct drm_mode_cursor *req = data;
  2859.         struct drm_mode_cursor2 new_req;
  2860.  
  2861.         memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
  2862.         new_req.hot_x = new_req.hot_y = 0;
  2863.  
  2864.         return drm_mode_cursor_common(dev, &new_req, file_priv);
  2865. }
  2866.  
  2867. /**
  2868.  * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
  2869.  * @dev: drm device for the ioctl
  2870.  * @data: data pointer for the ioctl
  2871.  * @file_priv: drm file for the ioctl call
  2872.  *
  2873.  * Set the cursor configuration based on user request. This implements the 2nd
  2874.  * version of the cursor ioctl, which allows userspace to additionally specify
  2875.  * the hotspot of the pointer.
  2876.  *
  2877.  * Called by the user via ioctl.
  2878.  *
  2879.  * Returns:
  2880.  * Zero on success, negative errno on failure.
  2881.  */
  2882. int drm_mode_cursor2_ioctl(struct drm_device *dev,
  2883.                            void *data, struct drm_file *file_priv)
  2884. {
  2885.         struct drm_mode_cursor2 *req = data;
  2886.  
  2887.         return drm_mode_cursor_common(dev, req, file_priv);
  2888. }
  2889. #endif
  2890.  
  2891. /**
  2892.  * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
  2893.  * @bpp: bits per pixels
  2894.  * @depth: bit depth per pixel
  2895.  *
  2896.  * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
  2897.  * Useful in fbdev emulation code, since that deals in those values.
  2898.  */
  2899. uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
  2900. {
  2901.         uint32_t fmt;
  2902.  
  2903.         switch (bpp) {
  2904.         case 8:
  2905.                 fmt = DRM_FORMAT_C8;
  2906.                 break;
  2907.         case 16:
  2908.                 if (depth == 15)
  2909.                         fmt = DRM_FORMAT_XRGB1555;
  2910.                 else
  2911.                         fmt = DRM_FORMAT_RGB565;
  2912.                 break;
  2913.         case 24:
  2914.                 fmt = DRM_FORMAT_RGB888;
  2915.                 break;
  2916.         case 32:
  2917.                 if (depth == 24)
  2918.                         fmt = DRM_FORMAT_XRGB8888;
  2919.                 else if (depth == 30)
  2920.                         fmt = DRM_FORMAT_XRGB2101010;
  2921.                 else
  2922.                         fmt = DRM_FORMAT_ARGB8888;
  2923.                 break;
  2924.         default:
  2925.                 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
  2926.                 fmt = DRM_FORMAT_XRGB8888;
  2927.                 break;
  2928.         }
  2929.  
  2930.         return fmt;
  2931. }
  2932. EXPORT_SYMBOL(drm_mode_legacy_fb_format);
  2933. #if 0
  2934. /**
  2935.  * drm_mode_addfb - add an FB to the graphics configuration
  2936.  * @dev: drm device for the ioctl
  2937.  * @data: data pointer for the ioctl
  2938.  * @file_priv: drm file for the ioctl call
  2939.  *
  2940.  * Add a new FB to the specified CRTC, given a user request. This is the
  2941.  * original addfb ioctl which only supported RGB formats.
  2942.  *
  2943.  * Called by the user via ioctl.
  2944.  *
  2945.  * Returns:
  2946.  * Zero on success, negative errno on failure.
  2947.  */
  2948. int drm_mode_addfb(struct drm_device *dev,
  2949.                    void *data, struct drm_file *file_priv)
  2950. {
  2951.         struct drm_mode_fb_cmd *or = data;
  2952.         struct drm_mode_fb_cmd2 r = {};
  2953.         int ret;
  2954.  
  2955.         /* convert to new format and call new ioctl */
  2956.         r.fb_id = or->fb_id;
  2957.         r.width = or->width;
  2958.         r.height = or->height;
  2959.         r.pitches[0] = or->pitch;
  2960.         r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
  2961.         r.handles[0] = or->handle;
  2962.  
  2963.         ret = drm_mode_addfb2(dev, &r, file_priv);
  2964.         if (ret)
  2965.                 return ret;
  2966.  
  2967.         or->fb_id = r.fb_id;
  2968.  
  2969.         return 0;
  2970. }
  2971.  
  2972. static int format_check(const struct drm_mode_fb_cmd2 *r)
  2973. {
  2974.         uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
  2975.  
  2976.         switch (format) {
  2977.         case DRM_FORMAT_C8:
  2978.         case DRM_FORMAT_RGB332:
  2979.         case DRM_FORMAT_BGR233:
  2980.         case DRM_FORMAT_XRGB4444:
  2981.         case DRM_FORMAT_XBGR4444:
  2982.         case DRM_FORMAT_RGBX4444:
  2983.         case DRM_FORMAT_BGRX4444:
  2984.         case DRM_FORMAT_ARGB4444:
  2985.         case DRM_FORMAT_ABGR4444:
  2986.         case DRM_FORMAT_RGBA4444:
  2987.         case DRM_FORMAT_BGRA4444:
  2988.         case DRM_FORMAT_XRGB1555:
  2989.         case DRM_FORMAT_XBGR1555:
  2990.         case DRM_FORMAT_RGBX5551:
  2991.         case DRM_FORMAT_BGRX5551:
  2992.         case DRM_FORMAT_ARGB1555:
  2993.         case DRM_FORMAT_ABGR1555:
  2994.         case DRM_FORMAT_RGBA5551:
  2995.         case DRM_FORMAT_BGRA5551:
  2996.         case DRM_FORMAT_RGB565:
  2997.         case DRM_FORMAT_BGR565:
  2998.         case DRM_FORMAT_RGB888:
  2999.         case DRM_FORMAT_BGR888:
  3000.         case DRM_FORMAT_XRGB8888:
  3001.         case DRM_FORMAT_XBGR8888:
  3002.         case DRM_FORMAT_RGBX8888:
  3003.         case DRM_FORMAT_BGRX8888:
  3004.         case DRM_FORMAT_ARGB8888:
  3005.         case DRM_FORMAT_ABGR8888:
  3006.         case DRM_FORMAT_RGBA8888:
  3007.         case DRM_FORMAT_BGRA8888:
  3008.         case DRM_FORMAT_XRGB2101010:
  3009.         case DRM_FORMAT_XBGR2101010:
  3010.         case DRM_FORMAT_RGBX1010102:
  3011.         case DRM_FORMAT_BGRX1010102:
  3012.         case DRM_FORMAT_ARGB2101010:
  3013.         case DRM_FORMAT_ABGR2101010:
  3014.         case DRM_FORMAT_RGBA1010102:
  3015.         case DRM_FORMAT_BGRA1010102:
  3016.         case DRM_FORMAT_YUYV:
  3017.         case DRM_FORMAT_YVYU:
  3018.         case DRM_FORMAT_UYVY:
  3019.         case DRM_FORMAT_VYUY:
  3020.         case DRM_FORMAT_AYUV:
  3021.         case DRM_FORMAT_NV12:
  3022.         case DRM_FORMAT_NV21:
  3023.         case DRM_FORMAT_NV16:
  3024.         case DRM_FORMAT_NV61:
  3025.         case DRM_FORMAT_NV24:
  3026.         case DRM_FORMAT_NV42:
  3027.         case DRM_FORMAT_YUV410:
  3028.         case DRM_FORMAT_YVU410:
  3029.         case DRM_FORMAT_YUV411:
  3030.         case DRM_FORMAT_YVU411:
  3031.         case DRM_FORMAT_YUV420:
  3032.         case DRM_FORMAT_YVU420:
  3033.         case DRM_FORMAT_YUV422:
  3034.         case DRM_FORMAT_YVU422:
  3035.         case DRM_FORMAT_YUV444:
  3036.         case DRM_FORMAT_YVU444:
  3037.                 return 0;
  3038.         default:
  3039.                 DRM_DEBUG_KMS("invalid pixel format %s\n",
  3040.                               drm_get_format_name(r->pixel_format));
  3041.                 return -EINVAL;
  3042.         }
  3043. }
  3044.  
  3045. static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
  3046. {
  3047.         int ret, hsub, vsub, num_planes, i;
  3048.  
  3049.         ret = format_check(r);
  3050.         if (ret) {
  3051.                 DRM_DEBUG_KMS("bad framebuffer format %s\n",
  3052.                               drm_get_format_name(r->pixel_format));
  3053.                 return ret;
  3054.         }
  3055.  
  3056.         hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
  3057.         vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
  3058.         num_planes = drm_format_num_planes(r->pixel_format);
  3059.  
  3060.         if (r->width == 0 || r->width % hsub) {
  3061.                 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
  3062.                 return -EINVAL;
  3063.         }
  3064.  
  3065.         if (r->height == 0 || r->height % vsub) {
  3066.                 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
  3067.                 return -EINVAL;
  3068.         }
  3069.  
  3070.         for (i = 0; i < num_planes; i++) {
  3071.                 unsigned int width = r->width / (i != 0 ? hsub : 1);
  3072.                 unsigned int height = r->height / (i != 0 ? vsub : 1);
  3073.                 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
  3074.  
  3075.                 if (!r->handles[i]) {
  3076.                         DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
  3077.                         return -EINVAL;
  3078.                 }
  3079.  
  3080.                 if ((uint64_t) width * cpp > UINT_MAX)
  3081.                         return -ERANGE;
  3082.  
  3083.                 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
  3084.                         return -ERANGE;
  3085.  
  3086.                 if (r->pitches[i] < width * cpp) {
  3087.                         DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
  3088.                         return -EINVAL;
  3089.                 }
  3090.  
  3091.                 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
  3092.                         DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
  3093.                                       r->modifier[i], i);
  3094.                         return -EINVAL;
  3095.                 }
  3096.  
  3097.                 /* modifier specific checks: */
  3098.                 switch (r->modifier[i]) {
  3099.                 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
  3100.                         /* NOTE: the pitch restriction may be lifted later if it turns
  3101.                          * out that no hw has this restriction:
  3102.                          */
  3103.                         if (r->pixel_format != DRM_FORMAT_NV12 ||
  3104.                                         width % 128 || height % 32 ||
  3105.                                         r->pitches[i] % 128) {
  3106.                                 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
  3107.                                 return -EINVAL;
  3108.                         }
  3109.                         break;
  3110.  
  3111.                 default:
  3112.                         break;
  3113.                 }
  3114.         }
  3115.  
  3116.         for (i = num_planes; i < 4; i++) {
  3117.                 if (r->modifier[i]) {
  3118.                         DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
  3119.                         return -EINVAL;
  3120.                 }
  3121.  
  3122.                 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
  3123.                 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
  3124.                         continue;
  3125.  
  3126.                 if (r->handles[i]) {
  3127.                         DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
  3128.                         return -EINVAL;
  3129.                 }
  3130.  
  3131.                 if (r->pitches[i]) {
  3132.                         DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
  3133.                         return -EINVAL;
  3134.                 }
  3135.  
  3136.                 if (r->offsets[i]) {
  3137.                         DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
  3138.                         return -EINVAL;
  3139.                 }
  3140.         }
  3141.  
  3142.         return 0;
  3143. }
  3144.  
  3145. static struct drm_framebuffer *
  3146. internal_framebuffer_create(struct drm_device *dev,
  3147.                             struct drm_mode_fb_cmd2 *r,
  3148.                             struct drm_file *file_priv)
  3149. {
  3150.         struct drm_mode_config *config = &dev->mode_config;
  3151.         struct drm_framebuffer *fb;
  3152.         int ret;
  3153.  
  3154.         if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
  3155.                 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
  3156.                 return ERR_PTR(-EINVAL);
  3157.         }
  3158.  
  3159.         if ((config->min_width > r->width) || (r->width > config->max_width)) {
  3160.                 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
  3161.                           r->width, config->min_width, config->max_width);
  3162.                 return ERR_PTR(-EINVAL);
  3163.         }
  3164.         if ((config->min_height > r->height) || (r->height > config->max_height)) {
  3165.                 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
  3166.                           r->height, config->min_height, config->max_height);
  3167.                 return ERR_PTR(-EINVAL);
  3168.         }
  3169.  
  3170.         if (r->flags & DRM_MODE_FB_MODIFIERS &&
  3171.             !dev->mode_config.allow_fb_modifiers) {
  3172.                 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
  3173.                 return ERR_PTR(-EINVAL);
  3174.         }
  3175.  
  3176.         ret = framebuffer_check(r);
  3177.         if (ret)
  3178.                 return ERR_PTR(ret);
  3179.  
  3180.         fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
  3181.         if (IS_ERR(fb)) {
  3182.                 DRM_DEBUG_KMS("could not create framebuffer\n");
  3183.                 return fb;
  3184.         }
  3185.  
  3186.         return fb;
  3187. }
  3188.  
  3189. /**
  3190.  * drm_mode_addfb2 - add an FB to the graphics configuration
  3191.  * @dev: drm device for the ioctl
  3192.  * @data: data pointer for the ioctl
  3193.  * @file_priv: drm file for the ioctl call
  3194.  *
  3195.  * Add a new FB to the specified CRTC, given a user request with format. This is
  3196.  * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
  3197.  * and uses fourcc codes as pixel format specifiers.
  3198.  *
  3199.  * Called by the user via ioctl.
  3200.  *
  3201.  * Returns:
  3202.  * Zero on success, negative errno on failure.
  3203.  */
  3204. int drm_mode_addfb2(struct drm_device *dev,
  3205.                     void *data, struct drm_file *file_priv)
  3206. {
  3207.         struct drm_mode_fb_cmd2 *r = data;
  3208.         struct drm_framebuffer *fb;
  3209.  
  3210.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3211.                 return -EINVAL;
  3212.  
  3213.         fb = internal_framebuffer_create(dev, r, file_priv);
  3214.         if (IS_ERR(fb))
  3215.                 return PTR_ERR(fb);
  3216.  
  3217.         /* Transfer ownership to the filp for reaping on close */
  3218.  
  3219.         DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  3220.         mutex_lock(&file_priv->fbs_lock);
  3221.         r->fb_id = fb->base.id;
  3222.         list_add(&fb->filp_head, &file_priv->fbs);
  3223.         mutex_unlock(&file_priv->fbs_lock);
  3224.  
  3225.         return 0;
  3226. }
  3227.  
  3228. /**
  3229.  * drm_mode_rmfb - remove an FB from the configuration
  3230.  * @dev: drm device for the ioctl
  3231.  * @data: data pointer for the ioctl
  3232.  * @file_priv: drm file for the ioctl call
  3233.  *
  3234.  * Remove the FB specified by the user.
  3235.  *
  3236.  * Called by the user via ioctl.
  3237.  *
  3238.  * Returns:
  3239.  * Zero on success, negative errno on failure.
  3240.  */
  3241. int drm_mode_rmfb(struct drm_device *dev,
  3242.                    void *data, struct drm_file *file_priv)
  3243. {
  3244.         struct drm_framebuffer *fb = NULL;
  3245.         struct drm_framebuffer *fbl = NULL;
  3246.         uint32_t *id = data;
  3247.         int found = 0;
  3248.  
  3249.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3250.                 return -EINVAL;
  3251.  
  3252.         mutex_lock(&file_priv->fbs_lock);
  3253.         mutex_lock(&dev->mode_config.fb_lock);
  3254.         fb = __drm_framebuffer_lookup(dev, *id);
  3255.         if (!fb)
  3256.                 goto fail_lookup;
  3257.  
  3258.         list_for_each_entry(fbl, &file_priv->fbs, filp_head)
  3259.                 if (fb == fbl)
  3260.                         found = 1;
  3261.         if (!found)
  3262.                 goto fail_lookup;
  3263.  
  3264.         list_del_init(&fb->filp_head);
  3265.         mutex_unlock(&dev->mode_config.fb_lock);
  3266.         mutex_unlock(&file_priv->fbs_lock);
  3267.  
  3268.         drm_framebuffer_unreference(fb);
  3269.  
  3270.         return 0;
  3271.  
  3272. fail_lookup:
  3273.         mutex_unlock(&dev->mode_config.fb_lock);
  3274.         mutex_unlock(&file_priv->fbs_lock);
  3275.  
  3276.         return -ENOENT;
  3277. }
  3278.  
  3279. /**
  3280.  * drm_mode_getfb - get FB info
  3281.  * @dev: drm device for the ioctl
  3282.  * @data: data pointer for the ioctl
  3283.  * @file_priv: drm file for the ioctl call
  3284.  *
  3285.  * Lookup the FB given its ID and return info about it.
  3286.  *
  3287.  * Called by the user via ioctl.
  3288.  *
  3289.  * Returns:
  3290.  * Zero on success, negative errno on failure.
  3291.  */
  3292. int drm_mode_getfb(struct drm_device *dev,
  3293.                    void *data, struct drm_file *file_priv)
  3294. {
  3295.         struct drm_mode_fb_cmd *r = data;
  3296.         struct drm_framebuffer *fb;
  3297.         int ret;
  3298.  
  3299.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3300.                 return -EINVAL;
  3301.  
  3302.         fb = drm_framebuffer_lookup(dev, r->fb_id);
  3303.         if (!fb)
  3304.                 return -ENOENT;
  3305.  
  3306.         r->height = fb->height;
  3307.         r->width = fb->width;
  3308.         r->depth = fb->depth;
  3309.         r->bpp = fb->bits_per_pixel;
  3310.         r->pitch = fb->pitches[0];
  3311.         if (fb->funcs->create_handle) {
  3312.                 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
  3313.                     drm_is_control_client(file_priv)) {
  3314.                         ret = fb->funcs->create_handle(fb, file_priv,
  3315.                                                        &r->handle);
  3316.                 } else {
  3317.                         /* GET_FB() is an unprivileged ioctl so we must not
  3318.                          * return a buffer-handle to non-master processes! For
  3319.                          * backwards-compatibility reasons, we cannot make
  3320.                          * GET_FB() privileged, so just return an invalid handle
  3321.                          * for non-masters. */
  3322.                         r->handle = 0;
  3323.                         ret = 0;
  3324.                 }
  3325.         } else {
  3326.                 ret = -ENODEV;
  3327.         }
  3328.  
  3329.         drm_framebuffer_unreference(fb);
  3330.  
  3331.         return ret;
  3332. }
  3333.  
  3334. /**
  3335.  * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
  3336.  * @dev: drm device for the ioctl
  3337.  * @data: data pointer for the ioctl
  3338.  * @file_priv: drm file for the ioctl call
  3339.  *
  3340.  * Lookup the FB and flush out the damaged area supplied by userspace as a clip
  3341.  * rectangle list. Generic userspace which does frontbuffer rendering must call
  3342.  * this ioctl to flush out the changes on manual-update display outputs, e.g.
  3343.  * usb display-link, mipi manual update panels or edp panel self refresh modes.
  3344.  *
  3345.  * Modesetting drivers which always update the frontbuffer do not need to
  3346.  * implement the corresponding ->dirty framebuffer callback.
  3347.  *
  3348.  * Called by the user via ioctl.
  3349.  *
  3350.  * Returns:
  3351.  * Zero on success, negative errno on failure.
  3352.  */
  3353. int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
  3354.                            void *data, struct drm_file *file_priv)
  3355. {
  3356.         struct drm_clip_rect __user *clips_ptr;
  3357.         struct drm_clip_rect *clips = NULL;
  3358.         struct drm_mode_fb_dirty_cmd *r = data;
  3359.         struct drm_framebuffer *fb;
  3360.         unsigned flags;
  3361.         int num_clips;
  3362.         int ret;
  3363.  
  3364.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3365.                 return -EINVAL;
  3366.  
  3367.         fb = drm_framebuffer_lookup(dev, r->fb_id);
  3368.         if (!fb)
  3369.                 return -ENOENT;
  3370.  
  3371.         num_clips = r->num_clips;
  3372.         clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
  3373.  
  3374.         if (!num_clips != !clips_ptr) {
  3375.                 ret = -EINVAL;
  3376.                 goto out_err1;
  3377.         }
  3378.  
  3379.         flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
  3380.  
  3381.         /* If userspace annotates copy, clips must come in pairs */
  3382.         if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
  3383.                 ret = -EINVAL;
  3384.                 goto out_err1;
  3385.         }
  3386.  
  3387.         if (num_clips && clips_ptr) {
  3388.                 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
  3389.                         ret = -EINVAL;
  3390.                         goto out_err1;
  3391.                 }
  3392.                 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
  3393.                 if (!clips) {
  3394.                         ret = -ENOMEM;
  3395.                         goto out_err1;
  3396.                 }
  3397.  
  3398.                 ret = copy_from_user(clips, clips_ptr,
  3399.                                      num_clips * sizeof(*clips));
  3400.                 if (ret) {
  3401.                         ret = -EFAULT;
  3402.                         goto out_err2;
  3403.                 }
  3404.         }
  3405.  
  3406.         if (fb->funcs->dirty) {
  3407.                 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
  3408.                                        clips, num_clips);
  3409.         } else {
  3410.                 ret = -ENOSYS;
  3411.         }
  3412.  
  3413. out_err2:
  3414.         kfree(clips);
  3415. out_err1:
  3416.         drm_framebuffer_unreference(fb);
  3417.  
  3418.         return ret;
  3419. }
  3420.  
  3421. /**
  3422.  * drm_fb_release - remove and free the FBs on this file
  3423.  * @priv: drm file for the ioctl
  3424.  *
  3425.  * Destroy all the FBs associated with @filp.
  3426.  *
  3427.  * Called by the user via ioctl.
  3428.  *
  3429.  * Returns:
  3430.  * Zero on success, negative errno on failure.
  3431.  */
  3432. void drm_fb_release(struct drm_file *priv)
  3433. {
  3434.         struct drm_framebuffer *fb, *tfb;
  3435.  
  3436.         /*
  3437.          * When the file gets released that means no one else can access the fb
  3438.          * list any more, so no need to grab fpriv->fbs_lock. And we need to
  3439.          * avoid upsetting lockdep since the universal cursor code adds a
  3440.          * framebuffer while holding mutex locks.
  3441.          *
  3442.          * Note that a real deadlock between fpriv->fbs_lock and the modeset
  3443.          * locks is impossible here since no one else but this function can get
  3444.          * at it any more.
  3445.          */
  3446.         list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
  3447.                 list_del_init(&fb->filp_head);
  3448.  
  3449.                 /* This drops the fpriv->fbs reference. */
  3450.                 drm_framebuffer_unreference(fb);
  3451.         }
  3452. }
  3453. #endif
  3454.  
  3455.  
  3456. /**
  3457.  * drm_property_create - create a new property type
  3458.  * @dev: drm device
  3459.  * @flags: flags specifying the property type
  3460.  * @name: name of the property
  3461.  * @num_values: number of pre-defined values
  3462.  *
  3463.  * This creates a new generic drm property which can then be attached to a drm
  3464.  * object with drm_object_attach_property. The returned property object must be
  3465.  * freed with drm_property_destroy.
  3466.  *
  3467.  * Note that the DRM core keeps a per-device list of properties and that, if
  3468.  * drm_mode_config_cleanup() is called, it will destroy all properties created
  3469.  * by the driver.
  3470.  *
  3471.  * Returns:
  3472.  * A pointer to the newly created property on success, NULL on failure.
  3473.  */
  3474. struct drm_property *drm_property_create(struct drm_device *dev, int flags,
  3475.                                          const char *name, int num_values)
  3476. {
  3477.         struct drm_property *property = NULL;
  3478.         int ret;
  3479.  
  3480.         property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
  3481.         if (!property)
  3482.                 return NULL;
  3483.  
  3484.         property->dev = dev;
  3485.  
  3486.         if (num_values) {
  3487.                 property->values = kcalloc(num_values, sizeof(uint64_t),
  3488.                                            GFP_KERNEL);
  3489.                 if (!property->values)
  3490.                         goto fail;
  3491.         }
  3492.  
  3493.         ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
  3494.         if (ret)
  3495.                 goto fail;
  3496.  
  3497.         property->flags = flags;
  3498.         property->num_values = num_values;
  3499.         INIT_LIST_HEAD(&property->enum_list);
  3500.  
  3501.         if (name) {
  3502.                 strncpy(property->name, name, DRM_PROP_NAME_LEN);
  3503.                 property->name[DRM_PROP_NAME_LEN-1] = '\0';
  3504.         }
  3505.  
  3506.         list_add_tail(&property->head, &dev->mode_config.property_list);
  3507.  
  3508.         WARN_ON(!drm_property_type_valid(property));
  3509.  
  3510.         return property;
  3511. fail:
  3512.         kfree(property->values);
  3513.         kfree(property);
  3514.         return NULL;
  3515. }
  3516. EXPORT_SYMBOL(drm_property_create);
  3517.  
  3518. /**
  3519.  * drm_property_create_enum - create a new enumeration property type
  3520.  * @dev: drm device
  3521.  * @flags: flags specifying the property type
  3522.  * @name: name of the property
  3523.  * @props: enumeration lists with property values
  3524.  * @num_values: number of pre-defined values
  3525.  *
  3526.  * This creates a new generic drm property which can then be attached to a drm
  3527.  * object with drm_object_attach_property. The returned property object must be
  3528.  * freed with drm_property_destroy.
  3529.  *
  3530.  * Userspace is only allowed to set one of the predefined values for enumeration
  3531.  * properties.
  3532.  *
  3533.  * Returns:
  3534.  * A pointer to the newly created property on success, NULL on failure.
  3535.  */
  3536. struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
  3537.                                          const char *name,
  3538.                                          const struct drm_prop_enum_list *props,
  3539.                                          int num_values)
  3540. {
  3541.         struct drm_property *property;
  3542.         int i, ret;
  3543.  
  3544.         flags |= DRM_MODE_PROP_ENUM;
  3545.  
  3546.         property = drm_property_create(dev, flags, name, num_values);
  3547.         if (!property)
  3548.                 return NULL;
  3549.  
  3550.         for (i = 0; i < num_values; i++) {
  3551.                 ret = drm_property_add_enum(property, i,
  3552.                                       props[i].type,
  3553.                                       props[i].name);
  3554.                 if (ret) {
  3555.                         drm_property_destroy(dev, property);
  3556.                         return NULL;
  3557.                 }
  3558.         }
  3559.  
  3560.         return property;
  3561. }
  3562. EXPORT_SYMBOL(drm_property_create_enum);
  3563.  
  3564. /**
  3565.  * drm_property_create_bitmask - create a new bitmask property type
  3566.  * @dev: drm device
  3567.  * @flags: flags specifying the property type
  3568.  * @name: name of the property
  3569.  * @props: enumeration lists with property bitflags
  3570.  * @num_props: size of the @props array
  3571.  * @supported_bits: bitmask of all supported enumeration values
  3572.  *
  3573.  * This creates a new bitmask drm property which can then be attached to a drm
  3574.  * object with drm_object_attach_property. The returned property object must be
  3575.  * freed with drm_property_destroy.
  3576.  *
  3577.  * Compared to plain enumeration properties userspace is allowed to set any
  3578.  * or'ed together combination of the predefined property bitflag values
  3579.  *
  3580.  * Returns:
  3581.  * A pointer to the newly created property on success, NULL on failure.
  3582.  */
  3583. struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
  3584.                                          int flags, const char *name,
  3585.                                          const struct drm_prop_enum_list *props,
  3586.                                          int num_props,
  3587.                                          uint64_t supported_bits)
  3588. {
  3589.         struct drm_property *property;
  3590.         int i, ret, index = 0;
  3591.         int num_values = hweight64(supported_bits);
  3592.  
  3593.         flags |= DRM_MODE_PROP_BITMASK;
  3594.  
  3595.         property = drm_property_create(dev, flags, name, num_values);
  3596.         if (!property)
  3597.                 return NULL;
  3598.         for (i = 0; i < num_props; i++) {
  3599.                 if (!(supported_bits & (1ULL << props[i].type)))
  3600.                         continue;
  3601.  
  3602.                 if (WARN_ON(index >= num_values)) {
  3603.                         drm_property_destroy(dev, property);
  3604.                         return NULL;
  3605.                 }
  3606.  
  3607.                 ret = drm_property_add_enum(property, index++,
  3608.                                       props[i].type,
  3609.                                       props[i].name);
  3610.                 if (ret) {
  3611.                         drm_property_destroy(dev, property);
  3612.                         return NULL;
  3613.                 }
  3614.         }
  3615.  
  3616.         return property;
  3617. }
  3618. EXPORT_SYMBOL(drm_property_create_bitmask);
  3619.  
  3620. static struct drm_property *property_create_range(struct drm_device *dev,
  3621.                                          int flags, const char *name,
  3622.                                          uint64_t min, uint64_t max)
  3623. {
  3624.         struct drm_property *property;
  3625.  
  3626.         property = drm_property_create(dev, flags, name, 2);
  3627.         if (!property)
  3628.                 return NULL;
  3629.  
  3630.         property->values[0] = min;
  3631.         property->values[1] = max;
  3632.  
  3633.         return property;
  3634. }
  3635.  
  3636. /**
  3637.  * drm_property_create_range - create a new unsigned ranged property type
  3638.  * @dev: drm device
  3639.  * @flags: flags specifying the property type
  3640.  * @name: name of the property
  3641.  * @min: minimum value of the property
  3642.  * @max: maximum value of the property
  3643.  *
  3644.  * This creates a new generic drm property which can then be attached to a drm
  3645.  * object with drm_object_attach_property. The returned property object must be
  3646.  * freed with drm_property_destroy.
  3647.  *
  3648.  * Userspace is allowed to set any unsigned integer value in the (min, max)
  3649.  * range inclusive.
  3650.  *
  3651.  * Returns:
  3652.  * A pointer to the newly created property on success, NULL on failure.
  3653.  */
  3654. struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
  3655.                                          const char *name,
  3656.                                          uint64_t min, uint64_t max)
  3657. {
  3658.         return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
  3659.                         name, min, max);
  3660. }
  3661. EXPORT_SYMBOL(drm_property_create_range);
  3662.  
  3663. /**
  3664.  * drm_property_create_signed_range - create a new signed ranged property type
  3665.  * @dev: drm device
  3666.  * @flags: flags specifying the property type
  3667.  * @name: name of the property
  3668.  * @min: minimum value of the property
  3669.  * @max: maximum value of the property
  3670.  *
  3671.  * This creates a new generic drm property which can then be attached to a drm
  3672.  * object with drm_object_attach_property. The returned property object must be
  3673.  * freed with drm_property_destroy.
  3674.  *
  3675.  * Userspace is allowed to set any signed integer value in the (min, max)
  3676.  * range inclusive.
  3677.  *
  3678.  * Returns:
  3679.  * A pointer to the newly created property on success, NULL on failure.
  3680.  */
  3681. struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
  3682.                                          int flags, const char *name,
  3683.                                          int64_t min, int64_t max)
  3684. {
  3685.         return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
  3686.                         name, I642U64(min), I642U64(max));
  3687. }
  3688. EXPORT_SYMBOL(drm_property_create_signed_range);
  3689.  
  3690. /**
  3691.  * drm_property_create_object - create a new object property type
  3692.  * @dev: drm device
  3693.  * @flags: flags specifying the property type
  3694.  * @name: name of the property
  3695.  * @type: object type from DRM_MODE_OBJECT_* defines
  3696.  *
  3697.  * This creates a new generic drm property which can then be attached to a drm
  3698.  * object with drm_object_attach_property. The returned property object must be
  3699.  * freed with drm_property_destroy.
  3700.  *
  3701.  * Userspace is only allowed to set this to any property value of the given
  3702.  * @type. Only useful for atomic properties, which is enforced.
  3703.  *
  3704.  * Returns:
  3705.  * A pointer to the newly created property on success, NULL on failure.
  3706.  */
  3707. struct drm_property *drm_property_create_object(struct drm_device *dev,
  3708.                                          int flags, const char *name, uint32_t type)
  3709. {
  3710.         struct drm_property *property;
  3711.  
  3712.         flags |= DRM_MODE_PROP_OBJECT;
  3713.  
  3714.         if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
  3715.                 return NULL;
  3716.  
  3717.         property = drm_property_create(dev, flags, name, 1);
  3718.         if (!property)
  3719.                 return NULL;
  3720.  
  3721.         property->values[0] = type;
  3722.  
  3723.         return property;
  3724. }
  3725. EXPORT_SYMBOL(drm_property_create_object);
  3726.  
  3727. /**
  3728.  * drm_property_create_bool - create a new boolean property type
  3729.  * @dev: drm device
  3730.  * @flags: flags specifying the property type
  3731.  * @name: name of the property
  3732.  *
  3733.  * This creates a new generic drm property which can then be attached to a drm
  3734.  * object with drm_object_attach_property. The returned property object must be
  3735.  * freed with drm_property_destroy.
  3736.  *
  3737.  * This is implemented as a ranged property with only {0, 1} as valid values.
  3738.  *
  3739.  * Returns:
  3740.  * A pointer to the newly created property on success, NULL on failure.
  3741.  */
  3742. struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
  3743.                                          const char *name)
  3744. {
  3745.         return drm_property_create_range(dev, flags, name, 0, 1);
  3746. }
  3747. EXPORT_SYMBOL(drm_property_create_bool);
  3748.  
  3749. /**
  3750.  * drm_property_add_enum - add a possible value to an enumeration property
  3751.  * @property: enumeration property to change
  3752.  * @index: index of the new enumeration
  3753.  * @value: value of the new enumeration
  3754.  * @name: symbolic name of the new enumeration
  3755.  *
  3756.  * This functions adds enumerations to a property.
  3757.  *
  3758.  * It's use is deprecated, drivers should use one of the more specific helpers
  3759.  * to directly create the property with all enumerations already attached.
  3760.  *
  3761.  * Returns:
  3762.  * Zero on success, error code on failure.
  3763.  */
  3764. int drm_property_add_enum(struct drm_property *property, int index,
  3765.                           uint64_t value, const char *name)
  3766. {
  3767.         struct drm_property_enum *prop_enum;
  3768.  
  3769.         if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3770.                         drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
  3771.                 return -EINVAL;
  3772.  
  3773.         /*
  3774.          * Bitmask enum properties have the additional constraint of values
  3775.          * from 0 to 63
  3776.          */
  3777.         if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
  3778.                         (value > 63))
  3779.                 return -EINVAL;
  3780.  
  3781.         if (!list_empty(&property->enum_list)) {
  3782.                 list_for_each_entry(prop_enum, &property->enum_list, head) {
  3783.                         if (prop_enum->value == value) {
  3784.                                 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  3785.                                 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  3786.                                 return 0;
  3787.                         }
  3788.                 }
  3789.         }
  3790.  
  3791.         prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
  3792.         if (!prop_enum)
  3793.                 return -ENOMEM;
  3794.  
  3795.         strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  3796.         prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  3797.         prop_enum->value = value;
  3798.  
  3799.         property->values[index] = value;
  3800.         list_add_tail(&prop_enum->head, &property->enum_list);
  3801.         return 0;
  3802. }
  3803. EXPORT_SYMBOL(drm_property_add_enum);
  3804.  
  3805. /**
  3806.  * drm_property_destroy - destroy a drm property
  3807.  * @dev: drm device
  3808.  * @property: property to destry
  3809.  *
  3810.  * This function frees a property including any attached resources like
  3811.  * enumeration values.
  3812.  */
  3813. void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
  3814. {
  3815.         struct drm_property_enum *prop_enum, *pt;
  3816.  
  3817.         list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
  3818.                 list_del(&prop_enum->head);
  3819.                 kfree(prop_enum);
  3820.         }
  3821.  
  3822.         if (property->num_values)
  3823.                 kfree(property->values);
  3824.         drm_mode_object_put(dev, &property->base);
  3825.         list_del(&property->head);
  3826.         kfree(property);
  3827. }
  3828. EXPORT_SYMBOL(drm_property_destroy);
  3829.  
  3830. /**
  3831.  * drm_object_attach_property - attach a property to a modeset object
  3832.  * @obj: drm modeset object
  3833.  * @property: property to attach
  3834.  * @init_val: initial value of the property
  3835.  *
  3836.  * This attaches the given property to the modeset object with the given initial
  3837.  * value. Currently this function cannot fail since the properties are stored in
  3838.  * a statically sized array.
  3839.  */
  3840. void drm_object_attach_property(struct drm_mode_object *obj,
  3841.                                 struct drm_property *property,
  3842.                                 uint64_t init_val)
  3843. {
  3844.         int count = obj->properties->count;
  3845.  
  3846.         if (count == DRM_OBJECT_MAX_PROPERTY) {
  3847.                 WARN(1, "Failed to attach object property (type: 0x%x). Please "
  3848.                         "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
  3849.                         "you see this message on the same object type.\n",
  3850.                         obj->type);
  3851.                 return;
  3852.         }
  3853.  
  3854.         obj->properties->properties[count] = property;
  3855.         obj->properties->values[count] = init_val;
  3856.         obj->properties->count++;
  3857.         if (property->flags & DRM_MODE_PROP_ATOMIC)
  3858.                 obj->properties->atomic_count++;
  3859. }
  3860. EXPORT_SYMBOL(drm_object_attach_property);
  3861.  
  3862. /**
  3863.  * drm_object_property_set_value - set the value of a property
  3864.  * @obj: drm mode object to set property value for
  3865.  * @property: property to set
  3866.  * @val: value the property should be set to
  3867.  *
  3868.  * This functions sets a given property on a given object. This function only
  3869.  * changes the software state of the property, it does not call into the
  3870.  * driver's ->set_property callback.
  3871.  *
  3872.  * Returns:
  3873.  * Zero on success, error code on failure.
  3874.  */
  3875. int drm_object_property_set_value(struct drm_mode_object *obj,
  3876.                                   struct drm_property *property, uint64_t val)
  3877. {
  3878.         int i;
  3879.  
  3880.         for (i = 0; i < obj->properties->count; i++) {
  3881.                 if (obj->properties->properties[i] == property) {
  3882.                         obj->properties->values[i] = val;
  3883.                         return 0;
  3884.                 }
  3885.         }
  3886.  
  3887.         return -EINVAL;
  3888. }
  3889. EXPORT_SYMBOL(drm_object_property_set_value);
  3890.  
  3891. /**
  3892.  * drm_object_property_get_value - retrieve the value of a property
  3893.  * @obj: drm mode object to get property value from
  3894.  * @property: property to retrieve
  3895.  * @val: storage for the property value
  3896.  *
  3897.  * This function retrieves the softare state of the given property for the given
  3898.  * property. Since there is no driver callback to retrieve the current property
  3899.  * value this might be out of sync with the hardware, depending upon the driver
  3900.  * and property.
  3901.  *
  3902.  * Returns:
  3903.  * Zero on success, error code on failure.
  3904.  */
  3905. int drm_object_property_get_value(struct drm_mode_object *obj,
  3906.                                   struct drm_property *property, uint64_t *val)
  3907. {
  3908.         int i;
  3909.  
  3910.         /* read-only properties bypass atomic mechanism and still store
  3911.          * their value in obj->properties->values[].. mostly to avoid
  3912.          * having to deal w/ EDID and similar props in atomic paths:
  3913.          */
  3914.         if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
  3915.                         !(property->flags & DRM_MODE_PROP_IMMUTABLE))
  3916.                 return drm_atomic_get_property(obj, property, val);
  3917.  
  3918.         for (i = 0; i < obj->properties->count; i++) {
  3919.                 if (obj->properties->properties[i] == property) {
  3920.                         *val = obj->properties->values[i];
  3921.                         return 0;
  3922.                 }
  3923.         }
  3924.  
  3925.         return -EINVAL;
  3926. }
  3927. EXPORT_SYMBOL(drm_object_property_get_value);
  3928.  
  3929. #if 0
  3930. /**
  3931.  * drm_mode_getproperty_ioctl - get the property metadata
  3932.  * @dev: DRM device
  3933.  * @data: ioctl data
  3934.  * @file_priv: DRM file info
  3935.  *
  3936.  * This function retrieves the metadata for a given property, like the different
  3937.  * possible values for an enum property or the limits for a range property.
  3938.  *
  3939.  * Blob properties are special
  3940.  *
  3941.  * Called by the user via ioctl.
  3942.  *
  3943.  * Returns:
  3944.  * Zero on success, negative errno on failure.
  3945.  */
  3946. int drm_mode_getproperty_ioctl(struct drm_device *dev,
  3947.                                void *data, struct drm_file *file_priv)
  3948. {
  3949.         struct drm_mode_get_property *out_resp = data;
  3950.         struct drm_property *property;
  3951.         int enum_count = 0;
  3952.         int value_count = 0;
  3953.         int ret = 0, i;
  3954.         int copied;
  3955.         struct drm_property_enum *prop_enum;
  3956.         struct drm_mode_property_enum __user *enum_ptr;
  3957.         uint64_t __user *values_ptr;
  3958.  
  3959.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3960.                 return -EINVAL;
  3961.  
  3962.         drm_modeset_lock_all(dev);
  3963.         property = drm_property_find(dev, out_resp->prop_id);
  3964.         if (!property) {
  3965.                 ret = -ENOENT;
  3966.                 goto done;
  3967.         }
  3968.  
  3969.         if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3970.                         drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  3971.                 list_for_each_entry(prop_enum, &property->enum_list, head)
  3972.                         enum_count++;
  3973.         }
  3974.  
  3975.         value_count = property->num_values;
  3976.  
  3977.         strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
  3978.         out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
  3979.         out_resp->flags = property->flags;
  3980.  
  3981.         if ((out_resp->count_values >= value_count) && value_count) {
  3982.                 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
  3983.                 for (i = 0; i < value_count; i++) {
  3984.                         if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
  3985.                                 ret = -EFAULT;
  3986.                                 goto done;
  3987.                         }
  3988.                 }
  3989.         }
  3990.         out_resp->count_values = value_count;
  3991.  
  3992.         if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3993.                         drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  3994.                 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
  3995.                         copied = 0;
  3996.                         enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
  3997.                         list_for_each_entry(prop_enum, &property->enum_list, head) {
  3998.  
  3999.                                 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
  4000.                                         ret = -EFAULT;
  4001.                                         goto done;
  4002.                                 }
  4003.  
  4004.                                 if (copy_to_user(&enum_ptr[copied].name,
  4005.                                                  &prop_enum->name, DRM_PROP_NAME_LEN)) {
  4006.                                         ret = -EFAULT;
  4007.                                         goto done;
  4008.                                 }
  4009.                                 copied++;
  4010.                         }
  4011.                 }
  4012.                 out_resp->count_enum_blobs = enum_count;
  4013.         }
  4014.  
  4015.         /*
  4016.          * NOTE: The idea seems to have been to use this to read all the blob
  4017.          * property values. But nothing ever added them to the corresponding
  4018.          * list, userspace always used the special-purpose get_blob ioctl to
  4019.          * read the value for a blob property. It also doesn't make a lot of
  4020.          * sense to return values here when everything else is just metadata for
  4021.          * the property itself.
  4022.          */
  4023.         if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
  4024.                 out_resp->count_enum_blobs = 0;
  4025. done:
  4026.         drm_modeset_unlock_all(dev);
  4027.         return ret;
  4028. }
  4029. #endif
  4030.  
  4031. /**
  4032.  * drm_property_create_blob - Create new blob property
  4033.  *
  4034.  * Creates a new blob property for a specified DRM device, optionally
  4035.  * copying data.
  4036.  *
  4037.  * @dev: DRM device to create property for
  4038.  * @length: Length to allocate for blob data
  4039.  * @data: If specified, copies data into blob
  4040.  *
  4041.  * Returns:
  4042.  * New blob property with a single reference on success, or an ERR_PTR
  4043.  * value on failure.
  4044.  */
  4045. struct drm_property_blob *
  4046. drm_property_create_blob(struct drm_device *dev, size_t length,
  4047.                          const void *data)
  4048. {
  4049.         struct drm_property_blob *blob;
  4050.         int ret;
  4051.  
  4052.         if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
  4053.                 return ERR_PTR(-EINVAL);
  4054.  
  4055.         blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
  4056.         if (!blob)
  4057.                 return ERR_PTR(-ENOMEM);
  4058.  
  4059.         /* This must be explicitly initialised, so we can safely call list_del
  4060.          * on it in the removal handler, even if it isn't in a file list. */
  4061.         INIT_LIST_HEAD(&blob->head_file);
  4062.         blob->length = length;
  4063.         blob->dev = dev;
  4064.  
  4065.         if (data)
  4066.                 memcpy(blob->data, data, length);
  4067.  
  4068.         mutex_lock(&dev->mode_config.blob_lock);
  4069.  
  4070.         ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
  4071.         if (ret) {
  4072.                 kfree(blob);
  4073.                 mutex_unlock(&dev->mode_config.blob_lock);
  4074.                 return ERR_PTR(-EINVAL);
  4075.         }
  4076.  
  4077.         kref_init(&blob->refcount);
  4078.  
  4079.         list_add_tail(&blob->head_global,
  4080.                       &dev->mode_config.property_blob_list);
  4081.  
  4082.         mutex_unlock(&dev->mode_config.blob_lock);
  4083.  
  4084.         return blob;
  4085. }
  4086. EXPORT_SYMBOL(drm_property_create_blob);
  4087.  
  4088. /**
  4089.  * drm_property_free_blob - Blob property destructor
  4090.  *
  4091.  * Internal free function for blob properties; must not be used directly.
  4092.  *
  4093.  * @kref: Reference
  4094.  */
  4095. static void drm_property_free_blob(struct kref *kref)
  4096. {
  4097.         struct drm_property_blob *blob =
  4098.                 container_of(kref, struct drm_property_blob, refcount);
  4099.  
  4100.         WARN_ON(!mutex_is_locked(&blob->dev->mode_config.blob_lock));
  4101.  
  4102.         list_del(&blob->head_global);
  4103.         list_del(&blob->head_file);
  4104.         drm_mode_object_put(blob->dev, &blob->base);
  4105.  
  4106.         kfree(blob);
  4107. }
  4108.  
  4109. /**
  4110.  * drm_property_unreference_blob - Unreference a blob property
  4111.  *
  4112.  * Drop a reference on a blob property. May free the object.
  4113.  *
  4114.  * @blob: Pointer to blob property
  4115.  */
  4116. void drm_property_unreference_blob(struct drm_property_blob *blob)
  4117. {
  4118.         struct drm_device *dev;
  4119.  
  4120.         if (!blob)
  4121.                 return;
  4122.  
  4123.         dev = blob->dev;
  4124.  
  4125.         DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
  4126.  
  4127.         if (kref_put_mutex(&blob->refcount, drm_property_free_blob,
  4128.                            &dev->mode_config.blob_lock))
  4129.                 mutex_unlock(&dev->mode_config.blob_lock);
  4130.         else
  4131.                 might_lock(&dev->mode_config.blob_lock);
  4132. }
  4133. EXPORT_SYMBOL(drm_property_unreference_blob);
  4134.  
  4135. /**
  4136.  * drm_property_unreference_blob_locked - Unreference a blob property with blob_lock held
  4137.  *
  4138.  * Drop a reference on a blob property. May free the object. This must be
  4139.  * called with blob_lock held.
  4140.  *
  4141.  * @blob: Pointer to blob property
  4142.  */
  4143. static void drm_property_unreference_blob_locked(struct drm_property_blob *blob)
  4144. {
  4145.         if (!blob)
  4146.                 return;
  4147.  
  4148.         DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
  4149.  
  4150.         kref_put(&blob->refcount, drm_property_free_blob);
  4151. }
  4152.  
  4153. /**
  4154.  * drm_property_destroy_user_blobs - destroy all blobs created by this client
  4155.  * @dev:       DRM device
  4156.  * @file_priv: destroy all blobs owned by this file handle
  4157.  */
  4158. void drm_property_destroy_user_blobs(struct drm_device *dev,
  4159.                                      struct drm_file *file_priv)
  4160. {
  4161.         struct drm_property_blob *blob, *bt;
  4162.  
  4163.         mutex_lock(&dev->mode_config.blob_lock);
  4164.  
  4165.         list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
  4166.                 list_del_init(&blob->head_file);
  4167.                 drm_property_unreference_blob_locked(blob);
  4168.         }
  4169.  
  4170.         mutex_unlock(&dev->mode_config.blob_lock);
  4171. }
  4172.  
  4173. /**
  4174.  * drm_property_reference_blob - Take a reference on an existing property
  4175.  *
  4176.  * Take a new reference on an existing blob property.
  4177.  *
  4178.  * @blob: Pointer to blob property
  4179.  */
  4180. struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
  4181. {
  4182.         DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
  4183.         kref_get(&blob->refcount);
  4184.         return blob;
  4185. }
  4186. EXPORT_SYMBOL(drm_property_reference_blob);
  4187.  
  4188. /*
  4189.  * Like drm_property_lookup_blob, but does not return an additional reference.
  4190.  * Must be called with blob_lock held.
  4191.  */
  4192. static struct drm_property_blob *__drm_property_lookup_blob(struct drm_device *dev,
  4193.                                                             uint32_t id)
  4194. {
  4195.         struct drm_mode_object *obj = NULL;
  4196.         struct drm_property_blob *blob;
  4197.  
  4198.         WARN_ON(!mutex_is_locked(&dev->mode_config.blob_lock));
  4199.  
  4200.         mutex_lock(&dev->mode_config.idr_mutex);
  4201.         obj = idr_find(&dev->mode_config.crtc_idr, id);
  4202.         if (!obj || (obj->type != DRM_MODE_OBJECT_BLOB) || (obj->id != id))
  4203.                 blob = NULL;
  4204.         else
  4205.                 blob = obj_to_blob(obj);
  4206.         mutex_unlock(&dev->mode_config.idr_mutex);
  4207.  
  4208.         return blob;
  4209. }
  4210.  
  4211. /**
  4212.  * drm_property_lookup_blob - look up a blob property and take a reference
  4213.  * @dev: drm device
  4214.  * @id: id of the blob property
  4215.  *
  4216.  * If successful, this takes an additional reference to the blob property.
  4217.  * callers need to make sure to eventually unreference the returned property
  4218.  * again, using @drm_property_unreference_blob.
  4219.  */
  4220. struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
  4221.                                                    uint32_t id)
  4222. {
  4223.         struct drm_property_blob *blob;
  4224.  
  4225.         mutex_lock(&dev->mode_config.blob_lock);
  4226.         blob = __drm_property_lookup_blob(dev, id);
  4227.         if (blob) {
  4228.                 if (!kref_get_unless_zero(&blob->refcount))
  4229.                         blob = NULL;
  4230.         }
  4231.         mutex_unlock(&dev->mode_config.blob_lock);
  4232.  
  4233.         return blob;
  4234. }
  4235. EXPORT_SYMBOL(drm_property_lookup_blob);
  4236.  
  4237. /**
  4238.  * drm_property_replace_global_blob - atomically replace existing blob property
  4239.  * @dev: drm device
  4240.  * @replace: location of blob property pointer to be replaced
  4241.  * @length: length of data for new blob, or 0 for no data
  4242.  * @data: content for new blob, or NULL for no data
  4243.  * @obj_holds_id: optional object for property holding blob ID
  4244.  * @prop_holds_id: optional property holding blob ID
  4245.  * @return 0 on success or error on failure
  4246.  *
  4247.  * This function will atomically replace a global property in the blob list,
  4248.  * optionally updating a property which holds the ID of that property. It is
  4249.  * guaranteed to be atomic: no caller will be allowed to see intermediate
  4250.  * results, and either the entire operation will succeed and clean up the
  4251.  * previous property, or it will fail and the state will be unchanged.
  4252.  *
  4253.  * If length is 0 or data is NULL, no new blob will be created, and the holding
  4254.  * property, if specified, will be set to 0.
  4255.  *
  4256.  * Access to the replace pointer is assumed to be protected by the caller, e.g.
  4257.  * by holding the relevant modesetting object lock for its parent.
  4258.  *
  4259.  * For example, a drm_connector has a 'PATH' property, which contains the ID
  4260.  * of a blob property with the value of the MST path information. Calling this
  4261.  * function with replace pointing to the connector's path_blob_ptr, length and
  4262.  * data set for the new path information, obj_holds_id set to the connector's
  4263.  * base object, and prop_holds_id set to the path property name, will perform
  4264.  * a completely atomic update. The access to path_blob_ptr is protected by the
  4265.  * caller holding a lock on the connector.
  4266.  */
  4267. static int drm_property_replace_global_blob(struct drm_device *dev,
  4268.                                             struct drm_property_blob **replace,
  4269.                                             size_t length,
  4270.                                             const void *data,
  4271.                                             struct drm_mode_object *obj_holds_id,
  4272.                                             struct drm_property *prop_holds_id)
  4273. {
  4274.         struct drm_property_blob *new_blob = NULL;
  4275.         struct drm_property_blob *old_blob = NULL;
  4276.         int ret;
  4277.  
  4278.         WARN_ON(replace == NULL);
  4279.  
  4280.         old_blob = *replace;
  4281.  
  4282.         if (length && data) {
  4283.                 new_blob = drm_property_create_blob(dev, length, data);
  4284.                 if (IS_ERR(new_blob))
  4285.                         return PTR_ERR(new_blob);
  4286.         }
  4287.  
  4288.         /* This does not need to be synchronised with blob_lock, as the
  4289.          * get_properties ioctl locks all modesetting objects, and
  4290.          * obj_holds_id must be locked before calling here, so we cannot
  4291.          * have its value out of sync with the list membership modified
  4292.          * below under blob_lock. */
  4293.         if (obj_holds_id) {
  4294.                 ret = drm_object_property_set_value(obj_holds_id,
  4295.                                                     prop_holds_id,
  4296.                                                     new_blob ?
  4297.                                                         new_blob->base.id : 0);
  4298.                 if (ret != 0)
  4299.                         goto err_created;
  4300.         }
  4301.  
  4302.         drm_property_unreference_blob(old_blob);
  4303.         *replace = new_blob;
  4304.  
  4305.         return 0;
  4306.  
  4307. err_created:
  4308.         drm_property_unreference_blob(new_blob);
  4309.         return ret;
  4310. }
  4311.  
  4312.  
  4313. #if 0
  4314. /**
  4315.  * drm_mode_getblob_ioctl - get the contents of a blob property value
  4316.  * @dev: DRM device
  4317.  * @data: ioctl data
  4318.  * @file_priv: DRM file info
  4319.  *
  4320.  * This function retrieves the contents of a blob property. The value stored in
  4321.  * an object's blob property is just a normal modeset object id.
  4322.  *
  4323.  * Called by the user via ioctl.
  4324.  *
  4325.  * Returns:
  4326.  * Zero on success, negative errno on failure.
  4327.  */
  4328. int drm_mode_getblob_ioctl(struct drm_device *dev,
  4329.                            void *data, struct drm_file *file_priv)
  4330. {
  4331.         struct drm_mode_get_blob *out_resp = data;
  4332.         struct drm_property_blob *blob;
  4333.         int ret = 0;
  4334.         void __user *blob_ptr;
  4335.  
  4336.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4337.                 return -EINVAL;
  4338.  
  4339.         drm_modeset_lock_all(dev);
  4340.         mutex_lock(&dev->mode_config.blob_lock);
  4341.         blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
  4342.         if (!blob) {
  4343.                 ret = -ENOENT;
  4344.                 goto done;
  4345.         }
  4346.  
  4347.         if (out_resp->length == blob->length) {
  4348.                 blob_ptr = (void __user *)(unsigned long)out_resp->data;
  4349.                 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
  4350.                         ret = -EFAULT;
  4351.                         goto done;
  4352.                 }
  4353.         }
  4354.         out_resp->length = blob->length;
  4355.  
  4356. done:
  4357.         mutex_unlock(&dev->mode_config.blob_lock);
  4358.         drm_modeset_unlock_all(dev);
  4359.         return ret;
  4360. }
  4361. #endif
  4362.  
  4363. /**
  4364.  * drm_mode_connector_set_path_property - set tile property on connector
  4365.  * @connector: connector to set property on.
  4366.  * @path: path to use for property; must not be NULL.
  4367.  *
  4368.  * This creates a property to expose to userspace to specify a
  4369.  * connector path. This is mainly used for DisplayPort MST where
  4370.  * connectors have a topology and we want to allow userspace to give
  4371.  * them more meaningful names.
  4372.  *
  4373.  * Returns:
  4374.  * Zero on success, negative errno on failure.
  4375.  */
  4376. int drm_mode_connector_set_path_property(struct drm_connector *connector,
  4377.                                          const char *path)
  4378. {
  4379.         struct drm_device *dev = connector->dev;
  4380.         int ret;
  4381.  
  4382.         ret = drm_property_replace_global_blob(dev,
  4383.                                                &connector->path_blob_ptr,
  4384.                                                strlen(path) + 1,
  4385.                                                path,
  4386.                                                &connector->base,
  4387.                                                dev->mode_config.path_property);
  4388.         return ret;
  4389. }
  4390. EXPORT_SYMBOL(drm_mode_connector_set_path_property);
  4391.  
  4392. /**
  4393.  * drm_mode_connector_set_tile_property - set tile property on connector
  4394.  * @connector: connector to set property on.
  4395.  *
  4396.  * This looks up the tile information for a connector, and creates a
  4397.  * property for userspace to parse if it exists. The property is of
  4398.  * the form of 8 integers using ':' as a separator.
  4399.  *
  4400.  * Returns:
  4401.  * Zero on success, errno on failure.
  4402.  */
  4403. int drm_mode_connector_set_tile_property(struct drm_connector *connector)
  4404. {
  4405.         struct drm_device *dev = connector->dev;
  4406.         char tile[256];
  4407.         int ret;
  4408.  
  4409.         if (!connector->has_tile) {
  4410.                 ret  = drm_property_replace_global_blob(dev,
  4411.                                                         &connector->tile_blob_ptr,
  4412.                                                         0,
  4413.                                                         NULL,
  4414.                                                         &connector->base,
  4415.                                                         dev->mode_config.tile_property);
  4416.                 return ret;
  4417.         }
  4418.  
  4419.         snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
  4420.                  connector->tile_group->id, connector->tile_is_single_monitor,
  4421.                  connector->num_h_tile, connector->num_v_tile,
  4422.                  connector->tile_h_loc, connector->tile_v_loc,
  4423.                  connector->tile_h_size, connector->tile_v_size);
  4424.  
  4425.         ret = drm_property_replace_global_blob(dev,
  4426.                                                &connector->tile_blob_ptr,
  4427.                                                strlen(tile) + 1,
  4428.                                                tile,
  4429.                                                &connector->base,
  4430.                                                dev->mode_config.tile_property);
  4431.         return ret;
  4432. }
  4433. EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
  4434.  
  4435. /**
  4436.  * drm_mode_connector_update_edid_property - update the edid property of a connector
  4437.  * @connector: drm connector
  4438.  * @edid: new value of the edid property
  4439.  *
  4440.  * This function creates a new blob modeset object and assigns its id to the
  4441.  * connector's edid property.
  4442.  *
  4443.  * Returns:
  4444.  * Zero on success, negative errno on failure.
  4445.  */
  4446. int drm_mode_connector_update_edid_property(struct drm_connector *connector,
  4447.                                             const struct edid *edid)
  4448. {
  4449.         struct drm_device *dev = connector->dev;
  4450.         size_t size = 0;
  4451.         int ret;
  4452.  
  4453.         /* ignore requests to set edid when overridden */
  4454.         if (connector->override_edid)
  4455.                 return 0;
  4456.  
  4457.         if (edid)
  4458.                 size = EDID_LENGTH * (1 + edid->extensions);
  4459.  
  4460.         ret = drm_property_replace_global_blob(dev,
  4461.                                                &connector->edid_blob_ptr,
  4462.                                                size,
  4463.                                                edid,
  4464.                                                &connector->base,
  4465.                                                dev->mode_config.edid_property);
  4466.         return ret;
  4467. }
  4468. EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
  4469.  
  4470. /* Some properties could refer to dynamic refcnt'd objects, or things that
  4471.  * need special locking to handle lifetime issues (ie. to ensure the prop
  4472.  * value doesn't become invalid part way through the property update due to
  4473.  * race).  The value returned by reference via 'obj' should be passed back
  4474.  * to drm_property_change_valid_put() after the property is set (and the
  4475.  * object to which the property is attached has a chance to take it's own
  4476.  * reference).
  4477.  */
  4478. bool drm_property_change_valid_get(struct drm_property *property,
  4479.                                          uint64_t value, struct drm_mode_object **ref)
  4480. {
  4481.         int i;
  4482.  
  4483.         if (property->flags & DRM_MODE_PROP_IMMUTABLE)
  4484.                 return false;
  4485.  
  4486.         *ref = NULL;
  4487.  
  4488.         if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
  4489.                 if (value < property->values[0] || value > property->values[1])
  4490.                         return false;
  4491.                 return true;
  4492.         } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
  4493.                 int64_t svalue = U642I64(value);
  4494.  
  4495.                 if (svalue < U642I64(property->values[0]) ||
  4496.                                 svalue > U642I64(property->values[1]))
  4497.                         return false;
  4498.                 return true;
  4499.         } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  4500.                 uint64_t valid_mask = 0;
  4501.  
  4502.                 for (i = 0; i < property->num_values; i++)
  4503.                         valid_mask |= (1ULL << property->values[i]);
  4504.                 return !(value & ~valid_mask);
  4505.         } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
  4506.                 struct drm_property_blob *blob;
  4507.  
  4508.                 if (value == 0)
  4509.                         return true;
  4510.  
  4511.                 blob = drm_property_lookup_blob(property->dev, value);
  4512.                 if (blob) {
  4513.                         *ref = &blob->base;
  4514.                         return true;
  4515.                 } else {
  4516.                         return false;
  4517.                 }
  4518.         } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
  4519.                 /* a zero value for an object property translates to null: */
  4520.                 if (value == 0)
  4521.                         return true;
  4522.  
  4523.                 /* handle refcnt'd objects specially: */
  4524.                 if (property->values[0] == DRM_MODE_OBJECT_FB) {
  4525.                         struct drm_framebuffer *fb;
  4526.                         fb = drm_framebuffer_lookup(property->dev, value);
  4527.                         if (fb) {
  4528.                                 *ref = &fb->base;
  4529.                                 return true;
  4530.                         } else {
  4531.                                 return false;
  4532.                         }
  4533.                 } else {
  4534.                         return _object_find(property->dev, value, property->values[0]) != NULL;
  4535.                 }
  4536.         }
  4537.  
  4538.         for (i = 0; i < property->num_values; i++)
  4539.                 if (property->values[i] == value)
  4540.                         return true;
  4541.         return false;
  4542. }
  4543.  
  4544. void drm_property_change_valid_put(struct drm_property *property,
  4545.                 struct drm_mode_object *ref)
  4546. {
  4547.         if (!ref)
  4548.                 return;
  4549.  
  4550.         if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
  4551.                 if (property->values[0] == DRM_MODE_OBJECT_FB)
  4552.                         drm_framebuffer_unreference(obj_to_fb(ref));
  4553.         } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
  4554.                 drm_property_unreference_blob(obj_to_blob(ref));
  4555. }
  4556.  
  4557.  
  4558.  
  4559. static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
  4560.                                            struct drm_property *property,
  4561.                                            uint64_t value)
  4562. {
  4563.         int ret = -EINVAL;
  4564.         struct drm_connector *connector = obj_to_connector(obj);
  4565.  
  4566.         /* Do DPMS ourselves */
  4567.         if (property == connector->dev->mode_config.dpms_property) {
  4568.                 ret = 0;
  4569.                 if (connector->funcs->dpms)
  4570.                         ret = (*connector->funcs->dpms)(connector, (int)value);
  4571.         } else if (connector->funcs->set_property)
  4572.                 ret = connector->funcs->set_property(connector, property, value);
  4573.  
  4574.         /* store the property value if successful */
  4575.         if (!ret)
  4576.                 drm_object_property_set_value(&connector->base, property, value);
  4577.         return ret;
  4578. }
  4579.  
  4580. static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
  4581.                                       struct drm_property *property,
  4582.                                       uint64_t value)
  4583. {
  4584.         int ret = -EINVAL;
  4585.         struct drm_crtc *crtc = obj_to_crtc(obj);
  4586.  
  4587.         if (crtc->funcs->set_property)
  4588.                 ret = crtc->funcs->set_property(crtc, property, value);
  4589.         if (!ret)
  4590.                 drm_object_property_set_value(obj, property, value);
  4591.  
  4592.         return ret;
  4593. }
  4594.  
  4595. /**
  4596.  * drm_mode_plane_set_obj_prop - set the value of a property
  4597.  * @plane: drm plane object to set property value for
  4598.  * @property: property to set
  4599.  * @value: value the property should be set to
  4600.  *
  4601.  * This functions sets a given property on a given plane object. This function
  4602.  * calls the driver's ->set_property callback and changes the software state of
  4603.  * the property if the callback succeeds.
  4604.  *
  4605.  * Returns:
  4606.  * Zero on success, error code on failure.
  4607.  */
  4608. int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
  4609.                                 struct drm_property *property,
  4610.                                 uint64_t value)
  4611. {
  4612.         int ret = -EINVAL;
  4613.         struct drm_mode_object *obj = &plane->base;
  4614.  
  4615.         if (plane->funcs->set_property)
  4616.                 ret = plane->funcs->set_property(plane, property, value);
  4617.         if (!ret)
  4618.                 drm_object_property_set_value(obj, property, value);
  4619.  
  4620.         return ret;
  4621. }
  4622. EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
  4623.  
  4624. #if 0
  4625. /**
  4626.  * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
  4627.  * @dev: DRM device
  4628.  * @data: ioctl data
  4629.  * @file_priv: DRM file info
  4630.  *
  4631.  * This function retrieves the current value for an object's property. Compared
  4632.  * to the connector specific ioctl this one is extended to also work on crtc and
  4633.  * plane objects.
  4634.  *
  4635.  * Called by the user via ioctl.
  4636.  *
  4637.  * Returns:
  4638.  * Zero on success, negative errno on failure.
  4639.  */
  4640. int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
  4641.                                       struct drm_file *file_priv)
  4642. {
  4643.         struct drm_mode_obj_get_properties *arg = data;
  4644.         struct drm_mode_object *obj;
  4645.         int ret = 0;
  4646.  
  4647.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4648.                 return -EINVAL;
  4649.  
  4650.         drm_modeset_lock_all(dev);
  4651.  
  4652.         obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  4653.         if (!obj) {
  4654.                 ret = -ENOENT;
  4655.                 goto out;
  4656.         }
  4657.         if (!obj->properties) {
  4658.                 ret = -EINVAL;
  4659.                 goto out;
  4660.         }
  4661.  
  4662.         ret = get_properties(obj, file_priv->atomic,
  4663.                         (uint32_t __user *)(unsigned long)(arg->props_ptr),
  4664.                         (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
  4665.                         &arg->count_props);
  4666.  
  4667. out:
  4668.         drm_modeset_unlock_all(dev);
  4669.         return ret;
  4670. }
  4671.  
  4672. /**
  4673.  * drm_mode_obj_set_property_ioctl - set the current value of an object's property
  4674.  * @dev: DRM device
  4675.  * @data: ioctl data
  4676.  * @file_priv: DRM file info
  4677.  *
  4678.  * This function sets the current value for an object's property. It also calls
  4679.  * into a driver's ->set_property callback to update the hardware state.
  4680.  * Compared to the connector specific ioctl this one is extended to also work on
  4681.  * crtc and plane objects.
  4682.  *
  4683.  * Called by the user via ioctl.
  4684.  *
  4685.  * Returns:
  4686.  * Zero on success, negative errno on failure.
  4687.  */
  4688. int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
  4689.                                     struct drm_file *file_priv)
  4690. {
  4691.         struct drm_mode_obj_set_property *arg = data;
  4692.         struct drm_mode_object *arg_obj;
  4693.         struct drm_mode_object *prop_obj;
  4694.         struct drm_property *property;
  4695.         int i, ret = -EINVAL;
  4696.         struct drm_mode_object *ref;
  4697.  
  4698.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4699.                 return -EINVAL;
  4700.  
  4701.         drm_modeset_lock_all(dev);
  4702.  
  4703.         arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  4704.         if (!arg_obj) {
  4705.                 ret = -ENOENT;
  4706.                 goto out;
  4707.         }
  4708.         if (!arg_obj->properties)
  4709.                 goto out;
  4710.  
  4711.         for (i = 0; i < arg_obj->properties->count; i++)
  4712.                 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
  4713.                         break;
  4714.  
  4715.         if (i == arg_obj->properties->count)
  4716.                 goto out;
  4717.  
  4718.         prop_obj = drm_mode_object_find(dev, arg->prop_id,
  4719.                                         DRM_MODE_OBJECT_PROPERTY);
  4720.         if (!prop_obj) {
  4721.                 ret = -ENOENT;
  4722.                 goto out;
  4723.         }
  4724.         property = obj_to_property(prop_obj);
  4725.  
  4726.         if (!drm_property_change_valid_get(property, arg->value, &ref))
  4727.                 goto out;
  4728.  
  4729.         switch (arg_obj->type) {
  4730.         case DRM_MODE_OBJECT_CONNECTOR:
  4731.                 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
  4732.                                                       arg->value);
  4733.                 break;
  4734.         case DRM_MODE_OBJECT_CRTC:
  4735.                 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
  4736.                 break;
  4737.         case DRM_MODE_OBJECT_PLANE:
  4738.                 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
  4739.                                                   property, arg->value);
  4740.                 break;
  4741.         }
  4742.  
  4743.         drm_property_change_valid_put(property, ref);
  4744.  
  4745. out:
  4746.         drm_modeset_unlock_all(dev);
  4747.         return ret;
  4748. }
  4749. #endif
  4750.  
  4751. /**
  4752.  * drm_mode_connector_attach_encoder - attach a connector to an encoder
  4753.  * @connector: connector to attach
  4754.  * @encoder: encoder to attach @connector to
  4755.  *
  4756.  * This function links up a connector to an encoder. Note that the routing
  4757.  * restrictions between encoders and crtcs are exposed to userspace through the
  4758.  * possible_clones and possible_crtcs bitmasks.
  4759.  *
  4760.  * Returns:
  4761.  * Zero on success, negative errno on failure.
  4762.  */
  4763. int drm_mode_connector_attach_encoder(struct drm_connector *connector,
  4764.                                       struct drm_encoder *encoder)
  4765. {
  4766.         int i;
  4767.  
  4768.         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  4769.                 if (connector->encoder_ids[i] == 0) {
  4770.                         connector->encoder_ids[i] = encoder->base.id;
  4771.                         return 0;
  4772.                 }
  4773.         }
  4774.         return -ENOMEM;
  4775. }
  4776. EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
  4777.  
  4778. /**
  4779.  * drm_mode_crtc_set_gamma_size - set the gamma table size
  4780.  * @crtc: CRTC to set the gamma table size for
  4781.  * @gamma_size: size of the gamma table
  4782.  *
  4783.  * Drivers which support gamma tables should set this to the supported gamma
  4784.  * table size when initializing the CRTC. Currently the drm core only supports a
  4785.  * fixed gamma table size.
  4786.  *
  4787.  * Returns:
  4788.  * Zero on success, negative errno on failure.
  4789.  */
  4790. int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
  4791.                                  int gamma_size)
  4792. {
  4793.         crtc->gamma_size = gamma_size;
  4794.  
  4795.         crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
  4796.                                     GFP_KERNEL);
  4797.         if (!crtc->gamma_store) {
  4798.                 crtc->gamma_size = 0;
  4799.                 return -ENOMEM;
  4800.         }
  4801.  
  4802.         return 0;
  4803. }
  4804. EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
  4805.  
  4806. #if 0
  4807. /**
  4808.  * drm_mode_gamma_set_ioctl - set the gamma table
  4809.  * @dev: DRM device
  4810.  * @data: ioctl data
  4811.  * @file_priv: DRM file info
  4812.  *
  4813.  * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
  4814.  * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
  4815.  *
  4816.  * Called by the user via ioctl.
  4817.  *
  4818.  * Returns:
  4819.  * Zero on success, negative errno on failure.
  4820.  */
  4821. int drm_mode_gamma_set_ioctl(struct drm_device *dev,
  4822.                              void *data, struct drm_file *file_priv)
  4823. {
  4824.         struct drm_mode_crtc_lut *crtc_lut = data;
  4825.         struct drm_crtc *crtc;
  4826.         void *r_base, *g_base, *b_base;
  4827.         int size;
  4828.         int ret = 0;
  4829.  
  4830.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4831.                 return -EINVAL;
  4832.  
  4833.         drm_modeset_lock_all(dev);
  4834.         crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
  4835.         if (!crtc) {
  4836.                 ret = -ENOENT;
  4837.                 goto out;
  4838.         }
  4839.  
  4840.         if (crtc->funcs->gamma_set == NULL) {
  4841.                 ret = -ENOSYS;
  4842.                 goto out;
  4843.         }
  4844.  
  4845.         /* memcpy into gamma store */
  4846.         if (crtc_lut->gamma_size != crtc->gamma_size) {
  4847.                 ret = -EINVAL;
  4848.                 goto out;
  4849.         }
  4850.  
  4851.         size = crtc_lut->gamma_size * (sizeof(uint16_t));
  4852.         r_base = crtc->gamma_store;
  4853.         if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
  4854.                 ret = -EFAULT;
  4855.                 goto out;
  4856.         }
  4857.  
  4858.         g_base = r_base + size;
  4859.         if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
  4860.                 ret = -EFAULT;
  4861.                 goto out;
  4862.         }
  4863.  
  4864.         b_base = g_base + size;
  4865.         if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
  4866.                 ret = -EFAULT;
  4867.                 goto out;
  4868.         }
  4869.  
  4870.         crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
  4871.  
  4872. out:
  4873.         drm_modeset_unlock_all(dev);
  4874.         return ret;
  4875.  
  4876. }
  4877.  
  4878. /**
  4879.  * drm_mode_gamma_get_ioctl - get the gamma table
  4880.  * @dev: DRM device
  4881.  * @data: ioctl data
  4882.  * @file_priv: DRM file info
  4883.  *
  4884.  * Copy the current gamma table into the storage provided. This also provides
  4885.  * the gamma table size the driver expects, which can be used to size the
  4886.  * allocated storage.
  4887.  *
  4888.  * Called by the user via ioctl.
  4889.  *
  4890.  * Returns:
  4891.  * Zero on success, negative errno on failure.
  4892.  */
  4893. int drm_mode_gamma_get_ioctl(struct drm_device *dev,
  4894.                              void *data, struct drm_file *file_priv)
  4895. {
  4896.         struct drm_mode_crtc_lut *crtc_lut = data;
  4897.         struct drm_crtc *crtc;
  4898.         void *r_base, *g_base, *b_base;
  4899.         int size;
  4900.         int ret = 0;
  4901.  
  4902.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4903.                 return -EINVAL;
  4904.  
  4905.         drm_modeset_lock_all(dev);
  4906.         crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
  4907.         if (!crtc) {
  4908.                 ret = -ENOENT;
  4909.                 goto out;
  4910.         }
  4911.  
  4912.         /* memcpy into gamma store */
  4913.         if (crtc_lut->gamma_size != crtc->gamma_size) {
  4914.                 ret = -EINVAL;
  4915.                 goto out;
  4916.         }
  4917.  
  4918.         size = crtc_lut->gamma_size * (sizeof(uint16_t));
  4919.         r_base = crtc->gamma_store;
  4920.         if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
  4921.                 ret = -EFAULT;
  4922.                 goto out;
  4923.         }
  4924.  
  4925.         g_base = r_base + size;
  4926.         if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
  4927.                 ret = -EFAULT;
  4928.                 goto out;
  4929.         }
  4930.  
  4931.         b_base = g_base + size;
  4932.         if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
  4933.                 ret = -EFAULT;
  4934.                 goto out;
  4935.         }
  4936. out:
  4937.         drm_modeset_unlock_all(dev);
  4938.         return ret;
  4939. }
  4940.  
  4941. #endif
  4942.  
  4943.  
  4944. /**
  4945.  * drm_mode_config_reset - call ->reset callbacks
  4946.  * @dev: drm device
  4947.  *
  4948.  * This functions calls all the crtc's, encoder's and connector's ->reset
  4949.  * callback. Drivers can use this in e.g. their driver load or resume code to
  4950.  * reset hardware and software state.
  4951.  */
  4952. void drm_mode_config_reset(struct drm_device *dev)
  4953. {
  4954.         struct drm_crtc *crtc;
  4955.         struct drm_plane *plane;
  4956.         struct drm_encoder *encoder;
  4957.         struct drm_connector *connector;
  4958.  
  4959.         drm_for_each_plane(plane, dev)
  4960.                 if (plane->funcs->reset)
  4961.                         plane->funcs->reset(plane);
  4962.  
  4963.         drm_for_each_crtc(crtc, dev)
  4964.                 if (crtc->funcs->reset)
  4965.                         crtc->funcs->reset(crtc);
  4966.  
  4967.         drm_for_each_encoder(encoder, dev)
  4968.                 if (encoder->funcs->reset)
  4969.                         encoder->funcs->reset(encoder);
  4970.  
  4971.         mutex_lock(&dev->mode_config.mutex);
  4972.         drm_for_each_connector(connector, dev)
  4973.                 if (connector->funcs->reset)
  4974.                         connector->funcs->reset(connector);
  4975.         mutex_unlock(&dev->mode_config.mutex);
  4976. }
  4977. EXPORT_SYMBOL(drm_mode_config_reset);
  4978. /*
  4979.  * Just need to support RGB formats here for compat with code that doesn't
  4980.  * use pixel formats directly yet.
  4981.  */
  4982. void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
  4983.                           int *bpp)
  4984. {
  4985.         switch (format) {
  4986.         case DRM_FORMAT_C8:
  4987.         case DRM_FORMAT_RGB332:
  4988.         case DRM_FORMAT_BGR233:
  4989.                 *depth = 8;
  4990.                 *bpp = 8;
  4991.                 break;
  4992.         case DRM_FORMAT_XRGB1555:
  4993.         case DRM_FORMAT_XBGR1555:
  4994.         case DRM_FORMAT_RGBX5551:
  4995.         case DRM_FORMAT_BGRX5551:
  4996.         case DRM_FORMAT_ARGB1555:
  4997.         case DRM_FORMAT_ABGR1555:
  4998.         case DRM_FORMAT_RGBA5551:
  4999.         case DRM_FORMAT_BGRA5551:
  5000.                 *depth = 15;
  5001.                 *bpp = 16;
  5002.                 break;
  5003.         case DRM_FORMAT_RGB565:
  5004.         case DRM_FORMAT_BGR565:
  5005.                 *depth = 16;
  5006.                 *bpp = 16;
  5007.                 break;
  5008.         case DRM_FORMAT_RGB888:
  5009.         case DRM_FORMAT_BGR888:
  5010.                 *depth = 24;
  5011.                 *bpp = 24;
  5012.                 break;
  5013.         case DRM_FORMAT_XRGB8888:
  5014.         case DRM_FORMAT_XBGR8888:
  5015.         case DRM_FORMAT_RGBX8888:
  5016.         case DRM_FORMAT_BGRX8888:
  5017.                 *depth = 24;
  5018.                 *bpp = 32;
  5019.                 break;
  5020.         case DRM_FORMAT_XRGB2101010:
  5021.         case DRM_FORMAT_XBGR2101010:
  5022.         case DRM_FORMAT_RGBX1010102:
  5023.         case DRM_FORMAT_BGRX1010102:
  5024.         case DRM_FORMAT_ARGB2101010:
  5025.         case DRM_FORMAT_ABGR2101010:
  5026.         case DRM_FORMAT_RGBA1010102:
  5027.         case DRM_FORMAT_BGRA1010102:
  5028.                 *depth = 30;
  5029.                 *bpp = 32;
  5030.                 break;
  5031.         case DRM_FORMAT_ARGB8888:
  5032.         case DRM_FORMAT_ABGR8888:
  5033.         case DRM_FORMAT_RGBA8888:
  5034.         case DRM_FORMAT_BGRA8888:
  5035.                 *depth = 32;
  5036.                 *bpp = 32;
  5037.                 break;
  5038.         default:
  5039.                 DRM_DEBUG_KMS("unsupported pixel format %s\n",
  5040.                               drm_get_format_name(format));
  5041.                 *depth = 0;
  5042.                 *bpp = 0;
  5043.                 break;
  5044.         }
  5045. }
  5046. EXPORT_SYMBOL(drm_fb_get_bpp_depth);
  5047.  
  5048. /**
  5049.  * drm_format_num_planes - get the number of planes for format
  5050.  * @format: pixel format (DRM_FORMAT_*)
  5051.  *
  5052.  * Returns:
  5053.  * The number of planes used by the specified pixel format.
  5054.  */
  5055. int drm_format_num_planes(uint32_t format)
  5056. {
  5057.         switch (format) {
  5058.         case DRM_FORMAT_YUV410:
  5059.         case DRM_FORMAT_YVU410:
  5060.         case DRM_FORMAT_YUV411:
  5061.         case DRM_FORMAT_YVU411:
  5062.         case DRM_FORMAT_YUV420:
  5063.         case DRM_FORMAT_YVU420:
  5064.         case DRM_FORMAT_YUV422:
  5065.         case DRM_FORMAT_YVU422:
  5066.         case DRM_FORMAT_YUV444:
  5067.         case DRM_FORMAT_YVU444:
  5068.                 return 3;
  5069.         case DRM_FORMAT_NV12:
  5070.         case DRM_FORMAT_NV21:
  5071.         case DRM_FORMAT_NV16:
  5072.         case DRM_FORMAT_NV61:
  5073.         case DRM_FORMAT_NV24:
  5074.         case DRM_FORMAT_NV42:
  5075.                 return 2;
  5076.         default:
  5077.                 return 1;
  5078.         }
  5079. }
  5080. EXPORT_SYMBOL(drm_format_num_planes);
  5081.  
  5082. /**
  5083.  * drm_format_plane_cpp - determine the bytes per pixel value
  5084.  * @format: pixel format (DRM_FORMAT_*)
  5085.  * @plane: plane index
  5086.  *
  5087.  * Returns:
  5088.  * The bytes per pixel value for the specified plane.
  5089.  */
  5090. int drm_format_plane_cpp(uint32_t format, int plane)
  5091. {
  5092.         unsigned int depth;
  5093.         int bpp;
  5094.  
  5095.         if (plane >= drm_format_num_planes(format))
  5096.                 return 0;
  5097.  
  5098.         switch (format) {
  5099.         case DRM_FORMAT_YUYV:
  5100.         case DRM_FORMAT_YVYU:
  5101.         case DRM_FORMAT_UYVY:
  5102.         case DRM_FORMAT_VYUY:
  5103.                 return 2;
  5104.         case DRM_FORMAT_NV12:
  5105.         case DRM_FORMAT_NV21:
  5106.         case DRM_FORMAT_NV16:
  5107.         case DRM_FORMAT_NV61:
  5108.         case DRM_FORMAT_NV24:
  5109.         case DRM_FORMAT_NV42:
  5110.                 return plane ? 2 : 1;
  5111.         case DRM_FORMAT_YUV410:
  5112.         case DRM_FORMAT_YVU410:
  5113.         case DRM_FORMAT_YUV411:
  5114.         case DRM_FORMAT_YVU411:
  5115.         case DRM_FORMAT_YUV420:
  5116.         case DRM_FORMAT_YVU420:
  5117.         case DRM_FORMAT_YUV422:
  5118.         case DRM_FORMAT_YVU422:
  5119.         case DRM_FORMAT_YUV444:
  5120.         case DRM_FORMAT_YVU444:
  5121.                 return 1;
  5122.         default:
  5123.                 drm_fb_get_bpp_depth(format, &depth, &bpp);
  5124.                 return bpp >> 3;
  5125.         }
  5126. }
  5127. EXPORT_SYMBOL(drm_format_plane_cpp);
  5128.  
  5129. /**
  5130.  * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
  5131.  * @format: pixel format (DRM_FORMAT_*)
  5132.  *
  5133.  * Returns:
  5134.  * The horizontal chroma subsampling factor for the
  5135.  * specified pixel format.
  5136.  */
  5137. int drm_format_horz_chroma_subsampling(uint32_t format)
  5138. {
  5139.         switch (format) {
  5140.         case DRM_FORMAT_YUV411:
  5141.         case DRM_FORMAT_YVU411:
  5142.         case DRM_FORMAT_YUV410:
  5143.         case DRM_FORMAT_YVU410:
  5144.                 return 4;
  5145.         case DRM_FORMAT_YUYV:
  5146.         case DRM_FORMAT_YVYU:
  5147.         case DRM_FORMAT_UYVY:
  5148.         case DRM_FORMAT_VYUY:
  5149.         case DRM_FORMAT_NV12:
  5150.         case DRM_FORMAT_NV21:
  5151.         case DRM_FORMAT_NV16:
  5152.         case DRM_FORMAT_NV61:
  5153.         case DRM_FORMAT_YUV422:
  5154.         case DRM_FORMAT_YVU422:
  5155.         case DRM_FORMAT_YUV420:
  5156.         case DRM_FORMAT_YVU420:
  5157.                 return 2;
  5158.         default:
  5159.                 return 1;
  5160.         }
  5161. }
  5162. EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
  5163.  
  5164. /**
  5165.  * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
  5166.  * @format: pixel format (DRM_FORMAT_*)
  5167.  *
  5168.  * Returns:
  5169.  * The vertical chroma subsampling factor for the
  5170.  * specified pixel format.
  5171.  */
  5172. int drm_format_vert_chroma_subsampling(uint32_t format)
  5173. {
  5174.         switch (format) {
  5175.         case DRM_FORMAT_YUV410:
  5176.         case DRM_FORMAT_YVU410:
  5177.                 return 4;
  5178.         case DRM_FORMAT_YUV420:
  5179.         case DRM_FORMAT_YVU420:
  5180.         case DRM_FORMAT_NV12:
  5181.         case DRM_FORMAT_NV21:
  5182.                 return 2;
  5183.         default:
  5184.                 return 1;
  5185.         }
  5186. }
  5187. EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
  5188.  
  5189. /**
  5190.  * drm_rotation_simplify() - Try to simplify the rotation
  5191.  * @rotation: Rotation to be simplified
  5192.  * @supported_rotations: Supported rotations
  5193.  *
  5194.  * Attempt to simplify the rotation to a form that is supported.
  5195.  * Eg. if the hardware supports everything except DRM_REFLECT_X
  5196.  * one could call this function like this:
  5197.  *
  5198.  * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
  5199.  *                       BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
  5200.  *                       BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
  5201.  *
  5202.  * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
  5203.  * transforms the hardware supports, this function may not
  5204.  * be able to produce a supported transform, so the caller should
  5205.  * check the result afterwards.
  5206.  */
  5207. unsigned int drm_rotation_simplify(unsigned int rotation,
  5208.                                    unsigned int supported_rotations)
  5209. {
  5210.         if (rotation & ~supported_rotations) {
  5211.                 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
  5212.                 rotation = (rotation & DRM_REFLECT_MASK) |
  5213.                            BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
  5214.         }
  5215.  
  5216.         return rotation;
  5217. }
  5218. EXPORT_SYMBOL(drm_rotation_simplify);
  5219.  
  5220. /**
  5221.  * drm_mode_config_init - initialize DRM mode_configuration structure
  5222.  * @dev: DRM device
  5223.  *
  5224.  * Initialize @dev's mode_config structure, used for tracking the graphics
  5225.  * configuration of @dev.
  5226.  *
  5227.  * Since this initializes the modeset locks, no locking is possible. Which is no
  5228.  * problem, since this should happen single threaded at init time. It is the
  5229.  * driver's problem to ensure this guarantee.
  5230.  *
  5231.  */
  5232. void drm_mode_config_init(struct drm_device *dev)
  5233. {
  5234.         mutex_init(&dev->mode_config.mutex);
  5235.         drm_modeset_lock_init(&dev->mode_config.connection_mutex);
  5236.         mutex_init(&dev->mode_config.idr_mutex);
  5237.         mutex_init(&dev->mode_config.fb_lock);
  5238.         mutex_init(&dev->mode_config.blob_lock);
  5239.         INIT_LIST_HEAD(&dev->mode_config.fb_list);
  5240.         INIT_LIST_HEAD(&dev->mode_config.crtc_list);
  5241.         INIT_LIST_HEAD(&dev->mode_config.connector_list);
  5242.         INIT_LIST_HEAD(&dev->mode_config.encoder_list);
  5243.         INIT_LIST_HEAD(&dev->mode_config.property_list);
  5244.         INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
  5245.         INIT_LIST_HEAD(&dev->mode_config.plane_list);
  5246.         idr_init(&dev->mode_config.crtc_idr);
  5247.         idr_init(&dev->mode_config.tile_idr);
  5248.  
  5249.         drm_modeset_lock_all(dev);
  5250.         drm_mode_create_standard_properties(dev);
  5251.         drm_modeset_unlock_all(dev);
  5252.  
  5253.         /* Just to be sure */
  5254.         dev->mode_config.num_fb = 0;
  5255.         dev->mode_config.num_connector = 0;
  5256.         dev->mode_config.num_crtc = 0;
  5257.         dev->mode_config.num_encoder = 0;
  5258.         dev->mode_config.num_overlay_plane = 0;
  5259.         dev->mode_config.num_total_plane = 0;
  5260. }
  5261. EXPORT_SYMBOL(drm_mode_config_init);
  5262.  
  5263. /**
  5264.  * drm_mode_config_cleanup - free up DRM mode_config info
  5265.  * @dev: DRM device
  5266.  *
  5267.  * Free up all the connectors and CRTCs associated with this DRM device, then
  5268.  * free up the framebuffers and associated buffer objects.
  5269.  *
  5270.  * Note that since this /should/ happen single-threaded at driver/device
  5271.  * teardown time, no locking is required. It's the driver's job to ensure that
  5272.  * this guarantee actually holds true.
  5273.  *
  5274.  * FIXME: cleanup any dangling user buffer objects too
  5275.  */
  5276. void drm_mode_config_cleanup(struct drm_device *dev)
  5277. {
  5278.         struct drm_connector *connector, *ot;
  5279.         struct drm_crtc *crtc, *ct;
  5280.         struct drm_encoder *encoder, *enct;
  5281.         struct drm_framebuffer *fb, *fbt;
  5282.         struct drm_property *property, *pt;
  5283.         struct drm_property_blob *blob, *bt;
  5284.         struct drm_plane *plane, *plt;
  5285.  
  5286.         list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
  5287.                                  head) {
  5288.                 encoder->funcs->destroy(encoder);
  5289.         }
  5290.  
  5291.         list_for_each_entry_safe(connector, ot,
  5292.                                  &dev->mode_config.connector_list, head) {
  5293.                 connector->funcs->destroy(connector);
  5294.         }
  5295.  
  5296.         list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
  5297.                                  head) {
  5298.                 drm_property_destroy(dev, property);
  5299.         }
  5300.  
  5301.         list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
  5302.                                  head_global) {
  5303.                 drm_property_unreference_blob(blob);
  5304.         }
  5305.  
  5306.         /*
  5307.          * Single-threaded teardown context, so it's not required to grab the
  5308.          * fb_lock to protect against concurrent fb_list access. Contrary, it
  5309.          * would actually deadlock with the drm_framebuffer_cleanup function.
  5310.          *
  5311.          * Also, if there are any framebuffers left, that's a driver leak now,
  5312.          * so politely WARN about this.
  5313.          */
  5314.         WARN_ON(!list_empty(&dev->mode_config.fb_list));
  5315.         list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
  5316.                 drm_framebuffer_free(&fb->refcount);
  5317.         }
  5318.  
  5319.         list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
  5320.                                  head) {
  5321.                 plane->funcs->destroy(plane);
  5322.         }
  5323.  
  5324.         list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
  5325.                 crtc->funcs->destroy(crtc);
  5326.         }
  5327.  
  5328.         idr_destroy(&dev->mode_config.tile_idr);
  5329.         idr_destroy(&dev->mode_config.crtc_idr);
  5330.         drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
  5331. }
  5332. EXPORT_SYMBOL(drm_mode_config_cleanup);
  5333.  
  5334. struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
  5335.                                                        unsigned int supported_rotations)
  5336. {
  5337.         static const struct drm_prop_enum_list props[] = {
  5338.                 { DRM_ROTATE_0,   "rotate-0" },
  5339.                 { DRM_ROTATE_90,  "rotate-90" },
  5340.                 { DRM_ROTATE_180, "rotate-180" },
  5341.                 { DRM_ROTATE_270, "rotate-270" },
  5342.                 { DRM_REFLECT_X,  "reflect-x" },
  5343.                 { DRM_REFLECT_Y,  "reflect-y" },
  5344.         };
  5345.  
  5346.         return drm_property_create_bitmask(dev, 0, "rotation",
  5347.                                            props, ARRAY_SIZE(props),
  5348.                                            supported_rotations);
  5349. }
  5350. EXPORT_SYMBOL(drm_mode_create_rotation_property);
  5351.  
  5352. /**
  5353.  * DOC: Tile group
  5354.  *
  5355.  * Tile groups are used to represent tiled monitors with a unique
  5356.  * integer identifier. Tiled monitors using DisplayID v1.3 have
  5357.  * a unique 8-byte handle, we store this in a tile group, so we
  5358.  * have a common identifier for all tiles in a monitor group.
  5359.  */
  5360. static void drm_tile_group_free(struct kref *kref)
  5361. {
  5362.         struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
  5363.         struct drm_device *dev = tg->dev;
  5364.         mutex_lock(&dev->mode_config.idr_mutex);
  5365.         idr_remove(&dev->mode_config.tile_idr, tg->id);
  5366.         mutex_unlock(&dev->mode_config.idr_mutex);
  5367.         kfree(tg);
  5368. }
  5369.  
  5370. /**
  5371.  * drm_mode_put_tile_group - drop a reference to a tile group.
  5372.  * @dev: DRM device
  5373.  * @tg: tile group to drop reference to.
  5374.  *
  5375.  * drop reference to tile group and free if 0.
  5376.  */
  5377. void drm_mode_put_tile_group(struct drm_device *dev,
  5378.                              struct drm_tile_group *tg)
  5379. {
  5380.         kref_put(&tg->refcount, drm_tile_group_free);
  5381. }
  5382.  
  5383. /**
  5384.  * drm_mode_get_tile_group - get a reference to an existing tile group
  5385.  * @dev: DRM device
  5386.  * @topology: 8-bytes unique per monitor.
  5387.  *
  5388.  * Use the unique bytes to get a reference to an existing tile group.
  5389.  *
  5390.  * RETURNS:
  5391.  * tile group or NULL if not found.
  5392.  */
  5393. struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
  5394.                                                char topology[8])
  5395. {
  5396.         struct drm_tile_group *tg;
  5397.         int id;
  5398.         mutex_lock(&dev->mode_config.idr_mutex);
  5399.         idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
  5400.                 if (!memcmp(tg->group_data, topology, 8)) {
  5401.                         if (!kref_get_unless_zero(&tg->refcount))
  5402.                                 tg = NULL;
  5403.                         mutex_unlock(&dev->mode_config.idr_mutex);
  5404.                         return tg;
  5405.                 }
  5406.         }
  5407.         mutex_unlock(&dev->mode_config.idr_mutex);
  5408.         return NULL;
  5409. }
  5410. EXPORT_SYMBOL(drm_mode_get_tile_group);
  5411.  
  5412. /**
  5413.  * drm_mode_create_tile_group - create a tile group from a displayid description
  5414.  * @dev: DRM device
  5415.  * @topology: 8-bytes unique per monitor.
  5416.  *
  5417.  * Create a tile group for the unique monitor, and get a unique
  5418.  * identifier for the tile group.
  5419.  *
  5420.  * RETURNS:
  5421.  * new tile group or error.
  5422.  */
  5423. struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
  5424.                                                   char topology[8])
  5425. {
  5426.         struct drm_tile_group *tg;
  5427.         int ret;
  5428.  
  5429.         tg = kzalloc(sizeof(*tg), GFP_KERNEL);
  5430.         if (!tg)
  5431.                 return ERR_PTR(-ENOMEM);
  5432.  
  5433.         kref_init(&tg->refcount);
  5434.         memcpy(tg->group_data, topology, 8);
  5435.         tg->dev = dev;
  5436.  
  5437.         mutex_lock(&dev->mode_config.idr_mutex);
  5438.         ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
  5439.         if (ret >= 0) {
  5440.                 tg->id = ret;
  5441.         } else {
  5442.                 kfree(tg);
  5443.                 tg = ERR_PTR(ret);
  5444.         }
  5445.  
  5446.         mutex_unlock(&dev->mode_config.idr_mutex);
  5447.         return tg;
  5448. }
  5449. EXPORT_SYMBOL(drm_mode_create_tile_group);
  5450.