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