treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / xen / sys-hypervisor.c
blobfeb1d16252e70566a17cad56dbd70068ce7285b4
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * copyright (c) 2006 IBM Corporation
4 * Authored by: Mike D. Day <ncmike@us.ibm.com>
5 */
7 #include <linux/slab.h>
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/kobject.h>
11 #include <linux/err.h>
13 #include <asm/xen/hypervisor.h>
14 #include <asm/xen/hypercall.h>
16 #include <xen/xen.h>
17 #include <xen/xenbus.h>
18 #include <xen/interface/xen.h>
19 #include <xen/interface/version.h>
20 #ifdef CONFIG_XEN_HAVE_VPMU
21 #include <xen/interface/xenpmu.h>
22 #endif
24 #define HYPERVISOR_ATTR_RO(_name) \
25 static struct hyp_sysfs_attr _name##_attr = __ATTR_RO(_name)
27 #define HYPERVISOR_ATTR_RW(_name) \
28 static struct hyp_sysfs_attr _name##_attr = \
29 __ATTR(_name, 0644, _name##_show, _name##_store)
31 struct hyp_sysfs_attr {
32 struct attribute attr;
33 ssize_t (*show)(struct hyp_sysfs_attr *, char *);
34 ssize_t (*store)(struct hyp_sysfs_attr *, const char *, size_t);
35 void *hyp_attr_data;
38 static ssize_t type_show(struct hyp_sysfs_attr *attr, char *buffer)
40 return sprintf(buffer, "xen\n");
43 HYPERVISOR_ATTR_RO(type);
45 static int __init xen_sysfs_type_init(void)
47 return sysfs_create_file(hypervisor_kobj, &type_attr.attr);
50 static ssize_t guest_type_show(struct hyp_sysfs_attr *attr, char *buffer)
52 const char *type;
54 switch (xen_domain_type) {
55 case XEN_NATIVE:
56 /* ARM only. */
57 type = "Xen";
58 break;
59 case XEN_PV_DOMAIN:
60 type = "PV";
61 break;
62 case XEN_HVM_DOMAIN:
63 type = xen_pvh_domain() ? "PVH" : "HVM";
64 break;
65 default:
66 return -EINVAL;
69 return sprintf(buffer, "%s\n", type);
72 HYPERVISOR_ATTR_RO(guest_type);
74 static int __init xen_sysfs_guest_type_init(void)
76 return sysfs_create_file(hypervisor_kobj, &guest_type_attr.attr);
79 /* xen version attributes */
80 static ssize_t major_show(struct hyp_sysfs_attr *attr, char *buffer)
82 int version = HYPERVISOR_xen_version(XENVER_version, NULL);
83 if (version)
84 return sprintf(buffer, "%d\n", version >> 16);
85 return -ENODEV;
88 HYPERVISOR_ATTR_RO(major);
90 static ssize_t minor_show(struct hyp_sysfs_attr *attr, char *buffer)
92 int version = HYPERVISOR_xen_version(XENVER_version, NULL);
93 if (version)
94 return sprintf(buffer, "%d\n", version & 0xff);
95 return -ENODEV;
98 HYPERVISOR_ATTR_RO(minor);
100 static ssize_t extra_show(struct hyp_sysfs_attr *attr, char *buffer)
102 int ret = -ENOMEM;
103 char *extra;
105 extra = kmalloc(XEN_EXTRAVERSION_LEN, GFP_KERNEL);
106 if (extra) {
107 ret = HYPERVISOR_xen_version(XENVER_extraversion, extra);
108 if (!ret)
109 ret = sprintf(buffer, "%s\n", extra);
110 kfree(extra);
113 return ret;
116 HYPERVISOR_ATTR_RO(extra);
118 static struct attribute *version_attrs[] = {
119 &major_attr.attr,
120 &minor_attr.attr,
121 &extra_attr.attr,
122 NULL
125 static const struct attribute_group version_group = {
126 .name = "version",
127 .attrs = version_attrs,
130 static int __init xen_sysfs_version_init(void)
132 return sysfs_create_group(hypervisor_kobj, &version_group);
135 /* UUID */
137 static ssize_t uuid_show_fallback(struct hyp_sysfs_attr *attr, char *buffer)
139 char *vm, *val;
140 int ret;
141 extern int xenstored_ready;
143 if (!xenstored_ready)
144 return -EBUSY;
146 vm = xenbus_read(XBT_NIL, "vm", "", NULL);
147 if (IS_ERR(vm))
148 return PTR_ERR(vm);
149 val = xenbus_read(XBT_NIL, vm, "uuid", NULL);
150 kfree(vm);
151 if (IS_ERR(val))
152 return PTR_ERR(val);
153 ret = sprintf(buffer, "%s\n", val);
154 kfree(val);
155 return ret;
158 static ssize_t uuid_show(struct hyp_sysfs_attr *attr, char *buffer)
160 xen_domain_handle_t uuid;
161 int ret;
162 ret = HYPERVISOR_xen_version(XENVER_guest_handle, uuid);
163 if (ret)
164 return uuid_show_fallback(attr, buffer);
165 ret = sprintf(buffer, "%pU\n", uuid);
166 return ret;
169 HYPERVISOR_ATTR_RO(uuid);
171 static int __init xen_sysfs_uuid_init(void)
173 return sysfs_create_file(hypervisor_kobj, &uuid_attr.attr);
176 /* xen compilation attributes */
178 static ssize_t compiler_show(struct hyp_sysfs_attr *attr, char *buffer)
180 int ret = -ENOMEM;
181 struct xen_compile_info *info;
183 info = kmalloc(sizeof(struct xen_compile_info), GFP_KERNEL);
184 if (info) {
185 ret = HYPERVISOR_xen_version(XENVER_compile_info, info);
186 if (!ret)
187 ret = sprintf(buffer, "%s\n", info->compiler);
188 kfree(info);
191 return ret;
194 HYPERVISOR_ATTR_RO(compiler);
196 static ssize_t compiled_by_show(struct hyp_sysfs_attr *attr, char *buffer)
198 int ret = -ENOMEM;
199 struct xen_compile_info *info;
201 info = kmalloc(sizeof(struct xen_compile_info), GFP_KERNEL);
202 if (info) {
203 ret = HYPERVISOR_xen_version(XENVER_compile_info, info);
204 if (!ret)
205 ret = sprintf(buffer, "%s\n", info->compile_by);
206 kfree(info);
209 return ret;
212 HYPERVISOR_ATTR_RO(compiled_by);
214 static ssize_t compile_date_show(struct hyp_sysfs_attr *attr, char *buffer)
216 int ret = -ENOMEM;
217 struct xen_compile_info *info;
219 info = kmalloc(sizeof(struct xen_compile_info), GFP_KERNEL);
220 if (info) {
221 ret = HYPERVISOR_xen_version(XENVER_compile_info, info);
222 if (!ret)
223 ret = sprintf(buffer, "%s\n", info->compile_date);
224 kfree(info);
227 return ret;
230 HYPERVISOR_ATTR_RO(compile_date);
232 static struct attribute *xen_compile_attrs[] = {
233 &compiler_attr.attr,
234 &compiled_by_attr.attr,
235 &compile_date_attr.attr,
236 NULL
239 static const struct attribute_group xen_compilation_group = {
240 .name = "compilation",
241 .attrs = xen_compile_attrs,
244 static int __init xen_sysfs_compilation_init(void)
246 return sysfs_create_group(hypervisor_kobj, &xen_compilation_group);
249 /* xen properties info */
251 static ssize_t capabilities_show(struct hyp_sysfs_attr *attr, char *buffer)
253 int ret = -ENOMEM;
254 char *caps;
256 caps = kmalloc(XEN_CAPABILITIES_INFO_LEN, GFP_KERNEL);
257 if (caps) {
258 ret = HYPERVISOR_xen_version(XENVER_capabilities, caps);
259 if (!ret)
260 ret = sprintf(buffer, "%s\n", caps);
261 kfree(caps);
264 return ret;
267 HYPERVISOR_ATTR_RO(capabilities);
269 static ssize_t changeset_show(struct hyp_sysfs_attr *attr, char *buffer)
271 int ret = -ENOMEM;
272 char *cset;
274 cset = kmalloc(XEN_CHANGESET_INFO_LEN, GFP_KERNEL);
275 if (cset) {
276 ret = HYPERVISOR_xen_version(XENVER_changeset, cset);
277 if (!ret)
278 ret = sprintf(buffer, "%s\n", cset);
279 kfree(cset);
282 return ret;
285 HYPERVISOR_ATTR_RO(changeset);
287 static ssize_t virtual_start_show(struct hyp_sysfs_attr *attr, char *buffer)
289 int ret = -ENOMEM;
290 struct xen_platform_parameters *parms;
292 parms = kmalloc(sizeof(struct xen_platform_parameters), GFP_KERNEL);
293 if (parms) {
294 ret = HYPERVISOR_xen_version(XENVER_platform_parameters,
295 parms);
296 if (!ret)
297 ret = sprintf(buffer, "%"PRI_xen_ulong"\n",
298 parms->virt_start);
299 kfree(parms);
302 return ret;
305 HYPERVISOR_ATTR_RO(virtual_start);
307 static ssize_t pagesize_show(struct hyp_sysfs_attr *attr, char *buffer)
309 int ret;
311 ret = HYPERVISOR_xen_version(XENVER_pagesize, NULL);
312 if (ret > 0)
313 ret = sprintf(buffer, "%x\n", ret);
315 return ret;
318 HYPERVISOR_ATTR_RO(pagesize);
320 static ssize_t xen_feature_show(int index, char *buffer)
322 ssize_t ret;
323 struct xen_feature_info info;
325 info.submap_idx = index;
326 ret = HYPERVISOR_xen_version(XENVER_get_features, &info);
327 if (!ret)
328 ret = sprintf(buffer, "%08x", info.submap);
330 return ret;
333 static ssize_t features_show(struct hyp_sysfs_attr *attr, char *buffer)
335 ssize_t len;
336 int i;
338 len = 0;
339 for (i = XENFEAT_NR_SUBMAPS-1; i >= 0; i--) {
340 int ret = xen_feature_show(i, buffer + len);
341 if (ret < 0) {
342 if (len == 0)
343 len = ret;
344 break;
346 len += ret;
348 if (len > 0)
349 buffer[len++] = '\n';
351 return len;
354 HYPERVISOR_ATTR_RO(features);
356 static ssize_t buildid_show(struct hyp_sysfs_attr *attr, char *buffer)
358 ssize_t ret;
359 struct xen_build_id *buildid;
361 ret = HYPERVISOR_xen_version(XENVER_build_id, NULL);
362 if (ret < 0) {
363 if (ret == -EPERM)
364 ret = sprintf(buffer, "<denied>");
365 return ret;
368 buildid = kmalloc(sizeof(*buildid) + ret, GFP_KERNEL);
369 if (!buildid)
370 return -ENOMEM;
372 buildid->len = ret;
373 ret = HYPERVISOR_xen_version(XENVER_build_id, buildid);
374 if (ret > 0)
375 ret = sprintf(buffer, "%s", buildid->buf);
376 kfree(buildid);
378 return ret;
381 HYPERVISOR_ATTR_RO(buildid);
383 static struct attribute *xen_properties_attrs[] = {
384 &capabilities_attr.attr,
385 &changeset_attr.attr,
386 &virtual_start_attr.attr,
387 &pagesize_attr.attr,
388 &features_attr.attr,
389 &buildid_attr.attr,
390 NULL
393 static const struct attribute_group xen_properties_group = {
394 .name = "properties",
395 .attrs = xen_properties_attrs,
398 static int __init xen_sysfs_properties_init(void)
400 return sysfs_create_group(hypervisor_kobj, &xen_properties_group);
403 #ifdef CONFIG_XEN_HAVE_VPMU
404 struct pmu_mode {
405 const char *name;
406 uint32_t mode;
409 static struct pmu_mode pmu_modes[] = {
410 {"off", XENPMU_MODE_OFF},
411 {"self", XENPMU_MODE_SELF},
412 {"hv", XENPMU_MODE_HV},
413 {"all", XENPMU_MODE_ALL}
416 static ssize_t pmu_mode_store(struct hyp_sysfs_attr *attr,
417 const char *buffer, size_t len)
419 int ret;
420 struct xen_pmu_params xp;
421 int i;
423 for (i = 0; i < ARRAY_SIZE(pmu_modes); i++) {
424 if (strncmp(buffer, pmu_modes[i].name, len - 1) == 0) {
425 xp.val = pmu_modes[i].mode;
426 break;
430 if (i == ARRAY_SIZE(pmu_modes))
431 return -EINVAL;
433 xp.version.maj = XENPMU_VER_MAJ;
434 xp.version.min = XENPMU_VER_MIN;
435 ret = HYPERVISOR_xenpmu_op(XENPMU_mode_set, &xp);
436 if (ret)
437 return ret;
439 return len;
442 static ssize_t pmu_mode_show(struct hyp_sysfs_attr *attr, char *buffer)
444 int ret;
445 struct xen_pmu_params xp;
446 int i;
447 uint32_t mode;
449 xp.version.maj = XENPMU_VER_MAJ;
450 xp.version.min = XENPMU_VER_MIN;
451 ret = HYPERVISOR_xenpmu_op(XENPMU_mode_get, &xp);
452 if (ret)
453 return ret;
455 mode = (uint32_t)xp.val;
456 for (i = 0; i < ARRAY_SIZE(pmu_modes); i++) {
457 if (mode == pmu_modes[i].mode)
458 return sprintf(buffer, "%s\n", pmu_modes[i].name);
461 return -EINVAL;
463 HYPERVISOR_ATTR_RW(pmu_mode);
465 static ssize_t pmu_features_store(struct hyp_sysfs_attr *attr,
466 const char *buffer, size_t len)
468 int ret;
469 uint32_t features;
470 struct xen_pmu_params xp;
472 ret = kstrtou32(buffer, 0, &features);
473 if (ret)
474 return ret;
476 xp.val = features;
477 xp.version.maj = XENPMU_VER_MAJ;
478 xp.version.min = XENPMU_VER_MIN;
479 ret = HYPERVISOR_xenpmu_op(XENPMU_feature_set, &xp);
480 if (ret)
481 return ret;
483 return len;
486 static ssize_t pmu_features_show(struct hyp_sysfs_attr *attr, char *buffer)
488 int ret;
489 struct xen_pmu_params xp;
491 xp.version.maj = XENPMU_VER_MAJ;
492 xp.version.min = XENPMU_VER_MIN;
493 ret = HYPERVISOR_xenpmu_op(XENPMU_feature_get, &xp);
494 if (ret)
495 return ret;
497 return sprintf(buffer, "0x%x\n", (uint32_t)xp.val);
499 HYPERVISOR_ATTR_RW(pmu_features);
501 static struct attribute *xen_pmu_attrs[] = {
502 &pmu_mode_attr.attr,
503 &pmu_features_attr.attr,
504 NULL
507 static const struct attribute_group xen_pmu_group = {
508 .name = "pmu",
509 .attrs = xen_pmu_attrs,
512 static int __init xen_sysfs_pmu_init(void)
514 return sysfs_create_group(hypervisor_kobj, &xen_pmu_group);
516 #endif
518 static int __init hyper_sysfs_init(void)
520 int ret;
522 if (!xen_domain())
523 return -ENODEV;
525 ret = xen_sysfs_type_init();
526 if (ret)
527 goto out;
528 ret = xen_sysfs_guest_type_init();
529 if (ret)
530 goto guest_type_out;
531 ret = xen_sysfs_version_init();
532 if (ret)
533 goto version_out;
534 ret = xen_sysfs_compilation_init();
535 if (ret)
536 goto comp_out;
537 ret = xen_sysfs_uuid_init();
538 if (ret)
539 goto uuid_out;
540 ret = xen_sysfs_properties_init();
541 if (ret)
542 goto prop_out;
543 #ifdef CONFIG_XEN_HAVE_VPMU
544 if (xen_initial_domain()) {
545 ret = xen_sysfs_pmu_init();
546 if (ret) {
547 sysfs_remove_group(hypervisor_kobj,
548 &xen_properties_group);
549 goto prop_out;
552 #endif
553 goto out;
555 prop_out:
556 sysfs_remove_file(hypervisor_kobj, &uuid_attr.attr);
557 uuid_out:
558 sysfs_remove_group(hypervisor_kobj, &xen_compilation_group);
559 comp_out:
560 sysfs_remove_group(hypervisor_kobj, &version_group);
561 version_out:
562 sysfs_remove_file(hypervisor_kobj, &guest_type_attr.attr);
563 guest_type_out:
564 sysfs_remove_file(hypervisor_kobj, &type_attr.attr);
565 out:
566 return ret;
568 device_initcall(hyper_sysfs_init);
570 static ssize_t hyp_sysfs_show(struct kobject *kobj,
571 struct attribute *attr,
572 char *buffer)
574 struct hyp_sysfs_attr *hyp_attr;
575 hyp_attr = container_of(attr, struct hyp_sysfs_attr, attr);
576 if (hyp_attr->show)
577 return hyp_attr->show(hyp_attr, buffer);
578 return 0;
581 static ssize_t hyp_sysfs_store(struct kobject *kobj,
582 struct attribute *attr,
583 const char *buffer,
584 size_t len)
586 struct hyp_sysfs_attr *hyp_attr;
587 hyp_attr = container_of(attr, struct hyp_sysfs_attr, attr);
588 if (hyp_attr->store)
589 return hyp_attr->store(hyp_attr, buffer, len);
590 return 0;
593 static const struct sysfs_ops hyp_sysfs_ops = {
594 .show = hyp_sysfs_show,
595 .store = hyp_sysfs_store,
598 static struct kobj_type hyp_sysfs_kobj_type = {
599 .sysfs_ops = &hyp_sysfs_ops,
602 static int __init hypervisor_subsys_init(void)
604 if (!xen_domain())
605 return -ENODEV;
607 hypervisor_kobj->ktype = &hyp_sysfs_kobj_type;
608 return 0;
610 device_initcall(hypervisor_subsys_init);