Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Framebuffer driver for EFI/UEFI based system |
| 4 | * |
| 5 | * (c) 2006 Edgar Hucek <gimli@dark-green.com> |
| 6 | * Original efi driver written by Gerd Knorr <kraxel@goldbach.in-berlin.de> |
| 7 | * |
| 8 | */ |
| 9 | |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 10 | #include <linux/kernel.h> |
Ard Biesheuvel | 21289ec | 2016-04-25 21:06:50 +0100 | [diff] [blame] | 11 | #include <linux/efi.h> |
Hans de Goede | 34db50e | 2018-07-03 17:43:10 +0200 | [diff] [blame] | 12 | #include <linux/efi-bgrt.h> |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 13 | #include <linux/errno.h> |
| 14 | #include <linux/fb.h> |
Ard Biesheuvel | 55d728a | 2017-04-04 16:27:44 +0100 | [diff] [blame] | 15 | #include <linux/pci.h> |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 16 | #include <linux/platform_device.h> |
Hans de Goede | 34db50e | 2018-07-03 17:43:10 +0200 | [diff] [blame] | 17 | #include <linux/printk.h> |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 18 | #include <linux/screen_info.h> |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 19 | #include <video/vga.h> |
Ard Biesheuvel | 21289ec | 2016-04-25 21:06:50 +0100 | [diff] [blame] | 20 | #include <asm/efi.h> |
Hans de Goede | 028b186 | 2017-11-25 20:35:52 +0100 | [diff] [blame] | 21 | #include <drm/drm_utils.h> /* For drm_get_panel_orientation_quirk */ |
| 22 | #include <drm/drm_connector.h> /* For DRM_MODE_PANEL_ORIENTATION_* */ |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 23 | |
Hans de Goede | 34db50e | 2018-07-03 17:43:10 +0200 | [diff] [blame] | 24 | struct bmp_file_header { |
| 25 | u16 id; |
| 26 | u32 file_size; |
| 27 | u32 reserved; |
| 28 | u32 bitmap_offset; |
| 29 | } __packed; |
| 30 | |
| 31 | struct bmp_dib_header { |
| 32 | u32 dib_header_size; |
| 33 | s32 width; |
| 34 | s32 height; |
| 35 | u16 planes; |
| 36 | u16 bpp; |
| 37 | u32 compression; |
| 38 | u32 bitmap_size; |
| 39 | u32 horz_resolution; |
| 40 | u32 vert_resolution; |
| 41 | u32 colors_used; |
| 42 | u32 colors_important; |
| 43 | } __packed; |
| 44 | |
Hans de Goede | cf7389b | 2018-09-26 18:11:22 +0200 | [diff] [blame] | 45 | static bool use_bgrt = true; |
Andy Lutomirski | da0241f | 2011-05-26 10:13:32 -0400 | [diff] [blame] | 46 | static bool request_mem_succeeded = false; |
Ard Biesheuvel | 38ac028 | 2018-07-11 11:40:40 +0200 | [diff] [blame] | 47 | static u64 mem_flags = EFI_MEMORY_WC | EFI_MEMORY_UC; |
Andy Lutomirski | da0241f | 2011-05-26 10:13:32 -0400 | [diff] [blame] | 48 | |
Greg Kroah-Hartman | 48c68c4 | 2012-12-21 13:07:39 -0800 | [diff] [blame] | 49 | static struct fb_var_screeninfo efifb_defined = { |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 50 | .activate = FB_ACTIVATE_NOW, |
| 51 | .height = -1, |
| 52 | .width = -1, |
| 53 | .right_margin = 32, |
| 54 | .upper_margin = 16, |
| 55 | .lower_margin = 4, |
| 56 | .vsync_len = 4, |
| 57 | .vmode = FB_VMODE_NONINTERLACED, |
| 58 | }; |
| 59 | |
Greg Kroah-Hartman | 48c68c4 | 2012-12-21 13:07:39 -0800 | [diff] [blame] | 60 | static struct fb_fix_screeninfo efifb_fix = { |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 61 | .id = "EFI VGA", |
| 62 | .type = FB_TYPE_PACKED_PIXELS, |
| 63 | .accel = FB_ACCEL_NONE, |
| 64 | .visual = FB_VISUAL_TRUECOLOR, |
| 65 | }; |
| 66 | |
| 67 | static int efifb_setcolreg(unsigned regno, unsigned red, unsigned green, |
| 68 | unsigned blue, unsigned transp, |
| 69 | struct fb_info *info) |
| 70 | { |
| 71 | /* |
| 72 | * Set a single color register. The values supplied are |
| 73 | * already rounded down to the hardware's capabilities |
| 74 | * (according to the entries in the `var' structure). Return |
| 75 | * != 0 for invalid regno. |
| 76 | */ |
| 77 | |
| 78 | if (regno >= info->cmap.len) |
| 79 | return 1; |
| 80 | |
| 81 | if (regno < 16) { |
Max Staudt | d50b3f4 | 2016-06-13 19:15:59 +0200 | [diff] [blame] | 82 | red >>= 16 - info->var.red.length; |
| 83 | green >>= 16 - info->var.green.length; |
| 84 | blue >>= 16 - info->var.blue.length; |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 85 | ((u32 *)(info->pseudo_palette))[regno] = |
| 86 | (red << info->var.red.offset) | |
| 87 | (green << info->var.green.offset) | |
| 88 | (blue << info->var.blue.offset); |
| 89 | } |
| 90 | return 0; |
| 91 | } |
| 92 | |
Hans de Goede | 34db50e | 2018-07-03 17:43:10 +0200 | [diff] [blame] | 93 | /* |
| 94 | * If fbcon deffered console takeover is configured, the intent is for the |
| 95 | * framebuffer to show the boot graphics (e.g. vendor logo) until there is some |
| 96 | * (error) message to display. But the boot graphics may have been destroyed by |
| 97 | * e.g. option ROM output, detect this and restore the boot graphics. |
| 98 | */ |
| 99 | #if defined CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER && \ |
| 100 | defined CONFIG_ACPI_BGRT |
| 101 | static void efifb_copy_bmp(u8 *src, u32 *dst, int width, struct screen_info *si) |
| 102 | { |
| 103 | u8 r, g, b; |
| 104 | |
| 105 | while (width--) { |
| 106 | b = *src++; |
| 107 | g = *src++; |
| 108 | r = *src++; |
| 109 | *dst++ = (r << si->red_pos) | |
| 110 | (g << si->green_pos) | |
| 111 | (b << si->blue_pos); |
| 112 | } |
| 113 | } |
| 114 | |
Hans de Goede | 88fe4ce | 2018-07-24 19:11:28 +0200 | [diff] [blame] | 115 | #ifdef CONFIG_X86 |
| 116 | /* |
| 117 | * On x86 some firmwares use a low non native resolution for the display when |
| 118 | * they have shown some text messages. While keeping the bgrt filled with info |
| 119 | * for the native resolution. If the bgrt image intended for the native |
| 120 | * resolution still fits, it will be displayed very close to the right edge of |
| 121 | * the display looking quite bad. This function checks for this. |
| 122 | */ |
| 123 | static bool efifb_bgrt_sanity_check(struct screen_info *si, u32 bmp_width) |
| 124 | { |
| 125 | static const int default_resolutions[][2] = { |
| 126 | { 800, 600 }, |
| 127 | { 1024, 768 }, |
| 128 | { 1280, 1024 }, |
| 129 | }; |
| 130 | u32 i, right_margin; |
| 131 | |
| 132 | for (i = 0; i < ARRAY_SIZE(default_resolutions); i++) { |
| 133 | if (default_resolutions[i][0] == si->lfb_width && |
| 134 | default_resolutions[i][1] == si->lfb_height) |
| 135 | break; |
| 136 | } |
| 137 | /* If not a default resolution used for textmode, this should be fine */ |
| 138 | if (i >= ARRAY_SIZE(default_resolutions)) |
| 139 | return true; |
| 140 | |
| 141 | /* If the right margin is 5 times smaller then the left one, reject */ |
| 142 | right_margin = si->lfb_width - (bgrt_tab.image_offset_x + bmp_width); |
| 143 | if (right_margin < (bgrt_tab.image_offset_x / 5)) |
| 144 | return false; |
| 145 | |
| 146 | return true; |
| 147 | } |
| 148 | #else |
| 149 | static bool efifb_bgrt_sanity_check(struct screen_info *si, u32 bmp_width) |
| 150 | { |
| 151 | return true; |
| 152 | } |
| 153 | #endif |
| 154 | |
Hans de Goede | 34db50e | 2018-07-03 17:43:10 +0200 | [diff] [blame] | 155 | static void efifb_show_boot_graphics(struct fb_info *info) |
| 156 | { |
| 157 | u32 bmp_width, bmp_height, bmp_pitch, screen_pitch, dst_x, y, src_y; |
| 158 | struct screen_info *si = &screen_info; |
| 159 | struct bmp_file_header *file_header; |
| 160 | struct bmp_dib_header *dib_header; |
| 161 | void *bgrt_image = NULL; |
| 162 | u8 *dst = info->screen_base; |
| 163 | |
Hans de Goede | cf7389b | 2018-09-26 18:11:22 +0200 | [diff] [blame] | 164 | if (!use_bgrt) |
| 165 | return; |
| 166 | |
Hans de Goede | 34db50e | 2018-07-03 17:43:10 +0200 | [diff] [blame] | 167 | if (!bgrt_tab.image_address) { |
| 168 | pr_info("efifb: No BGRT, not showing boot graphics\n"); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | /* Avoid flashing the logo if we're going to print std probe messages */ |
| 173 | if (console_loglevel > CONSOLE_LOGLEVEL_QUIET) |
| 174 | return; |
| 175 | |
| 176 | /* bgrt_tab.status is unreliable, so we don't check it */ |
| 177 | |
| 178 | if (si->lfb_depth != 32) { |
| 179 | pr_info("efifb: not 32 bits, not showing boot graphics\n"); |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | bgrt_image = memremap(bgrt_tab.image_address, bgrt_image_size, |
| 184 | MEMREMAP_WB); |
| 185 | if (!bgrt_image) { |
| 186 | pr_warn("efifb: Ignoring BGRT: failed to map image memory\n"); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | if (bgrt_image_size < (sizeof(*file_header) + sizeof(*dib_header))) |
| 191 | goto error; |
| 192 | |
| 193 | file_header = bgrt_image; |
| 194 | if (file_header->id != 0x4d42 || file_header->reserved != 0) |
| 195 | goto error; |
| 196 | |
| 197 | dib_header = bgrt_image + sizeof(*file_header); |
| 198 | if (dib_header->dib_header_size != 40 || dib_header->width < 0 || |
| 199 | dib_header->planes != 1 || dib_header->bpp != 24 || |
| 200 | dib_header->compression != 0) |
| 201 | goto error; |
| 202 | |
| 203 | bmp_width = dib_header->width; |
| 204 | bmp_height = abs(dib_header->height); |
| 205 | bmp_pitch = round_up(3 * bmp_width, 4); |
| 206 | screen_pitch = si->lfb_linelength; |
| 207 | |
| 208 | if ((file_header->bitmap_offset + bmp_pitch * bmp_height) > |
| 209 | bgrt_image_size) |
| 210 | goto error; |
| 211 | |
| 212 | if ((bgrt_tab.image_offset_x + bmp_width) > si->lfb_width || |
| 213 | (bgrt_tab.image_offset_y + bmp_height) > si->lfb_height) |
| 214 | goto error; |
| 215 | |
Hans de Goede | 88fe4ce | 2018-07-24 19:11:28 +0200 | [diff] [blame] | 216 | if (!efifb_bgrt_sanity_check(si, bmp_width)) |
| 217 | goto error; |
| 218 | |
Hans de Goede | 34db50e | 2018-07-03 17:43:10 +0200 | [diff] [blame] | 219 | pr_info("efifb: showing boot graphics\n"); |
| 220 | |
| 221 | for (y = 0; y < si->lfb_height; y++, dst += si->lfb_linelength) { |
| 222 | /* Only background? */ |
| 223 | if (y < bgrt_tab.image_offset_y || |
| 224 | y >= (bgrt_tab.image_offset_y + bmp_height)) { |
| 225 | memset(dst, 0, 4 * si->lfb_width); |
| 226 | continue; |
| 227 | } |
| 228 | |
| 229 | src_y = y - bgrt_tab.image_offset_y; |
| 230 | /* Positive header height means upside down row order */ |
| 231 | if (dib_header->height > 0) |
| 232 | src_y = (bmp_height - 1) - src_y; |
| 233 | |
| 234 | memset(dst, 0, bgrt_tab.image_offset_x * 4); |
| 235 | dst_x = bgrt_tab.image_offset_x; |
| 236 | efifb_copy_bmp(bgrt_image + file_header->bitmap_offset + |
| 237 | src_y * bmp_pitch, |
| 238 | (u32 *)dst + dst_x, bmp_width, si); |
| 239 | dst_x += bmp_width; |
| 240 | memset((u32 *)dst + dst_x, 0, (si->lfb_width - dst_x) * 4); |
| 241 | } |
| 242 | |
| 243 | memunmap(bgrt_image); |
| 244 | return; |
| 245 | |
| 246 | error: |
| 247 | memunmap(bgrt_image); |
| 248 | pr_warn("efifb: Ignoring BGRT: unexpected or invalid BMP data\n"); |
| 249 | } |
| 250 | #else |
| 251 | static inline void efifb_show_boot_graphics(struct fb_info *info) {} |
| 252 | #endif |
| 253 | |
Marcin Slusarz | 89f3f21 | 2010-02-22 12:44:22 -0800 | [diff] [blame] | 254 | static void efifb_destroy(struct fb_info *info) |
| 255 | { |
Ard Biesheuvel | 38ac028 | 2018-07-11 11:40:40 +0200 | [diff] [blame] | 256 | if (info->screen_base) { |
| 257 | if (mem_flags & (EFI_MEMORY_UC | EFI_MEMORY_WC)) |
| 258 | iounmap(info->screen_base); |
| 259 | else |
| 260 | memunmap(info->screen_base); |
| 261 | } |
Andy Lutomirski | da0241f | 2011-05-26 10:13:32 -0400 | [diff] [blame] | 262 | if (request_mem_succeeded) |
| 263 | release_mem_region(info->apertures->ranges[0].base, |
| 264 | info->apertures->ranges[0].size); |
Peter Jones | 907355a | 2013-07-25 11:48:11 -0400 | [diff] [blame] | 265 | fb_dealloc_cmap(&info->cmap); |
Marcin Slusarz | 89f3f21 | 2010-02-22 12:44:22 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 268 | static struct fb_ops efifb_ops = { |
| 269 | .owner = THIS_MODULE, |
Marcin Slusarz | 89f3f21 | 2010-02-22 12:44:22 -0800 | [diff] [blame] | 270 | .fb_destroy = efifb_destroy, |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 271 | .fb_setcolreg = efifb_setcolreg, |
| 272 | .fb_fillrect = cfb_fillrect, |
| 273 | .fb_copyarea = cfb_copyarea, |
| 274 | .fb_imageblit = cfb_imageblit, |
| 275 | }; |
| 276 | |
David Herrmann | e6816a8 | 2013-08-02 14:05:26 +0200 | [diff] [blame] | 277 | static int efifb_setup(char *options) |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 278 | { |
| 279 | char *this_opt; |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 280 | |
Matthew Garrett | b4aa016 | 2012-04-16 16:26:05 -0400 | [diff] [blame] | 281 | if (options && *options) { |
| 282 | while ((this_opt = strsep(&options, ",")) != NULL) { |
| 283 | if (!*this_opt) continue; |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 284 | |
Ard Biesheuvel | 21289ec | 2016-04-25 21:06:50 +0100 | [diff] [blame] | 285 | efifb_setup_from_dmi(&screen_info, this_opt); |
| 286 | |
Matthew Garrett | b4aa016 | 2012-04-16 16:26:05 -0400 | [diff] [blame] | 287 | if (!strncmp(this_opt, "base:", 5)) |
| 288 | screen_info.lfb_base = simple_strtoul(this_opt+5, NULL, 0); |
| 289 | else if (!strncmp(this_opt, "stride:", 7)) |
| 290 | screen_info.lfb_linelength = simple_strtoul(this_opt+7, NULL, 0) * 4; |
| 291 | else if (!strncmp(this_opt, "height:", 7)) |
| 292 | screen_info.lfb_height = simple_strtoul(this_opt+7, NULL, 0); |
| 293 | else if (!strncmp(this_opt, "width:", 6)) |
| 294 | screen_info.lfb_width = simple_strtoul(this_opt+6, NULL, 0); |
Dave Airlie | dd0c41f | 2017-07-31 18:45:41 +0200 | [diff] [blame] | 295 | else if (!strcmp(this_opt, "nowc")) |
Ard Biesheuvel | 38ac028 | 2018-07-11 11:40:40 +0200 | [diff] [blame] | 296 | mem_flags &= ~EFI_MEMORY_WC; |
Hans de Goede | cf7389b | 2018-09-26 18:11:22 +0200 | [diff] [blame] | 297 | else if (!strcmp(this_opt, "nobgrt")) |
| 298 | use_bgrt = false; |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 299 | } |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 300 | } |
Matthew Garrett | b4aa016 | 2012-04-16 16:26:05 -0400 | [diff] [blame] | 301 | |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 302 | return 0; |
| 303 | } |
| 304 | |
Matt Fleming | ae2ee62 | 2015-08-25 16:32:55 +0100 | [diff] [blame] | 305 | static inline bool fb_base_is_valid(void) |
| 306 | { |
| 307 | if (screen_info.lfb_base) |
| 308 | return true; |
| 309 | |
| 310 | if (!(screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)) |
| 311 | return false; |
| 312 | |
| 313 | if (screen_info.ext_lfb_base) |
| 314 | return true; |
| 315 | |
| 316 | return false; |
| 317 | } |
| 318 | |
Peter Jones | 753375a | 2016-10-18 15:33:17 +0100 | [diff] [blame] | 319 | #define efifb_attr_decl(name, fmt) \ |
| 320 | static ssize_t name##_show(struct device *dev, \ |
| 321 | struct device_attribute *attr, \ |
| 322 | char *buf) \ |
| 323 | { \ |
| 324 | return sprintf(buf, fmt "\n", (screen_info.lfb_##name)); \ |
| 325 | } \ |
| 326 | static DEVICE_ATTR_RO(name) |
| 327 | |
| 328 | efifb_attr_decl(base, "0x%x"); |
| 329 | efifb_attr_decl(linelength, "%u"); |
| 330 | efifb_attr_decl(height, "%u"); |
| 331 | efifb_attr_decl(width, "%u"); |
| 332 | efifb_attr_decl(depth, "%u"); |
| 333 | |
| 334 | static struct attribute *efifb_attrs[] = { |
| 335 | &dev_attr_base.attr, |
| 336 | &dev_attr_linelength.attr, |
| 337 | &dev_attr_width.attr, |
| 338 | &dev_attr_height.attr, |
| 339 | &dev_attr_depth.attr, |
| 340 | NULL |
| 341 | }; |
| 342 | ATTRIBUTE_GROUPS(efifb); |
| 343 | |
Ard Biesheuvel | 55d728a | 2017-04-04 16:27:44 +0100 | [diff] [blame] | 344 | static bool pci_dev_disabled; /* FB base matches BAR of a disabled device */ |
| 345 | |
Ard Biesheuvel | dcf8f5c | 2017-08-18 20:49:40 +0100 | [diff] [blame] | 346 | static struct pci_dev *efifb_pci_dev; /* dev with BAR covering the efifb */ |
| 347 | static struct resource *bar_resource; |
| 348 | static u64 bar_offset; |
| 349 | |
David Herrmann | e6816a8 | 2013-08-02 14:05:26 +0200 | [diff] [blame] | 350 | static int efifb_probe(struct platform_device *dev) |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 351 | { |
| 352 | struct fb_info *info; |
Hans de Goede | 028b186 | 2017-11-25 20:35:52 +0100 | [diff] [blame] | 353 | int err, orientation; |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 354 | unsigned int size_vmode; |
| 355 | unsigned int size_remap; |
| 356 | unsigned int size_total; |
David Herrmann | e6816a8 | 2013-08-02 14:05:26 +0200 | [diff] [blame] | 357 | char *option = NULL; |
Ard Biesheuvel | 38ac028 | 2018-07-11 11:40:40 +0200 | [diff] [blame] | 358 | efi_memory_desc_t md; |
David Herrmann | e6816a8 | 2013-08-02 14:05:26 +0200 | [diff] [blame] | 359 | |
Ard Biesheuvel | 55d728a | 2017-04-04 16:27:44 +0100 | [diff] [blame] | 360 | if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI || pci_dev_disabled) |
David Herrmann | e6816a8 | 2013-08-02 14:05:26 +0200 | [diff] [blame] | 361 | return -ENODEV; |
| 362 | |
| 363 | if (fb_get_options("efifb", &option)) |
| 364 | return -ENODEV; |
| 365 | efifb_setup(option); |
| 366 | |
| 367 | /* We don't get linelength from UGA Draw Protocol, only from |
| 368 | * EFI Graphics Protocol. So if it's not in DMI, and it's not |
| 369 | * passed in from the user, we really can't use the framebuffer. |
| 370 | */ |
| 371 | if (!screen_info.lfb_linelength) |
| 372 | return -ENODEV; |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 373 | |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 374 | if (!screen_info.lfb_depth) |
| 375 | screen_info.lfb_depth = 32; |
| 376 | if (!screen_info.pages) |
| 377 | screen_info.pages = 1; |
Matt Fleming | ae2ee62 | 2015-08-25 16:32:55 +0100 | [diff] [blame] | 378 | if (!fb_base_is_valid()) { |
Matthew Garrett | 133bb07 | 2009-04-13 14:39:44 -0700 | [diff] [blame] | 379 | printk(KERN_DEBUG "efifb: invalid framebuffer address\n"); |
| 380 | return -ENODEV; |
| 381 | } |
| 382 | printk(KERN_INFO "efifb: probing for efifb\n"); |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 383 | |
| 384 | /* just assume they're all unset if any are */ |
| 385 | if (!screen_info.blue_size) { |
| 386 | screen_info.blue_size = 8; |
| 387 | screen_info.blue_pos = 0; |
| 388 | screen_info.green_size = 8; |
| 389 | screen_info.green_pos = 8; |
| 390 | screen_info.red_size = 8; |
| 391 | screen_info.red_pos = 16; |
| 392 | screen_info.rsvd_size = 8; |
| 393 | screen_info.rsvd_pos = 24; |
| 394 | } |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 395 | |
| 396 | efifb_fix.smem_start = screen_info.lfb_base; |
Matt Fleming | ae2ee62 | 2015-08-25 16:32:55 +0100 | [diff] [blame] | 397 | |
| 398 | if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE) { |
| 399 | u64 ext_lfb_base; |
| 400 | |
| 401 | ext_lfb_base = (u64)(unsigned long)screen_info.ext_lfb_base << 32; |
| 402 | efifb_fix.smem_start |= ext_lfb_base; |
| 403 | } |
| 404 | |
Ard Biesheuvel | dcf8f5c | 2017-08-18 20:49:40 +0100 | [diff] [blame] | 405 | if (bar_resource && |
| 406 | bar_resource->start + bar_offset != efifb_fix.smem_start) { |
| 407 | dev_info(&efifb_pci_dev->dev, |
| 408 | "BAR has moved, updating efifb address\n"); |
| 409 | efifb_fix.smem_start = bar_resource->start + bar_offset; |
| 410 | } |
| 411 | |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 412 | efifb_defined.bits_per_pixel = screen_info.lfb_depth; |
| 413 | efifb_defined.xres = screen_info.lfb_width; |
| 414 | efifb_defined.yres = screen_info.lfb_height; |
| 415 | efifb_fix.line_length = screen_info.lfb_linelength; |
| 416 | |
| 417 | /* size_vmode -- that is the amount of memory needed for the |
| 418 | * used video mode, i.e. the minimum amount of |
| 419 | * memory we need. */ |
| 420 | size_vmode = efifb_defined.yres * efifb_fix.line_length; |
| 421 | |
| 422 | /* size_total -- all video memory we have. Used for |
| 423 | * entries, ressource allocation and bounds |
| 424 | * checking. */ |
| 425 | size_total = screen_info.lfb_size; |
| 426 | if (size_total < size_vmode) |
| 427 | size_total = size_vmode; |
| 428 | |
| 429 | /* size_remap -- the amount of video memory we are going to |
| 430 | * use for efifb. With modern cards it is no |
| 431 | * option to simply use size_total as that |
| 432 | * wastes plenty of kernel address space. */ |
| 433 | size_remap = size_vmode * 2; |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 434 | if (size_remap > size_total) |
| 435 | size_remap = size_total; |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 436 | if (size_remap % PAGE_SIZE) |
| 437 | size_remap += PAGE_SIZE - (size_remap % PAGE_SIZE); |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 438 | efifb_fix.smem_len = size_remap; |
| 439 | |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 440 | if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) { |
Andy Lutomirski | da0241f | 2011-05-26 10:13:32 -0400 | [diff] [blame] | 441 | request_mem_succeeded = true; |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 442 | } else { |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 443 | /* We cannot make this fatal. Sometimes this comes from magic |
| 444 | spaces our resource handlers simply don't know about */ |
Peter Jones | 753375a | 2016-10-18 15:33:17 +0100 | [diff] [blame] | 445 | pr_warn("efifb: cannot reserve video memory at 0x%lx\n", |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 446 | efifb_fix.smem_start); |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 447 | } |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 448 | |
| 449 | info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev); |
| 450 | if (!info) { |
Peter Jones | 753375a | 2016-10-18 15:33:17 +0100 | [diff] [blame] | 451 | pr_err("efifb: cannot allocate framebuffer\n"); |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 452 | err = -ENOMEM; |
| 453 | goto err_release_mem; |
| 454 | } |
David Herrmann | 65b4021 | 2014-01-23 15:14:55 +0100 | [diff] [blame] | 455 | platform_set_drvdata(dev, info); |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 456 | info->pseudo_palette = info->par; |
| 457 | info->par = NULL; |
| 458 | |
Marcin Slusarz | 1471ca9 | 2010-05-16 17:27:03 +0200 | [diff] [blame] | 459 | info->apertures = alloc_apertures(1); |
| 460 | if (!info->apertures) { |
| 461 | err = -ENOMEM; |
| 462 | goto err_release_fb; |
| 463 | } |
| 464 | info->apertures->ranges[0].base = efifb_fix.smem_start; |
| 465 | info->apertures->ranges[0].size = size_remap; |
Dave Airlie | 4410f39 | 2009-06-16 15:34:38 -0700 | [diff] [blame] | 466 | |
Ard Biesheuvel | c2999c2 | 2019-03-28 20:34:26 +0100 | [diff] [blame^] | 467 | if (efi_enabled(EFI_BOOT) && |
| 468 | !efi_mem_desc_lookup(efifb_fix.smem_start, &md)) { |
Ard Biesheuvel | 38ac028 | 2018-07-11 11:40:40 +0200 | [diff] [blame] | 469 | if ((efifb_fix.smem_start + efifb_fix.smem_len) > |
| 470 | (md.phys_addr + (md.num_pages << EFI_PAGE_SHIFT))) { |
| 471 | pr_err("efifb: video memory @ 0x%lx spans multiple EFI memory regions\n", |
| 472 | efifb_fix.smem_start); |
| 473 | err = -EIO; |
| 474 | goto err_release_fb; |
| 475 | } |
| 476 | /* |
| 477 | * If the UEFI memory map covers the efifb region, we may only |
| 478 | * remap it using the attributes the memory map prescribes. |
| 479 | */ |
| 480 | mem_flags |= EFI_MEMORY_WT | EFI_MEMORY_WB; |
| 481 | mem_flags &= md.attribute; |
| 482 | } |
| 483 | if (mem_flags & EFI_MEMORY_WC) |
| 484 | info->screen_base = ioremap_wc(efifb_fix.smem_start, |
| 485 | efifb_fix.smem_len); |
| 486 | else if (mem_flags & EFI_MEMORY_UC) |
| 487 | info->screen_base = ioremap(efifb_fix.smem_start, |
| 488 | efifb_fix.smem_len); |
| 489 | else if (mem_flags & EFI_MEMORY_WT) |
| 490 | info->screen_base = memremap(efifb_fix.smem_start, |
| 491 | efifb_fix.smem_len, MEMREMAP_WT); |
| 492 | else if (mem_flags & EFI_MEMORY_WB) |
| 493 | info->screen_base = memremap(efifb_fix.smem_start, |
| 494 | efifb_fix.smem_len, MEMREMAP_WB); |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 495 | if (!info->screen_base) { |
Ard Biesheuvel | 38ac028 | 2018-07-11 11:40:40 +0200 | [diff] [blame] | 496 | pr_err("efifb: abort, cannot remap video memory 0x%x @ 0x%lx\n", |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 497 | efifb_fix.smem_len, efifb_fix.smem_start); |
| 498 | err = -EIO; |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 499 | goto err_release_fb; |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 500 | } |
| 501 | |
Hans de Goede | 34db50e | 2018-07-03 17:43:10 +0200 | [diff] [blame] | 502 | efifb_show_boot_graphics(info); |
| 503 | |
Peter Jones | 753375a | 2016-10-18 15:33:17 +0100 | [diff] [blame] | 504 | pr_info("efifb: framebuffer at 0x%lx, using %dk, total %dk\n", |
Andy Lutomirski | a53dd25 | 2016-05-11 16:57:47 -0700 | [diff] [blame] | 505 | efifb_fix.smem_start, size_remap/1024, size_total/1024); |
Peter Jones | 753375a | 2016-10-18 15:33:17 +0100 | [diff] [blame] | 506 | pr_info("efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n", |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 507 | efifb_defined.xres, efifb_defined.yres, |
| 508 | efifb_defined.bits_per_pixel, efifb_fix.line_length, |
| 509 | screen_info.pages); |
| 510 | |
| 511 | efifb_defined.xres_virtual = efifb_defined.xres; |
| 512 | efifb_defined.yres_virtual = efifb_fix.smem_len / |
| 513 | efifb_fix.line_length; |
Peter Jones | 753375a | 2016-10-18 15:33:17 +0100 | [diff] [blame] | 514 | pr_info("efifb: scrolling: redraw\n"); |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 515 | efifb_defined.yres_virtual = efifb_defined.yres; |
| 516 | |
| 517 | /* some dummy values for timing to make fbset happy */ |
| 518 | efifb_defined.pixclock = 10000000 / efifb_defined.xres * |
| 519 | 1000 / efifb_defined.yres; |
| 520 | efifb_defined.left_margin = (efifb_defined.xres / 8) & 0xf8; |
| 521 | efifb_defined.hsync_len = (efifb_defined.xres / 8) & 0xf8; |
| 522 | |
| 523 | efifb_defined.red.offset = screen_info.red_pos; |
| 524 | efifb_defined.red.length = screen_info.red_size; |
| 525 | efifb_defined.green.offset = screen_info.green_pos; |
| 526 | efifb_defined.green.length = screen_info.green_size; |
| 527 | efifb_defined.blue.offset = screen_info.blue_pos; |
| 528 | efifb_defined.blue.length = screen_info.blue_size; |
| 529 | efifb_defined.transp.offset = screen_info.rsvd_pos; |
| 530 | efifb_defined.transp.length = screen_info.rsvd_size; |
| 531 | |
Peter Jones | 753375a | 2016-10-18 15:33:17 +0100 | [diff] [blame] | 532 | pr_info("efifb: %s: " |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 533 | "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n", |
| 534 | "Truecolor", |
| 535 | screen_info.rsvd_size, |
| 536 | screen_info.red_size, |
| 537 | screen_info.green_size, |
| 538 | screen_info.blue_size, |
| 539 | screen_info.rsvd_pos, |
| 540 | screen_info.red_pos, |
| 541 | screen_info.green_pos, |
| 542 | screen_info.blue_pos); |
| 543 | |
| 544 | efifb_fix.ypanstep = 0; |
| 545 | efifb_fix.ywrapstep = 0; |
| 546 | |
| 547 | info->fbops = &efifb_ops; |
| 548 | info->var = efifb_defined; |
| 549 | info->fix = efifb_fix; |
Dave Airlie | 4410f39 | 2009-06-16 15:34:38 -0700 | [diff] [blame] | 550 | info->flags = FBINFO_FLAG_DEFAULT | FBINFO_MISC_FIRMWARE; |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 551 | |
Hans de Goede | 028b186 | 2017-11-25 20:35:52 +0100 | [diff] [blame] | 552 | orientation = drm_get_panel_orientation_quirk(efifb_defined.xres, |
| 553 | efifb_defined.yres); |
| 554 | switch (orientation) { |
| 555 | default: |
| 556 | info->fbcon_rotate_hint = FB_ROTATE_UR; |
| 557 | break; |
| 558 | case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP: |
| 559 | info->fbcon_rotate_hint = FB_ROTATE_UD; |
| 560 | break; |
| 561 | case DRM_MODE_PANEL_ORIENTATION_LEFT_UP: |
| 562 | info->fbcon_rotate_hint = FB_ROTATE_CCW; |
| 563 | break; |
| 564 | case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP: |
| 565 | info->fbcon_rotate_hint = FB_ROTATE_CW; |
| 566 | break; |
| 567 | } |
| 568 | |
Peter Jones | 753375a | 2016-10-18 15:33:17 +0100 | [diff] [blame] | 569 | err = sysfs_create_groups(&dev->dev.kobj, efifb_groups); |
| 570 | if (err) { |
| 571 | pr_err("efifb: cannot add sysfs attrs\n"); |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 572 | goto err_unmap; |
| 573 | } |
Peter Jones | 753375a | 2016-10-18 15:33:17 +0100 | [diff] [blame] | 574 | err = fb_alloc_cmap(&info->cmap, 256, 0); |
| 575 | if (err < 0) { |
| 576 | pr_err("efifb: cannot allocate colormap\n"); |
| 577 | goto err_groups; |
| 578 | } |
| 579 | err = register_framebuffer(info); |
| 580 | if (err < 0) { |
| 581 | pr_err("efifb: cannot register framebuffer\n"); |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 582 | goto err_fb_dealoc; |
| 583 | } |
Joe Perches | 31b6780 | 2013-09-19 18:35:55 -0700 | [diff] [blame] | 584 | fb_info(info, "%s frame buffer device\n", info->fix.id); |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 585 | return 0; |
| 586 | |
| 587 | err_fb_dealoc: |
| 588 | fb_dealloc_cmap(&info->cmap); |
Peter Jones | 753375a | 2016-10-18 15:33:17 +0100 | [diff] [blame] | 589 | err_groups: |
| 590 | sysfs_remove_groups(&dev->dev.kobj, efifb_groups); |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 591 | err_unmap: |
Ard Biesheuvel | 38ac028 | 2018-07-11 11:40:40 +0200 | [diff] [blame] | 592 | if (mem_flags & (EFI_MEMORY_UC | EFI_MEMORY_WC)) |
| 593 | iounmap(info->screen_base); |
| 594 | else |
| 595 | memunmap(info->screen_base); |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 596 | err_release_fb: |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 597 | framebuffer_release(info); |
| 598 | err_release_mem: |
Andy Lutomirski | da0241f | 2011-05-26 10:13:32 -0400 | [diff] [blame] | 599 | if (request_mem_succeeded) |
Peter Jones | 7c08c9a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 600 | release_mem_region(efifb_fix.smem_start, size_total); |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 601 | return err; |
| 602 | } |
| 603 | |
David Herrmann | 65b4021 | 2014-01-23 15:14:55 +0100 | [diff] [blame] | 604 | static int efifb_remove(struct platform_device *pdev) |
| 605 | { |
| 606 | struct fb_info *info = platform_get_drvdata(pdev); |
| 607 | |
| 608 | unregister_framebuffer(info); |
Peter Jones | 753375a | 2016-10-18 15:33:17 +0100 | [diff] [blame] | 609 | sysfs_remove_groups(&pdev->dev.kobj, efifb_groups); |
David Herrmann | 65b4021 | 2014-01-23 15:14:55 +0100 | [diff] [blame] | 610 | framebuffer_release(info); |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 615 | static struct platform_driver efifb_driver = { |
David Herrmann | e6816a8 | 2013-08-02 14:05:26 +0200 | [diff] [blame] | 616 | .driver = { |
| 617 | .name = "efi-framebuffer", |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 618 | }, |
David Herrmann | e6816a8 | 2013-08-02 14:05:26 +0200 | [diff] [blame] | 619 | .probe = efifb_probe, |
David Herrmann | 65b4021 | 2014-01-23 15:14:55 +0100 | [diff] [blame] | 620 | .remove = efifb_remove, |
Huang, Ying | 7c83172 | 2007-11-28 16:21:55 -0800 | [diff] [blame] | 621 | }; |
| 622 | |
Ard Biesheuvel | 07ea7ec | 2016-04-25 21:06:51 +0100 | [diff] [blame] | 623 | builtin_platform_driver(efifb_driver); |
Ard Biesheuvel | 55d728a | 2017-04-04 16:27:44 +0100 | [diff] [blame] | 624 | |
Ard Biesheuvel | dcf8f5c | 2017-08-18 20:49:40 +0100 | [diff] [blame] | 625 | #if defined(CONFIG_PCI) |
Ard Biesheuvel | 55d728a | 2017-04-04 16:27:44 +0100 | [diff] [blame] | 626 | |
Ard Biesheuvel | dcf8f5c | 2017-08-18 20:49:40 +0100 | [diff] [blame] | 627 | static void record_efifb_bar_resource(struct pci_dev *dev, int idx, u64 offset) |
Ard Biesheuvel | 55d728a | 2017-04-04 16:27:44 +0100 | [diff] [blame] | 628 | { |
| 629 | u16 word; |
| 630 | |
Ard Biesheuvel | dcf8f5c | 2017-08-18 20:49:40 +0100 | [diff] [blame] | 631 | efifb_pci_dev = dev; |
Ard Biesheuvel | 55d728a | 2017-04-04 16:27:44 +0100 | [diff] [blame] | 632 | |
| 633 | pci_read_config_word(dev, PCI_COMMAND, &word); |
| 634 | if (!(word & PCI_COMMAND_MEMORY)) { |
| 635 | pci_dev_disabled = true; |
| 636 | dev_err(&dev->dev, |
| 637 | "BAR %d: assigned to efifb but device is disabled!\n", |
| 638 | idx); |
| 639 | return; |
| 640 | } |
| 641 | |
Ard Biesheuvel | dcf8f5c | 2017-08-18 20:49:40 +0100 | [diff] [blame] | 642 | bar_resource = &dev->resource[idx]; |
| 643 | bar_offset = offset; |
Ard Biesheuvel | 55d728a | 2017-04-04 16:27:44 +0100 | [diff] [blame] | 644 | |
| 645 | dev_info(&dev->dev, "BAR %d: assigned to efifb\n", idx); |
| 646 | } |
| 647 | |
| 648 | static void efifb_fixup_resources(struct pci_dev *dev) |
| 649 | { |
| 650 | u64 base = screen_info.lfb_base; |
| 651 | u64 size = screen_info.lfb_size; |
| 652 | int i; |
| 653 | |
Ard Biesheuvel | dcf8f5c | 2017-08-18 20:49:40 +0100 | [diff] [blame] | 654 | if (efifb_pci_dev || screen_info.orig_video_isVGA != VIDEO_TYPE_EFI) |
Ard Biesheuvel | 55d728a | 2017-04-04 16:27:44 +0100 | [diff] [blame] | 655 | return; |
| 656 | |
| 657 | if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE) |
| 658 | base |= (u64)screen_info.ext_lfb_base << 32; |
| 659 | |
| 660 | if (!base) |
| 661 | return; |
| 662 | |
Bjorn Helgaas | 92a16c8 | 2017-05-19 14:37:53 -0500 | [diff] [blame] | 663 | for (i = 0; i <= PCI_STD_RESOURCE_END; i++) { |
Ard Biesheuvel | 55d728a | 2017-04-04 16:27:44 +0100 | [diff] [blame] | 664 | struct resource *res = &dev->resource[i]; |
| 665 | |
| 666 | if (!(res->flags & IORESOURCE_MEM)) |
| 667 | continue; |
| 668 | |
| 669 | if (res->start <= base && res->end >= base + size - 1) { |
Ard Biesheuvel | dcf8f5c | 2017-08-18 20:49:40 +0100 | [diff] [blame] | 670 | record_efifb_bar_resource(dev, i, base - res->start); |
Ard Biesheuvel | 55d728a | 2017-04-04 16:27:44 +0100 | [diff] [blame] | 671 | break; |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY, |
| 676 | 16, efifb_fixup_resources); |
| 677 | |
| 678 | #endif |