dma-mapping.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Dynamic DMA mapping support
  3. *
  4. * Copyright 2005-2009 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later
  7. */
  8. #include <linux/types.h>
  9. #include <linux/gfp.h>
  10. #include <linux/string.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/scatterlist.h>
  14. #include <linux/export.h>
  15. #include <linux/bitmap.h>
  16. static spinlock_t dma_page_lock;
  17. static unsigned long *dma_page;
  18. static unsigned int dma_pages;
  19. static unsigned long dma_base;
  20. static unsigned long dma_size;
  21. static unsigned int dma_initialized;
  22. static void dma_alloc_init(unsigned long start, unsigned long end)
  23. {
  24. spin_lock_init(&dma_page_lock);
  25. dma_initialized = 0;
  26. dma_page = (unsigned long *)__get_free_page(GFP_KERNEL);
  27. memset(dma_page, 0, PAGE_SIZE);
  28. dma_base = PAGE_ALIGN(start);
  29. dma_size = PAGE_ALIGN(end) - PAGE_ALIGN(start);
  30. dma_pages = dma_size >> PAGE_SHIFT;
  31. memset((void *)dma_base, 0, DMA_UNCACHED_REGION);
  32. dma_initialized = 1;
  33. printk(KERN_INFO "%s: dma_page @ 0x%p - %d pages at 0x%08lx\n", __func__,
  34. dma_page, dma_pages, dma_base);
  35. }
  36. static inline unsigned int get_pages(size_t size)
  37. {
  38. return ((size - 1) >> PAGE_SHIFT) + 1;
  39. }
  40. static unsigned long __alloc_dma_pages(unsigned int pages)
  41. {
  42. unsigned long ret = 0, flags;
  43. unsigned long start;
  44. if (dma_initialized == 0)
  45. dma_alloc_init(_ramend - DMA_UNCACHED_REGION, _ramend);
  46. spin_lock_irqsave(&dma_page_lock, flags);
  47. start = bitmap_find_next_zero_area(dma_page, dma_pages, 0, pages, 0);
  48. if (start < dma_pages) {
  49. ret = dma_base + (start << PAGE_SHIFT);
  50. bitmap_set(dma_page, start, pages);
  51. }
  52. spin_unlock_irqrestore(&dma_page_lock, flags);
  53. return ret;
  54. }
  55. static void __free_dma_pages(unsigned long addr, unsigned int pages)
  56. {
  57. unsigned long page = (addr - dma_base) >> PAGE_SHIFT;
  58. unsigned long flags;
  59. if ((page + pages) > dma_pages) {
  60. printk(KERN_ERR "%s: freeing outside range.\n", __func__);
  61. BUG();
  62. }
  63. spin_lock_irqsave(&dma_page_lock, flags);
  64. bitmap_clear(dma_page, page, pages);
  65. spin_unlock_irqrestore(&dma_page_lock, flags);
  66. }
  67. void *dma_alloc_coherent(struct device *dev, size_t size,
  68. dma_addr_t *dma_handle, gfp_t gfp)
  69. {
  70. void *ret;
  71. ret = (void *)__alloc_dma_pages(get_pages(size));
  72. if (ret) {
  73. memset(ret, 0, size);
  74. *dma_handle = virt_to_phys(ret);
  75. }
  76. return ret;
  77. }
  78. EXPORT_SYMBOL(dma_alloc_coherent);
  79. void
  80. dma_free_coherent(struct device *dev, size_t size, void *vaddr,
  81. dma_addr_t dma_handle)
  82. {
  83. __free_dma_pages((unsigned long)vaddr, get_pages(size));
  84. }
  85. EXPORT_SYMBOL(dma_free_coherent);
  86. /*
  87. * Streaming DMA mappings
  88. */
  89. void __dma_sync(dma_addr_t addr, size_t size,
  90. enum dma_data_direction dir)
  91. {
  92. __dma_sync_inline(addr, size, dir);
  93. }
  94. EXPORT_SYMBOL(__dma_sync);
  95. int
  96. dma_map_sg(struct device *dev, struct scatterlist *sg_list, int nents,
  97. enum dma_data_direction direction)
  98. {
  99. struct scatterlist *sg;
  100. int i;
  101. for_each_sg(sg_list, sg, nents, i) {
  102. sg->dma_address = (dma_addr_t) sg_virt(sg);
  103. __dma_sync(sg_dma_address(sg), sg_dma_len(sg), direction);
  104. }
  105. return nents;
  106. }
  107. EXPORT_SYMBOL(dma_map_sg);
  108. void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg_list,
  109. int nelems, enum dma_data_direction direction)
  110. {
  111. struct scatterlist *sg;
  112. int i;
  113. for_each_sg(sg_list, sg, nelems, i) {
  114. sg->dma_address = (dma_addr_t) sg_virt(sg);
  115. __dma_sync(sg_dma_address(sg), sg_dma_len(sg), direction);
  116. }
  117. }
  118. EXPORT_SYMBOL(dma_sync_sg_for_device);