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