iommu.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. pdev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED;
  155. printk(KERN_DEBUG "assign device %x:%x:%x.%x\n",
  156. assigned_dev->host_segnr,
  157. assigned_dev->host_busnr,
  158. PCI_SLOT(assigned_dev->host_devfn),
  159. PCI_FUNC(assigned_dev->host_devfn));
  160. return 0;
  161. out_unmap:
  162. kvm_iommu_unmap_memslots(kvm);
  163. return r;
  164. }
  165. int kvm_deassign_device(struct kvm *kvm,
  166. struct kvm_assigned_dev_kernel *assigned_dev)
  167. {
  168. struct iommu_domain *domain = kvm->arch.iommu_domain;
  169. struct pci_dev *pdev = NULL;
  170. /* check if iommu exists and in use */
  171. if (!domain)
  172. return 0;
  173. pdev = assigned_dev->dev;
  174. if (pdev == NULL)
  175. return -ENODEV;
  176. iommu_detach_device(domain, &pdev->dev);
  177. pdev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED;
  178. printk(KERN_DEBUG "deassign device %x:%x:%x.%x\n",
  179. assigned_dev->host_segnr,
  180. assigned_dev->host_busnr,
  181. PCI_SLOT(assigned_dev->host_devfn),
  182. PCI_FUNC(assigned_dev->host_devfn));
  183. return 0;
  184. }
  185. int kvm_iommu_map_guest(struct kvm *kvm)
  186. {
  187. int r;
  188. if (!iommu_present(&pci_bus_type)) {
  189. printk(KERN_ERR "%s: iommu not found\n", __func__);
  190. return -ENODEV;
  191. }
  192. kvm->arch.iommu_domain = iommu_domain_alloc(&pci_bus_type);
  193. if (!kvm->arch.iommu_domain)
  194. return -ENOMEM;
  195. if (!allow_unsafe_assigned_interrupts &&
  196. !iommu_domain_has_cap(kvm->arch.iommu_domain,
  197. IOMMU_CAP_INTR_REMAP)) {
  198. printk(KERN_WARNING "%s: No interrupt remapping support,"
  199. " disallowing device assignment."
  200. " Re-enble with \"allow_unsafe_assigned_interrupts=1\""
  201. " module option.\n", __func__);
  202. iommu_domain_free(kvm->arch.iommu_domain);
  203. kvm->arch.iommu_domain = NULL;
  204. return -EPERM;
  205. }
  206. r = kvm_iommu_map_memslots(kvm);
  207. if (r)
  208. goto out_unmap;
  209. return 0;
  210. out_unmap:
  211. kvm_iommu_unmap_memslots(kvm);
  212. return r;
  213. }
  214. static void kvm_unpin_pages(struct kvm *kvm, pfn_t pfn, unsigned long npages)
  215. {
  216. unsigned long i;
  217. for (i = 0; i < npages; ++i)
  218. kvm_release_pfn_clean(pfn + i);
  219. }
  220. static void kvm_iommu_put_pages(struct kvm *kvm,
  221. gfn_t base_gfn, unsigned long npages)
  222. {
  223. struct iommu_domain *domain;
  224. gfn_t end_gfn, gfn;
  225. pfn_t pfn;
  226. u64 phys;
  227. domain = kvm->arch.iommu_domain;
  228. end_gfn = base_gfn + npages;
  229. gfn = base_gfn;
  230. /* check if iommu exists and in use */
  231. if (!domain)
  232. return;
  233. while (gfn < end_gfn) {
  234. unsigned long unmap_pages;
  235. int order;
  236. /* Get physical address */
  237. phys = iommu_iova_to_phys(domain, gfn_to_gpa(gfn));
  238. pfn = phys >> PAGE_SHIFT;
  239. /* Unmap address from IO address space */
  240. order = iommu_unmap(domain, gfn_to_gpa(gfn), 0);
  241. unmap_pages = 1ULL << order;
  242. /* Unpin all pages we just unmapped to not leak any memory */
  243. kvm_unpin_pages(kvm, pfn, unmap_pages);
  244. gfn += unmap_pages;
  245. }
  246. }
  247. static int kvm_iommu_unmap_memslots(struct kvm *kvm)
  248. {
  249. int i, idx;
  250. struct kvm_memslots *slots;
  251. idx = srcu_read_lock(&kvm->srcu);
  252. slots = kvm_memslots(kvm);
  253. for (i = 0; i < slots->nmemslots; i++) {
  254. kvm_iommu_put_pages(kvm, slots->memslots[i].base_gfn,
  255. slots->memslots[i].npages);
  256. }
  257. srcu_read_unlock(&kvm->srcu, idx);
  258. return 0;
  259. }
  260. int kvm_iommu_unmap_guest(struct kvm *kvm)
  261. {
  262. struct iommu_domain *domain = kvm->arch.iommu_domain;
  263. /* check if iommu exists and in use */
  264. if (!domain)
  265. return 0;
  266. kvm_iommu_unmap_memslots(kvm);
  267. iommu_domain_free(domain);
  268. return 0;
  269. }