Import 2.3.41pre2
[davej-history.git] / arch / sparc64 / mm / generic.c
blob9a1ab1de33350cda8f0b8bba101e774d8a066615
1 /* $Id: generic.c,v 1.13 1999/12/20 05:02:33 davem Exp $
2 * generic.c: Generic Sparc mm routines that are not dependent upon
3 * MMU type but are Sparc specific.
5 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
6 */
8 #include <linux/kernel.h>
9 #include <linux/mm.h>
10 #include <linux/swap.h>
12 #include <asm/pgalloc.h>
13 #include <asm/pgtable.h>
14 #include <asm/page.h>
16 static inline void forget_pte(pte_t page)
18 if (pte_none(page))
19 return;
20 if (pte_present(page)) {
21 unsigned long nr = pte_pagenr(page);
22 if (nr >= max_mapnr || PageReserved(mem_map+nr))
23 return;
24 /*
25 * free_page() used to be able to clear swap cache
26 * entries. We may now have to do it manually.
28 free_page_and_swap_cache(mem_map+nr);
29 return;
31 swap_free(pte_to_swp_entry(page));
34 /* Remap IO memory, the same way as remap_page_range(), but use
35 * the obio memory space.
37 * They use a pgprot that sets PAGE_IO and does not check the
38 * mem_map table as this is independent of normal memory.
40 * As a special hack if the lowest bit of offset is set the
41 * side-effect bit will be turned off. This is used as a
42 * performance improvement on FFB/AFB. -DaveM
44 static inline void io_remap_pte_range(pte_t * pte, unsigned long address, unsigned long size,
45 unsigned long offset, pgprot_t prot, int space)
47 unsigned long end;
49 address &= ~PMD_MASK;
50 end = address + size;
51 if (end > PMD_SIZE)
52 end = PMD_SIZE;
53 do {
54 pte_t oldpage;
55 pte_t entry;
56 unsigned long curend = address + PAGE_SIZE;
58 entry = mk_pte_io((offset & ~(0x1UL)), prot, space);
59 if (!(address & 0xffff)) {
60 if (!(address & 0x3fffff) && !(offset & 0x3ffffe) && end >= address + 0x400000) {
61 entry = mk_pte_io((offset & ~(0x1UL)),
62 __pgprot(pgprot_val (prot) | _PAGE_SZ4MB),
63 space);
64 curend = address + 0x400000;
65 offset += 0x400000;
66 } else if (!(address & 0x7ffff) && !(offset & 0x7fffe) && end >= address + 0x80000) {
67 entry = mk_pte_io((offset & ~(0x1UL)),
68 __pgprot(pgprot_val (prot) | _PAGE_SZ512K),
69 space);
70 curend = address + 0x80000;
71 offset += 0x80000;
72 } else if (!(offset & 0xfffe) && end >= address + 0x10000) {
73 entry = mk_pte_io((offset & ~(0x1UL)),
74 __pgprot(pgprot_val (prot) | _PAGE_SZ64K),
75 space);
76 curend = address + 0x10000;
77 offset += 0x10000;
78 } else
79 offset += PAGE_SIZE;
80 } else
81 offset += PAGE_SIZE;
83 if (offset & 0x1UL)
84 pte_val(entry) &= ~(_PAGE_E);
85 do {
86 oldpage = *pte;
87 pte_clear(pte);
88 set_pte(pte, entry);
89 forget_pte(oldpage);
90 address += PAGE_SIZE;
91 pte++;
92 } while (address < curend);
93 } while (address < end);
96 static inline int io_remap_pmd_range(pmd_t * pmd, unsigned long address, unsigned long size,
97 unsigned long offset, pgprot_t prot, int space)
99 unsigned long end;
101 address &= ~PGDIR_MASK;
102 end = address + size;
103 if (end > PGDIR_SIZE)
104 end = PGDIR_SIZE;
105 offset -= address;
106 do {
107 pte_t * pte = pte_alloc(pmd, address);
108 if (!pte)
109 return -ENOMEM;
110 spin_lock(&current->mm->page_table_lock);
111 io_remap_pte_range(pte, address, end - address, address + offset, prot, space);
112 spin_unlock(&current->mm->page_table_lock);
113 address = (address + PMD_SIZE) & PMD_MASK;
114 pmd++;
115 } while (address < end);
116 return 0;
119 int io_remap_page_range(unsigned long from, unsigned long offset, unsigned long size, pgprot_t prot, int space)
121 int error = 0;
122 pgd_t * dir;
123 unsigned long beg = from;
124 unsigned long end = from + size;
126 prot = __pgprot(pg_iobits);
127 offset -= from;
128 dir = pgd_offset(current->mm, from);
129 flush_cache_range(current->mm, beg, end);
130 while (from < end) {
131 pmd_t *pmd = pmd_alloc(dir, from);
132 error = -ENOMEM;
133 if (!pmd)
134 break;
135 error = io_remap_pmd_range(pmd, from, end - from, offset + from, prot, space);
136 if (error)
137 break;
138 from = (from + PGDIR_SIZE) & PGDIR_MASK;
139 dir++;
141 flush_tlb_range(current->mm, beg, end);
142 return error;