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