xref: /openbmc/linux/drivers/video/fbdev/efifb.c (revision 905e46ac)
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/kernel.h>
10 #include <linux/efi.h>
11 #include <linux/errno.h>
12 #include <linux/fb.h>
13 #include <linux/pci.h>
14 #include <linux/platform_device.h>
15 #include <linux/screen_info.h>
16 #include <video/vga.h>
17 #include <asm/efi.h>
18 
19 static bool request_mem_succeeded = false;
20 
21 static struct fb_var_screeninfo efifb_defined = {
22 	.activate		= FB_ACTIVATE_NOW,
23 	.height			= -1,
24 	.width			= -1,
25 	.right_margin		= 32,
26 	.upper_margin		= 16,
27 	.lower_margin		= 4,
28 	.vsync_len		= 4,
29 	.vmode			= FB_VMODE_NONINTERLACED,
30 };
31 
32 static struct fb_fix_screeninfo efifb_fix = {
33 	.id			= "EFI VGA",
34 	.type			= FB_TYPE_PACKED_PIXELS,
35 	.accel			= FB_ACCEL_NONE,
36 	.visual			= FB_VISUAL_TRUECOLOR,
37 };
38 
39 static int efifb_setcolreg(unsigned regno, unsigned red, unsigned green,
40 			   unsigned blue, unsigned transp,
41 			   struct fb_info *info)
42 {
43 	/*
44 	 *  Set a single color register. The values supplied are
45 	 *  already rounded down to the hardware's capabilities
46 	 *  (according to the entries in the `var' structure). Return
47 	 *  != 0 for invalid regno.
48 	 */
49 
50 	if (regno >= info->cmap.len)
51 		return 1;
52 
53 	if (regno < 16) {
54 		red   >>= 16 - info->var.red.length;
55 		green >>= 16 - info->var.green.length;
56 		blue  >>= 16 - info->var.blue.length;
57 		((u32 *)(info->pseudo_palette))[regno] =
58 			(red   << info->var.red.offset)   |
59 			(green << info->var.green.offset) |
60 			(blue  << info->var.blue.offset);
61 	}
62 	return 0;
63 }
64 
65 static void efifb_destroy(struct fb_info *info)
66 {
67 	if (info->screen_base)
68 		iounmap(info->screen_base);
69 	if (request_mem_succeeded)
70 		release_mem_region(info->apertures->ranges[0].base,
71 				   info->apertures->ranges[0].size);
72 	fb_dealloc_cmap(&info->cmap);
73 }
74 
75 static struct fb_ops efifb_ops = {
76 	.owner		= THIS_MODULE,
77 	.fb_destroy	= efifb_destroy,
78 	.fb_setcolreg	= efifb_setcolreg,
79 	.fb_fillrect	= cfb_fillrect,
80 	.fb_copyarea	= cfb_copyarea,
81 	.fb_imageblit	= cfb_imageblit,
82 };
83 
84 static int efifb_setup(char *options)
85 {
86 	char *this_opt;
87 
88 	if (options && *options) {
89 		while ((this_opt = strsep(&options, ",")) != NULL) {
90 			if (!*this_opt) continue;
91 
92 			efifb_setup_from_dmi(&screen_info, this_opt);
93 
94 			if (!strncmp(this_opt, "base:", 5))
95 				screen_info.lfb_base = simple_strtoul(this_opt+5, NULL, 0);
96 			else if (!strncmp(this_opt, "stride:", 7))
97 				screen_info.lfb_linelength = simple_strtoul(this_opt+7, NULL, 0) * 4;
98 			else if (!strncmp(this_opt, "height:", 7))
99 				screen_info.lfb_height = simple_strtoul(this_opt+7, NULL, 0);
100 			else if (!strncmp(this_opt, "width:", 6))
101 				screen_info.lfb_width = simple_strtoul(this_opt+6, NULL, 0);
102 		}
103 	}
104 
105 	return 0;
106 }
107 
108 static inline bool fb_base_is_valid(void)
109 {
110 	if (screen_info.lfb_base)
111 		return true;
112 
113 	if (!(screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE))
114 		return false;
115 
116 	if (screen_info.ext_lfb_base)
117 		return true;
118 
119 	return false;
120 }
121 
122 #define efifb_attr_decl(name, fmt)					\
123 static ssize_t name##_show(struct device *dev,				\
124 			   struct device_attribute *attr,		\
125 			   char *buf)					\
126 {									\
127 	return sprintf(buf, fmt "\n", (screen_info.lfb_##name));	\
128 }									\
129 static DEVICE_ATTR_RO(name)
130 
131 efifb_attr_decl(base, "0x%x");
132 efifb_attr_decl(linelength, "%u");
133 efifb_attr_decl(height, "%u");
134 efifb_attr_decl(width, "%u");
135 efifb_attr_decl(depth, "%u");
136 
137 static struct attribute *efifb_attrs[] = {
138 	&dev_attr_base.attr,
139 	&dev_attr_linelength.attr,
140 	&dev_attr_width.attr,
141 	&dev_attr_height.attr,
142 	&dev_attr_depth.attr,
143 	NULL
144 };
145 ATTRIBUTE_GROUPS(efifb);
146 
147 static bool pci_dev_disabled;	/* FB base matches BAR of a disabled device */
148 
149 static int efifb_probe(struct platform_device *dev)
150 {
151 	struct fb_info *info;
152 	int err;
153 	unsigned int size_vmode;
154 	unsigned int size_remap;
155 	unsigned int size_total;
156 	char *option = NULL;
157 
158 	if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI || pci_dev_disabled)
159 		return -ENODEV;
160 
161 	if (fb_get_options("efifb", &option))
162 		return -ENODEV;
163 	efifb_setup(option);
164 
165 	/* We don't get linelength from UGA Draw Protocol, only from
166 	 * EFI Graphics Protocol.  So if it's not in DMI, and it's not
167 	 * passed in from the user, we really can't use the framebuffer.
168 	 */
169 	if (!screen_info.lfb_linelength)
170 		return -ENODEV;
171 
172 	if (!screen_info.lfb_depth)
173 		screen_info.lfb_depth = 32;
174 	if (!screen_info.pages)
175 		screen_info.pages = 1;
176 	if (!fb_base_is_valid()) {
177 		printk(KERN_DEBUG "efifb: invalid framebuffer address\n");
178 		return -ENODEV;
179 	}
180 	printk(KERN_INFO "efifb: probing for efifb\n");
181 
182 	/* just assume they're all unset if any are */
183 	if (!screen_info.blue_size) {
184 		screen_info.blue_size = 8;
185 		screen_info.blue_pos = 0;
186 		screen_info.green_size = 8;
187 		screen_info.green_pos = 8;
188 		screen_info.red_size = 8;
189 		screen_info.red_pos = 16;
190 		screen_info.rsvd_size = 8;
191 		screen_info.rsvd_pos = 24;
192 	}
193 
194 	efifb_fix.smem_start = screen_info.lfb_base;
195 
196 	if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE) {
197 		u64 ext_lfb_base;
198 
199 		ext_lfb_base = (u64)(unsigned long)screen_info.ext_lfb_base << 32;
200 		efifb_fix.smem_start |= ext_lfb_base;
201 	}
202 
203 	efifb_defined.bits_per_pixel = screen_info.lfb_depth;
204 	efifb_defined.xres = screen_info.lfb_width;
205 	efifb_defined.yres = screen_info.lfb_height;
206 	efifb_fix.line_length = screen_info.lfb_linelength;
207 
208 	/*   size_vmode -- that is the amount of memory needed for the
209 	 *                 used video mode, i.e. the minimum amount of
210 	 *                 memory we need. */
211 	size_vmode = efifb_defined.yres * efifb_fix.line_length;
212 
213 	/*   size_total -- all video memory we have. Used for
214 	 *                 entries, ressource allocation and bounds
215 	 *                 checking. */
216 	size_total = screen_info.lfb_size;
217 	if (size_total < size_vmode)
218 		size_total = size_vmode;
219 
220 	/*   size_remap -- the amount of video memory we are going to
221 	 *                 use for efifb.  With modern cards it is no
222 	 *                 option to simply use size_total as that
223 	 *                 wastes plenty of kernel address space. */
224 	size_remap  = size_vmode * 2;
225 	if (size_remap > size_total)
226 		size_remap = size_total;
227 	if (size_remap % PAGE_SIZE)
228 		size_remap += PAGE_SIZE - (size_remap % PAGE_SIZE);
229 	efifb_fix.smem_len = size_remap;
230 
231 	if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) {
232 		request_mem_succeeded = true;
233 	} else {
234 		/* We cannot make this fatal. Sometimes this comes from magic
235 		   spaces our resource handlers simply don't know about */
236 		pr_warn("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 		pr_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 		pr_err("efifb: abort, cannot ioremap video memory 0x%x @ 0x%lx\n",
261 			efifb_fix.smem_len, efifb_fix.smem_start);
262 		err = -EIO;
263 		goto err_release_fb;
264 	}
265 
266 	pr_info("efifb: framebuffer at 0x%lx, using %dk, total %dk\n",
267 	       efifb_fix.smem_start, size_remap/1024, size_total/1024);
268 	pr_info("efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
269 	       efifb_defined.xres, efifb_defined.yres,
270 	       efifb_defined.bits_per_pixel, efifb_fix.line_length,
271 	       screen_info.pages);
272 
273 	efifb_defined.xres_virtual = efifb_defined.xres;
274 	efifb_defined.yres_virtual = efifb_fix.smem_len /
275 					efifb_fix.line_length;
276 	pr_info("efifb: scrolling: redraw\n");
277 	efifb_defined.yres_virtual = efifb_defined.yres;
278 
279 	/* some dummy values for timing to make fbset happy */
280 	efifb_defined.pixclock     = 10000000 / efifb_defined.xres *
281 					1000 / efifb_defined.yres;
282 	efifb_defined.left_margin  = (efifb_defined.xres / 8) & 0xf8;
283 	efifb_defined.hsync_len    = (efifb_defined.xres / 8) & 0xf8;
284 
285 	efifb_defined.red.offset    = screen_info.red_pos;
286 	efifb_defined.red.length    = screen_info.red_size;
287 	efifb_defined.green.offset  = screen_info.green_pos;
288 	efifb_defined.green.length  = screen_info.green_size;
289 	efifb_defined.blue.offset   = screen_info.blue_pos;
290 	efifb_defined.blue.length   = screen_info.blue_size;
291 	efifb_defined.transp.offset = screen_info.rsvd_pos;
292 	efifb_defined.transp.length = screen_info.rsvd_size;
293 
294 	pr_info("efifb: %s: "
295 	       "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
296 	       "Truecolor",
297 	       screen_info.rsvd_size,
298 	       screen_info.red_size,
299 	       screen_info.green_size,
300 	       screen_info.blue_size,
301 	       screen_info.rsvd_pos,
302 	       screen_info.red_pos,
303 	       screen_info.green_pos,
304 	       screen_info.blue_pos);
305 
306 	efifb_fix.ypanstep  = 0;
307 	efifb_fix.ywrapstep = 0;
308 
309 	info->fbops = &efifb_ops;
310 	info->var = efifb_defined;
311 	info->fix = efifb_fix;
312 	info->flags = FBINFO_FLAG_DEFAULT | FBINFO_MISC_FIRMWARE;
313 
314 	err = sysfs_create_groups(&dev->dev.kobj, efifb_groups);
315 	if (err) {
316 		pr_err("efifb: cannot add sysfs attrs\n");
317 		goto err_unmap;
318 	}
319 	err = fb_alloc_cmap(&info->cmap, 256, 0);
320 	if (err < 0) {
321 		pr_err("efifb: cannot allocate colormap\n");
322 		goto err_groups;
323 	}
324 	err = register_framebuffer(info);
325 	if (err < 0) {
326 		pr_err("efifb: cannot register framebuffer\n");
327 		goto err_fb_dealoc;
328 	}
329 	fb_info(info, "%s frame buffer device\n", info->fix.id);
330 	return 0;
331 
332 err_fb_dealoc:
333 	fb_dealloc_cmap(&info->cmap);
334 err_groups:
335 	sysfs_remove_groups(&dev->dev.kobj, efifb_groups);
336 err_unmap:
337 	iounmap(info->screen_base);
338 err_release_fb:
339 	framebuffer_release(info);
340 err_release_mem:
341 	if (request_mem_succeeded)
342 		release_mem_region(efifb_fix.smem_start, size_total);
343 	return err;
344 }
345 
346 static int efifb_remove(struct platform_device *pdev)
347 {
348 	struct fb_info *info = platform_get_drvdata(pdev);
349 
350 	unregister_framebuffer(info);
351 	sysfs_remove_groups(&pdev->dev.kobj, efifb_groups);
352 	framebuffer_release(info);
353 
354 	return 0;
355 }
356 
357 static struct platform_driver efifb_driver = {
358 	.driver = {
359 		.name = "efi-framebuffer",
360 	},
361 	.probe = efifb_probe,
362 	.remove = efifb_remove,
363 };
364 
365 builtin_platform_driver(efifb_driver);
366 
367 #if defined(CONFIG_PCI) && !defined(CONFIG_X86)
368 
369 static bool pci_bar_found;	/* did we find a BAR matching the efifb base? */
370 
371 static void claim_efifb_bar(struct pci_dev *dev, int idx)
372 {
373 	u16 word;
374 
375 	pci_bar_found = true;
376 
377 	pci_read_config_word(dev, PCI_COMMAND, &word);
378 	if (!(word & PCI_COMMAND_MEMORY)) {
379 		pci_dev_disabled = true;
380 		dev_err(&dev->dev,
381 			"BAR %d: assigned to efifb but device is disabled!\n",
382 			idx);
383 		return;
384 	}
385 
386 	if (pci_claim_resource(dev, idx)) {
387 		pci_dev_disabled = true;
388 		dev_err(&dev->dev,
389 			"BAR %d: failed to claim resource for efifb!\n", idx);
390 		return;
391 	}
392 
393 	dev_info(&dev->dev, "BAR %d: assigned to efifb\n", idx);
394 }
395 
396 static void efifb_fixup_resources(struct pci_dev *dev)
397 {
398 	u64 base = screen_info.lfb_base;
399 	u64 size = screen_info.lfb_size;
400 	int i;
401 
402 	if (pci_bar_found || screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
403 		return;
404 
405 	if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
406 		base |= (u64)screen_info.ext_lfb_base << 32;
407 
408 	if (!base)
409 		return;
410 
411 	for (i = 0; i < PCI_STD_RESOURCE_END; i++) {
412 		struct resource *res = &dev->resource[i];
413 
414 		if (!(res->flags & IORESOURCE_MEM))
415 			continue;
416 
417 		if (res->start <= base && res->end >= base + size - 1) {
418 			claim_efifb_bar(dev, i);
419 			break;
420 		}
421 	}
422 }
423 DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY,
424 			       16, efifb_fixup_resources);
425 
426 #endif
427