1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/export.h>
24#include <drm/drmP.h>
25#include <drm/drm_auth.h>
26#include <drm/drm_framebuffer.h>
27
28#include "drm_crtc_internal.h"
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65int drm_framebuffer_check_src_coords(uint32_t src_x, uint32_t src_y,
66 uint32_t src_w, uint32_t src_h,
67 const struct drm_framebuffer *fb)
68{
69 unsigned int fb_width, fb_height;
70
71 fb_width = fb->width << 16;
72 fb_height = fb->height << 16;
73
74
75 if (src_w > fb_width ||
76 src_x > fb_width - src_w ||
77 src_h > fb_height ||
78 src_y > fb_height - src_h) {
79 DRM_DEBUG_KMS("Invalid source coordinates "
80 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
81 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
82 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
83 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
84 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
85 return -ENOSPC;
86 }
87
88 return 0;
89}
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105int drm_mode_addfb(struct drm_device *dev,
106 void *data, struct drm_file *file_priv)
107{
108 struct drm_mode_fb_cmd *or = data;
109 struct drm_mode_fb_cmd2 r = {};
110 int ret;
111
112
113 r.fb_id = or->fb_id;
114 r.width = or->width;
115 r.height = or->height;
116 r.pitches[0] = or->pitch;
117 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
118 r.handles[0] = or->handle;
119
120 ret = drm_mode_addfb2(dev, &r, file_priv);
121 if (ret)
122 return ret;
123
124 or->fb_id = r.fb_id;
125
126 return 0;
127}
128
129static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
130{
131 const struct drm_format_info *info;
132 int i;
133
134 info = __drm_format_info(r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN);
135 if (!info) {
136 struct drm_format_name_buf format_name;
137 DRM_DEBUG_KMS("bad framebuffer format %s\n",
138 drm_get_format_name(r->pixel_format,
139 &format_name));
140 return -EINVAL;
141 }
142
143 if (r->width == 0 || r->width % info->hsub) {
144 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
145 return -EINVAL;
146 }
147
148 if (r->height == 0 || r->height % info->vsub) {
149 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
150 return -EINVAL;
151 }
152
153 for (i = 0; i < info->num_planes; i++) {
154 unsigned int width = r->width / (i != 0 ? info->hsub : 1);
155 unsigned int height = r->height / (i != 0 ? info->vsub : 1);
156 unsigned int cpp = info->cpp[i];
157
158 if (!r->handles[i]) {
159 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
160 return -EINVAL;
161 }
162
163 if ((uint64_t) width * cpp > UINT_MAX)
164 return -ERANGE;
165
166 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
167 return -ERANGE;
168
169 if (r->pitches[i] < width * cpp) {
170 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
171 return -EINVAL;
172 }
173
174 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
175 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
176 r->modifier[i], i);
177 return -EINVAL;
178 }
179
180 if (r->flags & DRM_MODE_FB_MODIFIERS &&
181 r->modifier[i] != r->modifier[0]) {
182 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
183 r->modifier[i], i);
184 return -EINVAL;
185 }
186
187
188 switch (r->modifier[i]) {
189 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
190
191
192
193 if (r->pixel_format != DRM_FORMAT_NV12 ||
194 width % 128 || height % 32 ||
195 r->pitches[i] % 128) {
196 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
197 return -EINVAL;
198 }
199 break;
200
201 default:
202 break;
203 }
204 }
205
206 for (i = info->num_planes; i < 4; i++) {
207 if (r->modifier[i]) {
208 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
209 return -EINVAL;
210 }
211
212
213 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
214 continue;
215
216 if (r->handles[i]) {
217 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
218 return -EINVAL;
219 }
220
221 if (r->pitches[i]) {
222 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
223 return -EINVAL;
224 }
225
226 if (r->offsets[i]) {
227 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
228 return -EINVAL;
229 }
230 }
231
232 return 0;
233}
234
235struct drm_framebuffer *
236drm_internal_framebuffer_create(struct drm_device *dev,
237 const struct drm_mode_fb_cmd2 *r,
238 struct drm_file *file_priv)
239{
240 struct drm_mode_config *config = &dev->mode_config;
241 struct drm_framebuffer *fb;
242 int ret;
243
244 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
245 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
246 return ERR_PTR(-EINVAL);
247 }
248
249 if ((config->min_width > r->width) || (r->width > config->max_width)) {
250 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
251 r->width, config->min_width, config->max_width);
252 return ERR_PTR(-EINVAL);
253 }
254 if ((config->min_height > r->height) || (r->height > config->max_height)) {
255 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
256 r->height, config->min_height, config->max_height);
257 return ERR_PTR(-EINVAL);
258 }
259
260 if (r->flags & DRM_MODE_FB_MODIFIERS &&
261 !dev->mode_config.allow_fb_modifiers) {
262 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
263 return ERR_PTR(-EINVAL);
264 }
265
266 ret = framebuffer_check(r);
267 if (ret)
268 return ERR_PTR(ret);
269
270 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
271 if (IS_ERR(fb)) {
272 DRM_DEBUG_KMS("could not create framebuffer\n");
273 return fb;
274 }
275
276 return fb;
277}
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294int drm_mode_addfb2(struct drm_device *dev,
295 void *data, struct drm_file *file_priv)
296{
297 struct drm_mode_fb_cmd2 *r = data;
298 struct drm_framebuffer *fb;
299
300 if (!drm_core_check_feature(dev, DRIVER_MODESET))
301 return -EINVAL;
302
303 fb = drm_internal_framebuffer_create(dev, r, file_priv);
304 if (IS_ERR(fb))
305 return PTR_ERR(fb);
306
307 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
308 r->fb_id = fb->base.id;
309
310
311 mutex_lock(&file_priv->fbs_lock);
312 list_add(&fb->filp_head, &file_priv->fbs);
313 mutex_unlock(&file_priv->fbs_lock);
314
315 return 0;
316}
317
318struct drm_mode_rmfb_work {
319 struct work_struct work;
320 struct list_head fbs;
321};
322
323static void drm_mode_rmfb_work_fn(struct work_struct *w)
324{
325 struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
326
327 while (!list_empty(&arg->fbs)) {
328 struct drm_framebuffer *fb =
329 list_first_entry(&arg->fbs, typeof(*fb), filp_head);
330
331 list_del_init(&fb->filp_head);
332 drm_framebuffer_remove(fb);
333 }
334}
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349int drm_mode_rmfb(struct drm_device *dev,
350 void *data, struct drm_file *file_priv)
351{
352 struct drm_framebuffer *fb = NULL;
353 struct drm_framebuffer *fbl = NULL;
354 uint32_t *id = data;
355 int found = 0;
356
357 if (!drm_core_check_feature(dev, DRIVER_MODESET))
358 return -EINVAL;
359
360 fb = drm_framebuffer_lookup(dev, *id);
361 if (!fb)
362 return -ENOENT;
363
364 mutex_lock(&file_priv->fbs_lock);
365 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
366 if (fb == fbl)
367 found = 1;
368 if (!found) {
369 mutex_unlock(&file_priv->fbs_lock);
370 goto fail_unref;
371 }
372
373 list_del_init(&fb->filp_head);
374 mutex_unlock(&file_priv->fbs_lock);
375
376
377 drm_framebuffer_unreference(fb);
378
379
380
381
382
383
384
385
386 if (drm_framebuffer_read_refcount(fb) > 1) {
387 struct drm_mode_rmfb_work arg;
388
389 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
390 INIT_LIST_HEAD(&arg.fbs);
391 list_add_tail(&fb->filp_head, &arg.fbs);
392
393 schedule_work(&arg.work);
394 flush_work(&arg.work);
395 destroy_work_on_stack(&arg.work);
396 } else
397 drm_framebuffer_unreference(fb);
398
399 return 0;
400
401fail_unref:
402 drm_framebuffer_unreference(fb);
403 return -ENOENT;
404}
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419int drm_mode_getfb(struct drm_device *dev,
420 void *data, struct drm_file *file_priv)
421{
422 struct drm_mode_fb_cmd *r = data;
423 struct drm_framebuffer *fb;
424 int ret;
425
426 if (!drm_core_check_feature(dev, DRIVER_MODESET))
427 return -EINVAL;
428
429 fb = drm_framebuffer_lookup(dev, r->fb_id);
430 if (!fb)
431 return -ENOENT;
432
433 r->height = fb->height;
434 r->width = fb->width;
435 r->depth = fb->depth;
436 r->bpp = fb->bits_per_pixel;
437 r->pitch = fb->pitches[0];
438 if (fb->funcs->create_handle) {
439 if (drm_is_current_master(file_priv) || capable(CAP_SYS_ADMIN) ||
440 drm_is_control_client(file_priv)) {
441 ret = fb->funcs->create_handle(fb, file_priv,
442 &r->handle);
443 } else {
444
445
446
447
448
449 r->handle = 0;
450 ret = 0;
451 }
452 } else {
453 ret = -ENODEV;
454 }
455
456 drm_framebuffer_unreference(fb);
457
458 return ret;
459}
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
481 void *data, struct drm_file *file_priv)
482{
483 struct drm_clip_rect __user *clips_ptr;
484 struct drm_clip_rect *clips = NULL;
485 struct drm_mode_fb_dirty_cmd *r = data;
486 struct drm_framebuffer *fb;
487 unsigned flags;
488 int num_clips;
489 int ret;
490
491 if (!drm_core_check_feature(dev, DRIVER_MODESET))
492 return -EINVAL;
493
494 fb = drm_framebuffer_lookup(dev, r->fb_id);
495 if (!fb)
496 return -ENOENT;
497
498 num_clips = r->num_clips;
499 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
500
501 if (!num_clips != !clips_ptr) {
502 ret = -EINVAL;
503 goto out_err1;
504 }
505
506 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
507
508
509 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
510 ret = -EINVAL;
511 goto out_err1;
512 }
513
514 if (num_clips && clips_ptr) {
515 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
516 ret = -EINVAL;
517 goto out_err1;
518 }
519 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
520 if (!clips) {
521 ret = -ENOMEM;
522 goto out_err1;
523 }
524
525 ret = copy_from_user(clips, clips_ptr,
526 num_clips * sizeof(*clips));
527 if (ret) {
528 ret = -EFAULT;
529 goto out_err2;
530 }
531 }
532
533 if (fb->funcs->dirty) {
534 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
535 clips, num_clips);
536 } else {
537 ret = -ENOSYS;
538 }
539
540out_err2:
541 kfree(clips);
542out_err1:
543 drm_framebuffer_unreference(fb);
544
545 return ret;
546}
547
548
549
550
551
552
553
554
555
556
557
558
559void drm_fb_release(struct drm_file *priv)
560{
561 struct drm_framebuffer *fb, *tfb;
562 struct drm_mode_rmfb_work arg;
563
564 INIT_LIST_HEAD(&arg.fbs);
565
566
567
568
569
570
571
572
573
574
575
576 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
577 if (drm_framebuffer_read_refcount(fb) > 1) {
578 list_move_tail(&fb->filp_head, &arg.fbs);
579 } else {
580 list_del_init(&fb->filp_head);
581
582
583 drm_framebuffer_unreference(fb);
584 }
585 }
586
587 if (!list_empty(&arg.fbs)) {
588 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
589
590 schedule_work(&arg.work);
591 flush_work(&arg.work);
592 destroy_work_on_stack(&arg.work);
593 }
594}
595
596void drm_framebuffer_free(struct kref *kref)
597{
598 struct drm_framebuffer *fb =
599 container_of(kref, struct drm_framebuffer, base.refcount);
600 struct drm_device *dev = fb->dev;
601
602
603
604
605
606 drm_mode_object_unregister(dev, &fb->base);
607
608 fb->funcs->destroy(fb);
609}
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
630 const struct drm_framebuffer_funcs *funcs)
631{
632 int ret;
633
634 INIT_LIST_HEAD(&fb->filp_head);
635 fb->dev = dev;
636 fb->funcs = funcs;
637
638 ret = drm_mode_object_get_reg(dev, &fb->base, DRM_MODE_OBJECT_FB,
639 false, drm_framebuffer_free);
640 if (ret)
641 goto out;
642
643 mutex_lock(&dev->mode_config.fb_lock);
644 dev->mode_config.num_fb++;
645 list_add(&fb->head, &dev->mode_config.fb_list);
646 mutex_unlock(&dev->mode_config.fb_lock);
647
648 drm_mode_object_register(dev, &fb->base);
649out:
650 return ret;
651}
652EXPORT_SYMBOL(drm_framebuffer_init);
653
654
655
656
657
658
659
660
661
662
663struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
664 uint32_t id)
665{
666 struct drm_mode_object *obj;
667 struct drm_framebuffer *fb = NULL;
668
669 obj = __drm_mode_object_find(dev, id, DRM_MODE_OBJECT_FB);
670 if (obj)
671 fb = obj_to_fb(obj);
672 return fb;
673}
674EXPORT_SYMBOL(drm_framebuffer_lookup);
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
691{
692 struct drm_device *dev;
693
694 if (!fb)
695 return;
696
697 dev = fb->dev;
698
699
700 drm_mode_object_unregister(dev, &fb->base);
701}
702EXPORT_SYMBOL(drm_framebuffer_unregister_private);
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
722{
723 struct drm_device *dev = fb->dev;
724
725 mutex_lock(&dev->mode_config.fb_lock);
726 list_del(&fb->head);
727 dev->mode_config.num_fb--;
728 mutex_unlock(&dev->mode_config.fb_lock);
729}
730EXPORT_SYMBOL(drm_framebuffer_cleanup);
731
732
733
734
735
736
737
738
739
740
741
742
743
744void drm_framebuffer_remove(struct drm_framebuffer *fb)
745{
746 struct drm_device *dev;
747 struct drm_crtc *crtc;
748 struct drm_plane *plane;
749
750 if (!fb)
751 return;
752
753 dev = fb->dev;
754
755 WARN_ON(!list_empty(&fb->filp_head));
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772 if (drm_framebuffer_read_refcount(fb) > 1) {
773 drm_modeset_lock_all(dev);
774
775 drm_for_each_crtc(crtc, dev) {
776 if (crtc->primary->fb == fb) {
777
778 if (drm_crtc_force_disable(crtc))
779 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
780 }
781 }
782
783 drm_for_each_plane(plane, dev) {
784 if (plane->fb == fb)
785 drm_plane_force_disable(plane);
786 }
787 drm_modeset_unlock_all(dev);
788 }
789
790 drm_framebuffer_unreference(fb);
791}
792EXPORT_SYMBOL(drm_framebuffer_remove);
793