]> Git Repo - J-linux.git/blob - drivers/gpu/drm/drm_atomic.c
Merge tag 'topic/drmp-cleanup-2019-01-02' of git://anongit.freedesktop.org/drm/drm...
[J-linux.git] / drivers / gpu / drm / drm_atomic.c
1 /*
2  * Copyright (C) 2014 Red Hat
3  * Copyright (C) 2014 Intel Corp.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  * Rob Clark <[email protected]>
25  * Daniel Vetter <[email protected]>
26  */
27
28
29 #include <drm/drmP.h>
30 #include <drm/drm_atomic.h>
31 #include <drm/drm_atomic_uapi.h>
32 #include <drm/drm_mode.h>
33 #include <drm/drm_print.h>
34 #include <drm/drm_writeback.h>
35 #include <linux/sync_file.h>
36
37 #include "drm_crtc_internal.h"
38 #include "drm_internal.h"
39
40 void __drm_crtc_commit_free(struct kref *kref)
41 {
42         struct drm_crtc_commit *commit =
43                 container_of(kref, struct drm_crtc_commit, ref);
44
45         kfree(commit);
46 }
47 EXPORT_SYMBOL(__drm_crtc_commit_free);
48
49 /**
50  * drm_atomic_state_default_release -
51  * release memory initialized by drm_atomic_state_init
52  * @state: atomic state
53  *
54  * Free all the memory allocated by drm_atomic_state_init.
55  * This should only be used by drivers which are still subclassing
56  * &drm_atomic_state and haven't switched to &drm_private_state yet.
57  */
58 void drm_atomic_state_default_release(struct drm_atomic_state *state)
59 {
60         kfree(state->connectors);
61         kfree(state->crtcs);
62         kfree(state->planes);
63         kfree(state->private_objs);
64 }
65 EXPORT_SYMBOL(drm_atomic_state_default_release);
66
67 /**
68  * drm_atomic_state_init - init new atomic state
69  * @dev: DRM device
70  * @state: atomic state
71  *
72  * Default implementation for filling in a new atomic state.
73  * This should only be used by drivers which are still subclassing
74  * &drm_atomic_state and haven't switched to &drm_private_state yet.
75  */
76 int
77 drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
78 {
79         kref_init(&state->ref);
80
81         /* TODO legacy paths should maybe do a better job about
82          * setting this appropriately?
83          */
84         state->allow_modeset = true;
85
86         state->crtcs = kcalloc(dev->mode_config.num_crtc,
87                                sizeof(*state->crtcs), GFP_KERNEL);
88         if (!state->crtcs)
89                 goto fail;
90         state->planes = kcalloc(dev->mode_config.num_total_plane,
91                                 sizeof(*state->planes), GFP_KERNEL);
92         if (!state->planes)
93                 goto fail;
94
95         state->dev = dev;
96
97         DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
98
99         return 0;
100 fail:
101         drm_atomic_state_default_release(state);
102         return -ENOMEM;
103 }
104 EXPORT_SYMBOL(drm_atomic_state_init);
105
106 /**
107  * drm_atomic_state_alloc - allocate atomic state
108  * @dev: DRM device
109  *
110  * This allocates an empty atomic state to track updates.
111  */
112 struct drm_atomic_state *
113 drm_atomic_state_alloc(struct drm_device *dev)
114 {
115         struct drm_mode_config *config = &dev->mode_config;
116
117         if (!config->funcs->atomic_state_alloc) {
118                 struct drm_atomic_state *state;
119
120                 state = kzalloc(sizeof(*state), GFP_KERNEL);
121                 if (!state)
122                         return NULL;
123                 if (drm_atomic_state_init(dev, state) < 0) {
124                         kfree(state);
125                         return NULL;
126                 }
127                 return state;
128         }
129
130         return config->funcs->atomic_state_alloc(dev);
131 }
132 EXPORT_SYMBOL(drm_atomic_state_alloc);
133
134 /**
135  * drm_atomic_state_default_clear - clear base atomic state
136  * @state: atomic state
137  *
138  * Default implementation for clearing atomic state.
139  * This should only be used by drivers which are still subclassing
140  * &drm_atomic_state and haven't switched to &drm_private_state yet.
141  */
142 void drm_atomic_state_default_clear(struct drm_atomic_state *state)
143 {
144         struct drm_device *dev = state->dev;
145         struct drm_mode_config *config = &dev->mode_config;
146         int i;
147
148         DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
149
150         for (i = 0; i < state->num_connector; i++) {
151                 struct drm_connector *connector = state->connectors[i].ptr;
152
153                 if (!connector)
154                         continue;
155
156                 connector->funcs->atomic_destroy_state(connector,
157                                                        state->connectors[i].state);
158                 state->connectors[i].ptr = NULL;
159                 state->connectors[i].state = NULL;
160                 state->connectors[i].old_state = NULL;
161                 state->connectors[i].new_state = NULL;
162                 drm_connector_put(connector);
163         }
164
165         for (i = 0; i < config->num_crtc; i++) {
166                 struct drm_crtc *crtc = state->crtcs[i].ptr;
167
168                 if (!crtc)
169                         continue;
170
171                 crtc->funcs->atomic_destroy_state(crtc,
172                                                   state->crtcs[i].state);
173
174                 state->crtcs[i].ptr = NULL;
175                 state->crtcs[i].state = NULL;
176                 state->crtcs[i].old_state = NULL;
177                 state->crtcs[i].new_state = NULL;
178
179                 if (state->crtcs[i].commit) {
180                         drm_crtc_commit_put(state->crtcs[i].commit);
181                         state->crtcs[i].commit = NULL;
182                 }
183         }
184
185         for (i = 0; i < config->num_total_plane; i++) {
186                 struct drm_plane *plane = state->planes[i].ptr;
187
188                 if (!plane)
189                         continue;
190
191                 plane->funcs->atomic_destroy_state(plane,
192                                                    state->planes[i].state);
193                 state->planes[i].ptr = NULL;
194                 state->planes[i].state = NULL;
195                 state->planes[i].old_state = NULL;
196                 state->planes[i].new_state = NULL;
197         }
198
199         for (i = 0; i < state->num_private_objs; i++) {
200                 struct drm_private_obj *obj = state->private_objs[i].ptr;
201
202                 obj->funcs->atomic_destroy_state(obj,
203                                                  state->private_objs[i].state);
204                 state->private_objs[i].ptr = NULL;
205                 state->private_objs[i].state = NULL;
206                 state->private_objs[i].old_state = NULL;
207                 state->private_objs[i].new_state = NULL;
208         }
209         state->num_private_objs = 0;
210
211         if (state->fake_commit) {
212                 drm_crtc_commit_put(state->fake_commit);
213                 state->fake_commit = NULL;
214         }
215 }
216 EXPORT_SYMBOL(drm_atomic_state_default_clear);
217
218 /**
219  * drm_atomic_state_clear - clear state object
220  * @state: atomic state
221  *
222  * When the w/w mutex algorithm detects a deadlock we need to back off and drop
223  * all locks. So someone else could sneak in and change the current modeset
224  * configuration. Which means that all the state assembled in @state is no
225  * longer an atomic update to the current state, but to some arbitrary earlier
226  * state. Which could break assumptions the driver's
227  * &drm_mode_config_funcs.atomic_check likely relies on.
228  *
229  * Hence we must clear all cached state and completely start over, using this
230  * function.
231  */
232 void drm_atomic_state_clear(struct drm_atomic_state *state)
233 {
234         struct drm_device *dev = state->dev;
235         struct drm_mode_config *config = &dev->mode_config;
236
237         if (config->funcs->atomic_state_clear)
238                 config->funcs->atomic_state_clear(state);
239         else
240                 drm_atomic_state_default_clear(state);
241 }
242 EXPORT_SYMBOL(drm_atomic_state_clear);
243
244 /**
245  * __drm_atomic_state_free - free all memory for an atomic state
246  * @ref: This atomic state to deallocate
247  *
248  * This frees all memory associated with an atomic state, including all the
249  * per-object state for planes, crtcs and connectors.
250  */
251 void __drm_atomic_state_free(struct kref *ref)
252 {
253         struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
254         struct drm_mode_config *config = &state->dev->mode_config;
255
256         drm_atomic_state_clear(state);
257
258         DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
259
260         if (config->funcs->atomic_state_free) {
261                 config->funcs->atomic_state_free(state);
262         } else {
263                 drm_atomic_state_default_release(state);
264                 kfree(state);
265         }
266 }
267 EXPORT_SYMBOL(__drm_atomic_state_free);
268
269 /**
270  * drm_atomic_get_crtc_state - get crtc state
271  * @state: global atomic state object
272  * @crtc: crtc to get state object for
273  *
274  * This function returns the crtc state for the given crtc, allocating it if
275  * needed. It will also grab the relevant crtc lock to make sure that the state
276  * is consistent.
277  *
278  * Returns:
279  *
280  * Either the allocated state or the error code encoded into the pointer. When
281  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
282  * entire atomic sequence must be restarted. All other errors are fatal.
283  */
284 struct drm_crtc_state *
285 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
286                           struct drm_crtc *crtc)
287 {
288         int ret, index = drm_crtc_index(crtc);
289         struct drm_crtc_state *crtc_state;
290
291         WARN_ON(!state->acquire_ctx);
292
293         crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
294         if (crtc_state)
295                 return crtc_state;
296
297         ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
298         if (ret)
299                 return ERR_PTR(ret);
300
301         crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
302         if (!crtc_state)
303                 return ERR_PTR(-ENOMEM);
304
305         state->crtcs[index].state = crtc_state;
306         state->crtcs[index].old_state = crtc->state;
307         state->crtcs[index].new_state = crtc_state;
308         state->crtcs[index].ptr = crtc;
309         crtc_state->state = state;
310
311         DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
312                          crtc->base.id, crtc->name, crtc_state, state);
313
314         return crtc_state;
315 }
316 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
317
318 static int drm_atomic_crtc_check(const struct drm_crtc_state *old_crtc_state,
319                                  const struct drm_crtc_state *new_crtc_state)
320 {
321         struct drm_crtc *crtc = new_crtc_state->crtc;
322
323         /* NOTE: we explicitly don't enforce constraints such as primary
324          * layer covering entire screen, since that is something we want
325          * to allow (on hw that supports it).  For hw that does not, it
326          * should be checked in driver's crtc->atomic_check() vfunc.
327          *
328          * TODO: Add generic modeset state checks once we support those.
329          */
330
331         if (new_crtc_state->active && !new_crtc_state->enable) {
332                 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
333                                  crtc->base.id, crtc->name);
334                 return -EINVAL;
335         }
336
337         /* The state->enable vs. state->mode_blob checks can be WARN_ON,
338          * as this is a kernel-internal detail that userspace should never
339          * be able to trigger. */
340         if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
341             WARN_ON(new_crtc_state->enable && !new_crtc_state->mode_blob)) {
342                 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
343                                  crtc->base.id, crtc->name);
344                 return -EINVAL;
345         }
346
347         if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
348             WARN_ON(!new_crtc_state->enable && new_crtc_state->mode_blob)) {
349                 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
350                                  crtc->base.id, crtc->name);
351                 return -EINVAL;
352         }
353
354         /*
355          * Reject event generation for when a CRTC is off and stays off.
356          * It wouldn't be hard to implement this, but userspace has a track
357          * record of happily burning through 100% cpu (or worse, crash) when the
358          * display pipe is suspended. To avoid all that fun just reject updates
359          * that ask for events since likely that indicates a bug in the
360          * compositor's drawing loop. This is consistent with the vblank IOCTL
361          * and legacy page_flip IOCTL which also reject service on a disabled
362          * pipe.
363          */
364         if (new_crtc_state->event &&
365             !new_crtc_state->active && !old_crtc_state->active) {
366                 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requesting event but off\n",
367                                  crtc->base.id, crtc->name);
368                 return -EINVAL;
369         }
370
371         return 0;
372 }
373
374 static void drm_atomic_crtc_print_state(struct drm_printer *p,
375                 const struct drm_crtc_state *state)
376 {
377         struct drm_crtc *crtc = state->crtc;
378
379         drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
380         drm_printf(p, "\tenable=%d\n", state->enable);
381         drm_printf(p, "\tactive=%d\n", state->active);
382         drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
383         drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
384         drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
385         drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
386         drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
387         drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
388         drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
389         drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
390         drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
391
392         if (crtc->funcs->atomic_print_state)
393                 crtc->funcs->atomic_print_state(p, state);
394 }
395
396 static int drm_atomic_connector_check(struct drm_connector *connector,
397                 struct drm_connector_state *state)
398 {
399         struct drm_crtc_state *crtc_state;
400         struct drm_writeback_job *writeback_job = state->writeback_job;
401         const struct drm_display_info *info = &connector->display_info;
402
403         state->max_bpc = info->bpc ? info->bpc : 8;
404         if (connector->max_bpc_property)
405                 state->max_bpc = min(state->max_bpc, state->max_requested_bpc);
406
407         if ((connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK) || !writeback_job)
408                 return 0;
409
410         if (writeback_job->fb && !state->crtc) {
411                 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] framebuffer without CRTC\n",
412                                  connector->base.id, connector->name);
413                 return -EINVAL;
414         }
415
416         if (state->crtc)
417                 crtc_state = drm_atomic_get_existing_crtc_state(state->state,
418                                                                 state->crtc);
419
420         if (writeback_job->fb && !crtc_state->active) {
421                 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] has framebuffer, but [CRTC:%d] is off\n",
422                                  connector->base.id, connector->name,
423                                  state->crtc->base.id);
424                 return -EINVAL;
425         }
426
427         if (writeback_job->out_fence && !writeback_job->fb) {
428                 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] requesting out-fence without framebuffer\n",
429                                  connector->base.id, connector->name);
430                 return -EINVAL;
431         }
432
433         return 0;
434 }
435
436 /**
437  * drm_atomic_get_plane_state - get plane state
438  * @state: global atomic state object
439  * @plane: plane to get state object for
440  *
441  * This function returns the plane state for the given plane, allocating it if
442  * needed. It will also grab the relevant plane lock to make sure that the state
443  * is consistent.
444  *
445  * Returns:
446  *
447  * Either the allocated state or the error code encoded into the pointer. When
448  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
449  * entire atomic sequence must be restarted. All other errors are fatal.
450  */
451 struct drm_plane_state *
452 drm_atomic_get_plane_state(struct drm_atomic_state *state,
453                           struct drm_plane *plane)
454 {
455         int ret, index = drm_plane_index(plane);
456         struct drm_plane_state *plane_state;
457
458         WARN_ON(!state->acquire_ctx);
459
460         /* the legacy pointers should never be set */
461         WARN_ON(plane->fb);
462         WARN_ON(plane->old_fb);
463         WARN_ON(plane->crtc);
464
465         plane_state = drm_atomic_get_existing_plane_state(state, plane);
466         if (plane_state)
467                 return plane_state;
468
469         ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
470         if (ret)
471                 return ERR_PTR(ret);
472
473         plane_state = plane->funcs->atomic_duplicate_state(plane);
474         if (!plane_state)
475                 return ERR_PTR(-ENOMEM);
476
477         state->planes[index].state = plane_state;
478         state->planes[index].ptr = plane;
479         state->planes[index].old_state = plane->state;
480         state->planes[index].new_state = plane_state;
481         plane_state->state = state;
482
483         DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
484                          plane->base.id, plane->name, plane_state, state);
485
486         if (plane_state->crtc) {
487                 struct drm_crtc_state *crtc_state;
488
489                 crtc_state = drm_atomic_get_crtc_state(state,
490                                                        plane_state->crtc);
491                 if (IS_ERR(crtc_state))
492                         return ERR_CAST(crtc_state);
493         }
494
495         return plane_state;
496 }
497 EXPORT_SYMBOL(drm_atomic_get_plane_state);
498
499 static bool
500 plane_switching_crtc(const struct drm_plane_state *old_plane_state,
501                      const struct drm_plane_state *new_plane_state)
502 {
503         if (!old_plane_state->crtc || !new_plane_state->crtc)
504                 return false;
505
506         if (old_plane_state->crtc == new_plane_state->crtc)
507                 return false;
508
509         /* This could be refined, but currently there's no helper or driver code
510          * to implement direct switching of active planes nor userspace to take
511          * advantage of more direct plane switching without the intermediate
512          * full OFF state.
513          */
514         return true;
515 }
516
517 /**
518  * drm_atomic_plane_check - check plane state
519  * @old_plane_state: old plane state to check
520  * @new_plane_state: new plane state to check
521  *
522  * Provides core sanity checks for plane state.
523  *
524  * RETURNS:
525  * Zero on success, error code on failure
526  */
527 static int drm_atomic_plane_check(const struct drm_plane_state *old_plane_state,
528                                   const struct drm_plane_state *new_plane_state)
529 {
530         struct drm_plane *plane = new_plane_state->plane;
531         struct drm_crtc *crtc = new_plane_state->crtc;
532         const struct drm_framebuffer *fb = new_plane_state->fb;
533         unsigned int fb_width, fb_height;
534         int ret;
535
536         /* either *both* CRTC and FB must be set, or neither */
537         if (crtc && !fb) {
538                 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] CRTC set but no FB\n",
539                                  plane->base.id, plane->name);
540                 return -EINVAL;
541         } else if (fb && !crtc) {
542                 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] FB set but no CRTC\n",
543                                  plane->base.id, plane->name);
544                 return -EINVAL;
545         }
546
547         /* if disabled, we don't care about the rest of the state: */
548         if (!crtc)
549                 return 0;
550
551         /* Check whether this plane is usable on this CRTC */
552         if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
553                 DRM_DEBUG_ATOMIC("Invalid [CRTC:%d:%s] for [PLANE:%d:%s]\n",
554                                  crtc->base.id, crtc->name,
555                                  plane->base.id, plane->name);
556                 return -EINVAL;
557         }
558
559         /* Check whether this plane supports the fb pixel format. */
560         ret = drm_plane_check_pixel_format(plane, fb->format->format,
561                                            fb->modifier);
562         if (ret) {
563                 struct drm_format_name_buf format_name;
564                 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid pixel format %s, modifier 0x%llx\n",
565                                  plane->base.id, plane->name,
566                                  drm_get_format_name(fb->format->format,
567                                                      &format_name),
568                                  fb->modifier);
569                 return ret;
570         }
571
572         /* Give drivers some help against integer overflows */
573         if (new_plane_state->crtc_w > INT_MAX ||
574             new_plane_state->crtc_x > INT_MAX - (int32_t) new_plane_state->crtc_w ||
575             new_plane_state->crtc_h > INT_MAX ||
576             new_plane_state->crtc_y > INT_MAX - (int32_t) new_plane_state->crtc_h) {
577                 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid CRTC coordinates %ux%u+%d+%d\n",
578                                  plane->base.id, plane->name,
579                                  new_plane_state->crtc_w, new_plane_state->crtc_h,
580                                  new_plane_state->crtc_x, new_plane_state->crtc_y);
581                 return -ERANGE;
582         }
583
584         fb_width = fb->width << 16;
585         fb_height = fb->height << 16;
586
587         /* Make sure source coordinates are inside the fb. */
588         if (new_plane_state->src_w > fb_width ||
589             new_plane_state->src_x > fb_width - new_plane_state->src_w ||
590             new_plane_state->src_h > fb_height ||
591             new_plane_state->src_y > fb_height - new_plane_state->src_h) {
592                 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid source coordinates "
593                                  "%u.%06ux%u.%06u+%u.%06u+%u.%06u (fb %ux%u)\n",
594                                  plane->base.id, plane->name,
595                                  new_plane_state->src_w >> 16,
596                                  ((new_plane_state->src_w & 0xffff) * 15625) >> 10,
597                                  new_plane_state->src_h >> 16,
598                                  ((new_plane_state->src_h & 0xffff) * 15625) >> 10,
599                                  new_plane_state->src_x >> 16,
600                                  ((new_plane_state->src_x & 0xffff) * 15625) >> 10,
601                                  new_plane_state->src_y >> 16,
602                                  ((new_plane_state->src_y & 0xffff) * 15625) >> 10,
603                                  fb->width, fb->height);
604                 return -ENOSPC;
605         }
606
607         if (plane_switching_crtc(old_plane_state, new_plane_state)) {
608                 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
609                                  plane->base.id, plane->name);
610                 return -EINVAL;
611         }
612
613         return 0;
614 }
615
616 static void drm_atomic_plane_print_state(struct drm_printer *p,
617                 const struct drm_plane_state *state)
618 {
619         struct drm_plane *plane = state->plane;
620         struct drm_rect src  = drm_plane_state_src(state);
621         struct drm_rect dest = drm_plane_state_dest(state);
622
623         drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
624         drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
625         drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
626         if (state->fb)
627                 drm_framebuffer_print_info(p, 2, state->fb);
628         drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
629         drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
630         drm_printf(p, "\trotation=%x\n", state->rotation);
631         drm_printf(p, "\tnormalized-zpos=%x\n", state->normalized_zpos);
632         drm_printf(p, "\tcolor-encoding=%s\n",
633                    drm_get_color_encoding_name(state->color_encoding));
634         drm_printf(p, "\tcolor-range=%s\n",
635                    drm_get_color_range_name(state->color_range));
636
637         if (plane->funcs->atomic_print_state)
638                 plane->funcs->atomic_print_state(p, state);
639 }
640
641 /**
642  * DOC: handling driver private state
643  *
644  * Very often the DRM objects exposed to userspace in the atomic modeset api
645  * (&drm_connector, &drm_crtc and &drm_plane) do not map neatly to the
646  * underlying hardware. Especially for any kind of shared resources (e.g. shared
647  * clocks, scaler units, bandwidth and fifo limits shared among a group of
648  * planes or CRTCs, and so on) it makes sense to model these as independent
649  * objects. Drivers then need to do similar state tracking and commit ordering for
650  * such private (since not exposed to userpace) objects as the atomic core and
651  * helpers already provide for connectors, planes and CRTCs.
652  *
653  * To make this easier on drivers the atomic core provides some support to track
654  * driver private state objects using struct &drm_private_obj, with the
655  * associated state struct &drm_private_state.
656  *
657  * Similar to userspace-exposed objects, private state structures can be
658  * acquired by calling drm_atomic_get_private_obj_state(). Since this function
659  * does not take care of locking, drivers should wrap it for each type of
660  * private state object they have with the required call to drm_modeset_lock()
661  * for the corresponding &drm_modeset_lock.
662  *
663  * All private state structures contained in a &drm_atomic_state update can be
664  * iterated using for_each_oldnew_private_obj_in_state(),
665  * for_each_new_private_obj_in_state() and for_each_old_private_obj_in_state().
666  * Drivers are recommended to wrap these for each type of driver private state
667  * object they have, filtering on &drm_private_obj.funcs using for_each_if(), at
668  * least if they want to iterate over all objects of a given type.
669  *
670  * An earlier way to handle driver private state was by subclassing struct
671  * &drm_atomic_state. But since that encourages non-standard ways to implement
672  * the check/commit split atomic requires (by using e.g. "check and rollback or
673  * commit instead" of "duplicate state, check, then either commit or release
674  * duplicated state) it is deprecated in favour of using &drm_private_state.
675  */
676
677 /**
678  * drm_atomic_private_obj_init - initialize private object
679  * @dev: DRM device this object will be attached to
680  * @obj: private object
681  * @state: initial private object state
682  * @funcs: pointer to the struct of function pointers that identify the object
683  * type
684  *
685  * Initialize the private object, which can be embedded into any
686  * driver private object that needs its own atomic state.
687  */
688 void
689 drm_atomic_private_obj_init(struct drm_device *dev,
690                             struct drm_private_obj *obj,
691                             struct drm_private_state *state,
692                             const struct drm_private_state_funcs *funcs)
693 {
694         memset(obj, 0, sizeof(*obj));
695
696         drm_modeset_lock_init(&obj->lock);
697
698         obj->state = state;
699         obj->funcs = funcs;
700         list_add_tail(&obj->head, &dev->mode_config.privobj_list);
701 }
702 EXPORT_SYMBOL(drm_atomic_private_obj_init);
703
704 /**
705  * drm_atomic_private_obj_fini - finalize private object
706  * @obj: private object
707  *
708  * Finalize the private object.
709  */
710 void
711 drm_atomic_private_obj_fini(struct drm_private_obj *obj)
712 {
713         list_del(&obj->head);
714         obj->funcs->atomic_destroy_state(obj, obj->state);
715         drm_modeset_lock_fini(&obj->lock);
716 }
717 EXPORT_SYMBOL(drm_atomic_private_obj_fini);
718
719 /**
720  * drm_atomic_get_private_obj_state - get private object state
721  * @state: global atomic state
722  * @obj: private object to get the state for
723  *
724  * This function returns the private object state for the given private object,
725  * allocating the state if needed. It will also grab the relevant private
726  * object lock to make sure that the state is consistent.
727  *
728  * RETURNS:
729  *
730  * Either the allocated state or the error code encoded into a pointer.
731  */
732 struct drm_private_state *
733 drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
734                                  struct drm_private_obj *obj)
735 {
736         int index, num_objs, i, ret;
737         size_t size;
738         struct __drm_private_objs_state *arr;
739         struct drm_private_state *obj_state;
740
741         for (i = 0; i < state->num_private_objs; i++)
742                 if (obj == state->private_objs[i].ptr)
743                         return state->private_objs[i].state;
744
745         ret = drm_modeset_lock(&obj->lock, state->acquire_ctx);
746         if (ret)
747                 return ERR_PTR(ret);
748
749         num_objs = state->num_private_objs + 1;
750         size = sizeof(*state->private_objs) * num_objs;
751         arr = krealloc(state->private_objs, size, GFP_KERNEL);
752         if (!arr)
753                 return ERR_PTR(-ENOMEM);
754
755         state->private_objs = arr;
756         index = state->num_private_objs;
757         memset(&state->private_objs[index], 0, sizeof(*state->private_objs));
758
759         obj_state = obj->funcs->atomic_duplicate_state(obj);
760         if (!obj_state)
761                 return ERR_PTR(-ENOMEM);
762
763         state->private_objs[index].state = obj_state;
764         state->private_objs[index].old_state = obj->state;
765         state->private_objs[index].new_state = obj_state;
766         state->private_objs[index].ptr = obj;
767         obj_state->state = state;
768
769         state->num_private_objs = num_objs;
770
771         DRM_DEBUG_ATOMIC("Added new private object %p state %p to %p\n",
772                          obj, obj_state, state);
773
774         return obj_state;
775 }
776 EXPORT_SYMBOL(drm_atomic_get_private_obj_state);
777
778 /**
779  * drm_atomic_get_connector_state - get connector state
780  * @state: global atomic state object
781  * @connector: connector to get state object for
782  *
783  * This function returns the connector state for the given connector,
784  * allocating it if needed. It will also grab the relevant connector lock to
785  * make sure that the state is consistent.
786  *
787  * Returns:
788  *
789  * Either the allocated state or the error code encoded into the pointer. When
790  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
791  * entire atomic sequence must be restarted. All other errors are fatal.
792  */
793 struct drm_connector_state *
794 drm_atomic_get_connector_state(struct drm_atomic_state *state,
795                           struct drm_connector *connector)
796 {
797         int ret, index;
798         struct drm_mode_config *config = &connector->dev->mode_config;
799         struct drm_connector_state *connector_state;
800
801         WARN_ON(!state->acquire_ctx);
802
803         ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
804         if (ret)
805                 return ERR_PTR(ret);
806
807         index = drm_connector_index(connector);
808
809         if (index >= state->num_connector) {
810                 struct __drm_connnectors_state *c;
811                 int alloc = max(index + 1, config->num_connector);
812
813                 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
814                 if (!c)
815                         return ERR_PTR(-ENOMEM);
816
817                 state->connectors = c;
818                 memset(&state->connectors[state->num_connector], 0,
819                        sizeof(*state->connectors) * (alloc - state->num_connector));
820
821                 state->num_connector = alloc;
822         }
823
824         if (state->connectors[index].state)
825                 return state->connectors[index].state;
826
827         connector_state = connector->funcs->atomic_duplicate_state(connector);
828         if (!connector_state)
829                 return ERR_PTR(-ENOMEM);
830
831         drm_connector_get(connector);
832         state->connectors[index].state = connector_state;
833         state->connectors[index].old_state = connector->state;
834         state->connectors[index].new_state = connector_state;
835         state->connectors[index].ptr = connector;
836         connector_state->state = state;
837
838         DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d:%s] %p state to %p\n",
839                          connector->base.id, connector->name,
840                          connector_state, state);
841
842         if (connector_state->crtc) {
843                 struct drm_crtc_state *crtc_state;
844
845                 crtc_state = drm_atomic_get_crtc_state(state,
846                                                        connector_state->crtc);
847                 if (IS_ERR(crtc_state))
848                         return ERR_CAST(crtc_state);
849         }
850
851         return connector_state;
852 }
853 EXPORT_SYMBOL(drm_atomic_get_connector_state);
854
855 static void drm_atomic_connector_print_state(struct drm_printer *p,
856                 const struct drm_connector_state *state)
857 {
858         struct drm_connector *connector = state->connector;
859
860         drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
861         drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
862
863         if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
864                 if (state->writeback_job && state->writeback_job->fb)
865                         drm_printf(p, "\tfb=%d\n", state->writeback_job->fb->base.id);
866
867         if (connector->funcs->atomic_print_state)
868                 connector->funcs->atomic_print_state(p, state);
869 }
870
871 /**
872  * drm_atomic_add_affected_connectors - add connectors for crtc
873  * @state: atomic state
874  * @crtc: DRM crtc
875  *
876  * This function walks the current configuration and adds all connectors
877  * currently using @crtc to the atomic configuration @state. Note that this
878  * function must acquire the connection mutex. This can potentially cause
879  * unneeded seralization if the update is just for the planes on one crtc. Hence
880  * drivers and helpers should only call this when really needed (e.g. when a
881  * full modeset needs to happen due to some change).
882  *
883  * Returns:
884  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
885  * then the w/w mutex code has detected a deadlock and the entire atomic
886  * sequence must be restarted. All other errors are fatal.
887  */
888 int
889 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
890                                    struct drm_crtc *crtc)
891 {
892         struct drm_mode_config *config = &state->dev->mode_config;
893         struct drm_connector *connector;
894         struct drm_connector_state *conn_state;
895         struct drm_connector_list_iter conn_iter;
896         struct drm_crtc_state *crtc_state;
897         int ret;
898
899         crtc_state = drm_atomic_get_crtc_state(state, crtc);
900         if (IS_ERR(crtc_state))
901                 return PTR_ERR(crtc_state);
902
903         ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
904         if (ret)
905                 return ret;
906
907         DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
908                          crtc->base.id, crtc->name, state);
909
910         /*
911          * Changed connectors are already in @state, so only need to look
912          * at the connector_mask in crtc_state.
913          */
914         drm_connector_list_iter_begin(state->dev, &conn_iter);
915         drm_for_each_connector_iter(connector, &conn_iter) {
916                 if (!(crtc_state->connector_mask & drm_connector_mask(connector)))
917                         continue;
918
919                 conn_state = drm_atomic_get_connector_state(state, connector);
920                 if (IS_ERR(conn_state)) {
921                         drm_connector_list_iter_end(&conn_iter);
922                         return PTR_ERR(conn_state);
923                 }
924         }
925         drm_connector_list_iter_end(&conn_iter);
926
927         return 0;
928 }
929 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
930
931 /**
932  * drm_atomic_add_affected_planes - add planes for crtc
933  * @state: atomic state
934  * @crtc: DRM crtc
935  *
936  * This function walks the current configuration and adds all planes
937  * currently used by @crtc to the atomic configuration @state. This is useful
938  * when an atomic commit also needs to check all currently enabled plane on
939  * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
940  * to avoid special code to force-enable all planes.
941  *
942  * Since acquiring a plane state will always also acquire the w/w mutex of the
943  * current CRTC for that plane (if there is any) adding all the plane states for
944  * a CRTC will not reduce parallism of atomic updates.
945  *
946  * Returns:
947  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
948  * then the w/w mutex code has detected a deadlock and the entire atomic
949  * sequence must be restarted. All other errors are fatal.
950  */
951 int
952 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
953                                struct drm_crtc *crtc)
954 {
955         const struct drm_crtc_state *old_crtc_state =
956                 drm_atomic_get_old_crtc_state(state, crtc);
957         struct drm_plane *plane;
958
959         WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc));
960
961         DRM_DEBUG_ATOMIC("Adding all current planes for [CRTC:%d:%s] to %p\n",
962                          crtc->base.id, crtc->name, state);
963
964         drm_for_each_plane_mask(plane, state->dev, old_crtc_state->plane_mask) {
965                 struct drm_plane_state *plane_state =
966                         drm_atomic_get_plane_state(state, plane);
967
968                 if (IS_ERR(plane_state))
969                         return PTR_ERR(plane_state);
970         }
971         return 0;
972 }
973 EXPORT_SYMBOL(drm_atomic_add_affected_planes);
974
975 /**
976  * drm_atomic_check_only - check whether a given config would work
977  * @state: atomic configuration to check
978  *
979  * Note that this function can return -EDEADLK if the driver needed to acquire
980  * more locks but encountered a deadlock. The caller must then do the usual w/w
981  * backoff dance and restart. All other errors are fatal.
982  *
983  * Returns:
984  * 0 on success, negative error code on failure.
985  */
986 int drm_atomic_check_only(struct drm_atomic_state *state)
987 {
988         struct drm_device *dev = state->dev;
989         struct drm_mode_config *config = &dev->mode_config;
990         struct drm_plane *plane;
991         struct drm_plane_state *old_plane_state;
992         struct drm_plane_state *new_plane_state;
993         struct drm_crtc *crtc;
994         struct drm_crtc_state *old_crtc_state;
995         struct drm_crtc_state *new_crtc_state;
996         struct drm_connector *conn;
997         struct drm_connector_state *conn_state;
998         int i, ret = 0;
999
1000         DRM_DEBUG_ATOMIC("checking %p\n", state);
1001
1002         for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
1003                 ret = drm_atomic_plane_check(old_plane_state, new_plane_state);
1004                 if (ret) {
1005                         DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1006                                          plane->base.id, plane->name);
1007                         return ret;
1008                 }
1009         }
1010
1011         for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1012                 ret = drm_atomic_crtc_check(old_crtc_state, new_crtc_state);
1013                 if (ret) {
1014                         DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1015                                          crtc->base.id, crtc->name);
1016                         return ret;
1017                 }
1018         }
1019
1020         for_each_new_connector_in_state(state, conn, conn_state, i) {
1021                 ret = drm_atomic_connector_check(conn, conn_state);
1022                 if (ret) {
1023                         DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] atomic core check failed\n",
1024                                          conn->base.id, conn->name);
1025                         return ret;
1026                 }
1027         }
1028
1029         if (config->funcs->atomic_check) {
1030                 ret = config->funcs->atomic_check(state->dev, state);
1031
1032                 if (ret) {
1033                         DRM_DEBUG_ATOMIC("atomic driver check for %p failed: %d\n",
1034                                          state, ret);
1035                         return ret;
1036                 }
1037         }
1038
1039         if (!state->allow_modeset) {
1040                 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1041                         if (drm_atomic_crtc_needs_modeset(new_crtc_state)) {
1042                                 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1043                                                  crtc->base.id, crtc->name);
1044                                 return -EINVAL;
1045                         }
1046                 }
1047         }
1048
1049         return 0;
1050 }
1051 EXPORT_SYMBOL(drm_atomic_check_only);
1052
1053 /**
1054  * drm_atomic_commit - commit configuration atomically
1055  * @state: atomic configuration to check
1056  *
1057  * Note that this function can return -EDEADLK if the driver needed to acquire
1058  * more locks but encountered a deadlock. The caller must then do the usual w/w
1059  * backoff dance and restart. All other errors are fatal.
1060  *
1061  * This function will take its own reference on @state.
1062  * Callers should always release their reference with drm_atomic_state_put().
1063  *
1064  * Returns:
1065  * 0 on success, negative error code on failure.
1066  */
1067 int drm_atomic_commit(struct drm_atomic_state *state)
1068 {
1069         struct drm_mode_config *config = &state->dev->mode_config;
1070         int ret;
1071
1072         ret = drm_atomic_check_only(state);
1073         if (ret)
1074                 return ret;
1075
1076         DRM_DEBUG_ATOMIC("committing %p\n", state);
1077
1078         return config->funcs->atomic_commit(state->dev, state, false);
1079 }
1080 EXPORT_SYMBOL(drm_atomic_commit);
1081
1082 /**
1083  * drm_atomic_nonblocking_commit - atomic nonblocking commit
1084  * @state: atomic configuration to check
1085  *
1086  * Note that this function can return -EDEADLK if the driver needed to acquire
1087  * more locks but encountered a deadlock. The caller must then do the usual w/w
1088  * backoff dance and restart. All other errors are fatal.
1089  *
1090  * This function will take its own reference on @state.
1091  * Callers should always release their reference with drm_atomic_state_put().
1092  *
1093  * Returns:
1094  * 0 on success, negative error code on failure.
1095  */
1096 int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
1097 {
1098         struct drm_mode_config *config = &state->dev->mode_config;
1099         int ret;
1100
1101         ret = drm_atomic_check_only(state);
1102         if (ret)
1103                 return ret;
1104
1105         DRM_DEBUG_ATOMIC("committing %p nonblocking\n", state);
1106
1107         return config->funcs->atomic_commit(state->dev, state, true);
1108 }
1109 EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
1110
1111 void drm_atomic_print_state(const struct drm_atomic_state *state)
1112 {
1113         struct drm_printer p = drm_info_printer(state->dev->dev);
1114         struct drm_plane *plane;
1115         struct drm_plane_state *plane_state;
1116         struct drm_crtc *crtc;
1117         struct drm_crtc_state *crtc_state;
1118         struct drm_connector *connector;
1119         struct drm_connector_state *connector_state;
1120         int i;
1121
1122         DRM_DEBUG_ATOMIC("checking %p\n", state);
1123
1124         for_each_new_plane_in_state(state, plane, plane_state, i)
1125                 drm_atomic_plane_print_state(&p, plane_state);
1126
1127         for_each_new_crtc_in_state(state, crtc, crtc_state, i)
1128                 drm_atomic_crtc_print_state(&p, crtc_state);
1129
1130         for_each_new_connector_in_state(state, connector, connector_state, i)
1131                 drm_atomic_connector_print_state(&p, connector_state);
1132 }
1133
1134 static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p,
1135                              bool take_locks)
1136 {
1137         struct drm_mode_config *config = &dev->mode_config;
1138         struct drm_plane *plane;
1139         struct drm_crtc *crtc;
1140         struct drm_connector *connector;
1141         struct drm_connector_list_iter conn_iter;
1142
1143         if (!drm_drv_uses_atomic_modeset(dev))
1144                 return;
1145
1146         list_for_each_entry(plane, &config->plane_list, head) {
1147                 if (take_locks)
1148                         drm_modeset_lock(&plane->mutex, NULL);
1149                 drm_atomic_plane_print_state(p, plane->state);
1150                 if (take_locks)
1151                         drm_modeset_unlock(&plane->mutex);
1152         }
1153
1154         list_for_each_entry(crtc, &config->crtc_list, head) {
1155                 if (take_locks)
1156                         drm_modeset_lock(&crtc->mutex, NULL);
1157                 drm_atomic_crtc_print_state(p, crtc->state);
1158                 if (take_locks)
1159                         drm_modeset_unlock(&crtc->mutex);
1160         }
1161
1162         drm_connector_list_iter_begin(dev, &conn_iter);
1163         if (take_locks)
1164                 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1165         drm_for_each_connector_iter(connector, &conn_iter)
1166                 drm_atomic_connector_print_state(p, connector->state);
1167         if (take_locks)
1168                 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1169         drm_connector_list_iter_end(&conn_iter);
1170 }
1171
1172 /**
1173  * drm_state_dump - dump entire device atomic state
1174  * @dev: the drm device
1175  * @p: where to print the state to
1176  *
1177  * Just for debugging.  Drivers might want an option to dump state
1178  * to dmesg in case of error irq's.  (Hint, you probably want to
1179  * ratelimit this!)
1180  *
1181  * The caller must drm_modeset_lock_all(), or if this is called
1182  * from error irq handler, it should not be enabled by default.
1183  * (Ie. if you are debugging errors you might not care that this
1184  * is racey.  But calling this without all modeset locks held is
1185  * not inherently safe.)
1186  */
1187 void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
1188 {
1189         __drm_state_dump(dev, p, false);
1190 }
1191 EXPORT_SYMBOL(drm_state_dump);
1192
1193 #ifdef CONFIG_DEBUG_FS
1194 static int drm_state_info(struct seq_file *m, void *data)
1195 {
1196         struct drm_info_node *node = (struct drm_info_node *) m->private;
1197         struct drm_device *dev = node->minor->dev;
1198         struct drm_printer p = drm_seq_file_printer(m);
1199
1200         __drm_state_dump(dev, &p, true);
1201
1202         return 0;
1203 }
1204
1205 /* any use in debugfs files to dump individual planes/crtc/etc? */
1206 static const struct drm_info_list drm_atomic_debugfs_list[] = {
1207         {"state", drm_state_info, 0},
1208 };
1209
1210 int drm_atomic_debugfs_init(struct drm_minor *minor)
1211 {
1212         return drm_debugfs_create_files(drm_atomic_debugfs_list,
1213                         ARRAY_SIZE(drm_atomic_debugfs_list),
1214                         minor->debugfs_root, minor);
1215 }
1216 #endif
1217
This page took 0.100851 seconds and 4 git commands to generate.