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