dma.c 5.5 KB

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