• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
efifb_setcolreg(unsigned regno,unsigned red,unsigned green,unsigned blue,unsigned transp,struct fb_info * info)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 
efifb_destroy(struct fb_info * info)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 
efifb_setup(char * options)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 
fb_base_is_valid(void)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 static bool pci_dev_disabled;	/* FB base matches BAR of a disabled device */
123 
efifb_probe(struct platform_device * dev)124 static int efifb_probe(struct platform_device *dev)
125 {
126 	struct fb_info *info;
127 	int err;
128 	unsigned int size_vmode;
129 	unsigned int size_remap;
130 	unsigned int size_total;
131 	char *option = NULL;
132 
133 	if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI || pci_dev_disabled)
134 		return -ENODEV;
135 
136 	if (fb_get_options("efifb", &option))
137 		return -ENODEV;
138 	efifb_setup(option);
139 
140 	/* We don't get linelength from UGA Draw Protocol, only from
141 	 * EFI Graphics Protocol.  So if it's not in DMI, and it's not
142 	 * passed in from the user, we really can't use the framebuffer.
143 	 */
144 	if (!screen_info.lfb_linelength)
145 		return -ENODEV;
146 
147 	if (!screen_info.lfb_depth)
148 		screen_info.lfb_depth = 32;
149 	if (!screen_info.pages)
150 		screen_info.pages = 1;
151 	if (!fb_base_is_valid()) {
152 		printk(KERN_DEBUG "efifb: invalid framebuffer address\n");
153 		return -ENODEV;
154 	}
155 	printk(KERN_INFO "efifb: probing for efifb\n");
156 
157 	/* just assume they're all unset if any are */
158 	if (!screen_info.blue_size) {
159 		screen_info.blue_size = 8;
160 		screen_info.blue_pos = 0;
161 		screen_info.green_size = 8;
162 		screen_info.green_pos = 8;
163 		screen_info.red_size = 8;
164 		screen_info.red_pos = 16;
165 		screen_info.rsvd_size = 8;
166 		screen_info.rsvd_pos = 24;
167 	}
168 
169 	efifb_fix.smem_start = screen_info.lfb_base;
170 
171 	if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE) {
172 		u64 ext_lfb_base;
173 
174 		ext_lfb_base = (u64)(unsigned long)screen_info.ext_lfb_base << 32;
175 		efifb_fix.smem_start |= ext_lfb_base;
176 	}
177 
178 	efifb_defined.bits_per_pixel = screen_info.lfb_depth;
179 	efifb_defined.xres = screen_info.lfb_width;
180 	efifb_defined.yres = screen_info.lfb_height;
181 	efifb_fix.line_length = screen_info.lfb_linelength;
182 
183 	/*   size_vmode -- that is the amount of memory needed for the
184 	 *                 used video mode, i.e. the minimum amount of
185 	 *                 memory we need. */
186 	size_vmode = efifb_defined.yres * efifb_fix.line_length;
187 
188 	/*   size_total -- all video memory we have. Used for
189 	 *                 entries, ressource allocation and bounds
190 	 *                 checking. */
191 	size_total = screen_info.lfb_size;
192 	if (size_total < size_vmode)
193 		size_total = size_vmode;
194 
195 	/*   size_remap -- the amount of video memory we are going to
196 	 *                 use for efifb.  With modern cards it is no
197 	 *                 option to simply use size_total as that
198 	 *                 wastes plenty of kernel address space. */
199 	size_remap  = size_vmode * 2;
200 	if (size_remap > size_total)
201 		size_remap = size_total;
202 	if (size_remap % PAGE_SIZE)
203 		size_remap += PAGE_SIZE - (size_remap % PAGE_SIZE);
204 	efifb_fix.smem_len = size_remap;
205 
206 	if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) {
207 		request_mem_succeeded = true;
208 	} else {
209 		/* We cannot make this fatal. Sometimes this comes from magic
210 		   spaces our resource handlers simply don't know about */
211 		printk(KERN_WARNING
212 		       "efifb: cannot reserve video memory at 0x%lx\n",
213 			efifb_fix.smem_start);
214 	}
215 
216 	info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
217 	if (!info) {
218 		printk(KERN_ERR "efifb: cannot allocate framebuffer\n");
219 		err = -ENOMEM;
220 		goto err_release_mem;
221 	}
222 	platform_set_drvdata(dev, info);
223 	info->pseudo_palette = info->par;
224 	info->par = NULL;
225 
226 	info->apertures = alloc_apertures(1);
227 	if (!info->apertures) {
228 		err = -ENOMEM;
229 		goto err_release_fb;
230 	}
231 	info->apertures->ranges[0].base = efifb_fix.smem_start;
232 	info->apertures->ranges[0].size = size_remap;
233 
234 	info->screen_base = ioremap_wc(efifb_fix.smem_start, efifb_fix.smem_len);
235 	if (!info->screen_base) {
236 		printk(KERN_ERR "efifb: abort, cannot ioremap video memory "
237 				"0x%x @ 0x%lx\n",
238 			efifb_fix.smem_len, efifb_fix.smem_start);
239 		err = -EIO;
240 		goto err_release_fb;
241 	}
242 
243 	printk(KERN_INFO "efifb: framebuffer at 0x%lx, using %dk, total %dk\n",
244 	       efifb_fix.smem_start, size_remap/1024, size_total/1024);
245 	printk(KERN_INFO "efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
246 	       efifb_defined.xres, efifb_defined.yres,
247 	       efifb_defined.bits_per_pixel, efifb_fix.line_length,
248 	       screen_info.pages);
249 
250 	efifb_defined.xres_virtual = efifb_defined.xres;
251 	efifb_defined.yres_virtual = efifb_fix.smem_len /
252 					efifb_fix.line_length;
253 	printk(KERN_INFO "efifb: scrolling: redraw\n");
254 	efifb_defined.yres_virtual = efifb_defined.yres;
255 
256 	/* some dummy values for timing to make fbset happy */
257 	efifb_defined.pixclock     = 10000000 / efifb_defined.xres *
258 					1000 / efifb_defined.yres;
259 	efifb_defined.left_margin  = (efifb_defined.xres / 8) & 0xf8;
260 	efifb_defined.hsync_len    = (efifb_defined.xres / 8) & 0xf8;
261 
262 	efifb_defined.red.offset    = screen_info.red_pos;
263 	efifb_defined.red.length    = screen_info.red_size;
264 	efifb_defined.green.offset  = screen_info.green_pos;
265 	efifb_defined.green.length  = screen_info.green_size;
266 	efifb_defined.blue.offset   = screen_info.blue_pos;
267 	efifb_defined.blue.length   = screen_info.blue_size;
268 	efifb_defined.transp.offset = screen_info.rsvd_pos;
269 	efifb_defined.transp.length = screen_info.rsvd_size;
270 
271 	printk(KERN_INFO "efifb: %s: "
272 	       "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
273 	       "Truecolor",
274 	       screen_info.rsvd_size,
275 	       screen_info.red_size,
276 	       screen_info.green_size,
277 	       screen_info.blue_size,
278 	       screen_info.rsvd_pos,
279 	       screen_info.red_pos,
280 	       screen_info.green_pos,
281 	       screen_info.blue_pos);
282 
283 	efifb_fix.ypanstep  = 0;
284 	efifb_fix.ywrapstep = 0;
285 
286 	info->fbops = &efifb_ops;
287 	info->var = efifb_defined;
288 	info->fix = efifb_fix;
289 	info->flags = FBINFO_FLAG_DEFAULT | FBINFO_MISC_FIRMWARE;
290 
291 	if ((err = fb_alloc_cmap(&info->cmap, 256, 0)) < 0) {
292 		printk(KERN_ERR "efifb: cannot allocate colormap\n");
293 		goto err_unmap;
294 	}
295 	if ((err = register_framebuffer(info)) < 0) {
296 		printk(KERN_ERR "efifb: cannot register framebuffer\n");
297 		goto err_fb_dealoc;
298 	}
299 	fb_info(info, "%s frame buffer device\n", info->fix.id);
300 	return 0;
301 
302 err_fb_dealoc:
303 	fb_dealloc_cmap(&info->cmap);
304 err_unmap:
305 	iounmap(info->screen_base);
306 err_release_fb:
307 	framebuffer_release(info);
308 err_release_mem:
309 	if (request_mem_succeeded)
310 		release_mem_region(efifb_fix.smem_start, size_total);
311 	return err;
312 }
313 
efifb_remove(struct platform_device * pdev)314 static int efifb_remove(struct platform_device *pdev)
315 {
316 	struct fb_info *info = platform_get_drvdata(pdev);
317 
318 	unregister_framebuffer(info);
319 	framebuffer_release(info);
320 
321 	return 0;
322 }
323 
324 static struct platform_driver efifb_driver = {
325 	.driver = {
326 		.name = "efi-framebuffer",
327 	},
328 	.probe = efifb_probe,
329 	.remove = efifb_remove,
330 };
331 
332 builtin_platform_driver(efifb_driver);
333 
334 #if defined(CONFIG_PCI) && !defined(CONFIG_X86)
335 
336 static bool pci_bar_found;	/* did we find a BAR matching the efifb base? */
337 
claim_efifb_bar(struct pci_dev * dev,int idx)338 static void claim_efifb_bar(struct pci_dev *dev, int idx)
339 {
340 	u16 word;
341 
342 	pci_bar_found = true;
343 
344 	pci_read_config_word(dev, PCI_COMMAND, &word);
345 	if (!(word & PCI_COMMAND_MEMORY)) {
346 		pci_dev_disabled = true;
347 		dev_err(&dev->dev,
348 			"BAR %d: assigned to efifb but device is disabled!\n",
349 			idx);
350 		return;
351 	}
352 
353 	if (pci_claim_resource(dev, idx)) {
354 		pci_dev_disabled = true;
355 		dev_err(&dev->dev,
356 			"BAR %d: failed to claim resource for efifb!\n", idx);
357 		return;
358 	}
359 
360 	dev_info(&dev->dev, "BAR %d: assigned to efifb\n", idx);
361 }
362 
efifb_fixup_resources(struct pci_dev * dev)363 static void efifb_fixup_resources(struct pci_dev *dev)
364 {
365 	u64 base = screen_info.lfb_base;
366 	u64 size = screen_info.lfb_size;
367 	int i;
368 
369 	if (pci_bar_found || screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
370 		return;
371 
372 	if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
373 		base |= (u64)screen_info.ext_lfb_base << 32;
374 
375 	if (!base)
376 		return;
377 
378 	for (i = 0; i < PCI_STD_RESOURCE_END; i++) {
379 		struct resource *res = &dev->resource[i];
380 
381 		if (!(res->flags & IORESOURCE_MEM))
382 			continue;
383 
384 		if (res->start <= base && res->end >= base + size - 1) {
385 			claim_efifb_bar(dev, i);
386 			break;
387 		}
388 	}
389 }
390 DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY,
391 			       16, efifb_fixup_resources);
392 
393 #endif
394