iwlwifi: get_hw_cmd_size
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / mm / highmem.c
blob7da4a7b6af11f1d359af60568b312838f8fa2802
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);
52 if (zone_movable_is_highmem())
53 pages += zone_page_state(
54 &pgdat->node_zones[ZONE_MOVABLE],
55 NR_FREE_PAGES);
58 return pages;
61 static int pkmap_count[LAST_PKMAP];
62 static unsigned int last_pkmap_nr;
63 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
65 pte_t * pkmap_page_table;
67 static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
69 static void flush_all_zero_pkmaps(void)
71 int i;
73 flush_cache_kmaps();
75 for (i = 0; i < LAST_PKMAP; i++) {
76 struct page *page;
79 * zero means we don't have anything to do,
80 * >1 means that it is still in use. Only
81 * a count of 1 means that it is free but
82 * needs to be unmapped
84 if (pkmap_count[i] != 1)
85 continue;
86 pkmap_count[i] = 0;
88 /* sanity check */
89 BUG_ON(pte_none(pkmap_page_table[i]));
92 * Don't need an atomic fetch-and-clear op here;
93 * no-one has the page mapped, and cannot get at
94 * its virtual address (and hence PTE) without first
95 * getting the kmap_lock (which is held here).
96 * So no dangers, even with speculative execution.
98 page = pte_page(pkmap_page_table[i]);
99 pte_clear(&init_mm, (unsigned long)page_address(page),
100 &pkmap_page_table[i]);
102 set_page_address(page, NULL);
104 flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
108 * kmap_flush_unused - flush all unused kmap mappings in order to remove stray mappings
110 void kmap_flush_unused(void)
112 spin_lock(&kmap_lock);
113 flush_all_zero_pkmaps();
114 spin_unlock(&kmap_lock);
117 static inline unsigned long map_new_virtual(struct page *page)
119 unsigned long vaddr;
120 int count;
122 start:
123 count = LAST_PKMAP;
124 /* Find an empty entry */
125 for (;;) {
126 last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
127 if (!last_pkmap_nr) {
128 flush_all_zero_pkmaps();
129 count = LAST_PKMAP;
131 if (!pkmap_count[last_pkmap_nr])
132 break; /* Found a usable entry */
133 if (--count)
134 continue;
137 * Sleep for somebody else to unmap their entries
140 DECLARE_WAITQUEUE(wait, current);
142 __set_current_state(TASK_UNINTERRUPTIBLE);
143 add_wait_queue(&pkmap_map_wait, &wait);
144 spin_unlock(&kmap_lock);
145 schedule();
146 remove_wait_queue(&pkmap_map_wait, &wait);
147 spin_lock(&kmap_lock);
149 /* Somebody else might have mapped it while we slept */
150 if (page_address(page))
151 return (unsigned long)page_address(page);
153 /* Re-start */
154 goto start;
157 vaddr = PKMAP_ADDR(last_pkmap_nr);
158 set_pte_at(&init_mm, vaddr,
159 &(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
161 pkmap_count[last_pkmap_nr] = 1;
162 set_page_address(page, (void *)vaddr);
164 return vaddr;
168 * kmap_high - map a highmem page into memory
169 * @page: &struct page to map
171 * Returns the page's virtual memory address.
173 * We cannot call this from interrupts, as it may block.
175 void *kmap_high(struct page *page)
177 unsigned long vaddr;
180 * For highmem pages, we can't trust "virtual" until
181 * after we have the lock.
183 spin_lock(&kmap_lock);
184 vaddr = (unsigned long)page_address(page);
185 if (!vaddr)
186 vaddr = map_new_virtual(page);
187 pkmap_count[PKMAP_NR(vaddr)]++;
188 BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2);
189 spin_unlock(&kmap_lock);
190 return (void*) vaddr;
193 EXPORT_SYMBOL(kmap_high);
196 * kunmap_high - map a highmem page into memory
197 * @page: &struct page to unmap
199 void kunmap_high(struct page *page)
201 unsigned long vaddr;
202 unsigned long nr;
203 int need_wakeup;
205 spin_lock(&kmap_lock);
206 vaddr = (unsigned long)page_address(page);
207 BUG_ON(!vaddr);
208 nr = PKMAP_NR(vaddr);
211 * A count must never go down to zero
212 * without a TLB flush!
214 need_wakeup = 0;
215 switch (--pkmap_count[nr]) {
216 case 0:
217 BUG();
218 case 1:
220 * Avoid an unnecessary wake_up() function call.
221 * The common case is pkmap_count[] == 1, but
222 * no waiters.
223 * The tasks queued in the wait-queue are guarded
224 * by both the lock in the wait-queue-head and by
225 * the kmap_lock. As the kmap_lock is held here,
226 * no need for the wait-queue-head's lock. Simply
227 * test if the queue is empty.
229 need_wakeup = waitqueue_active(&pkmap_map_wait);
231 spin_unlock(&kmap_lock);
233 /* do wake-up, if needed, race-free outside of the spin lock */
234 if (need_wakeup)
235 wake_up(&pkmap_map_wait);
238 EXPORT_SYMBOL(kunmap_high);
239 #endif
241 #if defined(HASHED_PAGE_VIRTUAL)
243 #define PA_HASH_ORDER 7
246 * Describes one page->virtual association
248 struct page_address_map {
249 struct page *page;
250 void *virtual;
251 struct list_head list;
255 * page_address_map freelist, allocated from page_address_maps.
257 static struct list_head page_address_pool; /* freelist */
258 static spinlock_t pool_lock; /* protects page_address_pool */
261 * Hash table bucket
263 static struct page_address_slot {
264 struct list_head lh; /* List of page_address_maps */
265 spinlock_t lock; /* Protect this bucket's list */
266 } ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
268 static struct page_address_slot *page_slot(struct page *page)
270 return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
274 * page_address - get the mapped virtual address of a page
275 * @page: &struct page to get the virtual address of
277 * Returns the page's virtual address.
279 void *page_address(struct page *page)
281 unsigned long flags;
282 void *ret;
283 struct page_address_slot *pas;
285 if (!PageHighMem(page))
286 return lowmem_page_address(page);
288 pas = page_slot(page);
289 ret = NULL;
290 spin_lock_irqsave(&pas->lock, flags);
291 if (!list_empty(&pas->lh)) {
292 struct page_address_map *pam;
294 list_for_each_entry(pam, &pas->lh, list) {
295 if (pam->page == page) {
296 ret = pam->virtual;
297 goto done;
301 done:
302 spin_unlock_irqrestore(&pas->lock, flags);
303 return ret;
306 EXPORT_SYMBOL(page_address);
309 * set_page_address - set a page's virtual address
310 * @page: &struct page to set
311 * @virtual: virtual address to use
313 void set_page_address(struct page *page, void *virtual)
315 unsigned long flags;
316 struct page_address_slot *pas;
317 struct page_address_map *pam;
319 BUG_ON(!PageHighMem(page));
321 pas = page_slot(page);
322 if (virtual) { /* Add */
323 BUG_ON(list_empty(&page_address_pool));
325 spin_lock_irqsave(&pool_lock, flags);
326 pam = list_entry(page_address_pool.next,
327 struct page_address_map, list);
328 list_del(&pam->list);
329 spin_unlock_irqrestore(&pool_lock, flags);
331 pam->page = page;
332 pam->virtual = virtual;
334 spin_lock_irqsave(&pas->lock, flags);
335 list_add_tail(&pam->list, &pas->lh);
336 spin_unlock_irqrestore(&pas->lock, flags);
337 } else { /* Remove */
338 spin_lock_irqsave(&pas->lock, flags);
339 list_for_each_entry(pam, &pas->lh, list) {
340 if (pam->page == page) {
341 list_del(&pam->list);
342 spin_unlock_irqrestore(&pas->lock, flags);
343 spin_lock_irqsave(&pool_lock, flags);
344 list_add_tail(&pam->list, &page_address_pool);
345 spin_unlock_irqrestore(&pool_lock, flags);
346 goto done;
349 spin_unlock_irqrestore(&pas->lock, flags);
351 done:
352 return;
355 static struct page_address_map page_address_maps[LAST_PKMAP];
357 void __init page_address_init(void)
359 int i;
361 INIT_LIST_HEAD(&page_address_pool);
362 for (i = 0; i < ARRAY_SIZE(page_address_maps); i++)
363 list_add(&page_address_maps[i].list, &page_address_pool);
364 for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
365 INIT_LIST_HEAD(&page_address_htable[i].lh);
366 spin_lock_init(&page_address_htable[i].lock);
368 spin_lock_init(&pool_lock);
371 #endif /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */