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