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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Michael Schmitz | 217bbd8 | 2010-11-01 19:54:00 +0100 | [diff] [blame] | 51 | static int __init atari_stram_setup(char *arg) |
| 52 | { |
| 53 | if (!MACH_IS_ATARI) |
| 54 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Michael Schmitz | 217bbd8 | 2010-11-01 19:54:00 +0100 | [diff] [blame] | 56 | pool_size = memparse(arg, NULL); |
| 57 | return 0; |
| 58 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Michael Schmitz | 217bbd8 | 2010-11-01 19:54:00 +0100 | [diff] [blame] | 60 | early_param("stram_pool", atari_stram_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 63 | /* |
| 64 | * This init function is called very early by atari/config.c |
| 65 | * It initializes some internal variables needed for stram_alloc() |
| 66 | */ |
| 67 | void __init atari_stram_init(void) |
| 68 | { |
| 69 | int i; |
Michael Schmitz | 217bbd8 | 2010-11-01 19:54:00 +0100 | [diff] [blame] | 70 | void *stram_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | stram_start = phys_to_virt(0); |
| 77 | kernel_in_stram = (stram_start == 0); |
| 78 | |
Michael Schmitz | 217bbd8 | 2010-11-01 19:54:00 +0100 | [diff] [blame] | 79 | for (i = 0; i < m68k_num_memory; ++i) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | if (m68k_memory[i].addr == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | return; |
| 82 | } |
| 83 | } |
Michael Schmitz | 217bbd8 | 2010-11-01 19:54:00 +0100 | [diff] [blame] | 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | /* Should never come here! (There is always ST-Ram!) */ |
Michael Schmitz | 217bbd8 | 2010-11-01 19:54:00 +0100 | [diff] [blame] | 86 | panic("atari_stram_init: no ST-RAM found!"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | |
| 90 | /* |
| 91 | * This function is called from setup_arch() to reserve the pages needed for |
| 92 | * ST-RAM management. |
| 93 | */ |
| 94 | void __init atari_stram_reserve_pages(void *start_mem) |
| 95 | { |
Michael Schmitz | 217bbd8 | 2010-11-01 19:54:00 +0100 | [diff] [blame] | 96 | /* |
| 97 | * always reserve first page of ST-RAM, the first 2 KiB are |
| 98 | * supervisor-only! |
| 99 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | if (!kernel_in_stram) |
Bernhard Walle | 72a7fe3 | 2008-02-07 00:15:17 -0800 | [diff] [blame] | 101 | reserve_bootmem(0, PAGE_SIZE, BOOTMEM_DEFAULT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Michael Schmitz | 217bbd8 | 2010-11-01 19:54:00 +0100 | [diff] [blame] | 103 | stram_pool.start = (resource_size_t)alloc_bootmem_low_pages(pool_size); |
| 104 | stram_pool.end = stram_pool.start + pool_size - 1; |
| 105 | request_resource(&iomem_resource, &stram_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Michael Schmitz | 217bbd8 | 2010-11-01 19:54:00 +0100 | [diff] [blame] | 107 | pr_debug("atari_stram pool: size = %lu bytes, resource = %pR\n", |
| 108 | pool_size, &stram_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | |
Michael Schmitz | 217bbd8 | 2010-11-01 19:54:00 +0100 | [diff] [blame] | 112 | void *atari_stram_alloc(unsigned long size, const char *owner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | { |
Michael Schmitz | 217bbd8 | 2010-11-01 19:54:00 +0100 | [diff] [blame] | 114 | struct resource *res; |
| 115 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
Michael Schmitz | 217bbd8 | 2010-11-01 19:54:00 +0100 | [diff] [blame] | 117 | pr_debug("atari_stram_alloc: allocate %lu bytes\n", size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Michael Schmitz | 217bbd8 | 2010-11-01 19:54:00 +0100 | [diff] [blame] | 119 | /* round up */ |
| 120 | size = PAGE_ALIGN(size); |
| 121 | |
| 122 | res = kzalloc(sizeof(struct resource), GFP_KERNEL); |
| 123 | if (!res) |
| 124 | return NULL; |
| 125 | |
| 126 | res->name = owner; |
| 127 | error = allocate_resource(&stram_pool, res, size, 0, UINT_MAX, |
| 128 | PAGE_SIZE, NULL, NULL); |
| 129 | if (error < 0) { |
| 130 | pr_err("atari_stram_alloc: allocate_resource() failed %d!\n", |
| 131 | error); |
| 132 | kfree(res); |
| 133 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Michael Schmitz | 217bbd8 | 2010-11-01 19:54:00 +0100 | [diff] [blame] | 136 | pr_debug("atari_stram_alloc: returning %pR\n", res); |
| 137 | return (void *)res->start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
Adrian Bunk | a3b2004 | 2008-02-04 22:30:26 -0800 | [diff] [blame] | 139 | EXPORT_SYMBOL(atari_stram_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Michael Schmitz | 217bbd8 | 2010-11-01 19:54:00 +0100 | [diff] [blame] | 142 | void atari_stram_free(void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
Michael Schmitz | 217bbd8 | 2010-11-01 19:54:00 +0100 | [diff] [blame] | 144 | unsigned long start = (unsigned long)addr; |
| 145 | struct resource *res; |
| 146 | unsigned long size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
Michael Schmitz | 217bbd8 | 2010-11-01 19:54:00 +0100 | [diff] [blame] | 148 | res = lookup_resource(&stram_pool, start); |
| 149 | if (!res) { |
| 150 | pr_err("atari_stram_free: trying to free nonexistent region " |
| 151 | "at %p\n", addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | return; |
| 153 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
Michael Schmitz | 217bbd8 | 2010-11-01 19:54:00 +0100 | [diff] [blame] | 155 | size = resource_size(res); |
| 156 | pr_debug("atari_stram_free: free %lu bytes at %p\n", size, addr); |
| 157 | release_resource(res); |
| 158 | kfree(res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
Adrian Bunk | a3b2004 | 2008-02-04 22:30:26 -0800 | [diff] [blame] | 160 | EXPORT_SYMBOL(atari_stram_free); |