hvapi.c 4.4 KB

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