pci-swiotlb.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. static 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. vaddr = dma_generic_alloc_coherent(hwdev, size, dma_handle, flags,
  20. attrs);
  21. if (vaddr)
  22. return vaddr;
  23. return swiotlb_alloc_coherent(hwdev, size, dma_handle, flags);
  24. }
  25. static void x86_swiotlb_free_coherent(struct device *dev, size_t size,
  26. void *vaddr, dma_addr_t dma_addr,
  27. struct dma_attrs *attrs)
  28. {
  29. swiotlb_free_coherent(dev, size, vaddr, dma_addr);
  30. }
  31. static struct dma_map_ops swiotlb_dma_ops = {
  32. .mapping_error = swiotlb_dma_mapping_error,
  33. .alloc = x86_swiotlb_alloc_coherent,
  34. .free = x86_swiotlb_free_coherent,
  35. .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
  36. .sync_single_for_device = swiotlb_sync_single_for_device,
  37. .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
  38. .sync_sg_for_device = swiotlb_sync_sg_for_device,
  39. .map_sg = swiotlb_map_sg_attrs,
  40. .unmap_sg = swiotlb_unmap_sg_attrs,
  41. .map_page = swiotlb_map_page,
  42. .unmap_page = swiotlb_unmap_page,
  43. .dma_supported = NULL,
  44. };
  45. /*
  46. * pci_swiotlb_detect_override - set swiotlb to 1 if necessary
  47. *
  48. * This returns non-zero if we are forced to use swiotlb (by the boot
  49. * option).
  50. */
  51. int __init pci_swiotlb_detect_override(void)
  52. {
  53. int use_swiotlb = swiotlb | swiotlb_force;
  54. if (swiotlb_force)
  55. swiotlb = 1;
  56. return use_swiotlb;
  57. }
  58. IOMMU_INIT_FINISH(pci_swiotlb_detect_override,
  59. pci_xen_swiotlb_detect,
  60. pci_swiotlb_init,
  61. pci_swiotlb_late_init);
  62. /*
  63. * if 4GB or more detected (and iommu=off not set) return 1
  64. * and set swiotlb to 1.
  65. */
  66. int __init pci_swiotlb_detect_4gb(void)
  67. {
  68. /* don't initialize swiotlb if iommu=off (no_iommu=1) */
  69. #ifdef CONFIG_X86_64
  70. if (!no_iommu && max_pfn > MAX_DMA32_PFN)
  71. swiotlb = 1;
  72. #endif
  73. return swiotlb;
  74. }
  75. IOMMU_INIT(pci_swiotlb_detect_4gb,
  76. pci_swiotlb_detect_override,
  77. pci_swiotlb_init,
  78. pci_swiotlb_late_init);
  79. void __init pci_swiotlb_init(void)
  80. {
  81. if (swiotlb) {
  82. swiotlb_init(0);
  83. dma_ops = &swiotlb_dma_ops;
  84. }
  85. }
  86. void __init pci_swiotlb_late_init(void)
  87. {
  88. /* An IOMMU turned us off. */
  89. if (!swiotlb)
  90. swiotlb_free();
  91. else {
  92. printk(KERN_INFO "PCI-DMA: "
  93. "Using software bounce buffering for IO (SWIOTLB)\n");
  94. swiotlb_print_info();
  95. }
  96. }