memory.c 7.5 KB

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