[MIPS] Misc fixes for plat_irq_dispatch functions
[linux-2.6/linux-mips/linux-dm7025.git] / mm / highmem.c
blob51e1c1995fec1d87a1e8e80d4518008d4ec4f4f1
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;
44 unsigned int nr_free_highpages (void)
46 pg_data_t *pgdat;
47 unsigned int pages = 0;
49 for_each_online_pgdat(pgdat)
50 pages += zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
51 NR_FREE_PAGES);
53 return pages;
56 static int pkmap_count[LAST_PKMAP];
57 static unsigned int last_pkmap_nr;
58 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
60 pte_t * pkmap_page_table;
62 static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
64 static void flush_all_zero_pkmaps(void)
66 int i;
68 flush_cache_kmaps();
70 for (i = 0; i < LAST_PKMAP; i++) {
71 struct page *page;
74 * zero means we don't have anything to do,
75 * >1 means that it is still in use. Only
76 * a count of 1 means that it is free but
77 * needs to be unmapped
79 if (pkmap_count[i] != 1)
80 continue;
81 pkmap_count[i] = 0;
83 /* sanity check */
84 BUG_ON(pte_none(pkmap_page_table[i]));
87 * Don't need an atomic fetch-and-clear op here;
88 * no-one has the page mapped, and cannot get at
89 * its virtual address (and hence PTE) without first
90 * getting the kmap_lock (which is held here).
91 * So no dangers, even with speculative execution.
93 page = pte_page(pkmap_page_table[i]);
94 pte_clear(&init_mm, (unsigned long)page_address(page),
95 &pkmap_page_table[i]);
97 set_page_address(page, NULL);
99 flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
102 static inline unsigned long map_new_virtual(struct page *page)
104 unsigned long vaddr;
105 int count;
107 start:
108 count = LAST_PKMAP;
109 /* Find an empty entry */
110 for (;;) {
111 last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
112 if (!last_pkmap_nr) {
113 flush_all_zero_pkmaps();
114 count = LAST_PKMAP;
116 if (!pkmap_count[last_pkmap_nr])
117 break; /* Found a usable entry */
118 if (--count)
119 continue;
122 * Sleep for somebody else to unmap their entries
125 DECLARE_WAITQUEUE(wait, current);
127 __set_current_state(TASK_UNINTERRUPTIBLE);
128 add_wait_queue(&pkmap_map_wait, &wait);
129 spin_unlock(&kmap_lock);
130 schedule();
131 remove_wait_queue(&pkmap_map_wait, &wait);
132 spin_lock(&kmap_lock);
134 /* Somebody else might have mapped it while we slept */
135 if (page_address(page))
136 return (unsigned long)page_address(page);
138 /* Re-start */
139 goto start;
142 vaddr = PKMAP_ADDR(last_pkmap_nr);
143 set_pte_at(&init_mm, vaddr,
144 &(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
146 pkmap_count[last_pkmap_nr] = 1;
147 set_page_address(page, (void *)vaddr);
149 return vaddr;
152 void fastcall *kmap_high(struct page *page)
154 unsigned long vaddr;
157 * For highmem pages, we can't trust "virtual" until
158 * after we have the lock.
160 * We cannot call this from interrupts, as it may block
162 spin_lock(&kmap_lock);
163 vaddr = (unsigned long)page_address(page);
164 if (!vaddr)
165 vaddr = map_new_virtual(page);
166 pkmap_count[PKMAP_NR(vaddr)]++;
167 BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2);
168 spin_unlock(&kmap_lock);
169 return (void*) vaddr;
172 EXPORT_SYMBOL(kmap_high);
174 void fastcall kunmap_high(struct page *page)
176 unsigned long vaddr;
177 unsigned long nr;
178 int need_wakeup;
180 spin_lock(&kmap_lock);
181 vaddr = (unsigned long)page_address(page);
182 BUG_ON(!vaddr);
183 nr = PKMAP_NR(vaddr);
186 * A count must never go down to zero
187 * without a TLB flush!
189 need_wakeup = 0;
190 switch (--pkmap_count[nr]) {
191 case 0:
192 BUG();
193 case 1:
195 * Avoid an unnecessary wake_up() function call.
196 * The common case is pkmap_count[] == 1, but
197 * no waiters.
198 * The tasks queued in the wait-queue are guarded
199 * by both the lock in the wait-queue-head and by
200 * the kmap_lock. As the kmap_lock is held here,
201 * no need for the wait-queue-head's lock. Simply
202 * test if the queue is empty.
204 need_wakeup = waitqueue_active(&pkmap_map_wait);
206 spin_unlock(&kmap_lock);
208 /* do wake-up, if needed, race-free outside of the spin lock */
209 if (need_wakeup)
210 wake_up(&pkmap_map_wait);
213 EXPORT_SYMBOL(kunmap_high);
214 #endif
216 #if defined(HASHED_PAGE_VIRTUAL)
218 #define PA_HASH_ORDER 7
221 * Describes one page->virtual association
223 struct page_address_map {
224 struct page *page;
225 void *virtual;
226 struct list_head list;
230 * page_address_map freelist, allocated from page_address_maps.
232 static struct list_head page_address_pool; /* freelist */
233 static spinlock_t pool_lock; /* protects page_address_pool */
236 * Hash table bucket
238 static struct page_address_slot {
239 struct list_head lh; /* List of page_address_maps */
240 spinlock_t lock; /* Protect this bucket's list */
241 } ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
243 static struct page_address_slot *page_slot(struct page *page)
245 return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
248 void *page_address(struct page *page)
250 unsigned long flags;
251 void *ret;
252 struct page_address_slot *pas;
254 if (!PageHighMem(page))
255 return lowmem_page_address(page);
257 pas = page_slot(page);
258 ret = NULL;
259 spin_lock_irqsave(&pas->lock, flags);
260 if (!list_empty(&pas->lh)) {
261 struct page_address_map *pam;
263 list_for_each_entry(pam, &pas->lh, list) {
264 if (pam->page == page) {
265 ret = pam->virtual;
266 goto done;
270 done:
271 spin_unlock_irqrestore(&pas->lock, flags);
272 return ret;
275 EXPORT_SYMBOL(page_address);
277 void set_page_address(struct page *page, void *virtual)
279 unsigned long flags;
280 struct page_address_slot *pas;
281 struct page_address_map *pam;
283 BUG_ON(!PageHighMem(page));
285 pas = page_slot(page);
286 if (virtual) { /* Add */
287 BUG_ON(list_empty(&page_address_pool));
289 spin_lock_irqsave(&pool_lock, flags);
290 pam = list_entry(page_address_pool.next,
291 struct page_address_map, list);
292 list_del(&pam->list);
293 spin_unlock_irqrestore(&pool_lock, flags);
295 pam->page = page;
296 pam->virtual = virtual;
298 spin_lock_irqsave(&pas->lock, flags);
299 list_add_tail(&pam->list, &pas->lh);
300 spin_unlock_irqrestore(&pas->lock, flags);
301 } else { /* Remove */
302 spin_lock_irqsave(&pas->lock, flags);
303 list_for_each_entry(pam, &pas->lh, list) {
304 if (pam->page == page) {
305 list_del(&pam->list);
306 spin_unlock_irqrestore(&pas->lock, flags);
307 spin_lock_irqsave(&pool_lock, flags);
308 list_add_tail(&pam->list, &page_address_pool);
309 spin_unlock_irqrestore(&pool_lock, flags);
310 goto done;
313 spin_unlock_irqrestore(&pas->lock, flags);
315 done:
316 return;
319 static struct page_address_map page_address_maps[LAST_PKMAP];
321 void __init page_address_init(void)
323 int i;
325 INIT_LIST_HEAD(&page_address_pool);
326 for (i = 0; i < ARRAY_SIZE(page_address_maps); i++)
327 list_add(&page_address_maps[i].list, &page_address_pool);
328 for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
329 INIT_LIST_HEAD(&page_address_htable[i].lh);
330 spin_lock_init(&page_address_htable[i].lock);
332 spin_lock_init(&pool_lock);
335 #endif /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */