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