xref: /freebsd/sys/dev/hyperv/vmbus/aarch64/hyperv_machdep.c (revision a2464ee12761660f50d0b6f59f233949ebcacc87)
1 /*- SPDX-License-Identifier: BSD-2-Clause-FreeBSD
2  *
3  * Copyright (c) 2016-2017,2022 Microsoft Corp.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    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 ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <dev/hyperv/include/hyperv.h>
31 #include <dev/hyperv/include/hyperv_busdma.h>
32 #include <dev/hyperv/vmbus/aarch64/hyperv_machdep.h>
33 #include <dev/hyperv/vmbus/aarch64/hyperv_reg.h>
34 #include <dev/hyperv/vmbus/hyperv_var.h>
35 #include <dev/psci/smccc.h>
36 
37 #define HVCALL_SET_VP_REGISTERS 0x0051
38 #define HVCALL_GET_VP_REGISTERS 0x0050
39 #define BIT(A) (1 << (A))
40 #define HV_HYPERCALL_FAST_BIT BIT(16)
41 #define BIT_ULL(a) (1ULL << (a))
42 #define HV_HYPERCALL_REP_COMP_1 BIT_ULL(32)
43 #define HV_PARTITION_ID_SELF ((u64)-1)
44 #define HV_VP_INDEX_SELF ((u32)-2)
45 #define HV_SMCCC_FUNC_NUMBER 1
46 
47 #define HV_FUNC_ID                                           \
48 	SMCCC_FUNC_ID(SMCCC_YIELDING_CALL, SMCCC_64BIT_CALL, \
49 	    SMCCC_VENDOR_HYP_SERVICE_CALLS, HV_SMCCC_FUNC_NUMBER)
50 
51 void
52 arm_hv_set_vreg(u32 msr, u64 value)
53 {
54 	arm_smccc_hvc(HV_FUNC_ID,
55 	    HVCALL_SET_VP_REGISTERS | HV_HYPERCALL_FAST_BIT |
56 		HV_HYPERCALL_REP_COMP_1,
57 	    HV_PARTITION_ID_SELF, HV_VP_INDEX_SELF, msr, 0, value, 0, NULL);
58 }
59 
60 void
61 hv_get_vpreg_128(u32 msr, struct hv_get_vp_registers_output *result)
62 {
63 	struct arm_smccc_1_2_regs args;
64 	struct arm_smccc_1_2_regs res;
65 
66 	args.a0 = HV_FUNC_ID;
67 	args.a1 = HVCALL_GET_VP_REGISTERS | HV_HYPERCALL_FAST_BIT |
68 	    HV_HYPERCALL_REP_COMP_1;
69 	args.a2 = HV_PARTITION_ID_SELF;
70 	args.a3 = HV_VP_INDEX_SELF;
71 	args.a4 = msr;
72 
73 	/*
74 	 * Use the SMCCC 1.2 interface because the results are in registers
75 	 * beyond X0-X3.
76 	 */
77 	arm_smccc_1_2_hvc(&args, &res);
78 
79 	result->as64.low = res.a6;
80 	result->as64.high = res.a7;
81 }
82 
83 u64
84 arm_hv_get_vreg(u32 msr)
85 {
86 	struct hv_get_vp_registers_output output;
87 
88 	hv_get_vpreg_128(msr, &output);
89 
90 	return (output.as64.low);
91 }
92 
93 uint64_t
94 hypercall_md(volatile void *hc_addr, uint64_t in_val, uint64_t in_paddr,
95     uint64_t out_paddr)
96 {
97 	struct arm_smccc_res res;
98 
99 	arm_smccc_hvc(HV_FUNC_ID, in_val, in_paddr, out_paddr, 0, 0, 0, 0,
100 	    &res);
101 
102 	return (res.a0);
103 }
104