Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / video / bochs.c
blob3bcfa53a9e96e262fe5609171da10b2525bbedd5
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #define grub_video_render_target grub_video_fbrender_target
21 #include <grub/err.h>
22 #include <grub/types.h>
23 #include <grub/dl.h>
24 #include <grub/misc.h>
25 #include <grub/mm.h>
26 #include <grub/video.h>
27 #include <grub/video_fb.h>
28 #include <grub/pci.h>
29 #include <grub/vga.h>
31 GRUB_MOD_LICENSE ("GPLv3+");
33 static struct
35 struct grub_video_mode_info mode_info;
37 grub_uint8_t *ptr;
38 int mapped;
39 grub_uint32_t base;
40 grub_pci_device_t dev;
41 } framebuffer;
43 #define BOCHS_APERTURE_SIZE 0x800000
44 #define BOCHS_MAX_WIDTH 1600
45 #define BOCHS_MAX_HEIGHT 1200
46 #define BOCHS_WIDTH_ALIGN 8
48 enum
50 BOCHS_VBE_INDEX = 0x1ce,
51 BOCHS_VBE_DATA = 0x1cf,
54 enum
56 BOCHS_VBE_WIDTH = 1,
57 BOCHS_VBE_HEIGHT = 2,
58 BOCHS_VBE_BPP = 3,
59 BOCHS_VBE_ENABLE = 4,
60 BOCHS_VBE_Y_OFFSET = 9,
61 BOCHS_VBE_MAX
64 static void
65 vbe_write (grub_uint16_t val, grub_uint16_t addr)
67 grub_outw (addr, BOCHS_VBE_INDEX);
68 grub_outw (val, BOCHS_VBE_DATA);
71 static grub_uint16_t
72 vbe_read (grub_uint16_t addr)
74 grub_outw (addr, BOCHS_VBE_INDEX);
75 return grub_inw (BOCHS_VBE_DATA);
78 struct saved_state
80 grub_uint8_t cr[256];
81 grub_uint8_t gr[256];
82 grub_uint8_t sr[256];
83 grub_uint8_t r[256];
84 grub_uint8_t g[256];
85 grub_uint8_t b[256];
86 grub_uint8_t vbe[BOCHS_VBE_MAX];
87 int vbe_enable;
88 /* We need to preserve VGA font and VGA text. */
89 grub_uint8_t vram[32 * 4 * 256];
92 static struct saved_state initial_state;
93 static int state_saved = 0;
95 static void
96 save_state (struct saved_state *st)
98 unsigned i;
100 for (i = 0; i < ARRAY_SIZE (st->cr); i++)
101 st->cr[i] = grub_vga_cr_read (i);
102 for (i = 0; i < ARRAY_SIZE (st->gr); i++)
103 st->gr[i] = grub_vga_gr_read (i);
104 for (i = 0; i < ARRAY_SIZE (st->sr); i++)
105 st->sr[i] = grub_vga_sr_read (i);
107 for (i = 0; i < 256; i++)
108 grub_vga_palette_read (i, st->r + i, st->g + i, st->b + i);
110 st->vbe_enable = vbe_read (BOCHS_VBE_ENABLE) & 1;
111 if (st->vbe_enable)
112 for (i = 0; i < ARRAY_SIZE (st->vbe); i++)
113 st->vbe[i] = vbe_read (i);
115 grub_vga_sr_write (GRUB_VGA_SR_MEMORY_MODE_CHAIN4, GRUB_VGA_SR_MEMORY_MODE);
116 grub_memcpy (st->vram, framebuffer.ptr, sizeof (st->vram));
117 grub_vga_sr_write (st->sr[GRUB_VGA_SR_MEMORY_MODE], GRUB_VGA_SR_MEMORY_MODE);
120 static void
121 restore_state (struct saved_state *st)
123 unsigned i;
125 if (st->vbe_enable)
126 for (i = 0; i < ARRAY_SIZE (st->vbe); i++)
127 vbe_write (st->vbe[i], i);
128 else
129 vbe_write (0, BOCHS_VBE_ENABLE);
131 grub_vga_cr_write (0, 0x11);
132 for (i = 0; i < ARRAY_SIZE (st->cr); i++)
133 grub_vga_cr_write (st->cr[i], i);
134 for (i = 0; i < ARRAY_SIZE (st->sr); i++)
135 grub_vga_sr_write (st->sr[i], i);
136 for (i = 0; i < ARRAY_SIZE (st->gr); i++)
137 grub_vga_gr_write (st->gr[i], i);
139 for (i = 0; i < 256; i++)
140 grub_vga_palette_write (i, st->r[i], st->g[i], st->b[i]);
142 grub_vga_sr_write (GRUB_VGA_SR_MEMORY_MODE_CHAIN4, GRUB_VGA_SR_MEMORY_MODE);
143 grub_memcpy (framebuffer.ptr, st->vram, sizeof (st->vram));
144 grub_vga_sr_write (st->sr[GRUB_VGA_SR_MEMORY_MODE], GRUB_VGA_SR_MEMORY_MODE);
147 static grub_err_t
148 grub_video_bochs_video_init (void)
150 /* Reset frame buffer. */
151 grub_memset (&framebuffer, 0, sizeof(framebuffer));
153 return grub_video_fb_init ();
156 static grub_err_t
157 grub_video_bochs_video_fini (void)
159 if (framebuffer.mapped)
160 grub_pci_device_unmap_range (framebuffer.dev, framebuffer.ptr,
161 BOCHS_APERTURE_SIZE);
163 if (state_saved)
165 restore_state (&initial_state);
166 state_saved = 0;
169 return grub_video_fb_fini ();
172 static grub_err_t
173 doublebuf_pageflipping_set_page (int page)
175 int start = framebuffer.mode_info.height * page;
177 vbe_write (start, BOCHS_VBE_Y_OFFSET);
178 return GRUB_ERR_NONE;
181 static grub_err_t
182 grub_video_bochs_set_palette (unsigned int start, unsigned int count,
183 struct grub_video_palette_data *palette_data)
185 if (framebuffer.mode_info.mode_type == GRUB_VIDEO_MODE_TYPE_INDEX_COLOR)
187 unsigned i;
188 if (start >= 0x100)
189 return GRUB_ERR_NONE;
190 if (start + count >= 0x100)
191 count = 0x100 - start;
193 for (i = 0; i < count; i++)
194 grub_vga_palette_write (start + i, palette_data[i].r, palette_data[i].g,
195 palette_data[i].b);
198 /* Then set color to emulated palette. */
199 return grub_video_fb_set_palette (start, count, palette_data);
202 /* Helper for grub_video_bochs_setup. */
203 static int
204 find_card (grub_pci_device_t dev, grub_pci_id_t pciid, void *data)
206 int *found = data;
207 grub_pci_address_t addr;
208 grub_uint32_t class;
210 addr = grub_pci_make_address (dev, GRUB_PCI_REG_CLASS);
211 class = grub_pci_read (addr);
213 if (((class >> 16) & 0xffff) != 0x0300 || pciid != 0x11111234)
214 return 0;
216 addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG0);
217 framebuffer.base = grub_pci_read (addr) & GRUB_PCI_ADDR_MEM_MASK;
218 if (!framebuffer.base)
219 return 0;
220 *found = 1;
221 framebuffer.dev = dev;
223 /* Enable address spaces. */
224 addr = grub_pci_make_address (framebuffer.dev, GRUB_PCI_REG_COMMAND);
225 grub_pci_write (addr, 0x7);
227 return 1;
230 static grub_err_t
231 grub_video_bochs_setup (unsigned int width, unsigned int height,
232 grub_video_mode_type_t mode_type,
233 grub_video_mode_type_t mode_mask)
235 int depth;
236 grub_err_t err;
237 int found = 0;
238 int pitch, bytes_per_pixel;
239 grub_size_t page_size; /* The size of a page in bytes. */
241 /* Decode depth from mode_type. If it is zero, then autodetect. */
242 depth = (mode_type & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK)
243 >> GRUB_VIDEO_MODE_TYPE_DEPTH_POS;
245 if (width == 0 || height == 0)
247 width = 800;
248 height = 600;
251 if (width > BOCHS_MAX_WIDTH)
252 return grub_error (GRUB_ERR_IO, "width must be at most",
253 BOCHS_MAX_WIDTH);
255 if (height > BOCHS_MAX_HEIGHT)
256 return grub_error (GRUB_ERR_IO, "height must be at most",
257 BOCHS_MAX_HEIGHT);
259 if (width & (BOCHS_WIDTH_ALIGN - 1))
260 return grub_error (GRUB_ERR_IO, "width must be a multiple of %d",
261 BOCHS_WIDTH_ALIGN);
263 if (depth == 0
264 && !grub_video_check_mode_flag (mode_type, mode_mask,
265 GRUB_VIDEO_MODE_TYPE_INDEX_COLOR, 0))
266 depth = 24;
268 if (depth == 0)
269 depth = 8;
271 if (depth != 32 && depth != 24 && depth != 16 && depth != 15 && depth != 8
272 && depth != 4)
273 return grub_error (GRUB_ERR_IO, "only 32, 24, 16, 15 and 8-bpp are"
274 " supported by bochs video");
276 if (depth == 4)
277 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, "4-bpp isn't cupported");
279 bytes_per_pixel = (depth + 7) / 8;
280 if (depth == 4)
281 pitch = width / 2;
282 else
283 pitch = width * bytes_per_pixel;
285 page_size = pitch * height;
287 if (page_size > BOCHS_APERTURE_SIZE)
288 return grub_error (GRUB_ERR_IO, "Not enough video memory for this mode");
290 grub_pci_iterate (find_card, &found);
291 if (!found)
292 return grub_error (GRUB_ERR_IO, "Couldn't find graphics card");
294 if (found && framebuffer.base == 0)
296 /* FIXME: change framebuffer base */
297 return grub_error (GRUB_ERR_IO, "PCI BAR not set");
300 /* We can safely discard volatile attribute. */
301 framebuffer.ptr = (void *) grub_pci_device_map_range (framebuffer.dev,
302 framebuffer.base,
303 BOCHS_APERTURE_SIZE);
304 framebuffer.mapped = 1;
306 if (!state_saved)
308 save_state (&initial_state);
309 state_saved = 1;
313 vbe_write (0, BOCHS_VBE_ENABLE);
315 vbe_write (width, BOCHS_VBE_WIDTH);
316 vbe_write (height, BOCHS_VBE_HEIGHT);
317 vbe_write (depth, BOCHS_VBE_BPP);
319 vbe_write (1, BOCHS_VBE_ENABLE);
320 doublebuf_pageflipping_set_page (0);
323 /* Fill mode info details. */
324 framebuffer.mode_info.width = width;
325 framebuffer.mode_info.height = height;
326 framebuffer.mode_info.mode_type = GRUB_VIDEO_MODE_TYPE_RGB;
327 framebuffer.mode_info.bpp = depth;
328 framebuffer.mode_info.bytes_per_pixel = bytes_per_pixel;
329 framebuffer.mode_info.pitch = pitch;
330 framebuffer.mode_info.number_of_colors = 256;
331 framebuffer.mode_info.reserved_mask_size = 0;
332 framebuffer.mode_info.reserved_field_pos = 0;
334 switch (depth)
336 case 4:
337 case 8:
338 framebuffer.mode_info.mode_type = GRUB_VIDEO_MODE_TYPE_INDEX_COLOR;
339 framebuffer.mode_info.number_of_colors = 16;
340 break;
341 case 16:
342 framebuffer.mode_info.red_mask_size = 5;
343 framebuffer.mode_info.red_field_pos = 11;
344 framebuffer.mode_info.green_mask_size = 6;
345 framebuffer.mode_info.green_field_pos = 5;
346 framebuffer.mode_info.blue_mask_size = 5;
347 framebuffer.mode_info.blue_field_pos = 0;
348 break;
350 case 15:
351 framebuffer.mode_info.red_mask_size = 5;
352 framebuffer.mode_info.red_field_pos = 10;
353 framebuffer.mode_info.green_mask_size = 5;
354 framebuffer.mode_info.green_field_pos = 5;
355 framebuffer.mode_info.blue_mask_size = 5;
356 framebuffer.mode_info.blue_field_pos = 0;
357 break;
359 case 32:
360 framebuffer.mode_info.reserved_mask_size = 8;
361 framebuffer.mode_info.reserved_field_pos = 24;
362 /* Fallthrough. */
364 case 24:
365 framebuffer.mode_info.red_mask_size = 8;
366 framebuffer.mode_info.red_field_pos = 16;
367 framebuffer.mode_info.green_mask_size = 8;
368 framebuffer.mode_info.green_field_pos = 8;
369 framebuffer.mode_info.blue_mask_size = 8;
370 framebuffer.mode_info.blue_field_pos = 0;
371 break;
374 framebuffer.mode_info.blit_format = grub_video_get_blit_format (&framebuffer.mode_info);
376 if (BOCHS_APERTURE_SIZE >= 2 * page_size)
377 err = grub_video_fb_setup (mode_type, mode_mask,
378 &framebuffer.mode_info,
379 framebuffer.ptr,
380 doublebuf_pageflipping_set_page,
381 framebuffer.ptr + page_size);
382 else
383 err = grub_video_fb_setup (mode_type, mode_mask,
384 &framebuffer.mode_info,
385 framebuffer.ptr, 0, 0);
388 /* Copy default palette to initialize emulated palette. */
389 err = grub_video_bochs_set_palette (0, GRUB_VIDEO_FBSTD_NUMCOLORS,
390 grub_video_fbstd_colors);
391 return err;
394 static struct grub_video_adapter grub_video_bochs_adapter =
396 .name = "Bochs PCI Video Driver",
397 .id = GRUB_VIDEO_DRIVER_BOCHS,
399 .prio = GRUB_VIDEO_ADAPTER_PRIO_NATIVE,
401 .init = grub_video_bochs_video_init,
402 .fini = grub_video_bochs_video_fini,
403 .setup = grub_video_bochs_setup,
404 .get_info = grub_video_fb_get_info,
405 .get_info_and_fini = grub_video_fb_get_info_and_fini,
406 .set_palette = grub_video_bochs_set_palette,
407 .get_palette = grub_video_fb_get_palette,
408 .set_viewport = grub_video_fb_set_viewport,
409 .get_viewport = grub_video_fb_get_viewport,
410 .set_region = grub_video_fb_set_region,
411 .get_region = grub_video_fb_get_region,
412 .set_area_status = grub_video_fb_set_area_status,
413 .get_area_status = grub_video_fb_get_area_status,
414 .map_color = grub_video_fb_map_color,
415 .map_rgb = grub_video_fb_map_rgb,
416 .map_rgba = grub_video_fb_map_rgba,
417 .unmap_color = grub_video_fb_unmap_color,
418 .fill_rect = grub_video_fb_fill_rect,
419 .blit_bitmap = grub_video_fb_blit_bitmap,
420 .blit_render_target = grub_video_fb_blit_render_target,
421 .scroll = grub_video_fb_scroll,
422 .swap_buffers = grub_video_fb_swap_buffers,
423 .create_render_target = grub_video_fb_create_render_target,
424 .delete_render_target = grub_video_fb_delete_render_target,
425 .set_active_render_target = grub_video_fb_set_active_render_target,
426 .get_active_render_target = grub_video_fb_get_active_render_target,
428 .next = 0
431 GRUB_MOD_INIT(video_bochs)
433 grub_video_register (&grub_video_bochs_adapter);
436 GRUB_MOD_FINI(video_bochs)
438 grub_video_unregister (&grub_video_bochs_adapter);