Date: Mon, 24 Nov 1997 10:48:22 GMT
From: Roman Hodek <rnhodek@faui22c.informatik.uni-erlangen.de>
To: linux-m68k@lists.linux-m68k.org
Subject: L68K: ST-RAM swapping patch
Sender: owner-linux-m68k@phil.uni-sb.de


[ Monday is patch day ;-) ]

I was working on it a rather long time :-) (Jes knows... ;-)), but now
it's there: my ST-RAM swapping patch.

What's that about? Maybe some of you have already seen that it's
rather hard to load modules that need ST-RAM buffers. That is due to
the fact that those buffers are allocated by __get_dma_pages(), and
that function isn't very clever at allocating more than one page. That
is not a bug in the memory allocation, but a more general problem.
(See comments in stram.c for details.) But the net effect was that
you often were out of luck if you wanted more than one ST-RAM (= DMA)
page :-(

My workaround for this problem is to turn ST-RAM into swap space. This
has the big advantage that the kernel can now make free space in
ST-RAM on request, and thus fulfill ST-RAM requests much better.
(Again, see stram.c for details of this.) It's now even possible to
load acsi_slm as module, which needs a (contiguous) 1 MB ST-RAM buffer
:-) (BTW, you can still have the kernel in ST-RAM, and the rest is
swap.)

Of course, the ST-RAM swapping is a config option, so you're free to
turn it off alltogether. But even if enabled, you have dynamic control
over it with a "stram_swap=" command line option. The parameter of
this option is the max. number of kByte to use as swap space. This
allows you also to use only parts of ST-RAM for swapping. If 0, ST-RAM
swapping is turned off completely.

There's also a config option for a info file in /proc: /proc/stram. It
gives you at least a list of allocated regions in ST-RAM. If swapping
is enabled, also some statistics about the swap space are printed.

Using ST-RAM as swap (still a rather high-speed swap device...)
can/should even improve overall system performance a bit! :-) I expect
this, because then all code is executed in faster TT-RAM, and also
often-needed data are kept there. ST-RAM becomes just a secondary
storage for not-so-often needed data. Maybe the effect isn't already
visible on a TT, but it should be more obvious on machines where the
speed difference between kinds of RAM is more drastic (e.g.
Afterburner, PAK(?)). However, I haven't made any performance
measurements... (any volunteers? :-)

Other goodies included in this patch:

 - I forgot to export atari_{MFP,SCC}_init_done symbols to modules.

 - Removed forgotten debug code in SCC init (for debug/sercons)

 - If kgdb was enabled, all kernel messages were sent over the serial
   port, too, and thus got lost (went to gdb which ignores them). The
   variable 'kgdb_initialized' isn't initialized where
   atari_debug_init is called now... Fix: Never do debugging init if
   kgdb is enabled.

 - Avoid warning about unused var 'do_floppy' in ataflop.c

 - kgdb.c: gdb doesn't send an ACK after kill or reset command, don't
   wait for it.

 - m68kserial.c always registers its struct console, even if no serial
   port has been selected. This can cause problems if sercons support
   is selected, but no console= option is given, because the write
   method is initialized to NULL. Therefore replaced uninitialize
   write and wait_key methods by dummy functions that simply do
   nothing.

 - Removed unused var in atafb.c:ext_getcolreg().

 - Fix comment in <asm/signal.h> about SA_STACK.

Roman


diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/Documentation/Configure.help linux-2.1.64/Documentation/Configure.help
--- linux-2.1.64.orig/Documentation/Configure.help	Mon Nov 17 12:23:35 1997
+++ linux-2.1.64/Documentation/Configure.help	Mon Nov 17 22:54:48 1997
@@ -5951,6 +5951,18 @@
   Include support in the kernel for the Commodore/University of Lowell
   A2410 graphics card.  If you have one, say Y; otherwise, say N.
 
+Atari ST-RAM swap support
+CONFIG_STRAM_SWAP
+  This enables support for using (parts of) ST-RAM as swap space,
+  instead of as normal system memory. This can first enhance system
+  performace if you have lots of alternate RAM (compared to the size
+  of ST-RAM), because executable code always will reside in faster
+  memory. ST-RAM will remain as ultra-fast swap space. On the other
+  hand, it allows much improved dynamic allocations of ST-RAM buffers
+  for device driver modules (e.g. floppy, ACSI, SLM printer, DMA
+  sound). The probability that such allocations at module load time
+  fail is drastically reduced.
+
 Amiga Zorro II ramdisk support
 CONFIG_AMIGA_Z2RAM
   This enables support for using Chip RAM and Zorro II RAM as a
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/Documentation/m68k/kernel-options.txt linux-2.1.64/Documentation/m68k/kernel-options.txt
--- linux-2.1.64.orig/Documentation/m68k/kernel-options.txt	Mon Oct  6 10:31:52 1997
+++ linux-2.1.64/Documentation/m68k/kernel-options.txt	Mon Nov 17 22:54:48 1997
@@ -650,6 +650,27 @@
     can be performed in optimal order. Not all SCSI devices support
     tagged queuing (:-().
 
+4.5) stram_swap=
+----------------
+
+Syntax: stram_swap=<do_swap>[,<max_swap>]
+
+  This option is available only if the kernel has been compiled with
+CONFIG_STRAM_SWAP enabled. Normally, the kernel then determines
+dynamically whether to actually use ST-RAM as swap space. (Currently,
+the fraction of ST-RAM must be less or equal 1/3 of total memory to
+enable this swapping.) You can override the kernel's decision by
+specifying this option. 1 for <do_swap> means always enable the swap,
+even if you have less alternate RAM. 0 stands for never swap to
+ST-RAM, even if it's small enough compared to the rest of memory.
+
+  If ST-RAM swapping is enabled, the kernel usually uses all free
+ST-RAM as swap "device". (If the kernel resides in ST-RAM, the region
+allocated by it is obviously never used for swapping :-) You can also
+limit this amount by specifying the second parameter, <max_swap>, if
+you want to use parts of ST-RAM as normal system memory. <max_swap> is
+in kBytes and the number should be a multiple of 4 (otherwise: rounded
+down).
 
 
 5) Options for Amiga Only:
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/arch/m68k/atari/atari_ksyms.c linux-2.1.64/arch/m68k/atari/atari_ksyms.c
--- linux-2.1.64.orig/arch/m68k/atari/atari_ksyms.c	Mon Nov 17 12:47:37 1997
+++ linux-2.1.64/arch/m68k/atari/atari_ksyms.c	Sat Nov 22 01:08:08 1997
@@ -7,8 +7,11 @@
 #include <asm/atarikb.h>
 #include <asm/atari_joystick.h>
 #include <asm/atari_stdma.h>
+#include <asm/atari_stram.h>
 
 extern void atari_microwire_cmd( int cmd );
+extern int atari_MFP_init_done;
+extern int atari_SCC_init_done;
 extern int atari_SCC_reset_done;
 
 EXPORT_SYMBOL(atari_mch_cookie);
@@ -20,10 +23,14 @@
 EXPORT_SYMBOL(stdma_release);
 EXPORT_SYMBOL(stdma_others_waiting);
 EXPORT_SYMBOL(stdma_islocked);
+EXPORT_SYMBOL(atari_stram_alloc);
+EXPORT_SYMBOL(atari_stram_free);
 
 EXPORT_SYMBOL(atari_mouse_buttons);
 EXPORT_SYMBOL(atari_mouse_interrupt_hook);
 EXPORT_SYMBOL(atari_MIDI_interrupt_hook);
+EXPORT_SYMBOL(atari_MFP_init_done);
+EXPORT_SYMBOL(atari_SCC_init_done);
 EXPORT_SYMBOL(atari_SCC_reset_done);
 EXPORT_SYMBOL(ikbd_write);
 EXPORT_SYMBOL(ikbd_mouse_y0_top);
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/arch/m68k/atari/config.c linux-2.1.64/arch/m68k/atari/config.c
--- linux-2.1.64.orig/arch/m68k/atari/config.c	Mon Nov 17 21:37:23 1997
+++ linux-2.1.64/arch/m68k/atari/config.c	Fri Nov 21 22:18:51 1997
@@ -39,6 +39,7 @@
 #include <asm/setup.h>
 #include <asm/atarihw.h>
 #include <asm/atariints.h>
+#include <asm/atari_stram.h>
 
 #include <asm/system.h>
 #include <asm/io.h>
@@ -1062,7 +1063,6 @@
 	MFPDELAY();					\
 	scc.cha_b_ctrl = (val);				\
 	MFPDELAY();					\
-	printk("WR%02d: %02x\n",(reg),(val));	\
     } while(0)
 
 /* loops_per_sec isn't initialized yet, so we can't use udelay(). This does a
@@ -1144,11 +1144,9 @@
 __initfunc(static void atari_debug_init(void))
 {
 #ifdef CONFIG_KGDB
-	/* if the m68k_debug_device is used by the GDB stub, do nothing here */
-    if (kgdb_initialized)
-	return;
-#endif
-
+    /* the m68k_debug_device is used by the GDB stub, do nothing here */
+    return;
+#else
     if (!strcmp( m68k_debug_device, "ser" )) {
 	/* defaults to ser2 for a Falcon and ser1 otherwise */
 	strcpy( m68k_debug_device, MACH_IS_FALCON ? "ser2" : "ser1" );
@@ -1177,6 +1175,7 @@
     }
     if (atari_console_driver.write)
 	register_console(&atari_console_driver);
+#endif
 }
 
 /* ++roman:
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/arch/m68k/atari/stram.c linux-2.1.64/arch/m68k/atari/stram.c
--- linux-2.1.64.orig/arch/m68k/atari/stram.c	Fri May 16 19:47:07 1997
+++ linux-2.1.64/arch/m68k/atari/stram.c	Sat Nov 22 01:35:04 1997
@@ -1,157 +1,54 @@
+/*
+ * arch/m68k/atari/stram.c: Functions for ST-RAM allocations
+ *
+ * Copyright 1994-97 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License.  See the file COPYING in the main directory of this archive
+ * for more details.
+ */
 
+#include <linux/config.h>
 #include <linux/types.h>
 #include <linux/kernel.h>
 #include <linux/mm.h>
+#include <linux/kdev_t.h>
+#include <linux/major.h>
+#include <linux/init.h>
+#include <linux/swap.h>
+#include <linux/malloc.h>
+#include <linux/vmalloc.h>
+#include <linux/pagemap.h>
 #include <asm/setup.h>
-#include <asm/atarihw.h>
+#include <asm/machdep.h>
 #include <asm/page.h>
 #include <asm/pgtable.h>
+#include <asm/atarihw.h>
+#include <asm/atari_stram.h>
 
-#if 0
-
-struct stram_desc
-  {
-    unsigned first:1;
-    unsigned last:1;
-    unsigned alloced:1;
-    unsigned length:24;
-  };
-
-#define DP(ptr) ((struct stram_desc *) (ptr))
-
-static unsigned long stramsize;
-static unsigned long stramaddr;
-
-void
-atari_stram_init (void)
-{
-  struct stram_desc *dp;
-  stramaddr = atari_stram_start;
-  stramsize = atari_stram_size;
-
-  /* initialize start boundary */
-  dp = DP (stramaddr);
-  dp->first = 1;
-  dp->alloced = 0;
-  dp->length = stramsize - 2 * sizeof (*dp);
-
-  /* initialize end boundary */
-  dp = DP (stramaddr + stramsize) - 1;
-  dp->last = 1;
-  dp->alloced = 0;
-  dp->length = stramsize - 2 * sizeof (*dp);
-
-#ifdef DEBUG
-  printk ("stram end boundary is %p, length is %d\n", dp,
-	  dp->length);
-#endif
-}
-
-void *
-atari_stram_alloc (long size)
-{
-  /* last chunk */
-  struct stram_desc *dp;
-  void *ptr;
-
-  /* round off */
-  size = (size + 3) & ~3;
 
-#ifdef DEBUG
-  printk ("stram_alloc: allocate %ld bytes\n", size);
-#endif
+#define MAJOR_NR    Z2RAM_MAJOR
+#include <linux/blk.h>
+#undef DEVICE_NAME
+#define DEVICE_NAME	"stram"
 
-  /*
-   * get pointer to descriptor for last chunk by 
-   * going backwards from end chunk
-   */
-  dp = DP (stramaddr + stramsize) - 1;
-  dp = DP ((unsigned long) dp - dp->length) - 1;
-
-  while ((dp->alloced || dp->length < size) && !dp->first)
-    dp = DP ((unsigned long) dp - dp[-1].length) - 2;
-
-  if (dp->alloced || dp->length < size)
-    {
-      printk ("no stram available for %ld allocation\n", size);
-      return NULL;
-    }
+#undef DEBUG
 
-  if (dp->length < size + 2 * sizeof (*dp))
-    {
-      /* length too small to split; allocate the whole thing */
-      dp->alloced = 1;
-      ptr = (void *) (dp + 1);
-      dp = DP ((unsigned long) ptr + dp->length);
-      dp->alloced = 1;
 #ifdef DEBUG
-      printk ("stram_alloc: no split\n");
-#endif
-    }
-  else
-    {
-      /* split the extent; use the end part */
-      long newsize = dp->length - (2 * sizeof (*dp) + size);
-
-#ifdef DEBUG
-      printk ("stram_alloc: splitting %d to %ld\n", dp->length,
-	      newsize);
+#define	DPRINTK(fmt,args...) printk( fmt, ##args )
+#else
+#define DPRINTK(fmt,args...)
 #endif
-      dp->length = newsize;
-      dp = DP ((unsigned long) (dp + 1) + newsize);
-      dp->first = dp->last = 0;
-      dp->alloced = 0;
-      dp->length = newsize;
-      dp++;
-      dp->first = dp->last = 0;
-      dp->alloced = 1;
-      dp->length = size;
-      ptr = (void *) (dp + 1);
-      dp = DP ((unsigned long) ptr + size);
-      dp->alloced = 1;
-      dp->length = size;
-    }
 
-#ifdef DEBUG
-  printk ("stram_alloc: returning %p\n", ptr);
+#if defined(CONFIG_PROC_FS) && defined(CONFIG_STRAM_PROC)
+/* abbrev for the && above... */
+#define DO_PROC
+#include <linux/proc_fs.h>
 #endif
-  return ptr;
-}
-
-void 
-atari_stram_free (void *ptr)
-{
-  struct stram_desc *sdp = DP (ptr) - 1, *dp2;
-  struct stram_desc *edp = DP ((unsigned long) ptr + sdp->length);
-
-  /* deallocate the chunk */
-  sdp->alloced = edp->alloced = 0;
-
-  /* check if we should merge with the previous chunk */
-  if (!sdp->first && !sdp[-1].alloced)
-    {
-      dp2 = DP ((unsigned long) sdp - sdp[-1].length) - 2;
-      dp2->length += sdp->length + 2 * sizeof (*sdp);
-      edp->length = dp2->length;
-      sdp = dp2;
-    }
 
-  /* check if we should merge with the following chunk */
-  if (!edp->last && !edp[1].alloced)
-    {
-      dp2 = DP ((unsigned long) edp + edp[1].length) + 2;
-      dp2->length += edp->length + 2 * sizeof (*sdp);
-      sdp->length = dp2->length;
-      edp = dp2;
-    }
-}
-
-#else
-
-#include <linux/mm.h>
-#include <linux/init.h>
-
-/* ++roman:
+/* Pre-swapping comments:
+ *
+ * ++roman:
  * 
  * New version of ST-Ram buffer allocation. Instead of using the
  * 1 MB - 4 KB that remain when the the ST-Ram chunk starts at $1000
@@ -173,72 +70,1400 @@
  * no provision now for freeing ST-Ram buffers. It seems that isn't
  * really needed.
  *
- * ToDo:
- * Check the high level scsi code what is done when the
- * UNCHECKED_ISA_DMA flag is set. It guess, it is just a test for adr
- * < 16 Mega. There should be call to atari_stram_alloc() instead.
- *
- * Also ToDo:
- * Go through head.S and delete parts no longer needed (transparent
- * mapping of ST-Ram etc.)
+ */
+
+/*
+ * New Nov 1997: Use ST-RAM as swap space!
+ *
+ * In the past, there were often problems with modules that require ST-RAM
+ * buffers. Such drivers have to use __get_dma_pages(), which unfortunately
+ * often isn't very successful in allocating more than 1 page :-( [1] The net
+ * result was that most of the time you couldn't insmod such modules (ataflop,
+ * ACSI, SCSI on Falcon, Atari internal framebuffer, not to speak of acsi_slm,
+ * which needs a 1 MB buffer... :-).
+ *
+ * To overcome this limitation, ST-RAM can now be turned into a very
+ * high-speed swap space. If a request for an ST-RAM buffer comes, the kernel
+ * now tries to unswap some pages on that swap device to make some free (and
+ * contiguous) space. This works much better in comparison to
+ * __get_dma_pages(), since used swap pages can be selectively freed by either
+ * moving them to somewhere else in swap space, or by reading them back into
+ * system memory. Ok, there operation of unswapping isn't really cheap (for
+ * each page, one has to go through the page tables of all processes), but it
+ * doesn't happen that often (only when allocation ST-RAM, i.e. when loading a
+ * module that needs ST-RAM). But it at least makes it possible to load such
+ * modules!
+ *
+ * It could also be that overall system performance increases a bit due to
+ * ST-RAM swapping, since slow ST-RAM isn't used anymore for holding data or
+ * executing code in. It's then just a (very fast, compared to disk) back
+ * storage for not-so-often needed data. (But this effect must be compared
+ * with the loss of total memory...) Don't know if the effect is already
+ * visible on a TT, where the speed difference between ST- and TT-RAM isn't
+ * that dramatic, but it should on machines where TT-RAM is really much faster
+ * (e.g. Afterburner).
  * 
+ *   [1]: __get_free_pages() does a fine job if you only want one page, but if
+ * you want more (contiguous) pages, it can give you such a block only if
+ * there's already a free one. The algorithm can't try to free buffers or swap
+ * out something in order to make more free space, since all that page-freeing
+ * mechanisms work "target-less", i.e. they just free something, but not in a
+ * specific place. I.e., __get_free_pages() can't do anything to free
+ * *adjacent* pages :-( This situation becomes even worse for DMA memory,
+ * since the freeing algorithms are also blind to DMA capability of pages.
  */
-   
 
-unsigned long rsvd_stram_beg, rsvd_stram_end;
-    /* Start and end of the reserved ST-Ram region */
-static unsigned long stram_end;
-    /* Overall end of ST-Ram */
+#ifdef CONFIG_STRAM_SWAP
+#define ALIGN_IF_SWAP(x)	PAGE_ALIGN(x)
+#else
+#define ALIGN_IF_SWAP(x)	(x)
+#endif
+
+/* map entry for reserved swap page (used as buffer) */
+#define SWP_RSVD			0x80
 
+/* get index of swap page at address 'addr' */
+#define SWAP_NR(addr)		(((unsigned long)(addr)-swap_start) >> PAGE_SHIFT)
 
+/* get address of swap page #'nr' */
+#define SWAP_ADDR(nr)		((void *)(swap_start + ((nr)<<PAGE_SHIFT)))
+
+/* get number of pages for 'n' bytes (already page-aligned) */
+#define N_PAGES(n)			((n) >> PAGE_SHIFT)
+
+/* The following two numbers define the maximum fraction of ST-RAM in total
+ * memory, below that the kernel would automatically use ST-RAM as swap
+ * space. This decision can be overriden with stram_swap= */
+#define MAX_STRAM_FRACTION_NOM		1
+#define MAX_STRAM_FRACTION_DENOM	3
+
+/* Start and end of the (pre-mem_init) reserved ST-RAM region */
+static unsigned long rsvd_stram_beg, rsvd_stram_end;
+
+/* Start and end (virtual) of ST-RAM */
+static unsigned long stram_start, stram_end;
+
+/* set after memory_init() executed and allocations via start_mem aren't
+ * possible anymore */
+static int mem_init_done = 0;
+
+/* set if kernel is in ST-RAM */
+static int kernel_in_stram;
+
+#ifdef CONFIG_STRAM_SWAP
+/* max. number of bytes to use for swapping
+ *  0 = no ST-RAM swapping
+ * -1 = do swapping (to whole ST-RAM) if it's less than MAX_STRAM_FRACTION of
+ *      total memory
+ */
+static int max_swap_size = -1;
+
+/* start and end of swapping area */
+static unsigned long swap_start, swap_end;
+
+/* The ST-RAM's swap info structure */
+static struct swap_info_struct *stram_swap_info;
+
+/* The ST-RAM's swap type */
+static int stram_swap_type;
+
+typedef struct stram_block {
+	struct stram_block *next;
+	unsigned long start;
+	unsigned long size;
+	unsigned flags;
+	const char *owner;
+} BLOCK;
+
+/* values for flags field */
+#define BLOCK_FREE		0x01	/* free structure in the BLOCKs pool */
+#define BLOCK_KMALLOCED	0x02	/* structure allocated by kmalloc() */
+#define BLOCK_STATIC	0x04	/* pre-mem_init() allocated block */
+#define BLOCK_GFP		0x08	/* block allocated with __get_dma_pages() */
+#define BLOCK_INSWAP	0x10	/* block allocated in swap space */
+
+/* list of allocated blocks */
+static BLOCK *alloc_list = NULL;
+
+/* We can't always use kmalloc() to allocate BLOCK structures, since
+ * stram_alloc() can be called rather early. So we need some pool of
+ * statically allocated structures. 20 of them is more than enough, so in most
+ * cases we never should need to call kmalloc(). */
+#define N_STATIC_BLOCKS	20
+static BLOCK static_blocks[N_STATIC_BLOCKS];
+
+/* major and minor device number of the ST-RAM device; for the major, we use
+ * the same as Amiga z2ram, which is really similar and impossible on Atari,
+ * and for the minor a relatively odd number to avoid the user creating and
+ * using that device. */
+#define	STRAM_MAJOR		Z2RAM_MAJOR
+#define	STRAM_MINOR		13
+
+/* Some impossible pointer value */
+#define MAGIC_FILE_P	(struct file *)0xffffdead
+
+#ifdef DO_PROC
+static unsigned stat_swap_read = 0;
+static unsigned stat_swap_write = 0;
+static unsigned stat_swap_move = 0;
+static unsigned stat_swap_force = 0;
+#endif /* DO_PROC */
+
+#endif /* CONFIG_STRAM_SWAP */
+
+
+/***************************** Prototypes *****************************/
+
+#ifdef CONFIG_STRAM_SWAP
+static int swap_init( unsigned long start_mem, unsigned long swap_data );
+static inline int unswap_pte( struct vm_area_struct * vma, unsigned long
+                              address, pte_t *dir, unsigned long entry,
+                              unsigned long page, int isswap );
+static inline int unswap_pmd( struct vm_area_struct * vma, pmd_t *dir,
+                              unsigned long address, unsigned long size,
+                              unsigned long offset, unsigned long entry,
+                              unsigned long page, int isswap );
+static inline int unswap_pgd( struct vm_area_struct * vma, pgd_t *dir,
+                              unsigned long address, unsigned long size,
+                              unsigned long entry, unsigned long page, int
+                              isswap );
+static int unswap_vma( struct vm_area_struct * vma, pgd_t *pgdir, unsigned
+                       long entry, unsigned long page, int isswap );
+static int unswap_process( struct mm_struct * mm, unsigned long entry,
+                           unsigned long page, int isswap );
+static int unswap_by_move( unsigned char *map, unsigned long max, unsigned
+                           long start, unsigned long n_pages );
+static int unswap_by_read( unsigned char *map, unsigned long max, unsigned
+                           long start, unsigned long n_pages );
+static void *get_stram_region( unsigned long n_pages );
+static void free_stram_region( unsigned long offset, unsigned long n_pages
+                               );
+static int in_some_region( unsigned long addr );
+static unsigned long find_free_region( unsigned long n_pages, unsigned long
+                                       *total_free, unsigned long
+                                       *region_free );
+static void do_stram_request( void );
+static int stram_open( struct inode *inode, struct file *filp );
+static int stram_release( struct inode *inode, struct file *filp );
+static void do_z2_request( void );
+static int get_gfp_order( unsigned long size );
+#endif
+static void reserve_region( unsigned long addr, unsigned long end );
+static BLOCK *add_region( void *addr, unsigned long size );
+static BLOCK *find_region( void *addr );
+static int remove_region( BLOCK *block );
+
+/************************* End of Prototypes **************************/
+
+
+/* ------------------------------------------------------------------------ */
+/*							   Public Interface								*/
+/* ------------------------------------------------------------------------ */
+
+/*
+ * This init function is called very early by atari/config.c
+ * It initializes some internal variables needed for stram_alloc()
+ */
 __initfunc(void atari_stram_init( void ))
+{
+	int i;
 
-{	int		i;
+	/* initialize static blocks */
+	for( i = 0; i < N_STATIC_BLOCKS; ++i )
+		static_blocks[i].flags = BLOCK_FREE;
+
+	/* determine whether kernel code resides in ST-RAM (then ST-RAM is the
+	 * first memory block at virtual 0x0) */
+	stram_start = PTOV( 0 );
+	kernel_in_stram = (stram_start == 0);
 
 	for( i = 0; i < m68k_num_memory; ++i ) {
 		if (m68k_memory[i].addr == 0) {
-			rsvd_stram_beg = PTOV( 0x800 ); /* skip super-only first 2 KB! */
+			/* skip first 2kB or page (supervisor-only!) */
+			rsvd_stram_beg = stram_start + ALIGN_IF_SWAP(0x800);
 			rsvd_stram_end = rsvd_stram_beg;
-			stram_end = rsvd_stram_beg - 0x800 + m68k_memory[i].size;
+			stram_end = stram_start + m68k_memory[i].size;
 			return;
 		}
 	}
 	/* Should never come here! (There is always ST-Ram!) */
+	panic( "atari_stram_init: no ST-RAM found!" );
+}
+
+
+/*
+ * This function is called from mem_init() to reserve the pages needed for
+ * ST-RAM management.
+ */
+__initfunc(void atari_stram_reserve_pages( unsigned long start_mem ))
+{
+#ifdef CONFIG_STRAM_SWAP
+	/* if max_swap_size is negative (i.e. no stram_swap= option given),
+	 * determine at run time whether to use ST-RAM swapping */
+	if (max_swap_size < 0)
+		/* Use swapping if ST-RAM doesn't make up more than MAX_STRAM_FRACTION
+		 * of total memory. In that case, the max. size is set to 16 MB,
+		 * because ST-RAM can never be bigger than that.
+		 * Also, never use swapping on a Hades, there's no separate ST-RAM in
+		 * that machine. */
+		max_swap_size =
+			(!MACH_IS_HADES &&
+			 (N_PAGES(stram_end-stram_start)*MAX_STRAM_FRACTION_DENOM <=
+			  max_mapnr*MAX_STRAM_FRACTION_NOM)) ? 16*1024*1024 : 0;
+	DPRINTK( "atari_stram_reserve_pages: max_swap_size = %d\n", max_swap_size );
+#endif
+
+	/* always reserve first page of ST-RAM, the first 2 kB are
+	 * supervisor-only! */
+	set_bit( PG_reserved, &mem_map[MAP_NR(stram_start)].flags );
+
+#ifdef CONFIG_STRAM_SWAP
+	if (!max_swap_size) {
+	  fallback:
+#endif
+		DPRINTK( "atari_stram_reserve_pages: swapping disabled\n" );
+		if (!kernel_in_stram) {
+			/* Reserve all pages that have been marked by pre-mem_init
+			 * stram_alloc() (e.g. for the screen memory). */
+			reserve_region( rsvd_stram_beg, rsvd_stram_end );
+			DPRINTK( "atari_stram_reserve_pages: reseverved %08lx-%08lx\n",
+					 rsvd_stram_beg, rsvd_stram_end );
+		}
+		/* else (kernel in ST-RAM): nothing to do, ST-RAM buffers are
+		 * kernel data */
+#ifdef CONFIG_STRAM_SWAP
+	}
+	else {
+		unsigned long swap_data;
+		BLOCK *p;
+
+		/* determine first page to use as swap:
+		 * if the kernel is in TT-RAM, this is the first page of (usable)
+		 * ST-RAM; else if there were already some allocations (probable...),
+		 * use the lowest address of these (the list is sorted by address!);
+		 * otherwise just use the end of kernel data (= start_mem) */
+		swap_start = !kernel_in_stram ? stram_start + PAGE_SIZE :
+					 alloc_list ? alloc_list->start :
+					 start_mem;
+		/* decrement by one page, rest of kernel assumes that first swap page
+		 * is always reserved and maybe doesn't handle SWP_ENTRY == 0
+		 * correctly */
+		swap_start -= PAGE_SIZE;
+		swap_end = stram_end;
+		if (swap_end-swap_start > max_swap_size)
+			swap_end =  swap_start + max_swap_size;
+		DPRINTK( "atari_stram_reserve_pages: swapping enabled; "
+				 "swap=%08lx-%08lx\n", swap_start, swap_end );
+		
+		/* reserve some amount of memory for maintainance of swapping itself:
+		 * 1 page for the lockmap, and one page for each 4096 (PAGE_SIZE) swap
+		 * pages. (1 byte for each page) */
+		swap_data = start_mem;
+		start_mem += PAGE_ALIGN(SWAP_NR(swap_end)) + PAGE_SIZE;
+		/* correct swap_start if necessary */
+		if (swap_start == swap_data)
+			swap_start = start_mem;
+		
+		if (!swap_init( start_mem, swap_data )) {
+			printk( KERN_ERR "ST-RAM swap space initialization failed\n" );
+			max_swap_size = 0;
+			goto fallback;
+		}
+		/* reserve region for swapping meta-data */
+		reserve_region( swap_data, start_mem );
+		/* reserve swapping area itself */
+		reserve_region( swap_start+PAGE_SIZE, swap_end );
+
+		/* Formerly static areas have been included in the swap area. */
+		for( p = alloc_list; p; p = p->next ) {
+			if (p->flags & BLOCK_STATIC)
+				p->flags = (p->flags & ~BLOCK_STATIC) | BLOCK_INSWAP;
+		}
+
+		/*
+		 * If the whole ST-RAM is used for swapping, there are no allocatable
+		 * dma pages left. But unfortunately, some shared parts of the kernel
+		 * (particularily the SCSI mid-level) call __get_dma_pages()
+		 * unconditionally :-( These calls then fail, and scsi.c even doesn't
+		 * check for NULL return values and just crashes. The quick fix for
+		 * this (instead of doing much clean up work in the SCSI code) is to
+		 * pretend all pages are DMA-able by setting mach_max_dma_address to
+		 * ULONG_MAX. This doesn't change any functionality so far, since
+		 * get_dma_pages() shouldn't be used on Atari anyway anymore (better
+		 * use atari_stram_alloc()), and the Atari SCSI drivers don't need DMA
+		 * memory. But unfortunately there's now no kind of warning (even not
+		 * a NULL return value) if you use get_dma_pages() nevertheless :-(
+		 * You just will get non-DMA-able memory...
+		 */
+		mach_max_dma_address = 0xffffffff;
+	}
+#endif
+	
+	mem_init_done = 1;
+}
+
+
+/*
+ * This is main public interface: somehow allocate a ST-RAM block
+ * There are three strategies:
+ * 
+ *  - If we're before mem_init(), we have to make a static allocation. The
+ *    region is taken in the kernel data area (if the kernel is in ST-RAM) or
+ *    from the start of ST-RAM (if the kernel is in TT-RAM) and added to the
+ *    rsvd_stram_* region. The ST-RAM is somewhere in the middle of kernel
+ *    address space in the latter case.
+ * 
+ *  - If mem_init() already has been called and ST-RAM swapping is enabled,
+ *    try to get the memory from the (pseudo) swap-space, either free already
+ *    or by moving some other pages out of the swap.
+ *
+ *  - If mem_init() already has been called, and ST-RAM swapping is not
+ *    enabled, the only possibility is to try with __get_dma_pages(). This has
+ *    the disadvantage that it's very hard to get more than 1 page, and it is
+ *    likely to fail :-(
+ * 
+ */
+void *atari_stram_alloc( long size, unsigned long *start_mem,
+						 const char *owner )
+{
+	void *addr = NULL;
+	BLOCK *block;
+	int flags;
+
+	DPRINTK( "atari_stram_alloc(size=%08lx,*start_mem=%08lx,owner=%s)\n",
+			 size, start_mem ? *start_mem : 0xffffffff, owner );
+	
+	if (start_mem && mem_init_done) {
+		printk( KERN_ERR "atari_stram_alloc called with start_mem!=NULL "
+				"after mem_init() from %p\n", __builtin_return_address(0) );
+		return( NULL );
+	}
+	if (!start_mem && !mem_init_done) {
+		printk( KERN_ERR "atari_stram_alloc called with start_mem==NULL "
+				"before mem_init() from %p\n", __builtin_return_address(0) );
+		return( NULL );
+	}
+
+	size = ALIGN_IF_SWAP(size);
+	DPRINTK( "atari_stram_alloc: rounded size = %08lx\n", size );
+	if (!mem_init_done) {
+		/* before mem_init(): allocate "statically", i.e. either in the kernel
+		 * data space (current end in *start_mem), or at the end of currently
+		 * reserved ST-RAM. */
+		if (kernel_in_stram) {
+			/* Get memory from kernel data space */
+			*start_mem = ALIGN_IF_SWAP(*start_mem);
+			addr = (void *)*start_mem;
+			*start_mem += size;
+			DPRINTK( "atari_stram_alloc: pre-mem_init and k/ST: "
+					 "shifted start_mem to %08lx, addr=%p\n",
+					 *start_mem, addr );
+		}
+		else {
+			/* Get memory from rsvd_stram_beg */
+			if (rsvd_stram_end + size < stram_end) {
+				addr = (void *) rsvd_stram_end;
+				rsvd_stram_end += size;
+				DPRINTK( "atari_stram_alloc: pre-mem_init and k/TT: "
+						 "shifted rsvd_stram_end to %08lx, addr=%p\n",
+						 rsvd_stram_end, addr );
+			}
+		}
+		flags = BLOCK_STATIC;
+	}
+#ifdef CONFIG_STRAM_SWAP
+	else if (max_swap_size) {
+		/* If swapping is active (can only be the case after mem_init()!):
+		 * make some free space in the swap "device". */
+		DPRINTK( "atari_stram_alloc: after mem_init, swapping ok, "
+				 "calling get_region\n" );
+		addr = get_stram_region( N_PAGES(size) );
+		flags = BLOCK_INSWAP;
+	}
+#endif
+	else {
+		/* After mem_init() and no swapping: can only resort to
+		 * __get_dma_pages() */
+		addr = (void *)__get_dma_pages(GFP_KERNEL, get_gfp_order(size));
+		flags = BLOCK_GFP;
+		DPRINTK( "atari_stram_alloc: after mem_init, swapping off, "
+				 "get_pages=%p\n", addr );
+	}
+
+	if (addr) {
+		if (!(block = add_region( addr, size ))) {
+			/* out of memory for BLOCK structure :-( */
+			DPRINTK( "atari_stram_alloc: out of mem for BLOCK -- "
+					 "freeing again\n" );
+			if (flags == BLOCK_STATIC)
+				rsvd_stram_end -= size;
+			else if (flags == BLOCK_INSWAP)
+				free_stram_region( SWAP_NR(addr), N_PAGES(size) );
+			else
+				free_pages( (unsigned long)addr, get_gfp_order(size));
+			return( NULL );
+		}
+		block->owner = owner;
+		block->flags |= flags;
+	}
+	return( addr );
 }
 
+void atari_stram_free( void *addr )
 
-void *atari_stram_alloc( long size, unsigned long *start_mem )
+{
+	BLOCK *block;
+
+	DPRINTK( "atari_stram_free(addr=%p)\n", addr );
+
+	if (!(block = find_region( addr ))) {
+		printk( KERN_ERR "Attempt to free non-allocated ST-RAM block at %p "
+				"from %p\n", addr, __builtin_return_address(0) );
+		return;
+	}
+	DPRINTK( "atari_stram_free: found block (%p): size=%08lx, owner=%s, "
+			 "flags=%02x\n", block, block->size, block->owner, block->flags );
+	
+#ifdef CONFIG_STRAM_SWAP
+	if (!max_swap_size) {
+#endif
+		if (block->flags & BLOCK_GFP) {
+			DPRINTK( "atari_stram_free: is kmalloced, order_size=%d\n",
+					 get_gfp_order(block->size) );
+			free_pages( (unsigned long)addr, get_gfp_order(block->size) );
+		}
+		else
+			goto fail;
+#ifdef CONFIG_STRAM_SWAP
+	}
+	else if (block->flags & (BLOCK_INSWAP|BLOCK_STATIC)) {
+		DPRINTK( "atari_stram_free: is swap-alloced\n" );
+		free_stram_region( SWAP_NR(block->start), N_PAGES(block->size) );
+	}
+	else
+		goto fail;
+#endif
+	remove_region( block );
+	return;
 
+  fail:
+	printk( KERN_ERR "atari_stram_free: cannot free block at %p "
+			"(called from %p)\n", addr, __builtin_return_address(0) );
+}
+
+
+#ifdef CONFIG_STRAM_SWAP
+
+
+/* ------------------------------------------------------------------------ */
+/*						   Main Swapping Functions							*/
+/* ------------------------------------------------------------------------ */
+
+
+/*
+ * Initialize ST-RAM swap device
+ * (lots copied and modified from sys_swapon() in mm/swapfile.c)
+ */
+__initfunc(static int swap_init( unsigned long start_mem,
+								 unsigned long swap_data ))
 {
-	static int				kernel_in_stram = -1;
+	static struct dentry fake_dentry[3];
+	struct swap_info_struct *p;
+	struct inode swap_inode;
+	unsigned int type;
+	unsigned long addr;
+	int i, j, k, prev;
+
+	DPRINTK( "swap_init(start_mem=%08lx, swap_data=%08lx)\n",
+			 start_mem, swap_data );
 	
-	void	*adr = 0;
+	/* need at least one page for swapping to (and this also isn't very
+	 * much... :-) */
+	if (swap_end - swap_start < 2*PAGE_SIZE) {
+		printk( KERN_WARNING "stram_swap_init: swap space too small\n" );
+		return( 0 );
+	}
 	
-	if (kernel_in_stram < 0)
-		kernel_in_stram = (PTOV( 0 ) == 0);
+	/* find free slot in swap_info */
+	for( p = swap_info, type = 0; type < nr_swapfiles; type++, p++ )
+		if (!(p->flags & SWP_USED))
+			break;
+	if (type >= MAX_SWAPFILES) {
+		printk( KERN_WARNING "stram_swap_init: max. number of "
+				"swap devices exhausted\n" );
+		return( 0 );
+	}
+	if (type >= nr_swapfiles)
+		nr_swapfiles = type+1;
+
+	stram_swap_info = p;
+	stram_swap_type = type;
+
+	/* fake some dir cache entries to give us some name in /dev/swaps */
+	fake_dentry[0].d_covers = &fake_dentry[1];
+	fake_dentry[0].d_parent = &fake_dentry[0];
+	fake_dentry[1].d_parent = &fake_dentry[2];
+	fake_dentry[1].d_name.name = "stram (internal)";
+	fake_dentry[1].d_name.len = 16;
+	fake_dentry[2].d_covers = &fake_dentry[2];
+	fake_dentry[2].d_parent = &fake_dentry[2];
+	
+	p->flags        = SWP_USED;
+	p->swap_file    = &fake_dentry[0];
+	p->swap_device  = 0;
+	p->swap_lockmap = (unsigned char *)(swap_data);
+	p->swap_map	    = (unsigned char *)(swap_data + PAGE_SIZE);
+	p->cluster_nr   = 0;
+	p->next         = -1;
+	p->prio         = 0x7ff0;	/* a rather high priority, but not the higest
+								 * to give the user a chance to override */
+
+	/* call stram_open() directly, avoids at least the overhead in
+	 * constructing a dummy file structure... */
+	p->swap_device = MKDEV( STRAM_MAJOR, STRAM_MINOR );
+	swap_inode.i_rdev = p->swap_device;
+	stram_open( &swap_inode, MAGIC_FILE_P );
+	p->max = SWAP_NR(swap_end);
+
+	/* initialize lockmap */
+	memset( p->swap_lockmap, 0, PAGE_SIZE );
+
+	/* initialize swap_map: set regions that are already allocated or belong
+	 * to kernel data space to SWP_RSVD, otherwise to free */
+	j = 0; /* # of free pages */
+	k = 0; /* # of already allocated pages (from pre-mem_init stram_alloc()) */
+	p->lowest_bit = 0;
+	p->highest_bit = 0;
+	for( i = 1, addr = (unsigned long)SWAP_ADDR(1); i < p->max;
+		 i++, addr += PAGE_SIZE ) {
+		if (in_some_region( addr )) {
+			p->swap_map[i] = SWP_RSVD;
+			++k;
+		}
+		else if (kernel_in_stram && addr < start_mem ) {
+			p->swap_map[i] = SWP_RSVD;
+		}
+		else {
+			p->swap_map[i] = 0;
+			++j;
+			if (!p->lowest_bit) p->lowest_bit = i;
+			p->highest_bit = i;
+		}
+	}
+	/* first page always reserved (and doesn't really belong to swap space) */
+	p->swap_map[0] = SWP_RSVD;
+
+	/* now swapping to this device ok */
+	p->pages = j + k;
+	nr_swap_pages += j;
+	p->flags = SWP_WRITEOK;
+
+	/* insert swap space into swap_list */
+	prev = -1;
+	for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
+		if (p->prio >= swap_info[i].prio) {
+			break;
+		}
+		prev = i;
+	}
+	p->next = i;
+	if (prev < 0) {
+		swap_list.head = swap_list.next = p - swap_info;
+	} else {
+		swap_info[prev].next = p - swap_info;
+	}
+
+	printk( KERN_INFO "Using %dk (%d pages) of ST-RAM as swap space.\n",
+			p->pages << 2, p->pages );
+	return( 1 );
+}
+
+
+/*
+ * The swap entry has been read in advance, and we return 1 to indicate
+ * that the page has been used or is no longer needed.
+ */
+static inline int unswap_pte( struct vm_area_struct * vma, unsigned long
+							  address, pte_t *dir, unsigned long entry,
+							  unsigned long page, int isswap )
+{
+	pte_t pte = *dir;
+
+	if (pte_none(pte))
+		return 0;
+	if (pte_present(pte)) {
+		struct page *pg;
+		unsigned long page_nr = MAP_NR(pte_page(pte));
+		unsigned long pg_swap_entry;
+
+		if (page_nr >= max_mapnr)
+			return 0;
+		pg = mem_map + page_nr;
+		if (!(pg_swap_entry = in_swap_cache(pg)))
+			return 0;
+		if (pg_swap_entry != entry)
+			return 0;
+		if (isswap) {
+			DPRINTK( "unswap_pte: page %08lx = entry %08lx was in swap cache; "
+					 "exchanging to %08lx\n",
+					 page_address(pg), entry, page );
+			pg->pg_swap_entry = page;
+			swap_free(entry);
+			return 1;
+		}
+		else {
+			DPRINTK( "unswap_pte: page %08lx = entry %08lx was in swap cache; "
+					 "deleted there\n", page_address(pg), entry );
+			delete_from_swap_cache(pg);
+			set_pte(dir, pte_mkdirty(pte));
+			free_page(page);
+			return 1;
+		}
+	}
+	if (pte_val(pte) != entry)
+		return 0;
 
-	if (kernel_in_stram) {
-		/* Get memory from kernel data space */
-		adr = (void *) *start_mem;
-		*start_mem += size;
+	if (isswap) {
+		DPRINTK( "unswap_pte: replacing entry %08lx by %08lx", entry, page );
+		set_pte(dir, __pte(page));
 	}
 	else {
-		/* Get memory from rsvd_stram_beg */
-		if (rsvd_stram_end + size < stram_end) {
-			adr = (void *) rsvd_stram_end;
-			rsvd_stram_end += size;
+		DPRINTK( "unswap_pte: replacing entry %08lx by new page %08lx",
+				 entry, page );
+		set_pte(dir, pte_mkwrite(pte_mkdirty(mk_pte(page,vma->vm_page_prot))));
+		++vma->vm_mm->rss;
+	}
+	swap_free(entry);
+	return 1;
+}
+
+static inline int unswap_pmd( struct vm_area_struct * vma, pmd_t *dir,
+							  unsigned long address, unsigned long size,
+							  unsigned long offset, unsigned long entry,
+							  unsigned long page, int isswap )
+{
+	pte_t * pte;
+	unsigned long end;
+
+	if (pmd_none(*dir))
+		return 0;
+	if (pmd_bad(*dir)) {
+		printk("unswap_pmd: bad pmd (%08lx)\n", pmd_val(*dir));
+		pmd_clear(dir);
+		return 0;
+	}
+	pte = pte_offset(dir, address);
+	offset += address & PMD_MASK;
+	address &= ~PMD_MASK;
+	end = address + size;
+	if (end > PMD_SIZE)
+		end = PMD_SIZE;
+	do {
+		if (unswap_pte( vma, offset+address-vma->vm_start, pte, entry, 
+						page, isswap ))
+			return 1;
+		address += PAGE_SIZE;
+		pte++;
+	} while (address < end);
+	return 0;
+}
+
+static inline int unswap_pgd( struct vm_area_struct * vma, pgd_t *dir,
+							  unsigned long address, unsigned long size,
+							  unsigned long entry, unsigned long page,
+							  int isswap )
+{
+	pmd_t * pmd;
+	unsigned long offset, end;
+
+	if (pgd_none(*dir))
+		return 0;
+	if (pgd_bad(*dir)) {
+		printk("unswap_pgd: bad pgd (%08lx)\n", pgd_val(*dir));
+		pgd_clear(dir);
+		return 0;
+	}
+	pmd = pmd_offset(dir, address);
+	offset = address & PGDIR_MASK;
+	address &= ~PGDIR_MASK;
+	end = address + size;
+	if (end > PGDIR_SIZE)
+		end = PGDIR_SIZE;
+	do {
+		if (unswap_pmd( vma, pmd, address, end - address, offset, entry,
+						page, isswap ))
+			return 1;
+		address = (address + PMD_SIZE) & PMD_MASK;
+		pmd++;
+	} while (address < end);
+	return 0;
+}
+
+static int unswap_vma( struct vm_area_struct * vma, pgd_t *pgdir,
+					   unsigned long entry, unsigned long page, int isswap )
+{
+	unsigned long start = vma->vm_start, end = vma->vm_end;
+
+	while( start < end ) {
+		if (unswap_pgd( vma, pgdir, start, end - start, entry, page, isswap ))
+			return 1;
+		start = (start + PGDIR_SIZE) & PGDIR_MASK;
+		pgdir++;
+	}
+	return 0;
+}
+
+static int unswap_process( struct mm_struct * mm, unsigned long entry, 
+						   unsigned long page, int isswap )
+{
+	struct vm_area_struct* vma;
+
+	/*
+	 * Go through process' page directory.
+	 */
+	if (!mm || mm == &init_mm)
+		return 0;
+	for( vma = mm->mmap; vma; vma = vma->vm_next ) {
+		pgd_t * pgd = pgd_offset(mm, vma->vm_start);
+		if (unswap_vma( vma, pgd, entry, page, isswap ))
+			return 1;
+	}
+	return 0;
+}
+
+
+static int unswap_by_move( unsigned char *map, unsigned long max,
+						   unsigned long start, unsigned long n_pages )
+{
+	struct task_struct *p;
+	unsigned long entry, rover = (start == 1) ? n_pages+1 : 1;
+	unsigned long i, j;
+
+	DPRINTK( "unswapping %lu..%lu by moving in swap\n",
+			 start, start+n_pages-1 );
+	
+	/* can free the allocated pages by moving them to other swap pages */
+	for( i = start; i < start+n_pages; ++i ) {
+		if (!map[i]) {
+			map[i] = SWP_RSVD;
+			DPRINTK( "unswap: page %lu was free\n", i );
+			continue;
+		}
+		else if (map[i] == SWP_RSVD) {
+			printk( KERN_ERR "get_stram_region: page %lu already "
+					"reserved??\n", i );
+		}
+		DPRINTK( "unswap: page %lu is alloced, count=%u\n", i, map[i] );
+
+		/* find a free page not in our region */
+		for( j = rover; j != rover-1; j = (j == max-1) ? 1 : j+1 ) {
+			if (j >= start && j < start+n_pages)
+				continue;
+			if (!map[j]) {
+				rover = j+1;
+				break;
+			}
+		}
+		if (j == rover-1) {
+			printk( KERN_ERR "get_stram_region: not enough free swap "
+					"pages now??\n" );
+			return( -ENOMEM );
+		}
+		DPRINTK( "unswap: map[i=%lu]=%u map[j=%lu]=%u nr_swap=%u\n",
+				 i, map[i], j, map[j], nr_swap_pages );
+		
+		--nr_swap_pages;
+		entry = SWP_ENTRY( stram_swap_type, j );
+		if (stram_swap_info->lowest_bit == j)
+			stram_swap_info->lowest_bit++;
+		if (stram_swap_info->highest_bit == j)
+			stram_swap_info->highest_bit--;
+		
+		memcpy( SWAP_ADDR(j), SWAP_ADDR(i), PAGE_SIZE );
+#ifdef DO_PROC
+		stat_swap_move++;
+#endif
+
+		while( map[i] ) {
+			for_each_task(p) {
+				if (unswap_process( p->mm, SWP_ENTRY( stram_swap_type, i ),
+									entry, 1 )) {
+					map[j]++;
+					goto repeat;
+				}
+			}
+			if (map[i] && map[i] != 127) {
+				printk( KERN_ERR "get_stram_region: ST-RAM swap page %lu "
+						"not used by any process\n", i );
+				/* quit while loop and overwrite bad map entry */
+				break;
+			}
+			else if (!map[i]) {
+				/* somebody else must have swapped in that page, so free the
+				 * new one (we're moving to) */
+				DPRINTK( "unswap: map[i] became 0, also clearing map[j]\n" );
+				map[j] = 0;
+			}
+		  repeat:
+		}
+
+		DPRINTK( "unswap: map[i=%lu]=%u map[j=%lu]=%u nr_swap=%u\n",
+				 i, map[i], j, map[j], nr_swap_pages );
+		map[i] = SWP_RSVD;
+		if (stram_swap_info->lowest_bit == i)
+			stram_swap_info->lowest_bit++;
+		if (stram_swap_info->highest_bit == i)
+			stram_swap_info->highest_bit--;
+		--nr_swap_pages;
+	}
+	return( 0 );
+}
+
+static int unswap_by_read( unsigned char *map, unsigned long max,
+						   unsigned long start, unsigned long n_pages )
+{
+	struct task_struct *p;
+	unsigned long entry, page = 0;
+	unsigned long i;
+
+	DPRINTK( "unswapping %lu..%lu by reading in\n",
+			 start, start+n_pages-1 );
+
+	for( i = start; i < start+n_pages; ++i ) {
+		if (map[i] == SWP_RSVD) {
+			printk( KERN_ERR "get_stram_region: page %lu already "
+					"reserved??\n", i );
+			continue;
+		}
+		entry = SWP_ENTRY( stram_swap_type, i );
+		DPRINTK( "unswap: map[i=%lu]=%u nr_swap=%u\n",
+				 i, map[i], nr_swap_pages );
+
+		while( map[i] ) {
+			if (!page && !(page = __get_free_page(GFP_KERNEL))) {
+				printk( KERN_NOTICE "get_stram_region: out of memory\n" );
+				return( -ENOMEM );
+			}
+			DPRINTK( "unswap: reading swap page %lu to %08lx\n", i, page );
+			read_swap_page( entry, (char *)page );
+
+			for_each_task(p) {
+				if (unswap_process( p->mm, entry, page, 0 )) {
+					page = 0;
+#ifdef DO_PROC
+					stat_swap_force++;
+#endif
+					break;
+				}
+			}
+			if (page) {
+				/*
+				 * If we couldn't find an entry, there are several
+				 * possible reasons: someone else freed it first,
+				 * we freed the last reference to an overflowed entry,
+				 * or the system has lost track of the use counts.
+				 */
+				if (map[i] && map[i] != SWP_RSVD-1)
+					printk( KERN_ERR "get_stram_region: swap entry %08lx "
+							"not used by any process\n", entry );
+				/* quit while loop and overwrite bad map entry */
+				if (!map[i]) {
+					DPRINTK( "unswap: map[i] became 0\n" );
+				}
+				break;
+			}
+		}
+
+		DPRINTK( "unswap: map[i=%lu]=%u nr_swap=%u\n",
+				 i, map[i], nr_swap_pages );
+		map[i] = SWP_RSVD;
+		if (stram_swap_info->lowest_bit == i)
+			stram_swap_info->lowest_bit++;
+		if (stram_swap_info->highest_bit == i)
+			stram_swap_info->highest_bit--;
+		--nr_swap_pages;
+	}
+
+	if (page)
+		free_page(page);
+	return( 0 );
+}
+
+/*
+ * reserve a region in ST-RAM swap space for an allocation
+ */
+static void *get_stram_region( unsigned long n_pages )
+{
+	unsigned char *map = stram_swap_info->swap_map;
+	unsigned long max = stram_swap_info->max;
+	unsigned long start, total_free, region_free;
+	int err;
+	void *ret = NULL;
+	
+	DPRINTK( "get_stram_region(n_pages=%lu)\n", n_pages );
+	
+	/* disallow writing to the swap device now */
+	stram_swap_info->flags = SWP_USED;
+
+	/* find a region of n_pages pages in the swap space including as much free
+	 * pages as possible (and excluding any already-reserved pages). */
+	if (!(start = find_free_region( n_pages, &total_free, &region_free )))
+		goto end;
+	DPRINTK( "get_stram_region: region starts at %lu, has %lu free pages\n",
+			 start, region_free );
+
+	err = ((total_free-region_free >= n_pages-region_free) ?
+		   unswap_by_move( map, max, start, n_pages ) :
+		   unswap_by_read( map, max, start, n_pages ));
+	if (err)
+		goto end;
+
+	ret = SWAP_ADDR(start);
+  end:
+	/* allow using swap device again */
+	stram_swap_info->flags = SWP_WRITEOK;
+	DPRINTK( "get_stram_region: returning %p\n", ret );
+	return( ret );
+}
+
+
+/*
+ * free a reserved region in ST-RAM swap space
+ */
+static void free_stram_region( unsigned long offset, unsigned long n_pages )
+{
+	unsigned char *map = stram_swap_info->swap_map;
+
+	DPRINTK( "free_stram_region(offset=%lu,n_pages=%lu)\n", offset, n_pages );
+
+	if (offset < 1 || offset + n_pages > stram_swap_info->max) {
+		printk( KERN_ERR "free_stram_region: Trying to free non-ST-RAM\n" );
+		return;
+	}
+
+	/* un-reserve the freed pages */
+	for( ; n_pages > 0; ++offset, --n_pages ) {
+		if (map[offset] != SWP_RSVD)
+			printk( KERN_ERR "free_stram_region: Swap page %lu was not "
+					"reserved\n", offset );
+		map[offset] = 0;
+	}
+
+	/* update swapping meta-data */
+	if (offset < stram_swap_info->lowest_bit)
+		stram_swap_info->lowest_bit = offset;
+	if (offset+n_pages-1 > stram_swap_info->highest_bit)
+		stram_swap_info->highest_bit = offset+n_pages-1;
+	if (stram_swap_info->prio > swap_info[swap_list.next].prio)
+		swap_list.next = swap_list.head;
+	nr_swap_pages += n_pages;
+}
+
+
+/* ------------------------------------------------------------------------ */
+/*						Utility Functions for Swapping						*/
+/* ------------------------------------------------------------------------ */
+
+
+/* is addr in some of the allocated regions? */
+static int in_some_region( unsigned long addr )
+{
+	BLOCK *p;
+	
+	for( p = alloc_list; p; p = p->next ) {
+		if (p->start <= addr && addr < p->start + p->size)
+			return( 1 );
+	}
+	return( 0 );
+}
+
+
+static unsigned long find_free_region( unsigned long n_pages,
+									   unsigned long *total_free,
+									   unsigned long *region_free )
+{
+	unsigned char *map = stram_swap_info->swap_map;
+	unsigned long max = stram_swap_info->max;
+	unsigned long head, tail, max_start;
+	long nfree, max_free;
+
+	/* first scan the swap space for a suitable place for the allocation */
+	head = 1;
+	max_start = 0;
+	max_free = -1;
+	*total_free = 0;
+
+  start_over:
+	/* increment tail until final window size reached, and count free pages */
+	nfree = 0;
+	for( tail = head; tail-head < n_pages && tail < max-n_pages; ++tail ) {
+		if (map[tail] == SWP_RSVD) {
+			head = tail+1;
+			goto start_over;
+		}
+		if (!map[tail]) {
+			++nfree;
+			++*total_free;
+		}
+	}
+	if (tail-head < n_pages)
+		goto out;
+	if (nfree > max_free) {
+		max_start = head;
+		max_free  = nfree;
+		if (max_free >= n_pages)
+			/* don't need more free pages... :-) */
+			goto out;
+	}
+	
+	/* now shift the window and look for the area where as much pages as
+	 * possible are free */
+	while( tail < max ) {
+		nfree -= (map[head++] == 0);
+		if (map[tail] == SWP_RSVD) {
+			head = tail+1;
+			goto start_over;
+		}
+		if (!map[tail]) {
+			++nfree;
+			++*total_free;
+		}
+		++tail;
+		if (nfree > max_free) {
+			max_start = head;
+			max_free  = nfree;
+			if (max_free >= n_pages)
+				/* don't need more free pages... :-) */
+				goto out;
+		}
+	}
+
+  out:
+	if (max_free < 0) {
+		printk( KERN_NOTICE "get_stram_region: ST-RAM too full or fragmented "
+				"-- can't allocate %lu pages\n", n_pages );
+		return( 0 );
+	}
+
+	*region_free = max_free;
+	return( max_start );
+}
+
+
+/* setup parameters from command line */
+void stram_swap_setup( char *str, int *ints )
+{
+	if (ints[0] >= 1)
+		max_swap_size = ((ints[1] < 0 ? 0 : ints[1]) * 1024) & PAGE_MASK;
+}
+
+
+/* ------------------------------------------------------------------------ */
+/*								ST-RAM device								*/
+/* ------------------------------------------------------------------------ */
+
+static int stram_blocksizes[14] = {
+	0, 0, 0, 0, 0, 0, 0, 0,	0, 0, 0, 0, 0, 4096 };
+static int stram_sizes[14] = {
+	0, 0, 0, 0, 0, 0, 0, 0,	0, 0, 0, 0, 0, 0 };
+static int refcnt = 0;
+
+static void do_stram_request( void )
+{
+	unsigned long start, len;
+
+	while( CURRENT ) {
+		if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
+			panic("stram: request list destroyed");
+		if (CURRENT->bh) {
+			if (!buffer_locked(CURRENT->bh))
+				panic("stram: block not locked");
+		}
+		
+		start = swap_start + (CURRENT->sector << 9);
+		len   = CURRENT->current_nr_sectors << 9;
+		if ((start + len) > swap_end) {
+			printk( KERN_ERR "stram: bad access beyond end of device: "
+					"block=%ld, count=%ld\n",
+					CURRENT->sector,
+					CURRENT->current_nr_sectors );
+			end_request( 0 );
+			continue;
+		}
+
+		if (CURRENT->cmd == READ) {
+			memcpy( CURRENT->buffer, (char *)start, len );
+#ifdef DO_PROC
+			stat_swap_read += N_PAGES(len);
+#endif
 		}
+		else {
+			memcpy( (char *)start, CURRENT->buffer, len );
+#ifdef DO_PROC
+			stat_swap_write += N_PAGES(len);
+#endif
+		}
+		end_request( 1 );
+	}
+}
+
+
+static int stram_open( struct inode *inode, struct file *filp )
+{
+	if (filp != MAGIC_FILE_P) {
+		printk( KERN_NOTICE "Only kernel can open ST-RAM device\n" );
+		return( -EPERM );
+	}
+	if (MINOR(inode->i_rdev) != STRAM_MINOR)
+		return( -ENXIO );
+	if (refcnt)
+		return( -EBUSY );
+	++refcnt;
+	return( 0 );
+}
+
+static int stram_release( struct inode *inode, struct file *filp )
+{
+	if (filp != MAGIC_FILE_P) {
+		printk( KERN_NOTICE "Only kernel can close ST-RAM device\n" );
+		return( -EPERM );
 	}
+	if (refcnt > 0)
+		--refcnt;
+	return( 0 );
+}
+
+
+static struct file_operations stram_fops = {
+        NULL,                   /* lseek - default */
+        block_read,             /* read - general block-dev read */
+        block_write,            /* write - general block-dev write */
+        NULL,                   /* readdir - bad */
+        NULL,                   /* select */
+        NULL,                   /* ioctl */
+        NULL,                   /* mmap */
+        stram_open,             /* open */
+        stram_release,          /* release */
+        block_fsync             /* fsync */
+};
+
+int stram_device_init( void )
+{
+	if (!max_swap_size)
+		/* swapping not enabled */
+		return( -ENXIO );
 	
-	return( adr );
+    if (register_blkdev( STRAM_MAJOR, "stram", &stram_fops)) {
+        printk( KERN_ERR "stram: Unable to get major %d\n", STRAM_MAJOR );
+        return( -ENXIO );
+    }
+
+	blk_dev[STRAM_MAJOR].request_fn = do_stram_request;
+    blksize_size[STRAM_MAJOR] = stram_blocksizes;
+	stram_sizes[STRAM_MINOR] = (swap_end - swap_start)/1024;
+    blk_size[STRAM_MAJOR] = stram_sizes;
+	do_z2_request(); /* to avoid warning */
+	return( 0 );
 }
 
-void atari_stram_free( void *ptr )
+/* to avoid warning */
+static void do_z2_request( void ) { }
 
+
+
+/* ------------------------------------------------------------------------ */
+/*							Misc Utility Functions							*/
+/* ------------------------------------------------------------------------ */
+
+
+/* return log2 of #pages for size */
+static int get_gfp_order( unsigned long size )
 {
-	/* Sorry, this is a dummy. It isn't needed anyway. */
+	int order;
+
+	size = N_PAGES( size + PAGE_SIZE -1 );
+	order = -1;
+	do {
+		size >>= 1;
+		order++;
+	} while (size);
+
+	return( order );
 }
 
+#endif /* CONFIG_STRAM_SWAP */
+
+
+/* reserve a range of pages in mem_map[] */
+static void reserve_region( unsigned long addr, unsigned long end )
+{
+	mem_map_t *mapp = &mem_map[MAP_NR(addr)];
+
+	for( ; addr < end; addr += PAGE_SIZE, ++mapp )
+		set_bit( PG_reserved, &mapp->flags );
+}
+
+
+
+/* ------------------------------------------------------------------------ */
+/*							  Region Management								*/
+/* ------------------------------------------------------------------------ */
+
+
+/* insert a region into the alloced list (sorted) */
+static BLOCK *add_region( void *addr, unsigned long size )
+{
+	BLOCK **p, *n = NULL;
+	int i;
+
+	for( i = 0; i < N_STATIC_BLOCKS; ++i ) {
+		if (static_blocks[i].flags & BLOCK_FREE) {
+			n = &static_blocks[i];
+			n->flags = 0;
+			break;
+		}
+	}
+	if (!n && mem_init_done) {
+		/* if statics block pool exhausted and we can call kmalloc() already
+		 * (after mem_init()), try that */
+		n = kmalloc( sizeof(BLOCK), GFP_KERNEL );
+		if (n)
+			n->flags = BLOCK_KMALLOCED;
+	}
+	if (!n) {
+		printk( KERN_ERR "Out of memory for ST-RAM descriptor blocks\n" );
+		return( NULL );
+	}
+	n->start = (unsigned long)addr;
+	n->size  = size;
+
+	for( p = &alloc_list; *p; p = &((*p)->next) )
+		if ((*p)->start > (unsigned long)addr) break;
+	n->next = *p;
+	*p = n;
+
+	return( n );
+}
+
+
+/* find a region (by start addr) in the alloced list */
+static BLOCK *find_region( void *addr )
+{
+	BLOCK *p;
+	
+	for( p = alloc_list; p; p = p->next ) {
+		if (p->start == (unsigned long)addr)
+			return( p );
+		if (p->start > (unsigned long)addr)
+			break;
+	}
+	return( NULL );
+}
+
+
+/* remove a block from the alloced list */
+static int remove_region( BLOCK *block )
+{
+	BLOCK **p;
+	
+	for( p = &alloc_list; *p; p = &((*p)->next) )
+		if (*p == block) break;
+	if (!*p)
+		return( 0 );
+
+	*p = block->next;
+	if (block->flags & BLOCK_KMALLOCED)
+		kfree( block );
+	else
+		block->flags |= BLOCK_FREE;
+	return( 1 );
+}
+
+
+
+/* ------------------------------------------------------------------------ */
+/*						 /proc statistics file stuff						*/
+/* ------------------------------------------------------------------------ */
+
+#ifdef DO_PROC
+
+#define	PRINT_PROC(fmt,args...) len += sprintf( buf+len, fmt, ##args )
+
+int get_stram_list( char *buf )
+{
+    int i, len = 0;
+	unsigned char *map = stram_swap_info->swap_map;
+	unsigned long max = stram_swap_info->max;
+	unsigned free = 0, used = 0, rsvd = 0;
+	BLOCK *p;
+
+#ifdef CONFIG_STRAM_SWAP
+	if (max_swap_size) {
+		for( i = 1; i < max; ++i ) {
+			if (!map[i])
+				++free;
+			else if (map[i] == SWP_RSVD)
+				++rsvd;
+			else
+				++used;
+		}
+		PRINT_PROC(
+			"Total ST-RAM:      %8lu kB\n"
+			"Total ST-RAM swap: %8lu kB\n"
+			"Free swap:         %8u kB\n"
+			"Used swap:         %8u kB\n"
+			"Allocated swap:    %8u kB\n"
+			"Swap Reads:        %8u\n"
+			"Swap Writes:       %8u\n"
+			"Swap Moves:        %8u\n"
+			"Swap Forced Reads: %8u\n",
+			(stram_end - stram_start) >> 10,
+			(max-1) << (PAGE_SHIFT-10),
+			free << (PAGE_SHIFT-10),
+			used << (PAGE_SHIFT-10),
+			rsvd << (PAGE_SHIFT-10),
+			stat_swap_read,
+			stat_swap_write,
+			stat_swap_move,
+			stat_swap_force );
+	}
+	else {
+#endif
+		PRINT_PROC( "ST-RAM swapping disabled\n" );
+		PRINT_PROC(
+			"Total ST-RAM:      %8lu kB\n"
+			"Reserved ST-RAM:   %8lu kB\n",
+			(stram_end - stram_start) >> 10,
+			(rsvd_stram_end - rsvd_stram_beg) >> 10 );
+#ifdef CONFIG_STRAM_SWAP
+	}
 #endif
 
+	PRINT_PROC( "Allocated regions:\n" );
+	for( p = alloc_list; p; p = p->next ) {
+		if (len + 50 >= PAGE_SIZE)
+			break;
+		PRINT_PROC( "0x%08lx-0x%08lx: %s (",
+					VTOP(p->start), VTOP(p->start+p->size-1), p->owner );
+		if (p->flags & BLOCK_STATIC)
+			PRINT_PROC( "static)\n" );
+		else if (p->flags & BLOCK_GFP)
+			PRINT_PROC( "page-alloced)\n" );
+		else if (p->flags & BLOCK_INSWAP)
+			PRINT_PROC( "in swap)\n" );
+		else
+			PRINT_PROC( "??)\n" );
+	}
+
+	return( len );
+}
 
+#endif
+
+
+/*
+ * Local variables:
+ *  c-indent-level: 4
+ *  tab-width: 4
+ * End:
+ */
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/arch/m68k/config.in linux-2.1.64/arch/m68k/config.in
--- linux-2.1.64.orig/arch/m68k/config.in	Mon Nov 17 12:47:37 1997
+++ linux-2.1.64/arch/m68k/config.in	Wed Nov 19 22:02:42 1997
@@ -63,6 +63,10 @@
 #    bool 'A2410 support' CONFIG_GSP_A2410
 #  fi
 fi
+if [ "$CONFIG_ATARI" = "y" ]; then
+  bool 'Support for ST-RAM as swap space' CONFIG_STRAM_SWAP
+  bool 'ST-RAM statistics in /proc' CONFIG_STRAM_PROC
+fi
 endmenu
 
 #
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/arch/m68k/kernel/kgdb.c linux-2.1.64/arch/m68k/kernel/kgdb.c
--- linux-2.1.64.orig/arch/m68k/kernel/kgdb.c	Mon Nov 17 12:47:37 1997
+++ linux-2.1.64/arch/m68k/kernel/kgdb.c	Sat Nov 22 01:05:22 1997
@@ -215,7 +215,7 @@
 
 static int hex( unsigned char ch);
 static void getpacket( char *buffer);
-static void putpacket( char *buffer);
+static void putpacket( char *buffer, int expect_ack);
 static inline unsigned long *get_vbr( void );
 static int protected_read( char *p, unsigned long *vbr );
 static int protected_write( char *p, char val, unsigned long *vbr );
@@ -358,7 +358,7 @@
 /*
  * send the packet in buffer.
  */
-static void putpacket(char *buffer)
+static void putpacket(char *buffer, int expect_ack)
 {
 	unsigned char checksum;
 	int count;
@@ -388,7 +388,7 @@
 		putDebugChar(hexchars[checksum & 0xf]);
 
 	}
-	while ((getDebugChar() & 0x7f) != '+');
+	while (expect_ack && (getDebugChar() & 0x7f) != '+');
 }
 
 
@@ -922,7 +922,7 @@
 	*ptr++ = ';';
 
 	*ptr++ = 0;
-	putpacket(output_buffer);	/* send it off... */
+	putpacket(output_buffer,1);	/* send it off... */
 
 	/*
 	 * Wait for input from remote GDB
@@ -1065,8 +1065,8 @@
 			if (mach_reset) {
 				/* reply OK before actual reset */
 				strcpy(output_buffer,"OK");
-				putpacket(output_buffer);
-				(*mach_reset)();
+				putpacket(output_buffer,0);
+				mach_reset();
 			}
 			else
 				strcpy(output_buffer,"E01");
@@ -1086,7 +1086,7 @@
 		 * reply to the request
 		 */
 
-		putpacket(output_buffer);
+		putpacket(output_buffer,1);
 
 	}
 }
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/arch/m68k/mm/init.c linux-2.1.64/arch/m68k/mm/init.c
--- linux-2.1.64.orig/arch/m68k/mm/init.c	Mon Nov 17 21:37:30 1997
+++ linux-2.1.64/arch/m68k/mm/init.c	Sat Nov 22 01:05:54 1997
@@ -23,6 +23,9 @@
 #include <asm/pgtable.h>
 #include <asm/system.h>
 #include <asm/machdep.h>
+#ifdef CONFIG_ATARI
+#include <asm/atari_stram.h>
+#endif
 
 extern void die_if_kernel(char *,struct pt_regs *,long);
 extern void init_kpointer_table(void);
@@ -422,42 +425,15 @@
 	high_memory = (void *) end_mem;
 	max_mapnr = num_physpages = MAP_NR(end_mem);
 
-	start_mem = PAGE_ALIGN(start_mem);
-	while (start_mem < end_mem) {
-		clear_bit(PG_reserved, &mem_map[MAP_NR(start_mem)].flags);
-		start_mem += PAGE_SIZE;
+	tmp = start_mem = PAGE_ALIGN(start_mem);
+	while (tmp < end_mem) {
+		clear_bit(PG_reserved, &mem_map[MAP_NR(tmp)].flags);
+		tmp += PAGE_SIZE;
 	}
 
 #ifdef CONFIG_ATARI
-
-	if (MACH_IS_ATARI) {
-
-		/* If the page with physical address 0 isn't the first kernel
-		 * code page, it has to be reserved because the first 2 KB of
-		 * ST-Ram can only be accessed from supervisor mode by
-		 * hardware.
-		 */
-
-		unsigned long virt0 = PTOV( 0 ), adr;
-		extern unsigned long rsvd_stram_beg, rsvd_stram_end;
-		
-		if (virt0 != 0) {
-
-			set_bit(PG_reserved, &mem_map[MAP_NR(virt0)].flags);
-
-			/* Also, reserve all pages that have been marked by
-			 * stram_alloc() (e.g. for the screen memory). (This may
-			 * treat the first ST-Ram page a second time, but that
-			 * doesn't hurt...) */
-			
-			rsvd_stram_end += PAGE_SIZE - 1;
-			rsvd_stram_end &= PAGE_MASK;
-			rsvd_stram_beg &= PAGE_MASK;
-			for( adr = rsvd_stram_beg; adr < rsvd_stram_end; adr += PAGE_SIZE )
-				set_bit(PG_reserved, &mem_map[MAP_NR(adr)].flags);
-		}
-	}
-	
+	if (MACH_IS_ATARI)
+		atari_stram_reserve_pages( start_mem );
 #endif
 
 	for (tmp = 0 ; tmp < end_mem ; tmp += PAGE_SIZE) {
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/drivers/block/acsi.c linux-2.1.64/drivers/block/acsi.c
--- linux-2.1.64.orig/drivers/block/acsi.c	Mon Nov 17 12:47:38 1997
+++ linux-2.1.64/drivers/block/acsi.c	Wed Nov 19 22:28:46 1997
@@ -73,6 +73,7 @@
 #include <asm/atariints.h>
 #include <asm/atari_acsi.h>
 #include <asm/atari_stdma.h>
+#include <asm/atari_stram.h>
 
 
 #define DEBUG
@@ -1804,8 +1805,8 @@
 		return -EBUSY;
 	}
 
-	if (!(acsi_buffer = (char *)__get_free_pages(GFP_KERNEL,
-						     ACSI_BUFFER_ORDER, 1))) {
+	if (!(acsi_buffer =
+		  (char *)atari_stram_alloc( ACSI_BUFFER_SIZE, NULL, "acsi" ))) {
 		printk( KERN_ERR "Unable to get ACSI ST-Ram buffer.\n" );
 		unregister_blkdev( MAJOR_NR, "ad" );
 		return -ENOMEM;
@@ -1842,7 +1843,7 @@
 {
 	del_timer( &acsi_timer );
 	blk_dev[MAJOR_NR].request_fn = 0;
-	free_pages( (unsigned long)acsi_buffer, ACSI_BUFFER_ORDER );
+	atari_stram_free( acsi_buffer );
 
 	if (unregister_blkdev( MAJOR_NR, "ad" ) != 0)
 		printk( KERN_ERR "acsi: cleanup_module failed\n");
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/drivers/block/acsi_slm.c linux-2.1.64/drivers/block/acsi_slm.c
--- linux-2.1.64.orig/drivers/block/acsi_slm.c	Mon Nov 17 12:47:38 1997
+++ linux-2.1.64/drivers/block/acsi_slm.c	Wed Nov 19 22:28:54 1997
@@ -73,6 +73,7 @@
 #include <asm/atariints.h>
 #include <asm/atari_acsi.h>
 #include <asm/atari_stdma.h>
+#include <asm/atari_stram.h>
 #include <asm/atari_SLM.h>
 
 
@@ -1009,7 +1010,7 @@
 		return -EBUSY;
 	}
 	
-	if (!(SLMBuffer = kmalloc( SLM_BUFFER_SIZE,  GFP_KERNEL | GFP_DMA))) {
+	if (!(SLMBuffer = atari_stram_alloc( SLM_BUFFER_SIZE, NULL, "SLM" ))) {
 		printk( KERN_ERR "Unable to get SLM ST-Ram buffer.\n" );
 		unregister_chrdev( MAJOR_NR, "slm" );
 		return -ENOMEM;
@@ -1041,5 +1042,6 @@
 {
 	if (unregister_chrdev( MAJOR_NR, "slm" ) != 0)
 		printk( KERN_ERR "acsi_slm: cleanup_module failed\n");
+	atari_stram_free( SLMBuffer );
 }
 #endif
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/drivers/block/ataflop.c linux-2.1.64/drivers/block/ataflop.c
--- linux-2.1.64.orig/drivers/block/ataflop.c	Mon Nov 17 12:47:38 1997
+++ linux-2.1.64/drivers/block/ataflop.c	Sat Nov 22 00:01:33 1997
@@ -89,6 +89,7 @@
 #include <asm/atarihw.h>
 #include <asm/atariints.h>
 #include <asm/atari_stdma.h>
+#include <asm/atari_stram.h>
 
 #define MAJOR_NR FLOPPY_MAJOR
 #include <linux/blk.h>
@@ -2047,7 +2048,7 @@
 	timer_table[FLOPPY_TIMER].fn = check_change;
 	timer_active &= ~(1 << FLOPPY_TIMER);
 
-	DMABuffer = kmalloc(BUFFER_SIZE + 512, GFP_KERNEL | GFP_DMA);
+	DMABuffer = atari_stram_alloc( BUFFER_SIZE+512, NULL, "ataflop" );
 	if (!DMABuffer) {
 		printk(KERN_ERR "atari_floppy_init: cannot get dma buffer\n");
 		unregister_blkdev(MAJOR_NR, "fd");
@@ -2079,6 +2080,7 @@
 	       UseTrackbuffer ? "" : "no ");
 	config_types();
 
+	(void)do_floppy; /* avoid warning about unused variable */
 	return 0;
 }
 
@@ -2126,7 +2128,7 @@
 	blk_dev[MAJOR_NR].request_fn = 0;
 	timer_active &= ~(1 << FLOPPY_TIMER);
 	timer_table[FLOPPY_TIMER].fn = 0;
-	kfree (DMABuffer);
+	atari_stram_free( DMABuffer );
 }
 #endif
 
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/drivers/block/ll_rw_blk.c linux-2.1.64/drivers/block/ll_rw_blk.c
--- linux-2.1.64.orig/drivers/block/ll_rw_blk.c	Thu Nov 13 15:20:22 1997
+++ linux-2.1.64/drivers/block/ll_rw_blk.c	Thu Nov 20 18:50:37 1997
@@ -661,6 +661,9 @@
 #ifdef CONFIG_BPCD
 extern void bpcd_init( void );
 #endif
+#ifdef CONFIG_STRAM_SWAP
+extern int stram_device_init( void );
+#endif
 
 __initfunc(int blk_dev_init(void))
 {
@@ -687,6 +690,9 @@
 	memset(max_readahead, 0, sizeof(max_readahead));
 #ifdef CONFIG_AMIGA_Z2RAM
 	z2_init();
+#endif
+#ifdef CONFIG_STRAM_SWAP
+	stram_device_init();
 #endif
 #ifdef CONFIG_BLK_DEV_RAM
 	rd_init();
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/drivers/char/m68kserial.c linux-2.1.64/drivers/char/m68kserial.c
--- linux-2.1.64.orig/drivers/char/m68kserial.c	Sat Nov 22 00:52:39 1997
+++ linux-2.1.64/drivers/char/m68kserial.c	Wed Nov 19 23:10:56 1997
@@ -1756,12 +1756,21 @@
 	}
 }
 
+static void dummy_console_write( const char *str, unsigned int count )
+{
+}
+
+static int dummy_wait_key(void)
+{
+	return( '\r' );
+}
+
 static struct console sercons = {
 	"ttyS",
-	NULL,			/* filled in by serial_console_setup */
+	dummy_console_write,	/* filled in by serial_console_setup */
 	NULL,
 	serial_console_device,
-	NULL,			/* filled in by serial_console_setup */
+	dummy_wait_key,			/* filled in by serial_console_setup */
 	NULL,
 	serial_console_setup,
 	CON_PRINTBUFFER,
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/drivers/scsi/atari_scsi.c linux-2.1.64/drivers/scsi/atari_scsi.c
--- linux-2.1.64.orig/drivers/scsi/atari_scsi.c	Mon Nov 17 12:47:39 1997
+++ linux-2.1.64/drivers/scsi/atari_scsi.c	Wed Nov 19 22:28:28 1997
@@ -107,6 +107,7 @@
 #include "NCR5380.h"
 #include "constants.h"
 #include <asm/atari_stdma.h>
+#include <asm/atari_stram.h>
 #include <asm/io.h>
 
 #include <linux/stat.h>
@@ -652,8 +653,12 @@
 	 */
 	if (MACH_IS_ATARI && ATARIHW_PRESENT(ST_SCSI) &&
 	    !ATARIHW_PRESENT(EXTD_DMA) && m68k_num_memory > 1) {
-		atari_dma_buffer = scsi_init_malloc(STRAM_BUFFER_SIZE,
-						    GFP_ATOMIC | GFP_DMA);
+		atari_dma_buffer = atari_stram_alloc( STRAM_BUFFER_SIZE, NULL, "SCSI" );
+		if (!atari_dma_buffer) {
+			printk( KERN_ERR "atari_scsi_detect: can't allocate ST-RAM "
+					"double buffer\n" );
+			return( 0 );
+		}
 		atari_dma_phys_buffer = VTOP( atari_dma_buffer );
 		atari_dma_orig_addr = 0;
 	}
@@ -739,7 +744,7 @@
 	if (IS_A_TT())
 		free_irq(IRQ_TT_MFP_SCSI, scsi_tt_intr);
 	if (atari_dma_buffer)
-		scsi_init_free (atari_dma_buffer, STRAM_BUFFER_SIZE);
+		atari_stram_free (atari_dma_buffer);
 	return 1;
 }
 #endif
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/drivers/sound/dmasound.c linux-2.1.64/drivers/sound/dmasound.c
--- linux-2.1.64.orig/drivers/sound/dmasound.c	Mon Nov 17 12:47:39 1997
+++ linux-2.1.64/drivers/sound/dmasound.c	Wed Nov 19 22:33:41 1997
@@ -94,6 +94,7 @@
 #ifdef CONFIG_ATARI
 #include <asm/atarihw.h>
 #include <asm/atariints.h>
+#include <asm/atari_stram.h>
 #endif /* CONFIG_ATARI */
 #ifdef CONFIG_AMIGA
 #include <asm/amigahw.h>
@@ -1580,28 +1581,12 @@
 
 static void *AtaAlloc(unsigned int size, int flags)
 {
-    int order;
-    unsigned int a_size;
-    order = 0;
-    a_size = PAGE_SIZE;
-    while (a_size < size) {
-	order++;
-	a_size <<= 1;
-    }
-    return (void *) __get_dma_pages(flags, order);
+    return( atari_stram_alloc( size, NULL, "dmasound" ));
 }
 
 static void AtaFree(void *obj, unsigned int size)
 {
-    int order;
-    unsigned int a_size;
-    order = 0;
-    a_size = PAGE_SIZE;
-    while (a_size < size) {
-	order++;
-	a_size <<= 1;
-    }
-    free_pages ((unsigned long) obj, order);
+    atari_stram_free( obj );
 }
 
 static int AtaIrqInit(void)
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/drivers/video/atafb.c linux-2.1.64/drivers/video/atafb.c
--- linux-2.1.64.orig/drivers/video/atafb.c	Mon Nov 10 15:10:16 1997
+++ linux-2.1.64/drivers/video/atafb.c	Wed Nov 19 22:34:49 1997
@@ -65,6 +65,7 @@
 
 #include <asm/atarihw.h>
 #include <asm/atariints.h>
+#include <asm/atari_stram.h>
 
 #include <linux/fb.h>
 #include <asm/atarikb.h>
@@ -2250,9 +2251,7 @@
 static int ext_getcolreg( unsigned regno, unsigned *red,
 						  unsigned *green, unsigned *blue,
 						  unsigned *transp )
-
-{	unsigned char colmask = (1 << external_bitspercol) - 1;
-		
+{
 	if (! external_vgaiobase)
 		return 1;
 
@@ -2788,7 +2787,8 @@
 		mem_req = default_mem_req + ovsc_offset +
 			ovsc_addlen;
 		mem_req = ((mem_req + PAGE_SIZE - 1) & PAGE_MASK) + PAGE_SIZE;
-		screen_base = (unsigned long) atari_stram_alloc(mem_req, &mem_start);
+		screen_base = (unsigned long)atari_stram_alloc(mem_req, &mem_start,
+													   "atafb");
 		memset((char *) screen_base, 0, mem_req);
 		pad = ((screen_base + PAGE_SIZE-1) & PAGE_MASK) - screen_base;
 		screen_base+=pad;
@@ -3157,6 +3157,7 @@
 	/* Not reached because the usecount will never
 	   be decremented to zero */
 	unregister_framebuffer(&fb_info);
-	/* TODO: clean up ... */
+	/* atari_stram_free( screen_base ); */
+	/* TODO: further clean up ... */
 }
 #endif /* MODULE */
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/fs/proc/array.c linux-2.1.64/fs/proc/array.c
--- linux-2.1.64.orig/fs/proc/array.c	Sun Oct 26 16:59:39 1997
+++ linux-2.1.64/fs/proc/array.c	Wed Nov 19 11:38:12 1997
@@ -1076,6 +1076,9 @@
 #if defined (CONFIG_AMIGA) || defined (CONFIG_ATARI)
 extern int get_hardware_list(char *);
 #endif
+#ifdef CONFIG_STRAM_PROC
+extern int get_stram_list(char *);
+#endif
 
 static long get_root_array(char * page, int type, char **start,
 	off_t offset, unsigned long length)
@@ -1163,6 +1166,10 @@
 #if defined (CONFIG_AMIGA) || defined (CONFIG_ATARI)
 		case PROC_HARDWARE:
 			return get_hardware_list(page);
+#endif
+#ifdef CONFIG_STRAM_PROC
+		case PROC_STRAM:
+			return get_stram_list(page);
 #endif
 	}
 	return -EBADF;
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/fs/proc/root.c linux-2.1.64/fs/proc/root.c
--- linux-2.1.64.orig/fs/proc/root.c	Thu Oct 16 13:53:01 1997
+++ linux-2.1.64/fs/proc/root.c	Fri Nov 21 22:27:46 1997
@@ -460,6 +460,13 @@
 	0, &proc_array_inode_operations
 };
 #endif
+#ifdef CONFIG_STRAM_PROC
+static struct proc_dir_entry proc_root_stram = {
+	PROC_STRAM, 5, "stram",
+	S_IFREG | S_IRUGO, 1, 0, 0,
+	0, &proc_array_inode_operations
+};
+#endif
 static struct proc_dir_entry proc_root_self = {
 	PROC_SELF, 4, "self",
 	S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO, 1, 0, 0,
@@ -643,6 +650,9 @@
 #endif
 #if defined (CONFIG_AMIGA) || defined (CONFIG_ATARI)
 	proc_register(&proc_root, &proc_root_hardware);
+#endif
+#ifdef CONFIG_STRAM_SWAP
+	proc_register(&proc_root, &proc_root_stram);
 #endif
 	proc_register(&proc_root, &proc_root_slab);
 
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/include/asm-m68k/atari_stram.h linux-2.1.64/include/asm-m68k/atari_stram.h
--- linux-2.1.64.orig/include/asm-m68k/atari_stram.h	Thu Jan  1 01:00:00 1970
+++ linux-2.1.64/include/asm-m68k/atari_stram.h	Wed Nov 19 11:15:49 1997
@@ -0,0 +1,17 @@
+#ifndef _M68K_ATARI_STRAM_H
+#define _M68K_ATARI_STRAM_H
+
+/*
+ * Functions for Atari ST-RAM management
+ */
+
+/* public interface */
+void *atari_stram_alloc( long size, unsigned long *start_mem,
+						 const char *owner );
+void atari_stram_free( void *);
+
+/* functions called internally by other parts of the kernel */
+void atari_stram_init( void);
+void atari_stram_reserve_pages( unsigned long start_mem );
+
+#endif /*_M68K_ATARI_STRAM_H */
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/include/asm-m68k/atarihw.h linux-2.1.64/include/asm-m68k/atarihw.h
--- linux-2.1.64.orig/include/asm-m68k/atarihw.h	Mon Nov 17 12:47:39 1997
+++ linux-2.1.64/include/asm-m68k/atarihw.h	Sat Nov 22 00:06:48 1997
@@ -101,11 +101,6 @@
 #define	MFPDELAY() \
 	__asm__ __volatile__ ( "tstb %0" : : "m" (mfp.par_dt_reg) : "cc" );
 
-/* Memory used for screen ram and stdma buffers */
-void atari_stram_init (void);
-void *atari_stram_alloc (long size, unsigned long *start_mem );
-void atari_stram_free (void *);
-
 /* Do cache push/invalidate for DMA read/write. This function obeys the
  * snooping on some machines (Medusa) and processors: The Medusa itself can
  * snoop, but only the '040 can source data from its cache to DMA writes i.e.,
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/include/asm-m68k/signal.h linux-2.1.64/include/asm-m68k/signal.h
--- linux-2.1.64.orig/include/asm-m68k/signal.h	Sat Aug 24 14:45:27 1996
+++ linux-2.1.64/include/asm-m68k/signal.h	Fri Nov 21 23:36:06 1997
@@ -44,10 +44,7 @@
 #define	SIGUNUSED	31
 
 /*
- * sa_flags values: SA_STACK is not currently supported, but will allow the
- * usage of signal stacks by using the (now obsolete) sa_restorer field in
- * the sigaction structure as a stack pointer. This is now possible due to
- * the changes in signal handling. LBT 010493.
+ * sa_flags values: SA_STACK is now supported (++roman)
  * SA_INTERRUPT is a no-op, but left due to historical reasons. Use the
  * SA_RESTART flag to get restarting signals (which were the default long ago)
  * SA_SHIRQ flag is for shared interrupt support on PCI and EISA.
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/include/linux/modversions.h linux-2.1.64/include/linux/modversions.h
--- linux-2.1.64.orig/include/linux/modversions.h	Thu Jan  1 01:00:00 1970
+++ linux-2.1.64/include/linux/modversions.h	Mon Nov 17 21:39:39 1997
@@ -0,0 +1 @@
+#include <linux/modsetver.h>
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/include/linux/proc_fs.h linux-2.1.64/include/linux/proc_fs.h
--- linux-2.1.64.orig/include/linux/proc_fs.h	Thu Nov 13 15:21:21 1997
+++ linux-2.1.64/include/linux/proc_fs.h	Sat Nov 22 00:13:15 1997
@@ -52,7 +52,8 @@
 	PROC_SLABINFO,
 	PROC_PARPORT,
 	PROC_OMIRR, /* whether enabled or not */
-	PROC_PPC_HTAB
+	PROC_PPC_HTAB,
+	PROC_STRAM
 };
 
 enum pid_directory_inos {
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/include/linux/swap.h linux-2.1.64/include/linux/swap.h
--- linux-2.1.64.orig/include/linux/swap.h	Sun Oct 19 18:14:18 1997
+++ linux-2.1.64/include/linux/swap.h	Sat Nov 22 00:23:25 1997
@@ -74,6 +74,11 @@
 void si_swapinfo(struct sysinfo *);
 unsigned long get_swap_page(void);
 extern void FASTCALL(swap_free(unsigned long));
+struct swap_list_t {
+	int head;	/* head of priority-ordered swapfile list */
+	int next;	/* swapfile to be used next */
+};
+extern struct swap_list_t swap_list;
 
 /*
  * vm_ops not present page codes for shared memory.
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/init/main.c linux-2.1.64/init/main.c
--- linux-2.1.64.orig/init/main.c	Mon Nov 17 12:23:37 1997
+++ linux-2.1.64/init/main.c	Mon Nov 17 22:54:48 1997
@@ -211,6 +211,7 @@
 #ifdef CONFIG_ATARI_SCSI
 extern void atari_scsi_setup (char *str, int *ints);
 #endif
+extern void stram_swap_setup (char *str, int *ints);
 extern void wd33c93_setup (char *str, int *ints);
 extern void gvp11_setup (char *str, int *ints);
 extern void ncr53c7xx_setup (char *str, int *ints);
@@ -518,6 +519,9 @@
 #endif
 #ifdef CONFIG_ATARI_SCSI
 	{ "atascsi=", atari_scsi_setup },
+#endif
+#ifdef CONFIG_STRAM_SWAP
+	{ "stram_swap=", stram_swap_setup },
 #endif
 #if defined(CONFIG_A4000T_SCSI) || defined(CONFIG_WARPENGINE_SCSI) || defined(CONFIG_A4091_SCSI)
         { "53c7xx=", ncr53c7xx_setup },
diff -u --recursive --exclude-from=diff-excludes --new-file linux-2.1.64.orig/mm/swapfile.c linux-2.1.64/mm/swapfile.c
--- linux-2.1.64.orig/mm/swapfile.c	Fri Oct 31 13:49:18 1997
+++ linux-2.1.64/mm/swapfile.c	Sat Nov 22 00:58:36 1997
@@ -27,10 +27,7 @@
 
 unsigned int nr_swapfiles = 0;
 
-static struct {
-	int head;	/* head of priority-ordered swapfile list */
-	int next;	/* swapfile to be used next */
-} swap_list = {-1, -1};
+struct swap_list_t swap_list = {-1, -1};
 
 struct swap_info_struct swap_info[MAX_SWAPFILES];
 
Binary files linux-2.1.64.orig/scripts/mkdep and linux-2.1.64/scripts/mkdep differ
