efifb.c 13 KB

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