memory.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/arch/m68k/mm/memory.c
  4. *
  5. * Copyright (C) 1995 Hamish Macdonald
  6. */
  7. #include <linux/module.h>
  8. #include <linux/mm.h>
  9. #include <linux/kernel.h>
  10. #include <linux/string.h>
  11. #include <linux/types.h>
  12. #include <linux/init.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/gfp.h>
  15. #include <asm/setup.h>
  16. #include <asm/segment.h>
  17. #include <asm/page.h>
  18. #include <asm/pgalloc.h>
  19. #include <asm/traps.h>
  20. #include <asm/machdep.h>
  21. /* ++andreas: {get,free}_pointer_table rewritten to use unused fields from
  22. struct page instead of separately kmalloced struct. Stolen from
  23. arch/sparc/mm/srmmu.c ... */
  24. typedef struct list_head ptable_desc;
  25. static LIST_HEAD(ptable_list);
  26. #define PD_PTABLE(page) ((ptable_desc *)&(virt_to_page(page)->lru))
  27. #define PD_PAGE(ptable) (list_entry(ptable, struct page, lru))
  28. #define PD_MARKBITS(dp) (*(unsigned char *)&PD_PAGE(dp)->index)
  29. #define PTABLE_SIZE (PTRS_PER_PMD * sizeof(pmd_t))
  30. void __init init_pointer_table(unsigned long ptable)
  31. {
  32. ptable_desc *dp;
  33. unsigned long page = ptable & PAGE_MASK;
  34. unsigned char mask = 1 << ((ptable - page)/PTABLE_SIZE);
  35. dp = PD_PTABLE(page);
  36. if (!(PD_MARKBITS(dp) & mask)) {
  37. PD_MARKBITS(dp) = 0xff;
  38. list_add(dp, &ptable_list);
  39. }
  40. PD_MARKBITS(dp) &= ~mask;
  41. pr_debug("init_pointer_table: %lx, %x\n", ptable, PD_MARKBITS(dp));
  42. /* unreserve the page so it's possible to free that page */
  43. PD_PAGE(dp)->flags &= ~(1 << PG_reserved);
  44. init_page_count(PD_PAGE(dp));
  45. return;
  46. }
  47. pmd_t *get_pointer_table (void)
  48. {
  49. ptable_desc *dp = ptable_list.next;
  50. unsigned char mask = PD_MARKBITS (dp);
  51. unsigned char tmp;
  52. unsigned int off;
  53. /*
  54. * For a pointer table for a user process address space, a
  55. * table is taken from a page allocated for the purpose. Each
  56. * page can hold 8 pointer tables. The page is remapped in
  57. * virtual address space to be noncacheable.
  58. */
  59. if (mask == 0) {
  60. void *page;
  61. ptable_desc *new;
  62. if (!(page = (void *)get_zeroed_page(GFP_KERNEL)))
  63. return NULL;
  64. flush_tlb_kernel_page(page);
  65. nocache_page(page);
  66. new = PD_PTABLE(page);
  67. PD_MARKBITS(new) = 0xfe;
  68. list_add_tail(new, dp);
  69. return (pmd_t *)page;
  70. }
  71. for (tmp = 1, off = 0; (mask & tmp) == 0; tmp <<= 1, off += PTABLE_SIZE)
  72. ;
  73. PD_MARKBITS(dp) = mask & ~tmp;
  74. if (!PD_MARKBITS(dp)) {
  75. /* move to end of list */
  76. list_move_tail(dp, &ptable_list);
  77. }
  78. return (pmd_t *) (page_address(PD_PAGE(dp)) + off);
  79. }
  80. int free_pointer_table (pmd_t *ptable)
  81. {
  82. ptable_desc *dp;
  83. unsigned long page = (unsigned long)ptable & PAGE_MASK;
  84. unsigned char mask = 1 << (((unsigned long)ptable - page)/PTABLE_SIZE);
  85. dp = PD_PTABLE(page);
  86. if (PD_MARKBITS (dp) & mask)
  87. panic ("table already free!");
  88. PD_MARKBITS (dp) |= mask;
  89. if (PD_MARKBITS(dp) == 0xff) {
  90. /* all tables in page are free, free page */
  91. list_del(dp);
  92. cache_page((void *)page);
  93. free_page (page);
  94. return 1;
  95. } else if (ptable_list.next != dp) {
  96. /*
  97. * move this descriptor to the front of the list, since
  98. * it has one or more free tables.
  99. */
  100. list_move(dp, &ptable_list);
  101. }
  102. return 0;
  103. }
  104. /* invalidate page in both caches */
  105. static inline void clear040(unsigned long paddr)
  106. {
  107. asm volatile (
  108. "nop\n\t"
  109. ".chip 68040\n\t"
  110. "cinvp %%bc,(%0)\n\t"
  111. ".chip 68k"
  112. : : "a" (paddr));
  113. }
  114. /* invalidate page in i-cache */
  115. static inline void cleari040(unsigned long paddr)
  116. {
  117. asm volatile (
  118. "nop\n\t"
  119. ".chip 68040\n\t"
  120. "cinvp %%ic,(%0)\n\t"
  121. ".chip 68k"
  122. : : "a" (paddr));
  123. }
  124. /* push page in both caches */
  125. /* RZ: cpush %bc DOES invalidate %ic, regardless of DPI */
  126. static inline void push040(unsigned long paddr)
  127. {
  128. asm volatile (
  129. "nop\n\t"
  130. ".chip 68040\n\t"
  131. "cpushp %%bc,(%0)\n\t"
  132. ".chip 68k"
  133. : : "a" (paddr));
  134. }
  135. /* push and invalidate page in both caches, must disable ints
  136. * to avoid invalidating valid data */
  137. static inline void pushcl040(unsigned long paddr)
  138. {
  139. unsigned long flags;
  140. local_irq_save(flags);
  141. push040(paddr);
  142. if (CPU_IS_060)
  143. clear040(paddr);
  144. local_irq_restore(flags);
  145. }
  146. /*
  147. * 040: Hit every page containing an address in the range paddr..paddr+len-1.
  148. * (Low order bits of the ea of a CINVP/CPUSHP are "don't care"s).
  149. * Hit every page until there is a page or less to go. Hit the next page,
  150. * and the one after that if the range hits it.
  151. */
  152. /* ++roman: A little bit more care is required here: The CINVP instruction
  153. * invalidates cache entries WITHOUT WRITING DIRTY DATA BACK! So the beginning
  154. * and the end of the region must be treated differently if they are not
  155. * exactly at the beginning or end of a page boundary. Else, maybe too much
  156. * data becomes invalidated and thus lost forever. CPUSHP does what we need:
  157. * it invalidates the page after pushing dirty data to memory. (Thanks to Jes
  158. * for discovering the problem!)
  159. */
  160. /* ... but on the '060, CPUSH doesn't invalidate (for us, since we have set
  161. * the DPI bit in the CACR; would it cause problems with temporarily changing
  162. * this?). So we have to push first and then additionally to invalidate.
  163. */
  164. /*
  165. * cache_clear() semantics: Clear any cache entries for the area in question,
  166. * without writing back dirty entries first. This is useful if the data will
  167. * be overwritten anyway, e.g. by DMA to memory. The range is defined by a
  168. * _physical_ address.
  169. */
  170. void cache_clear (unsigned long paddr, int len)
  171. {
  172. if (CPU_IS_COLDFIRE) {
  173. clear_cf_bcache(0, DCACHE_MAX_ADDR);
  174. } else if (CPU_IS_040_OR_060) {
  175. int tmp;
  176. /*
  177. * We need special treatment for the first page, in case it
  178. * is not page-aligned. Page align the addresses to work
  179. * around bug I17 in the 68060.
  180. */
  181. if ((tmp = -paddr & (PAGE_SIZE - 1))) {
  182. pushcl040(paddr & PAGE_MASK);
  183. if ((len -= tmp) <= 0)
  184. return;
  185. paddr += tmp;
  186. }
  187. tmp = PAGE_SIZE;
  188. paddr &= PAGE_MASK;
  189. while ((len -= tmp) >= 0) {
  190. clear040(paddr);
  191. paddr += tmp;
  192. }
  193. if ((len += tmp))
  194. /* a page boundary gets crossed at the end */
  195. pushcl040(paddr);
  196. }
  197. else /* 68030 or 68020 */
  198. asm volatile ("movec %/cacr,%/d0\n\t"
  199. "oriw %0,%/d0\n\t"
  200. "movec %/d0,%/cacr"
  201. : : "i" (FLUSH_I_AND_D)
  202. : "d0");
  203. #ifdef CONFIG_M68K_L2_CACHE
  204. if(mach_l2_flush)
  205. mach_l2_flush(0);
  206. #endif
  207. }
  208. EXPORT_SYMBOL(cache_clear);
  209. /*
  210. * cache_push() semantics: Write back any dirty cache data in the given area,
  211. * and invalidate the range in the instruction cache. It needs not (but may)
  212. * invalidate those entries also in the data cache. The range is defined by a
  213. * _physical_ address.
  214. */
  215. void cache_push (unsigned long paddr, int len)
  216. {
  217. if (CPU_IS_COLDFIRE) {
  218. flush_cf_bcache(0, DCACHE_MAX_ADDR);
  219. } else if (CPU_IS_040_OR_060) {
  220. int tmp = PAGE_SIZE;
  221. /*
  222. * on 68040 or 68060, push cache lines for pages in the range;
  223. * on the '040 this also invalidates the pushed lines, but not on
  224. * the '060!
  225. */
  226. len += paddr & (PAGE_SIZE - 1);
  227. /*
  228. * Work around bug I17 in the 68060 affecting some instruction
  229. * lines not being invalidated properly.
  230. */
  231. paddr &= PAGE_MASK;
  232. do {
  233. push040(paddr);
  234. paddr += tmp;
  235. } while ((len -= tmp) > 0);
  236. }
  237. /*
  238. * 68030/68020 have no writeback cache. On the other hand,
  239. * cache_push is actually a superset of cache_clear (the lines
  240. * get written back and invalidated), so we should make sure
  241. * to perform the corresponding actions. After all, this is getting
  242. * called in places where we've just loaded code, or whatever, so
  243. * flushing the icache is appropriate; flushing the dcache shouldn't
  244. * be required.
  245. */
  246. else /* 68030 or 68020 */
  247. asm volatile ("movec %/cacr,%/d0\n\t"
  248. "oriw %0,%/d0\n\t"
  249. "movec %/d0,%/cacr"
  250. : : "i" (FLUSH_I)
  251. : "d0");
  252. #ifdef CONFIG_M68K_L2_CACHE
  253. if(mach_l2_flush)
  254. mach_l2_flush(1);
  255. #endif
  256. }
  257. EXPORT_SYMBOL(cache_push);