pci-swiotlb.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Glue code to lib/swiotlb.c */
  2. #include <linux/pci.h>
  3. #include <linux/cache.h>
  4. #include <linux/module.h>
  5. #include <linux/swiotlb.h>
  6. #include <linux/bootmem.h>
  7. #include <linux/dma-mapping.h>
  8. #include <asm/iommu.h>
  9. #include <asm/swiotlb.h>
  10. #include <asm/dma.h>
  11. #include <asm/xen/swiotlb-xen.h>
  12. #include <asm/iommu_table.h>
  13. int swiotlb __read_mostly;
  14. void *x86_swiotlb_alloc_coherent(struct device *hwdev, size_t size,
  15. dma_addr_t *dma_handle, gfp_t flags,
  16. struct dma_attrs *attrs)
  17. {
  18. void *vaddr;
  19. /*
  20. * Don't print a warning when the first allocation attempt fails.
  21. * swiotlb_alloc_coherent() will print a warning when the DMA
  22. * memory allocation ultimately failed.
  23. */
  24. flags |= __GFP_NOWARN;
  25. vaddr = dma_generic_alloc_coherent(hwdev, size, dma_handle, flags,
  26. attrs);
  27. if (vaddr)
  28. return vaddr;
  29. return swiotlb_alloc_coherent(hwdev, size, dma_handle, flags);
  30. }
  31. void x86_swiotlb_free_coherent(struct device *dev, size_t size,
  32. void *vaddr, dma_addr_t dma_addr,
  33. struct dma_attrs *attrs)
  34. {
  35. if (is_swiotlb_buffer(dma_to_phys(dev, dma_addr)))
  36. swiotlb_free_coherent(dev, size, vaddr, dma_addr);
  37. else
  38. dma_generic_free_coherent(dev, size, vaddr, dma_addr, attrs);
  39. }
  40. static struct dma_map_ops swiotlb_dma_ops = {
  41. .mapping_error = swiotlb_dma_mapping_error,
  42. .alloc = x86_swiotlb_alloc_coherent,
  43. .free = x86_swiotlb_free_coherent,
  44. .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
  45. .sync_single_for_device = swiotlb_sync_single_for_device,
  46. .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
  47. .sync_sg_for_device = swiotlb_sync_sg_for_device,
  48. .map_sg = swiotlb_map_sg_attrs,
  49. .unmap_sg = swiotlb_unmap_sg_attrs,
  50. .map_page = swiotlb_map_page,
  51. .unmap_page = swiotlb_unmap_page,
  52. .dma_supported = NULL,
  53. };
  54. /*
  55. * pci_swiotlb_detect_override - set swiotlb to 1 if necessary
  56. *
  57. * This returns non-zero if we are forced to use swiotlb (by the boot
  58. * option).
  59. */
  60. int __init pci_swiotlb_detect_override(void)
  61. {
  62. int use_swiotlb = swiotlb | swiotlb_force;
  63. if (swiotlb_force)
  64. swiotlb = 1;
  65. return use_swiotlb;
  66. }
  67. IOMMU_INIT_FINISH(pci_swiotlb_detect_override,
  68. pci_xen_swiotlb_detect,
  69. pci_swiotlb_init,
  70. pci_swiotlb_late_init);
  71. /*
  72. * if 4GB or more detected (and iommu=off not set) return 1
  73. * and set swiotlb to 1.
  74. */
  75. int __init pci_swiotlb_detect_4gb(void)
  76. {
  77. /* don't initialize swiotlb if iommu=off (no_iommu=1) */
  78. #ifdef CONFIG_X86_64
  79. if (!no_iommu && max_pfn > MAX_DMA32_PFN)
  80. swiotlb = 1;
  81. #endif
  82. return swiotlb;
  83. }
  84. IOMMU_INIT(pci_swiotlb_detect_4gb,
  85. pci_swiotlb_detect_override,
  86. pci_swiotlb_init,
  87. pci_swiotlb_late_init);
  88. void __init pci_swiotlb_init(void)
  89. {
  90. if (swiotlb) {
  91. swiotlb_init(0);
  92. dma_ops = &swiotlb_dma_ops;
  93. }
  94. }
  95. void __init pci_swiotlb_late_init(void)
  96. {
  97. /* An IOMMU turned us off. */
  98. if (!swiotlb)
  99. swiotlb_free();
  100. else {
  101. printk(KERN_INFO "PCI-DMA: "
  102. "Using software bounce buffering for IO (SWIOTLB)\n");
  103. swiotlb_print_info();
  104. }
  105. }