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