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