iommu.c 8.0 KB

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