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