projects/linux·blob master · refs

587 lines · 16.0 KiB

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/intel/display_parent_interface.h>
42#include <drm/drm_crtc.h>
43#include <drm/drm_crtc_helper.h>
44#include <drm/drm_fb_helper.h>
45#include <drm/drm_fourcc.h>
46#include <drm/drm_gem.h>
47#include <drm/drm_gem_framebuffer_helper.h>
48#include <drm/drm_managed.h>
49#include <drm/drm_print.h>
50
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_fbdev.h"
57#include "intel_frontbuffer.h"
58#include "intel_parent.h"
59#include "intel_plane.h"
60
61struct intel_fbdev {
62 struct intel_framebuffer *fb;
63 struct i915_vma *vma;
64};
65
66static struct intel_fbdev *to_intel_fbdev(struct drm_fb_helper *fb_helper)
67{
68 struct intel_display *display = to_intel_display(fb_helper->client.dev);
69
70 return display->fbdev.fbdev;
71}
72
73static struct intel_frontbuffer *to_frontbuffer(struct intel_fbdev *ifbdev)
74{
75 return ifbdev->fb->frontbuffer;
76}
77
78static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev)
79{
80 intel_frontbuffer_invalidate(to_frontbuffer(ifbdev), ORIGIN_CPU);
81}
82
83FB_GEN_DEFAULT_DEFERRED_IOMEM_OPS(intel_fbdev,
84 drm_fb_helper_damage_range,
85 drm_fb_helper_damage_area)
86
87static 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
99static 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
111static 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
124static 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
132static void intel_fbdev_fb_destroy(struct fb_info *info)
133{
134 struct drm_fb_helper *fb_helper = info->par;
135 struct intel_display *display = to_intel_display(fb_helper->client.dev);
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_parent_fb_pin_ggtt_unpin(display, ifbdev->vma, -1);
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
154static 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
168static 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
179static 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
186static 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
202static 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
208static void intel_fbdev_fill_mode_cmd(struct intel_display *display,
209 struct drm_fb_helper_surface_size *sizes,
210 struct drm_mode_fb_cmd2 *mode_cmd)
211{
212 /* we don't do packed 24bpp */
213 if (sizes->surface_bpp == 24)
214 sizes->surface_bpp = 32;
215
216 mode_cmd->flags = DRM_MODE_FB_MODIFIERS;
217 mode_cmd->width = sizes->surface_width;
218 mode_cmd->height = sizes->surface_height;
219
220 mode_cmd->pitches[0] = intel_bo_fbdev_pitch_align(display, mode_cmd->width * DIV_ROUND_UP(sizes->surface_bpp, 8));
221 mode_cmd->pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
222 sizes->surface_depth);
223 mode_cmd->modifier[0] = DRM_FORMAT_MOD_LINEAR;
224}
225
226static struct intel_framebuffer *
227__intel_fbdev_fb_alloc(struct intel_display *display,
228 struct drm_fb_helper_surface_size *sizes)
229{
230 struct drm_mode_fb_cmd2 mode_cmd = {};
231 struct drm_framebuffer *fb;
232 struct drm_gem_object *obj;
233 int size;
234
235 intel_fbdev_fill_mode_cmd(display, sizes, &mode_cmd);
236
237 size = mode_cmd.pitches[0] * mode_cmd.height;
238 size = PAGE_ALIGN(size);
239
240 obj = intel_bo_fbdev_create(display, size);
241 if (IS_ERR(obj)) {
242 fb = ERR_CAST(obj);
243 goto err;
244 }
245
246 fb = intel_framebuffer_create(obj,
247 drm_get_format_info(display->drm,
248 mode_cmd.pixel_format,
249 mode_cmd.modifier[0]),
250 &mode_cmd);
251 if (IS_ERR(fb)) {
252 intel_bo_fbdev_destroy(obj);
253 goto err;
254 }
255
256 drm_gem_object_put(obj);
257
258 return to_intel_framebuffer(fb);
259
260err:
261 return ERR_CAST(fb);
262
263}
264
265static bool bios_fb_ok(const struct intel_framebuffer *fb,
266 const struct drm_fb_helper_surface_size *sizes)
267{
268 struct intel_display *display = to_intel_display(fb->base.dev);
269 int width = fb->base.width;
270 int height = fb->base.height;
271 int depth = fb->base.format->depth;
272 int bpp = fb->base.format->cpp[0] * 8;
273
274 if (sizes->fb_width > width || sizes->fb_height > height) {
275 drm_dbg_kms(display->drm,
276 "BIOS fb too small (%dx%d), we require (%dx%d), releasing it\n",
277 width, height, sizes->fb_width, sizes->fb_height);
278 return false;
279 }
280
281 if (sizes->surface_depth != depth || sizes->surface_bpp != bpp) {
282 drm_dbg_kms(display->drm,
283 "BIOS fb using wrong depth/bpp (%d/%d), we require (%d/%d), releasing it\n",
284 depth, bpp, sizes->surface_depth, sizes->surface_bpp);
285 return false;
286 }
287
288 return true;
289}
290
291int intel_fbdev_driver_fbdev_probe(struct drm_fb_helper *helper,
292 struct drm_fb_helper_surface_size *sizes)
293{
294 struct intel_display *display = to_intel_display(helper->dev);
295 struct intel_fbdev *ifbdev = to_intel_fbdev(helper);
296 struct intel_fb_pin_params pin_params = {};
297 struct intel_framebuffer *fb = ifbdev->fb;
298 struct fb_info *info = helper->info;
299 struct ref_tracker *wakeref;
300 struct i915_vma *vma;
301 bool prealloc = false;
302 struct drm_gem_object *obj;
303 u32 offset;
304 int ret;
305
306 ifbdev->fb = NULL;
307
308 if (fb && !bios_fb_ok(fb, sizes)) {
309 drm_framebuffer_put(&fb->base);
310 fb = NULL;
311 }
312
313 wakeref = intel_display_rpm_get(display);
314
315 if (!fb || drm_WARN_ON(display->drm, !intel_fb_bo(&fb->base))) {
316 drm_dbg_kms(display->drm,
317 "no BIOS fb, allocating a new one\n");
318
319 fb = __intel_fbdev_fb_alloc(display, sizes);
320 if (IS_ERR(fb)) {
321 ret = PTR_ERR(fb);
322 goto out_unlock;
323 }
324 } else {
325 drm_dbg_kms(display->drm, "re-using BIOS fb\n");
326 prealloc = true;
327 sizes->fb_width = fb->base.width;
328 sizes->fb_height = fb->base.height;
329 }
330
331 obj = intel_fb_bo(&fb->base);
332
333 /* Pin the GGTT vma for our access via info->screen_base.
334 * This also validates that any existing fb inherited from the
335 * BIOS is suitable for own access.
336 */
337 pin_params.view = &fb->normal_view.gtt;
338 pin_params.alignment = fb->min_alignment;
339 pin_params.vtd_guard = intel_fb_view_vtd_guard(&fb->base,
340 &fb->normal_view,
341 DRM_MODE_ROTATE_0);
342 pin_params.needs_low_address = intel_plane_needs_low_address(display);
343
344 ret = intel_parent_fb_pin_ggtt_pin(display, obj, &pin_params,
345 &vma, &offset, NULL);
346 if (ret)
347 goto out_unlock;
348
349 helper->funcs = &intel_fb_helper_funcs;
350 helper->fb = &fb->base;
351
352 info->fbops = &intelfb_ops;
353
354 ret = intel_bo_fbdev_fill_info(obj, info, vma);
355 if (ret)
356 goto out_unpin;
357
358 drm_fb_helper_fill_info(info, display->drm->fb_helper, sizes);
359
360 /* If the object is shmemfs backed, it will have given us zeroed pages.
361 * If the object is stolen however, it will be full of whatever
362 * garbage was left in there.
363 */
364 if (!intel_bo_is_shmem(obj) && !prealloc)
365 memset_io(info->screen_base, 0, info->screen_size);
366
367 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
368
369 drm_dbg_kms(display->drm, "allocated %dx%d fb\n", fb->base.width, fb->base.height);
370 ifbdev->fb = fb;
371 ifbdev->vma = vma;
372
373 intel_display_rpm_put(display, wakeref);
374
375 return 0;
376
377out_unpin:
378 intel_parent_fb_pin_ggtt_unpin(display, vma, -1);
379out_unlock:
380 intel_display_rpm_put(display, wakeref);
381
382 return ret;
383}
384
385/*
386 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
387 * The core display code will have read out the current plane configuration,
388 * so we use that to figure out if there's an object for us to use as the
389 * fb, and if so, we re-use it for the fbdev configuration.
390 *
391 * Note we only support a single fb shared across pipes for boot (mostly for
392 * fbcon), so we just find the biggest and use that.
393 */
394static bool intel_fbdev_init_bios(struct intel_display *display,
395 struct intel_fbdev *ifbdev)
396{
397 struct intel_framebuffer *fb = NULL;
398 struct intel_crtc *crtc;
399 unsigned int max_size = 0;
400
401 /* Find the largest fb */
402 for_each_intel_crtc(display, crtc) {
403 struct intel_crtc_state *crtc_state =
404 to_intel_crtc_state(crtc->base.state);
405 struct intel_plane *plane =
406 to_intel_plane(crtc->base.primary);
407 struct intel_plane_state *plane_state =
408 to_intel_plane_state(plane->base.state);
409 struct drm_gem_object *obj = intel_fb_bo(plane_state->uapi.fb);
410
411 if (!crtc_state->uapi.active) {
412 drm_dbg_kms(display->drm,
413 "[CRTC:%d:%s] not active, skipping\n",
414 crtc->base.base.id, crtc->base.name);
415 continue;
416 }
417
418 if (!obj) {
419 drm_dbg_kms(display->drm,
420 "[PLANE:%d:%s] no fb, skipping\n",
421 plane->base.base.id, plane->base.name);
422 continue;
423 }
424
425 if (obj->size > max_size) {
426 drm_dbg_kms(display->drm,
427 "found possible fb from [PLANE:%d:%s]\n",
428 plane->base.base.id, plane->base.name);
429 fb = to_intel_framebuffer(plane_state->uapi.fb);
430 max_size = obj->size;
431 }
432 }
433
434 if (!fb) {
435 drm_dbg_kms(display->drm,
436 "no active fbs found, not using BIOS config\n");
437 goto out;
438 }
439
440 /* Now make sure all the pipes will fit into it */
441 for_each_intel_crtc(display, crtc) {
442 struct intel_crtc_state *crtc_state =
443 to_intel_crtc_state(crtc->base.state);
444 struct intel_plane *plane =
445 to_intel_plane(crtc->base.primary);
446 unsigned int cur_size;
447
448 if (!crtc_state->uapi.active) {
449 drm_dbg_kms(display->drm,
450 "[CRTC:%d:%s] not active, skipping\n",
451 crtc->base.base.id, crtc->base.name);
452 continue;
453 }
454
455 drm_dbg_kms(display->drm, "checking [PLANE:%d:%s] for BIOS fb\n",
456 plane->base.base.id, plane->base.name);
457
458 /*
459 * See if the plane fb we found above will fit on this
460 * pipe. Note we need to use the selected fb's pitch and bpp
461 * rather than the current pipe's, since they differ.
462 */
463 cur_size = crtc_state->uapi.adjusted_mode.crtc_hdisplay;
464 cur_size = cur_size * fb->base.format->cpp[0];
465 if (fb->base.pitches[0] < cur_size) {
466 drm_dbg_kms(display->drm,
467 "fb not wide enough for [PLANE:%d:%s] (%d vs %d)\n",
468 plane->base.base.id, plane->base.name,
469 cur_size, fb->base.pitches[0]);
470 fb = NULL;
471 break;
472 }
473
474 cur_size = crtc_state->uapi.adjusted_mode.crtc_vdisplay;
475 cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
476 cur_size *= fb->base.pitches[0];
477 drm_dbg_kms(display->drm,
478 "[CRTC:%d:%s] area: %dx%d, bpp: %d, size: %d\n",
479 crtc->base.base.id, crtc->base.name,
480 crtc_state->uapi.adjusted_mode.crtc_hdisplay,
481 crtc_state->uapi.adjusted_mode.crtc_vdisplay,
482 fb->base.format->cpp[0] * 8,
483 cur_size);
484
485 if (cur_size > max_size) {
486 drm_dbg_kms(display->drm,
487 "fb not big enough for [PLANE:%d:%s] (%d vs %d)\n",
488 plane->base.base.id, plane->base.name,
489 cur_size, max_size);
490 fb = NULL;
491 break;
492 }
493
494 drm_dbg_kms(display->drm,
495 "fb big enough [PLANE:%d:%s] (%d >= %d)\n",
496 plane->base.base.id, plane->base.name,
497 max_size, cur_size);
498 }
499
500 if (!fb) {
501 drm_dbg_kms(display->drm,
502 "BIOS fb not suitable for all pipes, not using\n");
503 goto out;
504 }
505
506 ifbdev->fb = fb;
507
508 drm_framebuffer_get(&ifbdev->fb->base);
509
510 /* Final pass to check if any active pipes don't have fbs */
511 for_each_intel_crtc(display, crtc) {
512 struct intel_crtc_state *crtc_state =
513 to_intel_crtc_state(crtc->base.state);
514 struct intel_plane *plane =
515 to_intel_plane(crtc->base.primary);
516 struct intel_plane_state *plane_state =
517 to_intel_plane_state(plane->base.state);
518
519 if (!crtc_state->uapi.active)
520 continue;
521
522 drm_WARN(display->drm, !plane_state->uapi.fb,
523 "re-used BIOS config but lost an fb on [PLANE:%d:%s]\n",
524 plane->base.base.id, plane->base.name);
525 }
526
527
528 drm_dbg_kms(display->drm, "using BIOS fb for initial console\n");
529 return true;
530
531out:
532
533 return false;
534}
535
536static unsigned int intel_fbdev_color_mode(const struct drm_format_info *info)
537{
538 unsigned int bpp;
539
540 if (!info->depth || info->num_planes != 1 || info->has_alpha || info->is_yuv)
541 return 0;
542
543 bpp = drm_format_info_bpp(info, 0);
544
545 switch (bpp) {
546 case 16:
547 return info->depth; // 15 or 16
548 default:
549 return bpp;
550 }
551}
552
553void intel_fbdev_setup(struct intel_display *display)
554{
555 struct intel_fbdev *ifbdev;
556 unsigned int preferred_bpp = 0;
557
558 if (!HAS_DISPLAY(display))
559 return;
560
561 ifbdev = drmm_kzalloc(display->drm, sizeof(*ifbdev), GFP_KERNEL);
562 if (!ifbdev)
563 return;
564
565 display->fbdev.fbdev = ifbdev;
566 if (intel_fbdev_init_bios(display, ifbdev))
567 preferred_bpp = intel_fbdev_color_mode(ifbdev->fb->base.format);
568 if (!preferred_bpp)
569 preferred_bpp = 32;
570
571 drm_client_setup_with_color_mode(display->drm, preferred_bpp);
572}
573
574struct intel_framebuffer *intel_fbdev_framebuffer(struct intel_fbdev *fbdev)
575{
576 if (!fbdev)
577 return NULL;
578
579 return fbdev->fb;
580}
581
582void intel_fbdev_get_map(struct intel_display *display, struct iosys_map *map)
583{
584 struct intel_fbdev *fbdev = display->fbdev.fbdev;
585
586 intel_parent_fb_pin_get_map(display, fbdev->vma, map);
587}