dma.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. struct dma_attrs *attrs)
  37. {
  38. dma_addr_t handle = virt_to_phys(page_address(page) + offset);
  39. c6x_dma_sync(handle, size, dir);
  40. return handle;
  41. }
  42. static void c6x_dma_unmap_page(struct device *dev, dma_addr_t handle,
  43. size_t size, enum dma_data_direction dir, struct dma_attrs *attrs)
  44. {
  45. c6x_dma_sync(handle, size, dir);
  46. }
  47. static int c6x_dma_map_sg(struct device *dev, struct scatterlist *sglist,
  48. int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
  49. {
  50. struct scatterlist *sg;
  51. int i;
  52. for_each_sg(sglist, sg, nents, i) {
  53. sg->dma_address = sg_phys(sg);
  54. c6x_dma_sync(sg->dma_address, sg->length, dir);
  55. }
  56. return nents;
  57. }
  58. static void c6x_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
  59. int nents, enum dma_data_direction dir,
  60. struct dma_attrs *attrs)
  61. {
  62. struct scatterlist *sg;
  63. int i;
  64. for_each_sg(sglist, sg, nents, i)
  65. c6x_dma_sync(sg_dma_address(sg), sg->length, dir);
  66. }
  67. static void c6x_dma_sync_single_for_cpu(struct device *dev, dma_addr_t handle,
  68. size_t size, enum dma_data_direction dir)
  69. {
  70. c6x_dma_sync(handle, size, dir);
  71. }
  72. static void c6x_dma_sync_single_for_device(struct device *dev,
  73. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  74. {
  75. c6x_dma_sync(handle, size, dir);
  76. }
  77. static void c6x_dma_sync_sg_for_cpu(struct device *dev,
  78. struct scatterlist *sglist, int nents,
  79. enum dma_data_direction dir)
  80. {
  81. struct scatterlist *sg;
  82. int i;
  83. for_each_sg(sglist, sg, nents, i)
  84. c6x_dma_sync_single_for_cpu(dev, sg_dma_address(sg),
  85. sg->length, dir);
  86. }
  87. static void c6x_dma_sync_sg_for_device(struct device *dev,
  88. struct scatterlist *sglist, int nents,
  89. enum dma_data_direction dir)
  90. {
  91. struct scatterlist *sg;
  92. int i;
  93. for_each_sg(sglist, sg, nents, i)
  94. c6x_dma_sync_single_for_device(dev, sg_dma_address(sg),
  95. sg->length, dir);
  96. }
  97. struct dma_map_ops c6x_dma_ops = {
  98. .alloc = c6x_dma_alloc,
  99. .free = c6x_dma_free,
  100. .map_page = c6x_dma_map_page,
  101. .unmap_page = c6x_dma_unmap_page,
  102. .map_sg = c6x_dma_map_sg,
  103. .unmap_sg = c6x_dma_unmap_sg,
  104. .sync_single_for_device = c6x_dma_sync_single_for_device,
  105. .sync_single_for_cpu = c6x_dma_sync_single_for_cpu,
  106. .sync_sg_for_device = c6x_dma_sync_sg_for_device,
  107. .sync_sg_for_cpu = c6x_dma_sync_sg_for_cpu,
  108. };
  109. EXPORT_SYMBOL(c6x_dma_ops);
  110. /* Number of entries preallocated for DMA-API debugging */
  111. #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
  112. static int __init dma_init(void)
  113. {
  114. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  115. return 0;
  116. }
  117. fs_initcall(dma_init);