hvapi.c 4.6 KB

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