vtd.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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;
  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. r = -EINVAL;
  42. for (i = 0; i < npages; i++) {
  43. /* check if already mapped */
  44. pfn = (pfn_t)intel_iommu_iova_to_pfn(domain,
  45. gfn_to_gpa(gfn));
  46. if (pfn && !is_mmio_pfn(pfn))
  47. continue;
  48. pfn = gfn_to_pfn(kvm, gfn);
  49. if (!is_mmio_pfn(pfn)) {
  50. r = intel_iommu_page_mapping(domain,
  51. gfn_to_gpa(gfn),
  52. pfn_to_hpa(pfn),
  53. PAGE_SIZE,
  54. DMA_PTE_READ |
  55. DMA_PTE_WRITE);
  56. if (r) {
  57. printk(KERN_DEBUG "kvm_iommu_map_pages:"
  58. "iommu failed to map pfn=%lx\n", pfn);
  59. goto unmap_pages;
  60. }
  61. } else {
  62. printk(KERN_DEBUG "kvm_iommu_map_page:"
  63. "invalid pfn=%lx\n", pfn);
  64. goto unmap_pages;
  65. }
  66. gfn++;
  67. }
  68. return 0;
  69. unmap_pages:
  70. kvm_iommu_put_pages(kvm, base_gfn, i);
  71. return r;
  72. }
  73. static int kvm_iommu_map_memslots(struct kvm *kvm)
  74. {
  75. int i, r;
  76. down_read(&kvm->slots_lock);
  77. for (i = 0; i < kvm->nmemslots; i++) {
  78. r = kvm_iommu_map_pages(kvm, kvm->memslots[i].base_gfn,
  79. kvm->memslots[i].npages);
  80. if (r)
  81. break;
  82. }
  83. up_read(&kvm->slots_lock);
  84. return r;
  85. }
  86. int kvm_iommu_map_guest(struct kvm *kvm,
  87. struct kvm_assigned_dev_kernel *assigned_dev)
  88. {
  89. struct pci_dev *pdev = NULL;
  90. int r;
  91. if (!intel_iommu_found()) {
  92. printk(KERN_ERR "%s: intel iommu not found\n", __func__);
  93. return -ENODEV;
  94. }
  95. printk(KERN_DEBUG "VT-d direct map: host bdf = %x:%x:%x\n",
  96. assigned_dev->host_busnr,
  97. PCI_SLOT(assigned_dev->host_devfn),
  98. PCI_FUNC(assigned_dev->host_devfn));
  99. pdev = assigned_dev->dev;
  100. if (pdev == NULL) {
  101. if (kvm->arch.intel_iommu_domain) {
  102. intel_iommu_domain_exit(kvm->arch.intel_iommu_domain);
  103. kvm->arch.intel_iommu_domain = NULL;
  104. }
  105. return -ENODEV;
  106. }
  107. kvm->arch.intel_iommu_domain = intel_iommu_domain_alloc(pdev);
  108. if (!kvm->arch.intel_iommu_domain)
  109. return -ENODEV;
  110. r = kvm_iommu_map_memslots(kvm);
  111. if (r)
  112. goto out_unmap;
  113. intel_iommu_detach_dev(kvm->arch.intel_iommu_domain,
  114. pdev->bus->number, pdev->devfn);
  115. r = intel_iommu_context_mapping(kvm->arch.intel_iommu_domain,
  116. pdev);
  117. if (r) {
  118. printk(KERN_ERR "Domain context map for %s failed",
  119. pci_name(pdev));
  120. goto out_unmap;
  121. }
  122. return 0;
  123. out_unmap:
  124. kvm_iommu_unmap_memslots(kvm);
  125. return r;
  126. }
  127. static void kvm_iommu_put_pages(struct kvm *kvm,
  128. gfn_t base_gfn, unsigned long npages)
  129. {
  130. gfn_t gfn = base_gfn;
  131. pfn_t pfn;
  132. struct dmar_domain *domain = kvm->arch.intel_iommu_domain;
  133. int i;
  134. for (i = 0; i < npages; i++) {
  135. pfn = (pfn_t)intel_iommu_iova_to_pfn(domain,
  136. gfn_to_gpa(gfn));
  137. kvm_release_pfn_clean(pfn);
  138. gfn++;
  139. }
  140. }
  141. static int kvm_iommu_unmap_memslots(struct kvm *kvm)
  142. {
  143. int i;
  144. down_read(&kvm->slots_lock);
  145. for (i = 0; i < kvm->nmemslots; i++) {
  146. kvm_iommu_put_pages(kvm, kvm->memslots[i].base_gfn,
  147. kvm->memslots[i].npages);
  148. }
  149. up_read(&kvm->slots_lock);
  150. return 0;
  151. }
  152. int kvm_iommu_unmap_guest(struct kvm *kvm)
  153. {
  154. struct kvm_assigned_dev_kernel *entry;
  155. struct dmar_domain *domain = kvm->arch.intel_iommu_domain;
  156. /* check if iommu exists and in use */
  157. if (!domain)
  158. return 0;
  159. list_for_each_entry(entry, &kvm->arch.assigned_dev_head, list) {
  160. printk(KERN_DEBUG "VT-d unmap: host bdf = %x:%x:%x\n",
  161. entry->host_busnr,
  162. PCI_SLOT(entry->host_devfn),
  163. PCI_FUNC(entry->host_devfn));
  164. /* detach kvm dmar domain */
  165. intel_iommu_detach_dev(domain, entry->host_busnr,
  166. entry->host_devfn);
  167. }
  168. kvm_iommu_unmap_memslots(kvm);
  169. intel_iommu_domain_exit(domain);
  170. return 0;
  171. }