consistent.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Microblaze support for cache consistent memory.
  3. * Copyright (C) 2010 Michal Simek <monstr@monstr.eu>
  4. * Copyright (C) 2010 PetaLogix
  5. * Copyright (C) 2005 John Williams <jwilliams@itee.uq.edu.au>
  6. *
  7. * Based on PowerPC version derived from arch/arm/mm/consistent.c
  8. * Copyright (C) 2001 Dan Malek (dmalek@jlc.net)
  9. * Copyright (C) 2000 Russell King
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/signal.h>
  17. #include <linux/sched.h>
  18. #include <linux/kernel.h>
  19. #include <linux/errno.h>
  20. #include <linux/string.h>
  21. #include <linux/types.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/mman.h>
  24. #include <linux/mm.h>
  25. #include <linux/swap.h>
  26. #include <linux/stddef.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/init.h>
  29. #include <linux/delay.h>
  30. #include <linux/bootmem.h>
  31. #include <linux/highmem.h>
  32. #include <linux/pci.h>
  33. #include <linux/interrupt.h>
  34. #include <asm/pgalloc.h>
  35. #include <linux/io.h>
  36. #include <linux/hardirq.h>
  37. #include <asm/mmu_context.h>
  38. #include <asm/mmu.h>
  39. #include <linux/uaccess.h>
  40. #include <asm/pgtable.h>
  41. #include <asm/cpuinfo.h>
  42. #ifndef CONFIG_MMU
  43. /* I have to use dcache values because I can't relate on ram size */
  44. #define UNCACHED_SHADOW_MASK (cpuinfo.dcache_high - cpuinfo.dcache_base + 1)
  45. /*
  46. * Consistent memory allocators. Used for DMA devices that want to
  47. * share uncached memory with the processor core.
  48. * My crufty no-MMU approach is simple. In the HW platform we can optionally
  49. * mirror the DDR up above the processor cacheable region. So, memory accessed
  50. * in this mirror region will not be cached. It's alloced from the same
  51. * pool as normal memory, but the handle we return is shifted up into the
  52. * uncached region. This will no doubt cause big problems if memory allocated
  53. * here is not also freed properly. -- JW
  54. */
  55. void *consistent_alloc(int gfp, size_t size, dma_addr_t *dma_handle)
  56. {
  57. struct page *page, *end, *free;
  58. unsigned long order;
  59. void *ret, *virt;
  60. if (in_interrupt())
  61. BUG();
  62. size = PAGE_ALIGN(size);
  63. order = get_order(size);
  64. page = alloc_pages(gfp, order);
  65. if (!page)
  66. goto no_page;
  67. /* We could do with a page_to_phys and page_to_bus here. */
  68. virt = page_address(page);
  69. ret = ioremap(virt_to_phys(virt), size);
  70. if (!ret)
  71. goto no_remap;
  72. /*
  73. * Here's the magic! Note if the uncached shadow is not implemented,
  74. * it's up to the calling code to also test that condition and make
  75. * other arranegments, such as manually flushing the cache and so on.
  76. */
  77. #ifdef CONFIG_XILINX_UNCACHED_SHADOW
  78. ret = (void *)((unsigned) ret | UNCACHED_SHADOW_MASK);
  79. #endif
  80. /* dma_handle is same as physical (shadowed) address */
  81. *dma_handle = (dma_addr_t)ret;
  82. /*
  83. * free wasted pages. We skip the first page since we know
  84. * that it will have count = 1 and won't require freeing.
  85. * We also mark the pages in use as reserved so that
  86. * remap_page_range works.
  87. */
  88. page = virt_to_page(virt);
  89. free = page + (size >> PAGE_SHIFT);
  90. end = page + (1 << order);
  91. for (; page < end; page++) {
  92. init_page_count(page);
  93. if (page >= free)
  94. __free_page(page);
  95. else
  96. SetPageReserved(page);
  97. }
  98. return ret;
  99. no_remap:
  100. __free_pages(page, order);
  101. no_page:
  102. return NULL;
  103. }
  104. #else
  105. void *consistent_alloc(int gfp, size_t size, dma_addr_t *dma_handle)
  106. {
  107. int order, err, i;
  108. unsigned long page, va, flags;
  109. phys_addr_t pa;
  110. struct vm_struct *area;
  111. void *ret;
  112. if (in_interrupt())
  113. BUG();
  114. /* Only allocate page size areas. */
  115. size = PAGE_ALIGN(size);
  116. order = get_order(size);
  117. page = __get_free_pages(gfp, order);
  118. if (!page) {
  119. BUG();
  120. return NULL;
  121. }
  122. /*
  123. * we need to ensure that there are no cachelines in use,
  124. * or worse dirty in this area.
  125. */
  126. flush_dcache_range(virt_to_phys(page), virt_to_phys(page) + size);
  127. /* Allocate some common virtual space to map the new pages. */
  128. area = get_vm_area(size, VM_ALLOC);
  129. if (area == NULL) {
  130. free_pages(page, order);
  131. return NULL;
  132. }
  133. va = (unsigned long) area->addr;
  134. ret = (void *)va;
  135. /* This gives us the real physical address of the first page. */
  136. *dma_handle = pa = virt_to_bus((void *)page);
  137. /* MS: This is the whole magic - use cache inhibit pages */
  138. flags = _PAGE_KERNEL | _PAGE_NO_CACHE;
  139. /*
  140. * Set refcount=1 on all pages in an order>0
  141. * allocation so that vfree() will actually
  142. * free all pages that were allocated.
  143. */
  144. if (order > 0) {
  145. struct page *rpage = virt_to_page(page);
  146. for (i = 1; i < (1 << order); i++)
  147. init_page_count(rpage+i);
  148. }
  149. err = 0;
  150. for (i = 0; i < size && err == 0; i += PAGE_SIZE)
  151. err = map_page(va+i, pa+i, flags);
  152. if (err) {
  153. vfree((void *)va);
  154. return NULL;
  155. }
  156. return ret;
  157. }
  158. #endif /* CONFIG_MMU */
  159. EXPORT_SYMBOL(consistent_alloc);
  160. /*
  161. * free page(s) as defined by the above mapping.
  162. */
  163. void consistent_free(void *vaddr)
  164. {
  165. if (in_interrupt())
  166. BUG();
  167. /* Clear SHADOW_MASK bit in address, and free as per usual */
  168. #ifdef CONFIG_XILINX_UNCACHED_SHADOW
  169. vaddr = (void *)((unsigned)vaddr & ~UNCACHED_SHADOW_MASK);
  170. #endif
  171. vfree(vaddr);
  172. }
  173. EXPORT_SYMBOL(consistent_free);
  174. /*
  175. * make an area consistent.
  176. */
  177. void consistent_sync(void *vaddr, size_t size, int direction)
  178. {
  179. unsigned long start;
  180. unsigned long end;
  181. start = (unsigned long)vaddr;
  182. /* Convert start address back down to unshadowed memory region */
  183. #ifdef CONFIG_XILINX_UNCACHED_SHADOW
  184. start &= ~UNCACHED_SHADOW_MASK;
  185. #endif
  186. end = start + size;
  187. switch (direction) {
  188. case PCI_DMA_NONE:
  189. BUG();
  190. case PCI_DMA_FROMDEVICE: /* invalidate only */
  191. flush_dcache_range(start, end);
  192. break;
  193. case PCI_DMA_TODEVICE: /* writeback only */
  194. flush_dcache_range(start, end);
  195. break;
  196. case PCI_DMA_BIDIRECTIONAL: /* writeback and invalidate */
  197. flush_dcache_range(start, end);
  198. break;
  199. }
  200. }
  201. EXPORT_SYMBOL(consistent_sync);
  202. /*
  203. * consistent_sync_page makes memory consistent. identical
  204. * to consistent_sync, but takes a struct page instead of a
  205. * virtual address
  206. */
  207. void consistent_sync_page(struct page *page, unsigned long offset,
  208. size_t size, int direction)
  209. {
  210. unsigned long start = (unsigned long)page_address(page) + offset;
  211. consistent_sync((void *)start, size, direction);
  212. }
  213. EXPORT_SYMBOL(consistent_sync_page);