dma.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/dma-noncoherent.h>
  9. #include <asm/cache.h>
  10. #include <asm/cacheflush.h>
  11. /*
  12. * ARCH specific callbacks for generic noncoherent DMA ops (dma/noncoherent.c)
  13. * - hardware IOC not available (or "dma-coherent" not set for device in DT)
  14. * - But still handle both coherent and non-coherent requests from caller
  15. *
  16. * For DMA coherent hardware (IOC) generic code suffices
  17. */
  18. void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
  19. gfp_t gfp, unsigned long attrs)
  20. {
  21. unsigned long order = get_order(size);
  22. struct page *page;
  23. phys_addr_t paddr;
  24. void *kvaddr;
  25. bool need_coh = !(attrs & DMA_ATTR_NON_CONSISTENT);
  26. /*
  27. * __GFP_HIGHMEM flag is cleared by upper layer functions
  28. * (in include/linux/dma-mapping.h) so we should never get a
  29. * __GFP_HIGHMEM here.
  30. */
  31. BUG_ON(gfp & __GFP_HIGHMEM);
  32. page = alloc_pages(gfp, order);
  33. if (!page)
  34. return NULL;
  35. /* This is linear addr (0x8000_0000 based) */
  36. paddr = page_to_phys(page);
  37. *dma_handle = paddr;
  38. /*
  39. * A coherent buffer needs MMU mapping to enforce non-cachability.
  40. * kvaddr is kernel Virtual address (0x7000_0000 based).
  41. */
  42. if (need_coh) {
  43. kvaddr = ioremap_nocache(paddr, size);
  44. if (kvaddr == NULL) {
  45. __free_pages(page, order);
  46. return NULL;
  47. }
  48. } else {
  49. kvaddr = (void *)(u32)paddr;
  50. }
  51. /*
  52. * Evict any existing L1 and/or L2 lines for the backing page
  53. * in case it was used earlier as a normal "cached" page.
  54. * Yeah this bit us - STAR 9000898266
  55. *
  56. * Although core does call flush_cache_vmap(), it gets kvaddr hence
  57. * can't be used to efficiently flush L1 and/or L2 which need paddr
  58. * Currently flush_cache_vmap nukes the L1 cache completely which
  59. * will be optimized as a separate commit
  60. */
  61. if (need_coh)
  62. dma_cache_wback_inv(paddr, size);
  63. return kvaddr;
  64. }
  65. void arch_dma_free(struct device *dev, size_t size, void *vaddr,
  66. dma_addr_t dma_handle, unsigned long attrs)
  67. {
  68. phys_addr_t paddr = dma_handle;
  69. struct page *page = virt_to_page(paddr);
  70. if (!(attrs & DMA_ATTR_NON_CONSISTENT))
  71. iounmap((void __force __iomem *)vaddr);
  72. __free_pages(page, get_order(size));
  73. }
  74. long arch_dma_coherent_to_pfn(struct device *dev, void *cpu_addr,
  75. dma_addr_t dma_addr)
  76. {
  77. return __phys_to_pfn(dma_addr);
  78. }
  79. /*
  80. * Cache operations depending on function and direction argument, inspired by
  81. * https://lkml.org/lkml/2018/5/18/979
  82. * "dma_sync_*_for_cpu and direction=TO_DEVICE (was Re: [PATCH 02/20]
  83. * dma-mapping: provide a generic dma-noncoherent implementation)"
  84. *
  85. * | map == for_device | unmap == for_cpu
  86. * |----------------------------------------------------------------
  87. * TO_DEV | writeback writeback | none none
  88. * FROM_DEV | invalidate invalidate | invalidate* invalidate*
  89. * BIDIR | writeback+inv writeback+inv | invalidate invalidate
  90. *
  91. * [*] needed for CPU speculative prefetches
  92. *
  93. * NOTE: we don't check the validity of direction argument as it is done in
  94. * upper layer functions (in include/linux/dma-mapping.h)
  95. */
  96. void arch_sync_dma_for_device(struct device *dev, phys_addr_t paddr,
  97. size_t size, enum dma_data_direction dir)
  98. {
  99. switch (dir) {
  100. case DMA_TO_DEVICE:
  101. dma_cache_wback(paddr, size);
  102. break;
  103. case DMA_FROM_DEVICE:
  104. dma_cache_inv(paddr, size);
  105. break;
  106. case DMA_BIDIRECTIONAL:
  107. dma_cache_wback_inv(paddr, size);
  108. break;
  109. default:
  110. break;
  111. }
  112. }
  113. void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr,
  114. size_t size, enum dma_data_direction dir)
  115. {
  116. switch (dir) {
  117. case DMA_TO_DEVICE:
  118. break;
  119. /* FROM_DEVICE invalidate needed if speculative CPU prefetch only */
  120. case DMA_FROM_DEVICE:
  121. case DMA_BIDIRECTIONAL:
  122. dma_cache_inv(paddr, size);
  123. break;
  124. default:
  125. break;
  126. }
  127. }
  128. /*
  129. * Plug in direct dma map ops.
  130. */
  131. void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
  132. const struct iommu_ops *iommu, bool coherent)
  133. {
  134. /*
  135. * IOC hardware snoops all DMA traffic keeping the caches consistent
  136. * with memory - eliding need for any explicit cache maintenance of
  137. * DMA buffers.
  138. */
  139. if (is_isa_arcv2() && ioc_enable && coherent)
  140. dev->dma_coherent = true;
  141. dev_info(dev, "use %sncoherent DMA ops\n",
  142. dev->dma_coherent ? "" : "non");
  143. }