* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / arch / sparc / mm / generic.c
blobea94a8f60aaff5d1c5734b22ec6afb6a4dbf69a6
1 /* $Id: generic.c,v 1.6 1998/10/27 23:28:00 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/pgtable.h>
13 #include <asm/page.h>
16 /* Allocate a block of RAM which is aligned to its size.
17 * This procedure can be used until the call to mem_init().
19 void *sparc_init_alloc(unsigned long *kbrk, unsigned long size)
21 unsigned long mask = size - 1;
22 unsigned long ret;
24 if(!size)
25 return 0x0;
26 if(size & mask) {
27 prom_printf("panic: sparc_init_alloc botch\n");
28 prom_halt();
30 ret = (*kbrk + mask) & ~mask;
31 *kbrk = ret + size;
32 memset((void*) ret, 0, size);
33 return (void*) ret;
36 static inline void forget_pte(pte_t page)
38 if (pte_none(page))
39 return;
40 if (pte_present(page)) {
41 unsigned long addr = pte_page(page);
42 if (MAP_NR(addr) >= max_mapnr || PageReserved(mem_map+MAP_NR(addr)))
43 return;
44 /*
45 * free_page() used to be able to clear swap cache
46 * entries. We may now have to do it manually.
48 free_page_and_swap_cache(addr);
49 return;
51 swap_free(pte_val(page));
54 /* Remap IO memory, the same way as remap_page_range(), but use
55 * the obio memory space.
57 * They use a pgprot that sets PAGE_IO and does not check the
58 * mem_map table as this is independent of normal memory.
60 static inline void io_remap_pte_range(pte_t * pte, unsigned long address, unsigned long size,
61 unsigned long offset, pgprot_t prot, int space)
63 unsigned long end;
65 address &= ~PMD_MASK;
66 end = address + size;
67 if (end > PMD_SIZE)
68 end = PMD_SIZE;
69 do {
70 pte_t oldpage = *pte;
71 pte_clear(pte);
72 set_pte(pte, mk_pte_io(offset, prot, space));
73 forget_pte(oldpage);
74 address += PAGE_SIZE;
75 offset += PAGE_SIZE;
76 pte++;
77 } while (address < end);
80 static inline int io_remap_pmd_range(pmd_t * pmd, unsigned long address, unsigned long size,
81 unsigned long offset, pgprot_t prot, int space)
83 unsigned long end;
85 address &= ~PGDIR_MASK;
86 end = address + size;
87 if (end > PGDIR_SIZE)
88 end = PGDIR_SIZE;
89 offset -= address;
90 do {
91 pte_t * pte = pte_alloc(pmd, address);
92 if (!pte)
93 return -ENOMEM;
94 io_remap_pte_range(pte, address, end - address, address + offset, prot, space);
95 address = (address + PMD_SIZE) & PMD_MASK;
96 pmd++;
97 } while (address < end);
98 return 0;
101 int io_remap_page_range(unsigned long from, unsigned long offset, unsigned long size, pgprot_t prot, int space)
103 int error = 0;
104 pgd_t * dir;
105 unsigned long beg = from;
106 unsigned long end = from + size;
108 prot = __pgprot(pg_iobits);
109 offset -= from;
110 dir = pgd_offset(current->mm, from);
111 flush_cache_range(current->mm, beg, end);
112 while (from < end) {
113 pmd_t *pmd = pmd_alloc(dir, from);
114 error = -ENOMEM;
115 if (!pmd)
116 break;
117 error = io_remap_pmd_range(pmd, from, end - from, offset + from, prot, space);
118 if (error)
119 break;
120 from = (from + PGDIR_SIZE) & PGDIR_MASK;
121 dir++;
123 flush_tlb_range(current->mm, beg, end);
124 return error;