vgai81x.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "io.h"
  7. #include "../port/error.h"
  8. #define Image IMAGE
  9. #include <draw.h>
  10. #include <memdraw.h>
  11. #include <cursor.h>
  12. #include "screen.h"
  13. typedef struct
  14. {
  15. ushort ctl;
  16. ushort pad;
  17. ulong base;
  18. ulong pos;
  19. } CursorI81x;
  20. enum {
  21. Fbsize = 8*MB,
  22. hwCur = 0x70080,
  23. };
  24. static Pcidev *
  25. i81xpcimatch(void)
  26. {
  27. Pcidev *p;
  28. p = nil;
  29. while((p = pcimatch(p, 0x8086, 0)) != nil){
  30. switch(p->did){
  31. default:
  32. continue;
  33. case 0x7121:
  34. case 0x7123:
  35. case 0x7125:
  36. case 0x1102:
  37. case 0x1112:
  38. case 0x1132:
  39. case 0x3577: /* IBM R31 uses intel 830M chipset */
  40. return p;
  41. }
  42. }
  43. return nil;
  44. }
  45. static void
  46. i81xenable(VGAscr* scr)
  47. {
  48. Pcidev *p;
  49. int size;
  50. Mach *mach0;
  51. ulong *pgtbl, *rp, cursor, *pte, fbuf, fbend;
  52. if(scr->mmio)
  53. return;
  54. p = i81xpcimatch();
  55. if(p == nil)
  56. return;
  57. scr->mmio = vmap(p->mem[1].bar & ~0x0F, p->mem[1].size);
  58. if(scr->mmio == 0)
  59. return;
  60. addvgaseg("i81xmmio", p->mem[1].bar&~0x0F, p->mem[1].size);
  61. /* allocate page table */
  62. pgtbl = xspanalloc(64*1024, BY2PG, 0);
  63. scr->mmio[0x2020/4] = PADDR(pgtbl) | 1;
  64. size = p->mem[0].size;
  65. if(size > 0)
  66. size = Fbsize;
  67. vgalinearaddr(scr, p->mem[0].bar&~0xF, size);
  68. addvgaseg("i81xscreen", p->mem[0].bar&~0xF, size);
  69. /*
  70. * allocate backing store for frame buffer
  71. * and populate device page tables.
  72. */
  73. fbuf = PADDR(xspanalloc(size, BY2PG, 0));
  74. fbend = PGROUND(fbuf+size);
  75. rp = scr->mmio+0x10000/4;
  76. while(fbuf < fbend) {
  77. *rp++ = fbuf | 1;
  78. fbuf += BY2PG;
  79. }
  80. /*
  81. * allocate space for the cursor data in system memory.
  82. * must be uncached.
  83. */
  84. cursor = (ulong)xspanalloc(BY2PG, BY2PG, 0);
  85. mach0 = MACHP(0);
  86. pte = mmuwalk(mach0->pdb, cursor, 2, 0);
  87. if(pte == nil)
  88. panic("i81x cursor mmuwalk");
  89. *pte |= PTEUNCACHED;
  90. scr->storage = cursor;
  91. }
  92. static void
  93. i81xcurdisable(VGAscr* scr)
  94. {
  95. CursorI81x *hwcurs;
  96. if(scr->mmio == 0)
  97. return;
  98. hwcurs = (void*)((uchar*)scr->mmio+hwCur);
  99. hwcurs->ctl = (1<<4);
  100. }
  101. static void
  102. i81xcurload(VGAscr* scr, Cursor* curs)
  103. {
  104. int y;
  105. uchar *p;
  106. CursorI81x *hwcurs;
  107. if(scr->mmio == 0)
  108. return;
  109. hwcurs = (void*)((uchar*)scr->mmio+hwCur);
  110. /*
  111. * Disable the cursor then load the new image in
  112. * the top-left of the 32x32 array.
  113. * Unused portions of the image have been initialised to be
  114. * transparent.
  115. */
  116. hwcurs->ctl = (1<<4);
  117. p = (uchar*)scr->storage;
  118. for(y = 0; y < 16; y += 2) {
  119. *p++ = ~(curs->clr[2*y]|curs->set[2*y]);
  120. *p++ = ~(curs->clr[2*y+1]|curs->set[2*y+1]);
  121. p += 2;
  122. *p++ = ~(curs->clr[2*y+2]|curs->set[2*y+2]);
  123. *p++ = ~(curs->clr[2*y+3]|curs->set[2*y+3]);
  124. p += 2;
  125. *p++ = curs->set[2*y];
  126. *p++ = curs->set[2*y+1];
  127. p += 2;
  128. *p++ = curs->set[2*y+2];
  129. *p++ = curs->set[2*y+3];
  130. p += 2;
  131. }
  132. /*
  133. * Save the cursor hotpoint and enable the cursor.
  134. * The 0,0 cursor point is top-left.
  135. */
  136. scr->offset.x = curs->offset.x;
  137. scr->offset.y = curs->offset.y;
  138. hwcurs->ctl = (1<<4)|1;
  139. }
  140. static int
  141. i81xcurmove(VGAscr* scr, Point p)
  142. {
  143. int x, y;
  144. ulong pos;
  145. CursorI81x *hwcurs;
  146. if(scr->mmio == 0)
  147. return 1;
  148. hwcurs = (void*)((uchar*)scr->mmio+hwCur);
  149. x = p.x+scr->offset.x;
  150. y = p.y+scr->offset.y;
  151. pos = 0;
  152. if(x < 0) {
  153. pos |= (1<<15);
  154. x = -x;
  155. }
  156. if(y < 0) {
  157. pos |= (1<<31);
  158. y = -y;
  159. }
  160. pos |= ((y&0x7ff)<<16)|(x&0x7ff);
  161. hwcurs->pos = pos;
  162. return 0;
  163. }
  164. static void
  165. i81xcurenable(VGAscr* scr)
  166. {
  167. int i;
  168. uchar *p;
  169. CursorI81x *hwcurs;
  170. i81xenable(scr);
  171. if(scr->mmio == 0)
  172. return;
  173. hwcurs = (void*)((uchar*)scr->mmio+hwCur);
  174. /*
  175. * Initialise the 32x32 cursor to be transparent in 2bpp mode.
  176. */
  177. hwcurs->base = PADDR(scr->storage);
  178. p = (uchar*)scr->storage;
  179. for(i = 0; i < 32/2; i++) {
  180. memset(p, 0xff, 8);
  181. memset(p+8, 0, 8);
  182. p += 16;
  183. }
  184. /*
  185. * Load, locate and enable the 32x32 cursor in 2bpp mode.
  186. */
  187. i81xcurload(scr, &arrow);
  188. i81xcurmove(scr, ZP);
  189. }
  190. VGAdev vgai81xdev = {
  191. "i81x",
  192. i81xenable,
  193. nil,
  194. nil,
  195. nil,
  196. };
  197. VGAcur vgai81xcur = {
  198. "i81xhwgc",
  199. i81xcurenable,
  200. i81xcurdisable,
  201. i81xcurload,
  202. i81xcurmove,
  203. };