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