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