dma.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2011 Texas Instruments Incorporated
  3. * Author: Mark Salter <msalter@redhat.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/mm.h>
  12. #include <linux/mm_types.h>
  13. #include <linux/scatterlist.h>
  14. #include <asm/cacheflush.h>
  15. static void c6x_dma_sync(dma_addr_t handle, size_t size,
  16. enum dma_data_direction dir)
  17. {
  18. unsigned long paddr = handle;
  19. BUG_ON(!valid_dma_direction(dir));
  20. switch (dir) {
  21. case DMA_FROM_DEVICE:
  22. L2_cache_block_invalidate(paddr, paddr + size);
  23. break;
  24. case DMA_TO_DEVICE:
  25. L2_cache_block_writeback(paddr, paddr + size);
  26. break;
  27. case DMA_BIDIRECTIONAL:
  28. L2_cache_block_writeback_invalidate(paddr, paddr + size);
  29. break;
  30. default:
  31. break;
  32. }
  33. }
  34. static dma_addr_t c6x_dma_map_page(struct device *dev, struct page *page,
  35. unsigned long offset, size_t size, enum dma_data_direction dir,
  36. unsigned long attrs)
  37. {
  38. dma_addr_t handle = virt_to_phys(page_address(page) + offset);
  39. if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
  40. c6x_dma_sync(handle, size, dir);
  41. return handle;
  42. }
  43. static void c6x_dma_unmap_page(struct device *dev, dma_addr_t handle,
  44. size_t size, enum dma_data_direction dir, unsigned long attrs)
  45. {
  46. if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
  47. c6x_dma_sync(handle, size, dir);
  48. }
  49. static int c6x_dma_map_sg(struct device *dev, struct scatterlist *sglist,
  50. int nents, enum dma_data_direction dir, unsigned long attrs)
  51. {
  52. struct scatterlist *sg;
  53. int i;
  54. for_each_sg(sglist, sg, nents, i) {
  55. sg->dma_address = sg_phys(sg);
  56. if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
  57. c6x_dma_sync(sg->dma_address, sg->length, dir);
  58. }
  59. return nents;
  60. }
  61. static void c6x_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
  62. int nents, enum dma_data_direction dir, unsigned long attrs)
  63. {
  64. struct scatterlist *sg;
  65. int i;
  66. if (attrs & DMA_ATTR_SKIP_CPU_SYNC)
  67. return;
  68. for_each_sg(sglist, sg, nents, i)
  69. c6x_dma_sync(sg_dma_address(sg), sg->length, dir);
  70. }
  71. static void c6x_dma_sync_single_for_cpu(struct device *dev, dma_addr_t handle,
  72. size_t size, enum dma_data_direction dir)
  73. {
  74. c6x_dma_sync(handle, size, dir);
  75. }
  76. static void c6x_dma_sync_single_for_device(struct device *dev,
  77. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  78. {
  79. c6x_dma_sync(handle, size, dir);
  80. }
  81. static void c6x_dma_sync_sg_for_cpu(struct device *dev,
  82. struct scatterlist *sglist, int nents,
  83. enum dma_data_direction dir)
  84. {
  85. struct scatterlist *sg;
  86. int i;
  87. for_each_sg(sglist, sg, nents, i)
  88. c6x_dma_sync_single_for_cpu(dev, sg_dma_address(sg),
  89. sg->length, dir);
  90. }
  91. static void c6x_dma_sync_sg_for_device(struct device *dev,
  92. struct scatterlist *sglist, int nents,
  93. enum dma_data_direction dir)
  94. {
  95. struct scatterlist *sg;
  96. int i;
  97. for_each_sg(sglist, sg, nents, i)
  98. c6x_dma_sync_single_for_device(dev, sg_dma_address(sg),
  99. sg->length, dir);
  100. }
  101. const struct dma_map_ops c6x_dma_ops = {
  102. .alloc = c6x_dma_alloc,
  103. .free = c6x_dma_free,
  104. .map_page = c6x_dma_map_page,
  105. .unmap_page = c6x_dma_unmap_page,
  106. .map_sg = c6x_dma_map_sg,
  107. .unmap_sg = c6x_dma_unmap_sg,
  108. .sync_single_for_device = c6x_dma_sync_single_for_device,
  109. .sync_single_for_cpu = c6x_dma_sync_single_for_cpu,
  110. .sync_sg_for_device = c6x_dma_sync_sg_for_device,
  111. .sync_sg_for_cpu = c6x_dma_sync_sg_for_cpu,
  112. };
  113. EXPORT_SYMBOL(c6x_dma_ops);
  114. /* Number of entries preallocated for DMA-API debugging */
  115. #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
  116. static int __init dma_init(void)
  117. {
  118. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  119. return 0;
  120. }
  121. fs_initcall(dma_init);