pci-dma_64.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Dynamic DMA mapping support.
  3. */
  4. #include <linux/types.h>
  5. #include <linux/mm.h>
  6. #include <linux/string.h>
  7. #include <linux/pci.h>
  8. #include <linux/module.h>
  9. #include <linux/dmar.h>
  10. #include <linux/bootmem.h>
  11. #include <asm/proto.h>
  12. #include <asm/io.h>
  13. #include <asm/gart.h>
  14. #include <asm/calgary.h>
  15. int iommu_merge __read_mostly = 0;
  16. dma_addr_t bad_dma_address __read_mostly;
  17. EXPORT_SYMBOL(bad_dma_address);
  18. /* This tells the BIO block layer to assume merging. Default to off
  19. because we cannot guarantee merging later. */
  20. int iommu_bio_merge __read_mostly = 0;
  21. EXPORT_SYMBOL(iommu_bio_merge);
  22. static int iommu_sac_force __read_mostly = 0;
  23. int no_iommu __read_mostly;
  24. #ifdef CONFIG_IOMMU_DEBUG
  25. int panic_on_overflow __read_mostly = 1;
  26. int force_iommu __read_mostly = 1;
  27. #else
  28. int panic_on_overflow __read_mostly = 0;
  29. int force_iommu __read_mostly= 0;
  30. #endif
  31. /* Set this to 1 if there is a HW IOMMU in the system */
  32. int iommu_detected __read_mostly = 0;
  33. /* Dummy device used for NULL arguments (normally ISA). Better would
  34. be probably a smaller DMA mask, but this is bug-to-bug compatible
  35. to i386. */
  36. struct device fallback_dev = {
  37. .bus_id = "fallback device",
  38. .coherent_dma_mask = DMA_32BIT_MASK,
  39. .dma_mask = &fallback_dev.coherent_dma_mask,
  40. };
  41. /* Allocate DMA memory on node near device */
  42. noinline static void *
  43. dma_alloc_pages(struct device *dev, gfp_t gfp, unsigned order)
  44. {
  45. struct page *page;
  46. int node;
  47. node = dev_to_node(dev);
  48. page = alloc_pages_node(node, gfp, order);
  49. return page ? page_address(page) : NULL;
  50. }
  51. /*
  52. * Allocate memory for a coherent mapping.
  53. */
  54. void *
  55. dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
  56. gfp_t gfp)
  57. {
  58. void *memory;
  59. unsigned long dma_mask = 0;
  60. u64 bus;
  61. if (!dev)
  62. dev = &fallback_dev;
  63. dma_mask = dev->coherent_dma_mask;
  64. if (dma_mask == 0)
  65. dma_mask = DMA_32BIT_MASK;
  66. /* Device not DMA able */
  67. if (dev->dma_mask == NULL)
  68. return NULL;
  69. /* Don't invoke OOM killer */
  70. gfp |= __GFP_NORETRY;
  71. /* Kludge to make it bug-to-bug compatible with i386. i386
  72. uses the normal dma_mask for alloc_coherent. */
  73. dma_mask &= *dev->dma_mask;
  74. /* Why <=? Even when the mask is smaller than 4GB it is often
  75. larger than 16MB and in this case we have a chance of
  76. finding fitting memory in the next higher zone first. If
  77. not retry with true GFP_DMA. -AK */
  78. if (dma_mask <= DMA_32BIT_MASK)
  79. gfp |= GFP_DMA32;
  80. again:
  81. memory = dma_alloc_pages(dev, gfp, get_order(size));
  82. if (memory == NULL)
  83. return NULL;
  84. {
  85. int high, mmu;
  86. bus = virt_to_bus(memory);
  87. high = (bus + size) >= dma_mask;
  88. mmu = high;
  89. if (force_iommu && !(gfp & GFP_DMA))
  90. mmu = 1;
  91. else if (high) {
  92. free_pages((unsigned long)memory,
  93. get_order(size));
  94. /* Don't use the 16MB ZONE_DMA unless absolutely
  95. needed. It's better to use remapping first. */
  96. if (dma_mask < DMA_32BIT_MASK && !(gfp & GFP_DMA)) {
  97. gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
  98. goto again;
  99. }
  100. /* Let low level make its own zone decisions */
  101. gfp &= ~(GFP_DMA32|GFP_DMA);
  102. if (dma_ops->alloc_coherent)
  103. return dma_ops->alloc_coherent(dev, size,
  104. dma_handle, gfp);
  105. return NULL;
  106. }
  107. memset(memory, 0, size);
  108. if (!mmu) {
  109. *dma_handle = virt_to_bus(memory);
  110. return memory;
  111. }
  112. }
  113. if (dma_ops->alloc_coherent) {
  114. free_pages((unsigned long)memory, get_order(size));
  115. gfp &= ~(GFP_DMA|GFP_DMA32);
  116. return dma_ops->alloc_coherent(dev, size, dma_handle, gfp);
  117. }
  118. if (dma_ops->map_simple) {
  119. *dma_handle = dma_ops->map_simple(dev, virt_to_phys(memory),
  120. size,
  121. PCI_DMA_BIDIRECTIONAL);
  122. if (*dma_handle != bad_dma_address)
  123. return memory;
  124. }
  125. if (panic_on_overflow)
  126. panic("dma_alloc_coherent: IOMMU overflow by %lu bytes\n",size);
  127. free_pages((unsigned long)memory, get_order(size));
  128. return NULL;
  129. }
  130. EXPORT_SYMBOL(dma_alloc_coherent);
  131. /*
  132. * Unmap coherent memory.
  133. * The caller must ensure that the device has finished accessing the mapping.
  134. */
  135. void dma_free_coherent(struct device *dev, size_t size,
  136. void *vaddr, dma_addr_t bus)
  137. {
  138. WARN_ON(irqs_disabled()); /* for portability */
  139. if (dma_ops->unmap_single)
  140. dma_ops->unmap_single(dev, bus, size, 0);
  141. free_pages((unsigned long)vaddr, get_order(size));
  142. }
  143. EXPORT_SYMBOL(dma_free_coherent);
  144. static int forbid_dac __read_mostly;
  145. int dma_supported(struct device *dev, u64 mask)
  146. {
  147. #ifdef CONFIG_PCI
  148. if (mask > 0xffffffff && forbid_dac > 0) {
  149. printk(KERN_INFO "PCI: Disallowing DAC for device %s\n", dev->bus_id);
  150. return 0;
  151. }
  152. #endif
  153. if (dma_ops->dma_supported)
  154. return dma_ops->dma_supported(dev, mask);
  155. /* Copied from i386. Doesn't make much sense, because it will
  156. only work for pci_alloc_coherent.
  157. The caller just has to use GFP_DMA in this case. */
  158. if (mask < DMA_24BIT_MASK)
  159. return 0;
  160. /* Tell the device to use SAC when IOMMU force is on. This
  161. allows the driver to use cheaper accesses in some cases.
  162. Problem with this is that if we overflow the IOMMU area and
  163. return DAC as fallback address the device may not handle it
  164. correctly.
  165. As a special case some controllers have a 39bit address
  166. mode that is as efficient as 32bit (aic79xx). Don't force
  167. SAC for these. Assume all masks <= 40 bits are of this
  168. type. Normally this doesn't make any difference, but gives
  169. more gentle handling of IOMMU overflow. */
  170. if (iommu_sac_force && (mask >= DMA_40BIT_MASK)) {
  171. printk(KERN_INFO "%s: Force SAC with mask %Lx\n", dev->bus_id,mask);
  172. return 0;
  173. }
  174. return 1;
  175. }
  176. EXPORT_SYMBOL(dma_supported);
  177. int dma_set_mask(struct device *dev, u64 mask)
  178. {
  179. if (!dev->dma_mask || !dma_supported(dev, mask))
  180. return -EIO;
  181. *dev->dma_mask = mask;
  182. return 0;
  183. }
  184. EXPORT_SYMBOL(dma_set_mask);
  185. /*
  186. * See <Documentation/x86_64/boot-options.txt> for the iommu kernel parameter
  187. * documentation.
  188. */
  189. static __init int iommu_setup(char *p)
  190. {
  191. iommu_merge = 1;
  192. if (!p)
  193. return -EINVAL;
  194. while (*p) {
  195. if (!strncmp(p, "off", 3))
  196. no_iommu = 1;
  197. /* gart_parse_options has more force support */
  198. if (!strncmp(p, "force", 5))
  199. force_iommu = 1;
  200. if (!strncmp(p, "noforce", 7)) {
  201. iommu_merge = 0;
  202. force_iommu = 0;
  203. }
  204. if (!strncmp(p, "biomerge", 8)) {
  205. iommu_bio_merge = 4096;
  206. iommu_merge = 1;
  207. force_iommu = 1;
  208. }
  209. if (!strncmp(p, "panic", 5))
  210. panic_on_overflow = 1;
  211. if (!strncmp(p, "nopanic", 7))
  212. panic_on_overflow = 0;
  213. if (!strncmp(p, "merge", 5)) {
  214. iommu_merge = 1;
  215. force_iommu = 1;
  216. }
  217. if (!strncmp(p, "nomerge", 7))
  218. iommu_merge = 0;
  219. if (!strncmp(p, "forcesac", 8))
  220. iommu_sac_force = 1;
  221. if (!strncmp(p, "allowdac", 8))
  222. forbid_dac = 0;
  223. if (!strncmp(p, "nodac", 5))
  224. forbid_dac = -1;
  225. #ifdef CONFIG_SWIOTLB
  226. if (!strncmp(p, "soft", 4))
  227. swiotlb = 1;
  228. #endif
  229. #ifdef CONFIG_GART_IOMMU
  230. gart_parse_options(p);
  231. #endif
  232. #ifdef CONFIG_CALGARY_IOMMU
  233. if (!strncmp(p, "calgary", 7))
  234. use_calgary = 1;
  235. #endif /* CONFIG_CALGARY_IOMMU */
  236. p += strcspn(p, ",");
  237. if (*p == ',')
  238. ++p;
  239. }
  240. return 0;
  241. }
  242. early_param("iommu", iommu_setup);
  243. static __initdata void *dma32_bootmem_ptr;
  244. static unsigned long dma32_bootmem_size __initdata = (128ULL<<20);
  245. static int __init parse_dma32_size_opt(char *p)
  246. {
  247. if (!p)
  248. return -EINVAL;
  249. dma32_bootmem_size = memparse(p, &p);
  250. return 0;
  251. }
  252. early_param("dma32_size", parse_dma32_size_opt);
  253. void __init dma32_reserve_bootmem(void)
  254. {
  255. unsigned long size, align;
  256. if (end_pfn <= MAX_DMA32_PFN)
  257. return;
  258. align = 64ULL<<20;
  259. size = round_up(dma32_bootmem_size, align);
  260. dma32_bootmem_ptr = __alloc_bootmem_nopanic(size, align,
  261. __pa(MAX_DMA_ADDRESS));
  262. if (dma32_bootmem_ptr)
  263. dma32_bootmem_size = size;
  264. else
  265. dma32_bootmem_size = 0;
  266. }
  267. static void __init dma32_free_bootmem(void)
  268. {
  269. int node;
  270. if (end_pfn <= MAX_DMA32_PFN)
  271. return;
  272. if (!dma32_bootmem_ptr)
  273. return;
  274. for_each_online_node(node)
  275. free_bootmem_node(NODE_DATA(node), __pa(dma32_bootmem_ptr),
  276. dma32_bootmem_size);
  277. dma32_bootmem_ptr = NULL;
  278. dma32_bootmem_size = 0;
  279. }
  280. void __init pci_iommu_alloc(void)
  281. {
  282. /* free the range so iommu could get some range less than 4G */
  283. dma32_free_bootmem();
  284. /*
  285. * The order of these functions is important for
  286. * fall-back/fail-over reasons
  287. */
  288. #ifdef CONFIG_GART_IOMMU
  289. gart_iommu_hole_init();
  290. #endif
  291. #ifdef CONFIG_CALGARY_IOMMU
  292. detect_calgary();
  293. #endif
  294. detect_intel_iommu();
  295. #ifdef CONFIG_SWIOTLB
  296. pci_swiotlb_init();
  297. #endif
  298. }
  299. static int __init pci_iommu_init(void)
  300. {
  301. #ifdef CONFIG_CALGARY_IOMMU
  302. calgary_iommu_init();
  303. #endif
  304. intel_iommu_init();
  305. #ifdef CONFIG_GART_IOMMU
  306. gart_iommu_init();
  307. #endif
  308. no_iommu_init();
  309. return 0;
  310. }
  311. void pci_iommu_shutdown(void)
  312. {
  313. gart_iommu_shutdown();
  314. }
  315. #ifdef CONFIG_PCI
  316. /* Many VIA bridges seem to corrupt data for DAC. Disable it here */
  317. static __devinit void via_no_dac(struct pci_dev *dev)
  318. {
  319. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI && forbid_dac == 0) {
  320. printk(KERN_INFO "PCI: VIA PCI bridge detected. Disabling DAC.\n");
  321. forbid_dac = 1;
  322. }
  323. }
  324. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID, via_no_dac);
  325. #endif
  326. /* Must execute after PCI subsystem */
  327. fs_initcall(pci_iommu_init);