1 /* 2 * Functions for ST-RAM allocations 3 * 4 * Copyright 1994-97 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> 5 * 6 * This file is subject to the terms and conditions of the GNU General Public 7 * License. See the file COPYING in the main directory of this archive 8 * for more details. 9 */ 10 11 #include <linux/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/slab.h> 18 #include <linux/vmalloc.h> 19 #include <linux/pagemap.h> 20 #include <linux/memblock.h> 21 #include <linux/mount.h> 22 #include <linux/blkdev.h> 23 #include <linux/module.h> 24 #include <linux/ioport.h> 25 26 #include <asm/setup.h> 27 #include <asm/machdep.h> 28 #include <asm/page.h> 29 #include <asm/pgtable.h> 30 #include <asm/atarihw.h> 31 #include <asm/atari_stram.h> 32 #include <asm/io.h> 33 34 35 /* 36 * The ST-RAM allocator allocates memory from a pool of reserved ST-RAM of 37 * configurable size, set aside on ST-RAM init. 38 * As long as this pool is not exhausted, allocation of real ST-RAM can be 39 * guaranteed. 40 */ 41 42 /* set if kernel is in ST-RAM */ 43 static int kernel_in_stram; 44 45 static struct resource stram_pool = { 46 .name = "ST-RAM Pool" 47 }; 48 49 static unsigned long pool_size = 1024*1024; 50 51 static unsigned long stram_virt_offset; 52 53 static int __init atari_stram_setup(char *arg) 54 { 55 if (!MACH_IS_ATARI) 56 return 0; 57 58 pool_size = memparse(arg, NULL); 59 return 0; 60 } 61 62 early_param("stram_pool", atari_stram_setup); 63 64 65 /* 66 * This init function is called very early by atari/config.c 67 * It initializes some internal variables needed for stram_alloc() 68 */ 69 void __init atari_stram_init(void) 70 { 71 int i; 72 73 /* 74 * determine whether kernel code resides in ST-RAM 75 * (then ST-RAM is the first memory block at virtual 0x0) 76 */ 77 kernel_in_stram = (m68k_memory[0].addr == 0); 78 79 for (i = 0; i < m68k_num_memory; ++i) { 80 if (m68k_memory[i].addr == 0) { 81 return; 82 } 83 } 84 85 /* Should never come here! (There is always ST-Ram!) */ 86 panic("atari_stram_init: no ST-RAM found!"); 87 } 88 89 90 /* 91 * This function is called from setup_arch() to reserve the pages needed for 92 * ST-RAM management, if the kernel resides in ST-RAM. 93 */ 94 void __init atari_stram_reserve_pages(void *start_mem) 95 { 96 if (kernel_in_stram) { 97 pr_debug("atari_stram pool: kernel in ST-RAM, using alloc_bootmem!\n"); 98 stram_pool.start = (resource_size_t)memblock_alloc_low(pool_size, 99 PAGE_SIZE); 100 stram_pool.end = stram_pool.start + pool_size - 1; 101 request_resource(&iomem_resource, &stram_pool); 102 stram_virt_offset = 0; 103 pr_debug("atari_stram pool: size = %lu bytes, resource = %pR\n", 104 pool_size, &stram_pool); 105 pr_debug("atari_stram pool: stram_virt_offset = %lx\n", 106 stram_virt_offset); 107 } 108 } 109 110 111 /* 112 * This function is called as arch initcall to reserve the pages needed for 113 * ST-RAM management, if the kernel does not reside in ST-RAM. 114 */ 115 int __init atari_stram_map_pages(void) 116 { 117 if (!kernel_in_stram) { 118 /* 119 * Skip page 0, as the fhe first 2 KiB are supervisor-only! 120 */ 121 pr_debug("atari_stram pool: kernel not in ST-RAM, using ioremap!\n"); 122 stram_pool.start = PAGE_SIZE; 123 stram_pool.end = stram_pool.start + pool_size - 1; 124 request_resource(&iomem_resource, &stram_pool); 125 stram_virt_offset = (unsigned long) ioremap(stram_pool.start, 126 resource_size(&stram_pool)) - stram_pool.start; 127 pr_debug("atari_stram pool: size = %lu bytes, resource = %pR\n", 128 pool_size, &stram_pool); 129 pr_debug("atari_stram pool: stram_virt_offset = %lx\n", 130 stram_virt_offset); 131 } 132 return 0; 133 } 134 arch_initcall(atari_stram_map_pages); 135 136 137 void *atari_stram_to_virt(unsigned long phys) 138 { 139 return (void *)(phys + stram_virt_offset); 140 } 141 EXPORT_SYMBOL(atari_stram_to_virt); 142 143 144 unsigned long atari_stram_to_phys(void *virt) 145 { 146 return (unsigned long)(virt - stram_virt_offset); 147 } 148 EXPORT_SYMBOL(atari_stram_to_phys); 149 150 151 void *atari_stram_alloc(unsigned long size, const char *owner) 152 { 153 struct resource *res; 154 int error; 155 156 pr_debug("atari_stram_alloc: allocate %lu bytes\n", size); 157 158 /* round up */ 159 size = PAGE_ALIGN(size); 160 161 res = kzalloc(sizeof(struct resource), GFP_KERNEL); 162 if (!res) 163 return NULL; 164 165 res->name = owner; 166 error = allocate_resource(&stram_pool, res, size, 0, UINT_MAX, 167 PAGE_SIZE, NULL, NULL); 168 if (error < 0) { 169 pr_err("atari_stram_alloc: allocate_resource() failed %d!\n", 170 error); 171 kfree(res); 172 return NULL; 173 } 174 175 pr_debug("atari_stram_alloc: returning %pR\n", res); 176 return atari_stram_to_virt(res->start); 177 } 178 EXPORT_SYMBOL(atari_stram_alloc); 179 180 181 void atari_stram_free(void *addr) 182 { 183 unsigned long start = atari_stram_to_phys(addr); 184 struct resource *res; 185 unsigned long size; 186 187 res = lookup_resource(&stram_pool, start); 188 if (!res) { 189 pr_err("atari_stram_free: trying to free nonexistent region " 190 "at %p\n", addr); 191 return; 192 } 193 194 size = resource_size(res); 195 pr_debug("atari_stram_free: free %lu bytes at %p\n", size, addr); 196 release_resource(res); 197 kfree(res); 198 } 199 EXPORT_SYMBOL(atari_stram_free); 200