kvm_para.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License, version 2, as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. *
  15. * Copyright IBM Corp. 2008
  16. *
  17. * Authors: Hollis Blanchard <hollisb@us.ibm.com>
  18. */
  19. #ifndef __POWERPC_KVM_PARA_H__
  20. #define __POWERPC_KVM_PARA_H__
  21. #include <linux/types.h>
  22. #include <linux/of.h>
  23. struct kvm_vcpu_arch_shared {
  24. __u64 scratch1;
  25. __u64 scratch2;
  26. __u64 scratch3;
  27. __u64 critical; /* Guest may not get interrupts if == r1 */
  28. __u64 sprg0;
  29. __u64 sprg1;
  30. __u64 sprg2;
  31. __u64 sprg3;
  32. __u64 srr0;
  33. __u64 srr1;
  34. __u64 dar;
  35. __u64 msr;
  36. __u32 dsisr;
  37. };
  38. #define KVM_SC_MAGIC_R0 0x4b564d21 /* "KVM!" */
  39. #define HC_VENDOR_KVM (42 << 16)
  40. #define HC_EV_SUCCESS 0
  41. #define HC_EV_UNIMPLEMENTED 12
  42. #ifdef __KERNEL__
  43. #ifdef CONFIG_KVM_GUEST
  44. static inline int kvm_para_available(void)
  45. {
  46. struct device_node *hyper_node;
  47. hyper_node = of_find_node_by_path("/hypervisor");
  48. if (!hyper_node)
  49. return 0;
  50. if (!of_device_is_compatible(hyper_node, "linux,kvm"))
  51. return 0;
  52. return 1;
  53. }
  54. extern unsigned long kvm_hypercall(unsigned long *in,
  55. unsigned long *out,
  56. unsigned long nr);
  57. #else
  58. static inline int kvm_para_available(void)
  59. {
  60. return 0;
  61. }
  62. static unsigned long kvm_hypercall(unsigned long *in,
  63. unsigned long *out,
  64. unsigned long nr)
  65. {
  66. return HC_EV_UNIMPLEMENTED;
  67. }
  68. #endif
  69. static inline long kvm_hypercall0_1(unsigned int nr, unsigned long *r2)
  70. {
  71. unsigned long in[8];
  72. unsigned long out[8];
  73. unsigned long r;
  74. r = kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
  75. *r2 = out[0];
  76. return r;
  77. }
  78. static inline long kvm_hypercall0(unsigned int nr)
  79. {
  80. unsigned long in[8];
  81. unsigned long out[8];
  82. return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
  83. }
  84. static inline long kvm_hypercall1(unsigned int nr, unsigned long p1)
  85. {
  86. unsigned long in[8];
  87. unsigned long out[8];
  88. in[0] = p1;
  89. return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
  90. }
  91. static inline long kvm_hypercall2(unsigned int nr, unsigned long p1,
  92. unsigned long p2)
  93. {
  94. unsigned long in[8];
  95. unsigned long out[8];
  96. in[0] = p1;
  97. in[1] = p2;
  98. return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
  99. }
  100. static inline long kvm_hypercall3(unsigned int nr, unsigned long p1,
  101. unsigned long p2, unsigned long p3)
  102. {
  103. unsigned long in[8];
  104. unsigned long out[8];
  105. in[0] = p1;
  106. in[1] = p2;
  107. in[2] = p3;
  108. return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
  109. }
  110. static inline long kvm_hypercall4(unsigned int nr, unsigned long p1,
  111. unsigned long p2, unsigned long p3,
  112. unsigned long p4)
  113. {
  114. unsigned long in[8];
  115. unsigned long out[8];
  116. in[0] = p1;
  117. in[1] = p2;
  118. in[2] = p3;
  119. in[3] = p4;
  120. return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
  121. }
  122. static inline unsigned int kvm_arch_para_features(void)
  123. {
  124. unsigned long r;
  125. if (!kvm_para_available())
  126. return 0;
  127. if(kvm_hypercall0_1(KVM_HC_FEATURES, &r))
  128. return 0;
  129. return r;
  130. }
  131. #endif /* __KERNEL__ */
  132. #endif /* __POWERPC_KVM_PARA_H__ */