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