efifb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. if (screen_info.orig_video_isVGA == 0)
  121. screen_info.orig_video_isVGA = VIDEO_TYPE_EFI;
  122. return 0;
  123. }
  124. static int efifb_setcolreg(unsigned regno, unsigned red, unsigned green,
  125. unsigned blue, unsigned transp,
  126. struct fb_info *info)
  127. {
  128. /*
  129. * Set a single color register. The values supplied are
  130. * already rounded down to the hardware's capabilities
  131. * (according to the entries in the `var' structure). Return
  132. * != 0 for invalid regno.
  133. */
  134. if (regno >= info->cmap.len)
  135. return 1;
  136. if (regno < 16) {
  137. red >>= 8;
  138. green >>= 8;
  139. blue >>= 8;
  140. ((u32 *)(info->pseudo_palette))[regno] =
  141. (red << info->var.red.offset) |
  142. (green << info->var.green.offset) |
  143. (blue << info->var.blue.offset);
  144. }
  145. return 0;
  146. }
  147. static struct fb_ops efifb_ops = {
  148. .owner = THIS_MODULE,
  149. .fb_setcolreg = efifb_setcolreg,
  150. .fb_fillrect = cfb_fillrect,
  151. .fb_copyarea = cfb_copyarea,
  152. .fb_imageblit = cfb_imageblit,
  153. };
  154. static int __init efifb_setup(char *options)
  155. {
  156. char *this_opt;
  157. int i;
  158. if (!options || !*options)
  159. return 0;
  160. while ((this_opt = strsep(&options, ",")) != NULL) {
  161. if (!*this_opt) continue;
  162. for (i = 0; i < M_UNKNOWN; i++) {
  163. if (!strcmp(this_opt, dmi_list[i].optname) &&
  164. dmi_list[i].base != 0) {
  165. screen_info.lfb_base = dmi_list[i].base;
  166. screen_info.lfb_linelength = dmi_list[i].stride;
  167. screen_info.lfb_width = dmi_list[i].width;
  168. screen_info.lfb_height = dmi_list[i].height;
  169. }
  170. }
  171. if (!strncmp(this_opt, "base:", 5))
  172. screen_info.lfb_base = simple_strtoul(this_opt+5, NULL, 0);
  173. else if (!strncmp(this_opt, "stride:", 7))
  174. screen_info.lfb_linelength = simple_strtoul(this_opt+7, NULL, 0) * 4;
  175. else if (!strncmp(this_opt, "height:", 7))
  176. screen_info.lfb_height = simple_strtoul(this_opt+7, NULL, 0);
  177. else if (!strncmp(this_opt, "width:", 6))
  178. screen_info.lfb_width = simple_strtoul(this_opt+6, NULL, 0);
  179. }
  180. return 0;
  181. }
  182. static int __init efifb_probe(struct platform_device *dev)
  183. {
  184. struct fb_info *info;
  185. int err;
  186. unsigned int size_vmode;
  187. unsigned int size_remap;
  188. unsigned int size_total;
  189. int request_succeeded = 0;
  190. if (!screen_info.lfb_depth)
  191. screen_info.lfb_depth = 32;
  192. if (!screen_info.pages)
  193. screen_info.pages = 1;
  194. if (!screen_info.lfb_base) {
  195. printk(KERN_DEBUG "efifb: invalid framebuffer address\n");
  196. return -ENODEV;
  197. }
  198. printk(KERN_INFO "efifb: probing for efifb\n");
  199. /* just assume they're all unset if any are */
  200. if (!screen_info.blue_size) {
  201. screen_info.blue_size = 8;
  202. screen_info.blue_pos = 0;
  203. screen_info.green_size = 8;
  204. screen_info.green_pos = 8;
  205. screen_info.red_size = 8;
  206. screen_info.red_pos = 16;
  207. screen_info.rsvd_size = 8;
  208. screen_info.rsvd_pos = 24;
  209. }
  210. efifb_fix.smem_start = screen_info.lfb_base;
  211. efifb_defined.bits_per_pixel = screen_info.lfb_depth;
  212. efifb_defined.xres = screen_info.lfb_width;
  213. efifb_defined.yres = screen_info.lfb_height;
  214. efifb_fix.line_length = screen_info.lfb_linelength;
  215. /* size_vmode -- that is the amount of memory needed for the
  216. * used video mode, i.e. the minimum amount of
  217. * memory we need. */
  218. size_vmode = efifb_defined.yres * efifb_fix.line_length;
  219. /* size_total -- all video memory we have. Used for
  220. * entries, ressource allocation and bounds
  221. * checking. */
  222. size_total = screen_info.lfb_size;
  223. if (size_total < size_vmode)
  224. size_total = size_vmode;
  225. /* size_remap -- the amount of video memory we are going to
  226. * use for efifb. With modern cards it is no
  227. * option to simply use size_total as that
  228. * wastes plenty of kernel address space. */
  229. size_remap = size_vmode * 2;
  230. if (size_remap > size_total)
  231. size_remap = size_total;
  232. if (size_remap % PAGE_SIZE)
  233. size_remap += PAGE_SIZE - (size_remap % PAGE_SIZE);
  234. efifb_fix.smem_len = size_remap;
  235. if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) {
  236. request_succeeded = 1;
  237. } else {
  238. /* We cannot make this fatal. Sometimes this comes from magic
  239. spaces our resource handlers simply don't know about */
  240. printk(KERN_WARNING
  241. "efifb: cannot reserve video memory at 0x%lx\n",
  242. efifb_fix.smem_start);
  243. }
  244. info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
  245. if (!info) {
  246. printk(KERN_ERR "efifb: cannot allocate framebuffer\n");
  247. err = -ENOMEM;
  248. goto err_release_mem;
  249. }
  250. info->pseudo_palette = info->par;
  251. info->par = NULL;
  252. info->screen_base = ioremap(efifb_fix.smem_start, efifb_fix.smem_len);
  253. if (!info->screen_base) {
  254. printk(KERN_ERR "efifb: abort, cannot ioremap video memory "
  255. "0x%x @ 0x%lx\n",
  256. efifb_fix.smem_len, efifb_fix.smem_start);
  257. err = -EIO;
  258. goto err_release_fb;
  259. }
  260. printk(KERN_INFO "efifb: framebuffer at 0x%lx, mapped to 0x%p, "
  261. "using %dk, total %dk\n",
  262. efifb_fix.smem_start, info->screen_base,
  263. size_remap/1024, size_total/1024);
  264. printk(KERN_INFO "efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
  265. efifb_defined.xres, efifb_defined.yres,
  266. efifb_defined.bits_per_pixel, efifb_fix.line_length,
  267. screen_info.pages);
  268. efifb_defined.xres_virtual = efifb_defined.xres;
  269. efifb_defined.yres_virtual = efifb_fix.smem_len /
  270. efifb_fix.line_length;
  271. printk(KERN_INFO "efifb: scrolling: redraw\n");
  272. efifb_defined.yres_virtual = efifb_defined.yres;
  273. /* some dummy values for timing to make fbset happy */
  274. efifb_defined.pixclock = 10000000 / efifb_defined.xres *
  275. 1000 / efifb_defined.yres;
  276. efifb_defined.left_margin = (efifb_defined.xres / 8) & 0xf8;
  277. efifb_defined.hsync_len = (efifb_defined.xres / 8) & 0xf8;
  278. efifb_defined.red.offset = screen_info.red_pos;
  279. efifb_defined.red.length = screen_info.red_size;
  280. efifb_defined.green.offset = screen_info.green_pos;
  281. efifb_defined.green.length = screen_info.green_size;
  282. efifb_defined.blue.offset = screen_info.blue_pos;
  283. efifb_defined.blue.length = screen_info.blue_size;
  284. efifb_defined.transp.offset = screen_info.rsvd_pos;
  285. efifb_defined.transp.length = screen_info.rsvd_size;
  286. printk(KERN_INFO "efifb: %s: "
  287. "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
  288. "Truecolor",
  289. screen_info.rsvd_size,
  290. screen_info.red_size,
  291. screen_info.green_size,
  292. screen_info.blue_size,
  293. screen_info.rsvd_pos,
  294. screen_info.red_pos,
  295. screen_info.green_pos,
  296. screen_info.blue_pos);
  297. efifb_fix.ypanstep = 0;
  298. efifb_fix.ywrapstep = 0;
  299. info->fbops = &efifb_ops;
  300. info->var = efifb_defined;
  301. info->fix = efifb_fix;
  302. info->flags = FBINFO_FLAG_DEFAULT;
  303. if ((err = fb_alloc_cmap(&info->cmap, 256, 0)) < 0) {
  304. printk(KERN_ERR "efifb: cannot allocate colormap\n");
  305. goto err_unmap;
  306. }
  307. if ((err = register_framebuffer(info)) < 0) {
  308. printk(KERN_ERR "efifb: cannot register framebuffer\n");
  309. goto err_fb_dealoc;
  310. }
  311. printk(KERN_INFO "fb%d: %s frame buffer device\n",
  312. info->node, info->fix.id);
  313. return 0;
  314. err_fb_dealoc:
  315. fb_dealloc_cmap(&info->cmap);
  316. err_unmap:
  317. iounmap(info->screen_base);
  318. err_release_fb:
  319. framebuffer_release(info);
  320. err_release_mem:
  321. if (request_succeeded)
  322. release_mem_region(efifb_fix.smem_start, size_total);
  323. return err;
  324. }
  325. static struct platform_driver efifb_driver = {
  326. .probe = efifb_probe,
  327. .driver = {
  328. .name = "efifb",
  329. },
  330. };
  331. static struct platform_device efifb_device = {
  332. .name = "efifb",
  333. };
  334. static int __init efifb_init(void)
  335. {
  336. int ret;
  337. char *option = NULL;
  338. dmi_check_system(dmi_system_table);
  339. if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
  340. return -ENODEV;
  341. if (fb_get_options("efifb", &option))
  342. return -ENODEV;
  343. efifb_setup(option);
  344. /* We don't get linelength from UGA Draw Protocol, only from
  345. * EFI Graphics Protocol. So if it's not in DMI, and it's not
  346. * passed in from the user, we really can't use the framebuffer.
  347. */
  348. if (!screen_info.lfb_linelength)
  349. return -ENODEV;
  350. ret = platform_driver_register(&efifb_driver);
  351. if (!ret) {
  352. ret = platform_device_register(&efifb_device);
  353. if (ret)
  354. platform_driver_unregister(&efifb_driver);
  355. }
  356. return ret;
  357. }
  358. module_init(efifb_init);
  359. MODULE_LICENSE("GPL");