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 30 #include <sys/types.h> 31 32 #include <time.h> 33 #include <assert.h> 34 35 #include <machine/vmm.h> 36 #include <vmmapi.h> 37 38 #include "acpi.h" 39 #include "config.h" 40 #include "pci_lpc.h" 41 #include "rtc.h" 42 43 #define IO_RTC 0x70 44 45 #define RTC_LMEM_LSB 0x34 46 #define RTC_LMEM_MSB 0x35 47 #define RTC_HMEM_LSB 0x5b 48 #define RTC_HMEM_SB 0x5c 49 #define RTC_HMEM_MSB 0x5d 50 51 #define m_64KB (64*1024) 52 #define m_16MB (16*1024*1024) 53 #define m_4GB (4ULL*1024*1024*1024) 54 55 /* 56 * Returns the current RTC time as number of seconds since 00:00:00 Jan 1, 1970 57 */ 58 #ifdef __FreeBSD__ 59 static time_t 60 rtc_time(void) 61 { 62 struct tm tm; 63 time_t t; 64 65 time(&t); 66 if (get_config_bool_default("rtc.use_localtime", true)) { 67 localtime_r(&t, &tm); 68 t = timegm(&tm); 69 } 70 return (t); 71 } 72 #else /* __FreeBSD__ */ 73 static void 74 rtc_time(timespec_t *ts) 75 { 76 (void) clock_gettime(CLOCK_REALTIME, ts); 77 78 if (get_config_bool_default("rtc.use_localtime", true)) { 79 struct tm tm; 80 81 localtime_r(&ts->tv_sec, &tm); 82 ts->tv_sec = timegm(&tm); 83 } 84 } 85 #endif /* __FreeBSD__ */ 86 87 void 88 rtc_init(struct vmctx *ctx) 89 { 90 size_t himem; 91 size_t lomem; 92 int err; 93 94 /* XXX init diag/reset code/equipment/checksum ? */ 95 96 /* 97 * Report guest memory size in nvram cells as required by UEFI. 98 * Little-endian encoding. 99 * 0x34/0x35 - 64KB chunks above 16MB, below 4GB 100 * 0x5b/0x5c/0x5d - 64KB chunks above 4GB 101 */ 102 lomem = (vm_get_lowmem_size(ctx) - m_16MB) / m_64KB; 103 err = vm_rtc_write(ctx, RTC_LMEM_LSB, lomem); 104 assert(err == 0); 105 err = vm_rtc_write(ctx, RTC_LMEM_MSB, lomem >> 8); 106 assert(err == 0); 107 108 himem = vm_get_highmem_size(ctx) / m_64KB; 109 err = vm_rtc_write(ctx, RTC_HMEM_LSB, himem); 110 assert(err == 0); 111 err = vm_rtc_write(ctx, RTC_HMEM_SB, himem >> 8); 112 assert(err == 0); 113 err = vm_rtc_write(ctx, RTC_HMEM_MSB, himem >> 16); 114 assert(err == 0); 115 116 #ifdef __FreeBSD__ 117 err = vm_rtc_settime(ctx, rtc_time()); 118 #else 119 timespec_t ts; 120 121 rtc_time(&ts); 122 err = vm_rtc_settime(ctx, &ts); 123 #endif 124 assert(err == 0); 125 } 126 127 static void 128 rtc_dsdt(void) 129 { 130 131 dsdt_line(""); 132 dsdt_line("Device (RTC)"); 133 dsdt_line("{"); 134 dsdt_line(" Name (_HID, EisaId (\"PNP0B00\"))"); 135 dsdt_line(" Name (_CRS, ResourceTemplate ()"); 136 dsdt_line(" {"); 137 dsdt_indent(2); 138 dsdt_fixed_ioport(IO_RTC, 2); 139 dsdt_fixed_irq(8); 140 dsdt_unindent(2); 141 dsdt_line(" })"); 142 dsdt_line("}"); 143 } 144 LPC_DSDT(rtc_dsdt); 145 146 /* 147 * Reserve the extended RTC I/O ports although they are not emulated at this 148 * time. 149 */ 150 SYSRES_IO(0x72, 6); 151