xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/i915/display/intel_fbdev.c (revision 0f6253180151af85efabd9c64633c2365e1e18b9)
1*0f625318Schs /*	$NetBSD: intel_fbdev.c,v 1.10 2021/12/20 20:34:58 chs Exp $	*/
24e390cabSriastradh 
34e390cabSriastradh /*
44e390cabSriastradh  * Copyright © 2007 David Airlie
54e390cabSriastradh  *
64e390cabSriastradh  * Permission is hereby granted, free of charge, to any person obtaining a
74e390cabSriastradh  * copy of this software and associated documentation files (the "Software"),
84e390cabSriastradh  * to deal in the Software without restriction, including without limitation
94e390cabSriastradh  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
104e390cabSriastradh  * and/or sell copies of the Software, and to permit persons to whom the
114e390cabSriastradh  * Software is furnished to do so, subject to the following conditions:
124e390cabSriastradh  *
134e390cabSriastradh  * The above copyright notice and this permission notice (including the next
144e390cabSriastradh  * paragraph) shall be included in all copies or substantial portions of the
154e390cabSriastradh  * Software.
164e390cabSriastradh  *
174e390cabSriastradh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
184e390cabSriastradh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
194e390cabSriastradh  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
204e390cabSriastradh  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
214e390cabSriastradh  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
224e390cabSriastradh  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
234e390cabSriastradh  * DEALINGS IN THE SOFTWARE.
244e390cabSriastradh  *
254e390cabSriastradh  * Authors:
264e390cabSriastradh  *     David Airlie
274e390cabSriastradh  */
284e390cabSriastradh 
294e390cabSriastradh #include <sys/cdefs.h>
30*0f625318Schs __KERNEL_RCSID(0, "$NetBSD: intel_fbdev.c,v 1.10 2021/12/20 20:34:58 chs Exp $");
314e390cabSriastradh 
324e390cabSriastradh #include <linux/async.h>
334e390cabSriastradh #include <linux/console.h>
344e390cabSriastradh #include <linux/delay.h>
354e390cabSriastradh #include <linux/errno.h>
364e390cabSriastradh #include <linux/init.h>
374e390cabSriastradh #include <linux/kernel.h>
384e390cabSriastradh #include <linux/mm.h>
394e390cabSriastradh #include <linux/module.h>
404e390cabSriastradh #include <linux/string.h>
414e390cabSriastradh #include <linux/sysrq.h>
424e390cabSriastradh #include <linux/tty.h>
434e390cabSriastradh #include <linux/vga_switcheroo.h>
444e390cabSriastradh 
454e390cabSriastradh #include <drm/drm_crtc.h>
464e390cabSriastradh #include <drm/drm_fb_helper.h>
474e390cabSriastradh #include <drm/drm_fourcc.h>
484e390cabSriastradh #include <drm/i915_drm.h>
494e390cabSriastradh 
504e390cabSriastradh #include "i915_drv.h"
514e390cabSriastradh #include "intel_display_types.h"
524e390cabSriastradh #include "intel_fbdev.h"
534e390cabSriastradh #include "intel_frontbuffer.h"
544e390cabSriastradh 
to_frontbuffer(struct intel_fbdev * ifbdev)554e390cabSriastradh static struct intel_frontbuffer *to_frontbuffer(struct intel_fbdev *ifbdev)
564e390cabSriastradh {
574e390cabSriastradh 	return ifbdev->fb->frontbuffer;
584e390cabSriastradh }
594e390cabSriastradh 
intel_fbdev_invalidate(struct intel_fbdev * ifbdev)604e390cabSriastradh static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev)
614e390cabSriastradh {
624e390cabSriastradh 	intel_frontbuffer_invalidate(to_frontbuffer(ifbdev), ORIGIN_CPU);
634e390cabSriastradh }
644e390cabSriastradh 
6541ec0267Sriastradh #ifdef __NetBSD__
6641ec0267Sriastradh #include "intelfb.h"
6741ec0267Sriastradh #include <linux/nbsd-namespace.h>
6841ec0267Sriastradh #endif
6941ec0267Sriastradh 
7041ec0267Sriastradh #ifndef __NetBSD__
intel_fbdev_set_par(struct fb_info * info)714e390cabSriastradh static int intel_fbdev_set_par(struct fb_info *info)
724e390cabSriastradh {
734e390cabSriastradh 	struct drm_fb_helper *fb_helper = info->par;
744e390cabSriastradh 	struct intel_fbdev *ifbdev =
754e390cabSriastradh 		container_of(fb_helper, struct intel_fbdev, helper);
764e390cabSriastradh 	int ret;
774e390cabSriastradh 
784e390cabSriastradh 	ret = drm_fb_helper_set_par(info);
794e390cabSriastradh 	if (ret == 0)
804e390cabSriastradh 		intel_fbdev_invalidate(ifbdev);
814e390cabSriastradh 
824e390cabSriastradh 	return ret;
834e390cabSriastradh }
844e390cabSriastradh 
intel_fbdev_blank(int blank,struct fb_info * info)854e390cabSriastradh static int intel_fbdev_blank(int blank, struct fb_info *info)
864e390cabSriastradh {
874e390cabSriastradh 	struct drm_fb_helper *fb_helper = info->par;
884e390cabSriastradh 	struct intel_fbdev *ifbdev =
894e390cabSriastradh 		container_of(fb_helper, struct intel_fbdev, helper);
904e390cabSriastradh 	int ret;
914e390cabSriastradh 
924e390cabSriastradh 	ret = drm_fb_helper_blank(blank, info);
934e390cabSriastradh 	if (ret == 0)
944e390cabSriastradh 		intel_fbdev_invalidate(ifbdev);
954e390cabSriastradh 
964e390cabSriastradh 	return ret;
974e390cabSriastradh }
984e390cabSriastradh 
intel_fbdev_pan_display(struct fb_var_screeninfo * var,struct fb_info * info)994e390cabSriastradh static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
1004e390cabSriastradh 				   struct fb_info *info)
1014e390cabSriastradh {
1024e390cabSriastradh 	struct drm_fb_helper *fb_helper = info->par;
1034e390cabSriastradh 	struct intel_fbdev *ifbdev =
1044e390cabSriastradh 		container_of(fb_helper, struct intel_fbdev, helper);
1054e390cabSriastradh 	int ret;
1064e390cabSriastradh 
1074e390cabSriastradh 	ret = drm_fb_helper_pan_display(var, info);
1084e390cabSriastradh 	if (ret == 0)
1094e390cabSriastradh 		intel_fbdev_invalidate(ifbdev);
1104e390cabSriastradh 
1114e390cabSriastradh 	return ret;
1124e390cabSriastradh }
1134e390cabSriastradh 
1144e390cabSriastradh static const struct fb_ops intelfb_ops = {
1154e390cabSriastradh 	.owner = THIS_MODULE,
1164e390cabSriastradh 	DRM_FB_HELPER_DEFAULT_OPS,
1174e390cabSriastradh 	.fb_set_par = intel_fbdev_set_par,
1184e390cabSriastradh 	.fb_fillrect = drm_fb_helper_cfb_fillrect,
1194e390cabSriastradh 	.fb_copyarea = drm_fb_helper_cfb_copyarea,
1204e390cabSriastradh 	.fb_imageblit = drm_fb_helper_cfb_imageblit,
1214e390cabSriastradh 	.fb_pan_display = intel_fbdev_pan_display,
1224e390cabSriastradh 	.fb_blank = intel_fbdev_blank,
1234e390cabSriastradh };
12441ec0267Sriastradh #endif
1254e390cabSriastradh 
intelfb_alloc(struct drm_fb_helper * helper,struct drm_fb_helper_surface_size * sizes)1264e390cabSriastradh static int intelfb_alloc(struct drm_fb_helper *helper,
1274e390cabSriastradh 			 struct drm_fb_helper_surface_size *sizes)
1284e390cabSriastradh {
1294e390cabSriastradh 	struct intel_fbdev *ifbdev =
1304e390cabSriastradh 		container_of(helper, struct intel_fbdev, helper);
1314e390cabSriastradh 	struct drm_framebuffer *fb;
1324e390cabSriastradh 	struct drm_device *dev = helper->dev;
1334e390cabSriastradh 	struct drm_i915_private *dev_priv = to_i915(dev);
1344e390cabSriastradh 	struct drm_mode_fb_cmd2 mode_cmd = {};
1354e390cabSriastradh 	struct drm_i915_gem_object *obj;
1364e390cabSriastradh 	int size;
1374e390cabSriastradh 
1384e390cabSriastradh 	/* we don't do packed 24bpp */
1394e390cabSriastradh 	if (sizes->surface_bpp == 24)
1404e390cabSriastradh 		sizes->surface_bpp = 32;
1414e390cabSriastradh 
1424e390cabSriastradh 	mode_cmd.width = sizes->surface_width;
1434e390cabSriastradh 	mode_cmd.height = sizes->surface_height;
1444e390cabSriastradh 
1454e390cabSriastradh 	mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
1464e390cabSriastradh 				    DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
1474e390cabSriastradh 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
1484e390cabSriastradh 							  sizes->surface_depth);
1494e390cabSriastradh 
1504e390cabSriastradh 	size = mode_cmd.pitches[0] * mode_cmd.height;
1514e390cabSriastradh 	size = PAGE_ALIGN(size);
1524e390cabSriastradh 
1534e390cabSriastradh 	/* If the FB is too big, just don't use it since fbdev is not very
1544e390cabSriastradh 	 * important and we should probably use that space with FBC or other
1554e390cabSriastradh 	 * features. */
1564e390cabSriastradh 	obj = ERR_PTR(-ENODEV);
1574e390cabSriastradh 	if (size * 2 < dev_priv->stolen_usable_size)
1584e390cabSriastradh 		obj = i915_gem_object_create_stolen(dev_priv, size);
1594e390cabSriastradh 	if (IS_ERR(obj))
1604e390cabSriastradh 		obj = i915_gem_object_create_shmem(dev_priv, size);
1614e390cabSriastradh 	if (IS_ERR(obj)) {
1624e390cabSriastradh 		DRM_ERROR("failed to allocate framebuffer\n");
1634e390cabSriastradh 		return PTR_ERR(obj);
1644e390cabSriastradh 	}
1654e390cabSriastradh 
1664e390cabSriastradh 	fb = intel_framebuffer_create(obj, &mode_cmd);
1674e390cabSriastradh 	i915_gem_object_put(obj);
1684e390cabSriastradh 	if (IS_ERR(fb))
1694e390cabSriastradh 		return PTR_ERR(fb);
1704e390cabSriastradh 
1714e390cabSriastradh 	ifbdev->fb = to_intel_framebuffer(fb);
1724e390cabSriastradh 	return 0;
1734e390cabSriastradh }
1744e390cabSriastradh 
17589f96c87Sriastradh #ifdef __NetBSD__
17689f96c87Sriastradh #  define	__iomem		__i915_vma_iomem
17789f96c87Sriastradh #endif
17889f96c87Sriastradh 
intelfb_create(struct drm_fb_helper * helper,struct drm_fb_helper_surface_size * sizes)1794e390cabSriastradh static int intelfb_create(struct drm_fb_helper *helper,
1804e390cabSriastradh 			  struct drm_fb_helper_surface_size *sizes)
1814e390cabSriastradh {
1824e390cabSriastradh 	struct intel_fbdev *ifbdev =
1834e390cabSriastradh 		container_of(helper, struct intel_fbdev, helper);
1844e390cabSriastradh 	struct intel_framebuffer *intel_fb = ifbdev->fb;
1854e390cabSriastradh 	struct drm_device *dev = helper->dev;
1864e390cabSriastradh 	struct drm_i915_private *dev_priv = to_i915(dev);
1874e390cabSriastradh 	struct pci_dev *pdev = dev_priv->drm.pdev;
1884e390cabSriastradh 	struct i915_ggtt *ggtt = &dev_priv->ggtt;
1894e390cabSriastradh 	const struct i915_ggtt_view view = {
1904e390cabSriastradh 		.type = I915_GGTT_VIEW_NORMAL,
1914e390cabSriastradh 	};
1924e390cabSriastradh 	intel_wakeref_t wakeref;
19341ec0267Sriastradh #ifndef __NetBSD__
1944e390cabSriastradh 	struct fb_info *info;
19541ec0267Sriastradh #endif
1964e390cabSriastradh 	struct i915_vma *vma;
1974e390cabSriastradh 	unsigned long flags = 0;
1984e390cabSriastradh 	bool prealloc = false;
1994e390cabSriastradh 	void __iomem *vaddr;
2004e390cabSriastradh 	int ret;
2014e390cabSriastradh 
2024e390cabSriastradh 	if (intel_fb &&
2034e390cabSriastradh 	    (sizes->fb_width > intel_fb->base.width ||
2044e390cabSriastradh 	     sizes->fb_height > intel_fb->base.height)) {
2054e390cabSriastradh 		DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d),"
2064e390cabSriastradh 			      " releasing it\n",
2074e390cabSriastradh 			      intel_fb->base.width, intel_fb->base.height,
2084e390cabSriastradh 			      sizes->fb_width, sizes->fb_height);
2094e390cabSriastradh 		drm_framebuffer_put(&intel_fb->base);
2104e390cabSriastradh 		intel_fb = ifbdev->fb = NULL;
2114e390cabSriastradh 	}
2124e390cabSriastradh 	if (!intel_fb || WARN_ON(!intel_fb_obj(&intel_fb->base))) {
2134e390cabSriastradh 		DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
2144e390cabSriastradh 		ret = intelfb_alloc(helper, sizes);
2154e390cabSriastradh 		if (ret)
2164e390cabSriastradh 			return ret;
2174e390cabSriastradh 		intel_fb = ifbdev->fb;
2184e390cabSriastradh 	} else {
2194e390cabSriastradh 		DRM_DEBUG_KMS("re-using BIOS fb\n");
2204e390cabSriastradh 		prealloc = true;
2214e390cabSriastradh 		sizes->fb_width = intel_fb->base.width;
2224e390cabSriastradh 		sizes->fb_height = intel_fb->base.height;
2234e390cabSriastradh 	}
2244e390cabSriastradh 
2254e390cabSriastradh 	wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
2264e390cabSriastradh 
2274e390cabSriastradh 	/* Pin the GGTT vma for our access via info->screen_base.
2284e390cabSriastradh 	 * This also validates that any existing fb inherited from the
2294e390cabSriastradh 	 * BIOS is suitable for own access.
2304e390cabSriastradh 	 */
2314e390cabSriastradh 	vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base,
2324e390cabSriastradh 					 &view, false, &flags);
2334e390cabSriastradh 	if (IS_ERR(vma)) {
2344e390cabSriastradh 		ret = PTR_ERR(vma);
2354e390cabSriastradh 		goto out_unlock;
2364e390cabSriastradh 	}
2374e390cabSriastradh 
2384e390cabSriastradh 	intel_frontbuffer_flush(to_frontbuffer(ifbdev), ORIGIN_DIRTYFB);
2394e390cabSriastradh 
24041ec0267Sriastradh #ifdef __NetBSD__
24141ec0267Sriastradh     {
24241ec0267Sriastradh 	static const struct intelfb_attach_args zero_ifa;
24341ec0267Sriastradh 	struct intelfb_attach_args ifa = zero_ifa;
24441ec0267Sriastradh 
24589f96c87Sriastradh 	__USE(ggtt);
24689f96c87Sriastradh 	__USE(pdev);
24789f96c87Sriastradh 
24889f96c87Sriastradh 	vaddr = i915_vma_pin_iomap(vma);
24989f96c87Sriastradh 	if (IS_ERR(vaddr)) {
25089f96c87Sriastradh 		DRM_ERROR("Failed to remap framebuffer into virtual memory\n");
25189f96c87Sriastradh 		ret = PTR_ERR(vaddr);
25289f96c87Sriastradh 		goto out_unpin;
25389f96c87Sriastradh 	}
25489f96c87Sriastradh 
25589f96c87Sriastradh         if (vma->obj->stolen && !prealloc)
25689f96c87Sriastradh 		memset_io(vaddr, 0, vma->node.size);
25789f96c87Sriastradh 
25841ec0267Sriastradh 	ifa.ifa_drm_dev = dev;
25941ec0267Sriastradh 	ifa.ifa_fb_helper = helper;
26041ec0267Sriastradh 	ifa.ifa_fb_sizes = *sizes;
26189f96c87Sriastradh 	ifa.ifa_fb_vaddr = vaddr;
26241ec0267Sriastradh 
26341ec0267Sriastradh 	/*
26441ec0267Sriastradh 	 * XXX Should do this asynchronously, since we hold
26541ec0267Sriastradh 	 * dev->struct_mutex.
26641ec0267Sriastradh 	 */
267*0f625318Schs 	KERNEL_LOCK(1, NULL);
26841ec0267Sriastradh 	helper->fbdev = config_found(dev->dev, &ifa, NULL,
26941ec0267Sriastradh 	    CFARGS(.iattr = "intelfbbus"));
270*0f625318Schs 	KERNEL_UNLOCK_ONE(NULL);
27141ec0267Sriastradh 	if (helper->fbdev == NULL) {
27241ec0267Sriastradh 		DRM_ERROR("unable to attach intelfb\n");
27341ec0267Sriastradh 		ret = -ENXIO;
27441ec0267Sriastradh 		goto out_unpin;
27541ec0267Sriastradh 	}
27689f96c87Sriastradh 	ifbdev->helper.fb = &ifbdev->fb->base;
27741ec0267Sriastradh     }
27841ec0267Sriastradh #else
2794e390cabSriastradh 	info = drm_fb_helper_alloc_fbi(helper);
2804e390cabSriastradh 	if (IS_ERR(info)) {
2814e390cabSriastradh 		DRM_ERROR("Failed to allocate fb_info\n");
2824e390cabSriastradh 		ret = PTR_ERR(info);
2834e390cabSriastradh 		goto out_unpin;
2844e390cabSriastradh 	}
2854e390cabSriastradh 
2864e390cabSriastradh 	ifbdev->helper.fb = &ifbdev->fb->base;
2874e390cabSriastradh 
2884e390cabSriastradh 	info->fbops = &intelfb_ops;
2894e390cabSriastradh 
2904e390cabSriastradh 	/* setup aperture base/size for vesafb takeover */
2914e390cabSriastradh 	info->apertures->ranges[0].base = ggtt->gmadr.start;
2924e390cabSriastradh 	info->apertures->ranges[0].size = ggtt->mappable_end;
2934e390cabSriastradh 
2944e390cabSriastradh 	/* Our framebuffer is the entirety of fbdev's system memory */
2954e390cabSriastradh 	info->fix.smem_start =
2964e390cabSriastradh 		(unsigned long)(ggtt->gmadr.start + vma->node.start);
2974e390cabSriastradh 	info->fix.smem_len = vma->node.size;
2984e390cabSriastradh 
2994e390cabSriastradh 	vaddr = i915_vma_pin_iomap(vma);
3004e390cabSriastradh 	if (IS_ERR(vaddr)) {
3014e390cabSriastradh 		DRM_ERROR("Failed to remap framebuffer into virtual memory\n");
3024e390cabSriastradh 		ret = PTR_ERR(vaddr);
3034e390cabSriastradh 		goto out_unpin;
3044e390cabSriastradh 	}
3054e390cabSriastradh 	info->screen_base = vaddr;
3064e390cabSriastradh 	info->screen_size = vma->node.size;
3074e390cabSriastradh 
3084e390cabSriastradh 	drm_fb_helper_fill_info(info, &ifbdev->helper, sizes);
3094e390cabSriastradh 
3104e390cabSriastradh 	/* If the object is shmemfs backed, it will have given us zeroed pages.
3114e390cabSriastradh 	 * If the object is stolen however, it will be full of whatever
3124e390cabSriastradh 	 * garbage was left in there.
3134e390cabSriastradh 	 */
3144e390cabSriastradh 	if (vma->obj->stolen && !prealloc)
3154e390cabSriastradh 		memset_io(info->screen_base, 0, info->screen_size);
31641ec0267Sriastradh #endif
3174e390cabSriastradh 
3184e390cabSriastradh 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
3194e390cabSriastradh 
3209ef226f0Sriastradh 	DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08"PRIx32"\n",
3214e390cabSriastradh 		      ifbdev->fb->base.width, ifbdev->fb->base.height,
3224e390cabSriastradh 		      i915_ggtt_offset(vma));
3234e390cabSriastradh 	ifbdev->vma = vma;
3244e390cabSriastradh 	ifbdev->vma_flags = flags;
3254e390cabSriastradh 
3264e390cabSriastradh 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
32741ec0267Sriastradh #ifndef __NetBSD__
3284e390cabSriastradh 	vga_switcheroo_client_fb_set(pdev, info);
32941ec0267Sriastradh #endif
3304e390cabSriastradh 	return 0;
3314e390cabSriastradh 
3324e390cabSriastradh out_unpin:
3334e390cabSriastradh 	intel_unpin_fb_vma(vma, flags);
3344e390cabSriastradh out_unlock:
3354e390cabSriastradh 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
3364e390cabSriastradh 	return ret;
3374e390cabSriastradh }
3384fc39d46Sriastradh 
33989f96c87Sriastradh #ifdef __NetBSD__
34089f96c87Sriastradh #  undef	__iomem
34189f96c87Sriastradh #endif
3424e390cabSriastradh 
3434e390cabSriastradh static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
3444e390cabSriastradh 	.fb_probe = intelfb_create,
3454e390cabSriastradh };
3464e390cabSriastradh 
intel_fbdev_destroy(struct intel_fbdev * ifbdev)3474e390cabSriastradh static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
3484e390cabSriastradh {
3494e390cabSriastradh 	/* We rely on the object-free to release the VMA pinning for
3504e390cabSriastradh 	 * the info->screen_base mmaping. Leaking the VMA is simpler than
3514e390cabSriastradh 	 * trying to rectify all the possible error paths leading here.
3524e390cabSriastradh 	 */
3534e390cabSriastradh 
3544e390cabSriastradh 	drm_fb_helper_fini(&ifbdev->helper);
3554e390cabSriastradh 
3564e390cabSriastradh 	if (ifbdev->vma)
3574e390cabSriastradh 		intel_unpin_fb_vma(ifbdev->vma, ifbdev->vma_flags);
3584e390cabSriastradh 
3594e390cabSriastradh 	if (ifbdev->fb)
3604e390cabSriastradh 		drm_framebuffer_remove(&ifbdev->fb->base);
3614e390cabSriastradh 
3622a8c33eaSriastradh 	mutex_destroy(&ifbdev->hpd_lock);
3632a8c33eaSriastradh 
3644e390cabSriastradh 	kfree(ifbdev);
3654e390cabSriastradh }
3664e390cabSriastradh 
3674e390cabSriastradh /*
3684e390cabSriastradh  * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
3694e390cabSriastradh  * The core display code will have read out the current plane configuration,
3704e390cabSriastradh  * so we use that to figure out if there's an object for us to use as the
3714e390cabSriastradh  * fb, and if so, we re-use it for the fbdev configuration.
3724e390cabSriastradh  *
3734e390cabSriastradh  * Note we only support a single fb shared across pipes for boot (mostly for
3744e390cabSriastradh  * fbcon), so we just find the biggest and use that.
3754e390cabSriastradh  */
intel_fbdev_init_bios(struct drm_device * dev,struct intel_fbdev * ifbdev)3764e390cabSriastradh static bool intel_fbdev_init_bios(struct drm_device *dev,
3774e390cabSriastradh 				 struct intel_fbdev *ifbdev)
3784e390cabSriastradh {
3794e390cabSriastradh 	struct intel_framebuffer *fb = NULL;
3804e390cabSriastradh 	struct drm_crtc *crtc;
3814e390cabSriastradh 	struct intel_crtc *intel_crtc;
3824e390cabSriastradh 	unsigned int max_size = 0;
3834e390cabSriastradh 
3844e390cabSriastradh 	/* Find the largest fb */
3854e390cabSriastradh 	for_each_crtc(dev, crtc) {
3864e390cabSriastradh 		struct drm_i915_gem_object *obj =
3874e390cabSriastradh 			intel_fb_obj(crtc->primary->state->fb);
3884e390cabSriastradh 		intel_crtc = to_intel_crtc(crtc);
3894e390cabSriastradh 
3904e390cabSriastradh 		if (!crtc->state->active || !obj) {
3914e390cabSriastradh 			DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
3924e390cabSriastradh 				      pipe_name(intel_crtc->pipe));
3934e390cabSriastradh 			continue;
3944e390cabSriastradh 		}
3954e390cabSriastradh 
3964e390cabSriastradh 		if (obj->base.size > max_size) {
3974e390cabSriastradh 			DRM_DEBUG_KMS("found possible fb from plane %c\n",
3984e390cabSriastradh 				      pipe_name(intel_crtc->pipe));
3994e390cabSriastradh 			fb = to_intel_framebuffer(crtc->primary->state->fb);
4004e390cabSriastradh 			max_size = obj->base.size;
4014e390cabSriastradh 		}
4024e390cabSriastradh 	}
4034e390cabSriastradh 
4044e390cabSriastradh 	if (!fb) {
4054e390cabSriastradh 		DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
4064e390cabSriastradh 		goto out;
4074e390cabSriastradh 	}
4084e390cabSriastradh 
4094e390cabSriastradh 	/* Now make sure all the pipes will fit into it */
4104e390cabSriastradh 	for_each_crtc(dev, crtc) {
4114e390cabSriastradh 		unsigned int cur_size;
4124e390cabSriastradh 
4134e390cabSriastradh 		intel_crtc = to_intel_crtc(crtc);
4144e390cabSriastradh 
4154e390cabSriastradh 		if (!crtc->state->active) {
4164e390cabSriastradh 			DRM_DEBUG_KMS("pipe %c not active, skipping\n",
4174e390cabSriastradh 				      pipe_name(intel_crtc->pipe));
4184e390cabSriastradh 			continue;
4194e390cabSriastradh 		}
4204e390cabSriastradh 
4214e390cabSriastradh 		DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
4224e390cabSriastradh 			      pipe_name(intel_crtc->pipe));
4234e390cabSriastradh 
4244e390cabSriastradh 		/*
4254e390cabSriastradh 		 * See if the plane fb we found above will fit on this
4264e390cabSriastradh 		 * pipe.  Note we need to use the selected fb's pitch and bpp
4274e390cabSriastradh 		 * rather than the current pipe's, since they differ.
4284e390cabSriastradh 		 */
4294e390cabSriastradh 		cur_size = crtc->state->adjusted_mode.crtc_hdisplay;
4304e390cabSriastradh 		cur_size = cur_size * fb->base.format->cpp[0];
4314e390cabSriastradh 		if (fb->base.pitches[0] < cur_size) {
4324e390cabSriastradh 			DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
4334e390cabSriastradh 				      pipe_name(intel_crtc->pipe),
4344e390cabSriastradh 				      cur_size, fb->base.pitches[0]);
4354e390cabSriastradh 			fb = NULL;
4364e390cabSriastradh 			break;
4374e390cabSriastradh 		}
4384e390cabSriastradh 
4394e390cabSriastradh 		cur_size = crtc->state->adjusted_mode.crtc_vdisplay;
4404e390cabSriastradh 		cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
4414e390cabSriastradh 		cur_size *= fb->base.pitches[0];
4424e390cabSriastradh 		DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
4434e390cabSriastradh 			      pipe_name(intel_crtc->pipe),
4444e390cabSriastradh 			      crtc->state->adjusted_mode.crtc_hdisplay,
4454e390cabSriastradh 			      crtc->state->adjusted_mode.crtc_vdisplay,
4464e390cabSriastradh 			      fb->base.format->cpp[0] * 8,
4474e390cabSriastradh 			      cur_size);
4484e390cabSriastradh 
4494e390cabSriastradh 		if (cur_size > max_size) {
4504e390cabSriastradh 			DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
4514e390cabSriastradh 				      pipe_name(intel_crtc->pipe),
4524e390cabSriastradh 				      cur_size, max_size);
4534e390cabSriastradh 			fb = NULL;
4544e390cabSriastradh 			break;
4554e390cabSriastradh 		}
4564e390cabSriastradh 
4574e390cabSriastradh 		DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
4584e390cabSriastradh 			      pipe_name(intel_crtc->pipe),
4594e390cabSriastradh 			      max_size, cur_size);
4604e390cabSriastradh 	}
4614e390cabSriastradh 
4624e390cabSriastradh 	if (!fb) {
4634e390cabSriastradh 		DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
4644e390cabSriastradh 		goto out;
4654e390cabSriastradh 	}
4664e390cabSriastradh 
4674e390cabSriastradh 	ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
4684e390cabSriastradh 	ifbdev->fb = fb;
4694e390cabSriastradh 
4704e390cabSriastradh 	drm_framebuffer_get(&ifbdev->fb->base);
4714e390cabSriastradh 
4724e390cabSriastradh 	/* Final pass to check if any active pipes don't have fbs */
4734e390cabSriastradh 	for_each_crtc(dev, crtc) {
4744e390cabSriastradh 		intel_crtc = to_intel_crtc(crtc);
4754e390cabSriastradh 
4764e390cabSriastradh 		if (!crtc->state->active)
4774e390cabSriastradh 			continue;
4784e390cabSriastradh 
4794e390cabSriastradh 		WARN(!crtc->primary->state->fb,
4804e390cabSriastradh 		     "re-used BIOS config but lost an fb on crtc %d\n",
4814e390cabSriastradh 		     crtc->base.id);
4824e390cabSriastradh 	}
4834e390cabSriastradh 
4844e390cabSriastradh 
4854e390cabSriastradh 	DRM_DEBUG_KMS("using BIOS fb for initial console\n");
4864e390cabSriastradh 	return true;
4874e390cabSriastradh 
4884e390cabSriastradh out:
4894e390cabSriastradh 
4904e390cabSriastradh 	return false;
4914e390cabSriastradh }
4924e390cabSriastradh 
intel_fbdev_suspend_worker(struct work_struct * work)4934e390cabSriastradh static void intel_fbdev_suspend_worker(struct work_struct *work)
4944e390cabSriastradh {
49541ec0267Sriastradh #ifndef __NetBSD__		/* XXX fb suspend */
4964e390cabSriastradh 	intel_fbdev_set_suspend(&container_of(work,
4974e390cabSriastradh 					      struct drm_i915_private,
4984e390cabSriastradh 					      fbdev_suspend_work)->drm,
4994e390cabSriastradh 				FBINFO_STATE_RUNNING,
5004e390cabSriastradh 				true);
50141ec0267Sriastradh #endif
5024e390cabSriastradh }
5034e390cabSriastradh 
intel_fbdev_init(struct drm_device * dev)5044e390cabSriastradh int intel_fbdev_init(struct drm_device *dev)
5054e390cabSriastradh {
5064e390cabSriastradh 	struct drm_i915_private *dev_priv = to_i915(dev);
5074e390cabSriastradh 	struct intel_fbdev *ifbdev;
5084e390cabSriastradh 	int ret;
5094e390cabSriastradh 
5104e390cabSriastradh 	if (WARN_ON(!HAS_DISPLAY(dev_priv) || !INTEL_DISPLAY_ENABLED(dev_priv)))
5114e390cabSriastradh 		return -ENODEV;
5124e390cabSriastradh 
5134e390cabSriastradh 	ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
5144e390cabSriastradh 	if (ifbdev == NULL)
5154e390cabSriastradh 		return -ENOMEM;
5164e390cabSriastradh 
5174e390cabSriastradh 	mutex_init(&ifbdev->hpd_lock);
5184e390cabSriastradh 	drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
5194e390cabSriastradh 
5204e390cabSriastradh 	if (!intel_fbdev_init_bios(dev, ifbdev))
5214e390cabSriastradh 		ifbdev->preferred_bpp = 32;
5224e390cabSriastradh 
5234e390cabSriastradh 	ret = drm_fb_helper_init(dev, &ifbdev->helper, 4);
5244e390cabSriastradh 	if (ret) {
5254e390cabSriastradh 		kfree(ifbdev);
5264e390cabSriastradh 		return ret;
5274e390cabSriastradh 	}
5284e390cabSriastradh 
5294e390cabSriastradh 	dev_priv->fbdev = ifbdev;
5304e390cabSriastradh 	INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
5314e390cabSriastradh 
5324e390cabSriastradh 	drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
5334e390cabSriastradh 
5344e390cabSriastradh 	return 0;
5354e390cabSriastradh }
5364e390cabSriastradh 
intel_fbdev_initial_config(void * data,async_cookie_t cookie)5374e390cabSriastradh static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
5384e390cabSriastradh {
5394e390cabSriastradh 	struct intel_fbdev *ifbdev = data;
5404e390cabSriastradh 
5414e390cabSriastradh 	/* Due to peculiar init order wrt to hpd handling this is separate. */
5424e390cabSriastradh 	if (drm_fb_helper_initial_config(&ifbdev->helper,
5434e390cabSriastradh 					 ifbdev->preferred_bpp))
5444e390cabSriastradh 		intel_fbdev_unregister(to_i915(ifbdev->helper.dev));
5454e390cabSriastradh }
5464e390cabSriastradh 
intel_fbdev_initial_config_async(struct drm_device * dev)5474e390cabSriastradh void intel_fbdev_initial_config_async(struct drm_device *dev)
5484e390cabSriastradh {
5494e390cabSriastradh 	struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
5504e390cabSriastradh 
5514e390cabSriastradh 	if (!ifbdev)
5524e390cabSriastradh 		return;
5534e390cabSriastradh 
5544e390cabSriastradh 	ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
5554e390cabSriastradh }
5564e390cabSriastradh 
intel_fbdev_sync(struct intel_fbdev * ifbdev)5574e390cabSriastradh static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
5584e390cabSriastradh {
5594e390cabSriastradh 	if (!ifbdev->cookie)
5604e390cabSriastradh 		return;
5614e390cabSriastradh 
5624e390cabSriastradh 	/* Only serialises with all preceding async calls, hence +1 */
5634e390cabSriastradh 	async_synchronize_cookie(ifbdev->cookie + 1);
5644e390cabSriastradh 	ifbdev->cookie = 0;
5654e390cabSriastradh }
5664e390cabSriastradh 
intel_fbdev_unregister(struct drm_i915_private * dev_priv)5674e390cabSriastradh void intel_fbdev_unregister(struct drm_i915_private *dev_priv)
5684e390cabSriastradh {
5694e390cabSriastradh 	struct intel_fbdev *ifbdev = dev_priv->fbdev;
5704e390cabSriastradh 
5714e390cabSriastradh 	if (!ifbdev)
5724e390cabSriastradh 		return;
5734e390cabSriastradh 
5744e390cabSriastradh 	cancel_work_sync(&dev_priv->fbdev_suspend_work);
575fba37ecdSriastradh #ifndef __NetBSD__		/* XXX fb async */
5764e390cabSriastradh 	if (!current_is_async())
577fba37ecdSriastradh #endif
5784e390cabSriastradh 		intel_fbdev_sync(ifbdev);
5794e390cabSriastradh 
5804e390cabSriastradh 	drm_fb_helper_unregister_fbi(&ifbdev->helper);
5814e390cabSriastradh }
5824e390cabSriastradh 
intel_fbdev_fini(struct drm_i915_private * dev_priv)5834e390cabSriastradh void intel_fbdev_fini(struct drm_i915_private *dev_priv)
5844e390cabSriastradh {
5854e390cabSriastradh 	struct intel_fbdev *ifbdev = fetch_and_zero(&dev_priv->fbdev);
5864e390cabSriastradh 
5874e390cabSriastradh 	if (!ifbdev)
5884e390cabSriastradh 		return;
5894e390cabSriastradh 
5904e390cabSriastradh 	intel_fbdev_destroy(ifbdev);
5914e390cabSriastradh }
5924e390cabSriastradh 
5934e390cabSriastradh /* Suspends/resumes fbdev processing of incoming HPD events. When resuming HPD
5944e390cabSriastradh  * processing, fbdev will perform a full connector reprobe if a hotplug event
5954e390cabSriastradh  * was received while HPD was suspended.
5964e390cabSriastradh  */
59743f68184Sriastradh #ifndef __NetBSD__		/* XXX fb suspend */
intel_fbdev_hpd_set_suspend(struct intel_fbdev * ifbdev,int state)5984e390cabSriastradh static void intel_fbdev_hpd_set_suspend(struct intel_fbdev *ifbdev, int state)
5994e390cabSriastradh {
6004e390cabSriastradh 	bool send_hpd = false;
6014e390cabSriastradh 
6024e390cabSriastradh 	mutex_lock(&ifbdev->hpd_lock);
6034e390cabSriastradh 	ifbdev->hpd_suspended = state == FBINFO_STATE_SUSPENDED;
6044e390cabSriastradh 	send_hpd = !ifbdev->hpd_suspended && ifbdev->hpd_waiting;
6054e390cabSriastradh 	ifbdev->hpd_waiting = false;
6064e390cabSriastradh 	mutex_unlock(&ifbdev->hpd_lock);
6074e390cabSriastradh 
6084e390cabSriastradh 	if (send_hpd) {
6094e390cabSriastradh 		DRM_DEBUG_KMS("Handling delayed fbcon HPD event\n");
6104e390cabSriastradh 		drm_fb_helper_hotplug_event(&ifbdev->helper);
6114e390cabSriastradh 	}
6124e390cabSriastradh }
61343f68184Sriastradh #endif	/* __NetBSD__ */
6144e390cabSriastradh 
intel_fbdev_set_suspend(struct drm_device * dev,int state,bool synchronous)6154e390cabSriastradh void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
6164e390cabSriastradh {
61741ec0267Sriastradh #ifndef __NetBSD__		/* XXX fb suspend */
6184e390cabSriastradh 	struct drm_i915_private *dev_priv = to_i915(dev);
6194e390cabSriastradh 	struct intel_fbdev *ifbdev = dev_priv->fbdev;
6204e390cabSriastradh 	struct fb_info *info;
6214e390cabSriastradh 
6224e390cabSriastradh 	if (!ifbdev || !ifbdev->vma)
6234e390cabSriastradh 		return;
6244e390cabSriastradh 
6254e390cabSriastradh 	info = ifbdev->helper.fbdev;
6264e390cabSriastradh 
6274e390cabSriastradh 	if (synchronous) {
6284e390cabSriastradh 		/* Flush any pending work to turn the console on, and then
6294e390cabSriastradh 		 * wait to turn it off. It must be synchronous as we are
6304e390cabSriastradh 		 * about to suspend or unload the driver.
6314e390cabSriastradh 		 *
6324e390cabSriastradh 		 * Note that from within the work-handler, we cannot flush
6334e390cabSriastradh 		 * ourselves, so only flush outstanding work upon suspend!
6344e390cabSriastradh 		 */
6354e390cabSriastradh 		if (state != FBINFO_STATE_RUNNING)
6364e390cabSriastradh 			flush_work(&dev_priv->fbdev_suspend_work);
6374e390cabSriastradh 
6384e390cabSriastradh 		console_lock();
6394e390cabSriastradh 	} else {
6404e390cabSriastradh 		/*
6414e390cabSriastradh 		 * The console lock can be pretty contented on resume due
6424e390cabSriastradh 		 * to all the printk activity.  Try to keep it out of the hot
6434e390cabSriastradh 		 * path of resume if possible.
6444e390cabSriastradh 		 */
6454e390cabSriastradh 		WARN_ON(state != FBINFO_STATE_RUNNING);
6464e390cabSriastradh 		if (!console_trylock()) {
6474e390cabSriastradh 			/* Don't block our own workqueue as this can
6484e390cabSriastradh 			 * be run in parallel with other i915.ko tasks.
6494e390cabSriastradh 			 */
6504e390cabSriastradh 			schedule_work(&dev_priv->fbdev_suspend_work);
6514e390cabSriastradh 			return;
6524e390cabSriastradh 		}
6534e390cabSriastradh 	}
6544e390cabSriastradh 
6554e390cabSriastradh 	/* On resume from hibernation: If the object is shmemfs backed, it has
6564e390cabSriastradh 	 * been restored from swap. If the object is stolen however, it will be
6574e390cabSriastradh 	 * full of whatever garbage was left in there.
6584e390cabSriastradh 	 */
6594e390cabSriastradh 	if (state == FBINFO_STATE_RUNNING &&
6604e390cabSriastradh 	    intel_fb_obj(&ifbdev->fb->base)->stolen)
6614e390cabSriastradh 		memset_io(info->screen_base, 0, info->screen_size);
6624e390cabSriastradh 
6634e390cabSriastradh 	drm_fb_helper_set_suspend(&ifbdev->helper, state);
6644e390cabSriastradh 	console_unlock();
6654e390cabSriastradh 
6664e390cabSriastradh 	intel_fbdev_hpd_set_suspend(ifbdev, state);
66741ec0267Sriastradh #endif
6684e390cabSriastradh }
6694e390cabSriastradh 
intel_fbdev_output_poll_changed(struct drm_device * dev)6704e390cabSriastradh void intel_fbdev_output_poll_changed(struct drm_device *dev)
6714e390cabSriastradh {
6724e390cabSriastradh 	struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
6734e390cabSriastradh 	bool send_hpd;
6744e390cabSriastradh 
6754e390cabSriastradh 	if (!ifbdev)
6764e390cabSriastradh 		return;
6774e390cabSriastradh 
6784e390cabSriastradh 	intel_fbdev_sync(ifbdev);
6794e390cabSriastradh 
6804e390cabSriastradh 	mutex_lock(&ifbdev->hpd_lock);
6814e390cabSriastradh 	send_hpd = !ifbdev->hpd_suspended;
6824e390cabSriastradh 	ifbdev->hpd_waiting = true;
6834e390cabSriastradh 	mutex_unlock(&ifbdev->hpd_lock);
6844e390cabSriastradh 
6854e390cabSriastradh 	if (send_hpd && (ifbdev->vma || ifbdev->helper.deferred_setup))
6864e390cabSriastradh 		drm_fb_helper_hotplug_event(&ifbdev->helper);
6874e390cabSriastradh }
6884e390cabSriastradh 
intel_fbdev_restore_mode(struct drm_device * dev)6894e390cabSriastradh void intel_fbdev_restore_mode(struct drm_device *dev)
6904e390cabSriastradh {
6914e390cabSriastradh 	struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
6924e390cabSriastradh 
6934e390cabSriastradh 	if (!ifbdev)
6944e390cabSriastradh 		return;
6954e390cabSriastradh 
6964e390cabSriastradh 	intel_fbdev_sync(ifbdev);
6974e390cabSriastradh 	if (!ifbdev->vma)
6984e390cabSriastradh 		return;
6994e390cabSriastradh 
7004e390cabSriastradh 	if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper) == 0)
7014e390cabSriastradh 		intel_fbdev_invalidate(ifbdev);
7024e390cabSriastradh }
703