dma.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * DMA implementation for Hexagon
  3. *
  4. * Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 and
  8. * only version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA.
  19. */
  20. #include <linux/dma-mapping.h>
  21. #include <linux/dma-direct.h>
  22. #include <linux/bootmem.h>
  23. #include <linux/genalloc.h>
  24. #include <asm/dma-mapping.h>
  25. #include <linux/module.h>
  26. #include <asm/page.h>
  27. #define HEXAGON_MAPPING_ERROR 0
  28. const struct dma_map_ops *dma_ops;
  29. EXPORT_SYMBOL(dma_ops);
  30. static inline void *dma_addr_to_virt(dma_addr_t dma_addr)
  31. {
  32. return phys_to_virt((unsigned long) dma_addr);
  33. }
  34. static struct gen_pool *coherent_pool;
  35. /* Allocates from a pool of uncached memory that was reserved at boot time */
  36. static void *hexagon_dma_alloc_coherent(struct device *dev, size_t size,
  37. dma_addr_t *dma_addr, gfp_t flag,
  38. unsigned long attrs)
  39. {
  40. void *ret;
  41. /*
  42. * Our max_low_pfn should have been backed off by 16MB in
  43. * mm/init.c to create DMA coherent space. Use that as the VA
  44. * for the pool.
  45. */
  46. if (coherent_pool == NULL) {
  47. coherent_pool = gen_pool_create(PAGE_SHIFT, -1);
  48. if (coherent_pool == NULL)
  49. panic("Can't create %s() memory pool!", __func__);
  50. else
  51. gen_pool_add(coherent_pool,
  52. (unsigned long)pfn_to_virt(max_low_pfn),
  53. hexagon_coherent_pool_size, -1);
  54. }
  55. ret = (void *) gen_pool_alloc(coherent_pool, size);
  56. if (ret) {
  57. memset(ret, 0, size);
  58. *dma_addr = (dma_addr_t) virt_to_phys(ret);
  59. } else
  60. *dma_addr = ~0;
  61. return ret;
  62. }
  63. static void hexagon_free_coherent(struct device *dev, size_t size, void *vaddr,
  64. dma_addr_t dma_addr, unsigned long attrs)
  65. {
  66. gen_pool_free(coherent_pool, (unsigned long) vaddr, size);
  67. }
  68. static int check_addr(const char *name, struct device *hwdev,
  69. dma_addr_t bus, size_t size)
  70. {
  71. if (hwdev && hwdev->dma_mask && !dma_capable(hwdev, bus, size)) {
  72. if (*hwdev->dma_mask >= DMA_BIT_MASK(32))
  73. printk(KERN_ERR
  74. "%s: overflow %Lx+%zu of device mask %Lx\n",
  75. name, (long long)bus, size,
  76. (long long)*hwdev->dma_mask);
  77. return 0;
  78. }
  79. return 1;
  80. }
  81. static int hexagon_map_sg(struct device *hwdev, struct scatterlist *sg,
  82. int nents, enum dma_data_direction dir,
  83. unsigned long attrs)
  84. {
  85. struct scatterlist *s;
  86. int i;
  87. WARN_ON(nents == 0 || sg[0].length == 0);
  88. for_each_sg(sg, s, nents, i) {
  89. s->dma_address = sg_phys(s);
  90. if (!check_addr("map_sg", hwdev, s->dma_address, s->length))
  91. return 0;
  92. s->dma_length = s->length;
  93. if (attrs & DMA_ATTR_SKIP_CPU_SYNC)
  94. continue;
  95. flush_dcache_range(dma_addr_to_virt(s->dma_address),
  96. dma_addr_to_virt(s->dma_address + s->length));
  97. }
  98. return nents;
  99. }
  100. /*
  101. * address is virtual
  102. */
  103. static inline void dma_sync(void *addr, size_t size,
  104. enum dma_data_direction dir)
  105. {
  106. switch (dir) {
  107. case DMA_TO_DEVICE:
  108. hexagon_clean_dcache_range((unsigned long) addr,
  109. (unsigned long) addr + size);
  110. break;
  111. case DMA_FROM_DEVICE:
  112. hexagon_inv_dcache_range((unsigned long) addr,
  113. (unsigned long) addr + size);
  114. break;
  115. case DMA_BIDIRECTIONAL:
  116. flush_dcache_range((unsigned long) addr,
  117. (unsigned long) addr + size);
  118. break;
  119. default:
  120. BUG();
  121. }
  122. }
  123. /**
  124. * hexagon_map_page() - maps an address for device DMA
  125. * @dev: pointer to DMA device
  126. * @page: pointer to page struct of DMA memory
  127. * @offset: offset within page
  128. * @size: size of memory to map
  129. * @dir: transfer direction
  130. * @attrs: pointer to DMA attrs (not used)
  131. *
  132. * Called to map a memory address to a DMA address prior
  133. * to accesses to/from device.
  134. *
  135. * We don't particularly have many hoops to jump through
  136. * so far. Straight translation between phys and virtual.
  137. *
  138. * DMA is not cache coherent so sync is necessary; this
  139. * seems to be a convenient place to do it.
  140. *
  141. */
  142. static dma_addr_t hexagon_map_page(struct device *dev, struct page *page,
  143. unsigned long offset, size_t size,
  144. enum dma_data_direction dir,
  145. unsigned long attrs)
  146. {
  147. dma_addr_t bus = page_to_phys(page) + offset;
  148. WARN_ON(size == 0);
  149. if (!check_addr("map_single", dev, bus, size))
  150. return HEXAGON_MAPPING_ERROR;
  151. if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
  152. dma_sync(dma_addr_to_virt(bus), size, dir);
  153. return bus;
  154. }
  155. static void hexagon_sync_single_for_cpu(struct device *dev,
  156. dma_addr_t dma_handle, size_t size,
  157. enum dma_data_direction dir)
  158. {
  159. dma_sync(dma_addr_to_virt(dma_handle), size, dir);
  160. }
  161. static void hexagon_sync_single_for_device(struct device *dev,
  162. dma_addr_t dma_handle, size_t size,
  163. enum dma_data_direction dir)
  164. {
  165. dma_sync(dma_addr_to_virt(dma_handle), size, dir);
  166. }
  167. static int hexagon_mapping_error(struct device *dev, dma_addr_t dma_addr)
  168. {
  169. return dma_addr == HEXAGON_MAPPING_ERROR;
  170. }
  171. const struct dma_map_ops hexagon_dma_ops = {
  172. .alloc = hexagon_dma_alloc_coherent,
  173. .free = hexagon_free_coherent,
  174. .map_sg = hexagon_map_sg,
  175. .map_page = hexagon_map_page,
  176. .sync_single_for_cpu = hexagon_sync_single_for_cpu,
  177. .sync_single_for_device = hexagon_sync_single_for_device,
  178. .mapping_error = hexagon_mapping_error,
  179. };
  180. void __init hexagon_dma_init(void)
  181. {
  182. if (dma_ops)
  183. return;
  184. dma_ops = &hexagon_dma_ops;
  185. }