blob: 859524cb2362338f2e46d28174de89d59138c127 [file] [log] [blame]
Kevin O'Connor7c790292014-02-11 15:34:58 -05001// Simple framebuffer vgabios for use with coreboot native vga init.
2//
3// Copyright (C) 2014 Kevin O'Connor <kevin@koconnor.net>
Patrick Rudolph4b42cc42017-05-29 19:25:10 +02004// Copyright (C) 2017 Patrick Rudolph <siro@das-labor.org>
Kevin O'Connor7c790292014-02-11 15:34:58 -05005//
6// This file may be distributed under the terms of the GNU LGPLv3 license.
7
8#include "biosvar.h" // GET_BDA
Kevin O'Connor7c790292014-02-11 15:34:58 -05009#include "output.h" // dprintf
10#include "stdvga.h" // SEG_CTEXT
11#include "string.h" // memset16_far
12#include "util.h" // find_cb_table
Kevin O'Connor2f2ec112016-08-05 11:14:58 -040013#include "vgabios.h" // SET_VGA
Kevin O'Connor0397e802016-08-04 17:53:45 -040014#include "vgafb.h" // handle_gfx_op
Kevin O'Connor2f2ec112016-08-05 11:14:58 -040015#include "vgautil.h" // VBE_total_memory
Kevin O'Connor7c790292014-02-11 15:34:58 -050016
17static int CBmode VAR16;
18static struct vgamode_s CBmodeinfo VAR16;
Kevin O'Connor6fed3072014-10-30 12:09:19 -040019static struct vgamode_s CBemulinfo VAR16;
Kevin O'Connor7c790292014-02-11 15:34:58 -050020static u32 CBlinelength VAR16;
21
Patrick Rudolphe30d51c2017-05-29 19:25:14 +020022static struct cbvga_mode_s
23{
24 u16 mode;
25 struct vgamode_s info;
26} cbvesa_modes[] VAR16 = {
27 /* VESA 1.0 modes */
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 { 0x11D, { MM_DIRECT, 1600, 1200, 15, 8, 16, SEG_GRAPH } },
41 { 0x11E, { MM_DIRECT, 1600, 1200, 16, 8, 16, SEG_GRAPH } },
42 { 0x11F, { MM_DIRECT, 1600, 1200, 24, 8, 16, SEG_GRAPH } },
43 /* VESA 2.0 modes */
44 { 0x141, { MM_DIRECT, 640, 400, 32, 8, 16, SEG_GRAPH } },
45 { 0x142, { MM_DIRECT, 640, 480, 32, 8, 16, SEG_GRAPH } },
46 { 0x143, { MM_DIRECT, 800, 600, 32, 8, 16, SEG_GRAPH } },
47 { 0x144, { MM_DIRECT, 1024, 768, 32, 8, 16, SEG_GRAPH } },
48 { 0x145, { MM_DIRECT, 1280, 1024, 32, 8, 16, SEG_GRAPH } },
49 { 0x147, { MM_DIRECT, 1600, 1200, 32, 8, 16, SEG_GRAPH } },
50 { 0x149, { MM_DIRECT, 1152, 864, 15, 8, 16, SEG_GRAPH } },
51 { 0x14a, { MM_DIRECT, 1152, 864, 16, 8, 16, SEG_GRAPH } },
52 { 0x14b, { MM_DIRECT, 1152, 864, 24, 8, 16, SEG_GRAPH } },
53 { 0x14c, { MM_DIRECT, 1152, 864, 32, 8, 16, SEG_GRAPH } },
54 { 0x175, { MM_DIRECT, 1280, 768, 16, 8, 16, SEG_GRAPH } },
55 { 0x176, { MM_DIRECT, 1280, 768, 24, 8, 16, SEG_GRAPH } },
56 { 0x177, { MM_DIRECT, 1280, 768, 32, 8, 16, SEG_GRAPH } },
57 { 0x178, { MM_DIRECT, 1280, 800, 16, 8, 16, SEG_GRAPH } },
58 { 0x179, { MM_DIRECT, 1280, 800, 24, 8, 16, SEG_GRAPH } },
59 { 0x17a, { MM_DIRECT, 1280, 800, 32, 8, 16, SEG_GRAPH } },
60 { 0x17b, { MM_DIRECT, 1280, 960, 16, 8, 16, SEG_GRAPH } },
61 { 0x17c, { MM_DIRECT, 1280, 960, 24, 8, 16, SEG_GRAPH } },
62 { 0x17d, { MM_DIRECT, 1280, 960, 32, 8, 16, SEG_GRAPH } },
63 { 0x17e, { MM_DIRECT, 1440, 900, 16, 8, 16, SEG_GRAPH } },
64 { 0x17f, { MM_DIRECT, 1440, 900, 24, 8, 16, SEG_GRAPH } },
65 { 0x180, { MM_DIRECT, 1440, 900, 32, 8, 16, SEG_GRAPH } },
66 { 0x181, { MM_DIRECT, 1400, 1050, 16, 8, 16, SEG_GRAPH } },
67 { 0x182, { MM_DIRECT, 1400, 1050, 24, 8, 16, SEG_GRAPH } },
68 { 0x183, { MM_DIRECT, 1400, 1050, 32, 8, 16, SEG_GRAPH } },
69 { 0x184, { MM_DIRECT, 1680, 1050, 16, 8, 16, SEG_GRAPH } },
70 { 0x185, { MM_DIRECT, 1680, 1050, 24, 8, 16, SEG_GRAPH } },
71 { 0x186, { MM_DIRECT, 1680, 1050, 32, 8, 16, SEG_GRAPH } },
72 { 0x187, { MM_DIRECT, 1920, 1200, 16, 8, 16, SEG_GRAPH } },
73 { 0x188, { MM_DIRECT, 1920, 1200, 24, 8, 16, SEG_GRAPH } },
74 { 0x189, { MM_DIRECT, 1920, 1200, 32, 8, 16, SEG_GRAPH } },
75 { 0x18a, { MM_DIRECT, 2560, 1600, 16, 8, 16, SEG_GRAPH } },
76 { 0x18b, { MM_DIRECT, 2560, 1600, 24, 8, 16, SEG_GRAPH } },
77 { 0x18c, { MM_DIRECT, 2560, 1600, 32, 8, 16, SEG_GRAPH } },
78 { 0x18d, { MM_DIRECT, 1280, 720, 16, 8, 16, SEG_GRAPH } },
79 { 0x18e, { MM_DIRECT, 1280, 720, 24, 8, 16, SEG_GRAPH } },
80 { 0x18f, { MM_DIRECT, 1280, 720, 32, 8, 16, SEG_GRAPH } },
81 { 0x190, { MM_DIRECT, 1920, 1080, 16, 8, 16, SEG_GRAPH } },
82 { 0x191, { MM_DIRECT, 1920, 1080, 24, 8, 16, SEG_GRAPH } },
83 { 0x192, { MM_DIRECT, 1920, 1080, 32, 8, 16, SEG_GRAPH } },
84};
85
Kevin O'Connor7c790292014-02-11 15:34:58 -050086struct vgamode_s *cbvga_find_mode(int mode)
87{
88 if (mode == GET_GLOBAL(CBmode))
89 return &CBmodeinfo;
Kevin O'Connor6fed3072014-10-30 12:09:19 -040090 if (mode == 0x03)
91 return &CBemulinfo;
Patrick Rudolphe30d51c2017-05-29 19:25:14 +020092
93 int i;
94 for (i = 0; i < ARRAY_SIZE(cbvesa_modes); i++) {
95 struct cbvga_mode_s *cbmode_g = &cbvesa_modes[i];
96 if (GET_GLOBAL(cbmode_g->mode) == 0xffff)
97 continue;
98 if (GET_GLOBAL(cbmode_g->mode) == mode)
99 return &cbmode_g->info;
100 }
Kevin O'Connor7c790292014-02-11 15:34:58 -0500101 return NULL;
102}
103
104void
105cbvga_list_modes(u16 seg, u16 *dest, u16 *last)
106{
Gerd Hoffmann0c2b3b12018-05-30 16:19:02 +0200107 int seen = 0;
108
Patrick Rudolphe30d51c2017-05-29 19:25:14 +0200109 if (GET_GLOBAL(CBmode) != 0x3) {
110 /* Advertise additional SVGA modes for Microsoft NTLDR graphical mode.
111 * Microsoft NTLDR:
112 * + Graphical mode uses a maximum resolution of 1600x1200.
113 * + Expects to find VESA mode with 800x600 or 1024x768.
114 * + 24 Bpp and 32 Bpp are supported
115 */
116 int i;
117 for (i = 0; i < ARRAY_SIZE(cbvesa_modes) && dest < last; i++) {
118 struct cbvga_mode_s *cbmode_g = &cbvesa_modes[i];
119 u16 mode = GET_GLOBAL(cbmode_g->mode);
120 if (mode == 0xffff)
121 continue;
122 SET_FARVAR(seg, *dest, mode);
123 dest++;
Gerd Hoffmann0c2b3b12018-05-30 16:19:02 +0200124 if (GET_GLOBAL(CBmode) == mode)
125 seen = 1;
Patrick Rudolphe30d51c2017-05-29 19:25:14 +0200126 }
127 }
Gerd Hoffmann0c2b3b12018-05-30 16:19:02 +0200128 if (dest < last && !seen) {
Kevin O'Connor7c790292014-02-11 15:34:58 -0500129 SET_FARVAR(seg, *dest, GET_GLOBAL(CBmode));
130 dest++;
131 }
132 SET_FARVAR(seg, *dest, 0xffff);
133}
134
135int
136cbvga_get_window(struct vgamode_s *vmode_g, int window)
137{
138 return -1;
139}
140
141int
142cbvga_set_window(struct vgamode_s *vmode_g, int window, int val)
143{
144 return -1;
145}
146
147int
148cbvga_get_linelength(struct vgamode_s *vmode_g)
149{
150 return GET_GLOBAL(CBlinelength);
151}
152
153int
154cbvga_set_linelength(struct vgamode_s *vmode_g, int val)
155{
156 return -1;
157}
158
159int
160cbvga_get_displaystart(struct vgamode_s *vmode_g)
161{
162 return 0;
163}
164
165int
166cbvga_set_displaystart(struct vgamode_s *vmode_g, int val)
167{
168 return -1;
169}
170
171int
172cbvga_get_dacformat(struct vgamode_s *vmode_g)
173{
174 return -1;
175}
176
177int
178cbvga_set_dacformat(struct vgamode_s *vmode_g, int val)
179{
180 return -1;
181}
182
183int
184cbvga_save_restore(int cmd, u16 seg, void *data)
185{
186 if (cmd & (SR_HARDWARE|SR_DAC|SR_REGISTERS))
187 return -1;
188 return bda_save_restore(cmd, seg, data);
189}
190
191int
192cbvga_set_mode(struct vgamode_s *vmode_g, int flags)
193{
Kevin O'Connorb4eb6fc2014-10-17 22:15:42 -0400194 u8 emul = vmode_g == &CBemulinfo || GET_GLOBAL(CBmode) == 0x03;
Gerd Hoffmannddbb6da2018-05-30 23:57:06 +0200195 /*
196 * The extra_stack flag is false when running in windows x86
197 * emulator, to avoid stack switching triggering bugs. Using the
198 * same flag here to skip screen clearing, because the windows
199 * emulator seems to have problems to handle the int 1587 call
200 * too, and GO_MEMSET uses that.
201 */
202 u8 extra_stack = GET_BDA_EXT(flags) & BF_EXTRA_STACK;
Kevin O'Connorb4eb6fc2014-10-17 22:15:42 -0400203 MASK_BDA_EXT(flags, BF_EMULATE_TEXT, emul ? BF_EMULATE_TEXT : 0);
Gerd Hoffmann69ea6da2018-06-22 08:43:04 +0200204 if (!(flags & MF_NOCLEARMEM)) {
Kevin O'Connor7c790292014-02-11 15:34:58 -0500205 if (GET_GLOBAL(CBmodeinfo.memmodel) == MM_TEXT) {
206 memset16_far(SEG_CTEXT, (void*)0, 0x0720, 80*25*2);
207 return 0;
208 }
Gerd Hoffmann69ea6da2018-06-22 08:43:04 +0200209 if (extra_stack || flags & MF_LEGACY) {
210 struct gfx_op op;
211 init_gfx_op(&op, &CBmodeinfo);
212 op.x = op.y = 0;
213 op.xlen = GET_GLOBAL(CBmodeinfo.width);
214 op.ylen = GET_GLOBAL(CBmodeinfo.height);
215 op.op = GO_MEMSET;
216 handle_gfx_op(&op);
217 }
Kevin O'Connor7c790292014-02-11 15:34:58 -0500218 }
219 return 0;
220}
221
Patrick Rudolph4902b8a2017-05-29 19:25:12 +0200222int
223cbvga_get_linesize(struct vgamode_s *vmode_g)
224{
225 /* Can't change mode, always report active pitch. */
226 return GET_GLOBAL(CBlinelength);
227}
228
Kevin O'Connor7c790292014-02-11 15:34:58 -0500229#define CB_TAG_FRAMEBUFFER 0x0012
230struct cb_framebuffer {
231 u32 tag;
232 u32 size;
233
234 u64 physical_address;
235 u32 x_resolution;
236 u32 y_resolution;
237 u32 bytes_per_line;
238 u8 bits_per_pixel;
239 u8 red_mask_pos;
240 u8 red_mask_size;
241 u8 green_mask_pos;
242 u8 green_mask_size;
243 u8 blue_mask_pos;
244 u8 blue_mask_size;
245 u8 reserved_mask_pos;
246 u8 reserved_mask_size;
247};
248
Gerd Hoffmann4d70b242018-05-30 22:50:42 +0200249void
250cbvga_setup_modes(u64 addr, u8 bpp, u32 xlines, u32 ylines, u32 linelength)
251{
252 int i;
253
254 SET_VGA(CBmode, 0x140);
255 SET_VGA(VBE_framebuffer, addr);
256 SET_VGA(VBE_total_memory, linelength * ylines);
257 SET_VGA(CBlinelength, linelength);
258 SET_VGA(CBmodeinfo.memmodel, MM_DIRECT);
259 SET_VGA(CBmodeinfo.width, xlines);
260 SET_VGA(CBmodeinfo.height, ylines);
261 SET_VGA(CBmodeinfo.depth, bpp);
262 SET_VGA(CBmodeinfo.cwidth, 8);
263 SET_VGA(CBmodeinfo.cheight, 16);
264 memcpy_far(get_global_seg(), &CBemulinfo
265 , get_global_seg(), &CBmodeinfo, sizeof(CBemulinfo));
266
267 // Validate modes
268 for (i = 0; i < ARRAY_SIZE(cbvesa_modes); i++) {
269 struct cbvga_mode_s *cbmode_g = &cbvesa_modes[i];
270 /* Skip VBE modes that doesn't fit into coreboot's framebuffer */
271 if ((GET_GLOBAL(cbmode_g->info.height) > ylines)
272 || (GET_GLOBAL(cbmode_g->info.width) > xlines)
273 || (GET_GLOBAL(cbmode_g->info.depth) != bpp)) {
274 dprintf(3, "Removing mode %x\n", GET_GLOBAL(cbmode_g->mode));
275 SET_VGA(cbmode_g->mode, 0xffff);
276 }
Gerd Hoffmann902bacb2018-05-30 22:52:04 +0200277 if ((GET_GLOBAL(cbmode_g->info.height) == ylines)
278 && (GET_GLOBAL(cbmode_g->info.width) == xlines)
279 && (GET_GLOBAL(cbmode_g->info.depth) == bpp)) {
280 SET_VGA(CBmode, GET_GLOBAL(cbmode_g->mode));
281 }
Gerd Hoffmann4d70b242018-05-30 22:50:42 +0200282 }
283}
284
Kevin O'Connor7c790292014-02-11 15:34:58 -0500285int
286cbvga_setup(void)
287{
288 dprintf(1, "coreboot vga init\n");
289
290 if (GET_GLOBAL(HaveRunInit))
291 return 0;
292
293 struct cb_header *cbh = find_cb_table();
294 if (!cbh) {
295 dprintf(1, "Unable to find coreboot table\n");
296 return -1;
297 }
298 struct cb_framebuffer *cbfb = find_cb_subtable(cbh, CB_TAG_FRAMEBUFFER);
299 if (!cbfb) {
300 // Assume there is an EGA text framebuffer.
301 dprintf(1, "Did not find coreboot framebuffer - assuming EGA text\n");
302 SET_VGA(CBmode, 0x03);
303 SET_VGA(CBlinelength, 80*2);
304 SET_VGA(CBmodeinfo.memmodel, MM_TEXT);
305 SET_VGA(CBmodeinfo.width, 80);
306 SET_VGA(CBmodeinfo.height, 25);
307 SET_VGA(CBmodeinfo.depth, 4);
308 SET_VGA(CBmodeinfo.cwidth, 9);
309 SET_VGA(CBmodeinfo.cheight, 16);
310 SET_VGA(CBmodeinfo.sstart, SEG_CTEXT);
311 return 0;
312 }
313
314 u64 addr = GET_FARVAR(0, cbfb->physical_address);
Patrick Rudolph4b42cc42017-05-29 19:25:10 +0200315 u8 bpp = cbfb->blue_mask_size + cbfb->green_mask_size
316 + cbfb->red_mask_size + cbfb->reserved_mask_size;
Kevin O'Connor7c790292014-02-11 15:34:58 -0500317 u32 xlines = GET_FARVAR(0, cbfb->x_resolution);
318 u32 ylines = GET_FARVAR(0, cbfb->y_resolution);
319 u32 linelength = GET_FARVAR(0, cbfb->bytes_per_line);
320 dprintf(1, "Found FB @ %llx %dx%d with %d bpp (%d stride)\n"
321 , addr, xlines, ylines, bpp, linelength);
322
323 if (!addr || addr > 0xffffffff
324 || (bpp != 15 && bpp != 16 && bpp != 24 && bpp != 32)) {
325 dprintf(1, "Unable to use FB\n");
326 return -1;
327 }
328
Gerd Hoffmann4d70b242018-05-30 22:50:42 +0200329 cbvga_setup_modes(addr, bpp, xlines, ylines, linelength);
Kevin O'Connor7c790292014-02-11 15:34:58 -0500330 return 0;
331}