2018-08-15 13:21:11 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*/
|
|
|
|
|
2019-02-25 16:44:07 +08:00
|
|
|
#include <vm.h>
|
|
|
|
#include <io.h>
|
2018-08-15 13:21:11 +08:00
|
|
|
|
|
|
|
#define CMOS_ADDR_PORT 0x70U
|
|
|
|
#define CMOS_DATA_PORT 0x71U
|
|
|
|
|
|
|
|
#define RTC_STATUSA 0x0AU /* status register A */
|
|
|
|
#define RTCSA_TUP 0x80U /* time update, don't look now */
|
|
|
|
|
|
|
|
static spinlock_t cmos_lock = { .head = 0U, .tail = 0U };
|
|
|
|
|
|
|
|
static uint8_t cmos_read(uint8_t addr)
|
|
|
|
{
|
|
|
|
pio_write8(addr, CMOS_ADDR_PORT);
|
|
|
|
return pio_read8(CMOS_DATA_PORT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool cmos_update_in_progress(void)
|
|
|
|
{
|
|
|
|
return (cmos_read(RTC_STATUSA) & RTCSA_TUP)?1:0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t cmos_get_reg_val(uint8_t addr)
|
|
|
|
{
|
|
|
|
uint8_t reg;
|
2019-07-09 15:40:13 +08:00
|
|
|
int32_t tries = 2000;
|
2018-08-15 13:21:11 +08:00
|
|
|
|
|
|
|
spinlock_obtain(&cmos_lock);
|
|
|
|
|
|
|
|
/* Make sure an update isn't in progress */
|
2019-06-25 14:45:39 +08:00
|
|
|
while (cmos_update_in_progress() && (tries != 0)) {
|
|
|
|
tries -= 1;
|
|
|
|
}
|
2018-08-15 13:21:11 +08:00
|
|
|
|
|
|
|
reg = cmos_read(addr);
|
|
|
|
|
|
|
|
spinlock_release(&cmos_lock);
|
|
|
|
return reg;
|
|
|
|
}
|
|
|
|
|
2019-08-09 11:21:16 +08:00
|
|
|
/**
|
|
|
|
* @pre vcpu != NULL
|
|
|
|
* @pre vcpu->vm != NULL
|
|
|
|
*/
|
|
|
|
static bool vrtc_read(struct acrn_vcpu *vcpu, uint16_t addr, __unused size_t width)
|
2018-08-15 13:21:11 +08:00
|
|
|
{
|
|
|
|
uint8_t offset;
|
2019-03-29 00:23:30 +08:00
|
|
|
struct pio_request *pio_req = &vcpu->req.reqs.pio;
|
2019-08-09 11:21:16 +08:00
|
|
|
struct acrn_vm *vm = vcpu->vm;
|
2018-08-15 13:21:11 +08:00
|
|
|
|
|
|
|
offset = vm->vrtc_offset;
|
|
|
|
|
|
|
|
if (addr == CMOS_ADDR_PORT) {
|
2019-03-29 00:23:30 +08:00
|
|
|
pio_req->value = vm->vrtc_offset;
|
|
|
|
} else {
|
|
|
|
pio_req->value = cmos_get_reg_val(offset);
|
2018-08-15 13:21:11 +08:00
|
|
|
}
|
|
|
|
|
2019-03-29 00:23:30 +08:00
|
|
|
return true;
|
2018-08-15 13:21:11 +08:00
|
|
|
}
|
|
|
|
|
2019-08-09 11:21:16 +08:00
|
|
|
/**
|
|
|
|
* @pre vcpu != NULL
|
|
|
|
* @pre vcpu->vm != NULL
|
|
|
|
*/
|
|
|
|
static bool vrtc_write(struct acrn_vcpu *vcpu, uint16_t addr, size_t width,
|
2018-09-25 15:05:15 +08:00
|
|
|
uint32_t value)
|
2018-08-15 13:21:11 +08:00
|
|
|
{
|
2019-03-28 22:36:50 +08:00
|
|
|
if ((width == 1U) && (addr == CMOS_ADDR_PORT)) {
|
2019-08-09 11:21:16 +08:00
|
|
|
vcpu->vm->vrtc_offset = (uint8_t)value & 0x7FU;
|
2018-08-15 13:21:11 +08:00
|
|
|
}
|
2019-03-28 22:36:50 +08:00
|
|
|
|
|
|
|
return true;
|
2018-08-15 13:21:11 +08:00
|
|
|
}
|
|
|
|
|
2018-11-05 13:28:23 +08:00
|
|
|
void vrtc_init(struct acrn_vm *vm)
|
2018-08-15 13:21:11 +08:00
|
|
|
{
|
|
|
|
struct vm_io_range range = {
|
2019-08-16 14:29:55 +08:00
|
|
|
.base = CMOS_ADDR_PORT, .len = 2U};
|
2018-08-15 13:21:11 +08:00
|
|
|
|
|
|
|
/* Initializing the CMOS RAM offset to 0U */
|
|
|
|
vm->vrtc_offset = 0U;
|
|
|
|
|
2019-01-08 08:47:51 +08:00
|
|
|
register_pio_emulation_handler(vm, RTC_PIO_IDX, &range, vrtc_read, vrtc_write);
|
2018-08-15 13:21:11 +08:00
|
|
|
}
|