consistent.c 6.1 KB

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