1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include <drm/drmP.h>
30#include <drm/drm_atomic.h>
31#include <drm/drm_plane_helper.h>
32
33
34
35
36
37
38
39
40
41void drm_atomic_state_default_release(struct drm_atomic_state *state)
42{
43 kfree(state->connectors);
44 kfree(state->connector_states);
45 kfree(state->crtcs);
46 kfree(state->crtc_states);
47 kfree(state->planes);
48 kfree(state->plane_states);
49}
50EXPORT_SYMBOL(drm_atomic_state_default_release);
51
52
53
54
55
56
57
58
59
60int
61drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
62{
63
64
65
66 state->allow_modeset = true;
67
68 state->crtcs = kcalloc(dev->mode_config.num_crtc,
69 sizeof(*state->crtcs), GFP_KERNEL);
70 if (!state->crtcs)
71 goto fail;
72 state->crtc_states = kcalloc(dev->mode_config.num_crtc,
73 sizeof(*state->crtc_states), GFP_KERNEL);
74 if (!state->crtc_states)
75 goto fail;
76 state->planes = kcalloc(dev->mode_config.num_total_plane,
77 sizeof(*state->planes), GFP_KERNEL);
78 if (!state->planes)
79 goto fail;
80 state->plane_states = kcalloc(dev->mode_config.num_total_plane,
81 sizeof(*state->plane_states), GFP_KERNEL);
82 if (!state->plane_states)
83 goto fail;
84
85 state->dev = dev;
86
87 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
88
89 return 0;
90fail:
91 drm_atomic_state_default_release(state);
92 return -ENOMEM;
93}
94EXPORT_SYMBOL(drm_atomic_state_init);
95
96
97
98
99
100
101
102struct drm_atomic_state *
103drm_atomic_state_alloc(struct drm_device *dev)
104{
105 struct drm_mode_config *config = &dev->mode_config;
106 struct drm_atomic_state *state;
107
108 if (!config->funcs->atomic_state_alloc) {
109 state = kzalloc(sizeof(*state), GFP_KERNEL);
110 if (!state)
111 return NULL;
112 if (drm_atomic_state_init(dev, state) < 0) {
113 kfree(state);
114 return NULL;
115 }
116 return state;
117 }
118
119 return config->funcs->atomic_state_alloc(dev);
120}
121EXPORT_SYMBOL(drm_atomic_state_alloc);
122
123
124
125
126
127
128
129
130void drm_atomic_state_default_clear(struct drm_atomic_state *state)
131{
132 struct drm_device *dev = state->dev;
133 struct drm_mode_config *config = &dev->mode_config;
134 int i;
135
136 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
137
138 for (i = 0; i < state->num_connector; i++) {
139 struct drm_connector *connector = state->connectors[i];
140
141 if (!connector)
142 continue;
143
144
145
146
147
148
149
150
151 state->connector_states[i]->connector = NULL;
152 connector->funcs->atomic_destroy_state(NULL,
153 state->connector_states[i]);
154 state->connectors[i] = NULL;
155 state->connector_states[i] = NULL;
156 }
157
158 for (i = 0; i < config->num_crtc; i++) {
159 struct drm_crtc *crtc = state->crtcs[i];
160
161 if (!crtc)
162 continue;
163
164 crtc->funcs->atomic_destroy_state(crtc,
165 state->crtc_states[i]);
166 state->crtcs[i] = NULL;
167 state->crtc_states[i] = NULL;
168 }
169
170 for (i = 0; i < config->num_total_plane; i++) {
171 struct drm_plane *plane = state->planes[i];
172
173 if (!plane)
174 continue;
175
176 plane->funcs->atomic_destroy_state(plane,
177 state->plane_states[i]);
178 state->planes[i] = NULL;
179 state->plane_states[i] = NULL;
180 }
181}
182EXPORT_SYMBOL(drm_atomic_state_default_clear);
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198void drm_atomic_state_clear(struct drm_atomic_state *state)
199{
200 struct drm_device *dev = state->dev;
201 struct drm_mode_config *config = &dev->mode_config;
202
203 if (config->funcs->atomic_state_clear)
204 config->funcs->atomic_state_clear(state);
205 else
206 drm_atomic_state_default_clear(state);
207}
208EXPORT_SYMBOL(drm_atomic_state_clear);
209
210
211
212
213
214
215
216
217void drm_atomic_state_free(struct drm_atomic_state *state)
218{
219 struct drm_device *dev;
220 struct drm_mode_config *config;
221
222 if (!state)
223 return;
224
225 dev = state->dev;
226 config = &dev->mode_config;
227
228 drm_atomic_state_clear(state);
229
230 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
231
232 if (config->funcs->atomic_state_free) {
233 config->funcs->atomic_state_free(state);
234 } else {
235 drm_atomic_state_default_release(state);
236 kfree(state);
237 }
238}
239EXPORT_SYMBOL(drm_atomic_state_free);
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256struct drm_crtc_state *
257drm_atomic_get_crtc_state(struct drm_atomic_state *state,
258 struct drm_crtc *crtc)
259{
260 int ret, index = drm_crtc_index(crtc);
261 struct drm_crtc_state *crtc_state;
262
263 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
264 if (crtc_state)
265 return crtc_state;
266
267 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
268 if (ret)
269 return ERR_PTR(ret);
270
271 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
272 if (!crtc_state)
273 return ERR_PTR(-ENOMEM);
274
275 state->crtc_states[index] = crtc_state;
276 state->crtcs[index] = crtc;
277 crtc_state->state = state;
278
279 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
280 crtc->base.id, crtc->name, crtc_state, state);
281
282 return crtc_state;
283}
284EXPORT_SYMBOL(drm_atomic_get_crtc_state);
285
286
287
288
289
290
291
292
293
294
295
296
297
298int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
299 struct drm_display_mode *mode)
300{
301 struct drm_mode_modeinfo umode;
302
303
304 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
305 return 0;
306
307 drm_property_unreference_blob(state->mode_blob);
308 state->mode_blob = NULL;
309
310 if (mode) {
311 drm_mode_convert_to_umode(&umode, mode);
312 state->mode_blob =
313 drm_property_create_blob(state->crtc->dev,
314 sizeof(umode),
315 &umode);
316 if (IS_ERR(state->mode_blob))
317 return PTR_ERR(state->mode_blob);
318
319 drm_mode_copy(&state->mode, mode);
320 state->enable = true;
321 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
322 mode->name, state);
323 } else {
324 memset(&state->mode, 0, sizeof(state->mode));
325 state->enable = false;
326 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
327 state);
328 }
329
330 return 0;
331}
332EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
348 struct drm_property_blob *blob)
349{
350 if (blob == state->mode_blob)
351 return 0;
352
353 drm_property_unreference_blob(state->mode_blob);
354 state->mode_blob = NULL;
355
356 if (blob) {
357 if (blob->length != sizeof(struct drm_mode_modeinfo) ||
358 drm_mode_convert_umode(&state->mode,
359 (const struct drm_mode_modeinfo *)
360 blob->data))
361 return -EINVAL;
362
363 state->mode_blob = drm_property_reference_blob(blob);
364 state->enable = true;
365 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
366 state->mode.name, state);
367 } else {
368 memset(&state->mode, 0, sizeof(state->mode));
369 state->enable = false;
370 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
371 state);
372 }
373
374 return 0;
375}
376EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
395 struct drm_crtc_state *state, struct drm_property *property,
396 uint64_t val)
397{
398 struct drm_device *dev = crtc->dev;
399 struct drm_mode_config *config = &dev->mode_config;
400 int ret;
401
402 if (property == config->prop_active)
403 state->active = val;
404 else if (property == config->prop_mode_id) {
405 struct drm_property_blob *mode =
406 drm_property_lookup_blob(dev, val);
407 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
408 drm_property_unreference_blob(mode);
409 return ret;
410 }
411 else if (crtc->funcs->atomic_set_property)
412 return crtc->funcs->atomic_set_property(crtc, state, property, val);
413 else
414 return -EINVAL;
415
416 return 0;
417}
418EXPORT_SYMBOL(drm_atomic_crtc_set_property);
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435static int
436drm_atomic_crtc_get_property(struct drm_crtc *crtc,
437 const struct drm_crtc_state *state,
438 struct drm_property *property, uint64_t *val)
439{
440 struct drm_device *dev = crtc->dev;
441 struct drm_mode_config *config = &dev->mode_config;
442
443 if (property == config->prop_active)
444 *val = state->active;
445 else if (property == config->prop_mode_id)
446 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
447 else if (crtc->funcs->atomic_get_property)
448 return crtc->funcs->atomic_get_property(crtc, state, property, val);
449 else
450 return -EINVAL;
451
452 return 0;
453}
454
455
456
457
458
459
460
461
462
463
464
465static int drm_atomic_crtc_check(struct drm_crtc *crtc,
466 struct drm_crtc_state *state)
467{
468
469
470
471
472
473
474
475
476 if (state->active && !state->enable) {
477 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
478 crtc->base.id, crtc->name);
479 return -EINVAL;
480 }
481
482
483
484
485 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
486 WARN_ON(state->enable && !state->mode_blob)) {
487 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
488 crtc->base.id, crtc->name);
489 return -EINVAL;
490 }
491
492 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
493 WARN_ON(!state->enable && state->mode_blob)) {
494 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
495 crtc->base.id, crtc->name);
496 return -EINVAL;
497 }
498
499
500
501
502
503
504
505
506
507
508
509 if (state->event && !state->active && !crtc->state->active) {
510 DRM_DEBUG_ATOMIC("[CRTC:%d] requesting event but off\n",
511 crtc->base.id);
512 return -EINVAL;
513 }
514
515 return 0;
516}
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533struct drm_plane_state *
534drm_atomic_get_plane_state(struct drm_atomic_state *state,
535 struct drm_plane *plane)
536{
537 int ret, index = drm_plane_index(plane);
538 struct drm_plane_state *plane_state;
539
540 plane_state = drm_atomic_get_existing_plane_state(state, plane);
541 if (plane_state)
542 return plane_state;
543
544 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
545 if (ret)
546 return ERR_PTR(ret);
547
548 plane_state = plane->funcs->atomic_duplicate_state(plane);
549 if (!plane_state)
550 return ERR_PTR(-ENOMEM);
551
552 state->plane_states[index] = plane_state;
553 state->planes[index] = plane;
554 plane_state->state = state;
555
556 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
557 plane->base.id, plane->name, plane_state, state);
558
559 if (plane_state->crtc) {
560 struct drm_crtc_state *crtc_state;
561
562 crtc_state = drm_atomic_get_crtc_state(state,
563 plane_state->crtc);
564 if (IS_ERR(crtc_state))
565 return ERR_CAST(crtc_state);
566 }
567
568 return plane_state;
569}
570EXPORT_SYMBOL(drm_atomic_get_plane_state);
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588int drm_atomic_plane_set_property(struct drm_plane *plane,
589 struct drm_plane_state *state, struct drm_property *property,
590 uint64_t val)
591{
592 struct drm_device *dev = plane->dev;
593 struct drm_mode_config *config = &dev->mode_config;
594
595 if (property == config->prop_fb_id) {
596 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
597 drm_atomic_set_fb_for_plane(state, fb);
598 if (fb)
599 drm_framebuffer_unreference(fb);
600 } else if (property == config->prop_crtc_id) {
601 struct drm_crtc *crtc = drm_crtc_find(dev, val);
602 return drm_atomic_set_crtc_for_plane(state, crtc);
603 } else if (property == config->prop_crtc_x) {
604 state->crtc_x = U642I64(val);
605 } else if (property == config->prop_crtc_y) {
606 state->crtc_y = U642I64(val);
607 } else if (property == config->prop_crtc_w) {
608 state->crtc_w = val;
609 } else if (property == config->prop_crtc_h) {
610 state->crtc_h = val;
611 } else if (property == config->prop_src_x) {
612 state->src_x = val;
613 } else if (property == config->prop_src_y) {
614 state->src_y = val;
615 } else if (property == config->prop_src_w) {
616 state->src_w = val;
617 } else if (property == config->prop_src_h) {
618 state->src_h = val;
619 } else if (property == config->rotation_property) {
620 state->rotation = val;
621 } else if (plane->funcs->atomic_set_property) {
622 return plane->funcs->atomic_set_property(plane, state,
623 property, val);
624 } else {
625 return -EINVAL;
626 }
627
628 return 0;
629}
630EXPORT_SYMBOL(drm_atomic_plane_set_property);
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647static int
648drm_atomic_plane_get_property(struct drm_plane *plane,
649 const struct drm_plane_state *state,
650 struct drm_property *property, uint64_t *val)
651{
652 struct drm_device *dev = plane->dev;
653 struct drm_mode_config *config = &dev->mode_config;
654
655 if (property == config->prop_fb_id) {
656 *val = (state->fb) ? state->fb->base.id : 0;
657 } else if (property == config->prop_crtc_id) {
658 *val = (state->crtc) ? state->crtc->base.id : 0;
659 } else if (property == config->prop_crtc_x) {
660 *val = I642U64(state->crtc_x);
661 } else if (property == config->prop_crtc_y) {
662 *val = I642U64(state->crtc_y);
663 } else if (property == config->prop_crtc_w) {
664 *val = state->crtc_w;
665 } else if (property == config->prop_crtc_h) {
666 *val = state->crtc_h;
667 } else if (property == config->prop_src_x) {
668 *val = state->src_x;
669 } else if (property == config->prop_src_y) {
670 *val = state->src_y;
671 } else if (property == config->prop_src_w) {
672 *val = state->src_w;
673 } else if (property == config->prop_src_h) {
674 *val = state->src_h;
675 } else if (property == config->rotation_property) {
676 *val = state->rotation;
677 } else if (plane->funcs->atomic_get_property) {
678 return plane->funcs->atomic_get_property(plane, state, property, val);
679 } else {
680 return -EINVAL;
681 }
682
683 return 0;
684}
685
686static bool
687plane_switching_crtc(struct drm_atomic_state *state,
688 struct drm_plane *plane,
689 struct drm_plane_state *plane_state)
690{
691 if (!plane->state->crtc || !plane_state->crtc)
692 return false;
693
694 if (plane->state->crtc == plane_state->crtc)
695 return false;
696
697
698
699
700
701
702 return true;
703}
704
705
706
707
708
709
710
711
712
713
714
715static int drm_atomic_plane_check(struct drm_plane *plane,
716 struct drm_plane_state *state)
717{
718 unsigned int fb_width, fb_height;
719 int ret;
720
721
722 if (WARN_ON(state->crtc && !state->fb)) {
723 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
724 return -EINVAL;
725 } else if (WARN_ON(state->fb && !state->crtc)) {
726 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
727 return -EINVAL;
728 }
729
730
731 if (!state->crtc)
732 return 0;
733
734
735 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
736 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
737 return -EINVAL;
738 }
739
740
741 ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
742 if (ret) {
743 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
744 drm_get_format_name(state->fb->pixel_format));
745 return ret;
746 }
747
748
749 if (state->crtc_w > INT_MAX ||
750 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
751 state->crtc_h > INT_MAX ||
752 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
753 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
754 state->crtc_w, state->crtc_h,
755 state->crtc_x, state->crtc_y);
756 return -ERANGE;
757 }
758
759 fb_width = state->fb->width << 16;
760 fb_height = state->fb->height << 16;
761
762
763 if (state->src_w > fb_width ||
764 state->src_x > fb_width - state->src_w ||
765 state->src_h > fb_height ||
766 state->src_y > fb_height - state->src_h) {
767 DRM_DEBUG_ATOMIC("Invalid source coordinates "
768 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
769 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
770 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
771 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
772 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
773 return -ENOSPC;
774 }
775
776 if (plane_switching_crtc(state->state, plane, state)) {
777 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
778 plane->base.id, plane->name);
779 return -EINVAL;
780 }
781
782 return 0;
783}
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800struct drm_connector_state *
801drm_atomic_get_connector_state(struct drm_atomic_state *state,
802 struct drm_connector *connector)
803{
804 int ret, index;
805 struct drm_mode_config *config = &connector->dev->mode_config;
806 struct drm_connector_state *connector_state;
807
808 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
809 if (ret)
810 return ERR_PTR(ret);
811
812 index = drm_connector_index(connector);
813
814 if (index >= state->num_connector) {
815 struct drm_connector **c;
816 struct drm_connector_state **cs;
817 int alloc = max(index + 1, config->num_connector);
818
819 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
820 if (!c)
821 return ERR_PTR(-ENOMEM);
822
823 state->connectors = c;
824 memset(&state->connectors[state->num_connector], 0,
825 sizeof(*state->connectors) * (alloc - state->num_connector));
826
827 cs = krealloc(state->connector_states, alloc * sizeof(*state->connector_states), GFP_KERNEL);
828 if (!cs)
829 return ERR_PTR(-ENOMEM);
830
831 state->connector_states = cs;
832 memset(&state->connector_states[state->num_connector], 0,
833 sizeof(*state->connector_states) * (alloc - state->num_connector));
834 state->num_connector = alloc;
835 }
836
837 if (state->connector_states[index])
838 return state->connector_states[index];
839
840 connector_state = connector->funcs->atomic_duplicate_state(connector);
841 if (!connector_state)
842 return ERR_PTR(-ENOMEM);
843
844 state->connector_states[index] = connector_state;
845 state->connectors[index] = connector;
846 connector_state->state = state;
847
848 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
849 connector->base.id, connector_state, state);
850
851 if (connector_state->crtc) {
852 struct drm_crtc_state *crtc_state;
853
854 crtc_state = drm_atomic_get_crtc_state(state,
855 connector_state->crtc);
856 if (IS_ERR(crtc_state))
857 return ERR_CAST(crtc_state);
858 }
859
860 return connector_state;
861}
862EXPORT_SYMBOL(drm_atomic_get_connector_state);
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880int drm_atomic_connector_set_property(struct drm_connector *connector,
881 struct drm_connector_state *state, struct drm_property *property,
882 uint64_t val)
883{
884 struct drm_device *dev = connector->dev;
885 struct drm_mode_config *config = &dev->mode_config;
886
887 if (property == config->prop_crtc_id) {
888 struct drm_crtc *crtc = drm_crtc_find(dev, val);
889 return drm_atomic_set_crtc_for_connector(state, crtc);
890 } else if (property == config->dpms_property) {
891
892
893
894
895 return -EINVAL;
896 } else if (connector->funcs->atomic_set_property) {
897 return connector->funcs->atomic_set_property(connector,
898 state, property, val);
899 } else {
900 return -EINVAL;
901 }
902}
903EXPORT_SYMBOL(drm_atomic_connector_set_property);
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920static int
921drm_atomic_connector_get_property(struct drm_connector *connector,
922 const struct drm_connector_state *state,
923 struct drm_property *property, uint64_t *val)
924{
925 struct drm_device *dev = connector->dev;
926 struct drm_mode_config *config = &dev->mode_config;
927
928 if (property == config->prop_crtc_id) {
929 *val = (state->crtc) ? state->crtc->base.id : 0;
930 } else if (property == config->dpms_property) {
931 *val = connector->dpms;
932 } else if (connector->funcs->atomic_get_property) {
933 return connector->funcs->atomic_get_property(connector,
934 state, property, val);
935 } else {
936 return -EINVAL;
937 }
938
939 return 0;
940}
941
942int drm_atomic_get_property(struct drm_mode_object *obj,
943 struct drm_property *property, uint64_t *val)
944{
945 struct drm_device *dev = property->dev;
946 int ret;
947
948 switch (obj->type) {
949 case DRM_MODE_OBJECT_CONNECTOR: {
950 struct drm_connector *connector = obj_to_connector(obj);
951 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
952 ret = drm_atomic_connector_get_property(connector,
953 connector->state, property, val);
954 break;
955 }
956 case DRM_MODE_OBJECT_CRTC: {
957 struct drm_crtc *crtc = obj_to_crtc(obj);
958 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
959 ret = drm_atomic_crtc_get_property(crtc,
960 crtc->state, property, val);
961 break;
962 }
963 case DRM_MODE_OBJECT_PLANE: {
964 struct drm_plane *plane = obj_to_plane(obj);
965 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
966 ret = drm_atomic_plane_get_property(plane,
967 plane->state, property, val);
968 break;
969 }
970 default:
971 ret = -EINVAL;
972 break;
973 }
974
975 return ret;
976}
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992int
993drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
994 struct drm_crtc *crtc)
995{
996 struct drm_plane *plane = plane_state->plane;
997 struct drm_crtc_state *crtc_state;
998
999 if (plane_state->crtc) {
1000 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1001 plane_state->crtc);
1002 if (WARN_ON(IS_ERR(crtc_state)))
1003 return PTR_ERR(crtc_state);
1004
1005 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
1006 }
1007
1008 plane_state->crtc = crtc;
1009
1010 if (crtc) {
1011 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1012 crtc);
1013 if (IS_ERR(crtc_state))
1014 return PTR_ERR(crtc_state);
1015 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
1016 }
1017
1018 if (crtc)
1019 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1020 plane_state, crtc->base.id, crtc->name);
1021 else
1022 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1023 plane_state);
1024
1025 return 0;
1026}
1027EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039void
1040drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1041 struct drm_framebuffer *fb)
1042{
1043 if (plane_state->fb)
1044 drm_framebuffer_unreference(plane_state->fb);
1045 if (fb)
1046 drm_framebuffer_reference(fb);
1047 plane_state->fb = fb;
1048
1049 if (fb)
1050 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1051 fb->base.id, plane_state);
1052 else
1053 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1054 plane_state);
1055}
1056EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072int
1073drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1074 struct drm_crtc *crtc)
1075{
1076 struct drm_crtc_state *crtc_state;
1077
1078 if (conn_state->crtc && conn_state->crtc != crtc) {
1079 crtc_state = drm_atomic_get_existing_crtc_state(conn_state->state,
1080 conn_state->crtc);
1081
1082 crtc_state->connector_mask &=
1083 ~(1 << drm_connector_index(conn_state->connector));
1084 }
1085
1086 if (crtc) {
1087 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1088 if (IS_ERR(crtc_state))
1089 return PTR_ERR(crtc_state);
1090
1091 crtc_state->connector_mask |=
1092 1 << drm_connector_index(conn_state->connector);
1093 }
1094
1095 conn_state->crtc = crtc;
1096
1097 if (crtc)
1098 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1099 conn_state, crtc->base.id, crtc->name);
1100 else
1101 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1102 conn_state);
1103
1104 return 0;
1105}
1106EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125int
1126drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1127 struct drm_crtc *crtc)
1128{
1129 struct drm_mode_config *config = &state->dev->mode_config;
1130 struct drm_connector *connector;
1131 struct drm_connector_state *conn_state;
1132 int ret;
1133
1134 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1135 if (ret)
1136 return ret;
1137
1138 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1139 crtc->base.id, crtc->name, state);
1140
1141
1142
1143
1144
1145 drm_for_each_connector(connector, state->dev) {
1146 if (connector->state->crtc != crtc)
1147 continue;
1148
1149 conn_state = drm_atomic_get_connector_state(state, connector);
1150 if (IS_ERR(conn_state))
1151 return PTR_ERR(conn_state);
1152 }
1153
1154 return 0;
1155}
1156EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178int
1179drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1180 struct drm_crtc *crtc)
1181{
1182 struct drm_plane *plane;
1183
1184 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
1185
1186 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1187 struct drm_plane_state *plane_state =
1188 drm_atomic_get_plane_state(state, plane);
1189
1190 if (IS_ERR(plane_state))
1191 return PTR_ERR(plane_state);
1192 }
1193 return 0;
1194}
1195EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1206{
1207 int ret;
1208
1209retry:
1210 drm_modeset_backoff(state->acquire_ctx);
1211
1212 ret = drm_modeset_lock_all_ctx(state->dev, state->acquire_ctx);
1213 if (ret)
1214 goto retry;
1215}
1216EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229int drm_atomic_check_only(struct drm_atomic_state *state)
1230{
1231 struct drm_device *dev = state->dev;
1232 struct drm_mode_config *config = &dev->mode_config;
1233 struct drm_plane *plane;
1234 struct drm_plane_state *plane_state;
1235 struct drm_crtc *crtc;
1236 struct drm_crtc_state *crtc_state;
1237 int i, ret = 0;
1238
1239 DRM_DEBUG_ATOMIC("checking %p\n", state);
1240
1241 for_each_plane_in_state(state, plane, plane_state, i) {
1242 ret = drm_atomic_plane_check(plane, plane_state);
1243 if (ret) {
1244 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1245 plane->base.id, plane->name);
1246 return ret;
1247 }
1248 }
1249
1250 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1251 ret = drm_atomic_crtc_check(crtc, crtc_state);
1252 if (ret) {
1253 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1254 crtc->base.id, crtc->name);
1255 return ret;
1256 }
1257 }
1258
1259 if (config->funcs->atomic_check)
1260 ret = config->funcs->atomic_check(state->dev, state);
1261
1262 if (!state->allow_modeset) {
1263 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1264 if (drm_atomic_crtc_needs_modeset(crtc_state)) {
1265 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1266 crtc->base.id, crtc->name);
1267 return -EINVAL;
1268 }
1269 }
1270 }
1271
1272 return ret;
1273}
1274EXPORT_SYMBOL(drm_atomic_check_only);
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292int drm_atomic_commit(struct drm_atomic_state *state)
1293{
1294 struct drm_mode_config *config = &state->dev->mode_config;
1295 int ret;
1296
1297 ret = drm_atomic_check_only(state);
1298 if (ret)
1299 return ret;
1300
1301 DRM_DEBUG_ATOMIC("commiting %p\n", state);
1302
1303 return config->funcs->atomic_commit(state->dev, state, false);
1304}
1305EXPORT_SYMBOL(drm_atomic_commit);
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323int drm_atomic_async_commit(struct drm_atomic_state *state)
1324{
1325 struct drm_mode_config *config = &state->dev->mode_config;
1326 int ret;
1327
1328 ret = drm_atomic_check_only(state);
1329 if (ret)
1330 return ret;
1331
1332 DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state);
1333
1334 return config->funcs->atomic_commit(state->dev, state, true);
1335}
1336EXPORT_SYMBOL(drm_atomic_async_commit);
1337
1338
1339
1340
1341
1342static struct drm_pending_vblank_event *create_vblank_event(
1343 struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
1344{
1345 struct drm_pending_vblank_event *e = NULL;
1346 unsigned long flags;
1347
1348 spin_lock_irqsave(&dev->event_lock, flags);
1349 if (file_priv->event_space < sizeof e->event) {
1350 spin_unlock_irqrestore(&dev->event_lock, flags);
1351 goto out;
1352 }
1353 file_priv->event_space -= sizeof e->event;
1354 spin_unlock_irqrestore(&dev->event_lock, flags);
1355
1356 e = kzalloc(sizeof *e, GFP_KERNEL);
1357 if (e == NULL) {
1358 spin_lock_irqsave(&dev->event_lock, flags);
1359 file_priv->event_space += sizeof e->event;
1360 spin_unlock_irqrestore(&dev->event_lock, flags);
1361 goto out;
1362 }
1363
1364 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1365 e->event.base.length = sizeof e->event;
1366 e->event.user_data = user_data;
1367 e->base.event = &e->event.base;
1368 e->base.file_priv = file_priv;
1369 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1370
1371out:
1372 return e;
1373}
1374
1375static void destroy_vblank_event(struct drm_device *dev,
1376 struct drm_file *file_priv, struct drm_pending_vblank_event *e)
1377{
1378 unsigned long flags;
1379
1380 spin_lock_irqsave(&dev->event_lock, flags);
1381 file_priv->event_space += sizeof e->event;
1382 spin_unlock_irqrestore(&dev->event_lock, flags);
1383 kfree(e);
1384}
1385
1386static int atomic_set_prop(struct drm_atomic_state *state,
1387 struct drm_mode_object *obj, struct drm_property *prop,
1388 uint64_t prop_value)
1389{
1390 struct drm_mode_object *ref;
1391 int ret;
1392
1393 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1394 return -EINVAL;
1395
1396 switch (obj->type) {
1397 case DRM_MODE_OBJECT_CONNECTOR: {
1398 struct drm_connector *connector = obj_to_connector(obj);
1399 struct drm_connector_state *connector_state;
1400
1401 connector_state = drm_atomic_get_connector_state(state, connector);
1402 if (IS_ERR(connector_state)) {
1403 ret = PTR_ERR(connector_state);
1404 break;
1405 }
1406
1407 ret = drm_atomic_connector_set_property(connector,
1408 connector_state, prop, prop_value);
1409 break;
1410 }
1411 case DRM_MODE_OBJECT_CRTC: {
1412 struct drm_crtc *crtc = obj_to_crtc(obj);
1413 struct drm_crtc_state *crtc_state;
1414
1415 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1416 if (IS_ERR(crtc_state)) {
1417 ret = PTR_ERR(crtc_state);
1418 break;
1419 }
1420
1421 ret = drm_atomic_crtc_set_property(crtc,
1422 crtc_state, prop, prop_value);
1423 break;
1424 }
1425 case DRM_MODE_OBJECT_PLANE: {
1426 struct drm_plane *plane = obj_to_plane(obj);
1427 struct drm_plane_state *plane_state;
1428
1429 plane_state = drm_atomic_get_plane_state(state, plane);
1430 if (IS_ERR(plane_state)) {
1431 ret = PTR_ERR(plane_state);
1432 break;
1433 }
1434
1435 ret = drm_atomic_plane_set_property(plane,
1436 plane_state, prop, prop_value);
1437 break;
1438 }
1439 default:
1440 ret = -EINVAL;
1441 break;
1442 }
1443
1444 drm_property_change_valid_put(prop, ref);
1445 return ret;
1446}
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460void drm_atomic_clean_old_fb(struct drm_device *dev,
1461 unsigned plane_mask,
1462 int ret)
1463{
1464 struct drm_plane *plane;
1465
1466
1467
1468
1469
1470
1471 drm_for_each_plane_mask(plane, dev, plane_mask) {
1472 if (ret == 0) {
1473 struct drm_framebuffer *new_fb = plane->state->fb;
1474 if (new_fb)
1475 drm_framebuffer_reference(new_fb);
1476 plane->fb = new_fb;
1477 plane->crtc = plane->state->crtc;
1478
1479 if (plane->old_fb)
1480 drm_framebuffer_unreference(plane->old_fb);
1481 }
1482 plane->old_fb = NULL;
1483 }
1484}
1485EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1486
1487int drm_mode_atomic_ioctl(struct drm_device *dev,
1488 void *data, struct drm_file *file_priv)
1489{
1490 struct drm_mode_atomic *arg = data;
1491 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1492 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1493 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1494 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1495 unsigned int copied_objs, copied_props;
1496 struct drm_atomic_state *state;
1497 struct drm_modeset_acquire_ctx ctx;
1498 struct drm_plane *plane;
1499 struct drm_crtc *crtc;
1500 struct drm_crtc_state *crtc_state;
1501 unsigned plane_mask;
1502 int ret = 0;
1503 unsigned int i, j;
1504
1505
1506 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1507 return -EINVAL;
1508
1509
1510
1511
1512
1513 if (!file_priv->atomic)
1514 return -EINVAL;
1515
1516 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1517 return -EINVAL;
1518
1519 if (arg->reserved)
1520 return -EINVAL;
1521
1522 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1523 !dev->mode_config.async_page_flip)
1524 return -EINVAL;
1525
1526
1527 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1528 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1529 return -EINVAL;
1530
1531 drm_modeset_acquire_init(&ctx, 0);
1532
1533 state = drm_atomic_state_alloc(dev);
1534 if (!state)
1535 return -ENOMEM;
1536
1537 state->acquire_ctx = &ctx;
1538 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1539
1540retry:
1541 plane_mask = 0;
1542 copied_objs = 0;
1543 copied_props = 0;
1544
1545 for (i = 0; i < arg->count_objs; i++) {
1546 uint32_t obj_id, count_props;
1547 struct drm_mode_object *obj;
1548
1549 if (get_user(obj_id, objs_ptr + copied_objs)) {
1550 ret = -EFAULT;
1551 goto out;
1552 }
1553
1554 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
1555 if (!obj || !obj->properties) {
1556 ret = -ENOENT;
1557 goto out;
1558 }
1559
1560 if (get_user(count_props, count_props_ptr + copied_objs)) {
1561 ret = -EFAULT;
1562 goto out;
1563 }
1564
1565 copied_objs++;
1566
1567 for (j = 0; j < count_props; j++) {
1568 uint32_t prop_id;
1569 uint64_t prop_value;
1570 struct drm_property *prop;
1571
1572 if (get_user(prop_id, props_ptr + copied_props)) {
1573 ret = -EFAULT;
1574 goto out;
1575 }
1576
1577 prop = drm_property_find(dev, prop_id);
1578 if (!prop) {
1579 ret = -ENOENT;
1580 goto out;
1581 }
1582
1583 if (copy_from_user(&prop_value,
1584 prop_values_ptr + copied_props,
1585 sizeof(prop_value))) {
1586 ret = -EFAULT;
1587 goto out;
1588 }
1589
1590 ret = atomic_set_prop(state, obj, prop, prop_value);
1591 if (ret)
1592 goto out;
1593
1594 copied_props++;
1595 }
1596
1597 if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
1598 !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
1599 plane = obj_to_plane(obj);
1600 plane_mask |= (1 << drm_plane_index(plane));
1601 plane->old_fb = plane->fb;
1602 }
1603 }
1604
1605 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1606 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1607 struct drm_pending_vblank_event *e;
1608
1609 e = create_vblank_event(dev, file_priv, arg->user_data);
1610 if (!e) {
1611 ret = -ENOMEM;
1612 goto out;
1613 }
1614
1615 crtc_state->event = e;
1616 }
1617 }
1618
1619 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1620
1621
1622
1623
1624 ret = drm_atomic_check_only(state);
1625 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1626 ret = drm_atomic_async_commit(state);
1627 } else {
1628 ret = drm_atomic_commit(state);
1629 }
1630
1631out:
1632 drm_atomic_clean_old_fb(dev, plane_mask, ret);
1633
1634 if (ret && arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1635
1636
1637
1638
1639
1640
1641 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1642 if (!crtc_state->event)
1643 continue;
1644
1645 destroy_vblank_event(dev, file_priv,
1646 crtc_state->event);
1647 }
1648 }
1649
1650 if (ret == -EDEADLK) {
1651 drm_atomic_state_clear(state);
1652 drm_modeset_backoff(&ctx);
1653 goto retry;
1654 }
1655
1656 if (ret || arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
1657 drm_atomic_state_free(state);
1658
1659 drm_modeset_drop_locks(&ctx);
1660 drm_modeset_acquire_fini(&ctx);
1661
1662 return ret;
1663}
1664