^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* hvapi.c: Hypervisor API management.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2007 David S. Miller <davem@davemloft.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <asm/hypervisor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <asm/oplib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) /* If the hypervisor indicates that the API setting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * calls are unsupported, by returning HV_EBADTRAP or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * HV_ENOTSUPPORTED, we assume that API groups with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * PRE_API flag set are major 1 minor 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct api_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) unsigned long group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) unsigned long major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) unsigned long minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) unsigned int refcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) unsigned int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define FLAG_PRE_API 0x00000001
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static struct api_info api_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) { .group = HV_GRP_SUN4V, .flags = FLAG_PRE_API },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) { .group = HV_GRP_CORE, .flags = FLAG_PRE_API },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) { .group = HV_GRP_INTR, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) { .group = HV_GRP_SOFT_STATE, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) { .group = HV_GRP_TM, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) { .group = HV_GRP_PCI, .flags = FLAG_PRE_API },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) { .group = HV_GRP_LDOM, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) { .group = HV_GRP_SVC_CHAN, .flags = FLAG_PRE_API },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) { .group = HV_GRP_NCS, .flags = FLAG_PRE_API },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) { .group = HV_GRP_RNG, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) { .group = HV_GRP_PBOOT, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) { .group = HV_GRP_TPM, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) { .group = HV_GRP_SDIO, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) { .group = HV_GRP_SDIO_ERR, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) { .group = HV_GRP_REBOOT_DATA, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) { .group = HV_GRP_ATU, .flags = FLAG_PRE_API },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) { .group = HV_GRP_DAX, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) { .group = HV_GRP_NIAG_PERF, .flags = FLAG_PRE_API },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) { .group = HV_GRP_FIRE_PERF, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) { .group = HV_GRP_N2_CPU, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) { .group = HV_GRP_NIU, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) { .group = HV_GRP_VF_CPU, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) { .group = HV_GRP_KT_CPU, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) { .group = HV_GRP_VT_CPU, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) { .group = HV_GRP_T5_CPU, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) { .group = HV_GRP_DIAG, .flags = FLAG_PRE_API },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) { .group = HV_GRP_M7_PERF, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static DEFINE_SPINLOCK(hvapi_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static struct api_info *__get_info(unsigned long group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) for (i = 0; i < ARRAY_SIZE(api_table); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (api_table[i].group == group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return &api_table[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static void __get_ref(struct api_info *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) p->refcnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static void __put_ref(struct api_info *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (--p->refcnt == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) unsigned long ignore;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) sun4v_set_version(p->group, 0, 0, &ignore);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) p->major = p->minor = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /* Register a hypervisor API specification. It indicates the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * API group and desired major+minor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * If an existing API registration exists '0' (success) will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * be returned if it is compatible with the one being registered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * Otherwise a negative error code will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * Otherwise an attempt will be made to negotiate the requested
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * API group/major/minor with the hypervisor, and errors returned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * if that does not succeed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) int sun4v_hvapi_register(unsigned long group, unsigned long major,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) unsigned long *minor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct api_info *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) spin_lock_irqsave(&hvapi_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) p = __get_info(group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (p->refcnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (p->major == major) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) *minor = p->minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) unsigned long actual_minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) unsigned long hv_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) hv_ret = sun4v_set_version(group, major, *minor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) &actual_minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (hv_ret == HV_EOK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) *minor = actual_minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) p->major = major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) p->minor = actual_minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) } else if (hv_ret == HV_EBADTRAP ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) hv_ret == HV_ENOTSUPPORTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (p->flags & FLAG_PRE_API) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (major == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) p->major = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) p->minor = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) *minor = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) __get_ref(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) spin_unlock_irqrestore(&hvapi_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) EXPORT_SYMBOL(sun4v_hvapi_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) void sun4v_hvapi_unregister(unsigned long group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct api_info *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) spin_lock_irqsave(&hvapi_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) p = __get_info(group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) __put_ref(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) spin_unlock_irqrestore(&hvapi_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) EXPORT_SYMBOL(sun4v_hvapi_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) int sun4v_hvapi_get(unsigned long group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) unsigned long *major,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) unsigned long *minor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) struct api_info *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) spin_lock_irqsave(&hvapi_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) p = __get_info(group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (p && p->refcnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) *major = p->major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) *minor = p->minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) spin_unlock_irqrestore(&hvapi_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) EXPORT_SYMBOL(sun4v_hvapi_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) void __init sun4v_hvapi_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) unsigned long group, major, minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) group = HV_GRP_SUN4V;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) major = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) minor = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (sun4v_hvapi_register(group, major, &minor))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) group = HV_GRP_CORE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) major = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) minor = 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (sun4v_hvapi_register(group, major, &minor))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) prom_printf("HVAPI: Cannot register API group "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) "%lx with major(%lu) minor(%lu)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) group, major, minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) prom_halt();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }