book3s_64_vio.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  16. * Copyright 2011 David Gibson, IBM Corporation <dwg@au1.ibm.com>
  17. */
  18. #include <linux/types.h>
  19. #include <linux/string.h>
  20. #include <linux/kvm.h>
  21. #include <linux/kvm_host.h>
  22. #include <linux/highmem.h>
  23. #include <linux/gfp.h>
  24. #include <linux/slab.h>
  25. #include <linux/hugetlb.h>
  26. #include <linux/list.h>
  27. #include <linux/anon_inodes.h>
  28. #include <asm/tlbflush.h>
  29. #include <asm/kvm_ppc.h>
  30. #include <asm/kvm_book3s.h>
  31. #include <asm/mmu-hash64.h>
  32. #include <asm/hvcall.h>
  33. #include <asm/synch.h>
  34. #include <asm/ppc-opcode.h>
  35. #include <asm/kvm_host.h>
  36. #include <asm/udbg.h>
  37. #define TCES_PER_PAGE (PAGE_SIZE / sizeof(u64))
  38. static long kvmppc_stt_npages(unsigned long window_size)
  39. {
  40. return ALIGN((window_size >> SPAPR_TCE_SHIFT)
  41. * sizeof(u64), PAGE_SIZE) / PAGE_SIZE;
  42. }
  43. static void release_spapr_tce_table(struct kvmppc_spapr_tce_table *stt)
  44. {
  45. struct kvm *kvm = stt->kvm;
  46. int i;
  47. mutex_lock(&kvm->lock);
  48. list_del(&stt->list);
  49. for (i = 0; i < kvmppc_stt_npages(stt->window_size); i++)
  50. __free_page(stt->pages[i]);
  51. kfree(stt);
  52. mutex_unlock(&kvm->lock);
  53. kvm_put_kvm(kvm);
  54. }
  55. static int kvm_spapr_tce_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  56. {
  57. struct kvmppc_spapr_tce_table *stt = vma->vm_file->private_data;
  58. struct page *page;
  59. if (vmf->pgoff >= kvmppc_stt_npages(stt->window_size))
  60. return VM_FAULT_SIGBUS;
  61. page = stt->pages[vmf->pgoff];
  62. get_page(page);
  63. vmf->page = page;
  64. return 0;
  65. }
  66. static const struct vm_operations_struct kvm_spapr_tce_vm_ops = {
  67. .fault = kvm_spapr_tce_fault,
  68. };
  69. static int kvm_spapr_tce_mmap(struct file *file, struct vm_area_struct *vma)
  70. {
  71. vma->vm_ops = &kvm_spapr_tce_vm_ops;
  72. return 0;
  73. }
  74. static int kvm_spapr_tce_release(struct inode *inode, struct file *filp)
  75. {
  76. struct kvmppc_spapr_tce_table *stt = filp->private_data;
  77. release_spapr_tce_table(stt);
  78. return 0;
  79. }
  80. static const struct file_operations kvm_spapr_tce_fops = {
  81. .mmap = kvm_spapr_tce_mmap,
  82. .release = kvm_spapr_tce_release,
  83. };
  84. long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
  85. struct kvm_create_spapr_tce *args)
  86. {
  87. struct kvmppc_spapr_tce_table *stt = NULL;
  88. struct kvmppc_spapr_tce_table *siter;
  89. long npages;
  90. int ret = -ENOMEM;
  91. int i;
  92. npages = kvmppc_stt_npages(args->window_size);
  93. stt = kzalloc(sizeof(*stt) + npages * sizeof(struct page *),
  94. GFP_KERNEL);
  95. if (!stt)
  96. return ret;
  97. stt->liobn = args->liobn;
  98. stt->window_size = args->window_size;
  99. stt->kvm = kvm;
  100. for (i = 0; i < npages; i++) {
  101. stt->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
  102. if (!stt->pages[i])
  103. goto fail;
  104. }
  105. mutex_lock(&kvm->lock);
  106. /* Check this LIOBN hasn't been previously allocated */
  107. ret = 0;
  108. list_for_each_entry(siter, &kvm->arch.spapr_tce_tables, list) {
  109. if (siter->liobn == args->liobn) {
  110. ret = -EBUSY;
  111. break;
  112. }
  113. }
  114. if (!ret)
  115. ret = anon_inode_getfd("kvm-spapr-tce", &kvm_spapr_tce_fops,
  116. stt, O_RDWR | O_CLOEXEC);
  117. if (ret >= 0) {
  118. list_add(&stt->list, &kvm->arch.spapr_tce_tables);
  119. kvm_get_kvm(kvm);
  120. }
  121. mutex_unlock(&kvm->lock);
  122. if (ret >= 0)
  123. return ret;
  124. fail:
  125. for (i = 0; i < npages; i++)
  126. if (stt->pages[i])
  127. __free_page(stt->pages[i]);
  128. kfree(stt);
  129. return ret;
  130. }