memory.c 7.6 KB

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