dma-swiotlb.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include <linux/mm.h>
  2. #include <linux/init.h>
  3. #include <linux/dma-mapping.h>
  4. #include <linux/scatterlist.h>
  5. #include <linux/swiotlb.h>
  6. #include <linux/bootmem.h>
  7. #include <asm/bootinfo.h>
  8. #include <boot_param.h>
  9. #include <dma-coherence.h>
  10. static void *loongson_dma_alloc_coherent(struct device *dev, size_t size,
  11. dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
  12. {
  13. void *ret;
  14. /* ignore region specifiers */
  15. gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
  16. #ifdef CONFIG_ISA
  17. if (dev == NULL)
  18. gfp |= __GFP_DMA;
  19. else
  20. #endif
  21. #ifdef CONFIG_ZONE_DMA
  22. if (dev->coherent_dma_mask < DMA_BIT_MASK(32))
  23. gfp |= __GFP_DMA;
  24. else
  25. #endif
  26. #ifdef CONFIG_ZONE_DMA32
  27. if (dev->coherent_dma_mask < DMA_BIT_MASK(40))
  28. gfp |= __GFP_DMA32;
  29. else
  30. #endif
  31. ;
  32. gfp |= __GFP_NORETRY;
  33. ret = swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
  34. mb();
  35. return ret;
  36. }
  37. static void loongson_dma_free_coherent(struct device *dev, size_t size,
  38. void *vaddr, dma_addr_t dma_handle, unsigned long attrs)
  39. {
  40. swiotlb_free_coherent(dev, size, vaddr, dma_handle);
  41. }
  42. static dma_addr_t loongson_dma_map_page(struct device *dev, struct page *page,
  43. unsigned long offset, size_t size,
  44. enum dma_data_direction dir,
  45. unsigned long attrs)
  46. {
  47. dma_addr_t daddr = swiotlb_map_page(dev, page, offset, size,
  48. dir, attrs);
  49. mb();
  50. return daddr;
  51. }
  52. static int loongson_dma_map_sg(struct device *dev, struct scatterlist *sg,
  53. int nents, enum dma_data_direction dir,
  54. unsigned long attrs)
  55. {
  56. int r = swiotlb_map_sg_attrs(dev, sg, nents, dir, 0);
  57. mb();
  58. return r;
  59. }
  60. static void loongson_dma_sync_single_for_device(struct device *dev,
  61. dma_addr_t dma_handle, size_t size,
  62. enum dma_data_direction dir)
  63. {
  64. swiotlb_sync_single_for_device(dev, dma_handle, size, dir);
  65. mb();
  66. }
  67. static void loongson_dma_sync_sg_for_device(struct device *dev,
  68. struct scatterlist *sg, int nents,
  69. enum dma_data_direction dir)
  70. {
  71. swiotlb_sync_sg_for_device(dev, sg, nents, dir);
  72. mb();
  73. }
  74. static int loongson_dma_set_mask(struct device *dev, u64 mask)
  75. {
  76. if (!dev->dma_mask || !dma_supported(dev, mask))
  77. return -EIO;
  78. if (mask > DMA_BIT_MASK(loongson_sysconf.dma_mask_bits)) {
  79. *dev->dma_mask = DMA_BIT_MASK(loongson_sysconf.dma_mask_bits);
  80. return -EIO;
  81. }
  82. *dev->dma_mask = mask;
  83. return 0;
  84. }
  85. dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
  86. {
  87. long nid;
  88. #ifdef CONFIG_PHYS48_TO_HT40
  89. /* We extract 2bit node id (bit 44~47, only bit 44~45 used now) from
  90. * Loongson-3's 48bit address space and embed it into 40bit */
  91. nid = (paddr >> 44) & 0x3;
  92. paddr = ((nid << 44) ^ paddr) | (nid << 37);
  93. #endif
  94. return paddr;
  95. }
  96. phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
  97. {
  98. long nid;
  99. #ifdef CONFIG_PHYS48_TO_HT40
  100. /* We extract 2bit node id (bit 44~47, only bit 44~45 used now) from
  101. * Loongson-3's 48bit address space and embed it into 40bit */
  102. nid = (daddr >> 37) & 0x3;
  103. daddr = ((nid << 37) ^ daddr) | (nid << 44);
  104. #endif
  105. return daddr;
  106. }
  107. static struct dma_map_ops loongson_dma_map_ops = {
  108. .alloc = loongson_dma_alloc_coherent,
  109. .free = loongson_dma_free_coherent,
  110. .map_page = loongson_dma_map_page,
  111. .unmap_page = swiotlb_unmap_page,
  112. .map_sg = loongson_dma_map_sg,
  113. .unmap_sg = swiotlb_unmap_sg_attrs,
  114. .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
  115. .sync_single_for_device = loongson_dma_sync_single_for_device,
  116. .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
  117. .sync_sg_for_device = loongson_dma_sync_sg_for_device,
  118. .mapping_error = swiotlb_dma_mapping_error,
  119. .dma_supported = swiotlb_dma_supported,
  120. .set_dma_mask = loongson_dma_set_mask
  121. };
  122. void __init plat_swiotlb_setup(void)
  123. {
  124. swiotlb_init(1);
  125. mips_dma_map_ops = &loongson_dma_map_ops;
  126. }