stram.c 34 KB

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