blob: b919137bb1830c5427c5745c79b0310b825f952e [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
Uwe Kleine-König120996f2019-10-20 22:07:26 +020016#include "svgamodes.h" // svga_modes
Kevin O'Connor7c790292014-02-11 15:34:58 -050017
18static int CBmode VAR16;
19static struct vgamode_s CBmodeinfo VAR16;
Kevin O'Connor6fed3072014-10-30 12:09:19 -040020static struct vgamode_s CBemulinfo VAR16;
Kevin O'Connor7c790292014-02-11 15:34:58 -050021static u32 CBlinelength VAR16;
22
23struct vgamode_s *cbvga_find_mode(int mode)
24{
25 if (mode == GET_GLOBAL(CBmode))
26 return &CBmodeinfo;
Kevin O'Connor6fed3072014-10-30 12:09:19 -040027 if (mode == 0x03)
28 return &CBemulinfo;
Patrick Rudolphe30d51c2017-05-29 19:25:14 +020029
30 int i;
Uwe Kleine-König120996f2019-10-20 22:07:26 +020031 for (i = 0; i < GET_GLOBAL(svga_mcount); i++) {
32 struct generic_svga_mode *cbmode_g = &svga_modes[i];
Patrick Rudolphe30d51c2017-05-29 19:25:14 +020033 if (GET_GLOBAL(cbmode_g->mode) == 0xffff)
34 continue;
35 if (GET_GLOBAL(cbmode_g->mode) == mode)
36 return &cbmode_g->info;
37 }
Kevin O'Connor7c790292014-02-11 15:34:58 -050038 return NULL;
39}
40
41void
42cbvga_list_modes(u16 seg, u16 *dest, u16 *last)
43{
Gerd Hoffmann0c2b3b12018-05-30 16:19:02 +020044 int seen = 0;
45
Patrick Rudolphe30d51c2017-05-29 19:25:14 +020046 if (GET_GLOBAL(CBmode) != 0x3) {
47 /* Advertise additional SVGA modes for Microsoft NTLDR graphical mode.
48 * Microsoft NTLDR:
49 * + Graphical mode uses a maximum resolution of 1600x1200.
50 * + Expects to find VESA mode with 800x600 or 1024x768.
51 * + 24 Bpp and 32 Bpp are supported
52 */
53 int i;
Uwe Kleine-König120996f2019-10-20 22:07:26 +020054 for (i = 0; i < GET_GLOBAL(svga_mcount) && dest < last; i++) {
55 struct generic_svga_mode *cbmode_g = &svga_modes[i];
Patrick Rudolphe30d51c2017-05-29 19:25:14 +020056 u16 mode = GET_GLOBAL(cbmode_g->mode);
57 if (mode == 0xffff)
58 continue;
59 SET_FARVAR(seg, *dest, mode);
60 dest++;
Gerd Hoffmann0c2b3b12018-05-30 16:19:02 +020061 if (GET_GLOBAL(CBmode) == mode)
62 seen = 1;
Patrick Rudolphe30d51c2017-05-29 19:25:14 +020063 }
64 }
Gerd Hoffmann0c2b3b12018-05-30 16:19:02 +020065 if (dest < last && !seen) {
Kevin O'Connor7c790292014-02-11 15:34:58 -050066 SET_FARVAR(seg, *dest, GET_GLOBAL(CBmode));
67 dest++;
68 }
69 SET_FARVAR(seg, *dest, 0xffff);
70}
71
72int
73cbvga_get_window(struct vgamode_s *vmode_g, int window)
74{
75 return -1;
76}
77
78int
79cbvga_set_window(struct vgamode_s *vmode_g, int window, int val)
80{
81 return -1;
82}
83
84int
85cbvga_get_linelength(struct vgamode_s *vmode_g)
86{
87 return GET_GLOBAL(CBlinelength);
88}
89
90int
91cbvga_set_linelength(struct vgamode_s *vmode_g, int val)
92{
93 return -1;
94}
95
96int
97cbvga_get_displaystart(struct vgamode_s *vmode_g)
98{
99 return 0;
100}
101
102int
103cbvga_set_displaystart(struct vgamode_s *vmode_g, int val)
104{
105 return -1;
106}
107
108int
109cbvga_get_dacformat(struct vgamode_s *vmode_g)
110{
111 return -1;
112}
113
114int
115cbvga_set_dacformat(struct vgamode_s *vmode_g, int val)
116{
117 return -1;
118}
119
120int
121cbvga_save_restore(int cmd, u16 seg, void *data)
122{
123 if (cmd & (SR_HARDWARE|SR_DAC|SR_REGISTERS))
124 return -1;
125 return bda_save_restore(cmd, seg, data);
126}
127
128int
129cbvga_set_mode(struct vgamode_s *vmode_g, int flags)
130{
Kevin O'Connorb4eb6fc2014-10-17 22:15:42 -0400131 u8 emul = vmode_g == &CBemulinfo || GET_GLOBAL(CBmode) == 0x03;
Gerd Hoffmannddbb6da2018-05-30 23:57:06 +0200132 /*
133 * The extra_stack flag is false when running in windows x86
134 * emulator, to avoid stack switching triggering bugs. Using the
135 * same flag here to skip screen clearing, because the windows
136 * emulator seems to have problems to handle the int 1587 call
137 * too, and GO_MEMSET uses that.
138 */
139 u8 extra_stack = GET_BDA_EXT(flags) & BF_EXTRA_STACK;
Kevin O'Connorb4eb6fc2014-10-17 22:15:42 -0400140 MASK_BDA_EXT(flags, BF_EMULATE_TEXT, emul ? BF_EMULATE_TEXT : 0);
Gerd Hoffmann69ea6da2018-06-22 08:43:04 +0200141 if (!(flags & MF_NOCLEARMEM)) {
Kevin O'Connor7c790292014-02-11 15:34:58 -0500142 if (GET_GLOBAL(CBmodeinfo.memmodel) == MM_TEXT) {
143 memset16_far(SEG_CTEXT, (void*)0, 0x0720, 80*25*2);
144 return 0;
145 }
Gerd Hoffmann69ea6da2018-06-22 08:43:04 +0200146 if (extra_stack || flags & MF_LEGACY) {
147 struct gfx_op op;
148 init_gfx_op(&op, &CBmodeinfo);
149 op.x = op.y = 0;
150 op.xlen = GET_GLOBAL(CBmodeinfo.width);
151 op.ylen = GET_GLOBAL(CBmodeinfo.height);
152 op.op = GO_MEMSET;
153 handle_gfx_op(&op);
154 }
Kevin O'Connor7c790292014-02-11 15:34:58 -0500155 }
156 return 0;
157}
158
Patrick Rudolph4902b8a2017-05-29 19:25:12 +0200159int
160cbvga_get_linesize(struct vgamode_s *vmode_g)
161{
162 /* Can't change mode, always report active pitch. */
163 return GET_GLOBAL(CBlinelength);
164}
165
Kevin O'Connor7c790292014-02-11 15:34:58 -0500166#define CB_TAG_FRAMEBUFFER 0x0012
167struct cb_framebuffer {
168 u32 tag;
169 u32 size;
170
171 u64 physical_address;
172 u32 x_resolution;
173 u32 y_resolution;
174 u32 bytes_per_line;
175 u8 bits_per_pixel;
176 u8 red_mask_pos;
177 u8 red_mask_size;
178 u8 green_mask_pos;
179 u8 green_mask_size;
180 u8 blue_mask_pos;
181 u8 blue_mask_size;
182 u8 reserved_mask_pos;
183 u8 reserved_mask_size;
184};
185
Gerd Hoffmann4d70b242018-05-30 22:50:42 +0200186void
187cbvga_setup_modes(u64 addr, u8 bpp, u32 xlines, u32 ylines, u32 linelength)
188{
189 int i;
190
191 SET_VGA(CBmode, 0x140);
192 SET_VGA(VBE_framebuffer, addr);
193 SET_VGA(VBE_total_memory, linelength * ylines);
194 SET_VGA(CBlinelength, linelength);
195 SET_VGA(CBmodeinfo.memmodel, MM_DIRECT);
196 SET_VGA(CBmodeinfo.width, xlines);
197 SET_VGA(CBmodeinfo.height, ylines);
198 SET_VGA(CBmodeinfo.depth, bpp);
199 SET_VGA(CBmodeinfo.cwidth, 8);
200 SET_VGA(CBmodeinfo.cheight, 16);
201 memcpy_far(get_global_seg(), &CBemulinfo
202 , get_global_seg(), &CBmodeinfo, sizeof(CBemulinfo));
203
204 // Validate modes
Uwe Kleine-König120996f2019-10-20 22:07:26 +0200205 for (i = 0; i < GET_GLOBAL(svga_mcount); i++) {
206 struct generic_svga_mode *cbmode_g = &svga_modes[i];
Gerd Hoffmann4d70b242018-05-30 22:50:42 +0200207 /* Skip VBE modes that doesn't fit into coreboot's framebuffer */
Uwe Kleine-König120996f2019-10-20 22:07:26 +0200208 if ((GET_GLOBAL(cbmode_g->info.memmodel) != MM_DIRECT)
209 || (GET_GLOBAL(cbmode_g->info.height) > ylines)
Gerd Hoffmann4d70b242018-05-30 22:50:42 +0200210 || (GET_GLOBAL(cbmode_g->info.width) > xlines)
211 || (GET_GLOBAL(cbmode_g->info.depth) != bpp)) {
212 dprintf(3, "Removing mode %x\n", GET_GLOBAL(cbmode_g->mode));
213 SET_VGA(cbmode_g->mode, 0xffff);
214 }
Gerd Hoffmann902bacb2018-05-30 22:52:04 +0200215 if ((GET_GLOBAL(cbmode_g->info.height) == ylines)
216 && (GET_GLOBAL(cbmode_g->info.width) == xlines)
217 && (GET_GLOBAL(cbmode_g->info.depth) == bpp)) {
218 SET_VGA(CBmode, GET_GLOBAL(cbmode_g->mode));
219 }
Gerd Hoffmann4d70b242018-05-30 22:50:42 +0200220 }
221}
222
Kevin O'Connor7c790292014-02-11 15:34:58 -0500223int
224cbvga_setup(void)
225{
226 dprintf(1, "coreboot vga init\n");
227
228 if (GET_GLOBAL(HaveRunInit))
229 return 0;
230
231 struct cb_header *cbh = find_cb_table();
232 if (!cbh) {
233 dprintf(1, "Unable to find coreboot table\n");
234 return -1;
235 }
236 struct cb_framebuffer *cbfb = find_cb_subtable(cbh, CB_TAG_FRAMEBUFFER);
237 if (!cbfb) {
238 // Assume there is an EGA text framebuffer.
239 dprintf(1, "Did not find coreboot framebuffer - assuming EGA text\n");
240 SET_VGA(CBmode, 0x03);
241 SET_VGA(CBlinelength, 80*2);
242 SET_VGA(CBmodeinfo.memmodel, MM_TEXT);
243 SET_VGA(CBmodeinfo.width, 80);
244 SET_VGA(CBmodeinfo.height, 25);
245 SET_VGA(CBmodeinfo.depth, 4);
246 SET_VGA(CBmodeinfo.cwidth, 9);
247 SET_VGA(CBmodeinfo.cheight, 16);
248 SET_VGA(CBmodeinfo.sstart, SEG_CTEXT);
249 return 0;
250 }
251
252 u64 addr = GET_FARVAR(0, cbfb->physical_address);
Matt DeVillierbf8e4f92018-09-11 16:54:53 -0500253 u8 bpp = GET_FARVAR(0, cbfb->blue_mask_size)
254 + GET_FARVAR(0, cbfb->green_mask_size)
255 + GET_FARVAR(0, cbfb->red_mask_size)
256 + GET_FARVAR(0, cbfb->reserved_mask_size);
Kevin O'Connor7c790292014-02-11 15:34:58 -0500257 u32 xlines = GET_FARVAR(0, cbfb->x_resolution);
258 u32 ylines = GET_FARVAR(0, cbfb->y_resolution);
259 u32 linelength = GET_FARVAR(0, cbfb->bytes_per_line);
Matt DeVillierbf8e4f92018-09-11 16:54:53 -0500260 //fall back to coreboot reported bpp if calculated value invalid
261 if (bpp != 15 && bpp != 16 && bpp != 24 && bpp != 32)
262 bpp = GET_FARVAR(0, cbfb->bits_per_pixel);
263
Kevin O'Connor7c790292014-02-11 15:34:58 -0500264 dprintf(1, "Found FB @ %llx %dx%d with %d bpp (%d stride)\n"
265 , addr, xlines, ylines, bpp, linelength);
266
267 if (!addr || addr > 0xffffffff
268 || (bpp != 15 && bpp != 16 && bpp != 24 && bpp != 32)) {
269 dprintf(1, "Unable to use FB\n");
270 return -1;
271 }
272
Gerd Hoffmann4d70b242018-05-30 22:50:42 +0200273 cbvga_setup_modes(addr, bpp, xlines, ylines, linelength);
Kevin O'Connor7c790292014-02-11 15:34:58 -0500274 return 0;
275}