screen_info_generic.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/export.h>
  3. #include <linux/ioport.h>
  4. #include <linux/screen_info.h>
  5. #include <linux/string.h>
  6. #include <video/pixel_format.h>
  7. static void resource_init_named(struct resource *r,
  8. resource_size_t start, resource_size_t size,
  9. const char *name, unsigned int flags)
  10. {
  11. memset(r, 0, sizeof(*r));
  12. r->start = start;
  13. r->end = start + size - 1;
  14. r->name = name;
  15. r->flags = flags;
  16. }
  17. static void resource_init_io_named(struct resource *r,
  18. resource_size_t start, resource_size_t size,
  19. const char *name)
  20. {
  21. resource_init_named(r, start, size, name, IORESOURCE_IO);
  22. }
  23. static void resource_init_mem_named(struct resource *r,
  24. resource_size_t start, resource_size_t size,
  25. const char *name)
  26. {
  27. resource_init_named(r, start, size, name, IORESOURCE_MEM);
  28. }
  29. static inline bool __screen_info_has_ega_gfx(unsigned int mode)
  30. {
  31. switch (mode) {
  32. case 0x0d: /* 320x200-4 */
  33. case 0x0e: /* 640x200-4 */
  34. case 0x0f: /* 640x350-1 */
  35. case 0x10: /* 640x350-4 */
  36. return true;
  37. default:
  38. return false;
  39. }
  40. }
  41. static inline bool __screen_info_has_vga_gfx(unsigned int mode)
  42. {
  43. switch (mode) {
  44. case 0x10: /* 640x480-1 */
  45. case 0x12: /* 640x480-4 */
  46. case 0x13: /* 320-200-8 */
  47. case 0x6a: /* 800x600-4 (VESA) */
  48. return true;
  49. default:
  50. return __screen_info_has_ega_gfx(mode);
  51. }
  52. }
  53. /**
  54. * screen_info_resources() - Get resources from screen_info structure
  55. * @si: the screen_info
  56. * @r: pointer to an array of resource structures
  57. * @num: number of elements in @r:
  58. *
  59. * Returns:
  60. * The number of resources stored in @r on success, or a negative errno code otherwise.
  61. *
  62. * A call to screen_info_resources() returns the resources consumed by the
  63. * screen_info's device or framebuffer. The result is stored in the caller-supplied
  64. * array @r with up to @num elements. The function returns the number of
  65. * initialized elements.
  66. */
  67. ssize_t screen_info_resources(const struct screen_info *si, struct resource *r, size_t num)
  68. {
  69. struct resource *pos = r;
  70. unsigned int type = screen_info_video_type(si);
  71. u64 base, size;
  72. switch (type) {
  73. case VIDEO_TYPE_MDA:
  74. if (num > 0)
  75. resource_init_io_named(pos++, 0x3b0, 12, "mda");
  76. if (num > 1)
  77. resource_init_io_named(pos++, 0x3bf, 0x01, "mda");
  78. if (num > 2)
  79. resource_init_mem_named(pos++, 0xb0000, 0x2000, "mda");
  80. break;
  81. case VIDEO_TYPE_CGA:
  82. if (num > 0)
  83. resource_init_io_named(pos++, 0x3d4, 0x02, "cga");
  84. if (num > 1)
  85. resource_init_mem_named(pos++, 0xb8000, 0x2000, "cga");
  86. break;
  87. case VIDEO_TYPE_EGAM:
  88. if (num > 0)
  89. resource_init_io_named(pos++, 0x3bf, 0x10, "ega");
  90. if (num > 1)
  91. resource_init_mem_named(pos++, 0xb0000, 0x8000, "ega");
  92. break;
  93. case VIDEO_TYPE_EGAC:
  94. if (num > 0)
  95. resource_init_io_named(pos++, 0x3c0, 0x20, "ega");
  96. if (num > 1) {
  97. if (__screen_info_has_ega_gfx(si->orig_video_mode))
  98. resource_init_mem_named(pos++, 0xa0000, 0x10000, "ega");
  99. else
  100. resource_init_mem_named(pos++, 0xb8000, 0x8000, "ega");
  101. }
  102. break;
  103. case VIDEO_TYPE_VGAC:
  104. if (num > 0)
  105. resource_init_io_named(pos++, 0x3c0, 0x20, "vga+");
  106. if (num > 1) {
  107. if (__screen_info_has_vga_gfx(si->orig_video_mode))
  108. resource_init_mem_named(pos++, 0xa0000, 0x10000, "vga+");
  109. else
  110. resource_init_mem_named(pos++, 0xb8000, 0x8000, "vga+");
  111. }
  112. break;
  113. case VIDEO_TYPE_VLFB:
  114. case VIDEO_TYPE_EFI:
  115. base = __screen_info_lfb_base(si);
  116. if (!base)
  117. break;
  118. size = __screen_info_lfb_size(si, type);
  119. if (!size)
  120. break;
  121. if (num > 0)
  122. resource_init_mem_named(pos++, base, size, "lfb");
  123. break;
  124. case VIDEO_TYPE_PICA_S3:
  125. case VIDEO_TYPE_MIPS_G364:
  126. case VIDEO_TYPE_SGI:
  127. case VIDEO_TYPE_TGAC:
  128. case VIDEO_TYPE_SUN:
  129. case VIDEO_TYPE_SUNPCI:
  130. case VIDEO_TYPE_PMAC:
  131. default:
  132. /* not supported */
  133. return -EINVAL;
  134. }
  135. return pos - r;
  136. }
  137. EXPORT_SYMBOL(screen_info_resources);
  138. /*
  139. * The meaning of depth and bpp for direct-color formats is
  140. * inconsistent:
  141. *
  142. * - DRM format info specifies depth as the number of color
  143. * bits; including alpha, but not including filler bits.
  144. * - Linux' EFI platform code computes lfb_depth from the
  145. * individual color channels, including the reserved bits.
  146. * - VBE 1.1 defines lfb_depth for XRGB1555 as 16, but later
  147. * versions use 15.
  148. * - On the kernel command line, 'bpp' of 32 is usually
  149. * XRGB8888 including the filler bits, but 15 is XRGB1555
  150. * not including the filler bit.
  151. *
  152. * It is not easily possible to fix this in struct screen_info,
  153. * as this could break UAPI. The best solution is to compute
  154. * bits_per_pixel from the color bits, reserved bits and
  155. * reported lfb_depth, whichever is highest.
  156. */
  157. u32 __screen_info_lfb_bits_per_pixel(const struct screen_info *si)
  158. {
  159. u32 bits_per_pixel = si->lfb_depth;
  160. if (bits_per_pixel > 8) {
  161. bits_per_pixel = max(max3(si->red_size + si->red_pos,
  162. si->green_size + si->green_pos,
  163. si->blue_size + si->blue_pos),
  164. si->rsvd_size + si->rsvd_pos);
  165. bits_per_pixel = max_t(u32, bits_per_pixel, si->lfb_depth);
  166. }
  167. return bits_per_pixel;
  168. }
  169. EXPORT_SYMBOL(__screen_info_lfb_bits_per_pixel);
  170. static int __screen_info_lfb_pixel_format(const struct screen_info *si, struct pixel_format *f)
  171. {
  172. u32 bits_per_pixel = __screen_info_lfb_bits_per_pixel(si);
  173. if (bits_per_pixel > U8_MAX)
  174. return -EINVAL;
  175. f->bits_per_pixel = bits_per_pixel;
  176. if (si->lfb_depth > 8) {
  177. f->indexed = false;
  178. f->alpha.offset = 0;
  179. f->alpha.length = 0;
  180. f->red.offset = si->red_pos;
  181. f->red.length = si->red_size;
  182. f->green.offset = si->green_pos;
  183. f->green.length = si->green_size;
  184. f->blue.offset = si->blue_pos;
  185. f->blue.length = si->blue_size;
  186. } else {
  187. f->indexed = true;
  188. f->index.offset = 0;
  189. f->index.length = si->lfb_depth;
  190. }
  191. return 0;
  192. }
  193. /**
  194. * screen_info_pixel_format - Returns the screen-info format as pixel-format description
  195. *
  196. * @si: the screen_info
  197. * @f: pointer to return pixel-format description
  198. *
  199. * Returns:
  200. * 0 on success, or a negative errno code otherwise.
  201. */
  202. int screen_info_pixel_format(const struct screen_info *si, struct pixel_format *f)
  203. {
  204. unsigned int type = screen_info_video_type(si);
  205. /* TODO: Add support for additional types as needed. */
  206. switch (type) {
  207. case VIDEO_TYPE_VLFB:
  208. case VIDEO_TYPE_EFI:
  209. return __screen_info_lfb_pixel_format(si, f);
  210. }
  211. /* not supported */
  212. return -EINVAL;
  213. }
  214. EXPORT_SYMBOL(screen_info_pixel_format);