iommu.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/iommu.h>
  28. #include <linux/intel-iommu.h>
  29. static int kvm_iommu_unmap_memslots(struct kvm *kvm);
  30. static void kvm_iommu_put_pages(struct kvm *kvm,
  31. gfn_t base_gfn, unsigned long npages);
  32. int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot)
  33. {
  34. gfn_t gfn = slot->base_gfn;
  35. unsigned long npages = slot->npages;
  36. pfn_t pfn;
  37. int i, r = 0;
  38. struct iommu_domain *domain = kvm->arch.iommu_domain;
  39. int flags;
  40. /* check if iommu exists and in use */
  41. if (!domain)
  42. return 0;
  43. flags = IOMMU_READ | IOMMU_WRITE;
  44. if (kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY)
  45. flags |= IOMMU_CACHE;
  46. for (i = 0; i < npages; i++) {
  47. /* check if already mapped */
  48. if (iommu_iova_to_phys(domain, gfn_to_gpa(gfn)))
  49. continue;
  50. pfn = gfn_to_pfn_memslot(kvm, slot, gfn);
  51. r = iommu_map_range(domain,
  52. gfn_to_gpa(gfn),
  53. pfn_to_hpa(pfn),
  54. PAGE_SIZE, flags);
  55. if (r) {
  56. printk(KERN_ERR "kvm_iommu_map_address:"
  57. "iommu failed to map pfn=%lx\n", pfn);
  58. goto unmap_pages;
  59. }
  60. gfn++;
  61. }
  62. return 0;
  63. unmap_pages:
  64. kvm_iommu_put_pages(kvm, slot->base_gfn, i);
  65. return r;
  66. }
  67. static int kvm_iommu_map_memslots(struct kvm *kvm)
  68. {
  69. int i, r = 0;
  70. struct kvm_memslots *slots;
  71. slots = rcu_dereference(kvm->memslots);
  72. for (i = 0; i < slots->nmemslots; i++) {
  73. r = kvm_iommu_map_pages(kvm, &slots->memslots[i]);
  74. if (r)
  75. break;
  76. }
  77. return r;
  78. }
  79. int kvm_assign_device(struct kvm *kvm,
  80. struct kvm_assigned_dev_kernel *assigned_dev)
  81. {
  82. struct pci_dev *pdev = NULL;
  83. struct iommu_domain *domain = kvm->arch.iommu_domain;
  84. int r, last_flags;
  85. /* check if iommu exists and in use */
  86. if (!domain)
  87. return 0;
  88. pdev = assigned_dev->dev;
  89. if (pdev == NULL)
  90. return -ENODEV;
  91. r = iommu_attach_device(domain, &pdev->dev);
  92. if (r) {
  93. printk(KERN_ERR "assign device %x:%x:%x.%x failed",
  94. pci_domain_nr(pdev->bus),
  95. pdev->bus->number,
  96. PCI_SLOT(pdev->devfn),
  97. PCI_FUNC(pdev->devfn));
  98. return r;
  99. }
  100. last_flags = kvm->arch.iommu_flags;
  101. if (iommu_domain_has_cap(kvm->arch.iommu_domain,
  102. IOMMU_CAP_CACHE_COHERENCY))
  103. kvm->arch.iommu_flags |= KVM_IOMMU_CACHE_COHERENCY;
  104. /* Check if need to update IOMMU page table for guest memory */
  105. if ((last_flags ^ kvm->arch.iommu_flags) ==
  106. KVM_IOMMU_CACHE_COHERENCY) {
  107. kvm_iommu_unmap_memslots(kvm);
  108. r = kvm_iommu_map_memslots(kvm);
  109. if (r)
  110. goto out_unmap;
  111. }
  112. printk(KERN_DEBUG "assign device %x:%x:%x.%x\n",
  113. assigned_dev->host_segnr,
  114. assigned_dev->host_busnr,
  115. PCI_SLOT(assigned_dev->host_devfn),
  116. PCI_FUNC(assigned_dev->host_devfn));
  117. return 0;
  118. out_unmap:
  119. kvm_iommu_unmap_memslots(kvm);
  120. return r;
  121. }
  122. int kvm_deassign_device(struct kvm *kvm,
  123. struct kvm_assigned_dev_kernel *assigned_dev)
  124. {
  125. struct iommu_domain *domain = kvm->arch.iommu_domain;
  126. struct pci_dev *pdev = NULL;
  127. /* check if iommu exists and in use */
  128. if (!domain)
  129. return 0;
  130. pdev = assigned_dev->dev;
  131. if (pdev == NULL)
  132. return -ENODEV;
  133. iommu_detach_device(domain, &pdev->dev);
  134. printk(KERN_DEBUG "deassign device %x:%x:%x.%x\n",
  135. assigned_dev->host_segnr,
  136. assigned_dev->host_busnr,
  137. PCI_SLOT(assigned_dev->host_devfn),
  138. PCI_FUNC(assigned_dev->host_devfn));
  139. return 0;
  140. }
  141. int kvm_iommu_map_guest(struct kvm *kvm)
  142. {
  143. int r;
  144. if (!iommu_found()) {
  145. printk(KERN_ERR "%s: iommu not found\n", __func__);
  146. return -ENODEV;
  147. }
  148. kvm->arch.iommu_domain = iommu_domain_alloc();
  149. if (!kvm->arch.iommu_domain)
  150. return -ENOMEM;
  151. r = kvm_iommu_map_memslots(kvm);
  152. if (r)
  153. goto out_unmap;
  154. return 0;
  155. out_unmap:
  156. kvm_iommu_unmap_memslots(kvm);
  157. return r;
  158. }
  159. static void kvm_iommu_put_pages(struct kvm *kvm,
  160. gfn_t base_gfn, unsigned long npages)
  161. {
  162. gfn_t gfn = base_gfn;
  163. pfn_t pfn;
  164. struct iommu_domain *domain = kvm->arch.iommu_domain;
  165. unsigned long i;
  166. u64 phys;
  167. /* check if iommu exists and in use */
  168. if (!domain)
  169. return;
  170. for (i = 0; i < npages; i++) {
  171. phys = iommu_iova_to_phys(domain, gfn_to_gpa(gfn));
  172. pfn = phys >> PAGE_SHIFT;
  173. kvm_release_pfn_clean(pfn);
  174. gfn++;
  175. }
  176. iommu_unmap_range(domain, gfn_to_gpa(base_gfn), PAGE_SIZE * npages);
  177. }
  178. static int kvm_iommu_unmap_memslots(struct kvm *kvm)
  179. {
  180. int i;
  181. struct kvm_memslots *slots;
  182. slots = rcu_dereference(kvm->memslots);
  183. for (i = 0; i < slots->nmemslots; i++) {
  184. kvm_iommu_put_pages(kvm, slots->memslots[i].base_gfn,
  185. slots->memslots[i].npages);
  186. }
  187. return 0;
  188. }
  189. int kvm_iommu_unmap_guest(struct kvm *kvm)
  190. {
  191. struct iommu_domain *domain = kvm->arch.iommu_domain;
  192. /* check if iommu exists and in use */
  193. if (!domain)
  194. return 0;
  195. kvm_iommu_unmap_memslots(kvm);
  196. iommu_domain_free(domain);
  197. return 0;
  198. }