xref: /freebsd/sys/arm64/vmm/io/vtimer.h (revision 47e07394)
147e07394SAndrew Turner /*-
247e07394SAndrew Turner  * SPDX-License-Identifier: BSD-2-Clause
347e07394SAndrew Turner  *
447e07394SAndrew Turner  * Copyright (c) 2017 The FreeBSD Foundation
547e07394SAndrew Turner  *
647e07394SAndrew Turner  * Redistribution and use in source and binary forms, with or without
747e07394SAndrew Turner  * modification, are permitted provided that the following conditions
847e07394SAndrew Turner  * are met:
947e07394SAndrew Turner  * 1. Redistributions of source code must retain the above copyright
1047e07394SAndrew Turner  *    notice, this list of conditions and the following disclaimer.
1147e07394SAndrew Turner  * 2. Redistributions in binary form must reproduce the above copyright
1247e07394SAndrew Turner  *    notice, this list of conditions and the following disclaimer in the
1347e07394SAndrew Turner  *    documentation and/or other materials provided with the distribution.
1447e07394SAndrew Turner  * 3. The name of the company nor the name of the author may be used to
1547e07394SAndrew Turner  *    endorse or promote products derived from this software without specific
1647e07394SAndrew Turner  *    prior written permission.
1747e07394SAndrew Turner  *
1847e07394SAndrew Turner  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1947e07394SAndrew Turner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2047e07394SAndrew Turner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2147e07394SAndrew Turner  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2247e07394SAndrew Turner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2347e07394SAndrew Turner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2447e07394SAndrew Turner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2547e07394SAndrew Turner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2647e07394SAndrew Turner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2747e07394SAndrew Turner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2847e07394SAndrew Turner  * SUCH DAMAGE.
2947e07394SAndrew Turner  */
3047e07394SAndrew Turner 
3147e07394SAndrew Turner #ifndef _VMM_VTIMER_H_
3247e07394SAndrew Turner #define _VMM_VTIMER_H_
3347e07394SAndrew Turner 
3447e07394SAndrew Turner #define	GT_PHYS_NS_IRQ	30
3547e07394SAndrew Turner #define	GT_VIRT_IRQ	27
3647e07394SAndrew Turner 
3747e07394SAndrew Turner struct hyp;
3847e07394SAndrew Turner struct hypctx;
3947e07394SAndrew Turner 
4047e07394SAndrew Turner struct vtimer {
4147e07394SAndrew Turner 	uint64_t	cnthctl_el2;
4247e07394SAndrew Turner 	uint64_t	cntvoff_el2;
4347e07394SAndrew Turner };
4447e07394SAndrew Turner 
4547e07394SAndrew Turner struct vtimer_timer {
4647e07394SAndrew Turner 	struct callout	callout;
4747e07394SAndrew Turner 	struct mtx	mtx;
4847e07394SAndrew Turner 
4947e07394SAndrew Turner 	uint32_t	irqid;
5047e07394SAndrew Turner 
5147e07394SAndrew Turner 	/*
5247e07394SAndrew Turner 	 * These registers are either emulated for the physical timer, or
5347e07394SAndrew Turner 	 * the guest has full access to them for the virtual timer.
5447e07394SAndrew Turner 
5547e07394SAndrew Turner 	 * CNTx_CTL_EL0:  Counter-timer Timer Control Register
5647e07394SAndrew Turner 	 * CNTx_CVAL_EL0: Counter-timer Timer CompareValue Register
5747e07394SAndrew Turner 	 */
5847e07394SAndrew Turner 	uint64_t	cntx_cval_el0;
5947e07394SAndrew Turner 	uint64_t	cntx_ctl_el0;
6047e07394SAndrew Turner };
6147e07394SAndrew Turner 
6247e07394SAndrew Turner struct vtimer_cpu {
6347e07394SAndrew Turner 	struct vtimer_timer phys_timer;
6447e07394SAndrew Turner 	struct vtimer_timer virt_timer;
6547e07394SAndrew Turner 
6647e07394SAndrew Turner 	uint32_t	cntkctl_el1;
6747e07394SAndrew Turner };
6847e07394SAndrew Turner 
6947e07394SAndrew Turner int 	vtimer_init(uint64_t cnthctl_el2);
7047e07394SAndrew Turner void 	vtimer_vminit(struct hyp *);
7147e07394SAndrew Turner void 	vtimer_cpuinit(struct hypctx *);
7247e07394SAndrew Turner void 	vtimer_cpucleanup(struct hypctx *);
7347e07394SAndrew Turner void	vtimer_vmcleanup(struct hyp *);
7447e07394SAndrew Turner void	vtimer_cleanup(void);
7547e07394SAndrew Turner void	vtimer_sync_hwstate(struct hypctx *hypctx);
7647e07394SAndrew Turner 
7747e07394SAndrew Turner int 	vtimer_phys_ctl_read(struct vcpu *vcpu, uint64_t *rval, void *arg);
7847e07394SAndrew Turner int 	vtimer_phys_ctl_write(struct vcpu *vcpu, uint64_t wval, void *arg);
7947e07394SAndrew Turner int 	vtimer_phys_cnt_read(struct vcpu *vcpu, uint64_t *rval, void *arg);
8047e07394SAndrew Turner int 	vtimer_phys_cnt_write(struct vcpu *vcpu, uint64_t wval, void *arg);
8147e07394SAndrew Turner int 	vtimer_phys_cval_read(struct vcpu *vcpu, uint64_t *rval, void *arg);
8247e07394SAndrew Turner int 	vtimer_phys_cval_write(struct vcpu *vcpu, uint64_t wval, void *arg);
8347e07394SAndrew Turner int 	vtimer_phys_tval_read(struct vcpu *vcpu, uint64_t *rval, void *arg);
8447e07394SAndrew Turner int 	vtimer_phys_tval_write(struct vcpu *vcpu, uint64_t wval, void *arg);
8547e07394SAndrew Turner #endif
86