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