Subversion Repositories Kolibri OS

Rev

Rev 6084 | Rev 6296 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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