iommu.c 8.5 KB

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