book3s_64_vio.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 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. long npages;
  89. int ret = -ENOMEM;
  90. int i;
  91. /* Check this LIOBN hasn't been previously allocated */
  92. list_for_each_entry(stt, &kvm->arch.spapr_tce_tables, list) {
  93. if (stt->liobn == args->liobn)
  94. return -EBUSY;
  95. }
  96. npages = kvmppc_stt_npages(args->window_size);
  97. stt = kzalloc(sizeof(*stt) + npages * sizeof(struct page *),
  98. GFP_KERNEL);
  99. if (!stt)
  100. goto fail;
  101. stt->liobn = args->liobn;
  102. stt->window_size = args->window_size;
  103. stt->kvm = kvm;
  104. for (i = 0; i < npages; i++) {
  105. stt->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
  106. if (!stt->pages[i])
  107. goto fail;
  108. }
  109. kvm_get_kvm(kvm);
  110. mutex_lock(&kvm->lock);
  111. list_add(&stt->list, &kvm->arch.spapr_tce_tables);
  112. mutex_unlock(&kvm->lock);
  113. return anon_inode_getfd("kvm-spapr-tce", &kvm_spapr_tce_fops,
  114. stt, O_RDWR);
  115. fail:
  116. if (stt) {
  117. for (i = 0; i < npages; i++)
  118. if (stt->pages[i])
  119. __free_page(stt->pages[i]);
  120. kfree(stt);
  121. }
  122. return ret;
  123. }