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