summaryrefstree
path: root/kernel/drivers/virt/haven/hh_ctrl.c
blob: c019bf4112826592bfd24d99f96bbcc4063d535e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2020, The Linux Foundation. All rights reserved.
 */

#define pr_fmt(fmt) "haven: " fmt

#include <linux/arm-smccc.h>
#include <linux/debugfs.h>
#include <linux/module.h>
#include <linux/kobject.h>
#include <linux/of.h>
#include <linux/printk.h>
#include <linux/slab.h>
#include <linux/haven/hcall.h>
#include <linux/haven/hh_errno.h>

#define QC_HYP_SMCCC_UART_DISABLE                                              \
	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_32,              \
			   ARM_SMCCC_OWNER_VENDOR_HYPERVISOR, 0x0)
#define QC_HYP_SMCCC_CALL_UID                                                  \
	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_32,              \
			   ARM_SMCCC_OWNER_VENDOR_HYPERVISOR, 0xff01)
#define QC_HYP_SMCCC_REVISION                                                  \
	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_32,              \
			   ARM_SMCCC_OWNER_VENDOR_HYPERVISOR, 0xff03)

#define QC_HYP_UID0 0x19bd54bd
#define QC_HYP_UID1 0x0b37571b
#define QC_HYP_UID2 0x946f609b
#define QC_HYP_UID3 0x54539de6

#define HH_API_INFO_API_VERSION(x)	(((x) >> 0) & 0x3fff)
#define HH_API_INFO_BIG_ENDIAN(x)	(((x) >> 14) & 1)
#define HH_API_INFO_IS_64BIT(x)		(((x) >> 15) & 1)
#define HH_API_INFO_VARIANT(x)		(((x) >> 56) & 0xff)

#define HH_IDENTIFY_PARTITION_CSPACE(x)	(((x) >> 0) & 1)
#define HH_IDENTIFY_DOORBELL(x)		(((x) >> 1) & 1)
#define HH_IDENTIFY_MSGQUEUE(x)		(((x) >> 2) & 1)
#define HH_IDENTIFY_VIC(x)		(((x) >> 3) & 1)
#define HH_IDENTIFY_VPM(x)		(((x) >> 4) & 1)
#define HH_IDENTIFY_VCPU(x)		(((x) >> 5) & 1)
#define HH_IDENTIFY_MEMEXTENT(x)	(((x) >> 6) & 1)
#define HH_IDENTIFY_TRACE_CTRL(x)	(((x) >> 7) & 1)
#define HH_IDENTIFY_ROOTVM_CHANNEL(x)	(((x) >> 16) & 1)
#define HH_IDENTIFY_SCHEDULER(x)	(((x) >> 28) & 0xf)

static bool qc_hyp_calls;
static struct hh_hcall_hyp_identify_resp haven_api;
static bool disable_uart;

#if !defined(CONFIG_HH_DISABLE_UART)
static int __init setup_nohyp_uart(char *arg)
{
	disable_uart = true;
	return 0;
}
early_param("nohyp_uart", setup_nohyp_uart);
#endif

static void hh_disable_uart(void)
{
#if defined(CONFIG_HH_DISABLE_UART)
	disable_uart = true;
#endif
	if (disable_uart) {
		pr_info("Haven: disabling HYP UART\n");
		arm_smccc_1_1_smc(QC_HYP_SMCCC_UART_DISABLE, NULL);
	}
}

static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr,
			 char *buffer)
{
	return scnprintf(buffer, PAGE_SIZE, "haven\n");
}
static struct kobj_attribute type_attr = __ATTR_RO(type);

static ssize_t api_show(struct kobject *kobj, struct kobj_attribute *attr,
			char *buffer)
{
	return scnprintf(buffer, PAGE_SIZE, "%d\n",
		(int)HH_API_INFO_API_VERSION(haven_api.api_info));
}
static struct kobj_attribute api_attr = __ATTR_RO(api);

static ssize_t variant_show(struct kobject *kobj, struct kobj_attribute *attr,
			    char *buffer)
{
	return scnprintf(buffer, PAGE_SIZE, "%d\n",
		(int)HH_API_INFO_VARIANT(haven_api.api_info));
}
static struct kobj_attribute variant_attr = __ATTR_RO(variant);

static struct attribute *version_attrs[] = { &api_attr.attr,
					     &variant_attr.attr, NULL };

static const struct attribute_group version_group = {
	.name = "version",
	.attrs = version_attrs,
};

static int __init hh_sysfs_register(void)
{
	int ret;

	ret = sysfs_create_file(hypervisor_kobj, &type_attr.attr);
	if (ret)
		return ret;

	return sysfs_create_group(hypervisor_kobj, &version_group);
}

static void __exit hh_sysfs_unregister(void)
{
	sysfs_remove_file(hypervisor_kobj, &type_attr.attr);
	sysfs_remove_group(hypervisor_kobj, &version_group);
}

#if defined(CONFIG_DEBUG_FS)
static struct dentry *hh_dbgfs_dir;

static int hh_dbgfs_trace_class_set(void *data, u64 val)
{
	return hh_remap_error(hh_hcall_trace_update_class_flags(val, 0, NULL));
}

static int hh_dbgfs_trace_class_clear(void *data, u64 val)
{
	return hh_remap_error(hh_hcall_trace_update_class_flags(0, val, NULL));
}

static int hh_dbgfs_trace_class_get(void *data, u64 *val)
{
	*val = 0;
	return hh_remap_error(hh_hcall_trace_update_class_flags(0, 0, val));
}

DEFINE_DEBUGFS_ATTRIBUTE(hh_dbgfs_trace_class_set_fops,
			 hh_dbgfs_trace_class_get,
			 hh_dbgfs_trace_class_set,
			 "0x%llx\n");

DEFINE_DEBUGFS_ATTRIBUTE(hh_dbgfs_trace_class_clear_fops,
			 hh_dbgfs_trace_class_get,
			 hh_dbgfs_trace_class_clear,
			 "0x%llx\n");

static int __init hh_dbgfs_register(void)
{
	struct dentry *dentry;

	hh_dbgfs_dir = debugfs_create_dir("haven", NULL);
	if (IS_ERR_OR_NULL(hh_dbgfs_dir))
		return PTR_ERR(hh_dbgfs_dir);

	if (HH_IDENTIFY_TRACE_CTRL(haven_api.flags[0])) {
		dentry = debugfs_create_file("trace_set", 0600, hh_dbgfs_dir,
					NULL, &hh_dbgfs_trace_class_set_fops);
		if (IS_ERR(dentry))
			return PTR_ERR(dentry);

		dentry = debugfs_create_file("trace_clear", 0600, hh_dbgfs_dir,
					NULL, &hh_dbgfs_trace_class_clear_fops);
		if (IS_ERR(dentry))
			return PTR_ERR(dentry);
	}

	return 0;
}

static void __exit hh_dbgfs_unregister(void)
{
	debugfs_remove_recursive(hh_dbgfs_dir);
}
#else /* !defined (CONFIG_DEBUG_FS) */
static inline int hh_dbgfs_register(void) { return 0; }
static inline void hh_dbgfs_unregister(void) { return; }
#endif

static int __init hh_ctrl_init(void)
{
	int ret;
	struct device_node *hyp;
	struct arm_smccc_res res;

	hyp = of_find_node_by_path("/hypervisor");

	if (!hyp || !of_device_is_compatible(hyp, "qcom,haven-hypervisor"))
		return -ENODEV;

	(void)hh_hcall_hyp_identify(&haven_api);

	if (HH_API_INFO_API_VERSION(haven_api.api_info) != 1) {
		pr_err("unknown version\n");
		return -ENODEV;
	}

	/* Check for ARM SMCCC VENDOR_HYP service calls by UID. */
	arm_smccc_1_1_smc(QC_HYP_SMCCC_CALL_UID, &res);
	if ((res.a0 == QC_HYP_UID0) && (res.a1 == QC_HYP_UID1) &&
	    (res.a2 == QC_HYP_UID2) && (res.a3 == QC_HYP_UID3))
		qc_hyp_calls = true;

	if (qc_hyp_calls) {
		hh_disable_uart();
		ret = hh_sysfs_register();
		if (ret)
			return ret;

		ret = hh_dbgfs_register();
		if (ret)
			pr_warn("failed to register dbgfs: %d\n", ret);
	} else {
		pr_info("Haven: no QC HYP interface detected\n");
	}

	return 0;
}
module_init(hh_ctrl_init);

static void __exit hh_ctrl_exit(void)
{
	hh_sysfs_unregister();
	hh_dbgfs_unregister();
}
module_exit(hh_ctrl_exit);

MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Haven Hypervisor Control Driver");