efifb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/errno.h>
  11. #include <linux/fb.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/screen_info.h>
  14. #include <linux/dmi.h>
  15. #include <video/vga.h>
  16. static struct fb_var_screeninfo efifb_defined __initdata = {
  17. .activate = FB_ACTIVATE_NOW,
  18. .height = -1,
  19. .width = -1,
  20. .right_margin = 32,
  21. .upper_margin = 16,
  22. .lower_margin = 4,
  23. .vsync_len = 4,
  24. .vmode = FB_VMODE_NONINTERLACED,
  25. };
  26. static struct fb_fix_screeninfo efifb_fix __initdata = {
  27. .id = "EFI VGA",
  28. .type = FB_TYPE_PACKED_PIXELS,
  29. .accel = FB_ACCEL_NONE,
  30. .visual = FB_VISUAL_TRUECOLOR,
  31. };
  32. enum {
  33. M_I17, /* 17-Inch iMac */
  34. M_I20, /* 20-Inch iMac */
  35. M_I20_SR, /* 20-Inch iMac (Santa Rosa) */
  36. M_I24, /* 24-Inch iMac */
  37. M_MINI, /* Mac Mini */
  38. M_MB, /* MacBook */
  39. M_MB_2, /* MacBook, 2nd rev. */
  40. M_MB_3, /* MacBook, 3rd rev. */
  41. M_MB_SR, /* MacBook, 2nd gen, (Santa Rosa) */
  42. M_MBA, /* MacBook Air */
  43. M_MBP, /* MacBook Pro */
  44. M_MBP_2, /* MacBook Pro 2nd gen */
  45. M_MBP_SR, /* MacBook Pro (Santa Rosa) */
  46. M_MBP_4, /* MacBook Pro, 4th gen */
  47. M_UNKNOWN /* placeholder */
  48. };
  49. static struct efifb_dmi_info {
  50. char *optname;
  51. unsigned long base;
  52. int stride;
  53. int width;
  54. int height;
  55. } dmi_list[] = {
  56. [M_I17] = { "i17", 0x80010000, 1472 * 4, 1440, 900 },
  57. [M_I20] = { "i20", 0x80010000, 1728 * 4, 1680, 1050 }, /* guess */
  58. [M_I20_SR] = { "imac7", 0x40010000, 1728 * 4, 1680, 1050 },
  59. [M_I24] = { "i24", 0x80010000, 2048 * 4, 1920, 1200 }, /* guess */
  60. [M_MINI]= { "mini", 0x80000000, 2048 * 4, 1024, 768 },
  61. [M_MB] = { "macbook", 0x80000000, 2048 * 4, 1280, 800 },
  62. [M_MBA] = { "mba", 0x80000000, 2048 * 4, 1280, 800 },
  63. [M_MBP] = { "mbp", 0x80010000, 1472 * 4, 1440, 900 },
  64. [M_MBP_2] = { "mbp2", 0, 0, 0, 0 }, /* placeholder */
  65. [M_MBP_SR] = { "mbp3", 0x80030000, 2048 * 4, 1440, 900 },
  66. [M_MBP_4] = { "mbp4", 0xc0060000, 2048 * 4, 1920, 1200 },
  67. [M_UNKNOWN] = { NULL, 0, 0, 0, 0 }
  68. };
  69. static int set_system(const struct dmi_system_id *id);
  70. #define EFIFB_DMI_SYSTEM_ID(vendor, name, enumid) \
  71. { set_system, name, { \
  72. DMI_MATCH(DMI_BIOS_VENDOR, vendor), \
  73. DMI_MATCH(DMI_PRODUCT_NAME, name) }, \
  74. &dmi_list[enumid] }
  75. static struct dmi_system_id __initdata dmi_system_table[] = {
  76. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "iMac4,1", M_I17),
  77. /* At least one of these two will be right; maybe both? */
  78. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "iMac5,1", M_I20),
  79. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac5,1", M_I20),
  80. /* At least one of these two will be right; maybe both? */
  81. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "iMac6,1", M_I24),
  82. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac6,1", M_I24),
  83. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac7,1", M_I20_SR),
  84. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "Macmini1,1", M_MINI),
  85. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBook1,1", M_MB),
  86. /* At least one of these two will be right; maybe both? */
  87. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBook2,1", M_MB),
  88. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook2,1", M_MB),
  89. /* At least one of these two will be right; maybe both? */
  90. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBook3,1", M_MB),
  91. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook3,1", M_MB),
  92. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook4,1", M_MB),
  93. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookAir1,1", M_MBA),
  94. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro1,1", M_MBP),
  95. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro2,1", M_MBP_2),
  96. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro2,1", M_MBP_2),
  97. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro3,1", M_MBP_SR),
  98. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro3,1", M_MBP_SR),
  99. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro4,1", M_MBP_4),
  100. {},
  101. };
  102. static int set_system(const struct dmi_system_id *id)
  103. {
  104. struct efifb_dmi_info *info = id->driver_data;
  105. if (info->base == 0)
  106. return -ENODEV;
  107. printk(KERN_INFO "efifb: dmi detected %s - framebuffer at %p "
  108. "(%dx%d, stride %d)\n", id->ident,
  109. (void *)info->base, info->width, info->height,
  110. info->stride);
  111. /* Trust the bootloader over the DMI tables */
  112. if (screen_info.lfb_base == 0)
  113. screen_info.lfb_base = info->base;
  114. if (screen_info.lfb_linelength == 0)
  115. screen_info.lfb_linelength = info->stride;
  116. if (screen_info.lfb_width == 0)
  117. screen_info.lfb_width = info->width;
  118. if (screen_info.lfb_height == 0)
  119. screen_info.lfb_height = info->height;
  120. return 0;
  121. }
  122. static int efifb_setcolreg(unsigned regno, unsigned red, unsigned green,
  123. unsigned blue, unsigned transp,
  124. struct fb_info *info)
  125. {
  126. /*
  127. * Set a single color register. The values supplied are
  128. * already rounded down to the hardware's capabilities
  129. * (according to the entries in the `var' structure). Return
  130. * != 0 for invalid regno.
  131. */
  132. if (regno >= info->cmap.len)
  133. return 1;
  134. if (regno < 16) {
  135. red >>= 8;
  136. green >>= 8;
  137. blue >>= 8;
  138. ((u32 *)(info->pseudo_palette))[regno] =
  139. (red << info->var.red.offset) |
  140. (green << info->var.green.offset) |
  141. (blue << info->var.blue.offset);
  142. }
  143. return 0;
  144. }
  145. static struct fb_ops efifb_ops = {
  146. .owner = THIS_MODULE,
  147. .fb_setcolreg = efifb_setcolreg,
  148. .fb_fillrect = cfb_fillrect,
  149. .fb_copyarea = cfb_copyarea,
  150. .fb_imageblit = cfb_imageblit,
  151. };
  152. static int __init efifb_setup(char *options)
  153. {
  154. char *this_opt;
  155. int i;
  156. if (!options || !*options)
  157. return 0;
  158. while ((this_opt = strsep(&options, ",")) != NULL) {
  159. if (!*this_opt) continue;
  160. for (i = 0; i < M_UNKNOWN; i++) {
  161. if (!strcmp(this_opt, dmi_list[i].optname) &&
  162. dmi_list[i].base != 0) {
  163. screen_info.lfb_base = dmi_list[i].base;
  164. screen_info.lfb_linelength = dmi_list[i].stride;
  165. screen_info.lfb_width = dmi_list[i].width;
  166. screen_info.lfb_height = dmi_list[i].height;
  167. }
  168. }
  169. if (!strncmp(this_opt, "base:", 5))
  170. screen_info.lfb_base = simple_strtoul(this_opt+5, NULL, 0);
  171. else if (!strncmp(this_opt, "stride:", 7))
  172. screen_info.lfb_linelength = simple_strtoul(this_opt+7, NULL, 0) * 4;
  173. else if (!strncmp(this_opt, "height:", 7))
  174. screen_info.lfb_height = simple_strtoul(this_opt+7, NULL, 0);
  175. else if (!strncmp(this_opt, "width:", 6))
  176. screen_info.lfb_width = simple_strtoul(this_opt+6, NULL, 0);
  177. }
  178. return 0;
  179. }
  180. static int __init efifb_probe(struct platform_device *dev)
  181. {
  182. struct fb_info *info;
  183. int err;
  184. unsigned int size_vmode;
  185. unsigned int size_remap;
  186. unsigned int size_total;
  187. int request_succeeded = 0;
  188. printk(KERN_INFO "efifb: probing for efifb\n");
  189. if (!screen_info.lfb_depth)
  190. screen_info.lfb_depth = 32;
  191. if (!screen_info.pages)
  192. screen_info.pages = 1;
  193. /* just assume they're all unset if any are */
  194. if (!screen_info.blue_size) {
  195. screen_info.blue_size = 8;
  196. screen_info.blue_pos = 0;
  197. screen_info.green_size = 8;
  198. screen_info.green_pos = 8;
  199. screen_info.red_size = 8;
  200. screen_info.red_pos = 16;
  201. screen_info.rsvd_size = 8;
  202. screen_info.rsvd_pos = 24;
  203. }
  204. efifb_fix.smem_start = screen_info.lfb_base;
  205. efifb_defined.bits_per_pixel = screen_info.lfb_depth;
  206. efifb_defined.xres = screen_info.lfb_width;
  207. efifb_defined.yres = screen_info.lfb_height;
  208. efifb_fix.line_length = screen_info.lfb_linelength;
  209. /* size_vmode -- that is the amount of memory needed for the
  210. * used video mode, i.e. the minimum amount of
  211. * memory we need. */
  212. size_vmode = efifb_defined.yres * efifb_fix.line_length;
  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. /* size_remap -- the amount of video memory we are going to
  220. * use for efifb. With modern cards it is no
  221. * option to simply use size_total as that
  222. * wastes plenty of kernel address space. */
  223. size_remap = size_vmode * 2;
  224. if (size_remap > size_total)
  225. size_remap = size_total;
  226. if (size_remap % PAGE_SIZE)
  227. size_remap += PAGE_SIZE - (size_remap % PAGE_SIZE);
  228. efifb_fix.smem_len = size_remap;
  229. if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) {
  230. request_succeeded = 1;
  231. } else {
  232. /* We cannot make this fatal. Sometimes this comes from magic
  233. spaces our resource handlers simply don't know about */
  234. printk(KERN_WARNING
  235. "efifb: cannot reserve video memory at 0x%lx\n",
  236. efifb_fix.smem_start);
  237. }
  238. info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
  239. if (!info) {
  240. printk(KERN_ERR "efifb: cannot allocate framebuffer\n");
  241. err = -ENOMEM;
  242. goto err_release_mem;
  243. }
  244. info->pseudo_palette = info->par;
  245. info->par = NULL;
  246. info->screen_base = ioremap(efifb_fix.smem_start, efifb_fix.smem_len);
  247. if (!info->screen_base) {
  248. printk(KERN_ERR "efifb: abort, cannot ioremap video memory "
  249. "0x%x @ 0x%lx\n",
  250. efifb_fix.smem_len, efifb_fix.smem_start);
  251. err = -EIO;
  252. goto err_release_fb;
  253. }
  254. printk(KERN_INFO "efifb: framebuffer at 0x%lx, mapped to 0x%p, "
  255. "using %dk, total %dk\n",
  256. efifb_fix.smem_start, info->screen_base,
  257. size_remap/1024, size_total/1024);
  258. printk(KERN_INFO "efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
  259. efifb_defined.xres, efifb_defined.yres,
  260. efifb_defined.bits_per_pixel, efifb_fix.line_length,
  261. screen_info.pages);
  262. efifb_defined.xres_virtual = efifb_defined.xres;
  263. efifb_defined.yres_virtual = efifb_fix.smem_len /
  264. efifb_fix.line_length;
  265. printk(KERN_INFO "efifb: scrolling: redraw\n");
  266. efifb_defined.yres_virtual = efifb_defined.yres;
  267. /* some dummy values for timing to make fbset happy */
  268. efifb_defined.pixclock = 10000000 / efifb_defined.xres *
  269. 1000 / efifb_defined.yres;
  270. efifb_defined.left_margin = (efifb_defined.xres / 8) & 0xf8;
  271. efifb_defined.hsync_len = (efifb_defined.xres / 8) & 0xf8;
  272. efifb_defined.red.offset = screen_info.red_pos;
  273. efifb_defined.red.length = screen_info.red_size;
  274. efifb_defined.green.offset = screen_info.green_pos;
  275. efifb_defined.green.length = screen_info.green_size;
  276. efifb_defined.blue.offset = screen_info.blue_pos;
  277. efifb_defined.blue.length = screen_info.blue_size;
  278. efifb_defined.transp.offset = screen_info.rsvd_pos;
  279. efifb_defined.transp.length = screen_info.rsvd_size;
  280. printk(KERN_INFO "efifb: %s: "
  281. "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
  282. "Truecolor",
  283. screen_info.rsvd_size,
  284. screen_info.red_size,
  285. screen_info.green_size,
  286. screen_info.blue_size,
  287. screen_info.rsvd_pos,
  288. screen_info.red_pos,
  289. screen_info.green_pos,
  290. screen_info.blue_pos);
  291. efifb_fix.ypanstep = 0;
  292. efifb_fix.ywrapstep = 0;
  293. info->fbops = &efifb_ops;
  294. info->var = efifb_defined;
  295. info->fix = efifb_fix;
  296. info->flags = FBINFO_FLAG_DEFAULT;
  297. if ((err = fb_alloc_cmap(&info->cmap, 256, 0)) < 0) {
  298. printk(KERN_ERR "efifb: cannot allocate colormap\n");
  299. goto err_unmap;
  300. }
  301. if ((err = register_framebuffer(info)) < 0) {
  302. printk(KERN_ERR "efifb: cannot register framebuffer\n");
  303. goto err_fb_dealoc;
  304. }
  305. printk(KERN_INFO "fb%d: %s frame buffer device\n",
  306. info->node, info->fix.id);
  307. return 0;
  308. err_fb_dealoc:
  309. fb_dealloc_cmap(&info->cmap);
  310. err_unmap:
  311. iounmap(info->screen_base);
  312. err_release_fb:
  313. framebuffer_release(info);
  314. err_release_mem:
  315. if (request_succeeded)
  316. release_mem_region(efifb_fix.smem_start, size_total);
  317. return err;
  318. }
  319. static struct platform_driver efifb_driver = {
  320. .probe = efifb_probe,
  321. .driver = {
  322. .name = "efifb",
  323. },
  324. };
  325. static struct platform_device efifb_device = {
  326. .name = "efifb",
  327. };
  328. static int __init efifb_init(void)
  329. {
  330. int ret;
  331. char *option = NULL;
  332. if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
  333. return -ENODEV;
  334. dmi_check_system(dmi_system_table);
  335. if (fb_get_options("efifb", &option))
  336. return -ENODEV;
  337. efifb_setup(option);
  338. /* We don't get linelength from UGA Draw Protocol, only from
  339. * EFI Graphics Protocol. So if it's not in DMI, and it's not
  340. * passed in from the user, we really can't use the framebuffer.
  341. */
  342. if (!screen_info.lfb_linelength)
  343. return -ENODEV;
  344. ret = platform_driver_register(&efifb_driver);
  345. if (!ret) {
  346. ret = platform_device_register(&efifb_device);
  347. if (ret)
  348. platform_driver_unregister(&efifb_driver);
  349. }
  350. return ret;
  351. }
  352. module_init(efifb_init);
  353. MODULE_LICENSE("GPL");