pci-swiotlb_64.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. int swiotlb __read_mostly;
  12. void * __init swiotlb_alloc_boot(size_t size, unsigned long nslabs)
  13. {
  14. return alloc_bootmem_low_pages(size);
  15. }
  16. void *swiotlb_alloc(unsigned order, unsigned long nslabs)
  17. {
  18. return (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN, order);
  19. }
  20. dma_addr_t swiotlb_phys_to_bus(struct device *hwdev, phys_addr_t paddr)
  21. {
  22. return paddr;
  23. }
  24. phys_addr_t swiotlb_bus_to_phys(dma_addr_t baddr)
  25. {
  26. return baddr;
  27. }
  28. int __weak swiotlb_arch_range_needs_mapping(void *ptr, size_t size)
  29. {
  30. return 0;
  31. }
  32. /* these will be moved to lib/swiotlb.c later on */
  33. static dma_addr_t swiotlb_map_page(struct device *dev, struct page *page,
  34. unsigned long offset, size_t size,
  35. enum dma_data_direction dir,
  36. struct dma_attrs *attrs)
  37. {
  38. return swiotlb_map_single(dev, page_address(page) + offset, size, dir);
  39. }
  40. static void swiotlb_unmap_page(struct device *dev, dma_addr_t dma_handle,
  41. size_t size, enum dma_data_direction dir,
  42. struct dma_attrs *attrs)
  43. {
  44. swiotlb_unmap_single(dev, dma_handle, size, dir);
  45. }
  46. static void *x86_swiotlb_alloc_coherent(struct device *hwdev, size_t size,
  47. dma_addr_t *dma_handle, gfp_t flags)
  48. {
  49. void *vaddr;
  50. vaddr = dma_generic_alloc_coherent(hwdev, size, dma_handle, flags);
  51. if (vaddr)
  52. return vaddr;
  53. return swiotlb_alloc_coherent(hwdev, size, dma_handle, flags);
  54. }
  55. struct dma_map_ops swiotlb_dma_ops = {
  56. .mapping_error = swiotlb_dma_mapping_error,
  57. .alloc_coherent = x86_swiotlb_alloc_coherent,
  58. .free_coherent = swiotlb_free_coherent,
  59. .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
  60. .sync_single_for_device = swiotlb_sync_single_for_device,
  61. .sync_single_range_for_cpu = swiotlb_sync_single_range_for_cpu,
  62. .sync_single_range_for_device = swiotlb_sync_single_range_for_device,
  63. .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
  64. .sync_sg_for_device = swiotlb_sync_sg_for_device,
  65. .map_sg = swiotlb_map_sg_attrs,
  66. .unmap_sg = swiotlb_unmap_sg_attrs,
  67. .map_page = swiotlb_map_page,
  68. .unmap_page = swiotlb_unmap_page,
  69. .dma_supported = NULL,
  70. };
  71. void __init pci_swiotlb_init(void)
  72. {
  73. /* don't initialize swiotlb if iommu=off (no_iommu=1) */
  74. #ifdef CONFIG_X86_64
  75. if (!iommu_detected && !no_iommu && max_pfn > MAX_DMA32_PFN)
  76. swiotlb = 1;
  77. #endif
  78. if (swiotlb_force)
  79. swiotlb = 1;
  80. if (swiotlb) {
  81. printk(KERN_INFO "PCI-DMA: Using software bounce buffering for IO (SWIOTLB)\n");
  82. swiotlb_init();
  83. dma_ops = &swiotlb_dma_ops;
  84. }
  85. }