iommu.c 5.3 KB

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