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