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