Subversion Repositories Kolibri OS

Rev

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