2 * Copyright © 2007 David Airlie
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:
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
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.
27 #include <linux/async.h>
28 #include <linux/console.h>
29 #include <linux/delay.h>
30 #include <linux/errno.h>
31 #include <linux/init.h>
32 #include <linux/kernel.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>
40 #include <drm/drm_crtc.h>
41 #include <drm/drm_fb_helper.h>
42 #include <drm/drm_fourcc.h>
45 #include "intel_display_types.h"
46 #include "intel_fbdev.h"
47 #include "intel_frontbuffer.h"
49 static struct intel_frontbuffer
*to_frontbuffer(struct intel_fbdev
*ifbdev
)
51 return ifbdev
->fb
->frontbuffer
;
54 static void intel_fbdev_invalidate(struct intel_fbdev
*ifbdev
)
56 intel_frontbuffer_invalidate(to_frontbuffer(ifbdev
), ORIGIN_CPU
);
59 static int intel_fbdev_set_par(struct fb_info
*info
)
61 struct drm_fb_helper
*fb_helper
= info
->par
;
62 struct intel_fbdev
*ifbdev
=
63 container_of(fb_helper
, struct intel_fbdev
, helper
);
66 ret
= drm_fb_helper_set_par(info
);
68 intel_fbdev_invalidate(ifbdev
);
73 static int intel_fbdev_blank(int blank
, struct fb_info
*info
)
75 struct drm_fb_helper
*fb_helper
= info
->par
;
76 struct intel_fbdev
*ifbdev
=
77 container_of(fb_helper
, struct intel_fbdev
, helper
);
80 ret
= drm_fb_helper_blank(blank
, info
);
82 intel_fbdev_invalidate(ifbdev
);
87 static int intel_fbdev_pan_display(struct fb_var_screeninfo
*var
,
90 struct drm_fb_helper
*fb_helper
= info
->par
;
91 struct intel_fbdev
*ifbdev
=
92 container_of(fb_helper
, struct intel_fbdev
, helper
);
95 ret
= drm_fb_helper_pan_display(var
, info
);
97 intel_fbdev_invalidate(ifbdev
);
102 static const struct fb_ops intelfb_ops
= {
103 .owner
= THIS_MODULE
,
104 DRM_FB_HELPER_DEFAULT_OPS
,
105 .fb_set_par
= intel_fbdev_set_par
,
106 .fb_fillrect
= drm_fb_helper_cfb_fillrect
,
107 .fb_copyarea
= drm_fb_helper_cfb_copyarea
,
108 .fb_imageblit
= drm_fb_helper_cfb_imageblit
,
109 .fb_pan_display
= intel_fbdev_pan_display
,
110 .fb_blank
= intel_fbdev_blank
,
113 static int intelfb_alloc(struct drm_fb_helper
*helper
,
114 struct drm_fb_helper_surface_size
*sizes
)
116 struct intel_fbdev
*ifbdev
=
117 container_of(helper
, struct intel_fbdev
, helper
);
118 struct drm_framebuffer
*fb
;
119 struct drm_device
*dev
= helper
->dev
;
120 struct drm_i915_private
*dev_priv
= to_i915(dev
);
121 struct drm_mode_fb_cmd2 mode_cmd
= {};
122 struct drm_i915_gem_object
*obj
;
125 /* we don't do packed 24bpp */
126 if (sizes
->surface_bpp
== 24)
127 sizes
->surface_bpp
= 32;
129 mode_cmd
.width
= sizes
->surface_width
;
130 mode_cmd
.height
= sizes
->surface_height
;
132 mode_cmd
.pitches
[0] = ALIGN(mode_cmd
.width
*
133 DIV_ROUND_UP(sizes
->surface_bpp
, 8), 64);
134 mode_cmd
.pixel_format
= drm_mode_legacy_fb_format(sizes
->surface_bpp
,
135 sizes
->surface_depth
);
137 size
= mode_cmd
.pitches
[0] * mode_cmd
.height
;
138 size
= PAGE_ALIGN(size
);
140 /* If the FB is too big, just don't use it since fbdev is not very
141 * important and we should probably use that space with FBC or other
143 obj
= ERR_PTR(-ENODEV
);
144 if (size
* 2 < dev_priv
->stolen_usable_size
)
145 obj
= i915_gem_object_create_stolen(dev_priv
, size
);
147 obj
= i915_gem_object_create_shmem(dev_priv
, size
);
149 drm_err(&dev_priv
->drm
, "failed to allocate framebuffer\n");
153 fb
= intel_framebuffer_create(obj
, &mode_cmd
);
154 i915_gem_object_put(obj
);
158 ifbdev
->fb
= to_intel_framebuffer(fb
);
162 static int intelfb_create(struct drm_fb_helper
*helper
,
163 struct drm_fb_helper_surface_size
*sizes
)
165 struct intel_fbdev
*ifbdev
=
166 container_of(helper
, struct intel_fbdev
, helper
);
167 struct intel_framebuffer
*intel_fb
= ifbdev
->fb
;
168 struct drm_device
*dev
= helper
->dev
;
169 struct drm_i915_private
*dev_priv
= to_i915(dev
);
170 struct pci_dev
*pdev
= dev_priv
->drm
.pdev
;
171 struct i915_ggtt
*ggtt
= &dev_priv
->ggtt
;
172 const struct i915_ggtt_view view
= {
173 .type
= I915_GGTT_VIEW_NORMAL
,
175 intel_wakeref_t wakeref
;
176 struct fb_info
*info
;
177 struct i915_vma
*vma
;
178 unsigned long flags
= 0;
179 bool prealloc
= false;
184 (sizes
->fb_width
> intel_fb
->base
.width
||
185 sizes
->fb_height
> intel_fb
->base
.height
)) {
186 drm_dbg_kms(&dev_priv
->drm
,
187 "BIOS fb too small (%dx%d), we require (%dx%d),"
189 intel_fb
->base
.width
, intel_fb
->base
.height
,
190 sizes
->fb_width
, sizes
->fb_height
);
191 drm_framebuffer_put(&intel_fb
->base
);
192 intel_fb
= ifbdev
->fb
= NULL
;
194 if (!intel_fb
|| drm_WARN_ON(dev
, !intel_fb_obj(&intel_fb
->base
))) {
195 drm_dbg_kms(&dev_priv
->drm
,
196 "no BIOS fb, allocating a new one\n");
197 ret
= intelfb_alloc(helper
, sizes
);
200 intel_fb
= ifbdev
->fb
;
202 drm_dbg_kms(&dev_priv
->drm
, "re-using BIOS fb\n");
204 sizes
->fb_width
= intel_fb
->base
.width
;
205 sizes
->fb_height
= intel_fb
->base
.height
;
208 wakeref
= intel_runtime_pm_get(&dev_priv
->runtime_pm
);
210 /* Pin the GGTT vma for our access via info->screen_base.
211 * This also validates that any existing fb inherited from the
212 * BIOS is suitable for own access.
214 vma
= intel_pin_and_fence_fb_obj(&ifbdev
->fb
->base
,
215 &view
, false, &flags
);
221 intel_frontbuffer_flush(to_frontbuffer(ifbdev
), ORIGIN_DIRTYFB
);
223 info
= drm_fb_helper_alloc_fbi(helper
);
225 drm_err(&dev_priv
->drm
, "Failed to allocate fb_info\n");
230 ifbdev
->helper
.fb
= &ifbdev
->fb
->base
;
232 info
->fbops
= &intelfb_ops
;
234 /* setup aperture base/size for vesafb takeover */
235 info
->apertures
->ranges
[0].base
= ggtt
->gmadr
.start
;
236 info
->apertures
->ranges
[0].size
= ggtt
->mappable_end
;
238 /* Our framebuffer is the entirety of fbdev's system memory */
239 info
->fix
.smem_start
=
240 (unsigned long)(ggtt
->gmadr
.start
+ vma
->node
.start
);
241 info
->fix
.smem_len
= vma
->node
.size
;
243 vaddr
= i915_vma_pin_iomap(vma
);
245 drm_err(&dev_priv
->drm
,
246 "Failed to remap framebuffer into virtual memory\n");
247 ret
= PTR_ERR(vaddr
);
250 info
->screen_base
= vaddr
;
251 info
->screen_size
= vma
->node
.size
;
253 drm_fb_helper_fill_info(info
, &ifbdev
->helper
, sizes
);
255 /* If the object is shmemfs backed, it will have given us zeroed pages.
256 * If the object is stolen however, it will be full of whatever
257 * garbage was left in there.
259 if (vma
->obj
->stolen
&& !prealloc
)
260 memset_io(info
->screen_base
, 0, info
->screen_size
);
262 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
264 drm_dbg_kms(&dev_priv
->drm
, "allocated %dx%d fb: 0x%08x\n",
265 ifbdev
->fb
->base
.width
, ifbdev
->fb
->base
.height
,
266 i915_ggtt_offset(vma
));
268 ifbdev
->vma_flags
= flags
;
270 intel_runtime_pm_put(&dev_priv
->runtime_pm
, wakeref
);
271 vga_switcheroo_client_fb_set(pdev
, info
);
275 intel_unpin_fb_vma(vma
, flags
);
277 intel_runtime_pm_put(&dev_priv
->runtime_pm
, wakeref
);
281 static const struct drm_fb_helper_funcs intel_fb_helper_funcs
= {
282 .fb_probe
= intelfb_create
,
285 static void intel_fbdev_destroy(struct intel_fbdev
*ifbdev
)
287 /* We rely on the object-free to release the VMA pinning for
288 * the info->screen_base mmaping. Leaking the VMA is simpler than
289 * trying to rectify all the possible error paths leading here.
292 drm_fb_helper_fini(&ifbdev
->helper
);
295 intel_unpin_fb_vma(ifbdev
->vma
, ifbdev
->vma_flags
);
298 drm_framebuffer_remove(&ifbdev
->fb
->base
);
304 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
305 * The core display code will have read out the current plane configuration,
306 * so we use that to figure out if there's an object for us to use as the
307 * fb, and if so, we re-use it for the fbdev configuration.
309 * Note we only support a single fb shared across pipes for boot (mostly for
310 * fbcon), so we just find the biggest and use that.
312 static bool intel_fbdev_init_bios(struct drm_device
*dev
,
313 struct intel_fbdev
*ifbdev
)
315 struct drm_i915_private
*i915
= to_i915(dev
);
316 struct intel_framebuffer
*fb
= NULL
;
317 struct drm_crtc
*crtc
;
318 struct intel_crtc
*intel_crtc
;
319 unsigned int max_size
= 0;
321 /* Find the largest fb */
322 for_each_crtc(dev
, crtc
) {
323 struct drm_i915_gem_object
*obj
=
324 intel_fb_obj(crtc
->primary
->state
->fb
);
325 intel_crtc
= to_intel_crtc(crtc
);
327 if (!crtc
->state
->active
|| !obj
) {
328 drm_dbg_kms(&i915
->drm
,
329 "pipe %c not active or no fb, skipping\n",
330 pipe_name(intel_crtc
->pipe
));
334 if (obj
->base
.size
> max_size
) {
335 drm_dbg_kms(&i915
->drm
,
336 "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
;
344 drm_dbg_kms(&i915
->drm
,
345 "no active fbs found, not using BIOS config\n");
349 /* Now make sure all the pipes will fit into it */
350 for_each_crtc(dev
, crtc
) {
351 unsigned int cur_size
;
353 intel_crtc
= to_intel_crtc(crtc
);
355 if (!crtc
->state
->active
) {
356 drm_dbg_kms(&i915
->drm
,
357 "pipe %c not active, skipping\n",
358 pipe_name(intel_crtc
->pipe
));
362 drm_dbg_kms(&i915
->drm
, "checking plane %c for BIOS fb\n",
363 pipe_name(intel_crtc
->pipe
));
366 * See if the plane fb we found above will fit on this
367 * pipe. Note we need to use the selected fb's pitch and bpp
368 * rather than the current pipe's, since they differ.
370 cur_size
= crtc
->state
->adjusted_mode
.crtc_hdisplay
;
371 cur_size
= cur_size
* fb
->base
.format
->cpp
[0];
372 if (fb
->base
.pitches
[0] < cur_size
) {
373 drm_dbg_kms(&i915
->drm
,
374 "fb not wide enough for plane %c (%d vs %d)\n",
375 pipe_name(intel_crtc
->pipe
),
376 cur_size
, fb
->base
.pitches
[0]);
381 cur_size
= crtc
->state
->adjusted_mode
.crtc_vdisplay
;
382 cur_size
= intel_fb_align_height(&fb
->base
, 0, cur_size
);
383 cur_size
*= fb
->base
.pitches
[0];
384 drm_dbg_kms(&i915
->drm
,
385 "pipe %c area: %dx%d, bpp: %d, size: %d\n",
386 pipe_name(intel_crtc
->pipe
),
387 crtc
->state
->adjusted_mode
.crtc_hdisplay
,
388 crtc
->state
->adjusted_mode
.crtc_vdisplay
,
389 fb
->base
.format
->cpp
[0] * 8,
392 if (cur_size
> max_size
) {
393 drm_dbg_kms(&i915
->drm
,
394 "fb not big enough for plane %c (%d vs %d)\n",
395 pipe_name(intel_crtc
->pipe
),
401 drm_dbg_kms(&i915
->drm
,
402 "fb big enough for plane %c (%d >= %d)\n",
403 pipe_name(intel_crtc
->pipe
),
408 drm_dbg_kms(&i915
->drm
,
409 "BIOS fb not suitable for all pipes, not using\n");
413 ifbdev
->preferred_bpp
= fb
->base
.format
->cpp
[0] * 8;
416 drm_framebuffer_get(&ifbdev
->fb
->base
);
418 /* Final pass to check if any active pipes don't have fbs */
419 for_each_crtc(dev
, crtc
) {
420 intel_crtc
= to_intel_crtc(crtc
);
422 if (!crtc
->state
->active
)
425 drm_WARN(dev
, !crtc
->primary
->state
->fb
,
426 "re-used BIOS config but lost an fb on crtc %d\n",
431 drm_dbg_kms(&i915
->drm
, "using BIOS fb for initial console\n");
439 static void intel_fbdev_suspend_worker(struct work_struct
*work
)
441 intel_fbdev_set_suspend(&container_of(work
,
442 struct drm_i915_private
,
443 fbdev_suspend_work
)->drm
,
444 FBINFO_STATE_RUNNING
,
448 int intel_fbdev_init(struct drm_device
*dev
)
450 struct drm_i915_private
*dev_priv
= to_i915(dev
);
451 struct intel_fbdev
*ifbdev
;
454 if (drm_WARN_ON(dev
, !HAS_DISPLAY(dev_priv
)))
457 ifbdev
= kzalloc(sizeof(struct intel_fbdev
), GFP_KERNEL
);
461 mutex_init(&ifbdev
->hpd_lock
);
462 drm_fb_helper_prepare(dev
, &ifbdev
->helper
, &intel_fb_helper_funcs
);
464 if (!intel_fbdev_init_bios(dev
, ifbdev
))
465 ifbdev
->preferred_bpp
= 32;
467 ret
= drm_fb_helper_init(dev
, &ifbdev
->helper
);
473 dev_priv
->fbdev
= ifbdev
;
474 INIT_WORK(&dev_priv
->fbdev_suspend_work
, intel_fbdev_suspend_worker
);
479 static void intel_fbdev_initial_config(void *data
, async_cookie_t cookie
)
481 struct intel_fbdev
*ifbdev
= data
;
483 /* Due to peculiar init order wrt to hpd handling this is separate. */
484 if (drm_fb_helper_initial_config(&ifbdev
->helper
,
485 ifbdev
->preferred_bpp
))
486 intel_fbdev_unregister(to_i915(ifbdev
->helper
.dev
));
489 void intel_fbdev_initial_config_async(struct drm_device
*dev
)
491 struct intel_fbdev
*ifbdev
= to_i915(dev
)->fbdev
;
496 ifbdev
->cookie
= async_schedule(intel_fbdev_initial_config
, ifbdev
);
499 static void intel_fbdev_sync(struct intel_fbdev
*ifbdev
)
504 /* Only serialises with all preceding async calls, hence +1 */
505 async_synchronize_cookie(ifbdev
->cookie
+ 1);
509 void intel_fbdev_unregister(struct drm_i915_private
*dev_priv
)
511 struct intel_fbdev
*ifbdev
= dev_priv
->fbdev
;
516 cancel_work_sync(&dev_priv
->fbdev_suspend_work
);
517 if (!current_is_async())
518 intel_fbdev_sync(ifbdev
);
520 drm_fb_helper_unregister_fbi(&ifbdev
->helper
);
523 void intel_fbdev_fini(struct drm_i915_private
*dev_priv
)
525 struct intel_fbdev
*ifbdev
= fetch_and_zero(&dev_priv
->fbdev
);
530 intel_fbdev_destroy(ifbdev
);
533 /* Suspends/resumes fbdev processing of incoming HPD events. When resuming HPD
534 * processing, fbdev will perform a full connector reprobe if a hotplug event
535 * was received while HPD was suspended.
537 static void intel_fbdev_hpd_set_suspend(struct drm_i915_private
*i915
, int state
)
539 struct intel_fbdev
*ifbdev
= i915
->fbdev
;
540 bool send_hpd
= false;
542 mutex_lock(&ifbdev
->hpd_lock
);
543 ifbdev
->hpd_suspended
= state
== FBINFO_STATE_SUSPENDED
;
544 send_hpd
= !ifbdev
->hpd_suspended
&& ifbdev
->hpd_waiting
;
545 ifbdev
->hpd_waiting
= false;
546 mutex_unlock(&ifbdev
->hpd_lock
);
549 drm_dbg_kms(&i915
->drm
, "Handling delayed fbcon HPD event\n");
550 drm_fb_helper_hotplug_event(&ifbdev
->helper
);
554 void intel_fbdev_set_suspend(struct drm_device
*dev
, int state
, bool synchronous
)
556 struct drm_i915_private
*dev_priv
= to_i915(dev
);
557 struct intel_fbdev
*ifbdev
= dev_priv
->fbdev
;
558 struct fb_info
*info
;
560 if (!ifbdev
|| !ifbdev
->vma
)
563 info
= ifbdev
->helper
.fbdev
;
566 /* Flush any pending work to turn the console on, and then
567 * wait to turn it off. It must be synchronous as we are
568 * about to suspend or unload the driver.
570 * Note that from within the work-handler, we cannot flush
571 * ourselves, so only flush outstanding work upon suspend!
573 if (state
!= FBINFO_STATE_RUNNING
)
574 flush_work(&dev_priv
->fbdev_suspend_work
);
579 * The console lock can be pretty contented on resume due
580 * to all the printk activity. Try to keep it out of the hot
581 * path of resume if possible.
583 drm_WARN_ON(dev
, state
!= FBINFO_STATE_RUNNING
);
584 if (!console_trylock()) {
585 /* Don't block our own workqueue as this can
586 * be run in parallel with other i915.ko tasks.
588 schedule_work(&dev_priv
->fbdev_suspend_work
);
593 /* On resume from hibernation: If the object is shmemfs backed, it has
594 * been restored from swap. If the object is stolen however, it will be
595 * full of whatever garbage was left in there.
597 if (state
== FBINFO_STATE_RUNNING
&&
598 intel_fb_obj(&ifbdev
->fb
->base
)->stolen
)
599 memset_io(info
->screen_base
, 0, info
->screen_size
);
601 drm_fb_helper_set_suspend(&ifbdev
->helper
, state
);
604 intel_fbdev_hpd_set_suspend(dev_priv
, state
);
607 void intel_fbdev_output_poll_changed(struct drm_device
*dev
)
609 struct intel_fbdev
*ifbdev
= to_i915(dev
)->fbdev
;
615 intel_fbdev_sync(ifbdev
);
617 mutex_lock(&ifbdev
->hpd_lock
);
618 send_hpd
= !ifbdev
->hpd_suspended
;
619 ifbdev
->hpd_waiting
= true;
620 mutex_unlock(&ifbdev
->hpd_lock
);
622 if (send_hpd
&& (ifbdev
->vma
|| ifbdev
->helper
.deferred_setup
))
623 drm_fb_helper_hotplug_event(&ifbdev
->helper
);
626 void intel_fbdev_restore_mode(struct drm_device
*dev
)
628 struct intel_fbdev
*ifbdev
= to_i915(dev
)->fbdev
;
633 intel_fbdev_sync(ifbdev
);
637 if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev
->helper
) == 0)
638 intel_fbdev_invalidate(ifbdev
);