hvapi.c 4.5 KB

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