stram.c 9.6 KB

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