drm: Add plane type property (v2)
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / drm_crtc.c
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
41 #include "drm_crtc_internal.h"
42
43 /**
44  * drm_modeset_lock_all - take all modeset locks
45  * @dev: drm device
46  *
47  * This function takes all modeset locks, suitable where a more fine-grained
48  * scheme isn't (yet) implemented. Locks must be dropped with
49  * drm_modeset_unlock_all.
50  */
51 void drm_modeset_lock_all(struct drm_device *dev)
52 {
53         struct drm_crtc *crtc;
54
55         mutex_lock(&dev->mode_config.mutex);
56
57         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
58                 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
59 }
60 EXPORT_SYMBOL(drm_modeset_lock_all);
61
62 /**
63  * drm_modeset_unlock_all - drop all modeset locks
64  * @dev: device
65  *
66  * This function drop all modeset locks taken by drm_modeset_lock_all.
67  */
68 void drm_modeset_unlock_all(struct drm_device *dev)
69 {
70         struct drm_crtc *crtc;
71
72         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
73                 mutex_unlock(&crtc->mutex);
74
75         mutex_unlock(&dev->mode_config.mutex);
76 }
77 EXPORT_SYMBOL(drm_modeset_unlock_all);
78
79 /**
80  * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
81  * @dev: device
82  *
83  * Useful as a debug assert.
84  */
85 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
86 {
87         struct drm_crtc *crtc;
88
89         /* Locking is currently fubar in the panic handler. */
90         if (oops_in_progress)
91                 return;
92
93         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
94                 WARN_ON(!mutex_is_locked(&crtc->mutex));
95
96         WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
97 }
98 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
99
100 /* Avoid boilerplate.  I'm tired of typing. */
101 #define DRM_ENUM_NAME_FN(fnname, list)                          \
102         const char *fnname(int val)                             \
103         {                                                       \
104                 int i;                                          \
105                 for (i = 0; i < ARRAY_SIZE(list); i++) {        \
106                         if (list[i].type == val)                \
107                                 return list[i].name;            \
108                 }                                               \
109                 return "(unknown)";                             \
110         }
111
112 /*
113  * Global properties
114  */
115 static const struct drm_prop_enum_list drm_dpms_enum_list[] =
116 {       { DRM_MODE_DPMS_ON, "On" },
117         { DRM_MODE_DPMS_STANDBY, "Standby" },
118         { DRM_MODE_DPMS_SUSPEND, "Suspend" },
119         { DRM_MODE_DPMS_OFF, "Off" }
120 };
121
122 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
123
124 static const struct drm_prop_enum_list drm_plane_type_enum_list[] =
125 {
126         { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
127         { DRM_PLANE_TYPE_PRIMARY, "Primary" },
128         { DRM_PLANE_TYPE_CURSOR, "Cursor" },
129 };
130
131 /*
132  * Optional properties
133  */
134 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
135 {
136         { DRM_MODE_SCALE_NONE, "None" },
137         { DRM_MODE_SCALE_FULLSCREEN, "Full" },
138         { DRM_MODE_SCALE_CENTER, "Center" },
139         { DRM_MODE_SCALE_ASPECT, "Full aspect" },
140 };
141
142 /*
143  * Non-global properties, but "required" for certain connectors.
144  */
145 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
146 {
147         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
148         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
149         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
150 };
151
152 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
153
154 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
155 {
156         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
157         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
158         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
159 };
160
161 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
162                  drm_dvi_i_subconnector_enum_list)
163
164 static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
165 {
166         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
167         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
168         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
169         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
170         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
171 };
172
173 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
174
175 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
176 {
177         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
178         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
179         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
180         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
181         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
182 };
183
184 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
185                  drm_tv_subconnector_enum_list)
186
187 static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
188         { DRM_MODE_DIRTY_OFF,      "Off"      },
189         { DRM_MODE_DIRTY_ON,       "On"       },
190         { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
191 };
192
193 struct drm_conn_prop_enum_list {
194         int type;
195         const char *name;
196         struct ida ida;
197 };
198
199 /*
200  * Connector and encoder types.
201  */
202 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
203 {       { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
204         { DRM_MODE_CONNECTOR_VGA, "VGA" },
205         { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
206         { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
207         { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
208         { DRM_MODE_CONNECTOR_Composite, "Composite" },
209         { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
210         { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
211         { DRM_MODE_CONNECTOR_Component, "Component" },
212         { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
213         { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
214         { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
215         { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
216         { DRM_MODE_CONNECTOR_TV, "TV" },
217         { DRM_MODE_CONNECTOR_eDP, "eDP" },
218         { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
219         { DRM_MODE_CONNECTOR_DSI, "DSI" },
220 };
221
222 static const struct drm_prop_enum_list drm_encoder_enum_list[] =
223 {       { DRM_MODE_ENCODER_NONE, "None" },
224         { DRM_MODE_ENCODER_DAC, "DAC" },
225         { DRM_MODE_ENCODER_TMDS, "TMDS" },
226         { DRM_MODE_ENCODER_LVDS, "LVDS" },
227         { DRM_MODE_ENCODER_TVDAC, "TV" },
228         { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
229         { DRM_MODE_ENCODER_DSI, "DSI" },
230 };
231
232 static const struct drm_prop_enum_list drm_subpixel_enum_list[] =
233 {
234         { SubPixelUnknown, "Unknown" },
235         { SubPixelHorizontalRGB, "Horizontal RGB" },
236         { SubPixelHorizontalBGR, "Horizontal BGR" },
237         { SubPixelVerticalRGB, "Vertical RGB" },
238         { SubPixelVerticalBGR, "Vertical BGR" },
239         { SubPixelNone, "None" },
240 };
241
242 void drm_connector_ida_init(void)
243 {
244         int i;
245
246         for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
247                 ida_init(&drm_connector_enum_list[i].ida);
248 }
249
250 void drm_connector_ida_destroy(void)
251 {
252         int i;
253
254         for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
255                 ida_destroy(&drm_connector_enum_list[i].ida);
256 }
257
258 /**
259  * drm_get_encoder_name - return a string for encoder
260  * @encoder: encoder to compute name of
261  *
262  * Note that the buffer used by this function is globally shared and owned by
263  * the function itself.
264  *
265  * FIXME: This isn't really multithreading safe.
266  */
267 const char *drm_get_encoder_name(const struct drm_encoder *encoder)
268 {
269         static char buf[32];
270
271         snprintf(buf, 32, "%s-%d",
272                  drm_encoder_enum_list[encoder->encoder_type].name,
273                  encoder->base.id);
274         return buf;
275 }
276 EXPORT_SYMBOL(drm_get_encoder_name);
277
278 /**
279  * drm_get_connector_name - return a string for connector
280  * @connector: connector to compute name of
281  *
282  * Note that the buffer used by this function is globally shared and owned by
283  * the function itself.
284  *
285  * FIXME: This isn't really multithreading safe.
286  */
287 const char *drm_get_connector_name(const struct drm_connector *connector)
288 {
289         static char buf[32];
290
291         snprintf(buf, 32, "%s-%d",
292                  drm_connector_enum_list[connector->connector_type].name,
293                  connector->connector_type_id);
294         return buf;
295 }
296 EXPORT_SYMBOL(drm_get_connector_name);
297
298 /**
299  * drm_get_connector_status_name - return a string for connector status
300  * @status: connector status to compute name of
301  *
302  * In contrast to the other drm_get_*_name functions this one here returns a
303  * const pointer and hence is threadsafe.
304  */
305 const char *drm_get_connector_status_name(enum drm_connector_status status)
306 {
307         if (status == connector_status_connected)
308                 return "connected";
309         else if (status == connector_status_disconnected)
310                 return "disconnected";
311         else
312                 return "unknown";
313 }
314 EXPORT_SYMBOL(drm_get_connector_status_name);
315
316 /**
317  * drm_get_subpixel_order_name - return a string for a given subpixel enum
318  * @order: enum of subpixel_order
319  *
320  * Note you could abuse this and return something out of bounds, but that
321  * would be a caller error.  No unscrubbed user data should make it here.
322  */
323 const char *drm_get_subpixel_order_name(enum subpixel_order order)
324 {
325         return drm_subpixel_enum_list[order].name;
326 }
327 EXPORT_SYMBOL(drm_get_subpixel_order_name);
328
329 static char printable_char(int c)
330 {
331         return isascii(c) && isprint(c) ? c : '?';
332 }
333
334 /**
335  * drm_get_format_name - return a string for drm fourcc format
336  * @format: format to compute name of
337  *
338  * Note that the buffer used by this function is globally shared and owned by
339  * the function itself.
340  *
341  * FIXME: This isn't really multithreading safe.
342  */
343 const char *drm_get_format_name(uint32_t format)
344 {
345         static char buf[32];
346
347         snprintf(buf, sizeof(buf),
348                  "%c%c%c%c %s-endian (0x%08x)",
349                  printable_char(format & 0xff),
350                  printable_char((format >> 8) & 0xff),
351                  printable_char((format >> 16) & 0xff),
352                  printable_char((format >> 24) & 0x7f),
353                  format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
354                  format);
355
356         return buf;
357 }
358 EXPORT_SYMBOL(drm_get_format_name);
359
360 /**
361  * drm_mode_object_get - allocate a new modeset identifier
362  * @dev: DRM device
363  * @obj: object pointer, used to generate unique ID
364  * @obj_type: object type
365  *
366  * Create a unique identifier based on @ptr in @dev's identifier space.  Used
367  * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
368  * modeset identifiers are _not_ reference counted. Hence don't use this for
369  * reference counted modeset objects like framebuffers.
370  *
371  * Returns:
372  * New unique (relative to other objects in @dev) integer identifier for the
373  * object.
374  */
375 int drm_mode_object_get(struct drm_device *dev,
376                         struct drm_mode_object *obj, uint32_t obj_type)
377 {
378         int ret;
379
380         mutex_lock(&dev->mode_config.idr_mutex);
381         ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
382         if (ret >= 0) {
383                 /*
384                  * Set up the object linking under the protection of the idr
385                  * lock so that other users can't see inconsistent state.
386                  */
387                 obj->id = ret;
388                 obj->type = obj_type;
389         }
390         mutex_unlock(&dev->mode_config.idr_mutex);
391
392         return ret < 0 ? ret : 0;
393 }
394
395 /**
396  * drm_mode_object_put - free a modeset identifer
397  * @dev: DRM device
398  * @object: object to free
399  *
400  * Free @id from @dev's unique identifier pool. Note that despite the _get
401  * postfix modeset identifiers are _not_ reference counted. Hence don't use this
402  * for reference counted modeset objects like framebuffers.
403  */
404 void drm_mode_object_put(struct drm_device *dev,
405                          struct drm_mode_object *object)
406 {
407         mutex_lock(&dev->mode_config.idr_mutex);
408         idr_remove(&dev->mode_config.crtc_idr, object->id);
409         mutex_unlock(&dev->mode_config.idr_mutex);
410 }
411
412 /**
413  * drm_mode_object_find - look up a drm object with static lifetime
414  * @dev: drm device
415  * @id: id of the mode object
416  * @type: type of the mode object
417  *
418  * Note that framebuffers cannot be looked up with this functions - since those
419  * are reference counted, they need special treatment.
420  */
421 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
422                 uint32_t id, uint32_t type)
423 {
424         struct drm_mode_object *obj = NULL;
425
426         /* Framebuffers are reference counted and need their own lookup
427          * function.*/
428         WARN_ON(type == DRM_MODE_OBJECT_FB);
429
430         mutex_lock(&dev->mode_config.idr_mutex);
431         obj = idr_find(&dev->mode_config.crtc_idr, id);
432         if (!obj || (obj->type != type) || (obj->id != id))
433                 obj = NULL;
434         mutex_unlock(&dev->mode_config.idr_mutex);
435
436         return obj;
437 }
438 EXPORT_SYMBOL(drm_mode_object_find);
439
440 /**
441  * drm_framebuffer_init - initialize a framebuffer
442  * @dev: DRM device
443  * @fb: framebuffer to be initialized
444  * @funcs: ... with these functions
445  *
446  * Allocates an ID for the framebuffer's parent mode object, sets its mode
447  * functions & device file and adds it to the master fd list.
448  *
449  * IMPORTANT:
450  * This functions publishes the fb and makes it available for concurrent access
451  * by other users. Which means by this point the fb _must_ be fully set up -
452  * since all the fb attributes are invariant over its lifetime, no further
453  * locking but only correct reference counting is required.
454  *
455  * Returns:
456  * Zero on success, error code on failure.
457  */
458 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
459                          const struct drm_framebuffer_funcs *funcs)
460 {
461         int ret;
462
463         mutex_lock(&dev->mode_config.fb_lock);
464         kref_init(&fb->refcount);
465         INIT_LIST_HEAD(&fb->filp_head);
466         fb->dev = dev;
467         fb->funcs = funcs;
468
469         ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
470         if (ret)
471                 goto out;
472
473         /* Grab the idr reference. */
474         drm_framebuffer_reference(fb);
475
476         dev->mode_config.num_fb++;
477         list_add(&fb->head, &dev->mode_config.fb_list);
478 out:
479         mutex_unlock(&dev->mode_config.fb_lock);
480
481         return 0;
482 }
483 EXPORT_SYMBOL(drm_framebuffer_init);
484
485 static void drm_framebuffer_free(struct kref *kref)
486 {
487         struct drm_framebuffer *fb =
488                         container_of(kref, struct drm_framebuffer, refcount);
489         fb->funcs->destroy(fb);
490 }
491
492 static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
493                                                         uint32_t id)
494 {
495         struct drm_mode_object *obj = NULL;
496         struct drm_framebuffer *fb;
497
498         mutex_lock(&dev->mode_config.idr_mutex);
499         obj = idr_find(&dev->mode_config.crtc_idr, id);
500         if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
501                 fb = NULL;
502         else
503                 fb = obj_to_fb(obj);
504         mutex_unlock(&dev->mode_config.idr_mutex);
505
506         return fb;
507 }
508
509 /**
510  * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
511  * @dev: drm device
512  * @id: id of the fb object
513  *
514  * If successful, this grabs an additional reference to the framebuffer -
515  * callers need to make sure to eventually unreference the returned framebuffer
516  * again, using @drm_framebuffer_unreference.
517  */
518 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
519                                                uint32_t id)
520 {
521         struct drm_framebuffer *fb;
522
523         mutex_lock(&dev->mode_config.fb_lock);
524         fb = __drm_framebuffer_lookup(dev, id);
525         if (fb)
526                 drm_framebuffer_reference(fb);
527         mutex_unlock(&dev->mode_config.fb_lock);
528
529         return fb;
530 }
531 EXPORT_SYMBOL(drm_framebuffer_lookup);
532
533 /**
534  * drm_framebuffer_unreference - unref a framebuffer
535  * @fb: framebuffer to unref
536  *
537  * This functions decrements the fb's refcount and frees it if it drops to zero.
538  */
539 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
540 {
541         DRM_DEBUG("FB ID: %d\n", fb->base.id);
542         kref_put(&fb->refcount, drm_framebuffer_free);
543 }
544 EXPORT_SYMBOL(drm_framebuffer_unreference);
545
546 /**
547  * drm_framebuffer_reference - incr the fb refcnt
548  * @fb: framebuffer
549  *
550  * This functions increments the fb's refcount.
551  */
552 void drm_framebuffer_reference(struct drm_framebuffer *fb)
553 {
554         DRM_DEBUG("FB ID: %d\n", fb->base.id);
555         kref_get(&fb->refcount);
556 }
557 EXPORT_SYMBOL(drm_framebuffer_reference);
558
559 static void drm_framebuffer_free_bug(struct kref *kref)
560 {
561         BUG();
562 }
563
564 static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
565 {
566         DRM_DEBUG("FB ID: %d\n", fb->base.id);
567         kref_put(&fb->refcount, drm_framebuffer_free_bug);
568 }
569
570 /* dev->mode_config.fb_lock must be held! */
571 static void __drm_framebuffer_unregister(struct drm_device *dev,
572                                          struct drm_framebuffer *fb)
573 {
574         mutex_lock(&dev->mode_config.idr_mutex);
575         idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
576         mutex_unlock(&dev->mode_config.idr_mutex);
577
578         fb->base.id = 0;
579
580         __drm_framebuffer_unreference(fb);
581 }
582
583 /**
584  * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
585  * @fb: fb to unregister
586  *
587  * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
588  * those used for fbdev. Note that the caller must hold a reference of it's own,
589  * i.e. the object may not be destroyed through this call (since it'll lead to a
590  * locking inversion).
591  */
592 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
593 {
594         struct drm_device *dev = fb->dev;
595
596         mutex_lock(&dev->mode_config.fb_lock);
597         /* Mark fb as reaped and drop idr ref. */
598         __drm_framebuffer_unregister(dev, fb);
599         mutex_unlock(&dev->mode_config.fb_lock);
600 }
601 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
602
603 /**
604  * drm_framebuffer_cleanup - remove a framebuffer object
605  * @fb: framebuffer to remove
606  *
607  * Cleanup framebuffer. This function is intended to be used from the drivers
608  * ->destroy callback. It can also be used to clean up driver private
609  *  framebuffers embedded into a larger structure.
610  *
611  * Note that this function does not remove the fb from active usuage - if it is
612  * still used anywhere, hilarity can ensue since userspace could call getfb on
613  * the id and get back -EINVAL. Obviously no concern at driver unload time.
614  *
615  * Also, the framebuffer will not be removed from the lookup idr - for
616  * user-created framebuffers this will happen in in the rmfb ioctl. For
617  * driver-private objects (e.g. for fbdev) drivers need to explicitly call
618  * drm_framebuffer_unregister_private.
619  */
620 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
621 {
622         struct drm_device *dev = fb->dev;
623
624         mutex_lock(&dev->mode_config.fb_lock);
625         list_del(&fb->head);
626         dev->mode_config.num_fb--;
627         mutex_unlock(&dev->mode_config.fb_lock);
628 }
629 EXPORT_SYMBOL(drm_framebuffer_cleanup);
630
631 /**
632  * drm_framebuffer_remove - remove and unreference a framebuffer object
633  * @fb: framebuffer to remove
634  *
635  * Scans all the CRTCs and planes in @dev's mode_config.  If they're
636  * using @fb, removes it, setting it to NULL. Then drops the reference to the
637  * passed-in framebuffer. Might take the modeset locks.
638  *
639  * Note that this function optimizes the cleanup away if the caller holds the
640  * last reference to the framebuffer. It is also guaranteed to not take the
641  * modeset locks in this case.
642  */
643 void drm_framebuffer_remove(struct drm_framebuffer *fb)
644 {
645         struct drm_device *dev = fb->dev;
646         struct drm_crtc *crtc;
647         struct drm_plane *plane;
648         struct drm_mode_set set;
649         int ret;
650
651         WARN_ON(!list_empty(&fb->filp_head));
652
653         /*
654          * drm ABI mandates that we remove any deleted framebuffers from active
655          * useage. But since most sane clients only remove framebuffers they no
656          * longer need, try to optimize this away.
657          *
658          * Since we're holding a reference ourselves, observing a refcount of 1
659          * means that we're the last holder and can skip it. Also, the refcount
660          * can never increase from 1 again, so we don't need any barriers or
661          * locks.
662          *
663          * Note that userspace could try to race with use and instate a new
664          * usage _after_ we've cleared all current ones. End result will be an
665          * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
666          * in this manner.
667          */
668         if (atomic_read(&fb->refcount.refcount) > 1) {
669                 drm_modeset_lock_all(dev);
670                 /* remove from any CRTC */
671                 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
672                         if (crtc->fb == fb) {
673                                 /* should turn off the crtc */
674                                 memset(&set, 0, sizeof(struct drm_mode_set));
675                                 set.crtc = crtc;
676                                 set.fb = NULL;
677                                 ret = drm_mode_set_config_internal(&set);
678                                 if (ret)
679                                         DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
680                         }
681                 }
682
683                 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
684                         if (plane->fb == fb)
685                                 drm_plane_force_disable(plane);
686                 }
687                 drm_modeset_unlock_all(dev);
688         }
689
690         drm_framebuffer_unreference(fb);
691 }
692 EXPORT_SYMBOL(drm_framebuffer_remove);
693
694 /**
695  * drm_crtc_init - Initialise a new CRTC object
696  * @dev: DRM device
697  * @crtc: CRTC object to init
698  * @funcs: callbacks for the new CRTC
699  *
700  * Inits a new object created as base part of a driver crtc object.
701  *
702  * Returns:
703  * Zero on success, error code on failure.
704  */
705 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
706                    const struct drm_crtc_funcs *funcs)
707 {
708         int ret;
709
710         crtc->dev = dev;
711         crtc->funcs = funcs;
712         crtc->invert_dimensions = false;
713
714         drm_modeset_lock_all(dev);
715         mutex_init(&crtc->mutex);
716         mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
717
718         ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
719         if (ret)
720                 goto out;
721
722         crtc->base.properties = &crtc->properties;
723
724         list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
725         dev->mode_config.num_crtc++;
726
727  out:
728         drm_modeset_unlock_all(dev);
729
730         return ret;
731 }
732 EXPORT_SYMBOL(drm_crtc_init);
733
734 /**
735  * drm_crtc_cleanup - Clean up the core crtc usage
736  * @crtc: CRTC to cleanup
737  *
738  * This function cleans up @crtc and removes it from the DRM mode setting
739  * core. Note that the function does *not* free the crtc structure itself,
740  * this is the responsibility of the caller.
741  */
742 void drm_crtc_cleanup(struct drm_crtc *crtc)
743 {
744         struct drm_device *dev = crtc->dev;
745
746         kfree(crtc->gamma_store);
747         crtc->gamma_store = NULL;
748
749         drm_mode_object_put(dev, &crtc->base);
750         list_del(&crtc->head);
751         dev->mode_config.num_crtc--;
752 }
753 EXPORT_SYMBOL(drm_crtc_cleanup);
754
755 /**
756  * drm_crtc_index - find the index of a registered CRTC
757  * @crtc: CRTC to find index for
758  *
759  * Given a registered CRTC, return the index of that CRTC within a DRM
760  * device's list of CRTCs.
761  */
762 unsigned int drm_crtc_index(struct drm_crtc *crtc)
763 {
764         unsigned int index = 0;
765         struct drm_crtc *tmp;
766
767         list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
768                 if (tmp == crtc)
769                         return index;
770
771                 index++;
772         }
773
774         BUG();
775 }
776 EXPORT_SYMBOL(drm_crtc_index);
777
778 /*
779  * drm_mode_remove - remove and free a mode
780  * @connector: connector list to modify
781  * @mode: mode to remove
782  *
783  * Remove @mode from @connector's mode list, then free it.
784  */
785 static void drm_mode_remove(struct drm_connector *connector,
786                             struct drm_display_mode *mode)
787 {
788         list_del(&mode->head);
789         drm_mode_destroy(connector->dev, mode);
790 }
791
792 /**
793  * drm_connector_init - Init a preallocated connector
794  * @dev: DRM device
795  * @connector: the connector to init
796  * @funcs: callbacks for this connector
797  * @connector_type: user visible type of the connector
798  *
799  * Initialises a preallocated connector. Connectors should be
800  * subclassed as part of driver connector objects.
801  *
802  * Returns:
803  * Zero on success, error code on failure.
804  */
805 int drm_connector_init(struct drm_device *dev,
806                        struct drm_connector *connector,
807                        const struct drm_connector_funcs *funcs,
808                        int connector_type)
809 {
810         int ret;
811         struct ida *connector_ida =
812                 &drm_connector_enum_list[connector_type].ida;
813
814         drm_modeset_lock_all(dev);
815
816         ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
817         if (ret)
818                 goto out;
819
820         connector->base.properties = &connector->properties;
821         connector->dev = dev;
822         connector->funcs = funcs;
823         connector->connector_type = connector_type;
824         connector->connector_type_id =
825                 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
826         if (connector->connector_type_id < 0) {
827                 ret = connector->connector_type_id;
828                 drm_mode_object_put(dev, &connector->base);
829                 goto out;
830         }
831         INIT_LIST_HEAD(&connector->probed_modes);
832         INIT_LIST_HEAD(&connector->modes);
833         connector->edid_blob_ptr = NULL;
834         connector->status = connector_status_unknown;
835
836         list_add_tail(&connector->head, &dev->mode_config.connector_list);
837         dev->mode_config.num_connector++;
838
839         if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
840                 drm_object_attach_property(&connector->base,
841                                               dev->mode_config.edid_property,
842                                               0);
843
844         drm_object_attach_property(&connector->base,
845                                       dev->mode_config.dpms_property, 0);
846
847  out:
848         drm_modeset_unlock_all(dev);
849
850         return ret;
851 }
852 EXPORT_SYMBOL(drm_connector_init);
853
854 /**
855  * drm_connector_cleanup - cleans up an initialised connector
856  * @connector: connector to cleanup
857  *
858  * Cleans up the connector but doesn't free the object.
859  */
860 void drm_connector_cleanup(struct drm_connector *connector)
861 {
862         struct drm_device *dev = connector->dev;
863         struct drm_display_mode *mode, *t;
864
865         list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
866                 drm_mode_remove(connector, mode);
867
868         list_for_each_entry_safe(mode, t, &connector->modes, head)
869                 drm_mode_remove(connector, mode);
870
871         ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
872                    connector->connector_type_id);
873
874         drm_mode_object_put(dev, &connector->base);
875         list_del(&connector->head);
876         dev->mode_config.num_connector--;
877 }
878 EXPORT_SYMBOL(drm_connector_cleanup);
879
880 /**
881  * drm_connector_unplug_all - unregister connector userspace interfaces
882  * @dev: drm device
883  *
884  * This function unregisters all connector userspace interfaces in sysfs. Should
885  * be call when the device is disconnected, e.g. from an usb driver's
886  * ->disconnect callback.
887  */
888 void drm_connector_unplug_all(struct drm_device *dev)
889 {
890         struct drm_connector *connector;
891
892         /* taking the mode config mutex ends up in a clash with sysfs */
893         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
894                 drm_sysfs_connector_remove(connector);
895
896 }
897 EXPORT_SYMBOL(drm_connector_unplug_all);
898
899 /**
900  * drm_bridge_init - initialize a drm transcoder/bridge
901  * @dev: drm device
902  * @bridge: transcoder/bridge to set up
903  * @funcs: bridge function table
904  *
905  * Initialises a preallocated bridge. Bridges should be
906  * subclassed as part of driver connector objects.
907  *
908  * Returns:
909  * Zero on success, error code on failure.
910  */
911 int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
912                 const struct drm_bridge_funcs *funcs)
913 {
914         int ret;
915
916         drm_modeset_lock_all(dev);
917
918         ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
919         if (ret)
920                 goto out;
921
922         bridge->dev = dev;
923         bridge->funcs = funcs;
924
925         list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
926         dev->mode_config.num_bridge++;
927
928  out:
929         drm_modeset_unlock_all(dev);
930         return ret;
931 }
932 EXPORT_SYMBOL(drm_bridge_init);
933
934 /**
935  * drm_bridge_cleanup - cleans up an initialised bridge
936  * @bridge: bridge to cleanup
937  *
938  * Cleans up the bridge but doesn't free the object.
939  */
940 void drm_bridge_cleanup(struct drm_bridge *bridge)
941 {
942         struct drm_device *dev = bridge->dev;
943
944         drm_modeset_lock_all(dev);
945         drm_mode_object_put(dev, &bridge->base);
946         list_del(&bridge->head);
947         dev->mode_config.num_bridge--;
948         drm_modeset_unlock_all(dev);
949 }
950 EXPORT_SYMBOL(drm_bridge_cleanup);
951
952 /**
953  * drm_encoder_init - Init a preallocated encoder
954  * @dev: drm device
955  * @encoder: the encoder to init
956  * @funcs: callbacks for this encoder
957  * @encoder_type: user visible type of the encoder
958  *
959  * Initialises a preallocated encoder. Encoder should be
960  * subclassed as part of driver encoder objects.
961  *
962  * Returns:
963  * Zero on success, error code on failure.
964  */
965 int drm_encoder_init(struct drm_device *dev,
966                       struct drm_encoder *encoder,
967                       const struct drm_encoder_funcs *funcs,
968                       int encoder_type)
969 {
970         int ret;
971
972         drm_modeset_lock_all(dev);
973
974         ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
975         if (ret)
976                 goto out;
977
978         encoder->dev = dev;
979         encoder->encoder_type = encoder_type;
980         encoder->funcs = funcs;
981
982         list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
983         dev->mode_config.num_encoder++;
984
985  out:
986         drm_modeset_unlock_all(dev);
987
988         return ret;
989 }
990 EXPORT_SYMBOL(drm_encoder_init);
991
992 /**
993  * drm_encoder_cleanup - cleans up an initialised encoder
994  * @encoder: encoder to cleanup
995  *
996  * Cleans up the encoder but doesn't free the object.
997  */
998 void drm_encoder_cleanup(struct drm_encoder *encoder)
999 {
1000         struct drm_device *dev = encoder->dev;
1001         drm_modeset_lock_all(dev);
1002         drm_mode_object_put(dev, &encoder->base);
1003         list_del(&encoder->head);
1004         dev->mode_config.num_encoder--;
1005         drm_modeset_unlock_all(dev);
1006 }
1007 EXPORT_SYMBOL(drm_encoder_cleanup);
1008
1009 /**
1010  * drm_universal_plane_init - Initialize a new universal plane object
1011  * @dev: DRM device
1012  * @plane: plane object to init
1013  * @possible_crtcs: bitmask of possible CRTCs
1014  * @funcs: callbacks for the new plane
1015  * @formats: array of supported formats (%DRM_FORMAT_*)
1016  * @format_count: number of elements in @formats
1017  * @type: type of plane (overlay, primary, cursor)
1018  *
1019  * Initializes a plane object of type @type.
1020  *
1021  * Returns:
1022  * Zero on success, error code on failure.
1023  */
1024 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1025                              unsigned long possible_crtcs,
1026                              const struct drm_plane_funcs *funcs,
1027                              const uint32_t *formats, uint32_t format_count,
1028                              enum drm_plane_type type)
1029 {
1030         int ret;
1031
1032         drm_modeset_lock_all(dev);
1033
1034         ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1035         if (ret)
1036                 goto out;
1037
1038         plane->base.properties = &plane->properties;
1039         plane->dev = dev;
1040         plane->funcs = funcs;
1041         plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1042                                       GFP_KERNEL);
1043         if (!plane->format_types) {
1044                 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1045                 drm_mode_object_put(dev, &plane->base);
1046                 ret = -ENOMEM;
1047                 goto out;
1048         }
1049
1050         memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1051         plane->format_count = format_count;
1052         plane->possible_crtcs = possible_crtcs;
1053         plane->type = type;
1054
1055         list_add_tail(&plane->head, &dev->mode_config.plane_list);
1056         dev->mode_config.num_total_plane++;
1057         if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1058                 dev->mode_config.num_overlay_plane++;
1059
1060         drm_object_attach_property(&plane->base,
1061                                    dev->mode_config.plane_type_property,
1062                                    plane->type);
1063
1064  out:
1065         drm_modeset_unlock_all(dev);
1066
1067         return ret;
1068 }
1069 EXPORT_SYMBOL(drm_universal_plane_init);
1070
1071 /**
1072  * drm_plane_init - Initialize a legacy plane
1073  * @dev: DRM device
1074  * @plane: plane object to init
1075  * @possible_crtcs: bitmask of possible CRTCs
1076  * @funcs: callbacks for the new plane
1077  * @formats: array of supported formats (%DRM_FORMAT_*)
1078  * @format_count: number of elements in @formats
1079  * @is_primary: plane type (primary vs overlay)
1080  *
1081  * Legacy API to initialize a DRM plane.
1082  *
1083  * New drivers should call drm_universal_plane_init() instead.
1084  *
1085  * Returns:
1086  * Zero on success, error code on failure.
1087  */
1088 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1089                    unsigned long possible_crtcs,
1090                    const struct drm_plane_funcs *funcs,
1091                    const uint32_t *formats, uint32_t format_count,
1092                    bool is_primary)
1093 {
1094         enum drm_plane_type type;
1095
1096         type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1097         return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1098                                         formats, format_count, type);
1099 }
1100 EXPORT_SYMBOL(drm_plane_init);
1101
1102 /**
1103  * drm_plane_cleanup - Clean up the core plane usage
1104  * @plane: plane to cleanup
1105  *
1106  * This function cleans up @plane and removes it from the DRM mode setting
1107  * core. Note that the function does *not* free the plane structure itself,
1108  * this is the responsibility of the caller.
1109  */
1110 void drm_plane_cleanup(struct drm_plane *plane)
1111 {
1112         struct drm_device *dev = plane->dev;
1113
1114         drm_modeset_lock_all(dev);
1115         kfree(plane->format_types);
1116         drm_mode_object_put(dev, &plane->base);
1117
1118         BUG_ON(list_empty(&plane->head));
1119
1120         list_del(&plane->head);
1121         dev->mode_config.num_total_plane--;
1122         if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1123                 dev->mode_config.num_overlay_plane--;
1124         drm_modeset_unlock_all(dev);
1125 }
1126 EXPORT_SYMBOL(drm_plane_cleanup);
1127
1128 /**
1129  * drm_plane_force_disable - Forcibly disable a plane
1130  * @plane: plane to disable
1131  *
1132  * Forces the plane to be disabled.
1133  *
1134  * Used when the plane's current framebuffer is destroyed,
1135  * and when restoring fbdev mode.
1136  */
1137 void drm_plane_force_disable(struct drm_plane *plane)
1138 {
1139         int ret;
1140
1141         if (!plane->fb)
1142                 return;
1143
1144         ret = plane->funcs->disable_plane(plane);
1145         if (ret)
1146                 DRM_ERROR("failed to disable plane with busy fb\n");
1147         /* disconnect the plane from the fb and crtc: */
1148         __drm_framebuffer_unreference(plane->fb);
1149         plane->fb = NULL;
1150         plane->crtc = NULL;
1151 }
1152 EXPORT_SYMBOL(drm_plane_force_disable);
1153
1154 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1155 {
1156         struct drm_property *edid;
1157         struct drm_property *dpms;
1158
1159         /*
1160          * Standard properties (apply to all connectors)
1161          */
1162         edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1163                                    DRM_MODE_PROP_IMMUTABLE,
1164                                    "EDID", 0);
1165         dev->mode_config.edid_property = edid;
1166
1167         dpms = drm_property_create_enum(dev, 0,
1168                                    "DPMS", drm_dpms_enum_list,
1169                                    ARRAY_SIZE(drm_dpms_enum_list));
1170         dev->mode_config.dpms_property = dpms;
1171
1172         return 0;
1173 }
1174
1175 static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1176 {
1177         struct drm_property *type;
1178
1179         /*
1180          * Standard properties (apply to all planes)
1181          */
1182         type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1183                                         "type", drm_plane_type_enum_list,
1184                                         ARRAY_SIZE(drm_plane_type_enum_list));
1185         dev->mode_config.plane_type_property = type;
1186
1187         return 0;
1188 }
1189
1190 /**
1191  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1192  * @dev: DRM device
1193  *
1194  * Called by a driver the first time a DVI-I connector is made.
1195  */
1196 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1197 {
1198         struct drm_property *dvi_i_selector;
1199         struct drm_property *dvi_i_subconnector;
1200
1201         if (dev->mode_config.dvi_i_select_subconnector_property)
1202                 return 0;
1203
1204         dvi_i_selector =
1205                 drm_property_create_enum(dev, 0,
1206                                     "select subconnector",
1207                                     drm_dvi_i_select_enum_list,
1208                                     ARRAY_SIZE(drm_dvi_i_select_enum_list));
1209         dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1210
1211         dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1212                                     "subconnector",
1213                                     drm_dvi_i_subconnector_enum_list,
1214                                     ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1215         dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1216
1217         return 0;
1218 }
1219 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1220
1221 /**
1222  * drm_create_tv_properties - create TV specific connector properties
1223  * @dev: DRM device
1224  * @num_modes: number of different TV formats (modes) supported
1225  * @modes: array of pointers to strings containing name of each format
1226  *
1227  * Called by a driver's TV initialization routine, this function creates
1228  * the TV specific connector properties for a given device.  Caller is
1229  * responsible for allocating a list of format names and passing them to
1230  * this routine.
1231  */
1232 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1233                                   char *modes[])
1234 {
1235         struct drm_property *tv_selector;
1236         struct drm_property *tv_subconnector;
1237         int i;
1238
1239         if (dev->mode_config.tv_select_subconnector_property)
1240                 return 0;
1241
1242         /*
1243          * Basic connector properties
1244          */
1245         tv_selector = drm_property_create_enum(dev, 0,
1246                                           "select subconnector",
1247                                           drm_tv_select_enum_list,
1248                                           ARRAY_SIZE(drm_tv_select_enum_list));
1249         dev->mode_config.tv_select_subconnector_property = tv_selector;
1250
1251         tv_subconnector =
1252                 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1253                                     "subconnector",
1254                                     drm_tv_subconnector_enum_list,
1255                                     ARRAY_SIZE(drm_tv_subconnector_enum_list));
1256         dev->mode_config.tv_subconnector_property = tv_subconnector;
1257
1258         /*
1259          * Other, TV specific properties: margins & TV modes.
1260          */
1261         dev->mode_config.tv_left_margin_property =
1262                 drm_property_create_range(dev, 0, "left margin", 0, 100);
1263
1264         dev->mode_config.tv_right_margin_property =
1265                 drm_property_create_range(dev, 0, "right margin", 0, 100);
1266
1267         dev->mode_config.tv_top_margin_property =
1268                 drm_property_create_range(dev, 0, "top margin", 0, 100);
1269
1270         dev->mode_config.tv_bottom_margin_property =
1271                 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1272
1273         dev->mode_config.tv_mode_property =
1274                 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1275                                     "mode", num_modes);
1276         for (i = 0; i < num_modes; i++)
1277                 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1278                                       i, modes[i]);
1279
1280         dev->mode_config.tv_brightness_property =
1281                 drm_property_create_range(dev, 0, "brightness", 0, 100);
1282
1283         dev->mode_config.tv_contrast_property =
1284                 drm_property_create_range(dev, 0, "contrast", 0, 100);
1285
1286         dev->mode_config.tv_flicker_reduction_property =
1287                 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1288
1289         dev->mode_config.tv_overscan_property =
1290                 drm_property_create_range(dev, 0, "overscan", 0, 100);
1291
1292         dev->mode_config.tv_saturation_property =
1293                 drm_property_create_range(dev, 0, "saturation", 0, 100);
1294
1295         dev->mode_config.tv_hue_property =
1296                 drm_property_create_range(dev, 0, "hue", 0, 100);
1297
1298         return 0;
1299 }
1300 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1301
1302 /**
1303  * drm_mode_create_scaling_mode_property - create scaling mode property
1304  * @dev: DRM device
1305  *
1306  * Called by a driver the first time it's needed, must be attached to desired
1307  * connectors.
1308  */
1309 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1310 {
1311         struct drm_property *scaling_mode;
1312
1313         if (dev->mode_config.scaling_mode_property)
1314                 return 0;
1315
1316         scaling_mode =
1317                 drm_property_create_enum(dev, 0, "scaling mode",
1318                                 drm_scaling_mode_enum_list,
1319                                     ARRAY_SIZE(drm_scaling_mode_enum_list));
1320
1321         dev->mode_config.scaling_mode_property = scaling_mode;
1322
1323         return 0;
1324 }
1325 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1326
1327 /**
1328  * drm_mode_create_dirty_property - create dirty property
1329  * @dev: DRM device
1330  *
1331  * Called by a driver the first time it's needed, must be attached to desired
1332  * connectors.
1333  */
1334 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1335 {
1336         struct drm_property *dirty_info;
1337
1338         if (dev->mode_config.dirty_info_property)
1339                 return 0;
1340
1341         dirty_info =
1342                 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1343                                     "dirty",
1344                                     drm_dirty_info_enum_list,
1345                                     ARRAY_SIZE(drm_dirty_info_enum_list));
1346         dev->mode_config.dirty_info_property = dirty_info;
1347
1348         return 0;
1349 }
1350 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1351
1352 static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1353 {
1354         uint32_t total_objects = 0;
1355
1356         total_objects += dev->mode_config.num_crtc;
1357         total_objects += dev->mode_config.num_connector;
1358         total_objects += dev->mode_config.num_encoder;
1359         total_objects += dev->mode_config.num_bridge;
1360
1361         group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1362         if (!group->id_list)
1363                 return -ENOMEM;
1364
1365         group->num_crtcs = 0;
1366         group->num_connectors = 0;
1367         group->num_encoders = 0;
1368         group->num_bridges = 0;
1369         return 0;
1370 }
1371
1372 /*
1373  * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1374  * the drm core's responsibility to set up mode control groups.
1375  */
1376 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1377                                      struct drm_mode_group *group)
1378 {
1379         struct drm_crtc *crtc;
1380         struct drm_encoder *encoder;
1381         struct drm_connector *connector;
1382         struct drm_bridge *bridge;
1383         int ret;
1384
1385         if ((ret = drm_mode_group_init(dev, group)))
1386                 return ret;
1387
1388         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1389                 group->id_list[group->num_crtcs++] = crtc->base.id;
1390
1391         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1392                 group->id_list[group->num_crtcs + group->num_encoders++] =
1393                 encoder->base.id;
1394
1395         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1396                 group->id_list[group->num_crtcs + group->num_encoders +
1397                                group->num_connectors++] = connector->base.id;
1398
1399         list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1400                 group->id_list[group->num_crtcs + group->num_encoders +
1401                                group->num_connectors + group->num_bridges++] =
1402                                         bridge->base.id;
1403
1404         return 0;
1405 }
1406 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1407
1408 /**
1409  * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1410  * @out: drm_mode_modeinfo struct to return to the user
1411  * @in: drm_display_mode to use
1412  *
1413  * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1414  * the user.
1415  */
1416 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1417                                       const struct drm_display_mode *in)
1418 {
1419         WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1420              in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1421              in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1422              in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1423              in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1424              "timing values too large for mode info\n");
1425
1426         out->clock = in->clock;
1427         out->hdisplay = in->hdisplay;
1428         out->hsync_start = in->hsync_start;
1429         out->hsync_end = in->hsync_end;
1430         out->htotal = in->htotal;
1431         out->hskew = in->hskew;
1432         out->vdisplay = in->vdisplay;
1433         out->vsync_start = in->vsync_start;
1434         out->vsync_end = in->vsync_end;
1435         out->vtotal = in->vtotal;
1436         out->vscan = in->vscan;
1437         out->vrefresh = in->vrefresh;
1438         out->flags = in->flags;
1439         out->type = in->type;
1440         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1441         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1442 }
1443
1444 /**
1445  * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1446  * @out: drm_display_mode to return to the user
1447  * @in: drm_mode_modeinfo to use
1448  *
1449  * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1450  * the caller.
1451  *
1452  * Returns:
1453  * Zero on success, errno on failure.
1454  */
1455 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1456                                   const struct drm_mode_modeinfo *in)
1457 {
1458         if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1459                 return -ERANGE;
1460
1461         if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1462                 return -EINVAL;
1463
1464         out->clock = in->clock;
1465         out->hdisplay = in->hdisplay;
1466         out->hsync_start = in->hsync_start;
1467         out->hsync_end = in->hsync_end;
1468         out->htotal = in->htotal;
1469         out->hskew = in->hskew;
1470         out->vdisplay = in->vdisplay;
1471         out->vsync_start = in->vsync_start;
1472         out->vsync_end = in->vsync_end;
1473         out->vtotal = in->vtotal;
1474         out->vscan = in->vscan;
1475         out->vrefresh = in->vrefresh;
1476         out->flags = in->flags;
1477         out->type = in->type;
1478         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1479         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1480
1481         return 0;
1482 }
1483
1484 /**
1485  * drm_mode_getresources - get graphics configuration
1486  * @dev: drm device for the ioctl
1487  * @data: data pointer for the ioctl
1488  * @file_priv: drm file for the ioctl call
1489  *
1490  * Construct a set of configuration description structures and return
1491  * them to the user, including CRTC, connector and framebuffer configuration.
1492  *
1493  * Called by the user via ioctl.
1494  *
1495  * Returns:
1496  * Zero on success, errno on failure.
1497  */
1498 int drm_mode_getresources(struct drm_device *dev, void *data,
1499                           struct drm_file *file_priv)
1500 {
1501         struct drm_mode_card_res *card_res = data;
1502         struct list_head *lh;
1503         struct drm_framebuffer *fb;
1504         struct drm_connector *connector;
1505         struct drm_crtc *crtc;
1506         struct drm_encoder *encoder;
1507         int ret = 0;
1508         int connector_count = 0;
1509         int crtc_count = 0;
1510         int fb_count = 0;
1511         int encoder_count = 0;
1512         int copied = 0, i;
1513         uint32_t __user *fb_id;
1514         uint32_t __user *crtc_id;
1515         uint32_t __user *connector_id;
1516         uint32_t __user *encoder_id;
1517         struct drm_mode_group *mode_group;
1518
1519         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1520                 return -EINVAL;
1521
1522
1523         mutex_lock(&file_priv->fbs_lock);
1524         /*
1525          * For the non-control nodes we need to limit the list of resources
1526          * by IDs in the group list for this node
1527          */
1528         list_for_each(lh, &file_priv->fbs)
1529                 fb_count++;
1530
1531         /* handle this in 4 parts */
1532         /* FBs */
1533         if (card_res->count_fbs >= fb_count) {
1534                 copied = 0;
1535                 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1536                 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1537                         if (put_user(fb->base.id, fb_id + copied)) {
1538                                 mutex_unlock(&file_priv->fbs_lock);
1539                                 return -EFAULT;
1540                         }
1541                         copied++;
1542                 }
1543         }
1544         card_res->count_fbs = fb_count;
1545         mutex_unlock(&file_priv->fbs_lock);
1546
1547         drm_modeset_lock_all(dev);
1548         if (!drm_is_primary_client(file_priv)) {
1549
1550                 mode_group = NULL;
1551                 list_for_each(lh, &dev->mode_config.crtc_list)
1552                         crtc_count++;
1553
1554                 list_for_each(lh, &dev->mode_config.connector_list)
1555                         connector_count++;
1556
1557                 list_for_each(lh, &dev->mode_config.encoder_list)
1558                         encoder_count++;
1559         } else {
1560
1561                 mode_group = &file_priv->master->minor->mode_group;
1562                 crtc_count = mode_group->num_crtcs;
1563                 connector_count = mode_group->num_connectors;
1564                 encoder_count = mode_group->num_encoders;
1565         }
1566
1567         card_res->max_height = dev->mode_config.max_height;
1568         card_res->min_height = dev->mode_config.min_height;
1569         card_res->max_width = dev->mode_config.max_width;
1570         card_res->min_width = dev->mode_config.min_width;
1571
1572         /* CRTCs */
1573         if (card_res->count_crtcs >= crtc_count) {
1574                 copied = 0;
1575                 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1576                 if (!mode_group) {
1577                         list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1578                                             head) {
1579                                 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1580                                 if (put_user(crtc->base.id, crtc_id + copied)) {
1581                                         ret = -EFAULT;
1582                                         goto out;
1583                                 }
1584                                 copied++;
1585                         }
1586                 } else {
1587                         for (i = 0; i < mode_group->num_crtcs; i++) {
1588                                 if (put_user(mode_group->id_list[i],
1589                                              crtc_id + copied)) {
1590                                         ret = -EFAULT;
1591                                         goto out;
1592                                 }
1593                                 copied++;
1594                         }
1595                 }
1596         }
1597         card_res->count_crtcs = crtc_count;
1598
1599         /* Encoders */
1600         if (card_res->count_encoders >= encoder_count) {
1601                 copied = 0;
1602                 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1603                 if (!mode_group) {
1604                         list_for_each_entry(encoder,
1605                                             &dev->mode_config.encoder_list,
1606                                             head) {
1607                                 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1608                                                 drm_get_encoder_name(encoder));
1609                                 if (put_user(encoder->base.id, encoder_id +
1610                                              copied)) {
1611                                         ret = -EFAULT;
1612                                         goto out;
1613                                 }
1614                                 copied++;
1615                         }
1616                 } else {
1617                         for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1618                                 if (put_user(mode_group->id_list[i],
1619                                              encoder_id + copied)) {
1620                                         ret = -EFAULT;
1621                                         goto out;
1622                                 }
1623                                 copied++;
1624                         }
1625
1626                 }
1627         }
1628         card_res->count_encoders = encoder_count;
1629
1630         /* Connectors */
1631         if (card_res->count_connectors >= connector_count) {
1632                 copied = 0;
1633                 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1634                 if (!mode_group) {
1635                         list_for_each_entry(connector,
1636                                             &dev->mode_config.connector_list,
1637                                             head) {
1638                                 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1639                                         connector->base.id,
1640                                         drm_get_connector_name(connector));
1641                                 if (put_user(connector->base.id,
1642                                              connector_id + copied)) {
1643                                         ret = -EFAULT;
1644                                         goto out;
1645                                 }
1646                                 copied++;
1647                         }
1648                 } else {
1649                         int start = mode_group->num_crtcs +
1650                                 mode_group->num_encoders;
1651                         for (i = start; i < start + mode_group->num_connectors; i++) {
1652                                 if (put_user(mode_group->id_list[i],
1653                                              connector_id + copied)) {
1654                                         ret = -EFAULT;
1655                                         goto out;
1656                                 }
1657                                 copied++;
1658                         }
1659                 }
1660         }
1661         card_res->count_connectors = connector_count;
1662
1663         DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1664                   card_res->count_connectors, card_res->count_encoders);
1665
1666 out:
1667         drm_modeset_unlock_all(dev);
1668         return ret;
1669 }
1670
1671 /**
1672  * drm_mode_getcrtc - get CRTC configuration
1673  * @dev: drm device for the ioctl
1674  * @data: data pointer for the ioctl
1675  * @file_priv: drm file for the ioctl call
1676  *
1677  * Construct a CRTC configuration structure to return to the user.
1678  *
1679  * Called by the user via ioctl.
1680  *
1681  * Returns:
1682  * Zero on success, errno on failure.
1683  */
1684 int drm_mode_getcrtc(struct drm_device *dev,
1685                      void *data, struct drm_file *file_priv)
1686 {
1687         struct drm_mode_crtc *crtc_resp = data;
1688         struct drm_crtc *crtc;
1689         struct drm_mode_object *obj;
1690         int ret = 0;
1691
1692         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1693                 return -EINVAL;
1694
1695         drm_modeset_lock_all(dev);
1696
1697         obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1698                                    DRM_MODE_OBJECT_CRTC);
1699         if (!obj) {
1700                 ret = -ENOENT;
1701                 goto out;
1702         }
1703         crtc = obj_to_crtc(obj);
1704
1705         crtc_resp->x = crtc->x;
1706         crtc_resp->y = crtc->y;
1707         crtc_resp->gamma_size = crtc->gamma_size;
1708         if (crtc->fb)
1709                 crtc_resp->fb_id = crtc->fb->base.id;
1710         else
1711                 crtc_resp->fb_id = 0;
1712
1713         if (crtc->enabled) {
1714
1715                 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1716                 crtc_resp->mode_valid = 1;
1717
1718         } else {
1719                 crtc_resp->mode_valid = 0;
1720         }
1721
1722 out:
1723         drm_modeset_unlock_all(dev);
1724         return ret;
1725 }
1726
1727 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1728                                          const struct drm_file *file_priv)
1729 {
1730         /*
1731          * If user-space hasn't configured the driver to expose the stereo 3D
1732          * modes, don't expose them.
1733          */
1734         if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1735                 return false;
1736
1737         return true;
1738 }
1739
1740 /**
1741  * drm_mode_getconnector - get connector configuration
1742  * @dev: drm device for the ioctl
1743  * @data: data pointer for the ioctl
1744  * @file_priv: drm file for the ioctl call
1745  *
1746  * Construct a connector configuration structure to return to the user.
1747  *
1748  * Called by the user via ioctl.
1749  *
1750  * Returns:
1751  * Zero on success, errno on failure.
1752  */
1753 int drm_mode_getconnector(struct drm_device *dev, void *data,
1754                           struct drm_file *file_priv)
1755 {
1756         struct drm_mode_get_connector *out_resp = data;
1757         struct drm_mode_object *obj;
1758         struct drm_connector *connector;
1759         struct drm_display_mode *mode;
1760         int mode_count = 0;
1761         int props_count = 0;
1762         int encoders_count = 0;
1763         int ret = 0;
1764         int copied = 0;
1765         int i;
1766         struct drm_mode_modeinfo u_mode;
1767         struct drm_mode_modeinfo __user *mode_ptr;
1768         uint32_t __user *prop_ptr;
1769         uint64_t __user *prop_values;
1770         uint32_t __user *encoder_ptr;
1771
1772         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1773                 return -EINVAL;
1774
1775         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1776
1777         DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1778
1779         mutex_lock(&dev->mode_config.mutex);
1780
1781         obj = drm_mode_object_find(dev, out_resp->connector_id,
1782                                    DRM_MODE_OBJECT_CONNECTOR);
1783         if (!obj) {
1784                 ret = -ENOENT;
1785                 goto out;
1786         }
1787         connector = obj_to_connector(obj);
1788
1789         props_count = connector->properties.count;
1790
1791         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1792                 if (connector->encoder_ids[i] != 0) {
1793                         encoders_count++;
1794                 }
1795         }
1796
1797         if (out_resp->count_modes == 0) {
1798                 connector->funcs->fill_modes(connector,
1799                                              dev->mode_config.max_width,
1800                                              dev->mode_config.max_height);
1801         }
1802
1803         /* delayed so we get modes regardless of pre-fill_modes state */
1804         list_for_each_entry(mode, &connector->modes, head)
1805                 if (drm_mode_expose_to_userspace(mode, file_priv))
1806                         mode_count++;
1807
1808         out_resp->connector_id = connector->base.id;
1809         out_resp->connector_type = connector->connector_type;
1810         out_resp->connector_type_id = connector->connector_type_id;
1811         out_resp->mm_width = connector->display_info.width_mm;
1812         out_resp->mm_height = connector->display_info.height_mm;
1813         out_resp->subpixel = connector->display_info.subpixel_order;
1814         out_resp->connection = connector->status;
1815         if (connector->encoder)
1816                 out_resp->encoder_id = connector->encoder->base.id;
1817         else
1818                 out_resp->encoder_id = 0;
1819
1820         /*
1821          * This ioctl is called twice, once to determine how much space is
1822          * needed, and the 2nd time to fill it.
1823          */
1824         if ((out_resp->count_modes >= mode_count) && mode_count) {
1825                 copied = 0;
1826                 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1827                 list_for_each_entry(mode, &connector->modes, head) {
1828                         if (!drm_mode_expose_to_userspace(mode, file_priv))
1829                                 continue;
1830
1831                         drm_crtc_convert_to_umode(&u_mode, mode);
1832                         if (copy_to_user(mode_ptr + copied,
1833                                          &u_mode, sizeof(u_mode))) {
1834                                 ret = -EFAULT;
1835                                 goto out;
1836                         }
1837                         copied++;
1838                 }
1839         }
1840         out_resp->count_modes = mode_count;
1841
1842         if ((out_resp->count_props >= props_count) && props_count) {
1843                 copied = 0;
1844                 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1845                 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1846                 for (i = 0; i < connector->properties.count; i++) {
1847                         if (put_user(connector->properties.ids[i],
1848                                      prop_ptr + copied)) {
1849                                 ret = -EFAULT;
1850                                 goto out;
1851                         }
1852
1853                         if (put_user(connector->properties.values[i],
1854                                      prop_values + copied)) {
1855                                 ret = -EFAULT;
1856                                 goto out;
1857                         }
1858                         copied++;
1859                 }
1860         }
1861         out_resp->count_props = props_count;
1862
1863         if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1864                 copied = 0;
1865                 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1866                 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1867                         if (connector->encoder_ids[i] != 0) {
1868                                 if (put_user(connector->encoder_ids[i],
1869                                              encoder_ptr + copied)) {
1870                                         ret = -EFAULT;
1871                                         goto out;
1872                                 }
1873                                 copied++;
1874                         }
1875                 }
1876         }
1877         out_resp->count_encoders = encoders_count;
1878
1879 out:
1880         mutex_unlock(&dev->mode_config.mutex);
1881
1882         return ret;
1883 }
1884
1885 /**
1886  * drm_mode_getencoder - get encoder configuration
1887  * @dev: drm device for the ioctl
1888  * @data: data pointer for the ioctl
1889  * @file_priv: drm file for the ioctl call
1890  *
1891  * Construct a encoder configuration structure to return to the user.
1892  *
1893  * Called by the user via ioctl.
1894  *
1895  * Returns:
1896  * Zero on success, errno on failure.
1897  */
1898 int drm_mode_getencoder(struct drm_device *dev, void *data,
1899                         struct drm_file *file_priv)
1900 {
1901         struct drm_mode_get_encoder *enc_resp = data;
1902         struct drm_mode_object *obj;
1903         struct drm_encoder *encoder;
1904         int ret = 0;
1905
1906         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1907                 return -EINVAL;
1908
1909         drm_modeset_lock_all(dev);
1910         obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1911                                    DRM_MODE_OBJECT_ENCODER);
1912         if (!obj) {
1913                 ret = -ENOENT;
1914                 goto out;
1915         }
1916         encoder = obj_to_encoder(obj);
1917
1918         if (encoder->crtc)
1919                 enc_resp->crtc_id = encoder->crtc->base.id;
1920         else
1921                 enc_resp->crtc_id = 0;
1922         enc_resp->encoder_type = encoder->encoder_type;
1923         enc_resp->encoder_id = encoder->base.id;
1924         enc_resp->possible_crtcs = encoder->possible_crtcs;
1925         enc_resp->possible_clones = encoder->possible_clones;
1926
1927 out:
1928         drm_modeset_unlock_all(dev);
1929         return ret;
1930 }
1931
1932 /**
1933  * drm_mode_getplane_res - enumerate all plane resources
1934  * @dev: DRM device
1935  * @data: ioctl data
1936  * @file_priv: DRM file info
1937  *
1938  * Construct a list of plane ids to return to the user.
1939  *
1940  * Called by the user via ioctl.
1941  *
1942  * Returns:
1943  * Zero on success, errno on failure.
1944  */
1945 int drm_mode_getplane_res(struct drm_device *dev, void *data,
1946                           struct drm_file *file_priv)
1947 {
1948         struct drm_mode_get_plane_res *plane_resp = data;
1949         struct drm_mode_config *config;
1950         struct drm_plane *plane;
1951         uint32_t __user *plane_ptr;
1952         int copied = 0, ret = 0;
1953
1954         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1955                 return -EINVAL;
1956
1957         drm_modeset_lock_all(dev);
1958         config = &dev->mode_config;
1959
1960         /*
1961          * This ioctl is called twice, once to determine how much space is
1962          * needed, and the 2nd time to fill it.
1963          */
1964         if (config->num_overlay_plane &&
1965             (plane_resp->count_planes >= config->num_overlay_plane)) {
1966                 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
1967
1968                 list_for_each_entry(plane, &config->plane_list, head) {
1969                         /* Only advertise overlays to userspace for now. */
1970                         if (plane->type != DRM_PLANE_TYPE_OVERLAY)
1971                                 continue;
1972
1973                         if (put_user(plane->base.id, plane_ptr + copied)) {
1974                                 ret = -EFAULT;
1975                                 goto out;
1976                         }
1977                         copied++;
1978                 }
1979         }
1980         plane_resp->count_planes = config->num_overlay_plane;
1981
1982 out:
1983         drm_modeset_unlock_all(dev);
1984         return ret;
1985 }
1986
1987 /**
1988  * drm_mode_getplane - get plane configuration
1989  * @dev: DRM device
1990  * @data: ioctl data
1991  * @file_priv: DRM file info
1992  *
1993  * Construct a plane configuration structure to return to the user.
1994  *
1995  * Called by the user via ioctl.
1996  *
1997  * Returns:
1998  * Zero on success, errno on failure.
1999  */
2000 int drm_mode_getplane(struct drm_device *dev, void *data,
2001                       struct drm_file *file_priv)
2002 {
2003         struct drm_mode_get_plane *plane_resp = data;
2004         struct drm_mode_object *obj;
2005         struct drm_plane *plane;
2006         uint32_t __user *format_ptr;
2007         int ret = 0;
2008
2009         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2010                 return -EINVAL;
2011
2012         drm_modeset_lock_all(dev);
2013         obj = drm_mode_object_find(dev, plane_resp->plane_id,
2014                                    DRM_MODE_OBJECT_PLANE);
2015         if (!obj) {
2016                 ret = -ENOENT;
2017                 goto out;
2018         }
2019         plane = obj_to_plane(obj);
2020
2021         if (plane->crtc)
2022                 plane_resp->crtc_id = plane->crtc->base.id;
2023         else
2024                 plane_resp->crtc_id = 0;
2025
2026         if (plane->fb)
2027                 plane_resp->fb_id = plane->fb->base.id;
2028         else
2029                 plane_resp->fb_id = 0;
2030
2031         plane_resp->plane_id = plane->base.id;
2032         plane_resp->possible_crtcs = plane->possible_crtcs;
2033         plane_resp->gamma_size = 0;
2034
2035         /*
2036          * This ioctl is called twice, once to determine how much space is
2037          * needed, and the 2nd time to fill it.
2038          */
2039         if (plane->format_count &&
2040             (plane_resp->count_format_types >= plane->format_count)) {
2041                 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2042                 if (copy_to_user(format_ptr,
2043                                  plane->format_types,
2044                                  sizeof(uint32_t) * plane->format_count)) {
2045                         ret = -EFAULT;
2046                         goto out;
2047                 }
2048         }
2049         plane_resp->count_format_types = plane->format_count;
2050
2051 out:
2052         drm_modeset_unlock_all(dev);
2053         return ret;
2054 }
2055
2056 /**
2057  * drm_mode_setplane - configure a plane's configuration
2058  * @dev: DRM device
2059  * @data: ioctl data*
2060  * @file_priv: DRM file info
2061  *
2062  * Set plane configuration, including placement, fb, scaling, and other factors.
2063  * Or pass a NULL fb to disable.
2064  *
2065  * Returns:
2066  * Zero on success, errno on failure.
2067  */
2068 int drm_mode_setplane(struct drm_device *dev, void *data,
2069                       struct drm_file *file_priv)
2070 {
2071         struct drm_mode_set_plane *plane_req = data;
2072         struct drm_mode_object *obj;
2073         struct drm_plane *plane;
2074         struct drm_crtc *crtc;
2075         struct drm_framebuffer *fb = NULL, *old_fb = NULL;
2076         int ret = 0;
2077         unsigned int fb_width, fb_height;
2078         int i;
2079
2080         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2081                 return -EINVAL;
2082
2083         /*
2084          * First, find the plane, crtc, and fb objects.  If not available,
2085          * we don't bother to call the driver.
2086          */
2087         obj = drm_mode_object_find(dev, plane_req->plane_id,
2088                                    DRM_MODE_OBJECT_PLANE);
2089         if (!obj) {
2090                 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2091                               plane_req->plane_id);
2092                 return -ENOENT;
2093         }
2094         plane = obj_to_plane(obj);
2095
2096         /* No fb means shut it down */
2097         if (!plane_req->fb_id) {
2098                 drm_modeset_lock_all(dev);
2099                 old_fb = plane->fb;
2100                 plane->funcs->disable_plane(plane);
2101                 plane->crtc = NULL;
2102                 plane->fb = NULL;
2103                 drm_modeset_unlock_all(dev);
2104                 goto out;
2105         }
2106
2107         obj = drm_mode_object_find(dev, plane_req->crtc_id,
2108                                    DRM_MODE_OBJECT_CRTC);
2109         if (!obj) {
2110                 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2111                               plane_req->crtc_id);
2112                 ret = -ENOENT;
2113                 goto out;
2114         }
2115         crtc = obj_to_crtc(obj);
2116
2117         fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2118         if (!fb) {
2119                 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2120                               plane_req->fb_id);
2121                 ret = -ENOENT;
2122                 goto out;
2123         }
2124
2125         /* Check whether this plane supports the fb pixel format. */
2126         for (i = 0; i < plane->format_count; i++)
2127                 if (fb->pixel_format == plane->format_types[i])
2128                         break;
2129         if (i == plane->format_count) {
2130                 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2131                               drm_get_format_name(fb->pixel_format));
2132                 ret = -EINVAL;
2133                 goto out;
2134         }
2135
2136         fb_width = fb->width << 16;
2137         fb_height = fb->height << 16;
2138
2139         /* Make sure source coordinates are inside the fb. */
2140         if (plane_req->src_w > fb_width ||
2141             plane_req->src_x > fb_width - plane_req->src_w ||
2142             plane_req->src_h > fb_height ||
2143             plane_req->src_y > fb_height - plane_req->src_h) {
2144                 DRM_DEBUG_KMS("Invalid source coordinates "
2145                               "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2146                               plane_req->src_w >> 16,
2147                               ((plane_req->src_w & 0xffff) * 15625) >> 10,
2148                               plane_req->src_h >> 16,
2149                               ((plane_req->src_h & 0xffff) * 15625) >> 10,
2150                               plane_req->src_x >> 16,
2151                               ((plane_req->src_x & 0xffff) * 15625) >> 10,
2152                               plane_req->src_y >> 16,
2153                               ((plane_req->src_y & 0xffff) * 15625) >> 10);
2154                 ret = -ENOSPC;
2155                 goto out;
2156         }
2157
2158         /* Give drivers some help against integer overflows */
2159         if (plane_req->crtc_w > INT_MAX ||
2160             plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2161             plane_req->crtc_h > INT_MAX ||
2162             plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2163                 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2164                               plane_req->crtc_w, plane_req->crtc_h,
2165                               plane_req->crtc_x, plane_req->crtc_y);
2166                 ret = -ERANGE;
2167                 goto out;
2168         }
2169
2170         drm_modeset_lock_all(dev);
2171         ret = plane->funcs->update_plane(plane, crtc, fb,
2172                                          plane_req->crtc_x, plane_req->crtc_y,
2173                                          plane_req->crtc_w, plane_req->crtc_h,
2174                                          plane_req->src_x, plane_req->src_y,
2175                                          plane_req->src_w, plane_req->src_h);
2176         if (!ret) {
2177                 old_fb = plane->fb;
2178                 plane->crtc = crtc;
2179                 plane->fb = fb;
2180                 fb = NULL;
2181         }
2182         drm_modeset_unlock_all(dev);
2183
2184 out:
2185         if (fb)
2186                 drm_framebuffer_unreference(fb);
2187         if (old_fb)
2188                 drm_framebuffer_unreference(old_fb);
2189
2190         return ret;
2191 }
2192
2193 /**
2194  * drm_mode_set_config_internal - helper to call ->set_config
2195  * @set: modeset config to set
2196  *
2197  * This is a little helper to wrap internal calls to the ->set_config driver
2198  * interface. The only thing it adds is correct refcounting dance.
2199  * 
2200  * Returns:
2201  * Zero on success, errno on failure.
2202  */
2203 int drm_mode_set_config_internal(struct drm_mode_set *set)
2204 {
2205         struct drm_crtc *crtc = set->crtc;
2206         struct drm_framebuffer *fb;
2207         struct drm_crtc *tmp;
2208         int ret;
2209
2210         /*
2211          * NOTE: ->set_config can also disable other crtcs (if we steal all
2212          * connectors from it), hence we need to refcount the fbs across all
2213          * crtcs. Atomic modeset will have saner semantics ...
2214          */
2215         list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2216                 tmp->old_fb = tmp->fb;
2217
2218         fb = set->fb;
2219
2220         ret = crtc->funcs->set_config(set);
2221         if (ret == 0) {
2222                 /* crtc->fb must be updated by ->set_config, enforces this. */
2223                 WARN_ON(fb != crtc->fb);
2224         }
2225
2226         list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2227                 if (tmp->fb)
2228                         drm_framebuffer_reference(tmp->fb);
2229                 if (tmp->old_fb)
2230                         drm_framebuffer_unreference(tmp->old_fb);
2231         }
2232
2233         return ret;
2234 }
2235 EXPORT_SYMBOL(drm_mode_set_config_internal);
2236
2237 /**
2238  * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2239  *     CRTC viewport
2240  * @crtc: CRTC that framebuffer will be displayed on
2241  * @x: x panning
2242  * @y: y panning
2243  * @mode: mode that framebuffer will be displayed under
2244  * @fb: framebuffer to check size of
2245  */
2246 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2247                             int x, int y,
2248                             const struct drm_display_mode *mode,
2249                             const struct drm_framebuffer *fb)
2250
2251 {
2252         int hdisplay, vdisplay;
2253
2254         hdisplay = mode->hdisplay;
2255         vdisplay = mode->vdisplay;
2256
2257         if (drm_mode_is_stereo(mode)) {
2258                 struct drm_display_mode adjusted = *mode;
2259
2260                 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2261                 hdisplay = adjusted.crtc_hdisplay;
2262                 vdisplay = adjusted.crtc_vdisplay;
2263         }
2264
2265         if (crtc->invert_dimensions)
2266                 swap(hdisplay, vdisplay);
2267
2268         if (hdisplay > fb->width ||
2269             vdisplay > fb->height ||
2270             x > fb->width - hdisplay ||
2271             y > fb->height - vdisplay) {
2272                 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2273                               fb->width, fb->height, hdisplay, vdisplay, x, y,
2274                               crtc->invert_dimensions ? " (inverted)" : "");
2275                 return -ENOSPC;
2276         }
2277
2278         return 0;
2279 }
2280 EXPORT_SYMBOL(drm_crtc_check_viewport);
2281
2282 /**
2283  * drm_mode_setcrtc - set CRTC configuration
2284  * @dev: drm device for the ioctl
2285  * @data: data pointer for the ioctl
2286  * @file_priv: drm file for the ioctl call
2287  *
2288  * Build a new CRTC configuration based on user request.
2289  *
2290  * Called by the user via ioctl.
2291  *
2292  * Returns:
2293  * Zero on success, errno on failure.
2294  */
2295 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2296                      struct drm_file *file_priv)
2297 {
2298         struct drm_mode_config *config = &dev->mode_config;
2299         struct drm_mode_crtc *crtc_req = data;
2300         struct drm_mode_object *obj;
2301         struct drm_crtc *crtc;
2302         struct drm_connector **connector_set = NULL, *connector;
2303         struct drm_framebuffer *fb = NULL;
2304         struct drm_display_mode *mode = NULL;
2305         struct drm_mode_set set;
2306         uint32_t __user *set_connectors_ptr;
2307         int ret;
2308         int i;
2309
2310         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2311                 return -EINVAL;
2312
2313         /* For some reason crtc x/y offsets are signed internally. */
2314         if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2315                 return -ERANGE;
2316
2317         drm_modeset_lock_all(dev);
2318         obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2319                                    DRM_MODE_OBJECT_CRTC);
2320         if (!obj) {
2321                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2322                 ret = -ENOENT;
2323                 goto out;
2324         }
2325         crtc = obj_to_crtc(obj);
2326         DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2327
2328         if (crtc_req->mode_valid) {
2329                 /* If we have a mode we need a framebuffer. */
2330                 /* If we pass -1, set the mode with the currently bound fb */
2331                 if (crtc_req->fb_id == -1) {
2332                         if (!crtc->fb) {
2333                                 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2334                                 ret = -EINVAL;
2335                                 goto out;
2336                         }
2337                         fb = crtc->fb;
2338                         /* Make refcounting symmetric with the lookup path. */
2339                         drm_framebuffer_reference(fb);
2340                 } else {
2341                         fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2342                         if (!fb) {
2343                                 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2344                                                 crtc_req->fb_id);
2345                                 ret = -ENOENT;
2346                                 goto out;
2347                         }
2348                 }
2349
2350                 mode = drm_mode_create(dev);
2351                 if (!mode) {
2352                         ret = -ENOMEM;
2353                         goto out;
2354                 }
2355
2356                 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2357                 if (ret) {
2358                         DRM_DEBUG_KMS("Invalid mode\n");
2359                         goto out;
2360                 }
2361
2362                 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2363
2364                 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2365                                               mode, fb);
2366                 if (ret)
2367                         goto out;
2368
2369         }
2370
2371         if (crtc_req->count_connectors == 0 && mode) {
2372                 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2373                 ret = -EINVAL;
2374                 goto out;
2375         }
2376
2377         if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2378                 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2379                           crtc_req->count_connectors);
2380                 ret = -EINVAL;
2381                 goto out;
2382         }
2383
2384         if (crtc_req->count_connectors > 0) {
2385                 u32 out_id;
2386
2387                 /* Avoid unbounded kernel memory allocation */
2388                 if (crtc_req->count_connectors > config->num_connector) {
2389                         ret = -EINVAL;
2390                         goto out;
2391                 }
2392
2393                 connector_set = kmalloc(crtc_req->count_connectors *
2394                                         sizeof(struct drm_connector *),
2395                                         GFP_KERNEL);
2396                 if (!connector_set) {
2397                         ret = -ENOMEM;
2398                         goto out;
2399                 }
2400
2401                 for (i = 0; i < crtc_req->count_connectors; i++) {
2402                         set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2403                         if (get_user(out_id, &set_connectors_ptr[i])) {
2404                                 ret = -EFAULT;
2405                                 goto out;
2406                         }
2407
2408                         obj = drm_mode_object_find(dev, out_id,
2409                                                    DRM_MODE_OBJECT_CONNECTOR);
2410                         if (!obj) {
2411                                 DRM_DEBUG_KMS("Connector id %d unknown\n",
2412                                                 out_id);
2413                                 ret = -ENOENT;
2414                                 goto out;
2415                         }
2416                         connector = obj_to_connector(obj);
2417                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2418                                         connector->base.id,
2419                                         drm_get_connector_name(connector));
2420
2421                         connector_set[i] = connector;
2422                 }
2423         }
2424
2425         set.crtc = crtc;
2426         set.x = crtc_req->x;
2427         set.y = crtc_req->y;
2428         set.mode = mode;
2429         set.connectors = connector_set;
2430         set.num_connectors = crtc_req->count_connectors;
2431         set.fb = fb;
2432         ret = drm_mode_set_config_internal(&set);
2433
2434 out:
2435         if (fb)
2436                 drm_framebuffer_unreference(fb);
2437
2438         kfree(connector_set);
2439         drm_mode_destroy(dev, mode);
2440         drm_modeset_unlock_all(dev);
2441         return ret;
2442 }
2443
2444 static int drm_mode_cursor_common(struct drm_device *dev,
2445                                   struct drm_mode_cursor2 *req,
2446                                   struct drm_file *file_priv)
2447 {
2448         struct drm_mode_object *obj;
2449         struct drm_crtc *crtc;
2450         int ret = 0;
2451
2452         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2453                 return -EINVAL;
2454
2455         if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2456                 return -EINVAL;
2457
2458         obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
2459         if (!obj) {
2460                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2461                 return -ENOENT;
2462         }
2463         crtc = obj_to_crtc(obj);
2464
2465         mutex_lock(&crtc->mutex);
2466         if (req->flags & DRM_MODE_CURSOR_BO) {
2467                 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
2468                         ret = -ENXIO;
2469                         goto out;
2470                 }
2471                 /* Turns off the cursor if handle is 0 */
2472                 if (crtc->funcs->cursor_set2)
2473                         ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2474                                                       req->width, req->height, req->hot_x, req->hot_y);
2475                 else
2476                         ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2477                                                       req->width, req->height);
2478         }
2479
2480         if (req->flags & DRM_MODE_CURSOR_MOVE) {
2481                 if (crtc->funcs->cursor_move) {
2482                         ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2483                 } else {
2484                         ret = -EFAULT;
2485                         goto out;
2486                 }
2487         }
2488 out:
2489         mutex_unlock(&crtc->mutex);
2490
2491         return ret;
2492
2493 }
2494
2495
2496 /**
2497  * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2498  * @dev: drm device for the ioctl
2499  * @data: data pointer for the ioctl
2500  * @file_priv: drm file for the ioctl call
2501  *
2502  * Set the cursor configuration based on user request.
2503  *
2504  * Called by the user via ioctl.
2505  *
2506  * Returns:
2507  * Zero on success, errno on failure.
2508  */
2509 int drm_mode_cursor_ioctl(struct drm_device *dev,
2510                           void *data, struct drm_file *file_priv)
2511 {
2512         struct drm_mode_cursor *req = data;
2513         struct drm_mode_cursor2 new_req;
2514
2515         memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2516         new_req.hot_x = new_req.hot_y = 0;
2517
2518         return drm_mode_cursor_common(dev, &new_req, file_priv);
2519 }
2520
2521 /**
2522  * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2523  * @dev: drm device for the ioctl
2524  * @data: data pointer for the ioctl
2525  * @file_priv: drm file for the ioctl call
2526  *
2527  * Set the cursor configuration based on user request. This implements the 2nd
2528  * version of the cursor ioctl, which allows userspace to additionally specify
2529  * the hotspot of the pointer.
2530  *
2531  * Called by the user via ioctl.
2532  *
2533  * Returns:
2534  * Zero on success, errno on failure.
2535  */
2536 int drm_mode_cursor2_ioctl(struct drm_device *dev,
2537                            void *data, struct drm_file *file_priv)
2538 {
2539         struct drm_mode_cursor2 *req = data;
2540         return drm_mode_cursor_common(dev, req, file_priv);
2541 }
2542
2543 /**
2544  * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2545  * @bpp: bits per pixels
2546  * @depth: bit depth per pixel
2547  *
2548  * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2549  * Useful in fbdev emulation code, since that deals in those values.
2550  */
2551 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2552 {
2553         uint32_t fmt;
2554
2555         switch (bpp) {
2556         case 8:
2557                 fmt = DRM_FORMAT_C8;
2558                 break;
2559         case 16:
2560                 if (depth == 15)
2561                         fmt = DRM_FORMAT_XRGB1555;
2562                 else
2563                         fmt = DRM_FORMAT_RGB565;
2564                 break;
2565         case 24:
2566                 fmt = DRM_FORMAT_RGB888;
2567                 break;
2568         case 32:
2569                 if (depth == 24)
2570                         fmt = DRM_FORMAT_XRGB8888;
2571                 else if (depth == 30)
2572                         fmt = DRM_FORMAT_XRGB2101010;
2573                 else
2574                         fmt = DRM_FORMAT_ARGB8888;
2575                 break;
2576         default:
2577                 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2578                 fmt = DRM_FORMAT_XRGB8888;
2579                 break;
2580         }
2581
2582         return fmt;
2583 }
2584 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2585
2586 /**
2587  * drm_mode_addfb - add an FB to the graphics configuration
2588  * @dev: drm device for the ioctl
2589  * @data: data pointer for the ioctl
2590  * @file_priv: drm file for the ioctl call
2591  *
2592  * Add a new FB to the specified CRTC, given a user request. This is the
2593  * original addfb ioclt which only supported RGB formats.
2594  *
2595  * Called by the user via ioctl.
2596  *
2597  * Returns:
2598  * Zero on success, errno on failure.
2599  */
2600 int drm_mode_addfb(struct drm_device *dev,
2601                    void *data, struct drm_file *file_priv)
2602 {
2603         struct drm_mode_fb_cmd *or = data;
2604         struct drm_mode_fb_cmd2 r = {};
2605         struct drm_mode_config *config = &dev->mode_config;
2606         struct drm_framebuffer *fb;
2607         int ret = 0;
2608
2609         /* Use new struct with format internally */
2610         r.fb_id = or->fb_id;
2611         r.width = or->width;
2612         r.height = or->height;
2613         r.pitches[0] = or->pitch;
2614         r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2615         r.handles[0] = or->handle;
2616
2617         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2618                 return -EINVAL;
2619
2620         if ((config->min_width > r.width) || (r.width > config->max_width))
2621                 return -EINVAL;
2622
2623         if ((config->min_height > r.height) || (r.height > config->max_height))
2624                 return -EINVAL;
2625
2626         fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2627         if (IS_ERR(fb)) {
2628                 DRM_DEBUG_KMS("could not create framebuffer\n");
2629                 return PTR_ERR(fb);
2630         }
2631
2632         mutex_lock(&file_priv->fbs_lock);
2633         or->fb_id = fb->base.id;
2634         list_add(&fb->filp_head, &file_priv->fbs);
2635         DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2636         mutex_unlock(&file_priv->fbs_lock);
2637
2638         return ret;
2639 }
2640
2641 static int format_check(const struct drm_mode_fb_cmd2 *r)
2642 {
2643         uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2644
2645         switch (format) {
2646         case DRM_FORMAT_C8:
2647         case DRM_FORMAT_RGB332:
2648         case DRM_FORMAT_BGR233:
2649         case DRM_FORMAT_XRGB4444:
2650         case DRM_FORMAT_XBGR4444:
2651         case DRM_FORMAT_RGBX4444:
2652         case DRM_FORMAT_BGRX4444:
2653         case DRM_FORMAT_ARGB4444:
2654         case DRM_FORMAT_ABGR4444:
2655         case DRM_FORMAT_RGBA4444:
2656         case DRM_FORMAT_BGRA4444:
2657         case DRM_FORMAT_XRGB1555:
2658         case DRM_FORMAT_XBGR1555:
2659         case DRM_FORMAT_RGBX5551:
2660         case DRM_FORMAT_BGRX5551:
2661         case DRM_FORMAT_ARGB1555:
2662         case DRM_FORMAT_ABGR1555:
2663         case DRM_FORMAT_RGBA5551:
2664         case DRM_FORMAT_BGRA5551:
2665         case DRM_FORMAT_RGB565:
2666         case DRM_FORMAT_BGR565:
2667         case DRM_FORMAT_RGB888:
2668         case DRM_FORMAT_BGR888:
2669         case DRM_FORMAT_XRGB8888:
2670         case DRM_FORMAT_XBGR8888:
2671         case DRM_FORMAT_RGBX8888:
2672         case DRM_FORMAT_BGRX8888:
2673         case DRM_FORMAT_ARGB8888:
2674         case DRM_FORMAT_ABGR8888:
2675         case DRM_FORMAT_RGBA8888:
2676         case DRM_FORMAT_BGRA8888:
2677         case DRM_FORMAT_XRGB2101010:
2678         case DRM_FORMAT_XBGR2101010:
2679         case DRM_FORMAT_RGBX1010102:
2680         case DRM_FORMAT_BGRX1010102:
2681         case DRM_FORMAT_ARGB2101010:
2682         case DRM_FORMAT_ABGR2101010:
2683         case DRM_FORMAT_RGBA1010102:
2684         case DRM_FORMAT_BGRA1010102:
2685         case DRM_FORMAT_YUYV:
2686         case DRM_FORMAT_YVYU:
2687         case DRM_FORMAT_UYVY:
2688         case DRM_FORMAT_VYUY:
2689         case DRM_FORMAT_AYUV:
2690         case DRM_FORMAT_NV12:
2691         case DRM_FORMAT_NV21:
2692         case DRM_FORMAT_NV16:
2693         case DRM_FORMAT_NV61:
2694         case DRM_FORMAT_NV24:
2695         case DRM_FORMAT_NV42:
2696         case DRM_FORMAT_YUV410:
2697         case DRM_FORMAT_YVU410:
2698         case DRM_FORMAT_YUV411:
2699         case DRM_FORMAT_YVU411:
2700         case DRM_FORMAT_YUV420:
2701         case DRM_FORMAT_YVU420:
2702         case DRM_FORMAT_YUV422:
2703         case DRM_FORMAT_YVU422:
2704         case DRM_FORMAT_YUV444:
2705         case DRM_FORMAT_YVU444:
2706                 return 0;
2707         default:
2708                 DRM_DEBUG_KMS("invalid pixel format %s\n",
2709                               drm_get_format_name(r->pixel_format));
2710                 return -EINVAL;
2711         }
2712 }
2713
2714 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2715 {
2716         int ret, hsub, vsub, num_planes, i;
2717
2718         ret = format_check(r);
2719         if (ret) {
2720                 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2721                               drm_get_format_name(r->pixel_format));
2722                 return ret;
2723         }
2724
2725         hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2726         vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2727         num_planes = drm_format_num_planes(r->pixel_format);
2728
2729         if (r->width == 0 || r->width % hsub) {
2730                 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2731                 return -EINVAL;
2732         }
2733
2734         if (r->height == 0 || r->height % vsub) {
2735                 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2736                 return -EINVAL;
2737         }
2738
2739         for (i = 0; i < num_planes; i++) {
2740                 unsigned int width = r->width / (i != 0 ? hsub : 1);
2741                 unsigned int height = r->height / (i != 0 ? vsub : 1);
2742                 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
2743
2744                 if (!r->handles[i]) {
2745                         DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2746                         return -EINVAL;
2747                 }
2748
2749                 if ((uint64_t) width * cpp > UINT_MAX)
2750                         return -ERANGE;
2751
2752                 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2753                         return -ERANGE;
2754
2755                 if (r->pitches[i] < width * cpp) {
2756                         DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2757                         return -EINVAL;
2758                 }
2759         }
2760
2761         return 0;
2762 }
2763
2764 /**
2765  * drm_mode_addfb2 - add an FB to the graphics configuration
2766  * @dev: drm device for the ioctl
2767  * @data: data pointer for the ioctl
2768  * @file_priv: drm file for the ioctl call
2769  *
2770  * Add a new FB to the specified CRTC, given a user request with format. This is
2771  * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
2772  * and uses fourcc codes as pixel format specifiers.
2773  *
2774  * Called by the user via ioctl.
2775  *
2776  * Returns:
2777  * Zero on success, errno on failure.
2778  */
2779 int drm_mode_addfb2(struct drm_device *dev,
2780                     void *data, struct drm_file *file_priv)
2781 {
2782         struct drm_mode_fb_cmd2 *r = data;
2783         struct drm_mode_config *config = &dev->mode_config;
2784         struct drm_framebuffer *fb;
2785         int ret;
2786
2787         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2788                 return -EINVAL;
2789
2790         if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2791                 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2792                 return -EINVAL;
2793         }
2794
2795         if ((config->min_width > r->width) || (r->width > config->max_width)) {
2796                 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2797                           r->width, config->min_width, config->max_width);
2798                 return -EINVAL;
2799         }
2800         if ((config->min_height > r->height) || (r->height > config->max_height)) {
2801                 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2802                           r->height, config->min_height, config->max_height);
2803                 return -EINVAL;
2804         }
2805
2806         ret = framebuffer_check(r);
2807         if (ret)
2808                 return ret;
2809
2810         fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2811         if (IS_ERR(fb)) {
2812                 DRM_DEBUG_KMS("could not create framebuffer\n");
2813                 return PTR_ERR(fb);
2814         }
2815
2816         mutex_lock(&file_priv->fbs_lock);
2817         r->fb_id = fb->base.id;
2818         list_add(&fb->filp_head, &file_priv->fbs);
2819         DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2820         mutex_unlock(&file_priv->fbs_lock);
2821
2822
2823         return ret;
2824 }
2825
2826 /**
2827  * drm_mode_rmfb - remove an FB from the configuration
2828  * @dev: drm device for the ioctl
2829  * @data: data pointer for the ioctl
2830  * @file_priv: drm file for the ioctl call
2831  *
2832  * Remove the FB specified by the user.
2833  *
2834  * Called by the user via ioctl.
2835  *
2836  * Returns:
2837  * Zero on success, errno on failure.
2838  */
2839 int drm_mode_rmfb(struct drm_device *dev,
2840                    void *data, struct drm_file *file_priv)
2841 {
2842         struct drm_framebuffer *fb = NULL;
2843         struct drm_framebuffer *fbl = NULL;
2844         uint32_t *id = data;
2845         int found = 0;
2846
2847         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2848                 return -EINVAL;
2849
2850         mutex_lock(&file_priv->fbs_lock);
2851         mutex_lock(&dev->mode_config.fb_lock);
2852         fb = __drm_framebuffer_lookup(dev, *id);
2853         if (!fb)
2854                 goto fail_lookup;
2855
2856         list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2857                 if (fb == fbl)
2858                         found = 1;
2859         if (!found)
2860                 goto fail_lookup;
2861
2862         /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2863         __drm_framebuffer_unregister(dev, fb);
2864
2865         list_del_init(&fb->filp_head);
2866         mutex_unlock(&dev->mode_config.fb_lock);
2867         mutex_unlock(&file_priv->fbs_lock);
2868
2869         drm_framebuffer_remove(fb);
2870
2871         return 0;
2872
2873 fail_lookup:
2874         mutex_unlock(&dev->mode_config.fb_lock);
2875         mutex_unlock(&file_priv->fbs_lock);
2876
2877         return -ENOENT;
2878 }
2879
2880 /**
2881  * drm_mode_getfb - get FB info
2882  * @dev: drm device for the ioctl
2883  * @data: data pointer for the ioctl
2884  * @file_priv: drm file for the ioctl call
2885  *
2886  * Lookup the FB given its ID and return info about it.
2887  *
2888  * Called by the user via ioctl.
2889  *
2890  * Returns:
2891  * Zero on success, errno on failure.
2892  */
2893 int drm_mode_getfb(struct drm_device *dev,
2894                    void *data, struct drm_file *file_priv)
2895 {
2896         struct drm_mode_fb_cmd *r = data;
2897         struct drm_framebuffer *fb;
2898         int ret;
2899
2900         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2901                 return -EINVAL;
2902
2903         fb = drm_framebuffer_lookup(dev, r->fb_id);
2904         if (!fb)
2905                 return -ENOENT;
2906
2907         r->height = fb->height;
2908         r->width = fb->width;
2909         r->depth = fb->depth;
2910         r->bpp = fb->bits_per_pixel;
2911         r->pitch = fb->pitches[0];
2912         if (fb->funcs->create_handle) {
2913                 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
2914                     drm_is_control_client(file_priv)) {
2915                         ret = fb->funcs->create_handle(fb, file_priv,
2916                                                        &r->handle);
2917                 } else {
2918                         /* GET_FB() is an unprivileged ioctl so we must not
2919                          * return a buffer-handle to non-master processes! For
2920                          * backwards-compatibility reasons, we cannot make
2921                          * GET_FB() privileged, so just return an invalid handle
2922                          * for non-masters. */
2923                         r->handle = 0;
2924                         ret = 0;
2925                 }
2926         } else {
2927                 ret = -ENODEV;
2928         }
2929
2930         drm_framebuffer_unreference(fb);
2931
2932         return ret;
2933 }
2934
2935 /**
2936  * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
2937  * @dev: drm device for the ioctl
2938  * @data: data pointer for the ioctl
2939  * @file_priv: drm file for the ioctl call
2940  *
2941  * Lookup the FB and flush out the damaged area supplied by userspace as a clip
2942  * rectangle list. Generic userspace which does frontbuffer rendering must call
2943  * this ioctl to flush out the changes on manual-update display outputs, e.g.
2944  * usb display-link, mipi manual update panels or edp panel self refresh modes.
2945  *
2946  * Modesetting drivers which always update the frontbuffer do not need to
2947  * implement the corresponding ->dirty framebuffer callback.
2948  *
2949  * Called by the user via ioctl.
2950  *
2951  * Returns:
2952  * Zero on success, errno on failure.
2953  */
2954 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2955                            void *data, struct drm_file *file_priv)
2956 {
2957         struct drm_clip_rect __user *clips_ptr;
2958         struct drm_clip_rect *clips = NULL;
2959         struct drm_mode_fb_dirty_cmd *r = data;
2960         struct drm_framebuffer *fb;
2961         unsigned flags;
2962         int num_clips;
2963         int ret;
2964
2965         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2966                 return -EINVAL;
2967
2968         fb = drm_framebuffer_lookup(dev, r->fb_id);
2969         if (!fb)
2970                 return -ENOENT;
2971
2972         num_clips = r->num_clips;
2973         clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
2974
2975         if (!num_clips != !clips_ptr) {
2976                 ret = -EINVAL;
2977                 goto out_err1;
2978         }
2979
2980         flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2981
2982         /* If userspace annotates copy, clips must come in pairs */
2983         if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2984                 ret = -EINVAL;
2985                 goto out_err1;
2986         }
2987
2988         if (num_clips && clips_ptr) {
2989                 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2990                         ret = -EINVAL;
2991                         goto out_err1;
2992                 }
2993                 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2994                 if (!clips) {
2995                         ret = -ENOMEM;
2996                         goto out_err1;
2997                 }
2998
2999                 ret = copy_from_user(clips, clips_ptr,
3000                                      num_clips * sizeof(*clips));
3001                 if (ret) {
3002                         ret = -EFAULT;
3003                         goto out_err2;
3004                 }
3005         }
3006
3007         if (fb->funcs->dirty) {
3008                 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3009                                        clips, num_clips);
3010         } else {
3011                 ret = -ENOSYS;
3012         }
3013
3014 out_err2:
3015         kfree(clips);
3016 out_err1:
3017         drm_framebuffer_unreference(fb);
3018
3019         return ret;
3020 }
3021
3022
3023 /**
3024  * drm_fb_release - remove and free the FBs on this file
3025  * @priv: drm file for the ioctl
3026  *
3027  * Destroy all the FBs associated with @filp.
3028  *
3029  * Called by the user via ioctl.
3030  *
3031  * Returns:
3032  * Zero on success, errno on failure.
3033  */
3034 void drm_fb_release(struct drm_file *priv)
3035 {
3036         struct drm_device *dev = priv->minor->dev;
3037         struct drm_framebuffer *fb, *tfb;
3038
3039         mutex_lock(&priv->fbs_lock);
3040         list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3041
3042                 mutex_lock(&dev->mode_config.fb_lock);
3043                 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3044                 __drm_framebuffer_unregister(dev, fb);
3045                 mutex_unlock(&dev->mode_config.fb_lock);
3046
3047                 list_del_init(&fb->filp_head);
3048
3049                 /* This will also drop the fpriv->fbs reference. */
3050                 drm_framebuffer_remove(fb);
3051         }
3052         mutex_unlock(&priv->fbs_lock);
3053 }
3054
3055 /**
3056  * drm_property_create - create a new property type
3057  * @dev: drm device
3058  * @flags: flags specifying the property type
3059  * @name: name of the property
3060  * @num_values: number of pre-defined values
3061  *
3062  * This creates a new generic drm property which can then be attached to a drm
3063  * object with drm_object_attach_property. The returned property object must be
3064  * freed with drm_property_destroy.
3065  *
3066  * Returns:
3067  * A pointer to the newly created property on success, NULL on failure.
3068  */
3069 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3070                                          const char *name, int num_values)
3071 {
3072         struct drm_property *property = NULL;
3073         int ret;
3074
3075         property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3076         if (!property)
3077                 return NULL;
3078
3079         if (num_values) {
3080                 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3081                 if (!property->values)
3082                         goto fail;
3083         }
3084
3085         ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3086         if (ret)
3087                 goto fail;
3088
3089         property->flags = flags;
3090         property->num_values = num_values;
3091         INIT_LIST_HEAD(&property->enum_blob_list);
3092
3093         if (name) {
3094                 strncpy(property->name, name, DRM_PROP_NAME_LEN);
3095                 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3096         }
3097
3098         list_add_tail(&property->head, &dev->mode_config.property_list);
3099         return property;
3100 fail:
3101         kfree(property->values);
3102         kfree(property);
3103         return NULL;
3104 }
3105 EXPORT_SYMBOL(drm_property_create);
3106
3107 /**
3108  * drm_property_create - create a new enumeration property type
3109  * @dev: drm device
3110  * @flags: flags specifying the property type
3111  * @name: name of the property
3112  * @props: enumeration lists with property values
3113  * @num_values: number of pre-defined values
3114  *
3115  * This creates a new generic drm property which can then be attached to a drm
3116  * object with drm_object_attach_property. The returned property object must be
3117  * freed with drm_property_destroy.
3118  *
3119  * Userspace is only allowed to set one of the predefined values for enumeration
3120  * properties.
3121  *
3122  * Returns:
3123  * A pointer to the newly created property on success, NULL on failure.
3124  */
3125 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3126                                          const char *name,
3127                                          const struct drm_prop_enum_list *props,
3128                                          int num_values)
3129 {
3130         struct drm_property *property;
3131         int i, ret;
3132
3133         flags |= DRM_MODE_PROP_ENUM;
3134
3135         property = drm_property_create(dev, flags, name, num_values);
3136         if (!property)
3137                 return NULL;
3138
3139         for (i = 0; i < num_values; i++) {
3140                 ret = drm_property_add_enum(property, i,
3141                                       props[i].type,
3142                                       props[i].name);
3143                 if (ret) {
3144                         drm_property_destroy(dev, property);
3145                         return NULL;
3146                 }
3147         }
3148
3149         return property;
3150 }
3151 EXPORT_SYMBOL(drm_property_create_enum);
3152
3153 /**
3154  * drm_property_create - create a new bitmask property type
3155  * @dev: drm device
3156  * @flags: flags specifying the property type
3157  * @name: name of the property
3158  * @props: enumeration lists with property bitflags
3159  * @num_values: number of pre-defined values
3160  *
3161  * This creates a new generic drm property which can then be attached to a drm
3162  * object with drm_object_attach_property. The returned property object must be
3163  * freed with drm_property_destroy.
3164  *
3165  * Compared to plain enumeration properties userspace is allowed to set any
3166  * or'ed together combination of the predefined property bitflag values
3167  *
3168  * Returns:
3169  * A pointer to the newly created property on success, NULL on failure.
3170  */
3171 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3172                                          int flags, const char *name,
3173                                          const struct drm_prop_enum_list *props,
3174                                          int num_values)
3175 {
3176         struct drm_property *property;
3177         int i, ret;
3178
3179         flags |= DRM_MODE_PROP_BITMASK;
3180
3181         property = drm_property_create(dev, flags, name, num_values);
3182         if (!property)
3183                 return NULL;
3184
3185         for (i = 0; i < num_values; i++) {
3186                 ret = drm_property_add_enum(property, i,
3187                                       props[i].type,
3188                                       props[i].name);
3189                 if (ret) {
3190                         drm_property_destroy(dev, property);
3191                         return NULL;
3192                 }
3193         }
3194
3195         return property;
3196 }
3197 EXPORT_SYMBOL(drm_property_create_bitmask);
3198
3199 /**
3200  * drm_property_create - create a new ranged property type
3201  * @dev: drm device
3202  * @flags: flags specifying the property type
3203  * @name: name of the property
3204  * @min: minimum value of the property
3205  * @max: maximum value of the property
3206  *
3207  * This creates a new generic drm property which can then be attached to a drm
3208  * object with drm_object_attach_property. The returned property object must be
3209  * freed with drm_property_destroy.
3210  *
3211  * Userspace is allowed to set any interger value in the (min, max) range
3212  * inclusive.
3213  *
3214  * Returns:
3215  * A pointer to the newly created property on success, NULL on failure.
3216  */
3217 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3218                                          const char *name,
3219                                          uint64_t min, uint64_t max)
3220 {
3221         struct drm_property *property;
3222
3223         flags |= DRM_MODE_PROP_RANGE;
3224
3225         property = drm_property_create(dev, flags, name, 2);
3226         if (!property)
3227                 return NULL;
3228
3229         property->values[0] = min;
3230         property->values[1] = max;
3231
3232         return property;
3233 }
3234 EXPORT_SYMBOL(drm_property_create_range);
3235
3236 /**
3237  * drm_property_add_enum - add a possible value to an enumeration property
3238  * @property: enumeration property to change
3239  * @index: index of the new enumeration
3240  * @value: value of the new enumeration
3241  * @name: symbolic name of the new enumeration
3242  *
3243  * This functions adds enumerations to a property.
3244  *
3245  * It's use is deprecated, drivers should use one of the more specific helpers
3246  * to directly create the property with all enumerations already attached.
3247  *
3248  * Returns:
3249  * Zero on success, error code on failure.
3250  */
3251 int drm_property_add_enum(struct drm_property *property, int index,
3252                           uint64_t value, const char *name)
3253 {
3254         struct drm_property_enum *prop_enum;
3255
3256         if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
3257                 return -EINVAL;
3258
3259         /*
3260          * Bitmask enum properties have the additional constraint of values
3261          * from 0 to 63
3262          */
3263         if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
3264                 return -EINVAL;
3265
3266         if (!list_empty(&property->enum_blob_list)) {
3267                 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3268                         if (prop_enum->value == value) {
3269                                 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3270                                 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3271                                 return 0;
3272                         }
3273                 }
3274         }
3275
3276         prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3277         if (!prop_enum)
3278                 return -ENOMEM;
3279
3280         strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3281         prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3282         prop_enum->value = value;
3283
3284         property->values[index] = value;
3285         list_add_tail(&prop_enum->head, &property->enum_blob_list);
3286         return 0;
3287 }
3288 EXPORT_SYMBOL(drm_property_add_enum);
3289
3290 /**
3291  * drm_property_destroy - destroy a drm property
3292  * @dev: drm device
3293  * @property: property to destry
3294  *
3295  * This function frees a property including any attached resources like
3296  * enumeration values.
3297  */
3298 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3299 {
3300         struct drm_property_enum *prop_enum, *pt;
3301
3302         list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3303                 list_del(&prop_enum->head);
3304                 kfree(prop_enum);
3305         }
3306
3307         if (property->num_values)
3308                 kfree(property->values);
3309         drm_mode_object_put(dev, &property->base);
3310         list_del(&property->head);
3311         kfree(property);
3312 }
3313 EXPORT_SYMBOL(drm_property_destroy);
3314
3315 /**
3316  * drm_object_attach_property - attach a property to a modeset object
3317  * @obj: drm modeset object
3318  * @property: property to attach
3319  * @init_val: initial value of the property
3320  *
3321  * This attaches the given property to the modeset object with the given initial
3322  * value. Currently this function cannot fail since the properties are stored in
3323  * a statically sized array.
3324  */
3325 void drm_object_attach_property(struct drm_mode_object *obj,
3326                                 struct drm_property *property,
3327                                 uint64_t init_val)
3328 {
3329         int count = obj->properties->count;
3330
3331         if (count == DRM_OBJECT_MAX_PROPERTY) {
3332                 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3333                         "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3334                         "you see this message on the same object type.\n",
3335                         obj->type);
3336                 return;
3337         }
3338
3339         obj->properties->ids[count] = property->base.id;
3340         obj->properties->values[count] = init_val;
3341         obj->properties->count++;
3342 }
3343 EXPORT_SYMBOL(drm_object_attach_property);
3344
3345 /**
3346  * drm_object_property_set_value - set the value of a property
3347  * @obj: drm mode object to set property value for
3348  * @property: property to set
3349  * @val: value the property should be set to
3350  *
3351  * This functions sets a given property on a given object. This function only
3352  * changes the software state of the property, it does not call into the
3353  * driver's ->set_property callback.
3354  *
3355  * Returns:
3356  * Zero on success, error code on failure.
3357  */
3358 int drm_object_property_set_value(struct drm_mode_object *obj,
3359                                   struct drm_property *property, uint64_t val)
3360 {
3361         int i;
3362
3363         for (i = 0; i < obj->properties->count; i++) {
3364                 if (obj->properties->ids[i] == property->base.id) {
3365                         obj->properties->values[i] = val;
3366                         return 0;
3367                 }
3368         }
3369
3370         return -EINVAL;
3371 }
3372 EXPORT_SYMBOL(drm_object_property_set_value);
3373
3374 /**
3375  * drm_object_property_get_value - retrieve the value of a property
3376  * @obj: drm mode object to get property value from
3377  * @property: property to retrieve
3378  * @val: storage for the property value
3379  *
3380  * This function retrieves the softare state of the given property for the given
3381  * property. Since there is no driver callback to retrieve the current property
3382  * value this might be out of sync with the hardware, depending upon the driver
3383  * and property.
3384  *
3385  * Returns:
3386  * Zero on success, error code on failure.
3387  */
3388 int drm_object_property_get_value(struct drm_mode_object *obj,
3389                                   struct drm_property *property, uint64_t *val)
3390 {
3391         int i;
3392
3393         for (i = 0; i < obj->properties->count; i++) {
3394                 if (obj->properties->ids[i] == property->base.id) {
3395                         *val = obj->properties->values[i];
3396                         return 0;
3397                 }
3398         }
3399
3400         return -EINVAL;
3401 }
3402 EXPORT_SYMBOL(drm_object_property_get_value);
3403
3404 /**
3405  * drm_mode_getproperty_ioctl - get the current value of a connector's property
3406  * @dev: DRM device
3407  * @data: ioctl data
3408  * @file_priv: DRM file info
3409  *
3410  * This function retrieves the current value for an connectors's property.
3411  *
3412  * Called by the user via ioctl.
3413  *
3414  * Returns:
3415  * Zero on success, errno on failure.
3416  */
3417 int drm_mode_getproperty_ioctl(struct drm_device *dev,
3418                                void *data, struct drm_file *file_priv)
3419 {
3420         struct drm_mode_object *obj;
3421         struct drm_mode_get_property *out_resp = data;
3422         struct drm_property *property;
3423         int enum_count = 0;
3424         int blob_count = 0;
3425         int value_count = 0;
3426         int ret = 0, i;
3427         int copied;
3428         struct drm_property_enum *prop_enum;
3429         struct drm_mode_property_enum __user *enum_ptr;
3430         struct drm_property_blob *prop_blob;
3431         uint32_t __user *blob_id_ptr;
3432         uint64_t __user *values_ptr;
3433         uint32_t __user *blob_length_ptr;
3434
3435         if (!drm_core_check_feature(dev, DRIVER_MODESET))
3436                 return -EINVAL;
3437
3438         drm_modeset_lock_all(dev);
3439         obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3440         if (!obj) {
3441                 ret = -ENOENT;
3442                 goto done;
3443         }
3444         property = obj_to_property(obj);
3445
3446         if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3447                 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3448                         enum_count++;
3449         } else if (property->flags & DRM_MODE_PROP_BLOB) {
3450                 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3451                         blob_count++;
3452         }
3453
3454         value_count = property->num_values;
3455
3456         strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3457         out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3458         out_resp->flags = property->flags;
3459
3460         if ((out_resp->count_values >= value_count) && value_count) {
3461                 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3462                 for (i = 0; i < value_count; i++) {
3463                         if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3464                                 ret = -EFAULT;
3465                                 goto done;
3466                         }
3467                 }
3468         }
3469         out_resp->count_values = value_count;
3470
3471         if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3472                 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3473                         copied = 0;
3474                         enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3475                         list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3476
3477                                 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3478                                         ret = -EFAULT;
3479                                         goto done;
3480                                 }
3481
3482                                 if (copy_to_user(&enum_ptr[copied].name,
3483                                                  &prop_enum->name, DRM_PROP_NAME_LEN)) {
3484                                         ret = -EFAULT;
3485                                         goto done;
3486                                 }
3487                                 copied++;
3488                         }
3489                 }
3490                 out_resp->count_enum_blobs = enum_count;
3491         }
3492
3493         if (property->flags & DRM_MODE_PROP_BLOB) {
3494                 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3495                         copied = 0;
3496                         blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3497                         blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
3498
3499                         list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3500                                 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3501                                         ret = -EFAULT;
3502                                         goto done;
3503                                 }
3504
3505                                 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3506                                         ret = -EFAULT;
3507                                         goto done;
3508                                 }
3509
3510                                 copied++;
3511                         }
3512                 }
3513                 out_resp->count_enum_blobs = blob_count;
3514         }
3515 done:
3516         drm_modeset_unlock_all(dev);
3517         return ret;
3518 }
3519
3520 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3521                                                           void *data)
3522 {
3523         struct drm_property_blob *blob;
3524         int ret;
3525
3526         if (!length || !data)
3527                 return NULL;
3528
3529         blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3530         if (!blob)
3531                 return NULL;
3532
3533         ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3534         if (ret) {
3535                 kfree(blob);
3536                 return NULL;
3537         }
3538
3539         blob->length = length;
3540
3541         memcpy(blob->data, data, length);
3542
3543         list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3544         return blob;
3545 }
3546
3547 static void drm_property_destroy_blob(struct drm_device *dev,
3548                                struct drm_property_blob *blob)
3549 {
3550         drm_mode_object_put(dev, &blob->base);
3551         list_del(&blob->head);
3552         kfree(blob);
3553 }
3554
3555 /**
3556  * drm_mode_getblob_ioctl - get the contents of a blob property value
3557  * @dev: DRM device
3558  * @data: ioctl data
3559  * @file_priv: DRM file info
3560  *
3561  * This function retrieves the contents of a blob property. The value stored in
3562  * an object's blob property is just a normal modeset object id.
3563  *
3564  * Called by the user via ioctl.
3565  *
3566  * Returns:
3567  * Zero on success, errno on failure.
3568  */
3569 int drm_mode_getblob_ioctl(struct drm_device *dev,
3570                            void *data, struct drm_file *file_priv)
3571 {
3572         struct drm_mode_object *obj;
3573         struct drm_mode_get_blob *out_resp = data;
3574         struct drm_property_blob *blob;
3575         int ret = 0;
3576         void __user *blob_ptr;
3577
3578         if (!drm_core_check_feature(dev, DRIVER_MODESET))
3579                 return -EINVAL;
3580
3581         drm_modeset_lock_all(dev);
3582         obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3583         if (!obj) {
3584                 ret = -ENOENT;
3585                 goto done;
3586         }
3587         blob = obj_to_blob(obj);
3588
3589         if (out_resp->length == blob->length) {
3590                 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3591                 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3592                         ret = -EFAULT;
3593                         goto done;
3594                 }
3595         }
3596         out_resp->length = blob->length;
3597
3598 done:
3599         drm_modeset_unlock_all(dev);
3600         return ret;
3601 }
3602
3603 /**
3604  * drm_mode_connector_update_edid_property - update the edid property of a connector
3605  * @connector: drm connector
3606  * @edid: new value of the edid property
3607  *
3608  * This function creates a new blob modeset object and assigns its id to the
3609  * connector's edid property.
3610  *
3611  * Returns:
3612  * Zero on success, errno on failure.
3613  */
3614 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3615                                             struct edid *edid)
3616 {
3617         struct drm_device *dev = connector->dev;
3618         int ret, size;
3619
3620         if (connector->edid_blob_ptr)
3621                 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3622
3623         /* Delete edid, when there is none. */
3624         if (!edid) {
3625                 connector->edid_blob_ptr = NULL;
3626                 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
3627                 return ret;
3628         }
3629
3630         size = EDID_LENGTH * (1 + edid->extensions);
3631         connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3632                                                             size, edid);
3633         if (!connector->edid_blob_ptr)
3634                 return -EINVAL;
3635
3636         ret = drm_object_property_set_value(&connector->base,
3637                                                dev->mode_config.edid_property,
3638                                                connector->edid_blob_ptr->base.id);
3639
3640         return ret;
3641 }
3642 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3643
3644 static bool drm_property_change_is_valid(struct drm_property *property,
3645                                          uint64_t value)
3646 {
3647         if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3648                 return false;
3649         if (property->flags & DRM_MODE_PROP_RANGE) {
3650                 if (value < property->values[0] || value > property->values[1])
3651                         return false;
3652                 return true;
3653         } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3654                 int i;
3655                 uint64_t valid_mask = 0;
3656                 for (i = 0; i < property->num_values; i++)
3657                         valid_mask |= (1ULL << property->values[i]);
3658                 return !(value & ~valid_mask);
3659         } else if (property->flags & DRM_MODE_PROP_BLOB) {
3660                 /* Only the driver knows */
3661                 return true;
3662         } else {
3663                 int i;
3664                 for (i = 0; i < property->num_values; i++)
3665                         if (property->values[i] == value)
3666                                 return true;
3667                 return false;
3668         }
3669 }
3670
3671 /**
3672  * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3673  * @dev: DRM device
3674  * @data: ioctl data
3675  * @file_priv: DRM file info
3676  *
3677  * This function sets the current value for a connectors's property. It also
3678  * calls into a driver's ->set_property callback to update the hardware state
3679  *
3680  * Called by the user via ioctl.
3681  *
3682  * Returns:
3683  * Zero on success, errno on failure.
3684  */
3685 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3686                                        void *data, struct drm_file *file_priv)
3687 {
3688         struct drm_mode_connector_set_property *conn_set_prop = data;
3689         struct drm_mode_obj_set_property obj_set_prop = {
3690                 .value = conn_set_prop->value,
3691                 .prop_id = conn_set_prop->prop_id,
3692                 .obj_id = conn_set_prop->connector_id,
3693                 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3694         };
3695
3696         /* It does all the locking and checking we need */
3697         return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3698 }
3699
3700 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3701                                            struct drm_property *property,
3702                                            uint64_t value)
3703 {
3704         int ret = -EINVAL;
3705         struct drm_connector *connector = obj_to_connector(obj);
3706
3707         /* Do DPMS ourselves */
3708         if (property == connector->dev->mode_config.dpms_property) {
3709                 if (connector->funcs->dpms)
3710                         (*connector->funcs->dpms)(connector, (int)value);
3711                 ret = 0;
3712         } else if (connector->funcs->set_property)
3713                 ret = connector->funcs->set_property(connector, property, value);
3714
3715         /* store the property value if successful */
3716         if (!ret)
3717                 drm_object_property_set_value(&connector->base, property, value);
3718         return ret;
3719 }
3720
3721 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3722                                       struct drm_property *property,
3723                                       uint64_t value)
3724 {
3725         int ret = -EINVAL;
3726         struct drm_crtc *crtc = obj_to_crtc(obj);
3727
3728         if (crtc->funcs->set_property)
3729                 ret = crtc->funcs->set_property(crtc, property, value);
3730         if (!ret)
3731                 drm_object_property_set_value(obj, property, value);
3732
3733         return ret;
3734 }
3735
3736 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3737                                       struct drm_property *property,
3738                                       uint64_t value)
3739 {
3740         int ret = -EINVAL;
3741         struct drm_plane *plane = obj_to_plane(obj);
3742
3743         if (plane->funcs->set_property)
3744                 ret = plane->funcs->set_property(plane, property, value);
3745         if (!ret)
3746                 drm_object_property_set_value(obj, property, value);
3747
3748         return ret;
3749 }
3750
3751 /**
3752  * drm_mode_getproperty_ioctl - get the current value of a object's property
3753  * @dev: DRM device
3754  * @data: ioctl data
3755  * @file_priv: DRM file info
3756  *
3757  * This function retrieves the current value for an object's property. Compared
3758  * to the connector specific ioctl this one is extended to also work on crtc and
3759  * plane objects.
3760  *
3761  * Called by the user via ioctl.
3762  *
3763  * Returns:
3764  * Zero on success, errno on failure.
3765  */
3766 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3767                                       struct drm_file *file_priv)
3768 {
3769         struct drm_mode_obj_get_properties *arg = data;
3770         struct drm_mode_object *obj;
3771         int ret = 0;
3772         int i;
3773         int copied = 0;
3774         int props_count = 0;
3775         uint32_t __user *props_ptr;
3776         uint64_t __user *prop_values_ptr;
3777
3778         if (!drm_core_check_feature(dev, DRIVER_MODESET))
3779                 return -EINVAL;
3780
3781         drm_modeset_lock_all(dev);
3782
3783         obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3784         if (!obj) {
3785                 ret = -ENOENT;
3786                 goto out;
3787         }
3788         if (!obj->properties) {
3789                 ret = -EINVAL;
3790                 goto out;
3791         }
3792
3793         props_count = obj->properties->count;
3794
3795         /* This ioctl is called twice, once to determine how much space is
3796          * needed, and the 2nd time to fill it. */
3797         if ((arg->count_props >= props_count) && props_count) {
3798                 copied = 0;
3799                 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3800                 prop_values_ptr = (uint64_t __user *)(unsigned long)
3801                                   (arg->prop_values_ptr);
3802                 for (i = 0; i < props_count; i++) {
3803                         if (put_user(obj->properties->ids[i],
3804                                      props_ptr + copied)) {
3805                                 ret = -EFAULT;
3806                                 goto out;
3807                         }
3808                         if (put_user(obj->properties->values[i],
3809                                      prop_values_ptr + copied)) {
3810                                 ret = -EFAULT;
3811                                 goto out;
3812                         }
3813                         copied++;
3814                 }
3815         }
3816         arg->count_props = props_count;
3817 out:
3818         drm_modeset_unlock_all(dev);
3819         return ret;
3820 }
3821
3822 /**
3823  * drm_mode_obj_set_property_ioctl - set the current value of an object's property
3824  * @dev: DRM device
3825  * @data: ioctl data
3826  * @file_priv: DRM file info
3827  *
3828  * This function sets the current value for an object's property. It also calls
3829  * into a driver's ->set_property callback to update the hardware state.
3830  * Compared to the connector specific ioctl this one is extended to also work on
3831  * crtc and plane objects.
3832  *
3833  * Called by the user via ioctl.
3834  *
3835  * Returns:
3836  * Zero on success, errno on failure.
3837  */
3838 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3839                                     struct drm_file *file_priv)
3840 {
3841         struct drm_mode_obj_set_property *arg = data;
3842         struct drm_mode_object *arg_obj;
3843         struct drm_mode_object *prop_obj;
3844         struct drm_property *property;
3845         int ret = -EINVAL;
3846         int i;
3847
3848         if (!drm_core_check_feature(dev, DRIVER_MODESET))
3849                 return -EINVAL;
3850
3851         drm_modeset_lock_all(dev);
3852
3853         arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3854         if (!arg_obj) {
3855                 ret = -ENOENT;
3856                 goto out;
3857         }
3858         if (!arg_obj->properties)
3859                 goto out;
3860
3861         for (i = 0; i < arg_obj->properties->count; i++)
3862                 if (arg_obj->properties->ids[i] == arg->prop_id)
3863                         break;
3864
3865         if (i == arg_obj->properties->count)
3866                 goto out;
3867
3868         prop_obj = drm_mode_object_find(dev, arg->prop_id,
3869                                         DRM_MODE_OBJECT_PROPERTY);
3870         if (!prop_obj) {
3871                 ret = -ENOENT;
3872                 goto out;
3873         }
3874         property = obj_to_property(prop_obj);
3875
3876         if (!drm_property_change_is_valid(property, arg->value))
3877                 goto out;
3878
3879         switch (arg_obj->type) {
3880         case DRM_MODE_OBJECT_CONNECTOR:
3881                 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3882                                                       arg->value);
3883                 break;
3884         case DRM_MODE_OBJECT_CRTC:
3885                 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3886                 break;
3887         case DRM_MODE_OBJECT_PLANE:
3888                 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3889                 break;
3890         }
3891
3892 out:
3893         drm_modeset_unlock_all(dev);
3894         return ret;
3895 }
3896
3897 /**
3898  * drm_mode_connector_attach_encoder - attach a connector to an encoder
3899  * @connector: connector to attach
3900  * @encoder: encoder to attach @connector to
3901  *
3902  * This function links up a connector to an encoder. Note that the routing
3903  * restrictions between encoders and crtcs are exposed to userspace through the
3904  * possible_clones and possible_crtcs bitmasks.
3905  *
3906  * Returns:
3907  * Zero on success, errno on failure.
3908  */
3909 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3910                                       struct drm_encoder *encoder)
3911 {
3912         int i;
3913
3914         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3915                 if (connector->encoder_ids[i] == 0) {
3916                         connector->encoder_ids[i] = encoder->base.id;
3917                         return 0;
3918                 }
3919         }
3920         return -ENOMEM;
3921 }
3922 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3923
3924 /**
3925  * drm_mode_crtc_set_gamma_size - set the gamma table size
3926  * @crtc: CRTC to set the gamma table size for
3927  * @gamma_size: size of the gamma table
3928  *
3929  * Drivers which support gamma tables should set this to the supported gamma
3930  * table size when initializing the CRTC. Currently the drm core only supports a
3931  * fixed gamma table size.
3932  *
3933  * Returns:
3934  * Zero on success, errno on failure.
3935  */
3936 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
3937                                  int gamma_size)
3938 {
3939         crtc->gamma_size = gamma_size;
3940
3941         crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3942         if (!crtc->gamma_store) {
3943                 crtc->gamma_size = 0;
3944                 return -ENOMEM;
3945         }
3946
3947         return 0;
3948 }
3949 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3950
3951 /**
3952  * drm_mode_gamma_set_ioctl - set the gamma table
3953  * @dev: DRM device
3954  * @data: ioctl data
3955  * @file_priv: DRM file info
3956  *
3957  * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
3958  * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
3959  *
3960  * Called by the user via ioctl.
3961  *
3962  * Returns:
3963  * Zero on success, errno on failure.
3964  */
3965 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3966                              void *data, struct drm_file *file_priv)
3967 {
3968         struct drm_mode_crtc_lut *crtc_lut = data;
3969         struct drm_mode_object *obj;
3970         struct drm_crtc *crtc;
3971         void *r_base, *g_base, *b_base;
3972         int size;
3973         int ret = 0;
3974
3975         if (!drm_core_check_feature(dev, DRIVER_MODESET))
3976                 return -EINVAL;
3977
3978         drm_modeset_lock_all(dev);
3979         obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3980         if (!obj) {
3981                 ret = -ENOENT;
3982                 goto out;
3983         }
3984         crtc = obj_to_crtc(obj);
3985
3986         if (crtc->funcs->gamma_set == NULL) {
3987                 ret = -ENOSYS;
3988                 goto out;
3989         }
3990
3991         /* memcpy into gamma store */
3992         if (crtc_lut->gamma_size != crtc->gamma_size) {
3993                 ret = -EINVAL;
3994                 goto out;
3995         }
3996
3997         size = crtc_lut->gamma_size * (sizeof(uint16_t));
3998         r_base = crtc->gamma_store;
3999         if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4000                 ret = -EFAULT;
4001                 goto out;
4002         }
4003
4004         g_base = r_base + size;
4005         if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4006                 ret = -EFAULT;
4007                 goto out;
4008         }
4009
4010         b_base = g_base + size;
4011         if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4012                 ret = -EFAULT;
4013                 goto out;
4014         }
4015
4016         crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
4017
4018 out:
4019         drm_modeset_unlock_all(dev);
4020         return ret;
4021
4022 }
4023
4024 /**
4025  * drm_mode_gamma_get_ioctl - get the gamma table
4026  * @dev: DRM device
4027  * @data: ioctl data
4028  * @file_priv: DRM file info
4029  *
4030  * Copy the current gamma table into the storage provided. This also provides
4031  * the gamma table size the driver expects, which can be used to size the
4032  * allocated storage.
4033  *
4034  * Called by the user via ioctl.
4035  *
4036  * Returns:
4037  * Zero on success, errno on failure.
4038  */
4039 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4040                              void *data, struct drm_file *file_priv)
4041 {
4042         struct drm_mode_crtc_lut *crtc_lut = data;
4043         struct drm_mode_object *obj;
4044         struct drm_crtc *crtc;
4045         void *r_base, *g_base, *b_base;
4046         int size;
4047         int ret = 0;
4048
4049         if (!drm_core_check_feature(dev, DRIVER_MODESET))
4050                 return -EINVAL;
4051
4052         drm_modeset_lock_all(dev);
4053         obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
4054         if (!obj) {
4055                 ret = -ENOENT;
4056                 goto out;
4057         }
4058         crtc = obj_to_crtc(obj);
4059
4060         /* memcpy into gamma store */
4061         if (crtc_lut->gamma_size != crtc->gamma_size) {
4062                 ret = -EINVAL;
4063                 goto out;
4064         }
4065
4066         size = crtc_lut->gamma_size * (sizeof(uint16_t));
4067         r_base = crtc->gamma_store;
4068         if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4069                 ret = -EFAULT;
4070                 goto out;
4071         }
4072
4073         g_base = r_base + size;
4074         if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4075                 ret = -EFAULT;
4076                 goto out;
4077         }
4078
4079         b_base = g_base + size;
4080         if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4081                 ret = -EFAULT;
4082                 goto out;
4083         }
4084 out:
4085         drm_modeset_unlock_all(dev);
4086         return ret;
4087 }
4088
4089 /**
4090  * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4091  * @dev: DRM device
4092  * @data: ioctl data
4093  * @file_priv: DRM file info
4094  *
4095  * This schedules an asynchronous update on a given CRTC, called page flip.
4096  * Optionally a drm event is generated to signal the completion of the event.
4097  * Generic drivers cannot assume that a pageflip with changed framebuffer
4098  * properties (including driver specific metadata like tiling layout) will work,
4099  * but some drivers support e.g. pixel format changes through the pageflip
4100  * ioctl.
4101  *
4102  * Called by the user via ioctl.
4103  *
4104  * Returns:
4105  * Zero on success, errno on failure.
4106  */
4107 int drm_mode_page_flip_ioctl(struct drm_device *dev,
4108                              void *data, struct drm_file *file_priv)
4109 {
4110         struct drm_mode_crtc_page_flip *page_flip = data;
4111         struct drm_mode_object *obj;
4112         struct drm_crtc *crtc;
4113         struct drm_framebuffer *fb = NULL, *old_fb = NULL;
4114         struct drm_pending_vblank_event *e = NULL;
4115         unsigned long flags;
4116         int ret = -EINVAL;
4117
4118         if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4119             page_flip->reserved != 0)
4120                 return -EINVAL;
4121
4122         if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4123                 return -EINVAL;
4124
4125         obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
4126         if (!obj)
4127                 return -ENOENT;
4128         crtc = obj_to_crtc(obj);
4129
4130         mutex_lock(&crtc->mutex);
4131         if (crtc->fb == NULL) {
4132                 /* The framebuffer is currently unbound, presumably
4133                  * due to a hotplug event, that userspace has not
4134                  * yet discovered.
4135                  */
4136                 ret = -EBUSY;
4137                 goto out;
4138         }
4139
4140         if (crtc->funcs->page_flip == NULL)
4141                 goto out;
4142
4143         fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
4144         if (!fb) {
4145                 ret = -ENOENT;
4146                 goto out;
4147         }
4148
4149         ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4150         if (ret)
4151                 goto out;
4152
4153         if (crtc->fb->pixel_format != fb->pixel_format) {
4154                 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4155                 ret = -EINVAL;
4156                 goto out;
4157         }
4158
4159         if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4160                 ret = -ENOMEM;
4161                 spin_lock_irqsave(&dev->event_lock, flags);
4162                 if (file_priv->event_space < sizeof e->event) {
4163                         spin_unlock_irqrestore(&dev->event_lock, flags);
4164                         goto out;
4165                 }
4166                 file_priv->event_space -= sizeof e->event;
4167                 spin_unlock_irqrestore(&dev->event_lock, flags);
4168
4169                 e = kzalloc(sizeof *e, GFP_KERNEL);
4170                 if (e == NULL) {
4171                         spin_lock_irqsave(&dev->event_lock, flags);
4172                         file_priv->event_space += sizeof e->event;
4173                         spin_unlock_irqrestore(&dev->event_lock, flags);
4174                         goto out;
4175                 }
4176
4177                 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
4178                 e->event.base.length = sizeof e->event;
4179                 e->event.user_data = page_flip->user_data;
4180                 e->base.event = &e->event.base;
4181                 e->base.file_priv = file_priv;
4182                 e->base.destroy =
4183                         (void (*) (struct drm_pending_event *)) kfree;
4184         }
4185
4186         old_fb = crtc->fb;
4187         ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
4188         if (ret) {
4189                 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4190                         spin_lock_irqsave(&dev->event_lock, flags);
4191                         file_priv->event_space += sizeof e->event;
4192                         spin_unlock_irqrestore(&dev->event_lock, flags);
4193                         kfree(e);
4194                 }
4195                 /* Keep the old fb, don't unref it. */
4196                 old_fb = NULL;
4197         } else {
4198                 /*
4199                  * Warn if the driver hasn't properly updated the crtc->fb
4200                  * field to reflect that the new framebuffer is now used.
4201                  * Failing to do so will screw with the reference counting
4202                  * on framebuffers.
4203                  */
4204                 WARN_ON(crtc->fb != fb);
4205                 /* Unref only the old framebuffer. */
4206                 fb = NULL;
4207         }
4208
4209 out:
4210         if (fb)
4211                 drm_framebuffer_unreference(fb);
4212         if (old_fb)
4213                 drm_framebuffer_unreference(old_fb);
4214         mutex_unlock(&crtc->mutex);
4215
4216         return ret;
4217 }
4218
4219 /**
4220  * drm_mode_config_reset - call ->reset callbacks
4221  * @dev: drm device
4222  *
4223  * This functions calls all the crtc's, encoder's and connector's ->reset
4224  * callback. Drivers can use this in e.g. their driver load or resume code to
4225  * reset hardware and software state.
4226  */
4227 void drm_mode_config_reset(struct drm_device *dev)
4228 {
4229         struct drm_crtc *crtc;
4230         struct drm_encoder *encoder;
4231         struct drm_connector *connector;
4232
4233         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4234                 if (crtc->funcs->reset)
4235                         crtc->funcs->reset(crtc);
4236
4237         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4238                 if (encoder->funcs->reset)
4239                         encoder->funcs->reset(encoder);
4240
4241         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4242                 connector->status = connector_status_unknown;
4243
4244                 if (connector->funcs->reset)
4245                         connector->funcs->reset(connector);
4246         }
4247 }
4248 EXPORT_SYMBOL(drm_mode_config_reset);
4249
4250 /**
4251  * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4252  * @dev: DRM device
4253  * @data: ioctl data
4254  * @file_priv: DRM file info
4255  *
4256  * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4257  * TTM or something else entirely) and returns the resulting buffer handle. This
4258  * handle can then be wrapped up into a framebuffer modeset object.
4259  *
4260  * Note that userspace is not allowed to use such objects for render
4261  * acceleration - drivers must create their own private ioctls for such a use
4262  * case.
4263  *
4264  * Called by the user via ioctl.
4265  *
4266  * Returns:
4267  * Zero on success, errno on failure.
4268  */
4269 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4270                                void *data, struct drm_file *file_priv)
4271 {
4272         struct drm_mode_create_dumb *args = data;
4273         u32 cpp, stride, size;
4274
4275         if (!dev->driver->dumb_create)
4276                 return -ENOSYS;
4277         if (!args->width || !args->height || !args->bpp)
4278                 return -EINVAL;
4279
4280         /* overflow checks for 32bit size calculations */
4281         cpp = DIV_ROUND_UP(args->bpp, 8);
4282         if (cpp > 0xffffffffU / args->width)
4283                 return -EINVAL;
4284         stride = cpp * args->width;
4285         if (args->height > 0xffffffffU / stride)
4286                 return -EINVAL;
4287
4288         /* test for wrap-around */
4289         size = args->height * stride;
4290         if (PAGE_ALIGN(size) == 0)
4291                 return -EINVAL;
4292
4293         return dev->driver->dumb_create(file_priv, dev, args);
4294 }
4295
4296 /**
4297  * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4298  * @dev: DRM device
4299  * @data: ioctl data
4300  * @file_priv: DRM file info
4301  *
4302  * Allocate an offset in the drm device node's address space to be able to
4303  * memory map a dumb buffer.
4304  *
4305  * Called by the user via ioctl.
4306  *
4307  * Returns:
4308  * Zero on success, errno on failure.
4309  */
4310 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4311                              void *data, struct drm_file *file_priv)
4312 {
4313         struct drm_mode_map_dumb *args = data;
4314
4315         /* call driver ioctl to get mmap offset */
4316         if (!dev->driver->dumb_map_offset)
4317                 return -ENOSYS;
4318
4319         return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4320 }
4321
4322 /**
4323  * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4324  * @dev: DRM device
4325  * @data: ioctl data
4326  * @file_priv: DRM file info
4327  *
4328  * This destroys the userspace handle for the given dumb backing storage buffer.
4329  * Since buffer objects must be reference counted in the kernel a buffer object
4330  * won't be immediately freed if a framebuffer modeset object still uses it.
4331  *
4332  * Called by the user via ioctl.
4333  *
4334  * Returns:
4335  * Zero on success, errno on failure.
4336  */
4337 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4338                                 void *data, struct drm_file *file_priv)
4339 {
4340         struct drm_mode_destroy_dumb *args = data;
4341
4342         if (!dev->driver->dumb_destroy)
4343                 return -ENOSYS;
4344
4345         return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4346 }
4347
4348 /**
4349  * drm_fb_get_bpp_depth - get the bpp/depth values for format
4350  * @format: pixel format (DRM_FORMAT_*)
4351  * @depth: storage for the depth value
4352  * @bpp: storage for the bpp value
4353  *
4354  * This only supports RGB formats here for compat with code that doesn't use
4355  * pixel formats directly yet.
4356  */
4357 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4358                           int *bpp)
4359 {
4360         switch (format) {
4361         case DRM_FORMAT_C8:
4362         case DRM_FORMAT_RGB332:
4363         case DRM_FORMAT_BGR233:
4364                 *depth = 8;
4365                 *bpp = 8;
4366                 break;
4367         case DRM_FORMAT_XRGB1555:
4368         case DRM_FORMAT_XBGR1555:
4369         case DRM_FORMAT_RGBX5551:
4370         case DRM_FORMAT_BGRX5551:
4371         case DRM_FORMAT_ARGB1555:
4372         case DRM_FORMAT_ABGR1555:
4373         case DRM_FORMAT_RGBA5551:
4374         case DRM_FORMAT_BGRA5551:
4375                 *depth = 15;
4376                 *bpp = 16;
4377                 break;
4378         case DRM_FORMAT_RGB565:
4379         case DRM_FORMAT_BGR565:
4380                 *depth = 16;
4381                 *bpp = 16;
4382                 break;
4383         case DRM_FORMAT_RGB888:
4384         case DRM_FORMAT_BGR888:
4385                 *depth = 24;
4386                 *bpp = 24;
4387                 break;
4388         case DRM_FORMAT_XRGB8888:
4389         case DRM_FORMAT_XBGR8888:
4390         case DRM_FORMAT_RGBX8888:
4391         case DRM_FORMAT_BGRX8888:
4392                 *depth = 24;
4393                 *bpp = 32;
4394                 break;
4395         case DRM_FORMAT_XRGB2101010:
4396         case DRM_FORMAT_XBGR2101010:
4397         case DRM_FORMAT_RGBX1010102:
4398         case DRM_FORMAT_BGRX1010102:
4399         case DRM_FORMAT_ARGB2101010:
4400         case DRM_FORMAT_ABGR2101010:
4401         case DRM_FORMAT_RGBA1010102:
4402         case DRM_FORMAT_BGRA1010102:
4403                 *depth = 30;
4404                 *bpp = 32;
4405                 break;
4406         case DRM_FORMAT_ARGB8888:
4407         case DRM_FORMAT_ABGR8888:
4408         case DRM_FORMAT_RGBA8888:
4409         case DRM_FORMAT_BGRA8888:
4410                 *depth = 32;
4411                 *bpp = 32;
4412                 break;
4413         default:
4414                 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4415                               drm_get_format_name(format));
4416                 *depth = 0;
4417                 *bpp = 0;
4418                 break;
4419         }
4420 }
4421 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
4422
4423 /**
4424  * drm_format_num_planes - get the number of planes for format
4425  * @format: pixel format (DRM_FORMAT_*)
4426  *
4427  * Returns:
4428  * The number of planes used by the specified pixel format.
4429  */
4430 int drm_format_num_planes(uint32_t format)
4431 {
4432         switch (format) {
4433         case DRM_FORMAT_YUV410:
4434         case DRM_FORMAT_YVU410:
4435         case DRM_FORMAT_YUV411:
4436         case DRM_FORMAT_YVU411:
4437         case DRM_FORMAT_YUV420:
4438         case DRM_FORMAT_YVU420:
4439         case DRM_FORMAT_YUV422:
4440         case DRM_FORMAT_YVU422:
4441         case DRM_FORMAT_YUV444:
4442         case DRM_FORMAT_YVU444:
4443                 return 3;
4444         case DRM_FORMAT_NV12:
4445         case DRM_FORMAT_NV21:
4446         case DRM_FORMAT_NV16:
4447         case DRM_FORMAT_NV61:
4448         case DRM_FORMAT_NV24:
4449         case DRM_FORMAT_NV42:
4450                 return 2;
4451         default:
4452                 return 1;
4453         }
4454 }
4455 EXPORT_SYMBOL(drm_format_num_planes);
4456
4457 /**
4458  * drm_format_plane_cpp - determine the bytes per pixel value
4459  * @format: pixel format (DRM_FORMAT_*)
4460  * @plane: plane index
4461  *
4462  * Returns:
4463  * The bytes per pixel value for the specified plane.
4464  */
4465 int drm_format_plane_cpp(uint32_t format, int plane)
4466 {
4467         unsigned int depth;
4468         int bpp;
4469
4470         if (plane >= drm_format_num_planes(format))
4471                 return 0;
4472
4473         switch (format) {
4474         case DRM_FORMAT_YUYV:
4475         case DRM_FORMAT_YVYU:
4476         case DRM_FORMAT_UYVY:
4477         case DRM_FORMAT_VYUY:
4478                 return 2;
4479         case DRM_FORMAT_NV12:
4480         case DRM_FORMAT_NV21:
4481         case DRM_FORMAT_NV16:
4482         case DRM_FORMAT_NV61:
4483         case DRM_FORMAT_NV24:
4484         case DRM_FORMAT_NV42:
4485                 return plane ? 2 : 1;
4486         case DRM_FORMAT_YUV410:
4487         case DRM_FORMAT_YVU410:
4488         case DRM_FORMAT_YUV411:
4489         case DRM_FORMAT_YVU411:
4490         case DRM_FORMAT_YUV420:
4491         case DRM_FORMAT_YVU420:
4492         case DRM_FORMAT_YUV422:
4493         case DRM_FORMAT_YVU422:
4494         case DRM_FORMAT_YUV444:
4495         case DRM_FORMAT_YVU444:
4496                 return 1;
4497         default:
4498                 drm_fb_get_bpp_depth(format, &depth, &bpp);
4499                 return bpp >> 3;
4500         }
4501 }
4502 EXPORT_SYMBOL(drm_format_plane_cpp);
4503
4504 /**
4505  * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4506  * @format: pixel format (DRM_FORMAT_*)
4507  *
4508  * Returns:
4509  * The horizontal chroma subsampling factor for the
4510  * specified pixel format.
4511  */
4512 int drm_format_horz_chroma_subsampling(uint32_t format)
4513 {
4514         switch (format) {
4515         case DRM_FORMAT_YUV411:
4516         case DRM_FORMAT_YVU411:
4517         case DRM_FORMAT_YUV410:
4518         case DRM_FORMAT_YVU410:
4519                 return 4;
4520         case DRM_FORMAT_YUYV:
4521         case DRM_FORMAT_YVYU:
4522         case DRM_FORMAT_UYVY:
4523         case DRM_FORMAT_VYUY:
4524         case DRM_FORMAT_NV12:
4525         case DRM_FORMAT_NV21:
4526         case DRM_FORMAT_NV16:
4527         case DRM_FORMAT_NV61:
4528         case DRM_FORMAT_YUV422:
4529         case DRM_FORMAT_YVU422:
4530         case DRM_FORMAT_YUV420:
4531         case DRM_FORMAT_YVU420:
4532                 return 2;
4533         default:
4534                 return 1;
4535         }
4536 }
4537 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4538
4539 /**
4540  * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4541  * @format: pixel format (DRM_FORMAT_*)
4542  *
4543  * Returns:
4544  * The vertical chroma subsampling factor for the
4545  * specified pixel format.
4546  */
4547 int drm_format_vert_chroma_subsampling(uint32_t format)
4548 {
4549         switch (format) {
4550         case DRM_FORMAT_YUV410:
4551         case DRM_FORMAT_YVU410:
4552                 return 4;
4553         case DRM_FORMAT_YUV420:
4554         case DRM_FORMAT_YVU420:
4555         case DRM_FORMAT_NV12:
4556         case DRM_FORMAT_NV21:
4557                 return 2;
4558         default:
4559                 return 1;
4560         }
4561 }
4562 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
4563
4564 /**
4565  * drm_mode_config_init - initialize DRM mode_configuration structure
4566  * @dev: DRM device
4567  *
4568  * Initialize @dev's mode_config structure, used for tracking the graphics
4569  * configuration of @dev.
4570  *
4571  * Since this initializes the modeset locks, no locking is possible. Which is no
4572  * problem, since this should happen single threaded at init time. It is the
4573  * driver's problem to ensure this guarantee.
4574  *
4575  */
4576 void drm_mode_config_init(struct drm_device *dev)
4577 {
4578         mutex_init(&dev->mode_config.mutex);
4579         mutex_init(&dev->mode_config.idr_mutex);
4580         mutex_init(&dev->mode_config.fb_lock);
4581         INIT_LIST_HEAD(&dev->mode_config.fb_list);
4582         INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4583         INIT_LIST_HEAD(&dev->mode_config.connector_list);
4584         INIT_LIST_HEAD(&dev->mode_config.bridge_list);
4585         INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4586         INIT_LIST_HEAD(&dev->mode_config.property_list);
4587         INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4588         INIT_LIST_HEAD(&dev->mode_config.plane_list);
4589         idr_init(&dev->mode_config.crtc_idr);
4590
4591         drm_modeset_lock_all(dev);
4592         drm_mode_create_standard_connector_properties(dev);
4593         drm_mode_create_standard_plane_properties(dev);
4594         drm_modeset_unlock_all(dev);
4595
4596         /* Just to be sure */
4597         dev->mode_config.num_fb = 0;
4598         dev->mode_config.num_connector = 0;
4599         dev->mode_config.num_crtc = 0;
4600         dev->mode_config.num_encoder = 0;
4601         dev->mode_config.num_overlay_plane = 0;
4602         dev->mode_config.num_total_plane = 0;
4603 }
4604 EXPORT_SYMBOL(drm_mode_config_init);
4605
4606 /**
4607  * drm_mode_config_cleanup - free up DRM mode_config info
4608  * @dev: DRM device
4609  *
4610  * Free up all the connectors and CRTCs associated with this DRM device, then
4611  * free up the framebuffers and associated buffer objects.
4612  *
4613  * Note that since this /should/ happen single-threaded at driver/device
4614  * teardown time, no locking is required. It's the driver's job to ensure that
4615  * this guarantee actually holds true.
4616  *
4617  * FIXME: cleanup any dangling user buffer objects too
4618  */
4619 void drm_mode_config_cleanup(struct drm_device *dev)
4620 {
4621         struct drm_connector *connector, *ot;
4622         struct drm_crtc *crtc, *ct;
4623         struct drm_encoder *encoder, *enct;
4624         struct drm_bridge *bridge, *brt;
4625         struct drm_framebuffer *fb, *fbt;
4626         struct drm_property *property, *pt;
4627         struct drm_property_blob *blob, *bt;
4628         struct drm_plane *plane, *plt;
4629
4630         list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4631                                  head) {
4632                 encoder->funcs->destroy(encoder);
4633         }
4634
4635         list_for_each_entry_safe(bridge, brt,
4636                                  &dev->mode_config.bridge_list, head) {
4637                 bridge->funcs->destroy(bridge);
4638         }
4639
4640         list_for_each_entry_safe(connector, ot,
4641                                  &dev->mode_config.connector_list, head) {
4642                 connector->funcs->destroy(connector);
4643         }
4644
4645         list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4646                                  head) {
4647                 drm_property_destroy(dev, property);
4648         }
4649
4650         list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4651                                  head) {
4652                 drm_property_destroy_blob(dev, blob);
4653         }
4654
4655         /*
4656          * Single-threaded teardown context, so it's not required to grab the
4657          * fb_lock to protect against concurrent fb_list access. Contrary, it
4658          * would actually deadlock with the drm_framebuffer_cleanup function.
4659          *
4660          * Also, if there are any framebuffers left, that's a driver leak now,
4661          * so politely WARN about this.
4662          */
4663         WARN_ON(!list_empty(&dev->mode_config.fb_list));
4664         list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4665                 drm_framebuffer_remove(fb);
4666         }
4667
4668         list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4669                                  head) {
4670                 plane->funcs->destroy(plane);
4671         }
4672
4673         list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4674                 crtc->funcs->destroy(crtc);
4675         }
4676
4677         idr_destroy(&dev->mode_config.crtc_idr);
4678 }
4679 EXPORT_SYMBOL(drm_mode_config_cleanup);