2 * Framebuffer driver for EFI/UEFI based system
4 * (c) 2006 Edgar Hucek <gimli@dark-green.com>
5 * Original efi driver written by Gerd Knorr <kraxel@goldbach.in-berlin.de>
9 #include <linux/kernel.h>
10 #include <linux/efi.h>
11 #include <linux/errno.h>
13 #include <linux/platform_device.h>
14 #include <linux/screen_info.h>
15 #include <video/vga.h>
18 static bool request_mem_succeeded
= false;
20 static struct fb_var_screeninfo efifb_defined
= {
21 .activate
= FB_ACTIVATE_NOW
,
28 .vmode
= FB_VMODE_NONINTERLACED
,
31 static struct fb_fix_screeninfo efifb_fix
= {
33 .type
= FB_TYPE_PACKED_PIXELS
,
34 .accel
= FB_ACCEL_NONE
,
35 .visual
= FB_VISUAL_TRUECOLOR
,
38 static int efifb_setcolreg(unsigned regno
, unsigned red
, unsigned green
,
39 unsigned blue
, unsigned transp
,
43 * Set a single color register. The values supplied are
44 * already rounded down to the hardware's capabilities
45 * (according to the entries in the `var' structure). Return
46 * != 0 for invalid regno.
49 if (regno
>= info
->cmap
.len
)
53 red
>>= 16 - info
->var
.red
.length
;
54 green
>>= 16 - info
->var
.green
.length
;
55 blue
>>= 16 - info
->var
.blue
.length
;
56 ((u32
*)(info
->pseudo_palette
))[regno
] =
57 (red
<< info
->var
.red
.offset
) |
58 (green
<< info
->var
.green
.offset
) |
59 (blue
<< info
->var
.blue
.offset
);
64 static void efifb_destroy(struct fb_info
*info
)
66 if (info
->screen_base
)
67 iounmap(info
->screen_base
);
68 if (request_mem_succeeded
)
69 release_mem_region(info
->apertures
->ranges
[0].base
,
70 info
->apertures
->ranges
[0].size
);
71 fb_dealloc_cmap(&info
->cmap
);
74 static struct fb_ops efifb_ops
= {
76 .fb_destroy
= efifb_destroy
,
77 .fb_setcolreg
= efifb_setcolreg
,
78 .fb_fillrect
= cfb_fillrect
,
79 .fb_copyarea
= cfb_copyarea
,
80 .fb_imageblit
= cfb_imageblit
,
83 static int efifb_setup(char *options
)
87 if (options
&& *options
) {
88 while ((this_opt
= strsep(&options
, ",")) != NULL
) {
89 if (!*this_opt
) continue;
91 efifb_setup_from_dmi(&screen_info
, this_opt
);
93 if (!strncmp(this_opt
, "base:", 5))
94 screen_info
.lfb_base
= simple_strtoul(this_opt
+5, NULL
, 0);
95 else if (!strncmp(this_opt
, "stride:", 7))
96 screen_info
.lfb_linelength
= simple_strtoul(this_opt
+7, NULL
, 0) * 4;
97 else if (!strncmp(this_opt
, "height:", 7))
98 screen_info
.lfb_height
= simple_strtoul(this_opt
+7, NULL
, 0);
99 else if (!strncmp(this_opt
, "width:", 6))
100 screen_info
.lfb_width
= simple_strtoul(this_opt
+6, NULL
, 0);
107 static inline bool fb_base_is_valid(void)
109 if (screen_info
.lfb_base
)
112 if (!(screen_info
.capabilities
& VIDEO_CAPABILITY_64BIT_BASE
))
115 if (screen_info
.ext_lfb_base
)
121 #define efifb_attr_decl(name, fmt) \
122 static ssize_t name##_show(struct device *dev, \
123 struct device_attribute *attr, \
126 return sprintf(buf, fmt "\n", (screen_info.lfb_##name)); \
128 static DEVICE_ATTR_RO(name)
130 efifb_attr_decl(base
, "0x%x");
131 efifb_attr_decl(linelength
, "%u");
132 efifb_attr_decl(height
, "%u");
133 efifb_attr_decl(width
, "%u");
134 efifb_attr_decl(depth
, "%u");
136 static struct attribute
*efifb_attrs
[] = {
138 &dev_attr_linelength
.attr
,
139 &dev_attr_width
.attr
,
140 &dev_attr_height
.attr
,
141 &dev_attr_depth
.attr
,
144 ATTRIBUTE_GROUPS(efifb
);
146 static int efifb_probe(struct platform_device
*dev
)
148 struct fb_info
*info
;
150 unsigned int size_vmode
;
151 unsigned int size_remap
;
152 unsigned int size_total
;
155 if (screen_info
.orig_video_isVGA
!= VIDEO_TYPE_EFI
)
158 if (fb_get_options("efifb", &option
))
162 /* We don't get linelength from UGA Draw Protocol, only from
163 * EFI Graphics Protocol. So if it's not in DMI, and it's not
164 * passed in from the user, we really can't use the framebuffer.
166 if (!screen_info
.lfb_linelength
)
169 if (!screen_info
.lfb_depth
)
170 screen_info
.lfb_depth
= 32;
171 if (!screen_info
.pages
)
172 screen_info
.pages
= 1;
173 if (!fb_base_is_valid()) {
174 printk(KERN_DEBUG
"efifb: invalid framebuffer address\n");
177 printk(KERN_INFO
"efifb: probing for efifb\n");
179 /* just assume they're all unset if any are */
180 if (!screen_info
.blue_size
) {
181 screen_info
.blue_size
= 8;
182 screen_info
.blue_pos
= 0;
183 screen_info
.green_size
= 8;
184 screen_info
.green_pos
= 8;
185 screen_info
.red_size
= 8;
186 screen_info
.red_pos
= 16;
187 screen_info
.rsvd_size
= 8;
188 screen_info
.rsvd_pos
= 24;
191 efifb_fix
.smem_start
= screen_info
.lfb_base
;
193 if (screen_info
.capabilities
& VIDEO_CAPABILITY_64BIT_BASE
) {
196 ext_lfb_base
= (u64
)(unsigned long)screen_info
.ext_lfb_base
<< 32;
197 efifb_fix
.smem_start
|= ext_lfb_base
;
200 efifb_defined
.bits_per_pixel
= screen_info
.lfb_depth
;
201 efifb_defined
.xres
= screen_info
.lfb_width
;
202 efifb_defined
.yres
= screen_info
.lfb_height
;
203 efifb_fix
.line_length
= screen_info
.lfb_linelength
;
205 /* size_vmode -- that is the amount of memory needed for the
206 * used video mode, i.e. the minimum amount of
208 size_vmode
= efifb_defined
.yres
* efifb_fix
.line_length
;
210 /* size_total -- all video memory we have. Used for
211 * entries, ressource allocation and bounds
213 size_total
= screen_info
.lfb_size
;
214 if (size_total
< size_vmode
)
215 size_total
= size_vmode
;
217 /* size_remap -- the amount of video memory we are going to
218 * use for efifb. With modern cards it is no
219 * option to simply use size_total as that
220 * wastes plenty of kernel address space. */
221 size_remap
= size_vmode
* 2;
222 if (size_remap
> size_total
)
223 size_remap
= size_total
;
224 if (size_remap
% PAGE_SIZE
)
225 size_remap
+= PAGE_SIZE
- (size_remap
% PAGE_SIZE
);
226 efifb_fix
.smem_len
= size_remap
;
228 if (request_mem_region(efifb_fix
.smem_start
, size_remap
, "efifb")) {
229 request_mem_succeeded
= true;
231 /* We cannot make this fatal. Sometimes this comes from magic
232 spaces our resource handlers simply don't know about */
233 pr_warn("efifb: cannot reserve video memory at 0x%lx\n",
234 efifb_fix
.smem_start
);
237 info
= framebuffer_alloc(sizeof(u32
) * 16, &dev
->dev
);
239 pr_err("efifb: cannot allocate framebuffer\n");
241 goto err_release_mem
;
243 platform_set_drvdata(dev
, info
);
244 info
->pseudo_palette
= info
->par
;
247 info
->apertures
= alloc_apertures(1);
248 if (!info
->apertures
) {
252 info
->apertures
->ranges
[0].base
= efifb_fix
.smem_start
;
253 info
->apertures
->ranges
[0].size
= size_remap
;
255 info
->screen_base
= ioremap_wc(efifb_fix
.smem_start
, efifb_fix
.smem_len
);
256 if (!info
->screen_base
) {
257 pr_err("efifb: abort, cannot ioremap video memory 0x%x @ 0x%lx\n",
258 efifb_fix
.smem_len
, efifb_fix
.smem_start
);
263 pr_info("efifb: framebuffer at 0x%lx, using %dk, total %dk\n",
264 efifb_fix
.smem_start
, size_remap
/1024, size_total
/1024);
265 pr_info("efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
266 efifb_defined
.xres
, efifb_defined
.yres
,
267 efifb_defined
.bits_per_pixel
, efifb_fix
.line_length
,
270 efifb_defined
.xres_virtual
= efifb_defined
.xres
;
271 efifb_defined
.yres_virtual
= efifb_fix
.smem_len
/
272 efifb_fix
.line_length
;
273 pr_info("efifb: scrolling: redraw\n");
274 efifb_defined
.yres_virtual
= efifb_defined
.yres
;
276 /* some dummy values for timing to make fbset happy */
277 efifb_defined
.pixclock
= 10000000 / efifb_defined
.xres
*
278 1000 / efifb_defined
.yres
;
279 efifb_defined
.left_margin
= (efifb_defined
.xres
/ 8) & 0xf8;
280 efifb_defined
.hsync_len
= (efifb_defined
.xres
/ 8) & 0xf8;
282 efifb_defined
.red
.offset
= screen_info
.red_pos
;
283 efifb_defined
.red
.length
= screen_info
.red_size
;
284 efifb_defined
.green
.offset
= screen_info
.green_pos
;
285 efifb_defined
.green
.length
= screen_info
.green_size
;
286 efifb_defined
.blue
.offset
= screen_info
.blue_pos
;
287 efifb_defined
.blue
.length
= screen_info
.blue_size
;
288 efifb_defined
.transp
.offset
= screen_info
.rsvd_pos
;
289 efifb_defined
.transp
.length
= screen_info
.rsvd_size
;
291 pr_info("efifb: %s: "
292 "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
294 screen_info
.rsvd_size
,
295 screen_info
.red_size
,
296 screen_info
.green_size
,
297 screen_info
.blue_size
,
298 screen_info
.rsvd_pos
,
300 screen_info
.green_pos
,
301 screen_info
.blue_pos
);
303 efifb_fix
.ypanstep
= 0;
304 efifb_fix
.ywrapstep
= 0;
306 info
->fbops
= &efifb_ops
;
307 info
->var
= efifb_defined
;
308 info
->fix
= efifb_fix
;
309 info
->flags
= FBINFO_FLAG_DEFAULT
| FBINFO_MISC_FIRMWARE
;
311 err
= sysfs_create_groups(&dev
->dev
.kobj
, efifb_groups
);
313 pr_err("efifb: cannot add sysfs attrs\n");
316 err
= fb_alloc_cmap(&info
->cmap
, 256, 0);
318 pr_err("efifb: cannot allocate colormap\n");
321 err
= register_framebuffer(info
);
323 pr_err("efifb: cannot register framebuffer\n");
326 fb_info(info
, "%s frame buffer device\n", info
->fix
.id
);
330 fb_dealloc_cmap(&info
->cmap
);
332 sysfs_remove_groups(&dev
->dev
.kobj
, efifb_groups
);
334 iounmap(info
->screen_base
);
336 framebuffer_release(info
);
338 if (request_mem_succeeded
)
339 release_mem_region(efifb_fix
.smem_start
, size_total
);
343 static int efifb_remove(struct platform_device
*pdev
)
345 struct fb_info
*info
= platform_get_drvdata(pdev
);
347 unregister_framebuffer(info
);
348 sysfs_remove_groups(&pdev
->dev
.kobj
, efifb_groups
);
349 framebuffer_release(info
);
354 static struct platform_driver efifb_driver
= {
356 .name
= "efi-framebuffer",
358 .probe
= efifb_probe
,
359 .remove
= efifb_remove
,
362 builtin_platform_driver(efifb_driver
);