Subversion Repositories Kolibri OS

Rev

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

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