iommu.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  20. *
  21. * Author: Allen M. Kay <allen.m.kay@intel.com>
  22. * Author: Weidong Han <weidong.han@intel.com>
  23. * Author: Ben-Ami Yassour <benami@il.ibm.com>
  24. */
  25. #include <linux/list.h>
  26. #include <linux/kvm_host.h>
  27. #include <linux/pci.h>
  28. #include <linux/dmar.h>
  29. #include <linux/iommu.h>
  30. #include <linux/intel-iommu.h>
  31. static int kvm_iommu_unmap_memslots(struct kvm *kvm);
  32. static void kvm_iommu_put_pages(struct kvm *kvm,
  33. gfn_t base_gfn, unsigned long npages);
  34. static pfn_t kvm_pin_pages(struct kvm *kvm, struct kvm_memory_slot *slot,
  35. gfn_t gfn, unsigned long size)
  36. {
  37. gfn_t end_gfn;
  38. pfn_t pfn;
  39. pfn = gfn_to_pfn_memslot(kvm, slot, gfn);
  40. end_gfn = gfn + (size >> PAGE_SHIFT);
  41. gfn += 1;
  42. if (is_error_pfn(pfn))
  43. return pfn;
  44. while (gfn < end_gfn)
  45. gfn_to_pfn_memslot(kvm, slot, gfn++);
  46. return pfn;
  47. }
  48. int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot)
  49. {
  50. gfn_t gfn, end_gfn;
  51. pfn_t pfn;
  52. int r = 0;
  53. struct iommu_domain *domain = kvm->arch.iommu_domain;
  54. int flags;
  55. /* check if iommu exists and in use */
  56. if (!domain)
  57. return 0;
  58. gfn = slot->base_gfn;
  59. end_gfn = gfn + slot->npages;
  60. flags = IOMMU_READ | IOMMU_WRITE;
  61. if (kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY)
  62. flags |= IOMMU_CACHE;
  63. while (gfn < end_gfn) {
  64. unsigned long page_size;
  65. /* Check if already mapped */
  66. if (iommu_iova_to_phys(domain, gfn_to_gpa(gfn))) {
  67. gfn += 1;
  68. continue;
  69. }
  70. /* Get the page size we could use to map */
  71. page_size = kvm_host_page_size(kvm, gfn);
  72. /* Make sure the page_size does not exceed the memslot */
  73. while ((gfn + (page_size >> PAGE_SHIFT)) > end_gfn)
  74. page_size >>= 1;
  75. /* Make sure gfn is aligned to the page size we want to map */
  76. while ((gfn << PAGE_SHIFT) & (page_size - 1))
  77. page_size >>= 1;
  78. /*
  79. * Pin all pages we are about to map in memory. This is
  80. * important because we unmap and unpin in 4kb steps later.
  81. */
  82. pfn = kvm_pin_pages(kvm, slot, gfn, page_size);
  83. if (is_error_pfn(pfn)) {
  84. gfn += 1;
  85. continue;
  86. }
  87. /* Map into IO address space */
  88. r = iommu_map(domain, gfn_to_gpa(gfn), pfn_to_hpa(pfn),
  89. get_order(page_size), flags);
  90. if (r) {
  91. printk(KERN_ERR "kvm_iommu_map_address:"
  92. "iommu failed to map pfn=%llx\n", pfn);
  93. goto unmap_pages;
  94. }
  95. gfn += page_size >> PAGE_SHIFT;
  96. }
  97. return 0;
  98. unmap_pages:
  99. kvm_iommu_put_pages(kvm, slot->base_gfn, gfn);
  100. return r;
  101. }
  102. static int kvm_iommu_map_memslots(struct kvm *kvm)
  103. {
  104. int i, idx, r = 0;
  105. struct kvm_memslots *slots;
  106. idx = srcu_read_lock(&kvm->srcu);
  107. slots = kvm_memslots(kvm);
  108. for (i = 0; i < slots->nmemslots; i++) {
  109. r = kvm_iommu_map_pages(kvm, &slots->memslots[i]);
  110. if (r)
  111. break;
  112. }
  113. srcu_read_unlock(&kvm->srcu, idx);
  114. return r;
  115. }
  116. int kvm_assign_device(struct kvm *kvm,
  117. struct kvm_assigned_dev_kernel *assigned_dev)
  118. {
  119. struct pci_dev *pdev = NULL;
  120. struct iommu_domain *domain = kvm->arch.iommu_domain;
  121. int r, last_flags;
  122. /* check if iommu exists and in use */
  123. if (!domain)
  124. return 0;
  125. pdev = assigned_dev->dev;
  126. if (pdev == NULL)
  127. return -ENODEV;
  128. r = iommu_attach_device(domain, &pdev->dev);
  129. if (r) {
  130. printk(KERN_ERR "assign device %x:%x:%x.%x failed",
  131. pci_domain_nr(pdev->bus),
  132. pdev->bus->number,
  133. PCI_SLOT(pdev->devfn),
  134. PCI_FUNC(pdev->devfn));
  135. return r;
  136. }
  137. last_flags = kvm->arch.iommu_flags;
  138. if (iommu_domain_has_cap(kvm->arch.iommu_domain,
  139. IOMMU_CAP_CACHE_COHERENCY))
  140. kvm->arch.iommu_flags |= KVM_IOMMU_CACHE_COHERENCY;
  141. /* Check if need to update IOMMU page table for guest memory */
  142. if ((last_flags ^ kvm->arch.iommu_flags) ==
  143. KVM_IOMMU_CACHE_COHERENCY) {
  144. kvm_iommu_unmap_memslots(kvm);
  145. r = kvm_iommu_map_memslots(kvm);
  146. if (r)
  147. goto out_unmap;
  148. }
  149. printk(KERN_DEBUG "assign device %x:%x:%x.%x\n",
  150. assigned_dev->host_segnr,
  151. assigned_dev->host_busnr,
  152. PCI_SLOT(assigned_dev->host_devfn),
  153. PCI_FUNC(assigned_dev->host_devfn));
  154. return 0;
  155. out_unmap:
  156. kvm_iommu_unmap_memslots(kvm);
  157. return r;
  158. }
  159. int kvm_deassign_device(struct kvm *kvm,
  160. struct kvm_assigned_dev_kernel *assigned_dev)
  161. {
  162. struct iommu_domain *domain = kvm->arch.iommu_domain;
  163. struct pci_dev *pdev = NULL;
  164. /* check if iommu exists and in use */
  165. if (!domain)
  166. return 0;
  167. pdev = assigned_dev->dev;
  168. if (pdev == NULL)
  169. return -ENODEV;
  170. iommu_detach_device(domain, &pdev->dev);
  171. printk(KERN_DEBUG "deassign device %x:%x:%x.%x\n",
  172. assigned_dev->host_segnr,
  173. assigned_dev->host_busnr,
  174. PCI_SLOT(assigned_dev->host_devfn),
  175. PCI_FUNC(assigned_dev->host_devfn));
  176. return 0;
  177. }
  178. int kvm_iommu_map_guest(struct kvm *kvm)
  179. {
  180. int r;
  181. if (!iommu_found()) {
  182. printk(KERN_ERR "%s: iommu not found\n", __func__);
  183. return -ENODEV;
  184. }
  185. kvm->arch.iommu_domain = iommu_domain_alloc();
  186. if (!kvm->arch.iommu_domain)
  187. return -ENOMEM;
  188. r = kvm_iommu_map_memslots(kvm);
  189. if (r)
  190. goto out_unmap;
  191. return 0;
  192. out_unmap:
  193. kvm_iommu_unmap_memslots(kvm);
  194. return r;
  195. }
  196. static void kvm_unpin_pages(struct kvm *kvm, pfn_t pfn, unsigned long npages)
  197. {
  198. unsigned long i;
  199. for (i = 0; i < npages; ++i)
  200. kvm_release_pfn_clean(pfn + i);
  201. }
  202. static void kvm_iommu_put_pages(struct kvm *kvm,
  203. gfn_t base_gfn, unsigned long npages)
  204. {
  205. struct iommu_domain *domain;
  206. gfn_t end_gfn, gfn;
  207. pfn_t pfn;
  208. u64 phys;
  209. domain = kvm->arch.iommu_domain;
  210. end_gfn = base_gfn + npages;
  211. gfn = base_gfn;
  212. /* check if iommu exists and in use */
  213. if (!domain)
  214. return;
  215. while (gfn < end_gfn) {
  216. unsigned long unmap_pages;
  217. int order;
  218. /* Get physical address */
  219. phys = iommu_iova_to_phys(domain, gfn_to_gpa(gfn));
  220. pfn = phys >> PAGE_SHIFT;
  221. /* Unmap address from IO address space */
  222. order = iommu_unmap(domain, gfn_to_gpa(gfn), 0);
  223. unmap_pages = 1ULL << order;
  224. /* Unpin all pages we just unmapped to not leak any memory */
  225. kvm_unpin_pages(kvm, pfn, unmap_pages);
  226. gfn += unmap_pages;
  227. }
  228. }
  229. void kvm_iommu_unmap_pages(struct kvm *kvm, struct kvm_memory_slot *slot)
  230. {
  231. kvm_iommu_put_pages(kvm, slot->base_gfn, slot->npages);
  232. }
  233. static int kvm_iommu_unmap_memslots(struct kvm *kvm)
  234. {
  235. int i, idx;
  236. struct kvm_memslots *slots;
  237. idx = srcu_read_lock(&kvm->srcu);
  238. slots = kvm_memslots(kvm);
  239. for (i = 0; i < slots->nmemslots; i++)
  240. kvm_iommu_unmap_pages(kvm, &slots->memslots[i]);
  241. srcu_read_unlock(&kvm->srcu, idx);
  242. return 0;
  243. }
  244. int kvm_iommu_unmap_guest(struct kvm *kvm)
  245. {
  246. struct iommu_domain *domain = kvm->arch.iommu_domain;
  247. /* check if iommu exists and in use */
  248. if (!domain)
  249. return 0;
  250. kvm_iommu_unmap_memslots(kvm);
  251. iommu_domain_free(domain);
  252. return 0;
  253. }