xref: /linux/drivers/gpu/drm/gma500/fbdev.c (revision f6e8dc9edf963dbc99085e54f6ced6da9daa6100)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /**************************************************************************
3  * Copyright (c) 2007-2011, Intel Corporation.
4  * All Rights Reserved.
5  *
6  **************************************************************************/
7 
8 #include <linux/fb.h>
9 
10 #include <drm/drm_crtc_helper.h>
11 #include <drm/drm_drv.h>
12 #include <drm/drm_fb_helper.h>
13 #include <drm/drm_framebuffer.h>
14 
15 #include "gem.h"
16 #include "psb_drv.h"
17 
18 /*
19  * VM area struct
20  */
21 
22 static vm_fault_t psb_fbdev_vm_fault(struct vm_fault *vmf)
23 {
24 	struct vm_area_struct *vma = vmf->vma;
25 	struct fb_info *info = vma->vm_private_data;
26 	unsigned long address = vmf->address - (vmf->pgoff << PAGE_SHIFT);
27 	unsigned long pfn = info->fix.smem_start >> PAGE_SHIFT;
28 	vm_fault_t err = VM_FAULT_SIGBUS;
29 	unsigned long page_num = vma_pages(vma);
30 	unsigned long i;
31 
32 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
33 
34 	for (i = 0; i < page_num; ++i) {
35 		err = vmf_insert_mixed(vma, address, pfn);
36 		if (unlikely(err & VM_FAULT_ERROR))
37 			break;
38 		address += PAGE_SIZE;
39 		++pfn;
40 	}
41 
42 	return err;
43 }
44 
45 static const struct vm_operations_struct psb_fbdev_vm_ops = {
46 	.fault	= psb_fbdev_vm_fault,
47 };
48 
49 /*
50  * struct fb_ops
51  */
52 
53 static int psb_fbdev_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
54 {
55 	if (vma->vm_pgoff != 0)
56 		return -EINVAL;
57 	if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
58 		return -EINVAL;
59 
60 	/*
61 	 * If this is a GEM object then info->screen_base is the virtual
62 	 * kernel remapping of the object. FIXME: Review if this is
63 	 * suitable for our mmap work
64 	 */
65 	vma->vm_ops = &psb_fbdev_vm_ops;
66 	vma->vm_private_data = info;
67 	vm_flags_set(vma, VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP);
68 
69 	return 0;
70 }
71 
72 static void psb_fbdev_fb_destroy(struct fb_info *info)
73 {
74 	struct drm_fb_helper *fb_helper = info->par;
75 	struct drm_framebuffer *fb = fb_helper->fb;
76 	struct drm_gem_object *obj = fb->obj[0];
77 
78 	drm_fb_helper_fini(fb_helper);
79 
80 	drm_framebuffer_unregister_private(fb);
81 	drm_framebuffer_cleanup(fb);
82 	kfree(fb);
83 
84 	drm_gem_object_put(obj);
85 
86 	drm_client_release(&fb_helper->client);
87 }
88 
89 static const struct fb_ops psb_fbdev_fb_ops = {
90 	.owner = THIS_MODULE,
91 	__FB_DEFAULT_IOMEM_OPS_RDWR,
92 	DRM_FB_HELPER_DEFAULT_OPS,
93 	__FB_DEFAULT_IOMEM_OPS_DRAW,
94 	.fb_mmap = psb_fbdev_fb_mmap,
95 	.fb_destroy = psb_fbdev_fb_destroy,
96 };
97 
98 static const struct drm_fb_helper_funcs psb_fbdev_fb_helper_funcs = {
99 };
100 
101 /*
102  * struct drm_driver
103  */
104 
105 int psb_fbdev_driver_fbdev_probe(struct drm_fb_helper *fb_helper,
106 				 struct drm_fb_helper_surface_size *sizes)
107 {
108 	struct drm_device *dev = fb_helper->dev;
109 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
110 	struct pci_dev *pdev = to_pci_dev(dev->dev);
111 	struct fb_info *info;
112 	struct drm_framebuffer *fb;
113 	struct drm_mode_fb_cmd2 mode_cmd = { };
114 	int size;
115 	int ret;
116 	struct psb_gem_object *backing;
117 	struct drm_gem_object *obj;
118 	u32 bpp, depth;
119 
120 	/* No 24-bit packed mode */
121 	if (sizes->surface_bpp == 24) {
122 		sizes->surface_bpp = 32;
123 		sizes->surface_depth = 24;
124 	}
125 	bpp = sizes->surface_bpp;
126 	depth = sizes->surface_depth;
127 
128 	/*
129 	 * If the mode does not fit in 32 bit then switch to 16 bit to get
130 	 * a console on full resolution. The X mode setting server will
131 	 * allocate its own 32-bit GEM framebuffer.
132 	 */
133 	size = ALIGN(sizes->surface_width * DIV_ROUND_UP(bpp, 8), 64) *
134 		     sizes->surface_height;
135 	size = ALIGN(size, PAGE_SIZE);
136 
137 	if (size > dev_priv->vram_stolen_size) {
138 		sizes->surface_bpp = 16;
139 		sizes->surface_depth = 16;
140 	}
141 	bpp = sizes->surface_bpp;
142 	depth = sizes->surface_depth;
143 
144 	mode_cmd.width = sizes->surface_width;
145 	mode_cmd.height = sizes->surface_height;
146 	mode_cmd.pitches[0] = ALIGN(mode_cmd.width * DIV_ROUND_UP(bpp, 8), 64);
147 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
148 
149 	size = mode_cmd.pitches[0] * mode_cmd.height;
150 	size = ALIGN(size, PAGE_SIZE);
151 
152 	/* Allocate the framebuffer in the GTT with stolen page backing */
153 	backing = psb_gem_create(dev, size, "fb", true, PAGE_SIZE);
154 	if (IS_ERR(backing))
155 		return PTR_ERR(backing);
156 	obj = &backing->base;
157 
158 	fb = psb_framebuffer_create(dev,
159 				    drm_get_format_info(dev, mode_cmd.pixel_format,
160 							mode_cmd.modifier[0]),
161 				    &mode_cmd, obj);
162 	if (IS_ERR(fb)) {
163 		ret = PTR_ERR(fb);
164 		goto err_drm_gem_object_put;
165 	}
166 
167 	fb_helper->funcs = &psb_fbdev_fb_helper_funcs;
168 	fb_helper->fb = fb;
169 
170 	info = drm_fb_helper_alloc_info(fb_helper);
171 	if (IS_ERR(info)) {
172 		ret = PTR_ERR(info);
173 		goto err_drm_framebuffer_unregister_private;
174 	}
175 
176 	info->fbops = &psb_fbdev_fb_ops;
177 
178 	/* Accessed stolen memory directly */
179 	info->screen_base = dev_priv->vram_addr + backing->offset;
180 	info->screen_size = size;
181 
182 	drm_fb_helper_fill_info(info, fb_helper, sizes);
183 
184 	info->fix.smem_start = dev_priv->stolen_base + backing->offset;
185 	info->fix.smem_len = size;
186 	info->fix.ywrapstep = 0;
187 	info->fix.ypanstep = 0;
188 	info->fix.mmio_start = pci_resource_start(pdev, 0);
189 	info->fix.mmio_len = pci_resource_len(pdev, 0);
190 
191 	fb_memset_io(info->screen_base, 0, info->screen_size);
192 
193 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
194 
195 	dev_dbg(dev->dev, "allocated %dx%d fb\n", fb->width, fb->height);
196 
197 	return 0;
198 
199 err_drm_framebuffer_unregister_private:
200 	drm_framebuffer_unregister_private(fb);
201 	drm_framebuffer_cleanup(fb);
202 	kfree(fb);
203 err_drm_gem_object_put:
204 	drm_gem_object_put(obj);
205 	return ret;
206 }
207