stram.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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/types.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mm.h>
  13. #include <linux/kdev_t.h>
  14. #include <linux/major.h>
  15. #include <linux/init.h>
  16. #include <linux/slab.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/bootmem.h>
  20. #include <linux/mount.h>
  21. #include <linux/blkdev.h>
  22. #include <asm/setup.h>
  23. #include <asm/machdep.h>
  24. #include <asm/page.h>
  25. #include <asm/pgtable.h>
  26. #include <asm/atarihw.h>
  27. #include <asm/atari_stram.h>
  28. #include <asm/io.h>
  29. #include <asm/semaphore.h>
  30. #undef DEBUG
  31. #ifdef DEBUG
  32. #define DPRINTK(fmt,args...) printk( fmt, ##args )
  33. #else
  34. #define DPRINTK(fmt,args...)
  35. #endif
  36. #if defined(CONFIG_PROC_FS) && defined(CONFIG_STRAM_PROC)
  37. /* abbrev for the && above... */
  38. #define DO_PROC
  39. #include <linux/proc_fs.h>
  40. #endif
  41. /*
  42. * ++roman:
  43. *
  44. * New version of ST-Ram buffer allocation. Instead of using the
  45. * 1 MB - 4 KB that remain when the ST-Ram chunk starts at $1000
  46. * (1 MB granularity!), such buffers are reserved like this:
  47. *
  48. * - If the kernel resides in ST-Ram anyway, we can take the buffer
  49. * from behind the current kernel data space the normal way
  50. * (incrementing start_mem).
  51. *
  52. * - If the kernel is in TT-Ram, stram_init() initializes start and
  53. * end of the available region. Buffers are allocated from there
  54. * and mem_init() later marks the such used pages as reserved.
  55. * Since each TT-Ram chunk is at least 4 MB in size, I hope there
  56. * won't be an overrun of the ST-Ram region by normal kernel data
  57. * space.
  58. *
  59. * For that, ST-Ram may only be allocated while kernel initialization
  60. * is going on, or exactly: before mem_init() is called. There is also
  61. * no provision now for freeing ST-Ram buffers. It seems that isn't
  62. * really needed.
  63. *
  64. */
  65. /* Start and end (virtual) of ST-RAM */
  66. static void *stram_start, *stram_end;
  67. /* set after memory_init() executed and allocations via start_mem aren't
  68. * possible anymore */
  69. static int mem_init_done;
  70. /* set if kernel is in ST-RAM */
  71. static int kernel_in_stram;
  72. typedef struct stram_block {
  73. struct stram_block *next;
  74. void *start;
  75. unsigned long size;
  76. unsigned flags;
  77. const char *owner;
  78. } BLOCK;
  79. /* values for flags field */
  80. #define BLOCK_FREE 0x01 /* free structure in the BLOCKs pool */
  81. #define BLOCK_KMALLOCED 0x02 /* structure allocated by kmalloc() */
  82. #define BLOCK_GFP 0x08 /* block allocated with __get_dma_pages() */
  83. /* list of allocated blocks */
  84. static BLOCK *alloc_list;
  85. /* We can't always use kmalloc() to allocate BLOCK structures, since
  86. * stram_alloc() can be called rather early. So we need some pool of
  87. * statically allocated structures. 20 of them is more than enough, so in most
  88. * cases we never should need to call kmalloc(). */
  89. #define N_STATIC_BLOCKS 20
  90. static BLOCK static_blocks[N_STATIC_BLOCKS];
  91. /***************************** Prototypes *****************************/
  92. static BLOCK *add_region( void *addr, unsigned long size );
  93. static BLOCK *find_region( void *addr );
  94. static int remove_region( BLOCK *block );
  95. /************************* End of Prototypes **************************/
  96. /* ------------------------------------------------------------------------ */
  97. /* Public Interface */
  98. /* ------------------------------------------------------------------------ */
  99. /*
  100. * This init function is called very early by atari/config.c
  101. * It initializes some internal variables needed for stram_alloc()
  102. */
  103. void __init atari_stram_init(void)
  104. {
  105. int i;
  106. /* initialize static blocks */
  107. for( i = 0; i < N_STATIC_BLOCKS; ++i )
  108. static_blocks[i].flags = BLOCK_FREE;
  109. /* determine whether kernel code resides in ST-RAM (then ST-RAM is the
  110. * first memory block at virtual 0x0) */
  111. stram_start = phys_to_virt(0);
  112. kernel_in_stram = (stram_start == 0);
  113. for( i = 0; i < m68k_num_memory; ++i ) {
  114. if (m68k_memory[i].addr == 0) {
  115. /* skip first 2kB or page (supervisor-only!) */
  116. stram_end = stram_start + m68k_memory[i].size;
  117. return;
  118. }
  119. }
  120. /* Should never come here! (There is always ST-Ram!) */
  121. panic( "atari_stram_init: no ST-RAM found!" );
  122. }
  123. /*
  124. * This function is called from setup_arch() to reserve the pages needed for
  125. * ST-RAM management.
  126. */
  127. void __init atari_stram_reserve_pages(void *start_mem)
  128. {
  129. /* always reserve first page of ST-RAM, the first 2 kB are
  130. * supervisor-only! */
  131. if (!kernel_in_stram)
  132. reserve_bootmem (0, PAGE_SIZE);
  133. }
  134. void atari_stram_mem_init_hook (void)
  135. {
  136. mem_init_done = 1;
  137. }
  138. /*
  139. * This is main public interface: somehow allocate a ST-RAM block
  140. *
  141. * - If we're before mem_init(), we have to make a static allocation. The
  142. * region is taken in the kernel data area (if the kernel is in ST-RAM) or
  143. * from the start of ST-RAM (if the kernel is in TT-RAM) and added to the
  144. * rsvd_stram_* region. The ST-RAM is somewhere in the middle of kernel
  145. * address space in the latter case.
  146. *
  147. * - If mem_init() already has been called, try with __get_dma_pages().
  148. * This has the disadvantage that it's very hard to get more than 1 page,
  149. * and it is likely to fail :-(
  150. *
  151. */
  152. void *atari_stram_alloc(long size, const char *owner)
  153. {
  154. void *addr = NULL;
  155. BLOCK *block;
  156. int flags;
  157. DPRINTK("atari_stram_alloc(size=%08lx,owner=%s)\n", size, owner);
  158. if (!mem_init_done)
  159. return alloc_bootmem_low(size);
  160. else {
  161. /* After mem_init(): can only resort to __get_dma_pages() */
  162. addr = (void *)__get_dma_pages(GFP_KERNEL, get_order(size));
  163. flags = BLOCK_GFP;
  164. DPRINTK( "atari_stram_alloc: after mem_init, "
  165. "get_pages=%p\n", addr );
  166. }
  167. if (addr) {
  168. if (!(block = add_region( addr, size ))) {
  169. /* out of memory for BLOCK structure :-( */
  170. DPRINTK( "atari_stram_alloc: out of mem for BLOCK -- "
  171. "freeing again\n" );
  172. free_pages((unsigned long)addr, get_order(size));
  173. return( NULL );
  174. }
  175. block->owner = owner;
  176. block->flags |= flags;
  177. }
  178. return( addr );
  179. }
  180. void atari_stram_free( void *addr )
  181. {
  182. BLOCK *block;
  183. DPRINTK( "atari_stram_free(addr=%p)\n", addr );
  184. if (!(block = find_region( addr ))) {
  185. printk( KERN_ERR "Attempt to free non-allocated ST-RAM block at %p "
  186. "from %p\n", addr, __builtin_return_address(0) );
  187. return;
  188. }
  189. DPRINTK( "atari_stram_free: found block (%p): size=%08lx, owner=%s, "
  190. "flags=%02x\n", block, block->size, block->owner, block->flags );
  191. if (!(block->flags & BLOCK_GFP))
  192. goto fail;
  193. DPRINTK("atari_stram_free: is kmalloced, order_size=%d\n",
  194. get_order(block->size));
  195. free_pages((unsigned long)addr, get_order(block->size));
  196. remove_region( block );
  197. return;
  198. fail:
  199. printk( KERN_ERR "atari_stram_free: cannot free block at %p "
  200. "(called from %p)\n", addr, __builtin_return_address(0) );
  201. }
  202. /* ------------------------------------------------------------------------ */
  203. /* Region Management */
  204. /* ------------------------------------------------------------------------ */
  205. /* insert a region into the alloced list (sorted) */
  206. static BLOCK *add_region( void *addr, unsigned long size )
  207. {
  208. BLOCK **p, *n = NULL;
  209. int i;
  210. for( i = 0; i < N_STATIC_BLOCKS; ++i ) {
  211. if (static_blocks[i].flags & BLOCK_FREE) {
  212. n = &static_blocks[i];
  213. n->flags = 0;
  214. break;
  215. }
  216. }
  217. if (!n && mem_init_done) {
  218. /* if statics block pool exhausted and we can call kmalloc() already
  219. * (after mem_init()), try that */
  220. n = kmalloc( sizeof(BLOCK), GFP_KERNEL );
  221. if (n)
  222. n->flags = BLOCK_KMALLOCED;
  223. }
  224. if (!n) {
  225. printk( KERN_ERR "Out of memory for ST-RAM descriptor blocks\n" );
  226. return( NULL );
  227. }
  228. n->start = addr;
  229. n->size = size;
  230. for( p = &alloc_list; *p; p = &((*p)->next) )
  231. if ((*p)->start > addr) break;
  232. n->next = *p;
  233. *p = n;
  234. return( n );
  235. }
  236. /* find a region (by start addr) in the alloced list */
  237. static BLOCK *find_region( void *addr )
  238. {
  239. BLOCK *p;
  240. for( p = alloc_list; p; p = p->next ) {
  241. if (p->start == addr)
  242. return( p );
  243. if (p->start > addr)
  244. break;
  245. }
  246. return( NULL );
  247. }
  248. /* remove a block from the alloced list */
  249. static int remove_region( BLOCK *block )
  250. {
  251. BLOCK **p;
  252. for( p = &alloc_list; *p; p = &((*p)->next) )
  253. if (*p == block) break;
  254. if (!*p)
  255. return( 0 );
  256. *p = block->next;
  257. if (block->flags & BLOCK_KMALLOCED)
  258. kfree( block );
  259. else
  260. block->flags |= BLOCK_FREE;
  261. return( 1 );
  262. }
  263. /* ------------------------------------------------------------------------ */
  264. /* /proc statistics file stuff */
  265. /* ------------------------------------------------------------------------ */
  266. #ifdef DO_PROC
  267. #define PRINT_PROC(fmt,args...) len += sprintf( buf+len, fmt, ##args )
  268. int get_stram_list( char *buf )
  269. {
  270. int len = 0;
  271. BLOCK *p;
  272. PRINT_PROC("Total ST-RAM: %8u kB\n",
  273. (stram_end - stram_start) >> 10);
  274. PRINT_PROC( "Allocated regions:\n" );
  275. for( p = alloc_list; p; p = p->next ) {
  276. if (len + 50 >= PAGE_SIZE)
  277. break;
  278. PRINT_PROC("0x%08lx-0x%08lx: %s (",
  279. virt_to_phys(p->start),
  280. virt_to_phys(p->start+p->size-1),
  281. p->owner);
  282. if (p->flags & BLOCK_GFP)
  283. PRINT_PROC( "page-alloced)\n" );
  284. else
  285. PRINT_PROC( "??)\n" );
  286. }
  287. return( len );
  288. }
  289. #endif
  290. /*
  291. * Local variables:
  292. * c-indent-level: 4
  293. * tab-width: 4
  294. * End:
  295. */