dma.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  4. */
  5. #include <linux/dma-map-ops.h>
  6. #include <asm/cache.h>
  7. #include <asm/cacheflush.h>
  8. /*
  9. * ARCH specific callbacks for generic noncoherent DMA ops
  10. * - hardware IOC not available (or "dma-coherent" not set for device in DT)
  11. * - But still handle both coherent and non-coherent requests from caller
  12. *
  13. * For DMA coherent hardware (IOC) generic code suffices
  14. */
  15. void arch_dma_prep_coherent(struct page *page, size_t size)
  16. {
  17. /*
  18. * Evict any existing L1 and/or L2 lines for the backing page
  19. * in case it was used earlier as a normal "cached" page.
  20. * Yeah this bit us - STAR 9000898266
  21. *
  22. * Although core does call flush_cache_vmap(), it gets kvaddr hence
  23. * can't be used to efficiently flush L1 and/or L2 which need paddr
  24. * Currently flush_cache_vmap nukes the L1 cache completely which
  25. * will be optimized as a separate commit
  26. */
  27. dma_cache_wback_inv(page_to_phys(page), size);
  28. }
  29. /*
  30. * Cache operations depending on function and direction argument, inspired by
  31. * https://lore.kernel.org/lkml/[email protected]
  32. * "dma_sync_*_for_cpu and direction=TO_DEVICE (was Re: [PATCH 02/20]
  33. * dma-mapping: provide a generic dma-noncoherent implementation)"
  34. *
  35. * | map == for_device | unmap == for_cpu
  36. * |----------------------------------------------------------------
  37. * TO_DEV | writeback writeback | none none
  38. * FROM_DEV | invalidate invalidate | invalidate* invalidate*
  39. * BIDIR | writeback+inv writeback+inv | invalidate invalidate
  40. *
  41. * [*] needed for CPU speculative prefetches
  42. *
  43. * NOTE: we don't check the validity of direction argument as it is done in
  44. * upper layer functions (in include/linux/dma-mapping.h)
  45. */
  46. void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
  47. enum dma_data_direction dir)
  48. {
  49. switch (dir) {
  50. case DMA_TO_DEVICE:
  51. dma_cache_wback(paddr, size);
  52. break;
  53. case DMA_FROM_DEVICE:
  54. dma_cache_inv(paddr, size);
  55. break;
  56. case DMA_BIDIRECTIONAL:
  57. dma_cache_wback_inv(paddr, size);
  58. break;
  59. default:
  60. break;
  61. }
  62. }
  63. void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
  64. enum dma_data_direction dir)
  65. {
  66. switch (dir) {
  67. case DMA_TO_DEVICE:
  68. break;
  69. /* FROM_DEVICE invalidate needed if speculative CPU prefetch only */
  70. case DMA_FROM_DEVICE:
  71. case DMA_BIDIRECTIONAL:
  72. dma_cache_inv(paddr, size);
  73. break;
  74. default:
  75. break;
  76. }
  77. }
  78. /*
  79. * Plug in direct dma map ops.
  80. */
  81. void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
  82. const struct iommu_ops *iommu, bool coherent)
  83. {
  84. /*
  85. * IOC hardware snoops all DMA traffic keeping the caches consistent
  86. * with memory - eliding need for any explicit cache maintenance of
  87. * DMA buffers.
  88. */
  89. if (is_isa_arcv2() && ioc_enable && coherent)
  90. dev->dma_coherent = true;
  91. dev_info(dev, "use %scoherent DMA ops\n",
  92. dev->dma_coherent ? "" : "non");
  93. }