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