memory.c 7.6 KB

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