Subversion Repositories Kolibri OS

Rev

Rev 6088 | 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->primary->old_fb)
  2541.                         drm_framebuffer_unreference(tmp->primary->old_fb);
  2542.                 tmp->primary->old_fb = NULL;
  2543.         }
  2544.  
  2545.         return ret;
  2546. }
  2547. EXPORT_SYMBOL(drm_mode_set_config_internal);
  2548.  
  2549. /**
  2550.  * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
  2551.  * @mode: mode to query
  2552.  * @hdisplay: hdisplay value to fill in
  2553.  * @vdisplay: vdisplay value to fill in
  2554.  *
  2555.  * The vdisplay value will be doubled if the specified mode is a stereo mode of
  2556.  * the appropriate layout.
  2557.  */
  2558. void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
  2559.                             int *hdisplay, int *vdisplay)
  2560. {
  2561.         struct drm_display_mode adjusted;
  2562.  
  2563.         drm_mode_copy(&adjusted, mode);
  2564.         drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
  2565.         *hdisplay = adjusted.crtc_hdisplay;
  2566.         *vdisplay = adjusted.crtc_vdisplay;
  2567. }
  2568. EXPORT_SYMBOL(drm_crtc_get_hv_timing);
  2569.  
  2570. /**
  2571.  * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
  2572.  *     CRTC viewport
  2573.  * @crtc: CRTC that framebuffer will be displayed on
  2574.  * @x: x panning
  2575.  * @y: y panning
  2576.  * @mode: mode that framebuffer will be displayed under
  2577.  * @fb: framebuffer to check size of
  2578.  */
  2579. int drm_crtc_check_viewport(const struct drm_crtc *crtc,
  2580.                             int x, int y,
  2581.                             const struct drm_display_mode *mode,
  2582.                             const struct drm_framebuffer *fb)
  2583.  
  2584. {
  2585.         int hdisplay, vdisplay;
  2586.  
  2587.         drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  2588.  
  2589.         if (crtc->state &&
  2590.             crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
  2591.                                               BIT(DRM_ROTATE_270)))
  2592.                 swap(hdisplay, vdisplay);
  2593.  
  2594.         return check_src_coords(x << 16, y << 16,
  2595.                                 hdisplay << 16, vdisplay << 16, fb);
  2596. }
  2597. EXPORT_SYMBOL(drm_crtc_check_viewport);
  2598.  
  2599. #if 0
  2600. /**
  2601.  * drm_mode_setcrtc - set CRTC configuration
  2602.  * @dev: drm device for the ioctl
  2603.  * @data: data pointer for the ioctl
  2604.  * @file_priv: drm file for the ioctl call
  2605.  *
  2606.  * Build a new CRTC configuration based on user request.
  2607.  *
  2608.  * Called by the user via ioctl.
  2609.  *
  2610.  * Returns:
  2611.  * Zero on success, negative errno on failure.
  2612.  */
  2613. int drm_mode_setcrtc(struct drm_device *dev, void *data,
  2614.                      struct drm_file *file_priv)
  2615. {
  2616.         struct drm_mode_config *config = &dev->mode_config;
  2617.         struct drm_mode_crtc *crtc_req = data;
  2618.         struct drm_crtc *crtc;
  2619.         struct drm_connector **connector_set = NULL, *connector;
  2620.         struct drm_framebuffer *fb = NULL;
  2621.         struct drm_display_mode *mode = NULL;
  2622.         struct drm_mode_set set;
  2623.         uint32_t __user *set_connectors_ptr;
  2624.         int ret;
  2625.         int i;
  2626.  
  2627.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2628.                 return -EINVAL;
  2629.  
  2630.         /*
  2631.          * Universal plane src offsets are only 16.16, prevent havoc for
  2632.          * drivers using universal plane code internally.
  2633.          */
  2634.         if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
  2635.                 return -ERANGE;
  2636.  
  2637.         drm_modeset_lock_all(dev);
  2638.         crtc = drm_crtc_find(dev, crtc_req->crtc_id);
  2639.         if (!crtc) {
  2640.                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
  2641.                 ret = -ENOENT;
  2642.                 goto out;
  2643.         }
  2644.         DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  2645.  
  2646.         if (crtc_req->mode_valid) {
  2647.                 /* If we have a mode we need a framebuffer. */
  2648.                 /* If we pass -1, set the mode with the currently bound fb */
  2649.                 if (crtc_req->fb_id == -1) {
  2650.                         if (!crtc->primary->fb) {
  2651.                                 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
  2652.                                 ret = -EINVAL;
  2653.                                 goto out;
  2654.                         }
  2655.                         fb = crtc->primary->fb;
  2656.                         /* Make refcounting symmetric with the lookup path. */
  2657.                         drm_framebuffer_reference(fb);
  2658.                 } else {
  2659.                         fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
  2660.                         if (!fb) {
  2661.                                 DRM_DEBUG_KMS("Unknown FB ID%d\n",
  2662.                                                 crtc_req->fb_id);
  2663.                                 ret = -ENOENT;
  2664.                                 goto out;
  2665.                         }
  2666.                 }
  2667.  
  2668.                 mode = drm_mode_create(dev);
  2669.                 if (!mode) {
  2670.                         ret = -ENOMEM;
  2671.                         goto out;
  2672.                 }
  2673.  
  2674.                 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
  2675.                 if (ret) {
  2676.                         DRM_DEBUG_KMS("Invalid mode\n");
  2677.                         goto out;
  2678.                 }
  2679.  
  2680.                 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  2681.  
  2682.                 /*
  2683.                  * Check whether the primary plane supports the fb pixel format.
  2684.                  * Drivers not implementing the universal planes API use a
  2685.                  * default formats list provided by the DRM core which doesn't
  2686.                  * match real hardware capabilities. Skip the check in that
  2687.                  * case.
  2688.                  */
  2689.                 if (!crtc->primary->format_default) {
  2690.                         ret = drm_plane_check_pixel_format(crtc->primary,
  2691.                                                            fb->pixel_format);
  2692.                         if (ret) {
  2693.                                 DRM_DEBUG_KMS("Invalid pixel format %s\n",
  2694.                                         drm_get_format_name(fb->pixel_format));
  2695.                                 goto out;
  2696.                         }
  2697.                 }
  2698.  
  2699.                 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
  2700.                                               mode, fb);
  2701.                 if (ret)
  2702.                         goto out;
  2703.  
  2704.         }
  2705.  
  2706.         if (crtc_req->count_connectors == 0 && mode) {
  2707.                 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
  2708.                 ret = -EINVAL;
  2709.                 goto out;
  2710.         }
  2711.  
  2712.         if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  2713.                 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
  2714.                           crtc_req->count_connectors);
  2715.                 ret = -EINVAL;
  2716.                 goto out;
  2717.         }
  2718.  
  2719.         if (crtc_req->count_connectors > 0) {
  2720.                 u32 out_id;
  2721.  
  2722.                 /* Avoid unbounded kernel memory allocation */
  2723.                 if (crtc_req->count_connectors > config->num_connector) {
  2724.                         ret = -EINVAL;
  2725.                         goto out;
  2726.                 }
  2727.  
  2728.                 connector_set = kmalloc_array(crtc_req->count_connectors,
  2729.                                               sizeof(struct drm_connector *),
  2730.                                               GFP_KERNEL);
  2731.                 if (!connector_set) {
  2732.                         ret = -ENOMEM;
  2733.                         goto out;
  2734.                 }
  2735.  
  2736.                 for (i = 0; i < crtc_req->count_connectors; i++) {
  2737.                         set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
  2738. //           if (get_user(out_id, &set_connectors_ptr[i])) {
  2739. //               ret = -EFAULT;
  2740. //               goto out;
  2741. //           }
  2742.  
  2743.                         connector = drm_connector_find(dev, out_id);
  2744.                         if (!connector) {
  2745.                                 DRM_DEBUG_KMS("Connector id %d unknown\n",
  2746.                                                 out_id);
  2747.                                 ret = -ENOENT;
  2748.                                 goto out;
  2749.                         }
  2750.                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  2751.                                         connector->base.id,
  2752.                                         connector->name);
  2753.  
  2754.                         connector_set[i] = connector;
  2755.                 }
  2756.         }
  2757.  
  2758.         set.crtc = crtc;
  2759.         set.x = crtc_req->x;
  2760.         set.y = crtc_req->y;
  2761.         set.mode = mode;
  2762.         set.connectors = connector_set;
  2763.         set.num_connectors = crtc_req->count_connectors;
  2764.         set.fb = fb;
  2765.         ret = drm_mode_set_config_internal(&set);
  2766.  
  2767. out:
  2768.         if (fb)
  2769.                 drm_framebuffer_unreference(fb);
  2770.  
  2771.         kfree(connector_set);
  2772.         drm_mode_destroy(dev, mode);
  2773.         drm_modeset_unlock_all(dev);
  2774.         return ret;
  2775. }
  2776.  
  2777. static int drm_mode_cursor_common(struct drm_device *dev,
  2778.                                   struct drm_mode_cursor2 *req,
  2779.                                   struct drm_file *file_priv)
  2780. {
  2781.         struct drm_crtc *crtc;
  2782.         int ret = 0;
  2783.  
  2784.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2785.                 return -EINVAL;
  2786.  
  2787.         if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
  2788.                 return -EINVAL;
  2789.  
  2790.         crtc = drm_crtc_find(dev, req->crtc_id);
  2791.         if (!crtc) {
  2792.                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  2793.                 return -ENOENT;
  2794.         }
  2795.  
  2796.         /*
  2797.          * If this crtc has a universal cursor plane, call that plane's update
  2798.          * handler rather than using legacy cursor handlers.
  2799.          */
  2800.         drm_modeset_lock_crtc(crtc, crtc->cursor);
  2801.         if (crtc->cursor) {
  2802.                 ret = drm_mode_cursor_universal(crtc, req, file_priv);
  2803.                 goto out;
  2804.         }
  2805.  
  2806.         if (req->flags & DRM_MODE_CURSOR_BO) {
  2807.                 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
  2808.                         ret = -ENXIO;
  2809.                         goto out;
  2810.                 }
  2811.                 /* Turns off the cursor if handle is 0 */
  2812.                 if (crtc->funcs->cursor_set2)
  2813.                         ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
  2814.                                                       req->width, req->height, req->hot_x, req->hot_y);
  2815.                 else
  2816.                         ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  2817.                                                       req->width, req->height);
  2818.         }
  2819.  
  2820.         if (req->flags & DRM_MODE_CURSOR_MOVE) {
  2821.                 if (crtc->funcs->cursor_move) {
  2822.                         ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  2823.                 } else {
  2824.                         ret = -EFAULT;
  2825.                         goto out;
  2826.                 }
  2827.         }
  2828. out:
  2829.         drm_modeset_unlock_crtc(crtc);
  2830.  
  2831.         return ret;
  2832.  
  2833. }
  2834.  
  2835.  
  2836. /**
  2837.  * drm_mode_cursor_ioctl - set CRTC's cursor configuration
  2838.  * @dev: drm device for the ioctl
  2839.  * @data: data pointer for the ioctl
  2840.  * @file_priv: drm file for the ioctl call
  2841.  *
  2842.  * Set the cursor configuration based on user request.
  2843.  *
  2844.  * Called by the user via ioctl.
  2845.  *
  2846.  * Returns:
  2847.  * Zero on success, negative errno on failure.
  2848.  */
  2849. int drm_mode_cursor_ioctl(struct drm_device *dev,
  2850.                           void *data, struct drm_file *file_priv)
  2851. {
  2852.         struct drm_mode_cursor *req = data;
  2853.         struct drm_mode_cursor2 new_req;
  2854.  
  2855.         memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
  2856.         new_req.hot_x = new_req.hot_y = 0;
  2857.  
  2858.         return drm_mode_cursor_common(dev, &new_req, file_priv);
  2859. }
  2860.  
  2861. /**
  2862.  * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
  2863.  * @dev: drm device for the ioctl
  2864.  * @data: data pointer for the ioctl
  2865.  * @file_priv: drm file for the ioctl call
  2866.  *
  2867.  * Set the cursor configuration based on user request. This implements the 2nd
  2868.  * version of the cursor ioctl, which allows userspace to additionally specify
  2869.  * the hotspot of the pointer.
  2870.  *
  2871.  * Called by the user via ioctl.
  2872.  *
  2873.  * Returns:
  2874.  * Zero on success, negative errno on failure.
  2875.  */
  2876. int drm_mode_cursor2_ioctl(struct drm_device *dev,
  2877.                            void *data, struct drm_file *file_priv)
  2878. {
  2879.         struct drm_mode_cursor2 *req = data;
  2880.  
  2881.         return drm_mode_cursor_common(dev, req, file_priv);
  2882. }
  2883. #endif
  2884.  
  2885. /**
  2886.  * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
  2887.  * @bpp: bits per pixels
  2888.  * @depth: bit depth per pixel
  2889.  *
  2890.  * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
  2891.  * Useful in fbdev emulation code, since that deals in those values.
  2892.  */
  2893. uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
  2894. {
  2895.         uint32_t fmt;
  2896.  
  2897.         switch (bpp) {
  2898.         case 8:
  2899.                 fmt = DRM_FORMAT_C8;
  2900.                 break;
  2901.         case 16:
  2902.                 if (depth == 15)
  2903.                         fmt = DRM_FORMAT_XRGB1555;
  2904.                 else
  2905.                         fmt = DRM_FORMAT_RGB565;
  2906.                 break;
  2907.         case 24:
  2908.                 fmt = DRM_FORMAT_RGB888;
  2909.                 break;
  2910.         case 32:
  2911.                 if (depth == 24)
  2912.                         fmt = DRM_FORMAT_XRGB8888;
  2913.                 else if (depth == 30)
  2914.                         fmt = DRM_FORMAT_XRGB2101010;
  2915.                 else
  2916.                         fmt = DRM_FORMAT_ARGB8888;
  2917.                 break;
  2918.         default:
  2919.                 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
  2920.                 fmt = DRM_FORMAT_XRGB8888;
  2921.                 break;
  2922.         }
  2923.  
  2924.         return fmt;
  2925. }
  2926. EXPORT_SYMBOL(drm_mode_legacy_fb_format);
  2927. #if 0
  2928. /**
  2929.  * drm_mode_addfb - add an FB to the graphics configuration
  2930.  * @dev: drm device for the ioctl
  2931.  * @data: data pointer for the ioctl
  2932.  * @file_priv: drm file for the ioctl call
  2933.  *
  2934.  * Add a new FB to the specified CRTC, given a user request. This is the
  2935.  * original addfb ioctl which only supported RGB formats.
  2936.  *
  2937.  * Called by the user via ioctl.
  2938.  *
  2939.  * Returns:
  2940.  * Zero on success, negative errno on failure.
  2941.  */
  2942. int drm_mode_addfb(struct drm_device *dev,
  2943.                    void *data, struct drm_file *file_priv)
  2944. {
  2945.         struct drm_mode_fb_cmd *or = data;
  2946.         struct drm_mode_fb_cmd2 r = {};
  2947.         int ret;
  2948.  
  2949.         /* convert to new format and call new ioctl */
  2950.         r.fb_id = or->fb_id;
  2951.         r.width = or->width;
  2952.         r.height = or->height;
  2953.         r.pitches[0] = or->pitch;
  2954.         r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
  2955.         r.handles[0] = or->handle;
  2956.  
  2957.         ret = drm_mode_addfb2(dev, &r, file_priv);
  2958.         if (ret)
  2959.                 return ret;
  2960.  
  2961.         or->fb_id = r.fb_id;
  2962.  
  2963.         return 0;
  2964. }
  2965.  
  2966. static int format_check(const struct drm_mode_fb_cmd2 *r)
  2967. {
  2968.         uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
  2969.  
  2970.         switch (format) {
  2971.         case DRM_FORMAT_C8:
  2972.         case DRM_FORMAT_RGB332:
  2973.         case DRM_FORMAT_BGR233:
  2974.         case DRM_FORMAT_XRGB4444:
  2975.         case DRM_FORMAT_XBGR4444:
  2976.         case DRM_FORMAT_RGBX4444:
  2977.         case DRM_FORMAT_BGRX4444:
  2978.         case DRM_FORMAT_ARGB4444:
  2979.         case DRM_FORMAT_ABGR4444:
  2980.         case DRM_FORMAT_RGBA4444:
  2981.         case DRM_FORMAT_BGRA4444:
  2982.         case DRM_FORMAT_XRGB1555:
  2983.         case DRM_FORMAT_XBGR1555:
  2984.         case DRM_FORMAT_RGBX5551:
  2985.         case DRM_FORMAT_BGRX5551:
  2986.         case DRM_FORMAT_ARGB1555:
  2987.         case DRM_FORMAT_ABGR1555:
  2988.         case DRM_FORMAT_RGBA5551:
  2989.         case DRM_FORMAT_BGRA5551:
  2990.         case DRM_FORMAT_RGB565:
  2991.         case DRM_FORMAT_BGR565:
  2992.         case DRM_FORMAT_RGB888:
  2993.         case DRM_FORMAT_BGR888:
  2994.         case DRM_FORMAT_XRGB8888:
  2995.         case DRM_FORMAT_XBGR8888:
  2996.         case DRM_FORMAT_RGBX8888:
  2997.         case DRM_FORMAT_BGRX8888:
  2998.         case DRM_FORMAT_ARGB8888:
  2999.         case DRM_FORMAT_ABGR8888:
  3000.         case DRM_FORMAT_RGBA8888:
  3001.         case DRM_FORMAT_BGRA8888:
  3002.         case DRM_FORMAT_XRGB2101010:
  3003.         case DRM_FORMAT_XBGR2101010:
  3004.         case DRM_FORMAT_RGBX1010102:
  3005.         case DRM_FORMAT_BGRX1010102:
  3006.         case DRM_FORMAT_ARGB2101010:
  3007.         case DRM_FORMAT_ABGR2101010:
  3008.         case DRM_FORMAT_RGBA1010102:
  3009.         case DRM_FORMAT_BGRA1010102:
  3010.         case DRM_FORMAT_YUYV:
  3011.         case DRM_FORMAT_YVYU:
  3012.         case DRM_FORMAT_UYVY:
  3013.         case DRM_FORMAT_VYUY:
  3014.         case DRM_FORMAT_AYUV:
  3015.         case DRM_FORMAT_NV12:
  3016.         case DRM_FORMAT_NV21:
  3017.         case DRM_FORMAT_NV16:
  3018.         case DRM_FORMAT_NV61:
  3019.         case DRM_FORMAT_NV24:
  3020.         case DRM_FORMAT_NV42:
  3021.         case DRM_FORMAT_YUV410:
  3022.         case DRM_FORMAT_YVU410:
  3023.         case DRM_FORMAT_YUV411:
  3024.         case DRM_FORMAT_YVU411:
  3025.         case DRM_FORMAT_YUV420:
  3026.         case DRM_FORMAT_YVU420:
  3027.         case DRM_FORMAT_YUV422:
  3028.         case DRM_FORMAT_YVU422:
  3029.         case DRM_FORMAT_YUV444:
  3030.         case DRM_FORMAT_YVU444:
  3031.                 return 0;
  3032.         default:
  3033.                 DRM_DEBUG_KMS("invalid pixel format %s\n",
  3034.                               drm_get_format_name(r->pixel_format));
  3035.                 return -EINVAL;
  3036.         }
  3037. }
  3038.  
  3039. static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
  3040. {
  3041.         int ret, hsub, vsub, num_planes, i;
  3042.  
  3043.         ret = format_check(r);
  3044.         if (ret) {
  3045.                 DRM_DEBUG_KMS("bad framebuffer format %s\n",
  3046.                               drm_get_format_name(r->pixel_format));
  3047.                 return ret;
  3048.         }
  3049.  
  3050.         hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
  3051.         vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
  3052.         num_planes = drm_format_num_planes(r->pixel_format);
  3053.  
  3054.         if (r->width == 0 || r->width % hsub) {
  3055.                 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
  3056.                 return -EINVAL;
  3057.         }
  3058.  
  3059.         if (r->height == 0 || r->height % vsub) {
  3060.                 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
  3061.                 return -EINVAL;
  3062.         }
  3063.  
  3064.         for (i = 0; i < num_planes; i++) {
  3065.                 unsigned int width = r->width / (i != 0 ? hsub : 1);
  3066.                 unsigned int height = r->height / (i != 0 ? vsub : 1);
  3067.                 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
  3068.  
  3069.                 if (!r->handles[i]) {
  3070.                         DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
  3071.                         return -EINVAL;
  3072.                 }
  3073.  
  3074.                 if ((uint64_t) width * cpp > UINT_MAX)
  3075.                         return -ERANGE;
  3076.  
  3077.                 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
  3078.                         return -ERANGE;
  3079.  
  3080.                 if (r->pitches[i] < width * cpp) {
  3081.                         DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
  3082.                         return -EINVAL;
  3083.                 }
  3084.  
  3085.                 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
  3086.                         DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
  3087.                                       r->modifier[i], i);
  3088.                         return -EINVAL;
  3089.                 }
  3090.  
  3091.                 /* modifier specific checks: */
  3092.                 switch (r->modifier[i]) {
  3093.                 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
  3094.                         /* NOTE: the pitch restriction may be lifted later if it turns
  3095.                          * out that no hw has this restriction:
  3096.                          */
  3097.                         if (r->pixel_format != DRM_FORMAT_NV12 ||
  3098.                                         width % 128 || height % 32 ||
  3099.                                         r->pitches[i] % 128) {
  3100.                                 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
  3101.                                 return -EINVAL;
  3102.                         }
  3103.                         break;
  3104.  
  3105.                 default:
  3106.                         break;
  3107.                 }
  3108.         }
  3109.  
  3110.         for (i = num_planes; i < 4; i++) {
  3111.                 if (r->modifier[i]) {
  3112.                         DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
  3113.                         return -EINVAL;
  3114.                 }
  3115.  
  3116.                 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
  3117.                 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
  3118.                         continue;
  3119.  
  3120.                 if (r->handles[i]) {
  3121.                         DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
  3122.                         return -EINVAL;
  3123.                 }
  3124.  
  3125.                 if (r->pitches[i]) {
  3126.                         DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
  3127.                         return -EINVAL;
  3128.                 }
  3129.  
  3130.                 if (r->offsets[i]) {
  3131.                         DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
  3132.                         return -EINVAL;
  3133.                 }
  3134.         }
  3135.  
  3136.         return 0;
  3137. }
  3138.  
  3139. static struct drm_framebuffer *
  3140. internal_framebuffer_create(struct drm_device *dev,
  3141.                             struct drm_mode_fb_cmd2 *r,
  3142.                             struct drm_file *file_priv)
  3143. {
  3144.         struct drm_mode_config *config = &dev->mode_config;
  3145.         struct drm_framebuffer *fb;
  3146.         int ret;
  3147.  
  3148.         if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
  3149.                 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
  3150.                 return ERR_PTR(-EINVAL);
  3151.         }
  3152.  
  3153.         if ((config->min_width > r->width) || (r->width > config->max_width)) {
  3154.                 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
  3155.                           r->width, config->min_width, config->max_width);
  3156.                 return ERR_PTR(-EINVAL);
  3157.         }
  3158.         if ((config->min_height > r->height) || (r->height > config->max_height)) {
  3159.                 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
  3160.                           r->height, config->min_height, config->max_height);
  3161.                 return ERR_PTR(-EINVAL);
  3162.         }
  3163.  
  3164.         if (r->flags & DRM_MODE_FB_MODIFIERS &&
  3165.             !dev->mode_config.allow_fb_modifiers) {
  3166.                 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
  3167.                 return ERR_PTR(-EINVAL);
  3168.         }
  3169.  
  3170.         ret = framebuffer_check(r);
  3171.         if (ret)
  3172.                 return ERR_PTR(ret);
  3173.  
  3174.         fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
  3175.         if (IS_ERR(fb)) {
  3176.                 DRM_DEBUG_KMS("could not create framebuffer\n");
  3177.                 return fb;
  3178.         }
  3179.  
  3180.         return fb;
  3181. }
  3182.  
  3183. /**
  3184.  * drm_mode_addfb2 - add an FB to the graphics configuration
  3185.  * @dev: drm device for the ioctl
  3186.  * @data: data pointer for the ioctl
  3187.  * @file_priv: drm file for the ioctl call
  3188.  *
  3189.  * Add a new FB to the specified CRTC, given a user request with format. This is
  3190.  * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
  3191.  * and uses fourcc codes as pixel format specifiers.
  3192.  *
  3193.  * Called by the user via ioctl.
  3194.  *
  3195.  * Returns:
  3196.  * Zero on success, negative errno on failure.
  3197.  */
  3198. int drm_mode_addfb2(struct drm_device *dev,
  3199.                     void *data, struct drm_file *file_priv)
  3200. {
  3201.         struct drm_mode_fb_cmd2 *r = data;
  3202.         struct drm_framebuffer *fb;
  3203.  
  3204.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3205.                 return -EINVAL;
  3206.  
  3207.         fb = internal_framebuffer_create(dev, r, file_priv);
  3208.         if (IS_ERR(fb))
  3209.                 return PTR_ERR(fb);
  3210.  
  3211.         /* Transfer ownership to the filp for reaping on close */
  3212.  
  3213.         DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  3214.         mutex_lock(&file_priv->fbs_lock);
  3215.         r->fb_id = fb->base.id;
  3216.         list_add(&fb->filp_head, &file_priv->fbs);
  3217.         mutex_unlock(&file_priv->fbs_lock);
  3218.  
  3219.         return 0;
  3220. }
  3221.  
  3222. /**
  3223.  * drm_mode_rmfb - remove an FB from the configuration
  3224.  * @dev: drm device for the ioctl
  3225.  * @data: data pointer for the ioctl
  3226.  * @file_priv: drm file for the ioctl call
  3227.  *
  3228.  * Remove the FB specified by the user.
  3229.  *
  3230.  * Called by the user via ioctl.
  3231.  *
  3232.  * Returns:
  3233.  * Zero on success, negative errno on failure.
  3234.  */
  3235. int drm_mode_rmfb(struct drm_device *dev,
  3236.                    void *data, struct drm_file *file_priv)
  3237. {
  3238.         struct drm_framebuffer *fb = NULL;
  3239.         struct drm_framebuffer *fbl = NULL;
  3240.         uint32_t *id = data;
  3241.         int found = 0;
  3242.  
  3243.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3244.                 return -EINVAL;
  3245.  
  3246.         mutex_lock(&file_priv->fbs_lock);
  3247.         mutex_lock(&dev->mode_config.fb_lock);
  3248.         fb = __drm_framebuffer_lookup(dev, *id);
  3249.         if (!fb)
  3250.                 goto fail_lookup;
  3251.  
  3252.         list_for_each_entry(fbl, &file_priv->fbs, filp_head)
  3253.                 if (fb == fbl)
  3254.                         found = 1;
  3255.         if (!found)
  3256.                 goto fail_lookup;
  3257.  
  3258.         list_del_init(&fb->filp_head);
  3259.         mutex_unlock(&dev->mode_config.fb_lock);
  3260.         mutex_unlock(&file_priv->fbs_lock);
  3261.  
  3262.         drm_framebuffer_unreference(fb);
  3263.  
  3264.         return 0;
  3265.  
  3266. fail_lookup:
  3267.         mutex_unlock(&dev->mode_config.fb_lock);
  3268.         mutex_unlock(&file_priv->fbs_lock);
  3269.  
  3270.         return -ENOENT;
  3271. }
  3272.  
  3273. /**
  3274.  * drm_mode_getfb - get FB info
  3275.  * @dev: drm device for the ioctl
  3276.  * @data: data pointer for the ioctl
  3277.  * @file_priv: drm file for the ioctl call
  3278.  *
  3279.  * Lookup the FB given its ID and return info about it.
  3280.  *
  3281.  * Called by the user via ioctl.
  3282.  *
  3283.  * Returns:
  3284.  * Zero on success, negative errno on failure.
  3285.  */
  3286. int drm_mode_getfb(struct drm_device *dev,
  3287.                    void *data, struct drm_file *file_priv)
  3288. {
  3289.         struct drm_mode_fb_cmd *r = data;
  3290.         struct drm_framebuffer *fb;
  3291.         int ret;
  3292.  
  3293.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3294.                 return -EINVAL;
  3295.  
  3296.         fb = drm_framebuffer_lookup(dev, r->fb_id);
  3297.         if (!fb)
  3298.                 return -ENOENT;
  3299.  
  3300.         r->height = fb->height;
  3301.         r->width = fb->width;
  3302.         r->depth = fb->depth;
  3303.         r->bpp = fb->bits_per_pixel;
  3304.         r->pitch = fb->pitches[0];
  3305.         if (fb->funcs->create_handle) {
  3306.                 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
  3307.                     drm_is_control_client(file_priv)) {
  3308.                         ret = fb->funcs->create_handle(fb, file_priv,
  3309.                                                        &r->handle);
  3310.                 } else {
  3311.                         /* GET_FB() is an unprivileged ioctl so we must not
  3312.                          * return a buffer-handle to non-master processes! For
  3313.                          * backwards-compatibility reasons, we cannot make
  3314.                          * GET_FB() privileged, so just return an invalid handle
  3315.                          * for non-masters. */
  3316.                         r->handle = 0;
  3317.                         ret = 0;
  3318.                 }
  3319.         } else {
  3320.                 ret = -ENODEV;
  3321.         }
  3322.  
  3323.         drm_framebuffer_unreference(fb);
  3324.  
  3325.         return ret;
  3326. }
  3327.  
  3328. /**
  3329.  * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
  3330.  * @dev: drm device for the ioctl
  3331.  * @data: data pointer for the ioctl
  3332.  * @file_priv: drm file for the ioctl call
  3333.  *
  3334.  * Lookup the FB and flush out the damaged area supplied by userspace as a clip
  3335.  * rectangle list. Generic userspace which does frontbuffer rendering must call
  3336.  * this ioctl to flush out the changes on manual-update display outputs, e.g.
  3337.  * usb display-link, mipi manual update panels or edp panel self refresh modes.
  3338.  *
  3339.  * Modesetting drivers which always update the frontbuffer do not need to
  3340.  * implement the corresponding ->dirty framebuffer callback.
  3341.  *
  3342.  * Called by the user via ioctl.
  3343.  *
  3344.  * Returns:
  3345.  * Zero on success, negative errno on failure.
  3346.  */
  3347. int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
  3348.                            void *data, struct drm_file *file_priv)
  3349. {
  3350.         struct drm_clip_rect __user *clips_ptr;
  3351.         struct drm_clip_rect *clips = NULL;
  3352.         struct drm_mode_fb_dirty_cmd *r = data;
  3353.         struct drm_framebuffer *fb;
  3354.         unsigned flags;
  3355.         int num_clips;
  3356.         int ret;
  3357.  
  3358.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3359.                 return -EINVAL;
  3360.  
  3361.         fb = drm_framebuffer_lookup(dev, r->fb_id);
  3362.         if (!fb)
  3363.                 return -ENOENT;
  3364.  
  3365.         num_clips = r->num_clips;
  3366.         clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
  3367.  
  3368.         if (!num_clips != !clips_ptr) {
  3369.                 ret = -EINVAL;
  3370.                 goto out_err1;
  3371.         }
  3372.  
  3373.         flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
  3374.  
  3375.         /* If userspace annotates copy, clips must come in pairs */
  3376.         if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
  3377.                 ret = -EINVAL;
  3378.                 goto out_err1;
  3379.         }
  3380.  
  3381.         if (num_clips && clips_ptr) {
  3382.                 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
  3383.                         ret = -EINVAL;
  3384.                         goto out_err1;
  3385.                 }
  3386.                 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
  3387.                 if (!clips) {
  3388.                         ret = -ENOMEM;
  3389.                         goto out_err1;
  3390.                 }
  3391.  
  3392.                 ret = copy_from_user(clips, clips_ptr,
  3393.                                      num_clips * sizeof(*clips));
  3394.                 if (ret) {
  3395.                         ret = -EFAULT;
  3396.                         goto out_err2;
  3397.                 }
  3398.         }
  3399.  
  3400.         if (fb->funcs->dirty) {
  3401.                 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
  3402.                                        clips, num_clips);
  3403.         } else {
  3404.                 ret = -ENOSYS;
  3405.         }
  3406.  
  3407. out_err2:
  3408.         kfree(clips);
  3409. out_err1:
  3410.         drm_framebuffer_unreference(fb);
  3411.  
  3412.         return ret;
  3413. }
  3414.  
  3415.  
  3416. /**
  3417.  * drm_fb_release - remove and free the FBs on this file
  3418.  * @priv: drm file for the ioctl
  3419.  *
  3420.  * Destroy all the FBs associated with @filp.
  3421.  *
  3422.  * Called by the user via ioctl.
  3423.  *
  3424.  * Returns:
  3425.  * Zero on success, negative errno on failure.
  3426.  */
  3427. void drm_fb_release(struct drm_file *priv)
  3428. {
  3429.         struct drm_framebuffer *fb, *tfb;
  3430.  
  3431.         /*
  3432.          * When the file gets released that means no one else can access the fb
  3433.          * list any more, so no need to grab fpriv->fbs_lock. And we need to
  3434.          * avoid upsetting lockdep since the universal cursor code adds a
  3435.          * framebuffer while holding mutex locks.
  3436.          *
  3437.          * Note that a real deadlock between fpriv->fbs_lock and the modeset
  3438.          * locks is impossible here since no one else but this function can get
  3439.          * at it any more.
  3440.          */
  3441.         list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
  3442.                 list_del_init(&fb->filp_head);
  3443.  
  3444.                 /* This drops the fpriv->fbs reference. */
  3445.                 drm_framebuffer_unreference(fb);
  3446.         }
  3447. }
  3448. #endif
  3449.  
  3450.  
  3451. /**
  3452.  * drm_property_create - create a new property type
  3453.  * @dev: drm device
  3454.  * @flags: flags specifying the property type
  3455.  * @name: name of the property
  3456.  * @num_values: number of pre-defined values
  3457.  *
  3458.  * This creates a new generic drm property which can then be attached to a drm
  3459.  * object with drm_object_attach_property. The returned property object must be
  3460.  * freed with drm_property_destroy.
  3461.  *
  3462.  * Note that the DRM core keeps a per-device list of properties and that, if
  3463.  * drm_mode_config_cleanup() is called, it will destroy all properties created
  3464.  * by the driver.
  3465.  *
  3466.  * Returns:
  3467.  * A pointer to the newly created property on success, NULL on failure.
  3468.  */
  3469. struct drm_property *drm_property_create(struct drm_device *dev, int flags,
  3470.                                          const char *name, int num_values)
  3471. {
  3472.         struct drm_property *property = NULL;
  3473.         int ret;
  3474.  
  3475.         property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
  3476.         if (!property)
  3477.                 return NULL;
  3478.  
  3479.         property->dev = dev;
  3480.  
  3481.         if (num_values) {
  3482.                 property->values = kcalloc(num_values, sizeof(uint64_t),
  3483.                                            GFP_KERNEL);
  3484.                 if (!property->values)
  3485.                         goto fail;
  3486.         }
  3487.  
  3488.         ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
  3489.         if (ret)
  3490.                 goto fail;
  3491.  
  3492.         property->flags = flags;
  3493.         property->num_values = num_values;
  3494.         INIT_LIST_HEAD(&property->enum_list);
  3495.  
  3496.         if (name) {
  3497.                 strncpy(property->name, name, DRM_PROP_NAME_LEN);
  3498.                 property->name[DRM_PROP_NAME_LEN-1] = '\0';
  3499.         }
  3500.  
  3501.         list_add_tail(&property->head, &dev->mode_config.property_list);
  3502.  
  3503.         WARN_ON(!drm_property_type_valid(property));
  3504.  
  3505.         return property;
  3506. fail:
  3507.         kfree(property->values);
  3508.         kfree(property);
  3509.         return NULL;
  3510. }
  3511. EXPORT_SYMBOL(drm_property_create);
  3512.  
  3513. /**
  3514.  * drm_property_create_enum - create a new enumeration property type
  3515.  * @dev: drm device
  3516.  * @flags: flags specifying the property type
  3517.  * @name: name of the property
  3518.  * @props: enumeration lists with property values
  3519.  * @num_values: number of pre-defined values
  3520.  *
  3521.  * This creates a new generic drm property which can then be attached to a drm
  3522.  * object with drm_object_attach_property. The returned property object must be
  3523.  * freed with drm_property_destroy.
  3524.  *
  3525.  * Userspace is only allowed to set one of the predefined values for enumeration
  3526.  * properties.
  3527.  *
  3528.  * Returns:
  3529.  * A pointer to the newly created property on success, NULL on failure.
  3530.  */
  3531. struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
  3532.                                          const char *name,
  3533.                                          const struct drm_prop_enum_list *props,
  3534.                                          int num_values)
  3535. {
  3536.         struct drm_property *property;
  3537.         int i, ret;
  3538.  
  3539.         flags |= DRM_MODE_PROP_ENUM;
  3540.  
  3541.         property = drm_property_create(dev, flags, name, num_values);
  3542.         if (!property)
  3543.                 return NULL;
  3544.  
  3545.         for (i = 0; i < num_values; i++) {
  3546.                 ret = drm_property_add_enum(property, i,
  3547.                                       props[i].type,
  3548.                                       props[i].name);
  3549.                 if (ret) {
  3550.                         drm_property_destroy(dev, property);
  3551.                         return NULL;
  3552.                 }
  3553.         }
  3554.  
  3555.         return property;
  3556. }
  3557. EXPORT_SYMBOL(drm_property_create_enum);
  3558.  
  3559. /**
  3560.  * drm_property_create_bitmask - create a new bitmask property type
  3561.  * @dev: drm device
  3562.  * @flags: flags specifying the property type
  3563.  * @name: name of the property
  3564.  * @props: enumeration lists with property bitflags
  3565.  * @num_props: size of the @props array
  3566.  * @supported_bits: bitmask of all supported enumeration values
  3567.  *
  3568.  * This creates a new bitmask drm property which can then be attached to a drm
  3569.  * object with drm_object_attach_property. The returned property object must be
  3570.  * freed with drm_property_destroy.
  3571.  *
  3572.  * Compared to plain enumeration properties userspace is allowed to set any
  3573.  * or'ed together combination of the predefined property bitflag values
  3574.  *
  3575.  * Returns:
  3576.  * A pointer to the newly created property on success, NULL on failure.
  3577.  */
  3578. struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
  3579.                                          int flags, const char *name,
  3580.                                          const struct drm_prop_enum_list *props,
  3581.                                          int num_props,
  3582.                                          uint64_t supported_bits)
  3583. {
  3584.         struct drm_property *property;
  3585.         int i, ret, index = 0;
  3586.         int num_values = hweight64(supported_bits);
  3587.  
  3588.         flags |= DRM_MODE_PROP_BITMASK;
  3589.  
  3590.         property = drm_property_create(dev, flags, name, num_values);
  3591.         if (!property)
  3592.                 return NULL;
  3593.         for (i = 0; i < num_props; i++) {
  3594.                 if (!(supported_bits & (1ULL << props[i].type)))
  3595.                         continue;
  3596.  
  3597.                 if (WARN_ON(index >= num_values)) {
  3598.                         drm_property_destroy(dev, property);
  3599.                         return NULL;
  3600.                 }
  3601.  
  3602.                 ret = drm_property_add_enum(property, index++,
  3603.                                       props[i].type,
  3604.                                       props[i].name);
  3605.                 if (ret) {
  3606.                         drm_property_destroy(dev, property);
  3607.                         return NULL;
  3608.                 }
  3609.         }
  3610.  
  3611.         return property;
  3612. }
  3613. EXPORT_SYMBOL(drm_property_create_bitmask);
  3614.  
  3615. static struct drm_property *property_create_range(struct drm_device *dev,
  3616.                                          int flags, const char *name,
  3617.                                          uint64_t min, uint64_t max)
  3618. {
  3619.         struct drm_property *property;
  3620.  
  3621.         property = drm_property_create(dev, flags, name, 2);
  3622.         if (!property)
  3623.                 return NULL;
  3624.  
  3625.         property->values[0] = min;
  3626.         property->values[1] = max;
  3627.  
  3628.         return property;
  3629. }
  3630.  
  3631. /**
  3632.  * drm_property_create_range - create a new unsigned ranged property type
  3633.  * @dev: drm device
  3634.  * @flags: flags specifying the property type
  3635.  * @name: name of the property
  3636.  * @min: minimum value of the property
  3637.  * @max: maximum value of the property
  3638.  *
  3639.  * This creates a new generic drm property which can then be attached to a drm
  3640.  * object with drm_object_attach_property. The returned property object must be
  3641.  * freed with drm_property_destroy.
  3642.  *
  3643.  * Userspace is allowed to set any unsigned integer value in the (min, max)
  3644.  * range inclusive.
  3645.  *
  3646.  * Returns:
  3647.  * A pointer to the newly created property on success, NULL on failure.
  3648.  */
  3649. struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
  3650.                                          const char *name,
  3651.                                          uint64_t min, uint64_t max)
  3652. {
  3653.         return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
  3654.                         name, min, max);
  3655. }
  3656. EXPORT_SYMBOL(drm_property_create_range);
  3657.  
  3658. /**
  3659.  * drm_property_create_signed_range - create a new signed ranged property type
  3660.  * @dev: drm device
  3661.  * @flags: flags specifying the property type
  3662.  * @name: name of the property
  3663.  * @min: minimum value of the property
  3664.  * @max: maximum value of the property
  3665.  *
  3666.  * This creates a new generic drm property which can then be attached to a drm
  3667.  * object with drm_object_attach_property. The returned property object must be
  3668.  * freed with drm_property_destroy.
  3669.  *
  3670.  * Userspace is allowed to set any signed integer value in the (min, max)
  3671.  * range inclusive.
  3672.  *
  3673.  * Returns:
  3674.  * A pointer to the newly created property on success, NULL on failure.
  3675.  */
  3676. struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
  3677.                                          int flags, const char *name,
  3678.                                          int64_t min, int64_t max)
  3679. {
  3680.         return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
  3681.                         name, I642U64(min), I642U64(max));
  3682. }
  3683. EXPORT_SYMBOL(drm_property_create_signed_range);
  3684.  
  3685. /**
  3686.  * drm_property_create_object - create a new object property type
  3687.  * @dev: drm device
  3688.  * @flags: flags specifying the property type
  3689.  * @name: name of the property
  3690.  * @type: object type from DRM_MODE_OBJECT_* defines
  3691.  *
  3692.  * This creates a new generic drm property which can then be attached to a drm
  3693.  * object with drm_object_attach_property. The returned property object must be
  3694.  * freed with drm_property_destroy.
  3695.  *
  3696.  * Userspace is only allowed to set this to any property value of the given
  3697.  * @type. Only useful for atomic properties, which is enforced.
  3698.  *
  3699.  * Returns:
  3700.  * A pointer to the newly created property on success, NULL on failure.
  3701.  */
  3702. struct drm_property *drm_property_create_object(struct drm_device *dev,
  3703.                                          int flags, const char *name, uint32_t type)
  3704. {
  3705.         struct drm_property *property;
  3706.  
  3707.         flags |= DRM_MODE_PROP_OBJECT;
  3708.  
  3709.         if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
  3710.                 return NULL;
  3711.  
  3712.         property = drm_property_create(dev, flags, name, 1);
  3713.         if (!property)
  3714.                 return NULL;
  3715.  
  3716.         property->values[0] = type;
  3717.  
  3718.         return property;
  3719. }
  3720. EXPORT_SYMBOL(drm_property_create_object);
  3721.  
  3722. /**
  3723.  * drm_property_create_bool - create a new boolean property type
  3724.  * @dev: drm device
  3725.  * @flags: flags specifying the property type
  3726.  * @name: name of the property
  3727.  *
  3728.  * This creates a new generic drm property which can then be attached to a drm
  3729.  * object with drm_object_attach_property. The returned property object must be
  3730.  * freed with drm_property_destroy.
  3731.  *
  3732.  * This is implemented as a ranged property with only {0, 1} as valid values.
  3733.  *
  3734.  * Returns:
  3735.  * A pointer to the newly created property on success, NULL on failure.
  3736.  */
  3737. struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
  3738.                                          const char *name)
  3739. {
  3740.         return drm_property_create_range(dev, flags, name, 0, 1);
  3741. }
  3742. EXPORT_SYMBOL(drm_property_create_bool);
  3743.  
  3744. /**
  3745.  * drm_property_add_enum - add a possible value to an enumeration property
  3746.  * @property: enumeration property to change
  3747.  * @index: index of the new enumeration
  3748.  * @value: value of the new enumeration
  3749.  * @name: symbolic name of the new enumeration
  3750.  *
  3751.  * This functions adds enumerations to a property.
  3752.  *
  3753.  * It's use is deprecated, drivers should use one of the more specific helpers
  3754.  * to directly create the property with all enumerations already attached.
  3755.  *
  3756.  * Returns:
  3757.  * Zero on success, error code on failure.
  3758.  */
  3759. int drm_property_add_enum(struct drm_property *property, int index,
  3760.                           uint64_t value, const char *name)
  3761. {
  3762.         struct drm_property_enum *prop_enum;
  3763.  
  3764.         if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3765.                         drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
  3766.                 return -EINVAL;
  3767.  
  3768.         /*
  3769.          * Bitmask enum properties have the additional constraint of values
  3770.          * from 0 to 63
  3771.          */
  3772.         if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
  3773.                         (value > 63))
  3774.                 return -EINVAL;
  3775.  
  3776.         if (!list_empty(&property->enum_list)) {
  3777.                 list_for_each_entry(prop_enum, &property->enum_list, head) {
  3778.                         if (prop_enum->value == value) {
  3779.                                 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  3780.                                 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  3781.                                 return 0;
  3782.                         }
  3783.                 }
  3784.         }
  3785.  
  3786.         prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
  3787.         if (!prop_enum)
  3788.                 return -ENOMEM;
  3789.  
  3790.         strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  3791.         prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  3792.         prop_enum->value = value;
  3793.  
  3794.         property->values[index] = value;
  3795.         list_add_tail(&prop_enum->head, &property->enum_list);
  3796.         return 0;
  3797. }
  3798. EXPORT_SYMBOL(drm_property_add_enum);
  3799.  
  3800. /**
  3801.  * drm_property_destroy - destroy a drm property
  3802.  * @dev: drm device
  3803.  * @property: property to destry
  3804.  *
  3805.  * This function frees a property including any attached resources like
  3806.  * enumeration values.
  3807.  */
  3808. void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
  3809. {
  3810.         struct drm_property_enum *prop_enum, *pt;
  3811.  
  3812.         list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
  3813.                 list_del(&prop_enum->head);
  3814.                 kfree(prop_enum);
  3815.         }
  3816.  
  3817.         if (property->num_values)
  3818.                 kfree(property->values);
  3819.         drm_mode_object_put(dev, &property->base);
  3820.         list_del(&property->head);
  3821.         kfree(property);
  3822. }
  3823. EXPORT_SYMBOL(drm_property_destroy);
  3824.  
  3825. /**
  3826.  * drm_object_attach_property - attach a property to a modeset object
  3827.  * @obj: drm modeset object
  3828.  * @property: property to attach
  3829.  * @init_val: initial value of the property
  3830.  *
  3831.  * This attaches the given property to the modeset object with the given initial
  3832.  * value. Currently this function cannot fail since the properties are stored in
  3833.  * a statically sized array.
  3834.  */
  3835. void drm_object_attach_property(struct drm_mode_object *obj,
  3836.                                 struct drm_property *property,
  3837.                                 uint64_t init_val)
  3838. {
  3839.         int count = obj->properties->count;
  3840.  
  3841.         if (count == DRM_OBJECT_MAX_PROPERTY) {
  3842.                 WARN(1, "Failed to attach object property (type: 0x%x). Please "
  3843.                         "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
  3844.                         "you see this message on the same object type.\n",
  3845.                         obj->type);
  3846.                 return;
  3847.         }
  3848.  
  3849.         obj->properties->properties[count] = property;
  3850.         obj->properties->values[count] = init_val;
  3851.         obj->properties->count++;
  3852.         if (property->flags & DRM_MODE_PROP_ATOMIC)
  3853.                 obj->properties->atomic_count++;
  3854. }
  3855. EXPORT_SYMBOL(drm_object_attach_property);
  3856.  
  3857. /**
  3858.  * drm_object_property_set_value - set the value of a property
  3859.  * @obj: drm mode object to set property value for
  3860.  * @property: property to set
  3861.  * @val: value the property should be set to
  3862.  *
  3863.  * This functions sets a given property on a given object. This function only
  3864.  * changes the software state of the property, it does not call into the
  3865.  * driver's ->set_property callback.
  3866.  *
  3867.  * Returns:
  3868.  * Zero on success, error code on failure.
  3869.  */
  3870. int drm_object_property_set_value(struct drm_mode_object *obj,
  3871.                                   struct drm_property *property, uint64_t val)
  3872. {
  3873.         int i;
  3874.  
  3875.         for (i = 0; i < obj->properties->count; i++) {
  3876.                 if (obj->properties->properties[i] == property) {
  3877.                         obj->properties->values[i] = val;
  3878.                         return 0;
  3879.                 }
  3880.         }
  3881.  
  3882.         return -EINVAL;
  3883. }
  3884. EXPORT_SYMBOL(drm_object_property_set_value);
  3885.  
  3886. /**
  3887.  * drm_object_property_get_value - retrieve the value of a property
  3888.  * @obj: drm mode object to get property value from
  3889.  * @property: property to retrieve
  3890.  * @val: storage for the property value
  3891.  *
  3892.  * This function retrieves the softare state of the given property for the given
  3893.  * property. Since there is no driver callback to retrieve the current property
  3894.  * value this might be out of sync with the hardware, depending upon the driver
  3895.  * and property.
  3896.  *
  3897.  * Returns:
  3898.  * Zero on success, error code on failure.
  3899.  */
  3900. int drm_object_property_get_value(struct drm_mode_object *obj,
  3901.                                   struct drm_property *property, uint64_t *val)
  3902. {
  3903.         int i;
  3904.  
  3905.         /* read-only properties bypass atomic mechanism and still store
  3906.          * their value in obj->properties->values[].. mostly to avoid
  3907.          * having to deal w/ EDID and similar props in atomic paths:
  3908.          */
  3909.         if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
  3910.                         !(property->flags & DRM_MODE_PROP_IMMUTABLE))
  3911.                 return drm_atomic_get_property(obj, property, val);
  3912.  
  3913.         for (i = 0; i < obj->properties->count; i++) {
  3914.                 if (obj->properties->properties[i] == property) {
  3915.                         *val = obj->properties->values[i];
  3916.                         return 0;
  3917.                 }
  3918.         }
  3919.  
  3920.         return -EINVAL;
  3921. }
  3922. EXPORT_SYMBOL(drm_object_property_get_value);
  3923.  
  3924. #if 0
  3925. /**
  3926.  * drm_mode_getproperty_ioctl - get the property metadata
  3927.  * @dev: DRM device
  3928.  * @data: ioctl data
  3929.  * @file_priv: DRM file info
  3930.  *
  3931.  * This function retrieves the metadata for a given property, like the different
  3932.  * possible values for an enum property or the limits for a range property.
  3933.  *
  3934.  * Blob properties are special
  3935.  *
  3936.  * Called by the user via ioctl.
  3937.  *
  3938.  * Returns:
  3939.  * Zero on success, negative errno on failure.
  3940.  */
  3941. int drm_mode_getproperty_ioctl(struct drm_device *dev,
  3942.                                void *data, struct drm_file *file_priv)
  3943. {
  3944.         struct drm_mode_get_property *out_resp = data;
  3945.         struct drm_property *property;
  3946.         int enum_count = 0;
  3947.         int value_count = 0;
  3948.         int ret = 0, i;
  3949.         int copied;
  3950.         struct drm_property_enum *prop_enum;
  3951.         struct drm_mode_property_enum __user *enum_ptr;
  3952.         uint64_t __user *values_ptr;
  3953.  
  3954.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3955.                 return -EINVAL;
  3956.  
  3957.         drm_modeset_lock_all(dev);
  3958.         property = drm_property_find(dev, out_resp->prop_id);
  3959.         if (!property) {
  3960.                 ret = -ENOENT;
  3961.                 goto done;
  3962.         }
  3963.  
  3964.         if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3965.                         drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  3966.                 list_for_each_entry(prop_enum, &property->enum_list, head)
  3967.                         enum_count++;
  3968.         }
  3969.  
  3970.         value_count = property->num_values;
  3971.  
  3972.         strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
  3973.         out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
  3974.         out_resp->flags = property->flags;
  3975.  
  3976.         if ((out_resp->count_values >= value_count) && value_count) {
  3977.                 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
  3978.                 for (i = 0; i < value_count; i++) {
  3979.                         if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
  3980.                                 ret = -EFAULT;
  3981.                                 goto done;
  3982.                         }
  3983.                 }
  3984.         }
  3985.         out_resp->count_values = value_count;
  3986.  
  3987.         if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3988.                         drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  3989.                 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
  3990.                         copied = 0;
  3991.                         enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
  3992.                         list_for_each_entry(prop_enum, &property->enum_list, head) {
  3993.  
  3994.                                 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
  3995.                                         ret = -EFAULT;
  3996.                                         goto done;
  3997.                                 }
  3998.  
  3999.                                 if (copy_to_user(&enum_ptr[copied].name,
  4000.                                                  &prop_enum->name, DRM_PROP_NAME_LEN)) {
  4001.                                         ret = -EFAULT;
  4002.                                         goto done;
  4003.                                 }
  4004.                                 copied++;
  4005.                         }
  4006.                 }
  4007.                 out_resp->count_enum_blobs = enum_count;
  4008.         }
  4009.  
  4010.         /*
  4011.          * NOTE: The idea seems to have been to use this to read all the blob
  4012.          * property values. But nothing ever added them to the corresponding
  4013.          * list, userspace always used the special-purpose get_blob ioctl to
  4014.          * read the value for a blob property. It also doesn't make a lot of
  4015.          * sense to return values here when everything else is just metadata for
  4016.          * the property itself.
  4017.          */
  4018.         if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
  4019.                 out_resp->count_enum_blobs = 0;
  4020. done:
  4021.         drm_modeset_unlock_all(dev);
  4022.         return ret;
  4023. }
  4024. #endif
  4025.  
  4026. /**
  4027.  * drm_property_create_blob - Create new blob property
  4028.  *
  4029.  * Creates a new blob property for a specified DRM device, optionally
  4030.  * copying data.
  4031.  *
  4032.  * @dev: DRM device to create property for
  4033.  * @length: Length to allocate for blob data
  4034.  * @data: If specified, copies data into blob
  4035.  *
  4036.  * Returns:
  4037.  * New blob property with a single reference on success, or an ERR_PTR
  4038.  * value on failure.
  4039.  */
  4040. struct drm_property_blob *
  4041. drm_property_create_blob(struct drm_device *dev, size_t length,
  4042.                          const void *data)
  4043. {
  4044.         struct drm_property_blob *blob;
  4045.         int ret;
  4046.  
  4047.         if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
  4048.                 return ERR_PTR(-EINVAL);
  4049.  
  4050.         blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
  4051.         if (!blob)
  4052.                 return ERR_PTR(-ENOMEM);
  4053.  
  4054.         /* This must be explicitly initialised, so we can safely call list_del
  4055.          * on it in the removal handler, even if it isn't in a file list. */
  4056.         INIT_LIST_HEAD(&blob->head_file);
  4057.         blob->length = length;
  4058.         blob->dev = dev;
  4059.  
  4060.         if (data)
  4061.                 memcpy(blob->data, data, length);
  4062.  
  4063.         mutex_lock(&dev->mode_config.blob_lock);
  4064.  
  4065.         ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
  4066.         if (ret) {
  4067.                 kfree(blob);
  4068.                 mutex_unlock(&dev->mode_config.blob_lock);
  4069.                 return ERR_PTR(-EINVAL);
  4070.         }
  4071.  
  4072.         kref_init(&blob->refcount);
  4073.  
  4074.         list_add_tail(&blob->head_global,
  4075.                       &dev->mode_config.property_blob_list);
  4076.  
  4077.         mutex_unlock(&dev->mode_config.blob_lock);
  4078.  
  4079.         return blob;
  4080. }
  4081. EXPORT_SYMBOL(drm_property_create_blob);
  4082.  
  4083. /**
  4084.  * drm_property_free_blob - Blob property destructor
  4085.  *
  4086.  * Internal free function for blob properties; must not be used directly.
  4087.  *
  4088.  * @kref: Reference
  4089.  */
  4090. static void drm_property_free_blob(struct kref *kref)
  4091. {
  4092.         struct drm_property_blob *blob =
  4093.                 container_of(kref, struct drm_property_blob, refcount);
  4094.  
  4095.         WARN_ON(!mutex_is_locked(&blob->dev->mode_config.blob_lock));
  4096.  
  4097.         list_del(&blob->head_global);
  4098.         list_del(&blob->head_file);
  4099.         drm_mode_object_put(blob->dev, &blob->base);
  4100.  
  4101.         kfree(blob);
  4102. }
  4103.  
  4104. /**
  4105.  * drm_property_unreference_blob - Unreference a blob property
  4106.  *
  4107.  * Drop a reference on a blob property. May free the object.
  4108.  *
  4109.  * @blob: Pointer to blob property
  4110.  */
  4111. void drm_property_unreference_blob(struct drm_property_blob *blob)
  4112. {
  4113.         struct drm_device *dev;
  4114.  
  4115.         if (!blob)
  4116.                 return;
  4117.  
  4118.         dev = blob->dev;
  4119.  
  4120.         DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
  4121.  
  4122.         if (kref_put_mutex(&blob->refcount, drm_property_free_blob,
  4123.                            &dev->mode_config.blob_lock))
  4124.                 mutex_unlock(&dev->mode_config.blob_lock);
  4125.         else
  4126.                 might_lock(&dev->mode_config.blob_lock);
  4127. }
  4128. EXPORT_SYMBOL(drm_property_unreference_blob);
  4129.  
  4130. /**
  4131.  * drm_property_unreference_blob_locked - Unreference a blob property with blob_lock held
  4132.  *
  4133.  * Drop a reference on a blob property. May free the object. This must be
  4134.  * called with blob_lock held.
  4135.  *
  4136.  * @blob: Pointer to blob property
  4137.  */
  4138. static void drm_property_unreference_blob_locked(struct drm_property_blob *blob)
  4139. {
  4140.         if (!blob)
  4141.                 return;
  4142.  
  4143.         DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
  4144.  
  4145.         kref_put(&blob->refcount, drm_property_free_blob);
  4146. }
  4147.  
  4148. /**
  4149.  * drm_property_destroy_user_blobs - destroy all blobs created by this client
  4150.  * @dev:       DRM device
  4151.  * @file_priv: destroy all blobs owned by this file handle
  4152.  */
  4153. void drm_property_destroy_user_blobs(struct drm_device *dev,
  4154.                                      struct drm_file *file_priv)
  4155. {
  4156.         struct drm_property_blob *blob, *bt;
  4157.  
  4158.         mutex_lock(&dev->mode_config.blob_lock);
  4159.  
  4160.         list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
  4161.                 list_del_init(&blob->head_file);
  4162.                 drm_property_unreference_blob_locked(blob);
  4163.         }
  4164.  
  4165.         mutex_unlock(&dev->mode_config.blob_lock);
  4166. }
  4167.  
  4168. /**
  4169.  * drm_property_reference_blob - Take a reference on an existing property
  4170.  *
  4171.  * Take a new reference on an existing blob property.
  4172.  *
  4173.  * @blob: Pointer to blob property
  4174.  */
  4175. struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
  4176. {
  4177.         DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
  4178.         kref_get(&blob->refcount);
  4179.         return blob;
  4180. }
  4181. EXPORT_SYMBOL(drm_property_reference_blob);
  4182.  
  4183. /*
  4184.  * Like drm_property_lookup_blob, but does not return an additional reference.
  4185.  * Must be called with blob_lock held.
  4186.  */
  4187. static struct drm_property_blob *__drm_property_lookup_blob(struct drm_device *dev,
  4188.                                                             uint32_t id)
  4189. {
  4190.         struct drm_mode_object *obj = NULL;
  4191.         struct drm_property_blob *blob;
  4192.  
  4193.         WARN_ON(!mutex_is_locked(&dev->mode_config.blob_lock));
  4194.  
  4195.         mutex_lock(&dev->mode_config.idr_mutex);
  4196.         obj = idr_find(&dev->mode_config.crtc_idr, id);
  4197.         if (!obj || (obj->type != DRM_MODE_OBJECT_BLOB) || (obj->id != id))
  4198.                 blob = NULL;
  4199.         else
  4200.                 blob = obj_to_blob(obj);
  4201.         mutex_unlock(&dev->mode_config.idr_mutex);
  4202.  
  4203.         return blob;
  4204. }
  4205.  
  4206. /**
  4207.  * drm_property_lookup_blob - look up a blob property and take a reference
  4208.  * @dev: drm device
  4209.  * @id: id of the blob property
  4210.  *
  4211.  * If successful, this takes an additional reference to the blob property.
  4212.  * callers need to make sure to eventually unreference the returned property
  4213.  * again, using @drm_property_unreference_blob.
  4214.  */
  4215. struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
  4216.                                                    uint32_t id)
  4217. {
  4218.         struct drm_property_blob *blob;
  4219.  
  4220.         mutex_lock(&dev->mode_config.blob_lock);
  4221.         blob = __drm_property_lookup_blob(dev, id);
  4222.         if (blob) {
  4223.                 if (!kref_get_unless_zero(&blob->refcount))
  4224.                         blob = NULL;
  4225.         }
  4226.         mutex_unlock(&dev->mode_config.blob_lock);
  4227.  
  4228.         return blob;
  4229. }
  4230. EXPORT_SYMBOL(drm_property_lookup_blob);
  4231.  
  4232. /**
  4233.  * drm_property_replace_global_blob - atomically replace existing blob property
  4234.  * @dev: drm device
  4235.  * @replace: location of blob property pointer to be replaced
  4236.  * @length: length of data for new blob, or 0 for no data
  4237.  * @data: content for new blob, or NULL for no data
  4238.  * @obj_holds_id: optional object for property holding blob ID
  4239.  * @prop_holds_id: optional property holding blob ID
  4240.  * @return 0 on success or error on failure
  4241.  *
  4242.  * This function will atomically replace a global property in the blob list,
  4243.  * optionally updating a property which holds the ID of that property. It is
  4244.  * guaranteed to be atomic: no caller will be allowed to see intermediate
  4245.  * results, and either the entire operation will succeed and clean up the
  4246.  * previous property, or it will fail and the state will be unchanged.
  4247.  *
  4248.  * If length is 0 or data is NULL, no new blob will be created, and the holding
  4249.  * property, if specified, will be set to 0.
  4250.  *
  4251.  * Access to the replace pointer is assumed to be protected by the caller, e.g.
  4252.  * by holding the relevant modesetting object lock for its parent.
  4253.  *
  4254.  * For example, a drm_connector has a 'PATH' property, which contains the ID
  4255.  * of a blob property with the value of the MST path information. Calling this
  4256.  * function with replace pointing to the connector's path_blob_ptr, length and
  4257.  * data set for the new path information, obj_holds_id set to the connector's
  4258.  * base object, and prop_holds_id set to the path property name, will perform
  4259.  * a completely atomic update. The access to path_blob_ptr is protected by the
  4260.  * caller holding a lock on the connector.
  4261.  */
  4262. static int drm_property_replace_global_blob(struct drm_device *dev,
  4263.                                             struct drm_property_blob **replace,
  4264.                                             size_t length,
  4265.                                             const void *data,
  4266.                                             struct drm_mode_object *obj_holds_id,
  4267.                                             struct drm_property *prop_holds_id)
  4268. {
  4269.         struct drm_property_blob *new_blob = NULL;
  4270.         struct drm_property_blob *old_blob = NULL;
  4271.         int ret;
  4272.  
  4273.         WARN_ON(replace == NULL);
  4274.  
  4275.         old_blob = *replace;
  4276.  
  4277.         if (length && data) {
  4278.                 new_blob = drm_property_create_blob(dev, length, data);
  4279.                 if (IS_ERR(new_blob))
  4280.                         return PTR_ERR(new_blob);
  4281.         }
  4282.  
  4283.         /* This does not need to be synchronised with blob_lock, as the
  4284.          * get_properties ioctl locks all modesetting objects, and
  4285.          * obj_holds_id must be locked before calling here, so we cannot
  4286.          * have its value out of sync with the list membership modified
  4287.          * below under blob_lock. */
  4288.         if (obj_holds_id) {
  4289.                 ret = drm_object_property_set_value(obj_holds_id,
  4290.                                                     prop_holds_id,
  4291.                                                     new_blob ?
  4292.                                                         new_blob->base.id : 0);
  4293.                 if (ret != 0)
  4294.                         goto err_created;
  4295.         }
  4296.  
  4297.         drm_property_unreference_blob(old_blob);
  4298.         *replace = new_blob;
  4299.  
  4300.         return 0;
  4301.  
  4302. err_created:
  4303.         drm_property_unreference_blob(new_blob);
  4304.         return ret;
  4305. }
  4306.  
  4307.  
  4308. #if 0
  4309. /**
  4310.  * drm_mode_getblob_ioctl - get the contents of a blob property value
  4311.  * @dev: DRM device
  4312.  * @data: ioctl data
  4313.  * @file_priv: DRM file info
  4314.  *
  4315.  * This function retrieves the contents of a blob property. The value stored in
  4316.  * an object's blob property is just a normal modeset object id.
  4317.  *
  4318.  * Called by the user via ioctl.
  4319.  *
  4320.  * Returns:
  4321.  * Zero on success, negative errno on failure.
  4322.  */
  4323. int drm_mode_getblob_ioctl(struct drm_device *dev,
  4324.                            void *data, struct drm_file *file_priv)
  4325. {
  4326.         struct drm_mode_get_blob *out_resp = data;
  4327.         struct drm_property_blob *blob;
  4328.         int ret = 0;
  4329.         void __user *blob_ptr;
  4330.  
  4331.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4332.                 return -EINVAL;
  4333.  
  4334.         drm_modeset_lock_all(dev);
  4335.         mutex_lock(&dev->mode_config.blob_lock);
  4336.         blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
  4337.         if (!blob) {
  4338.                 ret = -ENOENT;
  4339.                 goto done;
  4340.         }
  4341.  
  4342.         if (out_resp->length == blob->length) {
  4343.                 blob_ptr = (void __user *)(unsigned long)out_resp->data;
  4344.                 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
  4345.                         ret = -EFAULT;
  4346.                         goto done;
  4347.                 }
  4348.         }
  4349.         out_resp->length = blob->length;
  4350.  
  4351. done:
  4352.         mutex_unlock(&dev->mode_config.blob_lock);
  4353.         drm_modeset_unlock_all(dev);
  4354.         return ret;
  4355. }
  4356. #endif
  4357.  
  4358. /**
  4359.  * drm_mode_connector_set_path_property - set tile property on connector
  4360.  * @connector: connector to set property on.
  4361.  * @path: path to use for property; must not be NULL.
  4362.  *
  4363.  * This creates a property to expose to userspace to specify a
  4364.  * connector path. This is mainly used for DisplayPort MST where
  4365.  * connectors have a topology and we want to allow userspace to give
  4366.  * them more meaningful names.
  4367.  *
  4368.  * Returns:
  4369.  * Zero on success, negative errno on failure.
  4370.  */
  4371. int drm_mode_connector_set_path_property(struct drm_connector *connector,
  4372.                                          const char *path)
  4373. {
  4374.         struct drm_device *dev = connector->dev;
  4375.         int ret;
  4376.  
  4377.         ret = drm_property_replace_global_blob(dev,
  4378.                                                &connector->path_blob_ptr,
  4379.                                                strlen(path) + 1,
  4380.                                                path,
  4381.                                                &connector->base,
  4382.                                                dev->mode_config.path_property);
  4383.         return ret;
  4384. }
  4385. EXPORT_SYMBOL(drm_mode_connector_set_path_property);
  4386.  
  4387. /**
  4388.  * drm_mode_connector_set_tile_property - set tile property on connector
  4389.  * @connector: connector to set property on.
  4390.  *
  4391.  * This looks up the tile information for a connector, and creates a
  4392.  * property for userspace to parse if it exists. The property is of
  4393.  * the form of 8 integers using ':' as a separator.
  4394.  *
  4395.  * Returns:
  4396.  * Zero on success, errno on failure.
  4397.  */
  4398. int drm_mode_connector_set_tile_property(struct drm_connector *connector)
  4399. {
  4400.         struct drm_device *dev = connector->dev;
  4401.         char tile[256];
  4402.         int ret;
  4403.  
  4404.         if (!connector->has_tile) {
  4405.                 ret  = drm_property_replace_global_blob(dev,
  4406.                                                         &connector->tile_blob_ptr,
  4407.                                                         0,
  4408.                                                         NULL,
  4409.                                                         &connector->base,
  4410.                                                         dev->mode_config.tile_property);
  4411.                 return ret;
  4412.         }
  4413.  
  4414.         snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
  4415.                  connector->tile_group->id, connector->tile_is_single_monitor,
  4416.                  connector->num_h_tile, connector->num_v_tile,
  4417.                  connector->tile_h_loc, connector->tile_v_loc,
  4418.                  connector->tile_h_size, connector->tile_v_size);
  4419.  
  4420.         ret = drm_property_replace_global_blob(dev,
  4421.                                                &connector->tile_blob_ptr,
  4422.                                                strlen(tile) + 1,
  4423.                                                tile,
  4424.                                                &connector->base,
  4425.                                                dev->mode_config.tile_property);
  4426.         return ret;
  4427. }
  4428. EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
  4429.  
  4430. /**
  4431.  * drm_mode_connector_update_edid_property - update the edid property of a connector
  4432.  * @connector: drm connector
  4433.  * @edid: new value of the edid property
  4434.  *
  4435.  * This function creates a new blob modeset object and assigns its id to the
  4436.  * connector's edid property.
  4437.  *
  4438.  * Returns:
  4439.  * Zero on success, negative errno on failure.
  4440.  */
  4441. int drm_mode_connector_update_edid_property(struct drm_connector *connector,
  4442.                                             const struct edid *edid)
  4443. {
  4444.         struct drm_device *dev = connector->dev;
  4445.         size_t size = 0;
  4446.         int ret;
  4447.  
  4448.         /* ignore requests to set edid when overridden */
  4449.         if (connector->override_edid)
  4450.                 return 0;
  4451.  
  4452.         if (edid)
  4453.                 size = EDID_LENGTH * (1 + edid->extensions);
  4454.  
  4455.         ret = drm_property_replace_global_blob(dev,
  4456.                                                &connector->edid_blob_ptr,
  4457.                                                size,
  4458.                                                edid,
  4459.                                                &connector->base,
  4460.                                                dev->mode_config.edid_property);
  4461.         return ret;
  4462. }
  4463. EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
  4464.  
  4465. /* Some properties could refer to dynamic refcnt'd objects, or things that
  4466.  * need special locking to handle lifetime issues (ie. to ensure the prop
  4467.  * value doesn't become invalid part way through the property update due to
  4468.  * race).  The value returned by reference via 'obj' should be passed back
  4469.  * to drm_property_change_valid_put() after the property is set (and the
  4470.  * object to which the property is attached has a chance to take it's own
  4471.  * reference).
  4472.  */
  4473. bool drm_property_change_valid_get(struct drm_property *property,
  4474.                                          uint64_t value, struct drm_mode_object **ref)
  4475. {
  4476.         int i;
  4477.  
  4478.         if (property->flags & DRM_MODE_PROP_IMMUTABLE)
  4479.                 return false;
  4480.  
  4481.         *ref = NULL;
  4482.  
  4483.         if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
  4484.                 if (value < property->values[0] || value > property->values[1])
  4485.                         return false;
  4486.                 return true;
  4487.         } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
  4488.                 int64_t svalue = U642I64(value);
  4489.  
  4490.                 if (svalue < U642I64(property->values[0]) ||
  4491.                                 svalue > U642I64(property->values[1]))
  4492.                         return false;
  4493.                 return true;
  4494.         } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  4495.                 uint64_t valid_mask = 0;
  4496.  
  4497.                 for (i = 0; i < property->num_values; i++)
  4498.                         valid_mask |= (1ULL << property->values[i]);
  4499.                 return !(value & ~valid_mask);
  4500.         } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
  4501.                 struct drm_property_blob *blob;
  4502.  
  4503.                 if (value == 0)
  4504.                         return true;
  4505.  
  4506.                 blob = drm_property_lookup_blob(property->dev, value);
  4507.                 if (blob) {
  4508.                         *ref = &blob->base;
  4509.                         return true;
  4510.                 } else {
  4511.                         return false;
  4512.                 }
  4513.         } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
  4514.                 /* a zero value for an object property translates to null: */
  4515.                 if (value == 0)
  4516.                         return true;
  4517.  
  4518.                 /* handle refcnt'd objects specially: */
  4519.                 if (property->values[0] == DRM_MODE_OBJECT_FB) {
  4520.                         struct drm_framebuffer *fb;
  4521.                         fb = drm_framebuffer_lookup(property->dev, value);
  4522.                         if (fb) {
  4523.                                 *ref = &fb->base;
  4524.                                 return true;
  4525.                         } else {
  4526.                                 return false;
  4527.                         }
  4528.                 } else {
  4529.                         return _object_find(property->dev, value, property->values[0]) != NULL;
  4530.                 }
  4531.         }
  4532.  
  4533.         for (i = 0; i < property->num_values; i++)
  4534.                 if (property->values[i] == value)
  4535.                         return true;
  4536.         return false;
  4537. }
  4538.  
  4539. void drm_property_change_valid_put(struct drm_property *property,
  4540.                 struct drm_mode_object *ref)
  4541. {
  4542.         if (!ref)
  4543.                 return;
  4544.  
  4545.         if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
  4546.                 if (property->values[0] == DRM_MODE_OBJECT_FB)
  4547.                         drm_framebuffer_unreference(obj_to_fb(ref));
  4548.         } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
  4549.                 drm_property_unreference_blob(obj_to_blob(ref));
  4550. }
  4551.  
  4552.  
  4553.  
  4554. static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
  4555.                                            struct drm_property *property,
  4556.                                            uint64_t value)
  4557. {
  4558.         int ret = -EINVAL;
  4559.         struct drm_connector *connector = obj_to_connector(obj);
  4560.  
  4561.         /* Do DPMS ourselves */
  4562.         if (property == connector->dev->mode_config.dpms_property) {
  4563.                 ret = 0;
  4564.                 if (connector->funcs->dpms)
  4565.                         ret = (*connector->funcs->dpms)(connector, (int)value);
  4566.         } else if (connector->funcs->set_property)
  4567.                 ret = connector->funcs->set_property(connector, property, value);
  4568.  
  4569.         /* store the property value if successful */
  4570.         if (!ret)
  4571.                 drm_object_property_set_value(&connector->base, property, value);
  4572.         return ret;
  4573. }
  4574.  
  4575. static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
  4576.                                       struct drm_property *property,
  4577.                                       uint64_t value)
  4578. {
  4579.         int ret = -EINVAL;
  4580.         struct drm_crtc *crtc = obj_to_crtc(obj);
  4581.  
  4582.         if (crtc->funcs->set_property)
  4583.                 ret = crtc->funcs->set_property(crtc, property, value);
  4584.         if (!ret)
  4585.                 drm_object_property_set_value(obj, property, value);
  4586.  
  4587.         return ret;
  4588. }
  4589.  
  4590. /**
  4591.  * drm_mode_plane_set_obj_prop - set the value of a property
  4592.  * @plane: drm plane object to set property value for
  4593.  * @property: property to set
  4594.  * @value: value the property should be set to
  4595.  *
  4596.  * This functions sets a given property on a given plane object. This function
  4597.  * calls the driver's ->set_property callback and changes the software state of
  4598.  * the property if the callback succeeds.
  4599.  *
  4600.  * Returns:
  4601.  * Zero on success, error code on failure.
  4602.  */
  4603. int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
  4604.                                 struct drm_property *property,
  4605.                                 uint64_t value)
  4606. {
  4607.         int ret = -EINVAL;
  4608.         struct drm_mode_object *obj = &plane->base;
  4609.  
  4610.         if (plane->funcs->set_property)
  4611.                 ret = plane->funcs->set_property(plane, property, value);
  4612.         if (!ret)
  4613.                 drm_object_property_set_value(obj, property, value);
  4614.  
  4615.         return ret;
  4616. }
  4617. EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
  4618.  
  4619. #if 0
  4620. /**
  4621.  * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
  4622.  * @dev: DRM device
  4623.  * @data: ioctl data
  4624.  * @file_priv: DRM file info
  4625.  *
  4626.  * This function retrieves the current value for an object's property. Compared
  4627.  * to the connector specific ioctl this one is extended to also work on crtc and
  4628.  * plane objects.
  4629.  *
  4630.  * Called by the user via ioctl.
  4631.  *
  4632.  * Returns:
  4633.  * Zero on success, negative errno on failure.
  4634.  */
  4635. int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
  4636.                                       struct drm_file *file_priv)
  4637. {
  4638.         struct drm_mode_obj_get_properties *arg = data;
  4639.         struct drm_mode_object *obj;
  4640.         int ret = 0;
  4641.  
  4642.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4643.                 return -EINVAL;
  4644.  
  4645.         drm_modeset_lock_all(dev);
  4646.  
  4647.         obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  4648.         if (!obj) {
  4649.                 ret = -ENOENT;
  4650.                 goto out;
  4651.         }
  4652.         if (!obj->properties) {
  4653.                 ret = -EINVAL;
  4654.                 goto out;
  4655.         }
  4656.  
  4657.         ret = get_properties(obj, file_priv->atomic,
  4658.                         (uint32_t __user *)(unsigned long)(arg->props_ptr),
  4659.                         (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
  4660.                         &arg->count_props);
  4661.  
  4662. out:
  4663.         drm_modeset_unlock_all(dev);
  4664.         return ret;
  4665. }
  4666.  
  4667. /**
  4668.  * drm_mode_obj_set_property_ioctl - set the current value of an object's property
  4669.  * @dev: DRM device
  4670.  * @data: ioctl data
  4671.  * @file_priv: DRM file info
  4672.  *
  4673.  * This function sets the current value for an object's property. It also calls
  4674.  * into a driver's ->set_property callback to update the hardware state.
  4675.  * Compared to the connector specific ioctl this one is extended to also work on
  4676.  * crtc and plane objects.
  4677.  *
  4678.  * Called by the user via ioctl.
  4679.  *
  4680.  * Returns:
  4681.  * Zero on success, negative errno on failure.
  4682.  */
  4683. int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
  4684.                                     struct drm_file *file_priv)
  4685. {
  4686.         struct drm_mode_obj_set_property *arg = data;
  4687.         struct drm_mode_object *arg_obj;
  4688.         struct drm_mode_object *prop_obj;
  4689.         struct drm_property *property;
  4690.         int i, ret = -EINVAL;
  4691.         struct drm_mode_object *ref;
  4692.  
  4693.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4694.                 return -EINVAL;
  4695.  
  4696.         drm_modeset_lock_all(dev);
  4697.  
  4698.         arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  4699.         if (!arg_obj) {
  4700.                 ret = -ENOENT;
  4701.                 goto out;
  4702.         }
  4703.         if (!arg_obj->properties)
  4704.                 goto out;
  4705.  
  4706.         for (i = 0; i < arg_obj->properties->count; i++)
  4707.                 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
  4708.                         break;
  4709.  
  4710.         if (i == arg_obj->properties->count)
  4711.                 goto out;
  4712.  
  4713.         prop_obj = drm_mode_object_find(dev, arg->prop_id,
  4714.                                         DRM_MODE_OBJECT_PROPERTY);
  4715.         if (!prop_obj) {
  4716.                 ret = -ENOENT;
  4717.                 goto out;
  4718.         }
  4719.         property = obj_to_property(prop_obj);
  4720.  
  4721.         if (!drm_property_change_valid_get(property, arg->value, &ref))
  4722.                 goto out;
  4723.  
  4724.         switch (arg_obj->type) {
  4725.         case DRM_MODE_OBJECT_CONNECTOR:
  4726.                 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
  4727.                                                       arg->value);
  4728.                 break;
  4729.         case DRM_MODE_OBJECT_CRTC:
  4730.                 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
  4731.                 break;
  4732.         case DRM_MODE_OBJECT_PLANE:
  4733.                 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
  4734.                                                   property, arg->value);
  4735.                 break;
  4736.         }
  4737.  
  4738.         drm_property_change_valid_put(property, ref);
  4739.  
  4740. out:
  4741.         drm_modeset_unlock_all(dev);
  4742.         return ret;
  4743. }
  4744. #endif
  4745.  
  4746. /**
  4747.  * drm_mode_connector_attach_encoder - attach a connector to an encoder
  4748.  * @connector: connector to attach
  4749.  * @encoder: encoder to attach @connector to
  4750.  *
  4751.  * This function links up a connector to an encoder. Note that the routing
  4752.  * restrictions between encoders and crtcs are exposed to userspace through the
  4753.  * possible_clones and possible_crtcs bitmasks.
  4754.  *
  4755.  * Returns:
  4756.  * Zero on success, negative errno on failure.
  4757.  */
  4758. int drm_mode_connector_attach_encoder(struct drm_connector *connector,
  4759.                                       struct drm_encoder *encoder)
  4760. {
  4761.         int i;
  4762.  
  4763.         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  4764.                 if (connector->encoder_ids[i] == 0) {
  4765.                         connector->encoder_ids[i] = encoder->base.id;
  4766.                         return 0;
  4767.                 }
  4768.         }
  4769.         return -ENOMEM;
  4770. }
  4771. EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
  4772.  
  4773. /**
  4774.  * drm_mode_crtc_set_gamma_size - set the gamma table size
  4775.  * @crtc: CRTC to set the gamma table size for
  4776.  * @gamma_size: size of the gamma table
  4777.  *
  4778.  * Drivers which support gamma tables should set this to the supported gamma
  4779.  * table size when initializing the CRTC. Currently the drm core only supports a
  4780.  * fixed gamma table size.
  4781.  *
  4782.  * Returns:
  4783.  * Zero on success, negative errno on failure.
  4784.  */
  4785. int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
  4786.                                  int gamma_size)
  4787. {
  4788.         crtc->gamma_size = gamma_size;
  4789.  
  4790.         crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
  4791.                                     GFP_KERNEL);
  4792.         if (!crtc->gamma_store) {
  4793.                 crtc->gamma_size = 0;
  4794.                 return -ENOMEM;
  4795.         }
  4796.  
  4797.         return 0;
  4798. }
  4799. EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
  4800.  
  4801. #if 0
  4802. /**
  4803.  * drm_mode_gamma_set_ioctl - set the gamma table
  4804.  * @dev: DRM device
  4805.  * @data: ioctl data
  4806.  * @file_priv: DRM file info
  4807.  *
  4808.  * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
  4809.  * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
  4810.  *
  4811.  * Called by the user via ioctl.
  4812.  *
  4813.  * Returns:
  4814.  * Zero on success, negative errno on failure.
  4815.  */
  4816. int drm_mode_gamma_set_ioctl(struct drm_device *dev,
  4817.                              void *data, struct drm_file *file_priv)
  4818. {
  4819.         struct drm_mode_crtc_lut *crtc_lut = data;
  4820.         struct drm_crtc *crtc;
  4821.         void *r_base, *g_base, *b_base;
  4822.         int size;
  4823.         int ret = 0;
  4824.  
  4825.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4826.                 return -EINVAL;
  4827.  
  4828.         drm_modeset_lock_all(dev);
  4829.         crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
  4830.         if (!crtc) {
  4831.                 ret = -ENOENT;
  4832.                 goto out;
  4833.         }
  4834.  
  4835.         if (crtc->funcs->gamma_set == NULL) {
  4836.                 ret = -ENOSYS;
  4837.                 goto out;
  4838.         }
  4839.  
  4840.         /* memcpy into gamma store */
  4841.         if (crtc_lut->gamma_size != crtc->gamma_size) {
  4842.                 ret = -EINVAL;
  4843.                 goto out;
  4844.         }
  4845.  
  4846.         size = crtc_lut->gamma_size * (sizeof(uint16_t));
  4847.         r_base = crtc->gamma_store;
  4848.         if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
  4849.                 ret = -EFAULT;
  4850.                 goto out;
  4851.         }
  4852.  
  4853.         g_base = r_base + size;
  4854.         if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
  4855.                 ret = -EFAULT;
  4856.                 goto out;
  4857.         }
  4858.  
  4859.         b_base = g_base + size;
  4860.         if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
  4861.                 ret = -EFAULT;
  4862.                 goto out;
  4863.         }
  4864.  
  4865.         crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
  4866.  
  4867. out:
  4868.         drm_modeset_unlock_all(dev);
  4869.         return ret;
  4870.  
  4871. }
  4872.  
  4873. /**
  4874.  * drm_mode_gamma_get_ioctl - get the gamma table
  4875.  * @dev: DRM device
  4876.  * @data: ioctl data
  4877.  * @file_priv: DRM file info
  4878.  *
  4879.  * Copy the current gamma table into the storage provided. This also provides
  4880.  * the gamma table size the driver expects, which can be used to size the
  4881.  * allocated storage.
  4882.  *
  4883.  * Called by the user via ioctl.
  4884.  *
  4885.  * Returns:
  4886.  * Zero on success, negative errno on failure.
  4887.  */
  4888. int drm_mode_gamma_get_ioctl(struct drm_device *dev,
  4889.                              void *data, struct drm_file *file_priv)
  4890. {
  4891.         struct drm_mode_crtc_lut *crtc_lut = data;
  4892.         struct drm_crtc *crtc;
  4893.         void *r_base, *g_base, *b_base;
  4894.         int size;
  4895.         int ret = 0;
  4896.  
  4897.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4898.                 return -EINVAL;
  4899.  
  4900.         drm_modeset_lock_all(dev);
  4901.         crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
  4902.         if (!crtc) {
  4903.                 ret = -ENOENT;
  4904.                 goto out;
  4905.         }
  4906.  
  4907.         /* memcpy into gamma store */
  4908.         if (crtc_lut->gamma_size != crtc->gamma_size) {
  4909.                 ret = -EINVAL;
  4910.                 goto out;
  4911.         }
  4912.  
  4913.         size = crtc_lut->gamma_size * (sizeof(uint16_t));
  4914.         r_base = crtc->gamma_store;
  4915.         if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
  4916.                 ret = -EFAULT;
  4917.                 goto out;
  4918.         }
  4919.  
  4920.         g_base = r_base + size;
  4921.         if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
  4922.                 ret = -EFAULT;
  4923.                 goto out;
  4924.         }
  4925.  
  4926.         b_base = g_base + size;
  4927.         if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
  4928.                 ret = -EFAULT;
  4929.                 goto out;
  4930.         }
  4931. out:
  4932.         drm_modeset_unlock_all(dev);
  4933.         return ret;
  4934. }
  4935.  
  4936. #endif
  4937.  
  4938.  
  4939. /**
  4940.  * drm_mode_config_reset - call ->reset callbacks
  4941.  * @dev: drm device
  4942.  *
  4943.  * This functions calls all the crtc's, encoder's and connector's ->reset
  4944.  * callback. Drivers can use this in e.g. their driver load or resume code to
  4945.  * reset hardware and software state.
  4946.  */
  4947. void drm_mode_config_reset(struct drm_device *dev)
  4948. {
  4949.         struct drm_crtc *crtc;
  4950.         struct drm_plane *plane;
  4951.         struct drm_encoder *encoder;
  4952.         struct drm_connector *connector;
  4953.  
  4954.         drm_for_each_plane(plane, dev)
  4955.                 if (plane->funcs->reset)
  4956.                         plane->funcs->reset(plane);
  4957.  
  4958.         drm_for_each_crtc(crtc, dev)
  4959.                 if (crtc->funcs->reset)
  4960.                         crtc->funcs->reset(crtc);
  4961.  
  4962.         drm_for_each_encoder(encoder, dev)
  4963.                 if (encoder->funcs->reset)
  4964.                         encoder->funcs->reset(encoder);
  4965.  
  4966.         mutex_lock(&dev->mode_config.mutex);
  4967.         drm_for_each_connector(connector, dev)
  4968.                 if (connector->funcs->reset)
  4969.                         connector->funcs->reset(connector);
  4970.         mutex_unlock(&dev->mode_config.mutex);
  4971. }
  4972. EXPORT_SYMBOL(drm_mode_config_reset);
  4973. /*
  4974.  * Just need to support RGB formats here for compat with code that doesn't
  4975.  * use pixel formats directly yet.
  4976.  */
  4977. void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
  4978.                           int *bpp)
  4979. {
  4980.         switch (format) {
  4981.         case DRM_FORMAT_C8:
  4982.         case DRM_FORMAT_RGB332:
  4983.         case DRM_FORMAT_BGR233:
  4984.                 *depth = 8;
  4985.                 *bpp = 8;
  4986.                 break;
  4987.         case DRM_FORMAT_XRGB1555:
  4988.         case DRM_FORMAT_XBGR1555:
  4989.         case DRM_FORMAT_RGBX5551:
  4990.         case DRM_FORMAT_BGRX5551:
  4991.         case DRM_FORMAT_ARGB1555:
  4992.         case DRM_FORMAT_ABGR1555:
  4993.         case DRM_FORMAT_RGBA5551:
  4994.         case DRM_FORMAT_BGRA5551:
  4995.                 *depth = 15;
  4996.                 *bpp = 16;
  4997.                 break;
  4998.         case DRM_FORMAT_RGB565:
  4999.         case DRM_FORMAT_BGR565:
  5000.                 *depth = 16;
  5001.                 *bpp = 16;
  5002.                 break;
  5003.         case DRM_FORMAT_RGB888:
  5004.         case DRM_FORMAT_BGR888:
  5005.                 *depth = 24;
  5006.                 *bpp = 24;
  5007.                 break;
  5008.         case DRM_FORMAT_XRGB8888:
  5009.         case DRM_FORMAT_XBGR8888:
  5010.         case DRM_FORMAT_RGBX8888:
  5011.         case DRM_FORMAT_BGRX8888:
  5012.                 *depth = 24;
  5013.                 *bpp = 32;
  5014.                 break;
  5015.         case DRM_FORMAT_XRGB2101010:
  5016.         case DRM_FORMAT_XBGR2101010:
  5017.         case DRM_FORMAT_RGBX1010102:
  5018.         case DRM_FORMAT_BGRX1010102:
  5019.         case DRM_FORMAT_ARGB2101010:
  5020.         case DRM_FORMAT_ABGR2101010:
  5021.         case DRM_FORMAT_RGBA1010102:
  5022.         case DRM_FORMAT_BGRA1010102:
  5023.                 *depth = 30;
  5024.                 *bpp = 32;
  5025.                 break;
  5026.         case DRM_FORMAT_ARGB8888:
  5027.         case DRM_FORMAT_ABGR8888:
  5028.         case DRM_FORMAT_RGBA8888:
  5029.         case DRM_FORMAT_BGRA8888:
  5030.                 *depth = 32;
  5031.                 *bpp = 32;
  5032.                 break;
  5033.         default:
  5034.                 DRM_DEBUG_KMS("unsupported pixel format %s\n",
  5035.                               drm_get_format_name(format));
  5036.                 *depth = 0;
  5037.                 *bpp = 0;
  5038.                 break;
  5039.         }
  5040. }
  5041. EXPORT_SYMBOL(drm_fb_get_bpp_depth);
  5042.  
  5043. /**
  5044.  * drm_format_num_planes - get the number of planes for format
  5045.  * @format: pixel format (DRM_FORMAT_*)
  5046.  *
  5047.  * Returns:
  5048.  * The number of planes used by the specified pixel format.
  5049.  */
  5050. int drm_format_num_planes(uint32_t format)
  5051. {
  5052.         switch (format) {
  5053.         case DRM_FORMAT_YUV410:
  5054.         case DRM_FORMAT_YVU410:
  5055.         case DRM_FORMAT_YUV411:
  5056.         case DRM_FORMAT_YVU411:
  5057.         case DRM_FORMAT_YUV420:
  5058.         case DRM_FORMAT_YVU420:
  5059.         case DRM_FORMAT_YUV422:
  5060.         case DRM_FORMAT_YVU422:
  5061.         case DRM_FORMAT_YUV444:
  5062.         case DRM_FORMAT_YVU444:
  5063.                 return 3;
  5064.         case DRM_FORMAT_NV12:
  5065.         case DRM_FORMAT_NV21:
  5066.         case DRM_FORMAT_NV16:
  5067.         case DRM_FORMAT_NV61:
  5068.         case DRM_FORMAT_NV24:
  5069.         case DRM_FORMAT_NV42:
  5070.                 return 2;
  5071.         default:
  5072.                 return 1;
  5073.         }
  5074. }
  5075. EXPORT_SYMBOL(drm_format_num_planes);
  5076.  
  5077. /**
  5078.  * drm_format_plane_cpp - determine the bytes per pixel value
  5079.  * @format: pixel format (DRM_FORMAT_*)
  5080.  * @plane: plane index
  5081.  *
  5082.  * Returns:
  5083.  * The bytes per pixel value for the specified plane.
  5084.  */
  5085. int drm_format_plane_cpp(uint32_t format, int plane)
  5086. {
  5087.         unsigned int depth;
  5088.         int bpp;
  5089.  
  5090.         if (plane >= drm_format_num_planes(format))
  5091.                 return 0;
  5092.  
  5093.         switch (format) {
  5094.         case DRM_FORMAT_YUYV:
  5095.         case DRM_FORMAT_YVYU:
  5096.         case DRM_FORMAT_UYVY:
  5097.         case DRM_FORMAT_VYUY:
  5098.                 return 2;
  5099.         case DRM_FORMAT_NV12:
  5100.         case DRM_FORMAT_NV21:
  5101.         case DRM_FORMAT_NV16:
  5102.         case DRM_FORMAT_NV61:
  5103.         case DRM_FORMAT_NV24:
  5104.         case DRM_FORMAT_NV42:
  5105.                 return plane ? 2 : 1;
  5106.         case DRM_FORMAT_YUV410:
  5107.         case DRM_FORMAT_YVU410:
  5108.         case DRM_FORMAT_YUV411:
  5109.         case DRM_FORMAT_YVU411:
  5110.         case DRM_FORMAT_YUV420:
  5111.         case DRM_FORMAT_YVU420:
  5112.         case DRM_FORMAT_YUV422:
  5113.         case DRM_FORMAT_YVU422:
  5114.         case DRM_FORMAT_YUV444:
  5115.         case DRM_FORMAT_YVU444:
  5116.                 return 1;
  5117.         default:
  5118.                 drm_fb_get_bpp_depth(format, &depth, &bpp);
  5119.                 return bpp >> 3;
  5120.         }
  5121. }
  5122. EXPORT_SYMBOL(drm_format_plane_cpp);
  5123.  
  5124. /**
  5125.  * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
  5126.  * @format: pixel format (DRM_FORMAT_*)
  5127.  *
  5128.  * Returns:
  5129.  * The horizontal chroma subsampling factor for the
  5130.  * specified pixel format.
  5131.  */
  5132. int drm_format_horz_chroma_subsampling(uint32_t format)
  5133. {
  5134.         switch (format) {
  5135.         case DRM_FORMAT_YUV411:
  5136.         case DRM_FORMAT_YVU411:
  5137.         case DRM_FORMAT_YUV410:
  5138.         case DRM_FORMAT_YVU410:
  5139.                 return 4;
  5140.         case DRM_FORMAT_YUYV:
  5141.         case DRM_FORMAT_YVYU:
  5142.         case DRM_FORMAT_UYVY:
  5143.         case DRM_FORMAT_VYUY:
  5144.         case DRM_FORMAT_NV12:
  5145.         case DRM_FORMAT_NV21:
  5146.         case DRM_FORMAT_NV16:
  5147.         case DRM_FORMAT_NV61:
  5148.         case DRM_FORMAT_YUV422:
  5149.         case DRM_FORMAT_YVU422:
  5150.         case DRM_FORMAT_YUV420:
  5151.         case DRM_FORMAT_YVU420:
  5152.                 return 2;
  5153.         default:
  5154.                 return 1;
  5155.         }
  5156. }
  5157. EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
  5158.  
  5159. /**
  5160.  * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
  5161.  * @format: pixel format (DRM_FORMAT_*)
  5162.  *
  5163.  * Returns:
  5164.  * The vertical chroma subsampling factor for the
  5165.  * specified pixel format.
  5166.  */
  5167. int drm_format_vert_chroma_subsampling(uint32_t format)
  5168. {
  5169.         switch (format) {
  5170.         case DRM_FORMAT_YUV410:
  5171.         case DRM_FORMAT_YVU410:
  5172.                 return 4;
  5173.         case DRM_FORMAT_YUV420:
  5174.         case DRM_FORMAT_YVU420:
  5175.         case DRM_FORMAT_NV12:
  5176.         case DRM_FORMAT_NV21:
  5177.                 return 2;
  5178.         default:
  5179.                 return 1;
  5180.         }
  5181. }
  5182. EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
  5183.  
  5184. /**
  5185.  * drm_rotation_simplify() - Try to simplify the rotation
  5186.  * @rotation: Rotation to be simplified
  5187.  * @supported_rotations: Supported rotations
  5188.  *
  5189.  * Attempt to simplify the rotation to a form that is supported.
  5190.  * Eg. if the hardware supports everything except DRM_REFLECT_X
  5191.  * one could call this function like this:
  5192.  *
  5193.  * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
  5194.  *                       BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
  5195.  *                       BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
  5196.  *
  5197.  * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
  5198.  * transforms the hardware supports, this function may not
  5199.  * be able to produce a supported transform, so the caller should
  5200.  * check the result afterwards.
  5201.  */
  5202. unsigned int drm_rotation_simplify(unsigned int rotation,
  5203.                                    unsigned int supported_rotations)
  5204. {
  5205.         if (rotation & ~supported_rotations) {
  5206.                 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
  5207.                 rotation = (rotation & DRM_REFLECT_MASK) |
  5208.                            BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
  5209.         }
  5210.  
  5211.         return rotation;
  5212. }
  5213. EXPORT_SYMBOL(drm_rotation_simplify);
  5214.  
  5215. /**
  5216.  * drm_mode_config_init - initialize DRM mode_configuration structure
  5217.  * @dev: DRM device
  5218.  *
  5219.  * Initialize @dev's mode_config structure, used for tracking the graphics
  5220.  * configuration of @dev.
  5221.  *
  5222.  * Since this initializes the modeset locks, no locking is possible. Which is no
  5223.  * problem, since this should happen single threaded at init time. It is the
  5224.  * driver's problem to ensure this guarantee.
  5225.  *
  5226.  */
  5227. void drm_mode_config_init(struct drm_device *dev)
  5228. {
  5229.         mutex_init(&dev->mode_config.mutex);
  5230.         drm_modeset_lock_init(&dev->mode_config.connection_mutex);
  5231.         mutex_init(&dev->mode_config.idr_mutex);
  5232.         mutex_init(&dev->mode_config.fb_lock);
  5233.         mutex_init(&dev->mode_config.blob_lock);
  5234.         INIT_LIST_HEAD(&dev->mode_config.fb_list);
  5235.         INIT_LIST_HEAD(&dev->mode_config.crtc_list);
  5236.         INIT_LIST_HEAD(&dev->mode_config.connector_list);
  5237.         INIT_LIST_HEAD(&dev->mode_config.encoder_list);
  5238.         INIT_LIST_HEAD(&dev->mode_config.property_list);
  5239.         INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
  5240.         INIT_LIST_HEAD(&dev->mode_config.plane_list);
  5241.         idr_init(&dev->mode_config.crtc_idr);
  5242.         idr_init(&dev->mode_config.tile_idr);
  5243.  
  5244.         drm_modeset_lock_all(dev);
  5245.         drm_mode_create_standard_properties(dev);
  5246.         drm_modeset_unlock_all(dev);
  5247.  
  5248.         /* Just to be sure */
  5249.         dev->mode_config.num_fb = 0;
  5250.         dev->mode_config.num_connector = 0;
  5251.         dev->mode_config.num_crtc = 0;
  5252.         dev->mode_config.num_encoder = 0;
  5253.         dev->mode_config.num_overlay_plane = 0;
  5254.         dev->mode_config.num_total_plane = 0;
  5255. }
  5256. EXPORT_SYMBOL(drm_mode_config_init);
  5257.  
  5258. /**
  5259.  * drm_mode_config_cleanup - free up DRM mode_config info
  5260.  * @dev: DRM device
  5261.  *
  5262.  * Free up all the connectors and CRTCs associated with this DRM device, then
  5263.  * free up the framebuffers and associated buffer objects.
  5264.  *
  5265.  * Note that since this /should/ happen single-threaded at driver/device
  5266.  * teardown time, no locking is required. It's the driver's job to ensure that
  5267.  * this guarantee actually holds true.
  5268.  *
  5269.  * FIXME: cleanup any dangling user buffer objects too
  5270.  */
  5271. void drm_mode_config_cleanup(struct drm_device *dev)
  5272. {
  5273.         struct drm_connector *connector, *ot;
  5274.         struct drm_crtc *crtc, *ct;
  5275.         struct drm_encoder *encoder, *enct;
  5276.         struct drm_framebuffer *fb, *fbt;
  5277.         struct drm_property *property, *pt;
  5278.         struct drm_property_blob *blob, *bt;
  5279.         struct drm_plane *plane, *plt;
  5280.  
  5281.         list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
  5282.                                  head) {
  5283.                 encoder->funcs->destroy(encoder);
  5284.         }
  5285.  
  5286.         list_for_each_entry_safe(connector, ot,
  5287.                                  &dev->mode_config.connector_list, head) {
  5288.                 connector->funcs->destroy(connector);
  5289.         }
  5290.  
  5291.         list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
  5292.                                  head) {
  5293.                 drm_property_destroy(dev, property);
  5294.         }
  5295.  
  5296.         list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
  5297.                                  head_global) {
  5298.                 drm_property_unreference_blob(blob);
  5299.         }
  5300.  
  5301.         /*
  5302.          * Single-threaded teardown context, so it's not required to grab the
  5303.          * fb_lock to protect against concurrent fb_list access. Contrary, it
  5304.          * would actually deadlock with the drm_framebuffer_cleanup function.
  5305.          *
  5306.          * Also, if there are any framebuffers left, that's a driver leak now,
  5307.          * so politely WARN about this.
  5308.          */
  5309.         WARN_ON(!list_empty(&dev->mode_config.fb_list));
  5310.         list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
  5311.                 drm_framebuffer_free(&fb->refcount);
  5312.         }
  5313.  
  5314.         list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
  5315.                                  head) {
  5316.                 plane->funcs->destroy(plane);
  5317.         }
  5318.  
  5319.         list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
  5320.                 crtc->funcs->destroy(crtc);
  5321.         }
  5322.  
  5323.         idr_destroy(&dev->mode_config.tile_idr);
  5324.         idr_destroy(&dev->mode_config.crtc_idr);
  5325.         drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
  5326. }
  5327. EXPORT_SYMBOL(drm_mode_config_cleanup);
  5328.  
  5329. struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
  5330.                                                        unsigned int supported_rotations)
  5331. {
  5332.         static const struct drm_prop_enum_list props[] = {
  5333.                 { DRM_ROTATE_0,   "rotate-0" },
  5334.                 { DRM_ROTATE_90,  "rotate-90" },
  5335.                 { DRM_ROTATE_180, "rotate-180" },
  5336.                 { DRM_ROTATE_270, "rotate-270" },
  5337.                 { DRM_REFLECT_X,  "reflect-x" },
  5338.                 { DRM_REFLECT_Y,  "reflect-y" },
  5339.         };
  5340.  
  5341.         return drm_property_create_bitmask(dev, 0, "rotation",
  5342.                                            props, ARRAY_SIZE(props),
  5343.                                            supported_rotations);
  5344. }
  5345. EXPORT_SYMBOL(drm_mode_create_rotation_property);
  5346.  
  5347. /**
  5348.  * DOC: Tile group
  5349.  *
  5350.  * Tile groups are used to represent tiled monitors with a unique
  5351.  * integer identifier. Tiled monitors using DisplayID v1.3 have
  5352.  * a unique 8-byte handle, we store this in a tile group, so we
  5353.  * have a common identifier for all tiles in a monitor group.
  5354.  */
  5355. static void drm_tile_group_free(struct kref *kref)
  5356. {
  5357.         struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
  5358.         struct drm_device *dev = tg->dev;
  5359.         mutex_lock(&dev->mode_config.idr_mutex);
  5360.         idr_remove(&dev->mode_config.tile_idr, tg->id);
  5361.         mutex_unlock(&dev->mode_config.idr_mutex);
  5362.         kfree(tg);
  5363. }
  5364.  
  5365. /**
  5366.  * drm_mode_put_tile_group - drop a reference to a tile group.
  5367.  * @dev: DRM device
  5368.  * @tg: tile group to drop reference to.
  5369.  *
  5370.  * drop reference to tile group and free if 0.
  5371.  */
  5372. void drm_mode_put_tile_group(struct drm_device *dev,
  5373.                              struct drm_tile_group *tg)
  5374. {
  5375.         kref_put(&tg->refcount, drm_tile_group_free);
  5376. }
  5377.  
  5378. /**
  5379.  * drm_mode_get_tile_group - get a reference to an existing tile group
  5380.  * @dev: DRM device
  5381.  * @topology: 8-bytes unique per monitor.
  5382.  *
  5383.  * Use the unique bytes to get a reference to an existing tile group.
  5384.  *
  5385.  * RETURNS:
  5386.  * tile group or NULL if not found.
  5387.  */
  5388. struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
  5389.                                                char topology[8])
  5390. {
  5391.         struct drm_tile_group *tg;
  5392.         int id;
  5393.         mutex_lock(&dev->mode_config.idr_mutex);
  5394.         idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
  5395.                 if (!memcmp(tg->group_data, topology, 8)) {
  5396.                         if (!kref_get_unless_zero(&tg->refcount))
  5397.                                 tg = NULL;
  5398.                         mutex_unlock(&dev->mode_config.idr_mutex);
  5399.                         return tg;
  5400.                 }
  5401.         }
  5402.         mutex_unlock(&dev->mode_config.idr_mutex);
  5403.         return NULL;
  5404. }
  5405. EXPORT_SYMBOL(drm_mode_get_tile_group);
  5406.  
  5407. /**
  5408.  * drm_mode_create_tile_group - create a tile group from a displayid description
  5409.  * @dev: DRM device
  5410.  * @topology: 8-bytes unique per monitor.
  5411.  *
  5412.  * Create a tile group for the unique monitor, and get a unique
  5413.  * identifier for the tile group.
  5414.  *
  5415.  * RETURNS:
  5416.  * new tile group or error.
  5417.  */
  5418. struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
  5419.                                                   char topology[8])
  5420. {
  5421.         struct drm_tile_group *tg;
  5422.         int ret;
  5423.  
  5424.         tg = kzalloc(sizeof(*tg), GFP_KERNEL);
  5425.         if (!tg)
  5426.                 return ERR_PTR(-ENOMEM);
  5427.  
  5428.         kref_init(&tg->refcount);
  5429.         memcpy(tg->group_data, topology, 8);
  5430.         tg->dev = dev;
  5431.  
  5432.         mutex_lock(&dev->mode_config.idr_mutex);
  5433.         ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
  5434.         if (ret >= 0) {
  5435.                 tg->id = ret;
  5436.         } else {
  5437.                 kfree(tg);
  5438.                 tg = ERR_PTR(ret);
  5439.         }
  5440.  
  5441.         mutex_unlock(&dev->mode_config.idr_mutex);
  5442.         return tg;
  5443. }
  5444. EXPORT_SYMBOL(drm_mode_create_tile_group);
  5445.