efifb.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 <linux/pci.h>
  16. #include <video/vga.h>
  17. #include <asm/sysfb.h>
  18. static bool request_mem_succeeded = false;
  19. static struct fb_var_screeninfo efifb_defined = {
  20. .activate = FB_ACTIVATE_NOW,
  21. .height = -1,
  22. .width = -1,
  23. .right_margin = 32,
  24. .upper_margin = 16,
  25. .lower_margin = 4,
  26. .vsync_len = 4,
  27. .vmode = FB_VMODE_NONINTERLACED,
  28. };
  29. static struct fb_fix_screeninfo efifb_fix = {
  30. .id = "EFI VGA",
  31. .type = FB_TYPE_PACKED_PIXELS,
  32. .accel = FB_ACCEL_NONE,
  33. .visual = FB_VISUAL_TRUECOLOR,
  34. };
  35. static int efifb_setcolreg(unsigned regno, unsigned red, unsigned green,
  36. unsigned blue, unsigned transp,
  37. struct fb_info *info)
  38. {
  39. /*
  40. * Set a single color register. The values supplied are
  41. * already rounded down to the hardware's capabilities
  42. * (according to the entries in the `var' structure). Return
  43. * != 0 for invalid regno.
  44. */
  45. if (regno >= info->cmap.len)
  46. return 1;
  47. if (regno < 16) {
  48. red >>= 8;
  49. green >>= 8;
  50. blue >>= 8;
  51. ((u32 *)(info->pseudo_palette))[regno] =
  52. (red << info->var.red.offset) |
  53. (green << info->var.green.offset) |
  54. (blue << info->var.blue.offset);
  55. }
  56. return 0;
  57. }
  58. static void efifb_destroy(struct fb_info *info)
  59. {
  60. if (info->screen_base)
  61. iounmap(info->screen_base);
  62. if (request_mem_succeeded)
  63. release_mem_region(info->apertures->ranges[0].base,
  64. info->apertures->ranges[0].size);
  65. fb_dealloc_cmap(&info->cmap);
  66. }
  67. static struct fb_ops efifb_ops = {
  68. .owner = THIS_MODULE,
  69. .fb_destroy = efifb_destroy,
  70. .fb_setcolreg = efifb_setcolreg,
  71. .fb_fillrect = cfb_fillrect,
  72. .fb_copyarea = cfb_copyarea,
  73. .fb_imageblit = cfb_imageblit,
  74. };
  75. static int efifb_setup(char *options)
  76. {
  77. char *this_opt;
  78. int i;
  79. if (options && *options) {
  80. while ((this_opt = strsep(&options, ",")) != NULL) {
  81. if (!*this_opt) continue;
  82. for (i = 0; i < M_UNKNOWN; i++) {
  83. if (efifb_dmi_list[i].base != 0 &&
  84. !strcmp(this_opt, efifb_dmi_list[i].optname)) {
  85. screen_info.lfb_base = efifb_dmi_list[i].base;
  86. screen_info.lfb_linelength = efifb_dmi_list[i].stride;
  87. screen_info.lfb_width = efifb_dmi_list[i].width;
  88. screen_info.lfb_height = efifb_dmi_list[i].height;
  89. }
  90. }
  91. if (!strncmp(this_opt, "base:", 5))
  92. screen_info.lfb_base = simple_strtoul(this_opt+5, NULL, 0);
  93. else if (!strncmp(this_opt, "stride:", 7))
  94. screen_info.lfb_linelength = simple_strtoul(this_opt+7, NULL, 0) * 4;
  95. else if (!strncmp(this_opt, "height:", 7))
  96. screen_info.lfb_height = simple_strtoul(this_opt+7, NULL, 0);
  97. else if (!strncmp(this_opt, "width:", 6))
  98. screen_info.lfb_width = simple_strtoul(this_opt+6, NULL, 0);
  99. }
  100. }
  101. return 0;
  102. }
  103. static int efifb_probe(struct platform_device *dev)
  104. {
  105. struct fb_info *info;
  106. int err;
  107. unsigned int size_vmode;
  108. unsigned int size_remap;
  109. unsigned int size_total;
  110. char *option = NULL;
  111. if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
  112. return -ENODEV;
  113. if (fb_get_options("efifb", &option))
  114. return -ENODEV;
  115. efifb_setup(option);
  116. /* We don't get linelength from UGA Draw Protocol, only from
  117. * EFI Graphics Protocol. So if it's not in DMI, and it's not
  118. * passed in from the user, we really can't use the framebuffer.
  119. */
  120. if (!screen_info.lfb_linelength)
  121. return -ENODEV;
  122. if (!screen_info.lfb_depth)
  123. screen_info.lfb_depth = 32;
  124. if (!screen_info.pages)
  125. screen_info.pages = 1;
  126. if (!screen_info.lfb_base) {
  127. printk(KERN_DEBUG "efifb: invalid framebuffer address\n");
  128. return -ENODEV;
  129. }
  130. printk(KERN_INFO "efifb: probing for efifb\n");
  131. /* just assume they're all unset if any are */
  132. if (!screen_info.blue_size) {
  133. screen_info.blue_size = 8;
  134. screen_info.blue_pos = 0;
  135. screen_info.green_size = 8;
  136. screen_info.green_pos = 8;
  137. screen_info.red_size = 8;
  138. screen_info.red_pos = 16;
  139. screen_info.rsvd_size = 8;
  140. screen_info.rsvd_pos = 24;
  141. }
  142. efifb_fix.smem_start = screen_info.lfb_base;
  143. efifb_defined.bits_per_pixel = screen_info.lfb_depth;
  144. efifb_defined.xres = screen_info.lfb_width;
  145. efifb_defined.yres = screen_info.lfb_height;
  146. efifb_fix.line_length = screen_info.lfb_linelength;
  147. /* size_vmode -- that is the amount of memory needed for the
  148. * used video mode, i.e. the minimum amount of
  149. * memory we need. */
  150. size_vmode = efifb_defined.yres * efifb_fix.line_length;
  151. /* size_total -- all video memory we have. Used for
  152. * entries, ressource allocation and bounds
  153. * checking. */
  154. size_total = screen_info.lfb_size;
  155. if (size_total < size_vmode)
  156. size_total = size_vmode;
  157. /* size_remap -- the amount of video memory we are going to
  158. * use for efifb. With modern cards it is no
  159. * option to simply use size_total as that
  160. * wastes plenty of kernel address space. */
  161. size_remap = size_vmode * 2;
  162. if (size_remap > size_total)
  163. size_remap = size_total;
  164. if (size_remap % PAGE_SIZE)
  165. size_remap += PAGE_SIZE - (size_remap % PAGE_SIZE);
  166. efifb_fix.smem_len = size_remap;
  167. if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) {
  168. request_mem_succeeded = true;
  169. } else {
  170. /* We cannot make this fatal. Sometimes this comes from magic
  171. spaces our resource handlers simply don't know about */
  172. printk(KERN_WARNING
  173. "efifb: cannot reserve video memory at 0x%lx\n",
  174. efifb_fix.smem_start);
  175. }
  176. info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
  177. if (!info) {
  178. printk(KERN_ERR "efifb: cannot allocate framebuffer\n");
  179. err = -ENOMEM;
  180. goto err_release_mem;
  181. }
  182. platform_set_drvdata(dev, info);
  183. info->pseudo_palette = info->par;
  184. info->par = NULL;
  185. info->apertures = alloc_apertures(1);
  186. if (!info->apertures) {
  187. err = -ENOMEM;
  188. goto err_release_fb;
  189. }
  190. info->apertures->ranges[0].base = efifb_fix.smem_start;
  191. info->apertures->ranges[0].size = size_remap;
  192. info->screen_base = ioremap_wc(efifb_fix.smem_start, efifb_fix.smem_len);
  193. if (!info->screen_base) {
  194. printk(KERN_ERR "efifb: abort, cannot ioremap video memory "
  195. "0x%x @ 0x%lx\n",
  196. efifb_fix.smem_len, efifb_fix.smem_start);
  197. err = -EIO;
  198. goto err_release_fb;
  199. }
  200. printk(KERN_INFO "efifb: framebuffer at 0x%lx, mapped to 0x%p, "
  201. "using %dk, total %dk\n",
  202. efifb_fix.smem_start, info->screen_base,
  203. size_remap/1024, size_total/1024);
  204. printk(KERN_INFO "efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
  205. efifb_defined.xres, efifb_defined.yres,
  206. efifb_defined.bits_per_pixel, efifb_fix.line_length,
  207. screen_info.pages);
  208. efifb_defined.xres_virtual = efifb_defined.xres;
  209. efifb_defined.yres_virtual = efifb_fix.smem_len /
  210. efifb_fix.line_length;
  211. printk(KERN_INFO "efifb: scrolling: redraw\n");
  212. efifb_defined.yres_virtual = efifb_defined.yres;
  213. /* some dummy values for timing to make fbset happy */
  214. efifb_defined.pixclock = 10000000 / efifb_defined.xres *
  215. 1000 / efifb_defined.yres;
  216. efifb_defined.left_margin = (efifb_defined.xres / 8) & 0xf8;
  217. efifb_defined.hsync_len = (efifb_defined.xres / 8) & 0xf8;
  218. efifb_defined.red.offset = screen_info.red_pos;
  219. efifb_defined.red.length = screen_info.red_size;
  220. efifb_defined.green.offset = screen_info.green_pos;
  221. efifb_defined.green.length = screen_info.green_size;
  222. efifb_defined.blue.offset = screen_info.blue_pos;
  223. efifb_defined.blue.length = screen_info.blue_size;
  224. efifb_defined.transp.offset = screen_info.rsvd_pos;
  225. efifb_defined.transp.length = screen_info.rsvd_size;
  226. printk(KERN_INFO "efifb: %s: "
  227. "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
  228. "Truecolor",
  229. screen_info.rsvd_size,
  230. screen_info.red_size,
  231. screen_info.green_size,
  232. screen_info.blue_size,
  233. screen_info.rsvd_pos,
  234. screen_info.red_pos,
  235. screen_info.green_pos,
  236. screen_info.blue_pos);
  237. efifb_fix.ypanstep = 0;
  238. efifb_fix.ywrapstep = 0;
  239. info->fbops = &efifb_ops;
  240. info->var = efifb_defined;
  241. info->fix = efifb_fix;
  242. info->flags = FBINFO_FLAG_DEFAULT | FBINFO_MISC_FIRMWARE;
  243. if ((err = fb_alloc_cmap(&info->cmap, 256, 0)) < 0) {
  244. printk(KERN_ERR "efifb: cannot allocate colormap\n");
  245. goto err_unmap;
  246. }
  247. if ((err = register_framebuffer(info)) < 0) {
  248. printk(KERN_ERR "efifb: cannot register framebuffer\n");
  249. goto err_fb_dealoc;
  250. }
  251. fb_info(info, "%s frame buffer device\n", info->fix.id);
  252. return 0;
  253. err_fb_dealoc:
  254. fb_dealloc_cmap(&info->cmap);
  255. err_unmap:
  256. iounmap(info->screen_base);
  257. err_release_fb:
  258. framebuffer_release(info);
  259. err_release_mem:
  260. if (request_mem_succeeded)
  261. release_mem_region(efifb_fix.smem_start, size_total);
  262. return err;
  263. }
  264. static int efifb_remove(struct platform_device *pdev)
  265. {
  266. struct fb_info *info = platform_get_drvdata(pdev);
  267. unregister_framebuffer(info);
  268. framebuffer_release(info);
  269. return 0;
  270. }
  271. static struct platform_driver efifb_driver = {
  272. .driver = {
  273. .name = "efi-framebuffer",
  274. },
  275. .probe = efifb_probe,
  276. .remove = efifb_remove,
  277. };
  278. module_platform_driver(efifb_driver);
  279. MODULE_LICENSE("GPL");