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