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_drv.h"
51 #include "i915_vma.h"
52 #include "intel_bo.h"
53 #include "intel_display_types.h"
54 #include "intel_fb.h"
55 #include "intel_fb_pin.h"
56 #include "intel_fbdev.h"
57 #include "intel_fbdev_fb.h"
58 #include "intel_frontbuffer.h"
59 
60 struct intel_fbdev {
61 	struct intel_framebuffer *fb;
62 	struct i915_vma *vma;
63 	unsigned long vma_flags;
64 };
65 
to_intel_fbdev(struct drm_fb_helper * fb_helper)66 static struct intel_fbdev *to_intel_fbdev(struct drm_fb_helper *fb_helper)
67 {
68 	struct drm_i915_private *i915 = to_i915(fb_helper->client.dev);
69 
70 	return i915->display.fbdev.fbdev;
71 }
72 
to_frontbuffer(struct intel_fbdev * ifbdev)73 static struct intel_frontbuffer *to_frontbuffer(struct intel_fbdev *ifbdev)
74 {
75 	return ifbdev->fb->frontbuffer;
76 }
77 
intel_fbdev_invalidate(struct intel_fbdev * ifbdev)78 static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev)
79 {
80 	intel_frontbuffer_invalidate(to_frontbuffer(ifbdev), ORIGIN_CPU);
81 }
82 
FB_GEN_DEFAULT_DEFERRED_IOMEM_OPS(intel_fbdev,drm_fb_helper_damage_range,drm_fb_helper_damage_area)83 FB_GEN_DEFAULT_DEFERRED_IOMEM_OPS(intel_fbdev,
84 				  drm_fb_helper_damage_range,
85 				  drm_fb_helper_damage_area)
86 
87 static int intel_fbdev_set_par(struct fb_info *info)
88 {
89 	struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
90 	int ret;
91 
92 	ret = drm_fb_helper_set_par(info);
93 	if (ret == 0)
94 		intel_fbdev_invalidate(ifbdev);
95 
96 	return ret;
97 }
98 
intel_fbdev_blank(int blank,struct fb_info * info)99 static int intel_fbdev_blank(int blank, struct fb_info *info)
100 {
101 	struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
102 	int ret;
103 
104 	ret = drm_fb_helper_blank(blank, info);
105 	if (ret == 0)
106 		intel_fbdev_invalidate(ifbdev);
107 
108 	return ret;
109 }
110 
intel_fbdev_pan_display(struct fb_var_screeninfo * var,struct fb_info * info)111 static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
112 				   struct fb_info *info)
113 {
114 	struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
115 	int ret;
116 
117 	ret = drm_fb_helper_pan_display(var, info);
118 	if (ret == 0)
119 		intel_fbdev_invalidate(ifbdev);
120 
121 	return ret;
122 }
123 
intel_fbdev_mmap(struct fb_info * info,struct vm_area_struct * vma)124 static int intel_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma)
125 {
126 	struct drm_fb_helper *fb_helper = info->par;
127 	struct drm_gem_object *obj = drm_gem_fb_get_obj(fb_helper->fb, 0);
128 
129 	return intel_bo_fb_mmap(obj, vma);
130 }
131 
intel_fbdev_fb_destroy(struct fb_info * info)132 static void intel_fbdev_fb_destroy(struct fb_info *info)
133 {
134 	struct drm_fb_helper *fb_helper = info->par;
135 	struct intel_fbdev *ifbdev = to_intel_fbdev(fb_helper);
136 
137 	drm_fb_helper_fini(fb_helper);
138 
139 	/*
140 	 * We rely on the object-free to release the VMA pinning for
141 	 * the info->screen_base mmaping. Leaking the VMA is simpler than
142 	 * trying to rectify all the possible error paths leading here.
143 	 */
144 	intel_fb_unpin_vma(ifbdev->vma, ifbdev->vma_flags);
145 	drm_framebuffer_remove(fb_helper->fb);
146 
147 	drm_client_release(&fb_helper->client);
148 	drm_fb_helper_unprepare(fb_helper);
149 	kfree(fb_helper);
150 }
151 
152 __diag_push();
153 __diag_ignore_all("-Woverride-init", "Allow field initialization overrides for fb ops");
154 
155 static const struct fb_ops intelfb_ops = {
156 	.owner = THIS_MODULE,
157 	__FB_DEFAULT_DEFERRED_OPS_RDWR(intel_fbdev),
158 	DRM_FB_HELPER_DEFAULT_OPS,
159 	.fb_set_par = intel_fbdev_set_par,
160 	.fb_blank = intel_fbdev_blank,
161 	.fb_pan_display = intel_fbdev_pan_display,
162 	__FB_DEFAULT_DEFERRED_OPS_DRAW(intel_fbdev),
163 	.fb_mmap = intel_fbdev_mmap,
164 	.fb_destroy = intel_fbdev_fb_destroy,
165 };
166 
167 __diag_pop();
168 
intelfb_dirty(struct drm_fb_helper * helper,struct drm_clip_rect * clip)169 static int intelfb_dirty(struct drm_fb_helper *helper, struct drm_clip_rect *clip)
170 {
171 	if (!(clip->x1 < clip->x2 && clip->y1 < clip->y2))
172 		return 0;
173 
174 	if (helper->fb->funcs->dirty)
175 		return helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, clip, 1);
176 
177 	return 0;
178 }
179 
intelfb_restore(struct drm_fb_helper * fb_helper)180 static void intelfb_restore(struct drm_fb_helper *fb_helper)
181 {
182 	struct intel_fbdev *ifbdev = to_intel_fbdev(fb_helper);
183 
184 	intel_fbdev_invalidate(ifbdev);
185 }
186 
intelfb_set_suspend(struct drm_fb_helper * fb_helper,bool suspend)187 static void intelfb_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
188 {
189 	struct fb_info *info = fb_helper->info;
190 
191 	/*
192 	 * When resuming from hibernation, Linux restores the object's
193 	 * content from swap if the buffer is backed by shmemfs. If the
194 	 * object is stolen however, it will be full of whatever garbage
195 	 * was left in there. Clear it to zero in this case.
196 	 */
197 	if (!suspend && !intel_bo_is_shmem(intel_fb_bo(fb_helper->fb)))
198 		memset_io(info->screen_base, 0, info->screen_size);
199 
200 	fb_set_suspend(info, suspend);
201 }
202 
203 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
204 	.fb_dirty = intelfb_dirty,
205 	.fb_restore = intelfb_restore,
206 	.fb_set_suspend = intelfb_set_suspend,
207 };
208 
intel_fbdev_driver_fbdev_probe(struct drm_fb_helper * helper,struct drm_fb_helper_surface_size * sizes)209 int intel_fbdev_driver_fbdev_probe(struct drm_fb_helper *helper,
210 				   struct drm_fb_helper_surface_size *sizes)
211 {
212 	struct intel_fbdev *ifbdev = to_intel_fbdev(helper);
213 	struct intel_framebuffer *fb = ifbdev->fb;
214 	struct drm_device *dev = helper->dev;
215 	struct drm_i915_private *dev_priv = to_i915(dev);
216 	intel_wakeref_t 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(&dev_priv->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(dev, !intel_fb_bo(&fb->base))) {
238 		drm_dbg_kms(&dev_priv->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(&dev_priv->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_runtime_pm_get(&dev_priv->runtime_pm);
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(&dev_priv->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(dev_priv, info, obj, vma);
281 	if (ret)
282 		goto out_unpin;
283 
284 	drm_fb_helper_fill_info(info, dev->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(&dev_priv->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_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
303 
304 	return 0;
305 
306 out_unpin:
307 	intel_fb_unpin_vma(vma, flags);
308 out_unlock:
309 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
310 	return ret;
311 }
312 
313 /*
314  * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
315  * The core display code will have read out the current plane configuration,
316  * so we use that to figure out if there's an object for us to use as the
317  * fb, and if so, we re-use it for the fbdev configuration.
318  *
319  * Note we only support a single fb shared across pipes for boot (mostly for
320  * fbcon), so we just find the biggest and use that.
321  */
intel_fbdev_init_bios(struct drm_device * dev,struct intel_fbdev * ifbdev)322 static bool intel_fbdev_init_bios(struct drm_device *dev,
323 				  struct intel_fbdev *ifbdev)
324 {
325 	struct drm_i915_private *i915 = to_i915(dev);
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(dev, 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(&i915->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(&i915->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(&i915->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(&i915->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(dev, 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(&i915->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(&i915->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(&i915->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(&i915->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(&i915->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(&i915->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(&i915->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(dev, 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(dev, !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(&i915->drm, "using BIOS fb for initial console\n");
458 	return true;
459 
460 out:
461 
462 	return false;
463 }
464 
intel_fbdev_color_mode(const struct drm_format_info * info)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 
intel_fbdev_setup(struct drm_i915_private * i915)482 void intel_fbdev_setup(struct drm_i915_private *i915)
483 {
484 	struct drm_device *dev = &i915->drm;
485 	struct intel_fbdev *ifbdev;
486 	unsigned int preferred_bpp = 0;
487 
488 	if (!HAS_DISPLAY(i915))
489 		return;
490 
491 	ifbdev = drmm_kzalloc(dev, sizeof(*ifbdev), GFP_KERNEL);
492 	if (!ifbdev)
493 		return;
494 
495 	i915->display.fbdev.fbdev = ifbdev;
496 	if (intel_fbdev_init_bios(dev, ifbdev))
497 		preferred_bpp = intel_fbdev_color_mode(ifbdev->fb->base.format);
498 	if (!preferred_bpp)
499 		preferred_bpp = 32;
500 
501 	drm_client_setup_with_color_mode(dev, preferred_bpp);
502 }
503 
intel_fbdev_framebuffer(struct intel_fbdev * fbdev)504 struct intel_framebuffer *intel_fbdev_framebuffer(struct intel_fbdev *fbdev)
505 {
506 	if (!fbdev)
507 		return NULL;
508 
509 	return fbdev->fb;
510 }
511 
intel_fbdev_vma_pointer(struct intel_fbdev * fbdev)512 struct i915_vma *intel_fbdev_vma_pointer(struct intel_fbdev *fbdev)
513 {
514 	return fbdev ? fbdev->vma : NULL;
515 }
516