vtd.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2006, Intel Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  15. * Place - Suite 330, Boston, MA 02111-1307 USA.
  16. *
  17. * Copyright (C) 2006-2008 Intel Corporation
  18. * Copyright IBM Corporation, 2008
  19. * Author: Allen M. Kay <allen.m.kay@intel.com>
  20. * Author: Weidong Han <weidong.han@intel.com>
  21. * Author: Ben-Ami Yassour <benami@il.ibm.com>
  22. */
  23. #include <linux/list.h>
  24. #include <linux/kvm_host.h>
  25. #include <linux/pci.h>
  26. #include <linux/dmar.h>
  27. #include <linux/intel-iommu.h>
  28. static int kvm_iommu_unmap_memslots(struct kvm *kvm);
  29. static void kvm_iommu_put_pages(struct kvm *kvm,
  30. gfn_t base_gfn, unsigned long npages);
  31. int kvm_iommu_map_pages(struct kvm *kvm,
  32. gfn_t base_gfn, unsigned long npages)
  33. {
  34. gfn_t gfn = base_gfn;
  35. pfn_t pfn;
  36. int i, r = 0;
  37. struct dmar_domain *domain = kvm->arch.intel_iommu_domain;
  38. /* check if iommu exists and in use */
  39. if (!domain)
  40. return 0;
  41. for (i = 0; i < npages; i++) {
  42. /* check if already mapped */
  43. pfn = (pfn_t)intel_iommu_iova_to_pfn(domain,
  44. gfn_to_gpa(gfn));
  45. if (pfn)
  46. continue;
  47. pfn = gfn_to_pfn(kvm, gfn);
  48. r = intel_iommu_page_mapping(domain,
  49. gfn_to_gpa(gfn),
  50. pfn_to_hpa(pfn),
  51. PAGE_SIZE,
  52. DMA_PTE_READ |
  53. DMA_PTE_WRITE);
  54. if (r) {
  55. printk(KERN_ERR "kvm_iommu_map_pages:"
  56. "iommu failed to map pfn=%lx\n", pfn);
  57. goto unmap_pages;
  58. }
  59. gfn++;
  60. }
  61. return 0;
  62. unmap_pages:
  63. kvm_iommu_put_pages(kvm, base_gfn, i);
  64. return r;
  65. }
  66. static int kvm_iommu_map_memslots(struct kvm *kvm)
  67. {
  68. int i, r;
  69. down_read(&kvm->slots_lock);
  70. for (i = 0; i < kvm->nmemslots; i++) {
  71. r = kvm_iommu_map_pages(kvm, kvm->memslots[i].base_gfn,
  72. kvm->memslots[i].npages);
  73. if (r)
  74. break;
  75. }
  76. up_read(&kvm->slots_lock);
  77. return r;
  78. }
  79. int kvm_iommu_map_guest(struct kvm *kvm,
  80. struct kvm_assigned_dev_kernel *assigned_dev)
  81. {
  82. struct pci_dev *pdev = NULL;
  83. int r;
  84. if (!intel_iommu_found()) {
  85. printk(KERN_ERR "%s: intel iommu not found\n", __func__);
  86. return -ENODEV;
  87. }
  88. printk(KERN_DEBUG "VT-d direct map: host bdf = %x:%x:%x\n",
  89. assigned_dev->host_busnr,
  90. PCI_SLOT(assigned_dev->host_devfn),
  91. PCI_FUNC(assigned_dev->host_devfn));
  92. pdev = assigned_dev->dev;
  93. if (pdev == NULL) {
  94. if (kvm->arch.intel_iommu_domain) {
  95. intel_iommu_domain_exit(kvm->arch.intel_iommu_domain);
  96. kvm->arch.intel_iommu_domain = NULL;
  97. }
  98. return -ENODEV;
  99. }
  100. kvm->arch.intel_iommu_domain = intel_iommu_domain_alloc(pdev);
  101. if (!kvm->arch.intel_iommu_domain)
  102. return -ENODEV;
  103. r = kvm_iommu_map_memslots(kvm);
  104. if (r)
  105. goto out_unmap;
  106. intel_iommu_detach_dev(kvm->arch.intel_iommu_domain,
  107. pdev->bus->number, pdev->devfn);
  108. r = intel_iommu_context_mapping(kvm->arch.intel_iommu_domain,
  109. pdev);
  110. if (r) {
  111. printk(KERN_ERR "Domain context map for %s failed",
  112. pci_name(pdev));
  113. goto out_unmap;
  114. }
  115. return 0;
  116. out_unmap:
  117. kvm_iommu_unmap_memslots(kvm);
  118. return r;
  119. }
  120. static void kvm_iommu_put_pages(struct kvm *kvm,
  121. gfn_t base_gfn, unsigned long npages)
  122. {
  123. gfn_t gfn = base_gfn;
  124. pfn_t pfn;
  125. struct dmar_domain *domain = kvm->arch.intel_iommu_domain;
  126. int i;
  127. for (i = 0; i < npages; i++) {
  128. pfn = (pfn_t)intel_iommu_iova_to_pfn(domain,
  129. gfn_to_gpa(gfn));
  130. kvm_release_pfn_clean(pfn);
  131. gfn++;
  132. }
  133. }
  134. static int kvm_iommu_unmap_memslots(struct kvm *kvm)
  135. {
  136. int i;
  137. down_read(&kvm->slots_lock);
  138. for (i = 0; i < kvm->nmemslots; i++) {
  139. kvm_iommu_put_pages(kvm, kvm->memslots[i].base_gfn,
  140. kvm->memslots[i].npages);
  141. }
  142. up_read(&kvm->slots_lock);
  143. return 0;
  144. }
  145. int kvm_iommu_unmap_guest(struct kvm *kvm)
  146. {
  147. struct kvm_assigned_dev_kernel *entry;
  148. struct dmar_domain *domain = kvm->arch.intel_iommu_domain;
  149. /* check if iommu exists and in use */
  150. if (!domain)
  151. return 0;
  152. list_for_each_entry(entry, &kvm->arch.assigned_dev_head, list) {
  153. printk(KERN_DEBUG "VT-d unmap: host bdf = %x:%x:%x\n",
  154. entry->host_busnr,
  155. PCI_SLOT(entry->host_devfn),
  156. PCI_FUNC(entry->host_devfn));
  157. /* detach kvm dmar domain */
  158. intel_iommu_detach_dev(domain, entry->host_busnr,
  159. entry->host_devfn);
  160. }
  161. kvm_iommu_unmap_memslots(kvm);
  162. intel_iommu_domain_exit(domain);
  163. return 0;
  164. }