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
20static bool request_mem_succeeded = false;
21
22static 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
33static 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
40static 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
66static 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
76static 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
85static 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
117static inline bool fb_base_is_valid(void)
118{
119	if (screen_info.lfb_base)
120		return true;
121
122	if (!(screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE))
123		return false;
124
125	if (screen_info.ext_lfb_base)
126		return true;
127
128	return false;
129}
130
131static int efifb_probe(struct platform_device *dev)
132{
133	struct fb_info *info;
134	int err;
135	unsigned int size_vmode;
136	unsigned int size_remap;
137	unsigned int size_total;
138	char *option = NULL;
139
140	if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
141		return -ENODEV;
142
143	if (fb_get_options("efifb", &option))
144		return -ENODEV;
145	efifb_setup(option);
146
147	/* We don't get linelength from UGA Draw Protocol, only from
148	 * EFI Graphics Protocol.  So if it's not in DMI, and it's not
149	 * passed in from the user, we really can't use the framebuffer.
150	 */
151	if (!screen_info.lfb_linelength)
152		return -ENODEV;
153
154	if (!screen_info.lfb_depth)
155		screen_info.lfb_depth = 32;
156	if (!screen_info.pages)
157		screen_info.pages = 1;
158	if (!fb_base_is_valid()) {
159		printk(KERN_DEBUG "efifb: invalid framebuffer address\n");
160		return -ENODEV;
161	}
162	printk(KERN_INFO "efifb: probing for efifb\n");
163
164	/* just assume they're all unset if any are */
165	if (!screen_info.blue_size) {
166		screen_info.blue_size = 8;
167		screen_info.blue_pos = 0;
168		screen_info.green_size = 8;
169		screen_info.green_pos = 8;
170		screen_info.red_size = 8;
171		screen_info.red_pos = 16;
172		screen_info.rsvd_size = 8;
173		screen_info.rsvd_pos = 24;
174	}
175
176	efifb_fix.smem_start = screen_info.lfb_base;
177
178	if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE) {
179		u64 ext_lfb_base;
180
181		ext_lfb_base = (u64)(unsigned long)screen_info.ext_lfb_base << 32;
182		efifb_fix.smem_start |= ext_lfb_base;
183	}
184
185	efifb_defined.bits_per_pixel = screen_info.lfb_depth;
186	efifb_defined.xres = screen_info.lfb_width;
187	efifb_defined.yres = screen_info.lfb_height;
188	efifb_fix.line_length = screen_info.lfb_linelength;
189
190	/*   size_vmode -- that is the amount of memory needed for the
191	 *                 used video mode, i.e. the minimum amount of
192	 *                 memory we need. */
193	size_vmode = efifb_defined.yres * efifb_fix.line_length;
194
195	/*   size_total -- all video memory we have. Used for
196	 *                 entries, ressource allocation and bounds
197	 *                 checking. */
198	size_total = screen_info.lfb_size;
199	if (size_total < size_vmode)
200		size_total = size_vmode;
201
202	/*   size_remap -- the amount of video memory we are going to
203	 *                 use for efifb.  With modern cards it is no
204	 *                 option to simply use size_total as that
205	 *                 wastes plenty of kernel address space. */
206	size_remap  = size_vmode * 2;
207	if (size_remap > size_total)
208		size_remap = size_total;
209	if (size_remap % PAGE_SIZE)
210		size_remap += PAGE_SIZE - (size_remap % PAGE_SIZE);
211	efifb_fix.smem_len = size_remap;
212
213	if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) {
214		request_mem_succeeded = true;
215	} else {
216		/* We cannot make this fatal. Sometimes this comes from magic
217		   spaces our resource handlers simply don't know about */
218		printk(KERN_WARNING
219		       "efifb: cannot reserve video memory at 0x%lx\n",
220			efifb_fix.smem_start);
221	}
222
223	info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
224	if (!info) {
225		printk(KERN_ERR "efifb: cannot allocate framebuffer\n");
226		err = -ENOMEM;
227		goto err_release_mem;
228	}
229	platform_set_drvdata(dev, info);
230	info->pseudo_palette = info->par;
231	info->par = NULL;
232
233	info->apertures = alloc_apertures(1);
234	if (!info->apertures) {
235		err = -ENOMEM;
236		goto err_release_fb;
237	}
238	info->apertures->ranges[0].base = efifb_fix.smem_start;
239	info->apertures->ranges[0].size = size_remap;
240
241	info->screen_base = ioremap_wc(efifb_fix.smem_start, efifb_fix.smem_len);
242	if (!info->screen_base) {
243		printk(KERN_ERR "efifb: abort, cannot ioremap video memory "
244				"0x%x @ 0x%lx\n",
245			efifb_fix.smem_len, efifb_fix.smem_start);
246		err = -EIO;
247		goto err_release_fb;
248	}
249
250	printk(KERN_INFO "efifb: framebuffer at 0x%lx, mapped to 0x%p, "
251	       "using %dk, total %dk\n",
252	       efifb_fix.smem_start, info->screen_base,
253	       size_remap/1024, size_total/1024);
254	printk(KERN_INFO "efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
255	       efifb_defined.xres, efifb_defined.yres,
256	       efifb_defined.bits_per_pixel, efifb_fix.line_length,
257	       screen_info.pages);
258
259	efifb_defined.xres_virtual = efifb_defined.xres;
260	efifb_defined.yres_virtual = efifb_fix.smem_len /
261					efifb_fix.line_length;
262	printk(KERN_INFO "efifb: scrolling: redraw\n");
263	efifb_defined.yres_virtual = efifb_defined.yres;
264
265	/* some dummy values for timing to make fbset happy */
266	efifb_defined.pixclock     = 10000000 / efifb_defined.xres *
267					1000 / efifb_defined.yres;
268	efifb_defined.left_margin  = (efifb_defined.xres / 8) & 0xf8;
269	efifb_defined.hsync_len    = (efifb_defined.xres / 8) & 0xf8;
270
271	efifb_defined.red.offset    = screen_info.red_pos;
272	efifb_defined.red.length    = screen_info.red_size;
273	efifb_defined.green.offset  = screen_info.green_pos;
274	efifb_defined.green.length  = screen_info.green_size;
275	efifb_defined.blue.offset   = screen_info.blue_pos;
276	efifb_defined.blue.length   = screen_info.blue_size;
277	efifb_defined.transp.offset = screen_info.rsvd_pos;
278	efifb_defined.transp.length = screen_info.rsvd_size;
279
280	printk(KERN_INFO "efifb: %s: "
281	       "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
282	       "Truecolor",
283	       screen_info.rsvd_size,
284	       screen_info.red_size,
285	       screen_info.green_size,
286	       screen_info.blue_size,
287	       screen_info.rsvd_pos,
288	       screen_info.red_pos,
289	       screen_info.green_pos,
290	       screen_info.blue_pos);
291
292	efifb_fix.ypanstep  = 0;
293	efifb_fix.ywrapstep = 0;
294
295	info->fbops = &efifb_ops;
296	info->var = efifb_defined;
297	info->fix = efifb_fix;
298	info->flags = FBINFO_FLAG_DEFAULT | FBINFO_MISC_FIRMWARE;
299
300	if ((err = fb_alloc_cmap(&info->cmap, 256, 0)) < 0) {
301		printk(KERN_ERR "efifb: cannot allocate colormap\n");
302		goto err_unmap;
303	}
304	if ((err = register_framebuffer(info)) < 0) {
305		printk(KERN_ERR "efifb: cannot register framebuffer\n");
306		goto err_fb_dealoc;
307	}
308	fb_info(info, "%s frame buffer device\n", info->fix.id);
309	return 0;
310
311err_fb_dealoc:
312	fb_dealloc_cmap(&info->cmap);
313err_unmap:
314	iounmap(info->screen_base);
315err_release_fb:
316	framebuffer_release(info);
317err_release_mem:
318	if (request_mem_succeeded)
319		release_mem_region(efifb_fix.smem_start, size_total);
320	return err;
321}
322
323static int efifb_remove(struct platform_device *pdev)
324{
325	struct fb_info *info = platform_get_drvdata(pdev);
326
327	unregister_framebuffer(info);
328	framebuffer_release(info);
329
330	return 0;
331}
332
333static struct platform_driver efifb_driver = {
334	.driver = {
335		.name = "efi-framebuffer",
336	},
337	.probe = efifb_probe,
338	.remove = efifb_remove,
339};
340
341module_platform_driver(efifb_driver);
342MODULE_LICENSE("GPL");
343