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