avr32: Introduce arch/avr32/mach-*/include/mach
[linux-2.6/pdupreez.git] / mm / highmem.c
blobe16e1523b688680572513b16ef0e49d5ac0314f0
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.
40 #ifdef CONFIG_HIGHMEM
42 unsigned long totalhigh_pages __read_mostly;
43 EXPORT_SYMBOL(totalhigh_pages);
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);
53 if (zone_movable_is_highmem())
54 pages += zone_page_state(
55 &pgdat->node_zones[ZONE_MOVABLE],
56 NR_FREE_PAGES);
59 return pages;
62 static int pkmap_count[LAST_PKMAP];
63 static unsigned int last_pkmap_nr;
64 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
66 pte_t * pkmap_page_table;
68 static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
70 static void flush_all_zero_pkmaps(void)
72 int i;
74 flush_cache_kmaps();
76 for (i = 0; i < LAST_PKMAP; i++) {
77 struct page *page;
80 * zero means we don't have anything to do,
81 * >1 means that it is still in use. Only
82 * a count of 1 means that it is free but
83 * needs to be unmapped
85 if (pkmap_count[i] != 1)
86 continue;
87 pkmap_count[i] = 0;
89 /* sanity check */
90 BUG_ON(pte_none(pkmap_page_table[i]));
93 * Don't need an atomic fetch-and-clear op here;
94 * no-one has the page mapped, and cannot get at
95 * its virtual address (and hence PTE) without first
96 * getting the kmap_lock (which is held here).
97 * So no dangers, even with speculative execution.
99 page = pte_page(pkmap_page_table[i]);
100 pte_clear(&init_mm, (unsigned long)page_address(page),
101 &pkmap_page_table[i]);
103 set_page_address(page, NULL);
105 flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
109 * kmap_flush_unused - flush all unused kmap mappings in order to remove stray mappings
111 void kmap_flush_unused(void)
113 spin_lock(&kmap_lock);
114 flush_all_zero_pkmaps();
115 spin_unlock(&kmap_lock);
118 static inline unsigned long map_new_virtual(struct page *page)
120 unsigned long vaddr;
121 int count;
123 start:
124 count = LAST_PKMAP;
125 /* Find an empty entry */
126 for (;;) {
127 last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
128 if (!last_pkmap_nr) {
129 flush_all_zero_pkmaps();
130 count = LAST_PKMAP;
132 if (!pkmap_count[last_pkmap_nr])
133 break; /* Found a usable entry */
134 if (--count)
135 continue;
138 * Sleep for somebody else to unmap their entries
141 DECLARE_WAITQUEUE(wait, current);
143 __set_current_state(TASK_UNINTERRUPTIBLE);
144 add_wait_queue(&pkmap_map_wait, &wait);
145 spin_unlock(&kmap_lock);
146 schedule();
147 remove_wait_queue(&pkmap_map_wait, &wait);
148 spin_lock(&kmap_lock);
150 /* Somebody else might have mapped it while we slept */
151 if (page_address(page))
152 return (unsigned long)page_address(page);
154 /* Re-start */
155 goto start;
158 vaddr = PKMAP_ADDR(last_pkmap_nr);
159 set_pte_at(&init_mm, vaddr,
160 &(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
162 pkmap_count[last_pkmap_nr] = 1;
163 set_page_address(page, (void *)vaddr);
165 return vaddr;
169 * kmap_high - map a highmem page into memory
170 * @page: &struct page to map
172 * Returns the page's virtual memory address.
174 * We cannot call this from interrupts, as it may block.
176 void *kmap_high(struct page *page)
178 unsigned long vaddr;
181 * For highmem pages, we can't trust "virtual" until
182 * after we have the lock.
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);
197 * kunmap_high - map a highmem page into memory
198 * @page: &struct page to unmap
200 void kunmap_high(struct page *page)
202 unsigned long vaddr;
203 unsigned long nr;
204 int need_wakeup;
206 spin_lock(&kmap_lock);
207 vaddr = (unsigned long)page_address(page);
208 BUG_ON(!vaddr);
209 nr = PKMAP_NR(vaddr);
212 * A count must never go down to zero
213 * without a TLB flush!
215 need_wakeup = 0;
216 switch (--pkmap_count[nr]) {
217 case 0:
218 BUG();
219 case 1:
221 * Avoid an unnecessary wake_up() function call.
222 * The common case is pkmap_count[] == 1, but
223 * no waiters.
224 * The tasks queued in the wait-queue are guarded
225 * by both the lock in the wait-queue-head and by
226 * the kmap_lock. As the kmap_lock is held here,
227 * no need for the wait-queue-head's lock. Simply
228 * test if the queue is empty.
230 need_wakeup = waitqueue_active(&pkmap_map_wait);
232 spin_unlock(&kmap_lock);
234 /* do wake-up, if needed, race-free outside of the spin lock */
235 if (need_wakeup)
236 wake_up(&pkmap_map_wait);
239 EXPORT_SYMBOL(kunmap_high);
240 #endif
242 #if defined(HASHED_PAGE_VIRTUAL)
244 #define PA_HASH_ORDER 7
247 * Describes one page->virtual association
249 struct page_address_map {
250 struct page *page;
251 void *virtual;
252 struct list_head list;
256 * page_address_map freelist, allocated from page_address_maps.
258 static struct list_head page_address_pool; /* freelist */
259 static spinlock_t pool_lock; /* protects page_address_pool */
262 * Hash table bucket
264 static struct page_address_slot {
265 struct list_head lh; /* List of page_address_maps */
266 spinlock_t lock; /* Protect this bucket's list */
267 } ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
269 static struct page_address_slot *page_slot(struct page *page)
271 return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
275 * page_address - get the mapped virtual address of a page
276 * @page: &struct page to get the virtual address of
278 * Returns the page's virtual address.
280 void *page_address(struct page *page)
282 unsigned long flags;
283 void *ret;
284 struct page_address_slot *pas;
286 if (!PageHighMem(page))
287 return lowmem_page_address(page);
289 pas = page_slot(page);
290 ret = NULL;
291 spin_lock_irqsave(&pas->lock, flags);
292 if (!list_empty(&pas->lh)) {
293 struct page_address_map *pam;
295 list_for_each_entry(pam, &pas->lh, list) {
296 if (pam->page == page) {
297 ret = pam->virtual;
298 goto done;
302 done:
303 spin_unlock_irqrestore(&pas->lock, flags);
304 return ret;
307 EXPORT_SYMBOL(page_address);
310 * set_page_address - set a page's virtual address
311 * @page: &struct page to set
312 * @virtual: virtual address to use
314 void set_page_address(struct page *page, void *virtual)
316 unsigned long flags;
317 struct page_address_slot *pas;
318 struct page_address_map *pam;
320 BUG_ON(!PageHighMem(page));
322 pas = page_slot(page);
323 if (virtual) { /* Add */
324 BUG_ON(list_empty(&page_address_pool));
326 spin_lock_irqsave(&pool_lock, flags);
327 pam = list_entry(page_address_pool.next,
328 struct page_address_map, list);
329 list_del(&pam->list);
330 spin_unlock_irqrestore(&pool_lock, flags);
332 pam->page = page;
333 pam->virtual = virtual;
335 spin_lock_irqsave(&pas->lock, flags);
336 list_add_tail(&pam->list, &pas->lh);
337 spin_unlock_irqrestore(&pas->lock, flags);
338 } else { /* Remove */
339 spin_lock_irqsave(&pas->lock, flags);
340 list_for_each_entry(pam, &pas->lh, list) {
341 if (pam->page == page) {
342 list_del(&pam->list);
343 spin_unlock_irqrestore(&pas->lock, flags);
344 spin_lock_irqsave(&pool_lock, flags);
345 list_add_tail(&pam->list, &page_address_pool);
346 spin_unlock_irqrestore(&pool_lock, flags);
347 goto done;
350 spin_unlock_irqrestore(&pas->lock, flags);
352 done:
353 return;
356 static struct page_address_map page_address_maps[LAST_PKMAP];
358 void __init page_address_init(void)
360 int i;
362 INIT_LIST_HEAD(&page_address_pool);
363 for (i = 0; i < ARRAY_SIZE(page_address_maps); i++)
364 list_add(&page_address_maps[i].list, &page_address_pool);
365 for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
366 INIT_LIST_HEAD(&page_address_htable[i].lh);
367 spin_lock_init(&page_address_htable[i].lock);
369 spin_lock_init(&pool_lock);
372 #endif /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */