xref: /linux/drivers/gpu/drm/i915/display/intel_fbdev.c (revision 3f1c07fc21c68bd3bd2df9d2c9441f6485e934d9)
1 /*
2  * Copyright © 2007 David Airlie
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * 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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *     David Airlie
25  */
26 
27 #include <linux/console.h>
28 #include <linux/delay.h>
29 #include <linux/errno.h>
30 #include <linux/fb.h>
31 #include <linux/init.h>
32 #include <linux/kernel.h>
33 #include <linux/mm.h>
34 #include <linux/module.h>
35 #include <linux/string.h>
36 #include <linux/sysrq.h>
37 #include <linux/tty.h>
38 #include <linux/vga_switcheroo.h>
39 
40 #include <drm/clients/drm_client_setup.h>
41 #include <drm/drm_crtc.h>
42 #include <drm/drm_crtc_helper.h>
43 #include <drm/drm_fb_helper.h>
44 #include <drm/drm_fourcc.h>
45 #include <drm/drm_gem.h>
46 #include <drm/drm_gem_framebuffer_helper.h>
47 #include <drm/drm_managed.h>
48 #include <drm/drm_print.h>
49 
50 #include "i915_vma.h"
51 #include "intel_bo.h"
52 #include "intel_display_core.h"
53 #include "intel_display_rpm.h"
54 #include "intel_display_types.h"
55 #include "intel_fb.h"
56 #include "intel_fb_pin.h"
57 #include "intel_fbdev.h"
58 #include "intel_fbdev_fb.h"
59 #include "intel_frontbuffer.h"
60 
61 struct intel_fbdev {
62 	struct intel_framebuffer *fb;
63 	struct i915_vma *vma;
64 	unsigned long vma_flags;
65 };
66 
to_intel_fbdev(struct drm_fb_helper * fb_helper)67 static struct intel_fbdev *to_intel_fbdev(struct drm_fb_helper *fb_helper)
68 {
69 	struct intel_display *display = to_intel_display(fb_helper->client.dev);
70 
71 	return display->fbdev.fbdev;
72 }
73 
to_frontbuffer(struct intel_fbdev * ifbdev)74 static struct intel_frontbuffer *to_frontbuffer(struct intel_fbdev *ifbdev)
75 {
76 	return ifbdev->fb->frontbuffer;
77 }
78 
intel_fbdev_invalidate(struct intel_fbdev * ifbdev)79 static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev)
80 {
81 	intel_frontbuffer_invalidate(to_frontbuffer(ifbdev), ORIGIN_CPU);
82 }
83 
FB_GEN_DEFAULT_DEFERRED_IOMEM_OPS(intel_fbdev,drm_fb_helper_damage_range,drm_fb_helper_damage_area)84 FB_GEN_DEFAULT_DEFERRED_IOMEM_OPS(intel_fbdev,
85 				  drm_fb_helper_damage_range,
86 				  drm_fb_helper_damage_area)
87 
88 static int intel_fbdev_set_par(struct fb_info *info)
89 {
90 	struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
91 	int ret;
92 
93 	ret = drm_fb_helper_set_par(info);
94 	if (ret == 0)
95 		intel_fbdev_invalidate(ifbdev);
96 
97 	return ret;
98 }
99 
intel_fbdev_blank(int blank,struct fb_info * info)100 static int intel_fbdev_blank(int blank, struct fb_info *info)
101 {
102 	struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
103 	int ret;
104 
105 	ret = drm_fb_helper_blank(blank, info);
106 	if (ret == 0)
107 		intel_fbdev_invalidate(ifbdev);
108 
109 	return ret;
110 }
111 
intel_fbdev_pan_display(struct fb_var_screeninfo * var,struct fb_info * info)112 static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
113 				   struct fb_info *info)
114 {
115 	struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
116 	int ret;
117 
118 	ret = drm_fb_helper_pan_display(var, info);
119 	if (ret == 0)
120 		intel_fbdev_invalidate(ifbdev);
121 
122 	return ret;
123 }
124 
intel_fbdev_mmap(struct fb_info * info,struct vm_area_struct * vma)125 static int intel_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma)
126 {
127 	struct drm_fb_helper *fb_helper = info->par;
128 	struct drm_gem_object *obj = drm_gem_fb_get_obj(fb_helper->fb, 0);
129 
130 	return intel_bo_fb_mmap(obj, vma);
131 }
132 
intel_fbdev_fb_destroy(struct fb_info * info)133 static void intel_fbdev_fb_destroy(struct fb_info *info)
134 {
135 	struct drm_fb_helper *fb_helper = info->par;
136 	struct intel_fbdev *ifbdev = to_intel_fbdev(fb_helper);
137 
138 	drm_fb_helper_fini(fb_helper);
139 
140 	/*
141 	 * We rely on the object-free to release the VMA pinning for
142 	 * the info->screen_base mmaping. Leaking the VMA is simpler than
143 	 * trying to rectify all the possible error paths leading here.
144 	 */
145 	intel_fb_unpin_vma(ifbdev->vma, ifbdev->vma_flags);
146 	drm_framebuffer_remove(fb_helper->fb);
147 
148 	drm_client_release(&fb_helper->client);
149 }
150 
151 __diag_push();
152 __diag_ignore_all("-Woverride-init", "Allow field initialization overrides for fb ops");
153 
154 static const struct fb_ops intelfb_ops = {
155 	.owner = THIS_MODULE,
156 	__FB_DEFAULT_DEFERRED_OPS_RDWR(intel_fbdev),
157 	DRM_FB_HELPER_DEFAULT_OPS,
158 	.fb_set_par = intel_fbdev_set_par,
159 	.fb_blank = intel_fbdev_blank,
160 	.fb_pan_display = intel_fbdev_pan_display,
161 	__FB_DEFAULT_DEFERRED_OPS_DRAW(intel_fbdev),
162 	.fb_mmap = intel_fbdev_mmap,
163 	.fb_destroy = intel_fbdev_fb_destroy,
164 };
165 
166 __diag_pop();
167 
intelfb_dirty(struct drm_fb_helper * helper,struct drm_clip_rect * clip)168 static int intelfb_dirty(struct drm_fb_helper *helper, struct drm_clip_rect *clip)
169 {
170 	if (!(clip->x1 < clip->x2 && clip->y1 < clip->y2))
171 		return 0;
172 
173 	if (helper->fb->funcs->dirty)
174 		return helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, clip, 1);
175 
176 	return 0;
177 }
178 
intelfb_restore(struct drm_fb_helper * fb_helper)179 static void intelfb_restore(struct drm_fb_helper *fb_helper)
180 {
181 	struct intel_fbdev *ifbdev = to_intel_fbdev(fb_helper);
182 
183 	intel_fbdev_invalidate(ifbdev);
184 }
185 
intelfb_set_suspend(struct drm_fb_helper * fb_helper,bool suspend)186 static void intelfb_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
187 {
188 	struct fb_info *info = fb_helper->info;
189 
190 	/*
191 	 * When resuming from hibernation, Linux restores the object's
192 	 * content from swap if the buffer is backed by shmemfs. If the
193 	 * object is stolen however, it will be full of whatever garbage
194 	 * was left in there. Clear it to zero in this case.
195 	 */
196 	if (!suspend && !intel_bo_is_shmem(intel_fb_bo(fb_helper->fb)))
197 		memset_io(info->screen_base, 0, info->screen_size);
198 
199 	fb_set_suspend(info, suspend);
200 }
201 
202 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
203 	.fb_dirty = intelfb_dirty,
204 	.fb_restore = intelfb_restore,
205 	.fb_set_suspend = intelfb_set_suspend,
206 };
207 
intel_fbdev_fill_mode_cmd(struct drm_fb_helper_surface_size * sizes,struct drm_mode_fb_cmd2 * mode_cmd)208 static void intel_fbdev_fill_mode_cmd(struct drm_fb_helper_surface_size *sizes,
209 				      struct drm_mode_fb_cmd2 *mode_cmd)
210 {
211 	/* we don't do packed 24bpp */
212 	if (sizes->surface_bpp == 24)
213 		sizes->surface_bpp = 32;
214 
215 	mode_cmd->flags = DRM_MODE_FB_MODIFIERS;
216 	mode_cmd->width = sizes->surface_width;
217 	mode_cmd->height = sizes->surface_height;
218 
219 	mode_cmd->pitches[0] = intel_fbdev_fb_pitch_align(mode_cmd->width * DIV_ROUND_UP(sizes->surface_bpp, 8));
220 	mode_cmd->pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
221 							   sizes->surface_depth);
222 	mode_cmd->modifier[0] = DRM_FORMAT_MOD_LINEAR;
223 }
224 
225 static struct intel_framebuffer *
__intel_fbdev_fb_alloc(struct intel_display * display,struct drm_fb_helper_surface_size * sizes)226 __intel_fbdev_fb_alloc(struct intel_display *display,
227 		       struct drm_fb_helper_surface_size *sizes)
228 {
229 	struct drm_mode_fb_cmd2 mode_cmd = {};
230 	struct drm_framebuffer *fb;
231 	struct drm_gem_object *obj;
232 	int size;
233 
234 	intel_fbdev_fill_mode_cmd(sizes, &mode_cmd);
235 
236 	size = mode_cmd.pitches[0] * mode_cmd.height;
237 	size = PAGE_ALIGN(size);
238 
239 	obj = intel_fbdev_fb_bo_create(display->drm, size);
240 	if (IS_ERR(obj)) {
241 		fb = ERR_CAST(obj);
242 		goto err;
243 	}
244 
245 	fb = intel_framebuffer_create(obj,
246 				      drm_get_format_info(display->drm,
247 							  mode_cmd.pixel_format,
248 							  mode_cmd.modifier[0]),
249 				      &mode_cmd);
250 	if (IS_ERR(fb)) {
251 		intel_fbdev_fb_bo_destroy(obj);
252 		goto err;
253 	}
254 
255 	drm_gem_object_put(obj);
256 
257 	return to_intel_framebuffer(fb);
258 
259 err:
260 	return ERR_CAST(fb);
261 
262 }
263 
intel_fbdev_driver_fbdev_probe(struct drm_fb_helper * helper,struct drm_fb_helper_surface_size * sizes)264 int intel_fbdev_driver_fbdev_probe(struct drm_fb_helper *helper,
265 				   struct drm_fb_helper_surface_size *sizes)
266 {
267 	struct intel_display *display = to_intel_display(helper->dev);
268 	struct intel_fbdev *ifbdev = to_intel_fbdev(helper);
269 	struct intel_framebuffer *fb = ifbdev->fb;
270 	struct fb_info *info = helper->info;
271 	struct ref_tracker *wakeref;
272 	struct i915_vma *vma;
273 	unsigned long flags = 0;
274 	bool prealloc = false;
275 	struct drm_gem_object *obj;
276 	int ret;
277 
278 	ifbdev->fb = NULL;
279 
280 	if (fb &&
281 	    (sizes->fb_width > fb->base.width ||
282 	     sizes->fb_height > fb->base.height)) {
283 		drm_dbg_kms(display->drm,
284 			    "BIOS fb too small (%dx%d), we require (%dx%d),"
285 			    " releasing it\n",
286 			    fb->base.width, fb->base.height,
287 			    sizes->fb_width, sizes->fb_height);
288 		drm_framebuffer_put(&fb->base);
289 		fb = NULL;
290 	}
291 	if (!fb || drm_WARN_ON(display->drm, !intel_fb_bo(&fb->base))) {
292 		drm_dbg_kms(display->drm,
293 			    "no BIOS fb, allocating a new one\n");
294 
295 		fb = __intel_fbdev_fb_alloc(display, sizes);
296 		if (IS_ERR(fb))
297 			return PTR_ERR(fb);
298 	} else {
299 		drm_dbg_kms(display->drm, "re-using BIOS fb\n");
300 		prealloc = true;
301 		sizes->fb_width = fb->base.width;
302 		sizes->fb_height = fb->base.height;
303 	}
304 
305 	wakeref = intel_display_rpm_get(display);
306 
307 	/* Pin the GGTT vma for our access via info->screen_base.
308 	 * This also validates that any existing fb inherited from the
309 	 * BIOS is suitable for own access.
310 	 */
311 	vma = intel_fb_pin_to_ggtt(&fb->base, &fb->normal_view.gtt,
312 				   fb->min_alignment, 0,
313 				   intel_fb_view_vtd_guard(&fb->base, &fb->normal_view,
314 							   DRM_MODE_ROTATE_0),
315 				   false, &flags);
316 	if (IS_ERR(vma)) {
317 		ret = PTR_ERR(vma);
318 		goto out_unlock;
319 	}
320 
321 	helper->funcs = &intel_fb_helper_funcs;
322 	helper->fb = &fb->base;
323 
324 	info->fbops = &intelfb_ops;
325 
326 	obj = intel_fb_bo(&fb->base);
327 
328 	ret = intel_fbdev_fb_fill_info(display->drm, info, obj, vma);
329 	if (ret)
330 		goto out_unpin;
331 
332 	drm_fb_helper_fill_info(info, display->drm->fb_helper, sizes);
333 
334 	/* If the object is shmemfs backed, it will have given us zeroed pages.
335 	 * If the object is stolen however, it will be full of whatever
336 	 * garbage was left in there.
337 	 */
338 	if (!intel_bo_is_shmem(obj) && !prealloc)
339 		memset_io(info->screen_base, 0, info->screen_size);
340 
341 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
342 
343 	drm_dbg_kms(display->drm, "allocated %dx%d fb: 0x%08x\n",
344 		    fb->base.width, fb->base.height,
345 		    i915_ggtt_offset(vma));
346 	ifbdev->fb = fb;
347 	ifbdev->vma = vma;
348 	ifbdev->vma_flags = flags;
349 
350 	intel_display_rpm_put(display, wakeref);
351 
352 	return 0;
353 
354 out_unpin:
355 	intel_fb_unpin_vma(vma, flags);
356 out_unlock:
357 	intel_display_rpm_put(display, wakeref);
358 
359 	return ret;
360 }
361 
362 /*
363  * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
364  * The core display code will have read out the current plane configuration,
365  * so we use that to figure out if there's an object for us to use as the
366  * fb, and if so, we re-use it for the fbdev configuration.
367  *
368  * Note we only support a single fb shared across pipes for boot (mostly for
369  * fbcon), so we just find the biggest and use that.
370  */
intel_fbdev_init_bios(struct intel_display * display,struct intel_fbdev * ifbdev)371 static bool intel_fbdev_init_bios(struct intel_display *display,
372 				  struct intel_fbdev *ifbdev)
373 {
374 	struct intel_framebuffer *fb = NULL;
375 	struct intel_crtc *crtc;
376 	unsigned int max_size = 0;
377 
378 	/* Find the largest fb */
379 	for_each_intel_crtc(display->drm, crtc) {
380 		struct intel_crtc_state *crtc_state =
381 			to_intel_crtc_state(crtc->base.state);
382 		struct intel_plane *plane =
383 			to_intel_plane(crtc->base.primary);
384 		struct intel_plane_state *plane_state =
385 			to_intel_plane_state(plane->base.state);
386 		struct drm_gem_object *obj = intel_fb_bo(plane_state->uapi.fb);
387 
388 		if (!crtc_state->uapi.active) {
389 			drm_dbg_kms(display->drm,
390 				    "[CRTC:%d:%s] not active, skipping\n",
391 				    crtc->base.base.id, crtc->base.name);
392 			continue;
393 		}
394 
395 		if (!obj) {
396 			drm_dbg_kms(display->drm,
397 				    "[PLANE:%d:%s] no fb, skipping\n",
398 				    plane->base.base.id, plane->base.name);
399 			continue;
400 		}
401 
402 		if (obj->size > max_size) {
403 			drm_dbg_kms(display->drm,
404 				    "found possible fb from [PLANE:%d:%s]\n",
405 				    plane->base.base.id, plane->base.name);
406 			fb = to_intel_framebuffer(plane_state->uapi.fb);
407 			max_size = obj->size;
408 		}
409 	}
410 
411 	if (!fb) {
412 		drm_dbg_kms(display->drm,
413 			    "no active fbs found, not using BIOS config\n");
414 		goto out;
415 	}
416 
417 	/* Now make sure all the pipes will fit into it */
418 	for_each_intel_crtc(display->drm, crtc) {
419 		struct intel_crtc_state *crtc_state =
420 			to_intel_crtc_state(crtc->base.state);
421 		struct intel_plane *plane =
422 			to_intel_plane(crtc->base.primary);
423 		unsigned int cur_size;
424 
425 		if (!crtc_state->uapi.active) {
426 			drm_dbg_kms(display->drm,
427 				    "[CRTC:%d:%s] not active, skipping\n",
428 				    crtc->base.base.id, crtc->base.name);
429 			continue;
430 		}
431 
432 		drm_dbg_kms(display->drm, "checking [PLANE:%d:%s] for BIOS fb\n",
433 			    plane->base.base.id, plane->base.name);
434 
435 		/*
436 		 * See if the plane fb we found above will fit on this
437 		 * pipe.  Note we need to use the selected fb's pitch and bpp
438 		 * rather than the current pipe's, since they differ.
439 		 */
440 		cur_size = crtc_state->uapi.adjusted_mode.crtc_hdisplay;
441 		cur_size = cur_size * fb->base.format->cpp[0];
442 		if (fb->base.pitches[0] < cur_size) {
443 			drm_dbg_kms(display->drm,
444 				    "fb not wide enough for [PLANE:%d:%s] (%d vs %d)\n",
445 				    plane->base.base.id, plane->base.name,
446 				    cur_size, fb->base.pitches[0]);
447 			fb = NULL;
448 			break;
449 		}
450 
451 		cur_size = crtc_state->uapi.adjusted_mode.crtc_vdisplay;
452 		cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
453 		cur_size *= fb->base.pitches[0];
454 		drm_dbg_kms(display->drm,
455 			    "[CRTC:%d:%s] area: %dx%d, bpp: %d, size: %d\n",
456 			    crtc->base.base.id, crtc->base.name,
457 			    crtc_state->uapi.adjusted_mode.crtc_hdisplay,
458 			    crtc_state->uapi.adjusted_mode.crtc_vdisplay,
459 			    fb->base.format->cpp[0] * 8,
460 			    cur_size);
461 
462 		if (cur_size > max_size) {
463 			drm_dbg_kms(display->drm,
464 				    "fb not big enough for [PLANE:%d:%s] (%d vs %d)\n",
465 				    plane->base.base.id, plane->base.name,
466 				    cur_size, max_size);
467 			fb = NULL;
468 			break;
469 		}
470 
471 		drm_dbg_kms(display->drm,
472 			    "fb big enough [PLANE:%d:%s] (%d >= %d)\n",
473 			    plane->base.base.id, plane->base.name,
474 			    max_size, cur_size);
475 	}
476 
477 	if (!fb) {
478 		drm_dbg_kms(display->drm,
479 			    "BIOS fb not suitable for all pipes, not using\n");
480 		goto out;
481 	}
482 
483 	ifbdev->fb = fb;
484 
485 	drm_framebuffer_get(&ifbdev->fb->base);
486 
487 	/* Final pass to check if any active pipes don't have fbs */
488 	for_each_intel_crtc(display->drm, crtc) {
489 		struct intel_crtc_state *crtc_state =
490 			to_intel_crtc_state(crtc->base.state);
491 		struct intel_plane *plane =
492 			to_intel_plane(crtc->base.primary);
493 		struct intel_plane_state *plane_state =
494 			to_intel_plane_state(plane->base.state);
495 
496 		if (!crtc_state->uapi.active)
497 			continue;
498 
499 		drm_WARN(display->drm, !plane_state->uapi.fb,
500 			 "re-used BIOS config but lost an fb on [PLANE:%d:%s]\n",
501 			 plane->base.base.id, plane->base.name);
502 	}
503 
504 
505 	drm_dbg_kms(display->drm, "using BIOS fb for initial console\n");
506 	return true;
507 
508 out:
509 
510 	return false;
511 }
512 
intel_fbdev_color_mode(const struct drm_format_info * info)513 static unsigned int intel_fbdev_color_mode(const struct drm_format_info *info)
514 {
515 	unsigned int bpp;
516 
517 	if (!info->depth || info->num_planes != 1 || info->has_alpha || info->is_yuv)
518 		return 0;
519 
520 	bpp = drm_format_info_bpp(info, 0);
521 
522 	switch (bpp) {
523 	case 16:
524 		return info->depth; // 15 or 16
525 	default:
526 		return bpp;
527 	}
528 }
529 
intel_fbdev_setup(struct intel_display * display)530 void intel_fbdev_setup(struct intel_display *display)
531 {
532 	struct intel_fbdev *ifbdev;
533 	unsigned int preferred_bpp = 0;
534 
535 	if (!HAS_DISPLAY(display))
536 		return;
537 
538 	ifbdev = drmm_kzalloc(display->drm, sizeof(*ifbdev), GFP_KERNEL);
539 	if (!ifbdev)
540 		return;
541 
542 	display->fbdev.fbdev = ifbdev;
543 	if (intel_fbdev_init_bios(display, ifbdev))
544 		preferred_bpp = intel_fbdev_color_mode(ifbdev->fb->base.format);
545 	if (!preferred_bpp)
546 		preferred_bpp = 32;
547 
548 	drm_client_setup_with_color_mode(display->drm, preferred_bpp);
549 }
550 
intel_fbdev_framebuffer(struct intel_fbdev * fbdev)551 struct intel_framebuffer *intel_fbdev_framebuffer(struct intel_fbdev *fbdev)
552 {
553 	if (!fbdev)
554 		return NULL;
555 
556 	return fbdev->fb;
557 }
558 
intel_fbdev_vma_pointer(struct intel_fbdev * fbdev)559 struct i915_vma *intel_fbdev_vma_pointer(struct intel_fbdev *fbdev)
560 {
561 	return fbdev ? fbdev->vma : NULL;
562 }
563 
intel_fbdev_get_map(struct intel_fbdev * fbdev,struct iosys_map * map)564 void intel_fbdev_get_map(struct intel_fbdev *fbdev, struct iosys_map *map)
565 {
566 	intel_fb_get_map(fbdev->vma, map);
567 }
568