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