sysfb_simplefb.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Generic System Framebuffers
  4. * Copyright (c) 2012-2013 David Herrmann <[email protected]>
  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. const struct simplefb_format *f;
  27. __u8 type;
  28. unsigned int i;
  29. type = si->orig_video_isVGA;
  30. if (type != VIDEO_TYPE_VLFB && type != VIDEO_TYPE_EFI)
  31. return false;
  32. for (i = 0; i < ARRAY_SIZE(formats); ++i) {
  33. f = &formats[i];
  34. if (si->lfb_depth == f->bits_per_pixel &&
  35. si->red_size == f->red.length &&
  36. si->red_pos == f->red.offset &&
  37. si->green_size == f->green.length &&
  38. si->green_pos == f->green.offset &&
  39. si->blue_size == f->blue.length &&
  40. si->blue_pos == f->blue.offset &&
  41. si->rsvd_size == f->transp.length &&
  42. si->rsvd_pos == f->transp.offset) {
  43. mode->format = f->name;
  44. mode->width = si->lfb_width;
  45. mode->height = si->lfb_height;
  46. mode->stride = si->lfb_linelength;
  47. return true;
  48. }
  49. }
  50. return false;
  51. }
  52. __init struct platform_device *sysfb_create_simplefb(const struct screen_info *si,
  53. const struct simplefb_platform_data *mode)
  54. {
  55. struct platform_device *pd;
  56. struct resource res;
  57. u64 base, size;
  58. u32 length;
  59. int ret;
  60. /*
  61. * If the 64BIT_BASE capability is set, ext_lfb_base will contain the
  62. * upper half of the base address. Assemble the address, then make sure
  63. * it is valid and we can actually access it.
  64. */
  65. base = si->lfb_base;
  66. if (si->capabilities & VIDEO_CAPABILITY_64BIT_BASE)
  67. base |= (u64)si->ext_lfb_base << 32;
  68. if (!base || (u64)(resource_size_t)base != base) {
  69. printk(KERN_DEBUG "sysfb: inaccessible VRAM base\n");
  70. return ERR_PTR(-EINVAL);
  71. }
  72. /*
  73. * Don't use lfb_size as IORESOURCE size, since it may contain the
  74. * entire VMEM, and thus require huge mappings. Use just the part we
  75. * need, that is, the part where the framebuffer is located. But verify
  76. * that it does not exceed the advertised VMEM.
  77. * Note that in case of VBE, the lfb_size is shifted by 16 bits for
  78. * historical reasons.
  79. */
  80. size = si->lfb_size;
  81. if (si->orig_video_isVGA == VIDEO_TYPE_VLFB)
  82. size <<= 16;
  83. length = mode->height * mode->stride;
  84. if (length > size) {
  85. printk(KERN_WARNING "sysfb: VRAM smaller than advertised\n");
  86. return ERR_PTR(-EINVAL);
  87. }
  88. length = PAGE_ALIGN(length);
  89. /* setup IORESOURCE_MEM as framebuffer memory */
  90. memset(&res, 0, sizeof(res));
  91. res.flags = IORESOURCE_MEM;
  92. res.name = simplefb_resname;
  93. res.start = base;
  94. res.end = res.start + length - 1;
  95. if (res.end <= res.start)
  96. return ERR_PTR(-EINVAL);
  97. pd = platform_device_alloc("simple-framebuffer", 0);
  98. if (!pd)
  99. return ERR_PTR(-ENOMEM);
  100. sysfb_set_efifb_fwnode(pd);
  101. ret = platform_device_add_resources(pd, &res, 1);
  102. if (ret)
  103. goto err_put_device;
  104. ret = platform_device_add_data(pd, mode, sizeof(*mode));
  105. if (ret)
  106. goto err_put_device;
  107. ret = platform_device_add(pd);
  108. if (ret)
  109. goto err_put_device;
  110. return pd;
  111. err_put_device:
  112. platform_device_put(pd);
  113. return ERR_PTR(ret);
  114. }