psci.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/kvm_emulate.h>
  20. #include <asm/kvm_psci.h>
  21. /*
  22. * This is an implementation of the Power State Coordination Interface
  23. * as described in ARM document number ARM DEN 0022A.
  24. */
  25. static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu)
  26. {
  27. vcpu->arch.pause = true;
  28. }
  29. static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
  30. {
  31. struct kvm *kvm = source_vcpu->kvm;
  32. struct kvm_vcpu *vcpu;
  33. wait_queue_head_t *wq;
  34. unsigned long cpu_id;
  35. phys_addr_t target_pc;
  36. cpu_id = *vcpu_reg(source_vcpu, 1);
  37. if (vcpu_mode_is_32bit(source_vcpu))
  38. cpu_id &= ~((u32) 0);
  39. if (cpu_id >= atomic_read(&kvm->online_vcpus))
  40. return KVM_PSCI_RET_INVAL;
  41. target_pc = *vcpu_reg(source_vcpu, 2);
  42. vcpu = kvm_get_vcpu(kvm, cpu_id);
  43. wq = kvm_arch_vcpu_wq(vcpu);
  44. if (!waitqueue_active(wq))
  45. return KVM_PSCI_RET_INVAL;
  46. kvm_reset_vcpu(vcpu);
  47. /* Gracefully handle Thumb2 entry point */
  48. if (vcpu_mode_is_32bit(vcpu) && (target_pc & 1)) {
  49. target_pc &= ~((phys_addr_t) 1);
  50. vcpu_set_thumb(vcpu);
  51. }
  52. *vcpu_pc(vcpu) = target_pc;
  53. vcpu->arch.pause = false;
  54. smp_mb(); /* Make sure the above is visible */
  55. wake_up_interruptible(wq);
  56. return KVM_PSCI_RET_SUCCESS;
  57. }
  58. /**
  59. * kvm_psci_call - handle PSCI call if r0 value is in range
  60. * @vcpu: Pointer to the VCPU struct
  61. *
  62. * Handle PSCI calls from guests through traps from HVC or SMC instructions.
  63. * The calling convention is similar to SMC calls to the secure world where
  64. * the function number is placed in r0 and this function returns true if the
  65. * function number specified in r0 is withing the PSCI range, and false
  66. * otherwise.
  67. */
  68. bool kvm_psci_call(struct kvm_vcpu *vcpu)
  69. {
  70. unsigned long psci_fn = *vcpu_reg(vcpu, 0) & ~((u32) 0);
  71. unsigned long val;
  72. switch (psci_fn) {
  73. case KVM_PSCI_FN_CPU_OFF:
  74. kvm_psci_vcpu_off(vcpu);
  75. val = KVM_PSCI_RET_SUCCESS;
  76. break;
  77. case KVM_PSCI_FN_CPU_ON:
  78. val = kvm_psci_vcpu_on(vcpu);
  79. break;
  80. case KVM_PSCI_FN_CPU_SUSPEND:
  81. case KVM_PSCI_FN_MIGRATE:
  82. val = KVM_PSCI_RET_NI;
  83. break;
  84. default:
  85. return false;
  86. }
  87. *vcpu_reg(vcpu, 0) = val;
  88. return true;
  89. }