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