blob: 70ac449e16706200c6a4ff658c6c2faba4ea2a80 [file] [log] [blame]
Kevin O'Connore1e000b2011-12-31 03:30:40 -05001#include "vgabios.h" // struct vbe_modeinfo
Kevin O'Connor5108c692011-12-31 19:13:45 -05002#include "vbe.h" // VBE_MODE_VESA_DEFINED
3#include "bochsvga.h" // bochsvga_set_mode
4#include "util.h" // dprintf
Kevin O'Connore1e000b2011-12-31 03:30:40 -05005#include "config.h" // CONFIG_*
Julian Pidancet8bd766f2011-12-19 05:08:01 +00006#include "biosvar.h" // SET_BDA
Kevin O'Connored68e5b2011-12-31 04:15:12 -05007#include "stdvga.h" // VGAREG_SEQU_ADDRESS
Julian Pidancet87879e22011-12-19 05:08:00 +00008
Kevin O'Connorf1e217d2011-12-31 03:18:18 -05009static struct mode
Julian Pidancet87879e22011-12-19 05:08:00 +000010{
11 u16 mode;
12 u16 width;
13 u16 height;
14 u8 depth;
Kevin O'Connorf1e217d2011-12-31 03:18:18 -050015} bochsvga_modes[] VAR16 = {
Julian Pidancet87879e22011-12-19 05:08:00 +000016 /* standard modes */
17 { 0x100, 640, 400, 8 },
18 { 0x101, 640, 480, 8 },
19 { 0x102, 800, 600, 4 },
20 { 0x103, 800, 600, 8 },
21 { 0x104, 1024, 768, 4 },
22 { 0x105, 1024, 768, 8 },
23 { 0x106, 1280, 1024, 4 },
24 { 0x107, 1280, 1024, 8 },
25 { 0x10D, 320, 200, 15 },
26 { 0x10E, 320, 200, 16 },
27 { 0x10F, 320, 200, 24 },
28 { 0x110, 640, 480, 15 },
29 { 0x111, 640, 480, 16 },
30 { 0x112, 640, 480, 24 },
31 { 0x113, 800, 600, 15 },
32 { 0x114, 800, 600, 16 },
33 { 0x115, 800, 600, 24 },
34 { 0x116, 1024, 768, 15 },
35 { 0x117, 1024, 768, 16 },
36 { 0x118, 1024, 768, 24 },
37 { 0x119, 1280, 1024, 15 },
38 { 0x11A, 1280, 1024, 16 },
39 { 0x11B, 1280, 1024, 24 },
40 { 0x11C, 1600, 1200, 8 },
41 { 0x11D, 1600, 1200, 15 },
42 { 0x11E, 1600, 1200, 16 },
43 { 0x11F, 1600, 1200, 24 },
44 /* BOCHS modes */
45 { 0x140, 320, 200, 32 },
46 { 0x141, 640, 400, 32 },
47 { 0x142, 640, 480, 32 },
48 { 0x143, 800, 600, 32 },
49 { 0x144, 1024, 768, 32 },
50 { 0x145, 1280, 1024, 32 },
51 { 0x146, 320, 200, 8 },
52 { 0x147, 1600, 1200, 32 },
53 { 0x148, 1152, 864, 8 },
54 { 0x149, 1152, 864, 15 },
55 { 0x14a, 1152, 864, 16 },
56 { 0x14b, 1152, 864, 24 },
57 { 0x14c, 1152, 864, 32 },
58 { 0x178, 1280, 800, 16 },
59 { 0x179, 1280, 800, 24 },
60 { 0x17a, 1280, 800, 32 },
61 { 0x17b, 1280, 960, 16 },
62 { 0x17c, 1280, 960, 24 },
63 { 0x17d, 1280, 960, 32 },
64 { 0x17e, 1440, 900, 16 },
65 { 0x17f, 1440, 900, 24 },
66 { 0x180, 1440, 900, 32 },
67 { 0x181, 1400, 1050, 16 },
68 { 0x182, 1400, 1050, 24 },
69 { 0x183, 1400, 1050, 32 },
70 { 0x184, 1680, 1050, 16 },
71 { 0x185, 1680, 1050, 24 },
72 { 0x186, 1680, 1050, 32 },
73 { 0x187, 1920, 1200, 16 },
74 { 0x188, 1920, 1200, 24 },
75 { 0x189, 1920, 1200, 32 },
76 { 0x18a, 2560, 1600, 16 },
77 { 0x18b, 2560, 1600, 24 },
78 { 0x18c, 2560, 1600, 32 },
79 { 0, },
80};
81
Julian Pidancet8bd766f2011-12-19 05:08:01 +000082#define BYTES_PER_PIXEL(m) ((GET_GLOBAL((m)->depth) + 7) / 8)
83
84u32 pci_lfb_addr VAR16;
85
Kevin O'Connor161d2012011-12-31 19:42:21 -050086static inline u32 pci_config_readl(u16 bdf, u16 addr)
Julian Pidancet8bd766f2011-12-19 05:08:01 +000087{
88 int status;
89 u32 val;
Julian Pidancet8bd766f2011-12-19 05:08:01 +000090
91 addr &= ~3;
92
93 asm volatile(
94 "int $0x1a\n"
95 "cli\n"
96 "cld"
97 : "=a"(status), "=c"(val)
98 : "a"(0xb10a), "b"(bdf), "D"(addr)
99 : "cc", "memory");
100
101 if ((status >> 16))
102 return (u32)-1;
103
104 return val;
105}
106
107
108static u16 dispi_get_max_xres(void)
109{
110 u16 en;
111 u16 xres;
112
113 en = dispi_read(VBE_DISPI_INDEX_ENABLE);
114
115 dispi_write(VBE_DISPI_INDEX_ENABLE, en | VBE_DISPI_GETCAPS);
116 xres = dispi_read(VBE_DISPI_INDEX_XRES);
117 dispi_write(VBE_DISPI_INDEX_ENABLE, en);
118
119 return xres;
120}
121
122static u16 dispi_get_max_bpp(void)
123{
124 u16 en;
125 u16 bpp;
126
127 en = dispi_read(VBE_DISPI_INDEX_ENABLE);
128
129 dispi_write(VBE_DISPI_INDEX_ENABLE, en | VBE_DISPI_GETCAPS);
130 bpp = dispi_read(VBE_DISPI_INDEX_BPP);
131 dispi_write(VBE_DISPI_INDEX_ENABLE, en);
132
133 return bpp;
134}
135
Julian Pidancet87879e22011-12-19 05:08:00 +0000136/* Called only during POST */
137int
Kevin O'Connor161d2012011-12-31 19:42:21 -0500138bochsvga_init(void)
Julian Pidancet87879e22011-12-19 05:08:00 +0000139{
Kevin O'Connor161d2012011-12-31 19:42:21 -0500140 int ret = stdvga_init();
141 if (ret)
142 return ret;
Julian Pidancet8bd766f2011-12-19 05:08:01 +0000143
144 /* Sanity checks */
145 dispi_write(VBE_DISPI_INDEX_ID, VBE_DISPI_ID0);
146 if (dispi_read(VBE_DISPI_INDEX_ID) != VBE_DISPI_ID0) {
147 dprintf(1, "No VBE DISPI interface detected\n");
148 return -1;
149 }
150
151 SET_BDA(vbe_flag, 0x1);
152 dispi_write(VBE_DISPI_INDEX_ID, VBE_DISPI_ID5);
153
Kevin O'Connor161d2012011-12-31 19:42:21 -0500154 u32 lfb_addr;
Julian Pidancet8bd766f2011-12-19 05:08:01 +0000155 if (CONFIG_VGA_PCI)
Kevin O'Connor161d2012011-12-31 19:42:21 -0500156 lfb_addr = pci_config_readl(GET_GLOBAL(VgaBDF), 0x10) & ~0xf;
Julian Pidancet8bd766f2011-12-19 05:08:01 +0000157 else
158 lfb_addr = VBE_DISPI_LFB_PHYSICAL_ADDRESS;
159
160 SET_FARVAR(get_global_seg(), pci_lfb_addr, lfb_addr);
161
162 dprintf(1, "VBE DISPI detected. lfb_addr=%x\n", GET_GLOBAL(pci_lfb_addr));
163
164 return 0;
Julian Pidancet87879e22011-12-19 05:08:00 +0000165}
166
167int
Kevin O'Connorf1e217d2011-12-31 03:18:18 -0500168bochsvga_enabled(void)
Julian Pidancet87879e22011-12-19 05:08:00 +0000169{
Julian Pidancet8bd766f2011-12-19 05:08:01 +0000170 return GET_BDA(vbe_flag);
Julian Pidancet87879e22011-12-19 05:08:00 +0000171}
172
173u16
Kevin O'Connorf1e217d2011-12-31 03:18:18 -0500174bochsvga_total_mem(void)
Julian Pidancet87879e22011-12-19 05:08:00 +0000175{
Julian Pidancet8bd766f2011-12-19 05:08:01 +0000176 return dispi_read(VBE_DISPI_INDEX_VIDEO_MEMORY_64K);
177}
178
179static struct mode *find_mode_entry(u16 mode)
180{
181 struct mode *m;
182
Kevin O'Connorf1e217d2011-12-31 03:18:18 -0500183 for (m = bochsvga_modes; GET_GLOBAL(m->mode); m++) {
Julian Pidancet8bd766f2011-12-19 05:08:01 +0000184 if (GET_GLOBAL(m->mode) == mode)
185 return m;
186 }
187
188 return NULL;
189}
190
191static int mode_valid(struct mode *m)
192{
193 u16 max_xres = dispi_get_max_xres();
194 u16 max_bpp = dispi_get_max_bpp();
Kevin O'Connorf1e217d2011-12-31 03:18:18 -0500195 u32 max_mem = bochsvga_total_mem() * 64 * 1024;
Julian Pidancet8bd766f2011-12-19 05:08:01 +0000196
197 u32 mem = GET_GLOBAL(m->width) * GET_GLOBAL(m->height) *
198 BYTES_PER_PIXEL(m);
199
200 if (GET_GLOBAL(m->width) > max_xres ||
201 GET_GLOBAL(m->depth) > max_bpp ||
202 mem > max_mem)
203 return 0;
204
205 return 1;
Julian Pidancet87879e22011-12-19 05:08:00 +0000206}
207
208int
Kevin O'Connorf1e217d2011-12-31 03:18:18 -0500209bochsvga_list_modes(u16 seg, u16 ptr)
Julian Pidancet87879e22011-12-19 05:08:00 +0000210{
211 int count = 0;
212 u16 *dest = (u16 *)(u32)ptr;
Julian Pidancet8bd766f2011-12-19 05:08:01 +0000213 struct mode *m;
214
Kevin O'Connorf1e217d2011-12-31 03:18:18 -0500215 for (m = bochsvga_modes; GET_GLOBAL(m->mode); m++) {
Julian Pidancet8bd766f2011-12-19 05:08:01 +0000216 if (!mode_valid(m))
217 continue;
218
219 dprintf(1, "VBE found mode %x valid.\n", GET_GLOBAL(m->mode));
220 SET_FARVAR(seg, dest[count], GET_GLOBAL(m->mode));
221
222 count++;
223 }
Julian Pidancet87879e22011-12-19 05:08:00 +0000224
225 SET_FARVAR(seg, dest[count], 0xffff); /* End of list */
226
227 return count;
228}
229
230int
Kevin O'Connorf1e217d2011-12-31 03:18:18 -0500231bochsvga_mode_info(u16 mode, struct vbe_modeinfo *info)
Julian Pidancet87879e22011-12-19 05:08:00 +0000232{
Julian Pidancet8bd766f2011-12-19 05:08:01 +0000233 struct mode *m;
234
235 m = find_mode_entry(mode);
236 if (!m || !mode_valid(m))
237 return -1;
238
239 info->width = GET_GLOBAL(m->width);
240 info->height = GET_GLOBAL(m->height);
241 info->depth = GET_GLOBAL(m->depth);
242
243 info->linesize = info->width * ((info->depth + 7) / 8);
244 info->phys_base = GET_GLOBAL(pci_lfb_addr);
Kevin O'Connorf1e217d2011-12-31 03:18:18 -0500245 info->vram_size = bochsvga_total_mem() * 64 * 1024;
Julian Pidancet8bd766f2011-12-19 05:08:01 +0000246
247 return 0;
Julian Pidancet87879e22011-12-19 05:08:00 +0000248}
249
250void
Kevin O'Connorf1e217d2011-12-31 03:18:18 -0500251bochsvga_hires_enable(int enable)
Julian Pidancet87879e22011-12-19 05:08:00 +0000252{
Julian Pidancet8bd766f2011-12-19 05:08:01 +0000253 u16 flags = enable ?
254 VBE_DISPI_ENABLED |
255 VBE_DISPI_LFB_ENABLED |
256 VBE_DISPI_NOCLEARMEM : 0;
Julian Pidancet87879e22011-12-19 05:08:00 +0000257
Julian Pidancet8bd766f2011-12-19 05:08:01 +0000258 dispi_write(VBE_DISPI_INDEX_ENABLE, flags);
Julian Pidancet87879e22011-12-19 05:08:00 +0000259}
260
Kevin O'Connor5108c692011-12-31 19:13:45 -0500261int
262bochsvga_set_mode(int mode, int flags)
Julian Pidancet87879e22011-12-19 05:08:00 +0000263{
Kevin O'Connor5108c692011-12-31 19:13:45 -0500264 if (!(mode & VBE_MODE_VESA_DEFINED)) {
265 dprintf(1, "set VGA mode %x\n", mode);
266
267 bochsvga_hires_enable(0);
268 return stdvga_set_mode(mode, flags);
269 }
270
271 struct vbe_modeinfo modeinfo, *info = &modeinfo;
272 int ret = bochsvga_mode_info(mode, &modeinfo);
273 if (ret) {
274 dprintf(1, "VBE mode %x not found\n", mode);
275 return VBE_RETURN_STATUS_FAILED;
276 }
277 bochsvga_hires_enable(1);
278
Julian Pidancet8bd766f2011-12-19 05:08:01 +0000279 if (info->depth == 4)
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500280 stdvga_set_mode(0x6a, 0);
Julian Pidancet8bd766f2011-12-19 05:08:01 +0000281 if (info->depth == 8)
282 // XXX load_dac_palette(3);
283 ;
Julian Pidancet87879e22011-12-19 05:08:00 +0000284
Julian Pidancet8bd766f2011-12-19 05:08:01 +0000285 dispi_write(VBE_DISPI_INDEX_BPP, info->depth);
286 dispi_write(VBE_DISPI_INDEX_XRES, info->width);
287 dispi_write(VBE_DISPI_INDEX_YRES, info->height);
288 dispi_write(VBE_DISPI_INDEX_BANK, 0);
289
290 /* VGA compat setup */
291 //XXX: This probably needs some reverse engineering
292 u8 v;
293 outw(0x0011, VGAREG_VGA_CRTC_ADDRESS);
294 outw(((info->width * 4 - 1) << 8) | 0x1, VGAREG_VGA_CRTC_ADDRESS);
295 dispi_write(VBE_DISPI_INDEX_VIRT_WIDTH, info->width);
296 outw(((info->height - 1) << 8) | 0x12, VGAREG_VGA_CRTC_ADDRESS);
297 outw(((info->height - 1) & 0xff00) | 0x7, VGAREG_VGA_CRTC_ADDRESS);
298 v = inb(VGAREG_VGA_CRTC_DATA) & 0xbd;
299 if (v & 0x1)
300 v |= 0x2;
301 if (v & 0x2)
302 v |= 0x40;
303 outb(v, VGAREG_VGA_CRTC_DATA);
304
305 outw(0x9, VGAREG_VGA_CRTC_ADDRESS);
306 outb(0x17, VGAREG_VGA_CRTC_ADDRESS);
307 outb(inb(VGAREG_VGA_CRTC_DATA) | 0x3, VGAREG_VGA_CRTC_DATA);
308 v = inb(VGAREG_ACTL_RESET);
309 outw(0x10, VGAREG_ACTL_ADDRESS);
310 v = inb(VGAREG_ACTL_READ_DATA) | 0x1;
311 outb(v, VGAREG_ACTL_ADDRESS);
312 outb(0x20, VGAREG_ACTL_ADDRESS);
313 outw(0x0506, VGAREG_GRDC_ADDRESS);
314 outw(0x0f02, VGAREG_SEQU_ADDRESS);
315 if (info->depth >= 8) {
316 outb(0x14, VGAREG_VGA_CRTC_ADDRESS);
317 outb(inb(VGAREG_VGA_CRTC_DATA) | 0x40, VGAREG_VGA_CRTC_DATA);
318 v = inb(VGAREG_ACTL_RESET);
319 outw(0x10, VGAREG_ACTL_ADDRESS);
320 v = inb(VGAREG_ACTL_READ_DATA) | 0x40;
321 outb(v, VGAREG_ACTL_ADDRESS);
322 outb(0x20, VGAREG_ACTL_ADDRESS);
323 outb(0x04, VGAREG_SEQU_ADDRESS);
324 v = inb(VGAREG_SEQU_DATA) | 0x08;
325 outb(v, VGAREG_SEQU_DATA);
326 outb(0x05, VGAREG_GRDC_ADDRESS);
327 v = inb(VGAREG_GRDC_DATA) & 0x9f;
328 outb(v | 0x40, VGAREG_GRDC_DATA);
329 }
330
331 SET_BDA(vbe_mode, mode);
Kevin O'Connor5108c692011-12-31 19:13:45 -0500332
333 if (flags & MF_LINEARFB) {
334 /* Linear frame buffer */
335 /* XXX: ??? */
336 }
337 if (!(mode & MF_NOCLEARMEM)) {
338 bochsvga_clear_scr();
339 }
340
341 return 0;
Julian Pidancet87879e22011-12-19 05:08:00 +0000342}
343
344void
Kevin O'Connorf1e217d2011-12-31 03:18:18 -0500345bochsvga_clear_scr(void)
Julian Pidancet87879e22011-12-19 05:08:00 +0000346{
Julian Pidancet8bd766f2011-12-19 05:08:01 +0000347 u16 en;
Julian Pidancet87879e22011-12-19 05:08:00 +0000348
Julian Pidancet8bd766f2011-12-19 05:08:01 +0000349 en = dispi_read(VBE_DISPI_INDEX_ENABLE);
350 en &= ~VBE_DISPI_NOCLEARMEM;
351 dispi_write(VBE_DISPI_INDEX_ENABLE, en);
Julian Pidancet87879e22011-12-19 05:08:00 +0000352}
353
354int
Kevin O'Connorf1e217d2011-12-31 03:18:18 -0500355bochsvga_hires_enabled(void)
Julian Pidancet87879e22011-12-19 05:08:00 +0000356{
Julian Pidancet8bd766f2011-12-19 05:08:01 +0000357 return dispi_read(VBE_DISPI_INDEX_ENABLE) & VBE_DISPI_ENABLED;
Julian Pidancet87879e22011-12-19 05:08:00 +0000358}
359
360u16
Kevin O'Connorf1e217d2011-12-31 03:18:18 -0500361bochsvga_curr_mode(void)
Julian Pidancet87879e22011-12-19 05:08:00 +0000362{
Julian Pidancet8bd766f2011-12-19 05:08:01 +0000363 return GET_BDA(vbe_mode);
Julian Pidancet87879e22011-12-19 05:08:00 +0000364}