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