efifb.c 18 KB

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