]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/pc/vgai81x.c
vgai81x: use vmap() for uncached access to cursor data instead of manipulating kernel...
[plan9front.git] / sys / src / 9 / pc / vgai81x.c
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
9 #define Image   IMAGE
10 #include <draw.h>
11 #include <memdraw.h>
12 #include <cursor.h>
13 #include "screen.h"
14
15 typedef struct
16 {
17         ushort  ctl;
18         ushort  pad;
19         ulong   base;
20         ulong   pos;
21 } CursorI81x;
22
23 enum {
24         Fbsize          = 8*MB,
25
26         hwCur           = 0x70080,
27         SRX             = 0x3c4,
28         DPMSsync        = 0x5002,
29 };
30
31 static void
32 i81xblank(VGAscr *scr, int blank)
33 {
34         char *srx, *srxd, *dpms;
35         char sr01, mode;
36
37         srx = (char *)scr->mmio+SRX;
38         srxd = srx+1;
39         dpms = (char *)scr->mmio+DPMSsync;
40
41         *srx = 0x01;
42         sr01 = *srxd & ~0x20;
43         mode = *dpms & 0xf0;
44
45         if(blank) {
46                 sr01 |= 0x20;
47                 mode |= 0x0a;
48         }
49         *srxd = sr01;
50         *dpms = mode;
51 }
52
53 static void
54 i81xenable(VGAscr* scr)
55 {
56         Pcidev *p;
57         int size;
58         Mach *mach0;
59         ulong *pgtbl, *rp, fbuf, fbend;
60         
61         if(scr->mmio)
62                 return;
63         p = scr->pci;
64         if(p == nil)
65                 return;
66         scr->mmio = vmap(p->mem[1].bar & ~0x0F, p->mem[1].size);
67         if(scr->mmio == 0)
68                 return;
69         addvgaseg("i81xmmio", p->mem[1].bar&~0x0F, p->mem[1].size);
70
71         /* allocate page table */
72         pgtbl = xspanalloc(64*1024, BY2PG, 0);
73         scr->mmio[0x2020/4] = PADDR(pgtbl) | 1;
74
75         size = p->mem[0].size;
76         if(size > 0)
77                 size = Fbsize;
78         vgalinearaddr(scr, p->mem[0].bar&~0xF, size);
79         addvgaseg("i81xscreen", p->mem[0].bar&~0xF, size);
80
81         /*
82          * allocate backing store for frame buffer
83          * and populate device page tables.
84          */
85         fbuf = PADDR(xspanalloc(size, BY2PG, 0));
86         fbend = PGROUND(fbuf+size);
87         rp = scr->mmio+0x10000/4;
88         while(fbuf < fbend) {
89                 *rp++ = fbuf | 1;
90                 fbuf += BY2PG;
91         }
92
93         scr->storage = 0;
94         scr->blank = i81xblank;
95 }
96
97 static void
98 i81xcurdisable(VGAscr* scr)
99 {
100         CursorI81x *hwcurs;
101
102         if(scr->mmio == 0)
103                 return;
104         hwcurs = (void*)((uchar*)scr->mmio+hwCur);
105         hwcurs->ctl = (1<<4);
106 }
107
108 static void
109 i81xcurload(VGAscr* scr, Cursor* curs)
110 {
111         int y;
112         uchar *p;
113         CursorI81x *hwcurs;
114
115         if(scr->mmio == 0 || scr->storage == 0)
116                 return;
117         hwcurs = (void*)((uchar*)scr->mmio+hwCur);
118
119         /*
120          * Disable the cursor then load the new image in
121          * the top-left of the 32x32 array.
122          * Unused portions of the image have been initialised to be
123          * transparent.
124          */
125         hwcurs->ctl = (1<<4);
126         p = (uchar*)scr->storage;
127         for(y = 0; y < 16; y += 2) {
128                 *p++ = ~(curs->clr[2*y]|curs->set[2*y]);
129                 *p++ = ~(curs->clr[2*y+1]|curs->set[2*y+1]);
130                 p += 2;
131                 *p++ = ~(curs->clr[2*y+2]|curs->set[2*y+2]);
132                 *p++ = ~(curs->clr[2*y+3]|curs->set[2*y+3]);
133                 p += 2;
134                 *p++ = curs->set[2*y];
135                 *p++ = curs->set[2*y+1];
136                 p += 2;
137                 *p++ = curs->set[2*y+2];
138                 *p++ = curs->set[2*y+3];
139                 p += 2;
140         }
141
142         /*
143          * Save the cursor hotpoint and enable the cursor.
144          * The 0,0 cursor point is top-left.
145          */
146         scr->offset.x = curs->offset.x;
147         scr->offset.y = curs->offset.y;
148         hwcurs->ctl = (1<<4)|1;
149 }
150
151 static int
152 i81xcurmove(VGAscr* scr, Point p)
153 {
154         int x, y;
155         ulong pos;
156         CursorI81x *hwcurs;
157
158         if(scr->mmio == 0)
159                 return 1;
160         hwcurs = (void*)((uchar*)scr->mmio+hwCur);
161
162         x = p.x+scr->offset.x;
163         y = p.y+scr->offset.y;
164         pos = 0;
165         if(x < 0) {
166                 pos |= (1<<15);
167                 x = -x;
168         }
169         if(y < 0) {
170                 pos |= (1<<31);
171                 y = -y;
172         }
173         pos |= ((y&0x7ff)<<16)|(x&0x7ff);
174         hwcurs->pos = pos;
175
176         return 0;
177 }
178
179 static void
180 i81xcurenable(VGAscr* scr)
181 {
182         int i;
183         uchar *p;
184
185         i81xenable(scr);
186         if(scr->mmio == 0)
187                 return;
188
189         if(scr->storage == 0){
190                 CursorI81x *hwcurs;
191                 Page *pg = newpage(0, nil, 0);
192                 scr->storage = (uintptr)vmap(pg->pa, BY2PG);
193                 if(scr->storage == 0){
194                         putpage(pg);
195                         return;
196                 }
197                 hwcurs = (void*)((uchar*)scr->mmio+hwCur);
198                 hwcurs->base = pg->pa;
199         }
200
201         /*
202          * Initialise the 32x32 cursor to be transparent in 2bpp mode.
203          */
204         p = (uchar*)scr->storage;
205         for(i = 0; i < 32/2; i++) {
206                 memset(p, 0xff, 8);
207                 memset(p+8, 0, 8);
208                 p += 16;
209         }
210
211         /*
212          * Load, locate and enable the 32x32 cursor in 2bpp mode.
213          */
214         i81xcurload(scr, &arrow);
215         i81xcurmove(scr, ZP);
216 }
217
218 VGAdev vgai81xdev = {
219         "i81x",
220
221         i81xenable,
222         nil,
223         nil,
224         nil,
225 };
226
227 VGAcur vgai81xcur = {
228         "i81xhwgc",
229
230         i81xcurenable,
231         i81xcurdisable,
232         i81xcurload,
233         i81xcurmove,
234 };