]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/pc/vgai81x.c
usbehci: catch interrupt in tsleep
[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, cursor, *pte, 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         /*
94          * allocate space for the cursor data in system memory.
95          * must be uncached.
96          */
97         cursor = (ulong)xspanalloc(BY2PG, BY2PG, 0);
98         mach0 = MACHP(0);
99         pte = mmuwalk(mach0->pdb, cursor, 2, 0);
100         if(pte == nil)
101                 panic("i81x cursor mmuwalk");
102         *pte |= PTEUNCACHED;
103         scr->storage = cursor;
104
105         scr->blank = i81xblank;
106 }
107
108 static void
109 i81xcurdisable(VGAscr* scr)
110 {
111         CursorI81x *hwcurs;
112
113         if(scr->mmio == 0)
114                 return;
115         hwcurs = (void*)((uchar*)scr->mmio+hwCur);
116         hwcurs->ctl = (1<<4);
117 }
118
119 static void
120 i81xcurload(VGAscr* scr, Cursor* curs)
121 {
122         int y;
123         uchar *p;
124         CursorI81x *hwcurs;
125
126         if(scr->mmio == 0)
127                 return;
128         hwcurs = (void*)((uchar*)scr->mmio+hwCur);
129
130         /*
131          * Disable the cursor then load the new image in
132          * the top-left of the 32x32 array.
133          * Unused portions of the image have been initialised to be
134          * transparent.
135          */
136         hwcurs->ctl = (1<<4);
137         p = (uchar*)scr->storage;
138         for(y = 0; y < 16; y += 2) {
139                 *p++ = ~(curs->clr[2*y]|curs->set[2*y]);
140                 *p++ = ~(curs->clr[2*y+1]|curs->set[2*y+1]);
141                 p += 2;
142                 *p++ = ~(curs->clr[2*y+2]|curs->set[2*y+2]);
143                 *p++ = ~(curs->clr[2*y+3]|curs->set[2*y+3]);
144                 p += 2;
145                 *p++ = curs->set[2*y];
146                 *p++ = curs->set[2*y+1];
147                 p += 2;
148                 *p++ = curs->set[2*y+2];
149                 *p++ = curs->set[2*y+3];
150                 p += 2;
151         }
152
153         /*
154          * Save the cursor hotpoint and enable the cursor.
155          * The 0,0 cursor point is top-left.
156          */
157         scr->offset.x = curs->offset.x;
158         scr->offset.y = curs->offset.y;
159         hwcurs->ctl = (1<<4)|1;
160 }
161
162 static int
163 i81xcurmove(VGAscr* scr, Point p)
164 {
165         int x, y;
166         ulong pos;
167         CursorI81x *hwcurs;
168
169         if(scr->mmio == 0)
170                 return 1;
171         hwcurs = (void*)((uchar*)scr->mmio+hwCur);
172
173         x = p.x+scr->offset.x;
174         y = p.y+scr->offset.y;
175         pos = 0;
176         if(x < 0) {
177                 pos |= (1<<15);
178                 x = -x;
179         }
180         if(y < 0) {
181                 pos |= (1<<31);
182                 y = -y;
183         }
184         pos |= ((y&0x7ff)<<16)|(x&0x7ff);
185         hwcurs->pos = pos;
186
187         return 0;
188 }
189
190 static void
191 i81xcurenable(VGAscr* scr)
192 {
193         int i;
194         uchar *p;
195         CursorI81x *hwcurs;
196
197         i81xenable(scr);
198         if(scr->mmio == 0)
199                 return;
200         hwcurs = (void*)((uchar*)scr->mmio+hwCur);
201
202         /*
203          * Initialise the 32x32 cursor to be transparent in 2bpp mode.
204          */
205         hwcurs->base = PADDR(scr->storage);
206         p = (uchar*)scr->storage;
207         for(i = 0; i < 32/2; i++) {
208                 memset(p, 0xff, 8);
209                 memset(p+8, 0, 8);
210                 p += 16;
211         }
212         /*
213          * Load, locate and enable the 32x32 cursor in 2bpp mode.
214          */
215         i81xcurload(scr, &arrow);
216         i81xcurmove(scr, ZP);
217 }
218
219 VGAdev vgai81xdev = {
220         "i81x",
221
222         i81xenable,
223         nil,
224         nil,
225         nil,
226 };
227
228 VGAcur vgai81xcur = {
229         "i81xhwgc",
230
231         i81xcurenable,
232         i81xcurdisable,
233         i81xcurload,
234         i81xcurmove,
235 };