dma.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. /*
  9. * DMA Coherent API Notes
  10. *
  11. * I/O is inherently non-coherent on ARC. So a coherent DMA buffer is
  12. * implemented by accessintg it using a kernel virtual address, with
  13. * Cache bit off in the TLB entry.
  14. *
  15. * The default DMA address == Phy address which is 0x8000_0000 based.
  16. */
  17. #include <linux/dma-mapping.h>
  18. #include <asm/cache.h>
  19. #include <asm/cacheflush.h>
  20. static void *arc_dma_alloc(struct device *dev, size_t size,
  21. dma_addr_t *dma_handle, gfp_t gfp, struct dma_attrs *attrs)
  22. {
  23. void *paddr, *kvaddr;
  24. /* This is linear addr (0x8000_0000 based) */
  25. paddr = alloc_pages_exact(size, gfp);
  26. if (!paddr)
  27. return NULL;
  28. /* This is bus address, platform dependent */
  29. *dma_handle = (dma_addr_t)paddr;
  30. /*
  31. * IOC relies on all data (even coherent DMA data) being in cache
  32. * Thus allocate normal cached memory
  33. *
  34. * The gains with IOC are two pronged:
  35. * -For streaming data, elides needs for cache maintenance, saving
  36. * cycles in flush code, and bus bandwidth as all the lines of a
  37. * buffer need to be flushed out to memory
  38. * -For coherent data, Read/Write to buffers terminate early in cache
  39. * (vs. always going to memory - thus are faster)
  40. */
  41. if ((is_isa_arcv2() && ioc_exists) ||
  42. dma_get_attr(DMA_ATTR_NON_CONSISTENT, attrs))
  43. return paddr;
  44. /* This is kernel Virtual address (0x7000_0000 based) */
  45. kvaddr = ioremap_nocache((unsigned long)paddr, size);
  46. if (kvaddr == NULL)
  47. return NULL;
  48. /*
  49. * Evict any existing L1 and/or L2 lines for the backing page
  50. * in case it was used earlier as a normal "cached" page.
  51. * Yeah this bit us - STAR 9000898266
  52. *
  53. * Although core does call flush_cache_vmap(), it gets kvaddr hence
  54. * can't be used to efficiently flush L1 and/or L2 which need paddr
  55. * Currently flush_cache_vmap nukes the L1 cache completely which
  56. * will be optimized as a separate commit
  57. */
  58. dma_cache_wback_inv((unsigned long)paddr, size);
  59. return kvaddr;
  60. }
  61. static void arc_dma_free(struct device *dev, size_t size, void *vaddr,
  62. dma_addr_t dma_handle, struct dma_attrs *attrs)
  63. {
  64. if (!dma_get_attr(DMA_ATTR_NON_CONSISTENT, attrs) &&
  65. !(is_isa_arcv2() && ioc_exists))
  66. iounmap((void __force __iomem *)vaddr);
  67. free_pages_exact((void *)dma_handle, size);
  68. }
  69. /*
  70. * streaming DMA Mapping API...
  71. * CPU accesses page via normal paddr, thus needs to explicitly made
  72. * consistent before each use
  73. */
  74. static void _dma_cache_sync(unsigned long paddr, size_t size,
  75. enum dma_data_direction dir)
  76. {
  77. switch (dir) {
  78. case DMA_FROM_DEVICE:
  79. dma_cache_inv(paddr, size);
  80. break;
  81. case DMA_TO_DEVICE:
  82. dma_cache_wback(paddr, size);
  83. break;
  84. case DMA_BIDIRECTIONAL:
  85. dma_cache_wback_inv(paddr, size);
  86. break;
  87. default:
  88. pr_err("Invalid DMA dir [%d] for OP @ %lx\n", dir, paddr);
  89. }
  90. }
  91. static dma_addr_t arc_dma_map_page(struct device *dev, struct page *page,
  92. unsigned long offset, size_t size, enum dma_data_direction dir,
  93. struct dma_attrs *attrs)
  94. {
  95. unsigned long paddr = page_to_phys(page) + offset;
  96. _dma_cache_sync(paddr, size, dir);
  97. return (dma_addr_t)paddr;
  98. }
  99. static int arc_dma_map_sg(struct device *dev, struct scatterlist *sg,
  100. int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
  101. {
  102. struct scatterlist *s;
  103. int i;
  104. for_each_sg(sg, s, nents, i)
  105. s->dma_address = dma_map_page(dev, sg_page(s), s->offset,
  106. s->length, dir);
  107. return nents;
  108. }
  109. static void arc_dma_sync_single_for_cpu(struct device *dev,
  110. dma_addr_t dma_handle, size_t size, enum dma_data_direction dir)
  111. {
  112. _dma_cache_sync(dma_handle, size, DMA_FROM_DEVICE);
  113. }
  114. static void arc_dma_sync_single_for_device(struct device *dev,
  115. dma_addr_t dma_handle, size_t size, enum dma_data_direction dir)
  116. {
  117. _dma_cache_sync(dma_handle, size, DMA_TO_DEVICE);
  118. }
  119. static void arc_dma_sync_sg_for_cpu(struct device *dev,
  120. struct scatterlist *sglist, int nelems,
  121. enum dma_data_direction dir)
  122. {
  123. int i;
  124. struct scatterlist *sg;
  125. for_each_sg(sglist, sg, nelems, i)
  126. _dma_cache_sync((unsigned int)sg_virt(sg), sg->length, dir);
  127. }
  128. static void arc_dma_sync_sg_for_device(struct device *dev,
  129. struct scatterlist *sglist, int nelems,
  130. enum dma_data_direction dir)
  131. {
  132. int i;
  133. struct scatterlist *sg;
  134. for_each_sg(sglist, sg, nelems, i)
  135. _dma_cache_sync((unsigned int)sg_virt(sg), sg->length, dir);
  136. }
  137. static int arc_dma_supported(struct device *dev, u64 dma_mask)
  138. {
  139. /* Support 32 bit DMA mask exclusively */
  140. return dma_mask == DMA_BIT_MASK(32);
  141. }
  142. struct dma_map_ops arc_dma_ops = {
  143. .alloc = arc_dma_alloc,
  144. .free = arc_dma_free,
  145. .map_page = arc_dma_map_page,
  146. .map_sg = arc_dma_map_sg,
  147. .sync_single_for_device = arc_dma_sync_single_for_device,
  148. .sync_single_for_cpu = arc_dma_sync_single_for_cpu,
  149. .sync_sg_for_cpu = arc_dma_sync_sg_for_cpu,
  150. .sync_sg_for_device = arc_dma_sync_sg_for_device,
  151. .dma_supported = arc_dma_supported,
  152. };
  153. EXPORT_SYMBOL(arc_dma_ops);