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