allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / mm / highmem.c
blob9d3f8a6d83747b7f1bd3efeaddaa56d9e70dccee
1 /*
2 * High memory handling common code and variables.
4 * (C) 1999 Andrea Arcangeli, SuSE GmbH, andrea@suse.de
5 * Gerhard Wichert, Siemens AG, Gerhard.Wichert@pdb.siemens.de
8 * Redesigned the x86 32-bit VM architecture to deal with
9 * 64-bit physical space. With current x86 CPUs this
10 * means up to 64 Gigabytes physical RAM.
12 * Rewrote high memory support to move the page cache into
13 * high memory. Implemented permanent (schedulable) kmaps
14 * based on Linus' idea.
16 * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
19 #include <linux/mm.h>
20 #include <linux/module.h>
21 #include <linux/swap.h>
22 #include <linux/bio.h>
23 #include <linux/pagemap.h>
24 #include <linux/mempool.h>
25 #include <linux/blkdev.h>
26 #include <linux/init.h>
27 #include <linux/hash.h>
28 #include <linux/highmem.h>
29 #include <linux/blktrace_api.h>
30 #include <asm/tlbflush.h>
33 * Virtual_count is not a pure "count".
34 * 0 means that it is not mapped, and has not been mapped
35 * since a TLB flush - it is usable.
36 * 1 means that there are no users, but it has been mapped
37 * since the last TLB flush - so we can't use it.
38 * n means that there are (n-1) current users of it.
41 #ifdef CONFIG_HIGHMEM
43 unsigned long totalhigh_pages __read_mostly;
45 unsigned int nr_free_highpages (void)
47 pg_data_t *pgdat;
48 unsigned int pages = 0;
50 for_each_online_pgdat(pgdat)
51 pages += zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
52 NR_FREE_PAGES);
54 return pages;
57 static int pkmap_count[LAST_PKMAP];
58 static unsigned int last_pkmap_nr;
59 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
61 pte_t * pkmap_page_table;
63 static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
65 static void flush_all_zero_pkmaps(void)
67 int i;
69 flush_cache_kmaps();
71 for (i = 0; i < LAST_PKMAP; i++) {
72 struct page *page;
75 * zero means we don't have anything to do,
76 * >1 means that it is still in use. Only
77 * a count of 1 means that it is free but
78 * needs to be unmapped
80 if (pkmap_count[i] != 1)
81 continue;
82 pkmap_count[i] = 0;
84 /* sanity check */
85 BUG_ON(pte_none(pkmap_page_table[i]));
88 * Don't need an atomic fetch-and-clear op here;
89 * no-one has the page mapped, and cannot get at
90 * its virtual address (and hence PTE) without first
91 * getting the kmap_lock (which is held here).
92 * So no dangers, even with speculative execution.
94 page = pte_page(pkmap_page_table[i]);
95 pte_clear(&init_mm, (unsigned long)page_address(page),
96 &pkmap_page_table[i]);
98 set_page_address(page, NULL);
100 flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
103 /* Flush all unused kmap mappings in order to remove stray
104 mappings. */
105 void kmap_flush_unused(void)
107 spin_lock(&kmap_lock);
108 flush_all_zero_pkmaps();
109 spin_unlock(&kmap_lock);
112 static inline unsigned long map_new_virtual(struct page *page)
114 unsigned long vaddr;
115 int count;
117 start:
118 count = LAST_PKMAP;
119 /* Find an empty entry */
120 for (;;) {
121 last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
122 if (!last_pkmap_nr) {
123 flush_all_zero_pkmaps();
124 count = LAST_PKMAP;
126 if (!pkmap_count[last_pkmap_nr]) {
127 if (cpu_has_dc_aliases) {
128 unsigned int pfn, map_pfn;
130 /* check page color */
131 pfn = page_to_pfn(page);
132 map_pfn = PKMAP_ADDR(last_pkmap_nr) >> PAGE_SHIFT;
134 /* Avoide possibility of cache Aliasing */
135 if (!pages_do_alias((map_pfn << PAGE_SHIFT), (pfn << PAGE_SHIFT)))
136 break; /* Found a usable entry */
137 } else
138 break; /* Found a usable entry */
140 if (--count)
141 continue;
144 * Sleep for somebody else to unmap their entries
147 DECLARE_WAITQUEUE(wait, current);
149 __set_current_state(TASK_UNINTERRUPTIBLE);
150 add_wait_queue(&pkmap_map_wait, &wait);
151 spin_unlock(&kmap_lock);
152 schedule();
153 remove_wait_queue(&pkmap_map_wait, &wait);
154 spin_lock(&kmap_lock);
156 /* Somebody else might have mapped it while we slept */
157 if (page_address(page))
158 return (unsigned long)page_address(page);
160 /* Re-start */
161 goto start;
164 vaddr = PKMAP_ADDR(last_pkmap_nr);
165 set_pte_at(&init_mm, vaddr,
166 &(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
168 pkmap_count[last_pkmap_nr] = 1;
169 set_page_address(page, (void *)vaddr);
171 return vaddr;
174 void fastcall *kmap_high(struct page *page)
176 unsigned long vaddr;
179 * For highmem pages, we can't trust "virtual" until
180 * after we have the lock.
182 * We cannot call this from interrupts, as it may block
184 spin_lock(&kmap_lock);
185 vaddr = (unsigned long)page_address(page);
186 if (!vaddr)
187 vaddr = map_new_virtual(page);
188 pkmap_count[PKMAP_NR(vaddr)]++;
189 BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2);
190 spin_unlock(&kmap_lock);
191 return (void*) vaddr;
194 EXPORT_SYMBOL(kmap_high);
196 void fastcall kunmap_high(struct page *page)
198 unsigned long vaddr;
199 unsigned long nr;
200 int need_wakeup;
202 spin_lock(&kmap_lock);
203 vaddr = (unsigned long)page_address(page);
204 BUG_ON(!vaddr);
205 nr = PKMAP_NR(vaddr);
208 * A count must never go down to zero
209 * without a TLB flush!
211 need_wakeup = 0;
212 switch (--pkmap_count[nr]) {
213 case 0:
214 BUG();
215 case 1:
217 * Avoid an unnecessary wake_up() function call.
218 * The common case is pkmap_count[] == 1, but
219 * no waiters.
220 * The tasks queued in the wait-queue are guarded
221 * by both the lock in the wait-queue-head and by
222 * the kmap_lock. As the kmap_lock is held here,
223 * no need for the wait-queue-head's lock. Simply
224 * test if the queue is empty.
226 need_wakeup = waitqueue_active(&pkmap_map_wait);
228 spin_unlock(&kmap_lock);
230 /* do wake-up, if needed, race-free outside of the spin lock */
231 if (need_wakeup)
232 wake_up(&pkmap_map_wait);
235 EXPORT_SYMBOL(kunmap_high);
236 #endif
238 #if defined(HASHED_PAGE_VIRTUAL)
240 #define PA_HASH_ORDER 7
243 * Describes one page->virtual association
245 struct page_address_map {
246 struct page *page;
247 void *virtual;
248 struct list_head list;
252 * page_address_map freelist, allocated from page_address_maps.
254 static struct list_head page_address_pool; /* freelist */
255 static spinlock_t pool_lock; /* protects page_address_pool */
258 * Hash table bucket
260 static struct page_address_slot {
261 struct list_head lh; /* List of page_address_maps */
262 spinlock_t lock; /* Protect this bucket's list */
263 } ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
265 static struct page_address_slot *page_slot(struct page *page)
267 return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
270 void *page_address(struct page *page)
272 unsigned long flags;
273 void *ret;
274 struct page_address_slot *pas;
276 if (!PageHighMem(page))
277 return lowmem_page_address(page);
279 pas = page_slot(page);
280 ret = NULL;
281 spin_lock_irqsave(&pas->lock, flags);
282 if (!list_empty(&pas->lh)) {
283 struct page_address_map *pam;
285 list_for_each_entry(pam, &pas->lh, list) {
286 if (pam->page == page) {
287 ret = pam->virtual;
288 goto done;
292 done:
293 spin_unlock_irqrestore(&pas->lock, flags);
294 return ret;
297 EXPORT_SYMBOL(page_address);
299 void set_page_address(struct page *page, void *virtual)
301 unsigned long flags;
302 struct page_address_slot *pas;
303 struct page_address_map *pam;
305 BUG_ON(!PageHighMem(page));
307 pas = page_slot(page);
308 if (virtual) { /* Add */
309 BUG_ON(list_empty(&page_address_pool));
311 spin_lock_irqsave(&pool_lock, flags);
312 pam = list_entry(page_address_pool.next,
313 struct page_address_map, list);
314 list_del(&pam->list);
315 spin_unlock_irqrestore(&pool_lock, flags);
317 pam->page = page;
318 pam->virtual = virtual;
320 spin_lock_irqsave(&pas->lock, flags);
321 list_add_tail(&pam->list, &pas->lh);
322 spin_unlock_irqrestore(&pas->lock, flags);
323 } else { /* Remove */
324 spin_lock_irqsave(&pas->lock, flags);
325 list_for_each_entry(pam, &pas->lh, list) {
326 if (pam->page == page) {
327 list_del(&pam->list);
328 spin_unlock_irqrestore(&pas->lock, flags);
329 spin_lock_irqsave(&pool_lock, flags);
330 list_add_tail(&pam->list, &page_address_pool);
331 spin_unlock_irqrestore(&pool_lock, flags);
332 goto done;
335 spin_unlock_irqrestore(&pas->lock, flags);
337 done:
338 return;
341 static struct page_address_map page_address_maps[LAST_PKMAP];
343 void __init page_address_init(void)
345 int i;
347 INIT_LIST_HEAD(&page_address_pool);
348 for (i = 0; i < ARRAY_SIZE(page_address_maps); i++)
349 list_add(&page_address_maps[i].list, &page_address_pool);
350 for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
351 INIT_LIST_HEAD(&page_address_htable[i].lh);
352 spin_lock_init(&page_address_htable[i].lock);
354 spin_lock_init(&pool_lock);
357 #endif /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */