iommu.c 8.2 KB

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