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