1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2024 Ruslan Bukin <br@bsdpad.com>
5 *
6 * This software was developed by the University of Cambridge Computer
7 * Laboratory (Department of Computer Science and Technology) under Innovate
8 * UK project 105694, "Digital Security by Design (DSbD) Technology Platform
9 * Prototype".
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37
38 #include <dev/ofw/ofw_bus.h>
39 #include <dev/ofw/ofw_bus_subr.h>
40 #include <dev/ofw/openfirm.h>
41
42 #include "riscv.h"
43
44 #define VTIMER_DEFAULT_FREQ 1000000
45
46 static int
vtimer_get_timebase(uint32_t * freq)47 vtimer_get_timebase(uint32_t *freq)
48 {
49 phandle_t node;
50 int len;
51
52 node = OF_finddevice("/cpus");
53 if (node == -1)
54 return (ENXIO);
55
56 len = OF_getproplen(node, "timebase-frequency");
57 if (len != 4)
58 return (ENXIO);
59
60 OF_getencprop(node, "timebase-frequency", freq, len);
61
62 return (0);
63 }
64
65 void
vtimer_cpuinit(struct hypctx * hypctx)66 vtimer_cpuinit(struct hypctx *hypctx)
67 {
68 struct vtimer *vtimer;
69 uint32_t freq;
70 int error;
71
72 vtimer = &hypctx->vtimer;
73 mtx_init(&vtimer->mtx, "vtimer callout mutex", NULL, MTX_DEF);
74 callout_init_mtx(&vtimer->callout, &vtimer->mtx, 0);
75
76 error = vtimer_get_timebase(&freq);
77 if (error)
78 freq = VTIMER_DEFAULT_FREQ;
79
80 vtimer->freq = freq;
81 }
82
83 static void
vtimer_inject_irq_callout(void * arg)84 vtimer_inject_irq_callout(void *arg)
85 {
86 struct hypctx *hypctx;
87 struct hyp *hyp;
88
89 hypctx = arg;
90 hyp = hypctx->hyp;
91
92 atomic_set_32(&hypctx->interrupts_pending, HVIP_VSTIP);
93 vcpu_notify_event(vm_vcpu(hyp->vm, hypctx->cpu_id));
94 }
95
96 int
vtimer_set_timer(struct hypctx * hypctx,uint64_t next_val)97 vtimer_set_timer(struct hypctx *hypctx, uint64_t next_val)
98 {
99 struct vtimer *vtimer;
100 sbintime_t time;
101 uint64_t curtime;
102 uint64_t delta;
103
104 vtimer = &hypctx->vtimer;
105
106 curtime = rdtime();
107 if (curtime < next_val) {
108 delta = next_val - curtime;
109 time = delta * SBT_1S / vtimer->freq;
110 atomic_clear_32(&hypctx->interrupts_pending, HVIP_VSTIP);
111 callout_reset_sbt(&vtimer->callout, time, 0,
112 vtimer_inject_irq_callout, hypctx, 0);
113 } else
114 atomic_set_32(&hypctx->interrupts_pending, HVIP_VSTIP);
115
116 return (0);
117 }
118