memory.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * linux/arch/m68k/mm/memory.c
  3. *
  4. * Copyright (C) 1995 Hamish Macdonald
  5. */
  6. #include <linux/mm.h>
  7. #include <linux/kernel.h>
  8. #include <linux/string.h>
  9. #include <linux/types.h>
  10. #include <linux/slab.h>
  11. #include <linux/init.h>
  12. #include <linux/pagemap.h>
  13. #include <asm/setup.h>
  14. #include <asm/segment.h>
  15. #include <asm/page.h>
  16. #include <asm/pgalloc.h>
  17. #include <asm/system.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. #ifdef DEBUG_INVALID_PTOV
  106. int mm_inv_cnt = 5;
  107. #endif
  108. #ifndef CONFIG_SINGLE_MEMORY_CHUNK
  109. /*
  110. * The following two routines map from a physical address to a kernel
  111. * virtual address and vice versa.
  112. */
  113. unsigned long mm_vtop(unsigned long vaddr)
  114. {
  115. int i=0;
  116. unsigned long voff = (unsigned long)vaddr - PAGE_OFFSET;
  117. do {
  118. if (voff < m68k_memory[i].size) {
  119. #ifdef DEBUGPV
  120. printk ("VTOP(%p)=%lx\n", vaddr,
  121. m68k_memory[i].addr + voff);
  122. #endif
  123. return m68k_memory[i].addr + voff;
  124. }
  125. voff -= m68k_memory[i].size;
  126. } while (++i < m68k_num_memory);
  127. /* As a special case allow `__pa(high_memory)'. */
  128. if (voff == 0)
  129. return m68k_memory[i-1].addr + m68k_memory[i-1].size;
  130. return -1;
  131. }
  132. #endif
  133. #ifndef CONFIG_SINGLE_MEMORY_CHUNK
  134. unsigned long mm_ptov (unsigned long paddr)
  135. {
  136. int i = 0;
  137. unsigned long poff, voff = PAGE_OFFSET;
  138. do {
  139. poff = paddr - m68k_memory[i].addr;
  140. if (poff < m68k_memory[i].size) {
  141. #ifdef DEBUGPV
  142. printk ("PTOV(%lx)=%lx\n", paddr, poff + voff);
  143. #endif
  144. return poff + voff;
  145. }
  146. voff += m68k_memory[i].size;
  147. } while (++i < m68k_num_memory);
  148. #ifdef DEBUG_INVALID_PTOV
  149. if (mm_inv_cnt > 0) {
  150. mm_inv_cnt--;
  151. printk("Invalid use of phys_to_virt(0x%lx) at 0x%p!\n",
  152. paddr, __builtin_return_address(0));
  153. }
  154. #endif
  155. return -1;
  156. }
  157. #endif
  158. /* invalidate page in both caches */
  159. static inline void clear040(unsigned long paddr)
  160. {
  161. asm volatile (
  162. "nop\n\t"
  163. ".chip 68040\n\t"
  164. "cinvp %%bc,(%0)\n\t"
  165. ".chip 68k"
  166. : : "a" (paddr));
  167. }
  168. /* invalidate page in i-cache */
  169. static inline void cleari040(unsigned long paddr)
  170. {
  171. asm volatile (
  172. "nop\n\t"
  173. ".chip 68040\n\t"
  174. "cinvp %%ic,(%0)\n\t"
  175. ".chip 68k"
  176. : : "a" (paddr));
  177. }
  178. /* push page in both caches */
  179. /* RZ: cpush %bc DOES invalidate %ic, regardless of DPI */
  180. static inline void push040(unsigned long paddr)
  181. {
  182. asm volatile (
  183. "nop\n\t"
  184. ".chip 68040\n\t"
  185. "cpushp %%bc,(%0)\n\t"
  186. ".chip 68k"
  187. : : "a" (paddr));
  188. }
  189. /* push and invalidate page in both caches, must disable ints
  190. * to avoid invalidating valid data */
  191. static inline void pushcl040(unsigned long paddr)
  192. {
  193. unsigned long flags;
  194. local_irq_save(flags);
  195. push040(paddr);
  196. if (CPU_IS_060)
  197. clear040(paddr);
  198. local_irq_restore(flags);
  199. }
  200. /*
  201. * 040: Hit every page containing an address in the range paddr..paddr+len-1.
  202. * (Low order bits of the ea of a CINVP/CPUSHP are "don't care"s).
  203. * Hit every page until there is a page or less to go. Hit the next page,
  204. * and the one after that if the range hits it.
  205. */
  206. /* ++roman: A little bit more care is required here: The CINVP instruction
  207. * invalidates cache entries WITHOUT WRITING DIRTY DATA BACK! So the beginning
  208. * and the end of the region must be treated differently if they are not
  209. * exactly at the beginning or end of a page boundary. Else, maybe too much
  210. * data becomes invalidated and thus lost forever. CPUSHP does what we need:
  211. * it invalidates the page after pushing dirty data to memory. (Thanks to Jes
  212. * for discovering the problem!)
  213. */
  214. /* ... but on the '060, CPUSH doesn't invalidate (for us, since we have set
  215. * the DPI bit in the CACR; would it cause problems with temporarily changing
  216. * this?). So we have to push first and then additionally to invalidate.
  217. */
  218. /*
  219. * cache_clear() semantics: Clear any cache entries for the area in question,
  220. * without writing back dirty entries first. This is useful if the data will
  221. * be overwritten anyway, e.g. by DMA to memory. The range is defined by a
  222. * _physical_ address.
  223. */
  224. void cache_clear (unsigned long paddr, int len)
  225. {
  226. if (CPU_IS_040_OR_060) {
  227. int tmp;
  228. /*
  229. * We need special treatment for the first page, in case it
  230. * is not page-aligned. Page align the addresses to work
  231. * around bug I17 in the 68060.
  232. */
  233. if ((tmp = -paddr & (PAGE_SIZE - 1))) {
  234. pushcl040(paddr & PAGE_MASK);
  235. if ((len -= tmp) <= 0)
  236. return;
  237. paddr += tmp;
  238. }
  239. tmp = PAGE_SIZE;
  240. paddr &= PAGE_MASK;
  241. while ((len -= tmp) >= 0) {
  242. clear040(paddr);
  243. paddr += tmp;
  244. }
  245. if ((len += tmp))
  246. /* a page boundary gets crossed at the end */
  247. pushcl040(paddr);
  248. }
  249. else /* 68030 or 68020 */
  250. asm volatile ("movec %/cacr,%/d0\n\t"
  251. "oriw %0,%/d0\n\t"
  252. "movec %/d0,%/cacr"
  253. : : "i" (FLUSH_I_AND_D)
  254. : "d0");
  255. #ifdef CONFIG_M68K_L2_CACHE
  256. if(mach_l2_flush)
  257. mach_l2_flush(0);
  258. #endif
  259. }
  260. /*
  261. * cache_push() semantics: Write back any dirty cache data in the given area,
  262. * and invalidate the range in the instruction cache. It needs not (but may)
  263. * invalidate those entries also in the data cache. The range is defined by a
  264. * _physical_ address.
  265. */
  266. void cache_push (unsigned long paddr, int len)
  267. {
  268. if (CPU_IS_040_OR_060) {
  269. int tmp = PAGE_SIZE;
  270. /*
  271. * on 68040 or 68060, push cache lines for pages in the range;
  272. * on the '040 this also invalidates the pushed lines, but not on
  273. * the '060!
  274. */
  275. len += paddr & (PAGE_SIZE - 1);
  276. /*
  277. * Work around bug I17 in the 68060 affecting some instruction
  278. * lines not being invalidated properly.
  279. */
  280. paddr &= PAGE_MASK;
  281. do {
  282. push040(paddr);
  283. paddr += tmp;
  284. } while ((len -= tmp) > 0);
  285. }
  286. /*
  287. * 68030/68020 have no writeback cache. On the other hand,
  288. * cache_push is actually a superset of cache_clear (the lines
  289. * get written back and invalidated), so we should make sure
  290. * to perform the corresponding actions. After all, this is getting
  291. * called in places where we've just loaded code, or whatever, so
  292. * flushing the icache is appropriate; flushing the dcache shouldn't
  293. * be required.
  294. */
  295. else /* 68030 or 68020 */
  296. asm volatile ("movec %/cacr,%/d0\n\t"
  297. "oriw %0,%/d0\n\t"
  298. "movec %/d0,%/cacr"
  299. : : "i" (FLUSH_I)
  300. : "d0");
  301. #ifdef CONFIG_M68K_L2_CACHE
  302. if(mach_l2_flush)
  303. mach_l2_flush(1);
  304. #endif
  305. }
  306. #ifndef CONFIG_SINGLE_MEMORY_CHUNK
  307. int mm_end_of_chunk (unsigned long addr, int len)
  308. {
  309. int i;
  310. for (i = 0; i < m68k_num_memory; i++)
  311. if (m68k_memory[i].addr + m68k_memory[i].size == addr + len)
  312. return 1;
  313. return 0;
  314. }
  315. #endif