efifb.c 19 KB

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