memcpy_tile64.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/string.h>
  15. #include <linux/smp.h>
  16. #include <linux/module.h>
  17. #include <linux/uaccess.h>
  18. #include <asm/fixmap.h>
  19. #include <asm/kmap_types.h>
  20. #include <asm/tlbflush.h>
  21. #include <hv/hypervisor.h>
  22. #include <arch/chip.h>
  23. #if !CHIP_HAS_COHERENT_LOCAL_CACHE()
  24. /* Defined in memcpy.S */
  25. extern unsigned long __memcpy_asm(void *to, const void *from, unsigned long n);
  26. extern unsigned long __copy_to_user_inatomic_asm(
  27. void __user *to, const void *from, unsigned long n);
  28. extern unsigned long __copy_from_user_inatomic_asm(
  29. void *to, const void __user *from, unsigned long n);
  30. extern unsigned long __copy_from_user_zeroing_asm(
  31. void *to, const void __user *from, unsigned long n);
  32. typedef unsigned long (*memcpy_t)(void *, const void *, unsigned long);
  33. /* Size above which to consider TLB games for performance */
  34. #define LARGE_COPY_CUTOFF 2048
  35. /* Communicate to the simulator what we are trying to do. */
  36. #define sim_allow_multiple_caching(b) \
  37. __insn_mtspr(SPR_SIM_CONTROL, \
  38. SIM_CONTROL_ALLOW_MULTIPLE_CACHING | ((b) << _SIM_CONTROL_OPERATOR_BITS))
  39. /*
  40. * Copy memory by briefly enabling incoherent cacheline-at-a-time mode.
  41. *
  42. * We set up our own source and destination PTEs that we fully control.
  43. * This is the only way to guarantee that we don't race with another
  44. * thread that is modifying the PTE; we can't afford to try the
  45. * copy_{to,from}_user() technique of catching the interrupt, since
  46. * we must run with interrupts disabled to avoid the risk of some
  47. * other code seeing the incoherent data in our cache. (Recall that
  48. * our cache is indexed by PA, so even if the other code doesn't use
  49. * our KM_MEMCPY virtual addresses, they'll still hit in cache using
  50. * the normal VAs that aren't supposed to hit in cache.)
  51. */
  52. static void memcpy_multicache(void *dest, const void *source,
  53. pte_t dst_pte, pte_t src_pte, int len)
  54. {
  55. int idx;
  56. unsigned long flags, newsrc, newdst;
  57. pmd_t *pmdp;
  58. pte_t *ptep;
  59. int cpu = get_cpu();
  60. /*
  61. * Disable interrupts so that we don't recurse into memcpy()
  62. * in an interrupt handler, nor accidentally reference
  63. * the PA of the source from an interrupt routine. Also
  64. * notify the simulator that we're playing games so we don't
  65. * generate spurious coherency warnings.
  66. */
  67. local_irq_save(flags);
  68. sim_allow_multiple_caching(1);
  69. /* Set up the new dest mapping */
  70. idx = FIX_KMAP_BEGIN + (KM_TYPE_NR * cpu) + KM_MEMCPY0;
  71. newdst = __fix_to_virt(idx) + ((unsigned long)dest & (PAGE_SIZE-1));
  72. pmdp = pmd_offset(pud_offset(pgd_offset_k(newdst), newdst), newdst);
  73. ptep = pte_offset_kernel(pmdp, newdst);
  74. if (pte_val(*ptep) != pte_val(dst_pte)) {
  75. set_pte(ptep, dst_pte);
  76. local_flush_tlb_page(NULL, newdst, PAGE_SIZE);
  77. }
  78. /* Set up the new source mapping */
  79. idx += (KM_MEMCPY0 - KM_MEMCPY1);
  80. src_pte = hv_pte_set_nc(src_pte);
  81. src_pte = hv_pte_clear_writable(src_pte); /* be paranoid */
  82. newsrc = __fix_to_virt(idx) + ((unsigned long)source & (PAGE_SIZE-1));
  83. pmdp = pmd_offset(pud_offset(pgd_offset_k(newsrc), newsrc), newsrc);
  84. ptep = pte_offset_kernel(pmdp, newsrc);
  85. *ptep = src_pte; /* set_pte() would be confused by this */
  86. local_flush_tlb_page(NULL, newsrc, PAGE_SIZE);
  87. /* Actually move the data. */
  88. __memcpy_asm((void *)newdst, (const void *)newsrc, len);
  89. /*
  90. * Remap the source as locally-cached and not OLOC'ed so that
  91. * we can inval without also invaling the remote cpu's cache.
  92. * This also avoids known errata with inv'ing cacheable oloc data.
  93. */
  94. src_pte = hv_pte_set_mode(src_pte, HV_PTE_MODE_CACHE_NO_L3);
  95. src_pte = hv_pte_set_writable(src_pte); /* need write access for inv */
  96. *ptep = src_pte; /* set_pte() would be confused by this */
  97. local_flush_tlb_page(NULL, newsrc, PAGE_SIZE);
  98. /*
  99. * Do the actual invalidation, covering the full L2 cache line
  100. * at the end since __memcpy_asm() is somewhat aggressive.
  101. */
  102. __inv_buffer((void *)newsrc, len);
  103. /*
  104. * We're done: notify the simulator that all is back to normal,
  105. * and re-enable interrupts and pre-emption.
  106. */
  107. sim_allow_multiple_caching(0);
  108. local_irq_restore(flags);
  109. put_cpu();
  110. }
  111. /*
  112. * Identify large copies from remotely-cached memory, and copy them
  113. * via memcpy_multicache() if they look good, otherwise fall back
  114. * to the particular kind of copying passed as the memcpy_t function.
  115. */
  116. static unsigned long fast_copy(void *dest, const void *source, int len,
  117. memcpy_t func)
  118. {
  119. /*
  120. * Check if it's big enough to bother with. We may end up doing a
  121. * small copy via TLB manipulation if we're near a page boundary,
  122. * but presumably we'll make it up when we hit the second page.
  123. */
  124. while (len >= LARGE_COPY_CUTOFF) {
  125. int copy_size, bytes_left_on_page;
  126. pte_t *src_ptep, *dst_ptep;
  127. pte_t src_pte, dst_pte;
  128. struct page *src_page, *dst_page;
  129. /* Is the source page oloc'ed to a remote cpu? */
  130. retry_source:
  131. src_ptep = virt_to_pte(current->mm, (unsigned long)source);
  132. if (src_ptep == NULL)
  133. break;
  134. src_pte = *src_ptep;
  135. if (!hv_pte_get_present(src_pte) ||
  136. !hv_pte_get_readable(src_pte) ||
  137. hv_pte_get_mode(src_pte) != HV_PTE_MODE_CACHE_TILE_L3)
  138. break;
  139. if (get_remote_cache_cpu(src_pte) == smp_processor_id())
  140. break;
  141. src_page = pfn_to_page(hv_pte_get_pfn(src_pte));
  142. get_page(src_page);
  143. if (pte_val(src_pte) != pte_val(*src_ptep)) {
  144. put_page(src_page);
  145. goto retry_source;
  146. }
  147. if (pte_huge(src_pte)) {
  148. /* Adjust the PTE to correspond to a small page */
  149. int pfn = hv_pte_get_pfn(src_pte);
  150. pfn += (((unsigned long)source & (HPAGE_SIZE-1))
  151. >> PAGE_SHIFT);
  152. src_pte = pfn_pte(pfn, src_pte);
  153. src_pte = pte_mksmall(src_pte);
  154. }
  155. /* Is the destination page writable? */
  156. retry_dest:
  157. dst_ptep = virt_to_pte(current->mm, (unsigned long)dest);
  158. if (dst_ptep == NULL) {
  159. put_page(src_page);
  160. break;
  161. }
  162. dst_pte = *dst_ptep;
  163. if (!hv_pte_get_present(dst_pte) ||
  164. !hv_pte_get_writable(dst_pte)) {
  165. put_page(src_page);
  166. break;
  167. }
  168. dst_page = pfn_to_page(hv_pte_get_pfn(dst_pte));
  169. if (dst_page == src_page) {
  170. /*
  171. * Source and dest are on the same page; this
  172. * potentially exposes us to incoherence if any
  173. * part of src and dest overlap on a cache line.
  174. * Just give up rather than trying to be precise.
  175. */
  176. put_page(src_page);
  177. break;
  178. }
  179. get_page(dst_page);
  180. if (pte_val(dst_pte) != pte_val(*dst_ptep)) {
  181. put_page(dst_page);
  182. goto retry_dest;
  183. }
  184. if (pte_huge(dst_pte)) {
  185. /* Adjust the PTE to correspond to a small page */
  186. int pfn = hv_pte_get_pfn(dst_pte);
  187. pfn += (((unsigned long)dest & (HPAGE_SIZE-1))
  188. >> PAGE_SHIFT);
  189. dst_pte = pfn_pte(pfn, dst_pte);
  190. dst_pte = pte_mksmall(dst_pte);
  191. }
  192. /* All looks good: create a cachable PTE and copy from it */
  193. copy_size = len;
  194. bytes_left_on_page =
  195. PAGE_SIZE - (((int)source) & (PAGE_SIZE-1));
  196. if (copy_size > bytes_left_on_page)
  197. copy_size = bytes_left_on_page;
  198. bytes_left_on_page =
  199. PAGE_SIZE - (((int)dest) & (PAGE_SIZE-1));
  200. if (copy_size > bytes_left_on_page)
  201. copy_size = bytes_left_on_page;
  202. memcpy_multicache(dest, source, dst_pte, src_pte, copy_size);
  203. /* Release the pages */
  204. put_page(dst_page);
  205. put_page(src_page);
  206. /* Continue on the next page */
  207. dest += copy_size;
  208. source += copy_size;
  209. len -= copy_size;
  210. }
  211. return func(dest, source, len);
  212. }
  213. void *memcpy(void *to, const void *from, __kernel_size_t n)
  214. {
  215. if (n < LARGE_COPY_CUTOFF)
  216. return (void *)__memcpy_asm(to, from, n);
  217. else
  218. return (void *)fast_copy(to, from, n, __memcpy_asm);
  219. }
  220. unsigned long __copy_to_user_inatomic(void __user *to, const void *from,
  221. unsigned long n)
  222. {
  223. if (n < LARGE_COPY_CUTOFF)
  224. return __copy_to_user_inatomic_asm(to, from, n);
  225. else
  226. return fast_copy(to, from, n, __copy_to_user_inatomic_asm);
  227. }
  228. unsigned long __copy_from_user_inatomic(void *to, const void __user *from,
  229. unsigned long n)
  230. {
  231. if (n < LARGE_COPY_CUTOFF)
  232. return __copy_from_user_inatomic_asm(to, from, n);
  233. else
  234. return fast_copy(to, from, n, __copy_from_user_inatomic_asm);
  235. }
  236. unsigned long __copy_from_user_zeroing(void *to, const void __user *from,
  237. unsigned long n)
  238. {
  239. if (n < LARGE_COPY_CUTOFF)
  240. return __copy_from_user_zeroing_asm(to, from, n);
  241. else
  242. return fast_copy(to, from, n, __copy_from_user_zeroing_asm);
  243. }
  244. #endif /* !CHIP_HAS_COHERENT_LOCAL_CACHE() */