vm.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2019 Western Digital Corporation or its affiliates.
  4. *
  5. * Authors:
  6. * Anup Patel <anup.patel@wdc.com>
  7. */
  8. #include <linux/errno.h>
  9. #include <linux/err.h>
  10. #include <linux/module.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/kvm_host.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/of_address.h>
  17. static unsigned int kvm_arch_irq;
  18. static volatile unsigned char __iomem *kvm_arch_guest_intr_reg;
  19. extern bool kvm_riscv_vcpu_notify(void);
  20. void kvm_arch_notify_guest(void)
  21. {
  22. writel(1, kvm_arch_guest_intr_reg);
  23. }
  24. static irqreturn_t kvm_arch_interrupt(int irq, void *opaque)
  25. {
  26. struct kvm *kvm = (struct kvm *)opaque;
  27. #ifdef CONFIG_SOC_INT_SRC7
  28. writel(0, kvm->arch.backend_intr_reg);
  29. #else
  30. /* Clear mailbox interrupt */
  31. writel(kvm->arch.clear_intr, kvm->arch.backend_intr_reg + 0x4);
  32. #endif
  33. if (kvm_riscv_vcpu_notify())
  34. wake_up(&kvm->arch.waitq);
  35. return IRQ_HANDLED;
  36. }
  37. u64 khv_reserved_memory = (u64)-1;
  38. u64 khv_reserved_memory_size = (u64)-1;
  39. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
  40. {
  41. kvm->arch.khv_base_phys_addr = 0x1fd000;
  42. kvm->arch.khv_base = (unsigned long)ioremap(kvm->arch.khv_base_phys_addr, 0x100);
  43. memset((void *)kvm->arch.khv_base, 0, 0x100);
  44. kvm->arch.io_switch = (struct khv_io *)kvm->arch.khv_base;
  45. #ifdef CONFIG_SOC_INT_SRC7
  46. kvm->arch.backend_intr_reg = ioremap(0xFFFF019094, 4);
  47. kvm->arch.frontend_intr_reg = ioremap(0xFFEF018094, 4);
  48. kvm_arch_guest_intr_reg = kvm->arch.frontend_intr_reg;
  49. #else
  50. /* MPW use mailbox as interrupt */
  51. kvm->arch.backend_intr_reg = ioremap(0xffffc3b000, 0x100);
  52. kvm->arch.frontend_intr_reg = ioremap(0xffefc50000, 0x100);
  53. kvm->arch.enable_intr = 1;
  54. kvm->arch.clear_intr = 1;
  55. writel(kvm->arch.enable_intr, kvm->arch.frontend_intr_reg + 0xc);
  56. writel(kvm->arch.clear_intr, kvm->arch.frontend_intr_reg + 0x4);
  57. kvm_arch_guest_intr_reg = kvm->arch.frontend_intr_reg + 0x10;
  58. #endif
  59. init_waitqueue_head(&kvm->arch.waitq);
  60. if (request_irq(kvm_arch_irq, kvm_arch_interrupt, IRQF_SHARED,
  61. "khv-interrupt", kvm) < 0) {
  62. pr_err("kvm backend: register IRQ %d failed\n", kvm_arch_irq);
  63. return -EINVAL;
  64. }
  65. return 0;
  66. }
  67. void kvm_arch_destroy_vm(struct kvm *kvm)
  68. {
  69. int i;
  70. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  71. if (kvm->vcpus[i]) {
  72. kvm_arch_vcpu_destroy(kvm->vcpus[i]);
  73. kvm->vcpus[i] = NULL;
  74. }
  75. }
  76. }
  77. int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  78. {
  79. int r;
  80. switch (ext) {
  81. case KVM_CAP_IOEVENTFD:
  82. case KVM_CAP_DEVICE_CTRL:
  83. case KVM_CAP_USER_MEMORY:
  84. case KVM_CAP_SYNC_MMU:
  85. case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
  86. case KVM_CAP_ONE_REG:
  87. case KVM_CAP_READONLY_MEM:
  88. case KVM_CAP_MP_STATE:
  89. case KVM_CAP_IMMEDIATE_EXIT:
  90. r = 1;
  91. break;
  92. case KVM_CAP_NR_VCPUS:
  93. r = 0xffff;
  94. break;
  95. case KVM_CAP_MAX_VCPUS:
  96. r = KVM_MAX_VCPUS;
  97. break;
  98. case KVM_CAP_NR_MEMSLOTS:
  99. r = KVM_USER_MEM_SLOTS;
  100. break;
  101. default:
  102. r = 0;
  103. break;
  104. }
  105. return r;
  106. }
  107. long kvm_arch_vm_ioctl(struct file *filp,
  108. unsigned int ioctl, unsigned long arg)
  109. {
  110. return 0;
  111. }
  112. static int khv_probe(struct platform_device *pdev)
  113. {
  114. struct device_node *np;
  115. struct resource r;
  116. struct device *dev;
  117. int rc;
  118. dev = &pdev->dev;
  119. kvm_arch_irq = platform_get_irq(pdev, 0);
  120. if (kvm_arch_irq <= 0) {
  121. pr_err("Cannot get IRQ resource for kvm backend\n");
  122. return -EINVAL;
  123. }
  124. np = of_parse_phandle(dev->of_node, "memory-region", 0);
  125. if (!np) {
  126. goto out;
  127. }
  128. rc = of_address_to_resource(np, 0, &r);
  129. if (rc) {
  130. dev_err(dev, "No memory address assigned to the region\n");
  131. return -ENOMEM;
  132. }
  133. khv_reserved_memory = r.start;
  134. khv_reserved_memory_size = r.end - r.start + 1;
  135. out:
  136. printk("%s, %d, irq: %d.\n", __func__, __LINE__, kvm_arch_irq);
  137. return 0;
  138. }
  139. static int khv_remove(struct platform_device *pdev)
  140. {
  141. return 0;
  142. }
  143. static const struct of_device_id khv_of_table[] = {
  144. { .compatible = "thead,khv-host" },
  145. { }
  146. };
  147. MODULE_DEVICE_TABLE(of, khv_of_table);
  148. static struct platform_driver khv_driver = {
  149. .probe = khv_probe,
  150. .remove = khv_remove,
  151. .driver = {
  152. .name = "KVM-based Heterogeneous Virtualization",
  153. .of_match_table = khv_of_table,
  154. },
  155. };
  156. module_platform_driver(khv_driver);