dma-noncoherent.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * PowerPC version derived from arch/arm/mm/consistent.c
  3. * Copyright (C) 2001 Dan Malek (dmalek@jlc.net)
  4. *
  5. * Copyright (C) 2000 Russell King
  6. *
  7. * Consistent memory allocators. Used for DMA devices that want to
  8. * share uncached memory with the processor core. The function return
  9. * is the virtual address and 'dma_handle' is the physical address.
  10. * Mostly stolen from the ARM port, with some changes for PowerPC.
  11. * -- Dan
  12. *
  13. * Reorganized to get rid of the arch-specific consistent_* functions
  14. * and provide non-coherent implementations for the DMA API. -Matt
  15. *
  16. * Added in_interrupt() safe dma_alloc_coherent()/dma_free_coherent()
  17. * implementation. This is pulled straight from ARM and barely
  18. * modified. -Matt
  19. *
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License version 2 as
  22. * published by the Free Software Foundation.
  23. */
  24. #include <linux/sched.h>
  25. #include <linux/kernel.h>
  26. #include <linux/errno.h>
  27. #include <linux/string.h>
  28. #include <linux/types.h>
  29. #include <linux/highmem.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/vmalloc.h>
  32. #include <asm/tlbflush.h>
  33. /*
  34. * Allocate DMA-coherent memory space and return both the kernel remapped
  35. * virtual and bus address for that space.
  36. */
  37. void *
  38. __dma_alloc_coherent(size_t size, dma_addr_t *handle, gfp_t gfp)
  39. {
  40. struct page *page;
  41. unsigned long order;
  42. int i;
  43. unsigned int nr_pages = PAGE_ALIGN(size)>>PAGE_SHIFT;
  44. unsigned int array_size = nr_pages * sizeof(struct page *);
  45. struct page **pages;
  46. struct page *end;
  47. u64 mask = 0x00ffffff, limit; /* ISA default */
  48. struct vm_struct *area;
  49. BUG_ON(!mem_init_done);
  50. size = PAGE_ALIGN(size);
  51. limit = (mask + 1) & ~mask;
  52. if (limit && size >= limit) {
  53. printk(KERN_WARNING "coherent allocation too big (requested "
  54. "%#x mask %#Lx)\n", size, mask);
  55. return NULL;
  56. }
  57. order = get_order(size);
  58. if (mask != 0xffffffff)
  59. gfp |= GFP_DMA;
  60. page = alloc_pages(gfp, order);
  61. if (!page)
  62. goto no_page;
  63. end = page + (1 << order);
  64. /*
  65. * Invalidate any data that might be lurking in the
  66. * kernel direct-mapped region for device DMA.
  67. */
  68. {
  69. unsigned long kaddr = (unsigned long)page_address(page);
  70. memset(page_address(page), 0, size);
  71. flush_dcache_range(kaddr, kaddr + size);
  72. }
  73. split_page(page, order);
  74. /*
  75. * Set the "dma handle"
  76. */
  77. *handle = page_to_phys(page);
  78. area = get_vm_area_caller(size, VM_IOREMAP,
  79. __builtin_return_address(1));
  80. if (!area)
  81. goto out_free_pages;
  82. if (array_size > PAGE_SIZE) {
  83. pages = vmalloc(array_size);
  84. area->flags |= VM_VPAGES;
  85. } else {
  86. pages = kmalloc(array_size, GFP_KERNEL);
  87. }
  88. if (!pages)
  89. goto out_free_area;
  90. area->pages = pages;
  91. area->nr_pages = nr_pages;
  92. for (i = 0; i < nr_pages; i++)
  93. pages[i] = page + i;
  94. if (map_vm_area(area, pgprot_noncached(PAGE_KERNEL), &pages))
  95. goto out_unmap;
  96. /*
  97. * Free the otherwise unused pages.
  98. */
  99. page += nr_pages;
  100. while (page < end) {
  101. __free_page(page);
  102. page++;
  103. }
  104. return area->addr;
  105. out_unmap:
  106. vunmap(area->addr);
  107. if (array_size > PAGE_SIZE)
  108. vfree(pages);
  109. else
  110. kfree(pages);
  111. goto out_free_pages;
  112. out_free_area:
  113. free_vm_area(area);
  114. out_free_pages:
  115. if (page)
  116. __free_pages(page, order);
  117. no_page:
  118. return NULL;
  119. }
  120. EXPORT_SYMBOL(__dma_alloc_coherent);
  121. /*
  122. * free a page as defined by the above mapping.
  123. */
  124. void __dma_free_coherent(size_t size, void *vaddr)
  125. {
  126. vfree(vaddr);
  127. }
  128. EXPORT_SYMBOL(__dma_free_coherent);
  129. /*
  130. * make an area consistent.
  131. */
  132. void __dma_sync(void *vaddr, size_t size, int direction)
  133. {
  134. unsigned long start = (unsigned long)vaddr;
  135. unsigned long end = start + size;
  136. switch (direction) {
  137. case DMA_NONE:
  138. BUG();
  139. case DMA_FROM_DEVICE:
  140. /*
  141. * invalidate only when cache-line aligned otherwise there is
  142. * the potential for discarding uncommitted data from the cache
  143. */
  144. if ((start & (L1_CACHE_BYTES - 1)) || (size & (L1_CACHE_BYTES - 1)))
  145. flush_dcache_range(start, end);
  146. else
  147. invalidate_dcache_range(start, end);
  148. break;
  149. case DMA_TO_DEVICE: /* writeback only */
  150. clean_dcache_range(start, end);
  151. break;
  152. case DMA_BIDIRECTIONAL: /* writeback and invalidate */
  153. flush_dcache_range(start, end);
  154. break;
  155. }
  156. }
  157. EXPORT_SYMBOL(__dma_sync);
  158. #ifdef CONFIG_HIGHMEM
  159. /*
  160. * __dma_sync_page() implementation for systems using highmem.
  161. * In this case, each page of a buffer must be kmapped/kunmapped
  162. * in order to have a virtual address for __dma_sync(). This must
  163. * not sleep so kmap_atomic()/kunmap_atomic() are used.
  164. *
  165. * Note: yes, it is possible and correct to have a buffer extend
  166. * beyond the first page.
  167. */
  168. static inline void __dma_sync_page_highmem(struct page *page,
  169. unsigned long offset, size_t size, int direction)
  170. {
  171. size_t seg_size = min((size_t)(PAGE_SIZE - offset), size);
  172. size_t cur_size = seg_size;
  173. unsigned long flags, start, seg_offset = offset;
  174. int nr_segs = 1 + ((size - seg_size) + PAGE_SIZE - 1)/PAGE_SIZE;
  175. int seg_nr = 0;
  176. local_irq_save(flags);
  177. do {
  178. start = (unsigned long)kmap_atomic(page + seg_nr,
  179. KM_PPC_SYNC_PAGE) + seg_offset;
  180. /* Sync this buffer segment */
  181. __dma_sync((void *)start, seg_size, direction);
  182. kunmap_atomic((void *)start, KM_PPC_SYNC_PAGE);
  183. seg_nr++;
  184. /* Calculate next buffer segment size */
  185. seg_size = min((size_t)PAGE_SIZE, size - cur_size);
  186. /* Add the segment size to our running total */
  187. cur_size += seg_size;
  188. seg_offset = 0;
  189. } while (seg_nr < nr_segs);
  190. local_irq_restore(flags);
  191. }
  192. #endif /* CONFIG_HIGHMEM */
  193. /*
  194. * __dma_sync_page makes memory consistent. identical to __dma_sync, but
  195. * takes a struct page instead of a virtual address
  196. */
  197. void __dma_sync_page(struct page *page, unsigned long offset,
  198. size_t size, int direction)
  199. {
  200. #ifdef CONFIG_HIGHMEM
  201. __dma_sync_page_highmem(page, offset, size, direction);
  202. #else
  203. unsigned long start = (unsigned long)page_address(page) + offset;
  204. __dma_sync((void *)start, size, direction);
  205. #endif
  206. }
  207. EXPORT_SYMBOL(__dma_sync_page);