2 * linux/arch/m68k/mm/memory.c
4 * Copyright (C) 1995 Hamish Macdonald
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>
15 #include <asm/setup.h>
16 #include <asm/segment.h>
18 #include <asm/pgalloc.h>
19 #include <asm/system.h>
20 #include <asm/traps.h>
21 #include <asm/machdep.h>
24 /* ++andreas: {get,free}_pointer_table rewritten to use unused fields from
25 struct page instead of separately kmalloced struct. Stolen from
26 arch/sparc/mm/srmmu.c ... */
28 typedef struct list_head ptable_desc
;
29 static LIST_HEAD(ptable_list
);
31 #define PD_PTABLE(page) ((ptable_desc *)&(virt_to_page(page)->lru))
32 #define PD_PAGE(ptable) (list_entry(ptable, struct page, lru))
33 #define PD_MARKBITS(dp) (*(unsigned char *)&PD_PAGE(dp)->index)
35 #define PTABLE_SIZE (PTRS_PER_PMD * sizeof(pmd_t))
37 void __init
init_pointer_table(unsigned long ptable
)
40 unsigned long page
= ptable
& PAGE_MASK
;
41 unsigned char mask
= 1 << ((ptable
- page
)/PTABLE_SIZE
);
44 if (!(PD_MARKBITS(dp
) & mask
)) {
45 PD_MARKBITS(dp
) = 0xff;
46 list_add(dp
, &ptable_list
);
49 PD_MARKBITS(dp
) &= ~mask
;
51 printk("init_pointer_table: %lx, %x\n", ptable
, PD_MARKBITS(dp
));
54 /* unreserve the page so it's possible to free that page */
55 PD_PAGE(dp
)->flags
&= ~(1 << PG_reserved
);
56 init_page_count(PD_PAGE(dp
));
61 pmd_t
*get_pointer_table (void)
63 ptable_desc
*dp
= ptable_list
.next
;
64 unsigned char mask
= PD_MARKBITS (dp
);
69 * For a pointer table for a user process address space, a
70 * table is taken from a page allocated for the purpose. Each
71 * page can hold 8 pointer tables. The page is remapped in
72 * virtual address space to be noncacheable.
78 if (!(page
= (void *)get_zeroed_page(GFP_KERNEL
)))
81 flush_tlb_kernel_page(page
);
84 new = PD_PTABLE(page
);
85 PD_MARKBITS(new) = 0xfe;
86 list_add_tail(new, dp
);
91 for (tmp
= 1, off
= 0; (mask
& tmp
) == 0; tmp
<<= 1, off
+= PTABLE_SIZE
)
93 PD_MARKBITS(dp
) = mask
& ~tmp
;
94 if (!PD_MARKBITS(dp
)) {
95 /* move to end of list */
96 list_move_tail(dp
, &ptable_list
);
98 return (pmd_t
*) (page_address(PD_PAGE(dp
)) + off
);
101 int free_pointer_table (pmd_t
*ptable
)
104 unsigned long page
= (unsigned long)ptable
& PAGE_MASK
;
105 unsigned char mask
= 1 << (((unsigned long)ptable
- page
)/PTABLE_SIZE
);
107 dp
= PD_PTABLE(page
);
108 if (PD_MARKBITS (dp
) & mask
)
109 panic ("table already free!");
111 PD_MARKBITS (dp
) |= mask
;
113 if (PD_MARKBITS(dp
) == 0xff) {
114 /* all tables in page are free, free page */
116 cache_page((void *)page
);
119 } else if (ptable_list
.next
!= dp
) {
121 * move this descriptor to the front of the list, since
122 * it has one or more free tables.
124 list_move(dp
, &ptable_list
);
129 #ifdef DEBUG_INVALID_PTOV
133 #ifndef CONFIG_SINGLE_MEMORY_CHUNK
135 * The following two routines map from a physical address to a kernel
136 * virtual address and vice versa.
138 unsigned long mm_vtop(unsigned long vaddr
)
141 unsigned long voff
= (unsigned long)vaddr
- PAGE_OFFSET
;
144 if (voff
< m68k_memory
[i
].size
) {
146 printk ("VTOP(%p)=%lx\n", vaddr
,
147 m68k_memory
[i
].addr
+ voff
);
149 return m68k_memory
[i
].addr
+ voff
;
151 voff
-= m68k_memory
[i
].size
;
152 } while (++i
< m68k_num_memory
);
154 /* As a special case allow `__pa(high_memory)'. */
156 return m68k_memory
[i
-1].addr
+ m68k_memory
[i
-1].size
;
162 #ifndef CONFIG_SINGLE_MEMORY_CHUNK
163 unsigned long mm_ptov (unsigned long paddr
)
166 unsigned long poff
, voff
= PAGE_OFFSET
;
169 poff
= paddr
- m68k_memory
[i
].addr
;
170 if (poff
< m68k_memory
[i
].size
) {
172 printk ("PTOV(%lx)=%lx\n", paddr
, poff
+ voff
);
176 voff
+= m68k_memory
[i
].size
;
177 } while (++i
< m68k_num_memory
);
179 #ifdef DEBUG_INVALID_PTOV
180 if (mm_inv_cnt
> 0) {
182 printk("Invalid use of phys_to_virt(0x%lx) at 0x%p!\n",
183 paddr
, __builtin_return_address(0));
190 /* invalidate page in both caches */
191 static inline void clear040(unsigned long paddr
)
196 "cinvp %%bc,(%0)\n\t"
201 /* invalidate page in i-cache */
202 static inline void cleari040(unsigned long paddr
)
207 "cinvp %%ic,(%0)\n\t"
212 /* push page in both caches */
213 /* RZ: cpush %bc DOES invalidate %ic, regardless of DPI */
214 static inline void push040(unsigned long paddr
)
219 "cpushp %%bc,(%0)\n\t"
224 /* push and invalidate page in both caches, must disable ints
225 * to avoid invalidating valid data */
226 static inline void pushcl040(unsigned long paddr
)
230 local_irq_save(flags
);
234 local_irq_restore(flags
);
238 * 040: Hit every page containing an address in the range paddr..paddr+len-1.
239 * (Low order bits of the ea of a CINVP/CPUSHP are "don't care"s).
240 * Hit every page until there is a page or less to go. Hit the next page,
241 * and the one after that if the range hits it.
243 /* ++roman: A little bit more care is required here: The CINVP instruction
244 * invalidates cache entries WITHOUT WRITING DIRTY DATA BACK! So the beginning
245 * and the end of the region must be treated differently if they are not
246 * exactly at the beginning or end of a page boundary. Else, maybe too much
247 * data becomes invalidated and thus lost forever. CPUSHP does what we need:
248 * it invalidates the page after pushing dirty data to memory. (Thanks to Jes
249 * for discovering the problem!)
251 /* ... but on the '060, CPUSH doesn't invalidate (for us, since we have set
252 * the DPI bit in the CACR; would it cause problems with temporarily changing
253 * this?). So we have to push first and then additionally to invalidate.
258 * cache_clear() semantics: Clear any cache entries for the area in question,
259 * without writing back dirty entries first. This is useful if the data will
260 * be overwritten anyway, e.g. by DMA to memory. The range is defined by a
261 * _physical_ address.
264 void cache_clear (unsigned long paddr
, int len
)
266 if (CPU_IS_040_OR_060
) {
270 * We need special treatment for the first page, in case it
271 * is not page-aligned. Page align the addresses to work
272 * around bug I17 in the 68060.
274 if ((tmp
= -paddr
& (PAGE_SIZE
- 1))) {
275 pushcl040(paddr
& PAGE_MASK
);
276 if ((len
-= tmp
) <= 0)
282 while ((len
-= tmp
) >= 0) {
287 /* a page boundary gets crossed at the end */
290 else /* 68030 or 68020 */
291 asm volatile ("movec %/cacr,%/d0\n\t"
294 : : "i" (FLUSH_I_AND_D
)
296 #ifdef CONFIG_M68K_L2_CACHE
304 * cache_push() semantics: Write back any dirty cache data in the given area,
305 * and invalidate the range in the instruction cache. It needs not (but may)
306 * invalidate those entries also in the data cache. The range is defined by a
307 * _physical_ address.
310 void cache_push (unsigned long paddr
, int len
)
312 if (CPU_IS_040_OR_060
) {
316 * on 68040 or 68060, push cache lines for pages in the range;
317 * on the '040 this also invalidates the pushed lines, but not on
320 len
+= paddr
& (PAGE_SIZE
- 1);
323 * Work around bug I17 in the 68060 affecting some instruction
324 * lines not being invalidated properly.
331 } while ((len
-= tmp
) > 0);
334 * 68030/68020 have no writeback cache. On the other hand,
335 * cache_push is actually a superset of cache_clear (the lines
336 * get written back and invalidated), so we should make sure
337 * to perform the corresponding actions. After all, this is getting
338 * called in places where we've just loaded code, or whatever, so
339 * flushing the icache is appropriate; flushing the dcache shouldn't
342 else /* 68030 or 68020 */
343 asm volatile ("movec %/cacr,%/d0\n\t"
348 #ifdef CONFIG_M68K_L2_CACHE
354 #ifndef CONFIG_SINGLE_MEMORY_CHUNK
355 int mm_end_of_chunk (unsigned long addr
, int len
)
359 for (i
= 0; i
< m68k_num_memory
; i
++)
360 if (m68k_memory
[i
].addr
+ m68k_memory
[i
].size
== addr
+ len
)