memory.c 9.0 KB

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