vga.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* VGA: VESA LFB (from boot). */
  2. #include "vga.h"
  3. #include "pci.h"
  4. #include "gpu.h"
  5. #include "ati.h"
  6. /* Cached boot framebuffer info (set once when vga_fb_* first used). */
  7. static unsigned int vga_fb_w, vga_fb_h, vga_fb_pitch_val, vga_fb_bpp_val;
  8. static unsigned int vga_fb_vram_kb_val;
  9. static volatile unsigned char *vga_fb_lfb;
  10. static int vga_fb_cached;
  11. /*
  12. * Force CRTC Start Address to 0 and Line Compare to max (0x3FF).
  13. * Fixes real hardware where VBE mode set leaves stale CRTC state,
  14. * causing the display to appear rotated (bottom portion at top).
  15. */
  16. static void vga_reset_crtc(void)
  17. {
  18. unsigned char v;
  19. /* Start Address High/Low = 0 */
  20. port_outb(0x3D4, 0x0C);
  21. port_outb(0x3D5, 0x00);
  22. port_outb(0x3D4, 0x0D);
  23. port_outb(0x3D5, 0x00);
  24. /* Unlock CRTC registers 0-7: clear bit 7 of register 0x11 */
  25. port_outb(0x3D4, 0x11);
  26. v = port_inb(0x3D5);
  27. port_outb(0x3D5, v & 0x7F);
  28. /* Line Compare bits 7:0 = 0xFF (register 0x18) */
  29. port_outb(0x3D4, 0x18);
  30. port_outb(0x3D5, 0xFF);
  31. /* Line Compare bit 8 = 1 (register 0x07, bit 4) */
  32. port_outb(0x3D4, 0x07);
  33. v = port_inb(0x3D5);
  34. port_outb(0x3D5, v | 0x10);
  35. /* Line Compare bit 9 = 1 (register 0x09, bit 6) */
  36. port_outb(0x3D4, 0x09);
  37. v = port_inb(0x3D5);
  38. port_outb(0x3D5, v | 0x40);
  39. }
  40. static volatile struct vga_boot_fb_info *vga_get_boot_fb(void)
  41. {
  42. volatile struct vga_boot_fb_info *p = VGA_BOOT_FB;
  43. __asm__ volatile ("" : "+r"(p));
  44. return p;
  45. }
  46. static void vga_cache_boot_fb(void)
  47. {
  48. volatile struct vga_boot_fb_info *info;
  49. if (vga_fb_cached)
  50. return;
  51. vga_fb_cached = 1;
  52. info = vga_get_boot_fb();
  53. if (!info->valid) {
  54. vga_fb_w = vga_fb_h = vga_fb_pitch_val = vga_fb_bpp_val = 0;
  55. vga_fb_lfb = 0;
  56. gpu_init();
  57. return;
  58. }
  59. vga_fb_w = info->width;
  60. vga_fb_h = info->height;
  61. vga_fb_pitch_val = info->pitch;
  62. vga_fb_bpp_val = info->bpp;
  63. /* Some BIOSes (e.g. 1024x768) report pitch in pixels or too small; enforce minimum. */
  64. {
  65. unsigned int min_pitch = vga_fb_w;
  66. if (vga_fb_bpp_val == 16)
  67. min_pitch *= 2;
  68. else if (vga_fb_bpp_val == 24)
  69. min_pitch *= 3;
  70. else if (vga_fb_bpp_val == 32)
  71. min_pitch *= 4;
  72. if (vga_fb_pitch_val < min_pitch)
  73. vga_fb_pitch_val = min_pitch;
  74. }
  75. vga_fb_lfb = (volatile unsigned char *)(unsigned long)info->lfb_base;
  76. vga_fb_vram_kb_val = (unsigned int)info->vram_64k * 64;
  77. /* Clamp height to what fits in VRAM (prevents wrap on small VRAM cards) */
  78. if (vga_fb_vram_kb_val > 0 && vga_fb_pitch_val > 0) {
  79. unsigned int vram_bytes = vga_fb_vram_kb_val * 1024;
  80. unsigned int max_h = vram_bytes / vga_fb_pitch_val;
  81. if (max_h < vga_fb_h)
  82. vga_fb_h = max_h;
  83. }
  84. gpu_init();
  85. if (gpu_vendor() == GPU_VENDOR_ATI)
  86. vga_reset_crtc();
  87. }
  88. /* ---- LFB API ---- */
  89. unsigned int vga_fb_width(void)
  90. {
  91. vga_cache_boot_fb();
  92. return vga_fb_w;
  93. }
  94. unsigned int vga_fb_height(void)
  95. {
  96. vga_cache_boot_fb();
  97. return vga_fb_h;
  98. }
  99. unsigned int vga_fb_pitch(void)
  100. {
  101. vga_cache_boot_fb();
  102. return vga_fb_pitch_val;
  103. }
  104. unsigned int vga_fb_bpp(void)
  105. {
  106. vga_cache_boot_fb();
  107. return vga_fb_bpp_val;
  108. }
  109. unsigned int vga_fb_vram_kb(void)
  110. {
  111. vga_cache_boot_fb();
  112. return vga_fb_vram_kb_val;
  113. }
  114. unsigned int vga_fb_pci_id(void)
  115. {
  116. vga_cache_boot_fb();
  117. return gpu_pci_id();
  118. }
  119. void *vga_fb_ptr(void)
  120. {
  121. vga_cache_boot_fb();
  122. return (void *)vga_fb_lfb;
  123. }
  124. int vga_has_vesa(void)
  125. {
  126. vga_cache_boot_fb();
  127. return (vga_fb_w != 0 && vga_fb_lfb != 0);
  128. }
  129. /* 16bpp: RGB 5:6:5. 32bpp: BGRX byte order (standard VBE / QEMU). */
  130. static void vga_lfb_put_pixel(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
  131. {
  132. unsigned int pitch = vga_fb_pitch_val;
  133. unsigned int bpp = vga_fb_bpp_val;
  134. unsigned char *p;
  135. if (x >= vga_fb_w || y >= vga_fb_h || !vga_fb_lfb)
  136. return;
  137. p = (unsigned char *)vga_fb_lfb + (unsigned long)y * pitch;
  138. if (bpp == 16) {
  139. unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  140. unsigned int off = x * 2;
  141. p[off] = (unsigned char)(rgb16 & 0xFF);
  142. p[off + 1] = (unsigned char)(rgb16 >> 8);
  143. } else if (bpp == 24) {
  144. unsigned int off = x * 3;
  145. p[off] = (unsigned char)b;
  146. p[off + 1] = (unsigned char)g;
  147. p[off + 2] = (unsigned char)r;
  148. } else if (bpp == 32) {
  149. unsigned int off = x * 4;
  150. p[off] = (unsigned char)b;
  151. p[off + 1] = (unsigned char)g;
  152. p[off + 2] = (unsigned char)r;
  153. p[off + 3] = 0;
  154. }
  155. }
  156. void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
  157. {
  158. vga_cache_boot_fb();
  159. if (!vga_fb_lfb)
  160. return;
  161. vga_lfb_put_pixel(x, y, r, g, b);
  162. }
  163. static unsigned int vga_pixel_bytes(void)
  164. {
  165. if (vga_fb_bpp_val == 16)
  166. return 2;
  167. if (vga_fb_bpp_val == 24)
  168. return 3;
  169. return 4;
  170. }
  171. void vga_fb_read_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
  172. unsigned char *buf)
  173. {
  174. unsigned int pitch, pb, row, col;
  175. unsigned char *fb;
  176. volatile unsigned char *src;
  177. vga_cache_boot_fb();
  178. if (!vga_fb_lfb || vga_fb_w == 0 || w == 0 || h == 0)
  179. return;
  180. pitch = vga_fb_pitch_val;
  181. pb = vga_pixel_bytes();
  182. fb = (unsigned char *)vga_fb_lfb;
  183. for (row = 0; row < h; row++) {
  184. src = fb + (unsigned long)(y + row) * pitch + x * pb;
  185. for (col = 0; col < w * pb; col++)
  186. buf[row * w * pb + col] = src[col];
  187. }
  188. }
  189. void vga_fb_write_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
  190. const unsigned char *buf)
  191. {
  192. unsigned int pitch, pb, row, col;
  193. volatile unsigned char *dst;
  194. vga_cache_boot_fb();
  195. if (!vga_fb_lfb || vga_fb_w == 0 || w == 0 || h == 0)
  196. return;
  197. pitch = vga_fb_pitch_val;
  198. pb = vga_pixel_bytes();
  199. for (row = 0; row < h; row++) {
  200. dst = vga_fb_lfb + (unsigned long)(y + row) * pitch + x * pb;
  201. for (col = 0; col < w * pb; col++)
  202. dst[col] = buf[row * w * pb + col];
  203. }
  204. }
  205. void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b)
  206. {
  207. unsigned int pitch = vga_fb_pitch_val;
  208. unsigned int bpp = vga_fb_bpp_val;
  209. vga_cache_boot_fb();
  210. if (!vga_fb_lfb)
  211. return;
  212. if (bpp == 16) {
  213. unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  214. unsigned short val = (unsigned short)((rgb16 & 0xFF) | ((rgb16 >> 8) << 8));
  215. volatile unsigned short *fb = (volatile unsigned short *)vga_fb_lfb;
  216. unsigned int n = (pitch / 2) * vga_fb_h;
  217. unsigned int i;
  218. for (i = 0; i < n; i++)
  219. fb[i] = val;
  220. } else if (bpp == 24) {
  221. volatile unsigned char *p = vga_fb_lfb;
  222. unsigned int row, x;
  223. for (row = 0; row < vga_fb_h; row++) {
  224. unsigned int row_off = row * pitch;
  225. for (x = 0; x < vga_fb_w; x++) {
  226. unsigned int off = row_off + x * 3;
  227. p[off] = (unsigned char)b;
  228. p[off + 1] = (unsigned char)g;
  229. p[off + 2] = (unsigned char)r;
  230. }
  231. }
  232. } else if (bpp == 32) {
  233. volatile unsigned int *fb = (volatile unsigned int *)vga_fb_lfb;
  234. unsigned int pixel = ((unsigned int)r << 16) | ((unsigned int)g << 8) | b;
  235. unsigned int stride = pitch / 4;
  236. unsigned int row, x;
  237. for (row = 0; row < vga_fb_h; row++)
  238. for (x = 0; x < vga_fb_w; x++)
  239. fb[row * stride + x] = pixel;
  240. }
  241. }