psci.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (C) 2012 - ARM Ltd
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/kvm_host.h>
  18. #include <linux/wait.h>
  19. #include <asm/cputype.h>
  20. #include <asm/kvm_emulate.h>
  21. #include <asm/kvm_psci.h>
  22. /*
  23. * This is an implementation of the Power State Coordination Interface
  24. * as described in ARM document number ARM DEN 0022A.
  25. */
  26. static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu)
  27. {
  28. vcpu->arch.pause = true;
  29. }
  30. static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
  31. {
  32. struct kvm *kvm = source_vcpu->kvm;
  33. struct kvm_vcpu *vcpu = NULL, *tmp;
  34. wait_queue_head_t *wq;
  35. unsigned long cpu_id;
  36. unsigned long mpidr;
  37. phys_addr_t target_pc;
  38. int i;
  39. cpu_id = *vcpu_reg(source_vcpu, 1);
  40. if (vcpu_mode_is_32bit(source_vcpu))
  41. cpu_id &= ~((u32) 0);
  42. kvm_for_each_vcpu(i, tmp, kvm) {
  43. mpidr = kvm_vcpu_get_mpidr(tmp);
  44. if ((mpidr & MPIDR_HWID_BITMASK) == (cpu_id & MPIDR_HWID_BITMASK)) {
  45. vcpu = tmp;
  46. break;
  47. }
  48. }
  49. if (!vcpu)
  50. return KVM_PSCI_RET_INVAL;
  51. target_pc = *vcpu_reg(source_vcpu, 2);
  52. wq = kvm_arch_vcpu_wq(vcpu);
  53. if (!waitqueue_active(wq))
  54. return KVM_PSCI_RET_INVAL;
  55. kvm_reset_vcpu(vcpu);
  56. /* Gracefully handle Thumb2 entry point */
  57. if (vcpu_mode_is_32bit(vcpu) && (target_pc & 1)) {
  58. target_pc &= ~((phys_addr_t) 1);
  59. vcpu_set_thumb(vcpu);
  60. }
  61. /* Propagate caller endianness */
  62. if (kvm_vcpu_is_be(source_vcpu))
  63. kvm_vcpu_set_be(vcpu);
  64. *vcpu_pc(vcpu) = target_pc;
  65. vcpu->arch.pause = false;
  66. smp_mb(); /* Make sure the above is visible */
  67. wake_up_interruptible(wq);
  68. return KVM_PSCI_RET_SUCCESS;
  69. }
  70. /**
  71. * kvm_psci_call - handle PSCI call if r0 value is in range
  72. * @vcpu: Pointer to the VCPU struct
  73. *
  74. * Handle PSCI calls from guests through traps from HVC instructions.
  75. * The calling convention is similar to SMC calls to the secure world where
  76. * the function number is placed in r0 and this function returns true if the
  77. * function number specified in r0 is withing the PSCI range, and false
  78. * otherwise.
  79. */
  80. bool kvm_psci_call(struct kvm_vcpu *vcpu)
  81. {
  82. unsigned long psci_fn = *vcpu_reg(vcpu, 0) & ~((u32) 0);
  83. unsigned long val;
  84. switch (psci_fn) {
  85. case KVM_PSCI_FN_CPU_OFF:
  86. kvm_psci_vcpu_off(vcpu);
  87. val = KVM_PSCI_RET_SUCCESS;
  88. break;
  89. case KVM_PSCI_FN_CPU_ON:
  90. val = kvm_psci_vcpu_on(vcpu);
  91. break;
  92. case KVM_PSCI_FN_CPU_SUSPEND:
  93. case KVM_PSCI_FN_MIGRATE:
  94. val = KVM_PSCI_RET_NI;
  95. break;
  96. default:
  97. return false;
  98. }
  99. *vcpu_reg(vcpu, 0) = val;
  100. return true;
  101. }