Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 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> |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 15 | #include <linux/dmi.h> |
Peter Jones | 85a00d9 | 2010-09-22 13:05:04 -0700 | [diff] [blame] | 16 | #include <linux/pci.h> |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 17 | #include <video/vga.h> |
David Herrmann | 2995e50 | 2013-08-02 14:05:23 +0200 | [diff] [blame] | 18 | #include <asm/sysfb.h> |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 19 | |
Andy Lutomirski | da0241f | 2011-05-26 10:13:32 -0400 | [diff] [blame] | 20 | static bool request_mem_succeeded = false; |
| 21 | |
Matthew Garrett | b4aa016 | 2012-04-16 16:26:05 -0400 | [diff] [blame] | 22 | static struct pci_dev *default_vga; |
| 23 | |
Greg Kroah-Hartman | 48c68c4 | 2012-12-21 13:07:39 -0800 | [diff] [blame] | 24 | static struct fb_var_screeninfo efifb_defined = { |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 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 | |
Greg Kroah-Hartman | 48c68c4 | 2012-12-21 13:07:39 -0800 | [diff] [blame] | 35 | static struct fb_fix_screeninfo efifb_fix = { |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 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 | |
Marcin Slusarz | 89f3f21 | 2010-02-22 12:44:22 -0800 | [diff] [blame] | 68 | static void efifb_destroy(struct fb_info *info) |
| 69 | { |
| 70 | if (info->screen_base) |
| 71 | iounmap(info->screen_base); |
Andy Lutomirski | da0241f | 2011-05-26 10:13:32 -0400 | [diff] [blame] | 72 | if (request_mem_succeeded) |
| 73 | release_mem_region(info->apertures->ranges[0].base, |
| 74 | info->apertures->ranges[0].size); |
Peter Jones | 907355a | 2013-07-25 11:48:11 -0400 | [diff] [blame] | 75 | fb_dealloc_cmap(&info->cmap); |
Marcin Slusarz | 89f3f21 | 2010-02-22 12:44:22 -0800 | [diff] [blame] | 76 | framebuffer_release(info); |
| 77 | } |
| 78 | |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 79 | static struct fb_ops efifb_ops = { |
| 80 | .owner = THIS_MODULE, |
Marcin Slusarz | 89f3f21 | 2010-02-22 12:44:22 -0800 | [diff] [blame] | 81 | .fb_destroy = efifb_destroy, |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 82 | .fb_setcolreg = efifb_setcolreg, |
| 83 | .fb_fillrect = cfb_fillrect, |
| 84 | .fb_copyarea = cfb_copyarea, |
| 85 | .fb_imageblit = cfb_imageblit, |
| 86 | }; |
| 87 | |
Matthew Garrett | b4aa016 | 2012-04-16 16:26:05 -0400 | [diff] [blame] | 88 | struct pci_dev *vga_default_device(void) |
| 89 | { |
| 90 | return default_vga; |
| 91 | } |
| 92 | |
Matthew Garrett | 1b23170 | 2012-04-24 09:31:28 +0100 | [diff] [blame] | 93 | EXPORT_SYMBOL_GPL(vga_default_device); |
| 94 | |
Matthew Garrett | b4aa016 | 2012-04-16 16:26:05 -0400 | [diff] [blame] | 95 | void vga_set_default_device(struct pci_dev *pdev) |
| 96 | { |
| 97 | default_vga = pdev; |
| 98 | } |
| 99 | |
David Herrmann | e6816a8 | 2013-08-02 14:05:26 +0200 | [diff] [blame] | 100 | static int efifb_setup(char *options) |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 101 | { |
| 102 | char *this_opt; |
| 103 | int i; |
Matthew Garrett | b4aa016 | 2012-04-16 16:26:05 -0400 | [diff] [blame] | 104 | struct pci_dev *dev = NULL; |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 105 | |
Matthew Garrett | b4aa016 | 2012-04-16 16:26:05 -0400 | [diff] [blame] | 106 | if (options && *options) { |
| 107 | while ((this_opt = strsep(&options, ",")) != NULL) { |
| 108 | if (!*this_opt) continue; |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 109 | |
Matthew Garrett | b4aa016 | 2012-04-16 16:26:05 -0400 | [diff] [blame] | 110 | for (i = 0; i < M_UNKNOWN; i++) { |
David Herrmann | 2995e50 | 2013-08-02 14:05:23 +0200 | [diff] [blame] | 111 | if (!strcmp(this_opt, efifb_dmi_list[i].optname) && |
| 112 | efifb_dmi_list[i].base != 0) { |
| 113 | screen_info.lfb_base = efifb_dmi_list[i].base; |
| 114 | screen_info.lfb_linelength = efifb_dmi_list[i].stride; |
| 115 | screen_info.lfb_width = efifb_dmi_list[i].width; |
| 116 | screen_info.lfb_height = efifb_dmi_list[i].height; |
Matthew Garrett | b4aa016 | 2012-04-16 16:26:05 -0400 | [diff] [blame] | 117 | } |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 118 | } |
Matthew Garrett | b4aa016 | 2012-04-16 16:26:05 -0400 | [diff] [blame] | 119 | if (!strncmp(this_opt, "base:", 5)) |
| 120 | screen_info.lfb_base = simple_strtoul(this_opt+5, NULL, 0); |
| 121 | else if (!strncmp(this_opt, "stride:", 7)) |
| 122 | screen_info.lfb_linelength = simple_strtoul(this_opt+7, NULL, 0) * 4; |
| 123 | else if (!strncmp(this_opt, "height:", 7)) |
| 124 | screen_info.lfb_height = simple_strtoul(this_opt+7, NULL, 0); |
| 125 | else if (!strncmp(this_opt, "width:", 6)) |
| 126 | screen_info.lfb_width = simple_strtoul(this_opt+6, NULL, 0); |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 127 | } |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 128 | } |
Matthew Garrett | b4aa016 | 2012-04-16 16:26:05 -0400 | [diff] [blame] | 129 | |
| 130 | for_each_pci_dev(dev) { |
| 131 | int i; |
| 132 | |
| 133 | if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA) |
| 134 | continue; |
| 135 | |
| 136 | for (i=0; i < DEVICE_COUNT_RESOURCE; i++) { |
| 137 | resource_size_t start, end; |
| 138 | |
| 139 | if (!(pci_resource_flags(dev, i) & IORESOURCE_MEM)) |
| 140 | continue; |
| 141 | |
| 142 | start = pci_resource_start(dev, i); |
| 143 | end = pci_resource_end(dev, i); |
| 144 | |
| 145 | if (!start || !end) |
| 146 | continue; |
| 147 | |
| 148 | if (screen_info.lfb_base >= start && |
| 149 | (screen_info.lfb_base + screen_info.lfb_size) < end) |
| 150 | default_vga = dev; |
| 151 | } |
| 152 | } |
| 153 | |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | |
David Herrmann | e6816a8 | 2013-08-02 14:05:26 +0200 | [diff] [blame] | 157 | static int efifb_probe(struct platform_device *dev) |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 158 | { |
| 159 | struct fb_info *info; |
| 160 | int err; |
| 161 | unsigned int size_vmode; |
| 162 | unsigned int size_remap; |
| 163 | unsigned int size_total; |
David Herrmann | e6816a8 | 2013-08-02 14:05:26 +0200 | [diff] [blame] | 164 | char *option = NULL; |
| 165 | |
| 166 | if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI) |
| 167 | return -ENODEV; |
| 168 | |
| 169 | if (fb_get_options("efifb", &option)) |
| 170 | return -ENODEV; |
| 171 | efifb_setup(option); |
| 172 | |
| 173 | /* We don't get linelength from UGA Draw Protocol, only from |
| 174 | * EFI Graphics Protocol. So if it's not in DMI, and it's not |
| 175 | * passed in from the user, we really can't use the framebuffer. |
| 176 | */ |
| 177 | if (!screen_info.lfb_linelength) |
| 178 | return -ENODEV; |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 179 | |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 180 | if (!screen_info.lfb_depth) |
| 181 | screen_info.lfb_depth = 32; |
| 182 | if (!screen_info.pages) |
| 183 | screen_info.pages = 1; |
Matthew Garrett | 133bb07 | 2009-04-13 14:39:44 -0700 | [diff] [blame] | 184 | if (!screen_info.lfb_base) { |
| 185 | printk(KERN_DEBUG "efifb: invalid framebuffer address\n"); |
| 186 | return -ENODEV; |
| 187 | } |
| 188 | printk(KERN_INFO "efifb: probing for efifb\n"); |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 189 | |
| 190 | /* just assume they're all unset if any are */ |
| 191 | if (!screen_info.blue_size) { |
| 192 | screen_info.blue_size = 8; |
| 193 | screen_info.blue_pos = 0; |
| 194 | screen_info.green_size = 8; |
| 195 | screen_info.green_pos = 8; |
| 196 | screen_info.red_size = 8; |
| 197 | screen_info.red_pos = 16; |
| 198 | screen_info.rsvd_size = 8; |
| 199 | screen_info.rsvd_pos = 24; |
| 200 | } |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 201 | |
| 202 | efifb_fix.smem_start = screen_info.lfb_base; |
| 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; |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 225 | if (size_remap > size_total) |
| 226 | size_remap = size_total; |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 227 | if (size_remap % PAGE_SIZE) |
| 228 | size_remap += PAGE_SIZE - (size_remap % PAGE_SIZE); |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 229 | efifb_fix.smem_len = size_remap; |
| 230 | |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 231 | if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) { |
Andy Lutomirski | da0241f | 2011-05-26 10:13:32 -0400 | [diff] [blame] | 232 | request_mem_succeeded = true; |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 233 | } else { |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 234 | /* We cannot make this fatal. Sometimes this comes from magic |
| 235 | spaces our resource handlers simply don't know about */ |
| 236 | printk(KERN_WARNING |
| 237 | "efifb: cannot reserve video memory at 0x%lx\n", |
| 238 | efifb_fix.smem_start); |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 239 | } |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 240 | |
| 241 | info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev); |
| 242 | if (!info) { |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 243 | printk(KERN_ERR "efifb: cannot allocate framebuffer\n"); |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 244 | err = -ENOMEM; |
| 245 | goto err_release_mem; |
| 246 | } |
| 247 | info->pseudo_palette = info->par; |
| 248 | info->par = NULL; |
| 249 | |
Marcin Slusarz | 1471ca9 | 2010-05-16 17:27:03 +0200 | [diff] [blame] | 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; |
Dave Airlie | 4410f39 | 2009-06-16 15:34:38 -0700 | [diff] [blame] | 257 | |
Andy Lutomirski | 3c004b4f | 2011-05-26 10:13:31 -0400 | [diff] [blame] | 258 | info->screen_base = ioremap_wc(efifb_fix.smem_start, efifb_fix.smem_len); |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 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; |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 264 | goto err_release_fb; |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 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; |
Dave Airlie | 4410f39 | 2009-06-16 15:34:38 -0700 | [diff] [blame] | 315 | info->flags = FBINFO_FLAG_DEFAULT | FBINFO_MISC_FIRMWARE; |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 316 | |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 317 | if ((err = fb_alloc_cmap(&info->cmap, 256, 0)) < 0) { |
| 318 | printk(KERN_ERR "efifb: cannot allocate colormap\n"); |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 319 | goto err_unmap; |
| 320 | } |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 321 | if ((err = register_framebuffer(info)) < 0) { |
| 322 | printk(KERN_ERR "efifb: cannot register framebuffer\n"); |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 323 | goto err_fb_dealoc; |
| 324 | } |
| 325 | printk(KERN_INFO "fb%d: %s frame buffer device\n", |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 326 | info->node, info->fix.id); |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 327 | return 0; |
| 328 | |
| 329 | err_fb_dealoc: |
| 330 | fb_dealloc_cmap(&info->cmap); |
| 331 | err_unmap: |
| 332 | iounmap(info->screen_base); |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 333 | err_release_fb: |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 334 | framebuffer_release(info); |
| 335 | err_release_mem: |
Andy Lutomirski | da0241f | 2011-05-26 10:13:32 -0400 | [diff] [blame] | 336 | if (request_mem_succeeded) |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 337 | release_mem_region(efifb_fix.smem_start, size_total); |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 338 | return err; |
| 339 | } |
| 340 | |
| 341 | static struct platform_driver efifb_driver = { |
David Herrmann | e6816a8 | 2013-08-02 14:05:26 +0200 | [diff] [blame] | 342 | .driver = { |
| 343 | .name = "efi-framebuffer", |
| 344 | .owner = THIS_MODULE, |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 345 | }, |
David Herrmann | e6816a8 | 2013-08-02 14:05:26 +0200 | [diff] [blame] | 346 | .probe = efifb_probe, |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 347 | }; |
| 348 | |
David Herrmann | e6816a8 | 2013-08-02 14:05:26 +0200 | [diff] [blame] | 349 | module_platform_driver(efifb_driver); |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 350 | MODULE_LICENSE("GPL"); |