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