intel_fbdev.c revision 1.1 1 /* $NetBSD: intel_fbdev.c,v 1.1 2021/12/18 20:15:30 riastradh Exp $ */
2
3 /*
4 * Copyright 2007 David Airlie
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * David Airlie
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: intel_fbdev.c,v 1.1 2021/12/18 20:15:30 riastradh Exp $");
31
32 #include <linux/async.h>
33 #include <linux/console.h>
34 #include <linux/delay.h>
35 #include <linux/errno.h>
36 #include <linux/init.h>
37 #include <linux/kernel.h>
38 #include <linux/mm.h>
39 #include <linux/module.h>
40 #include <linux/string.h>
41 #include <linux/sysrq.h>
42 #include <linux/tty.h>
43 #include <linux/vga_switcheroo.h>
44
45 #include <drm/drm_crtc.h>
46 #include <drm/drm_fb_helper.h>
47 #include <drm/drm_fourcc.h>
48 #include <drm/i915_drm.h>
49
50 #include "i915_drv.h"
51 #include "intel_display_types.h"
52 #include "intel_fbdev.h"
53 #include "intel_frontbuffer.h"
54
55 static struct intel_frontbuffer *to_frontbuffer(struct intel_fbdev *ifbdev)
56 {
57 return ifbdev->fb->frontbuffer;
58 }
59
60 static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev)
61 {
62 intel_frontbuffer_invalidate(to_frontbuffer(ifbdev), ORIGIN_CPU);
63 }
64
65 static int intel_fbdev_set_par(struct fb_info *info)
66 {
67 struct drm_fb_helper *fb_helper = info->par;
68 struct intel_fbdev *ifbdev =
69 container_of(fb_helper, struct intel_fbdev, helper);
70 int ret;
71
72 ret = drm_fb_helper_set_par(info);
73 if (ret == 0)
74 intel_fbdev_invalidate(ifbdev);
75
76 return ret;
77 }
78
79 static int intel_fbdev_blank(int blank, struct fb_info *info)
80 {
81 struct drm_fb_helper *fb_helper = info->par;
82 struct intel_fbdev *ifbdev =
83 container_of(fb_helper, struct intel_fbdev, helper);
84 int ret;
85
86 ret = drm_fb_helper_blank(blank, info);
87 if (ret == 0)
88 intel_fbdev_invalidate(ifbdev);
89
90 return ret;
91 }
92
93 static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
94 struct fb_info *info)
95 {
96 struct drm_fb_helper *fb_helper = info->par;
97 struct intel_fbdev *ifbdev =
98 container_of(fb_helper, struct intel_fbdev, helper);
99 int ret;
100
101 ret = drm_fb_helper_pan_display(var, info);
102 if (ret == 0)
103 intel_fbdev_invalidate(ifbdev);
104
105 return ret;
106 }
107
108 static const struct fb_ops intelfb_ops = {
109 .owner = THIS_MODULE,
110 DRM_FB_HELPER_DEFAULT_OPS,
111 .fb_set_par = intel_fbdev_set_par,
112 .fb_fillrect = drm_fb_helper_cfb_fillrect,
113 .fb_copyarea = drm_fb_helper_cfb_copyarea,
114 .fb_imageblit = drm_fb_helper_cfb_imageblit,
115 .fb_pan_display = intel_fbdev_pan_display,
116 .fb_blank = intel_fbdev_blank,
117 };
118
119 static int intelfb_alloc(struct drm_fb_helper *helper,
120 struct drm_fb_helper_surface_size *sizes)
121 {
122 struct intel_fbdev *ifbdev =
123 container_of(helper, struct intel_fbdev, helper);
124 struct drm_framebuffer *fb;
125 struct drm_device *dev = helper->dev;
126 struct drm_i915_private *dev_priv = to_i915(dev);
127 struct drm_mode_fb_cmd2 mode_cmd = {};
128 struct drm_i915_gem_object *obj;
129 int size;
130
131 /* we don't do packed 24bpp */
132 if (sizes->surface_bpp == 24)
133 sizes->surface_bpp = 32;
134
135 mode_cmd.width = sizes->surface_width;
136 mode_cmd.height = sizes->surface_height;
137
138 mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
139 DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
140 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
141 sizes->surface_depth);
142
143 size = mode_cmd.pitches[0] * mode_cmd.height;
144 size = PAGE_ALIGN(size);
145
146 /* If the FB is too big, just don't use it since fbdev is not very
147 * important and we should probably use that space with FBC or other
148 * features. */
149 obj = ERR_PTR(-ENODEV);
150 if (size * 2 < dev_priv->stolen_usable_size)
151 obj = i915_gem_object_create_stolen(dev_priv, size);
152 if (IS_ERR(obj))
153 obj = i915_gem_object_create_shmem(dev_priv, size);
154 if (IS_ERR(obj)) {
155 DRM_ERROR("failed to allocate framebuffer\n");
156 return PTR_ERR(obj);
157 }
158
159 fb = intel_framebuffer_create(obj, &mode_cmd);
160 i915_gem_object_put(obj);
161 if (IS_ERR(fb))
162 return PTR_ERR(fb);
163
164 ifbdev->fb = to_intel_framebuffer(fb);
165 return 0;
166 }
167
168 static 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 const struct i915_ggtt_view view = {
179 .type = I915_GGTT_VIEW_NORMAL,
180 };
181 intel_wakeref_t wakeref;
182 struct fb_info *info;
183 struct i915_vma *vma;
184 unsigned long flags = 0;
185 bool prealloc = false;
186 void __iomem *vaddr;
187 int ret;
188
189 if (intel_fb &&
190 (sizes->fb_width > intel_fb->base.width ||
191 sizes->fb_height > intel_fb->base.height)) {
192 DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d),"
193 " releasing it\n",
194 intel_fb->base.width, intel_fb->base.height,
195 sizes->fb_width, sizes->fb_height);
196 drm_framebuffer_put(&intel_fb->base);
197 intel_fb = ifbdev->fb = NULL;
198 }
199 if (!intel_fb || WARN_ON(!intel_fb_obj(&intel_fb->base))) {
200 DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
201 ret = intelfb_alloc(helper, sizes);
202 if (ret)
203 return ret;
204 intel_fb = ifbdev->fb;
205 } else {
206 DRM_DEBUG_KMS("re-using BIOS fb\n");
207 prealloc = true;
208 sizes->fb_width = intel_fb->base.width;
209 sizes->fb_height = intel_fb->base.height;
210 }
211
212 wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
213
214 /* Pin the GGTT vma for our access via info->screen_base.
215 * This also validates that any existing fb inherited from the
216 * BIOS is suitable for own access.
217 */
218 vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base,
219 &view, false, &flags);
220 if (IS_ERR(vma)) {
221 ret = PTR_ERR(vma);
222 goto out_unlock;
223 }
224
225 intel_frontbuffer_flush(to_frontbuffer(ifbdev), ORIGIN_DIRTYFB);
226
227 info = drm_fb_helper_alloc_fbi(helper);
228 if (IS_ERR(info)) {
229 DRM_ERROR("Failed to allocate fb_info\n");
230 ret = PTR_ERR(info);
231 goto out_unpin;
232 }
233
234 ifbdev->helper.fb = &ifbdev->fb->base;
235
236 info->fbops = &intelfb_ops;
237
238 /* setup aperture base/size for vesafb takeover */
239 info->apertures->ranges[0].base = ggtt->gmadr.start;
240 info->apertures->ranges[0].size = ggtt->mappable_end;
241
242 /* Our framebuffer is the entirety of fbdev's system memory */
243 info->fix.smem_start =
244 (unsigned long)(ggtt->gmadr.start + vma->node.start);
245 info->fix.smem_len = vma->node.size;
246
247 vaddr = i915_vma_pin_iomap(vma);
248 if (IS_ERR(vaddr)) {
249 DRM_ERROR("Failed to remap framebuffer into virtual memory\n");
250 ret = PTR_ERR(vaddr);
251 goto out_unpin;
252 }
253 info->screen_base = vaddr;
254 info->screen_size = vma->node.size;
255
256 drm_fb_helper_fill_info(info, &ifbdev->helper, sizes);
257
258 /* If the object is shmemfs backed, it will have given us zeroed pages.
259 * If the object is stolen however, it will be full of whatever
260 * garbage was left in there.
261 */
262 if (vma->obj->stolen && !prealloc)
263 memset_io(info->screen_base, 0, info->screen_size);
264
265 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
266
267 DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08x\n",
268 ifbdev->fb->base.width, ifbdev->fb->base.height,
269 i915_ggtt_offset(vma));
270 ifbdev->vma = vma;
271 ifbdev->vma_flags = flags;
272
273 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
274 vga_switcheroo_client_fb_set(pdev, info);
275 return 0;
276
277 out_unpin:
278 intel_unpin_fb_vma(vma, flags);
279 out_unlock:
280 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
281 return ret;
282 }
283
284 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
285 .fb_probe = intelfb_create,
286 };
287
288 static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
289 {
290 /* We rely on the object-free to release the VMA pinning for
291 * the info->screen_base mmaping. Leaking the VMA is simpler than
292 * trying to rectify all the possible error paths leading here.
293 */
294
295 drm_fb_helper_fini(&ifbdev->helper);
296
297 if (ifbdev->vma)
298 intel_unpin_fb_vma(ifbdev->vma, ifbdev->vma_flags);
299
300 if (ifbdev->fb)
301 drm_framebuffer_remove(&ifbdev->fb->base);
302
303 kfree(ifbdev);
304 }
305
306 /*
307 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
308 * The core display code will have read out the current plane configuration,
309 * so we use that to figure out if there's an object for us to use as the
310 * fb, and if so, we re-use it for the fbdev configuration.
311 *
312 * Note we only support a single fb shared across pipes for boot (mostly for
313 * fbcon), so we just find the biggest and use that.
314 */
315 static bool intel_fbdev_init_bios(struct drm_device *dev,
316 struct intel_fbdev *ifbdev)
317 {
318 struct intel_framebuffer *fb = NULL;
319 struct drm_crtc *crtc;
320 struct intel_crtc *intel_crtc;
321 unsigned int max_size = 0;
322
323 /* Find the largest fb */
324 for_each_crtc(dev, crtc) {
325 struct drm_i915_gem_object *obj =
326 intel_fb_obj(crtc->primary->state->fb);
327 intel_crtc = to_intel_crtc(crtc);
328
329 if (!crtc->state->active || !obj) {
330 DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
331 pipe_name(intel_crtc->pipe));
332 continue;
333 }
334
335 if (obj->base.size > max_size) {
336 DRM_DEBUG_KMS("found possible fb from plane %c\n",
337 pipe_name(intel_crtc->pipe));
338 fb = to_intel_framebuffer(crtc->primary->state->fb);
339 max_size = obj->base.size;
340 }
341 }
342
343 if (!fb) {
344 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
345 goto out;
346 }
347
348 /* Now make sure all the pipes will fit into it */
349 for_each_crtc(dev, crtc) {
350 unsigned int cur_size;
351
352 intel_crtc = to_intel_crtc(crtc);
353
354 if (!crtc->state->active) {
355 DRM_DEBUG_KMS("pipe %c not active, skipping\n",
356 pipe_name(intel_crtc->pipe));
357 continue;
358 }
359
360 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
361 pipe_name(intel_crtc->pipe));
362
363 /*
364 * See if the plane fb we found above will fit on this
365 * pipe. Note we need to use the selected fb's pitch and bpp
366 * rather than the current pipe's, since they differ.
367 */
368 cur_size = crtc->state->adjusted_mode.crtc_hdisplay;
369 cur_size = cur_size * fb->base.format->cpp[0];
370 if (fb->base.pitches[0] < cur_size) {
371 DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
372 pipe_name(intel_crtc->pipe),
373 cur_size, fb->base.pitches[0]);
374 fb = NULL;
375 break;
376 }
377
378 cur_size = crtc->state->adjusted_mode.crtc_vdisplay;
379 cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
380 cur_size *= fb->base.pitches[0];
381 DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
382 pipe_name(intel_crtc->pipe),
383 crtc->state->adjusted_mode.crtc_hdisplay,
384 crtc->state->adjusted_mode.crtc_vdisplay,
385 fb->base.format->cpp[0] * 8,
386 cur_size);
387
388 if (cur_size > max_size) {
389 DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
390 pipe_name(intel_crtc->pipe),
391 cur_size, max_size);
392 fb = NULL;
393 break;
394 }
395
396 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
397 pipe_name(intel_crtc->pipe),
398 max_size, cur_size);
399 }
400
401 if (!fb) {
402 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
403 goto out;
404 }
405
406 ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
407 ifbdev->fb = fb;
408
409 drm_framebuffer_get(&ifbdev->fb->base);
410
411 /* Final pass to check if any active pipes don't have fbs */
412 for_each_crtc(dev, crtc) {
413 intel_crtc = to_intel_crtc(crtc);
414
415 if (!crtc->state->active)
416 continue;
417
418 WARN(!crtc->primary->state->fb,
419 "re-used BIOS config but lost an fb on crtc %d\n",
420 crtc->base.id);
421 }
422
423
424 DRM_DEBUG_KMS("using BIOS fb for initial console\n");
425 return true;
426
427 out:
428
429 return false;
430 }
431
432 static void intel_fbdev_suspend_worker(struct work_struct *work)
433 {
434 intel_fbdev_set_suspend(&container_of(work,
435 struct drm_i915_private,
436 fbdev_suspend_work)->drm,
437 FBINFO_STATE_RUNNING,
438 true);
439 }
440
441 int intel_fbdev_init(struct drm_device *dev)
442 {
443 struct drm_i915_private *dev_priv = to_i915(dev);
444 struct intel_fbdev *ifbdev;
445 int ret;
446
447 if (WARN_ON(!HAS_DISPLAY(dev_priv) || !INTEL_DISPLAY_ENABLED(dev_priv)))
448 return -ENODEV;
449
450 ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
451 if (ifbdev == NULL)
452 return -ENOMEM;
453
454 mutex_init(&ifbdev->hpd_lock);
455 drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
456
457 if (!intel_fbdev_init_bios(dev, ifbdev))
458 ifbdev->preferred_bpp = 32;
459
460 ret = drm_fb_helper_init(dev, &ifbdev->helper, 4);
461 if (ret) {
462 kfree(ifbdev);
463 return ret;
464 }
465
466 dev_priv->fbdev = ifbdev;
467 INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
468
469 drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
470
471 return 0;
472 }
473
474 static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
475 {
476 struct intel_fbdev *ifbdev = data;
477
478 /* Due to peculiar init order wrt to hpd handling this is separate. */
479 if (drm_fb_helper_initial_config(&ifbdev->helper,
480 ifbdev->preferred_bpp))
481 intel_fbdev_unregister(to_i915(ifbdev->helper.dev));
482 }
483
484 void intel_fbdev_initial_config_async(struct drm_device *dev)
485 {
486 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
487
488 if (!ifbdev)
489 return;
490
491 ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
492 }
493
494 static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
495 {
496 if (!ifbdev->cookie)
497 return;
498
499 /* Only serialises with all preceding async calls, hence +1 */
500 async_synchronize_cookie(ifbdev->cookie + 1);
501 ifbdev->cookie = 0;
502 }
503
504 void intel_fbdev_unregister(struct drm_i915_private *dev_priv)
505 {
506 struct intel_fbdev *ifbdev = dev_priv->fbdev;
507
508 if (!ifbdev)
509 return;
510
511 cancel_work_sync(&dev_priv->fbdev_suspend_work);
512 if (!current_is_async())
513 intel_fbdev_sync(ifbdev);
514
515 drm_fb_helper_unregister_fbi(&ifbdev->helper);
516 }
517
518 void intel_fbdev_fini(struct drm_i915_private *dev_priv)
519 {
520 struct intel_fbdev *ifbdev = fetch_and_zero(&dev_priv->fbdev);
521
522 if (!ifbdev)
523 return;
524
525 intel_fbdev_destroy(ifbdev);
526 }
527
528 /* Suspends/resumes fbdev processing of incoming HPD events. When resuming HPD
529 * processing, fbdev will perform a full connector reprobe if a hotplug event
530 * was received while HPD was suspended.
531 */
532 static void intel_fbdev_hpd_set_suspend(struct intel_fbdev *ifbdev, int state)
533 {
534 bool send_hpd = false;
535
536 mutex_lock(&ifbdev->hpd_lock);
537 ifbdev->hpd_suspended = state == FBINFO_STATE_SUSPENDED;
538 send_hpd = !ifbdev->hpd_suspended && ifbdev->hpd_waiting;
539 ifbdev->hpd_waiting = false;
540 mutex_unlock(&ifbdev->hpd_lock);
541
542 if (send_hpd) {
543 DRM_DEBUG_KMS("Handling delayed fbcon HPD event\n");
544 drm_fb_helper_hotplug_event(&ifbdev->helper);
545 }
546 }
547
548 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
549 {
550 struct drm_i915_private *dev_priv = to_i915(dev);
551 struct intel_fbdev *ifbdev = dev_priv->fbdev;
552 struct fb_info *info;
553
554 if (!ifbdev || !ifbdev->vma)
555 return;
556
557 info = ifbdev->helper.fbdev;
558
559 if (synchronous) {
560 /* Flush any pending work to turn the console on, and then
561 * wait to turn it off. It must be synchronous as we are
562 * about to suspend or unload the driver.
563 *
564 * Note that from within the work-handler, we cannot flush
565 * ourselves, so only flush outstanding work upon suspend!
566 */
567 if (state != FBINFO_STATE_RUNNING)
568 flush_work(&dev_priv->fbdev_suspend_work);
569
570 console_lock();
571 } else {
572 /*
573 * The console lock can be pretty contented on resume due
574 * to all the printk activity. Try to keep it out of the hot
575 * path of resume if possible.
576 */
577 WARN_ON(state != FBINFO_STATE_RUNNING);
578 if (!console_trylock()) {
579 /* Don't block our own workqueue as this can
580 * be run in parallel with other i915.ko tasks.
581 */
582 schedule_work(&dev_priv->fbdev_suspend_work);
583 return;
584 }
585 }
586
587 /* On resume from hibernation: If the object is shmemfs backed, it has
588 * been restored from swap. If the object is stolen however, it will be
589 * full of whatever garbage was left in there.
590 */
591 if (state == FBINFO_STATE_RUNNING &&
592 intel_fb_obj(&ifbdev->fb->base)->stolen)
593 memset_io(info->screen_base, 0, info->screen_size);
594
595 drm_fb_helper_set_suspend(&ifbdev->helper, state);
596 console_unlock();
597
598 intel_fbdev_hpd_set_suspend(ifbdev, state);
599 }
600
601 void intel_fbdev_output_poll_changed(struct drm_device *dev)
602 {
603 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
604 bool send_hpd;
605
606 if (!ifbdev)
607 return;
608
609 intel_fbdev_sync(ifbdev);
610
611 mutex_lock(&ifbdev->hpd_lock);
612 send_hpd = !ifbdev->hpd_suspended;
613 ifbdev->hpd_waiting = true;
614 mutex_unlock(&ifbdev->hpd_lock);
615
616 if (send_hpd && (ifbdev->vma || ifbdev->helper.deferred_setup))
617 drm_fb_helper_hotplug_event(&ifbdev->helper);
618 }
619
620 void intel_fbdev_restore_mode(struct drm_device *dev)
621 {
622 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
623
624 if (!ifbdev)
625 return;
626
627 intel_fbdev_sync(ifbdev);
628 if (!ifbdev->vma)
629 return;
630
631 if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper) == 0)
632 intel_fbdev_invalidate(ifbdev);
633 }
634