hvapi.c 4.3 KB

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