sysfb_simplefb.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Generic System Framebuffers
  4. * Copyright (c) 2012-2013 David Herrmann <dh.herrmann@gmail.com>
  5. */
  6. /*
  7. * simple-framebuffer probing
  8. * Try to convert "screen_info" into a "simple-framebuffer" compatible mode.
  9. * If the mode is incompatible, we return "false" and let the caller create
  10. * legacy nodes instead.
  11. */
  12. #include <linux/err.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/platform_data/simplefb.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/screen_info.h>
  19. #include <linux/sysfb.h>
  20. static const char simplefb_resname[] = "BOOTFB";
  21. static const struct simplefb_format formats[] = SIMPLEFB_FORMATS;
  22. /* try parsing screen_info into a simple-framebuffer mode struct */
  23. __init bool sysfb_parse_mode(const struct screen_info *si,
  24. struct simplefb_platform_data *mode)
  25. {
  26. __u8 type;
  27. u32 bits_per_pixel;
  28. unsigned int i;
  29. type = si->orig_video_isVGA;
  30. if (type != VIDEO_TYPE_VLFB && type != VIDEO_TYPE_EFI)
  31. return false;
  32. bits_per_pixel = __screen_info_lfb_bits_per_pixel(si);
  33. for (i = 0; i < ARRAY_SIZE(formats); ++i) {
  34. const struct simplefb_format *f = &formats[i];
  35. if (f->transp.length)
  36. continue; /* transparent formats are unsupported by VESA/EFI */
  37. if (bits_per_pixel == f->bits_per_pixel &&
  38. si->red_size == f->red.length &&
  39. si->red_pos == f->red.offset &&
  40. si->green_size == f->green.length &&
  41. si->green_pos == f->green.offset &&
  42. si->blue_size == f->blue.length &&
  43. si->blue_pos == f->blue.offset) {
  44. mode->format = f->name;
  45. mode->width = si->lfb_width;
  46. mode->height = si->lfb_height;
  47. mode->stride = si->lfb_linelength;
  48. return true;
  49. }
  50. }
  51. return false;
  52. }
  53. __init struct platform_device *sysfb_create_simplefb(const struct screen_info *si,
  54. const struct simplefb_platform_data *mode,
  55. struct device *parent)
  56. {
  57. struct platform_device *pd;
  58. struct resource res;
  59. u64 base, size;
  60. u32 length;
  61. int ret;
  62. /*
  63. * If the 64BIT_BASE capability is set, ext_lfb_base will contain the
  64. * upper half of the base address. Assemble the address, then make sure
  65. * it is valid and we can actually access it.
  66. */
  67. base = si->lfb_base;
  68. if (si->capabilities & VIDEO_CAPABILITY_64BIT_BASE)
  69. base |= (u64)si->ext_lfb_base << 32;
  70. if (!base || (u64)(resource_size_t)base != base) {
  71. printk(KERN_DEBUG "sysfb: inaccessible VRAM base\n");
  72. return ERR_PTR(-EINVAL);
  73. }
  74. /*
  75. * Don't use lfb_size as IORESOURCE size, since it may contain the
  76. * entire VMEM, and thus require huge mappings. Use just the part we
  77. * need, that is, the part where the framebuffer is located. But verify
  78. * that it does not exceed the advertised VMEM.
  79. * Note that in case of VBE, the lfb_size is shifted by 16 bits for
  80. * historical reasons.
  81. */
  82. size = si->lfb_size;
  83. if (si->orig_video_isVGA == VIDEO_TYPE_VLFB)
  84. size <<= 16;
  85. length = mode->height * mode->stride;
  86. if (length > size) {
  87. printk(KERN_WARNING "sysfb: VRAM smaller than advertised\n");
  88. return ERR_PTR(-EINVAL);
  89. }
  90. length = PAGE_ALIGN(length);
  91. /* setup IORESOURCE_MEM as framebuffer memory */
  92. memset(&res, 0, sizeof(res));
  93. res.flags = IORESOURCE_MEM;
  94. res.name = simplefb_resname;
  95. res.start = base;
  96. res.end = res.start + length - 1;
  97. if (res.end <= res.start)
  98. return ERR_PTR(-EINVAL);
  99. pd = platform_device_alloc("simple-framebuffer", 0);
  100. if (!pd)
  101. return ERR_PTR(-ENOMEM);
  102. pd->dev.parent = parent;
  103. sysfb_set_efifb_fwnode(si, pd);
  104. ret = platform_device_add_resources(pd, &res, 1);
  105. if (ret)
  106. goto err_put_device;
  107. ret = platform_device_add_data(pd, mode, sizeof(*mode));
  108. if (ret)
  109. goto err_put_device;
  110. ret = platform_device_add(pd);
  111. if (ret)
  112. goto err_put_device;
  113. return pd;
  114. err_put_device:
  115. platform_device_put(pd);
  116. return ERR_PTR(ret);
  117. }