iommu.c 8.2 KB

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