iommu.c 7.8 KB

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