cirrus.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* Cirrus Logic GD5446 BitBLT acceleration.
  2. Reference: QEMU hw/display/cirrus_vga.c (register offsets, ROP codes). */
  3. #include "cirrus.h"
  4. #include "cursor.h"
  5. #include "pci.h"
  6. #include "vga.h"
  7. /* Sequencer (0x3C4/0x3C5). */
  8. #define SR_UNLOCK 0x06
  9. #define SR_UNLOCK_VAL 0x12
  10. #define SR_CURSOR_X 0x10
  11. #define SR_CURSOR_Y 0x11
  12. #define SR_CURSOR_ATTR 0x12
  13. #define SR_CURSOR_PAT 0x13
  14. #define CURSOR_SHOW 0x01
  15. #define CURSOR_HIDDENPEL 0x02
  16. /* Graphics controller BitBLT registers (0x3CE/0x3CF). */
  17. #define GR_WIDTH_LO 0x20
  18. #define GR_WIDTH_HI 0x21
  19. #define GR_HEIGHT_LO 0x22
  20. #define GR_HEIGHT_HI 0x23
  21. #define GR_DSTPITCH_LO 0x24
  22. #define GR_DSTPITCH_HI 0x25
  23. #define GR_SRCPITCH_LO 0x26
  24. #define GR_SRCPITCH_HI 0x27
  25. #define GR_DSTADDR_LO 0x28
  26. #define GR_DSTADDR_MID 0x29
  27. #define GR_DSTADDR_HI 0x2a
  28. #define GR_SRCADDR_LO 0x2c
  29. #define GR_SRCADDR_MID 0x2d
  30. #define GR_SRCADDR_HI 0x2e
  31. #define GR_MODE 0x30
  32. #define GR_STATUS 0x31
  33. #define GR_ROP 0x32
  34. #define GR_MODEEXT 0x33
  35. #define BLT_BUSY 0x01
  36. #define BLT_START 0x02
  37. #define BLTMODE_BACKWARDS 0x01
  38. #define BLTMODE_PIXELWIDTH_MASK 0x30
  39. #define BLTMODE_PIXELWIDTH16 0x10
  40. #define BLTMODE_PIXELWIDTH24 0x20
  41. #define BLTMODE_PIXELWIDTH32 0x30
  42. #define BLTMODE_PATTERNCOPY 0x40
  43. #define BLTMODE_COLOREXPAND 0x80
  44. #define BLTMODEEXT_SOLIDFILL 0x04
  45. #define ROP_SRC 0x0d
  46. static int cirrus_present;
  47. static void gr_write(unsigned char idx, unsigned char val)
  48. {
  49. port_outb(0x3CE, idx);
  50. port_outb(0x3CF, val);
  51. }
  52. static unsigned char gr_read(unsigned char idx)
  53. {
  54. port_outb(0x3CE, idx);
  55. return port_inb(0x3CF);
  56. }
  57. static void sr_write(unsigned char idx, unsigned char val)
  58. {
  59. port_outb(0x3C4, idx);
  60. port_outb(0x3C5, val);
  61. }
  62. static unsigned char sr_read(unsigned char idx)
  63. {
  64. port_outb(0x3C4, idx);
  65. return port_inb(0x3C5);
  66. }
  67. void cirrus_init(void)
  68. {
  69. cirrus_present = 0;
  70. sr_write(SR_UNLOCK, SR_UNLOCK_VAL);
  71. if (sr_read(SR_UNLOCK) == SR_UNLOCK_VAL)
  72. cirrus_present = 1;
  73. }
  74. int cirrus_available(void)
  75. {
  76. return cirrus_present;
  77. }
  78. void cirrus_blit(unsigned int sx, unsigned int sy,
  79. unsigned int dx, unsigned int dy,
  80. unsigned int w, unsigned int h)
  81. {
  82. unsigned int bpp = vga_fb_bpp();
  83. unsigned int pb = (bpp == 16) ? 2 : (bpp == 24) ? 3 : 4;
  84. unsigned int pitch = vga_fb_pitch();
  85. unsigned int src = sy * pitch + sx * pb;
  86. unsigned int dst = dy * pitch + dx * pb;
  87. unsigned int width_bytes = w * pb;
  88. unsigned int mode = 0;
  89. if (bpp == 16)
  90. mode |= BLTMODE_PIXELWIDTH16;
  91. else if (bpp == 24)
  92. mode |= BLTMODE_PIXELWIDTH24;
  93. else
  94. mode |= BLTMODE_PIXELWIDTH32;
  95. if (dst > src) {
  96. /* Overlapping, moving down/right: walk from the bottom-right so the
  97. source isn't clobbered. The engine runs backward with a positive
  98. pitch written; the address points at the last byte of the region. */
  99. mode |= BLTMODE_BACKWARDS;
  100. src += (h - 1) * pitch + (width_bytes - 1);
  101. dst += (h - 1) * pitch + (width_bytes - 1);
  102. }
  103. gr_write(GR_WIDTH_LO, (unsigned char)((width_bytes - 1) & 0xFF));
  104. gr_write(GR_WIDTH_HI, (unsigned char)(((width_bytes - 1) >> 8) & 0xFF));
  105. gr_write(GR_HEIGHT_LO, (unsigned char)((h - 1) & 0xFF));
  106. gr_write(GR_HEIGHT_HI, (unsigned char)(((h - 1) >> 8) & 0xFF));
  107. gr_write(GR_DSTPITCH_LO, (unsigned char)(pitch & 0xFF));
  108. gr_write(GR_DSTPITCH_HI, (unsigned char)((pitch >> 8) & 0xFF));
  109. gr_write(GR_SRCPITCH_LO, (unsigned char)(pitch & 0xFF));
  110. gr_write(GR_SRCPITCH_HI, (unsigned char)((pitch >> 8) & 0xFF));
  111. gr_write(GR_DSTADDR_LO, (unsigned char)(dst & 0xFF));
  112. gr_write(GR_DSTADDR_MID, (unsigned char)((dst >> 8) & 0xFF));
  113. gr_write(GR_DSTADDR_HI, (unsigned char)((dst >> 16) & 0xFF));
  114. gr_write(GR_SRCADDR_LO, (unsigned char)(src & 0xFF));
  115. gr_write(GR_SRCADDR_MID, (unsigned char)((src >> 8) & 0xFF));
  116. gr_write(GR_SRCADDR_HI, (unsigned char)((src >> 16) & 0xFF));
  117. gr_write(GR_MODE, (unsigned char)mode);
  118. gr_write(GR_ROP, ROP_SRC);
  119. gr_write(GR_MODEEXT, 0);
  120. gr_write(GR_STATUS, 0); /* ensure START is clear so the write triggers */
  121. gr_write(GR_STATUS, BLT_START);
  122. while (gr_read(GR_STATUS) & BLT_BUSY)
  123. ;
  124. }
  125. void cirrus_fill(unsigned int x, unsigned int y,
  126. unsigned int w, unsigned int h,
  127. unsigned int r, unsigned int g, unsigned int b)
  128. {
  129. unsigned int bpp = vga_fb_bpp();
  130. unsigned int pb = (bpp == 16) ? 2 : (bpp == 24) ? 3 : 4;
  131. unsigned int pitch = vga_fb_pitch();
  132. unsigned int dst = y * pitch + x * pb;
  133. unsigned int width_bytes = w * pb;
  134. unsigned int mode = BLTMODE_PATTERNCOPY | BLTMODE_COLOREXPAND;
  135. /* Set the foreground color (the solid fill source). */
  136. if (bpp == 16) {
  137. unsigned short rgb565 = (unsigned short)
  138. (((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3));
  139. mode |= BLTMODE_PIXELWIDTH16;
  140. gr_write(0x01, (unsigned char)(rgb565 & 0xFF));
  141. gr_write(0x11, (unsigned char)((rgb565 >> 8) & 0xFF));
  142. } else if (bpp == 24) {
  143. mode |= BLTMODE_PIXELWIDTH24;
  144. gr_write(0x01, (unsigned char)b);
  145. gr_write(0x11, (unsigned char)g);
  146. gr_write(0x13, (unsigned char)r);
  147. } else {
  148. mode |= BLTMODE_PIXELWIDTH32;
  149. gr_write(0x01, (unsigned char)b);
  150. gr_write(0x11, (unsigned char)g);
  151. gr_write(0x13, (unsigned char)r);
  152. gr_write(0x15, 0);
  153. }
  154. gr_write(GR_WIDTH_LO, (unsigned char)((width_bytes - 1) & 0xFF));
  155. gr_write(GR_WIDTH_HI, (unsigned char)(((width_bytes - 1) >> 8) & 0xFF));
  156. gr_write(GR_HEIGHT_LO, (unsigned char)((h - 1) & 0xFF));
  157. gr_write(GR_HEIGHT_HI, (unsigned char)(((h - 1) >> 8) & 0xFF));
  158. gr_write(GR_DSTPITCH_LO, (unsigned char)(pitch & 0xFF));
  159. gr_write(GR_DSTPITCH_HI, (unsigned char)((pitch >> 8) & 0xFF));
  160. gr_write(GR_DSTADDR_LO, (unsigned char)(dst & 0xFF));
  161. gr_write(GR_DSTADDR_MID, (unsigned char)((dst >> 8) & 0xFF));
  162. gr_write(GR_DSTADDR_HI, (unsigned char)((dst >> 16) & 0xFF));
  163. gr_write(GR_MODE, (unsigned char)mode);
  164. gr_write(GR_ROP, ROP_SRC);
  165. gr_write(GR_MODEEXT, BLTMODEEXT_SOLIDFILL);
  166. gr_write(GR_STATUS, 0); /* ensure START is clear so the write triggers */
  167. gr_write(GR_STATUS, BLT_START);
  168. while (gr_read(GR_STATUS) & BLT_BUSY)
  169. ;
  170. }
  171. /* 32x32 two-plane cursor: AND plane (offset 0..127), XOR plane (128..255),
  172. * 4 bytes per row, the image in the top-left of the box:
  173. * AND=0 XOR=0 -> transparent
  174. * AND=0 XOR=1 -> color0 (white)
  175. * AND=1 XOR=1 -> color1 (black)
  176. * The hidden DAC entries 0 and 15 are programmed white and black. The ARGB
  177. * image is reduced to that: alpha >= 50% is opaque, and the (premultiplied)
  178. * luminance picks white or black. */
  179. void cirrus_cursor_load(const struct cursor_image *img)
  180. {
  181. unsigned int vram = vga_fb_vram_kb() * 1024;
  182. unsigned char *dst;
  183. unsigned int r, c, i;
  184. dst = (unsigned char *)(vga_fb_lfb_addr() + vram - 16384);
  185. for (i = 0; i < 256; i++)
  186. dst[i] = 0;
  187. if (!img || !img->px)
  188. return;
  189. for (r = 0; r < img->h && r < 32; r++)
  190. for (c = 0; c < img->w && c < 32; c++) {
  191. unsigned int p = img->px[r * img->w + c];
  192. unsigned int a = p >> 24;
  193. unsigned int lum = (((p >> 16) & 0xFF) * 3 +
  194. ((p >> 8) & 0xFF) * 6 +
  195. (p & 0xFF)) / 10;
  196. unsigned char bit = (unsigned char)(0x80u >> (c & 7));
  197. if (a < 128)
  198. continue;
  199. dst[128 + r * 4 + c / 8] |= bit; /* XOR: opaque */
  200. if (lum * 2 < a)
  201. dst[r * 4 + c / 8] |= bit; /* AND: black */
  202. }
  203. }
  204. void cirrus_cursor_setup(const struct cursor_image *img)
  205. {
  206. /* SR[0x13] = 0 selects the first 256-byte block at the top of VRAM. */
  207. sr_write(SR_CURSOR_PAT, 0);
  208. cirrus_cursor_load(img);
  209. /* Cursor colors via the hidden DAC palette: entry 0 = white, 15 = black. */
  210. sr_write(SR_CURSOR_ATTR, CURSOR_HIDDENPEL);
  211. port_outb(0x3C8, 0);
  212. port_outb(0x3C9, 63); port_outb(0x3C9, 63); port_outb(0x3C9, 63);
  213. port_outb(0x3C8, 15);
  214. port_outb(0x3C9, 0); port_outb(0x3C9, 0); port_outb(0x3C9, 0);
  215. sr_write(SR_CURSOR_ATTR, CURSOR_SHOW);
  216. }
  217. /* (x,y) is the image's top-left. The Cirrus position cannot go negative,
  218. * so near the top/left edge the image is clamped (the hotspot is then off
  219. * by up to its offset). */
  220. void cirrus_cursor_move(int x, int y)
  221. {
  222. unsigned int ux = x < 0 ? 0u : (unsigned int)x;
  223. unsigned int uy = y < 0 ? 0u : (unsigned int)y;
  224. /* Position is 11 bits; low 3 bits select the index alias, high 8 are data. */
  225. sr_write(SR_CURSOR_X | ((ux & 0x7) << 5), (unsigned char)(ux >> 3));
  226. sr_write(SR_CURSOR_Y | ((uy & 0x7) << 5), (unsigned char)(uy >> 3));
  227. }
  228. void cirrus_cursor_show(int show)
  229. {
  230. unsigned char v = sr_read(SR_CURSOR_ATTR);
  231. if (show)
  232. v |= CURSOR_SHOW;
  233. else
  234. v &= ~CURSOR_SHOW;
  235. sr_write(SR_CURSOR_ATTR, v);
  236. }