xref: /freebsd/sys/dev/hyperv/vmbus/aarch64/hyperv_machdep.c (revision 1719886f6d08408b834d270c59ffcfd821c8f63a)
1 /*- SPDX-License-Identifier: BSD-2-Clause
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 #include <dev/hyperv/include/hyperv.h>
29 #include <dev/hyperv/include/hyperv_busdma.h>
30 #include <dev/hyperv/vmbus/aarch64/hyperv_machdep.h>
31 #include <dev/hyperv/vmbus/aarch64/hyperv_reg.h>
32 #include <dev/hyperv/vmbus/hyperv_var.h>
33 #include <dev/hyperv/vmbus/hyperv_common_reg.h>
34 #include <dev/psci/smccc.h>
35 
36 #define HVCALL_SET_VP_REGISTERS 0x0051
37 #define HVCALL_GET_VP_REGISTERS 0x0050
38 #define BIT(A) (1 << (A))
39 #define HV_HYPERCALL_FAST_BIT BIT(16)
40 #define BIT_ULL(a) (1ULL << (a))
41 #define HV_HYPERCALL_REP_COMP_1 BIT_ULL(32)
42 #define HV_PARTITION_ID_SELF ((u64)-1)
43 #define HV_VP_INDEX_SELF ((u32)-2)
44 #define HV_SMCCC_FUNC_NUMBER 1
45 
46 #define HV_FUNC_ID                                           \
47 	SMCCC_FUNC_ID(SMCCC_YIELDING_CALL, SMCCC_64BIT_CALL, \
48 	    SMCCC_VENDOR_HYP_SERVICE_CALLS, HV_SMCCC_FUNC_NUMBER)
49 
50 void
51 arm_hv_set_vreg(u32 msr, u64 value)
52 {
53 	arm_smccc_hvc(HV_FUNC_ID,
54 	    HVCALL_SET_VP_REGISTERS | HV_HYPERCALL_FAST_BIT |
55 		HV_HYPERCALL_REP_COMP_1,
56 	    HV_PARTITION_ID_SELF, HV_VP_INDEX_SELF, msr, 0, value, 0, NULL);
57 }
58 
59 void
60 hv_get_vpreg_128(u32 msr, struct hv_get_vp_registers_output *result)
61 {
62 	struct arm_smccc_1_2_regs args;
63 	struct arm_smccc_1_2_regs res;
64 
65 	args.a0 = HV_FUNC_ID;
66 	args.a1 = HVCALL_GET_VP_REGISTERS | HV_HYPERCALL_FAST_BIT |
67 	    HV_HYPERCALL_REP_COMP_1;
68 	args.a2 = HV_PARTITION_ID_SELF;
69 	args.a3 = HV_VP_INDEX_SELF;
70 	args.a4 = msr;
71 
72 	/*
73 	 * Use the SMCCC 1.2 interface because the results are in registers
74 	 * beyond X0-X3.
75 	 */
76 	arm_smccc_1_2_hvc(&args, &res);
77 
78 	result->as64.low = res.a6;
79 	result->as64.high = res.a7;
80 }
81 
82 u64
83 arm_hv_get_vreg(u32 msr)
84 {
85 	struct hv_get_vp_registers_output output;
86 
87 	hv_get_vpreg_128(msr, &output);
88 
89 	return (output.as64.low);
90 }
91 
92 uint64_t
93 hypercall_md(volatile void *hc_addr, uint64_t in_val, uint64_t in_paddr,
94     uint64_t out_paddr)
95 {
96 	struct arm_smccc_res res;
97 
98 	arm_smccc_hvc(HV_FUNC_ID, in_val, in_paddr, out_paddr, 0, 0, 0, 0,
99 	    &res);
100 
101 	return (res.a0);
102 }
103