[PATCH] remove for_each_cpu()
[linux-2.6.git] / arch / sparc64 / mm / generic.c
1 /* $Id: generic.c,v 1.18 2001/12/21 04:56:15 davem Exp $
2  * generic.c: Generic Sparc mm routines that are not dependent upon
3  *            MMU type but are Sparc specific.
4  *
5  * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
6  */
7
8 #include <linux/kernel.h>
9 #include <linux/mm.h>
10 #include <linux/swap.h>
11 #include <linux/pagemap.h>
12
13 #include <asm/pgalloc.h>
14 #include <asm/pgtable.h>
15 #include <asm/page.h>
16 #include <asm/tlbflush.h>
17
18 /* Remap IO memory, the same way as remap_pfn_range(), but use
19  * the obio memory space.
20  *
21  * They use a pgprot that sets PAGE_IO and does not check the
22  * mem_map table as this is independent of normal memory.
23  */
24 static inline void io_remap_pte_range(struct mm_struct *mm, pte_t * pte,
25                                       unsigned long address,
26                                       unsigned long size,
27                                       unsigned long offset, pgprot_t prot,
28                                       int space)
29 {
30         unsigned long end;
31
32         /* clear hack bit that was used as a write_combine side-effect flag */
33         offset &= ~0x1UL;
34         address &= ~PMD_MASK;
35         end = address + size;
36         if (end > PMD_SIZE)
37                 end = PMD_SIZE;
38         do {
39                 pte_t entry;
40                 unsigned long curend = address + PAGE_SIZE;
41                 
42                 entry = mk_pte_io(offset, prot, space, PAGE_SIZE);
43                 if (!(address & 0xffff)) {
44                         if (PAGE_SIZE < (4 * 1024 * 1024) &&
45                             !(address & 0x3fffff) &&
46                             !(offset & 0x3ffffe) &&
47                             end >= address + 0x400000) {
48                                 entry = mk_pte_io(offset, prot, space,
49                                                   4 * 1024 * 1024);
50                                 curend = address + 0x400000;
51                                 offset += 0x400000;
52                         } else if (PAGE_SIZE < (512 * 1024) &&
53                                    !(address & 0x7ffff) &&
54                                    !(offset & 0x7fffe) &&
55                                    end >= address + 0x80000) {
56                                 entry = mk_pte_io(offset, prot, space,
57                                                   512 * 1024 * 1024);
58                                 curend = address + 0x80000;
59                                 offset += 0x80000;
60                         } else if (PAGE_SIZE < (64 * 1024) &&
61                                    !(offset & 0xfffe) &&
62                                    end >= address + 0x10000) {
63                                 entry = mk_pte_io(offset, prot, space,
64                                                   64 * 1024);
65                                 curend = address + 0x10000;
66                                 offset += 0x10000;
67                         } else
68                                 offset += PAGE_SIZE;
69                 } else
70                         offset += PAGE_SIZE;
71
72                 do {
73                         BUG_ON(!pte_none(*pte));
74                         set_pte_at(mm, address, pte, entry);
75                         address += PAGE_SIZE;
76                         pte_val(entry) += PAGE_SIZE;
77                         pte++;
78                 } while (address < curend);
79         } while (address < end);
80 }
81
82 static inline int io_remap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address, unsigned long size,
83         unsigned long offset, pgprot_t prot, int space)
84 {
85         unsigned long end;
86
87         address &= ~PGDIR_MASK;
88         end = address + size;
89         if (end > PGDIR_SIZE)
90                 end = PGDIR_SIZE;
91         offset -= address;
92         do {
93                 pte_t * pte = pte_alloc_map(mm, pmd, address);
94                 if (!pte)
95                         return -ENOMEM;
96                 io_remap_pte_range(mm, pte, address, end - address, address + offset, prot, space);
97                 pte_unmap(pte);
98                 address = (address + PMD_SIZE) & PMD_MASK;
99                 pmd++;
100         } while (address < end);
101         return 0;
102 }
103
104 static inline int io_remap_pud_range(struct mm_struct *mm, pud_t * pud, unsigned long address, unsigned long size,
105         unsigned long offset, pgprot_t prot, int space)
106 {
107         unsigned long end;
108
109         address &= ~PUD_MASK;
110         end = address + size;
111         if (end > PUD_SIZE)
112                 end = PUD_SIZE;
113         offset -= address;
114         do {
115                 pmd_t *pmd = pmd_alloc(mm, pud, address);
116                 if (!pud)
117                         return -ENOMEM;
118                 io_remap_pmd_range(mm, pmd, address, end - address, address + offset, prot, space);
119                 address = (address + PUD_SIZE) & PUD_MASK;
120                 pud++;
121         } while (address < end);
122         return 0;
123 }
124
125 int io_remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
126                 unsigned long pfn, unsigned long size, pgprot_t prot)
127 {
128         int error = 0;
129         pgd_t * dir;
130         unsigned long beg = from;
131         unsigned long end = from + size;
132         struct mm_struct *mm = vma->vm_mm;
133         int space = GET_IOSPACE(pfn);
134         unsigned long offset = GET_PFN(pfn) << PAGE_SHIFT;
135         unsigned long phys_base;
136
137         phys_base = offset | (((unsigned long) space) << 32UL);
138
139         /* See comment in mm/memory.c remap_pfn_range */
140         vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP;
141         vma->vm_pgoff = phys_base >> PAGE_SHIFT;
142
143         offset -= from;
144         dir = pgd_offset(mm, from);
145         flush_cache_range(vma, beg, end);
146
147         while (from < end) {
148                 pud_t *pud = pud_alloc(mm, dir, from);
149                 error = -ENOMEM;
150                 if (!pud)
151                         break;
152                 error = io_remap_pud_range(mm, pud, from, end - from, offset + from, prot, space);
153                 if (error)
154                         break;
155                 from = (from + PGDIR_SIZE) & PGDIR_MASK;
156                 dir++;
157         }
158
159         flush_tlb_range(vma, beg, end);
160         return error;
161 }