xref: /netbsd-src/sys/arch/xen/xen/genfb_xen.c (revision 1b2c77f6a3a4d386a295c61e52a15f6c51dcc552)
1 /*      $NetBSD: genfb_xen.c,v 1.2 2023/10/17 16:09:12 bouyer Exp $      */
2 
3 /*
4  * Copyright (c) 2023 Manuel Bouyer.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: genfb_xen.c,v 1.2 2023/10/17 16:09:12 bouyer Exp $");
30 
31 
32 #include <sys/device.h>
33 #include <xen/include/xen.h>
34 #include <xen/include/hypervisor.h>
35 #include <xen/include/public/xen.h>
36 #include <arch/x86/include/genfb_machdep.h>
37 #include <arch/x86/include/bootinfo.h>
38 
39 static struct btinfo_framebuffer _xen_genfb_btinfo = {0};
40 
41 const struct btinfo_framebuffer *
xen_genfb_getbtinfo(void)42 xen_genfb_getbtinfo(void)
43 {
44 	dom0_vga_console_info_t *d0_consi;
45 	int info_size;
46 
47 	if (!xendomain_is_dom0())
48 		return NULL;
49 
50 	if (_xen_genfb_btinfo.common.type == BTINFO_FRAMEBUFFER)
51 		return &_xen_genfb_btinfo;
52 
53 #ifdef XENPVHVM
54 	struct xen_platform_op op = {
55 		.cmd = XENPF_get_dom0_console,
56 	};
57 	info_size = HYPERVISOR_platform_op(&op);
58 	if (info_size < sizeof(dom0_vga_console_info_t)) {
59 		printf("XENPF_get_dom0_console fail %d\n", info_size);
60 		return NULL;
61 	}
62 	d0_consi = &op.u.dom0_console;
63 #else
64 	d0_consi = (void *)((char *)&xen_start_info +
65 	    xen_start_info.console.dom0.info_off);
66 	info_size = xen_start_info.console.dom0.info_size;
67 #endif
68 
69 	if (d0_consi->video_type != XEN_VGATYPE_VESA_LFB &&
70 	    d0_consi->video_type != XEN_VGATYPE_EFI_LFB)
71 		return NULL;
72 
73 	_xen_genfb_btinfo.common.type = BTINFO_FRAMEBUFFER;
74 	_xen_genfb_btinfo.common.len = sizeof(struct btinfo_framebuffer);
75 	_xen_genfb_btinfo.physaddr = d0_consi->u.vesa_lfb.lfb_base;
76 	if (info_size >=
77 	    offsetof(dom0_vga_console_info_t, u.vesa_lfb.ext_lfb_base)) {
78 		_xen_genfb_btinfo.physaddr |=
79 		    (uint64_t)d0_consi->u.vesa_lfb.ext_lfb_base << 32;
80 	}
81 	_xen_genfb_btinfo.flags = 0;
82 	_xen_genfb_btinfo.width = d0_consi->u.vesa_lfb.width;
83 	_xen_genfb_btinfo.height = d0_consi->u.vesa_lfb.height;
84 	_xen_genfb_btinfo.stride = d0_consi->u.vesa_lfb.bytes_per_line;
85 	_xen_genfb_btinfo.depth = d0_consi->u.vesa_lfb.bits_per_pixel;
86 	_xen_genfb_btinfo.rnum = d0_consi->u.vesa_lfb.red_pos;
87 	_xen_genfb_btinfo.gnum = d0_consi->u.vesa_lfb.green_pos;
88 	_xen_genfb_btinfo.bnum = d0_consi->u.vesa_lfb.blue_pos;
89 	_xen_genfb_btinfo.rpos = d0_consi->u.vesa_lfb.red_size;
90 	_xen_genfb_btinfo.gpos = d0_consi->u.vesa_lfb.green_size;
91 	_xen_genfb_btinfo.bpos = d0_consi->u.vesa_lfb.blue_size;
92 	_xen_genfb_btinfo.vbemode = 0; /* XXX */
93 
94 	return &_xen_genfb_btinfo;
95 }
96