drm: document drm_display_info
[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 #include <drm/drm_modeset_lock.h>
41 #include <drm/drm_atomic.h>
42 #include <drm/drm_auth.h>
43 #include <drm/drm_framebuffer.h>
44
45 #include "drm_crtc_internal.h"
46 #include "drm_internal.h"
47
48 /*
49 * Global properties
50 */
51 static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
52 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
53 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
54 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
55 };
56
57 static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
58 { DRM_MODE_ENCODER_NONE, "None" },
59 { DRM_MODE_ENCODER_DAC, "DAC" },
60 { DRM_MODE_ENCODER_TMDS, "TMDS" },
61 { DRM_MODE_ENCODER_LVDS, "LVDS" },
62 { DRM_MODE_ENCODER_TVDAC, "TV" },
63 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
64 { DRM_MODE_ENCODER_DSI, "DSI" },
65 { DRM_MODE_ENCODER_DPMST, "DP MST" },
66 { DRM_MODE_ENCODER_DPI, "DPI" },
67 };
68
69 /*
70 * Optional properties
71 */
72 /*
73 * Internal function to assign a slot in the object idr and optionally
74 * register the object into the idr.
75 */
76 int drm_mode_object_get_reg(struct drm_device *dev,
77 struct drm_mode_object *obj,
78 uint32_t obj_type,
79 bool register_obj,
80 void (*obj_free_cb)(struct kref *kref))
81 {
82 int ret;
83
84 mutex_lock(&dev->mode_config.idr_mutex);
85 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
86 if (ret >= 0) {
87 /*
88 * Set up the object linking under the protection of the idr
89 * lock so that other users can't see inconsistent state.
90 */
91 obj->id = ret;
92 obj->type = obj_type;
93 if (obj_free_cb) {
94 obj->free_cb = obj_free_cb;
95 kref_init(&obj->refcount);
96 }
97 }
98 mutex_unlock(&dev->mode_config.idr_mutex);
99
100 return ret < 0 ? ret : 0;
101 }
102
103 /**
104 * drm_mode_object_get - allocate a new modeset identifier
105 * @dev: DRM device
106 * @obj: object pointer, used to generate unique ID
107 * @obj_type: object type
108 *
109 * Create a unique identifier based on @ptr in @dev's identifier space. Used
110 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
111 * modeset identifiers are _not_ reference counted. Hence don't use this for
112 * reference counted modeset objects like framebuffers.
113 *
114 * Returns:
115 * Zero on success, error code on failure.
116 */
117 int drm_mode_object_get(struct drm_device *dev,
118 struct drm_mode_object *obj, uint32_t obj_type)
119 {
120 return drm_mode_object_get_reg(dev, obj, obj_type, true, NULL);
121 }
122
123 void drm_mode_object_register(struct drm_device *dev,
124 struct drm_mode_object *obj)
125 {
126 mutex_lock(&dev->mode_config.idr_mutex);
127 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
128 mutex_unlock(&dev->mode_config.idr_mutex);
129 }
130
131 /**
132 * drm_mode_object_unregister - free a modeset identifer
133 * @dev: DRM device
134 * @object: object to free
135 *
136 * Free @id from @dev's unique identifier pool.
137 * This function can be called multiple times, and guards against
138 * multiple removals.
139 * These modeset identifiers are _not_ reference counted. Hence don't use this
140 * for reference counted modeset objects like framebuffers.
141 */
142 void drm_mode_object_unregister(struct drm_device *dev,
143 struct drm_mode_object *object)
144 {
145 mutex_lock(&dev->mode_config.idr_mutex);
146 if (object->id) {
147 idr_remove(&dev->mode_config.crtc_idr, object->id);
148 object->id = 0;
149 }
150 mutex_unlock(&dev->mode_config.idr_mutex);
151 }
152
153 struct drm_mode_object *__drm_mode_object_find(struct drm_device *dev,
154 uint32_t id, uint32_t type)
155 {
156 struct drm_mode_object *obj = NULL;
157
158 mutex_lock(&dev->mode_config.idr_mutex);
159 obj = idr_find(&dev->mode_config.crtc_idr, id);
160 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
161 obj = NULL;
162 if (obj && obj->id != id)
163 obj = NULL;
164
165 if (obj && obj->free_cb) {
166 if (!kref_get_unless_zero(&obj->refcount))
167 obj = NULL;
168 }
169 mutex_unlock(&dev->mode_config.idr_mutex);
170
171 return obj;
172 }
173
174 /**
175 * drm_mode_object_find - look up a drm object with static lifetime
176 * @dev: drm device
177 * @id: id of the mode object
178 * @type: type of the mode object
179 *
180 * This function is used to look up a modeset object. It will acquire a
181 * reference for reference counted objects. This reference must be dropped again
182 * by callind drm_mode_object_unreference().
183 */
184 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
185 uint32_t id, uint32_t type)
186 {
187 struct drm_mode_object *obj = NULL;
188
189 obj = __drm_mode_object_find(dev, id, type);
190 return obj;
191 }
192 EXPORT_SYMBOL(drm_mode_object_find);
193
194 /**
195 * drm_mode_object_unreference - decr the object refcnt
196 * @obj: mode_object
197 *
198 * This functions decrements the object's refcount if it is a refcounted modeset
199 * object. It is a no-op on any other object. This is used to drop references
200 * acquired with drm_mode_object_reference().
201 */
202 void drm_mode_object_unreference(struct drm_mode_object *obj)
203 {
204 if (obj->free_cb) {
205 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
206 kref_put(&obj->refcount, obj->free_cb);
207 }
208 }
209 EXPORT_SYMBOL(drm_mode_object_unreference);
210
211 /**
212 * drm_mode_object_reference - incr the object refcnt
213 * @obj: mode_object
214 *
215 * This functions increments the object's refcount if it is a refcounted modeset
216 * object. It is a no-op on any other object. References should be dropped again
217 * by calling drm_mode_object_unreference().
218 */
219 void drm_mode_object_reference(struct drm_mode_object *obj)
220 {
221 if (obj->free_cb) {
222 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
223 kref_get(&obj->refcount);
224 }
225 }
226 EXPORT_SYMBOL(drm_mode_object_reference);
227
228 /**
229 * drm_crtc_force_disable - Forcibly turn off a CRTC
230 * @crtc: CRTC to turn off
231 *
232 * Returns:
233 * Zero on success, error code on failure.
234 */
235 int drm_crtc_force_disable(struct drm_crtc *crtc)
236 {
237 struct drm_mode_set set = {
238 .crtc = crtc,
239 };
240
241 return drm_mode_set_config_internal(&set);
242 }
243 EXPORT_SYMBOL(drm_crtc_force_disable);
244
245 /**
246 * drm_crtc_force_disable_all - Forcibly turn off all enabled CRTCs
247 * @dev: DRM device whose CRTCs to turn off
248 *
249 * Drivers may want to call this on unload to ensure that all displays are
250 * unlit and the GPU is in a consistent, low power state. Takes modeset locks.
251 *
252 * Returns:
253 * Zero on success, error code on failure.
254 */
255 int drm_crtc_force_disable_all(struct drm_device *dev)
256 {
257 struct drm_crtc *crtc;
258 int ret = 0;
259
260 drm_modeset_lock_all(dev);
261 drm_for_each_crtc(crtc, dev)
262 if (crtc->enabled) {
263 ret = drm_crtc_force_disable(crtc);
264 if (ret)
265 goto out;
266 }
267 out:
268 drm_modeset_unlock_all(dev);
269 return ret;
270 }
271 EXPORT_SYMBOL(drm_crtc_force_disable_all);
272
273 DEFINE_WW_CLASS(crtc_ww_class);
274
275 static unsigned int drm_num_crtcs(struct drm_device *dev)
276 {
277 unsigned int num = 0;
278 struct drm_crtc *tmp;
279
280 drm_for_each_crtc(tmp, dev) {
281 num++;
282 }
283
284 return num;
285 }
286
287 static int drm_crtc_register_all(struct drm_device *dev)
288 {
289 struct drm_crtc *crtc;
290 int ret = 0;
291
292 drm_for_each_crtc(crtc, dev) {
293 if (crtc->funcs->late_register)
294 ret = crtc->funcs->late_register(crtc);
295 if (ret)
296 return ret;
297 }
298
299 return 0;
300 }
301
302 static void drm_crtc_unregister_all(struct drm_device *dev)
303 {
304 struct drm_crtc *crtc;
305
306 drm_for_each_crtc(crtc, dev) {
307 if (crtc->funcs->early_unregister)
308 crtc->funcs->early_unregister(crtc);
309 }
310 }
311
312 /**
313 * drm_crtc_init_with_planes - Initialise a new CRTC object with
314 * specified primary and cursor planes.
315 * @dev: DRM device
316 * @crtc: CRTC object to init
317 * @primary: Primary plane for CRTC
318 * @cursor: Cursor plane for CRTC
319 * @funcs: callbacks for the new CRTC
320 * @name: printf style format string for the CRTC name, or NULL for default name
321 *
322 * Inits a new object created as base part of a driver crtc object.
323 *
324 * Returns:
325 * Zero on success, error code on failure.
326 */
327 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
328 struct drm_plane *primary,
329 struct drm_plane *cursor,
330 const struct drm_crtc_funcs *funcs,
331 const char *name, ...)
332 {
333 struct drm_mode_config *config = &dev->mode_config;
334 int ret;
335
336 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
337 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
338
339 crtc->dev = dev;
340 crtc->funcs = funcs;
341
342 INIT_LIST_HEAD(&crtc->commit_list);
343 spin_lock_init(&crtc->commit_lock);
344
345 drm_modeset_lock_init(&crtc->mutex);
346 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
347 if (ret)
348 return ret;
349
350 if (name) {
351 va_list ap;
352
353 va_start(ap, name);
354 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
355 va_end(ap);
356 } else {
357 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
358 drm_num_crtcs(dev));
359 }
360 if (!crtc->name) {
361 drm_mode_object_unregister(dev, &crtc->base);
362 return -ENOMEM;
363 }
364
365 crtc->base.properties = &crtc->properties;
366
367 list_add_tail(&crtc->head, &config->crtc_list);
368 crtc->index = config->num_crtc++;
369
370 crtc->primary = primary;
371 crtc->cursor = cursor;
372 if (primary)
373 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
374 if (cursor)
375 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
376
377 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
378 drm_object_attach_property(&crtc->base, config->prop_active, 0);
379 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
380 }
381
382 return 0;
383 }
384 EXPORT_SYMBOL(drm_crtc_init_with_planes);
385
386 /**
387 * drm_crtc_cleanup - Clean up the core crtc usage
388 * @crtc: CRTC to cleanup
389 *
390 * This function cleans up @crtc and removes it from the DRM mode setting
391 * core. Note that the function does *not* free the crtc structure itself,
392 * this is the responsibility of the caller.
393 */
394 void drm_crtc_cleanup(struct drm_crtc *crtc)
395 {
396 struct drm_device *dev = crtc->dev;
397
398 /* Note that the crtc_list is considered to be static; should we
399 * remove the drm_crtc at runtime we would have to decrement all
400 * the indices on the drm_crtc after us in the crtc_list.
401 */
402
403 kfree(crtc->gamma_store);
404 crtc->gamma_store = NULL;
405
406 drm_modeset_lock_fini(&crtc->mutex);
407
408 drm_mode_object_unregister(dev, &crtc->base);
409 list_del(&crtc->head);
410 dev->mode_config.num_crtc--;
411
412 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
413 if (crtc->state && crtc->funcs->atomic_destroy_state)
414 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
415
416 kfree(crtc->name);
417
418 memset(crtc, 0, sizeof(*crtc));
419 }
420 EXPORT_SYMBOL(drm_crtc_cleanup);
421
422 static int drm_encoder_register_all(struct drm_device *dev)
423 {
424 struct drm_encoder *encoder;
425 int ret = 0;
426
427 drm_for_each_encoder(encoder, dev) {
428 if (encoder->funcs->late_register)
429 ret = encoder->funcs->late_register(encoder);
430 if (ret)
431 return ret;
432 }
433
434 return 0;
435 }
436
437 static void drm_encoder_unregister_all(struct drm_device *dev)
438 {
439 struct drm_encoder *encoder;
440
441 drm_for_each_encoder(encoder, dev) {
442 if (encoder->funcs->early_unregister)
443 encoder->funcs->early_unregister(encoder);
444 }
445 }
446
447 /**
448 * drm_encoder_init - Init a preallocated encoder
449 * @dev: drm device
450 * @encoder: the encoder to init
451 * @funcs: callbacks for this encoder
452 * @encoder_type: user visible type of the encoder
453 * @name: printf style format string for the encoder name, or NULL for default name
454 *
455 * Initialises a preallocated encoder. Encoder should be
456 * subclassed as part of driver encoder objects.
457 *
458 * Returns:
459 * Zero on success, error code on failure.
460 */
461 int drm_encoder_init(struct drm_device *dev,
462 struct drm_encoder *encoder,
463 const struct drm_encoder_funcs *funcs,
464 int encoder_type, const char *name, ...)
465 {
466 int ret;
467
468 drm_modeset_lock_all(dev);
469
470 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
471 if (ret)
472 goto out_unlock;
473
474 encoder->dev = dev;
475 encoder->encoder_type = encoder_type;
476 encoder->funcs = funcs;
477 if (name) {
478 va_list ap;
479
480 va_start(ap, name);
481 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
482 va_end(ap);
483 } else {
484 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
485 drm_encoder_enum_list[encoder_type].name,
486 encoder->base.id);
487 }
488 if (!encoder->name) {
489 ret = -ENOMEM;
490 goto out_put;
491 }
492
493 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
494 encoder->index = dev->mode_config.num_encoder++;
495
496 out_put:
497 if (ret)
498 drm_mode_object_unregister(dev, &encoder->base);
499
500 out_unlock:
501 drm_modeset_unlock_all(dev);
502
503 return ret;
504 }
505 EXPORT_SYMBOL(drm_encoder_init);
506
507 /**
508 * drm_encoder_cleanup - cleans up an initialised encoder
509 * @encoder: encoder to cleanup
510 *
511 * Cleans up the encoder but doesn't free the object.
512 */
513 void drm_encoder_cleanup(struct drm_encoder *encoder)
514 {
515 struct drm_device *dev = encoder->dev;
516
517 /* Note that the encoder_list is considered to be static; should we
518 * remove the drm_encoder at runtime we would have to decrement all
519 * the indices on the drm_encoder after us in the encoder_list.
520 */
521
522 drm_modeset_lock_all(dev);
523 drm_mode_object_unregister(dev, &encoder->base);
524 kfree(encoder->name);
525 list_del(&encoder->head);
526 dev->mode_config.num_encoder--;
527 drm_modeset_unlock_all(dev);
528
529 memset(encoder, 0, sizeof(*encoder));
530 }
531 EXPORT_SYMBOL(drm_encoder_cleanup);
532
533 static unsigned int drm_num_planes(struct drm_device *dev)
534 {
535 unsigned int num = 0;
536 struct drm_plane *tmp;
537
538 drm_for_each_plane(tmp, dev) {
539 num++;
540 }
541
542 return num;
543 }
544
545 /**
546 * drm_universal_plane_init - Initialize a new universal plane object
547 * @dev: DRM device
548 * @plane: plane object to init
549 * @possible_crtcs: bitmask of possible CRTCs
550 * @funcs: callbacks for the new plane
551 * @formats: array of supported formats (DRM_FORMAT\_\*)
552 * @format_count: number of elements in @formats
553 * @type: type of plane (overlay, primary, cursor)
554 * @name: printf style format string for the plane name, or NULL for default name
555 *
556 * Initializes a plane object of type @type.
557 *
558 * Returns:
559 * Zero on success, error code on failure.
560 */
561 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
562 unsigned long possible_crtcs,
563 const struct drm_plane_funcs *funcs,
564 const uint32_t *formats, unsigned int format_count,
565 enum drm_plane_type type,
566 const char *name, ...)
567 {
568 struct drm_mode_config *config = &dev->mode_config;
569 int ret;
570
571 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
572 if (ret)
573 return ret;
574
575 drm_modeset_lock_init(&plane->mutex);
576
577 plane->base.properties = &plane->properties;
578 plane->dev = dev;
579 plane->funcs = funcs;
580 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
581 GFP_KERNEL);
582 if (!plane->format_types) {
583 DRM_DEBUG_KMS("out of memory when allocating plane\n");
584 drm_mode_object_unregister(dev, &plane->base);
585 return -ENOMEM;
586 }
587
588 if (name) {
589 va_list ap;
590
591 va_start(ap, name);
592 plane->name = kvasprintf(GFP_KERNEL, name, ap);
593 va_end(ap);
594 } else {
595 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
596 drm_num_planes(dev));
597 }
598 if (!plane->name) {
599 kfree(plane->format_types);
600 drm_mode_object_unregister(dev, &plane->base);
601 return -ENOMEM;
602 }
603
604 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
605 plane->format_count = format_count;
606 plane->possible_crtcs = possible_crtcs;
607 plane->type = type;
608
609 list_add_tail(&plane->head, &config->plane_list);
610 plane->index = config->num_total_plane++;
611 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
612 config->num_overlay_plane++;
613
614 drm_object_attach_property(&plane->base,
615 config->plane_type_property,
616 plane->type);
617
618 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
619 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
620 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
621 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
622 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
623 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
624 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
625 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
626 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
627 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
628 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
629 }
630
631 return 0;
632 }
633 EXPORT_SYMBOL(drm_universal_plane_init);
634
635 static int drm_plane_register_all(struct drm_device *dev)
636 {
637 struct drm_plane *plane;
638 int ret = 0;
639
640 drm_for_each_plane(plane, dev) {
641 if (plane->funcs->late_register)
642 ret = plane->funcs->late_register(plane);
643 if (ret)
644 return ret;
645 }
646
647 return 0;
648 }
649
650 static void drm_plane_unregister_all(struct drm_device *dev)
651 {
652 struct drm_plane *plane;
653
654 drm_for_each_plane(plane, dev) {
655 if (plane->funcs->early_unregister)
656 plane->funcs->early_unregister(plane);
657 }
658 }
659
660 /**
661 * drm_plane_init - Initialize a legacy plane
662 * @dev: DRM device
663 * @plane: plane object to init
664 * @possible_crtcs: bitmask of possible CRTCs
665 * @funcs: callbacks for the new plane
666 * @formats: array of supported formats (DRM_FORMAT\_\*)
667 * @format_count: number of elements in @formats
668 * @is_primary: plane type (primary vs overlay)
669 *
670 * Legacy API to initialize a DRM plane.
671 *
672 * New drivers should call drm_universal_plane_init() instead.
673 *
674 * Returns:
675 * Zero on success, error code on failure.
676 */
677 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
678 unsigned long possible_crtcs,
679 const struct drm_plane_funcs *funcs,
680 const uint32_t *formats, unsigned int format_count,
681 bool is_primary)
682 {
683 enum drm_plane_type type;
684
685 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
686 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
687 formats, format_count, type, NULL);
688 }
689 EXPORT_SYMBOL(drm_plane_init);
690
691 /**
692 * drm_plane_cleanup - Clean up the core plane usage
693 * @plane: plane to cleanup
694 *
695 * This function cleans up @plane and removes it from the DRM mode setting
696 * core. Note that the function does *not* free the plane structure itself,
697 * this is the responsibility of the caller.
698 */
699 void drm_plane_cleanup(struct drm_plane *plane)
700 {
701 struct drm_device *dev = plane->dev;
702
703 drm_modeset_lock_all(dev);
704 kfree(plane->format_types);
705 drm_mode_object_unregister(dev, &plane->base);
706
707 BUG_ON(list_empty(&plane->head));
708
709 /* Note that the plane_list is considered to be static; should we
710 * remove the drm_plane at runtime we would have to decrement all
711 * the indices on the drm_plane after us in the plane_list.
712 */
713
714 list_del(&plane->head);
715 dev->mode_config.num_total_plane--;
716 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
717 dev->mode_config.num_overlay_plane--;
718 drm_modeset_unlock_all(dev);
719
720 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
721 if (plane->state && plane->funcs->atomic_destroy_state)
722 plane->funcs->atomic_destroy_state(plane, plane->state);
723
724 kfree(plane->name);
725
726 memset(plane, 0, sizeof(*plane));
727 }
728 EXPORT_SYMBOL(drm_plane_cleanup);
729
730 /**
731 * drm_plane_from_index - find the registered plane at an index
732 * @dev: DRM device
733 * @idx: index of registered plane to find for
734 *
735 * Given a plane index, return the registered plane from DRM device's
736 * list of planes with matching index.
737 */
738 struct drm_plane *
739 drm_plane_from_index(struct drm_device *dev, int idx)
740 {
741 struct drm_plane *plane;
742
743 drm_for_each_plane(plane, dev)
744 if (idx == plane->index)
745 return plane;
746
747 return NULL;
748 }
749 EXPORT_SYMBOL(drm_plane_from_index);
750
751 /**
752 * drm_plane_force_disable - Forcibly disable a plane
753 * @plane: plane to disable
754 *
755 * Forces the plane to be disabled.
756 *
757 * Used when the plane's current framebuffer is destroyed,
758 * and when restoring fbdev mode.
759 */
760 void drm_plane_force_disable(struct drm_plane *plane)
761 {
762 int ret;
763
764 if (!plane->fb)
765 return;
766
767 plane->old_fb = plane->fb;
768 ret = plane->funcs->disable_plane(plane);
769 if (ret) {
770 DRM_ERROR("failed to disable plane with busy fb\n");
771 plane->old_fb = NULL;
772 return;
773 }
774 /* disconnect the plane from the fb and crtc: */
775 drm_framebuffer_unreference(plane->old_fb);
776 plane->old_fb = NULL;
777 plane->fb = NULL;
778 plane->crtc = NULL;
779 }
780 EXPORT_SYMBOL(drm_plane_force_disable);
781
782 int drm_modeset_register_all(struct drm_device *dev)
783 {
784 int ret;
785
786 ret = drm_plane_register_all(dev);
787 if (ret)
788 goto err_plane;
789
790 ret = drm_crtc_register_all(dev);
791 if (ret)
792 goto err_crtc;
793
794 ret = drm_encoder_register_all(dev);
795 if (ret)
796 goto err_encoder;
797
798 ret = drm_connector_register_all(dev);
799 if (ret)
800 goto err_connector;
801
802 return 0;
803
804 err_connector:
805 drm_encoder_unregister_all(dev);
806 err_encoder:
807 drm_crtc_unregister_all(dev);
808 err_crtc:
809 drm_plane_unregister_all(dev);
810 err_plane:
811 return ret;
812 }
813
814 void drm_modeset_unregister_all(struct drm_device *dev)
815 {
816 drm_connector_unregister_all(dev);
817 drm_encoder_unregister_all(dev);
818 drm_crtc_unregister_all(dev);
819 drm_plane_unregister_all(dev);
820 }
821
822 static int drm_mode_create_standard_properties(struct drm_device *dev)
823 {
824 struct drm_property *prop;
825 int ret;
826
827 ret = drm_connector_create_standard_properties(dev);
828 if (ret)
829 return ret;
830
831 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
832 "type", drm_plane_type_enum_list,
833 ARRAY_SIZE(drm_plane_type_enum_list));
834 if (!prop)
835 return -ENOMEM;
836 dev->mode_config.plane_type_property = prop;
837
838 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
839 "SRC_X", 0, UINT_MAX);
840 if (!prop)
841 return -ENOMEM;
842 dev->mode_config.prop_src_x = prop;
843
844 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
845 "SRC_Y", 0, UINT_MAX);
846 if (!prop)
847 return -ENOMEM;
848 dev->mode_config.prop_src_y = prop;
849
850 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
851 "SRC_W", 0, UINT_MAX);
852 if (!prop)
853 return -ENOMEM;
854 dev->mode_config.prop_src_w = prop;
855
856 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
857 "SRC_H", 0, UINT_MAX);
858 if (!prop)
859 return -ENOMEM;
860 dev->mode_config.prop_src_h = prop;
861
862 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
863 "CRTC_X", INT_MIN, INT_MAX);
864 if (!prop)
865 return -ENOMEM;
866 dev->mode_config.prop_crtc_x = prop;
867
868 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
869 "CRTC_Y", INT_MIN, INT_MAX);
870 if (!prop)
871 return -ENOMEM;
872 dev->mode_config.prop_crtc_y = prop;
873
874 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
875 "CRTC_W", 0, INT_MAX);
876 if (!prop)
877 return -ENOMEM;
878 dev->mode_config.prop_crtc_w = prop;
879
880 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
881 "CRTC_H", 0, INT_MAX);
882 if (!prop)
883 return -ENOMEM;
884 dev->mode_config.prop_crtc_h = prop;
885
886 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
887 "FB_ID", DRM_MODE_OBJECT_FB);
888 if (!prop)
889 return -ENOMEM;
890 dev->mode_config.prop_fb_id = prop;
891
892 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
893 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
894 if (!prop)
895 return -ENOMEM;
896 dev->mode_config.prop_crtc_id = prop;
897
898 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
899 "ACTIVE");
900 if (!prop)
901 return -ENOMEM;
902 dev->mode_config.prop_active = prop;
903
904 prop = drm_property_create(dev,
905 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
906 "MODE_ID", 0);
907 if (!prop)
908 return -ENOMEM;
909 dev->mode_config.prop_mode_id = prop;
910
911 prop = drm_property_create(dev,
912 DRM_MODE_PROP_BLOB,
913 "DEGAMMA_LUT", 0);
914 if (!prop)
915 return -ENOMEM;
916 dev->mode_config.degamma_lut_property = prop;
917
918 prop = drm_property_create_range(dev,
919 DRM_MODE_PROP_IMMUTABLE,
920 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
921 if (!prop)
922 return -ENOMEM;
923 dev->mode_config.degamma_lut_size_property = prop;
924
925 prop = drm_property_create(dev,
926 DRM_MODE_PROP_BLOB,
927 "CTM", 0);
928 if (!prop)
929 return -ENOMEM;
930 dev->mode_config.ctm_property = prop;
931
932 prop = drm_property_create(dev,
933 DRM_MODE_PROP_BLOB,
934 "GAMMA_LUT", 0);
935 if (!prop)
936 return -ENOMEM;
937 dev->mode_config.gamma_lut_property = prop;
938
939 prop = drm_property_create_range(dev,
940 DRM_MODE_PROP_IMMUTABLE,
941 "GAMMA_LUT_SIZE", 0, UINT_MAX);
942 if (!prop)
943 return -ENOMEM;
944 dev->mode_config.gamma_lut_size_property = prop;
945
946 return 0;
947 }
948
949 /**
950 * drm_mode_getresources - get graphics configuration
951 * @dev: drm device for the ioctl
952 * @data: data pointer for the ioctl
953 * @file_priv: drm file for the ioctl call
954 *
955 * Construct a set of configuration description structures and return
956 * them to the user, including CRTC, connector and framebuffer configuration.
957 *
958 * Called by the user via ioctl.
959 *
960 * Returns:
961 * Zero on success, negative errno on failure.
962 */
963 int drm_mode_getresources(struct drm_device *dev, void *data,
964 struct drm_file *file_priv)
965 {
966 struct drm_mode_card_res *card_res = data;
967 struct list_head *lh;
968 struct drm_framebuffer *fb;
969 struct drm_connector *connector;
970 struct drm_crtc *crtc;
971 struct drm_encoder *encoder;
972 int ret = 0;
973 int connector_count = 0;
974 int crtc_count = 0;
975 int fb_count = 0;
976 int encoder_count = 0;
977 int copied = 0;
978 uint32_t __user *fb_id;
979 uint32_t __user *crtc_id;
980 uint32_t __user *connector_id;
981 uint32_t __user *encoder_id;
982
983 if (!drm_core_check_feature(dev, DRIVER_MODESET))
984 return -EINVAL;
985
986
987 mutex_lock(&file_priv->fbs_lock);
988 /*
989 * For the non-control nodes we need to limit the list of resources
990 * by IDs in the group list for this node
991 */
992 list_for_each(lh, &file_priv->fbs)
993 fb_count++;
994
995 /* handle this in 4 parts */
996 /* FBs */
997 if (card_res->count_fbs >= fb_count) {
998 copied = 0;
999 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1000 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1001 if (put_user(fb->base.id, fb_id + copied)) {
1002 mutex_unlock(&file_priv->fbs_lock);
1003 return -EFAULT;
1004 }
1005 copied++;
1006 }
1007 }
1008 card_res->count_fbs = fb_count;
1009 mutex_unlock(&file_priv->fbs_lock);
1010
1011 /* mode_config.mutex protects the connector list against e.g. DP MST
1012 * connector hot-adding. CRTC/Plane lists are invariant. */
1013 mutex_lock(&dev->mode_config.mutex);
1014 drm_for_each_crtc(crtc, dev)
1015 crtc_count++;
1016
1017 drm_for_each_connector(connector, dev)
1018 connector_count++;
1019
1020 drm_for_each_encoder(encoder, dev)
1021 encoder_count++;
1022
1023 card_res->max_height = dev->mode_config.max_height;
1024 card_res->min_height = dev->mode_config.min_height;
1025 card_res->max_width = dev->mode_config.max_width;
1026 card_res->min_width = dev->mode_config.min_width;
1027
1028 /* CRTCs */
1029 if (card_res->count_crtcs >= crtc_count) {
1030 copied = 0;
1031 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1032 drm_for_each_crtc(crtc, dev) {
1033 if (put_user(crtc->base.id, crtc_id + copied)) {
1034 ret = -EFAULT;
1035 goto out;
1036 }
1037 copied++;
1038 }
1039 }
1040 card_res->count_crtcs = crtc_count;
1041
1042 /* Encoders */
1043 if (card_res->count_encoders >= encoder_count) {
1044 copied = 0;
1045 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1046 drm_for_each_encoder(encoder, dev) {
1047 if (put_user(encoder->base.id, encoder_id +
1048 copied)) {
1049 ret = -EFAULT;
1050 goto out;
1051 }
1052 copied++;
1053 }
1054 }
1055 card_res->count_encoders = encoder_count;
1056
1057 /* Connectors */
1058 if (card_res->count_connectors >= connector_count) {
1059 copied = 0;
1060 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1061 drm_for_each_connector(connector, dev) {
1062 if (put_user(connector->base.id,
1063 connector_id + copied)) {
1064 ret = -EFAULT;
1065 goto out;
1066 }
1067 copied++;
1068 }
1069 }
1070 card_res->count_connectors = connector_count;
1071
1072 out:
1073 mutex_unlock(&dev->mode_config.mutex);
1074 return ret;
1075 }
1076
1077 /**
1078 * drm_mode_getcrtc - get CRTC configuration
1079 * @dev: drm device for the ioctl
1080 * @data: data pointer for the ioctl
1081 * @file_priv: drm file for the ioctl call
1082 *
1083 * Construct a CRTC configuration structure to return to the user.
1084 *
1085 * Called by the user via ioctl.
1086 *
1087 * Returns:
1088 * Zero on success, negative errno on failure.
1089 */
1090 int drm_mode_getcrtc(struct drm_device *dev,
1091 void *data, struct drm_file *file_priv)
1092 {
1093 struct drm_mode_crtc *crtc_resp = data;
1094 struct drm_crtc *crtc;
1095
1096 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1097 return -EINVAL;
1098
1099 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1100 if (!crtc)
1101 return -ENOENT;
1102
1103 drm_modeset_lock_crtc(crtc, crtc->primary);
1104 crtc_resp->gamma_size = crtc->gamma_size;
1105 if (crtc->primary->fb)
1106 crtc_resp->fb_id = crtc->primary->fb->base.id;
1107 else
1108 crtc_resp->fb_id = 0;
1109
1110 if (crtc->state) {
1111 crtc_resp->x = crtc->primary->state->src_x >> 16;
1112 crtc_resp->y = crtc->primary->state->src_y >> 16;
1113 if (crtc->state->enable) {
1114 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
1115 crtc_resp->mode_valid = 1;
1116
1117 } else {
1118 crtc_resp->mode_valid = 0;
1119 }
1120 } else {
1121 crtc_resp->x = crtc->x;
1122 crtc_resp->y = crtc->y;
1123 if (crtc->enabled) {
1124 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1125 crtc_resp->mode_valid = 1;
1126
1127 } else {
1128 crtc_resp->mode_valid = 0;
1129 }
1130 }
1131 drm_modeset_unlock_crtc(crtc);
1132
1133 return 0;
1134 }
1135
1136 /* helper for getconnector and getproperties ioctls */
1137 int drm_mode_object_get_properties(struct drm_mode_object *obj, bool atomic,
1138 uint32_t __user *prop_ptr,
1139 uint64_t __user *prop_values,
1140 uint32_t *arg_count_props)
1141 {
1142 int props_count;
1143 int i, ret, copied;
1144
1145 props_count = obj->properties->count;
1146 if (!atomic)
1147 props_count -= obj->properties->atomic_count;
1148
1149 if ((*arg_count_props >= props_count) && props_count) {
1150 for (i = 0, copied = 0; copied < props_count; i++) {
1151 struct drm_property *prop = obj->properties->properties[i];
1152 uint64_t val;
1153
1154 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
1155 continue;
1156
1157 ret = drm_object_property_get_value(obj, prop, &val);
1158 if (ret)
1159 return ret;
1160
1161 if (put_user(prop->base.id, prop_ptr + copied))
1162 return -EFAULT;
1163
1164 if (put_user(val, prop_values + copied))
1165 return -EFAULT;
1166
1167 copied++;
1168 }
1169 }
1170 *arg_count_props = props_count;
1171
1172 return 0;
1173 }
1174
1175 static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
1176 {
1177 struct drm_connector *connector;
1178 struct drm_device *dev = encoder->dev;
1179 bool uses_atomic = false;
1180
1181 /* For atomic drivers only state objects are synchronously updated and
1182 * protected by modeset locks, so check those first. */
1183 drm_for_each_connector(connector, dev) {
1184 if (!connector->state)
1185 continue;
1186
1187 uses_atomic = true;
1188
1189 if (connector->state->best_encoder != encoder)
1190 continue;
1191
1192 return connector->state->crtc;
1193 }
1194
1195 /* Don't return stale data (e.g. pending async disable). */
1196 if (uses_atomic)
1197 return NULL;
1198
1199 return encoder->crtc;
1200 }
1201
1202 /**
1203 * drm_mode_getencoder - get encoder configuration
1204 * @dev: drm device for the ioctl
1205 * @data: data pointer for the ioctl
1206 * @file_priv: drm file for the ioctl call
1207 *
1208 * Construct a encoder configuration structure to return to the user.
1209 *
1210 * Called by the user via ioctl.
1211 *
1212 * Returns:
1213 * Zero on success, negative errno on failure.
1214 */
1215 int drm_mode_getencoder(struct drm_device *dev, void *data,
1216 struct drm_file *file_priv)
1217 {
1218 struct drm_mode_get_encoder *enc_resp = data;
1219 struct drm_encoder *encoder;
1220 struct drm_crtc *crtc;
1221
1222 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1223 return -EINVAL;
1224
1225 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
1226 if (!encoder)
1227 return -ENOENT;
1228
1229 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1230 crtc = drm_encoder_get_crtc(encoder);
1231 if (crtc)
1232 enc_resp->crtc_id = crtc->base.id;
1233 else
1234 enc_resp->crtc_id = 0;
1235 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1236
1237 enc_resp->encoder_type = encoder->encoder_type;
1238 enc_resp->encoder_id = encoder->base.id;
1239 enc_resp->possible_crtcs = encoder->possible_crtcs;
1240 enc_resp->possible_clones = encoder->possible_clones;
1241
1242 return 0;
1243 }
1244
1245 /**
1246 * drm_mode_getplane_res - enumerate all plane resources
1247 * @dev: DRM device
1248 * @data: ioctl data
1249 * @file_priv: DRM file info
1250 *
1251 * Construct a list of plane ids to return to the user.
1252 *
1253 * Called by the user via ioctl.
1254 *
1255 * Returns:
1256 * Zero on success, negative errno on failure.
1257 */
1258 int drm_mode_getplane_res(struct drm_device *dev, void *data,
1259 struct drm_file *file_priv)
1260 {
1261 struct drm_mode_get_plane_res *plane_resp = data;
1262 struct drm_mode_config *config;
1263 struct drm_plane *plane;
1264 uint32_t __user *plane_ptr;
1265 int copied = 0;
1266 unsigned num_planes;
1267
1268 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1269 return -EINVAL;
1270
1271 config = &dev->mode_config;
1272
1273 if (file_priv->universal_planes)
1274 num_planes = config->num_total_plane;
1275 else
1276 num_planes = config->num_overlay_plane;
1277
1278 /*
1279 * This ioctl is called twice, once to determine how much space is
1280 * needed, and the 2nd time to fill it.
1281 */
1282 if (num_planes &&
1283 (plane_resp->count_planes >= num_planes)) {
1284 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
1285
1286 /* Plane lists are invariant, no locking needed. */
1287 drm_for_each_plane(plane, dev) {
1288 /*
1289 * Unless userspace set the 'universal planes'
1290 * capability bit, only advertise overlays.
1291 */
1292 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
1293 !file_priv->universal_planes)
1294 continue;
1295
1296 if (put_user(plane->base.id, plane_ptr + copied))
1297 return -EFAULT;
1298 copied++;
1299 }
1300 }
1301 plane_resp->count_planes = num_planes;
1302
1303 return 0;
1304 }
1305
1306 /**
1307 * drm_mode_getplane - get plane configuration
1308 * @dev: DRM device
1309 * @data: ioctl data
1310 * @file_priv: DRM file info
1311 *
1312 * Construct a plane configuration structure to return to the user.
1313 *
1314 * Called by the user via ioctl.
1315 *
1316 * Returns:
1317 * Zero on success, negative errno on failure.
1318 */
1319 int drm_mode_getplane(struct drm_device *dev, void *data,
1320 struct drm_file *file_priv)
1321 {
1322 struct drm_mode_get_plane *plane_resp = data;
1323 struct drm_plane *plane;
1324 uint32_t __user *format_ptr;
1325
1326 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1327 return -EINVAL;
1328
1329 plane = drm_plane_find(dev, plane_resp->plane_id);
1330 if (!plane)
1331 return -ENOENT;
1332
1333 drm_modeset_lock(&plane->mutex, NULL);
1334 if (plane->crtc)
1335 plane_resp->crtc_id = plane->crtc->base.id;
1336 else
1337 plane_resp->crtc_id = 0;
1338
1339 if (plane->fb)
1340 plane_resp->fb_id = plane->fb->base.id;
1341 else
1342 plane_resp->fb_id = 0;
1343 drm_modeset_unlock(&plane->mutex);
1344
1345 plane_resp->plane_id = plane->base.id;
1346 plane_resp->possible_crtcs = plane->possible_crtcs;
1347 plane_resp->gamma_size = 0;
1348
1349 /*
1350 * This ioctl is called twice, once to determine how much space is
1351 * needed, and the 2nd time to fill it.
1352 */
1353 if (plane->format_count &&
1354 (plane_resp->count_format_types >= plane->format_count)) {
1355 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
1356 if (copy_to_user(format_ptr,
1357 plane->format_types,
1358 sizeof(uint32_t) * plane->format_count)) {
1359 return -EFAULT;
1360 }
1361 }
1362 plane_resp->count_format_types = plane->format_count;
1363
1364 return 0;
1365 }
1366
1367 /**
1368 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
1369 * @plane: plane to check for format support
1370 * @format: the pixel format
1371 *
1372 * Returns:
1373 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
1374 * otherwise.
1375 */
1376 int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
1377 {
1378 unsigned int i;
1379
1380 for (i = 0; i < plane->format_count; i++) {
1381 if (format == plane->format_types[i])
1382 return 0;
1383 }
1384
1385 return -EINVAL;
1386 }
1387
1388 static int check_src_coords(uint32_t src_x, uint32_t src_y,
1389 uint32_t src_w, uint32_t src_h,
1390 const struct drm_framebuffer *fb)
1391 {
1392 unsigned int fb_width, fb_height;
1393
1394 fb_width = fb->width << 16;
1395 fb_height = fb->height << 16;
1396
1397 /* Make sure source coordinates are inside the fb. */
1398 if (src_w > fb_width ||
1399 src_x > fb_width - src_w ||
1400 src_h > fb_height ||
1401 src_y > fb_height - src_h) {
1402 DRM_DEBUG_KMS("Invalid source coordinates "
1403 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1404 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
1405 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
1406 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
1407 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
1408 return -ENOSPC;
1409 }
1410
1411 return 0;
1412 }
1413
1414 /*
1415 * setplane_internal - setplane handler for internal callers
1416 *
1417 * Note that we assume an extra reference has already been taken on fb. If the
1418 * update fails, this reference will be dropped before return; if it succeeds,
1419 * the previous framebuffer (if any) will be unreferenced instead.
1420 *
1421 * src_{x,y,w,h} are provided in 16.16 fixed point format
1422 */
1423 static int __setplane_internal(struct drm_plane *plane,
1424 struct drm_crtc *crtc,
1425 struct drm_framebuffer *fb,
1426 int32_t crtc_x, int32_t crtc_y,
1427 uint32_t crtc_w, uint32_t crtc_h,
1428 /* src_{x,y,w,h} values are 16.16 fixed point */
1429 uint32_t src_x, uint32_t src_y,
1430 uint32_t src_w, uint32_t src_h)
1431 {
1432 int ret = 0;
1433
1434 /* No fb means shut it down */
1435 if (!fb) {
1436 plane->old_fb = plane->fb;
1437 ret = plane->funcs->disable_plane(plane);
1438 if (!ret) {
1439 plane->crtc = NULL;
1440 plane->fb = NULL;
1441 } else {
1442 plane->old_fb = NULL;
1443 }
1444 goto out;
1445 }
1446
1447 /* Check whether this plane is usable on this CRTC */
1448 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
1449 DRM_DEBUG_KMS("Invalid crtc for plane\n");
1450 ret = -EINVAL;
1451 goto out;
1452 }
1453
1454 /* Check whether this plane supports the fb pixel format. */
1455 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
1456 if (ret) {
1457 char *format_name = drm_get_format_name(fb->pixel_format);
1458 DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
1459 kfree(format_name);
1460 goto out;
1461 }
1462
1463 /* Give drivers some help against integer overflows */
1464 if (crtc_w > INT_MAX ||
1465 crtc_x > INT_MAX - (int32_t) crtc_w ||
1466 crtc_h > INT_MAX ||
1467 crtc_y > INT_MAX - (int32_t) crtc_h) {
1468 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1469 crtc_w, crtc_h, crtc_x, crtc_y);
1470 ret = -ERANGE;
1471 goto out;
1472 }
1473
1474 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
1475 if (ret)
1476 goto out;
1477
1478 plane->old_fb = plane->fb;
1479 ret = plane->funcs->update_plane(plane, crtc, fb,
1480 crtc_x, crtc_y, crtc_w, crtc_h,
1481 src_x, src_y, src_w, src_h);
1482 if (!ret) {
1483 plane->crtc = crtc;
1484 plane->fb = fb;
1485 fb = NULL;
1486 } else {
1487 plane->old_fb = NULL;
1488 }
1489
1490 out:
1491 if (fb)
1492 drm_framebuffer_unreference(fb);
1493 if (plane->old_fb)
1494 drm_framebuffer_unreference(plane->old_fb);
1495 plane->old_fb = NULL;
1496
1497 return ret;
1498 }
1499
1500 static int setplane_internal(struct drm_plane *plane,
1501 struct drm_crtc *crtc,
1502 struct drm_framebuffer *fb,
1503 int32_t crtc_x, int32_t crtc_y,
1504 uint32_t crtc_w, uint32_t crtc_h,
1505 /* src_{x,y,w,h} values are 16.16 fixed point */
1506 uint32_t src_x, uint32_t src_y,
1507 uint32_t src_w, uint32_t src_h)
1508 {
1509 int ret;
1510
1511 drm_modeset_lock_all(plane->dev);
1512 ret = __setplane_internal(plane, crtc, fb,
1513 crtc_x, crtc_y, crtc_w, crtc_h,
1514 src_x, src_y, src_w, src_h);
1515 drm_modeset_unlock_all(plane->dev);
1516
1517 return ret;
1518 }
1519
1520 /**
1521 * drm_mode_setplane - configure a plane's configuration
1522 * @dev: DRM device
1523 * @data: ioctl data*
1524 * @file_priv: DRM file info
1525 *
1526 * Set plane configuration, including placement, fb, scaling, and other factors.
1527 * Or pass a NULL fb to disable (planes may be disabled without providing a
1528 * valid crtc).
1529 *
1530 * Returns:
1531 * Zero on success, negative errno on failure.
1532 */
1533 int drm_mode_setplane(struct drm_device *dev, void *data,
1534 struct drm_file *file_priv)
1535 {
1536 struct drm_mode_set_plane *plane_req = data;
1537 struct drm_plane *plane;
1538 struct drm_crtc *crtc = NULL;
1539 struct drm_framebuffer *fb = NULL;
1540
1541 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1542 return -EINVAL;
1543
1544 /*
1545 * First, find the plane, crtc, and fb objects. If not available,
1546 * we don't bother to call the driver.
1547 */
1548 plane = drm_plane_find(dev, plane_req->plane_id);
1549 if (!plane) {
1550 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1551 plane_req->plane_id);
1552 return -ENOENT;
1553 }
1554
1555 if (plane_req->fb_id) {
1556 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1557 if (!fb) {
1558 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1559 plane_req->fb_id);
1560 return -ENOENT;
1561 }
1562
1563 crtc = drm_crtc_find(dev, plane_req->crtc_id);
1564 if (!crtc) {
1565 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1566 plane_req->crtc_id);
1567 return -ENOENT;
1568 }
1569 }
1570
1571 /*
1572 * setplane_internal will take care of deref'ing either the old or new
1573 * framebuffer depending on success.
1574 */
1575 return setplane_internal(plane, crtc, fb,
1576 plane_req->crtc_x, plane_req->crtc_y,
1577 plane_req->crtc_w, plane_req->crtc_h,
1578 plane_req->src_x, plane_req->src_y,
1579 plane_req->src_w, plane_req->src_h);
1580 }
1581
1582 /**
1583 * drm_mode_set_config_internal - helper to call ->set_config
1584 * @set: modeset config to set
1585 *
1586 * This is a little helper to wrap internal calls to the ->set_config driver
1587 * interface. The only thing it adds is correct refcounting dance.
1588 *
1589 * Returns:
1590 * Zero on success, negative errno on failure.
1591 */
1592 int drm_mode_set_config_internal(struct drm_mode_set *set)
1593 {
1594 struct drm_crtc *crtc = set->crtc;
1595 struct drm_framebuffer *fb;
1596 struct drm_crtc *tmp;
1597 int ret;
1598
1599 /*
1600 * NOTE: ->set_config can also disable other crtcs (if we steal all
1601 * connectors from it), hence we need to refcount the fbs across all
1602 * crtcs. Atomic modeset will have saner semantics ...
1603 */
1604 drm_for_each_crtc(tmp, crtc->dev)
1605 tmp->primary->old_fb = tmp->primary->fb;
1606
1607 fb = set->fb;
1608
1609 ret = crtc->funcs->set_config(set);
1610 if (ret == 0) {
1611 crtc->primary->crtc = crtc;
1612 crtc->primary->fb = fb;
1613 }
1614
1615 drm_for_each_crtc(tmp, crtc->dev) {
1616 if (tmp->primary->fb)
1617 drm_framebuffer_reference(tmp->primary->fb);
1618 if (tmp->primary->old_fb)
1619 drm_framebuffer_unreference(tmp->primary->old_fb);
1620 tmp->primary->old_fb = NULL;
1621 }
1622
1623 return ret;
1624 }
1625 EXPORT_SYMBOL(drm_mode_set_config_internal);
1626
1627 /**
1628 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
1629 * @mode: mode to query
1630 * @hdisplay: hdisplay value to fill in
1631 * @vdisplay: vdisplay value to fill in
1632 *
1633 * The vdisplay value will be doubled if the specified mode is a stereo mode of
1634 * the appropriate layout.
1635 */
1636 void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
1637 int *hdisplay, int *vdisplay)
1638 {
1639 struct drm_display_mode adjusted;
1640
1641 drm_mode_copy(&adjusted, mode);
1642 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
1643 *hdisplay = adjusted.crtc_hdisplay;
1644 *vdisplay = adjusted.crtc_vdisplay;
1645 }
1646 EXPORT_SYMBOL(drm_crtc_get_hv_timing);
1647
1648 /**
1649 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
1650 * CRTC viewport
1651 * @crtc: CRTC that framebuffer will be displayed on
1652 * @x: x panning
1653 * @y: y panning
1654 * @mode: mode that framebuffer will be displayed under
1655 * @fb: framebuffer to check size of
1656 */
1657 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
1658 int x, int y,
1659 const struct drm_display_mode *mode,
1660 const struct drm_framebuffer *fb)
1661
1662 {
1663 int hdisplay, vdisplay;
1664
1665 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
1666
1667 if (crtc->state &&
1668 crtc->primary->state->rotation & (DRM_ROTATE_90 |
1669 DRM_ROTATE_270))
1670 swap(hdisplay, vdisplay);
1671
1672 return check_src_coords(x << 16, y << 16,
1673 hdisplay << 16, vdisplay << 16, fb);
1674 }
1675 EXPORT_SYMBOL(drm_crtc_check_viewport);
1676
1677 /**
1678 * drm_mode_setcrtc - set CRTC configuration
1679 * @dev: drm device for the ioctl
1680 * @data: data pointer for the ioctl
1681 * @file_priv: drm file for the ioctl call
1682 *
1683 * Build a new CRTC configuration based on user request.
1684 *
1685 * Called by the user via ioctl.
1686 *
1687 * Returns:
1688 * Zero on success, negative errno on failure.
1689 */
1690 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1691 struct drm_file *file_priv)
1692 {
1693 struct drm_mode_config *config = &dev->mode_config;
1694 struct drm_mode_crtc *crtc_req = data;
1695 struct drm_crtc *crtc;
1696 struct drm_connector **connector_set = NULL, *connector;
1697 struct drm_framebuffer *fb = NULL;
1698 struct drm_display_mode *mode = NULL;
1699 struct drm_mode_set set;
1700 uint32_t __user *set_connectors_ptr;
1701 int ret;
1702 int i;
1703
1704 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1705 return -EINVAL;
1706
1707 /*
1708 * Universal plane src offsets are only 16.16, prevent havoc for
1709 * drivers using universal plane code internally.
1710 */
1711 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
1712 return -ERANGE;
1713
1714 drm_modeset_lock_all(dev);
1715 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
1716 if (!crtc) {
1717 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1718 ret = -ENOENT;
1719 goto out;
1720 }
1721 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
1722
1723 if (crtc_req->mode_valid) {
1724 /* If we have a mode we need a framebuffer. */
1725 /* If we pass -1, set the mode with the currently bound fb */
1726 if (crtc_req->fb_id == -1) {
1727 if (!crtc->primary->fb) {
1728 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1729 ret = -EINVAL;
1730 goto out;
1731 }
1732 fb = crtc->primary->fb;
1733 /* Make refcounting symmetric with the lookup path. */
1734 drm_framebuffer_reference(fb);
1735 } else {
1736 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
1737 if (!fb) {
1738 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1739 crtc_req->fb_id);
1740 ret = -ENOENT;
1741 goto out;
1742 }
1743 }
1744
1745 mode = drm_mode_create(dev);
1746 if (!mode) {
1747 ret = -ENOMEM;
1748 goto out;
1749 }
1750
1751 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
1752 if (ret) {
1753 DRM_DEBUG_KMS("Invalid mode\n");
1754 goto out;
1755 }
1756
1757 /*
1758 * Check whether the primary plane supports the fb pixel format.
1759 * Drivers not implementing the universal planes API use a
1760 * default formats list provided by the DRM core which doesn't
1761 * match real hardware capabilities. Skip the check in that
1762 * case.
1763 */
1764 if (!crtc->primary->format_default) {
1765 ret = drm_plane_check_pixel_format(crtc->primary,
1766 fb->pixel_format);
1767 if (ret) {
1768 char *format_name = drm_get_format_name(fb->pixel_format);
1769 DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
1770 kfree(format_name);
1771 goto out;
1772 }
1773 }
1774
1775 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
1776 mode, fb);
1777 if (ret)
1778 goto out;
1779
1780 }
1781
1782 if (crtc_req->count_connectors == 0 && mode) {
1783 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1784 ret = -EINVAL;
1785 goto out;
1786 }
1787
1788 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
1789 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1790 crtc_req->count_connectors);
1791 ret = -EINVAL;
1792 goto out;
1793 }
1794
1795 if (crtc_req->count_connectors > 0) {
1796 u32 out_id;
1797
1798 /* Avoid unbounded kernel memory allocation */
1799 if (crtc_req->count_connectors > config->num_connector) {
1800 ret = -EINVAL;
1801 goto out;
1802 }
1803
1804 connector_set = kmalloc_array(crtc_req->count_connectors,
1805 sizeof(struct drm_connector *),
1806 GFP_KERNEL);
1807 if (!connector_set) {
1808 ret = -ENOMEM;
1809 goto out;
1810 }
1811
1812 for (i = 0; i < crtc_req->count_connectors; i++) {
1813 connector_set[i] = NULL;
1814 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
1815 if (get_user(out_id, &set_connectors_ptr[i])) {
1816 ret = -EFAULT;
1817 goto out;
1818 }
1819
1820 connector = drm_connector_lookup(dev, out_id);
1821 if (!connector) {
1822 DRM_DEBUG_KMS("Connector id %d unknown\n",
1823 out_id);
1824 ret = -ENOENT;
1825 goto out;
1826 }
1827 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1828 connector->base.id,
1829 connector->name);
1830
1831 connector_set[i] = connector;
1832 }
1833 }
1834
1835 set.crtc = crtc;
1836 set.x = crtc_req->x;
1837 set.y = crtc_req->y;
1838 set.mode = mode;
1839 set.connectors = connector_set;
1840 set.num_connectors = crtc_req->count_connectors;
1841 set.fb = fb;
1842 ret = drm_mode_set_config_internal(&set);
1843
1844 out:
1845 if (fb)
1846 drm_framebuffer_unreference(fb);
1847
1848 if (connector_set) {
1849 for (i = 0; i < crtc_req->count_connectors; i++) {
1850 if (connector_set[i])
1851 drm_connector_unreference(connector_set[i]);
1852 }
1853 }
1854 kfree(connector_set);
1855 drm_mode_destroy(dev, mode);
1856 drm_modeset_unlock_all(dev);
1857 return ret;
1858 }
1859
1860 /**
1861 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
1862 * universal plane handler call
1863 * @crtc: crtc to update cursor for
1864 * @req: data pointer for the ioctl
1865 * @file_priv: drm file for the ioctl call
1866 *
1867 * Legacy cursor ioctl's work directly with driver buffer handles. To
1868 * translate legacy ioctl calls into universal plane handler calls, we need to
1869 * wrap the native buffer handle in a drm_framebuffer.
1870 *
1871 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
1872 * buffer with a pitch of 4*width; the universal plane interface should be used
1873 * directly in cases where the hardware can support other buffer settings and
1874 * userspace wants to make use of these capabilities.
1875 *
1876 * Returns:
1877 * Zero on success, negative errno on failure.
1878 */
1879 static int drm_mode_cursor_universal(struct drm_crtc *crtc,
1880 struct drm_mode_cursor2 *req,
1881 struct drm_file *file_priv)
1882 {
1883 struct drm_device *dev = crtc->dev;
1884 struct drm_framebuffer *fb = NULL;
1885 struct drm_mode_fb_cmd2 fbreq = {
1886 .width = req->width,
1887 .height = req->height,
1888 .pixel_format = DRM_FORMAT_ARGB8888,
1889 .pitches = { req->width * 4 },
1890 .handles = { req->handle },
1891 };
1892 int32_t crtc_x, crtc_y;
1893 uint32_t crtc_w = 0, crtc_h = 0;
1894 uint32_t src_w = 0, src_h = 0;
1895 int ret = 0;
1896
1897 BUG_ON(!crtc->cursor);
1898 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
1899
1900 /*
1901 * Obtain fb we'll be using (either new or existing) and take an extra
1902 * reference to it if fb != null. setplane will take care of dropping
1903 * the reference if the plane update fails.
1904 */
1905 if (req->flags & DRM_MODE_CURSOR_BO) {
1906 if (req->handle) {
1907 fb = drm_internal_framebuffer_create(dev, &fbreq, file_priv);
1908 if (IS_ERR(fb)) {
1909 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
1910 return PTR_ERR(fb);
1911 }
1912 fb->hot_x = req->hot_x;
1913 fb->hot_y = req->hot_y;
1914 } else {
1915 fb = NULL;
1916 }
1917 } else {
1918 fb = crtc->cursor->fb;
1919 if (fb)
1920 drm_framebuffer_reference(fb);
1921 }
1922
1923 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1924 crtc_x = req->x;
1925 crtc_y = req->y;
1926 } else {
1927 crtc_x = crtc->cursor_x;
1928 crtc_y = crtc->cursor_y;
1929 }
1930
1931 if (fb) {
1932 crtc_w = fb->width;
1933 crtc_h = fb->height;
1934 src_w = fb->width << 16;
1935 src_h = fb->height << 16;
1936 }
1937
1938 /*
1939 * setplane_internal will take care of deref'ing either the old or new
1940 * framebuffer depending on success.
1941 */
1942 ret = __setplane_internal(crtc->cursor, crtc, fb,
1943 crtc_x, crtc_y, crtc_w, crtc_h,
1944 0, 0, src_w, src_h);
1945
1946 /* Update successful; save new cursor position, if necessary */
1947 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
1948 crtc->cursor_x = req->x;
1949 crtc->cursor_y = req->y;
1950 }
1951
1952 return ret;
1953 }
1954
1955 static int drm_mode_cursor_common(struct drm_device *dev,
1956 struct drm_mode_cursor2 *req,
1957 struct drm_file *file_priv)
1958 {
1959 struct drm_crtc *crtc;
1960 int ret = 0;
1961
1962 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1963 return -EINVAL;
1964
1965 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
1966 return -EINVAL;
1967
1968 crtc = drm_crtc_find(dev, req->crtc_id);
1969 if (!crtc) {
1970 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
1971 return -ENOENT;
1972 }
1973
1974 /*
1975 * If this crtc has a universal cursor plane, call that plane's update
1976 * handler rather than using legacy cursor handlers.
1977 */
1978 drm_modeset_lock_crtc(crtc, crtc->cursor);
1979 if (crtc->cursor) {
1980 ret = drm_mode_cursor_universal(crtc, req, file_priv);
1981 goto out;
1982 }
1983
1984 if (req->flags & DRM_MODE_CURSOR_BO) {
1985 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
1986 ret = -ENXIO;
1987 goto out;
1988 }
1989 /* Turns off the cursor if handle is 0 */
1990 if (crtc->funcs->cursor_set2)
1991 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
1992 req->width, req->height, req->hot_x, req->hot_y);
1993 else
1994 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1995 req->width, req->height);
1996 }
1997
1998 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1999 if (crtc->funcs->cursor_move) {
2000 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2001 } else {
2002 ret = -EFAULT;
2003 goto out;
2004 }
2005 }
2006 out:
2007 drm_modeset_unlock_crtc(crtc);
2008
2009 return ret;
2010
2011 }
2012
2013
2014 /**
2015 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2016 * @dev: drm device for the ioctl
2017 * @data: data pointer for the ioctl
2018 * @file_priv: drm file for the ioctl call
2019 *
2020 * Set the cursor configuration based on user request.
2021 *
2022 * Called by the user via ioctl.
2023 *
2024 * Returns:
2025 * Zero on success, negative errno on failure.
2026 */
2027 int drm_mode_cursor_ioctl(struct drm_device *dev,
2028 void *data, struct drm_file *file_priv)
2029 {
2030 struct drm_mode_cursor *req = data;
2031 struct drm_mode_cursor2 new_req;
2032
2033 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2034 new_req.hot_x = new_req.hot_y = 0;
2035
2036 return drm_mode_cursor_common(dev, &new_req, file_priv);
2037 }
2038
2039 /**
2040 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2041 * @dev: drm device for the ioctl
2042 * @data: data pointer for the ioctl
2043 * @file_priv: drm file for the ioctl call
2044 *
2045 * Set the cursor configuration based on user request. This implements the 2nd
2046 * version of the cursor ioctl, which allows userspace to additionally specify
2047 * the hotspot of the pointer.
2048 *
2049 * Called by the user via ioctl.
2050 *
2051 * Returns:
2052 * Zero on success, negative errno on failure.
2053 */
2054 int drm_mode_cursor2_ioctl(struct drm_device *dev,
2055 void *data, struct drm_file *file_priv)
2056 {
2057 struct drm_mode_cursor2 *req = data;
2058
2059 return drm_mode_cursor_common(dev, req, file_priv);
2060 }
2061
2062 /**
2063 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2064 * @bpp: bits per pixels
2065 * @depth: bit depth per pixel
2066 *
2067 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2068 * Useful in fbdev emulation code, since that deals in those values.
2069 */
2070 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2071 {
2072 uint32_t fmt;
2073
2074 switch (bpp) {
2075 case 8:
2076 fmt = DRM_FORMAT_C8;
2077 break;
2078 case 16:
2079 if (depth == 15)
2080 fmt = DRM_FORMAT_XRGB1555;
2081 else
2082 fmt = DRM_FORMAT_RGB565;
2083 break;
2084 case 24:
2085 fmt = DRM_FORMAT_RGB888;
2086 break;
2087 case 32:
2088 if (depth == 24)
2089 fmt = DRM_FORMAT_XRGB8888;
2090 else if (depth == 30)
2091 fmt = DRM_FORMAT_XRGB2101010;
2092 else
2093 fmt = DRM_FORMAT_ARGB8888;
2094 break;
2095 default:
2096 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2097 fmt = DRM_FORMAT_XRGB8888;
2098 break;
2099 }
2100
2101 return fmt;
2102 }
2103 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2104
2105 static bool drm_property_type_valid(struct drm_property *property)
2106 {
2107 if (property->flags & DRM_MODE_PROP_EXTENDED_TYPE)
2108 return !(property->flags & DRM_MODE_PROP_LEGACY_TYPE);
2109 return !!(property->flags & DRM_MODE_PROP_LEGACY_TYPE);
2110 }
2111
2112 /**
2113 * drm_property_create - create a new property type
2114 * @dev: drm device
2115 * @flags: flags specifying the property type
2116 * @name: name of the property
2117 * @num_values: number of pre-defined values
2118 *
2119 * This creates a new generic drm property which can then be attached to a drm
2120 * object with drm_object_attach_property. The returned property object must be
2121 * freed with drm_property_destroy.
2122 *
2123 * Note that the DRM core keeps a per-device list of properties and that, if
2124 * drm_mode_config_cleanup() is called, it will destroy all properties created
2125 * by the driver.
2126 *
2127 * Returns:
2128 * A pointer to the newly created property on success, NULL on failure.
2129 */
2130 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2131 const char *name, int num_values)
2132 {
2133 struct drm_property *property = NULL;
2134 int ret;
2135
2136 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2137 if (!property)
2138 return NULL;
2139
2140 property->dev = dev;
2141
2142 if (num_values) {
2143 property->values = kcalloc(num_values, sizeof(uint64_t),
2144 GFP_KERNEL);
2145 if (!property->values)
2146 goto fail;
2147 }
2148
2149 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2150 if (ret)
2151 goto fail;
2152
2153 property->flags = flags;
2154 property->num_values = num_values;
2155 INIT_LIST_HEAD(&property->enum_list);
2156
2157 if (name) {
2158 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2159 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2160 }
2161
2162 list_add_tail(&property->head, &dev->mode_config.property_list);
2163
2164 WARN_ON(!drm_property_type_valid(property));
2165
2166 return property;
2167 fail:
2168 kfree(property->values);
2169 kfree(property);
2170 return NULL;
2171 }
2172 EXPORT_SYMBOL(drm_property_create);
2173
2174 /**
2175 * drm_property_create_enum - create a new enumeration property type
2176 * @dev: drm device
2177 * @flags: flags specifying the property type
2178 * @name: name of the property
2179 * @props: enumeration lists with property values
2180 * @num_values: number of pre-defined values
2181 *
2182 * This creates a new generic drm property which can then be attached to a drm
2183 * object with drm_object_attach_property. The returned property object must be
2184 * freed with drm_property_destroy.
2185 *
2186 * Userspace is only allowed to set one of the predefined values for enumeration
2187 * properties.
2188 *
2189 * Returns:
2190 * A pointer to the newly created property on success, NULL on failure.
2191 */
2192 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2193 const char *name,
2194 const struct drm_prop_enum_list *props,
2195 int num_values)
2196 {
2197 struct drm_property *property;
2198 int i, ret;
2199
2200 flags |= DRM_MODE_PROP_ENUM;
2201
2202 property = drm_property_create(dev, flags, name, num_values);
2203 if (!property)
2204 return NULL;
2205
2206 for (i = 0; i < num_values; i++) {
2207 ret = drm_property_add_enum(property, i,
2208 props[i].type,
2209 props[i].name);
2210 if (ret) {
2211 drm_property_destroy(dev, property);
2212 return NULL;
2213 }
2214 }
2215
2216 return property;
2217 }
2218 EXPORT_SYMBOL(drm_property_create_enum);
2219
2220 /**
2221 * drm_property_create_bitmask - create a new bitmask property type
2222 * @dev: drm device
2223 * @flags: flags specifying the property type
2224 * @name: name of the property
2225 * @props: enumeration lists with property bitflags
2226 * @num_props: size of the @props array
2227 * @supported_bits: bitmask of all supported enumeration values
2228 *
2229 * This creates a new bitmask drm property which can then be attached to a drm
2230 * object with drm_object_attach_property. The returned property object must be
2231 * freed with drm_property_destroy.
2232 *
2233 * Compared to plain enumeration properties userspace is allowed to set any
2234 * or'ed together combination of the predefined property bitflag values
2235 *
2236 * Returns:
2237 * A pointer to the newly created property on success, NULL on failure.
2238 */
2239 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2240 int flags, const char *name,
2241 const struct drm_prop_enum_list *props,
2242 int num_props,
2243 uint64_t supported_bits)
2244 {
2245 struct drm_property *property;
2246 int i, ret, index = 0;
2247 int num_values = hweight64(supported_bits);
2248
2249 flags |= DRM_MODE_PROP_BITMASK;
2250
2251 property = drm_property_create(dev, flags, name, num_values);
2252 if (!property)
2253 return NULL;
2254 for (i = 0; i < num_props; i++) {
2255 if (!(supported_bits & (1ULL << props[i].type)))
2256 continue;
2257
2258 if (WARN_ON(index >= num_values)) {
2259 drm_property_destroy(dev, property);
2260 return NULL;
2261 }
2262
2263 ret = drm_property_add_enum(property, index++,
2264 props[i].type,
2265 props[i].name);
2266 if (ret) {
2267 drm_property_destroy(dev, property);
2268 return NULL;
2269 }
2270 }
2271
2272 return property;
2273 }
2274 EXPORT_SYMBOL(drm_property_create_bitmask);
2275
2276 static struct drm_property *property_create_range(struct drm_device *dev,
2277 int flags, const char *name,
2278 uint64_t min, uint64_t max)
2279 {
2280 struct drm_property *property;
2281
2282 property = drm_property_create(dev, flags, name, 2);
2283 if (!property)
2284 return NULL;
2285
2286 property->values[0] = min;
2287 property->values[1] = max;
2288
2289 return property;
2290 }
2291
2292 /**
2293 * drm_property_create_range - create a new unsigned ranged property type
2294 * @dev: drm device
2295 * @flags: flags specifying the property type
2296 * @name: name of the property
2297 * @min: minimum value of the property
2298 * @max: maximum value of the property
2299 *
2300 * This creates a new generic drm property which can then be attached to a drm
2301 * object with drm_object_attach_property. The returned property object must be
2302 * freed with drm_property_destroy.
2303 *
2304 * Userspace is allowed to set any unsigned integer value in the (min, max)
2305 * range inclusive.
2306 *
2307 * Returns:
2308 * A pointer to the newly created property on success, NULL on failure.
2309 */
2310 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2311 const char *name,
2312 uint64_t min, uint64_t max)
2313 {
2314 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
2315 name, min, max);
2316 }
2317 EXPORT_SYMBOL(drm_property_create_range);
2318
2319 /**
2320 * drm_property_create_signed_range - create a new signed ranged property type
2321 * @dev: drm device
2322 * @flags: flags specifying the property type
2323 * @name: name of the property
2324 * @min: minimum value of the property
2325 * @max: maximum value of the property
2326 *
2327 * This creates a new generic drm property which can then be attached to a drm
2328 * object with drm_object_attach_property. The returned property object must be
2329 * freed with drm_property_destroy.
2330 *
2331 * Userspace is allowed to set any signed integer value in the (min, max)
2332 * range inclusive.
2333 *
2334 * Returns:
2335 * A pointer to the newly created property on success, NULL on failure.
2336 */
2337 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
2338 int flags, const char *name,
2339 int64_t min, int64_t max)
2340 {
2341 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
2342 name, I642U64(min), I642U64(max));
2343 }
2344 EXPORT_SYMBOL(drm_property_create_signed_range);
2345
2346 /**
2347 * drm_property_create_object - create a new object property type
2348 * @dev: drm device
2349 * @flags: flags specifying the property type
2350 * @name: name of the property
2351 * @type: object type from DRM_MODE_OBJECT_* defines
2352 *
2353 * This creates a new generic drm property which can then be attached to a drm
2354 * object with drm_object_attach_property. The returned property object must be
2355 * freed with drm_property_destroy.
2356 *
2357 * Userspace is only allowed to set this to any property value of the given
2358 * @type. Only useful for atomic properties, which is enforced.
2359 *
2360 * Returns:
2361 * A pointer to the newly created property on success, NULL on failure.
2362 */
2363 struct drm_property *drm_property_create_object(struct drm_device *dev,
2364 int flags, const char *name, uint32_t type)
2365 {
2366 struct drm_property *property;
2367
2368 flags |= DRM_MODE_PROP_OBJECT;
2369
2370 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
2371 return NULL;
2372
2373 property = drm_property_create(dev, flags, name, 1);
2374 if (!property)
2375 return NULL;
2376
2377 property->values[0] = type;
2378
2379 return property;
2380 }
2381 EXPORT_SYMBOL(drm_property_create_object);
2382
2383 /**
2384 * drm_property_create_bool - create a new boolean property type
2385 * @dev: drm device
2386 * @flags: flags specifying the property type
2387 * @name: name of the property
2388 *
2389 * This creates a new generic drm property which can then be attached to a drm
2390 * object with drm_object_attach_property. The returned property object must be
2391 * freed with drm_property_destroy.
2392 *
2393 * This is implemented as a ranged property with only {0, 1} as valid values.
2394 *
2395 * Returns:
2396 * A pointer to the newly created property on success, NULL on failure.
2397 */
2398 struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
2399 const char *name)
2400 {
2401 return drm_property_create_range(dev, flags, name, 0, 1);
2402 }
2403 EXPORT_SYMBOL(drm_property_create_bool);
2404
2405 /**
2406 * drm_property_add_enum - add a possible value to an enumeration property
2407 * @property: enumeration property to change
2408 * @index: index of the new enumeration
2409 * @value: value of the new enumeration
2410 * @name: symbolic name of the new enumeration
2411 *
2412 * This functions adds enumerations to a property.
2413 *
2414 * It's use is deprecated, drivers should use one of the more specific helpers
2415 * to directly create the property with all enumerations already attached.
2416 *
2417 * Returns:
2418 * Zero on success, error code on failure.
2419 */
2420 int drm_property_add_enum(struct drm_property *property, int index,
2421 uint64_t value, const char *name)
2422 {
2423 struct drm_property_enum *prop_enum;
2424
2425 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
2426 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
2427 return -EINVAL;
2428
2429 /*
2430 * Bitmask enum properties have the additional constraint of values
2431 * from 0 to 63
2432 */
2433 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
2434 (value > 63))
2435 return -EINVAL;
2436
2437 if (!list_empty(&property->enum_list)) {
2438 list_for_each_entry(prop_enum, &property->enum_list, head) {
2439 if (prop_enum->value == value) {
2440 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2441 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2442 return 0;
2443 }
2444 }
2445 }
2446
2447 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2448 if (!prop_enum)
2449 return -ENOMEM;
2450
2451 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2452 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2453 prop_enum->value = value;
2454
2455 property->values[index] = value;
2456 list_add_tail(&prop_enum->head, &property->enum_list);
2457 return 0;
2458 }
2459 EXPORT_SYMBOL(drm_property_add_enum);
2460
2461 /**
2462 * drm_property_destroy - destroy a drm property
2463 * @dev: drm device
2464 * @property: property to destry
2465 *
2466 * This function frees a property including any attached resources like
2467 * enumeration values.
2468 */
2469 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2470 {
2471 struct drm_property_enum *prop_enum, *pt;
2472
2473 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
2474 list_del(&prop_enum->head);
2475 kfree(prop_enum);
2476 }
2477
2478 if (property->num_values)
2479 kfree(property->values);
2480 drm_mode_object_unregister(dev, &property->base);
2481 list_del(&property->head);
2482 kfree(property);
2483 }
2484 EXPORT_SYMBOL(drm_property_destroy);
2485
2486 /**
2487 * drm_object_attach_property - attach a property to a modeset object
2488 * @obj: drm modeset object
2489 * @property: property to attach
2490 * @init_val: initial value of the property
2491 *
2492 * This attaches the given property to the modeset object with the given initial
2493 * value. Currently this function cannot fail since the properties are stored in
2494 * a statically sized array.
2495 */
2496 void drm_object_attach_property(struct drm_mode_object *obj,
2497 struct drm_property *property,
2498 uint64_t init_val)
2499 {
2500 int count = obj->properties->count;
2501
2502 if (count == DRM_OBJECT_MAX_PROPERTY) {
2503 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2504 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2505 "you see this message on the same object type.\n",
2506 obj->type);
2507 return;
2508 }
2509
2510 obj->properties->properties[count] = property;
2511 obj->properties->values[count] = init_val;
2512 obj->properties->count++;
2513 if (property->flags & DRM_MODE_PROP_ATOMIC)
2514 obj->properties->atomic_count++;
2515 }
2516 EXPORT_SYMBOL(drm_object_attach_property);
2517
2518 /**
2519 * drm_object_property_set_value - set the value of a property
2520 * @obj: drm mode object to set property value for
2521 * @property: property to set
2522 * @val: value the property should be set to
2523 *
2524 * This functions sets a given property on a given object. This function only
2525 * changes the software state of the property, it does not call into the
2526 * driver's ->set_property callback.
2527 *
2528 * Returns:
2529 * Zero on success, error code on failure.
2530 */
2531 int drm_object_property_set_value(struct drm_mode_object *obj,
2532 struct drm_property *property, uint64_t val)
2533 {
2534 int i;
2535
2536 for (i = 0; i < obj->properties->count; i++) {
2537 if (obj->properties->properties[i] == property) {
2538 obj->properties->values[i] = val;
2539 return 0;
2540 }
2541 }
2542
2543 return -EINVAL;
2544 }
2545 EXPORT_SYMBOL(drm_object_property_set_value);
2546
2547 /**
2548 * drm_object_property_get_value - retrieve the value of a property
2549 * @obj: drm mode object to get property value from
2550 * @property: property to retrieve
2551 * @val: storage for the property value
2552 *
2553 * This function retrieves the softare state of the given property for the given
2554 * property. Since there is no driver callback to retrieve the current property
2555 * value this might be out of sync with the hardware, depending upon the driver
2556 * and property.
2557 *
2558 * Returns:
2559 * Zero on success, error code on failure.
2560 */
2561 int drm_object_property_get_value(struct drm_mode_object *obj,
2562 struct drm_property *property, uint64_t *val)
2563 {
2564 int i;
2565
2566 /* read-only properties bypass atomic mechanism and still store
2567 * their value in obj->properties->values[].. mostly to avoid
2568 * having to deal w/ EDID and similar props in atomic paths:
2569 */
2570 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
2571 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
2572 return drm_atomic_get_property(obj, property, val);
2573
2574 for (i = 0; i < obj->properties->count; i++) {
2575 if (obj->properties->properties[i] == property) {
2576 *val = obj->properties->values[i];
2577 return 0;
2578 }
2579 }
2580
2581 return -EINVAL;
2582 }
2583 EXPORT_SYMBOL(drm_object_property_get_value);
2584
2585 /**
2586 * drm_mode_getproperty_ioctl - get the property metadata
2587 * @dev: DRM device
2588 * @data: ioctl data
2589 * @file_priv: DRM file info
2590 *
2591 * This function retrieves the metadata for a given property, like the different
2592 * possible values for an enum property or the limits for a range property.
2593 *
2594 * Blob properties are special
2595 *
2596 * Called by the user via ioctl.
2597 *
2598 * Returns:
2599 * Zero on success, negative errno on failure.
2600 */
2601 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2602 void *data, struct drm_file *file_priv)
2603 {
2604 struct drm_mode_get_property *out_resp = data;
2605 struct drm_property *property;
2606 int enum_count = 0;
2607 int value_count = 0;
2608 int ret = 0, i;
2609 int copied;
2610 struct drm_property_enum *prop_enum;
2611 struct drm_mode_property_enum __user *enum_ptr;
2612 uint64_t __user *values_ptr;
2613
2614 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2615 return -EINVAL;
2616
2617 drm_modeset_lock_all(dev);
2618 property = drm_property_find(dev, out_resp->prop_id);
2619 if (!property) {
2620 ret = -ENOENT;
2621 goto done;
2622 }
2623
2624 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
2625 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
2626 list_for_each_entry(prop_enum, &property->enum_list, head)
2627 enum_count++;
2628 }
2629
2630 value_count = property->num_values;
2631
2632 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2633 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2634 out_resp->flags = property->flags;
2635
2636 if ((out_resp->count_values >= value_count) && value_count) {
2637 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
2638 for (i = 0; i < value_count; i++) {
2639 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2640 ret = -EFAULT;
2641 goto done;
2642 }
2643 }
2644 }
2645 out_resp->count_values = value_count;
2646
2647 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
2648 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
2649 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2650 copied = 0;
2651 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
2652 list_for_each_entry(prop_enum, &property->enum_list, head) {
2653
2654 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2655 ret = -EFAULT;
2656 goto done;
2657 }
2658
2659 if (copy_to_user(&enum_ptr[copied].name,
2660 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2661 ret = -EFAULT;
2662 goto done;
2663 }
2664 copied++;
2665 }
2666 }
2667 out_resp->count_enum_blobs = enum_count;
2668 }
2669
2670 /*
2671 * NOTE: The idea seems to have been to use this to read all the blob
2672 * property values. But nothing ever added them to the corresponding
2673 * list, userspace always used the special-purpose get_blob ioctl to
2674 * read the value for a blob property. It also doesn't make a lot of
2675 * sense to return values here when everything else is just metadata for
2676 * the property itself.
2677 */
2678 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
2679 out_resp->count_enum_blobs = 0;
2680 done:
2681 drm_modeset_unlock_all(dev);
2682 return ret;
2683 }
2684
2685 static void drm_property_free_blob(struct kref *kref)
2686 {
2687 struct drm_property_blob *blob =
2688 container_of(kref, struct drm_property_blob, base.refcount);
2689
2690 mutex_lock(&blob->dev->mode_config.blob_lock);
2691 list_del(&blob->head_global);
2692 mutex_unlock(&blob->dev->mode_config.blob_lock);
2693
2694 drm_mode_object_unregister(blob->dev, &blob->base);
2695
2696 kfree(blob);
2697 }
2698
2699 /**
2700 * drm_property_create_blob - Create new blob property
2701 *
2702 * Creates a new blob property for a specified DRM device, optionally
2703 * copying data.
2704 *
2705 * @dev: DRM device to create property for
2706 * @length: Length to allocate for blob data
2707 * @data: If specified, copies data into blob
2708 *
2709 * Returns:
2710 * New blob property with a single reference on success, or an ERR_PTR
2711 * value on failure.
2712 */
2713 struct drm_property_blob *
2714 drm_property_create_blob(struct drm_device *dev, size_t length,
2715 const void *data)
2716 {
2717 struct drm_property_blob *blob;
2718 int ret;
2719
2720 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
2721 return ERR_PTR(-EINVAL);
2722
2723 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2724 if (!blob)
2725 return ERR_PTR(-ENOMEM);
2726
2727 /* This must be explicitly initialised, so we can safely call list_del
2728 * on it in the removal handler, even if it isn't in a file list. */
2729 INIT_LIST_HEAD(&blob->head_file);
2730 blob->length = length;
2731 blob->dev = dev;
2732
2733 if (data)
2734 memcpy(blob->data, data, length);
2735
2736 ret = drm_mode_object_get_reg(dev, &blob->base, DRM_MODE_OBJECT_BLOB,
2737 true, drm_property_free_blob);
2738 if (ret) {
2739 kfree(blob);
2740 return ERR_PTR(-EINVAL);
2741 }
2742
2743 mutex_lock(&dev->mode_config.blob_lock);
2744 list_add_tail(&blob->head_global,
2745 &dev->mode_config.property_blob_list);
2746 mutex_unlock(&dev->mode_config.blob_lock);
2747
2748 return blob;
2749 }
2750 EXPORT_SYMBOL(drm_property_create_blob);
2751
2752 /**
2753 * drm_property_unreference_blob - Unreference a blob property
2754 *
2755 * Drop a reference on a blob property. May free the object.
2756 *
2757 * @blob: Pointer to blob property
2758 */
2759 void drm_property_unreference_blob(struct drm_property_blob *blob)
2760 {
2761 if (!blob)
2762 return;
2763
2764 drm_mode_object_unreference(&blob->base);
2765 }
2766 EXPORT_SYMBOL(drm_property_unreference_blob);
2767
2768 /**
2769 * drm_property_destroy_user_blobs - destroy all blobs created by this client
2770 * @dev: DRM device
2771 * @file_priv: destroy all blobs owned by this file handle
2772 */
2773 void drm_property_destroy_user_blobs(struct drm_device *dev,
2774 struct drm_file *file_priv)
2775 {
2776 struct drm_property_blob *blob, *bt;
2777
2778 /*
2779 * When the file gets released that means no one else can access the
2780 * blob list any more, so no need to grab dev->blob_lock.
2781 */
2782 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
2783 list_del_init(&blob->head_file);
2784 drm_property_unreference_blob(blob);
2785 }
2786 }
2787
2788 /**
2789 * drm_property_reference_blob - Take a reference on an existing property
2790 *
2791 * Take a new reference on an existing blob property.
2792 *
2793 * @blob: Pointer to blob property
2794 */
2795 struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
2796 {
2797 drm_mode_object_reference(&blob->base);
2798 return blob;
2799 }
2800 EXPORT_SYMBOL(drm_property_reference_blob);
2801
2802 /**
2803 * drm_property_lookup_blob - look up a blob property and take a reference
2804 * @dev: drm device
2805 * @id: id of the blob property
2806 *
2807 * If successful, this takes an additional reference to the blob property.
2808 * callers need to make sure to eventually unreference the returned property
2809 * again, using @drm_property_unreference_blob.
2810 */
2811 struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
2812 uint32_t id)
2813 {
2814 struct drm_mode_object *obj;
2815 struct drm_property_blob *blob = NULL;
2816
2817 obj = __drm_mode_object_find(dev, id, DRM_MODE_OBJECT_BLOB);
2818 if (obj)
2819 blob = obj_to_blob(obj);
2820 return blob;
2821 }
2822 EXPORT_SYMBOL(drm_property_lookup_blob);
2823
2824 /**
2825 * drm_property_replace_global_blob - atomically replace existing blob property
2826 * @dev: drm device
2827 * @replace: location of blob property pointer to be replaced
2828 * @length: length of data for new blob, or 0 for no data
2829 * @data: content for new blob, or NULL for no data
2830 * @obj_holds_id: optional object for property holding blob ID
2831 * @prop_holds_id: optional property holding blob ID
2832 * @return 0 on success or error on failure
2833 *
2834 * This function will atomically replace a global property in the blob list,
2835 * optionally updating a property which holds the ID of that property. It is
2836 * guaranteed to be atomic: no caller will be allowed to see intermediate
2837 * results, and either the entire operation will succeed and clean up the
2838 * previous property, or it will fail and the state will be unchanged.
2839 *
2840 * If length is 0 or data is NULL, no new blob will be created, and the holding
2841 * property, if specified, will be set to 0.
2842 *
2843 * Access to the replace pointer is assumed to be protected by the caller, e.g.
2844 * by holding the relevant modesetting object lock for its parent.
2845 *
2846 * For example, a drm_connector has a 'PATH' property, which contains the ID
2847 * of a blob property with the value of the MST path information. Calling this
2848 * function with replace pointing to the connector's path_blob_ptr, length and
2849 * data set for the new path information, obj_holds_id set to the connector's
2850 * base object, and prop_holds_id set to the path property name, will perform
2851 * a completely atomic update. The access to path_blob_ptr is protected by the
2852 * caller holding a lock on the connector.
2853 */
2854 int drm_property_replace_global_blob(struct drm_device *dev,
2855 struct drm_property_blob **replace,
2856 size_t length,
2857 const void *data,
2858 struct drm_mode_object *obj_holds_id,
2859 struct drm_property *prop_holds_id)
2860 {
2861 struct drm_property_blob *new_blob = NULL;
2862 struct drm_property_blob *old_blob = NULL;
2863 int ret;
2864
2865 WARN_ON(replace == NULL);
2866
2867 old_blob = *replace;
2868
2869 if (length && data) {
2870 new_blob = drm_property_create_blob(dev, length, data);
2871 if (IS_ERR(new_blob))
2872 return PTR_ERR(new_blob);
2873 }
2874
2875 /* This does not need to be synchronised with blob_lock, as the
2876 * get_properties ioctl locks all modesetting objects, and
2877 * obj_holds_id must be locked before calling here, so we cannot
2878 * have its value out of sync with the list membership modified
2879 * below under blob_lock. */
2880 if (obj_holds_id) {
2881 ret = drm_object_property_set_value(obj_holds_id,
2882 prop_holds_id,
2883 new_blob ?
2884 new_blob->base.id : 0);
2885 if (ret != 0)
2886 goto err_created;
2887 }
2888
2889 drm_property_unreference_blob(old_blob);
2890 *replace = new_blob;
2891
2892 return 0;
2893
2894 err_created:
2895 drm_property_unreference_blob(new_blob);
2896 return ret;
2897 }
2898 EXPORT_SYMBOL(drm_property_replace_global_blob);
2899
2900 /**
2901 * drm_mode_getblob_ioctl - get the contents of a blob property value
2902 * @dev: DRM device
2903 * @data: ioctl data
2904 * @file_priv: DRM file info
2905 *
2906 * This function retrieves the contents of a blob property. The value stored in
2907 * an object's blob property is just a normal modeset object id.
2908 *
2909 * Called by the user via ioctl.
2910 *
2911 * Returns:
2912 * Zero on success, negative errno on failure.
2913 */
2914 int drm_mode_getblob_ioctl(struct drm_device *dev,
2915 void *data, struct drm_file *file_priv)
2916 {
2917 struct drm_mode_get_blob *out_resp = data;
2918 struct drm_property_blob *blob;
2919 int ret = 0;
2920 void __user *blob_ptr;
2921
2922 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2923 return -EINVAL;
2924
2925 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
2926 if (!blob)
2927 return -ENOENT;
2928
2929 if (out_resp->length == blob->length) {
2930 blob_ptr = (void __user *)(unsigned long)out_resp->data;
2931 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
2932 ret = -EFAULT;
2933 goto unref;
2934 }
2935 }
2936 out_resp->length = blob->length;
2937 unref:
2938 drm_property_unreference_blob(blob);
2939
2940 return ret;
2941 }
2942
2943 /**
2944 * drm_mode_createblob_ioctl - create a new blob property
2945 * @dev: DRM device
2946 * @data: ioctl data
2947 * @file_priv: DRM file info
2948 *
2949 * This function creates a new blob property with user-defined values. In order
2950 * to give us sensible validation and checking when creating, rather than at
2951 * every potential use, we also require a type to be provided upfront.
2952 *
2953 * Called by the user via ioctl.
2954 *
2955 * Returns:
2956 * Zero on success, negative errno on failure.
2957 */
2958 int drm_mode_createblob_ioctl(struct drm_device *dev,
2959 void *data, struct drm_file *file_priv)
2960 {
2961 struct drm_mode_create_blob *out_resp = data;
2962 struct drm_property_blob *blob;
2963 void __user *blob_ptr;
2964 int ret = 0;
2965
2966 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2967 return -EINVAL;
2968
2969 blob = drm_property_create_blob(dev, out_resp->length, NULL);
2970 if (IS_ERR(blob))
2971 return PTR_ERR(blob);
2972
2973 blob_ptr = (void __user *)(unsigned long)out_resp->data;
2974 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
2975 ret = -EFAULT;
2976 goto out_blob;
2977 }
2978
2979 /* Dropping the lock between create_blob and our access here is safe
2980 * as only the same file_priv can remove the blob; at this point, it is
2981 * not associated with any file_priv. */
2982 mutex_lock(&dev->mode_config.blob_lock);
2983 out_resp->blob_id = blob->base.id;
2984 list_add_tail(&blob->head_file, &file_priv->blobs);
2985 mutex_unlock(&dev->mode_config.blob_lock);
2986
2987 return 0;
2988
2989 out_blob:
2990 drm_property_unreference_blob(blob);
2991 return ret;
2992 }
2993
2994 /**
2995 * drm_mode_destroyblob_ioctl - destroy a user blob property
2996 * @dev: DRM device
2997 * @data: ioctl data
2998 * @file_priv: DRM file info
2999 *
3000 * Destroy an existing user-defined blob property.
3001 *
3002 * Called by the user via ioctl.
3003 *
3004 * Returns:
3005 * Zero on success, negative errno on failure.
3006 */
3007 int drm_mode_destroyblob_ioctl(struct drm_device *dev,
3008 void *data, struct drm_file *file_priv)
3009 {
3010 struct drm_mode_destroy_blob *out_resp = data;
3011 struct drm_property_blob *blob = NULL, *bt;
3012 bool found = false;
3013 int ret = 0;
3014
3015 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3016 return -EINVAL;
3017
3018 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
3019 if (!blob)
3020 return -ENOENT;
3021
3022 mutex_lock(&dev->mode_config.blob_lock);
3023 /* Ensure the property was actually created by this user. */
3024 list_for_each_entry(bt, &file_priv->blobs, head_file) {
3025 if (bt == blob) {
3026 found = true;
3027 break;
3028 }
3029 }
3030
3031 if (!found) {
3032 ret = -EPERM;
3033 goto err;
3034 }
3035
3036 /* We must drop head_file here, because we may not be the last
3037 * reference on the blob. */
3038 list_del_init(&blob->head_file);
3039 mutex_unlock(&dev->mode_config.blob_lock);
3040
3041 /* One reference from lookup, and one from the filp. */
3042 drm_property_unreference_blob(blob);
3043 drm_property_unreference_blob(blob);
3044
3045 return 0;
3046
3047 err:
3048 mutex_unlock(&dev->mode_config.blob_lock);
3049 drm_property_unreference_blob(blob);
3050
3051 return ret;
3052 }
3053
3054 /* Some properties could refer to dynamic refcnt'd objects, or things that
3055 * need special locking to handle lifetime issues (ie. to ensure the prop
3056 * value doesn't become invalid part way through the property update due to
3057 * race). The value returned by reference via 'obj' should be passed back
3058 * to drm_property_change_valid_put() after the property is set (and the
3059 * object to which the property is attached has a chance to take it's own
3060 * reference).
3061 */
3062 bool drm_property_change_valid_get(struct drm_property *property,
3063 uint64_t value, struct drm_mode_object **ref)
3064 {
3065 int i;
3066
3067 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3068 return false;
3069
3070 *ref = NULL;
3071
3072 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
3073 if (value < property->values[0] || value > property->values[1])
3074 return false;
3075 return true;
3076 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
3077 int64_t svalue = U642I64(value);
3078
3079 if (svalue < U642I64(property->values[0]) ||
3080 svalue > U642I64(property->values[1]))
3081 return false;
3082 return true;
3083 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3084 uint64_t valid_mask = 0;
3085
3086 for (i = 0; i < property->num_values; i++)
3087 valid_mask |= (1ULL << property->values[i]);
3088 return !(value & ~valid_mask);
3089 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
3090 struct drm_property_blob *blob;
3091
3092 if (value == 0)
3093 return true;
3094
3095 blob = drm_property_lookup_blob(property->dev, value);
3096 if (blob) {
3097 *ref = &blob->base;
3098 return true;
3099 } else {
3100 return false;
3101 }
3102 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
3103 /* a zero value for an object property translates to null: */
3104 if (value == 0)
3105 return true;
3106
3107 *ref = __drm_mode_object_find(property->dev, value,
3108 property->values[0]);
3109 return *ref != NULL;
3110 }
3111
3112 for (i = 0; i < property->num_values; i++)
3113 if (property->values[i] == value)
3114 return true;
3115 return false;
3116 }
3117
3118 void drm_property_change_valid_put(struct drm_property *property,
3119 struct drm_mode_object *ref)
3120 {
3121 if (!ref)
3122 return;
3123
3124 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
3125 drm_mode_object_unreference(ref);
3126 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
3127 drm_property_unreference_blob(obj_to_blob(ref));
3128 }
3129
3130 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3131 struct drm_property *property,
3132 uint64_t value)
3133 {
3134 int ret = -EINVAL;
3135 struct drm_crtc *crtc = obj_to_crtc(obj);
3136
3137 if (crtc->funcs->set_property)
3138 ret = crtc->funcs->set_property(crtc, property, value);
3139 if (!ret)
3140 drm_object_property_set_value(obj, property, value);
3141
3142 return ret;
3143 }
3144
3145 /**
3146 * drm_mode_plane_set_obj_prop - set the value of a property
3147 * @plane: drm plane object to set property value for
3148 * @property: property to set
3149 * @value: value the property should be set to
3150 *
3151 * This functions sets a given property on a given plane object. This function
3152 * calls the driver's ->set_property callback and changes the software state of
3153 * the property if the callback succeeds.
3154 *
3155 * Returns:
3156 * Zero on success, error code on failure.
3157 */
3158 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
3159 struct drm_property *property,
3160 uint64_t value)
3161 {
3162 int ret = -EINVAL;
3163 struct drm_mode_object *obj = &plane->base;
3164
3165 if (plane->funcs->set_property)
3166 ret = plane->funcs->set_property(plane, property, value);
3167 if (!ret)
3168 drm_object_property_set_value(obj, property, value);
3169
3170 return ret;
3171 }
3172 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
3173
3174 /**
3175 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
3176 * @dev: DRM device
3177 * @data: ioctl data
3178 * @file_priv: DRM file info
3179 *
3180 * This function retrieves the current value for an object's property. Compared
3181 * to the connector specific ioctl this one is extended to also work on crtc and
3182 * plane objects.
3183 *
3184 * Called by the user via ioctl.
3185 *
3186 * Returns:
3187 * Zero on success, negative errno on failure.
3188 */
3189 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3190 struct drm_file *file_priv)
3191 {
3192 struct drm_mode_obj_get_properties *arg = data;
3193 struct drm_mode_object *obj;
3194 int ret = 0;
3195
3196 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3197 return -EINVAL;
3198
3199 drm_modeset_lock_all(dev);
3200
3201 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3202 if (!obj) {
3203 ret = -ENOENT;
3204 goto out;
3205 }
3206 if (!obj->properties) {
3207 ret = -EINVAL;
3208 goto out_unref;
3209 }
3210
3211 ret = drm_mode_object_get_properties(obj, file_priv->atomic,
3212 (uint32_t __user *)(unsigned long)(arg->props_ptr),
3213 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
3214 &arg->count_props);
3215
3216 out_unref:
3217 drm_mode_object_unreference(obj);
3218 out:
3219 drm_modeset_unlock_all(dev);
3220 return ret;
3221 }
3222
3223 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3224 struct drm_file *file_priv)
3225 {
3226 struct drm_mode_obj_set_property *arg = data;
3227 struct drm_mode_object *arg_obj;
3228 struct drm_mode_object *prop_obj;
3229 struct drm_property *property;
3230 int i, ret = -EINVAL;
3231 struct drm_mode_object *ref;
3232
3233 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3234 return -EINVAL;
3235
3236 drm_modeset_lock_all(dev);
3237
3238 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3239 if (!arg_obj) {
3240 ret = -ENOENT;
3241 goto out;
3242 }
3243 if (!arg_obj->properties)
3244 goto out_unref;
3245
3246 for (i = 0; i < arg_obj->properties->count; i++)
3247 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
3248 break;
3249
3250 if (i == arg_obj->properties->count)
3251 goto out_unref;
3252
3253 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3254 DRM_MODE_OBJECT_PROPERTY);
3255 if (!prop_obj) {
3256 ret = -ENOENT;
3257 goto out_unref;
3258 }
3259 property = obj_to_property(prop_obj);
3260
3261 if (!drm_property_change_valid_get(property, arg->value, &ref))
3262 goto out_unref;
3263
3264 switch (arg_obj->type) {
3265 case DRM_MODE_OBJECT_CONNECTOR:
3266 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3267 arg->value);
3268 break;
3269 case DRM_MODE_OBJECT_CRTC:
3270 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3271 break;
3272 case DRM_MODE_OBJECT_PLANE:
3273 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
3274 property, arg->value);
3275 break;
3276 }
3277
3278 drm_property_change_valid_put(property, ref);
3279
3280 out_unref:
3281 drm_mode_object_unreference(arg_obj);
3282 out:
3283 drm_modeset_unlock_all(dev);
3284 return ret;
3285 }
3286
3287 /**
3288 * drm_mode_crtc_set_gamma_size - set the gamma table size
3289 * @crtc: CRTC to set the gamma table size for
3290 * @gamma_size: size of the gamma table
3291 *
3292 * Drivers which support gamma tables should set this to the supported gamma
3293 * table size when initializing the CRTC. Currently the drm core only supports a
3294 * fixed gamma table size.
3295 *
3296 * Returns:
3297 * Zero on success, negative errno on failure.
3298 */
3299 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
3300 int gamma_size)
3301 {
3302 uint16_t *r_base, *g_base, *b_base;
3303 int i;
3304
3305 crtc->gamma_size = gamma_size;
3306
3307 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
3308 GFP_KERNEL);
3309 if (!crtc->gamma_store) {
3310 crtc->gamma_size = 0;
3311 return -ENOMEM;
3312 }
3313
3314 r_base = crtc->gamma_store;
3315 g_base = r_base + gamma_size;
3316 b_base = g_base + gamma_size;
3317 for (i = 0; i < gamma_size; i++) {
3318 r_base[i] = i << 8;
3319 g_base[i] = i << 8;
3320 b_base[i] = i << 8;
3321 }
3322
3323
3324 return 0;
3325 }
3326 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3327
3328 /**
3329 * drm_mode_gamma_set_ioctl - set the gamma table
3330 * @dev: DRM device
3331 * @data: ioctl data
3332 * @file_priv: DRM file info
3333 *
3334 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
3335 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
3336 *
3337 * Called by the user via ioctl.
3338 *
3339 * Returns:
3340 * Zero on success, negative errno on failure.
3341 */
3342 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3343 void *data, struct drm_file *file_priv)
3344 {
3345 struct drm_mode_crtc_lut *crtc_lut = data;
3346 struct drm_crtc *crtc;
3347 void *r_base, *g_base, *b_base;
3348 int size;
3349 int ret = 0;
3350
3351 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3352 return -EINVAL;
3353
3354 drm_modeset_lock_all(dev);
3355 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
3356 if (!crtc) {
3357 ret = -ENOENT;
3358 goto out;
3359 }
3360
3361 if (crtc->funcs->gamma_set == NULL) {
3362 ret = -ENOSYS;
3363 goto out;
3364 }
3365
3366 /* memcpy into gamma store */
3367 if (crtc_lut->gamma_size != crtc->gamma_size) {
3368 ret = -EINVAL;
3369 goto out;
3370 }
3371
3372 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3373 r_base = crtc->gamma_store;
3374 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3375 ret = -EFAULT;
3376 goto out;
3377 }
3378
3379 g_base = r_base + size;
3380 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3381 ret = -EFAULT;
3382 goto out;
3383 }
3384
3385 b_base = g_base + size;
3386 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3387 ret = -EFAULT;
3388 goto out;
3389 }
3390
3391 ret = crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
3392
3393 out:
3394 drm_modeset_unlock_all(dev);
3395 return ret;
3396
3397 }
3398
3399 /**
3400 * drm_mode_gamma_get_ioctl - get the gamma table
3401 * @dev: DRM device
3402 * @data: ioctl data
3403 * @file_priv: DRM file info
3404 *
3405 * Copy the current gamma table into the storage provided. This also provides
3406 * the gamma table size the driver expects, which can be used to size the
3407 * allocated storage.
3408 *
3409 * Called by the user via ioctl.
3410 *
3411 * Returns:
3412 * Zero on success, negative errno on failure.
3413 */
3414 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3415 void *data, struct drm_file *file_priv)
3416 {
3417 struct drm_mode_crtc_lut *crtc_lut = data;
3418 struct drm_crtc *crtc;
3419 void *r_base, *g_base, *b_base;
3420 int size;
3421 int ret = 0;
3422
3423 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3424 return -EINVAL;
3425
3426 drm_modeset_lock_all(dev);
3427 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
3428 if (!crtc) {
3429 ret = -ENOENT;
3430 goto out;
3431 }
3432
3433 /* memcpy into gamma store */
3434 if (crtc_lut->gamma_size != crtc->gamma_size) {
3435 ret = -EINVAL;
3436 goto out;
3437 }
3438
3439 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3440 r_base = crtc->gamma_store;
3441 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3442 ret = -EFAULT;
3443 goto out;
3444 }
3445
3446 g_base = r_base + size;
3447 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3448 ret = -EFAULT;
3449 goto out;
3450 }
3451
3452 b_base = g_base + size;
3453 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3454 ret = -EFAULT;
3455 goto out;
3456 }
3457 out:
3458 drm_modeset_unlock_all(dev);
3459 return ret;
3460 }
3461
3462 /**
3463 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
3464 * @dev: DRM device
3465 * @data: ioctl data
3466 * @file_priv: DRM file info
3467 *
3468 * This schedules an asynchronous update on a given CRTC, called page flip.
3469 * Optionally a drm event is generated to signal the completion of the event.
3470 * Generic drivers cannot assume that a pageflip with changed framebuffer
3471 * properties (including driver specific metadata like tiling layout) will work,
3472 * but some drivers support e.g. pixel format changes through the pageflip
3473 * ioctl.
3474 *
3475 * Called by the user via ioctl.
3476 *
3477 * Returns:
3478 * Zero on success, negative errno on failure.
3479 */
3480 int drm_mode_page_flip_ioctl(struct drm_device *dev,
3481 void *data, struct drm_file *file_priv)
3482 {
3483 struct drm_mode_crtc_page_flip *page_flip = data;
3484 struct drm_crtc *crtc;
3485 struct drm_framebuffer *fb = NULL;
3486 struct drm_pending_vblank_event *e = NULL;
3487 int ret = -EINVAL;
3488
3489 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3490 page_flip->reserved != 0)
3491 return -EINVAL;
3492
3493 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
3494 return -EINVAL;
3495
3496 crtc = drm_crtc_find(dev, page_flip->crtc_id);
3497 if (!crtc)
3498 return -ENOENT;
3499
3500 drm_modeset_lock_crtc(crtc, crtc->primary);
3501 if (crtc->primary->fb == NULL) {
3502 /* The framebuffer is currently unbound, presumably
3503 * due to a hotplug event, that userspace has not
3504 * yet discovered.
3505 */
3506 ret = -EBUSY;
3507 goto out;
3508 }
3509
3510 if (crtc->funcs->page_flip == NULL)
3511 goto out;
3512
3513 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3514 if (!fb) {
3515 ret = -ENOENT;
3516 goto out;
3517 }
3518
3519 if (crtc->state) {
3520 const struct drm_plane_state *state = crtc->primary->state;
3521
3522 ret = check_src_coords(state->src_x, state->src_y,
3523 state->src_w, state->src_h, fb);
3524 } else {
3525 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
3526 }
3527 if (ret)
3528 goto out;
3529
3530 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
3531 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
3532 ret = -EINVAL;
3533 goto out;
3534 }
3535
3536 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3537 e = kzalloc(sizeof *e, GFP_KERNEL);
3538 if (!e) {
3539 ret = -ENOMEM;
3540 goto out;
3541 }
3542 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
3543 e->event.base.length = sizeof(e->event);
3544 e->event.user_data = page_flip->user_data;
3545 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
3546 if (ret) {
3547 kfree(e);
3548 goto out;
3549 }
3550 }
3551
3552 crtc->primary->old_fb = crtc->primary->fb;
3553 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
3554 if (ret) {
3555 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
3556 drm_event_cancel_free(dev, &e->base);
3557 /* Keep the old fb, don't unref it. */
3558 crtc->primary->old_fb = NULL;
3559 } else {
3560 crtc->primary->fb = fb;
3561 /* Unref only the old framebuffer. */
3562 fb = NULL;
3563 }
3564
3565 out:
3566 if (fb)
3567 drm_framebuffer_unreference(fb);
3568 if (crtc->primary->old_fb)
3569 drm_framebuffer_unreference(crtc->primary->old_fb);
3570 crtc->primary->old_fb = NULL;
3571 drm_modeset_unlock_crtc(crtc);
3572
3573 return ret;
3574 }
3575
3576 /**
3577 * drm_mode_config_reset - call ->reset callbacks
3578 * @dev: drm device
3579 *
3580 * This functions calls all the crtc's, encoder's and connector's ->reset
3581 * callback. Drivers can use this in e.g. their driver load or resume code to
3582 * reset hardware and software state.
3583 */
3584 void drm_mode_config_reset(struct drm_device *dev)
3585 {
3586 struct drm_crtc *crtc;
3587 struct drm_plane *plane;
3588 struct drm_encoder *encoder;
3589 struct drm_connector *connector;
3590
3591 drm_for_each_plane(plane, dev)
3592 if (plane->funcs->reset)
3593 plane->funcs->reset(plane);
3594
3595 drm_for_each_crtc(crtc, dev)
3596 if (crtc->funcs->reset)
3597 crtc->funcs->reset(crtc);
3598
3599 drm_for_each_encoder(encoder, dev)
3600 if (encoder->funcs->reset)
3601 encoder->funcs->reset(encoder);
3602
3603 mutex_lock(&dev->mode_config.mutex);
3604 drm_for_each_connector(connector, dev)
3605 if (connector->funcs->reset)
3606 connector->funcs->reset(connector);
3607 mutex_unlock(&dev->mode_config.mutex);
3608 }
3609 EXPORT_SYMBOL(drm_mode_config_reset);
3610
3611 /**
3612 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
3613 * @dev: DRM device
3614 * @data: ioctl data
3615 * @file_priv: DRM file info
3616 *
3617 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
3618 * TTM or something else entirely) and returns the resulting buffer handle. This
3619 * handle can then be wrapped up into a framebuffer modeset object.
3620 *
3621 * Note that userspace is not allowed to use such objects for render
3622 * acceleration - drivers must create their own private ioctls for such a use
3623 * case.
3624 *
3625 * Called by the user via ioctl.
3626 *
3627 * Returns:
3628 * Zero on success, negative errno on failure.
3629 */
3630 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3631 void *data, struct drm_file *file_priv)
3632 {
3633 struct drm_mode_create_dumb *args = data;
3634 u32 cpp, stride, size;
3635
3636 if (!dev->driver->dumb_create)
3637 return -ENOSYS;
3638 if (!args->width || !args->height || !args->bpp)
3639 return -EINVAL;
3640
3641 /* overflow checks for 32bit size calculations */
3642 /* NOTE: DIV_ROUND_UP() can overflow */
3643 cpp = DIV_ROUND_UP(args->bpp, 8);
3644 if (!cpp || cpp > 0xffffffffU / args->width)
3645 return -EINVAL;
3646 stride = cpp * args->width;
3647 if (args->height > 0xffffffffU / stride)
3648 return -EINVAL;
3649
3650 /* test for wrap-around */
3651 size = args->height * stride;
3652 if (PAGE_ALIGN(size) == 0)
3653 return -EINVAL;
3654
3655 /*
3656 * handle, pitch and size are output parameters. Zero them out to
3657 * prevent drivers from accidentally using uninitialized data. Since
3658 * not all existing userspace is clearing these fields properly we
3659 * cannot reject IOCTL with garbage in them.
3660 */
3661 args->handle = 0;
3662 args->pitch = 0;
3663 args->size = 0;
3664
3665 return dev->driver->dumb_create(file_priv, dev, args);
3666 }
3667
3668 /**
3669 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
3670 * @dev: DRM device
3671 * @data: ioctl data
3672 * @file_priv: DRM file info
3673 *
3674 * Allocate an offset in the drm device node's address space to be able to
3675 * memory map a dumb buffer.
3676 *
3677 * Called by the user via ioctl.
3678 *
3679 * Returns:
3680 * Zero on success, negative errno on failure.
3681 */
3682 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3683 void *data, struct drm_file *file_priv)
3684 {
3685 struct drm_mode_map_dumb *args = data;
3686
3687 /* call driver ioctl to get mmap offset */
3688 if (!dev->driver->dumb_map_offset)
3689 return -ENOSYS;
3690
3691 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3692 }
3693
3694 /**
3695 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
3696 * @dev: DRM device
3697 * @data: ioctl data
3698 * @file_priv: DRM file info
3699 *
3700 * This destroys the userspace handle for the given dumb backing storage buffer.
3701 * Since buffer objects must be reference counted in the kernel a buffer object
3702 * won't be immediately freed if a framebuffer modeset object still uses it.
3703 *
3704 * Called by the user via ioctl.
3705 *
3706 * Returns:
3707 * Zero on success, negative errno on failure.
3708 */
3709 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3710 void *data, struct drm_file *file_priv)
3711 {
3712 struct drm_mode_destroy_dumb *args = data;
3713
3714 if (!dev->driver->dumb_destroy)
3715 return -ENOSYS;
3716
3717 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3718 }
3719
3720 /**
3721 * drm_rotation_simplify() - Try to simplify the rotation
3722 * @rotation: Rotation to be simplified
3723 * @supported_rotations: Supported rotations
3724 *
3725 * Attempt to simplify the rotation to a form that is supported.
3726 * Eg. if the hardware supports everything except DRM_REFLECT_X
3727 * one could call this function like this:
3728 *
3729 * drm_rotation_simplify(rotation, DRM_ROTATE_0 |
3730 * DRM_ROTATE_90 | DRM_ROTATE_180 |
3731 * DRM_ROTATE_270 | DRM_REFLECT_Y);
3732 *
3733 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
3734 * transforms the hardware supports, this function may not
3735 * be able to produce a supported transform, so the caller should
3736 * check the result afterwards.
3737 */
3738 unsigned int drm_rotation_simplify(unsigned int rotation,
3739 unsigned int supported_rotations)
3740 {
3741 if (rotation & ~supported_rotations) {
3742 rotation ^= DRM_REFLECT_X | DRM_REFLECT_Y;
3743 rotation = (rotation & DRM_REFLECT_MASK) |
3744 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
3745 }
3746
3747 return rotation;
3748 }
3749 EXPORT_SYMBOL(drm_rotation_simplify);
3750
3751 /**
3752 * drm_mode_config_init - initialize DRM mode_configuration structure
3753 * @dev: DRM device
3754 *
3755 * Initialize @dev's mode_config structure, used for tracking the graphics
3756 * configuration of @dev.
3757 *
3758 * Since this initializes the modeset locks, no locking is possible. Which is no
3759 * problem, since this should happen single threaded at init time. It is the
3760 * driver's problem to ensure this guarantee.
3761 *
3762 */
3763 void drm_mode_config_init(struct drm_device *dev)
3764 {
3765 mutex_init(&dev->mode_config.mutex);
3766 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
3767 mutex_init(&dev->mode_config.idr_mutex);
3768 mutex_init(&dev->mode_config.fb_lock);
3769 mutex_init(&dev->mode_config.blob_lock);
3770 INIT_LIST_HEAD(&dev->mode_config.fb_list);
3771 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
3772 INIT_LIST_HEAD(&dev->mode_config.connector_list);
3773 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
3774 INIT_LIST_HEAD(&dev->mode_config.property_list);
3775 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
3776 INIT_LIST_HEAD(&dev->mode_config.plane_list);
3777 idr_init(&dev->mode_config.crtc_idr);
3778 idr_init(&dev->mode_config.tile_idr);
3779 ida_init(&dev->mode_config.connector_ida);
3780
3781 drm_modeset_lock_all(dev);
3782 drm_mode_create_standard_properties(dev);
3783 drm_modeset_unlock_all(dev);
3784
3785 /* Just to be sure */
3786 dev->mode_config.num_fb = 0;
3787 dev->mode_config.num_connector = 0;
3788 dev->mode_config.num_crtc = 0;
3789 dev->mode_config.num_encoder = 0;
3790 dev->mode_config.num_overlay_plane = 0;
3791 dev->mode_config.num_total_plane = 0;
3792 }
3793 EXPORT_SYMBOL(drm_mode_config_init);
3794
3795 /**
3796 * drm_mode_config_cleanup - free up DRM mode_config info
3797 * @dev: DRM device
3798 *
3799 * Free up all the connectors and CRTCs associated with this DRM device, then
3800 * free up the framebuffers and associated buffer objects.
3801 *
3802 * Note that since this /should/ happen single-threaded at driver/device
3803 * teardown time, no locking is required. It's the driver's job to ensure that
3804 * this guarantee actually holds true.
3805 *
3806 * FIXME: cleanup any dangling user buffer objects too
3807 */
3808 void drm_mode_config_cleanup(struct drm_device *dev)
3809 {
3810 struct drm_connector *connector, *ot;
3811 struct drm_crtc *crtc, *ct;
3812 struct drm_encoder *encoder, *enct;
3813 struct drm_framebuffer *fb, *fbt;
3814 struct drm_property *property, *pt;
3815 struct drm_property_blob *blob, *bt;
3816 struct drm_plane *plane, *plt;
3817
3818 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
3819 head) {
3820 encoder->funcs->destroy(encoder);
3821 }
3822
3823 list_for_each_entry_safe(connector, ot,
3824 &dev->mode_config.connector_list, head) {
3825 connector->funcs->destroy(connector);
3826 }
3827
3828 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
3829 head) {
3830 drm_property_destroy(dev, property);
3831 }
3832
3833 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
3834 head) {
3835 plane->funcs->destroy(plane);
3836 }
3837
3838 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
3839 crtc->funcs->destroy(crtc);
3840 }
3841
3842 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
3843 head_global) {
3844 drm_property_unreference_blob(blob);
3845 }
3846
3847 /*
3848 * Single-threaded teardown context, so it's not required to grab the
3849 * fb_lock to protect against concurrent fb_list access. Contrary, it
3850 * would actually deadlock with the drm_framebuffer_cleanup function.
3851 *
3852 * Also, if there are any framebuffers left, that's a driver leak now,
3853 * so politely WARN about this.
3854 */
3855 WARN_ON(!list_empty(&dev->mode_config.fb_list));
3856 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
3857 drm_framebuffer_free(&fb->base.refcount);
3858 }
3859
3860 ida_destroy(&dev->mode_config.connector_ida);
3861 idr_destroy(&dev->mode_config.tile_idr);
3862 idr_destroy(&dev->mode_config.crtc_idr);
3863 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
3864 }
3865 EXPORT_SYMBOL(drm_mode_config_cleanup);
3866
3867 struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
3868 unsigned int supported_rotations)
3869 {
3870 static const struct drm_prop_enum_list props[] = {
3871 { __builtin_ffs(DRM_ROTATE_0) - 1, "rotate-0" },
3872 { __builtin_ffs(DRM_ROTATE_90) - 1, "rotate-90" },
3873 { __builtin_ffs(DRM_ROTATE_180) - 1, "rotate-180" },
3874 { __builtin_ffs(DRM_ROTATE_270) - 1, "rotate-270" },
3875 { __builtin_ffs(DRM_REFLECT_X) - 1, "reflect-x" },
3876 { __builtin_ffs(DRM_REFLECT_Y) - 1, "reflect-y" },
3877 };
3878
3879 return drm_property_create_bitmask(dev, 0, "rotation",
3880 props, ARRAY_SIZE(props),
3881 supported_rotations);
3882 }
3883 EXPORT_SYMBOL(drm_mode_create_rotation_property);
3884
3885 /**
3886 * DOC: Tile group
3887 *
3888 * Tile groups are used to represent tiled monitors with a unique
3889 * integer identifier. Tiled monitors using DisplayID v1.3 have
3890 * a unique 8-byte handle, we store this in a tile group, so we
3891 * have a common identifier for all tiles in a monitor group.
3892 */
3893 static void drm_tile_group_free(struct kref *kref)
3894 {
3895 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
3896 struct drm_device *dev = tg->dev;
3897 mutex_lock(&dev->mode_config.idr_mutex);
3898 idr_remove(&dev->mode_config.tile_idr, tg->id);
3899 mutex_unlock(&dev->mode_config.idr_mutex);
3900 kfree(tg);
3901 }
3902
3903 /**
3904 * drm_mode_put_tile_group - drop a reference to a tile group.
3905 * @dev: DRM device
3906 * @tg: tile group to drop reference to.
3907 *
3908 * drop reference to tile group and free if 0.
3909 */
3910 void drm_mode_put_tile_group(struct drm_device *dev,
3911 struct drm_tile_group *tg)
3912 {
3913 kref_put(&tg->refcount, drm_tile_group_free);
3914 }
3915
3916 /**
3917 * drm_mode_get_tile_group - get a reference to an existing tile group
3918 * @dev: DRM device
3919 * @topology: 8-bytes unique per monitor.
3920 *
3921 * Use the unique bytes to get a reference to an existing tile group.
3922 *
3923 * RETURNS:
3924 * tile group or NULL if not found.
3925 */
3926 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
3927 char topology[8])
3928 {
3929 struct drm_tile_group *tg;
3930 int id;
3931 mutex_lock(&dev->mode_config.idr_mutex);
3932 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
3933 if (!memcmp(tg->group_data, topology, 8)) {
3934 if (!kref_get_unless_zero(&tg->refcount))
3935 tg = NULL;
3936 mutex_unlock(&dev->mode_config.idr_mutex);
3937 return tg;
3938 }
3939 }
3940 mutex_unlock(&dev->mode_config.idr_mutex);
3941 return NULL;
3942 }
3943 EXPORT_SYMBOL(drm_mode_get_tile_group);
3944
3945 /**
3946 * drm_mode_create_tile_group - create a tile group from a displayid description
3947 * @dev: DRM device
3948 * @topology: 8-bytes unique per monitor.
3949 *
3950 * Create a tile group for the unique monitor, and get a unique
3951 * identifier for the tile group.
3952 *
3953 * RETURNS:
3954 * new tile group or error.
3955 */
3956 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
3957 char topology[8])
3958 {
3959 struct drm_tile_group *tg;
3960 int ret;
3961
3962 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
3963 if (!tg)
3964 return ERR_PTR(-ENOMEM);
3965
3966 kref_init(&tg->refcount);
3967 memcpy(tg->group_data, topology, 8);
3968 tg->dev = dev;
3969
3970 mutex_lock(&dev->mode_config.idr_mutex);
3971 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
3972 if (ret >= 0) {
3973 tg->id = ret;
3974 } else {
3975 kfree(tg);
3976 tg = ERR_PTR(ret);
3977 }
3978
3979 mutex_unlock(&dev->mode_config.idr_mutex);
3980 return tg;
3981 }
3982 EXPORT_SYMBOL(drm_mode_create_tile_group);
3983
3984 /**
3985 * drm_crtc_enable_color_mgmt - enable color management properties
3986 * @crtc: DRM CRTC
3987 * @degamma_lut_size: the size of the degamma lut (before CSC)
3988 * @has_ctm: whether to attach ctm_property for CSC matrix
3989 * @gamma_lut_size: the size of the gamma lut (after CSC)
3990 *
3991 * This function lets the driver enable the color correction
3992 * properties on a CRTC. This includes 3 degamma, csc and gamma
3993 * properties that userspace can set and 2 size properties to inform
3994 * the userspace of the lut sizes. Each of the properties are
3995 * optional. The gamma and degamma properties are only attached if
3996 * their size is not 0 and ctm_property is only attached if has_ctm is
3997 * true.
3998 */
3999 void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
4000 uint degamma_lut_size,
4001 bool has_ctm,
4002 uint gamma_lut_size)
4003 {
4004 struct drm_device *dev = crtc->dev;
4005 struct drm_mode_config *config = &dev->mode_config;
4006
4007 if (degamma_lut_size) {
4008 drm_object_attach_property(&crtc->base,
4009 config->degamma_lut_property, 0);
4010 drm_object_attach_property(&crtc->base,
4011 config->degamma_lut_size_property,
4012 degamma_lut_size);
4013 }
4014
4015 if (has_ctm)
4016 drm_object_attach_property(&crtc->base,
4017 config->ctm_property, 0);
4018
4019 if (gamma_lut_size) {
4020 drm_object_attach_property(&crtc->base,
4021 config->gamma_lut_property, 0);
4022 drm_object_attach_property(&crtc->base,
4023 config->gamma_lut_size_property,
4024 gamma_lut_size);
4025 }
4026 }
4027 EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);
This page took 0.107785 seconds and 6 git commands to generate.