Kevin O'Connor | d9211ee | 2012-01-31 22:54:49 -0500 | [diff] [blame] | 1 | // Bochs VGA interface to extended "VBE" modes |
| 2 | // |
| 3 | // Copyright (C) 2012 Kevin O'Connor <kevin@koconnor.net> |
| 4 | // Copyright (C) 2011 Julian Pidancet <julian.pidancet@citrix.com> |
| 5 | // Copyright (C) 2002 Jeroen Janssen |
| 6 | // |
| 7 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
| 8 | |
Kevin O'Connor | e6bc4c1 | 2012-01-21 11:26:37 -0500 | [diff] [blame] | 9 | #include "biosvar.h" // GET_GLOBAL |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 10 | #include "bochsvga.h" // bochsvga_set_mode |
| 11 | #include "config.h" // CONFIG_* |
Kevin O'Connor | 5d369d8 | 2013-09-02 20:48:46 -0400 | [diff] [blame] | 12 | #include "hw/pci.h" // pci_config_readl |
| 13 | #include "hw/pci_regs.h" // PCI_BASE_ADDRESS_0 |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 14 | #include "output.h" // dprintf |
Kevin O'Connor | 2e57c81 | 2013-09-14 22:29:32 -0400 | [diff] [blame] | 15 | #include "std/vbe.h" // VBE_CAPABILITY_8BIT_DAC |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 16 | #include "stdvga.h" // VGAREG_SEQU_ADDRESS |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 17 | #include "vgabios.h" // struct vbe_modeinfo |
Kevin O'Connor | 4ade523 | 2013-09-18 21:41:48 -0400 | [diff] [blame] | 18 | #include "x86.h" // outw |
Julian Pidancet | 87879e2 | 2011-12-19 05:08:00 +0000 | [diff] [blame] | 19 | |
Kevin O'Connor | 933bb76 | 2012-02-01 21:54:55 -0500 | [diff] [blame] | 20 | |
| 21 | /**************************************************************** |
| 22 | * Mode tables |
| 23 | ****************************************************************/ |
| 24 | |
Kevin O'Connor | c4a0b97 | 2012-01-09 20:21:31 -0500 | [diff] [blame] | 25 | static struct bochsvga_mode |
Julian Pidancet | 87879e2 | 2011-12-19 05:08:00 +0000 | [diff] [blame] | 26 | { |
| 27 | u16 mode; |
Kevin O'Connor | c4a0b97 | 2012-01-09 20:21:31 -0500 | [diff] [blame] | 28 | struct vgamode_s info; |
Kevin O'Connor | f1e217d | 2011-12-31 03:18:18 -0500 | [diff] [blame] | 29 | } bochsvga_modes[] VAR16 = { |
Julian Pidancet | 87879e2 | 2011-12-19 05:08:00 +0000 | [diff] [blame] | 30 | /* standard modes */ |
Kevin O'Connor | 0377602 | 2012-01-21 11:00:11 -0500 | [diff] [blame] | 31 | { 0x100, { MM_PACKED, 640, 400, 8, 8, 16, SEG_GRAPH } }, |
| 32 | { 0x101, { MM_PACKED, 640, 480, 8, 8, 16, SEG_GRAPH } }, |
| 33 | { 0x102, { MM_PLANAR, 800, 600, 4, 8, 16, SEG_GRAPH } }, |
| 34 | { 0x103, { MM_PACKED, 800, 600, 8, 8, 16, SEG_GRAPH } }, |
| 35 | { 0x104, { MM_PLANAR, 1024, 768, 4, 8, 16, SEG_GRAPH } }, |
| 36 | { 0x105, { MM_PACKED, 1024, 768, 8, 8, 16, SEG_GRAPH } }, |
| 37 | { 0x106, { MM_PLANAR, 1280, 1024, 4, 8, 16, SEG_GRAPH } }, |
| 38 | { 0x107, { MM_PACKED, 1280, 1024, 8, 8, 16, SEG_GRAPH } }, |
| 39 | { 0x10D, { MM_DIRECT, 320, 200, 15, 8, 16, SEG_GRAPH } }, |
| 40 | { 0x10E, { MM_DIRECT, 320, 200, 16, 8, 16, SEG_GRAPH } }, |
| 41 | { 0x10F, { MM_DIRECT, 320, 200, 24, 8, 16, SEG_GRAPH } }, |
| 42 | { 0x110, { MM_DIRECT, 640, 480, 15, 8, 16, SEG_GRAPH } }, |
| 43 | { 0x111, { MM_DIRECT, 640, 480, 16, 8, 16, SEG_GRAPH } }, |
| 44 | { 0x112, { MM_DIRECT, 640, 480, 24, 8, 16, SEG_GRAPH } }, |
| 45 | { 0x113, { MM_DIRECT, 800, 600, 15, 8, 16, SEG_GRAPH } }, |
| 46 | { 0x114, { MM_DIRECT, 800, 600, 16, 8, 16, SEG_GRAPH } }, |
| 47 | { 0x115, { MM_DIRECT, 800, 600, 24, 8, 16, SEG_GRAPH } }, |
| 48 | { 0x116, { MM_DIRECT, 1024, 768, 15, 8, 16, SEG_GRAPH } }, |
| 49 | { 0x117, { MM_DIRECT, 1024, 768, 16, 8, 16, SEG_GRAPH } }, |
| 50 | { 0x118, { MM_DIRECT, 1024, 768, 24, 8, 16, SEG_GRAPH } }, |
| 51 | { 0x119, { MM_DIRECT, 1280, 1024, 15, 8, 16, SEG_GRAPH } }, |
| 52 | { 0x11A, { MM_DIRECT, 1280, 1024, 16, 8, 16, SEG_GRAPH } }, |
| 53 | { 0x11B, { MM_DIRECT, 1280, 1024, 24, 8, 16, SEG_GRAPH } }, |
| 54 | { 0x11C, { MM_PACKED, 1600, 1200, 8, 8, 16, SEG_GRAPH } }, |
| 55 | { 0x11D, { MM_DIRECT, 1600, 1200, 15, 8, 16, SEG_GRAPH } }, |
| 56 | { 0x11E, { MM_DIRECT, 1600, 1200, 16, 8, 16, SEG_GRAPH } }, |
| 57 | { 0x11F, { MM_DIRECT, 1600, 1200, 24, 8, 16, SEG_GRAPH } }, |
Julian Pidancet | 87879e2 | 2011-12-19 05:08:00 +0000 | [diff] [blame] | 58 | /* BOCHS modes */ |
Kevin O'Connor | 0377602 | 2012-01-21 11:00:11 -0500 | [diff] [blame] | 59 | { 0x140, { MM_DIRECT, 320, 200, 32, 8, 16, SEG_GRAPH } }, |
| 60 | { 0x141, { MM_DIRECT, 640, 400, 32, 8, 16, SEG_GRAPH } }, |
| 61 | { 0x142, { MM_DIRECT, 640, 480, 32, 8, 16, SEG_GRAPH } }, |
| 62 | { 0x143, { MM_DIRECT, 800, 600, 32, 8, 16, SEG_GRAPH } }, |
| 63 | { 0x144, { MM_DIRECT, 1024, 768, 32, 8, 16, SEG_GRAPH } }, |
| 64 | { 0x145, { MM_DIRECT, 1280, 1024, 32, 8, 16, SEG_GRAPH } }, |
| 65 | { 0x146, { MM_PACKED, 320, 200, 8, 8, 16, SEG_GRAPH } }, |
| 66 | { 0x147, { MM_DIRECT, 1600, 1200, 32, 8, 16, SEG_GRAPH } }, |
| 67 | { 0x148, { MM_PACKED, 1152, 864, 8, 8, 16, SEG_GRAPH } }, |
| 68 | { 0x149, { MM_DIRECT, 1152, 864, 15, 8, 16, SEG_GRAPH } }, |
| 69 | { 0x14a, { MM_DIRECT, 1152, 864, 16, 8, 16, SEG_GRAPH } }, |
| 70 | { 0x14b, { MM_DIRECT, 1152, 864, 24, 8, 16, SEG_GRAPH } }, |
| 71 | { 0x14c, { MM_DIRECT, 1152, 864, 32, 8, 16, SEG_GRAPH } }, |
Kevin O'Connor | a9dcc88 | 2013-11-30 11:04:09 -0500 | [diff] [blame] | 72 | { 0x175, { MM_DIRECT, 1280, 768, 16, 8, 16, SEG_GRAPH } }, |
| 73 | { 0x176, { MM_DIRECT, 1280, 768, 24, 8, 16, SEG_GRAPH } }, |
| 74 | { 0x177, { MM_DIRECT, 1280, 768, 32, 8, 16, SEG_GRAPH } }, |
Kevin O'Connor | 0377602 | 2012-01-21 11:00:11 -0500 | [diff] [blame] | 75 | { 0x178, { MM_DIRECT, 1280, 800, 16, 8, 16, SEG_GRAPH } }, |
| 76 | { 0x179, { MM_DIRECT, 1280, 800, 24, 8, 16, SEG_GRAPH } }, |
| 77 | { 0x17a, { MM_DIRECT, 1280, 800, 32, 8, 16, SEG_GRAPH } }, |
| 78 | { 0x17b, { MM_DIRECT, 1280, 960, 16, 8, 16, SEG_GRAPH } }, |
| 79 | { 0x17c, { MM_DIRECT, 1280, 960, 24, 8, 16, SEG_GRAPH } }, |
| 80 | { 0x17d, { MM_DIRECT, 1280, 960, 32, 8, 16, SEG_GRAPH } }, |
| 81 | { 0x17e, { MM_DIRECT, 1440, 900, 16, 8, 16, SEG_GRAPH } }, |
| 82 | { 0x17f, { MM_DIRECT, 1440, 900, 24, 8, 16, SEG_GRAPH } }, |
| 83 | { 0x180, { MM_DIRECT, 1440, 900, 32, 8, 16, SEG_GRAPH } }, |
| 84 | { 0x181, { MM_DIRECT, 1400, 1050, 16, 8, 16, SEG_GRAPH } }, |
| 85 | { 0x182, { MM_DIRECT, 1400, 1050, 24, 8, 16, SEG_GRAPH } }, |
| 86 | { 0x183, { MM_DIRECT, 1400, 1050, 32, 8, 16, SEG_GRAPH } }, |
| 87 | { 0x184, { MM_DIRECT, 1680, 1050, 16, 8, 16, SEG_GRAPH } }, |
| 88 | { 0x185, { MM_DIRECT, 1680, 1050, 24, 8, 16, SEG_GRAPH } }, |
| 89 | { 0x186, { MM_DIRECT, 1680, 1050, 32, 8, 16, SEG_GRAPH } }, |
| 90 | { 0x187, { MM_DIRECT, 1920, 1200, 16, 8, 16, SEG_GRAPH } }, |
| 91 | { 0x188, { MM_DIRECT, 1920, 1200, 24, 8, 16, SEG_GRAPH } }, |
| 92 | { 0x189, { MM_DIRECT, 1920, 1200, 32, 8, 16, SEG_GRAPH } }, |
| 93 | { 0x18a, { MM_DIRECT, 2560, 1600, 16, 8, 16, SEG_GRAPH } }, |
| 94 | { 0x18b, { MM_DIRECT, 2560, 1600, 24, 8, 16, SEG_GRAPH } }, |
| 95 | { 0x18c, { MM_DIRECT, 2560, 1600, 32, 8, 16, SEG_GRAPH } }, |
Kevin O'Connor | a9dcc88 | 2013-11-30 11:04:09 -0500 | [diff] [blame] | 96 | { 0x18d, { MM_DIRECT, 1280, 720, 16, 8, 16, SEG_GRAPH } }, |
| 97 | { 0x18e, { MM_DIRECT, 1280, 720, 24, 8, 16, SEG_GRAPH } }, |
| 98 | { 0x18f, { MM_DIRECT, 1280, 720, 32, 8, 16, SEG_GRAPH } }, |
| 99 | { 0x190, { MM_DIRECT, 1920, 1080, 16, 8, 16, SEG_GRAPH } }, |
| 100 | { 0x191, { MM_DIRECT, 1920, 1080, 24, 8, 16, SEG_GRAPH } }, |
| 101 | { 0x192, { MM_DIRECT, 1920, 1080, 32, 8, 16, SEG_GRAPH } }, |
Julian Pidancet | 87879e2 | 2011-12-19 05:08:00 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
Gerd Hoffmann | 5366350 | 2013-09-05 10:16:18 +0200 | [diff] [blame] | 104 | static int dispi_found VAR16 = 0; |
| 105 | |
Kevin O'Connor | e6bc4c1 | 2012-01-21 11:26:37 -0500 | [diff] [blame] | 106 | static int is_bochsvga_mode(struct vgamode_s *vmode_g) |
| 107 | { |
| 108 | return (vmode_g >= &bochsvga_modes[0].info |
| 109 | && vmode_g <= &bochsvga_modes[ARRAY_SIZE(bochsvga_modes)-1].info); |
| 110 | } |
| 111 | |
Kevin O'Connor | 3339c05 | 2012-01-13 20:00:35 -0500 | [diff] [blame] | 112 | struct vgamode_s *bochsvga_find_mode(int mode) |
| 113 | { |
| 114 | struct bochsvga_mode *m = bochsvga_modes; |
Gerd Hoffmann | 5366350 | 2013-09-05 10:16:18 +0200 | [diff] [blame] | 115 | if (GET_GLOBAL(dispi_found)) |
| 116 | for (; m < &bochsvga_modes[ARRAY_SIZE(bochsvga_modes)]; m++) |
| 117 | if (GET_GLOBAL(m->mode) == mode) |
| 118 | return &m->info; |
Kevin O'Connor | 3339c05 | 2012-01-13 20:00:35 -0500 | [diff] [blame] | 119 | return stdvga_find_mode(mode); |
Julian Pidancet | 87879e2 | 2011-12-19 05:08:00 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Kevin O'Connor | 34203cd | 2012-01-09 20:55:31 -0500 | [diff] [blame] | 122 | void |
| 123 | bochsvga_list_modes(u16 seg, u16 *dest, u16 *last) |
Julian Pidancet | 87879e2 | 2011-12-19 05:08:00 +0000 | [diff] [blame] | 124 | { |
Kevin O'Connor | c4a0b97 | 2012-01-09 20:21:31 -0500 | [diff] [blame] | 125 | struct bochsvga_mode *m = bochsvga_modes; |
Gerd Hoffmann | 5366350 | 2013-09-05 10:16:18 +0200 | [diff] [blame] | 126 | if (GET_GLOBAL(dispi_found)) { |
| 127 | for (; m < &bochsvga_modes[ARRAY_SIZE(bochsvga_modes)] && dest<last; m++) { |
| 128 | u16 mode = GET_GLOBAL(m->mode); |
| 129 | if (mode == 0xffff) |
| 130 | continue; |
| 131 | SET_FARVAR(seg, *dest, mode); |
| 132 | dest++; |
| 133 | } |
Julian Pidancet | 8bd766f | 2011-12-19 05:08:01 +0000 | [diff] [blame] | 134 | } |
Kevin O'Connor | 34203cd | 2012-01-09 20:55:31 -0500 | [diff] [blame] | 135 | stdvga_list_modes(seg, dest, last); |
Julian Pidancet | 87879e2 | 2011-12-19 05:08:00 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Kevin O'Connor | 933bb76 | 2012-02-01 21:54:55 -0500 | [diff] [blame] | 138 | |
| 139 | /**************************************************************** |
| 140 | * Helper functions |
| 141 | ****************************************************************/ |
| 142 | |
Kevin O'Connor | 4ade523 | 2013-09-18 21:41:48 -0400 | [diff] [blame] | 143 | static inline u16 dispi_read(u16 reg) |
| 144 | { |
| 145 | outw(reg, VBE_DISPI_IOPORT_INDEX); |
| 146 | return inw(VBE_DISPI_IOPORT_DATA); |
| 147 | } |
| 148 | static inline void dispi_write(u16 reg, u16 val) |
| 149 | { |
| 150 | outw(reg, VBE_DISPI_IOPORT_INDEX); |
| 151 | outw(val, VBE_DISPI_IOPORT_DATA); |
| 152 | } |
| 153 | |
Kevin O'Connor | 9961f99 | 2012-01-21 11:53:44 -0500 | [diff] [blame] | 154 | int |
| 155 | bochsvga_get_window(struct vgamode_s *vmode_g, int window) |
| 156 | { |
Gerd Hoffmann | 5366350 | 2013-09-05 10:16:18 +0200 | [diff] [blame] | 157 | if (!GET_GLOBAL(dispi_found)) |
| 158 | return stdvga_get_window(vmode_g, window); |
Kevin O'Connor | 9961f99 | 2012-01-21 11:53:44 -0500 | [diff] [blame] | 159 | if (window != 0) |
| 160 | return -1; |
| 161 | return dispi_read(VBE_DISPI_INDEX_BANK); |
| 162 | } |
| 163 | |
| 164 | int |
| 165 | bochsvga_set_window(struct vgamode_s *vmode_g, int window, int val) |
| 166 | { |
Gerd Hoffmann | 5366350 | 2013-09-05 10:16:18 +0200 | [diff] [blame] | 167 | if (!GET_GLOBAL(dispi_found)) |
| 168 | return stdvga_set_window(vmode_g, window, val); |
Kevin O'Connor | 9961f99 | 2012-01-21 11:53:44 -0500 | [diff] [blame] | 169 | if (window != 0) |
| 170 | return -1; |
| 171 | dispi_write(VBE_DISPI_INDEX_BANK, val); |
| 172 | if (dispi_read(VBE_DISPI_INDEX_BANK) != val) |
| 173 | return -1; |
| 174 | return 0; |
| 175 | } |
| 176 | |
Kevin O'Connor | 3876b53 | 2012-01-24 00:07:44 -0500 | [diff] [blame] | 177 | int |
| 178 | bochsvga_get_linelength(struct vgamode_s *vmode_g) |
| 179 | { |
Gerd Hoffmann | 5366350 | 2013-09-05 10:16:18 +0200 | [diff] [blame] | 180 | if (!GET_GLOBAL(dispi_found)) |
| 181 | return stdvga_get_linelength(vmode_g); |
Kevin O'Connor | 3876b53 | 2012-01-24 00:07:44 -0500 | [diff] [blame] | 182 | return dispi_read(VBE_DISPI_INDEX_VIRT_WIDTH) * vga_bpp(vmode_g) / 8; |
| 183 | } |
| 184 | |
| 185 | int |
| 186 | bochsvga_set_linelength(struct vgamode_s *vmode_g, int val) |
| 187 | { |
| 188 | stdvga_set_linelength(vmode_g, val); |
Gerd Hoffmann | 5366350 | 2013-09-05 10:16:18 +0200 | [diff] [blame] | 189 | if (GET_GLOBAL(dispi_found)) { |
| 190 | int pixels = (val * 8) / vga_bpp(vmode_g); |
| 191 | dispi_write(VBE_DISPI_INDEX_VIRT_WIDTH, pixels); |
| 192 | } |
Kevin O'Connor | 3876b53 | 2012-01-24 00:07:44 -0500 | [diff] [blame] | 193 | return 0; |
| 194 | } |
| 195 | |
Kevin O'Connor | d61fc53 | 2012-01-27 20:37:45 -0500 | [diff] [blame] | 196 | int |
| 197 | bochsvga_get_displaystart(struct vgamode_s *vmode_g) |
| 198 | { |
Gerd Hoffmann | 5366350 | 2013-09-05 10:16:18 +0200 | [diff] [blame] | 199 | if (!GET_GLOBAL(dispi_found)) |
| 200 | return stdvga_get_displaystart(vmode_g); |
Kevin O'Connor | d61fc53 | 2012-01-27 20:37:45 -0500 | [diff] [blame] | 201 | int bpp = vga_bpp(vmode_g); |
| 202 | int linelength = dispi_read(VBE_DISPI_INDEX_VIRT_WIDTH) * bpp / 8; |
| 203 | int x = dispi_read(VBE_DISPI_INDEX_X_OFFSET); |
| 204 | int y = dispi_read(VBE_DISPI_INDEX_Y_OFFSET); |
| 205 | return x * bpp / 8 + linelength * y; |
| 206 | } |
| 207 | |
| 208 | int |
| 209 | bochsvga_set_displaystart(struct vgamode_s *vmode_g, int val) |
| 210 | { |
| 211 | stdvga_set_displaystart(vmode_g, val); |
Gerd Hoffmann | 5366350 | 2013-09-05 10:16:18 +0200 | [diff] [blame] | 212 | if (GET_GLOBAL(dispi_found)) { |
| 213 | int bpp = vga_bpp(vmode_g); |
| 214 | int linelength = dispi_read(VBE_DISPI_INDEX_VIRT_WIDTH) * bpp / 8; |
Kevin O'Connor | 06c6d47 | 2013-11-30 11:45:46 -0500 | [diff] [blame] | 215 | if (!linelength) |
| 216 | return 0; |
Gerd Hoffmann | 5366350 | 2013-09-05 10:16:18 +0200 | [diff] [blame] | 217 | dispi_write(VBE_DISPI_INDEX_X_OFFSET, (val % linelength) * 8 / bpp); |
| 218 | dispi_write(VBE_DISPI_INDEX_Y_OFFSET, val / linelength); |
| 219 | } |
Kevin O'Connor | d61fc53 | 2012-01-27 20:37:45 -0500 | [diff] [blame] | 220 | return 0; |
| 221 | } |
| 222 | |
Kevin O'Connor | e737b17 | 2012-02-04 11:08:39 -0500 | [diff] [blame] | 223 | int |
| 224 | bochsvga_get_dacformat(struct vgamode_s *vmode_g) |
| 225 | { |
Gerd Hoffmann | 5366350 | 2013-09-05 10:16:18 +0200 | [diff] [blame] | 226 | if (!GET_GLOBAL(dispi_found)) |
| 227 | return stdvga_get_dacformat(vmode_g); |
Kevin O'Connor | e737b17 | 2012-02-04 11:08:39 -0500 | [diff] [blame] | 228 | u16 en = dispi_read(VBE_DISPI_INDEX_ENABLE); |
| 229 | return (en & VBE_DISPI_8BIT_DAC) ? 8 : 6; |
| 230 | } |
| 231 | |
| 232 | int |
| 233 | bochsvga_set_dacformat(struct vgamode_s *vmode_g, int val) |
| 234 | { |
Gerd Hoffmann | 5366350 | 2013-09-05 10:16:18 +0200 | [diff] [blame] | 235 | if (!GET_GLOBAL(dispi_found)) |
| 236 | return stdvga_set_dacformat(vmode_g, val); |
Kevin O'Connor | e737b17 | 2012-02-04 11:08:39 -0500 | [diff] [blame] | 237 | u16 en = dispi_read(VBE_DISPI_INDEX_ENABLE); |
| 238 | if (val == 6) |
| 239 | en &= ~VBE_DISPI_8BIT_DAC; |
| 240 | else if (val == 8) |
| 241 | en |= VBE_DISPI_8BIT_DAC; |
| 242 | else |
| 243 | return -1; |
| 244 | dispi_write(VBE_DISPI_INDEX_ENABLE, en); |
| 245 | return 0; |
| 246 | } |
| 247 | |
Kevin O'Connor | 2469f89 | 2012-02-04 12:40:02 -0500 | [diff] [blame] | 248 | int |
| 249 | bochsvga_size_state(int states) |
| 250 | { |
| 251 | int size = stdvga_size_state(states); |
| 252 | if (size < 0) |
| 253 | return size; |
Gerd Hoffmann | 5366350 | 2013-09-05 10:16:18 +0200 | [diff] [blame] | 254 | if (GET_GLOBAL(dispi_found) && (states & 8)) |
Kevin O'Connor | 2469f89 | 2012-02-04 12:40:02 -0500 | [diff] [blame] | 255 | size += (VBE_DISPI_INDEX_Y_OFFSET-VBE_DISPI_INDEX_XRES+1)*sizeof(u16); |
| 256 | return size; |
| 257 | } |
| 258 | |
| 259 | int |
| 260 | bochsvga_save_state(u16 seg, void *data, int states) |
| 261 | { |
| 262 | int ret = stdvga_save_state(seg, data, states); |
| 263 | if (ret < 0) |
| 264 | return ret; |
| 265 | |
Gerd Hoffmann | 5366350 | 2013-09-05 10:16:18 +0200 | [diff] [blame] | 266 | if (!GET_GLOBAL(dispi_found)) |
| 267 | return 0; |
Kevin O'Connor | 2469f89 | 2012-02-04 12:40:02 -0500 | [diff] [blame] | 268 | if (!(states & 8)) |
| 269 | return 0; |
| 270 | |
| 271 | u16 *info = (data + stdvga_size_state(states)); |
| 272 | u16 en = dispi_read(VBE_DISPI_INDEX_ENABLE); |
| 273 | SET_FARVAR(seg, *info, en); |
| 274 | info++; |
| 275 | if (!(en & VBE_DISPI_ENABLED)) |
| 276 | return 0; |
| 277 | int i; |
| 278 | for (i = VBE_DISPI_INDEX_XRES; i <= VBE_DISPI_INDEX_Y_OFFSET; i++) |
| 279 | if (i != VBE_DISPI_INDEX_ENABLE) { |
| 280 | u16 v = dispi_read(i); |
| 281 | SET_FARVAR(seg, *info, v); |
| 282 | info++; |
| 283 | } |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | int |
| 288 | bochsvga_restore_state(u16 seg, void *data, int states) |
| 289 | { |
| 290 | int ret = stdvga_restore_state(seg, data, states); |
| 291 | if (ret < 0) |
| 292 | return ret; |
| 293 | |
Gerd Hoffmann | 5366350 | 2013-09-05 10:16:18 +0200 | [diff] [blame] | 294 | if (!GET_GLOBAL(dispi_found)) |
| 295 | return 0; |
Kevin O'Connor | 2469f89 | 2012-02-04 12:40:02 -0500 | [diff] [blame] | 296 | if (!(states & 8)) |
| 297 | return 0; |
| 298 | |
| 299 | u16 *info = (data + stdvga_size_state(states)); |
| 300 | u16 en = GET_FARVAR(seg, *info); |
| 301 | info++; |
| 302 | if (!(en & VBE_DISPI_ENABLED)) { |
| 303 | dispi_write(VBE_DISPI_INDEX_ENABLE, en); |
| 304 | return 0; |
| 305 | } |
| 306 | int i; |
| 307 | for (i = VBE_DISPI_INDEX_XRES; i <= VBE_DISPI_INDEX_Y_OFFSET; i++) |
| 308 | if (i == VBE_DISPI_INDEX_ENABLE) { |
| 309 | dispi_write(i, en); |
| 310 | } else { |
| 311 | dispi_write(i, GET_FARVAR(seg, *info)); |
| 312 | info++; |
| 313 | } |
| 314 | return 0; |
| 315 | } |
| 316 | |
Kevin O'Connor | 933bb76 | 2012-02-01 21:54:55 -0500 | [diff] [blame] | 317 | |
| 318 | /**************************************************************** |
| 319 | * Mode setting |
| 320 | ****************************************************************/ |
| 321 | |
Kevin O'Connor | 5108c69 | 2011-12-31 19:13:45 -0500 | [diff] [blame] | 322 | int |
Kevin O'Connor | e6bc4c1 | 2012-01-21 11:26:37 -0500 | [diff] [blame] | 323 | bochsvga_set_mode(struct vgamode_s *vmode_g, int flags) |
Julian Pidancet | 87879e2 | 2011-12-19 05:08:00 +0000 | [diff] [blame] | 324 | { |
Gerd Hoffmann | 5366350 | 2013-09-05 10:16:18 +0200 | [diff] [blame] | 325 | if (GET_GLOBAL(dispi_found)) |
| 326 | dispi_write(VBE_DISPI_INDEX_ENABLE, VBE_DISPI_DISABLED); |
Kevin O'Connor | c884502 | 2012-01-31 22:51:56 -0500 | [diff] [blame] | 327 | if (! is_bochsvga_mode(vmode_g)) |
Kevin O'Connor | e6bc4c1 | 2012-01-21 11:26:37 -0500 | [diff] [blame] | 328 | return stdvga_set_mode(vmode_g, flags); |
Gerd Hoffmann | 5366350 | 2013-09-05 10:16:18 +0200 | [diff] [blame] | 329 | if (!GET_GLOBAL(dispi_found)) |
| 330 | return -1; |
Kevin O'Connor | 5108c69 | 2011-12-31 19:13:45 -0500 | [diff] [blame] | 331 | |
Kevin O'Connor | 5b6936e | 2013-11-29 18:43:35 -0500 | [diff] [blame] | 332 | u8 memmodel = GET_GLOBAL(vmode_g->memmodel); |
| 333 | if (memmodel == MM_PLANAR) |
Kevin O'Connor | e6bc4c1 | 2012-01-21 11:26:37 -0500 | [diff] [blame] | 334 | stdvga_set_mode(stdvga_find_mode(0x6a), 0); |
Kevin O'Connor | 5b6936e | 2013-11-29 18:43:35 -0500 | [diff] [blame] | 335 | if (memmodel == MM_PACKED && !(flags & MF_NOPALETTE)) |
| 336 | stdvga_set_packed_palette(); |
Julian Pidancet | 87879e2 | 2011-12-19 05:08:00 +0000 | [diff] [blame] | 337 | |
Kevin O'Connor | 5b6936e | 2013-11-29 18:43:35 -0500 | [diff] [blame] | 338 | dispi_write(VBE_DISPI_INDEX_BPP, GET_GLOBAL(vmode_g->depth)); |
Kevin O'Connor | 3339c05 | 2012-01-13 20:00:35 -0500 | [diff] [blame] | 339 | u16 width = GET_GLOBAL(vmode_g->width); |
| 340 | u16 height = GET_GLOBAL(vmode_g->height); |
| 341 | dispi_write(VBE_DISPI_INDEX_XRES, width); |
| 342 | dispi_write(VBE_DISPI_INDEX_YRES, height); |
Julian Pidancet | 8bd766f | 2011-12-19 05:08:01 +0000 | [diff] [blame] | 343 | dispi_write(VBE_DISPI_INDEX_BANK, 0); |
Kevin O'Connor | c884502 | 2012-01-31 22:51:56 -0500 | [diff] [blame] | 344 | u16 bf = ((flags & MF_NOCLEARMEM ? VBE_DISPI_NOCLEARMEM : 0) |
| 345 | | (flags & MF_LINEARFB ? VBE_DISPI_LFB_ENABLED : 0)); |
| 346 | dispi_write(VBE_DISPI_INDEX_ENABLE, VBE_DISPI_ENABLED | bf); |
Julian Pidancet | 8bd766f | 2011-12-19 05:08:01 +0000 | [diff] [blame] | 347 | |
| 348 | /* VGA compat setup */ |
Kevin O'Connor | 184705f | 2012-01-14 22:17:43 -0500 | [diff] [blame] | 349 | u16 crtc_addr = VGAREG_VGA_CRTC_ADDRESS; |
| 350 | stdvga_crtc_write(crtc_addr, 0x11, 0x00); |
| 351 | stdvga_crtc_write(crtc_addr, 0x01, width / 8 - 1); |
Kevin O'Connor | c884502 | 2012-01-31 22:51:56 -0500 | [diff] [blame] | 352 | stdvga_set_linelength(vmode_g, width); |
Kevin O'Connor | 184705f | 2012-01-14 22:17:43 -0500 | [diff] [blame] | 353 | stdvga_crtc_write(crtc_addr, 0x12, height - 1); |
| 354 | u8 v = 0; |
| 355 | if ((height - 1) & 0x0100) |
| 356 | v |= 0x02; |
| 357 | if ((height - 1) & 0x0200) |
Julian Pidancet | 8bd766f | 2011-12-19 05:08:01 +0000 | [diff] [blame] | 358 | v |= 0x40; |
Kevin O'Connor | 184705f | 2012-01-14 22:17:43 -0500 | [diff] [blame] | 359 | stdvga_crtc_mask(crtc_addr, 0x07, 0x42, v); |
Julian Pidancet | 8bd766f | 2011-12-19 05:08:01 +0000 | [diff] [blame] | 360 | |
Kevin O'Connor | 184705f | 2012-01-14 22:17:43 -0500 | [diff] [blame] | 361 | stdvga_crtc_write(crtc_addr, 0x09, 0x00); |
| 362 | stdvga_crtc_mask(crtc_addr, 0x17, 0x00, 0x03); |
| 363 | stdvga_attr_mask(0x10, 0x00, 0x01); |
| 364 | stdvga_grdc_write(0x06, 0x05); |
| 365 | stdvga_sequ_write(0x02, 0x0f); |
Kevin O'Connor | 5b6936e | 2013-11-29 18:43:35 -0500 | [diff] [blame] | 366 | if (memmodel != MM_PLANAR) { |
Kevin O'Connor | 184705f | 2012-01-14 22:17:43 -0500 | [diff] [blame] | 367 | stdvga_crtc_mask(crtc_addr, 0x14, 0x00, 0x40); |
| 368 | stdvga_attr_mask(0x10, 0x00, 0x40); |
| 369 | stdvga_sequ_mask(0x04, 0x00, 0x08); |
| 370 | stdvga_grdc_mask(0x05, 0x20, 0x40); |
Julian Pidancet | 8bd766f | 2011-12-19 05:08:01 +0000 | [diff] [blame] | 371 | } |
David Woodhouse | 0069a31 | 2013-02-08 15:50:54 +0000 | [diff] [blame] | 372 | stdvga_attrindex_write(0x20); |
Julian Pidancet | 8bd766f | 2011-12-19 05:08:01 +0000 | [diff] [blame] | 373 | |
Kevin O'Connor | 5108c69 | 2011-12-31 19:13:45 -0500 | [diff] [blame] | 374 | return 0; |
Julian Pidancet | 87879e2 | 2011-12-19 05:08:00 +0000 | [diff] [blame] | 375 | } |
Kevin O'Connor | 933bb76 | 2012-02-01 21:54:55 -0500 | [diff] [blame] | 376 | |
| 377 | |
| 378 | /**************************************************************** |
| 379 | * Init |
| 380 | ****************************************************************/ |
| 381 | |
| 382 | int |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 383 | bochsvga_setup(void) |
Kevin O'Connor | 933bb76 | 2012-02-01 21:54:55 -0500 | [diff] [blame] | 384 | { |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 385 | int ret = stdvga_setup(); |
Kevin O'Connor | 933bb76 | 2012-02-01 21:54:55 -0500 | [diff] [blame] | 386 | if (ret) |
| 387 | return ret; |
| 388 | |
| 389 | /* Sanity checks */ |
| 390 | dispi_write(VBE_DISPI_INDEX_ID, VBE_DISPI_ID0); |
| 391 | if (dispi_read(VBE_DISPI_INDEX_ID) != VBE_DISPI_ID0) { |
Gerd Hoffmann | 5366350 | 2013-09-05 10:16:18 +0200 | [diff] [blame] | 392 | dprintf(1, "No VBE DISPI interface detected, falling back to stdvga\n"); |
| 393 | return 0; |
Kevin O'Connor | 933bb76 | 2012-02-01 21:54:55 -0500 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | dispi_write(VBE_DISPI_INDEX_ID, VBE_DISPI_ID5); |
Gerd Hoffmann | 5366350 | 2013-09-05 10:16:18 +0200 | [diff] [blame] | 397 | SET_VGA(dispi_found, 1); |
Kevin O'Connor | 933bb76 | 2012-02-01 21:54:55 -0500 | [diff] [blame] | 398 | |
Kevin O'Connor | cfd7ef9 | 2012-02-02 22:52:17 -0500 | [diff] [blame] | 399 | if (GET_GLOBAL(HaveRunInit)) |
| 400 | return 0; |
| 401 | |
Kevin O'Connor | 933bb76 | 2012-02-01 21:54:55 -0500 | [diff] [blame] | 402 | u32 lfb_addr = VBE_DISPI_LFB_PHYSICAL_ADDRESS; |
| 403 | int bdf = GET_GLOBAL(VgaBDF); |
| 404 | if (CONFIG_VGA_PCI && bdf >= 0) { |
| 405 | int barid = 0; |
| 406 | u32 bar = pci_config_readl(bdf, PCI_BASE_ADDRESS_0); |
| 407 | if ((bar & PCI_BASE_ADDRESS_SPACE) != PCI_BASE_ADDRESS_SPACE_MEMORY) { |
| 408 | barid = 1; |
| 409 | bar = pci_config_readl(bdf, PCI_BASE_ADDRESS_1); |
| 410 | } |
| 411 | lfb_addr = bar & PCI_BASE_ADDRESS_MEM_MASK; |
| 412 | dprintf(1, "VBE DISPI: bdf %02x:%02x.%x, bar %d\n", pci_bdf_to_bus(bdf) |
| 413 | , pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf), barid); |
| 414 | } |
| 415 | |
| 416 | SET_VGA(VBE_framebuffer, lfb_addr); |
| 417 | u32 totalmem = dispi_read(VBE_DISPI_INDEX_VIDEO_MEMORY_64K) * 64 * 1024; |
| 418 | SET_VGA(VBE_total_memory, totalmem); |
Kevin O'Connor | 49ddd9e | 2012-09-04 13:16:36 -0400 | [diff] [blame] | 419 | SET_VGA(VBE_win_granularity, 64); |
Kevin O'Connor | 933bb76 | 2012-02-01 21:54:55 -0500 | [diff] [blame] | 420 | SET_VGA(VBE_capabilities, VBE_CAPABILITY_8BIT_DAC); |
| 421 | |
| 422 | dprintf(1, "VBE DISPI: lfb_addr=%x, size %d MB\n", |
Gerd Hoffmann | 091dd17 | 2012-02-06 15:51:43 +0100 | [diff] [blame] | 423 | lfb_addr, totalmem >> 20); |
Kevin O'Connor | 933bb76 | 2012-02-01 21:54:55 -0500 | [diff] [blame] | 424 | |
| 425 | // Validate modes |
| 426 | u16 en = dispi_read(VBE_DISPI_INDEX_ENABLE); |
| 427 | dispi_write(VBE_DISPI_INDEX_ENABLE, en | VBE_DISPI_GETCAPS); |
| 428 | u16 max_xres = dispi_read(VBE_DISPI_INDEX_XRES); |
| 429 | u16 max_bpp = dispi_read(VBE_DISPI_INDEX_BPP); |
| 430 | dispi_write(VBE_DISPI_INDEX_ENABLE, en); |
| 431 | struct bochsvga_mode *m = bochsvga_modes; |
| 432 | for (; m < &bochsvga_modes[ARRAY_SIZE(bochsvga_modes)]; m++) { |
| 433 | u16 width = GET_GLOBAL(m->info.width); |
| 434 | u16 height = GET_GLOBAL(m->info.height); |
| 435 | u8 depth = GET_GLOBAL(m->info.depth); |
| 436 | u32 mem = (height * DIV_ROUND_UP(width * vga_bpp(&m->info), 8) |
Kevin O'Connor | 68f56aa | 2013-09-10 10:41:33 -0400 | [diff] [blame] | 437 | * stdvga_vram_ratio(&m->info)); |
Kevin O'Connor | 933bb76 | 2012-02-01 21:54:55 -0500 | [diff] [blame] | 438 | |
| 439 | if (width > max_xres || depth > max_bpp || mem > totalmem) { |
| 440 | dprintf(1, "Removing mode %x\n", GET_GLOBAL(m->mode)); |
| 441 | SET_VGA(m->mode, 0xffff); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | return 0; |
| 446 | } |