drm: Fix lockinit() calls in previous commit
[dragonfly.git] / sys / dev / drm / drm_crtc.c
1 /*
2  * Copyright (c) 2006-2008 Intel Corporation
3  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4  * Copyright (c) 2008 Red Hat Inc.
5  *
6  * DRM core CRTC related functions
7  *
8  * Permission to use, copy, modify, distribute, and sell this software and its
9  * documentation for any purpose is hereby granted without fee, provided that
10  * the above copyright notice appear in all copies and that both that copyright
11  * notice and this permission notice appear in supporting documentation, and
12  * that the name of the copyright holders not be used in advertising or
13  * publicity pertaining to distribution of the software without specific,
14  * written prior permission.  The copyright holders make no representations
15  * about the suitability of this software for any purpose.  It is provided "as
16  * is" without express or implied warranty.
17  *
18  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  *
26  * Authors:
27  *      Keith Packard
28  *      Eric Anholt <eric@anholt.net>
29  *      Dave Airlie <airlied@linux.ie>
30  *      Jesse Barnes <jesse.barnes@intel.com>
31  */
32 #include <linux/ctype.h>
33 #include <linux/list.h>
34 #include <linux/export.h>
35 #include <drm/drmP.h>
36 #include <drm/drm_crtc.h>
37 #include <drm/drm_edid.h>
38 #include <drm/drm_fourcc.h>
39 #include <linux/slab.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. Drivers
155  * should use this function instead of drm_crtc_init(), which is only provided
156  * for backwards compatibility with drivers which do not yet support universal
157  * planes). For really simple hardware which has only 1 plane look at
158  * drm_simple_display_pipe_init() instead.
159  *
160  * Returns:
161  * Zero on success, error code on failure.
162  */
163 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
164                               struct drm_plane *primary,
165                               struct drm_plane *cursor,
166                               const struct drm_crtc_funcs *funcs,
167                               const char *name, ...)
168 {
169         struct drm_mode_config *config = &dev->mode_config;
170         int ret;
171
172         WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
173         WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
174
175         crtc->dev = dev;
176         crtc->funcs = funcs;
177
178         INIT_LIST_HEAD(&crtc->commit_list);
179         lockinit(&crtc->commit_lock, "dccl", 0, 0);
180
181         drm_modeset_lock_init(&crtc->mutex);
182         ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
183         if (ret)
184                 return ret;
185
186         if (name) {
187                 va_list ap;
188
189                 va_start(ap, name);
190                 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
191                 va_end(ap);
192         } else {
193                 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
194                                        drm_num_crtcs(dev));
195         }
196         if (!crtc->name) {
197                 drm_mode_object_unregister(dev, &crtc->base);
198                 return -ENOMEM;
199         }
200
201         crtc->base.properties = &crtc->properties;
202
203         list_add_tail(&crtc->head, &config->crtc_list);
204         crtc->index = config->num_crtc++;
205
206         crtc->primary = primary;
207         crtc->cursor = cursor;
208         if (primary)
209                 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
210         if (cursor)
211                 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
212
213         if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
214                 drm_object_attach_property(&crtc->base, config->prop_active, 0);
215                 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
216         }
217
218         return 0;
219 }
220 EXPORT_SYMBOL(drm_crtc_init_with_planes);
221
222 /**
223  * drm_crtc_cleanup - Clean up the core crtc usage
224  * @crtc: CRTC to cleanup
225  *
226  * This function cleans up @crtc and removes it from the DRM mode setting
227  * core. Note that the function does *not* free the crtc structure itself,
228  * this is the responsibility of the caller.
229  */
230 void drm_crtc_cleanup(struct drm_crtc *crtc)
231 {
232         struct drm_device *dev = crtc->dev;
233
234         /* Note that the crtc_list is considered to be static; should we
235          * remove the drm_crtc at runtime we would have to decrement all
236          * the indices on the drm_crtc after us in the crtc_list.
237          */
238
239         kfree(crtc->gamma_store);
240         crtc->gamma_store = NULL;
241
242         drm_modeset_lock_fini(&crtc->mutex);
243
244         drm_mode_object_unregister(dev, &crtc->base);
245         list_del(&crtc->head);
246         dev->mode_config.num_crtc--;
247
248         WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
249         if (crtc->state && crtc->funcs->atomic_destroy_state)
250                 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
251
252         kfree(crtc->name);
253
254         memset(crtc, 0, sizeof(*crtc));
255 }
256 EXPORT_SYMBOL(drm_crtc_cleanup);
257
258 /**
259  * drm_connector_index - find the index of a registered connector
260  * @connector: connector to find index for
261  *
262  * Given a registered connector, return the index of that connector within a DRM
263  * device's list of connectors.
264  */
265 unsigned int drm_connector_index(struct drm_connector *connector)
266 {
267         unsigned int index = 0;
268         struct drm_connector *tmp;
269         struct drm_mode_config *config = &connector->dev->mode_config;
270
271         WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
272
273         drm_for_each_connector(tmp, connector->dev) {
274                 if (tmp == connector)
275                         return index;
276
277                 index++;
278         }
279
280         BUG();
281 }
282 EXPORT_SYMBOL(drm_connector_index);
283
284 int drm_modeset_register_all(struct drm_device *dev)
285 {
286         int ret;
287
288         ret = drm_plane_register_all(dev);
289         if (ret)
290                 goto err_plane;
291
292         ret = drm_crtc_register_all(dev);
293         if  (ret)
294                 goto err_crtc;
295
296         ret = drm_encoder_register_all(dev);
297         if (ret)
298                 goto err_encoder;
299
300         ret = drm_connector_register_all(dev);
301         if (ret)
302                 goto err_connector;
303
304         return 0;
305
306 err_connector:
307         drm_encoder_unregister_all(dev);
308 err_encoder:
309         drm_crtc_unregister_all(dev);
310 err_crtc:
311         drm_plane_unregister_all(dev);
312 err_plane:
313         return ret;
314 }
315
316 void drm_modeset_unregister_all(struct drm_device *dev)
317 {
318         drm_connector_unregister_all(dev);
319         drm_encoder_unregister_all(dev);
320         drm_crtc_unregister_all(dev);
321         drm_plane_unregister_all(dev);
322 }
323
324 static int drm_mode_create_standard_properties(struct drm_device *dev)
325 {
326         struct drm_property *prop;
327         int ret;
328
329         ret = drm_connector_create_standard_properties(dev);
330         if (ret)
331                 return ret;
332
333         prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
334                                         "type", drm_plane_type_enum_list,
335                                         ARRAY_SIZE(drm_plane_type_enum_list));
336         if (!prop)
337                 return -ENOMEM;
338         dev->mode_config.plane_type_property = prop;
339
340         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
341                         "SRC_X", 0, UINT_MAX);
342         if (!prop)
343                 return -ENOMEM;
344         dev->mode_config.prop_src_x = prop;
345
346         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
347                         "SRC_Y", 0, UINT_MAX);
348         if (!prop)
349                 return -ENOMEM;
350         dev->mode_config.prop_src_y = prop;
351
352         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
353                         "SRC_W", 0, UINT_MAX);
354         if (!prop)
355                 return -ENOMEM;
356         dev->mode_config.prop_src_w = prop;
357
358         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
359                         "SRC_H", 0, UINT_MAX);
360         if (!prop)
361                 return -ENOMEM;
362         dev->mode_config.prop_src_h = prop;
363
364         prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
365                         "CRTC_X", INT_MIN, INT_MAX);
366         if (!prop)
367                 return -ENOMEM;
368         dev->mode_config.prop_crtc_x = prop;
369
370         prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
371                         "CRTC_Y", INT_MIN, INT_MAX);
372         if (!prop)
373                 return -ENOMEM;
374         dev->mode_config.prop_crtc_y = prop;
375
376         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
377                         "CRTC_W", 0, INT_MAX);
378         if (!prop)
379                 return -ENOMEM;
380         dev->mode_config.prop_crtc_w = prop;
381
382         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
383                         "CRTC_H", 0, INT_MAX);
384         if (!prop)
385                 return -ENOMEM;
386         dev->mode_config.prop_crtc_h = prop;
387
388         prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
389                         "FB_ID", DRM_MODE_OBJECT_FB);
390         if (!prop)
391                 return -ENOMEM;
392         dev->mode_config.prop_fb_id = prop;
393
394         prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
395                         "CRTC_ID", DRM_MODE_OBJECT_CRTC);
396         if (!prop)
397                 return -ENOMEM;
398         dev->mode_config.prop_crtc_id = prop;
399
400         prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
401                         "ACTIVE");
402         if (!prop)
403                 return -ENOMEM;
404         dev->mode_config.prop_active = prop;
405
406         prop = drm_property_create(dev,
407                         DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
408                         "MODE_ID", 0);
409         if (!prop)
410                 return -ENOMEM;
411         dev->mode_config.prop_mode_id = prop;
412
413         prop = drm_property_create(dev,
414                         DRM_MODE_PROP_BLOB,
415                         "DEGAMMA_LUT", 0);
416         if (!prop)
417                 return -ENOMEM;
418         dev->mode_config.degamma_lut_property = prop;
419
420         prop = drm_property_create_range(dev,
421                         DRM_MODE_PROP_IMMUTABLE,
422                         "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
423         if (!prop)
424                 return -ENOMEM;
425         dev->mode_config.degamma_lut_size_property = prop;
426
427         prop = drm_property_create(dev,
428                         DRM_MODE_PROP_BLOB,
429                         "CTM", 0);
430         if (!prop)
431                 return -ENOMEM;
432         dev->mode_config.ctm_property = prop;
433
434         prop = drm_property_create(dev,
435                         DRM_MODE_PROP_BLOB,
436                         "GAMMA_LUT", 0);
437         if (!prop)
438                 return -ENOMEM;
439         dev->mode_config.gamma_lut_property = prop;
440
441         prop = drm_property_create_range(dev,
442                         DRM_MODE_PROP_IMMUTABLE,
443                         "GAMMA_LUT_SIZE", 0, UINT_MAX);
444         if (!prop)
445                 return -ENOMEM;
446         dev->mode_config.gamma_lut_size_property = prop;
447
448         return 0;
449 }
450
451 /**
452  * drm_mode_getresources - get graphics configuration
453  * @dev: drm device for the ioctl
454  * @data: data pointer for the ioctl
455  * @file_priv: drm file for the ioctl call
456  *
457  * Construct a set of configuration description structures and return
458  * them to the user, including CRTC, connector and framebuffer configuration.
459  *
460  * Called by the user via ioctl.
461  *
462  * Returns:
463  * Zero on success, negative errno on failure.
464  */
465 int drm_mode_getresources(struct drm_device *dev, void *data,
466                           struct drm_file *file_priv)
467 {
468         struct drm_mode_card_res *card_res = data;
469         struct list_head *lh;
470         struct drm_framebuffer *fb;
471         struct drm_connector *connector;
472         struct drm_crtc *crtc;
473         struct drm_encoder *encoder;
474         int ret = 0;
475         int connector_count = 0;
476         int crtc_count = 0;
477         int fb_count = 0;
478         int encoder_count = 0;
479         int copied = 0;
480         uint32_t __user *fb_id;
481         uint32_t __user *crtc_id;
482         uint32_t __user *connector_id;
483         uint32_t __user *encoder_id;
484
485         if (!drm_core_check_feature(dev, DRIVER_MODESET))
486                 return -EINVAL;
487
488
489         mutex_lock(&file_priv->fbs_lock);
490         /*
491          * For the non-control nodes we need to limit the list of resources
492          * by IDs in the group list for this node
493          */
494         list_for_each(lh, &file_priv->fbs)
495                 fb_count++;
496
497         /* handle this in 4 parts */
498         /* FBs */
499         if (card_res->count_fbs >= fb_count) {
500                 copied = 0;
501                 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
502                 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
503                         if (put_user(fb->base.id, fb_id + copied)) {
504                                 mutex_unlock(&file_priv->fbs_lock);
505                                 return -EFAULT;
506                         }
507                         copied++;
508                 }
509         }
510         card_res->count_fbs = fb_count;
511         mutex_unlock(&file_priv->fbs_lock);
512
513         /* mode_config.mutex protects the connector list against e.g. DP MST
514          * connector hot-adding. CRTC/Plane lists are invariant. */
515         mutex_lock(&dev->mode_config.mutex);
516         drm_for_each_crtc(crtc, dev)
517                 crtc_count++;
518
519         drm_for_each_connector(connector, dev)
520                 connector_count++;
521
522         drm_for_each_encoder(encoder, dev)
523                 encoder_count++;
524
525         card_res->max_height = dev->mode_config.max_height;
526         card_res->min_height = dev->mode_config.min_height;
527         card_res->max_width = dev->mode_config.max_width;
528         card_res->min_width = dev->mode_config.min_width;
529
530         /* CRTCs */
531         if (card_res->count_crtcs >= crtc_count) {
532                 copied = 0;
533                 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
534                 drm_for_each_crtc(crtc, dev) {
535                         if (put_user(crtc->base.id, crtc_id + copied)) {
536                                 ret = -EFAULT;
537                                 goto out;
538                         }
539                         copied++;
540                 }
541         }
542         card_res->count_crtcs = crtc_count;
543
544         /* Encoders */
545         if (card_res->count_encoders >= encoder_count) {
546                 copied = 0;
547                 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
548                 drm_for_each_encoder(encoder, dev) {
549                         if (put_user(encoder->base.id, encoder_id +
550                                      copied)) {
551                                 ret = -EFAULT;
552                                 goto out;
553                         }
554                         copied++;
555                 }
556         }
557         card_res->count_encoders = encoder_count;
558
559         /* Connectors */
560         if (card_res->count_connectors >= connector_count) {
561                 copied = 0;
562                 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
563                 drm_for_each_connector(connector, dev) {
564                         if (put_user(connector->base.id,
565                                      connector_id + copied)) {
566                                 ret = -EFAULT;
567                                 goto out;
568                         }
569                         copied++;
570                 }
571         }
572         card_res->count_connectors = connector_count;
573
574 out:
575         mutex_unlock(&dev->mode_config.mutex);
576         return ret;
577 }
578
579 /**
580  * drm_mode_getcrtc - get CRTC configuration
581  * @dev: drm device for the ioctl
582  * @data: data pointer for the ioctl
583  * @file_priv: drm file for the ioctl call
584  *
585  * Construct a CRTC configuration structure to return to the user.
586  *
587  * Called by the user via ioctl.
588  *
589  * Returns:
590  * Zero on success, negative errno on failure.
591  */
592 int drm_mode_getcrtc(struct drm_device *dev,
593                      void *data, struct drm_file *file_priv)
594 {
595         struct drm_mode_crtc *crtc_resp = data;
596         struct drm_crtc *crtc;
597
598         if (!drm_core_check_feature(dev, DRIVER_MODESET))
599                 return -EINVAL;
600
601         crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
602         if (!crtc)
603                 return -ENOENT;
604
605         drm_modeset_lock_crtc(crtc, crtc->primary);
606         crtc_resp->gamma_size = crtc->gamma_size;
607         if (crtc->primary->fb)
608                 crtc_resp->fb_id = crtc->primary->fb->base.id;
609         else
610                 crtc_resp->fb_id = 0;
611
612         if (crtc->state) {
613                 crtc_resp->x = crtc->primary->state->src_x >> 16;
614                 crtc_resp->y = crtc->primary->state->src_y >> 16;
615                 if (crtc->state->enable) {
616                         drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
617                         crtc_resp->mode_valid = 1;
618
619                 } else {
620                         crtc_resp->mode_valid = 0;
621                 }
622         } else {
623                 crtc_resp->x = crtc->x;
624                 crtc_resp->y = crtc->y;
625                 if (crtc->enabled) {
626                         drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
627                         crtc_resp->mode_valid = 1;
628
629                 } else {
630                         crtc_resp->mode_valid = 0;
631                 }
632         }
633         drm_modeset_unlock_crtc(crtc);
634
635         return 0;
636 }
637
638 /**
639  * drm_mode_set_config_internal - helper to call ->set_config
640  * @set: modeset config to set
641  *
642  * This is a little helper to wrap internal calls to the ->set_config driver
643  * interface. The only thing it adds is correct refcounting dance.
644  *
645  * Returns:
646  * Zero on success, negative errno on failure.
647  */
648 int drm_mode_set_config_internal(struct drm_mode_set *set)
649 {
650         struct drm_crtc *crtc = set->crtc;
651         struct drm_framebuffer *fb;
652         struct drm_crtc *tmp;
653         int ret;
654
655         /*
656          * NOTE: ->set_config can also disable other crtcs (if we steal all
657          * connectors from it), hence we need to refcount the fbs across all
658          * crtcs. Atomic modeset will have saner semantics ...
659          */
660         drm_for_each_crtc(tmp, crtc->dev)
661                 tmp->primary->old_fb = tmp->primary->fb;
662
663         fb = set->fb;
664
665         ret = crtc->funcs->set_config(set);
666         if (ret == 0) {
667                 crtc->primary->crtc = crtc;
668                 crtc->primary->fb = fb;
669         }
670
671         drm_for_each_crtc(tmp, crtc->dev) {
672                 if (tmp->primary->fb)
673                         drm_framebuffer_reference(tmp->primary->fb);
674                 if (tmp->primary->old_fb)
675                         drm_framebuffer_unreference(tmp->primary->old_fb);
676                 tmp->primary->old_fb = NULL;
677         }
678
679         return ret;
680 }
681 EXPORT_SYMBOL(drm_mode_set_config_internal);
682
683 /**
684  * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
685  * @mode: mode to query
686  * @hdisplay: hdisplay value to fill in
687  * @vdisplay: vdisplay value to fill in
688  *
689  * The vdisplay value will be doubled if the specified mode is a stereo mode of
690  * the appropriate layout.
691  */
692 void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
693                             int *hdisplay, int *vdisplay)
694 {
695         struct drm_display_mode adjusted;
696
697         drm_mode_copy(&adjusted, mode);
698         drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
699         *hdisplay = adjusted.crtc_hdisplay;
700         *vdisplay = adjusted.crtc_vdisplay;
701 }
702 EXPORT_SYMBOL(drm_crtc_get_hv_timing);
703
704 /**
705  * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
706  *     CRTC viewport
707  * @crtc: CRTC that framebuffer will be displayed on
708  * @x: x panning
709  * @y: y panning
710  * @mode: mode that framebuffer will be displayed under
711  * @fb: framebuffer to check size of
712  */
713 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
714                             int x, int y,
715                             const struct drm_display_mode *mode,
716                             const struct drm_framebuffer *fb)
717
718 {
719         int hdisplay, vdisplay;
720
721         drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
722
723         if (crtc->state &&
724             crtc->primary->state->rotation & (DRM_ROTATE_90 |
725                                               DRM_ROTATE_270))
726                 swap(hdisplay, vdisplay);
727
728         return drm_framebuffer_check_src_coords(x << 16, y << 16,
729                                                 hdisplay << 16, vdisplay << 16,
730                                                 fb);
731 }
732 EXPORT_SYMBOL(drm_crtc_check_viewport);
733
734 /**
735  * drm_mode_setcrtc - set CRTC configuration
736  * @dev: drm device for the ioctl
737  * @data: data pointer for the ioctl
738  * @file_priv: drm file for the ioctl call
739  *
740  * Build a new CRTC configuration based on user request.
741  *
742  * Called by the user via ioctl.
743  *
744  * Returns:
745  * Zero on success, negative errno on failure.
746  */
747 int drm_mode_setcrtc(struct drm_device *dev, void *data,
748                      struct drm_file *file_priv)
749 {
750         struct drm_mode_config *config = &dev->mode_config;
751         struct drm_mode_crtc *crtc_req = data;
752         struct drm_crtc *crtc;
753         struct drm_connector **connector_set = NULL, *connector;
754         struct drm_framebuffer *fb = NULL;
755         struct drm_display_mode *mode = NULL;
756         struct drm_mode_set set;
757         uint32_t __user *set_connectors_ptr;
758         int ret;
759         int i;
760
761         if (!drm_core_check_feature(dev, DRIVER_MODESET))
762                 return -EINVAL;
763
764         /*
765          * Universal plane src offsets are only 16.16, prevent havoc for
766          * drivers using universal plane code internally.
767          */
768         if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
769                 return -ERANGE;
770
771         drm_modeset_lock_all(dev);
772         crtc = drm_crtc_find(dev, crtc_req->crtc_id);
773         if (!crtc) {
774                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
775                 ret = -ENOENT;
776                 goto out;
777         }
778         DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
779
780         if (crtc_req->mode_valid) {
781                 /* If we have a mode we need a framebuffer. */
782                 /* If we pass -1, set the mode with the currently bound fb */
783                 if (crtc_req->fb_id == -1) {
784                         if (!crtc->primary->fb) {
785                                 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
786                                 ret = -EINVAL;
787                                 goto out;
788                         }
789                         fb = crtc->primary->fb;
790                         /* Make refcounting symmetric with the lookup path. */
791                         drm_framebuffer_reference(fb);
792                 } else {
793                         fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
794                         if (!fb) {
795                                 DRM_DEBUG_KMS("Unknown FB ID%d\n",
796                                                 crtc_req->fb_id);
797                                 ret = -ENOENT;
798                                 goto out;
799                         }
800                 }
801
802                 mode = drm_mode_create(dev);
803                 if (!mode) {
804                         ret = -ENOMEM;
805                         goto out;
806                 }
807
808                 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
809                 if (ret) {
810                         DRM_DEBUG_KMS("Invalid mode\n");
811                         goto out;
812                 }
813
814                 /*
815                  * Check whether the primary plane supports the fb pixel format.
816                  * Drivers not implementing the universal planes API use a
817                  * default formats list provided by the DRM core which doesn't
818                  * match real hardware capabilities. Skip the check in that
819                  * case.
820                  */
821                 if (!crtc->primary->format_default) {
822                         ret = drm_plane_check_pixel_format(crtc->primary,
823                                                            fb->pixel_format);
824                         if (ret) {
825                                 char *format_name = drm_get_format_name(fb->pixel_format);
826                                 DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
827                                 kfree(format_name);
828                                 goto out;
829                         }
830                 }
831
832                 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
833                                               mode, fb);
834                 if (ret)
835                         goto out;
836
837         }
838
839         if (crtc_req->count_connectors == 0 && mode) {
840                 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
841                 ret = -EINVAL;
842                 goto out;
843         }
844
845         if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
846                 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
847                           crtc_req->count_connectors);
848                 ret = -EINVAL;
849                 goto out;
850         }
851
852         if (crtc_req->count_connectors > 0) {
853                 u32 out_id;
854
855                 /* Avoid unbounded kernel memory allocation */
856                 if (crtc_req->count_connectors > config->num_connector) {
857                         ret = -EINVAL;
858                         goto out;
859                 }
860
861                 connector_set = kmalloc_array(crtc_req->count_connectors,
862                                               sizeof(struct drm_connector *),
863                                               GFP_KERNEL);
864                 if (!connector_set) {
865                         ret = -ENOMEM;
866                         goto out;
867                 }
868
869                 for (i = 0; i < crtc_req->count_connectors; i++) {
870                         connector_set[i] = NULL;
871                         set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
872                         if (get_user(out_id, &set_connectors_ptr[i])) {
873                                 ret = -EFAULT;
874                                 goto out;
875                         }
876
877                         connector = drm_connector_lookup(dev, out_id);
878                         if (!connector) {
879                                 DRM_DEBUG_KMS("Connector id %d unknown\n",
880                                                 out_id);
881                                 ret = -ENOENT;
882                                 goto out;
883                         }
884                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
885                                         connector->base.id,
886                                         connector->name);
887
888                         connector_set[i] = connector;
889                 }
890         }
891
892         set.crtc = crtc;
893         set.x = crtc_req->x;
894         set.y = crtc_req->y;
895         set.mode = mode;
896         set.connectors = connector_set;
897         set.num_connectors = crtc_req->count_connectors;
898         set.fb = fb;
899         ret = drm_mode_set_config_internal(&set);
900
901 out:
902         if (fb)
903                 drm_framebuffer_unreference(fb);
904
905         if (connector_set) {
906                 for (i = 0; i < crtc_req->count_connectors; i++) {
907                         if (connector_set[i])
908                                 drm_connector_unreference(connector_set[i]);
909                 }
910         }
911         kfree(connector_set);
912         drm_mode_destroy(dev, mode);
913         drm_modeset_unlock_all(dev);
914         return ret;
915 }
916
917 int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
918                                struct drm_property *property,
919                                uint64_t value)
920 {
921         int ret = -EINVAL;
922         struct drm_crtc *crtc = obj_to_crtc(obj);
923
924         if (crtc->funcs->set_property)
925                 ret = crtc->funcs->set_property(crtc, property, value);
926         if (!ret)
927                 drm_object_property_set_value(obj, property, value);
928
929         return ret;
930 }
931
932 /**
933  * drm_mode_config_reset - call ->reset callbacks
934  * @dev: drm device
935  *
936  * This functions calls all the crtc's, encoder's and connector's ->reset
937  * callback. Drivers can use this in e.g. their driver load or resume code to
938  * reset hardware and software state.
939  */
940 void drm_mode_config_reset(struct drm_device *dev)
941 {
942         struct drm_crtc *crtc;
943         struct drm_plane *plane;
944         struct drm_encoder *encoder;
945         struct drm_connector *connector;
946
947         drm_for_each_plane(plane, dev)
948                 if (plane->funcs->reset)
949                         plane->funcs->reset(plane);
950
951         drm_for_each_crtc(crtc, dev)
952                 if (crtc->funcs->reset)
953                         crtc->funcs->reset(crtc);
954
955         drm_for_each_encoder(encoder, dev)
956                 if (encoder->funcs->reset)
957                         encoder->funcs->reset(encoder);
958
959         mutex_lock(&dev->mode_config.mutex);
960         drm_for_each_connector(connector, dev)
961                 if (connector->funcs->reset)
962                         connector->funcs->reset(connector);
963         mutex_unlock(&dev->mode_config.mutex);
964 }
965 EXPORT_SYMBOL(drm_mode_config_reset);
966
967 /**
968  * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
969  * @dev: DRM device
970  * @data: ioctl data
971  * @file_priv: DRM file info
972  *
973  * This creates a new dumb buffer in the driver's backing storage manager (GEM,
974  * TTM or something else entirely) and returns the resulting buffer handle. This
975  * handle can then be wrapped up into a framebuffer modeset object.
976  *
977  * Note that userspace is not allowed to use such objects for render
978  * acceleration - drivers must create their own private ioctls for such a use
979  * case.
980  *
981  * Called by the user via ioctl.
982  *
983  * Returns:
984  * Zero on success, negative errno on failure.
985  */
986 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
987                                void *data, struct drm_file *file_priv)
988 {
989         struct drm_mode_create_dumb *args = data;
990         u32 cpp, stride, size;
991
992         if (!dev->driver->dumb_create)
993                 return -ENOSYS;
994         if (!args->width || !args->height || !args->bpp)
995                 return -EINVAL;
996
997         /* overflow checks for 32bit size calculations */
998         /* NOTE: DIV_ROUND_UP() can overflow */
999         cpp = DIV_ROUND_UP(args->bpp, 8);
1000         if (!cpp || cpp > 0xffffffffU / args->width)
1001                 return -EINVAL;
1002         stride = cpp * args->width;
1003         if (args->height > 0xffffffffU / stride)
1004                 return -EINVAL;
1005
1006         /* test for wrap-around */
1007         size = args->height * stride;
1008         if (PAGE_ALIGN(size) == 0)
1009                 return -EINVAL;
1010
1011         /*
1012          * handle, pitch and size are output parameters. Zero them out to
1013          * prevent drivers from accidentally using uninitialized data. Since
1014          * not all existing userspace is clearing these fields properly we
1015          * cannot reject IOCTL with garbage in them.
1016          */
1017         args->handle = 0;
1018         args->pitch = 0;
1019         args->size = 0;
1020
1021         return dev->driver->dumb_create(file_priv, dev, args);
1022 }
1023
1024 /**
1025  * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
1026  * @dev: DRM device
1027  * @data: ioctl data
1028  * @file_priv: DRM file info
1029  *
1030  * Allocate an offset in the drm device node's address space to be able to
1031  * memory map a dumb buffer.
1032  *
1033  * Called by the user via ioctl.
1034  *
1035  * Returns:
1036  * Zero on success, negative errno on failure.
1037  */
1038 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
1039                              void *data, struct drm_file *file_priv)
1040 {
1041         struct drm_mode_map_dumb *args = data;
1042
1043         /* call driver ioctl to get mmap offset */
1044         if (!dev->driver->dumb_map_offset)
1045                 return -ENOSYS;
1046
1047         return dev->driver->dumb_map_offset(file_priv, dev, args->handle, (uint64_t *)&args->offset);
1048 }
1049
1050 /**
1051  * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
1052  * @dev: DRM device
1053  * @data: ioctl data
1054  * @file_priv: DRM file info
1055  *
1056  * This destroys the userspace handle for the given dumb backing storage buffer.
1057  * Since buffer objects must be reference counted in the kernel a buffer object
1058  * won't be immediately freed if a framebuffer modeset object still uses it.
1059  *
1060  * Called by the user via ioctl.
1061  *
1062  * Returns:
1063  * Zero on success, negative errno on failure.
1064  */
1065 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
1066                                 void *data, struct drm_file *file_priv)
1067 {
1068         struct drm_mode_destroy_dumb *args = data;
1069
1070         if (!dev->driver->dumb_destroy)
1071                 return -ENOSYS;
1072
1073         return dev->driver->dumb_destroy(file_priv, dev, args->handle);
1074 }
1075
1076 /**
1077  * drm_mode_config_init - initialize DRM mode_configuration structure
1078  * @dev: DRM device
1079  *
1080  * Initialize @dev's mode_config structure, used for tracking the graphics
1081  * configuration of @dev.
1082  *
1083  * Since this initializes the modeset locks, no locking is possible. Which is no
1084  * problem, since this should happen single threaded at init time. It is the
1085  * driver's problem to ensure this guarantee.
1086  *
1087  */
1088 void drm_mode_config_init(struct drm_device *dev)
1089 {
1090         lockinit(&dev->mode_config.mutex, "drmmcm", 0, LK_CANRECURSE);
1091         drm_modeset_lock_init(&dev->mode_config.connection_mutex);
1092         lockinit(&dev->mode_config.idr_mutex, "mcfgidr", 0, LK_CANRECURSE);
1093         lockinit(&dev->mode_config.fb_lock, "drmfbl", 0, LK_CANRECURSE);
1094         lockinit(&dev->mode_config.blob_lock, "drmcbl", 0, LK_CANRECURSE);
1095         INIT_LIST_HEAD(&dev->mode_config.fb_list);
1096         INIT_LIST_HEAD(&dev->mode_config.crtc_list);
1097         INIT_LIST_HEAD(&dev->mode_config.connector_list);
1098         INIT_LIST_HEAD(&dev->mode_config.encoder_list);
1099         INIT_LIST_HEAD(&dev->mode_config.property_list);
1100         INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
1101         INIT_LIST_HEAD(&dev->mode_config.plane_list);
1102         idr_init(&dev->mode_config.crtc_idr);
1103         idr_init(&dev->mode_config.tile_idr);
1104         ida_init(&dev->mode_config.connector_ida);
1105
1106         drm_modeset_lock_all(dev);
1107         drm_mode_create_standard_properties(dev);
1108         drm_modeset_unlock_all(dev);
1109
1110         /* Just to be sure */
1111         dev->mode_config.num_fb = 0;
1112         dev->mode_config.num_connector = 0;
1113         dev->mode_config.num_crtc = 0;
1114         dev->mode_config.num_encoder = 0;
1115         dev->mode_config.num_overlay_plane = 0;
1116         dev->mode_config.num_total_plane = 0;
1117 }
1118 EXPORT_SYMBOL(drm_mode_config_init);
1119
1120 /**
1121  * drm_mode_config_cleanup - free up DRM mode_config info
1122  * @dev: DRM device
1123  *
1124  * Free up all the connectors and CRTCs associated with this DRM device, then
1125  * free up the framebuffers and associated buffer objects.
1126  *
1127  * Note that since this /should/ happen single-threaded at driver/device
1128  * teardown time, no locking is required. It's the driver's job to ensure that
1129  * this guarantee actually holds true.
1130  *
1131  * FIXME: cleanup any dangling user buffer objects too
1132  */
1133 void drm_mode_config_cleanup(struct drm_device *dev)
1134 {
1135         struct drm_connector *connector, *ot;
1136         struct drm_crtc *crtc, *ct;
1137         struct drm_encoder *encoder, *enct;
1138         struct drm_framebuffer *fb, *fbt;
1139         struct drm_property *property, *pt;
1140         struct drm_property_blob *blob, *bt;
1141         struct drm_plane *plane, *plt;
1142
1143         list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1144                                  head) {
1145                 encoder->funcs->destroy(encoder);
1146         }
1147
1148         list_for_each_entry_safe(connector, ot,
1149                                  &dev->mode_config.connector_list, head) {
1150                 connector->funcs->destroy(connector);
1151         }
1152
1153         list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1154                                  head) {
1155                 drm_property_destroy(dev, property);
1156         }
1157
1158         list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1159                                  head) {
1160                 plane->funcs->destroy(plane);
1161         }
1162
1163         list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1164                 crtc->funcs->destroy(crtc);
1165         }
1166
1167         list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
1168                                  head_global) {
1169                 drm_property_unreference_blob(blob);
1170         }
1171
1172         /*
1173          * Single-threaded teardown context, so it's not required to grab the
1174          * fb_lock to protect against concurrent fb_list access. Contrary, it
1175          * would actually deadlock with the drm_framebuffer_cleanup function.
1176          *
1177          * Also, if there are any framebuffers left, that's a driver leak now,
1178          * so politely WARN about this.
1179          */
1180         WARN_ON(!list_empty(&dev->mode_config.fb_list));
1181         list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1182                 drm_framebuffer_free(&fb->base.refcount);
1183         }
1184
1185         ida_destroy(&dev->mode_config.connector_ida);
1186         idr_destroy(&dev->mode_config.tile_idr);
1187         idr_destroy(&dev->mode_config.crtc_idr);
1188         drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
1189 }
1190 EXPORT_SYMBOL(drm_mode_config_cleanup);
1191
1192 /**
1193  * DOC: Tile group
1194  *
1195  * Tile groups are used to represent tiled monitors with a unique
1196  * integer identifier. Tiled monitors using DisplayID v1.3 have
1197  * a unique 8-byte handle, we store this in a tile group, so we
1198  * have a common identifier for all tiles in a monitor group.
1199  */
1200 static void drm_tile_group_free(struct kref *kref)
1201 {
1202         struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
1203         struct drm_device *dev = tg->dev;
1204         mutex_lock(&dev->mode_config.idr_mutex);
1205         idr_remove(&dev->mode_config.tile_idr, tg->id);
1206         mutex_unlock(&dev->mode_config.idr_mutex);
1207         kfree(tg);
1208 }
1209
1210 /**
1211  * drm_mode_put_tile_group - drop a reference to a tile group.
1212  * @dev: DRM device
1213  * @tg: tile group to drop reference to.
1214  *
1215  * drop reference to tile group and free if 0.
1216  */
1217 void drm_mode_put_tile_group(struct drm_device *dev,
1218                              struct drm_tile_group *tg)
1219 {
1220         kref_put(&tg->refcount, drm_tile_group_free);
1221 }
1222
1223 /**
1224  * drm_mode_get_tile_group - get a reference to an existing tile group
1225  * @dev: DRM device
1226  * @topology: 8-bytes unique per monitor.
1227  *
1228  * Use the unique bytes to get a reference to an existing tile group.
1229  *
1230  * RETURNS:
1231  * tile group or NULL if not found.
1232  */
1233 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
1234                                                char topology[8])
1235 {
1236         struct drm_tile_group *tg;
1237         int id;
1238         mutex_lock(&dev->mode_config.idr_mutex);
1239         idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
1240                 if (!memcmp(tg->group_data, topology, 8)) {
1241                         if (!kref_get_unless_zero(&tg->refcount))
1242                                 tg = NULL;
1243                         mutex_unlock(&dev->mode_config.idr_mutex);
1244                         return tg;
1245                 }
1246         }
1247         mutex_unlock(&dev->mode_config.idr_mutex);
1248         return NULL;
1249 }
1250 EXPORT_SYMBOL(drm_mode_get_tile_group);
1251
1252 /**
1253  * drm_mode_create_tile_group - create a tile group from a displayid description
1254  * @dev: DRM device
1255  * @topology: 8-bytes unique per monitor.
1256  *
1257  * Create a tile group for the unique monitor, and get a unique
1258  * identifier for the tile group.
1259  *
1260  * RETURNS:
1261  * new tile group or error.
1262  */
1263 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
1264                                                   char topology[8])
1265 {
1266         struct drm_tile_group *tg;
1267         int ret;
1268
1269         tg = kzalloc(sizeof(*tg), GFP_KERNEL);
1270         if (!tg)
1271                 return ERR_PTR(-ENOMEM);
1272
1273         kref_init(&tg->refcount);
1274         memcpy(tg->group_data, topology, 8);
1275         tg->dev = dev;
1276
1277         mutex_lock(&dev->mode_config.idr_mutex);
1278         ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
1279         if (ret >= 0) {
1280                 tg->id = ret;
1281         } else {
1282                 kfree(tg);
1283                 tg = ERR_PTR(ret);
1284         }
1285
1286         mutex_unlock(&dev->mode_config.idr_mutex);
1287         return tg;
1288 }
1289 EXPORT_SYMBOL(drm_mode_create_tile_group);