]> Git Repo - linux.git/blob - drivers/gpu/drm/drm_crtc.c
drm/sti: fix compositor debugfs creation
[linux.git] / drivers / gpu / drm / drm_crtc.c
1 /*
2  * Copyright (c) 2006-2008 Intel Corporation
3  * Copyright (c) 2007 Dave Airlie <[email protected]>
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 <[email protected]>
29  *      Dave Airlie <[email protected]>
30  *      Jesse Barnes <[email protected]>
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 /*
58  * Optional properties
59  */
60 /**
61  * drm_crtc_force_disable - Forcibly turn off a CRTC
62  * @crtc: CRTC to turn off
63  *
64  * Returns:
65  * Zero on success, error code on failure.
66  */
67 int drm_crtc_force_disable(struct drm_crtc *crtc)
68 {
69         struct drm_mode_set set = {
70                 .crtc = crtc,
71         };
72
73         return drm_mode_set_config_internal(&set);
74 }
75 EXPORT_SYMBOL(drm_crtc_force_disable);
76
77 /**
78  * drm_crtc_force_disable_all - Forcibly turn off all enabled CRTCs
79  * @dev: DRM device whose CRTCs to turn off
80  *
81  * Drivers may want to call this on unload to ensure that all displays are
82  * unlit and the GPU is in a consistent, low power state. Takes modeset locks.
83  *
84  * Returns:
85  * Zero on success, error code on failure.
86  */
87 int drm_crtc_force_disable_all(struct drm_device *dev)
88 {
89         struct drm_crtc *crtc;
90         int ret = 0;
91
92         drm_modeset_lock_all(dev);
93         drm_for_each_crtc(crtc, dev)
94                 if (crtc->enabled) {
95                         ret = drm_crtc_force_disable(crtc);
96                         if (ret)
97                                 goto out;
98                 }
99 out:
100         drm_modeset_unlock_all(dev);
101         return ret;
102 }
103 EXPORT_SYMBOL(drm_crtc_force_disable_all);
104
105 DEFINE_WW_CLASS(crtc_ww_class);
106
107 static unsigned int drm_num_crtcs(struct drm_device *dev)
108 {
109         unsigned int num = 0;
110         struct drm_crtc *tmp;
111
112         drm_for_each_crtc(tmp, dev) {
113                 num++;
114         }
115
116         return num;
117 }
118
119 static int drm_crtc_register_all(struct drm_device *dev)
120 {
121         struct drm_crtc *crtc;
122         int ret = 0;
123
124         drm_for_each_crtc(crtc, dev) {
125                 if (crtc->funcs->late_register)
126                         ret = crtc->funcs->late_register(crtc);
127                 if (ret)
128                         return ret;
129         }
130
131         return 0;
132 }
133
134 static void drm_crtc_unregister_all(struct drm_device *dev)
135 {
136         struct drm_crtc *crtc;
137
138         drm_for_each_crtc(crtc, dev) {
139                 if (crtc->funcs->early_unregister)
140                         crtc->funcs->early_unregister(crtc);
141         }
142 }
143
144 /**
145  * drm_crtc_init_with_planes - Initialise a new CRTC object with
146  *    specified primary and cursor planes.
147  * @dev: DRM device
148  * @crtc: CRTC object to init
149  * @primary: Primary plane for CRTC
150  * @cursor: Cursor plane for CRTC
151  * @funcs: callbacks for the new CRTC
152  * @name: printf style format string for the CRTC name, or NULL for default name
153  *
154  * Inits a new object created as base part of a driver crtc object.
155  *
156  * Returns:
157  * Zero on success, error code on failure.
158  */
159 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
160                               struct drm_plane *primary,
161                               struct drm_plane *cursor,
162                               const struct drm_crtc_funcs *funcs,
163                               const char *name, ...)
164 {
165         struct drm_mode_config *config = &dev->mode_config;
166         int ret;
167
168         WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
169         WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
170
171         crtc->dev = dev;
172         crtc->funcs = funcs;
173
174         INIT_LIST_HEAD(&crtc->commit_list);
175         spin_lock_init(&crtc->commit_lock);
176
177         drm_modeset_lock_init(&crtc->mutex);
178         ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
179         if (ret)
180                 return ret;
181
182         if (name) {
183                 va_list ap;
184
185                 va_start(ap, name);
186                 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
187                 va_end(ap);
188         } else {
189                 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
190                                        drm_num_crtcs(dev));
191         }
192         if (!crtc->name) {
193                 drm_mode_object_unregister(dev, &crtc->base);
194                 return -ENOMEM;
195         }
196
197         crtc->base.properties = &crtc->properties;
198
199         list_add_tail(&crtc->head, &config->crtc_list);
200         crtc->index = config->num_crtc++;
201
202         crtc->primary = primary;
203         crtc->cursor = cursor;
204         if (primary)
205                 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
206         if (cursor)
207                 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
208
209         if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
210                 drm_object_attach_property(&crtc->base, config->prop_active, 0);
211                 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
212         }
213
214         return 0;
215 }
216 EXPORT_SYMBOL(drm_crtc_init_with_planes);
217
218 /**
219  * drm_crtc_cleanup - Clean up the core crtc usage
220  * @crtc: CRTC to cleanup
221  *
222  * This function cleans up @crtc and removes it from the DRM mode setting
223  * core. Note that the function does *not* free the crtc structure itself,
224  * this is the responsibility of the caller.
225  */
226 void drm_crtc_cleanup(struct drm_crtc *crtc)
227 {
228         struct drm_device *dev = crtc->dev;
229
230         /* Note that the crtc_list is considered to be static; should we
231          * remove the drm_crtc at runtime we would have to decrement all
232          * the indices on the drm_crtc after us in the crtc_list.
233          */
234
235         kfree(crtc->gamma_store);
236         crtc->gamma_store = NULL;
237
238         drm_modeset_lock_fini(&crtc->mutex);
239
240         drm_mode_object_unregister(dev, &crtc->base);
241         list_del(&crtc->head);
242         dev->mode_config.num_crtc--;
243
244         WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
245         if (crtc->state && crtc->funcs->atomic_destroy_state)
246                 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
247
248         kfree(crtc->name);
249
250         memset(crtc, 0, sizeof(*crtc));
251 }
252 EXPORT_SYMBOL(drm_crtc_cleanup);
253
254 static unsigned int drm_num_planes(struct drm_device *dev)
255 {
256         unsigned int num = 0;
257         struct drm_plane *tmp;
258
259         drm_for_each_plane(tmp, dev) {
260                 num++;
261         }
262
263         return num;
264 }
265
266 /**
267  * drm_universal_plane_init - Initialize a new universal plane object
268  * @dev: DRM device
269  * @plane: plane object to init
270  * @possible_crtcs: bitmask of possible CRTCs
271  * @funcs: callbacks for the new plane
272  * @formats: array of supported formats (DRM_FORMAT\_\*)
273  * @format_count: number of elements in @formats
274  * @type: type of plane (overlay, primary, cursor)
275  * @name: printf style format string for the plane name, or NULL for default name
276  *
277  * Initializes a plane object of type @type.
278  *
279  * Returns:
280  * Zero on success, error code on failure.
281  */
282 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
283                              unsigned long possible_crtcs,
284                              const struct drm_plane_funcs *funcs,
285                              const uint32_t *formats, unsigned int format_count,
286                              enum drm_plane_type type,
287                              const char *name, ...)
288 {
289         struct drm_mode_config *config = &dev->mode_config;
290         int ret;
291
292         ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
293         if (ret)
294                 return ret;
295
296         drm_modeset_lock_init(&plane->mutex);
297
298         plane->base.properties = &plane->properties;
299         plane->dev = dev;
300         plane->funcs = funcs;
301         plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
302                                             GFP_KERNEL);
303         if (!plane->format_types) {
304                 DRM_DEBUG_KMS("out of memory when allocating plane\n");
305                 drm_mode_object_unregister(dev, &plane->base);
306                 return -ENOMEM;
307         }
308
309         if (name) {
310                 va_list ap;
311
312                 va_start(ap, name);
313                 plane->name = kvasprintf(GFP_KERNEL, name, ap);
314                 va_end(ap);
315         } else {
316                 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
317                                         drm_num_planes(dev));
318         }
319         if (!plane->name) {
320                 kfree(plane->format_types);
321                 drm_mode_object_unregister(dev, &plane->base);
322                 return -ENOMEM;
323         }
324
325         memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
326         plane->format_count = format_count;
327         plane->possible_crtcs = possible_crtcs;
328         plane->type = type;
329
330         list_add_tail(&plane->head, &config->plane_list);
331         plane->index = config->num_total_plane++;
332         if (plane->type == DRM_PLANE_TYPE_OVERLAY)
333                 config->num_overlay_plane++;
334
335         drm_object_attach_property(&plane->base,
336                                    config->plane_type_property,
337                                    plane->type);
338
339         if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
340                 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
341                 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
342                 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
343                 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
344                 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
345                 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
346                 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
347                 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
348                 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
349                 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
350         }
351
352         return 0;
353 }
354 EXPORT_SYMBOL(drm_universal_plane_init);
355
356 static int drm_plane_register_all(struct drm_device *dev)
357 {
358         struct drm_plane *plane;
359         int ret = 0;
360
361         drm_for_each_plane(plane, dev) {
362                 if (plane->funcs->late_register)
363                         ret = plane->funcs->late_register(plane);
364                 if (ret)
365                         return ret;
366         }
367
368         return 0;
369 }
370
371 static void drm_plane_unregister_all(struct drm_device *dev)
372 {
373         struct drm_plane *plane;
374
375         drm_for_each_plane(plane, dev) {
376                 if (plane->funcs->early_unregister)
377                         plane->funcs->early_unregister(plane);
378         }
379 }
380
381 /**
382  * drm_plane_init - Initialize a legacy plane
383  * @dev: DRM device
384  * @plane: plane object to init
385  * @possible_crtcs: bitmask of possible CRTCs
386  * @funcs: callbacks for the new plane
387  * @formats: array of supported formats (DRM_FORMAT\_\*)
388  * @format_count: number of elements in @formats
389  * @is_primary: plane type (primary vs overlay)
390  *
391  * Legacy API to initialize a DRM plane.
392  *
393  * New drivers should call drm_universal_plane_init() instead.
394  *
395  * Returns:
396  * Zero on success, error code on failure.
397  */
398 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
399                    unsigned long possible_crtcs,
400                    const struct drm_plane_funcs *funcs,
401                    const uint32_t *formats, unsigned int format_count,
402                    bool is_primary)
403 {
404         enum drm_plane_type type;
405
406         type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
407         return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
408                                         formats, format_count, type, NULL);
409 }
410 EXPORT_SYMBOL(drm_plane_init);
411
412 /**
413  * drm_plane_cleanup - Clean up the core plane usage
414  * @plane: plane to cleanup
415  *
416  * This function cleans up @plane and removes it from the DRM mode setting
417  * core. Note that the function does *not* free the plane structure itself,
418  * this is the responsibility of the caller.
419  */
420 void drm_plane_cleanup(struct drm_plane *plane)
421 {
422         struct drm_device *dev = plane->dev;
423
424         drm_modeset_lock_all(dev);
425         kfree(plane->format_types);
426         drm_mode_object_unregister(dev, &plane->base);
427
428         BUG_ON(list_empty(&plane->head));
429
430         /* Note that the plane_list is considered to be static; should we
431          * remove the drm_plane at runtime we would have to decrement all
432          * the indices on the drm_plane after us in the plane_list.
433          */
434
435         list_del(&plane->head);
436         dev->mode_config.num_total_plane--;
437         if (plane->type == DRM_PLANE_TYPE_OVERLAY)
438                 dev->mode_config.num_overlay_plane--;
439         drm_modeset_unlock_all(dev);
440
441         WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
442         if (plane->state && plane->funcs->atomic_destroy_state)
443                 plane->funcs->atomic_destroy_state(plane, plane->state);
444
445         kfree(plane->name);
446
447         memset(plane, 0, sizeof(*plane));
448 }
449 EXPORT_SYMBOL(drm_plane_cleanup);
450
451 /**
452  * drm_plane_from_index - find the registered plane at an index
453  * @dev: DRM device
454  * @idx: index of registered plane to find for
455  *
456  * Given a plane index, return the registered plane from DRM device's
457  * list of planes with matching index.
458  */
459 struct drm_plane *
460 drm_plane_from_index(struct drm_device *dev, int idx)
461 {
462         struct drm_plane *plane;
463
464         drm_for_each_plane(plane, dev)
465                 if (idx == plane->index)
466                         return plane;
467
468         return NULL;
469 }
470 EXPORT_SYMBOL(drm_plane_from_index);
471
472 /**
473  * drm_plane_force_disable - Forcibly disable a plane
474  * @plane: plane to disable
475  *
476  * Forces the plane to be disabled.
477  *
478  * Used when the plane's current framebuffer is destroyed,
479  * and when restoring fbdev mode.
480  */
481 void drm_plane_force_disable(struct drm_plane *plane)
482 {
483         int ret;
484
485         if (!plane->fb)
486                 return;
487
488         plane->old_fb = plane->fb;
489         ret = plane->funcs->disable_plane(plane);
490         if (ret) {
491                 DRM_ERROR("failed to disable plane with busy fb\n");
492                 plane->old_fb = NULL;
493                 return;
494         }
495         /* disconnect the plane from the fb and crtc: */
496         drm_framebuffer_unreference(plane->old_fb);
497         plane->old_fb = NULL;
498         plane->fb = NULL;
499         plane->crtc = NULL;
500 }
501 EXPORT_SYMBOL(drm_plane_force_disable);
502
503 int drm_modeset_register_all(struct drm_device *dev)
504 {
505         int ret;
506
507         ret = drm_plane_register_all(dev);
508         if (ret)
509                 goto err_plane;
510
511         ret = drm_crtc_register_all(dev);
512         if  (ret)
513                 goto err_crtc;
514
515         ret = drm_encoder_register_all(dev);
516         if (ret)
517                 goto err_encoder;
518
519         ret = drm_connector_register_all(dev);
520         if (ret)
521                 goto err_connector;
522
523         return 0;
524
525 err_connector:
526         drm_encoder_unregister_all(dev);
527 err_encoder:
528         drm_crtc_unregister_all(dev);
529 err_crtc:
530         drm_plane_unregister_all(dev);
531 err_plane:
532         return ret;
533 }
534
535 void drm_modeset_unregister_all(struct drm_device *dev)
536 {
537         drm_connector_unregister_all(dev);
538         drm_encoder_unregister_all(dev);
539         drm_crtc_unregister_all(dev);
540         drm_plane_unregister_all(dev);
541 }
542
543 static int drm_mode_create_standard_properties(struct drm_device *dev)
544 {
545         struct drm_property *prop;
546         int ret;
547
548         ret = drm_connector_create_standard_properties(dev);
549         if (ret)
550                 return ret;
551
552         prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
553                                         "type", drm_plane_type_enum_list,
554                                         ARRAY_SIZE(drm_plane_type_enum_list));
555         if (!prop)
556                 return -ENOMEM;
557         dev->mode_config.plane_type_property = prop;
558
559         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
560                         "SRC_X", 0, UINT_MAX);
561         if (!prop)
562                 return -ENOMEM;
563         dev->mode_config.prop_src_x = prop;
564
565         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
566                         "SRC_Y", 0, UINT_MAX);
567         if (!prop)
568                 return -ENOMEM;
569         dev->mode_config.prop_src_y = prop;
570
571         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
572                         "SRC_W", 0, UINT_MAX);
573         if (!prop)
574                 return -ENOMEM;
575         dev->mode_config.prop_src_w = prop;
576
577         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
578                         "SRC_H", 0, UINT_MAX);
579         if (!prop)
580                 return -ENOMEM;
581         dev->mode_config.prop_src_h = prop;
582
583         prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
584                         "CRTC_X", INT_MIN, INT_MAX);
585         if (!prop)
586                 return -ENOMEM;
587         dev->mode_config.prop_crtc_x = prop;
588
589         prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
590                         "CRTC_Y", INT_MIN, INT_MAX);
591         if (!prop)
592                 return -ENOMEM;
593         dev->mode_config.prop_crtc_y = prop;
594
595         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
596                         "CRTC_W", 0, INT_MAX);
597         if (!prop)
598                 return -ENOMEM;
599         dev->mode_config.prop_crtc_w = prop;
600
601         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
602                         "CRTC_H", 0, INT_MAX);
603         if (!prop)
604                 return -ENOMEM;
605         dev->mode_config.prop_crtc_h = prop;
606
607         prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
608                         "FB_ID", DRM_MODE_OBJECT_FB);
609         if (!prop)
610                 return -ENOMEM;
611         dev->mode_config.prop_fb_id = prop;
612
613         prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
614                         "CRTC_ID", DRM_MODE_OBJECT_CRTC);
615         if (!prop)
616                 return -ENOMEM;
617         dev->mode_config.prop_crtc_id = prop;
618
619         prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
620                         "ACTIVE");
621         if (!prop)
622                 return -ENOMEM;
623         dev->mode_config.prop_active = prop;
624
625         prop = drm_property_create(dev,
626                         DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
627                         "MODE_ID", 0);
628         if (!prop)
629                 return -ENOMEM;
630         dev->mode_config.prop_mode_id = prop;
631
632         prop = drm_property_create(dev,
633                         DRM_MODE_PROP_BLOB,
634                         "DEGAMMA_LUT", 0);
635         if (!prop)
636                 return -ENOMEM;
637         dev->mode_config.degamma_lut_property = prop;
638
639         prop = drm_property_create_range(dev,
640                         DRM_MODE_PROP_IMMUTABLE,
641                         "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
642         if (!prop)
643                 return -ENOMEM;
644         dev->mode_config.degamma_lut_size_property = prop;
645
646         prop = drm_property_create(dev,
647                         DRM_MODE_PROP_BLOB,
648                         "CTM", 0);
649         if (!prop)
650                 return -ENOMEM;
651         dev->mode_config.ctm_property = prop;
652
653         prop = drm_property_create(dev,
654                         DRM_MODE_PROP_BLOB,
655                         "GAMMA_LUT", 0);
656         if (!prop)
657                 return -ENOMEM;
658         dev->mode_config.gamma_lut_property = prop;
659
660         prop = drm_property_create_range(dev,
661                         DRM_MODE_PROP_IMMUTABLE,
662                         "GAMMA_LUT_SIZE", 0, UINT_MAX);
663         if (!prop)
664                 return -ENOMEM;
665         dev->mode_config.gamma_lut_size_property = prop;
666
667         return 0;
668 }
669
670 /**
671  * drm_mode_getresources - get graphics configuration
672  * @dev: drm device for the ioctl
673  * @data: data pointer for the ioctl
674  * @file_priv: drm file for the ioctl call
675  *
676  * Construct a set of configuration description structures and return
677  * them to the user, including CRTC, connector and framebuffer configuration.
678  *
679  * Called by the user via ioctl.
680  *
681  * Returns:
682  * Zero on success, negative errno on failure.
683  */
684 int drm_mode_getresources(struct drm_device *dev, void *data,
685                           struct drm_file *file_priv)
686 {
687         struct drm_mode_card_res *card_res = data;
688         struct list_head *lh;
689         struct drm_framebuffer *fb;
690         struct drm_connector *connector;
691         struct drm_crtc *crtc;
692         struct drm_encoder *encoder;
693         int ret = 0;
694         int connector_count = 0;
695         int crtc_count = 0;
696         int fb_count = 0;
697         int encoder_count = 0;
698         int copied = 0;
699         uint32_t __user *fb_id;
700         uint32_t __user *crtc_id;
701         uint32_t __user *connector_id;
702         uint32_t __user *encoder_id;
703
704         if (!drm_core_check_feature(dev, DRIVER_MODESET))
705                 return -EINVAL;
706
707
708         mutex_lock(&file_priv->fbs_lock);
709         /*
710          * For the non-control nodes we need to limit the list of resources
711          * by IDs in the group list for this node
712          */
713         list_for_each(lh, &file_priv->fbs)
714                 fb_count++;
715
716         /* handle this in 4 parts */
717         /* FBs */
718         if (card_res->count_fbs >= fb_count) {
719                 copied = 0;
720                 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
721                 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
722                         if (put_user(fb->base.id, fb_id + copied)) {
723                                 mutex_unlock(&file_priv->fbs_lock);
724                                 return -EFAULT;
725                         }
726                         copied++;
727                 }
728         }
729         card_res->count_fbs = fb_count;
730         mutex_unlock(&file_priv->fbs_lock);
731
732         /* mode_config.mutex protects the connector list against e.g. DP MST
733          * connector hot-adding. CRTC/Plane lists are invariant. */
734         mutex_lock(&dev->mode_config.mutex);
735         drm_for_each_crtc(crtc, dev)
736                 crtc_count++;
737
738         drm_for_each_connector(connector, dev)
739                 connector_count++;
740
741         drm_for_each_encoder(encoder, dev)
742                 encoder_count++;
743
744         card_res->max_height = dev->mode_config.max_height;
745         card_res->min_height = dev->mode_config.min_height;
746         card_res->max_width = dev->mode_config.max_width;
747         card_res->min_width = dev->mode_config.min_width;
748
749         /* CRTCs */
750         if (card_res->count_crtcs >= crtc_count) {
751                 copied = 0;
752                 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
753                 drm_for_each_crtc(crtc, dev) {
754                         if (put_user(crtc->base.id, crtc_id + copied)) {
755                                 ret = -EFAULT;
756                                 goto out;
757                         }
758                         copied++;
759                 }
760         }
761         card_res->count_crtcs = crtc_count;
762
763         /* Encoders */
764         if (card_res->count_encoders >= encoder_count) {
765                 copied = 0;
766                 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
767                 drm_for_each_encoder(encoder, dev) {
768                         if (put_user(encoder->base.id, encoder_id +
769                                      copied)) {
770                                 ret = -EFAULT;
771                                 goto out;
772                         }
773                         copied++;
774                 }
775         }
776         card_res->count_encoders = encoder_count;
777
778         /* Connectors */
779         if (card_res->count_connectors >= connector_count) {
780                 copied = 0;
781                 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
782                 drm_for_each_connector(connector, dev) {
783                         if (put_user(connector->base.id,
784                                      connector_id + copied)) {
785                                 ret = -EFAULT;
786                                 goto out;
787                         }
788                         copied++;
789                 }
790         }
791         card_res->count_connectors = connector_count;
792
793 out:
794         mutex_unlock(&dev->mode_config.mutex);
795         return ret;
796 }
797
798 /**
799  * drm_mode_getcrtc - get CRTC configuration
800  * @dev: drm device for the ioctl
801  * @data: data pointer for the ioctl
802  * @file_priv: drm file for the ioctl call
803  *
804  * Construct a CRTC configuration structure to return to the user.
805  *
806  * Called by the user via ioctl.
807  *
808  * Returns:
809  * Zero on success, negative errno on failure.
810  */
811 int drm_mode_getcrtc(struct drm_device *dev,
812                      void *data, struct drm_file *file_priv)
813 {
814         struct drm_mode_crtc *crtc_resp = data;
815         struct drm_crtc *crtc;
816
817         if (!drm_core_check_feature(dev, DRIVER_MODESET))
818                 return -EINVAL;
819
820         crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
821         if (!crtc)
822                 return -ENOENT;
823
824         drm_modeset_lock_crtc(crtc, crtc->primary);
825         crtc_resp->gamma_size = crtc->gamma_size;
826         if (crtc->primary->fb)
827                 crtc_resp->fb_id = crtc->primary->fb->base.id;
828         else
829                 crtc_resp->fb_id = 0;
830
831         if (crtc->state) {
832                 crtc_resp->x = crtc->primary->state->src_x >> 16;
833                 crtc_resp->y = crtc->primary->state->src_y >> 16;
834                 if (crtc->state->enable) {
835                         drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
836                         crtc_resp->mode_valid = 1;
837
838                 } else {
839                         crtc_resp->mode_valid = 0;
840                 }
841         } else {
842                 crtc_resp->x = crtc->x;
843                 crtc_resp->y = crtc->y;
844                 if (crtc->enabled) {
845                         drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
846                         crtc_resp->mode_valid = 1;
847
848                 } else {
849                         crtc_resp->mode_valid = 0;
850                 }
851         }
852         drm_modeset_unlock_crtc(crtc);
853
854         return 0;
855 }
856
857 /**
858  * drm_mode_getplane_res - enumerate all plane resources
859  * @dev: DRM device
860  * @data: ioctl data
861  * @file_priv: DRM file info
862  *
863  * Construct a list of plane ids to return to the user.
864  *
865  * Called by the user via ioctl.
866  *
867  * Returns:
868  * Zero on success, negative errno on failure.
869  */
870 int drm_mode_getplane_res(struct drm_device *dev, void *data,
871                           struct drm_file *file_priv)
872 {
873         struct drm_mode_get_plane_res *plane_resp = data;
874         struct drm_mode_config *config;
875         struct drm_plane *plane;
876         uint32_t __user *plane_ptr;
877         int copied = 0;
878         unsigned num_planes;
879
880         if (!drm_core_check_feature(dev, DRIVER_MODESET))
881                 return -EINVAL;
882
883         config = &dev->mode_config;
884
885         if (file_priv->universal_planes)
886                 num_planes = config->num_total_plane;
887         else
888                 num_planes = config->num_overlay_plane;
889
890         /*
891          * This ioctl is called twice, once to determine how much space is
892          * needed, and the 2nd time to fill it.
893          */
894         if (num_planes &&
895             (plane_resp->count_planes >= num_planes)) {
896                 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
897
898                 /* Plane lists are invariant, no locking needed. */
899                 drm_for_each_plane(plane, dev) {
900                         /*
901                          * Unless userspace set the 'universal planes'
902                          * capability bit, only advertise overlays.
903                          */
904                         if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
905                             !file_priv->universal_planes)
906                                 continue;
907
908                         if (put_user(plane->base.id, plane_ptr + copied))
909                                 return -EFAULT;
910                         copied++;
911                 }
912         }
913         plane_resp->count_planes = num_planes;
914
915         return 0;
916 }
917
918 /**
919  * drm_mode_getplane - get plane configuration
920  * @dev: DRM device
921  * @data: ioctl data
922  * @file_priv: DRM file info
923  *
924  * Construct a plane configuration structure to return to the user.
925  *
926  * Called by the user via ioctl.
927  *
928  * Returns:
929  * Zero on success, negative errno on failure.
930  */
931 int drm_mode_getplane(struct drm_device *dev, void *data,
932                       struct drm_file *file_priv)
933 {
934         struct drm_mode_get_plane *plane_resp = data;
935         struct drm_plane *plane;
936         uint32_t __user *format_ptr;
937
938         if (!drm_core_check_feature(dev, DRIVER_MODESET))
939                 return -EINVAL;
940
941         plane = drm_plane_find(dev, plane_resp->plane_id);
942         if (!plane)
943                 return -ENOENT;
944
945         drm_modeset_lock(&plane->mutex, NULL);
946         if (plane->crtc)
947                 plane_resp->crtc_id = plane->crtc->base.id;
948         else
949                 plane_resp->crtc_id = 0;
950
951         if (plane->fb)
952                 plane_resp->fb_id = plane->fb->base.id;
953         else
954                 plane_resp->fb_id = 0;
955         drm_modeset_unlock(&plane->mutex);
956
957         plane_resp->plane_id = plane->base.id;
958         plane_resp->possible_crtcs = plane->possible_crtcs;
959         plane_resp->gamma_size = 0;
960
961         /*
962          * This ioctl is called twice, once to determine how much space is
963          * needed, and the 2nd time to fill it.
964          */
965         if (plane->format_count &&
966             (plane_resp->count_format_types >= plane->format_count)) {
967                 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
968                 if (copy_to_user(format_ptr,
969                                  plane->format_types,
970                                  sizeof(uint32_t) * plane->format_count)) {
971                         return -EFAULT;
972                 }
973         }
974         plane_resp->count_format_types = plane->format_count;
975
976         return 0;
977 }
978
979 /**
980  * drm_plane_check_pixel_format - Check if the plane supports the pixel format
981  * @plane: plane to check for format support
982  * @format: the pixel format
983  *
984  * Returns:
985  * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
986  * otherwise.
987  */
988 int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
989 {
990         unsigned int i;
991
992         for (i = 0; i < plane->format_count; i++) {
993                 if (format == plane->format_types[i])
994                         return 0;
995         }
996
997         return -EINVAL;
998 }
999
1000 static int check_src_coords(uint32_t src_x, uint32_t src_y,
1001                             uint32_t src_w, uint32_t src_h,
1002                             const struct drm_framebuffer *fb)
1003 {
1004         unsigned int fb_width, fb_height;
1005
1006         fb_width = fb->width << 16;
1007         fb_height = fb->height << 16;
1008
1009         /* Make sure source coordinates are inside the fb. */
1010         if (src_w > fb_width ||
1011             src_x > fb_width - src_w ||
1012             src_h > fb_height ||
1013             src_y > fb_height - src_h) {
1014                 DRM_DEBUG_KMS("Invalid source coordinates "
1015                               "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1016                               src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
1017                               src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
1018                               src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
1019                               src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
1020                 return -ENOSPC;
1021         }
1022
1023         return 0;
1024 }
1025
1026 /*
1027  * setplane_internal - setplane handler for internal callers
1028  *
1029  * Note that we assume an extra reference has already been taken on fb.  If the
1030  * update fails, this reference will be dropped before return; if it succeeds,
1031  * the previous framebuffer (if any) will be unreferenced instead.
1032  *
1033  * src_{x,y,w,h} are provided in 16.16 fixed point format
1034  */
1035 static int __setplane_internal(struct drm_plane *plane,
1036                                struct drm_crtc *crtc,
1037                                struct drm_framebuffer *fb,
1038                                int32_t crtc_x, int32_t crtc_y,
1039                                uint32_t crtc_w, uint32_t crtc_h,
1040                                /* src_{x,y,w,h} values are 16.16 fixed point */
1041                                uint32_t src_x, uint32_t src_y,
1042                                uint32_t src_w, uint32_t src_h)
1043 {
1044         int ret = 0;
1045
1046         /* No fb means shut it down */
1047         if (!fb) {
1048                 plane->old_fb = plane->fb;
1049                 ret = plane->funcs->disable_plane(plane);
1050                 if (!ret) {
1051                         plane->crtc = NULL;
1052                         plane->fb = NULL;
1053                 } else {
1054                         plane->old_fb = NULL;
1055                 }
1056                 goto out;
1057         }
1058
1059         /* Check whether this plane is usable on this CRTC */
1060         if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
1061                 DRM_DEBUG_KMS("Invalid crtc for plane\n");
1062                 ret = -EINVAL;
1063                 goto out;
1064         }
1065
1066         /* Check whether this plane supports the fb pixel format. */
1067         ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
1068         if (ret) {
1069                 char *format_name = drm_get_format_name(fb->pixel_format);
1070                 DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
1071                 kfree(format_name);
1072                 goto out;
1073         }
1074
1075         /* Give drivers some help against integer overflows */
1076         if (crtc_w > INT_MAX ||
1077             crtc_x > INT_MAX - (int32_t) crtc_w ||
1078             crtc_h > INT_MAX ||
1079             crtc_y > INT_MAX - (int32_t) crtc_h) {
1080                 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1081                               crtc_w, crtc_h, crtc_x, crtc_y);
1082                 ret = -ERANGE;
1083                 goto out;
1084         }
1085
1086         ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
1087         if (ret)
1088                 goto out;
1089
1090         plane->old_fb = plane->fb;
1091         ret = plane->funcs->update_plane(plane, crtc, fb,
1092                                          crtc_x, crtc_y, crtc_w, crtc_h,
1093                                          src_x, src_y, src_w, src_h);
1094         if (!ret) {
1095                 plane->crtc = crtc;
1096                 plane->fb = fb;
1097                 fb = NULL;
1098         } else {
1099                 plane->old_fb = NULL;
1100         }
1101
1102 out:
1103         if (fb)
1104                 drm_framebuffer_unreference(fb);
1105         if (plane->old_fb)
1106                 drm_framebuffer_unreference(plane->old_fb);
1107         plane->old_fb = NULL;
1108
1109         return ret;
1110 }
1111
1112 static int setplane_internal(struct drm_plane *plane,
1113                              struct drm_crtc *crtc,
1114                              struct drm_framebuffer *fb,
1115                              int32_t crtc_x, int32_t crtc_y,
1116                              uint32_t crtc_w, uint32_t crtc_h,
1117                              /* src_{x,y,w,h} values are 16.16 fixed point */
1118                              uint32_t src_x, uint32_t src_y,
1119                              uint32_t src_w, uint32_t src_h)
1120 {
1121         int ret;
1122
1123         drm_modeset_lock_all(plane->dev);
1124         ret = __setplane_internal(plane, crtc, fb,
1125                                   crtc_x, crtc_y, crtc_w, crtc_h,
1126                                   src_x, src_y, src_w, src_h);
1127         drm_modeset_unlock_all(plane->dev);
1128
1129         return ret;
1130 }
1131
1132 /**
1133  * drm_mode_setplane - configure a plane's configuration
1134  * @dev: DRM device
1135  * @data: ioctl data*
1136  * @file_priv: DRM file info
1137  *
1138  * Set plane configuration, including placement, fb, scaling, and other factors.
1139  * Or pass a NULL fb to disable (planes may be disabled without providing a
1140  * valid crtc).
1141  *
1142  * Returns:
1143  * Zero on success, negative errno on failure.
1144  */
1145 int drm_mode_setplane(struct drm_device *dev, void *data,
1146                       struct drm_file *file_priv)
1147 {
1148         struct drm_mode_set_plane *plane_req = data;
1149         struct drm_plane *plane;
1150         struct drm_crtc *crtc = NULL;
1151         struct drm_framebuffer *fb = NULL;
1152
1153         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1154                 return -EINVAL;
1155
1156         /*
1157          * First, find the plane, crtc, and fb objects.  If not available,
1158          * we don't bother to call the driver.
1159          */
1160         plane = drm_plane_find(dev, plane_req->plane_id);
1161         if (!plane) {
1162                 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1163                               plane_req->plane_id);
1164                 return -ENOENT;
1165         }
1166
1167         if (plane_req->fb_id) {
1168                 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1169                 if (!fb) {
1170                         DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1171                                       plane_req->fb_id);
1172                         return -ENOENT;
1173                 }
1174
1175                 crtc = drm_crtc_find(dev, plane_req->crtc_id);
1176                 if (!crtc) {
1177                         DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1178                                       plane_req->crtc_id);
1179                         return -ENOENT;
1180                 }
1181         }
1182
1183         /*
1184          * setplane_internal will take care of deref'ing either the old or new
1185          * framebuffer depending on success.
1186          */
1187         return setplane_internal(plane, crtc, fb,
1188                                  plane_req->crtc_x, plane_req->crtc_y,
1189                                  plane_req->crtc_w, plane_req->crtc_h,
1190                                  plane_req->src_x, plane_req->src_y,
1191                                  plane_req->src_w, plane_req->src_h);
1192 }
1193
1194 /**
1195  * drm_mode_set_config_internal - helper to call ->set_config
1196  * @set: modeset config to set
1197  *
1198  * This is a little helper to wrap internal calls to the ->set_config driver
1199  * interface. The only thing it adds is correct refcounting dance.
1200  *
1201  * Returns:
1202  * Zero on success, negative errno on failure.
1203  */
1204 int drm_mode_set_config_internal(struct drm_mode_set *set)
1205 {
1206         struct drm_crtc *crtc = set->crtc;
1207         struct drm_framebuffer *fb;
1208         struct drm_crtc *tmp;
1209         int ret;
1210
1211         /*
1212          * NOTE: ->set_config can also disable other crtcs (if we steal all
1213          * connectors from it), hence we need to refcount the fbs across all
1214          * crtcs. Atomic modeset will have saner semantics ...
1215          */
1216         drm_for_each_crtc(tmp, crtc->dev)
1217                 tmp->primary->old_fb = tmp->primary->fb;
1218
1219         fb = set->fb;
1220
1221         ret = crtc->funcs->set_config(set);
1222         if (ret == 0) {
1223                 crtc->primary->crtc = crtc;
1224                 crtc->primary->fb = fb;
1225         }
1226
1227         drm_for_each_crtc(tmp, crtc->dev) {
1228                 if (tmp->primary->fb)
1229                         drm_framebuffer_reference(tmp->primary->fb);
1230                 if (tmp->primary->old_fb)
1231                         drm_framebuffer_unreference(tmp->primary->old_fb);
1232                 tmp->primary->old_fb = NULL;
1233         }
1234
1235         return ret;
1236 }
1237 EXPORT_SYMBOL(drm_mode_set_config_internal);
1238
1239 /**
1240  * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
1241  * @mode: mode to query
1242  * @hdisplay: hdisplay value to fill in
1243  * @vdisplay: vdisplay value to fill in
1244  *
1245  * The vdisplay value will be doubled if the specified mode is a stereo mode of
1246  * the appropriate layout.
1247  */
1248 void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
1249                             int *hdisplay, int *vdisplay)
1250 {
1251         struct drm_display_mode adjusted;
1252
1253         drm_mode_copy(&adjusted, mode);
1254         drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
1255         *hdisplay = adjusted.crtc_hdisplay;
1256         *vdisplay = adjusted.crtc_vdisplay;
1257 }
1258 EXPORT_SYMBOL(drm_crtc_get_hv_timing);
1259
1260 /**
1261  * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
1262  *     CRTC viewport
1263  * @crtc: CRTC that framebuffer will be displayed on
1264  * @x: x panning
1265  * @y: y panning
1266  * @mode: mode that framebuffer will be displayed under
1267  * @fb: framebuffer to check size of
1268  */
1269 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
1270                             int x, int y,
1271                             const struct drm_display_mode *mode,
1272                             const struct drm_framebuffer *fb)
1273
1274 {
1275         int hdisplay, vdisplay;
1276
1277         drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
1278
1279         if (crtc->state &&
1280             crtc->primary->state->rotation & (DRM_ROTATE_90 |
1281                                               DRM_ROTATE_270))
1282                 swap(hdisplay, vdisplay);
1283
1284         return check_src_coords(x << 16, y << 16,
1285                                 hdisplay << 16, vdisplay << 16, fb);
1286 }
1287 EXPORT_SYMBOL(drm_crtc_check_viewport);
1288
1289 /**
1290  * drm_mode_setcrtc - set CRTC configuration
1291  * @dev: drm device for the ioctl
1292  * @data: data pointer for the ioctl
1293  * @file_priv: drm file for the ioctl call
1294  *
1295  * Build a new CRTC configuration based on user request.
1296  *
1297  * Called by the user via ioctl.
1298  *
1299  * Returns:
1300  * Zero on success, negative errno on failure.
1301  */
1302 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1303                      struct drm_file *file_priv)
1304 {
1305         struct drm_mode_config *config = &dev->mode_config;
1306         struct drm_mode_crtc *crtc_req = data;
1307         struct drm_crtc *crtc;
1308         struct drm_connector **connector_set = NULL, *connector;
1309         struct drm_framebuffer *fb = NULL;
1310         struct drm_display_mode *mode = NULL;
1311         struct drm_mode_set set;
1312         uint32_t __user *set_connectors_ptr;
1313         int ret;
1314         int i;
1315
1316         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1317                 return -EINVAL;
1318
1319         /*
1320          * Universal plane src offsets are only 16.16, prevent havoc for
1321          * drivers using universal plane code internally.
1322          */
1323         if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
1324                 return -ERANGE;
1325
1326         drm_modeset_lock_all(dev);
1327         crtc = drm_crtc_find(dev, crtc_req->crtc_id);
1328         if (!crtc) {
1329                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1330                 ret = -ENOENT;
1331                 goto out;
1332         }
1333         DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
1334
1335         if (crtc_req->mode_valid) {
1336                 /* If we have a mode we need a framebuffer. */
1337                 /* If we pass -1, set the mode with the currently bound fb */
1338                 if (crtc_req->fb_id == -1) {
1339                         if (!crtc->primary->fb) {
1340                                 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1341                                 ret = -EINVAL;
1342                                 goto out;
1343                         }
1344                         fb = crtc->primary->fb;
1345                         /* Make refcounting symmetric with the lookup path. */
1346                         drm_framebuffer_reference(fb);
1347                 } else {
1348                         fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
1349                         if (!fb) {
1350                                 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1351                                                 crtc_req->fb_id);
1352                                 ret = -ENOENT;
1353                                 goto out;
1354                         }
1355                 }
1356
1357                 mode = drm_mode_create(dev);
1358                 if (!mode) {
1359                         ret = -ENOMEM;
1360                         goto out;
1361                 }
1362
1363                 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
1364                 if (ret) {
1365                         DRM_DEBUG_KMS("Invalid mode\n");
1366                         goto out;
1367                 }
1368
1369                 /*
1370                  * Check whether the primary plane supports the fb pixel format.
1371                  * Drivers not implementing the universal planes API use a
1372                  * default formats list provided by the DRM core which doesn't
1373                  * match real hardware capabilities. Skip the check in that
1374                  * case.
1375                  */
1376                 if (!crtc->primary->format_default) {
1377                         ret = drm_plane_check_pixel_format(crtc->primary,
1378                                                            fb->pixel_format);
1379                         if (ret) {
1380                                 char *format_name = drm_get_format_name(fb->pixel_format);
1381                                 DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
1382                                 kfree(format_name);
1383                                 goto out;
1384                         }
1385                 }
1386
1387                 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
1388                                               mode, fb);
1389                 if (ret)
1390                         goto out;
1391
1392         }
1393
1394         if (crtc_req->count_connectors == 0 && mode) {
1395                 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1396                 ret = -EINVAL;
1397                 goto out;
1398         }
1399
1400         if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
1401                 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1402                           crtc_req->count_connectors);
1403                 ret = -EINVAL;
1404                 goto out;
1405         }
1406
1407         if (crtc_req->count_connectors > 0) {
1408                 u32 out_id;
1409
1410                 /* Avoid unbounded kernel memory allocation */
1411                 if (crtc_req->count_connectors > config->num_connector) {
1412                         ret = -EINVAL;
1413                         goto out;
1414                 }
1415
1416                 connector_set = kmalloc_array(crtc_req->count_connectors,
1417                                               sizeof(struct drm_connector *),
1418                                               GFP_KERNEL);
1419                 if (!connector_set) {
1420                         ret = -ENOMEM;
1421                         goto out;
1422                 }
1423
1424                 for (i = 0; i < crtc_req->count_connectors; i++) {
1425                         connector_set[i] = NULL;
1426                         set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
1427                         if (get_user(out_id, &set_connectors_ptr[i])) {
1428                                 ret = -EFAULT;
1429                                 goto out;
1430                         }
1431
1432                         connector = drm_connector_lookup(dev, out_id);
1433                         if (!connector) {
1434                                 DRM_DEBUG_KMS("Connector id %d unknown\n",
1435                                                 out_id);
1436                                 ret = -ENOENT;
1437                                 goto out;
1438                         }
1439                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1440                                         connector->base.id,
1441                                         connector->name);
1442
1443                         connector_set[i] = connector;
1444                 }
1445         }
1446
1447         set.crtc = crtc;
1448         set.x = crtc_req->x;
1449         set.y = crtc_req->y;
1450         set.mode = mode;
1451         set.connectors = connector_set;
1452         set.num_connectors = crtc_req->count_connectors;
1453         set.fb = fb;
1454         ret = drm_mode_set_config_internal(&set);
1455
1456 out:
1457         if (fb)
1458                 drm_framebuffer_unreference(fb);
1459
1460         if (connector_set) {
1461                 for (i = 0; i < crtc_req->count_connectors; i++) {
1462                         if (connector_set[i])
1463                                 drm_connector_unreference(connector_set[i]);
1464                 }
1465         }
1466         kfree(connector_set);
1467         drm_mode_destroy(dev, mode);
1468         drm_modeset_unlock_all(dev);
1469         return ret;
1470 }
1471
1472 /**
1473  * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
1474  *     universal plane handler call
1475  * @crtc: crtc to update cursor for
1476  * @req: data pointer for the ioctl
1477  * @file_priv: drm file for the ioctl call
1478  *
1479  * Legacy cursor ioctl's work directly with driver buffer handles.  To
1480  * translate legacy ioctl calls into universal plane handler calls, we need to
1481  * wrap the native buffer handle in a drm_framebuffer.
1482  *
1483  * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
1484  * buffer with a pitch of 4*width; the universal plane interface should be used
1485  * directly in cases where the hardware can support other buffer settings and
1486  * userspace wants to make use of these capabilities.
1487  *
1488  * Returns:
1489  * Zero on success, negative errno on failure.
1490  */
1491 static int drm_mode_cursor_universal(struct drm_crtc *crtc,
1492                                      struct drm_mode_cursor2 *req,
1493                                      struct drm_file *file_priv)
1494 {
1495         struct drm_device *dev = crtc->dev;
1496         struct drm_framebuffer *fb = NULL;
1497         struct drm_mode_fb_cmd2 fbreq = {
1498                 .width = req->width,
1499                 .height = req->height,
1500                 .pixel_format = DRM_FORMAT_ARGB8888,
1501                 .pitches = { req->width * 4 },
1502                 .handles = { req->handle },
1503         };
1504         int32_t crtc_x, crtc_y;
1505         uint32_t crtc_w = 0, crtc_h = 0;
1506         uint32_t src_w = 0, src_h = 0;
1507         int ret = 0;
1508
1509         BUG_ON(!crtc->cursor);
1510         WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
1511
1512         /*
1513          * Obtain fb we'll be using (either new or existing) and take an extra
1514          * reference to it if fb != null.  setplane will take care of dropping
1515          * the reference if the plane update fails.
1516          */
1517         if (req->flags & DRM_MODE_CURSOR_BO) {
1518                 if (req->handle) {
1519                         fb = drm_internal_framebuffer_create(dev, &fbreq, file_priv);
1520                         if (IS_ERR(fb)) {
1521                                 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
1522                                 return PTR_ERR(fb);
1523                         }
1524                         fb->hot_x = req->hot_x;
1525                         fb->hot_y = req->hot_y;
1526                 } else {
1527                         fb = NULL;
1528                 }
1529         } else {
1530                 fb = crtc->cursor->fb;
1531                 if (fb)
1532                         drm_framebuffer_reference(fb);
1533         }
1534
1535         if (req->flags & DRM_MODE_CURSOR_MOVE) {
1536                 crtc_x = req->x;
1537                 crtc_y = req->y;
1538         } else {
1539                 crtc_x = crtc->cursor_x;
1540                 crtc_y = crtc->cursor_y;
1541         }
1542
1543         if (fb) {
1544                 crtc_w = fb->width;
1545                 crtc_h = fb->height;
1546                 src_w = fb->width << 16;
1547                 src_h = fb->height << 16;
1548         }
1549
1550         /*
1551          * setplane_internal will take care of deref'ing either the old or new
1552          * framebuffer depending on success.
1553          */
1554         ret = __setplane_internal(crtc->cursor, crtc, fb,
1555                                 crtc_x, crtc_y, crtc_w, crtc_h,
1556                                 0, 0, src_w, src_h);
1557
1558         /* Update successful; save new cursor position, if necessary */
1559         if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
1560                 crtc->cursor_x = req->x;
1561                 crtc->cursor_y = req->y;
1562         }
1563
1564         return ret;
1565 }
1566
1567 static int drm_mode_cursor_common(struct drm_device *dev,
1568                                   struct drm_mode_cursor2 *req,
1569                                   struct drm_file *file_priv)
1570 {
1571         struct drm_crtc *crtc;
1572         int ret = 0;
1573
1574         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1575                 return -EINVAL;
1576
1577         if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
1578                 return -EINVAL;
1579
1580         crtc = drm_crtc_find(dev, req->crtc_id);
1581         if (!crtc) {
1582                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
1583                 return -ENOENT;
1584         }
1585
1586         /*
1587          * If this crtc has a universal cursor plane, call that plane's update
1588          * handler rather than using legacy cursor handlers.
1589          */
1590         drm_modeset_lock_crtc(crtc, crtc->cursor);
1591         if (crtc->cursor) {
1592                 ret = drm_mode_cursor_universal(crtc, req, file_priv);
1593                 goto out;
1594         }
1595
1596         if (req->flags & DRM_MODE_CURSOR_BO) {
1597                 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
1598                         ret = -ENXIO;
1599                         goto out;
1600                 }
1601                 /* Turns off the cursor if handle is 0 */
1602                 if (crtc->funcs->cursor_set2)
1603                         ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
1604                                                       req->width, req->height, req->hot_x, req->hot_y);
1605                 else
1606                         ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1607                                                       req->width, req->height);
1608         }
1609
1610         if (req->flags & DRM_MODE_CURSOR_MOVE) {
1611                 if (crtc->funcs->cursor_move) {
1612                         ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1613                 } else {
1614                         ret = -EFAULT;
1615                         goto out;
1616                 }
1617         }
1618 out:
1619         drm_modeset_unlock_crtc(crtc);
1620
1621         return ret;
1622
1623 }
1624
1625
1626 /**
1627  * drm_mode_cursor_ioctl - set CRTC's cursor configuration
1628  * @dev: drm device for the ioctl
1629  * @data: data pointer for the ioctl
1630  * @file_priv: drm file for the ioctl call
1631  *
1632  * Set the cursor configuration based on user request.
1633  *
1634  * Called by the user via ioctl.
1635  *
1636  * Returns:
1637  * Zero on success, negative errno on failure.
1638  */
1639 int drm_mode_cursor_ioctl(struct drm_device *dev,
1640                           void *data, struct drm_file *file_priv)
1641 {
1642         struct drm_mode_cursor *req = data;
1643         struct drm_mode_cursor2 new_req;
1644
1645         memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
1646         new_req.hot_x = new_req.hot_y = 0;
1647
1648         return drm_mode_cursor_common(dev, &new_req, file_priv);
1649 }
1650
1651 /**
1652  * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
1653  * @dev: drm device for the ioctl
1654  * @data: data pointer for the ioctl
1655  * @file_priv: drm file for the ioctl call
1656  *
1657  * Set the cursor configuration based on user request. This implements the 2nd
1658  * version of the cursor ioctl, which allows userspace to additionally specify
1659  * the hotspot of the pointer.
1660  *
1661  * Called by the user via ioctl.
1662  *
1663  * Returns:
1664  * Zero on success, negative errno on failure.
1665  */
1666 int drm_mode_cursor2_ioctl(struct drm_device *dev,
1667                            void *data, struct drm_file *file_priv)
1668 {
1669         struct drm_mode_cursor2 *req = data;
1670
1671         return drm_mode_cursor_common(dev, req, file_priv);
1672 }
1673
1674 int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
1675                                struct drm_property *property,
1676                                uint64_t value)
1677 {
1678         int ret = -EINVAL;
1679         struct drm_crtc *crtc = obj_to_crtc(obj);
1680
1681         if (crtc->funcs->set_property)
1682                 ret = crtc->funcs->set_property(crtc, property, value);
1683         if (!ret)
1684                 drm_object_property_set_value(obj, property, value);
1685
1686         return ret;
1687 }
1688
1689 /**
1690  * drm_mode_plane_set_obj_prop - set the value of a property
1691  * @plane: drm plane object to set property value for
1692  * @property: property to set
1693  * @value: value the property should be set to
1694  *
1695  * This functions sets a given property on a given plane object. This function
1696  * calls the driver's ->set_property callback and changes the software state of
1697  * the property if the callback succeeds.
1698  *
1699  * Returns:
1700  * Zero on success, error code on failure.
1701  */
1702 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
1703                                 struct drm_property *property,
1704                                 uint64_t value)
1705 {
1706         int ret = -EINVAL;
1707         struct drm_mode_object *obj = &plane->base;
1708
1709         if (plane->funcs->set_property)
1710                 ret = plane->funcs->set_property(plane, property, value);
1711         if (!ret)
1712                 drm_object_property_set_value(obj, property, value);
1713
1714         return ret;
1715 }
1716 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
1717
1718 /**
1719  * drm_mode_crtc_set_gamma_size - set the gamma table size
1720  * @crtc: CRTC to set the gamma table size for
1721  * @gamma_size: size of the gamma table
1722  *
1723  * Drivers which support gamma tables should set this to the supported gamma
1724  * table size when initializing the CRTC. Currently the drm core only supports a
1725  * fixed gamma table size.
1726  *
1727  * Returns:
1728  * Zero on success, negative errno on failure.
1729  */
1730 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
1731                                  int gamma_size)
1732 {
1733         uint16_t *r_base, *g_base, *b_base;
1734         int i;
1735
1736         crtc->gamma_size = gamma_size;
1737
1738         crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
1739                                     GFP_KERNEL);
1740         if (!crtc->gamma_store) {
1741                 crtc->gamma_size = 0;
1742                 return -ENOMEM;
1743         }
1744
1745         r_base = crtc->gamma_store;
1746         g_base = r_base + gamma_size;
1747         b_base = g_base + gamma_size;
1748         for (i = 0; i < gamma_size; i++) {
1749                 r_base[i] = i << 8;
1750                 g_base[i] = i << 8;
1751                 b_base[i] = i << 8;
1752         }
1753
1754
1755         return 0;
1756 }
1757 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
1758
1759 /**
1760  * drm_mode_gamma_set_ioctl - set the gamma table
1761  * @dev: DRM device
1762  * @data: ioctl data
1763  * @file_priv: DRM file info
1764  *
1765  * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
1766  * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
1767  *
1768  * Called by the user via ioctl.
1769  *
1770  * Returns:
1771  * Zero on success, negative errno on failure.
1772  */
1773 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
1774                              void *data, struct drm_file *file_priv)
1775 {
1776         struct drm_mode_crtc_lut *crtc_lut = data;
1777         struct drm_crtc *crtc;
1778         void *r_base, *g_base, *b_base;
1779         int size;
1780         int ret = 0;
1781
1782         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1783                 return -EINVAL;
1784
1785         drm_modeset_lock_all(dev);
1786         crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
1787         if (!crtc) {
1788                 ret = -ENOENT;
1789                 goto out;
1790         }
1791
1792         if (crtc->funcs->gamma_set == NULL) {
1793                 ret = -ENOSYS;
1794                 goto out;
1795         }
1796
1797         /* memcpy into gamma store */
1798         if (crtc_lut->gamma_size != crtc->gamma_size) {
1799                 ret = -EINVAL;
1800                 goto out;
1801         }
1802
1803         size = crtc_lut->gamma_size * (sizeof(uint16_t));
1804         r_base = crtc->gamma_store;
1805         if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
1806                 ret = -EFAULT;
1807                 goto out;
1808         }
1809
1810         g_base = r_base + size;
1811         if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
1812                 ret = -EFAULT;
1813                 goto out;
1814         }
1815
1816         b_base = g_base + size;
1817         if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
1818                 ret = -EFAULT;
1819                 goto out;
1820         }
1821
1822         ret = crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
1823
1824 out:
1825         drm_modeset_unlock_all(dev);
1826         return ret;
1827
1828 }
1829
1830 /**
1831  * drm_mode_gamma_get_ioctl - get the gamma table
1832  * @dev: DRM device
1833  * @data: ioctl data
1834  * @file_priv: DRM file info
1835  *
1836  * Copy the current gamma table into the storage provided. This also provides
1837  * the gamma table size the driver expects, which can be used to size the
1838  * allocated storage.
1839  *
1840  * Called by the user via ioctl.
1841  *
1842  * Returns:
1843  * Zero on success, negative errno on failure.
1844  */
1845 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
1846                              void *data, struct drm_file *file_priv)
1847 {
1848         struct drm_mode_crtc_lut *crtc_lut = data;
1849         struct drm_crtc *crtc;
1850         void *r_base, *g_base, *b_base;
1851         int size;
1852         int ret = 0;
1853
1854         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1855                 return -EINVAL;
1856
1857         drm_modeset_lock_all(dev);
1858         crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
1859         if (!crtc) {
1860                 ret = -ENOENT;
1861                 goto out;
1862         }
1863
1864         /* memcpy into gamma store */
1865         if (crtc_lut->gamma_size != crtc->gamma_size) {
1866                 ret = -EINVAL;
1867                 goto out;
1868         }
1869
1870         size = crtc_lut->gamma_size * (sizeof(uint16_t));
1871         r_base = crtc->gamma_store;
1872         if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
1873                 ret = -EFAULT;
1874                 goto out;
1875         }
1876
1877         g_base = r_base + size;
1878         if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
1879                 ret = -EFAULT;
1880                 goto out;
1881         }
1882
1883         b_base = g_base + size;
1884         if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
1885                 ret = -EFAULT;
1886                 goto out;
1887         }
1888 out:
1889         drm_modeset_unlock_all(dev);
1890         return ret;
1891 }
1892
1893 /**
1894  * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
1895  * @dev: DRM device
1896  * @data: ioctl data
1897  * @file_priv: DRM file info
1898  *
1899  * This schedules an asynchronous update on a given CRTC, called page flip.
1900  * Optionally a drm event is generated to signal the completion of the event.
1901  * Generic drivers cannot assume that a pageflip with changed framebuffer
1902  * properties (including driver specific metadata like tiling layout) will work,
1903  * but some drivers support e.g. pixel format changes through the pageflip
1904  * ioctl.
1905  *
1906  * Called by the user via ioctl.
1907  *
1908  * Returns:
1909  * Zero on success, negative errno on failure.
1910  */
1911 int drm_mode_page_flip_ioctl(struct drm_device *dev,
1912                              void *data, struct drm_file *file_priv)
1913 {
1914         struct drm_mode_crtc_page_flip_target *page_flip = data;
1915         struct drm_crtc *crtc;
1916         struct drm_framebuffer *fb = NULL;
1917         struct drm_pending_vblank_event *e = NULL;
1918         u32 target_vblank = page_flip->sequence;
1919         int ret = -EINVAL;
1920
1921         if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS)
1922                 return -EINVAL;
1923
1924         if (page_flip->sequence != 0 && !(page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET))
1925                 return -EINVAL;
1926
1927         /* Only one of the DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE/RELATIVE flags
1928          * can be specified
1929          */
1930         if ((page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) == DRM_MODE_PAGE_FLIP_TARGET)
1931                 return -EINVAL;
1932
1933         if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
1934                 return -EINVAL;
1935
1936         crtc = drm_crtc_find(dev, page_flip->crtc_id);
1937         if (!crtc)
1938                 return -ENOENT;
1939
1940         if (crtc->funcs->page_flip_target) {
1941                 u32 current_vblank;
1942                 int r;
1943
1944                 r = drm_crtc_vblank_get(crtc);
1945                 if (r)
1946                         return r;
1947
1948                 current_vblank = drm_crtc_vblank_count(crtc);
1949
1950                 switch (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) {
1951                 case DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE:
1952                         if ((int)(target_vblank - current_vblank) > 1) {
1953                                 DRM_DEBUG("Invalid absolute flip target %u, "
1954                                           "must be <= %u\n", target_vblank,
1955                                           current_vblank + 1);
1956                                 drm_crtc_vblank_put(crtc);
1957                                 return -EINVAL;
1958                         }
1959                         break;
1960                 case DRM_MODE_PAGE_FLIP_TARGET_RELATIVE:
1961                         if (target_vblank != 0 && target_vblank != 1) {
1962                                 DRM_DEBUG("Invalid relative flip target %u, "
1963                                           "must be 0 or 1\n", target_vblank);
1964                                 drm_crtc_vblank_put(crtc);
1965                                 return -EINVAL;
1966                         }
1967                         target_vblank += current_vblank;
1968                         break;
1969                 default:
1970                         target_vblank = current_vblank +
1971                                 !(page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC);
1972                         break;
1973                 }
1974         } else if (crtc->funcs->page_flip == NULL ||
1975                    (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET)) {
1976                 return -EINVAL;
1977         }
1978
1979         drm_modeset_lock_crtc(crtc, crtc->primary);
1980         if (crtc->primary->fb == NULL) {
1981                 /* The framebuffer is currently unbound, presumably
1982                  * due to a hotplug event, that userspace has not
1983                  * yet discovered.
1984                  */
1985                 ret = -EBUSY;
1986                 goto out;
1987         }
1988
1989         fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
1990         if (!fb) {
1991                 ret = -ENOENT;
1992                 goto out;
1993         }
1994
1995         if (crtc->state) {
1996                 const struct drm_plane_state *state = crtc->primary->state;
1997
1998                 ret = check_src_coords(state->src_x, state->src_y,
1999                                        state->src_w, state->src_h, fb);
2000         } else {
2001                 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
2002         }
2003         if (ret)
2004                 goto out;
2005
2006         if (crtc->primary->fb->pixel_format != fb->pixel_format) {
2007                 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
2008                 ret = -EINVAL;
2009                 goto out;
2010         }
2011
2012         if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
2013                 e = kzalloc(sizeof *e, GFP_KERNEL);
2014                 if (!e) {
2015                         ret = -ENOMEM;
2016                         goto out;
2017                 }
2018                 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
2019                 e->event.base.length = sizeof(e->event);
2020                 e->event.user_data = page_flip->user_data;
2021                 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
2022                 if (ret) {
2023                         kfree(e);
2024                         goto out;
2025                 }
2026         }
2027
2028         crtc->primary->old_fb = crtc->primary->fb;
2029         if (crtc->funcs->page_flip_target)
2030                 ret = crtc->funcs->page_flip_target(crtc, fb, e,
2031                                                     page_flip->flags,
2032                                                     target_vblank);
2033         else
2034                 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
2035         if (ret) {
2036                 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
2037                         drm_event_cancel_free(dev, &e->base);
2038                 /* Keep the old fb, don't unref it. */
2039                 crtc->primary->old_fb = NULL;
2040         } else {
2041                 crtc->primary->fb = fb;
2042                 /* Unref only the old framebuffer. */
2043                 fb = NULL;
2044         }
2045
2046 out:
2047         if (ret && crtc->funcs->page_flip_target)
2048                 drm_crtc_vblank_put(crtc);
2049         if (fb)
2050                 drm_framebuffer_unreference(fb);
2051         if (crtc->primary->old_fb)
2052                 drm_framebuffer_unreference(crtc->primary->old_fb);
2053         crtc->primary->old_fb = NULL;
2054         drm_modeset_unlock_crtc(crtc);
2055
2056         return ret;
2057 }
2058
2059 /**
2060  * drm_mode_config_reset - call ->reset callbacks
2061  * @dev: drm device
2062  *
2063  * This functions calls all the crtc's, encoder's and connector's ->reset
2064  * callback. Drivers can use this in e.g. their driver load or resume code to
2065  * reset hardware and software state.
2066  */
2067 void drm_mode_config_reset(struct drm_device *dev)
2068 {
2069         struct drm_crtc *crtc;
2070         struct drm_plane *plane;
2071         struct drm_encoder *encoder;
2072         struct drm_connector *connector;
2073
2074         drm_for_each_plane(plane, dev)
2075                 if (plane->funcs->reset)
2076                         plane->funcs->reset(plane);
2077
2078         drm_for_each_crtc(crtc, dev)
2079                 if (crtc->funcs->reset)
2080                         crtc->funcs->reset(crtc);
2081
2082         drm_for_each_encoder(encoder, dev)
2083                 if (encoder->funcs->reset)
2084                         encoder->funcs->reset(encoder);
2085
2086         mutex_lock(&dev->mode_config.mutex);
2087         drm_for_each_connector(connector, dev)
2088                 if (connector->funcs->reset)
2089                         connector->funcs->reset(connector);
2090         mutex_unlock(&dev->mode_config.mutex);
2091 }
2092 EXPORT_SYMBOL(drm_mode_config_reset);
2093
2094 /**
2095  * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
2096  * @dev: DRM device
2097  * @data: ioctl data
2098  * @file_priv: DRM file info
2099  *
2100  * This creates a new dumb buffer in the driver's backing storage manager (GEM,
2101  * TTM or something else entirely) and returns the resulting buffer handle. This
2102  * handle can then be wrapped up into a framebuffer modeset object.
2103  *
2104  * Note that userspace is not allowed to use such objects for render
2105  * acceleration - drivers must create their own private ioctls for such a use
2106  * case.
2107  *
2108  * Called by the user via ioctl.
2109  *
2110  * Returns:
2111  * Zero on success, negative errno on failure.
2112  */
2113 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
2114                                void *data, struct drm_file *file_priv)
2115 {
2116         struct drm_mode_create_dumb *args = data;
2117         u32 cpp, stride, size;
2118
2119         if (!dev->driver->dumb_create)
2120                 return -ENOSYS;
2121         if (!args->width || !args->height || !args->bpp)
2122                 return -EINVAL;
2123
2124         /* overflow checks for 32bit size calculations */
2125         /* NOTE: DIV_ROUND_UP() can overflow */
2126         cpp = DIV_ROUND_UP(args->bpp, 8);
2127         if (!cpp || cpp > 0xffffffffU / args->width)
2128                 return -EINVAL;
2129         stride = cpp * args->width;
2130         if (args->height > 0xffffffffU / stride)
2131                 return -EINVAL;
2132
2133         /* test for wrap-around */
2134         size = args->height * stride;
2135         if (PAGE_ALIGN(size) == 0)
2136                 return -EINVAL;
2137
2138         /*
2139          * handle, pitch and size are output parameters. Zero them out to
2140          * prevent drivers from accidentally using uninitialized data. Since
2141          * not all existing userspace is clearing these fields properly we
2142          * cannot reject IOCTL with garbage in them.
2143          */
2144         args->handle = 0;
2145         args->pitch = 0;
2146         args->size = 0;
2147
2148         return dev->driver->dumb_create(file_priv, dev, args);
2149 }
2150
2151 /**
2152  * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
2153  * @dev: DRM device
2154  * @data: ioctl data
2155  * @file_priv: DRM file info
2156  *
2157  * Allocate an offset in the drm device node's address space to be able to
2158  * memory map a dumb buffer.
2159  *
2160  * Called by the user via ioctl.
2161  *
2162  * Returns:
2163  * Zero on success, negative errno on failure.
2164  */
2165 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
2166                              void *data, struct drm_file *file_priv)
2167 {
2168         struct drm_mode_map_dumb *args = data;
2169
2170         /* call driver ioctl to get mmap offset */
2171         if (!dev->driver->dumb_map_offset)
2172                 return -ENOSYS;
2173
2174         return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
2175 }
2176
2177 /**
2178  * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
2179  * @dev: DRM device
2180  * @data: ioctl data
2181  * @file_priv: DRM file info
2182  *
2183  * This destroys the userspace handle for the given dumb backing storage buffer.
2184  * Since buffer objects must be reference counted in the kernel a buffer object
2185  * won't be immediately freed if a framebuffer modeset object still uses it.
2186  *
2187  * Called by the user via ioctl.
2188  *
2189  * Returns:
2190  * Zero on success, negative errno on failure.
2191  */
2192 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
2193                                 void *data, struct drm_file *file_priv)
2194 {
2195         struct drm_mode_destroy_dumb *args = data;
2196
2197         if (!dev->driver->dumb_destroy)
2198                 return -ENOSYS;
2199
2200         return dev->driver->dumb_destroy(file_priv, dev, args->handle);
2201 }
2202
2203 /**
2204  * drm_rotation_simplify() - Try to simplify the rotation
2205  * @rotation: Rotation to be simplified
2206  * @supported_rotations: Supported rotations
2207  *
2208  * Attempt to simplify the rotation to a form that is supported.
2209  * Eg. if the hardware supports everything except DRM_REFLECT_X
2210  * one could call this function like this:
2211  *
2212  * drm_rotation_simplify(rotation, DRM_ROTATE_0 |
2213  *                       DRM_ROTATE_90 | DRM_ROTATE_180 |
2214  *                       DRM_ROTATE_270 | DRM_REFLECT_Y);
2215  *
2216  * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
2217  * transforms the hardware supports, this function may not
2218  * be able to produce a supported transform, so the caller should
2219  * check the result afterwards.
2220  */
2221 unsigned int drm_rotation_simplify(unsigned int rotation,
2222                                    unsigned int supported_rotations)
2223 {
2224         if (rotation & ~supported_rotations) {
2225                 rotation ^= DRM_REFLECT_X | DRM_REFLECT_Y;
2226                 rotation = (rotation & DRM_REFLECT_MASK) |
2227                            BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
2228         }
2229
2230         return rotation;
2231 }
2232 EXPORT_SYMBOL(drm_rotation_simplify);
2233
2234 /**
2235  * drm_mode_config_init - initialize DRM mode_configuration structure
2236  * @dev: DRM device
2237  *
2238  * Initialize @dev's mode_config structure, used for tracking the graphics
2239  * configuration of @dev.
2240  *
2241  * Since this initializes the modeset locks, no locking is possible. Which is no
2242  * problem, since this should happen single threaded at init time. It is the
2243  * driver's problem to ensure this guarantee.
2244  *
2245  */
2246 void drm_mode_config_init(struct drm_device *dev)
2247 {
2248         mutex_init(&dev->mode_config.mutex);
2249         drm_modeset_lock_init(&dev->mode_config.connection_mutex);
2250         mutex_init(&dev->mode_config.idr_mutex);
2251         mutex_init(&dev->mode_config.fb_lock);
2252         mutex_init(&dev->mode_config.blob_lock);
2253         INIT_LIST_HEAD(&dev->mode_config.fb_list);
2254         INIT_LIST_HEAD(&dev->mode_config.crtc_list);
2255         INIT_LIST_HEAD(&dev->mode_config.connector_list);
2256         INIT_LIST_HEAD(&dev->mode_config.encoder_list);
2257         INIT_LIST_HEAD(&dev->mode_config.property_list);
2258         INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
2259         INIT_LIST_HEAD(&dev->mode_config.plane_list);
2260         idr_init(&dev->mode_config.crtc_idr);
2261         idr_init(&dev->mode_config.tile_idr);
2262         ida_init(&dev->mode_config.connector_ida);
2263
2264         drm_modeset_lock_all(dev);
2265         drm_mode_create_standard_properties(dev);
2266         drm_modeset_unlock_all(dev);
2267
2268         /* Just to be sure */
2269         dev->mode_config.num_fb = 0;
2270         dev->mode_config.num_connector = 0;
2271         dev->mode_config.num_crtc = 0;
2272         dev->mode_config.num_encoder = 0;
2273         dev->mode_config.num_overlay_plane = 0;
2274         dev->mode_config.num_total_plane = 0;
2275 }
2276 EXPORT_SYMBOL(drm_mode_config_init);
2277
2278 /**
2279  * drm_mode_config_cleanup - free up DRM mode_config info
2280  * @dev: DRM device
2281  *
2282  * Free up all the connectors and CRTCs associated with this DRM device, then
2283  * free up the framebuffers and associated buffer objects.
2284  *
2285  * Note that since this /should/ happen single-threaded at driver/device
2286  * teardown time, no locking is required. It's the driver's job to ensure that
2287  * this guarantee actually holds true.
2288  *
2289  * FIXME: cleanup any dangling user buffer objects too
2290  */
2291 void drm_mode_config_cleanup(struct drm_device *dev)
2292 {
2293         struct drm_connector *connector, *ot;
2294         struct drm_crtc *crtc, *ct;
2295         struct drm_encoder *encoder, *enct;
2296         struct drm_framebuffer *fb, *fbt;
2297         struct drm_property *property, *pt;
2298         struct drm_property_blob *blob, *bt;
2299         struct drm_plane *plane, *plt;
2300
2301         list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
2302                                  head) {
2303                 encoder->funcs->destroy(encoder);
2304         }
2305
2306         list_for_each_entry_safe(connector, ot,
2307                                  &dev->mode_config.connector_list, head) {
2308                 connector->funcs->destroy(connector);
2309         }
2310
2311         list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
2312                                  head) {
2313                 drm_property_destroy(dev, property);
2314         }
2315
2316         list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
2317                                  head) {
2318                 plane->funcs->destroy(plane);
2319         }
2320
2321         list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
2322                 crtc->funcs->destroy(crtc);
2323         }
2324
2325         list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
2326                                  head_global) {
2327                 drm_property_unreference_blob(blob);
2328         }
2329
2330         /*
2331          * Single-threaded teardown context, so it's not required to grab the
2332          * fb_lock to protect against concurrent fb_list access. Contrary, it
2333          * would actually deadlock with the drm_framebuffer_cleanup function.
2334          *
2335          * Also, if there are any framebuffers left, that's a driver leak now,
2336          * so politely WARN about this.
2337          */
2338         WARN_ON(!list_empty(&dev->mode_config.fb_list));
2339         list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
2340                 drm_framebuffer_free(&fb->base.refcount);
2341         }
2342
2343         ida_destroy(&dev->mode_config.connector_ida);
2344         idr_destroy(&dev->mode_config.tile_idr);
2345         idr_destroy(&dev->mode_config.crtc_idr);
2346         drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
2347 }
2348 EXPORT_SYMBOL(drm_mode_config_cleanup);
2349
2350 struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
2351                                                        unsigned int supported_rotations)
2352 {
2353         static const struct drm_prop_enum_list props[] = {
2354                 { __builtin_ffs(DRM_ROTATE_0) - 1,   "rotate-0" },
2355                 { __builtin_ffs(DRM_ROTATE_90) - 1,  "rotate-90" },
2356                 { __builtin_ffs(DRM_ROTATE_180) - 1, "rotate-180" },
2357                 { __builtin_ffs(DRM_ROTATE_270) - 1, "rotate-270" },
2358                 { __builtin_ffs(DRM_REFLECT_X) - 1,  "reflect-x" },
2359                 { __builtin_ffs(DRM_REFLECT_Y) - 1,  "reflect-y" },
2360         };
2361
2362         return drm_property_create_bitmask(dev, 0, "rotation",
2363                                            props, ARRAY_SIZE(props),
2364                                            supported_rotations);
2365 }
2366 EXPORT_SYMBOL(drm_mode_create_rotation_property);
2367
2368 /**
2369  * DOC: Tile group
2370  *
2371  * Tile groups are used to represent tiled monitors with a unique
2372  * integer identifier. Tiled monitors using DisplayID v1.3 have
2373  * a unique 8-byte handle, we store this in a tile group, so we
2374  * have a common identifier for all tiles in a monitor group.
2375  */
2376 static void drm_tile_group_free(struct kref *kref)
2377 {
2378         struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
2379         struct drm_device *dev = tg->dev;
2380         mutex_lock(&dev->mode_config.idr_mutex);
2381         idr_remove(&dev->mode_config.tile_idr, tg->id);
2382         mutex_unlock(&dev->mode_config.idr_mutex);
2383         kfree(tg);
2384 }
2385
2386 /**
2387  * drm_mode_put_tile_group - drop a reference to a tile group.
2388  * @dev: DRM device
2389  * @tg: tile group to drop reference to.
2390  *
2391  * drop reference to tile group and free if 0.
2392  */
2393 void drm_mode_put_tile_group(struct drm_device *dev,
2394                              struct drm_tile_group *tg)
2395 {
2396         kref_put(&tg->refcount, drm_tile_group_free);
2397 }
2398
2399 /**
2400  * drm_mode_get_tile_group - get a reference to an existing tile group
2401  * @dev: DRM device
2402  * @topology: 8-bytes unique per monitor.
2403  *
2404  * Use the unique bytes to get a reference to an existing tile group.
2405  *
2406  * RETURNS:
2407  * tile group or NULL if not found.
2408  */
2409 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
2410                                                char topology[8])
2411 {
2412         struct drm_tile_group *tg;
2413         int id;
2414         mutex_lock(&dev->mode_config.idr_mutex);
2415         idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
2416                 if (!memcmp(tg->group_data, topology, 8)) {
2417                         if (!kref_get_unless_zero(&tg->refcount))
2418                                 tg = NULL;
2419                         mutex_unlock(&dev->mode_config.idr_mutex);
2420                         return tg;
2421                 }
2422         }
2423         mutex_unlock(&dev->mode_config.idr_mutex);
2424         return NULL;
2425 }
2426 EXPORT_SYMBOL(drm_mode_get_tile_group);
2427
2428 /**
2429  * drm_mode_create_tile_group - create a tile group from a displayid description
2430  * @dev: DRM device
2431  * @topology: 8-bytes unique per monitor.
2432  *
2433  * Create a tile group for the unique monitor, and get a unique
2434  * identifier for the tile group.
2435  *
2436  * RETURNS:
2437  * new tile group or error.
2438  */
2439 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
2440                                                   char topology[8])
2441 {
2442         struct drm_tile_group *tg;
2443         int ret;
2444
2445         tg = kzalloc(sizeof(*tg), GFP_KERNEL);
2446         if (!tg)
2447                 return ERR_PTR(-ENOMEM);
2448
2449         kref_init(&tg->refcount);
2450         memcpy(tg->group_data, topology, 8);
2451         tg->dev = dev;
2452
2453         mutex_lock(&dev->mode_config.idr_mutex);
2454         ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
2455         if (ret >= 0) {
2456                 tg->id = ret;
2457         } else {
2458                 kfree(tg);
2459                 tg = ERR_PTR(ret);
2460         }
2461
2462         mutex_unlock(&dev->mode_config.idr_mutex);
2463         return tg;
2464 }
2465 EXPORT_SYMBOL(drm_mode_create_tile_group);
2466
2467 /**
2468  * drm_crtc_enable_color_mgmt - enable color management properties
2469  * @crtc: DRM CRTC
2470  * @degamma_lut_size: the size of the degamma lut (before CSC)
2471  * @has_ctm: whether to attach ctm_property for CSC matrix
2472  * @gamma_lut_size: the size of the gamma lut (after CSC)
2473  *
2474  * This function lets the driver enable the color correction
2475  * properties on a CRTC. This includes 3 degamma, csc and gamma
2476  * properties that userspace can set and 2 size properties to inform
2477  * the userspace of the lut sizes. Each of the properties are
2478  * optional. The gamma and degamma properties are only attached if
2479  * their size is not 0 and ctm_property is only attached if has_ctm is
2480  * true.
2481  */
2482 void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
2483                                 uint degamma_lut_size,
2484                                 bool has_ctm,
2485                                 uint gamma_lut_size)
2486 {
2487         struct drm_device *dev = crtc->dev;
2488         struct drm_mode_config *config = &dev->mode_config;
2489
2490         if (degamma_lut_size) {
2491                 drm_object_attach_property(&crtc->base,
2492                                            config->degamma_lut_property, 0);
2493                 drm_object_attach_property(&crtc->base,
2494                                            config->degamma_lut_size_property,
2495                                            degamma_lut_size);
2496         }
2497
2498         if (has_ctm)
2499                 drm_object_attach_property(&crtc->base,
2500                                            config->ctm_property, 0);
2501
2502         if (gamma_lut_size) {
2503                 drm_object_attach_property(&crtc->base,
2504                                            config->gamma_lut_property, 0);
2505                 drm_object_attach_property(&crtc->base,
2506                                            config->gamma_lut_size_property,
2507                                            gamma_lut_size);
2508         }
2509 }
2510 EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);
This page took 0.177953 seconds and 4 git commands to generate.