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