xref: /openbmc/linux/drivers/video/fbdev/efifb.c (revision 6774def6)
1 /*
2  * Framebuffer driver for EFI/UEFI based system
3  *
4  * (c) 2006 Edgar Hucek <gimli@dark-green.com>
5  * Original efi driver written by Gerd Knorr <kraxel@goldbach.in-berlin.de>
6  *
7  */
8 
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/fb.h>
13 #include <linux/platform_device.h>
14 #include <linux/screen_info.h>
15 #include <linux/dmi.h>
16 #include <linux/pci.h>
17 #include <video/vga.h>
18 #include <asm/sysfb.h>
19 
20 static bool request_mem_succeeded = false;
21 
22 static struct fb_var_screeninfo efifb_defined = {
23 	.activate		= FB_ACTIVATE_NOW,
24 	.height			= -1,
25 	.width			= -1,
26 	.right_margin		= 32,
27 	.upper_margin		= 16,
28 	.lower_margin		= 4,
29 	.vsync_len		= 4,
30 	.vmode			= FB_VMODE_NONINTERLACED,
31 };
32 
33 static struct fb_fix_screeninfo efifb_fix = {
34 	.id			= "EFI VGA",
35 	.type			= FB_TYPE_PACKED_PIXELS,
36 	.accel			= FB_ACCEL_NONE,
37 	.visual			= FB_VISUAL_TRUECOLOR,
38 };
39 
40 static int efifb_setcolreg(unsigned regno, unsigned red, unsigned green,
41 			   unsigned blue, unsigned transp,
42 			   struct fb_info *info)
43 {
44 	/*
45 	 *  Set a single color register. The values supplied are
46 	 *  already rounded down to the hardware's capabilities
47 	 *  (according to the entries in the `var' structure). Return
48 	 *  != 0 for invalid regno.
49 	 */
50 
51 	if (regno >= info->cmap.len)
52 		return 1;
53 
54 	if (regno < 16) {
55 		red   >>= 8;
56 		green >>= 8;
57 		blue  >>= 8;
58 		((u32 *)(info->pseudo_palette))[regno] =
59 			(red   << info->var.red.offset)   |
60 			(green << info->var.green.offset) |
61 			(blue  << info->var.blue.offset);
62 	}
63 	return 0;
64 }
65 
66 static void efifb_destroy(struct fb_info *info)
67 {
68 	if (info->screen_base)
69 		iounmap(info->screen_base);
70 	if (request_mem_succeeded)
71 		release_mem_region(info->apertures->ranges[0].base,
72 				   info->apertures->ranges[0].size);
73 	fb_dealloc_cmap(&info->cmap);
74 }
75 
76 static struct fb_ops efifb_ops = {
77 	.owner		= THIS_MODULE,
78 	.fb_destroy	= efifb_destroy,
79 	.fb_setcolreg	= efifb_setcolreg,
80 	.fb_fillrect	= cfb_fillrect,
81 	.fb_copyarea	= cfb_copyarea,
82 	.fb_imageblit	= cfb_imageblit,
83 };
84 
85 static int efifb_setup(char *options)
86 {
87 	char *this_opt;
88 	int i;
89 
90 	if (options && *options) {
91 		while ((this_opt = strsep(&options, ",")) != NULL) {
92 			if (!*this_opt) continue;
93 
94 			for (i = 0; i < M_UNKNOWN; i++) {
95 				if (efifb_dmi_list[i].base != 0 &&
96 				    !strcmp(this_opt, efifb_dmi_list[i].optname)) {
97 					screen_info.lfb_base = efifb_dmi_list[i].base;
98 					screen_info.lfb_linelength = efifb_dmi_list[i].stride;
99 					screen_info.lfb_width = efifb_dmi_list[i].width;
100 					screen_info.lfb_height = efifb_dmi_list[i].height;
101 				}
102 			}
103 			if (!strncmp(this_opt, "base:", 5))
104 				screen_info.lfb_base = simple_strtoul(this_opt+5, NULL, 0);
105 			else if (!strncmp(this_opt, "stride:", 7))
106 				screen_info.lfb_linelength = simple_strtoul(this_opt+7, NULL, 0) * 4;
107 			else if (!strncmp(this_opt, "height:", 7))
108 				screen_info.lfb_height = simple_strtoul(this_opt+7, NULL, 0);
109 			else if (!strncmp(this_opt, "width:", 6))
110 				screen_info.lfb_width = simple_strtoul(this_opt+6, NULL, 0);
111 		}
112 	}
113 
114 	return 0;
115 }
116 
117 static int efifb_probe(struct platform_device *dev)
118 {
119 	struct fb_info *info;
120 	int err;
121 	unsigned int size_vmode;
122 	unsigned int size_remap;
123 	unsigned int size_total;
124 	char *option = NULL;
125 
126 	if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
127 		return -ENODEV;
128 
129 	if (fb_get_options("efifb", &option))
130 		return -ENODEV;
131 	efifb_setup(option);
132 
133 	/* We don't get linelength from UGA Draw Protocol, only from
134 	 * EFI Graphics Protocol.  So if it's not in DMI, and it's not
135 	 * passed in from the user, we really can't use the framebuffer.
136 	 */
137 	if (!screen_info.lfb_linelength)
138 		return -ENODEV;
139 
140 	if (!screen_info.lfb_depth)
141 		screen_info.lfb_depth = 32;
142 	if (!screen_info.pages)
143 		screen_info.pages = 1;
144 	if (!screen_info.lfb_base) {
145 		printk(KERN_DEBUG "efifb: invalid framebuffer address\n");
146 		return -ENODEV;
147 	}
148 	printk(KERN_INFO "efifb: probing for efifb\n");
149 
150 	/* just assume they're all unset if any are */
151 	if (!screen_info.blue_size) {
152 		screen_info.blue_size = 8;
153 		screen_info.blue_pos = 0;
154 		screen_info.green_size = 8;
155 		screen_info.green_pos = 8;
156 		screen_info.red_size = 8;
157 		screen_info.red_pos = 16;
158 		screen_info.rsvd_size = 8;
159 		screen_info.rsvd_pos = 24;
160 	}
161 
162 	efifb_fix.smem_start = screen_info.lfb_base;
163 	efifb_defined.bits_per_pixel = screen_info.lfb_depth;
164 	efifb_defined.xres = screen_info.lfb_width;
165 	efifb_defined.yres = screen_info.lfb_height;
166 	efifb_fix.line_length = screen_info.lfb_linelength;
167 
168 	/*   size_vmode -- that is the amount of memory needed for the
169 	 *                 used video mode, i.e. the minimum amount of
170 	 *                 memory we need. */
171 	size_vmode = efifb_defined.yres * efifb_fix.line_length;
172 
173 	/*   size_total -- all video memory we have. Used for
174 	 *                 entries, ressource allocation and bounds
175 	 *                 checking. */
176 	size_total = screen_info.lfb_size;
177 	if (size_total < size_vmode)
178 		size_total = size_vmode;
179 
180 	/*   size_remap -- the amount of video memory we are going to
181 	 *                 use for efifb.  With modern cards it is no
182 	 *                 option to simply use size_total as that
183 	 *                 wastes plenty of kernel address space. */
184 	size_remap  = size_vmode * 2;
185 	if (size_remap > size_total)
186 		size_remap = size_total;
187 	if (size_remap % PAGE_SIZE)
188 		size_remap += PAGE_SIZE - (size_remap % PAGE_SIZE);
189 	efifb_fix.smem_len = size_remap;
190 
191 	if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) {
192 		request_mem_succeeded = true;
193 	} else {
194 		/* We cannot make this fatal. Sometimes this comes from magic
195 		   spaces our resource handlers simply don't know about */
196 		printk(KERN_WARNING
197 		       "efifb: cannot reserve video memory at 0x%lx\n",
198 			efifb_fix.smem_start);
199 	}
200 
201 	info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
202 	if (!info) {
203 		printk(KERN_ERR "efifb: cannot allocate framebuffer\n");
204 		err = -ENOMEM;
205 		goto err_release_mem;
206 	}
207 	platform_set_drvdata(dev, info);
208 	info->pseudo_palette = info->par;
209 	info->par = NULL;
210 
211 	info->apertures = alloc_apertures(1);
212 	if (!info->apertures) {
213 		err = -ENOMEM;
214 		goto err_release_fb;
215 	}
216 	info->apertures->ranges[0].base = efifb_fix.smem_start;
217 	info->apertures->ranges[0].size = size_remap;
218 
219 	info->screen_base = ioremap_wc(efifb_fix.smem_start, efifb_fix.smem_len);
220 	if (!info->screen_base) {
221 		printk(KERN_ERR "efifb: abort, cannot ioremap video memory "
222 				"0x%x @ 0x%lx\n",
223 			efifb_fix.smem_len, efifb_fix.smem_start);
224 		err = -EIO;
225 		goto err_release_fb;
226 	}
227 
228 	printk(KERN_INFO "efifb: framebuffer at 0x%lx, mapped to 0x%p, "
229 	       "using %dk, total %dk\n",
230 	       efifb_fix.smem_start, info->screen_base,
231 	       size_remap/1024, size_total/1024);
232 	printk(KERN_INFO "efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
233 	       efifb_defined.xres, efifb_defined.yres,
234 	       efifb_defined.bits_per_pixel, efifb_fix.line_length,
235 	       screen_info.pages);
236 
237 	efifb_defined.xres_virtual = efifb_defined.xres;
238 	efifb_defined.yres_virtual = efifb_fix.smem_len /
239 					efifb_fix.line_length;
240 	printk(KERN_INFO "efifb: scrolling: redraw\n");
241 	efifb_defined.yres_virtual = efifb_defined.yres;
242 
243 	/* some dummy values for timing to make fbset happy */
244 	efifb_defined.pixclock     = 10000000 / efifb_defined.xres *
245 					1000 / efifb_defined.yres;
246 	efifb_defined.left_margin  = (efifb_defined.xres / 8) & 0xf8;
247 	efifb_defined.hsync_len    = (efifb_defined.xres / 8) & 0xf8;
248 
249 	efifb_defined.red.offset    = screen_info.red_pos;
250 	efifb_defined.red.length    = screen_info.red_size;
251 	efifb_defined.green.offset  = screen_info.green_pos;
252 	efifb_defined.green.length  = screen_info.green_size;
253 	efifb_defined.blue.offset   = screen_info.blue_pos;
254 	efifb_defined.blue.length   = screen_info.blue_size;
255 	efifb_defined.transp.offset = screen_info.rsvd_pos;
256 	efifb_defined.transp.length = screen_info.rsvd_size;
257 
258 	printk(KERN_INFO "efifb: %s: "
259 	       "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
260 	       "Truecolor",
261 	       screen_info.rsvd_size,
262 	       screen_info.red_size,
263 	       screen_info.green_size,
264 	       screen_info.blue_size,
265 	       screen_info.rsvd_pos,
266 	       screen_info.red_pos,
267 	       screen_info.green_pos,
268 	       screen_info.blue_pos);
269 
270 	efifb_fix.ypanstep  = 0;
271 	efifb_fix.ywrapstep = 0;
272 
273 	info->fbops = &efifb_ops;
274 	info->var = efifb_defined;
275 	info->fix = efifb_fix;
276 	info->flags = FBINFO_FLAG_DEFAULT | FBINFO_MISC_FIRMWARE;
277 
278 	if ((err = fb_alloc_cmap(&info->cmap, 256, 0)) < 0) {
279 		printk(KERN_ERR "efifb: cannot allocate colormap\n");
280 		goto err_unmap;
281 	}
282 	if ((err = register_framebuffer(info)) < 0) {
283 		printk(KERN_ERR "efifb: cannot register framebuffer\n");
284 		goto err_fb_dealoc;
285 	}
286 	fb_info(info, "%s frame buffer device\n", info->fix.id);
287 	return 0;
288 
289 err_fb_dealoc:
290 	fb_dealloc_cmap(&info->cmap);
291 err_unmap:
292 	iounmap(info->screen_base);
293 err_release_fb:
294 	framebuffer_release(info);
295 err_release_mem:
296 	if (request_mem_succeeded)
297 		release_mem_region(efifb_fix.smem_start, size_total);
298 	return err;
299 }
300 
301 static int efifb_remove(struct platform_device *pdev)
302 {
303 	struct fb_info *info = platform_get_drvdata(pdev);
304 
305 	unregister_framebuffer(info);
306 	framebuffer_release(info);
307 
308 	return 0;
309 }
310 
311 static struct platform_driver efifb_driver = {
312 	.driver = {
313 		.name = "efi-framebuffer",
314 		.owner = THIS_MODULE,
315 	},
316 	.probe = efifb_probe,
317 	.remove = efifb_remove,
318 };
319 
320 module_platform_driver(efifb_driver);
321 MODULE_LICENSE("GPL");
322