iommu.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. 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].base_gfn,
  74. slots->memslots[i].npages);
  75. if (r)
  76. break;
  77. }
  78. return r;
  79. }
  80. int kvm_assign_device(struct kvm *kvm,
  81. struct kvm_assigned_dev_kernel *assigned_dev)
  82. {
  83. struct pci_dev *pdev = NULL;
  84. struct iommu_domain *domain = kvm->arch.iommu_domain;
  85. int r, last_flags;
  86. /* check if iommu exists and in use */
  87. if (!domain)
  88. return 0;
  89. pdev = assigned_dev->dev;
  90. if (pdev == NULL)
  91. return -ENODEV;
  92. r = iommu_attach_device(domain, &pdev->dev);
  93. if (r) {
  94. printk(KERN_ERR "assign device %x:%x.%x failed",
  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: host bdf = %x:%x:%x\n",
  113. assigned_dev->host_busnr,
  114. PCI_SLOT(assigned_dev->host_devfn),
  115. PCI_FUNC(assigned_dev->host_devfn));
  116. return 0;
  117. out_unmap:
  118. kvm_iommu_unmap_memslots(kvm);
  119. return r;
  120. }
  121. int kvm_deassign_device(struct kvm *kvm,
  122. struct kvm_assigned_dev_kernel *assigned_dev)
  123. {
  124. struct iommu_domain *domain = kvm->arch.iommu_domain;
  125. struct pci_dev *pdev = NULL;
  126. /* check if iommu exists and in use */
  127. if (!domain)
  128. return 0;
  129. pdev = assigned_dev->dev;
  130. if (pdev == NULL)
  131. return -ENODEV;
  132. iommu_detach_device(domain, &pdev->dev);
  133. printk(KERN_DEBUG "deassign device: host bdf = %x:%x:%x\n",
  134. assigned_dev->host_busnr,
  135. PCI_SLOT(assigned_dev->host_devfn),
  136. PCI_FUNC(assigned_dev->host_devfn));
  137. return 0;
  138. }
  139. int kvm_iommu_map_guest(struct kvm *kvm)
  140. {
  141. int r;
  142. if (!iommu_found()) {
  143. printk(KERN_ERR "%s: iommu not found\n", __func__);
  144. return -ENODEV;
  145. }
  146. kvm->arch.iommu_domain = iommu_domain_alloc();
  147. if (!kvm->arch.iommu_domain)
  148. return -ENOMEM;
  149. r = kvm_iommu_map_memslots(kvm);
  150. if (r)
  151. goto out_unmap;
  152. return 0;
  153. out_unmap:
  154. kvm_iommu_unmap_memslots(kvm);
  155. return r;
  156. }
  157. static void kvm_iommu_put_pages(struct kvm *kvm,
  158. gfn_t base_gfn, unsigned long npages)
  159. {
  160. gfn_t gfn = base_gfn;
  161. pfn_t pfn;
  162. struct iommu_domain *domain = kvm->arch.iommu_domain;
  163. unsigned long i;
  164. u64 phys;
  165. /* check if iommu exists and in use */
  166. if (!domain)
  167. return;
  168. for (i = 0; i < npages; i++) {
  169. phys = iommu_iova_to_phys(domain, gfn_to_gpa(gfn));
  170. pfn = phys >> PAGE_SHIFT;
  171. kvm_release_pfn_clean(pfn);
  172. gfn++;
  173. }
  174. iommu_unmap_range(domain, gfn_to_gpa(base_gfn), PAGE_SIZE * npages);
  175. }
  176. static int kvm_iommu_unmap_memslots(struct kvm *kvm)
  177. {
  178. int i;
  179. struct kvm_memslots *slots;
  180. slots = kvm->memslots;
  181. for (i = 0; i < slots->nmemslots; i++) {
  182. kvm_iommu_put_pages(kvm, slots->memslots[i].base_gfn,
  183. slots->memslots[i].npages);
  184. }
  185. return 0;
  186. }
  187. int kvm_iommu_unmap_guest(struct kvm *kvm)
  188. {
  189. struct iommu_domain *domain = kvm->arch.iommu_domain;
  190. /* check if iommu exists and in use */
  191. if (!domain)
  192. return 0;
  193. kvm_iommu_unmap_memslots(kvm);
  194. iommu_domain_free(domain);
  195. return 0;
  196. }