xref: /openbmc/linux/drivers/xen/sys-hypervisor.c (revision 982e4430beb94e65c482d36a11dbb779f20c38a7)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2cff7e81bSJeremy Fitzhardinge /*
3cff7e81bSJeremy Fitzhardinge  *  copyright (c) 2006 IBM Corporation
4cff7e81bSJeremy Fitzhardinge  *  Authored by: Mike D. Day <ncmike@us.ibm.com>
5cff7e81bSJeremy Fitzhardinge  */
6cff7e81bSJeremy Fitzhardinge 
75a0e3ad6STejun Heo #include <linux/slab.h>
8cff7e81bSJeremy Fitzhardinge #include <linux/kernel.h>
946894f17SPaul Gortmaker #include <linux/init.h>
10cff7e81bSJeremy Fitzhardinge #include <linux/kobject.h>
11609b0b8cSIan Campbell #include <linux/err.h>
12cff7e81bSJeremy Fitzhardinge 
13cff7e81bSJeremy Fitzhardinge #include <asm/xen/hypervisor.h>
14cff7e81bSJeremy Fitzhardinge #include <asm/xen/hypercall.h>
15cff7e81bSJeremy Fitzhardinge 
161ccbf534SJeremy Fitzhardinge #include <xen/xen.h>
17cff7e81bSJeremy Fitzhardinge #include <xen/xenbus.h>
18cff7e81bSJeremy Fitzhardinge #include <xen/interface/xen.h>
19cff7e81bSJeremy Fitzhardinge #include <xen/interface/version.h>
205f141548SBoris Ostrovsky #ifdef CONFIG_XEN_HAVE_VPMU
215f141548SBoris Ostrovsky #include <xen/interface/xenpmu.h>
225f141548SBoris Ostrovsky #endif
23cff7e81bSJeremy Fitzhardinge 
24cff7e81bSJeremy Fitzhardinge #define HYPERVISOR_ATTR_RO(_name) \
25cff7e81bSJeremy Fitzhardinge static struct hyp_sysfs_attr _name##_attr = __ATTR_RO(_name)
26cff7e81bSJeremy Fitzhardinge 
27cff7e81bSJeremy Fitzhardinge #define HYPERVISOR_ATTR_RW(_name) \
28*982e4430Szhanglianjie static struct hyp_sysfs_attr _name##_attr = __ATTR_RW(_name)
29cff7e81bSJeremy Fitzhardinge 
30cff7e81bSJeremy Fitzhardinge struct hyp_sysfs_attr {
31cff7e81bSJeremy Fitzhardinge 	struct attribute attr;
32cff7e81bSJeremy Fitzhardinge 	ssize_t (*show)(struct hyp_sysfs_attr *, char *);
33cff7e81bSJeremy Fitzhardinge 	ssize_t (*store)(struct hyp_sysfs_attr *, const char *, size_t);
34cff7e81bSJeremy Fitzhardinge 	void *hyp_attr_data;
35cff7e81bSJeremy Fitzhardinge };
36cff7e81bSJeremy Fitzhardinge 
37cff7e81bSJeremy Fitzhardinge static ssize_t type_show(struct hyp_sysfs_attr *attr, char *buffer)
38cff7e81bSJeremy Fitzhardinge {
39cff7e81bSJeremy Fitzhardinge 	return sprintf(buffer, "xen\n");
40cff7e81bSJeremy Fitzhardinge }
41cff7e81bSJeremy Fitzhardinge 
42cff7e81bSJeremy Fitzhardinge HYPERVISOR_ATTR_RO(type);
43cff7e81bSJeremy Fitzhardinge 
44cff7e81bSJeremy Fitzhardinge static int __init xen_sysfs_type_init(void)
45cff7e81bSJeremy Fitzhardinge {
46cff7e81bSJeremy Fitzhardinge 	return sysfs_create_file(hypervisor_kobj, &type_attr.attr);
47cff7e81bSJeremy Fitzhardinge }
48cff7e81bSJeremy Fitzhardinge 
494a4c29c9SJuergen Gross static ssize_t guest_type_show(struct hyp_sysfs_attr *attr, char *buffer)
504a4c29c9SJuergen Gross {
514a4c29c9SJuergen Gross 	const char *type;
524a4c29c9SJuergen Gross 
534a4c29c9SJuergen Gross 	switch (xen_domain_type) {
544a4c29c9SJuergen Gross 	case XEN_NATIVE:
554a4c29c9SJuergen Gross 		/* ARM only. */
564a4c29c9SJuergen Gross 		type = "Xen";
574a4c29c9SJuergen Gross 		break;
584a4c29c9SJuergen Gross 	case XEN_PV_DOMAIN:
594a4c29c9SJuergen Gross 		type = "PV";
604a4c29c9SJuergen Gross 		break;
614a4c29c9SJuergen Gross 	case XEN_HVM_DOMAIN:
624a4c29c9SJuergen Gross 		type = xen_pvh_domain() ? "PVH" : "HVM";
634a4c29c9SJuergen Gross 		break;
644a4c29c9SJuergen Gross 	default:
654a4c29c9SJuergen Gross 		return -EINVAL;
664a4c29c9SJuergen Gross 	}
674a4c29c9SJuergen Gross 
684a4c29c9SJuergen Gross 	return sprintf(buffer, "%s\n", type);
694a4c29c9SJuergen Gross }
704a4c29c9SJuergen Gross 
714a4c29c9SJuergen Gross HYPERVISOR_ATTR_RO(guest_type);
724a4c29c9SJuergen Gross 
734a4c29c9SJuergen Gross static int __init xen_sysfs_guest_type_init(void)
744a4c29c9SJuergen Gross {
754a4c29c9SJuergen Gross 	return sysfs_create_file(hypervisor_kobj, &guest_type_attr.attr);
764a4c29c9SJuergen Gross }
774a4c29c9SJuergen Gross 
78cff7e81bSJeremy Fitzhardinge /* xen version attributes */
79cff7e81bSJeremy Fitzhardinge static ssize_t major_show(struct hyp_sysfs_attr *attr, char *buffer)
80cff7e81bSJeremy Fitzhardinge {
81cff7e81bSJeremy Fitzhardinge 	int version = HYPERVISOR_xen_version(XENVER_version, NULL);
82cff7e81bSJeremy Fitzhardinge 	if (version)
83cff7e81bSJeremy Fitzhardinge 		return sprintf(buffer, "%d\n", version >> 16);
84cff7e81bSJeremy Fitzhardinge 	return -ENODEV;
85cff7e81bSJeremy Fitzhardinge }
86cff7e81bSJeremy Fitzhardinge 
87cff7e81bSJeremy Fitzhardinge HYPERVISOR_ATTR_RO(major);
88cff7e81bSJeremy Fitzhardinge 
89cff7e81bSJeremy Fitzhardinge static ssize_t minor_show(struct hyp_sysfs_attr *attr, char *buffer)
90cff7e81bSJeremy Fitzhardinge {
91cff7e81bSJeremy Fitzhardinge 	int version = HYPERVISOR_xen_version(XENVER_version, NULL);
92cff7e81bSJeremy Fitzhardinge 	if (version)
93cff7e81bSJeremy Fitzhardinge 		return sprintf(buffer, "%d\n", version & 0xff);
94cff7e81bSJeremy Fitzhardinge 	return -ENODEV;
95cff7e81bSJeremy Fitzhardinge }
96cff7e81bSJeremy Fitzhardinge 
97cff7e81bSJeremy Fitzhardinge HYPERVISOR_ATTR_RO(minor);
98cff7e81bSJeremy Fitzhardinge 
99cff7e81bSJeremy Fitzhardinge static ssize_t extra_show(struct hyp_sysfs_attr *attr, char *buffer)
100cff7e81bSJeremy Fitzhardinge {
101cff7e81bSJeremy Fitzhardinge 	int ret = -ENOMEM;
102cff7e81bSJeremy Fitzhardinge 	char *extra;
103cff7e81bSJeremy Fitzhardinge 
104cff7e81bSJeremy Fitzhardinge 	extra = kmalloc(XEN_EXTRAVERSION_LEN, GFP_KERNEL);
105cff7e81bSJeremy Fitzhardinge 	if (extra) {
106cff7e81bSJeremy Fitzhardinge 		ret = HYPERVISOR_xen_version(XENVER_extraversion, extra);
107cff7e81bSJeremy Fitzhardinge 		if (!ret)
108cff7e81bSJeremy Fitzhardinge 			ret = sprintf(buffer, "%s\n", extra);
109cff7e81bSJeremy Fitzhardinge 		kfree(extra);
110cff7e81bSJeremy Fitzhardinge 	}
111cff7e81bSJeremy Fitzhardinge 
112cff7e81bSJeremy Fitzhardinge 	return ret;
113cff7e81bSJeremy Fitzhardinge }
114cff7e81bSJeremy Fitzhardinge 
115cff7e81bSJeremy Fitzhardinge HYPERVISOR_ATTR_RO(extra);
116cff7e81bSJeremy Fitzhardinge 
117cff7e81bSJeremy Fitzhardinge static struct attribute *version_attrs[] = {
118cff7e81bSJeremy Fitzhardinge 	&major_attr.attr,
119cff7e81bSJeremy Fitzhardinge 	&minor_attr.attr,
120cff7e81bSJeremy Fitzhardinge 	&extra_attr.attr,
121cff7e81bSJeremy Fitzhardinge 	NULL
122cff7e81bSJeremy Fitzhardinge };
123cff7e81bSJeremy Fitzhardinge 
124ead1d014SJan Beulich static const struct attribute_group version_group = {
125cff7e81bSJeremy Fitzhardinge 	.name = "version",
126cff7e81bSJeremy Fitzhardinge 	.attrs = version_attrs,
127cff7e81bSJeremy Fitzhardinge };
128cff7e81bSJeremy Fitzhardinge 
129cff7e81bSJeremy Fitzhardinge static int __init xen_sysfs_version_init(void)
130cff7e81bSJeremy Fitzhardinge {
131cff7e81bSJeremy Fitzhardinge 	return sysfs_create_group(hypervisor_kobj, &version_group);
132cff7e81bSJeremy Fitzhardinge }
133cff7e81bSJeremy Fitzhardinge 
134cff7e81bSJeremy Fitzhardinge /* UUID */
135cff7e81bSJeremy Fitzhardinge 
1365c13f806SDaniel De Graaf static ssize_t uuid_show_fallback(struct hyp_sysfs_attr *attr, char *buffer)
137cff7e81bSJeremy Fitzhardinge {
138cff7e81bSJeremy Fitzhardinge 	char *vm, *val;
139cff7e81bSJeremy Fitzhardinge 	int ret;
140cff7e81bSJeremy Fitzhardinge 	extern int xenstored_ready;
141cff7e81bSJeremy Fitzhardinge 
142cff7e81bSJeremy Fitzhardinge 	if (!xenstored_ready)
143cff7e81bSJeremy Fitzhardinge 		return -EBUSY;
144cff7e81bSJeremy Fitzhardinge 
145cff7e81bSJeremy Fitzhardinge 	vm = xenbus_read(XBT_NIL, "vm", "", NULL);
146cff7e81bSJeremy Fitzhardinge 	if (IS_ERR(vm))
147cff7e81bSJeremy Fitzhardinge 		return PTR_ERR(vm);
148cff7e81bSJeremy Fitzhardinge 	val = xenbus_read(XBT_NIL, vm, "uuid", NULL);
149cff7e81bSJeremy Fitzhardinge 	kfree(vm);
150cff7e81bSJeremy Fitzhardinge 	if (IS_ERR(val))
151cff7e81bSJeremy Fitzhardinge 		return PTR_ERR(val);
152cff7e81bSJeremy Fitzhardinge 	ret = sprintf(buffer, "%s\n", val);
153cff7e81bSJeremy Fitzhardinge 	kfree(val);
154cff7e81bSJeremy Fitzhardinge 	return ret;
155cff7e81bSJeremy Fitzhardinge }
156cff7e81bSJeremy Fitzhardinge 
1575c13f806SDaniel De Graaf static ssize_t uuid_show(struct hyp_sysfs_attr *attr, char *buffer)
1585c13f806SDaniel De Graaf {
1595c13f806SDaniel De Graaf 	xen_domain_handle_t uuid;
1605c13f806SDaniel De Graaf 	int ret;
1615c13f806SDaniel De Graaf 	ret = HYPERVISOR_xen_version(XENVER_guest_handle, uuid);
1625c13f806SDaniel De Graaf 	if (ret)
1635c13f806SDaniel De Graaf 		return uuid_show_fallback(attr, buffer);
1645c13f806SDaniel De Graaf 	ret = sprintf(buffer, "%pU\n", uuid);
1655c13f806SDaniel De Graaf 	return ret;
1665c13f806SDaniel De Graaf }
1675c13f806SDaniel De Graaf 
168cff7e81bSJeremy Fitzhardinge HYPERVISOR_ATTR_RO(uuid);
169cff7e81bSJeremy Fitzhardinge 
170cff7e81bSJeremy Fitzhardinge static int __init xen_sysfs_uuid_init(void)
171cff7e81bSJeremy Fitzhardinge {
172cff7e81bSJeremy Fitzhardinge 	return sysfs_create_file(hypervisor_kobj, &uuid_attr.attr);
173cff7e81bSJeremy Fitzhardinge }
174cff7e81bSJeremy Fitzhardinge 
175cff7e81bSJeremy Fitzhardinge /* xen compilation attributes */
176cff7e81bSJeremy Fitzhardinge 
177cff7e81bSJeremy Fitzhardinge static ssize_t compiler_show(struct hyp_sysfs_attr *attr, char *buffer)
178cff7e81bSJeremy Fitzhardinge {
179cff7e81bSJeremy Fitzhardinge 	int ret = -ENOMEM;
180cff7e81bSJeremy Fitzhardinge 	struct xen_compile_info *info;
181cff7e81bSJeremy Fitzhardinge 
182cff7e81bSJeremy Fitzhardinge 	info = kmalloc(sizeof(struct xen_compile_info), GFP_KERNEL);
183cff7e81bSJeremy Fitzhardinge 	if (info) {
184cff7e81bSJeremy Fitzhardinge 		ret = HYPERVISOR_xen_version(XENVER_compile_info, info);
185cff7e81bSJeremy Fitzhardinge 		if (!ret)
186cff7e81bSJeremy Fitzhardinge 			ret = sprintf(buffer, "%s\n", info->compiler);
187cff7e81bSJeremy Fitzhardinge 		kfree(info);
188cff7e81bSJeremy Fitzhardinge 	}
189cff7e81bSJeremy Fitzhardinge 
190cff7e81bSJeremy Fitzhardinge 	return ret;
191cff7e81bSJeremy Fitzhardinge }
192cff7e81bSJeremy Fitzhardinge 
193cff7e81bSJeremy Fitzhardinge HYPERVISOR_ATTR_RO(compiler);
194cff7e81bSJeremy Fitzhardinge 
195cff7e81bSJeremy Fitzhardinge static ssize_t compiled_by_show(struct hyp_sysfs_attr *attr, char *buffer)
196cff7e81bSJeremy Fitzhardinge {
197cff7e81bSJeremy Fitzhardinge 	int ret = -ENOMEM;
198cff7e81bSJeremy Fitzhardinge 	struct xen_compile_info *info;
199cff7e81bSJeremy Fitzhardinge 
200cff7e81bSJeremy Fitzhardinge 	info = kmalloc(sizeof(struct xen_compile_info), GFP_KERNEL);
201cff7e81bSJeremy Fitzhardinge 	if (info) {
202cff7e81bSJeremy Fitzhardinge 		ret = HYPERVISOR_xen_version(XENVER_compile_info, info);
203cff7e81bSJeremy Fitzhardinge 		if (!ret)
204cff7e81bSJeremy Fitzhardinge 			ret = sprintf(buffer, "%s\n", info->compile_by);
205cff7e81bSJeremy Fitzhardinge 		kfree(info);
206cff7e81bSJeremy Fitzhardinge 	}
207cff7e81bSJeremy Fitzhardinge 
208cff7e81bSJeremy Fitzhardinge 	return ret;
209cff7e81bSJeremy Fitzhardinge }
210cff7e81bSJeremy Fitzhardinge 
211cff7e81bSJeremy Fitzhardinge HYPERVISOR_ATTR_RO(compiled_by);
212cff7e81bSJeremy Fitzhardinge 
213cff7e81bSJeremy Fitzhardinge static ssize_t compile_date_show(struct hyp_sysfs_attr *attr, char *buffer)
214cff7e81bSJeremy Fitzhardinge {
215cff7e81bSJeremy Fitzhardinge 	int ret = -ENOMEM;
216cff7e81bSJeremy Fitzhardinge 	struct xen_compile_info *info;
217cff7e81bSJeremy Fitzhardinge 
218cff7e81bSJeremy Fitzhardinge 	info = kmalloc(sizeof(struct xen_compile_info), GFP_KERNEL);
219cff7e81bSJeremy Fitzhardinge 	if (info) {
220cff7e81bSJeremy Fitzhardinge 		ret = HYPERVISOR_xen_version(XENVER_compile_info, info);
221cff7e81bSJeremy Fitzhardinge 		if (!ret)
222cff7e81bSJeremy Fitzhardinge 			ret = sprintf(buffer, "%s\n", info->compile_date);
223cff7e81bSJeremy Fitzhardinge 		kfree(info);
224cff7e81bSJeremy Fitzhardinge 	}
225cff7e81bSJeremy Fitzhardinge 
226cff7e81bSJeremy Fitzhardinge 	return ret;
227cff7e81bSJeremy Fitzhardinge }
228cff7e81bSJeremy Fitzhardinge 
229cff7e81bSJeremy Fitzhardinge HYPERVISOR_ATTR_RO(compile_date);
230cff7e81bSJeremy Fitzhardinge 
231cff7e81bSJeremy Fitzhardinge static struct attribute *xen_compile_attrs[] = {
232cff7e81bSJeremy Fitzhardinge 	&compiler_attr.attr,
233cff7e81bSJeremy Fitzhardinge 	&compiled_by_attr.attr,
234cff7e81bSJeremy Fitzhardinge 	&compile_date_attr.attr,
235cff7e81bSJeremy Fitzhardinge 	NULL
236cff7e81bSJeremy Fitzhardinge };
237cff7e81bSJeremy Fitzhardinge 
238ead1d014SJan Beulich static const struct attribute_group xen_compilation_group = {
239cff7e81bSJeremy Fitzhardinge 	.name = "compilation",
240cff7e81bSJeremy Fitzhardinge 	.attrs = xen_compile_attrs,
241cff7e81bSJeremy Fitzhardinge };
242cff7e81bSJeremy Fitzhardinge 
2435b00b504SJuergen Gross static int __init xen_sysfs_compilation_init(void)
244cff7e81bSJeremy Fitzhardinge {
245cff7e81bSJeremy Fitzhardinge 	return sysfs_create_group(hypervisor_kobj, &xen_compilation_group);
246cff7e81bSJeremy Fitzhardinge }
247cff7e81bSJeremy Fitzhardinge 
248cff7e81bSJeremy Fitzhardinge /* xen properties info */
249cff7e81bSJeremy Fitzhardinge 
250cff7e81bSJeremy Fitzhardinge static ssize_t capabilities_show(struct hyp_sysfs_attr *attr, char *buffer)
251cff7e81bSJeremy Fitzhardinge {
252cff7e81bSJeremy Fitzhardinge 	int ret = -ENOMEM;
253cff7e81bSJeremy Fitzhardinge 	char *caps;
254cff7e81bSJeremy Fitzhardinge 
255cff7e81bSJeremy Fitzhardinge 	caps = kmalloc(XEN_CAPABILITIES_INFO_LEN, GFP_KERNEL);
256cff7e81bSJeremy Fitzhardinge 	if (caps) {
257cff7e81bSJeremy Fitzhardinge 		ret = HYPERVISOR_xen_version(XENVER_capabilities, caps);
258cff7e81bSJeremy Fitzhardinge 		if (!ret)
259cff7e81bSJeremy Fitzhardinge 			ret = sprintf(buffer, "%s\n", caps);
260cff7e81bSJeremy Fitzhardinge 		kfree(caps);
261cff7e81bSJeremy Fitzhardinge 	}
262cff7e81bSJeremy Fitzhardinge 
263cff7e81bSJeremy Fitzhardinge 	return ret;
264cff7e81bSJeremy Fitzhardinge }
265cff7e81bSJeremy Fitzhardinge 
266cff7e81bSJeremy Fitzhardinge HYPERVISOR_ATTR_RO(capabilities);
267cff7e81bSJeremy Fitzhardinge 
268cff7e81bSJeremy Fitzhardinge static ssize_t changeset_show(struct hyp_sysfs_attr *attr, char *buffer)
269cff7e81bSJeremy Fitzhardinge {
270cff7e81bSJeremy Fitzhardinge 	int ret = -ENOMEM;
271cff7e81bSJeremy Fitzhardinge 	char *cset;
272cff7e81bSJeremy Fitzhardinge 
273cff7e81bSJeremy Fitzhardinge 	cset = kmalloc(XEN_CHANGESET_INFO_LEN, GFP_KERNEL);
274cff7e81bSJeremy Fitzhardinge 	if (cset) {
275cff7e81bSJeremy Fitzhardinge 		ret = HYPERVISOR_xen_version(XENVER_changeset, cset);
276cff7e81bSJeremy Fitzhardinge 		if (!ret)
277cff7e81bSJeremy Fitzhardinge 			ret = sprintf(buffer, "%s\n", cset);
278cff7e81bSJeremy Fitzhardinge 		kfree(cset);
279cff7e81bSJeremy Fitzhardinge 	}
280cff7e81bSJeremy Fitzhardinge 
281cff7e81bSJeremy Fitzhardinge 	return ret;
282cff7e81bSJeremy Fitzhardinge }
283cff7e81bSJeremy Fitzhardinge 
284cff7e81bSJeremy Fitzhardinge HYPERVISOR_ATTR_RO(changeset);
285cff7e81bSJeremy Fitzhardinge 
286cff7e81bSJeremy Fitzhardinge static ssize_t virtual_start_show(struct hyp_sysfs_attr *attr, char *buffer)
287cff7e81bSJeremy Fitzhardinge {
288cff7e81bSJeremy Fitzhardinge 	int ret = -ENOMEM;
289cff7e81bSJeremy Fitzhardinge 	struct xen_platform_parameters *parms;
290cff7e81bSJeremy Fitzhardinge 
291cff7e81bSJeremy Fitzhardinge 	parms = kmalloc(sizeof(struct xen_platform_parameters), GFP_KERNEL);
292cff7e81bSJeremy Fitzhardinge 	if (parms) {
293cff7e81bSJeremy Fitzhardinge 		ret = HYPERVISOR_xen_version(XENVER_platform_parameters,
294cff7e81bSJeremy Fitzhardinge 					     parms);
295cff7e81bSJeremy Fitzhardinge 		if (!ret)
29637ea0fcbSIan Campbell 			ret = sprintf(buffer, "%"PRI_xen_ulong"\n",
29737ea0fcbSIan Campbell 				      parms->virt_start);
298cff7e81bSJeremy Fitzhardinge 		kfree(parms);
299cff7e81bSJeremy Fitzhardinge 	}
300cff7e81bSJeremy Fitzhardinge 
301cff7e81bSJeremy Fitzhardinge 	return ret;
302cff7e81bSJeremy Fitzhardinge }
303cff7e81bSJeremy Fitzhardinge 
304cff7e81bSJeremy Fitzhardinge HYPERVISOR_ATTR_RO(virtual_start);
305cff7e81bSJeremy Fitzhardinge 
306cff7e81bSJeremy Fitzhardinge static ssize_t pagesize_show(struct hyp_sysfs_attr *attr, char *buffer)
307cff7e81bSJeremy Fitzhardinge {
308cff7e81bSJeremy Fitzhardinge 	int ret;
309cff7e81bSJeremy Fitzhardinge 
310cff7e81bSJeremy Fitzhardinge 	ret = HYPERVISOR_xen_version(XENVER_pagesize, NULL);
311cff7e81bSJeremy Fitzhardinge 	if (ret > 0)
312cff7e81bSJeremy Fitzhardinge 		ret = sprintf(buffer, "%x\n", ret);
313cff7e81bSJeremy Fitzhardinge 
314cff7e81bSJeremy Fitzhardinge 	return ret;
315cff7e81bSJeremy Fitzhardinge }
316cff7e81bSJeremy Fitzhardinge 
317cff7e81bSJeremy Fitzhardinge HYPERVISOR_ATTR_RO(pagesize);
318cff7e81bSJeremy Fitzhardinge 
319cff7e81bSJeremy Fitzhardinge static ssize_t xen_feature_show(int index, char *buffer)
320cff7e81bSJeremy Fitzhardinge {
321a649b720SJeremy Fitzhardinge 	ssize_t ret;
322a649b720SJeremy Fitzhardinge 	struct xen_feature_info info;
323cff7e81bSJeremy Fitzhardinge 
324a649b720SJeremy Fitzhardinge 	info.submap_idx = index;
325a649b720SJeremy Fitzhardinge 	ret = HYPERVISOR_xen_version(XENVER_get_features, &info);
326cff7e81bSJeremy Fitzhardinge 	if (!ret)
327a649b720SJeremy Fitzhardinge 		ret = sprintf(buffer, "%08x", info.submap);
328cff7e81bSJeremy Fitzhardinge 
329cff7e81bSJeremy Fitzhardinge 	return ret;
330cff7e81bSJeremy Fitzhardinge }
331cff7e81bSJeremy Fitzhardinge 
332a649b720SJeremy Fitzhardinge static ssize_t features_show(struct hyp_sysfs_attr *attr, char *buffer)
333cff7e81bSJeremy Fitzhardinge {
334a649b720SJeremy Fitzhardinge 	ssize_t len;
335a649b720SJeremy Fitzhardinge 	int i;
336a649b720SJeremy Fitzhardinge 
337a649b720SJeremy Fitzhardinge 	len = 0;
338a649b720SJeremy Fitzhardinge 	for (i = XENFEAT_NR_SUBMAPS-1; i >= 0; i--) {
339a649b720SJeremy Fitzhardinge 		int ret = xen_feature_show(i, buffer + len);
340a649b720SJeremy Fitzhardinge 		if (ret < 0) {
341a649b720SJeremy Fitzhardinge 			if (len == 0)
342a649b720SJeremy Fitzhardinge 				len = ret;
343a649b720SJeremy Fitzhardinge 			break;
344a649b720SJeremy Fitzhardinge 		}
345a649b720SJeremy Fitzhardinge 		len += ret;
346a649b720SJeremy Fitzhardinge 	}
347a649b720SJeremy Fitzhardinge 	if (len > 0)
348a649b720SJeremy Fitzhardinge 		buffer[len++] = '\n';
349a649b720SJeremy Fitzhardinge 
350a649b720SJeremy Fitzhardinge 	return len;
351cff7e81bSJeremy Fitzhardinge }
352cff7e81bSJeremy Fitzhardinge 
353a649b720SJeremy Fitzhardinge HYPERVISOR_ATTR_RO(features);
354cff7e81bSJeremy Fitzhardinge 
35584b76257SJuergen Gross static ssize_t buildid_show(struct hyp_sysfs_attr *attr, char *buffer)
35684b76257SJuergen Gross {
35784b76257SJuergen Gross 	ssize_t ret;
35884b76257SJuergen Gross 	struct xen_build_id *buildid;
35984b76257SJuergen Gross 
36084b76257SJuergen Gross 	ret = HYPERVISOR_xen_version(XENVER_build_id, NULL);
36184b76257SJuergen Gross 	if (ret < 0) {
36284b76257SJuergen Gross 		if (ret == -EPERM)
36384b76257SJuergen Gross 			ret = sprintf(buffer, "<denied>");
36484b76257SJuergen Gross 		return ret;
36584b76257SJuergen Gross 	}
36684b76257SJuergen Gross 
36784b76257SJuergen Gross 	buildid = kmalloc(sizeof(*buildid) + ret, GFP_KERNEL);
36884b76257SJuergen Gross 	if (!buildid)
36984b76257SJuergen Gross 		return -ENOMEM;
37084b76257SJuergen Gross 
37184b76257SJuergen Gross 	buildid->len = ret;
37284b76257SJuergen Gross 	ret = HYPERVISOR_xen_version(XENVER_build_id, buildid);
37384b76257SJuergen Gross 	if (ret > 0)
37484b76257SJuergen Gross 		ret = sprintf(buffer, "%s", buildid->buf);
37584b76257SJuergen Gross 	kfree(buildid);
37684b76257SJuergen Gross 
37784b76257SJuergen Gross 	return ret;
37884b76257SJuergen Gross }
37984b76257SJuergen Gross 
38084b76257SJuergen Gross HYPERVISOR_ATTR_RO(buildid);
38184b76257SJuergen Gross 
382cff7e81bSJeremy Fitzhardinge static struct attribute *xen_properties_attrs[] = {
383cff7e81bSJeremy Fitzhardinge 	&capabilities_attr.attr,
384cff7e81bSJeremy Fitzhardinge 	&changeset_attr.attr,
385cff7e81bSJeremy Fitzhardinge 	&virtual_start_attr.attr,
386cff7e81bSJeremy Fitzhardinge 	&pagesize_attr.attr,
387a649b720SJeremy Fitzhardinge 	&features_attr.attr,
38884b76257SJuergen Gross 	&buildid_attr.attr,
389cff7e81bSJeremy Fitzhardinge 	NULL
390cff7e81bSJeremy Fitzhardinge };
391cff7e81bSJeremy Fitzhardinge 
392ead1d014SJan Beulich static const struct attribute_group xen_properties_group = {
393cff7e81bSJeremy Fitzhardinge 	.name = "properties",
394cff7e81bSJeremy Fitzhardinge 	.attrs = xen_properties_attrs,
395cff7e81bSJeremy Fitzhardinge };
396cff7e81bSJeremy Fitzhardinge 
3975b00b504SJuergen Gross static int __init xen_sysfs_properties_init(void)
398cff7e81bSJeremy Fitzhardinge {
399cff7e81bSJeremy Fitzhardinge 	return sysfs_create_group(hypervisor_kobj, &xen_properties_group);
400cff7e81bSJeremy Fitzhardinge }
401cff7e81bSJeremy Fitzhardinge 
4025f141548SBoris Ostrovsky #ifdef CONFIG_XEN_HAVE_VPMU
4035f141548SBoris Ostrovsky struct pmu_mode {
4045f141548SBoris Ostrovsky 	const char *name;
4055f141548SBoris Ostrovsky 	uint32_t mode;
4065f141548SBoris Ostrovsky };
4075f141548SBoris Ostrovsky 
4085f141548SBoris Ostrovsky static struct pmu_mode pmu_modes[] = {
4095f141548SBoris Ostrovsky 	{"off", XENPMU_MODE_OFF},
4105f141548SBoris Ostrovsky 	{"self", XENPMU_MODE_SELF},
4115f141548SBoris Ostrovsky 	{"hv", XENPMU_MODE_HV},
4125f141548SBoris Ostrovsky 	{"all", XENPMU_MODE_ALL}
4135f141548SBoris Ostrovsky };
4145f141548SBoris Ostrovsky 
4155f141548SBoris Ostrovsky static ssize_t pmu_mode_store(struct hyp_sysfs_attr *attr,
4165f141548SBoris Ostrovsky 			      const char *buffer, size_t len)
4175f141548SBoris Ostrovsky {
4185f141548SBoris Ostrovsky 	int ret;
4195f141548SBoris Ostrovsky 	struct xen_pmu_params xp;
4205f141548SBoris Ostrovsky 	int i;
4215f141548SBoris Ostrovsky 
4225f141548SBoris Ostrovsky 	for (i = 0; i < ARRAY_SIZE(pmu_modes); i++) {
4235f141548SBoris Ostrovsky 		if (strncmp(buffer, pmu_modes[i].name, len - 1) == 0) {
4245f141548SBoris Ostrovsky 			xp.val = pmu_modes[i].mode;
4255f141548SBoris Ostrovsky 			break;
4265f141548SBoris Ostrovsky 		}
4275f141548SBoris Ostrovsky 	}
4285f141548SBoris Ostrovsky 
4295f141548SBoris Ostrovsky 	if (i == ARRAY_SIZE(pmu_modes))
4305f141548SBoris Ostrovsky 		return -EINVAL;
4315f141548SBoris Ostrovsky 
4325f141548SBoris Ostrovsky 	xp.version.maj = XENPMU_VER_MAJ;
4335f141548SBoris Ostrovsky 	xp.version.min = XENPMU_VER_MIN;
4345f141548SBoris Ostrovsky 	ret = HYPERVISOR_xenpmu_op(XENPMU_mode_set, &xp);
4355f141548SBoris Ostrovsky 	if (ret)
4365f141548SBoris Ostrovsky 		return ret;
4375f141548SBoris Ostrovsky 
4385f141548SBoris Ostrovsky 	return len;
4395f141548SBoris Ostrovsky }
4405f141548SBoris Ostrovsky 
4415f141548SBoris Ostrovsky static ssize_t pmu_mode_show(struct hyp_sysfs_attr *attr, char *buffer)
4425f141548SBoris Ostrovsky {
4435f141548SBoris Ostrovsky 	int ret;
4445f141548SBoris Ostrovsky 	struct xen_pmu_params xp;
4455f141548SBoris Ostrovsky 	int i;
4465f141548SBoris Ostrovsky 	uint32_t mode;
4475f141548SBoris Ostrovsky 
4485f141548SBoris Ostrovsky 	xp.version.maj = XENPMU_VER_MAJ;
4495f141548SBoris Ostrovsky 	xp.version.min = XENPMU_VER_MIN;
4505f141548SBoris Ostrovsky 	ret = HYPERVISOR_xenpmu_op(XENPMU_mode_get, &xp);
4515f141548SBoris Ostrovsky 	if (ret)
4525f141548SBoris Ostrovsky 		return ret;
4535f141548SBoris Ostrovsky 
4545f141548SBoris Ostrovsky 	mode = (uint32_t)xp.val;
4555f141548SBoris Ostrovsky 	for (i = 0; i < ARRAY_SIZE(pmu_modes); i++) {
4565f141548SBoris Ostrovsky 		if (mode == pmu_modes[i].mode)
4575f141548SBoris Ostrovsky 			return sprintf(buffer, "%s\n", pmu_modes[i].name);
4585f141548SBoris Ostrovsky 	}
4595f141548SBoris Ostrovsky 
4605f141548SBoris Ostrovsky 	return -EINVAL;
4615f141548SBoris Ostrovsky }
4625f141548SBoris Ostrovsky HYPERVISOR_ATTR_RW(pmu_mode);
4635f141548SBoris Ostrovsky 
4645f141548SBoris Ostrovsky static ssize_t pmu_features_store(struct hyp_sysfs_attr *attr,
4655f141548SBoris Ostrovsky 				  const char *buffer, size_t len)
4665f141548SBoris Ostrovsky {
4675f141548SBoris Ostrovsky 	int ret;
4685f141548SBoris Ostrovsky 	uint32_t features;
4695f141548SBoris Ostrovsky 	struct xen_pmu_params xp;
4705f141548SBoris Ostrovsky 
4715f141548SBoris Ostrovsky 	ret = kstrtou32(buffer, 0, &features);
4725f141548SBoris Ostrovsky 	if (ret)
4735f141548SBoris Ostrovsky 		return ret;
4745f141548SBoris Ostrovsky 
4755f141548SBoris Ostrovsky 	xp.val = features;
4765f141548SBoris Ostrovsky 	xp.version.maj = XENPMU_VER_MAJ;
4775f141548SBoris Ostrovsky 	xp.version.min = XENPMU_VER_MIN;
4785f141548SBoris Ostrovsky 	ret = HYPERVISOR_xenpmu_op(XENPMU_feature_set, &xp);
4795f141548SBoris Ostrovsky 	if (ret)
4805f141548SBoris Ostrovsky 		return ret;
4815f141548SBoris Ostrovsky 
4825f141548SBoris Ostrovsky 	return len;
4835f141548SBoris Ostrovsky }
4845f141548SBoris Ostrovsky 
4855f141548SBoris Ostrovsky static ssize_t pmu_features_show(struct hyp_sysfs_attr *attr, char *buffer)
4865f141548SBoris Ostrovsky {
4875f141548SBoris Ostrovsky 	int ret;
4885f141548SBoris Ostrovsky 	struct xen_pmu_params xp;
4895f141548SBoris Ostrovsky 
4905f141548SBoris Ostrovsky 	xp.version.maj = XENPMU_VER_MAJ;
4915f141548SBoris Ostrovsky 	xp.version.min = XENPMU_VER_MIN;
4925f141548SBoris Ostrovsky 	ret = HYPERVISOR_xenpmu_op(XENPMU_feature_get, &xp);
4935f141548SBoris Ostrovsky 	if (ret)
4945f141548SBoris Ostrovsky 		return ret;
4955f141548SBoris Ostrovsky 
4965f141548SBoris Ostrovsky 	return sprintf(buffer, "0x%x\n", (uint32_t)xp.val);
4975f141548SBoris Ostrovsky }
4985f141548SBoris Ostrovsky HYPERVISOR_ATTR_RW(pmu_features);
4995f141548SBoris Ostrovsky 
5005f141548SBoris Ostrovsky static struct attribute *xen_pmu_attrs[] = {
5015f141548SBoris Ostrovsky 	&pmu_mode_attr.attr,
5025f141548SBoris Ostrovsky 	&pmu_features_attr.attr,
5035f141548SBoris Ostrovsky 	NULL
5045f141548SBoris Ostrovsky };
5055f141548SBoris Ostrovsky 
5065f141548SBoris Ostrovsky static const struct attribute_group xen_pmu_group = {
5075f141548SBoris Ostrovsky 	.name = "pmu",
5085f141548SBoris Ostrovsky 	.attrs = xen_pmu_attrs,
5095f141548SBoris Ostrovsky };
5105f141548SBoris Ostrovsky 
5115b00b504SJuergen Gross static int __init xen_sysfs_pmu_init(void)
5125f141548SBoris Ostrovsky {
5135f141548SBoris Ostrovsky 	return sysfs_create_group(hypervisor_kobj, &xen_pmu_group);
5145f141548SBoris Ostrovsky }
5155f141548SBoris Ostrovsky #endif
5165f141548SBoris Ostrovsky 
517cff7e81bSJeremy Fitzhardinge static int __init hyper_sysfs_init(void)
518cff7e81bSJeremy Fitzhardinge {
519cff7e81bSJeremy Fitzhardinge 	int ret;
520cff7e81bSJeremy Fitzhardinge 
521cff7e81bSJeremy Fitzhardinge 	if (!xen_domain())
522cff7e81bSJeremy Fitzhardinge 		return -ENODEV;
523cff7e81bSJeremy Fitzhardinge 
524cff7e81bSJeremy Fitzhardinge 	ret = xen_sysfs_type_init();
525cff7e81bSJeremy Fitzhardinge 	if (ret)
526cff7e81bSJeremy Fitzhardinge 		goto out;
5274a4c29c9SJuergen Gross 	ret = xen_sysfs_guest_type_init();
5284a4c29c9SJuergen Gross 	if (ret)
5294a4c29c9SJuergen Gross 		goto guest_type_out;
530cff7e81bSJeremy Fitzhardinge 	ret = xen_sysfs_version_init();
531cff7e81bSJeremy Fitzhardinge 	if (ret)
532cff7e81bSJeremy Fitzhardinge 		goto version_out;
5335b00b504SJuergen Gross 	ret = xen_sysfs_compilation_init();
534cff7e81bSJeremy Fitzhardinge 	if (ret)
535cff7e81bSJeremy Fitzhardinge 		goto comp_out;
536cff7e81bSJeremy Fitzhardinge 	ret = xen_sysfs_uuid_init();
537cff7e81bSJeremy Fitzhardinge 	if (ret)
538cff7e81bSJeremy Fitzhardinge 		goto uuid_out;
5395b00b504SJuergen Gross 	ret = xen_sysfs_properties_init();
540cff7e81bSJeremy Fitzhardinge 	if (ret)
541cff7e81bSJeremy Fitzhardinge 		goto prop_out;
5425f141548SBoris Ostrovsky #ifdef CONFIG_XEN_HAVE_VPMU
5435f141548SBoris Ostrovsky 	if (xen_initial_domain()) {
5445b00b504SJuergen Gross 		ret = xen_sysfs_pmu_init();
5455f141548SBoris Ostrovsky 		if (ret) {
54646894f17SPaul Gortmaker 			sysfs_remove_group(hypervisor_kobj,
54746894f17SPaul Gortmaker 					   &xen_properties_group);
5485f141548SBoris Ostrovsky 			goto prop_out;
5495f141548SBoris Ostrovsky 		}
5505f141548SBoris Ostrovsky 	}
5515f141548SBoris Ostrovsky #endif
552cff7e81bSJeremy Fitzhardinge 	goto out;
553cff7e81bSJeremy Fitzhardinge 
554cff7e81bSJeremy Fitzhardinge prop_out:
55546894f17SPaul Gortmaker 	sysfs_remove_file(hypervisor_kobj, &uuid_attr.attr);
556cff7e81bSJeremy Fitzhardinge uuid_out:
55746894f17SPaul Gortmaker 	sysfs_remove_group(hypervisor_kobj, &xen_compilation_group);
558cff7e81bSJeremy Fitzhardinge comp_out:
55946894f17SPaul Gortmaker 	sysfs_remove_group(hypervisor_kobj, &version_group);
560cff7e81bSJeremy Fitzhardinge version_out:
5614a4c29c9SJuergen Gross 	sysfs_remove_file(hypervisor_kobj, &guest_type_attr.attr);
5624a4c29c9SJuergen Gross guest_type_out:
56346894f17SPaul Gortmaker 	sysfs_remove_file(hypervisor_kobj, &type_attr.attr);
564cff7e81bSJeremy Fitzhardinge out:
565cff7e81bSJeremy Fitzhardinge 	return ret;
566cff7e81bSJeremy Fitzhardinge }
56746894f17SPaul Gortmaker device_initcall(hyper_sysfs_init);
568cff7e81bSJeremy Fitzhardinge 
569cff7e81bSJeremy Fitzhardinge static ssize_t hyp_sysfs_show(struct kobject *kobj,
570cff7e81bSJeremy Fitzhardinge 			      struct attribute *attr,
571cff7e81bSJeremy Fitzhardinge 			      char *buffer)
572cff7e81bSJeremy Fitzhardinge {
573cff7e81bSJeremy Fitzhardinge 	struct hyp_sysfs_attr *hyp_attr;
574cff7e81bSJeremy Fitzhardinge 	hyp_attr = container_of(attr, struct hyp_sysfs_attr, attr);
575cff7e81bSJeremy Fitzhardinge 	if (hyp_attr->show)
576cff7e81bSJeremy Fitzhardinge 		return hyp_attr->show(hyp_attr, buffer);
577cff7e81bSJeremy Fitzhardinge 	return 0;
578cff7e81bSJeremy Fitzhardinge }
579cff7e81bSJeremy Fitzhardinge 
580cff7e81bSJeremy Fitzhardinge static ssize_t hyp_sysfs_store(struct kobject *kobj,
581cff7e81bSJeremy Fitzhardinge 			       struct attribute *attr,
582cff7e81bSJeremy Fitzhardinge 			       const char *buffer,
583cff7e81bSJeremy Fitzhardinge 			       size_t len)
584cff7e81bSJeremy Fitzhardinge {
585cff7e81bSJeremy Fitzhardinge 	struct hyp_sysfs_attr *hyp_attr;
586cff7e81bSJeremy Fitzhardinge 	hyp_attr = container_of(attr, struct hyp_sysfs_attr, attr);
587cff7e81bSJeremy Fitzhardinge 	if (hyp_attr->store)
588cff7e81bSJeremy Fitzhardinge 		return hyp_attr->store(hyp_attr, buffer, len);
589cff7e81bSJeremy Fitzhardinge 	return 0;
590cff7e81bSJeremy Fitzhardinge }
591cff7e81bSJeremy Fitzhardinge 
59252cf25d0SEmese Revfy static const struct sysfs_ops hyp_sysfs_ops = {
593cff7e81bSJeremy Fitzhardinge 	.show = hyp_sysfs_show,
594cff7e81bSJeremy Fitzhardinge 	.store = hyp_sysfs_store,
595cff7e81bSJeremy Fitzhardinge };
596cff7e81bSJeremy Fitzhardinge 
597cff7e81bSJeremy Fitzhardinge static struct kobj_type hyp_sysfs_kobj_type = {
598cff7e81bSJeremy Fitzhardinge 	.sysfs_ops = &hyp_sysfs_ops,
599cff7e81bSJeremy Fitzhardinge };
600cff7e81bSJeremy Fitzhardinge 
601cff7e81bSJeremy Fitzhardinge static int __init hypervisor_subsys_init(void)
602cff7e81bSJeremy Fitzhardinge {
603cff7e81bSJeremy Fitzhardinge 	if (!xen_domain())
604cff7e81bSJeremy Fitzhardinge 		return -ENODEV;
605cff7e81bSJeremy Fitzhardinge 
606cff7e81bSJeremy Fitzhardinge 	hypervisor_kobj->ktype = &hyp_sysfs_kobj_type;
607cff7e81bSJeremy Fitzhardinge 	return 0;
608cff7e81bSJeremy Fitzhardinge }
609cff7e81bSJeremy Fitzhardinge device_initcall(hypervisor_subsys_init);
610