iommu.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. /* check if iommu exists and in use */
  40. if (!domain)
  41. return 0;
  42. for (i = 0; i < npages; i++) {
  43. /* check if already mapped */
  44. if (iommu_iova_to_phys(domain, gfn_to_gpa(gfn)))
  45. continue;
  46. pfn = gfn_to_pfn(kvm, gfn);
  47. r = iommu_map_range(domain,
  48. gfn_to_gpa(gfn),
  49. pfn_to_hpa(pfn),
  50. PAGE_SIZE,
  51. IOMMU_READ | IOMMU_WRITE);
  52. if (r) {
  53. printk(KERN_ERR "kvm_iommu_map_address:"
  54. "iommu failed to map pfn=%lx\n", pfn);
  55. goto unmap_pages;
  56. }
  57. gfn++;
  58. }
  59. return 0;
  60. unmap_pages:
  61. kvm_iommu_put_pages(kvm, base_gfn, i);
  62. return r;
  63. }
  64. static int kvm_iommu_map_memslots(struct kvm *kvm)
  65. {
  66. int i, r = 0;
  67. for (i = 0; i < kvm->nmemslots; i++) {
  68. r = kvm_iommu_map_pages(kvm, kvm->memslots[i].base_gfn,
  69. kvm->memslots[i].npages);
  70. if (r)
  71. break;
  72. }
  73. return r;
  74. }
  75. int kvm_assign_device(struct kvm *kvm,
  76. struct kvm_assigned_dev_kernel *assigned_dev)
  77. {
  78. struct pci_dev *pdev = NULL;
  79. struct iommu_domain *domain = kvm->arch.iommu_domain;
  80. int r;
  81. /* check if iommu exists and in use */
  82. if (!domain)
  83. return 0;
  84. pdev = assigned_dev->dev;
  85. if (pdev == NULL)
  86. return -ENODEV;
  87. r = iommu_attach_device(domain, &pdev->dev);
  88. if (r) {
  89. printk(KERN_ERR "assign device %x:%x.%x failed",
  90. pdev->bus->number,
  91. PCI_SLOT(pdev->devfn),
  92. PCI_FUNC(pdev->devfn));
  93. return r;
  94. }
  95. printk(KERN_DEBUG "assign device: 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. return 0;
  100. }
  101. int kvm_deassign_device(struct kvm *kvm,
  102. struct kvm_assigned_dev_kernel *assigned_dev)
  103. {
  104. struct iommu_domain *domain = kvm->arch.iommu_domain;
  105. struct pci_dev *pdev = NULL;
  106. /* check if iommu exists and in use */
  107. if (!domain)
  108. return 0;
  109. pdev = assigned_dev->dev;
  110. if (pdev == NULL)
  111. return -ENODEV;
  112. iommu_detach_device(domain, &pdev->dev);
  113. printk(KERN_DEBUG "deassign device: host bdf = %x:%x:%x\n",
  114. assigned_dev->host_busnr,
  115. PCI_SLOT(assigned_dev->host_devfn),
  116. PCI_FUNC(assigned_dev->host_devfn));
  117. return 0;
  118. }
  119. int kvm_iommu_map_guest(struct kvm *kvm)
  120. {
  121. int r;
  122. if (!iommu_found()) {
  123. printk(KERN_ERR "%s: iommu not found\n", __func__);
  124. return -ENODEV;
  125. }
  126. kvm->arch.iommu_domain = iommu_domain_alloc();
  127. if (!kvm->arch.iommu_domain)
  128. return -ENOMEM;
  129. r = kvm_iommu_map_memslots(kvm);
  130. if (r)
  131. goto out_unmap;
  132. return 0;
  133. out_unmap:
  134. kvm_iommu_unmap_memslots(kvm);
  135. return r;
  136. }
  137. static void kvm_iommu_put_pages(struct kvm *kvm,
  138. gfn_t base_gfn, unsigned long npages)
  139. {
  140. gfn_t gfn = base_gfn;
  141. pfn_t pfn;
  142. struct iommu_domain *domain = kvm->arch.iommu_domain;
  143. unsigned long i;
  144. u64 phys;
  145. /* check if iommu exists and in use */
  146. if (!domain)
  147. return;
  148. for (i = 0; i < npages; i++) {
  149. phys = iommu_iova_to_phys(domain, gfn_to_gpa(gfn));
  150. pfn = phys >> PAGE_SHIFT;
  151. kvm_release_pfn_clean(pfn);
  152. gfn++;
  153. }
  154. iommu_unmap_range(domain, gfn_to_gpa(base_gfn), PAGE_SIZE * npages);
  155. }
  156. static int kvm_iommu_unmap_memslots(struct kvm *kvm)
  157. {
  158. int i;
  159. for (i = 0; i < kvm->nmemslots; i++) {
  160. kvm_iommu_put_pages(kvm, kvm->memslots[i].base_gfn,
  161. kvm->memslots[i].npages);
  162. }
  163. return 0;
  164. }
  165. int kvm_iommu_unmap_guest(struct kvm *kvm)
  166. {
  167. struct iommu_domain *domain = kvm->arch.iommu_domain;
  168. /* check if iommu exists and in use */
  169. if (!domain)
  170. return 0;
  171. kvm_iommu_unmap_memslots(kvm);
  172. iommu_domain_free(domain);
  173. return 0;
  174. }