[PATCH] media/video i2c updates
[linux-2.6/history.git] / mm / highmem.c
blobee5e622dcbfeace8b1bd60a2a3a8b17c35216bbb
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/bio.h>
21 #include <linux/pagemap.h>
22 #include <linux/mempool.h>
23 #include <linux/blkdev.h>
24 #include <linux/init.h>
25 #include <linux/hash.h>
26 #include <asm/pgalloc.h>
27 #include <asm/tlbflush.h>
29 static mempool_t *page_pool, *isa_page_pool;
31 static void *page_pool_alloc(int gfp_mask, void *data)
33 int gfp = gfp_mask | (int) (long) data;
35 return alloc_page(gfp);
38 static void page_pool_free(void *page, void *data)
40 __free_page(page);
44 * Virtual_count is not a pure "count".
45 * 0 means that it is not mapped, and has not been mapped
46 * since a TLB flush - it is usable.
47 * 1 means that there are no users, but it has been mapped
48 * since the last TLB flush - so we can't use it.
49 * n means that there are (n-1) current users of it.
51 #ifdef CONFIG_HIGHMEM
52 static int pkmap_count[LAST_PKMAP];
53 static unsigned int last_pkmap_nr;
54 static spinlock_t kmap_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
56 pte_t * pkmap_page_table;
58 static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
60 static void flush_all_zero_pkmaps(void)
62 int i;
64 flush_cache_all();
66 for (i = 0; i < LAST_PKMAP; i++) {
67 struct page *page;
70 * zero means we don't have anything to do,
71 * >1 means that it is still in use. Only
72 * a count of 1 means that it is free but
73 * needs to be unmapped
75 if (pkmap_count[i] != 1)
76 continue;
77 pkmap_count[i] = 0;
79 /* sanity check */
80 if (pte_none(pkmap_page_table[i]))
81 BUG();
84 * Don't need an atomic fetch-and-clear op here;
85 * no-one has the page mapped, and cannot get at
86 * its virtual address (and hence PTE) without first
87 * getting the kmap_lock (which is held here).
88 * So no dangers, even with speculative execution.
90 page = pte_page(pkmap_page_table[i]);
91 pte_clear(&pkmap_page_table[i]);
93 set_page_address(page, NULL);
95 flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
98 static inline unsigned long map_new_virtual(struct page *page)
100 unsigned long vaddr;
101 int count;
103 start:
104 count = LAST_PKMAP;
105 /* Find an empty entry */
106 for (;;) {
107 last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
108 if (!last_pkmap_nr) {
109 flush_all_zero_pkmaps();
110 count = LAST_PKMAP;
112 if (!pkmap_count[last_pkmap_nr])
113 break; /* Found a usable entry */
114 if (--count)
115 continue;
118 * Sleep for somebody else to unmap their entries
121 DECLARE_WAITQUEUE(wait, current);
123 current->state = TASK_UNINTERRUPTIBLE;
124 add_wait_queue(&pkmap_map_wait, &wait);
125 spin_unlock(&kmap_lock);
126 schedule();
127 remove_wait_queue(&pkmap_map_wait, &wait);
128 spin_lock(&kmap_lock);
130 /* Somebody else might have mapped it while we slept */
131 if (page_address(page))
132 return (unsigned long)page_address(page);
134 /* Re-start */
135 goto start;
138 vaddr = PKMAP_ADDR(last_pkmap_nr);
139 set_pte(&(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
141 pkmap_count[last_pkmap_nr] = 1;
142 set_page_address(page, (void *)vaddr);
144 return vaddr;
147 void *kmap_high(struct page *page)
149 unsigned long vaddr;
152 * For highmem pages, we can't trust "virtual" until
153 * after we have the lock.
155 * We cannot call this from interrupts, as it may block
157 spin_lock(&kmap_lock);
158 vaddr = (unsigned long)page_address(page);
159 if (!vaddr)
160 vaddr = map_new_virtual(page);
161 pkmap_count[PKMAP_NR(vaddr)]++;
162 if (pkmap_count[PKMAP_NR(vaddr)] < 2)
163 BUG();
164 spin_unlock(&kmap_lock);
165 return (void*) vaddr;
168 void kunmap_high(struct page *page)
170 unsigned long vaddr;
171 unsigned long nr;
172 int need_wakeup;
174 spin_lock(&kmap_lock);
175 vaddr = (unsigned long)page_address(page);
176 if (!vaddr)
177 BUG();
178 nr = PKMAP_NR(vaddr);
181 * A count must never go down to zero
182 * without a TLB flush!
184 need_wakeup = 0;
185 switch (--pkmap_count[nr]) {
186 case 0:
187 BUG();
188 case 1:
190 * Avoid an unnecessary wake_up() function call.
191 * The common case is pkmap_count[] == 1, but
192 * no waiters.
193 * The tasks queued in the wait-queue are guarded
194 * by both the lock in the wait-queue-head and by
195 * the kmap_lock. As the kmap_lock is held here,
196 * no need for the wait-queue-head's lock. Simply
197 * test if the queue is empty.
199 need_wakeup = waitqueue_active(&pkmap_map_wait);
201 spin_unlock(&kmap_lock);
203 /* do wake-up, if needed, race-free outside of the spin lock */
204 if (need_wakeup)
205 wake_up(&pkmap_map_wait);
208 #define POOL_SIZE 64
210 static __init int init_emergency_pool(void)
212 struct sysinfo i;
213 si_meminfo(&i);
214 si_swapinfo(&i);
216 if (!i.totalhigh)
217 return 0;
219 page_pool = mempool_create(POOL_SIZE, page_pool_alloc, page_pool_free, NULL);
220 if (!page_pool)
221 BUG();
222 printk("highmem bounce pool size: %d pages\n", POOL_SIZE);
224 return 0;
227 __initcall(init_emergency_pool);
230 * highmem version, map in to vec
232 static void bounce_copy_vec(struct bio_vec *to, unsigned char *vfrom)
234 unsigned long flags;
235 unsigned char *vto;
237 local_irq_save(flags);
238 vto = kmap_atomic(to->bv_page, KM_BOUNCE_READ);
239 memcpy(vto + to->bv_offset, vfrom, to->bv_len);
240 kunmap_atomic(vto, KM_BOUNCE_READ);
241 local_irq_restore(flags);
244 #else /* CONFIG_HIGHMEM */
246 #define bounce_copy_vec(to, vfrom) \
247 memcpy(page_address((to)->bv_page) + (to)->bv_offset, vfrom, (to)->bv_len)
249 #endif
251 #define ISA_POOL_SIZE 16
254 * gets called "every" time someone init's a queue with BLK_BOUNCE_ISA
255 * as the max address, so check if the pool has already been created.
257 int init_emergency_isa_pool(void)
259 if (isa_page_pool)
260 return 0;
262 isa_page_pool = mempool_create(ISA_POOL_SIZE, page_pool_alloc, page_pool_free, (void *) __GFP_DMA);
263 if (!isa_page_pool)
264 BUG();
266 printk("isa bounce pool size: %d pages\n", ISA_POOL_SIZE);
267 return 0;
271 * Simple bounce buffer support for highmem pages. Depending on the
272 * queue gfp mask set, *to may or may not be a highmem page. kmap it
273 * always, it will do the Right Thing
275 static void copy_to_high_bio_irq(struct bio *to, struct bio *from)
277 unsigned char *vfrom;
278 struct bio_vec *tovec, *fromvec;
279 int i;
281 __bio_for_each_segment(tovec, to, i, 0) {
282 fromvec = from->bi_io_vec + i;
285 * not bounced
287 if (tovec->bv_page == fromvec->bv_page)
288 continue;
290 vfrom = page_address(fromvec->bv_page) + fromvec->bv_offset;
292 bounce_copy_vec(tovec, vfrom);
296 static void bounce_end_io(struct bio *bio, mempool_t *pool)
298 struct bio *bio_orig = bio->bi_private;
299 struct bio_vec *bvec, *org_vec;
300 int i;
302 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
303 goto out_eio;
305 set_bit(BIO_UPTODATE, &bio_orig->bi_flags);
308 * free up bounce indirect pages used
310 __bio_for_each_segment(bvec, bio, i, 0) {
311 org_vec = bio_orig->bi_io_vec + i;
312 if (bvec->bv_page == org_vec->bv_page)
313 continue;
315 mempool_free(bvec->bv_page, pool);
318 out_eio:
319 bio_endio(bio_orig, bio_orig->bi_size, 0);
320 bio_put(bio);
323 static int bounce_end_io_write(struct bio *bio, unsigned int bytes_done,int err)
325 if (bio->bi_size)
326 return 1;
328 bounce_end_io(bio, page_pool);
329 return 0;
332 static int bounce_end_io_write_isa(struct bio *bio, unsigned int bytes_done, int err)
334 if (bio->bi_size)
335 return 1;
337 bounce_end_io(bio, isa_page_pool);
338 return 0;
341 static void __bounce_end_io_read(struct bio *bio, mempool_t *pool)
343 struct bio *bio_orig = bio->bi_private;
345 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
346 copy_to_high_bio_irq(bio_orig, bio);
348 bounce_end_io(bio, pool);
351 static int bounce_end_io_read(struct bio *bio, unsigned int bytes_done, int err)
353 if (bio->bi_size)
354 return 1;
356 __bounce_end_io_read(bio, page_pool);
357 return 0;
360 static int bounce_end_io_read_isa(struct bio *bio, unsigned int bytes_done, int err)
362 if (bio->bi_size)
363 return 1;
365 __bounce_end_io_read(bio, isa_page_pool);
366 return 0;
369 void __blk_queue_bounce(request_queue_t *q, struct bio **bio_orig, int bio_gfp,
370 mempool_t *pool)
372 struct page *page;
373 struct bio *bio = NULL;
374 int i, rw = bio_data_dir(*bio_orig);
375 struct bio_vec *to, *from;
377 bio_for_each_segment(from, *bio_orig, i) {
378 page = from->bv_page;
381 * is destination page below bounce pfn?
383 if ((page - page_zone(page)->zone_mem_map) + (page_zone(page)->zone_start_pfn) < q->bounce_pfn)
384 continue;
387 * irk, bounce it
389 if (!bio)
390 bio = bio_alloc(bio_gfp, (*bio_orig)->bi_vcnt);
392 to = bio->bi_io_vec + i;
394 to->bv_page = mempool_alloc(pool, q->bounce_gfp);
395 to->bv_len = from->bv_len;
396 to->bv_offset = from->bv_offset;
398 if (rw == WRITE) {
399 char *vto, *vfrom;
401 vto = page_address(to->bv_page) + to->bv_offset;
402 vfrom = kmap(from->bv_page) + from->bv_offset;
403 memcpy(vto, vfrom, to->bv_len);
404 kunmap(from->bv_page);
409 * no pages bounced
411 if (!bio)
412 return;
415 * at least one page was bounced, fill in possible non-highmem
416 * pages
418 bio_for_each_segment(from, *bio_orig, i) {
419 to = bio_iovec_idx(bio, i);
420 if (!to->bv_page) {
421 to->bv_page = from->bv_page;
422 to->bv_len = from->bv_len;
423 to->bv_offset = from->bv_offset;
427 bio->bi_bdev = (*bio_orig)->bi_bdev;
428 bio->bi_flags |= (1 << BIO_BOUNCED);
429 bio->bi_sector = (*bio_orig)->bi_sector;
430 bio->bi_rw = (*bio_orig)->bi_rw;
432 bio->bi_vcnt = (*bio_orig)->bi_vcnt;
433 bio->bi_idx = 0;
434 bio->bi_size = (*bio_orig)->bi_size;
436 if (pool == page_pool) {
437 bio->bi_end_io = bounce_end_io_write;
438 if (rw == READ)
439 bio->bi_end_io = bounce_end_io_read;
440 } else {
441 bio->bi_end_io = bounce_end_io_write_isa;
442 if (rw == READ)
443 bio->bi_end_io = bounce_end_io_read_isa;
446 bio->bi_private = *bio_orig;
447 *bio_orig = bio;
450 inline void blk_queue_bounce(request_queue_t *q, struct bio **bio_orig)
452 mempool_t *pool;
453 int bio_gfp;
456 * for non-isa bounce case, just check if the bounce pfn is equal
457 * to or bigger than the highest pfn in the system -- in that case,
458 * don't waste time iterating over bio segments
460 if (!(q->bounce_gfp & GFP_DMA)) {
461 if (q->bounce_pfn >= blk_max_pfn)
462 return;
464 bio_gfp = GFP_NOHIGHIO;
465 pool = page_pool;
466 } else {
467 BUG_ON(!isa_page_pool);
469 bio_gfp = GFP_NOIO;
470 pool = isa_page_pool;
474 * slow path
476 __blk_queue_bounce(q, bio_orig, bio_gfp, pool);
479 #if defined(CONFIG_DEBUG_HIGHMEM) && defined(CONFIG_HIGHMEM)
480 void check_highmem_ptes(void)
482 int idx, type;
484 preempt_disable();
485 for (type = 0; type < KM_TYPE_NR; type++) {
486 idx = type + KM_TYPE_NR*smp_processor_id();
487 if (!pte_none(*(kmap_pte-idx))) {
488 printk("scheduling with KM_TYPE %d held!\n", type);
489 BUG();
492 preempt_enable();
494 #endif
496 #if defined(HASHED_PAGE_VIRTUAL)
498 #define PA_HASH_ORDER 7
501 * Describes one page->virtual association
503 struct page_address_map {
504 struct page *page;
505 void *virtual;
506 struct list_head list;
510 * page_address_map freelist, allocated from page_address_maps.
512 static struct list_head page_address_pool; /* freelist */
513 static spinlock_t pool_lock; /* protects page_address_pool */
516 * Hash table bucket
518 static struct page_address_slot {
519 struct list_head lh; /* List of page_address_maps */
520 spinlock_t lock; /* Protect this bucket's list */
521 } ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
523 static struct page_address_slot *page_slot(struct page *page)
525 return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
528 void *page_address(struct page *page)
530 unsigned long flags;
531 void *ret;
532 struct page_address_slot *pas;
534 if (!PageHighMem(page))
535 return lowmem_page_address(page);
537 pas = page_slot(page);
538 ret = NULL;
539 spin_lock_irqsave(&pas->lock, flags);
540 if (!list_empty(&pas->lh)) {
541 struct page_address_map *pam;
543 list_for_each_entry(pam, &pas->lh, list) {
544 if (pam->page == page) {
545 ret = pam->virtual;
546 goto done;
550 done:
551 spin_unlock_irqrestore(&pas->lock, flags);
552 return ret;
555 void set_page_address(struct page *page, void *virtual)
557 unsigned long flags;
558 struct page_address_slot *pas;
559 struct page_address_map *pam;
561 BUG_ON(!PageHighMem(page));
563 pas = page_slot(page);
564 if (virtual) { /* Add */
565 BUG_ON(list_empty(&page_address_pool));
567 spin_lock_irqsave(&pool_lock, flags);
568 pam = list_entry(page_address_pool.next,
569 struct page_address_map, list);
570 list_del(&pam->list);
571 spin_unlock_irqrestore(&pool_lock, flags);
573 pam->page = page;
574 pam->virtual = virtual;
576 spin_lock_irqsave(&pas->lock, flags);
577 list_add_tail(&pam->list, &pas->lh);
578 spin_unlock_irqrestore(&pas->lock, flags);
579 } else { /* Remove */
580 spin_lock_irqsave(&pas->lock, flags);
581 list_for_each_entry(pam, &pas->lh, list) {
582 if (pam->page == page) {
583 list_del(&pam->list);
584 spin_unlock_irqrestore(&pas->lock, flags);
585 spin_lock_irqsave(&pool_lock, flags);
586 list_add_tail(&pam->list, &page_address_pool);
587 spin_unlock_irqrestore(&pool_lock, flags);
588 goto done;
591 spin_unlock_irqrestore(&pas->lock, flags);
593 done:
594 return;
597 static struct page_address_map page_address_maps[LAST_PKMAP];
599 void __init page_address_init(void)
601 int i;
603 INIT_LIST_HEAD(&page_address_pool);
604 for (i = 0; i < ARRAY_SIZE(page_address_maps); i++)
605 list_add(&page_address_maps[i].list, &page_address_pool);
606 for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
607 INIT_LIST_HEAD(&page_address_htable[i].lh);
608 spin_lock_init(&page_address_htable[i].lock);
610 spin_lock_init(&pool_lock);
613 #endif /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */