dma.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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-noncoherent.h>
  8. #include <linux/device.h>
  9. #include <linux/kernel.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/scatterlist.h>
  12. #include <linux/slab.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/export.h>
  15. #include <asm/pgalloc.h>
  16. #if defined(CONFIG_MMU) && !defined(CONFIG_COLDFIRE)
  17. void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
  18. gfp_t flag, unsigned long attrs)
  19. {
  20. struct page *page, **map;
  21. pgprot_t pgprot;
  22. void *addr;
  23. int i, order;
  24. pr_debug("dma_alloc_coherent: %d,%x\n", size, flag);
  25. size = PAGE_ALIGN(size);
  26. order = get_order(size);
  27. page = alloc_pages(flag, order);
  28. if (!page)
  29. return NULL;
  30. *handle = page_to_phys(page);
  31. map = kmalloc(sizeof(struct page *) << order, flag & ~__GFP_DMA);
  32. if (!map) {
  33. __free_pages(page, order);
  34. return NULL;
  35. }
  36. split_page(page, order);
  37. order = 1 << order;
  38. size >>= PAGE_SHIFT;
  39. map[0] = page;
  40. for (i = 1; i < size; i++)
  41. map[i] = page + i;
  42. for (; i < order; i++)
  43. __free_page(page + i);
  44. pgprot = __pgprot(_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_DIRTY);
  45. if (CPU_IS_040_OR_060)
  46. pgprot_val(pgprot) |= _PAGE_GLOBAL040 | _PAGE_NOCACHE_S;
  47. else
  48. pgprot_val(pgprot) |= _PAGE_NOCACHE030;
  49. addr = vmap(map, size, VM_MAP, pgprot);
  50. kfree(map);
  51. return addr;
  52. }
  53. void arch_dma_free(struct device *dev, size_t size, void *addr,
  54. dma_addr_t handle, unsigned long attrs)
  55. {
  56. pr_debug("dma_free_coherent: %p, %x\n", addr, handle);
  57. vfree(addr);
  58. }
  59. #else
  60. #include <asm/cacheflush.h>
  61. void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
  62. gfp_t gfp, unsigned long attrs)
  63. {
  64. void *ret;
  65. if (dev == NULL || (*dev->dma_mask < 0xffffffff))
  66. gfp |= GFP_DMA;
  67. ret = (void *)__get_free_pages(gfp, get_order(size));
  68. if (ret != NULL) {
  69. memset(ret, 0, size);
  70. *dma_handle = virt_to_phys(ret);
  71. }
  72. return ret;
  73. }
  74. void arch_dma_free(struct device *dev, size_t size, void *vaddr,
  75. dma_addr_t dma_handle, unsigned long attrs)
  76. {
  77. free_pages((unsigned long)vaddr, get_order(size));
  78. }
  79. #endif /* CONFIG_MMU && !CONFIG_COLDFIRE */
  80. void arch_sync_dma_for_device(struct device *dev, phys_addr_t handle,
  81. size_t size, enum dma_data_direction dir)
  82. {
  83. switch (dir) {
  84. case DMA_BIDIRECTIONAL:
  85. case DMA_TO_DEVICE:
  86. cache_push(handle, size);
  87. break;
  88. case DMA_FROM_DEVICE:
  89. cache_clear(handle, size);
  90. break;
  91. default:
  92. pr_err_ratelimited("dma_sync_single_for_device: unsupported dir %u\n",
  93. dir);
  94. break;
  95. }
  96. }
  97. void arch_setup_pdev_archdata(struct platform_device *pdev)
  98. {
  99. if (pdev->dev.coherent_dma_mask == DMA_MASK_NONE &&
  100. pdev->dev.dma_mask == NULL) {
  101. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  102. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  103. }
  104. }