Kevin O'Connor | 7c79029 | 2014-02-11 15:34:58 -0500 | [diff] [blame] | 1 | // Simple framebuffer vgabios for use with coreboot native vga init. |
| 2 | // |
| 3 | // Copyright (C) 2014 Kevin O'Connor <kevin@koconnor.net> |
Patrick Rudolph | 4b42cc4 | 2017-05-29 19:25:10 +0200 | [diff] [blame] | 4 | // Copyright (C) 2017 Patrick Rudolph <siro@das-labor.org> |
Kevin O'Connor | 7c79029 | 2014-02-11 15:34:58 -0500 | [diff] [blame] | 5 | // |
| 6 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
| 7 | |
| 8 | #include "biosvar.h" // GET_BDA |
Kevin O'Connor | 7c79029 | 2014-02-11 15:34:58 -0500 | [diff] [blame] | 9 | #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'Connor | 2f2ec11 | 2016-08-05 11:14:58 -0400 | [diff] [blame] | 13 | #include "vgabios.h" // SET_VGA |
Kevin O'Connor | 0397e80 | 2016-08-04 17:53:45 -0400 | [diff] [blame] | 14 | #include "vgafb.h" // handle_gfx_op |
Kevin O'Connor | 2f2ec11 | 2016-08-05 11:14:58 -0400 | [diff] [blame] | 15 | #include "vgautil.h" // VBE_total_memory |
Kevin O'Connor | 7c79029 | 2014-02-11 15:34:58 -0500 | [diff] [blame] | 16 | |
| 17 | static int CBmode VAR16; |
| 18 | static struct vgamode_s CBmodeinfo VAR16; |
Kevin O'Connor | 6fed307 | 2014-10-30 12:09:19 -0400 | [diff] [blame] | 19 | static struct vgamode_s CBemulinfo VAR16; |
Kevin O'Connor | 7c79029 | 2014-02-11 15:34:58 -0500 | [diff] [blame] | 20 | static u32 CBlinelength VAR16; |
| 21 | |
Patrick Rudolph | e30d51c | 2017-05-29 19:25:14 +0200 | [diff] [blame] | 22 | static 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'Connor | 7c79029 | 2014-02-11 15:34:58 -0500 | [diff] [blame] | 86 | struct vgamode_s *cbvga_find_mode(int mode) |
| 87 | { |
| 88 | if (mode == GET_GLOBAL(CBmode)) |
| 89 | return &CBmodeinfo; |
Kevin O'Connor | 6fed307 | 2014-10-30 12:09:19 -0400 | [diff] [blame] | 90 | if (mode == 0x03) |
| 91 | return &CBemulinfo; |
Patrick Rudolph | e30d51c | 2017-05-29 19:25:14 +0200 | [diff] [blame] | 92 | |
| 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'Connor | 7c79029 | 2014-02-11 15:34:58 -0500 | [diff] [blame] | 101 | return NULL; |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | cbvga_list_modes(u16 seg, u16 *dest, u16 *last) |
| 106 | { |
Patrick Rudolph | e30d51c | 2017-05-29 19:25:14 +0200 | [diff] [blame] | 107 | 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'Connor | 7c79029 | 2014-02-11 15:34:58 -0500 | [diff] [blame] | 125 | SET_FARVAR(seg, *dest, GET_GLOBAL(CBmode)); |
| 126 | dest++; |
| 127 | } |
| 128 | SET_FARVAR(seg, *dest, 0xffff); |
| 129 | } |
| 130 | |
| 131 | int |
| 132 | cbvga_get_window(struct vgamode_s *vmode_g, int window) |
| 133 | { |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | int |
| 138 | cbvga_set_window(struct vgamode_s *vmode_g, int window, int val) |
| 139 | { |
| 140 | return -1; |
| 141 | } |
| 142 | |
| 143 | int |
| 144 | cbvga_get_linelength(struct vgamode_s *vmode_g) |
| 145 | { |
| 146 | return GET_GLOBAL(CBlinelength); |
| 147 | } |
| 148 | |
| 149 | int |
| 150 | cbvga_set_linelength(struct vgamode_s *vmode_g, int val) |
| 151 | { |
| 152 | return -1; |
| 153 | } |
| 154 | |
| 155 | int |
| 156 | cbvga_get_displaystart(struct vgamode_s *vmode_g) |
| 157 | { |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | int |
| 162 | cbvga_set_displaystart(struct vgamode_s *vmode_g, int val) |
| 163 | { |
| 164 | return -1; |
| 165 | } |
| 166 | |
| 167 | int |
| 168 | cbvga_get_dacformat(struct vgamode_s *vmode_g) |
| 169 | { |
| 170 | return -1; |
| 171 | } |
| 172 | |
| 173 | int |
| 174 | cbvga_set_dacformat(struct vgamode_s *vmode_g, int val) |
| 175 | { |
| 176 | return -1; |
| 177 | } |
| 178 | |
| 179 | int |
| 180 | cbvga_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 | |
| 187 | int |
| 188 | cbvga_set_mode(struct vgamode_s *vmode_g, int flags) |
| 189 | { |
Kevin O'Connor | b4eb6fc | 2014-10-17 22:15:42 -0400 | [diff] [blame] | 190 | u8 emul = vmode_g == &CBemulinfo || GET_GLOBAL(CBmode) == 0x03; |
| 191 | MASK_BDA_EXT(flags, BF_EMULATE_TEXT, emul ? BF_EMULATE_TEXT : 0); |
Kevin O'Connor | 7c79029 | 2014-02-11 15:34:58 -0500 | [diff] [blame] | 192 | 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 Rudolph | 6b69446 | 2017-05-29 19:25:13 +0200 | [diff] [blame] | 198 | init_gfx_op(&op, &CBmodeinfo); |
Kevin O'Connor | 7c79029 | 2014-02-11 15:34:58 -0500 | [diff] [blame] | 199 | 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 Rudolph | 4902b8a | 2017-05-29 19:25:12 +0200 | [diff] [blame] | 208 | int |
| 209 | cbvga_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'Connor | 7c79029 | 2014-02-11 15:34:58 -0500 | [diff] [blame] | 215 | #define CB_TAG_FRAMEBUFFER 0x0012 |
| 216 | struct 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 | |
Gerd Hoffmann | 4d70b24 | 2018-05-30 22:50:42 +0200 | [diff] [blame^] | 235 | void |
| 236 | cbvga_setup_modes(u64 addr, u8 bpp, u32 xlines, u32 ylines, u32 linelength) |
| 237 | { |
| 238 | int i; |
| 239 | |
| 240 | SET_VGA(CBmode, 0x140); |
| 241 | SET_VGA(VBE_framebuffer, addr); |
| 242 | SET_VGA(VBE_total_memory, linelength * ylines); |
| 243 | SET_VGA(CBlinelength, linelength); |
| 244 | SET_VGA(CBmodeinfo.memmodel, MM_DIRECT); |
| 245 | SET_VGA(CBmodeinfo.width, xlines); |
| 246 | SET_VGA(CBmodeinfo.height, ylines); |
| 247 | SET_VGA(CBmodeinfo.depth, bpp); |
| 248 | SET_VGA(CBmodeinfo.cwidth, 8); |
| 249 | SET_VGA(CBmodeinfo.cheight, 16); |
| 250 | memcpy_far(get_global_seg(), &CBemulinfo |
| 251 | , get_global_seg(), &CBmodeinfo, sizeof(CBemulinfo)); |
| 252 | |
| 253 | // Validate modes |
| 254 | for (i = 0; i < ARRAY_SIZE(cbvesa_modes); i++) { |
| 255 | struct cbvga_mode_s *cbmode_g = &cbvesa_modes[i]; |
| 256 | /* Skip VBE modes that doesn't fit into coreboot's framebuffer */ |
| 257 | if ((GET_GLOBAL(cbmode_g->info.height) > ylines) |
| 258 | || (GET_GLOBAL(cbmode_g->info.width) > xlines) |
| 259 | || (GET_GLOBAL(cbmode_g->info.depth) != bpp)) { |
| 260 | dprintf(3, "Removing mode %x\n", GET_GLOBAL(cbmode_g->mode)); |
| 261 | SET_VGA(cbmode_g->mode, 0xffff); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
Kevin O'Connor | 7c79029 | 2014-02-11 15:34:58 -0500 | [diff] [blame] | 266 | int |
| 267 | cbvga_setup(void) |
| 268 | { |
| 269 | dprintf(1, "coreboot vga init\n"); |
| 270 | |
| 271 | if (GET_GLOBAL(HaveRunInit)) |
| 272 | return 0; |
| 273 | |
| 274 | struct cb_header *cbh = find_cb_table(); |
| 275 | if (!cbh) { |
| 276 | dprintf(1, "Unable to find coreboot table\n"); |
| 277 | return -1; |
| 278 | } |
| 279 | struct cb_framebuffer *cbfb = find_cb_subtable(cbh, CB_TAG_FRAMEBUFFER); |
| 280 | if (!cbfb) { |
| 281 | // Assume there is an EGA text framebuffer. |
| 282 | dprintf(1, "Did not find coreboot framebuffer - assuming EGA text\n"); |
| 283 | SET_VGA(CBmode, 0x03); |
| 284 | SET_VGA(CBlinelength, 80*2); |
| 285 | SET_VGA(CBmodeinfo.memmodel, MM_TEXT); |
| 286 | SET_VGA(CBmodeinfo.width, 80); |
| 287 | SET_VGA(CBmodeinfo.height, 25); |
| 288 | SET_VGA(CBmodeinfo.depth, 4); |
| 289 | SET_VGA(CBmodeinfo.cwidth, 9); |
| 290 | SET_VGA(CBmodeinfo.cheight, 16); |
| 291 | SET_VGA(CBmodeinfo.sstart, SEG_CTEXT); |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | u64 addr = GET_FARVAR(0, cbfb->physical_address); |
Patrick Rudolph | 4b42cc4 | 2017-05-29 19:25:10 +0200 | [diff] [blame] | 296 | u8 bpp = cbfb->blue_mask_size + cbfb->green_mask_size |
| 297 | + cbfb->red_mask_size + cbfb->reserved_mask_size; |
Kevin O'Connor | 7c79029 | 2014-02-11 15:34:58 -0500 | [diff] [blame] | 298 | u32 xlines = GET_FARVAR(0, cbfb->x_resolution); |
| 299 | u32 ylines = GET_FARVAR(0, cbfb->y_resolution); |
| 300 | u32 linelength = GET_FARVAR(0, cbfb->bytes_per_line); |
| 301 | dprintf(1, "Found FB @ %llx %dx%d with %d bpp (%d stride)\n" |
| 302 | , addr, xlines, ylines, bpp, linelength); |
| 303 | |
| 304 | if (!addr || addr > 0xffffffff |
| 305 | || (bpp != 15 && bpp != 16 && bpp != 24 && bpp != 32)) { |
| 306 | dprintf(1, "Unable to use FB\n"); |
| 307 | return -1; |
| 308 | } |
| 309 | |
Gerd Hoffmann | 4d70b24 | 2018-05-30 22:50:42 +0200 | [diff] [blame^] | 310 | cbvga_setup_modes(addr, bpp, xlines, ylines, linelength); |
Kevin O'Connor | 7c79029 | 2014-02-11 15:34:58 -0500 | [diff] [blame] | 311 | return 0; |
| 312 | } |