Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 1 | /* |
Robin Getz | 5e95320 | 2008-10-08 17:22:49 +0800 | [diff] [blame] | 2 | * File: arch/blackfin/mm/sram-alloc.c |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 3 | * Based on: |
| 4 | * Author: |
| 5 | * |
| 6 | * Created: |
Robin Getz | 5e95320 | 2008-10-08 17:22:49 +0800 | [diff] [blame] | 7 | * Description: SRAM allocator for Blackfin L1 and L2 memory |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 8 | * |
| 9 | * Modified: |
Robin Getz | 5e95320 | 2008-10-08 17:22:49 +0800 | [diff] [blame] | 10 | * Copyright 2004-2008 Analog Devices Inc. |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 11 | * |
| 12 | * Bugs: Enter bugs at http://blackfin.uclinux.org/ |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, see the file COPYING, or write |
| 26 | * to the Free Software Foundation, Inc., |
| 27 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 28 | */ |
| 29 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 30 | #include <linux/module.h> |
| 31 | #include <linux/kernel.h> |
| 32 | #include <linux/types.h> |
| 33 | #include <linux/miscdevice.h> |
| 34 | #include <linux/ioport.h> |
| 35 | #include <linux/fcntl.h> |
| 36 | #include <linux/init.h> |
| 37 | #include <linux/poll.h> |
| 38 | #include <linux/proc_fs.h> |
| 39 | #include <linux/spinlock.h> |
| 40 | #include <linux/rtc.h> |
| 41 | #include <asm/blackfin.h> |
Graf Yang | dbc895f | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 42 | #include <asm/mem_map.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 43 | #include "blackfin_sram.h" |
| 44 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 45 | /* the data structure for L1 scratchpad and DATA SRAM */ |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 46 | struct sram_piece { |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 47 | void *paddr; |
| 48 | int size; |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 49 | pid_t pid; |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 50 | struct sram_piece *next; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Mike Frysinger | 81c969a | 2009-07-01 15:42:13 +0000 | [diff] [blame^] | 53 | static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1sram_lock); |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 54 | static DEFINE_PER_CPU(struct sram_piece, free_l1_ssram_head); |
| 55 | static DEFINE_PER_CPU(struct sram_piece, used_l1_ssram_head); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 56 | |
| 57 | #if L1_DATA_A_LENGTH != 0 |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 58 | static DEFINE_PER_CPU(struct sram_piece, free_l1_data_A_sram_head); |
| 59 | static DEFINE_PER_CPU(struct sram_piece, used_l1_data_A_sram_head); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 60 | #endif |
| 61 | |
| 62 | #if L1_DATA_B_LENGTH != 0 |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 63 | static DEFINE_PER_CPU(struct sram_piece, free_l1_data_B_sram_head); |
| 64 | static DEFINE_PER_CPU(struct sram_piece, used_l1_data_B_sram_head); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 65 | #endif |
| 66 | |
Mike Frysinger | 81c969a | 2009-07-01 15:42:13 +0000 | [diff] [blame^] | 67 | #if L1_DATA_A_LENGTH || L1_DATA_B_LENGTH |
| 68 | static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_data_sram_lock); |
| 69 | #endif |
| 70 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 71 | #if L1_CODE_LENGTH != 0 |
Mike Frysinger | 81c969a | 2009-07-01 15:42:13 +0000 | [diff] [blame^] | 72 | static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_inst_sram_lock); |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 73 | static DEFINE_PER_CPU(struct sram_piece, free_l1_inst_sram_head); |
| 74 | static DEFINE_PER_CPU(struct sram_piece, used_l1_inst_sram_head); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 75 | #endif |
| 76 | |
Mike Frysinger | 07aa7be | 2008-08-13 16:16:11 +0800 | [diff] [blame] | 77 | #if L2_LENGTH != 0 |
Mike Frysinger | 81c969a | 2009-07-01 15:42:13 +0000 | [diff] [blame^] | 78 | static spinlock_t l2_sram_lock ____cacheline_aligned_in_smp; |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 79 | static struct sram_piece free_l2_sram_head, used_l2_sram_head; |
| 80 | #endif |
| 81 | |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 82 | static struct kmem_cache *sram_piece_cache; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 83 | |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 84 | /* L1 Scratchpad SRAM initialization function */ |
| 85 | static void __init l1sram_init(void) |
| 86 | { |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 87 | unsigned int cpu; |
Graf Yang | 89ecd50 | 2009-05-25 06:40:42 +0000 | [diff] [blame] | 88 | unsigned long reserve; |
| 89 | |
| 90 | #ifdef CONFIG_SMP |
| 91 | reserve = 0; |
| 92 | #else |
| 93 | reserve = sizeof(struct l1_scratch_task_info); |
| 94 | #endif |
| 95 | |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 96 | for (cpu = 0; cpu < num_possible_cpus(); ++cpu) { |
| 97 | per_cpu(free_l1_ssram_head, cpu).next = |
| 98 | kmem_cache_alloc(sram_piece_cache, GFP_KERNEL); |
| 99 | if (!per_cpu(free_l1_ssram_head, cpu).next) { |
| 100 | printk(KERN_INFO "Fail to initialize Scratchpad data SRAM.\n"); |
| 101 | return; |
| 102 | } |
| 103 | |
Graf Yang | 89ecd50 | 2009-05-25 06:40:42 +0000 | [diff] [blame] | 104 | per_cpu(free_l1_ssram_head, cpu).next->paddr = (void *)get_l1_scratch_start_cpu(cpu) + reserve; |
| 105 | per_cpu(free_l1_ssram_head, cpu).next->size = L1_SCRATCH_LENGTH - reserve; |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 106 | per_cpu(free_l1_ssram_head, cpu).next->pid = 0; |
| 107 | per_cpu(free_l1_ssram_head, cpu).next->next = NULL; |
| 108 | |
| 109 | per_cpu(used_l1_ssram_head, cpu).next = NULL; |
| 110 | |
| 111 | /* mutex initialize */ |
| 112 | spin_lock_init(&per_cpu(l1sram_lock, cpu)); |
| 113 | printk(KERN_INFO "Blackfin Scratchpad data SRAM: %d KB\n", |
| 114 | L1_SCRATCH_LENGTH >> 10); |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 115 | } |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 118 | static void __init l1_data_sram_init(void) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 119 | { |
Mike Frysinger | 0b82e27 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 120 | #if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0 |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 121 | unsigned int cpu; |
Mike Frysinger | 0b82e27 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 122 | #endif |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 123 | #if L1_DATA_A_LENGTH != 0 |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 124 | for (cpu = 0; cpu < num_possible_cpus(); ++cpu) { |
| 125 | per_cpu(free_l1_data_A_sram_head, cpu).next = |
| 126 | kmem_cache_alloc(sram_piece_cache, GFP_KERNEL); |
| 127 | if (!per_cpu(free_l1_data_A_sram_head, cpu).next) { |
| 128 | printk(KERN_INFO "Fail to initialize L1 Data A SRAM.\n"); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | per_cpu(free_l1_data_A_sram_head, cpu).next->paddr = |
| 133 | (void *)get_l1_data_a_start_cpu(cpu) + (_ebss_l1 - _sdata_l1); |
| 134 | per_cpu(free_l1_data_A_sram_head, cpu).next->size = |
| 135 | L1_DATA_A_LENGTH - (_ebss_l1 - _sdata_l1); |
| 136 | per_cpu(free_l1_data_A_sram_head, cpu).next->pid = 0; |
| 137 | per_cpu(free_l1_data_A_sram_head, cpu).next->next = NULL; |
| 138 | |
| 139 | per_cpu(used_l1_data_A_sram_head, cpu).next = NULL; |
| 140 | |
| 141 | printk(KERN_INFO "Blackfin L1 Data A SRAM: %d KB (%d KB free)\n", |
| 142 | L1_DATA_A_LENGTH >> 10, |
| 143 | per_cpu(free_l1_data_A_sram_head, cpu).next->size >> 10); |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 144 | } |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 145 | #endif |
| 146 | #if L1_DATA_B_LENGTH != 0 |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 147 | for (cpu = 0; cpu < num_possible_cpus(); ++cpu) { |
| 148 | per_cpu(free_l1_data_B_sram_head, cpu).next = |
| 149 | kmem_cache_alloc(sram_piece_cache, GFP_KERNEL); |
| 150 | if (!per_cpu(free_l1_data_B_sram_head, cpu).next) { |
| 151 | printk(KERN_INFO "Fail to initialize L1 Data B SRAM.\n"); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | per_cpu(free_l1_data_B_sram_head, cpu).next->paddr = |
| 156 | (void *)get_l1_data_b_start_cpu(cpu) + (_ebss_b_l1 - _sdata_b_l1); |
| 157 | per_cpu(free_l1_data_B_sram_head, cpu).next->size = |
| 158 | L1_DATA_B_LENGTH - (_ebss_b_l1 - _sdata_b_l1); |
| 159 | per_cpu(free_l1_data_B_sram_head, cpu).next->pid = 0; |
| 160 | per_cpu(free_l1_data_B_sram_head, cpu).next->next = NULL; |
| 161 | |
| 162 | per_cpu(used_l1_data_B_sram_head, cpu).next = NULL; |
| 163 | |
| 164 | printk(KERN_INFO "Blackfin L1 Data B SRAM: %d KB (%d KB free)\n", |
| 165 | L1_DATA_B_LENGTH >> 10, |
| 166 | per_cpu(free_l1_data_B_sram_head, cpu).next->size >> 10); |
| 167 | /* mutex initialize */ |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 168 | } |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 169 | #endif |
| 170 | |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 171 | #if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0 |
| 172 | for (cpu = 0; cpu < num_possible_cpus(); ++cpu) |
| 173 | spin_lock_init(&per_cpu(l1_data_sram_lock, cpu)); |
| 174 | #endif |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 177 | static void __init l1_inst_sram_init(void) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 178 | { |
| 179 | #if L1_CODE_LENGTH != 0 |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 180 | unsigned int cpu; |
| 181 | for (cpu = 0; cpu < num_possible_cpus(); ++cpu) { |
| 182 | per_cpu(free_l1_inst_sram_head, cpu).next = |
| 183 | kmem_cache_alloc(sram_piece_cache, GFP_KERNEL); |
| 184 | if (!per_cpu(free_l1_inst_sram_head, cpu).next) { |
| 185 | printk(KERN_INFO "Failed to initialize L1 Instruction SRAM\n"); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | per_cpu(free_l1_inst_sram_head, cpu).next->paddr = |
| 190 | (void *)get_l1_code_start_cpu(cpu) + (_etext_l1 - _stext_l1); |
| 191 | per_cpu(free_l1_inst_sram_head, cpu).next->size = |
| 192 | L1_CODE_LENGTH - (_etext_l1 - _stext_l1); |
| 193 | per_cpu(free_l1_inst_sram_head, cpu).next->pid = 0; |
| 194 | per_cpu(free_l1_inst_sram_head, cpu).next->next = NULL; |
| 195 | |
| 196 | per_cpu(used_l1_inst_sram_head, cpu).next = NULL; |
| 197 | |
| 198 | printk(KERN_INFO "Blackfin L1 Instruction SRAM: %d KB (%d KB free)\n", |
| 199 | L1_CODE_LENGTH >> 10, |
| 200 | per_cpu(free_l1_inst_sram_head, cpu).next->size >> 10); |
| 201 | |
| 202 | /* mutex initialize */ |
| 203 | spin_lock_init(&per_cpu(l1_inst_sram_lock, cpu)); |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 204 | } |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 205 | #endif |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 208 | static void __init l2_sram_init(void) |
| 209 | { |
Mike Frysinger | 07aa7be | 2008-08-13 16:16:11 +0800 | [diff] [blame] | 210 | #if L2_LENGTH != 0 |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 211 | free_l2_sram_head.next = |
| 212 | kmem_cache_alloc(sram_piece_cache, GFP_KERNEL); |
| 213 | if (!free_l2_sram_head.next) { |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 214 | printk(KERN_INFO "Fail to initialize L2 SRAM.\n"); |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 215 | return; |
| 216 | } |
| 217 | |
Jie Zhang | b2c2f30 | 2008-10-28 15:57:49 +0800 | [diff] [blame] | 218 | free_l2_sram_head.next->paddr = |
| 219 | (void *)L2_START + (_ebss_l2 - _stext_l2); |
| 220 | free_l2_sram_head.next->size = |
| 221 | L2_LENGTH - (_ebss_l2 - _stext_l2); |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 222 | free_l2_sram_head.next->pid = 0; |
| 223 | free_l2_sram_head.next->next = NULL; |
| 224 | |
| 225 | used_l2_sram_head.next = NULL; |
| 226 | |
| 227 | printk(KERN_INFO "Blackfin L2 SRAM: %d KB (%d KB free)\n", |
| 228 | L2_LENGTH >> 10, |
| 229 | free_l2_sram_head.next->size >> 10); |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 230 | |
| 231 | /* mutex initialize */ |
| 232 | spin_lock_init(&l2_sram_lock); |
Mike Frysinger | 81c969a | 2009-07-01 15:42:13 +0000 | [diff] [blame^] | 233 | #endif |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 234 | } |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 235 | |
Graf Yang | c72aa07 | 2009-05-25 04:44:00 +0000 | [diff] [blame] | 236 | static int __init bfin_sram_init(void) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 237 | { |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 238 | sram_piece_cache = kmem_cache_create("sram_piece_cache", |
| 239 | sizeof(struct sram_piece), |
| 240 | 0, SLAB_PANIC, NULL); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 241 | |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 242 | l1sram_init(); |
| 243 | l1_data_sram_init(); |
| 244 | l1_inst_sram_init(); |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 245 | l2_sram_init(); |
Graf Yang | c72aa07 | 2009-05-25 04:44:00 +0000 | [diff] [blame] | 246 | |
| 247 | return 0; |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 248 | } |
Graf Yang | c72aa07 | 2009-05-25 04:44:00 +0000 | [diff] [blame] | 249 | pure_initcall(bfin_sram_init); |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 250 | |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 251 | /* SRAM allocate function */ |
| 252 | static void *_sram_alloc(size_t size, struct sram_piece *pfree_head, |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 253 | struct sram_piece *pused_head) |
| 254 | { |
| 255 | struct sram_piece *pslot, *plast, *pavail; |
| 256 | |
| 257 | if (size <= 0 || !pfree_head || !pused_head) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 258 | return NULL; |
| 259 | |
| 260 | /* Align the size */ |
| 261 | size = (size + 3) & ~3; |
| 262 | |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 263 | pslot = pfree_head->next; |
| 264 | plast = pfree_head; |
| 265 | |
| 266 | /* search an available piece slot */ |
| 267 | while (pslot != NULL && size > pslot->size) { |
| 268 | plast = pslot; |
| 269 | pslot = pslot->next; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 270 | } |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 271 | |
| 272 | if (!pslot) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 273 | return NULL; |
| 274 | |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 275 | if (pslot->size == size) { |
| 276 | plast->next = pslot->next; |
| 277 | pavail = pslot; |
| 278 | } else { |
| 279 | pavail = kmem_cache_alloc(sram_piece_cache, GFP_KERNEL); |
| 280 | |
| 281 | if (!pavail) |
| 282 | return NULL; |
| 283 | |
| 284 | pavail->paddr = pslot->paddr; |
| 285 | pavail->size = size; |
| 286 | pslot->paddr += size; |
| 287 | pslot->size -= size; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 290 | pavail->pid = current->pid; |
| 291 | |
| 292 | pslot = pused_head->next; |
| 293 | plast = pused_head; |
| 294 | |
| 295 | /* insert new piece into used piece list !!! */ |
| 296 | while (pslot != NULL && pavail->paddr < pslot->paddr) { |
| 297 | plast = pslot; |
| 298 | pslot = pslot->next; |
| 299 | } |
| 300 | |
| 301 | pavail->next = pslot; |
| 302 | plast->next = pavail; |
| 303 | |
| 304 | return pavail->paddr; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /* Allocate the largest available block. */ |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 308 | static void *_sram_alloc_max(struct sram_piece *pfree_head, |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 309 | struct sram_piece *pused_head, |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 310 | unsigned long *psize) |
| 311 | { |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 312 | struct sram_piece *pslot, *pmax; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 313 | |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 314 | if (!pfree_head || !pused_head) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 315 | return NULL; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 316 | |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 317 | pmax = pslot = pfree_head->next; |
| 318 | |
| 319 | /* search an available piece slot */ |
| 320 | while (pslot != NULL) { |
| 321 | if (pslot->size > pmax->size) |
| 322 | pmax = pslot; |
| 323 | pslot = pslot->next; |
| 324 | } |
| 325 | |
| 326 | if (!pmax) |
| 327 | return NULL; |
| 328 | |
| 329 | *psize = pmax->size; |
| 330 | |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 331 | return _sram_alloc(*psize, pfree_head, pused_head); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 334 | /* SRAM free function */ |
| 335 | static int _sram_free(const void *addr, |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 336 | struct sram_piece *pfree_head, |
| 337 | struct sram_piece *pused_head) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 338 | { |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 339 | struct sram_piece *pslot, *plast, *pavail; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 340 | |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 341 | if (!pfree_head || !pused_head) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 342 | return -1; |
| 343 | |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 344 | /* search the relevant memory slot */ |
| 345 | pslot = pused_head->next; |
| 346 | plast = pused_head; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 347 | |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 348 | /* search an available piece slot */ |
| 349 | while (pslot != NULL && pslot->paddr != addr) { |
| 350 | plast = pslot; |
| 351 | pslot = pslot->next; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 354 | if (!pslot) |
| 355 | return -1; |
| 356 | |
| 357 | plast->next = pslot->next; |
| 358 | pavail = pslot; |
| 359 | pavail->pid = 0; |
| 360 | |
| 361 | /* insert free pieces back to the free list */ |
| 362 | pslot = pfree_head->next; |
| 363 | plast = pfree_head; |
| 364 | |
| 365 | while (pslot != NULL && addr > pslot->paddr) { |
| 366 | plast = pslot; |
| 367 | pslot = pslot->next; |
| 368 | } |
| 369 | |
| 370 | if (plast != pfree_head && plast->paddr + plast->size == pavail->paddr) { |
| 371 | plast->size += pavail->size; |
| 372 | kmem_cache_free(sram_piece_cache, pavail); |
| 373 | } else { |
Sonic Zhang | 225f7e1 | 2008-08-25 18:00:45 +0800 | [diff] [blame] | 374 | pavail->next = plast->next; |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 375 | plast->next = pavail; |
| 376 | plast = pavail; |
| 377 | } |
| 378 | |
| 379 | if (pslot && plast->paddr + plast->size == pslot->paddr) { |
| 380 | plast->size += pslot->size; |
| 381 | plast->next = pslot->next; |
| 382 | kmem_cache_free(sram_piece_cache, pslot); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | int sram_free(const void *addr) |
| 389 | { |
Robin Getz | 5e95320 | 2008-10-08 17:22:49 +0800 | [diff] [blame] | 390 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 391 | #if L1_CODE_LENGTH != 0 |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 392 | if (addr >= (void *)get_l1_code_start() |
| 393 | && addr < (void *)(get_l1_code_start() + L1_CODE_LENGTH)) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 394 | return l1_inst_sram_free(addr); |
Robin Getz | 5e95320 | 2008-10-08 17:22:49 +0800 | [diff] [blame] | 395 | else |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 396 | #endif |
| 397 | #if L1_DATA_A_LENGTH != 0 |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 398 | if (addr >= (void *)get_l1_data_a_start() |
| 399 | && addr < (void *)(get_l1_data_a_start() + L1_DATA_A_LENGTH)) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 400 | return l1_data_A_sram_free(addr); |
Robin Getz | 5e95320 | 2008-10-08 17:22:49 +0800 | [diff] [blame] | 401 | else |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 402 | #endif |
| 403 | #if L1_DATA_B_LENGTH != 0 |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 404 | if (addr >= (void *)get_l1_data_b_start() |
| 405 | && addr < (void *)(get_l1_data_b_start() + L1_DATA_B_LENGTH)) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 406 | return l1_data_B_sram_free(addr); |
Robin Getz | 5e95320 | 2008-10-08 17:22:49 +0800 | [diff] [blame] | 407 | else |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 408 | #endif |
Mike Frysinger | 07aa7be | 2008-08-13 16:16:11 +0800 | [diff] [blame] | 409 | #if L2_LENGTH != 0 |
Robin Getz | 5e95320 | 2008-10-08 17:22:49 +0800 | [diff] [blame] | 410 | if (addr >= (void *)L2_START |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 411 | && addr < (void *)(L2_START + L2_LENGTH)) |
| 412 | return l2_sram_free(addr); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 413 | else |
Robin Getz | 5e95320 | 2008-10-08 17:22:49 +0800 | [diff] [blame] | 414 | #endif |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 415 | return -1; |
| 416 | } |
| 417 | EXPORT_SYMBOL(sram_free); |
| 418 | |
| 419 | void *l1_data_A_sram_alloc(size_t size) |
| 420 | { |
Mike Frysinger | 81c969a | 2009-07-01 15:42:13 +0000 | [diff] [blame^] | 421 | #if L1_DATA_A_LENGTH != 0 |
Vegard Nossum | 226a6ec | 2008-08-28 17:28:46 +0800 | [diff] [blame] | 422 | unsigned long flags; |
Mike Frysinger | 81c969a | 2009-07-01 15:42:13 +0000 | [diff] [blame^] | 423 | void *addr; |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 424 | unsigned int cpu; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 425 | |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 426 | cpu = get_cpu(); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 427 | /* add mutex operation */ |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 428 | spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 429 | |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 430 | addr = _sram_alloc(size, &per_cpu(free_l1_data_A_sram_head, cpu), |
| 431 | &per_cpu(used_l1_data_A_sram_head, cpu)); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 432 | |
| 433 | /* add mutex operation */ |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 434 | spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags); |
| 435 | put_cpu(); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 436 | |
| 437 | pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n", |
| 438 | (long unsigned int)addr, size); |
| 439 | |
| 440 | return addr; |
Mike Frysinger | 81c969a | 2009-07-01 15:42:13 +0000 | [diff] [blame^] | 441 | #else |
| 442 | return NULL; |
| 443 | #endif |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 444 | } |
| 445 | EXPORT_SYMBOL(l1_data_A_sram_alloc); |
| 446 | |
| 447 | int l1_data_A_sram_free(const void *addr) |
| 448 | { |
Mike Frysinger | 81c969a | 2009-07-01 15:42:13 +0000 | [diff] [blame^] | 449 | #if L1_DATA_A_LENGTH != 0 |
Vegard Nossum | 226a6ec | 2008-08-28 17:28:46 +0800 | [diff] [blame] | 450 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 451 | int ret; |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 452 | unsigned int cpu; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 453 | |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 454 | cpu = get_cpu(); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 455 | /* add mutex operation */ |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 456 | spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 457 | |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 458 | ret = _sram_free(addr, &per_cpu(free_l1_data_A_sram_head, cpu), |
| 459 | &per_cpu(used_l1_data_A_sram_head, cpu)); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 460 | |
| 461 | /* add mutex operation */ |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 462 | spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags); |
| 463 | put_cpu(); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 464 | |
| 465 | return ret; |
Mike Frysinger | 81c969a | 2009-07-01 15:42:13 +0000 | [diff] [blame^] | 466 | #else |
| 467 | return -1; |
| 468 | #endif |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 469 | } |
| 470 | EXPORT_SYMBOL(l1_data_A_sram_free); |
| 471 | |
| 472 | void *l1_data_B_sram_alloc(size_t size) |
| 473 | { |
| 474 | #if L1_DATA_B_LENGTH != 0 |
Vegard Nossum | 226a6ec | 2008-08-28 17:28:46 +0800 | [diff] [blame] | 475 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 476 | void *addr; |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 477 | unsigned int cpu; |
| 478 | |
| 479 | cpu = get_cpu(); |
| 480 | /* add mutex operation */ |
| 481 | spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags); |
| 482 | |
| 483 | addr = _sram_alloc(size, &per_cpu(free_l1_data_B_sram_head, cpu), |
| 484 | &per_cpu(used_l1_data_B_sram_head, cpu)); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 485 | |
| 486 | /* add mutex operation */ |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 487 | spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags); |
| 488 | put_cpu(); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 489 | |
| 490 | pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n", |
| 491 | (long unsigned int)addr, size); |
| 492 | |
| 493 | return addr; |
| 494 | #else |
| 495 | return NULL; |
| 496 | #endif |
| 497 | } |
| 498 | EXPORT_SYMBOL(l1_data_B_sram_alloc); |
| 499 | |
| 500 | int l1_data_B_sram_free(const void *addr) |
| 501 | { |
| 502 | #if L1_DATA_B_LENGTH != 0 |
Vegard Nossum | 226a6ec | 2008-08-28 17:28:46 +0800 | [diff] [blame] | 503 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 504 | int ret; |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 505 | unsigned int cpu; |
| 506 | |
| 507 | cpu = get_cpu(); |
| 508 | /* add mutex operation */ |
| 509 | spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags); |
| 510 | |
| 511 | ret = _sram_free(addr, &per_cpu(free_l1_data_B_sram_head, cpu), |
| 512 | &per_cpu(used_l1_data_B_sram_head, cpu)); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 513 | |
| 514 | /* add mutex operation */ |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 515 | spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags); |
| 516 | put_cpu(); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 517 | |
| 518 | return ret; |
| 519 | #else |
| 520 | return -1; |
| 521 | #endif |
| 522 | } |
| 523 | EXPORT_SYMBOL(l1_data_B_sram_free); |
| 524 | |
| 525 | void *l1_data_sram_alloc(size_t size) |
| 526 | { |
| 527 | void *addr = l1_data_A_sram_alloc(size); |
| 528 | |
| 529 | if (!addr) |
| 530 | addr = l1_data_B_sram_alloc(size); |
| 531 | |
| 532 | return addr; |
| 533 | } |
| 534 | EXPORT_SYMBOL(l1_data_sram_alloc); |
| 535 | |
| 536 | void *l1_data_sram_zalloc(size_t size) |
| 537 | { |
| 538 | void *addr = l1_data_sram_alloc(size); |
| 539 | |
| 540 | if (addr) |
| 541 | memset(addr, 0x00, size); |
| 542 | |
| 543 | return addr; |
| 544 | } |
| 545 | EXPORT_SYMBOL(l1_data_sram_zalloc); |
| 546 | |
| 547 | int l1_data_sram_free(const void *addr) |
| 548 | { |
| 549 | int ret; |
| 550 | ret = l1_data_A_sram_free(addr); |
| 551 | if (ret == -1) |
| 552 | ret = l1_data_B_sram_free(addr); |
| 553 | return ret; |
| 554 | } |
| 555 | EXPORT_SYMBOL(l1_data_sram_free); |
| 556 | |
| 557 | void *l1_inst_sram_alloc(size_t size) |
| 558 | { |
Meihui Fan | c5b50df | 2008-04-23 08:55:26 +0800 | [diff] [blame] | 559 | #if L1_CODE_LENGTH != 0 |
Vegard Nossum | 226a6ec | 2008-08-28 17:28:46 +0800 | [diff] [blame] | 560 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 561 | void *addr; |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 562 | unsigned int cpu; |
| 563 | |
| 564 | cpu = get_cpu(); |
| 565 | /* add mutex operation */ |
| 566 | spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags); |
| 567 | |
| 568 | addr = _sram_alloc(size, &per_cpu(free_l1_inst_sram_head, cpu), |
| 569 | &per_cpu(used_l1_inst_sram_head, cpu)); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 570 | |
| 571 | /* add mutex operation */ |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 572 | spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags); |
| 573 | put_cpu(); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 574 | |
| 575 | pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n", |
| 576 | (long unsigned int)addr, size); |
| 577 | |
| 578 | return addr; |
| 579 | #else |
| 580 | return NULL; |
| 581 | #endif |
| 582 | } |
| 583 | EXPORT_SYMBOL(l1_inst_sram_alloc); |
| 584 | |
| 585 | int l1_inst_sram_free(const void *addr) |
| 586 | { |
| 587 | #if L1_CODE_LENGTH != 0 |
Vegard Nossum | 226a6ec | 2008-08-28 17:28:46 +0800 | [diff] [blame] | 588 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 589 | int ret; |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 590 | unsigned int cpu; |
| 591 | |
| 592 | cpu = get_cpu(); |
| 593 | /* add mutex operation */ |
| 594 | spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags); |
| 595 | |
| 596 | ret = _sram_free(addr, &per_cpu(free_l1_inst_sram_head, cpu), |
| 597 | &per_cpu(used_l1_inst_sram_head, cpu)); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 598 | |
| 599 | /* add mutex operation */ |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 600 | spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags); |
| 601 | put_cpu(); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 602 | |
| 603 | return ret; |
| 604 | #else |
| 605 | return -1; |
| 606 | #endif |
| 607 | } |
| 608 | EXPORT_SYMBOL(l1_inst_sram_free); |
| 609 | |
| 610 | /* L1 Scratchpad memory allocate function */ |
| 611 | void *l1sram_alloc(size_t size) |
| 612 | { |
Vegard Nossum | 226a6ec | 2008-08-28 17:28:46 +0800 | [diff] [blame] | 613 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 614 | void *addr; |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 615 | unsigned int cpu; |
| 616 | |
| 617 | cpu = get_cpu(); |
| 618 | /* add mutex operation */ |
| 619 | spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags); |
| 620 | |
| 621 | addr = _sram_alloc(size, &per_cpu(free_l1_ssram_head, cpu), |
| 622 | &per_cpu(used_l1_ssram_head, cpu)); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 623 | |
| 624 | /* add mutex operation */ |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 625 | spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags); |
| 626 | put_cpu(); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 627 | |
| 628 | return addr; |
| 629 | } |
| 630 | |
| 631 | /* L1 Scratchpad memory allocate function */ |
| 632 | void *l1sram_alloc_max(size_t *psize) |
| 633 | { |
Vegard Nossum | 226a6ec | 2008-08-28 17:28:46 +0800 | [diff] [blame] | 634 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 635 | void *addr; |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 636 | unsigned int cpu; |
| 637 | |
| 638 | cpu = get_cpu(); |
| 639 | /* add mutex operation */ |
| 640 | spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags); |
| 641 | |
| 642 | addr = _sram_alloc_max(&per_cpu(free_l1_ssram_head, cpu), |
| 643 | &per_cpu(used_l1_ssram_head, cpu), psize); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 644 | |
| 645 | /* add mutex operation */ |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 646 | spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags); |
| 647 | put_cpu(); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 648 | |
| 649 | return addr; |
| 650 | } |
| 651 | |
| 652 | /* L1 Scratchpad memory free function */ |
| 653 | int l1sram_free(const void *addr) |
| 654 | { |
Vegard Nossum | 226a6ec | 2008-08-28 17:28:46 +0800 | [diff] [blame] | 655 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 656 | int ret; |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 657 | unsigned int cpu; |
| 658 | |
| 659 | cpu = get_cpu(); |
| 660 | /* add mutex operation */ |
| 661 | spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags); |
| 662 | |
| 663 | ret = _sram_free(addr, &per_cpu(free_l1_ssram_head, cpu), |
| 664 | &per_cpu(used_l1_ssram_head, cpu)); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 665 | |
| 666 | /* add mutex operation */ |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 667 | spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags); |
| 668 | put_cpu(); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 669 | |
| 670 | return ret; |
| 671 | } |
| 672 | |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 673 | void *l2_sram_alloc(size_t size) |
| 674 | { |
Mike Frysinger | 07aa7be | 2008-08-13 16:16:11 +0800 | [diff] [blame] | 675 | #if L2_LENGTH != 0 |
Vegard Nossum | 226a6ec | 2008-08-28 17:28:46 +0800 | [diff] [blame] | 676 | unsigned long flags; |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 677 | void *addr; |
| 678 | |
| 679 | /* add mutex operation */ |
| 680 | spin_lock_irqsave(&l2_sram_lock, flags); |
| 681 | |
| 682 | addr = _sram_alloc(size, &free_l2_sram_head, |
| 683 | &used_l2_sram_head); |
| 684 | |
| 685 | /* add mutex operation */ |
| 686 | spin_unlock_irqrestore(&l2_sram_lock, flags); |
| 687 | |
| 688 | pr_debug("Allocated address in l2_sram_alloc is 0x%lx+0x%lx\n", |
| 689 | (long unsigned int)addr, size); |
| 690 | |
| 691 | return addr; |
| 692 | #else |
| 693 | return NULL; |
| 694 | #endif |
| 695 | } |
| 696 | EXPORT_SYMBOL(l2_sram_alloc); |
| 697 | |
| 698 | void *l2_sram_zalloc(size_t size) |
| 699 | { |
| 700 | void *addr = l2_sram_alloc(size); |
| 701 | |
| 702 | if (addr) |
| 703 | memset(addr, 0x00, size); |
| 704 | |
| 705 | return addr; |
| 706 | } |
| 707 | EXPORT_SYMBOL(l2_sram_zalloc); |
| 708 | |
| 709 | int l2_sram_free(const void *addr) |
| 710 | { |
Mike Frysinger | 07aa7be | 2008-08-13 16:16:11 +0800 | [diff] [blame] | 711 | #if L2_LENGTH != 0 |
Vegard Nossum | 226a6ec | 2008-08-28 17:28:46 +0800 | [diff] [blame] | 712 | unsigned long flags; |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 713 | int ret; |
| 714 | |
| 715 | /* add mutex operation */ |
| 716 | spin_lock_irqsave(&l2_sram_lock, flags); |
| 717 | |
| 718 | ret = _sram_free(addr, &free_l2_sram_head, |
| 719 | &used_l2_sram_head); |
| 720 | |
| 721 | /* add mutex operation */ |
| 722 | spin_unlock_irqrestore(&l2_sram_lock, flags); |
| 723 | |
| 724 | return ret; |
| 725 | #else |
| 726 | return -1; |
| 727 | #endif |
| 728 | } |
| 729 | EXPORT_SYMBOL(l2_sram_free); |
| 730 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 731 | int sram_free_with_lsl(const void *addr) |
| 732 | { |
| 733 | struct sram_list_struct *lsl, **tmp; |
| 734 | struct mm_struct *mm = current->mm; |
| 735 | |
| 736 | for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next) |
| 737 | if ((*tmp)->addr == addr) |
| 738 | goto found; |
| 739 | return -1; |
| 740 | found: |
| 741 | lsl = *tmp; |
| 742 | sram_free(addr); |
| 743 | *tmp = lsl->next; |
| 744 | kfree(lsl); |
| 745 | |
| 746 | return 0; |
| 747 | } |
| 748 | EXPORT_SYMBOL(sram_free_with_lsl); |
| 749 | |
Mike Frysinger | f1db88d | 2009-06-02 08:46:35 +0000 | [diff] [blame] | 750 | /* Allocate memory and keep in L1 SRAM List (lsl) so that the resources are |
| 751 | * tracked. These are designed for userspace so that when a process exits, |
| 752 | * we can safely reap their resources. |
| 753 | */ |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 754 | void *sram_alloc_with_lsl(size_t size, unsigned long flags) |
| 755 | { |
| 756 | void *addr = NULL; |
| 757 | struct sram_list_struct *lsl = NULL; |
| 758 | struct mm_struct *mm = current->mm; |
| 759 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 760 | lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 761 | if (!lsl) |
| 762 | return NULL; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 763 | |
| 764 | if (flags & L1_INST_SRAM) |
| 765 | addr = l1_inst_sram_alloc(size); |
| 766 | |
| 767 | if (addr == NULL && (flags & L1_DATA_A_SRAM)) |
| 768 | addr = l1_data_A_sram_alloc(size); |
| 769 | |
| 770 | if (addr == NULL && (flags & L1_DATA_B_SRAM)) |
| 771 | addr = l1_data_B_sram_alloc(size); |
| 772 | |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 773 | if (addr == NULL && (flags & L2_SRAM)) |
| 774 | addr = l2_sram_alloc(size); |
| 775 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 776 | if (addr == NULL) { |
| 777 | kfree(lsl); |
| 778 | return NULL; |
| 779 | } |
| 780 | lsl->addr = addr; |
| 781 | lsl->length = size; |
| 782 | lsl->next = mm->context.sram_list; |
| 783 | mm->context.sram_list = lsl; |
| 784 | return addr; |
| 785 | } |
| 786 | EXPORT_SYMBOL(sram_alloc_with_lsl); |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 787 | |
| 788 | #ifdef CONFIG_PROC_FS |
| 789 | /* Once we get a real allocator, we'll throw all of this away. |
| 790 | * Until then, we need some sort of visibility into the L1 alloc. |
| 791 | */ |
Mike Frysinger | 260d5d3 | 2008-07-14 16:34:05 +0800 | [diff] [blame] | 792 | /* Need to keep line of output the same. Currently, that is 44 bytes |
| 793 | * (including newline). |
| 794 | */ |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 795 | static int _sram_proc_read(char *buf, int *len, int count, const char *desc, |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 796 | struct sram_piece *pfree_head, |
| 797 | struct sram_piece *pused_head) |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 798 | { |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 799 | struct sram_piece *pslot; |
| 800 | |
| 801 | if (!pfree_head || !pused_head) |
| 802 | return -1; |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 803 | |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 804 | *len += sprintf(&buf[*len], "--- SRAM %-14s Size PID State \n", desc); |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 805 | |
| 806 | /* search the relevant memory slot */ |
| 807 | pslot = pused_head->next; |
| 808 | |
| 809 | while (pslot != NULL) { |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 810 | *len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n", |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 811 | pslot->paddr, pslot->paddr + pslot->size, |
| 812 | pslot->size, pslot->pid, "ALLOCATED"); |
| 813 | |
| 814 | pslot = pslot->next; |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 815 | } |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 816 | |
| 817 | pslot = pfree_head->next; |
| 818 | |
| 819 | while (pslot != NULL) { |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 820 | *len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n", |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 821 | pslot->paddr, pslot->paddr + pslot->size, |
| 822 | pslot->size, pslot->pid, "FREE"); |
| 823 | |
| 824 | pslot = pslot->next; |
| 825 | } |
| 826 | |
| 827 | return 0; |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 828 | } |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 829 | static int sram_proc_read(char *buf, char **start, off_t offset, int count, |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 830 | int *eof, void *data) |
| 831 | { |
| 832 | int len = 0; |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 833 | unsigned int cpu; |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 834 | |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 835 | for (cpu = 0; cpu < num_possible_cpus(); ++cpu) { |
| 836 | if (_sram_proc_read(buf, &len, count, "Scratchpad", |
| 837 | &per_cpu(free_l1_ssram_head, cpu), &per_cpu(used_l1_ssram_head, cpu))) |
| 838 | goto not_done; |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 839 | #if L1_DATA_A_LENGTH != 0 |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 840 | if (_sram_proc_read(buf, &len, count, "L1 Data A", |
| 841 | &per_cpu(free_l1_data_A_sram_head, cpu), |
| 842 | &per_cpu(used_l1_data_A_sram_head, cpu))) |
| 843 | goto not_done; |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 844 | #endif |
| 845 | #if L1_DATA_B_LENGTH != 0 |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 846 | if (_sram_proc_read(buf, &len, count, "L1 Data B", |
| 847 | &per_cpu(free_l1_data_B_sram_head, cpu), |
| 848 | &per_cpu(used_l1_data_B_sram_head, cpu))) |
| 849 | goto not_done; |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 850 | #endif |
| 851 | #if L1_CODE_LENGTH != 0 |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 852 | if (_sram_proc_read(buf, &len, count, "L1 Instruction", |
| 853 | &per_cpu(free_l1_inst_sram_head, cpu), |
| 854 | &per_cpu(used_l1_inst_sram_head, cpu))) |
| 855 | goto not_done; |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 856 | #endif |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 857 | } |
Mike Frysinger | 07aa7be | 2008-08-13 16:16:11 +0800 | [diff] [blame] | 858 | #if L2_LENGTH != 0 |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 859 | if (_sram_proc_read(buf, &len, count, "L2", &free_l2_sram_head, |
| 860 | &used_l2_sram_head)) |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 861 | goto not_done; |
| 862 | #endif |
Mike Frysinger | 260d5d3 | 2008-07-14 16:34:05 +0800 | [diff] [blame] | 863 | *eof = 1; |
| 864 | not_done: |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 865 | return len; |
| 866 | } |
| 867 | |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 868 | static int __init sram_proc_init(void) |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 869 | { |
| 870 | struct proc_dir_entry *ptr; |
| 871 | ptr = create_proc_entry("sram", S_IFREG | S_IRUGO, NULL); |
| 872 | if (!ptr) { |
| 873 | printk(KERN_WARNING "unable to create /proc/sram\n"); |
| 874 | return -1; |
| 875 | } |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 876 | ptr->read_proc = sram_proc_read; |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 877 | return 0; |
| 878 | } |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 879 | late_initcall(sram_proc_init); |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 880 | #endif |