xref: /freebsd/usr.sbin/bhyve/amd64/rtc.c (revision 4d65a7c6951cea0333f1a0c1b32c38489cdfa6c5)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 NetApp, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/types.h>
30 
31 #include <time.h>
32 #include <assert.h>
33 
34 #include <machine/vmm.h>
35 #include <vmmapi.h>
36 
37 #include "acpi.h"
38 #include "config.h"
39 #include "pci_lpc.h"
40 #include "rtc.h"
41 
42 #define	IO_RTC		0x70
43 
44 #define	RTC_LMEM_LSB	0x34
45 #define	RTC_LMEM_MSB	0x35
46 #define	RTC_HMEM_LSB	0x5b
47 #define	RTC_HMEM_SB	0x5c
48 #define	RTC_HMEM_MSB	0x5d
49 
50 #define	m_64KB		(64*1024)
51 #define	m_16MB		(16*1024*1024)
52 #define	m_4GB		(4ULL*1024*1024*1024)
53 
54 /*
55  * Returns the current RTC time as number of seconds since 00:00:00 Jan 1, 1970
56  */
57 static time_t
rtc_time(void)58 rtc_time(void)
59 {
60 	struct tm tm;
61 	time_t t;
62 
63 	time(&t);
64 	if (get_config_bool_default("rtc.use_localtime", true)) {
65 		localtime_r(&t, &tm);
66 		t = timegm(&tm);
67 	}
68 	return (t);
69 }
70 
71 void
rtc_init(struct vmctx * ctx)72 rtc_init(struct vmctx *ctx)
73 {
74 	size_t himem;
75 	size_t lomem;
76 	int err;
77 
78 	/* XXX init diag/reset code/equipment/checksum ? */
79 
80 	/*
81 	 * Report guest memory size in nvram cells as required by UEFI.
82 	 * Little-endian encoding.
83 	 * 0x34/0x35 - 64KB chunks above 16MB, below 4GB
84 	 * 0x5b/0x5c/0x5d - 64KB chunks above 4GB
85 	 */
86 	lomem = (vm_get_lowmem_size(ctx) - m_16MB) / m_64KB;
87 	err = vm_rtc_write(ctx, RTC_LMEM_LSB, lomem);
88 	assert(err == 0);
89 	err = vm_rtc_write(ctx, RTC_LMEM_MSB, lomem >> 8);
90 	assert(err == 0);
91 
92 	himem = vm_get_highmem_size(ctx) / m_64KB;
93 	err = vm_rtc_write(ctx, RTC_HMEM_LSB, himem);
94 	assert(err == 0);
95 	err = vm_rtc_write(ctx, RTC_HMEM_SB, himem >> 8);
96 	assert(err == 0);
97 	err = vm_rtc_write(ctx, RTC_HMEM_MSB, himem >> 16);
98 	assert(err == 0);
99 
100 	err = vm_rtc_settime(ctx, rtc_time());
101 	assert(err == 0);
102 }
103 
104 static void
rtc_dsdt(void)105 rtc_dsdt(void)
106 {
107 
108 	dsdt_line("");
109 	dsdt_line("Device (RTC)");
110 	dsdt_line("{");
111 	dsdt_line("  Name (_HID, EisaId (\"PNP0B00\"))");
112 	dsdt_line("  Name (_CRS, ResourceTemplate ()");
113 	dsdt_line("  {");
114 	dsdt_indent(2);
115 	dsdt_fixed_ioport(IO_RTC, 2);
116 	dsdt_fixed_irq(8);
117 	dsdt_unindent(2);
118 	dsdt_line("  })");
119 	dsdt_line("}");
120 }
121 LPC_DSDT(rtc_dsdt);
122 
123 /*
124  * Reserve the extended RTC I/O ports although they are not emulated at this
125  * time.
126  */
127 SYSRES_IO(0x72, 6);
128