dma.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file COPYING in the main directory of this archive
  4. * for more details.
  5. */
  6. #undef DEBUG
  7. #include <linux/dma-mapping.h>
  8. #include <linux/device.h>
  9. #include <linux/kernel.h>
  10. #include <linux/scatterlist.h>
  11. #include <linux/slab.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/export.h>
  14. #include <asm/pgalloc.h>
  15. #if defined(CONFIG_MMU) && !defined(CONFIG_COLDFIRE)
  16. static void *m68k_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
  17. gfp_t flag, unsigned long attrs)
  18. {
  19. struct page *page, **map;
  20. pgprot_t pgprot;
  21. void *addr;
  22. int i, order;
  23. pr_debug("dma_alloc_coherent: %d,%x\n", size, flag);
  24. size = PAGE_ALIGN(size);
  25. order = get_order(size);
  26. page = alloc_pages(flag, order);
  27. if (!page)
  28. return NULL;
  29. *handle = page_to_phys(page);
  30. map = kmalloc(sizeof(struct page *) << order, flag & ~__GFP_DMA);
  31. if (!map) {
  32. __free_pages(page, order);
  33. return NULL;
  34. }
  35. split_page(page, order);
  36. order = 1 << order;
  37. size >>= PAGE_SHIFT;
  38. map[0] = page;
  39. for (i = 1; i < size; i++)
  40. map[i] = page + i;
  41. for (; i < order; i++)
  42. __free_page(page + i);
  43. pgprot = __pgprot(_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_DIRTY);
  44. if (CPU_IS_040_OR_060)
  45. pgprot_val(pgprot) |= _PAGE_GLOBAL040 | _PAGE_NOCACHE_S;
  46. else
  47. pgprot_val(pgprot) |= _PAGE_NOCACHE030;
  48. addr = vmap(map, size, VM_MAP, pgprot);
  49. kfree(map);
  50. return addr;
  51. }
  52. static void m68k_dma_free(struct device *dev, size_t size, void *addr,
  53. dma_addr_t handle, unsigned long attrs)
  54. {
  55. pr_debug("dma_free_coherent: %p, %x\n", addr, handle);
  56. vfree(addr);
  57. }
  58. #else
  59. #include <asm/cacheflush.h>
  60. static void *m68k_dma_alloc(struct device *dev, size_t size,
  61. dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
  62. {
  63. void *ret;
  64. /* ignore region specifiers */
  65. gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
  66. if (dev == NULL || (*dev->dma_mask < 0xffffffff))
  67. gfp |= GFP_DMA;
  68. ret = (void *)__get_free_pages(gfp, get_order(size));
  69. if (ret != NULL) {
  70. memset(ret, 0, size);
  71. *dma_handle = virt_to_phys(ret);
  72. }
  73. return ret;
  74. }
  75. static void m68k_dma_free(struct device *dev, size_t size, void *vaddr,
  76. dma_addr_t dma_handle, unsigned long attrs)
  77. {
  78. free_pages((unsigned long)vaddr, get_order(size));
  79. }
  80. #endif /* CONFIG_MMU && !CONFIG_COLDFIRE */
  81. static void m68k_dma_sync_single_for_device(struct device *dev,
  82. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  83. {
  84. switch (dir) {
  85. case DMA_BIDIRECTIONAL:
  86. case DMA_TO_DEVICE:
  87. cache_push(handle, size);
  88. break;
  89. case DMA_FROM_DEVICE:
  90. cache_clear(handle, size);
  91. break;
  92. default:
  93. if (printk_ratelimit())
  94. printk("dma_sync_single_for_device: unsupported dir %u\n", dir);
  95. break;
  96. }
  97. }
  98. static void m68k_dma_sync_sg_for_device(struct device *dev,
  99. struct scatterlist *sglist, int nents, enum dma_data_direction dir)
  100. {
  101. int i;
  102. struct scatterlist *sg;
  103. for_each_sg(sglist, sg, nents, i) {
  104. dma_sync_single_for_device(dev, sg->dma_address, sg->length,
  105. dir);
  106. }
  107. }
  108. static dma_addr_t m68k_dma_map_page(struct device *dev, struct page *page,
  109. unsigned long offset, size_t size, enum dma_data_direction dir,
  110. unsigned long attrs)
  111. {
  112. dma_addr_t handle = page_to_phys(page) + offset;
  113. dma_sync_single_for_device(dev, handle, size, dir);
  114. return handle;
  115. }
  116. static int m68k_dma_map_sg(struct device *dev, struct scatterlist *sglist,
  117. int nents, enum dma_data_direction dir, unsigned long attrs)
  118. {
  119. int i;
  120. struct scatterlist *sg;
  121. for_each_sg(sglist, sg, nents, i) {
  122. sg->dma_address = sg_phys(sg);
  123. dma_sync_single_for_device(dev, sg->dma_address, sg->length,
  124. dir);
  125. }
  126. return nents;
  127. }
  128. struct dma_map_ops m68k_dma_ops = {
  129. .alloc = m68k_dma_alloc,
  130. .free = m68k_dma_free,
  131. .map_page = m68k_dma_map_page,
  132. .map_sg = m68k_dma_map_sg,
  133. .sync_single_for_device = m68k_dma_sync_single_for_device,
  134. .sync_sg_for_device = m68k_dma_sync_sg_for_device,
  135. };
  136. EXPORT_SYMBOL(m68k_dma_ops);