xref: /freebsd/sys/arm64/vmm/io/vgic.c (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2023 Arm Ltd
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #include <sys/types.h>
30 #include <sys/systm.h>
31 #include <sys/bus.h>
32 
33 #include "vgic.h"
34 #include "vgic_if.h"
35 
36 device_t vgic_dev;
37 
38 bool
39 vgic_present(void)
40 {
41 	return (vgic_dev != NULL);
42 }
43 
44 void
45 vgic_init(void)
46 {
47 	VGIC_INIT(vgic_dev);
48 }
49 
50 int
51 vgic_attach_to_vm(struct hyp *hyp, struct vm_vgic_descr *descr)
52 {
53 	return (VGIC_ATTACH_TO_VM(vgic_dev, hyp, descr));
54 }
55 
56 void
57 vgic_detach_from_vm(struct hyp *hyp)
58 {
59 	VGIC_DETACH_FROM_VM(vgic_dev, hyp);
60 }
61 
62 void
63 vgic_vminit(struct hyp *hyp)
64 {
65 	VGIC_VMINIT(vgic_dev, hyp);
66 }
67 
68 void
69 vgic_cpuinit(struct hypctx *hypctx)
70 {
71 	VGIC_CPUINIT(vgic_dev, hypctx);
72 }
73 
74 void
75 vgic_cpucleanup(struct hypctx *hypctx)
76 {
77 	VGIC_CPUCLEANUP(vgic_dev, hypctx);
78 }
79 
80 void
81 vgic_vmcleanup(struct hyp *hyp)
82 {
83 	VGIC_VMCLEANUP(vgic_dev, hyp);
84 }
85 
86 int
87 vgic_max_cpu_count(struct hyp *hyp)
88 {
89 	return (VGIC_MAX_CPU_COUNT(vgic_dev, hyp));
90 }
91 
92 bool
93 vgic_has_pending_irq(struct hypctx *hypctx)
94 {
95 	return (VGIC_HAS_PENDING_IRQ(vgic_dev, hypctx));
96 }
97 
98 /* TODO: vcpuid -> hypctx ? */
99 /* TODO: Add a vgic interface */
100 int
101 vgic_inject_irq(struct hyp *hyp, int vcpuid, uint32_t irqid, bool level)
102 {
103 	return (VGIC_INJECT_IRQ(vgic_dev, hyp, vcpuid, irqid, level));
104 }
105 
106 int
107 vgic_inject_msi(struct hyp *hyp, uint64_t msg, uint64_t addr)
108 {
109 	return (VGIC_INJECT_MSI(vgic_dev, hyp, msg, addr));
110 }
111 
112 void
113 vgic_flush_hwstate(struct hypctx *hypctx)
114 {
115 	VGIC_FLUSH_HWSTATE(vgic_dev, hypctx);
116 }
117 
118 void
119 vgic_sync_hwstate(struct hypctx *hypctx)
120 {
121 	VGIC_SYNC_HWSTATE(vgic_dev, hypctx);
122 }
123