xref: /linux/drivers/gpu/drm/i915/display/intel_fbdev.c (revision 22c55fb9eb92395d999b8404d73e58540d11bdd8)
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 
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 
74 static struct intel_frontbuffer *to_frontbuffer(struct intel_fbdev *ifbdev)
75 {
76 	return ifbdev->fb->frontbuffer;
77 }
78 
79 static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev)
80 {
81 	intel_frontbuffer_invalidate(to_frontbuffer(ifbdev), ORIGIN_CPU);
82 }
83 
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 
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 
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 
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 
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 	drm_fb_helper_unprepare(fb_helper);
150 	kfree(fb_helper);
151 }
152 
153 __diag_push();
154 __diag_ignore_all("-Woverride-init", "Allow field initialization overrides for fb ops");
155 
156 static const struct fb_ops intelfb_ops = {
157 	.owner = THIS_MODULE,
158 	__FB_DEFAULT_DEFERRED_OPS_RDWR(intel_fbdev),
159 	DRM_FB_HELPER_DEFAULT_OPS,
160 	.fb_set_par = intel_fbdev_set_par,
161 	.fb_blank = intel_fbdev_blank,
162 	.fb_pan_display = intel_fbdev_pan_display,
163 	__FB_DEFAULT_DEFERRED_OPS_DRAW(intel_fbdev),
164 	.fb_mmap = intel_fbdev_mmap,
165 	.fb_destroy = intel_fbdev_fb_destroy,
166 };
167 
168 __diag_pop();
169 
170 static int intelfb_dirty(struct drm_fb_helper *helper, struct drm_clip_rect *clip)
171 {
172 	if (!(clip->x1 < clip->x2 && clip->y1 < clip->y2))
173 		return 0;
174 
175 	if (helper->fb->funcs->dirty)
176 		return helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, clip, 1);
177 
178 	return 0;
179 }
180 
181 static void intelfb_restore(struct drm_fb_helper *fb_helper)
182 {
183 	struct intel_fbdev *ifbdev = to_intel_fbdev(fb_helper);
184 
185 	intel_fbdev_invalidate(ifbdev);
186 }
187 
188 static void intelfb_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
189 {
190 	struct fb_info *info = fb_helper->info;
191 
192 	/*
193 	 * When resuming from hibernation, Linux restores the object's
194 	 * content from swap if the buffer is backed by shmemfs. If the
195 	 * object is stolen however, it will be full of whatever garbage
196 	 * was left in there. Clear it to zero in this case.
197 	 */
198 	if (!suspend && !intel_bo_is_shmem(intel_fb_bo(fb_helper->fb)))
199 		memset_io(info->screen_base, 0, info->screen_size);
200 
201 	fb_set_suspend(info, suspend);
202 }
203 
204 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
205 	.fb_dirty = intelfb_dirty,
206 	.fb_restore = intelfb_restore,
207 	.fb_set_suspend = intelfb_set_suspend,
208 };
209 
210 int intel_fbdev_driver_fbdev_probe(struct drm_fb_helper *helper,
211 				   struct drm_fb_helper_surface_size *sizes)
212 {
213 	struct intel_display *display = to_intel_display(helper->dev);
214 	struct intel_fbdev *ifbdev = to_intel_fbdev(helper);
215 	struct intel_framebuffer *fb = ifbdev->fb;
216 	struct ref_tracker *wakeref;
217 	struct fb_info *info;
218 	struct i915_vma *vma;
219 	unsigned long flags = 0;
220 	bool prealloc = false;
221 	struct drm_gem_object *obj;
222 	int ret;
223 
224 	ifbdev->fb = NULL;
225 
226 	if (fb &&
227 	    (sizes->fb_width > fb->base.width ||
228 	     sizes->fb_height > fb->base.height)) {
229 		drm_dbg_kms(display->drm,
230 			    "BIOS fb too small (%dx%d), we require (%dx%d),"
231 			    " releasing it\n",
232 			    fb->base.width, fb->base.height,
233 			    sizes->fb_width, sizes->fb_height);
234 		drm_framebuffer_put(&fb->base);
235 		fb = NULL;
236 	}
237 	if (!fb || drm_WARN_ON(display->drm, !intel_fb_bo(&fb->base))) {
238 		drm_dbg_kms(display->drm,
239 			    "no BIOS fb, allocating a new one\n");
240 		fb = intel_fbdev_fb_alloc(helper, sizes);
241 		if (IS_ERR(fb))
242 			return PTR_ERR(fb);
243 	} else {
244 		drm_dbg_kms(display->drm, "re-using BIOS fb\n");
245 		prealloc = true;
246 		sizes->fb_width = fb->base.width;
247 		sizes->fb_height = fb->base.height;
248 	}
249 
250 	wakeref = intel_display_rpm_get(display);
251 
252 	/* Pin the GGTT vma for our access via info->screen_base.
253 	 * This also validates that any existing fb inherited from the
254 	 * BIOS is suitable for own access.
255 	 */
256 	vma = intel_fb_pin_to_ggtt(&fb->base, &fb->normal_view.gtt,
257 				   fb->min_alignment, 0,
258 				   intel_fb_view_vtd_guard(&fb->base, &fb->normal_view,
259 							   DRM_MODE_ROTATE_0),
260 				   false, &flags);
261 	if (IS_ERR(vma)) {
262 		ret = PTR_ERR(vma);
263 		goto out_unlock;
264 	}
265 
266 	info = drm_fb_helper_alloc_info(helper);
267 	if (IS_ERR(info)) {
268 		drm_err(display->drm, "Failed to allocate fb_info (%pe)\n", info);
269 		ret = PTR_ERR(info);
270 		goto out_unpin;
271 	}
272 
273 	helper->funcs = &intel_fb_helper_funcs;
274 	helper->fb = &fb->base;
275 
276 	info->fbops = &intelfb_ops;
277 
278 	obj = intel_fb_bo(&fb->base);
279 
280 	ret = intel_fbdev_fb_fill_info(display, info, obj, vma);
281 	if (ret)
282 		goto out_unpin;
283 
284 	drm_fb_helper_fill_info(info, display->drm->fb_helper, sizes);
285 
286 	/* If the object is shmemfs backed, it will have given us zeroed pages.
287 	 * If the object is stolen however, it will be full of whatever
288 	 * garbage was left in there.
289 	 */
290 	if (!intel_bo_is_shmem(obj) && !prealloc)
291 		memset_io(info->screen_base, 0, info->screen_size);
292 
293 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
294 
295 	drm_dbg_kms(display->drm, "allocated %dx%d fb: 0x%08x\n",
296 		    fb->base.width, fb->base.height,
297 		    i915_ggtt_offset(vma));
298 	ifbdev->fb = fb;
299 	ifbdev->vma = vma;
300 	ifbdev->vma_flags = flags;
301 
302 	intel_display_rpm_put(display, wakeref);
303 
304 	return 0;
305 
306 out_unpin:
307 	intel_fb_unpin_vma(vma, flags);
308 out_unlock:
309 	intel_display_rpm_put(display, wakeref);
310 
311 	return ret;
312 }
313 
314 /*
315  * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
316  * The core display code will have read out the current plane configuration,
317  * so we use that to figure out if there's an object for us to use as the
318  * fb, and if so, we re-use it for the fbdev configuration.
319  *
320  * Note we only support a single fb shared across pipes for boot (mostly for
321  * fbcon), so we just find the biggest and use that.
322  */
323 static bool intel_fbdev_init_bios(struct intel_display *display,
324 				  struct intel_fbdev *ifbdev)
325 {
326 	struct intel_framebuffer *fb = NULL;
327 	struct intel_crtc *crtc;
328 	unsigned int max_size = 0;
329 
330 	/* Find the largest fb */
331 	for_each_intel_crtc(display->drm, crtc) {
332 		struct intel_crtc_state *crtc_state =
333 			to_intel_crtc_state(crtc->base.state);
334 		struct intel_plane *plane =
335 			to_intel_plane(crtc->base.primary);
336 		struct intel_plane_state *plane_state =
337 			to_intel_plane_state(plane->base.state);
338 		struct drm_gem_object *obj = intel_fb_bo(plane_state->uapi.fb);
339 
340 		if (!crtc_state->uapi.active) {
341 			drm_dbg_kms(display->drm,
342 				    "[CRTC:%d:%s] not active, skipping\n",
343 				    crtc->base.base.id, crtc->base.name);
344 			continue;
345 		}
346 
347 		if (!obj) {
348 			drm_dbg_kms(display->drm,
349 				    "[PLANE:%d:%s] no fb, skipping\n",
350 				    plane->base.base.id, plane->base.name);
351 			continue;
352 		}
353 
354 		if (obj->size > max_size) {
355 			drm_dbg_kms(display->drm,
356 				    "found possible fb from [PLANE:%d:%s]\n",
357 				    plane->base.base.id, plane->base.name);
358 			fb = to_intel_framebuffer(plane_state->uapi.fb);
359 			max_size = obj->size;
360 		}
361 	}
362 
363 	if (!fb) {
364 		drm_dbg_kms(display->drm,
365 			    "no active fbs found, not using BIOS config\n");
366 		goto out;
367 	}
368 
369 	/* Now make sure all the pipes will fit into it */
370 	for_each_intel_crtc(display->drm, crtc) {
371 		struct intel_crtc_state *crtc_state =
372 			to_intel_crtc_state(crtc->base.state);
373 		struct intel_plane *plane =
374 			to_intel_plane(crtc->base.primary);
375 		unsigned int cur_size;
376 
377 		if (!crtc_state->uapi.active) {
378 			drm_dbg_kms(display->drm,
379 				    "[CRTC:%d:%s] not active, skipping\n",
380 				    crtc->base.base.id, crtc->base.name);
381 			continue;
382 		}
383 
384 		drm_dbg_kms(display->drm, "checking [PLANE:%d:%s] for BIOS fb\n",
385 			    plane->base.base.id, plane->base.name);
386 
387 		/*
388 		 * See if the plane fb we found above will fit on this
389 		 * pipe.  Note we need to use the selected fb's pitch and bpp
390 		 * rather than the current pipe's, since they differ.
391 		 */
392 		cur_size = crtc_state->uapi.adjusted_mode.crtc_hdisplay;
393 		cur_size = cur_size * fb->base.format->cpp[0];
394 		if (fb->base.pitches[0] < cur_size) {
395 			drm_dbg_kms(display->drm,
396 				    "fb not wide enough for [PLANE:%d:%s] (%d vs %d)\n",
397 				    plane->base.base.id, plane->base.name,
398 				    cur_size, fb->base.pitches[0]);
399 			fb = NULL;
400 			break;
401 		}
402 
403 		cur_size = crtc_state->uapi.adjusted_mode.crtc_vdisplay;
404 		cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
405 		cur_size *= fb->base.pitches[0];
406 		drm_dbg_kms(display->drm,
407 			    "[CRTC:%d:%s] area: %dx%d, bpp: %d, size: %d\n",
408 			    crtc->base.base.id, crtc->base.name,
409 			    crtc_state->uapi.adjusted_mode.crtc_hdisplay,
410 			    crtc_state->uapi.adjusted_mode.crtc_vdisplay,
411 			    fb->base.format->cpp[0] * 8,
412 			    cur_size);
413 
414 		if (cur_size > max_size) {
415 			drm_dbg_kms(display->drm,
416 				    "fb not big enough for [PLANE:%d:%s] (%d vs %d)\n",
417 				    plane->base.base.id, plane->base.name,
418 				    cur_size, max_size);
419 			fb = NULL;
420 			break;
421 		}
422 
423 		drm_dbg_kms(display->drm,
424 			    "fb big enough [PLANE:%d:%s] (%d >= %d)\n",
425 			    plane->base.base.id, plane->base.name,
426 			    max_size, cur_size);
427 	}
428 
429 	if (!fb) {
430 		drm_dbg_kms(display->drm,
431 			    "BIOS fb not suitable for all pipes, not using\n");
432 		goto out;
433 	}
434 
435 	ifbdev->fb = fb;
436 
437 	drm_framebuffer_get(&ifbdev->fb->base);
438 
439 	/* Final pass to check if any active pipes don't have fbs */
440 	for_each_intel_crtc(display->drm, crtc) {
441 		struct intel_crtc_state *crtc_state =
442 			to_intel_crtc_state(crtc->base.state);
443 		struct intel_plane *plane =
444 			to_intel_plane(crtc->base.primary);
445 		struct intel_plane_state *plane_state =
446 			to_intel_plane_state(plane->base.state);
447 
448 		if (!crtc_state->uapi.active)
449 			continue;
450 
451 		drm_WARN(display->drm, !plane_state->uapi.fb,
452 			 "re-used BIOS config but lost an fb on [PLANE:%d:%s]\n",
453 			 plane->base.base.id, plane->base.name);
454 	}
455 
456 
457 	drm_dbg_kms(display->drm, "using BIOS fb for initial console\n");
458 	return true;
459 
460 out:
461 
462 	return false;
463 }
464 
465 static unsigned int intel_fbdev_color_mode(const struct drm_format_info *info)
466 {
467 	unsigned int bpp;
468 
469 	if (!info->depth || info->num_planes != 1 || info->has_alpha || info->is_yuv)
470 		return 0;
471 
472 	bpp = drm_format_info_bpp(info, 0);
473 
474 	switch (bpp) {
475 	case 16:
476 		return info->depth; // 15 or 16
477 	default:
478 		return bpp;
479 	}
480 }
481 
482 void intel_fbdev_setup(struct intel_display *display)
483 {
484 	struct intel_fbdev *ifbdev;
485 	unsigned int preferred_bpp = 0;
486 
487 	if (!HAS_DISPLAY(display))
488 		return;
489 
490 	ifbdev = drmm_kzalloc(display->drm, sizeof(*ifbdev), GFP_KERNEL);
491 	if (!ifbdev)
492 		return;
493 
494 	display->fbdev.fbdev = ifbdev;
495 	if (intel_fbdev_init_bios(display, ifbdev))
496 		preferred_bpp = intel_fbdev_color_mode(ifbdev->fb->base.format);
497 	if (!preferred_bpp)
498 		preferred_bpp = 32;
499 
500 	drm_client_setup_with_color_mode(display->drm, preferred_bpp);
501 }
502 
503 struct intel_framebuffer *intel_fbdev_framebuffer(struct intel_fbdev *fbdev)
504 {
505 	if (!fbdev)
506 		return NULL;
507 
508 	return fbdev->fb;
509 }
510 
511 struct i915_vma *intel_fbdev_vma_pointer(struct intel_fbdev *fbdev)
512 {
513 	return fbdev ? fbdev->vma : NULL;
514 }
515 
516 void intel_fbdev_get_map(struct intel_fbdev *fbdev, struct iosys_map *map)
517 {
518 	intel_fb_get_map(fbdev->vma, map);
519 }
520