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#include <linux/async.h>
28#include <linux/module.h>
29#include <linux/kernel.h>
30#include <linux/console.h>
31#include <linux/errno.h>
32#include <linux/string.h>
33#include <linux/mm.h>
34#include <linux/tty.h>
35#include <linux/sysrq.h>
36#include <linux/delay.h>
37#include <linux/init.h>
38#include <linux/vga_switcheroo.h>
39
40#include <drm/drmP.h>
41#include <drm/drm_crtc.h>
42#include <drm/drm_fb_helper.h>
43#include "intel_drv.h"
44#include "intel_frontbuffer.h"
45#include <drm/i915_drm.h>
46#include "i915_drv.h"
47
48static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev)
49{
50 struct drm_i915_gem_object *obj = ifbdev->fb->obj;
51 unsigned int origin = ifbdev->vma->fence ? ORIGIN_GTT : ORIGIN_CPU;
52
53 intel_fb_obj_invalidate(obj, origin);
54}
55
56static int intel_fbdev_set_par(struct fb_info *info)
57{
58 struct drm_fb_helper *fb_helper = info->par;
59 struct intel_fbdev *ifbdev =
60 container_of(fb_helper, struct intel_fbdev, helper);
61 int ret;
62
63 ret = drm_fb_helper_set_par(info);
64 if (ret == 0)
65 intel_fbdev_invalidate(ifbdev);
66
67 return ret;
68}
69
70static int intel_fbdev_blank(int blank, struct fb_info *info)
71{
72 struct drm_fb_helper *fb_helper = info->par;
73 struct intel_fbdev *ifbdev =
74 container_of(fb_helper, struct intel_fbdev, helper);
75 int ret;
76
77 ret = drm_fb_helper_blank(blank, info);
78 if (ret == 0)
79 intel_fbdev_invalidate(ifbdev);
80
81 return ret;
82}
83
84static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
85 struct fb_info *info)
86{
87 struct drm_fb_helper *fb_helper = info->par;
88 struct intel_fbdev *ifbdev =
89 container_of(fb_helper, struct intel_fbdev, helper);
90 int ret;
91
92 ret = drm_fb_helper_pan_display(var, info);
93 if (ret == 0)
94 intel_fbdev_invalidate(ifbdev);
95
96 return ret;
97}
98
99static struct fb_ops intelfb_ops = {
100 .owner = THIS_MODULE,
101 DRM_FB_HELPER_DEFAULT_OPS,
102 .fb_set_par = intel_fbdev_set_par,
103 .fb_fillrect = drm_fb_helper_cfb_fillrect,
104 .fb_copyarea = drm_fb_helper_cfb_copyarea,
105 .fb_imageblit = drm_fb_helper_cfb_imageblit,
106 .fb_pan_display = intel_fbdev_pan_display,
107 .fb_blank = intel_fbdev_blank,
108};
109
110static int intelfb_alloc(struct drm_fb_helper *helper,
111 struct drm_fb_helper_surface_size *sizes)
112{
113 struct intel_fbdev *ifbdev =
114 container_of(helper, struct intel_fbdev, helper);
115 struct drm_framebuffer *fb;
116 struct drm_device *dev = helper->dev;
117 struct drm_i915_private *dev_priv = to_i915(dev);
118 struct i915_ggtt *ggtt = &dev_priv->ggtt;
119 struct drm_mode_fb_cmd2 mode_cmd = {};
120 struct drm_i915_gem_object *obj;
121 int size, ret;
122
123
124 if (sizes->surface_bpp == 24)
125 sizes->surface_bpp = 32;
126
127 mode_cmd.width = sizes->surface_width;
128 mode_cmd.height = sizes->surface_height;
129
130 mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
131 DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
132 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
133 sizes->surface_depth);
134
135 size = mode_cmd.pitches[0] * mode_cmd.height;
136 size = PAGE_ALIGN(size);
137
138
139
140
141 obj = NULL;
142 if (size * 2 < ggtt->stolen_usable_size)
143 obj = i915_gem_object_create_stolen(dev_priv, size);
144 if (obj == NULL)
145 obj = i915_gem_object_create(dev_priv, size);
146 if (IS_ERR(obj)) {
147 DRM_ERROR("failed to allocate framebuffer\n");
148 ret = PTR_ERR(obj);
149 goto err;
150 }
151
152 fb = intel_framebuffer_create(obj, &mode_cmd);
153 if (IS_ERR(fb)) {
154 ret = PTR_ERR(fb);
155 goto err_obj;
156 }
157
158 ifbdev->fb = to_intel_framebuffer(fb);
159
160 return 0;
161
162err_obj:
163 i915_gem_object_put(obj);
164err:
165 return ret;
166}
167
168static int intelfb_create(struct drm_fb_helper *helper,
169 struct drm_fb_helper_surface_size *sizes)
170{
171 struct intel_fbdev *ifbdev =
172 container_of(helper, struct intel_fbdev, helper);
173 struct intel_framebuffer *intel_fb = ifbdev->fb;
174 struct drm_device *dev = helper->dev;
175 struct drm_i915_private *dev_priv = to_i915(dev);
176 struct pci_dev *pdev = dev_priv->drm.pdev;
177 struct i915_ggtt *ggtt = &dev_priv->ggtt;
178 struct fb_info *info;
179 struct drm_framebuffer *fb;
180 struct i915_vma *vma;
181 bool prealloc = false;
182 void __iomem *vaddr;
183 int ret;
184
185 if (intel_fb &&
186 (sizes->fb_width > intel_fb->base.width ||
187 sizes->fb_height > intel_fb->base.height)) {
188 DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d),"
189 " releasing it\n",
190 intel_fb->base.width, intel_fb->base.height,
191 sizes->fb_width, sizes->fb_height);
192 drm_framebuffer_unreference(&intel_fb->base);
193 intel_fb = ifbdev->fb = NULL;
194 }
195 if (!intel_fb || WARN_ON(!intel_fb->obj)) {
196 DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
197 ret = intelfb_alloc(helper, sizes);
198 if (ret)
199 return ret;
200 intel_fb = ifbdev->fb;
201 } else {
202 DRM_DEBUG_KMS("re-using BIOS fb\n");
203 prealloc = true;
204 sizes->fb_width = intel_fb->base.width;
205 sizes->fb_height = intel_fb->base.height;
206 }
207
208 mutex_lock(&dev->struct_mutex);
209
210
211
212
213
214 vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base, DRM_MODE_ROTATE_0);
215 if (IS_ERR(vma)) {
216 ret = PTR_ERR(vma);
217 goto out_unlock;
218 }
219
220 info = drm_fb_helper_alloc_fbi(helper);
221 if (IS_ERR(info)) {
222 DRM_ERROR("Failed to allocate fb_info\n");
223 ret = PTR_ERR(info);
224 goto out_unpin;
225 }
226
227 info->par = helper;
228
229 fb = &ifbdev->fb->base;
230
231 ifbdev->helper.fb = fb;
232
233 strcpy(info->fix.id, "inteldrmfb");
234
235 info->fbops = &intelfb_ops;
236
237
238 info->apertures->ranges[0].base = dev->mode_config.fb_base;
239 info->apertures->ranges[0].size = ggtt->mappable_end;
240
241 info->fix.smem_start = dev->mode_config.fb_base + i915_ggtt_offset(vma);
242 info->fix.smem_len = vma->node.size;
243
244 vaddr = i915_vma_pin_iomap(vma);
245 if (IS_ERR(vaddr)) {
246 DRM_ERROR("Failed to remap framebuffer into virtual memory\n");
247 ret = PTR_ERR(vaddr);
248 goto out_unpin;
249 }
250 info->screen_base = vaddr;
251 info->screen_size = vma->node.size;
252
253
254 info->skip_vt_switch = true;
255
256 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
257 drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height);
258
259
260
261
262
263 if (intel_fb->obj->stolen && !prealloc)
264 memset_io(info->screen_base, 0, info->screen_size);
265
266
267
268 DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08x\n",
269 fb->width, fb->height, i915_ggtt_offset(vma));
270 ifbdev->vma = vma;
271
272 mutex_unlock(&dev->struct_mutex);
273 vga_switcheroo_client_fb_set(pdev, info);
274 return 0;
275
276out_unpin:
277 intel_unpin_fb_vma(vma);
278out_unlock:
279 mutex_unlock(&dev->struct_mutex);
280 return ret;
281}
282
283static struct drm_fb_helper_crtc *
284intel_fb_helper_crtc(struct drm_fb_helper *fb_helper, struct drm_crtc *crtc)
285{
286 int i;
287
288 for (i = 0; i < fb_helper->crtc_count; i++)
289 if (fb_helper->crtc_info[i].mode_set.crtc == crtc)
290 return &fb_helper->crtc_info[i];
291
292 return NULL;
293}
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322static bool intel_fb_initial_config(struct drm_fb_helper *fb_helper,
323 struct drm_fb_helper_crtc **crtcs,
324 struct drm_display_mode **modes,
325 struct drm_fb_offset *offsets,
326 bool *enabled, int width, int height)
327{
328 struct drm_i915_private *dev_priv = to_i915(fb_helper->dev);
329 unsigned long conn_configured, conn_seq, mask;
330 unsigned int count = min(fb_helper->connector_count, BITS_PER_LONG);
331 int i, j;
332 bool *save_enabled;
333 bool fallback = true, ret = true;
334 int num_connectors_enabled = 0;
335 int num_connectors_detected = 0;
336 struct drm_modeset_acquire_ctx ctx;
337
338 save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL);
339 if (!save_enabled)
340 return false;
341
342 drm_modeset_acquire_init(&ctx, 0);
343
344 while (drm_modeset_lock_all_ctx(fb_helper->dev, &ctx) != 0)
345 drm_modeset_backoff(&ctx);
346
347 memcpy(save_enabled, enabled, count);
348 mask = GENMASK(count - 1, 0);
349 conn_configured = 0;
350retry:
351 conn_seq = conn_configured;
352 for (i = 0; i < count; i++) {
353 struct drm_fb_helper_connector *fb_conn;
354 struct drm_connector *connector;
355 struct drm_encoder *encoder;
356 struct drm_fb_helper_crtc *new_crtc;
357
358 fb_conn = fb_helper->connector_info[i];
359 connector = fb_conn->connector;
360
361 if (conn_configured & BIT(i))
362 continue;
363
364 if (conn_seq == 0 && !connector->has_tile)
365 continue;
366
367 if (connector->status == connector_status_connected)
368 num_connectors_detected++;
369
370 if (!enabled[i]) {
371 DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
372 connector->name);
373 conn_configured |= BIT(i);
374 continue;
375 }
376
377 if (connector->force == DRM_FORCE_OFF) {
378 DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
379 connector->name);
380 enabled[i] = false;
381 continue;
382 }
383
384 encoder = connector->state->best_encoder;
385 if (!encoder || WARN_ON(!connector->state->crtc)) {
386 if (connector->force > DRM_FORCE_OFF)
387 goto bail;
388
389 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
390 connector->name);
391 enabled[i] = false;
392 conn_configured |= BIT(i);
393 continue;
394 }
395
396 num_connectors_enabled++;
397
398 new_crtc = intel_fb_helper_crtc(fb_helper,
399 connector->state->crtc);
400
401
402
403
404
405
406 for (j = 0; j < count; j++) {
407 if (crtcs[j] == new_crtc) {
408 DRM_DEBUG_KMS("fallback: cloned configuration\n");
409 goto bail;
410 }
411 }
412
413 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
414 connector->name);
415
416
417 modes[i] = drm_pick_cmdline_mode(fb_conn);
418
419
420 if (!modes[i]) {
421 DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
422 connector->name, connector->has_tile);
423 modes[i] = drm_has_preferred_mode(fb_conn, width,
424 height);
425 }
426
427
428 if (!modes[i] && !list_empty(&connector->modes)) {
429 DRM_DEBUG_KMS("using first mode listed on connector %s\n",
430 connector->name);
431 modes[i] = list_first_entry(&connector->modes,
432 struct drm_display_mode,
433 head);
434 }
435
436
437 if (!modes[i]) {
438
439
440
441
442
443
444
445
446
447
448
449
450
451 DRM_DEBUG_KMS("looking for current mode on connector %s\n",
452 connector->name);
453 modes[i] = &connector->state->crtc->mode;
454 }
455 crtcs[i] = new_crtc;
456
457 DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n",
458 connector->name,
459 connector->state->crtc->base.id,
460 connector->state->crtc->name,
461 modes[i]->hdisplay, modes[i]->vdisplay,
462 modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" :"");
463
464 fallback = false;
465 conn_configured |= BIT(i);
466 }
467
468 if ((conn_configured & mask) != mask && conn_configured != conn_seq)
469 goto retry;
470
471
472
473
474
475
476 if (num_connectors_enabled != num_connectors_detected &&
477 num_connectors_enabled < INTEL_INFO(dev_priv)->num_pipes) {
478 DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
479 DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
480 num_connectors_detected);
481 fallback = true;
482 }
483
484 if (fallback) {
485bail:
486 DRM_DEBUG_KMS("Not using firmware configuration\n");
487 memcpy(enabled, save_enabled, count);
488 ret = false;
489 }
490
491 drm_modeset_drop_locks(&ctx);
492 drm_modeset_acquire_fini(&ctx);
493
494 kfree(save_enabled);
495 return ret;
496}
497
498static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
499 .initial_config = intel_fb_initial_config,
500 .fb_probe = intelfb_create,
501};
502
503static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
504{
505
506
507
508
509
510 drm_fb_helper_fini(&ifbdev->helper);
511
512 if (ifbdev->vma) {
513 mutex_lock(&ifbdev->helper.dev->struct_mutex);
514 intel_unpin_fb_vma(ifbdev->vma);
515 mutex_unlock(&ifbdev->helper.dev->struct_mutex);
516 }
517
518 if (ifbdev->fb)
519 drm_framebuffer_remove(&ifbdev->fb->base);
520
521 kfree(ifbdev);
522}
523
524
525
526
527
528
529
530
531
532
533static bool intel_fbdev_init_bios(struct drm_device *dev,
534 struct intel_fbdev *ifbdev)
535{
536 struct intel_framebuffer *fb = NULL;
537 struct drm_crtc *crtc;
538 struct intel_crtc *intel_crtc;
539 unsigned int max_size = 0;
540
541
542 for_each_crtc(dev, crtc) {
543 struct drm_i915_gem_object *obj =
544 intel_fb_obj(crtc->primary->state->fb);
545 intel_crtc = to_intel_crtc(crtc);
546
547 if (!crtc->state->active || !obj) {
548 DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
549 pipe_name(intel_crtc->pipe));
550 continue;
551 }
552
553 if (obj->base.size > max_size) {
554 DRM_DEBUG_KMS("found possible fb from plane %c\n",
555 pipe_name(intel_crtc->pipe));
556 fb = to_intel_framebuffer(crtc->primary->state->fb);
557 max_size = obj->base.size;
558 }
559 }
560
561 if (!fb) {
562 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
563 goto out;
564 }
565
566
567 for_each_crtc(dev, crtc) {
568 unsigned int cur_size;
569
570 intel_crtc = to_intel_crtc(crtc);
571
572 if (!crtc->state->active) {
573 DRM_DEBUG_KMS("pipe %c not active, skipping\n",
574 pipe_name(intel_crtc->pipe));
575 continue;
576 }
577
578 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
579 pipe_name(intel_crtc->pipe));
580
581
582
583
584
585
586 cur_size = intel_crtc->config->base.adjusted_mode.crtc_hdisplay;
587 cur_size = cur_size * fb->base.format->cpp[0];
588 if (fb->base.pitches[0] < cur_size) {
589 DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
590 pipe_name(intel_crtc->pipe),
591 cur_size, fb->base.pitches[0]);
592 fb = NULL;
593 break;
594 }
595
596 cur_size = intel_crtc->config->base.adjusted_mode.crtc_vdisplay;
597 cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
598 cur_size *= fb->base.pitches[0];
599 DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
600 pipe_name(intel_crtc->pipe),
601 intel_crtc->config->base.adjusted_mode.crtc_hdisplay,
602 intel_crtc->config->base.adjusted_mode.crtc_vdisplay,
603 fb->base.format->cpp[0] * 8,
604 cur_size);
605
606 if (cur_size > max_size) {
607 DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
608 pipe_name(intel_crtc->pipe),
609 cur_size, max_size);
610 fb = NULL;
611 break;
612 }
613
614 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
615 pipe_name(intel_crtc->pipe),
616 max_size, cur_size);
617 }
618
619 if (!fb) {
620 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
621 goto out;
622 }
623
624 ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
625 ifbdev->fb = fb;
626
627 drm_framebuffer_reference(&ifbdev->fb->base);
628
629
630 for_each_crtc(dev, crtc) {
631 intel_crtc = to_intel_crtc(crtc);
632
633 if (!crtc->state->active)
634 continue;
635
636 WARN(!crtc->primary->fb,
637 "re-used BIOS config but lost an fb on crtc %d\n",
638 crtc->base.id);
639 }
640
641
642 DRM_DEBUG_KMS("using BIOS fb for initial console\n");
643 return true;
644
645out:
646
647 return false;
648}
649
650static void intel_fbdev_suspend_worker(struct work_struct *work)
651{
652 intel_fbdev_set_suspend(&container_of(work,
653 struct drm_i915_private,
654 fbdev_suspend_work)->drm,
655 FBINFO_STATE_RUNNING,
656 true);
657}
658
659int intel_fbdev_init(struct drm_device *dev)
660{
661 struct drm_i915_private *dev_priv = to_i915(dev);
662 struct intel_fbdev *ifbdev;
663 int ret;
664
665 if (WARN_ON(INTEL_INFO(dev_priv)->num_pipes == 0))
666 return -ENODEV;
667
668 ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
669 if (ifbdev == NULL)
670 return -ENOMEM;
671
672 drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
673
674 if (!intel_fbdev_init_bios(dev, ifbdev))
675 ifbdev->preferred_bpp = 32;
676
677 ret = drm_fb_helper_init(dev, &ifbdev->helper, 4);
678 if (ret) {
679 kfree(ifbdev);
680 return ret;
681 }
682
683 dev_priv->fbdev = ifbdev;
684 INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
685
686 drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
687
688 return 0;
689}
690
691static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
692{
693 struct intel_fbdev *ifbdev = data;
694
695
696 if (drm_fb_helper_initial_config(&ifbdev->helper,
697 ifbdev->preferred_bpp)) {
698 intel_fbdev_unregister(to_i915(ifbdev->helper.dev));
699 intel_fbdev_fini(to_i915(ifbdev->helper.dev));
700 }
701}
702
703void intel_fbdev_initial_config_async(struct drm_device *dev)
704{
705 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
706
707 if (!ifbdev)
708 return;
709
710 ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
711}
712
713static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
714{
715 if (!ifbdev->cookie)
716 return;
717
718
719 async_synchronize_cookie(ifbdev->cookie + 1);
720 ifbdev->cookie = 0;
721}
722
723void intel_fbdev_unregister(struct drm_i915_private *dev_priv)
724{
725 struct intel_fbdev *ifbdev = dev_priv->fbdev;
726
727 if (!ifbdev)
728 return;
729
730 cancel_work_sync(&dev_priv->fbdev_suspend_work);
731 if (!current_is_async())
732 intel_fbdev_sync(ifbdev);
733
734 drm_fb_helper_unregister_fbi(&ifbdev->helper);
735}
736
737void intel_fbdev_fini(struct drm_i915_private *dev_priv)
738{
739 struct intel_fbdev *ifbdev = fetch_and_zero(&dev_priv->fbdev);
740
741 if (!ifbdev)
742 return;
743
744 intel_fbdev_destroy(ifbdev);
745}
746
747void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
748{
749 struct drm_i915_private *dev_priv = to_i915(dev);
750 struct intel_fbdev *ifbdev = dev_priv->fbdev;
751 struct fb_info *info;
752
753 if (!ifbdev || !ifbdev->vma)
754 return;
755
756 info = ifbdev->helper.fbdev;
757
758 if (synchronous) {
759
760
761
762
763
764
765
766 if (state != FBINFO_STATE_RUNNING)
767 flush_work(&dev_priv->fbdev_suspend_work);
768 console_lock();
769 } else {
770
771
772
773
774
775 WARN_ON(state != FBINFO_STATE_RUNNING);
776 if (!console_trylock()) {
777
778
779
780 schedule_work(&dev_priv->fbdev_suspend_work);
781 return;
782 }
783 }
784
785
786
787
788
789 if (state == FBINFO_STATE_RUNNING && ifbdev->fb->obj->stolen)
790 memset_io(info->screen_base, 0, info->screen_size);
791
792 drm_fb_helper_set_suspend(&ifbdev->helper, state);
793 console_unlock();
794}
795
796void intel_fbdev_output_poll_changed(struct drm_device *dev)
797{
798 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
799
800 if (ifbdev)
801 drm_fb_helper_hotplug_event(&ifbdev->helper);
802}
803
804void intel_fbdev_restore_mode(struct drm_device *dev)
805{
806 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
807
808 if (!ifbdev)
809 return;
810
811 intel_fbdev_sync(ifbdev);
812 if (!ifbdev->vma)
813 return;
814
815 if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper) == 0)
816 intel_fbdev_invalidate(ifbdev);
817}
818