patch-2.6.6-vs1.9.0
[linux-2.6.git] / arch / m68k / atari / stram.c
1 /*
2  * arch/m68k/atari/stram.c: Functions for ST-RAM allocations
3  *
4  * Copyright 1994-97 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
5  *
6  * This file is subject to the terms and conditions of the GNU General Public
7  * License.  See the file COPYING in the main directory of this archive
8  * for more details.
9  */
10
11 #include <linux/config.h>
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/mm.h>
15 #include <linux/kdev_t.h>
16 #include <linux/major.h>
17 #include <linux/init.h>
18 #include <linux/swap.h>
19 #include <linux/slab.h>
20 #include <linux/vmalloc.h>
21 #include <linux/pagemap.h>
22 #include <linux/shm.h>
23 #include <linux/bootmem.h>
24 #include <linux/mount.h>
25 #include <linux/blkdev.h>
26
27 #include <asm/setup.h>
28 #include <asm/machdep.h>
29 #include <asm/page.h>
30 #include <asm/pgtable.h>
31 #include <asm/atarihw.h>
32 #include <asm/atari_stram.h>
33 #include <asm/io.h>
34 #include <asm/semaphore.h>
35
36 #include <linux/swapops.h>
37
38 #undef DEBUG
39
40 #ifdef DEBUG
41 #define DPRINTK(fmt,args...) printk( fmt, ##args )
42 #else
43 #define DPRINTK(fmt,args...)
44 #endif
45
46 #if defined(CONFIG_PROC_FS) && defined(CONFIG_STRAM_PROC)
47 /* abbrev for the && above... */
48 #define DO_PROC
49 #include <linux/proc_fs.h>
50 #endif
51
52 /* Pre-swapping comments:
53  *
54  * ++roman:
55  * 
56  * New version of ST-Ram buffer allocation. Instead of using the
57  * 1 MB - 4 KB that remain when the ST-Ram chunk starts at $1000
58  * (1 MB granularity!), such buffers are reserved like this:
59  *
60  *  - If the kernel resides in ST-Ram anyway, we can take the buffer
61  *    from behind the current kernel data space the normal way
62  *    (incrementing start_mem).
63  *    
64  *  - If the kernel is in TT-Ram, stram_init() initializes start and
65  *    end of the available region. Buffers are allocated from there
66  *    and mem_init() later marks the such used pages as reserved.
67  *    Since each TT-Ram chunk is at least 4 MB in size, I hope there
68  *    won't be an overrun of the ST-Ram region by normal kernel data
69  *    space.
70  *    
71  * For that, ST-Ram may only be allocated while kernel initialization
72  * is going on, or exactly: before mem_init() is called. There is also
73  * no provision now for freeing ST-Ram buffers. It seems that isn't
74  * really needed.
75  *
76  */
77
78 /*
79  * New Nov 1997: Use ST-RAM as swap space!
80  *
81  * In the past, there were often problems with modules that require ST-RAM
82  * buffers. Such drivers have to use __get_dma_pages(), which unfortunately
83  * often isn't very successful in allocating more than 1 page :-( [1] The net
84  * result was that most of the time you couldn't insmod such modules (ataflop,
85  * ACSI, SCSI on Falcon, Atari internal framebuffer, not to speak of acsi_slm,
86  * which needs a 1 MB buffer... :-).
87  *
88  * To overcome this limitation, ST-RAM can now be turned into a very
89  * high-speed swap space. If a request for an ST-RAM buffer comes, the kernel
90  * now tries to unswap some pages on that swap device to make some free (and
91  * contiguous) space. This works much better in comparison to
92  * __get_dma_pages(), since used swap pages can be selectively freed by either
93  * moving them to somewhere else in swap space, or by reading them back into
94  * system memory. Ok, there operation of unswapping isn't really cheap (for
95  * each page, one has to go through the page tables of all processes), but it
96  * doesn't happen that often (only when allocation ST-RAM, i.e. when loading a
97  * module that needs ST-RAM). But it at least makes it possible to load such
98  * modules!
99  *
100  * It could also be that overall system performance increases a bit due to
101  * ST-RAM swapping, since slow ST-RAM isn't used anymore for holding data or
102  * executing code in. It's then just a (very fast, compared to disk) back
103  * storage for not-so-often needed data. (But this effect must be compared
104  * with the loss of total memory...) Don't know if the effect is already
105  * visible on a TT, where the speed difference between ST- and TT-RAM isn't
106  * that dramatic, but it should on machines where TT-RAM is really much faster
107  * (e.g. Afterburner).
108  * 
109  *   [1]: __get_free_pages() does a fine job if you only want one page, but if
110  * you want more (contiguous) pages, it can give you such a block only if
111  * there's already a free one. The algorithm can't try to free buffers or swap
112  * out something in order to make more free space, since all that page-freeing
113  * mechanisms work "target-less", i.e. they just free something, but not in a
114  * specific place. I.e., __get_free_pages() can't do anything to free
115  * *adjacent* pages :-( This situation becomes even worse for DMA memory,
116  * since the freeing algorithms are also blind to DMA capability of pages.
117  */
118
119 /* 1998-10-20: ++andreas
120    unswap_by_move disabled because it does not handle swapped shm pages.
121 */
122
123 /* 2000-05-01: ++andreas
124    Integrated with bootmem.  Remove all traces of unswap_by_move.
125 */
126
127 #ifdef CONFIG_STRAM_SWAP
128 #define ALIGN_IF_SWAP(x)        PAGE_ALIGN(x)
129 #else
130 #define ALIGN_IF_SWAP(x)        (x)
131 #endif
132
133 /* get index of swap page at address 'addr' */
134 #define SWAP_NR(addr)           (((addr) - swap_start) >> PAGE_SHIFT)
135
136 /* get address of swap page #'nr' */
137 #define SWAP_ADDR(nr)           (swap_start + ((nr) << PAGE_SHIFT))
138
139 /* get number of pages for 'n' bytes (already page-aligned) */
140 #define N_PAGES(n)                      ((n) >> PAGE_SHIFT)
141
142 /* The following two numbers define the maximum fraction of ST-RAM in total
143  * memory, below that the kernel would automatically use ST-RAM as swap
144  * space. This decision can be overridden with stram_swap= */
145 #define MAX_STRAM_FRACTION_NOM          1
146 #define MAX_STRAM_FRACTION_DENOM        3
147
148 /* Start and end (virtual) of ST-RAM */
149 static void *stram_start, *stram_end;
150
151 /* set after memory_init() executed and allocations via start_mem aren't
152  * possible anymore */
153 static int mem_init_done;
154
155 /* set if kernel is in ST-RAM */
156 static int kernel_in_stram;
157
158 typedef struct stram_block {
159         struct stram_block *next;
160         void *start;
161         unsigned long size;
162         unsigned flags;
163         const char *owner;
164 } BLOCK;
165
166 /* values for flags field */
167 #define BLOCK_FREE              0x01    /* free structure in the BLOCKs pool */
168 #define BLOCK_KMALLOCED 0x02    /* structure allocated by kmalloc() */
169 #define BLOCK_GFP               0x08    /* block allocated with __get_dma_pages() */
170 #define BLOCK_INSWAP    0x10    /* block allocated in swap space */
171
172 /* list of allocated blocks */
173 static BLOCK *alloc_list;
174
175 /* We can't always use kmalloc() to allocate BLOCK structures, since
176  * stram_alloc() can be called rather early. So we need some pool of
177  * statically allocated structures. 20 of them is more than enough, so in most
178  * cases we never should need to call kmalloc(). */
179 #define N_STATIC_BLOCKS 20
180 static BLOCK static_blocks[N_STATIC_BLOCKS];
181
182 #ifdef CONFIG_STRAM_SWAP
183 /* max. number of bytes to use for swapping
184  *  0 = no ST-RAM swapping
185  * -1 = do swapping (to whole ST-RAM) if it's less than MAX_STRAM_FRACTION of
186  *      total memory
187  */
188 static int max_swap_size = -1;
189
190 /* start and end of swapping area */
191 static void *swap_start, *swap_end;
192
193 /* The ST-RAM's swap info structure */
194 static struct swap_info_struct *stram_swap_info;
195
196 /* The ST-RAM's swap type */
197 static int stram_swap_type;
198
199 /* Semaphore for get_stram_region.  */
200 static DECLARE_MUTEX(stram_swap_sem);
201
202 /* major and minor device number of the ST-RAM device; for the major, we use
203  * the same as Amiga z2ram, which is really similar and impossible on Atari,
204  * and for the minor a relatively odd number to avoid the user creating and
205  * using that device. */
206 #define STRAM_MAJOR             Z2RAM_MAJOR
207 #define STRAM_MINOR             13
208
209 /* Some impossible pointer value */
210 #define MAGIC_FILE_P    (struct file *)0xffffdead
211
212 #ifdef DO_PROC
213 static unsigned stat_swap_read;
214 static unsigned stat_swap_write;
215 static unsigned stat_swap_force;
216 #endif /* DO_PROC */
217
218 #endif /* CONFIG_STRAM_SWAP */
219
220 /***************************** Prototypes *****************************/
221
222 #ifdef CONFIG_STRAM_SWAP
223 static int swap_init(void *start_mem, void *swap_data);
224 static void *get_stram_region( unsigned long n_pages );
225 static void free_stram_region( unsigned long offset, unsigned long n_pages
226                                );
227 static int in_some_region(void *addr);
228 static unsigned long find_free_region( unsigned long n_pages, unsigned long
229                                        *total_free, unsigned long
230                                        *region_free );
231 static void do_stram_request(request_queue_t *);
232 static int stram_open( struct inode *inode, struct file *filp );
233 static int stram_release( struct inode *inode, struct file *filp );
234 static void reserve_region(void *start, void *end);
235 #endif
236 static BLOCK *add_region( void *addr, unsigned long size );
237 static BLOCK *find_region( void *addr );
238 static int remove_region( BLOCK *block );
239
240 /************************* End of Prototypes **************************/
241
242 \f
243 /* ------------------------------------------------------------------------ */
244 /*                                                         Public Interface                                                             */
245 /* ------------------------------------------------------------------------ */
246
247 /*
248  * This init function is called very early by atari/config.c
249  * It initializes some internal variables needed for stram_alloc()
250  */
251 void __init atari_stram_init(void)
252 {
253         int i;
254
255         /* initialize static blocks */
256         for( i = 0; i < N_STATIC_BLOCKS; ++i )
257                 static_blocks[i].flags = BLOCK_FREE;
258
259         /* determine whether kernel code resides in ST-RAM (then ST-RAM is the
260          * first memory block at virtual 0x0) */
261         stram_start = phys_to_virt(0);
262         kernel_in_stram = (stram_start == 0);
263
264         for( i = 0; i < m68k_num_memory; ++i ) {
265                 if (m68k_memory[i].addr == 0) {
266                         /* skip first 2kB or page (supervisor-only!) */
267                         stram_end = stram_start + m68k_memory[i].size;
268                         return;
269                 }
270         }
271         /* Should never come here! (There is always ST-Ram!) */
272         panic( "atari_stram_init: no ST-RAM found!" );
273 }
274
275
276 /*
277  * This function is called from setup_arch() to reserve the pages needed for
278  * ST-RAM management.
279  */
280 void __init atari_stram_reserve_pages(void *start_mem)
281 {
282 #ifdef CONFIG_STRAM_SWAP
283         /* if max_swap_size is negative (i.e. no stram_swap= option given),
284          * determine at run time whether to use ST-RAM swapping */
285         if (max_swap_size < 0)
286                 /* Use swapping if ST-RAM doesn't make up more than MAX_STRAM_FRACTION
287                  * of total memory. In that case, the max. size is set to 16 MB,
288                  * because ST-RAM can never be bigger than that.
289                  * Also, never use swapping on a Hades, there's no separate ST-RAM in
290                  * that machine. */
291                 max_swap_size =
292                         (!MACH_IS_HADES &&
293                          (N_PAGES(stram_end-stram_start)*MAX_STRAM_FRACTION_DENOM <=
294                           ((unsigned long)high_memory>>PAGE_SHIFT)*MAX_STRAM_FRACTION_NOM)) ? 16*1024*1024 : 0;
295         DPRINTK( "atari_stram_reserve_pages: max_swap_size = %d\n", max_swap_size );
296 #endif
297
298         /* always reserve first page of ST-RAM, the first 2 kB are
299          * supervisor-only! */
300         if (!kernel_in_stram)
301                 reserve_bootmem (0, PAGE_SIZE);
302
303 #ifdef CONFIG_STRAM_SWAP
304         {
305                 void *swap_data;
306
307                 start_mem = (void *) PAGE_ALIGN ((unsigned long) start_mem);
308                 /* determine first page to use as swap: if the kernel is
309                    in TT-RAM, this is the first page of (usable) ST-RAM;
310                    otherwise just use the end of kernel data (= start_mem) */
311                 swap_start = !kernel_in_stram ? stram_start + PAGE_SIZE : start_mem;
312                 /* decrement by one page, rest of kernel assumes that first swap page
313                  * is always reserved and maybe doesn't handle swp_entry == 0
314                  * correctly */
315                 swap_start -= PAGE_SIZE;
316                 swap_end = stram_end;
317                 if (swap_end-swap_start > max_swap_size)
318                         swap_end =  swap_start + max_swap_size;
319                 DPRINTK( "atari_stram_reserve_pages: swapping enabled; "
320                                  "swap=%p-%p\n", swap_start, swap_end);
321                 
322                 /* reserve some amount of memory for maintainance of
323                  * swapping itself: one page for each 2048 (PAGE_SIZE/2)
324                  * swap pages. (2 bytes for each page) */
325                 swap_data = start_mem;
326                 start_mem += ((SWAP_NR(swap_end) + PAGE_SIZE/2 - 1)
327                               >> (PAGE_SHIFT-1)) << PAGE_SHIFT;
328                 /* correct swap_start if necessary */
329                 if (swap_start + PAGE_SIZE == swap_data)
330                         swap_start = start_mem - PAGE_SIZE;
331                 
332                 if (!swap_init( start_mem, swap_data )) {
333                         printk( KERN_ERR "ST-RAM swap space initialization failed\n" );
334                         max_swap_size = 0;
335                         return;
336                 }
337                 /* reserve region for swapping meta-data */
338                 reserve_region(swap_data, start_mem);
339                 /* reserve swapping area itself */
340                 reserve_region(swap_start + PAGE_SIZE, swap_end);
341
342                 /*
343                  * If the whole ST-RAM is used for swapping, there are no allocatable
344                  * dma pages left. But unfortunately, some shared parts of the kernel
345                  * (particularly the SCSI mid-level) call __get_dma_pages()
346                  * unconditionally :-( These calls then fail, and scsi.c even doesn't
347                  * check for NULL return values and just crashes. The quick fix for
348                  * this (instead of doing much clean up work in the SCSI code) is to
349                  * pretend all pages are DMA-able by setting mach_max_dma_address to
350                  * ULONG_MAX. This doesn't change any functionality so far, since
351                  * get_dma_pages() shouldn't be used on Atari anyway anymore (better
352                  * use atari_stram_alloc()), and the Atari SCSI drivers don't need DMA
353                  * memory. But unfortunately there's now no kind of warning (even not
354                  * a NULL return value) if you use get_dma_pages() nevertheless :-(
355                  * You just will get non-DMA-able memory...
356                  */
357                 mach_max_dma_address = 0xffffffff;
358         }
359 #endif
360 }
361
362 void atari_stram_mem_init_hook (void)
363 {
364         mem_init_done = 1;
365 }
366
367
368 /*
369  * This is main public interface: somehow allocate a ST-RAM block
370  * There are three strategies:
371  * 
372  *  - If we're before mem_init(), we have to make a static allocation. The
373  *    region is taken in the kernel data area (if the kernel is in ST-RAM) or
374  *    from the start of ST-RAM (if the kernel is in TT-RAM) and added to the
375  *    rsvd_stram_* region. The ST-RAM is somewhere in the middle of kernel
376  *    address space in the latter case.
377  * 
378  *  - If mem_init() already has been called and ST-RAM swapping is enabled,
379  *    try to get the memory from the (pseudo) swap-space, either free already
380  *    or by moving some other pages out of the swap.
381  *
382  *  - If mem_init() already has been called, and ST-RAM swapping is not
383  *    enabled, the only possibility is to try with __get_dma_pages(). This has
384  *    the disadvantage that it's very hard to get more than 1 page, and it is
385  *    likely to fail :-(
386  * 
387  */
388 void *atari_stram_alloc(long size, const char *owner)
389 {
390         void *addr = NULL;
391         BLOCK *block;
392         int flags;
393
394         DPRINTK("atari_stram_alloc(size=%08lx,owner=%s)\n", size, owner);
395
396         size = ALIGN_IF_SWAP(size);
397         DPRINTK( "atari_stram_alloc: rounded size = %08lx\n", size );
398 #ifdef CONFIG_STRAM_SWAP
399         if (max_swap_size) {
400                 /* If swapping is active: make some free space in the swap
401                    "device". */
402                 DPRINTK( "atari_stram_alloc: after mem_init, swapping ok, "
403                                  "calling get_region\n" );
404                 addr = get_stram_region( N_PAGES(size) );
405                 flags = BLOCK_INSWAP;
406         }
407         else
408 #endif
409         if (!mem_init_done)
410                 return alloc_bootmem_low(size);
411         else {
412                 /* After mem_init() and no swapping: can only resort to
413                  * __get_dma_pages() */
414                 addr = (void *)__get_dma_pages(GFP_KERNEL, get_order(size));
415                 flags = BLOCK_GFP;
416                 DPRINTK( "atari_stram_alloc: after mem_init, swapping off, "
417                                  "get_pages=%p\n", addr );
418         }
419
420         if (addr) {
421                 if (!(block = add_region( addr, size ))) {
422                         /* out of memory for BLOCK structure :-( */
423                         DPRINTK( "atari_stram_alloc: out of mem for BLOCK -- "
424                                          "freeing again\n" );
425 #ifdef CONFIG_STRAM_SWAP
426                         if (flags == BLOCK_INSWAP)
427                                 free_stram_region( SWAP_NR(addr), N_PAGES(size) );
428                         else
429 #endif
430                                 free_pages((unsigned long)addr, get_order(size));
431                         return( NULL );
432                 }
433                 block->owner = owner;
434                 block->flags |= flags;
435         }
436         return( addr );
437 }
438
439 void atari_stram_free( void *addr )
440
441 {
442         BLOCK *block;
443
444         DPRINTK( "atari_stram_free(addr=%p)\n", addr );
445
446         if (!(block = find_region( addr ))) {
447                 printk( KERN_ERR "Attempt to free non-allocated ST-RAM block at %p "
448                                 "from %p\n", addr, __builtin_return_address(0) );
449                 return;
450         }
451         DPRINTK( "atari_stram_free: found block (%p): size=%08lx, owner=%s, "
452                          "flags=%02x\n", block, block->size, block->owner, block->flags );
453         
454 #ifdef CONFIG_STRAM_SWAP
455         if (!max_swap_size) {
456 #endif
457                 if (block->flags & BLOCK_GFP) {
458                         DPRINTK("atari_stram_free: is kmalloced, order_size=%d\n",
459                                 get_order(block->size));
460                         free_pages((unsigned long)addr, get_order(block->size));
461                 }
462                 else
463                         goto fail;
464 #ifdef CONFIG_STRAM_SWAP
465         }
466         else if (block->flags & BLOCK_INSWAP) {
467                 DPRINTK( "atari_stram_free: is swap-alloced\n" );
468                 free_stram_region( SWAP_NR(block->start), N_PAGES(block->size) );
469         }
470         else
471                 goto fail;
472 #endif
473         remove_region( block );
474         return;
475
476   fail:
477         printk( KERN_ERR "atari_stram_free: cannot free block at %p "
478                         "(called from %p)\n", addr, __builtin_return_address(0) );
479 }
480
481
482 #ifdef CONFIG_STRAM_SWAP
483
484 \f
485 /* ------------------------------------------------------------------------ */
486 /*                                                 Main Swapping Functions                                                      */
487 /* ------------------------------------------------------------------------ */
488
489
490 /*
491  * Initialize ST-RAM swap device
492  * (lots copied and modified from sys_swapon() in mm/swapfile.c)
493  */
494 static int __init swap_init(void *start_mem, void *swap_data)
495 {
496         static struct dentry fake_dentry;
497         static struct vfsmount fake_vfsmnt;
498         struct swap_info_struct *p;
499         struct inode swap_inode;
500         unsigned int type;
501         void *addr;
502         int i, j, k, prev;
503
504         DPRINTK("swap_init(start_mem=%p, swap_data=%p)\n",
505                 start_mem, swap_data);
506         
507         /* need at least one page for swapping to (and this also isn't very
508          * much... :-) */
509         if (swap_end - swap_start < 2*PAGE_SIZE) {
510                 printk( KERN_WARNING "stram_swap_init: swap space too small\n" );
511                 return( 0 );
512         }
513         
514         /* find free slot in swap_info */
515         for( p = swap_info, type = 0; type < nr_swapfiles; type++, p++ )
516                 if (!(p->flags & SWP_USED))
517                         break;
518         if (type >= MAX_SWAPFILES) {
519                 printk( KERN_WARNING "stram_swap_init: max. number of "
520                                 "swap devices exhausted\n" );
521                 return( 0 );
522         }
523         if (type >= nr_swapfiles)
524                 nr_swapfiles = type+1;
525
526         stram_swap_info = p;
527         stram_swap_type = type;
528
529         /* fake some dir cache entries to give us some name in /dev/swaps */
530         fake_dentry.d_parent = &fake_dentry;
531         fake_dentry.d_name.name = "stram (internal)";
532         fake_dentry.d_name.len = 16;
533         fake_vfsmnt.mnt_parent = &fake_vfsmnt;
534         
535         p->flags        = SWP_USED;
536         p->swap_file    = &fake_dentry;
537         p->swap_vfsmnt  = &fake_vfsmnt;
538         p->swap_map     = swap_data;
539         p->cluster_nr   = 0;
540         p->next         = -1;
541         p->prio         = 0x7ff0;       /* a rather high priority, but not the higest
542                                                                  * to give the user a chance to override */
543
544         /* call stram_open() directly, avoids at least the overhead in
545          * constructing a dummy file structure... */
546         swap_inode.i_rdev = MKDEV( STRAM_MAJOR, STRAM_MINOR );
547         stram_open( &swap_inode, MAGIC_FILE_P );
548         p->max = SWAP_NR(swap_end);
549
550         /* initialize swap_map: set regions that are already allocated or belong
551          * to kernel data space to SWAP_MAP_BAD, otherwise to free */
552         j = 0; /* # of free pages */
553         k = 0; /* # of already allocated pages (from pre-mem_init stram_alloc()) */
554         p->lowest_bit = 0;
555         p->highest_bit = 0;
556         for( i = 1, addr = SWAP_ADDR(1); i < p->max;
557                  i++, addr += PAGE_SIZE ) {
558                 if (in_some_region( addr )) {
559                         p->swap_map[i] = SWAP_MAP_BAD;
560                         ++k;
561                 }
562                 else if (kernel_in_stram && addr < start_mem ) {
563                         p->swap_map[i] = SWAP_MAP_BAD;
564                 }
565                 else {
566                         p->swap_map[i] = 0;
567                         ++j;
568                         if (!p->lowest_bit) p->lowest_bit = i;
569                         p->highest_bit = i;
570                 }
571         }
572         /* first page always reserved (and doesn't really belong to swap space) */
573         p->swap_map[0] = SWAP_MAP_BAD;
574
575         /* now swapping to this device ok */
576         p->pages = j + k;
577         swap_list_lock();
578         nr_swap_pages += j;
579         p->flags = SWP_WRITEOK;
580
581         /* insert swap space into swap_list */
582         prev = -1;
583         for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
584                 if (p->prio >= swap_info[i].prio) {
585                         break;
586                 }
587                 prev = i;
588         }
589         p->next = i;
590         if (prev < 0) {
591                 swap_list.head = swap_list.next = p - swap_info;
592         } else {
593                 swap_info[prev].next = p - swap_info;
594         }
595         swap_list_unlock();
596
597         printk( KERN_INFO "Using %dk (%d pages) of ST-RAM as swap space.\n",
598                         p->pages << 2, p->pages );
599         return( 1 );
600 }
601
602
603 /*
604  * The swap entry has been read in advance, and we return 1 to indicate
605  * that the page has been used or is no longer needed.
606  *
607  * Always set the resulting pte to be nowrite (the same as COW pages
608  * after one process has exited).  We don't know just how many PTEs will
609  * share this swap entry, so be cautious and let do_wp_page work out
610  * what to do if a write is requested later.
611  */
612 static inline void unswap_pte(struct vm_area_struct * vma, unsigned long
613                               address, pte_t *dir, swp_entry_t entry,
614                               struct page *page)
615 {
616         pte_t pte = *dir;
617
618         if (pte_none(pte))
619                 return;
620         if (pte_present(pte)) {
621                 /* If this entry is swap-cached, then page must already
622                    hold the right address for any copies in physical
623                    memory */
624                 if (pte_page(pte) != page)
625                         return;
626                 /* We will be removing the swap cache in a moment, so... */
627                 set_pte(dir, pte_mkdirty(pte));
628                 return;
629         }
630         if (pte_val(pte) != entry.val)
631                 return;
632
633         DPRINTK("unswap_pte: replacing entry %08lx by new page %p",
634                 entry.val, page);
635         set_pte(dir, pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
636         swap_free(entry);
637         get_page(page);
638         // ++vma->vm_mm->rss;
639         vx_rsspages_inc(vma->vm_mm);
640 }
641
642 static inline void unswap_pmd(struct vm_area_struct * vma, pmd_t *dir,
643                               unsigned long address, unsigned long size,
644                               unsigned long offset, swp_entry_t entry,
645                               struct page *page)
646 {
647         pte_t * pte;
648         unsigned long end;
649
650         if (pmd_none(*dir))
651                 return;
652         if (pmd_bad(*dir)) {
653                 pmd_ERROR(*dir);
654                 pmd_clear(dir);
655                 return;
656         }
657         pte = pte_offset_kernel(dir, address);
658         offset += address & PMD_MASK;
659         address &= ~PMD_MASK;
660         end = address + size;
661         if (end > PMD_SIZE)
662                 end = PMD_SIZE;
663         do {
664                 unswap_pte(vma, offset+address-vma->vm_start, pte, entry, page);
665                 address += PAGE_SIZE;
666                 pte++;
667         } while (address < end);
668 }
669
670 static inline void unswap_pgd(struct vm_area_struct * vma, pgd_t *dir,
671                               unsigned long address, unsigned long size,
672                               swp_entry_t entry, struct page *page)
673 {
674         pmd_t * pmd;
675         unsigned long offset, end;
676
677         if (pgd_none(*dir))
678                 return;
679         if (pgd_bad(*dir)) {
680                 pgd_ERROR(*dir);
681                 pgd_clear(dir);
682                 return;
683         }
684         pmd = pmd_offset(dir, address);
685         offset = address & PGDIR_MASK;
686         address &= ~PGDIR_MASK;
687         end = address + size;
688         if (end > PGDIR_SIZE)
689                 end = PGDIR_SIZE;
690         do {
691                 unswap_pmd(vma, pmd, address, end - address, offset, entry,
692                            page);
693                 address = (address + PMD_SIZE) & PMD_MASK;
694                 pmd++;
695         } while (address < end);
696 }
697
698 static void unswap_vma(struct vm_area_struct * vma, pgd_t *pgdir,
699                        swp_entry_t entry, struct page *page)
700 {
701         unsigned long start = vma->vm_start, end = vma->vm_end;
702
703         do {
704                 unswap_pgd(vma, pgdir, start, end - start, entry, page);
705                 start = (start + PGDIR_SIZE) & PGDIR_MASK;
706                 pgdir++;
707         } while (start < end);
708 }
709
710 static void unswap_process(struct mm_struct * mm, swp_entry_t entry, 
711                            struct page *page)
712 {
713         struct vm_area_struct* vma;
714
715         /*
716          * Go through process' page directory.
717          */
718         if (!mm)
719                 return;
720         for (vma = mm->mmap; vma; vma = vma->vm_next) {
721                 pgd_t * pgd = pgd_offset(mm, vma->vm_start);
722                 unswap_vma(vma, pgd, entry, page);
723         }
724 }
725
726
727 static int unswap_by_read(unsigned short *map, unsigned long max,
728                           unsigned long start, unsigned long n_pages)
729 {
730         struct task_struct *p;
731         struct page *page;
732         swp_entry_t entry;
733         unsigned long i;
734
735         DPRINTK( "unswapping %lu..%lu by reading in\n",
736                          start, start+n_pages-1 );
737
738         for( i = start; i < start+n_pages; ++i ) {
739                 if (map[i] == SWAP_MAP_BAD) {
740                         printk( KERN_ERR "get_stram_region: page %lu already "
741                                         "reserved??\n", i );
742                         continue;
743                 }
744
745                 if (map[i]) {
746                         entry = swp_entry(stram_swap_type, i);
747                         DPRINTK("unswap: map[i=%lu]=%u nr_swap=%u\n",
748                                 i, map[i], nr_swap_pages);
749
750                         swap_device_lock(stram_swap_info);
751                         map[i]++;
752                         swap_device_unlock(stram_swap_info);
753                         /* Get a page for the entry, using the existing
754                            swap cache page if there is one.  Otherwise,
755                            get a clean page and read the swap into it. */
756                         page = read_swap_cache_async(entry);
757                         if (!page) {
758                                 swap_free(entry);
759                                 return -ENOMEM;
760                         }
761                         read_lock(&tasklist_lock);
762                         for_each_process(p)
763                                 unswap_process(p->mm, entry, page);
764                         read_unlock(&tasklist_lock);
765                         shmem_unuse(entry, page);
766                         /* Now get rid of the extra reference to the
767                            temporary page we've been using. */
768                         if (PageSwapCache(page))
769                                 delete_from_swap_cache(page);
770                         __free_page(page);
771         #ifdef DO_PROC
772                         stat_swap_force++;
773         #endif
774                 }
775
776                 DPRINTK( "unswap: map[i=%lu]=%u nr_swap=%u\n",
777                                  i, map[i], nr_swap_pages );
778                 swap_list_lock();
779                 swap_device_lock(stram_swap_info);
780                 map[i] = SWAP_MAP_BAD;
781                 if (stram_swap_info->lowest_bit == i)
782                         stram_swap_info->lowest_bit++;
783                 if (stram_swap_info->highest_bit == i)
784                         stram_swap_info->highest_bit--;
785                 --nr_swap_pages;
786                 swap_device_unlock(stram_swap_info);
787                 swap_list_unlock();
788         }
789
790         return 0;
791 }
792
793 /*
794  * reserve a region in ST-RAM swap space for an allocation
795  */
796 static void *get_stram_region( unsigned long n_pages )
797 {
798         unsigned short *map = stram_swap_info->swap_map;
799         unsigned long max = stram_swap_info->max;
800         unsigned long start, total_free, region_free;
801         int err;
802         void *ret = NULL;
803         
804         DPRINTK( "get_stram_region(n_pages=%lu)\n", n_pages );
805
806         down(&stram_swap_sem);
807
808         /* disallow writing to the swap device now */
809         stram_swap_info->flags = SWP_USED;
810
811         /* find a region of n_pages pages in the swap space including as much free
812          * pages as possible (and excluding any already-reserved pages). */
813         if (!(start = find_free_region( n_pages, &total_free, &region_free )))
814                 goto end;
815         DPRINTK( "get_stram_region: region starts at %lu, has %lu free pages\n",
816                          start, region_free );
817
818         err = unswap_by_read(map, max, start, n_pages);
819         if (err)
820                 goto end;
821
822         ret = SWAP_ADDR(start);
823   end:
824         /* allow using swap device again */
825         stram_swap_info->flags = SWP_WRITEOK;
826         up(&stram_swap_sem);
827         DPRINTK( "get_stram_region: returning %p\n", ret );
828         return( ret );
829 }
830
831
832 /*
833  * free a reserved region in ST-RAM swap space
834  */
835 static void free_stram_region( unsigned long offset, unsigned long n_pages )
836 {
837         unsigned short *map = stram_swap_info->swap_map;
838
839         DPRINTK( "free_stram_region(offset=%lu,n_pages=%lu)\n", offset, n_pages );
840
841         if (offset < 1 || offset + n_pages > stram_swap_info->max) {
842                 printk( KERN_ERR "free_stram_region: Trying to free non-ST-RAM\n" );
843                 return;
844         }
845
846         swap_list_lock();
847         swap_device_lock(stram_swap_info);
848         /* un-reserve the freed pages */
849         for( ; n_pages > 0; ++offset, --n_pages ) {
850                 if (map[offset] != SWAP_MAP_BAD)
851                         printk( KERN_ERR "free_stram_region: Swap page %lu was not "
852                                         "reserved\n", offset );
853                 map[offset] = 0;
854         }
855
856         /* update swapping meta-data */
857         if (offset < stram_swap_info->lowest_bit)
858                 stram_swap_info->lowest_bit = offset;
859         if (offset+n_pages-1 > stram_swap_info->highest_bit)
860                 stram_swap_info->highest_bit = offset+n_pages-1;
861         if (stram_swap_info->prio > swap_info[swap_list.next].prio)
862                 swap_list.next = swap_list.head;
863         nr_swap_pages += n_pages;
864         swap_device_unlock(stram_swap_info);
865         swap_list_unlock();
866 }
867
868 \f
869 /* ------------------------------------------------------------------------ */
870 /*                                              Utility Functions for Swapping                                          */
871 /* ------------------------------------------------------------------------ */
872
873
874 /* is addr in some of the allocated regions? */
875 static int in_some_region(void *addr)
876 {
877         BLOCK *p;
878         
879         for( p = alloc_list; p; p = p->next ) {
880                 if (p->start <= addr && addr < p->start + p->size)
881                         return( 1 );
882         }
883         return( 0 );
884 }
885
886
887 static unsigned long find_free_region(unsigned long n_pages,
888                                       unsigned long *total_free,
889                                       unsigned long *region_free)
890 {
891         unsigned short *map = stram_swap_info->swap_map;
892         unsigned long max = stram_swap_info->max;
893         unsigned long head, tail, max_start;
894         long nfree, max_free;
895
896         /* first scan the swap space for a suitable place for the allocation */
897         head = 1;
898         max_start = 0;
899         max_free = -1;
900         *total_free = 0;
901
902   start_over:
903         /* increment tail until final window size reached, and count free pages */
904         nfree = 0;
905         for( tail = head; tail-head < n_pages && tail < max; ++tail ) {
906                 if (map[tail] == SWAP_MAP_BAD) {
907                         head = tail+1;
908                         goto start_over;
909                 }
910                 if (!map[tail]) {
911                         ++nfree;
912                         ++*total_free;
913                 }
914         }
915         if (tail-head < n_pages)
916                 goto out;
917         if (nfree > max_free) {
918                 max_start = head;
919                 max_free  = nfree;
920                 if (max_free >= n_pages)
921                         /* don't need more free pages... :-) */
922                         goto out;
923         }
924         
925         /* now shift the window and look for the area where as much pages as
926          * possible are free */
927         while( tail < max ) {
928                 nfree -= (map[head++] == 0);
929                 if (map[tail] == SWAP_MAP_BAD) {
930                         head = tail+1;
931                         goto start_over;
932                 }
933                 if (!map[tail]) {
934                         ++nfree;
935                         ++*total_free;
936                 }
937                 ++tail;
938                 if (nfree > max_free) {
939                         max_start = head;
940                         max_free  = nfree;
941                         if (max_free >= n_pages)
942                                 /* don't need more free pages... :-) */
943                                 goto out;
944                 }
945         }
946
947   out:
948         if (max_free < 0) {
949                 printk( KERN_NOTICE "get_stram_region: ST-RAM too full or fragmented "
950                                 "-- can't allocate %lu pages\n", n_pages );
951                 return( 0 );
952         }
953
954         *region_free = max_free;
955         return( max_start );
956 }
957
958
959 /* setup parameters from command line */
960 void __init stram_swap_setup(char *str, int *ints)
961 {
962         if (ints[0] >= 1)
963                 max_swap_size = ((ints[1] < 0 ? 0 : ints[1]) * 1024) & PAGE_MASK;
964 }
965
966 \f
967 /* ------------------------------------------------------------------------ */
968 /*                                                              ST-RAM device                                                           */
969 /* ------------------------------------------------------------------------ */
970
971 static int refcnt;
972
973 static void do_stram_request(request_queue_t *q)
974 {
975         struct request *req;
976
977         while ((req = elv_next_request(q)) != NULL) {
978                 void *start = swap_start + (req->sector << 9);
979                 unsigned long len = req->current_nr_sectors << 9;
980                 if ((start + len) > swap_end) {
981                         printk( KERN_ERR "stram: bad access beyond end of device: "
982                                         "block=%ld, count=%d\n",
983                                         req->sector,
984                                         req->current_nr_sectors );
985                         end_request(req, 0);
986                         continue;
987                 }
988
989                 if (req->cmd == READ) {
990                         memcpy(req->buffer, start, len);
991 #ifdef DO_PROC
992                         stat_swap_read += N_PAGES(len);
993 #endif
994                 }
995                 else {
996                         memcpy(start, req->buffer, len);
997 #ifdef DO_PROC
998                         stat_swap_write += N_PAGES(len);
999 #endif
1000                 }
1001                 end_request(req, 1);
1002         }
1003 }
1004
1005
1006 static int stram_open( struct inode *inode, struct file *filp )
1007 {
1008         if (filp != MAGIC_FILE_P) {
1009                 printk( KERN_NOTICE "Only kernel can open ST-RAM device\n" );
1010                 return( -EPERM );
1011         }
1012         if (refcnt)
1013                 return( -EBUSY );
1014         ++refcnt;
1015         return( 0 );
1016 }
1017
1018 static int stram_release( struct inode *inode, struct file *filp )
1019 {
1020         if (filp != MAGIC_FILE_P) {
1021                 printk( KERN_NOTICE "Only kernel can close ST-RAM device\n" );
1022                 return( -EPERM );
1023         }
1024         if (refcnt > 0)
1025                 --refcnt;
1026         return( 0 );
1027 }
1028
1029
1030 static struct block_device_operations stram_fops = {
1031         .open =         stram_open,
1032         .release =      stram_release,
1033 };
1034
1035 static struct gendisk *stram_disk;
1036 static struct request_queue *stram_queue;
1037 static spinlock_t stram_lock = SPIN_LOCK_UNLOCKED;
1038
1039 int __init stram_device_init(void)
1040 {
1041         if (!MACH_IS_ATARI)
1042                 /* no point in initializing this, I hope */
1043                 return -ENXIO;
1044
1045         if (!max_swap_size)
1046                 /* swapping not enabled */
1047                 return -ENXIO;
1048         stram_disk = alloc_disk(1);
1049         if (!stram_disk)
1050                 return -ENOMEM;
1051
1052         if (register_blkdev(STRAM_MAJOR, "stram")) {
1053                 put_disk(stram_disk);
1054                 return -ENXIO;
1055         }
1056
1057         stram_queue = blk_init_queue(do_stram_request, &stram_lock);
1058         if (!stram_queue) {
1059                 unregister_blkdev(STRAM_MAJOR, "stram");
1060                 put_disk(stram_disk);
1061                 return -ENOMEM;
1062         }
1063
1064         stram_disk->major = STRAM_MAJOR;
1065         stram_disk->first_minor = STRAM_MINOR;
1066         stram_disk->fops = &stram_fops;
1067         stram_disk->queue = stram_queue;
1068         sprintf(stram_disk->disk_name, "stram");
1069         set_capacity(stram_disk, (swap_end - swap_start)/512);
1070         add_disk(stram_disk);
1071         return 0;
1072 }
1073
1074
1075 \f
1076 /* ------------------------------------------------------------------------ */
1077 /*                                                      Misc Utility Functions                                                  */
1078 /* ------------------------------------------------------------------------ */
1079
1080 /* reserve a range of pages */
1081 static void reserve_region(void *start, void *end)
1082 {
1083         reserve_bootmem (virt_to_phys(start), end - start);
1084 }
1085
1086 #endif /* CONFIG_STRAM_SWAP */
1087
1088 \f
1089 /* ------------------------------------------------------------------------ */
1090 /*                                                        Region Management                                                             */
1091 /* ------------------------------------------------------------------------ */
1092
1093
1094 /* insert a region into the alloced list (sorted) */
1095 static BLOCK *add_region( void *addr, unsigned long size )
1096 {
1097         BLOCK **p, *n = NULL;
1098         int i;
1099
1100         for( i = 0; i < N_STATIC_BLOCKS; ++i ) {
1101                 if (static_blocks[i].flags & BLOCK_FREE) {
1102                         n = &static_blocks[i];
1103                         n->flags = 0;
1104                         break;
1105                 }
1106         }
1107         if (!n && mem_init_done) {
1108                 /* if statics block pool exhausted and we can call kmalloc() already
1109                  * (after mem_init()), try that */
1110                 n = kmalloc( sizeof(BLOCK), GFP_KERNEL );
1111                 if (n)
1112                         n->flags = BLOCK_KMALLOCED;
1113         }
1114         if (!n) {
1115                 printk( KERN_ERR "Out of memory for ST-RAM descriptor blocks\n" );
1116                 return( NULL );
1117         }
1118         n->start = addr;
1119         n->size  = size;
1120
1121         for( p = &alloc_list; *p; p = &((*p)->next) )
1122                 if ((*p)->start > addr) break;
1123         n->next = *p;
1124         *p = n;
1125
1126         return( n );
1127 }
1128
1129
1130 /* find a region (by start addr) in the alloced list */
1131 static BLOCK *find_region( void *addr )
1132 {
1133         BLOCK *p;
1134         
1135         for( p = alloc_list; p; p = p->next ) {
1136                 if (p->start == addr)
1137                         return( p );
1138                 if (p->start > addr)
1139                         break;
1140         }
1141         return( NULL );
1142 }
1143
1144
1145 /* remove a block from the alloced list */
1146 static int remove_region( BLOCK *block )
1147 {
1148         BLOCK **p;
1149         
1150         for( p = &alloc_list; *p; p = &((*p)->next) )
1151                 if (*p == block) break;
1152         if (!*p)
1153                 return( 0 );
1154
1155         *p = block->next;
1156         if (block->flags & BLOCK_KMALLOCED)
1157                 kfree( block );
1158         else
1159                 block->flags |= BLOCK_FREE;
1160         return( 1 );
1161 }
1162
1163
1164 \f
1165 /* ------------------------------------------------------------------------ */
1166 /*                                               /proc statistics file stuff                                            */
1167 /* ------------------------------------------------------------------------ */
1168
1169 #ifdef DO_PROC
1170
1171 #define PRINT_PROC(fmt,args...) len += sprintf( buf+len, fmt, ##args )
1172
1173 int get_stram_list( char *buf )
1174 {
1175         int len = 0;
1176         BLOCK *p;
1177 #ifdef CONFIG_STRAM_SWAP
1178         int i;
1179         unsigned short *map = stram_swap_info->swap_map;
1180         unsigned long max = stram_swap_info->max;
1181         unsigned free = 0, used = 0, rsvd = 0;
1182 #endif
1183
1184 #ifdef CONFIG_STRAM_SWAP
1185         if (max_swap_size) {
1186                 for( i = 1; i < max; ++i ) {
1187                         if (!map[i])
1188                                 ++free;
1189                         else if (map[i] == SWAP_MAP_BAD)
1190                                 ++rsvd;
1191                         else
1192                                 ++used;
1193                 }
1194                 PRINT_PROC(
1195                         "Total ST-RAM:      %8u kB\n"
1196                         "Total ST-RAM swap: %8lu kB\n"
1197                         "Free swap:         %8u kB\n"
1198                         "Used swap:         %8u kB\n"
1199                         "Allocated swap:    %8u kB\n"
1200                         "Swap Reads:        %8u\n"
1201                         "Swap Writes:       %8u\n"
1202                         "Swap Forced Reads: %8u\n",
1203                         (stram_end - stram_start) >> 10,
1204                         (max-1) << (PAGE_SHIFT-10),
1205                         free << (PAGE_SHIFT-10),
1206                         used << (PAGE_SHIFT-10),
1207                         rsvd << (PAGE_SHIFT-10),
1208                         stat_swap_read,
1209                         stat_swap_write,
1210                         stat_swap_force );
1211         }
1212         else {
1213 #endif
1214                 PRINT_PROC( "ST-RAM swapping disabled\n" );
1215                 PRINT_PROC("Total ST-RAM:      %8u kB\n",
1216                            (stram_end - stram_start) >> 10);
1217 #ifdef CONFIG_STRAM_SWAP
1218         }
1219 #endif
1220
1221         PRINT_PROC( "Allocated regions:\n" );
1222         for( p = alloc_list; p; p = p->next ) {
1223                 if (len + 50 >= PAGE_SIZE)
1224                         break;
1225                 PRINT_PROC("0x%08lx-0x%08lx: %s (",
1226                            virt_to_phys(p->start),
1227                            virt_to_phys(p->start+p->size-1),
1228                            p->owner);
1229                 if (p->flags & BLOCK_GFP)
1230                         PRINT_PROC( "page-alloced)\n" );
1231                 else if (p->flags & BLOCK_INSWAP)
1232                         PRINT_PROC( "in swap)\n" );
1233                 else
1234                         PRINT_PROC( "??)\n" );
1235         }
1236
1237         return( len );
1238 }
1239
1240 #endif
1241
1242
1243 /*
1244  * Local variables:
1245  *  c-indent-level: 4
1246  *  tab-width: 4
1247  * End:
1248  */