iommu.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 = 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 failed",
  94. pdev->bus->number,
  95. PCI_SLOT(pdev->devfn),
  96. PCI_FUNC(pdev->devfn));
  97. return r;
  98. }
  99. last_flags = kvm->arch.iommu_flags;
  100. if (iommu_domain_has_cap(kvm->arch.iommu_domain,
  101. IOMMU_CAP_CACHE_COHERENCY))
  102. kvm->arch.iommu_flags |= KVM_IOMMU_CACHE_COHERENCY;
  103. /* Check if need to update IOMMU page table for guest memory */
  104. if ((last_flags ^ kvm->arch.iommu_flags) ==
  105. KVM_IOMMU_CACHE_COHERENCY) {
  106. kvm_iommu_unmap_memslots(kvm);
  107. r = kvm_iommu_map_memslots(kvm);
  108. if (r)
  109. goto out_unmap;
  110. }
  111. printk(KERN_DEBUG "assign device: host bdf = %x:%x:%x\n",
  112. assigned_dev->host_busnr,
  113. PCI_SLOT(assigned_dev->host_devfn),
  114. PCI_FUNC(assigned_dev->host_devfn));
  115. return 0;
  116. out_unmap:
  117. kvm_iommu_unmap_memslots(kvm);
  118. return r;
  119. }
  120. int kvm_deassign_device(struct kvm *kvm,
  121. struct kvm_assigned_dev_kernel *assigned_dev)
  122. {
  123. struct iommu_domain *domain = kvm->arch.iommu_domain;
  124. struct pci_dev *pdev = NULL;
  125. /* check if iommu exists and in use */
  126. if (!domain)
  127. return 0;
  128. pdev = assigned_dev->dev;
  129. if (pdev == NULL)
  130. return -ENODEV;
  131. iommu_detach_device(domain, &pdev->dev);
  132. printk(KERN_DEBUG "deassign device: host bdf = %x:%x:%x\n",
  133. assigned_dev->host_busnr,
  134. PCI_SLOT(assigned_dev->host_devfn),
  135. PCI_FUNC(assigned_dev->host_devfn));
  136. return 0;
  137. }
  138. int kvm_iommu_map_guest(struct kvm *kvm)
  139. {
  140. int r;
  141. if (!iommu_found()) {
  142. printk(KERN_ERR "%s: iommu not found\n", __func__);
  143. return -ENODEV;
  144. }
  145. kvm->arch.iommu_domain = iommu_domain_alloc();
  146. if (!kvm->arch.iommu_domain)
  147. return -ENOMEM;
  148. r = kvm_iommu_map_memslots(kvm);
  149. if (r)
  150. goto out_unmap;
  151. return 0;
  152. out_unmap:
  153. kvm_iommu_unmap_memslots(kvm);
  154. return r;
  155. }
  156. static void kvm_iommu_put_pages(struct kvm *kvm,
  157. gfn_t base_gfn, unsigned long npages)
  158. {
  159. gfn_t gfn = base_gfn;
  160. pfn_t pfn;
  161. struct iommu_domain *domain = kvm->arch.iommu_domain;
  162. unsigned long i;
  163. u64 phys;
  164. /* check if iommu exists and in use */
  165. if (!domain)
  166. return;
  167. for (i = 0; i < npages; i++) {
  168. phys = iommu_iova_to_phys(domain, gfn_to_gpa(gfn));
  169. pfn = phys >> PAGE_SHIFT;
  170. kvm_release_pfn_clean(pfn);
  171. gfn++;
  172. }
  173. iommu_unmap_range(domain, gfn_to_gpa(base_gfn), PAGE_SIZE * npages);
  174. }
  175. static int kvm_iommu_unmap_memslots(struct kvm *kvm)
  176. {
  177. int i;
  178. struct kvm_memslots *slots;
  179. slots = kvm->memslots;
  180. for (i = 0; i < slots->nmemslots; i++) {
  181. kvm_iommu_put_pages(kvm, slots->memslots[i].base_gfn,
  182. slots->memslots[i].npages);
  183. }
  184. return 0;
  185. }
  186. int kvm_iommu_unmap_guest(struct kvm *kvm)
  187. {
  188. struct iommu_domain *domain = kvm->arch.iommu_domain;
  189. /* check if iommu exists and in use */
  190. if (!domain)
  191. return 0;
  192. kvm_iommu_unmap_memslots(kvm);
  193. iommu_domain_free(domain);
  194. return 0;
  195. }