vga.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* VGA: VESA LFB (from boot). */
  2. #include "vga.h"
  3. #include "pci.h"
  4. #include "ati.h"
  5. static unsigned int vga_pci_id; /* vendor(lo16) | device(hi16) */
  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. /* Find PCI VGA device and cache its ID (for non-ATI cards). */
  41. static void vga_find_pci_vga(void)
  42. {
  43. unsigned int bus, dev, id, class_rev;
  44. if (vga_pci_id)
  45. return;
  46. for (bus = 0; bus < 4; bus++) {
  47. for (dev = 0; dev < 32; dev++) {
  48. id = pci_cfg_read32(bus, dev, 0, 0x00);
  49. if (id == 0xFFFFFFFF || id == 0)
  50. continue;
  51. class_rev = pci_cfg_read32(bus, dev, 0, 0x08);
  52. if ((class_rev >> 24) == 0x03) {
  53. vga_pci_id = id;
  54. return;
  55. }
  56. }
  57. }
  58. }
  59. static volatile struct vga_boot_fb_info *vga_get_boot_fb(void)
  60. {
  61. volatile struct vga_boot_fb_info *p = VGA_BOOT_FB;
  62. __asm__ volatile ("" : "+r"(p));
  63. return p;
  64. }
  65. static void vga_cache_boot_fb(void)
  66. {
  67. volatile struct vga_boot_fb_info *info;
  68. if (vga_fb_cached)
  69. return;
  70. vga_fb_cached = 1;
  71. info = vga_get_boot_fb();
  72. if (!info->valid) {
  73. vga_fb_w = vga_fb_h = vga_fb_pitch_val = vga_fb_bpp_val = 0;
  74. vga_fb_lfb = 0;
  75. return;
  76. }
  77. vga_fb_w = info->width;
  78. vga_fb_h = info->height;
  79. vga_fb_pitch_val = info->pitch;
  80. vga_fb_bpp_val = info->bpp;
  81. /* Some BIOSes (e.g. 1024x768) report pitch in pixels or too small; enforce minimum. */
  82. {
  83. unsigned int min_pitch = vga_fb_w;
  84. if (vga_fb_bpp_val == 16)
  85. min_pitch *= 2;
  86. else if (vga_fb_bpp_val == 24)
  87. min_pitch *= 3;
  88. else if (vga_fb_bpp_val == 32)
  89. min_pitch *= 4;
  90. if (vga_fb_pitch_val < min_pitch)
  91. vga_fb_pitch_val = min_pitch;
  92. }
  93. vga_fb_lfb = (volatile unsigned char *)(unsigned long)info->lfb_base;
  94. vga_fb_vram_kb_val = (unsigned int)info->vram_64k * 64;
  95. /* Clamp height to what fits in VRAM (prevents wrap on small VRAM cards) */
  96. if (vga_fb_vram_kb_val > 0 && vga_fb_pitch_val > 0) {
  97. unsigned int vram_bytes = vga_fb_vram_kb_val * 1024;
  98. unsigned int max_h = vram_bytes / vga_fb_pitch_val;
  99. if (max_h < vga_fb_h)
  100. vga_fb_h = max_h;
  101. }
  102. vga_reset_crtc();
  103. ati_init();
  104. vga_find_pci_vga();
  105. if (ati_pci_id())
  106. vga_pci_id = ati_pci_id();
  107. }
  108. /* ---- LFB API ---- */
  109. unsigned int vga_fb_width(void)
  110. {
  111. vga_cache_boot_fb();
  112. return vga_fb_w;
  113. }
  114. unsigned int vga_fb_height(void)
  115. {
  116. vga_cache_boot_fb();
  117. return vga_fb_h;
  118. }
  119. unsigned int vga_fb_pitch(void)
  120. {
  121. vga_cache_boot_fb();
  122. return vga_fb_pitch_val;
  123. }
  124. unsigned int vga_fb_bpp(void)
  125. {
  126. vga_cache_boot_fb();
  127. return vga_fb_bpp_val;
  128. }
  129. unsigned int vga_fb_vram_kb(void)
  130. {
  131. vga_cache_boot_fb();
  132. return vga_fb_vram_kb_val;
  133. }
  134. unsigned int vga_fb_pci_id(void)
  135. {
  136. vga_cache_boot_fb();
  137. return vga_pci_id;
  138. }
  139. void *vga_fb_ptr(void)
  140. {
  141. vga_cache_boot_fb();
  142. return (void *)vga_fb_lfb;
  143. }
  144. int vga_has_vesa(void)
  145. {
  146. vga_cache_boot_fb();
  147. return (vga_fb_w != 0 && vga_fb_lfb != 0);
  148. }
  149. /* 16bpp: RGB 5:6:5. 32bpp: BGRX byte order (standard VBE / QEMU). */
  150. static void vga_lfb_put_pixel(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
  151. {
  152. unsigned int pitch = vga_fb_pitch_val;
  153. unsigned int bpp = vga_fb_bpp_val;
  154. unsigned char *p;
  155. if (x >= vga_fb_w || y >= vga_fb_h || !vga_fb_lfb)
  156. return;
  157. p = (unsigned char *)vga_fb_lfb + (unsigned long)y * pitch;
  158. if (bpp == 16) {
  159. unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  160. unsigned int off = x * 2;
  161. p[off] = (unsigned char)(rgb16 & 0xFF);
  162. p[off + 1] = (unsigned char)(rgb16 >> 8);
  163. } else if (bpp == 24) {
  164. unsigned int off = x * 3;
  165. p[off] = (unsigned char)b;
  166. p[off + 1] = (unsigned char)g;
  167. p[off + 2] = (unsigned char)r;
  168. } else if (bpp == 32) {
  169. unsigned int off = x * 4;
  170. p[off] = (unsigned char)b;
  171. p[off + 1] = (unsigned char)g;
  172. p[off + 2] = (unsigned char)r;
  173. p[off + 3] = 0;
  174. }
  175. }
  176. void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
  177. {
  178. vga_cache_boot_fb();
  179. if (!vga_fb_lfb)
  180. return;
  181. vga_lfb_put_pixel(x, y, r, g, b);
  182. }
  183. void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b)
  184. {
  185. unsigned int pitch = vga_fb_pitch_val;
  186. unsigned int bpp = vga_fb_bpp_val;
  187. vga_cache_boot_fb();
  188. if (!vga_fb_lfb)
  189. return;
  190. if (bpp == 16) {
  191. unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  192. unsigned short val = (unsigned short)((rgb16 & 0xFF) | ((rgb16 >> 8) << 8));
  193. volatile unsigned short *fb = (volatile unsigned short *)vga_fb_lfb;
  194. unsigned int n = (pitch / 2) * vga_fb_h;
  195. unsigned int i;
  196. for (i = 0; i < n; i++)
  197. fb[i] = val;
  198. } else if (bpp == 24) {
  199. volatile unsigned char *p = vga_fb_lfb;
  200. unsigned int row, x;
  201. for (row = 0; row < vga_fb_h; row++) {
  202. unsigned int row_off = row * pitch;
  203. for (x = 0; x < vga_fb_w; x++) {
  204. unsigned int off = row_off + x * 3;
  205. p[off] = (unsigned char)b;
  206. p[off + 1] = (unsigned char)g;
  207. p[off + 2] = (unsigned char)r;
  208. }
  209. }
  210. } else if (bpp == 32) {
  211. volatile unsigned int *fb = (volatile unsigned int *)vga_fb_lfb;
  212. unsigned int pixel = ((unsigned int)r << 16) | ((unsigned int)g << 8) | b;
  213. unsigned int stride = pitch / 4;
  214. unsigned int row, x;
  215. for (row = 0; row < vga_fb_h; row++)
  216. for (x = 0; x < vga_fb_w; x++)
  217. fb[row * stride + x] = pixel;
  218. }
  219. }