consistent.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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/export.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 <linux/dma-noncoherent.h>
  36. #include <asm/pgalloc.h>
  37. #include <linux/io.h>
  38. #include <linux/hardirq.h>
  39. #include <linux/mmu_context.h>
  40. #include <asm/mmu.h>
  41. #include <linux/uaccess.h>
  42. #include <asm/pgtable.h>
  43. #include <asm/cpuinfo.h>
  44. #include <asm/tlbflush.h>
  45. #ifndef CONFIG_MMU
  46. /* I have to use dcache values because I can't relate on ram size */
  47. # define UNCACHED_SHADOW_MASK (cpuinfo.dcache_high - cpuinfo.dcache_base + 1)
  48. #endif
  49. /*
  50. * Consistent memory allocators. Used for DMA devices that want to
  51. * share uncached memory with the processor core.
  52. * My crufty no-MMU approach is simple. In the HW platform we can optionally
  53. * mirror the DDR up above the processor cacheable region. So, memory accessed
  54. * in this mirror region will not be cached. It's alloced from the same
  55. * pool as normal memory, but the handle we return is shifted up into the
  56. * uncached region. This will no doubt cause big problems if memory allocated
  57. * here is not also freed properly. -- JW
  58. */
  59. void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
  60. gfp_t gfp, unsigned long attrs)
  61. {
  62. unsigned long order, vaddr;
  63. void *ret;
  64. unsigned int i, err = 0;
  65. struct page *page, *end;
  66. #ifdef CONFIG_MMU
  67. phys_addr_t pa;
  68. struct vm_struct *area;
  69. unsigned long va;
  70. #endif
  71. if (in_interrupt())
  72. BUG();
  73. /* Only allocate page size areas. */
  74. size = PAGE_ALIGN(size);
  75. order = get_order(size);
  76. vaddr = __get_free_pages(gfp, order);
  77. if (!vaddr)
  78. return NULL;
  79. /*
  80. * we need to ensure that there are no cachelines in use,
  81. * or worse dirty in this area.
  82. */
  83. flush_dcache_range(virt_to_phys((void *)vaddr),
  84. virt_to_phys((void *)vaddr) + size);
  85. #ifndef CONFIG_MMU
  86. ret = (void *)vaddr;
  87. /*
  88. * Here's the magic! Note if the uncached shadow is not implemented,
  89. * it's up to the calling code to also test that condition and make
  90. * other arranegments, such as manually flushing the cache and so on.
  91. */
  92. # ifdef CONFIG_XILINX_UNCACHED_SHADOW
  93. ret = (void *)((unsigned) ret | UNCACHED_SHADOW_MASK);
  94. # endif
  95. if ((unsigned int)ret > cpuinfo.dcache_base &&
  96. (unsigned int)ret < cpuinfo.dcache_high)
  97. pr_warn("ERROR: Your cache coherent area is CACHED!!!\n");
  98. /* dma_handle is same as physical (shadowed) address */
  99. *dma_handle = (dma_addr_t)ret;
  100. #else
  101. /* Allocate some common virtual space to map the new pages. */
  102. area = get_vm_area(size, VM_ALLOC);
  103. if (!area) {
  104. free_pages(vaddr, order);
  105. return NULL;
  106. }
  107. va = (unsigned long) area->addr;
  108. ret = (void *)va;
  109. /* This gives us the real physical address of the first page. */
  110. *dma_handle = pa = __virt_to_phys(vaddr);
  111. #endif
  112. /*
  113. * free wasted pages. We skip the first page since we know
  114. * that it will have count = 1 and won't require freeing.
  115. * We also mark the pages in use as reserved so that
  116. * remap_page_range works.
  117. */
  118. page = virt_to_page(vaddr);
  119. end = page + (1 << order);
  120. split_page(page, order);
  121. for (i = 0; i < size && err == 0; i += PAGE_SIZE) {
  122. #ifdef CONFIG_MMU
  123. /* MS: This is the whole magic - use cache inhibit pages */
  124. err = map_page(va + i, pa + i, _PAGE_KERNEL | _PAGE_NO_CACHE);
  125. #endif
  126. SetPageReserved(page);
  127. page++;
  128. }
  129. /* Free the otherwise unused pages. */
  130. while (page < end) {
  131. __free_page(page);
  132. page++;
  133. }
  134. if (err) {
  135. free_pages(vaddr, order);
  136. return NULL;
  137. }
  138. return ret;
  139. }
  140. #ifdef CONFIG_MMU
  141. static pte_t *consistent_virt_to_pte(void *vaddr)
  142. {
  143. unsigned long addr = (unsigned long)vaddr;
  144. return pte_offset_kernel(pmd_offset(pgd_offset_k(addr), addr), addr);
  145. }
  146. unsigned long consistent_virt_to_pfn(void *vaddr)
  147. {
  148. pte_t *ptep = consistent_virt_to_pte(vaddr);
  149. if (pte_none(*ptep) || !pte_present(*ptep))
  150. return 0;
  151. return pte_pfn(*ptep);
  152. }
  153. #endif
  154. /*
  155. * free page(s) as defined by the above mapping.
  156. */
  157. void arch_dma_free(struct device *dev, size_t size, void *vaddr,
  158. dma_addr_t dma_addr, unsigned long attrs)
  159. {
  160. struct page *page;
  161. if (in_interrupt())
  162. BUG();
  163. size = PAGE_ALIGN(size);
  164. #ifndef CONFIG_MMU
  165. /* Clear SHADOW_MASK bit in address, and free as per usual */
  166. # ifdef CONFIG_XILINX_UNCACHED_SHADOW
  167. vaddr = (void *)((unsigned)vaddr & ~UNCACHED_SHADOW_MASK);
  168. # endif
  169. page = virt_to_page(vaddr);
  170. do {
  171. __free_reserved_page(page);
  172. page++;
  173. } while (size -= PAGE_SIZE);
  174. #else
  175. do {
  176. pte_t *ptep = consistent_virt_to_pte(vaddr);
  177. unsigned long pfn;
  178. if (!pte_none(*ptep) && pte_present(*ptep)) {
  179. pfn = pte_pfn(*ptep);
  180. pte_clear(&init_mm, (unsigned int)vaddr, ptep);
  181. if (pfn_valid(pfn)) {
  182. page = pfn_to_page(pfn);
  183. __free_reserved_page(page);
  184. }
  185. }
  186. vaddr += PAGE_SIZE;
  187. } while (size -= PAGE_SIZE);
  188. /* flush tlb */
  189. flush_tlb_all();
  190. #endif
  191. }