blob: f85989a232df187f654b2964f41c1b45b34a3863 [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{
Patrick Rudolphe30d51c2017-05-29 19:25:14 +0200107 if (GET_GLOBAL(CBmode) != 0x3) {
108 /* Advertise additional SVGA modes for Microsoft NTLDR graphical mode.
109 * Microsoft NTLDR:
110 * + Graphical mode uses a maximum resolution of 1600x1200.
111 * + Expects to find VESA mode with 800x600 or 1024x768.
112 * + 24 Bpp and 32 Bpp are supported
113 */
114 int i;
115 for (i = 0; i < ARRAY_SIZE(cbvesa_modes) && dest < last; i++) {
116 struct cbvga_mode_s *cbmode_g = &cbvesa_modes[i];
117 u16 mode = GET_GLOBAL(cbmode_g->mode);
118 if (mode == 0xffff)
119 continue;
120 SET_FARVAR(seg, *dest, mode);
121 dest++;
122 }
123 }
124 if (dest < last) {
Kevin O'Connor7c790292014-02-11 15:34:58 -0500125 SET_FARVAR(seg, *dest, GET_GLOBAL(CBmode));
126 dest++;
127 }
128 SET_FARVAR(seg, *dest, 0xffff);
129}
130
131int
132cbvga_get_window(struct vgamode_s *vmode_g, int window)
133{
134 return -1;
135}
136
137int
138cbvga_set_window(struct vgamode_s *vmode_g, int window, int val)
139{
140 return -1;
141}
142
143int
144cbvga_get_linelength(struct vgamode_s *vmode_g)
145{
146 return GET_GLOBAL(CBlinelength);
147}
148
149int
150cbvga_set_linelength(struct vgamode_s *vmode_g, int val)
151{
152 return -1;
153}
154
155int
156cbvga_get_displaystart(struct vgamode_s *vmode_g)
157{
158 return 0;
159}
160
161int
162cbvga_set_displaystart(struct vgamode_s *vmode_g, int val)
163{
164 return -1;
165}
166
167int
168cbvga_get_dacformat(struct vgamode_s *vmode_g)
169{
170 return -1;
171}
172
173int
174cbvga_set_dacformat(struct vgamode_s *vmode_g, int val)
175{
176 return -1;
177}
178
179int
180cbvga_save_restore(int cmd, u16 seg, void *data)
181{
182 if (cmd & (SR_HARDWARE|SR_DAC|SR_REGISTERS))
183 return -1;
184 return bda_save_restore(cmd, seg, data);
185}
186
187int
188cbvga_set_mode(struct vgamode_s *vmode_g, int flags)
189{
Kevin O'Connorb4eb6fc2014-10-17 22:15:42 -0400190 u8 emul = vmode_g == &CBemulinfo || GET_GLOBAL(CBmode) == 0x03;
191 MASK_BDA_EXT(flags, BF_EMULATE_TEXT, emul ? BF_EMULATE_TEXT : 0);
Kevin O'Connor7c790292014-02-11 15:34:58 -0500192 if (!(flags & MF_NOCLEARMEM)) {
193 if (GET_GLOBAL(CBmodeinfo.memmodel) == MM_TEXT) {
194 memset16_far(SEG_CTEXT, (void*)0, 0x0720, 80*25*2);
195 return 0;
196 }
197 struct gfx_op op;
Patrick Rudolph6b694462017-05-29 19:25:13 +0200198 init_gfx_op(&op, &CBmodeinfo);
Kevin O'Connor7c790292014-02-11 15:34:58 -0500199 op.x = op.y = 0;
200 op.xlen = GET_GLOBAL(CBmodeinfo.width);
201 op.ylen = GET_GLOBAL(CBmodeinfo.height);
202 op.op = GO_MEMSET;
203 handle_gfx_op(&op);
204 }
205 return 0;
206}
207
Patrick Rudolph4902b8a2017-05-29 19:25:12 +0200208int
209cbvga_get_linesize(struct vgamode_s *vmode_g)
210{
211 /* Can't change mode, always report active pitch. */
212 return GET_GLOBAL(CBlinelength);
213}
214
Kevin O'Connor7c790292014-02-11 15:34:58 -0500215#define CB_TAG_FRAMEBUFFER 0x0012
216struct cb_framebuffer {
217 u32 tag;
218 u32 size;
219
220 u64 physical_address;
221 u32 x_resolution;
222 u32 y_resolution;
223 u32 bytes_per_line;
224 u8 bits_per_pixel;
225 u8 red_mask_pos;
226 u8 red_mask_size;
227 u8 green_mask_pos;
228 u8 green_mask_size;
229 u8 blue_mask_pos;
230 u8 blue_mask_size;
231 u8 reserved_mask_pos;
232 u8 reserved_mask_size;
233};
234
235int
236cbvga_setup(void)
237{
Patrick Rudolphe30d51c2017-05-29 19:25:14 +0200238 int i;
Kevin O'Connor7c790292014-02-11 15:34:58 -0500239 dprintf(1, "coreboot vga init\n");
240
241 if (GET_GLOBAL(HaveRunInit))
242 return 0;
243
244 struct cb_header *cbh = find_cb_table();
245 if (!cbh) {
246 dprintf(1, "Unable to find coreboot table\n");
247 return -1;
248 }
249 struct cb_framebuffer *cbfb = find_cb_subtable(cbh, CB_TAG_FRAMEBUFFER);
250 if (!cbfb) {
251 // Assume there is an EGA text framebuffer.
252 dprintf(1, "Did not find coreboot framebuffer - assuming EGA text\n");
253 SET_VGA(CBmode, 0x03);
254 SET_VGA(CBlinelength, 80*2);
255 SET_VGA(CBmodeinfo.memmodel, MM_TEXT);
256 SET_VGA(CBmodeinfo.width, 80);
257 SET_VGA(CBmodeinfo.height, 25);
258 SET_VGA(CBmodeinfo.depth, 4);
259 SET_VGA(CBmodeinfo.cwidth, 9);
260 SET_VGA(CBmodeinfo.cheight, 16);
261 SET_VGA(CBmodeinfo.sstart, SEG_CTEXT);
262 return 0;
263 }
264
265 u64 addr = GET_FARVAR(0, cbfb->physical_address);
Patrick Rudolph4b42cc42017-05-29 19:25:10 +0200266 u8 bpp = cbfb->blue_mask_size + cbfb->green_mask_size
267 + cbfb->red_mask_size + cbfb->reserved_mask_size;
Kevin O'Connor7c790292014-02-11 15:34:58 -0500268 u32 xlines = GET_FARVAR(0, cbfb->x_resolution);
269 u32 ylines = GET_FARVAR(0, cbfb->y_resolution);
270 u32 linelength = GET_FARVAR(0, cbfb->bytes_per_line);
271 dprintf(1, "Found FB @ %llx %dx%d with %d bpp (%d stride)\n"
272 , addr, xlines, ylines, bpp, linelength);
273
274 if (!addr || addr > 0xffffffff
275 || (bpp != 15 && bpp != 16 && bpp != 24 && bpp != 32)) {
276 dprintf(1, "Unable to use FB\n");
277 return -1;
278 }
279
280 SET_VGA(CBmode, 0x140);
281 SET_VGA(VBE_framebuffer, addr);
282 SET_VGA(VBE_total_memory, linelength * ylines);
283 SET_VGA(CBlinelength, linelength);
284 SET_VGA(CBmodeinfo.memmodel, MM_DIRECT);
285 SET_VGA(CBmodeinfo.width, xlines);
286 SET_VGA(CBmodeinfo.height, ylines);
287 SET_VGA(CBmodeinfo.depth, bpp);
288 SET_VGA(CBmodeinfo.cwidth, 8);
289 SET_VGA(CBmodeinfo.cheight, 16);
Kevin O'Connor6fed3072014-10-30 12:09:19 -0400290 memcpy_far(get_global_seg(), &CBemulinfo
291 , get_global_seg(), &CBmodeinfo, sizeof(CBemulinfo));
Kevin O'Connor7c790292014-02-11 15:34:58 -0500292
Patrick Rudolphe30d51c2017-05-29 19:25:14 +0200293 // Validate modes
294 for (i = 0; i < ARRAY_SIZE(cbvesa_modes); i++) {
295 struct cbvga_mode_s *cbmode_g = &cbvesa_modes[i];
296 /* Skip VBE modes that doesn't fit into coreboot's framebuffer */
297 if ((GET_GLOBAL(cbmode_g->info.height) > ylines)
298 || (GET_GLOBAL(cbmode_g->info.width) > xlines)
299 || (GET_GLOBAL(cbmode_g->info.depth) != bpp)) {
300 dprintf(3, "Removing mode %x\n", GET_GLOBAL(cbmode_g->mode));
301 SET_VGA(cbmode_g->mode, 0xffff);
302 }
303 }
Kevin O'Connor7c790292014-02-11 15:34:58 -0500304 return 0;
305}