vgabios: Add support for vesa get/set window function.
[seabios.git] / vgasrc / bochsvga.c
1 #include "vgabios.h" // struct vbe_modeinfo
2 #include "vbe.h" // VBE_CAPABILITY_8BIT_DAC
3 #include "bochsvga.h" // bochsvga_set_mode
4 #include "util.h" // dprintf
5 #include "config.h" // CONFIG_*
6 #include "biosvar.h" // GET_GLOBAL
7 #include "stdvga.h" // VGAREG_SEQU_ADDRESS
8 #include "pci.h" // pci_config_readl
9 #include "pci_regs.h" // PCI_BASE_ADDRESS_0
10
11 static struct bochsvga_mode
12 {
13     u16 mode;
14     struct vgamode_s info;
15 } bochsvga_modes[] VAR16 = {
16     /* standard modes */
17     { 0x100, { MM_PACKED, 640,  400,  8,  8, 16, SEG_GRAPH } },
18     { 0x101, { MM_PACKED, 640,  480,  8,  8, 16, SEG_GRAPH } },
19     { 0x102, { MM_PLANAR, 800,  600,  4,  8, 16, SEG_GRAPH } },
20     { 0x103, { MM_PACKED, 800,  600,  8,  8, 16, SEG_GRAPH } },
21     { 0x104, { MM_PLANAR, 1024, 768,  4,  8, 16, SEG_GRAPH } },
22     { 0x105, { MM_PACKED, 1024, 768,  8,  8, 16, SEG_GRAPH } },
23     { 0x106, { MM_PLANAR, 1280, 1024, 4,  8, 16, SEG_GRAPH } },
24     { 0x107, { MM_PACKED, 1280, 1024, 8,  8, 16, SEG_GRAPH } },
25     { 0x10D, { MM_DIRECT, 320,  200,  15, 8, 16, SEG_GRAPH } },
26     { 0x10E, { MM_DIRECT, 320,  200,  16, 8, 16, SEG_GRAPH } },
27     { 0x10F, { MM_DIRECT, 320,  200,  24, 8, 16, SEG_GRAPH } },
28     { 0x110, { MM_DIRECT, 640,  480,  15, 8, 16, SEG_GRAPH } },
29     { 0x111, { MM_DIRECT, 640,  480,  16, 8, 16, SEG_GRAPH } },
30     { 0x112, { MM_DIRECT, 640,  480,  24, 8, 16, SEG_GRAPH } },
31     { 0x113, { MM_DIRECT, 800,  600,  15, 8, 16, SEG_GRAPH } },
32     { 0x114, { MM_DIRECT, 800,  600,  16, 8, 16, SEG_GRAPH } },
33     { 0x115, { MM_DIRECT, 800,  600,  24, 8, 16, SEG_GRAPH } },
34     { 0x116, { MM_DIRECT, 1024, 768,  15, 8, 16, SEG_GRAPH } },
35     { 0x117, { MM_DIRECT, 1024, 768,  16, 8, 16, SEG_GRAPH } },
36     { 0x118, { MM_DIRECT, 1024, 768,  24, 8, 16, SEG_GRAPH } },
37     { 0x119, { MM_DIRECT, 1280, 1024, 15, 8, 16, SEG_GRAPH } },
38     { 0x11A, { MM_DIRECT, 1280, 1024, 16, 8, 16, SEG_GRAPH } },
39     { 0x11B, { MM_DIRECT, 1280, 1024, 24, 8, 16, SEG_GRAPH } },
40     { 0x11C, { MM_PACKED, 1600, 1200, 8,  8, 16, SEG_GRAPH } },
41     { 0x11D, { MM_DIRECT, 1600, 1200, 15, 8, 16, SEG_GRAPH } },
42     { 0x11E, { MM_DIRECT, 1600, 1200, 16, 8, 16, SEG_GRAPH } },
43     { 0x11F, { MM_DIRECT, 1600, 1200, 24, 8, 16, SEG_GRAPH } },
44     /* BOCHS modes */
45     { 0x140, { MM_DIRECT, 320,  200,  32, 8, 16, SEG_GRAPH } },
46     { 0x141, { MM_DIRECT, 640,  400,  32, 8, 16, SEG_GRAPH } },
47     { 0x142, { MM_DIRECT, 640,  480,  32, 8, 16, SEG_GRAPH } },
48     { 0x143, { MM_DIRECT, 800,  600,  32, 8, 16, SEG_GRAPH } },
49     { 0x144, { MM_DIRECT, 1024, 768,  32, 8, 16, SEG_GRAPH } },
50     { 0x145, { MM_DIRECT, 1280, 1024, 32, 8, 16, SEG_GRAPH } },
51     { 0x146, { MM_PACKED, 320,  200,  8,  8, 16, SEG_GRAPH } },
52     { 0x147, { MM_DIRECT, 1600, 1200, 32, 8, 16, SEG_GRAPH } },
53     { 0x148, { MM_PACKED, 1152, 864,  8,  8, 16, SEG_GRAPH } },
54     { 0x149, { MM_DIRECT, 1152, 864,  15, 8, 16, SEG_GRAPH } },
55     { 0x14a, { MM_DIRECT, 1152, 864,  16, 8, 16, SEG_GRAPH } },
56     { 0x14b, { MM_DIRECT, 1152, 864,  24, 8, 16, SEG_GRAPH } },
57     { 0x14c, { MM_DIRECT, 1152, 864,  32, 8, 16, SEG_GRAPH } },
58     { 0x178, { MM_DIRECT, 1280, 800,  16, 8, 16, SEG_GRAPH } },
59     { 0x179, { MM_DIRECT, 1280, 800,  24, 8, 16, SEG_GRAPH } },
60     { 0x17a, { MM_DIRECT, 1280, 800,  32, 8, 16, SEG_GRAPH } },
61     { 0x17b, { MM_DIRECT, 1280, 960,  16, 8, 16, SEG_GRAPH } },
62     { 0x17c, { MM_DIRECT, 1280, 960,  24, 8, 16, SEG_GRAPH } },
63     { 0x17d, { MM_DIRECT, 1280, 960,  32, 8, 16, SEG_GRAPH } },
64     { 0x17e, { MM_DIRECT, 1440, 900,  16, 8, 16, SEG_GRAPH } },
65     { 0x17f, { MM_DIRECT, 1440, 900,  24, 8, 16, SEG_GRAPH } },
66     { 0x180, { MM_DIRECT, 1440, 900,  32, 8, 16, SEG_GRAPH } },
67     { 0x181, { MM_DIRECT, 1400, 1050, 16, 8, 16, SEG_GRAPH } },
68     { 0x182, { MM_DIRECT, 1400, 1050, 24, 8, 16, SEG_GRAPH } },
69     { 0x183, { MM_DIRECT, 1400, 1050, 32, 8, 16, SEG_GRAPH } },
70     { 0x184, { MM_DIRECT, 1680, 1050, 16, 8, 16, SEG_GRAPH } },
71     { 0x185, { MM_DIRECT, 1680, 1050, 24, 8, 16, SEG_GRAPH } },
72     { 0x186, { MM_DIRECT, 1680, 1050, 32, 8, 16, SEG_GRAPH } },
73     { 0x187, { MM_DIRECT, 1920, 1200, 16, 8, 16, SEG_GRAPH } },
74     { 0x188, { MM_DIRECT, 1920, 1200, 24, 8, 16, SEG_GRAPH } },
75     { 0x189, { MM_DIRECT, 1920, 1200, 32, 8, 16, SEG_GRAPH } },
76     { 0x18a, { MM_DIRECT, 2560, 1600, 16, 8, 16, SEG_GRAPH } },
77     { 0x18b, { MM_DIRECT, 2560, 1600, 24, 8, 16, SEG_GRAPH } },
78     { 0x18c, { MM_DIRECT, 2560, 1600, 32, 8, 16, SEG_GRAPH } },
79 };
80
81 static int is_bochsvga_mode(struct vgamode_s *vmode_g)
82 {
83     return (vmode_g >= &bochsvga_modes[0].info
84             && vmode_g <= &bochsvga_modes[ARRAY_SIZE(bochsvga_modes)-1].info);
85 }
86
87 static u16 dispi_get_max_xres(void)
88 {
89     u16 en;
90     u16 xres;
91
92     en = dispi_read(VBE_DISPI_INDEX_ENABLE);
93
94     dispi_write(VBE_DISPI_INDEX_ENABLE, en | VBE_DISPI_GETCAPS);
95     xres = dispi_read(VBE_DISPI_INDEX_XRES);
96     dispi_write(VBE_DISPI_INDEX_ENABLE, en);
97
98     return xres;
99 }
100
101 static u16 dispi_get_max_bpp(void)
102 {
103     u16 en;
104     u16 bpp;
105
106     en = dispi_read(VBE_DISPI_INDEX_ENABLE);
107
108     dispi_write(VBE_DISPI_INDEX_ENABLE, en | VBE_DISPI_GETCAPS);
109     bpp = dispi_read(VBE_DISPI_INDEX_BPP);
110     dispi_write(VBE_DISPI_INDEX_ENABLE, en);
111
112     return bpp;
113 }
114
115 /* Called only during POST */
116 int
117 bochsvga_init(void)
118 {
119     int ret = stdvga_init();
120     if (ret)
121         return ret;
122
123     /* Sanity checks */
124     dispi_write(VBE_DISPI_INDEX_ID, VBE_DISPI_ID0);
125     if (dispi_read(VBE_DISPI_INDEX_ID) != VBE_DISPI_ID0) {
126         dprintf(1, "No VBE DISPI interface detected\n");
127         return -1;
128     }
129
130     dispi_write(VBE_DISPI_INDEX_ID, VBE_DISPI_ID5);
131
132     u32 lfb_addr = VBE_DISPI_LFB_PHYSICAL_ADDRESS;
133     int bdf = GET_GLOBAL(VgaBDF);
134     if (CONFIG_VGA_PCI && bdf >= 0)
135         lfb_addr = (pci_config_readl(bdf, PCI_BASE_ADDRESS_0)
136                     & PCI_BASE_ADDRESS_MEM_MASK);
137
138     SET_VGA(VBE_framebuffer, lfb_addr);
139     u16 totalmem = dispi_read(VBE_DISPI_INDEX_VIDEO_MEMORY_64K);
140     SET_VGA(VBE_total_memory, totalmem * 64 * 1024);
141     SET_VGA(VBE_capabilities, VBE_CAPABILITY_8BIT_DAC);
142
143     dprintf(1, "VBE DISPI detected. lfb_addr=%x\n", lfb_addr);
144
145     return 0;
146 }
147
148 static int mode_valid(struct vgamode_s *vmode_g)
149 {
150     u16 max_xres = dispi_get_max_xres();
151     u16 max_bpp = dispi_get_max_bpp();
152     u32 max_mem = GET_GLOBAL(VBE_total_memory);
153
154     u16 width = GET_GLOBAL(vmode_g->width);
155     u16 height = GET_GLOBAL(vmode_g->height);
156     u8 depth = GET_GLOBAL(vmode_g->depth);
157     u32 mem = width * height * DIV_ROUND_UP(depth, 8);
158
159     return width <= max_xres && depth <= max_bpp && mem <= max_mem;
160 }
161
162 struct vgamode_s *bochsvga_find_mode(int mode)
163 {
164     struct bochsvga_mode *m = bochsvga_modes;
165     for (; m < &bochsvga_modes[ARRAY_SIZE(bochsvga_modes)]; m++)
166         if (GET_GLOBAL(m->mode) == mode) {
167             if (! mode_valid(&m->info))
168                 return NULL;
169             return &m->info;
170         }
171     return stdvga_find_mode(mode);
172 }
173
174 void
175 bochsvga_list_modes(u16 seg, u16 *dest, u16 *last)
176 {
177     struct bochsvga_mode *m = bochsvga_modes;
178     for (; m < &bochsvga_modes[ARRAY_SIZE(bochsvga_modes)] && dest<last; m++) {
179         if (!mode_valid(&m->info))
180             continue;
181
182         dprintf(1, "VBE found mode %x valid.\n", GET_GLOBAL(m->mode));
183         SET_FARVAR(seg, *dest, GET_GLOBAL(m->mode));
184         dest++;
185     }
186
187     stdvga_list_modes(seg, dest, last);
188 }
189
190 static void
191 bochsvga_hires_enable(int enable)
192 {
193     u16 flags = enable ?
194         VBE_DISPI_ENABLED |
195         VBE_DISPI_LFB_ENABLED |
196         VBE_DISPI_NOCLEARMEM : 0;
197
198     dispi_write(VBE_DISPI_INDEX_ENABLE, flags);
199 }
200
201 int
202 bochsvga_get_window(struct vgamode_s *vmode_g, int window)
203 {
204     if (window != 0)
205         return -1;
206     return dispi_read(VBE_DISPI_INDEX_BANK);
207 }
208
209 int
210 bochsvga_set_window(struct vgamode_s *vmode_g, int window, int val)
211 {
212     if (window != 0)
213         return -1;
214     dispi_write(VBE_DISPI_INDEX_BANK, val);
215     if (dispi_read(VBE_DISPI_INDEX_BANK) != val)
216         return -1;
217     return 0;
218 }
219
220 static void
221 bochsvga_clear_scr(void)
222 {
223     u16 en;
224
225     en = dispi_read(VBE_DISPI_INDEX_ENABLE);
226     en &= ~VBE_DISPI_NOCLEARMEM;
227     dispi_write(VBE_DISPI_INDEX_ENABLE, en);
228 }
229
230 int
231 bochsvga_set_mode(struct vgamode_s *vmode_g, int flags)
232 {
233     if (! is_bochsvga_mode(vmode_g)) {
234         bochsvga_hires_enable(0);
235         return stdvga_set_mode(vmode_g, flags);
236     }
237
238     bochsvga_hires_enable(1);
239
240     u8 depth = GET_GLOBAL(vmode_g->depth);
241     if (depth == 4)
242         stdvga_set_mode(stdvga_find_mode(0x6a), 0);
243     if (depth == 8)
244         // XXX load_dac_palette(3);
245         ;
246
247     dispi_write(VBE_DISPI_INDEX_BPP, depth);
248     u16 width = GET_GLOBAL(vmode_g->width);
249     u16 height = GET_GLOBAL(vmode_g->height);
250     dispi_write(VBE_DISPI_INDEX_XRES, width);
251     dispi_write(VBE_DISPI_INDEX_YRES, height);
252     dispi_write(VBE_DISPI_INDEX_BANK, 0);
253
254     /* VGA compat setup */
255     //XXX: This probably needs some reverse engineering
256     u16 crtc_addr = VGAREG_VGA_CRTC_ADDRESS;
257     stdvga_crtc_write(crtc_addr, 0x11, 0x00);
258     stdvga_crtc_write(crtc_addr, 0x01, width / 8 - 1);
259     dispi_write(VBE_DISPI_INDEX_VIRT_WIDTH, width);
260     stdvga_crtc_write(crtc_addr, 0x12, height - 1);
261     u8 v = 0;
262     if ((height - 1) & 0x0100)
263         v |= 0x02;
264     if ((height - 1) & 0x0200)
265         v |= 0x40;
266     stdvga_crtc_mask(crtc_addr, 0x07, 0x42, v);
267
268     stdvga_crtc_write(crtc_addr, 0x09, 0x00);
269     stdvga_crtc_mask(crtc_addr, 0x17, 0x00, 0x03);
270     stdvga_attr_mask(0x10, 0x00, 0x01);
271     stdvga_grdc_write(0x06, 0x05);
272     stdvga_sequ_write(0x02, 0x0f);
273     if (depth >= 8) {
274         stdvga_crtc_mask(crtc_addr, 0x14, 0x00, 0x40);
275         stdvga_attr_mask(0x10, 0x00, 0x40);
276         stdvga_sequ_mask(0x04, 0x00, 0x08);
277         stdvga_grdc_mask(0x05, 0x20, 0x40);
278     }
279
280     if (flags & MF_LINEARFB) {
281         /* Linear frame buffer */
282         /* XXX: ??? */
283     }
284     if (!(flags & MF_NOCLEARMEM)) {
285         bochsvga_clear_scr();
286     }
287
288     return 0;
289 }