iommu.c 8.0 KB

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