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