dma.c 5.4 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/bootmem.h>
  22. #include <linux/genalloc.h>
  23. #include <asm/dma-mapping.h>
  24. #include <linux/module.h>
  25. #include <asm/page.h>
  26. struct dma_map_ops *dma_ops;
  27. EXPORT_SYMBOL(dma_ops);
  28. int bad_dma_address; /* globals are automatically initialized to zero */
  29. static inline void *dma_addr_to_virt(dma_addr_t dma_addr)
  30. {
  31. return phys_to_virt((unsigned long) dma_addr);
  32. }
  33. int dma_supported(struct device *dev, u64 mask)
  34. {
  35. if (mask == DMA_BIT_MASK(32))
  36. return 1;
  37. else
  38. return 0;
  39. }
  40. EXPORT_SYMBOL(dma_supported);
  41. static struct gen_pool *coherent_pool;
  42. /* Allocates from a pool of uncached memory that was reserved at boot time */
  43. static void *hexagon_dma_alloc_coherent(struct device *dev, size_t size,
  44. dma_addr_t *dma_addr, gfp_t flag,
  45. unsigned long attrs)
  46. {
  47. void *ret;
  48. /*
  49. * Our max_low_pfn should have been backed off by 16MB in
  50. * mm/init.c to create DMA coherent space. Use that as the VA
  51. * for the pool.
  52. */
  53. if (coherent_pool == NULL) {
  54. coherent_pool = gen_pool_create(PAGE_SHIFT, -1);
  55. if (coherent_pool == NULL)
  56. panic("Can't create %s() memory pool!", __func__);
  57. else
  58. gen_pool_add(coherent_pool,
  59. pfn_to_virt(max_low_pfn),
  60. hexagon_coherent_pool_size, -1);
  61. }
  62. ret = (void *) gen_pool_alloc(coherent_pool, size);
  63. if (ret) {
  64. memset(ret, 0, size);
  65. *dma_addr = (dma_addr_t) virt_to_phys(ret);
  66. } else
  67. *dma_addr = ~0;
  68. return ret;
  69. }
  70. static void hexagon_free_coherent(struct device *dev, size_t size, void *vaddr,
  71. dma_addr_t dma_addr, unsigned long attrs)
  72. {
  73. gen_pool_free(coherent_pool, (unsigned long) vaddr, size);
  74. }
  75. static int check_addr(const char *name, struct device *hwdev,
  76. dma_addr_t bus, size_t size)
  77. {
  78. if (hwdev && hwdev->dma_mask && !dma_capable(hwdev, bus, size)) {
  79. if (*hwdev->dma_mask >= DMA_BIT_MASK(32))
  80. printk(KERN_ERR
  81. "%s: overflow %Lx+%zu of device mask %Lx\n",
  82. name, (long long)bus, size,
  83. (long long)*hwdev->dma_mask);
  84. return 0;
  85. }
  86. return 1;
  87. }
  88. static int hexagon_map_sg(struct device *hwdev, struct scatterlist *sg,
  89. int nents, enum dma_data_direction dir,
  90. unsigned long attrs)
  91. {
  92. struct scatterlist *s;
  93. int i;
  94. WARN_ON(nents == 0 || sg[0].length == 0);
  95. for_each_sg(sg, s, nents, i) {
  96. s->dma_address = sg_phys(s);
  97. if (!check_addr("map_sg", hwdev, s->dma_address, s->length))
  98. return 0;
  99. s->dma_length = s->length;
  100. flush_dcache_range(dma_addr_to_virt(s->dma_address),
  101. dma_addr_to_virt(s->dma_address + s->length));
  102. }
  103. return nents;
  104. }
  105. /*
  106. * address is virtual
  107. */
  108. static inline void dma_sync(void *addr, size_t size,
  109. enum dma_data_direction dir)
  110. {
  111. switch (dir) {
  112. case DMA_TO_DEVICE:
  113. hexagon_clean_dcache_range((unsigned long) addr,
  114. (unsigned long) addr + size);
  115. break;
  116. case DMA_FROM_DEVICE:
  117. hexagon_inv_dcache_range((unsigned long) addr,
  118. (unsigned long) addr + size);
  119. break;
  120. case DMA_BIDIRECTIONAL:
  121. flush_dcache_range((unsigned long) addr,
  122. (unsigned long) addr + size);
  123. break;
  124. default:
  125. BUG();
  126. }
  127. }
  128. /**
  129. * hexagon_map_page() - maps an address for device DMA
  130. * @dev: pointer to DMA device
  131. * @page: pointer to page struct of DMA memory
  132. * @offset: offset within page
  133. * @size: size of memory to map
  134. * @dir: transfer direction
  135. * @attrs: pointer to DMA attrs (not used)
  136. *
  137. * Called to map a memory address to a DMA address prior
  138. * to accesses to/from device.
  139. *
  140. * We don't particularly have many hoops to jump through
  141. * so far. Straight translation between phys and virtual.
  142. *
  143. * DMA is not cache coherent so sync is necessary; this
  144. * seems to be a convenient place to do it.
  145. *
  146. */
  147. static dma_addr_t hexagon_map_page(struct device *dev, struct page *page,
  148. unsigned long offset, size_t size,
  149. enum dma_data_direction dir,
  150. unsigned long attrs)
  151. {
  152. dma_addr_t bus = page_to_phys(page) + offset;
  153. WARN_ON(size == 0);
  154. if (!check_addr("map_single", dev, bus, size))
  155. return bad_dma_address;
  156. dma_sync(dma_addr_to_virt(bus), size, dir);
  157. return bus;
  158. }
  159. static void hexagon_sync_single_for_cpu(struct device *dev,
  160. dma_addr_t dma_handle, size_t size,
  161. enum dma_data_direction dir)
  162. {
  163. dma_sync(dma_addr_to_virt(dma_handle), size, dir);
  164. }
  165. static void hexagon_sync_single_for_device(struct device *dev,
  166. dma_addr_t dma_handle, size_t size,
  167. enum dma_data_direction dir)
  168. {
  169. dma_sync(dma_addr_to_virt(dma_handle), size, dir);
  170. }
  171. 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. .is_phys = 1,
  179. };
  180. void __init hexagon_dma_init(void)
  181. {
  182. if (dma_ops)
  183. return;
  184. dma_ops = &hexagon_dma_ops;
  185. }