sysfb_simplefb.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. /*
  33. * The meaning of depth and bpp for direct-color formats is
  34. * inconsistent:
  35. *
  36. * - DRM format info specifies depth as the number of color
  37. * bits; including alpha, but not including filler bits.
  38. * - Linux' EFI platform code computes lfb_depth from the
  39. * individual color channels, including the reserved bits.
  40. * - VBE 1.1 defines lfb_depth for XRGB1555 as 16, but later
  41. * versions use 15.
  42. * - On the kernel command line, 'bpp' of 32 is usually
  43. * XRGB8888 including the filler bits, but 15 is XRGB1555
  44. * not including the filler bit.
  45. *
  46. * It's not easily possible to fix this in struct screen_info,
  47. * as this could break UAPI. The best solution is to compute
  48. * bits_per_pixel from the color bits, reserved bits and
  49. * reported lfb_depth, whichever is highest. In the loop below,
  50. * ignore simplefb formats with alpha bits, as EFI and VESA
  51. * don't specify alpha channels.
  52. */
  53. if (si->lfb_depth > 8) {
  54. bits_per_pixel = max(max3(si->red_size + si->red_pos,
  55. si->green_size + si->green_pos,
  56. si->blue_size + si->blue_pos),
  57. si->rsvd_size + si->rsvd_pos);
  58. bits_per_pixel = max_t(u32, bits_per_pixel, si->lfb_depth);
  59. } else {
  60. bits_per_pixel = si->lfb_depth;
  61. }
  62. for (i = 0; i < ARRAY_SIZE(formats); ++i) {
  63. const struct simplefb_format *f = &formats[i];
  64. if (f->transp.length)
  65. continue; /* transparent formats are unsupported by VESA/EFI */
  66. if (bits_per_pixel == f->bits_per_pixel &&
  67. si->red_size == f->red.length &&
  68. si->red_pos == f->red.offset &&
  69. si->green_size == f->green.length &&
  70. si->green_pos == f->green.offset &&
  71. si->blue_size == f->blue.length &&
  72. si->blue_pos == f->blue.offset) {
  73. mode->format = f->name;
  74. mode->width = si->lfb_width;
  75. mode->height = si->lfb_height;
  76. mode->stride = si->lfb_linelength;
  77. return true;
  78. }
  79. }
  80. return false;
  81. }
  82. __init struct platform_device *sysfb_create_simplefb(const struct screen_info *si,
  83. const struct simplefb_platform_data *mode,
  84. struct device *parent)
  85. {
  86. struct platform_device *pd;
  87. struct resource res;
  88. u64 base, size;
  89. u32 length;
  90. int ret;
  91. /*
  92. * If the 64BIT_BASE capability is set, ext_lfb_base will contain the
  93. * upper half of the base address. Assemble the address, then make sure
  94. * it is valid and we can actually access it.
  95. */
  96. base = si->lfb_base;
  97. if (si->capabilities & VIDEO_CAPABILITY_64BIT_BASE)
  98. base |= (u64)si->ext_lfb_base << 32;
  99. if (!base || (u64)(resource_size_t)base != base) {
  100. printk(KERN_DEBUG "sysfb: inaccessible VRAM base\n");
  101. return ERR_PTR(-EINVAL);
  102. }
  103. /*
  104. * Don't use lfb_size as IORESOURCE size, since it may contain the
  105. * entire VMEM, and thus require huge mappings. Use just the part we
  106. * need, that is, the part where the framebuffer is located. But verify
  107. * that it does not exceed the advertised VMEM.
  108. * Note that in case of VBE, the lfb_size is shifted by 16 bits for
  109. * historical reasons.
  110. */
  111. size = si->lfb_size;
  112. if (si->orig_video_isVGA == VIDEO_TYPE_VLFB)
  113. size <<= 16;
  114. length = mode->height * mode->stride;
  115. if (length > size) {
  116. printk(KERN_WARNING "sysfb: VRAM smaller than advertised\n");
  117. return ERR_PTR(-EINVAL);
  118. }
  119. length = PAGE_ALIGN(length);
  120. /* setup IORESOURCE_MEM as framebuffer memory */
  121. memset(&res, 0, sizeof(res));
  122. res.flags = IORESOURCE_MEM;
  123. res.name = simplefb_resname;
  124. res.start = base;
  125. res.end = res.start + length - 1;
  126. if (res.end <= res.start)
  127. return ERR_PTR(-EINVAL);
  128. pd = platform_device_alloc("simple-framebuffer", 0);
  129. if (!pd)
  130. return ERR_PTR(-ENOMEM);
  131. pd->dev.parent = parent;
  132. sysfb_set_efifb_fwnode(pd);
  133. ret = platform_device_add_resources(pd, &res, 1);
  134. if (ret)
  135. goto err_put_device;
  136. ret = platform_device_add_data(pd, mode, sizeof(*mode));
  137. if (ret)
  138. goto err_put_device;
  139. ret = platform_device_add(pd);
  140. if (ret)
  141. goto err_put_device;
  142. return pd;
  143. err_put_device:
  144. platform_device_put(pd);
  145. return ERR_PTR(ret);
  146. }