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