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