pci-dma.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include <linux/dma-mapping.h>
  2. #include <linux/dmar.h>
  3. #include <linux/bootmem.h>
  4. #include <linux/pci.h>
  5. #include <asm/proto.h>
  6. #include <asm/dma.h>
  7. #include <asm/gart.h>
  8. #include <asm/calgary.h>
  9. int forbid_dac __read_mostly;
  10. EXPORT_SYMBOL(forbid_dac);
  11. const struct dma_mapping_ops *dma_ops;
  12. EXPORT_SYMBOL(dma_ops);
  13. int iommu_sac_force __read_mostly = 0;
  14. #ifdef CONFIG_IOMMU_DEBUG
  15. int panic_on_overflow __read_mostly = 1;
  16. int force_iommu __read_mostly = 1;
  17. #else
  18. int panic_on_overflow __read_mostly = 0;
  19. int force_iommu __read_mostly = 0;
  20. #endif
  21. int dma_set_mask(struct device *dev, u64 mask)
  22. {
  23. if (!dev->dma_mask || !dma_supported(dev, mask))
  24. return -EIO;
  25. *dev->dma_mask = mask;
  26. return 0;
  27. }
  28. EXPORT_SYMBOL(dma_set_mask);
  29. #ifdef CONFIG_X86_64
  30. static __initdata void *dma32_bootmem_ptr;
  31. static unsigned long dma32_bootmem_size __initdata = (128ULL<<20);
  32. static int __init parse_dma32_size_opt(char *p)
  33. {
  34. if (!p)
  35. return -EINVAL;
  36. dma32_bootmem_size = memparse(p, &p);
  37. return 0;
  38. }
  39. early_param("dma32_size", parse_dma32_size_opt);
  40. void __init dma32_reserve_bootmem(void)
  41. {
  42. unsigned long size, align;
  43. if (end_pfn <= MAX_DMA32_PFN)
  44. return;
  45. align = 64ULL<<20;
  46. size = round_up(dma32_bootmem_size, align);
  47. dma32_bootmem_ptr = __alloc_bootmem_nopanic(size, align,
  48. __pa(MAX_DMA_ADDRESS));
  49. if (dma32_bootmem_ptr)
  50. dma32_bootmem_size = size;
  51. else
  52. dma32_bootmem_size = 0;
  53. }
  54. static void __init dma32_free_bootmem(void)
  55. {
  56. int node;
  57. if (end_pfn <= MAX_DMA32_PFN)
  58. return;
  59. if (!dma32_bootmem_ptr)
  60. return;
  61. for_each_online_node(node)
  62. free_bootmem_node(NODE_DATA(node), __pa(dma32_bootmem_ptr),
  63. dma32_bootmem_size);
  64. dma32_bootmem_ptr = NULL;
  65. dma32_bootmem_size = 0;
  66. }
  67. void __init pci_iommu_alloc(void)
  68. {
  69. /* free the range so iommu could get some range less than 4G */
  70. dma32_free_bootmem();
  71. /*
  72. * The order of these functions is important for
  73. * fall-back/fail-over reasons
  74. */
  75. #ifdef CONFIG_GART_IOMMU
  76. gart_iommu_hole_init();
  77. #endif
  78. #ifdef CONFIG_CALGARY_IOMMU
  79. detect_calgary();
  80. #endif
  81. detect_intel_iommu();
  82. #ifdef CONFIG_SWIOTLB
  83. pci_swiotlb_init();
  84. #endif
  85. }
  86. #endif
  87. int dma_supported(struct device *dev, u64 mask)
  88. {
  89. #ifdef CONFIG_PCI
  90. if (mask > 0xffffffff && forbid_dac > 0) {
  91. printk(KERN_INFO "PCI: Disallowing DAC for device %s\n",
  92. dev->bus_id);
  93. return 0;
  94. }
  95. #endif
  96. if (dma_ops->dma_supported)
  97. return dma_ops->dma_supported(dev, mask);
  98. /* Copied from i386. Doesn't make much sense, because it will
  99. only work for pci_alloc_coherent.
  100. The caller just has to use GFP_DMA in this case. */
  101. if (mask < DMA_24BIT_MASK)
  102. return 0;
  103. /* Tell the device to use SAC when IOMMU force is on. This
  104. allows the driver to use cheaper accesses in some cases.
  105. Problem with this is that if we overflow the IOMMU area and
  106. return DAC as fallback address the device may not handle it
  107. correctly.
  108. As a special case some controllers have a 39bit address
  109. mode that is as efficient as 32bit (aic79xx). Don't force
  110. SAC for these. Assume all masks <= 40 bits are of this
  111. type. Normally this doesn't make any difference, but gives
  112. more gentle handling of IOMMU overflow. */
  113. if (iommu_sac_force && (mask >= DMA_40BIT_MASK)) {
  114. printk(KERN_INFO "%s: Force SAC with mask %Lx\n",
  115. dev->bus_id, mask);
  116. return 0;
  117. }
  118. return 1;
  119. }
  120. EXPORT_SYMBOL(dma_supported);
  121. static int __init pci_iommu_init(void)
  122. {
  123. #ifdef CONFIG_CALGARY_IOMMU
  124. calgary_iommu_init();
  125. #endif
  126. intel_iommu_init();
  127. #ifdef CONFIG_GART_IOMMU
  128. gart_iommu_init();
  129. #endif
  130. no_iommu_init();
  131. return 0;
  132. }
  133. void pci_iommu_shutdown(void)
  134. {
  135. gart_iommu_shutdown();
  136. }
  137. /* Must execute after PCI subsystem */
  138. fs_initcall(pci_iommu_init);
  139. #ifdef CONFIG_PCI
  140. /* Many VIA bridges seem to corrupt data for DAC. Disable it here */
  141. static __devinit void via_no_dac(struct pci_dev *dev)
  142. {
  143. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI && forbid_dac == 0) {
  144. printk(KERN_INFO "PCI: VIA PCI bridge detected."
  145. "Disabling DAC.\n");
  146. forbid_dac = 1;
  147. }
  148. }
  149. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID, via_no_dac);
  150. #endif