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 | 934fe05b | 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 | |
Bob Liu | b2cfc65 | 2012-05-16 18:18:27 +0800 | [diff] [blame] | 189 | #ifdef __ADSPBF60x__ |
| 190 | static irqreturn_t l2_ecc_err(int irq, void *dev_id) |
| 191 | { |
| 192 | int status; |
| 193 | |
Masanari Iida | 744627e9 | 2012-11-05 23:30:40 +0900 | [diff] [blame] | 194 | printk(KERN_ERR "L2 ecc error happened\n"); |
Bob Liu | b2cfc65 | 2012-05-16 18:18:27 +0800 | [diff] [blame] | 195 | status = bfin_read32(L2CTL0_STAT); |
| 196 | if (status & 0x1) |
| 197 | printk(KERN_ERR "Core channel error type:0x%x, addr:0x%x\n", |
| 198 | bfin_read32(L2CTL0_ET0), bfin_read32(L2CTL0_EADDR0)); |
| 199 | if (status & 0x2) |
| 200 | printk(KERN_ERR "System channel error type:0x%x, addr:0x%x\n", |
| 201 | bfin_read32(L2CTL0_ET1), bfin_read32(L2CTL0_EADDR1)); |
| 202 | |
| 203 | status = status >> 8; |
| 204 | if (status) |
| 205 | printk(KERN_ERR "L2 Bank%d error, addr:0x%x\n", |
| 206 | status, bfin_read32(L2CTL0_ERRADDR0 + status)); |
| 207 | |
| 208 | panic("L2 Ecc error"); |
| 209 | return IRQ_HANDLED; |
| 210 | } |
| 211 | #endif |
| 212 | |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 213 | static void __init l2_sram_init(void) |
| 214 | { |
Mike Frysinger | 07aa7be | 2008-08-13 16:16:11 +0800 | [diff] [blame] | 215 | #if L2_LENGTH != 0 |
Bob Liu | b2cfc65 | 2012-05-16 18:18:27 +0800 | [diff] [blame] | 216 | |
| 217 | #ifdef __ADSPBF60x__ |
| 218 | int ret; |
| 219 | |
| 220 | ret = request_irq(IRQ_L2CTL0_ECC_ERR, l2_ecc_err, 0, "l2-ecc-err", |
| 221 | NULL); |
| 222 | if (unlikely(ret < 0)) { |
| 223 | printk(KERN_INFO "Fail to request l2 ecc error interrupt"); |
| 224 | return; |
| 225 | } |
| 226 | #endif |
| 227 | |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 228 | free_l2_sram_head.next = |
| 229 | kmem_cache_alloc(sram_piece_cache, GFP_KERNEL); |
| 230 | if (!free_l2_sram_head.next) { |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 231 | printk(KERN_INFO "Fail to initialize L2 SRAM.\n"); |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 232 | return; |
| 233 | } |
| 234 | |
Jie Zhang | b2c2f30 | 2008-10-28 15:57:49 +0800 | [diff] [blame] | 235 | free_l2_sram_head.next->paddr = |
| 236 | (void *)L2_START + (_ebss_l2 - _stext_l2); |
| 237 | free_l2_sram_head.next->size = |
| 238 | L2_LENGTH - (_ebss_l2 - _stext_l2); |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 239 | free_l2_sram_head.next->pid = 0; |
| 240 | free_l2_sram_head.next->next = NULL; |
| 241 | |
| 242 | used_l2_sram_head.next = NULL; |
| 243 | |
| 244 | printk(KERN_INFO "Blackfin L2 SRAM: %d KB (%d KB free)\n", |
| 245 | L2_LENGTH >> 10, |
| 246 | free_l2_sram_head.next->size >> 10); |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 247 | |
| 248 | /* mutex initialize */ |
| 249 | spin_lock_init(&l2_sram_lock); |
Mike Frysinger | 81c969a | 2009-07-01 15:42:13 +0000 | [diff] [blame] | 250 | #endif |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 251 | } |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 252 | |
Graf Yang | c72aa07 | 2009-05-25 04:44:00 +0000 | [diff] [blame] | 253 | static int __init bfin_sram_init(void) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 254 | { |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 255 | sram_piece_cache = kmem_cache_create("sram_piece_cache", |
| 256 | sizeof(struct sram_piece), |
| 257 | 0, SLAB_PANIC, NULL); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 258 | |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 259 | l1sram_init(); |
| 260 | l1_data_sram_init(); |
| 261 | l1_inst_sram_init(); |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 262 | l2_sram_init(); |
Graf Yang | c72aa07 | 2009-05-25 04:44:00 +0000 | [diff] [blame] | 263 | |
| 264 | return 0; |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 265 | } |
Graf Yang | c72aa07 | 2009-05-25 04:44:00 +0000 | [diff] [blame] | 266 | pure_initcall(bfin_sram_init); |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 267 | |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 268 | /* SRAM allocate function */ |
| 269 | static void *_sram_alloc(size_t size, struct sram_piece *pfree_head, |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 270 | struct sram_piece *pused_head) |
| 271 | { |
| 272 | struct sram_piece *pslot, *plast, *pavail; |
| 273 | |
| 274 | if (size <= 0 || !pfree_head || !pused_head) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 275 | return NULL; |
| 276 | |
| 277 | /* Align the size */ |
| 278 | size = (size + 3) & ~3; |
| 279 | |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 280 | pslot = pfree_head->next; |
| 281 | plast = pfree_head; |
| 282 | |
| 283 | /* search an available piece slot */ |
| 284 | while (pslot != NULL && size > pslot->size) { |
| 285 | plast = pslot; |
| 286 | pslot = pslot->next; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 287 | } |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 288 | |
| 289 | if (!pslot) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 290 | return NULL; |
| 291 | |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 292 | if (pslot->size == size) { |
| 293 | plast->next = pslot->next; |
| 294 | pavail = pslot; |
| 295 | } else { |
Mike Frysinger | eb5400b | 2010-05-11 04:43:19 +0000 | [diff] [blame] | 296 | /* use atomic so our L1 allocator can be used atomically */ |
| 297 | pavail = kmem_cache_alloc(sram_piece_cache, GFP_ATOMIC); |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 298 | |
| 299 | if (!pavail) |
| 300 | return NULL; |
| 301 | |
| 302 | pavail->paddr = pslot->paddr; |
| 303 | pavail->size = size; |
| 304 | pslot->paddr += size; |
| 305 | pslot->size -= size; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 308 | pavail->pid = current->pid; |
| 309 | |
| 310 | pslot = pused_head->next; |
| 311 | plast = pused_head; |
| 312 | |
| 313 | /* insert new piece into used piece list !!! */ |
| 314 | while (pslot != NULL && pavail->paddr < pslot->paddr) { |
| 315 | plast = pslot; |
| 316 | pslot = pslot->next; |
| 317 | } |
| 318 | |
| 319 | pavail->next = pslot; |
| 320 | plast->next = pavail; |
| 321 | |
| 322 | return pavail->paddr; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /* Allocate the largest available block. */ |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 326 | static void *_sram_alloc_max(struct sram_piece *pfree_head, |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 327 | struct sram_piece *pused_head, |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 328 | unsigned long *psize) |
| 329 | { |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 330 | struct sram_piece *pslot, *pmax; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 331 | |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 332 | if (!pfree_head || !pused_head) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 333 | return NULL; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 334 | |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 335 | pmax = pslot = pfree_head->next; |
| 336 | |
| 337 | /* search an available piece slot */ |
| 338 | while (pslot != NULL) { |
| 339 | if (pslot->size > pmax->size) |
| 340 | pmax = pslot; |
| 341 | pslot = pslot->next; |
| 342 | } |
| 343 | |
| 344 | if (!pmax) |
| 345 | return NULL; |
| 346 | |
| 347 | *psize = pmax->size; |
| 348 | |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 349 | return _sram_alloc(*psize, pfree_head, pused_head); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 352 | /* SRAM free function */ |
| 353 | static int _sram_free(const void *addr, |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 354 | struct sram_piece *pfree_head, |
| 355 | struct sram_piece *pused_head) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 356 | { |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 357 | struct sram_piece *pslot, *plast, *pavail; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 358 | |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 359 | if (!pfree_head || !pused_head) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 360 | return -1; |
| 361 | |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 362 | /* search the relevant memory slot */ |
| 363 | pslot = pused_head->next; |
| 364 | plast = pused_head; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 365 | |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 366 | /* search an available piece slot */ |
| 367 | while (pslot != NULL && pslot->paddr != addr) { |
| 368 | plast = pslot; |
| 369 | pslot = pslot->next; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 372 | if (!pslot) |
| 373 | return -1; |
| 374 | |
| 375 | plast->next = pslot->next; |
| 376 | pavail = pslot; |
| 377 | pavail->pid = 0; |
| 378 | |
| 379 | /* insert free pieces back to the free list */ |
| 380 | pslot = pfree_head->next; |
| 381 | plast = pfree_head; |
| 382 | |
| 383 | while (pslot != NULL && addr > pslot->paddr) { |
| 384 | plast = pslot; |
| 385 | pslot = pslot->next; |
| 386 | } |
| 387 | |
| 388 | if (plast != pfree_head && plast->paddr + plast->size == pavail->paddr) { |
| 389 | plast->size += pavail->size; |
| 390 | kmem_cache_free(sram_piece_cache, pavail); |
| 391 | } else { |
Sonic Zhang | 225f7e1 | 2008-08-25 18:00:45 +0800 | [diff] [blame] | 392 | pavail->next = plast->next; |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 393 | plast->next = pavail; |
| 394 | plast = pavail; |
| 395 | } |
| 396 | |
| 397 | if (pslot && plast->paddr + plast->size == pslot->paddr) { |
| 398 | plast->size += pslot->size; |
| 399 | plast->next = pslot->next; |
| 400 | kmem_cache_free(sram_piece_cache, pslot); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | int sram_free(const void *addr) |
| 407 | { |
Robin Getz | 5e95320 | 2008-10-08 17:22:49 +0800 | [diff] [blame] | 408 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 409 | #if L1_CODE_LENGTH != 0 |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 410 | if (addr >= (void *)get_l1_code_start() |
| 411 | && addr < (void *)(get_l1_code_start() + L1_CODE_LENGTH)) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 412 | return l1_inst_sram_free(addr); |
Robin Getz | 5e95320 | 2008-10-08 17:22:49 +0800 | [diff] [blame] | 413 | else |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 414 | #endif |
| 415 | #if L1_DATA_A_LENGTH != 0 |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 416 | if (addr >= (void *)get_l1_data_a_start() |
| 417 | && addr < (void *)(get_l1_data_a_start() + L1_DATA_A_LENGTH)) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 418 | return l1_data_A_sram_free(addr); |
Robin Getz | 5e95320 | 2008-10-08 17:22:49 +0800 | [diff] [blame] | 419 | else |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 420 | #endif |
| 421 | #if L1_DATA_B_LENGTH != 0 |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 422 | if (addr >= (void *)get_l1_data_b_start() |
| 423 | && addr < (void *)(get_l1_data_b_start() + L1_DATA_B_LENGTH)) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 424 | return l1_data_B_sram_free(addr); |
Robin Getz | 5e95320 | 2008-10-08 17:22:49 +0800 | [diff] [blame] | 425 | else |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 426 | #endif |
Mike Frysinger | 07aa7be | 2008-08-13 16:16:11 +0800 | [diff] [blame] | 427 | #if L2_LENGTH != 0 |
Robin Getz | 5e95320 | 2008-10-08 17:22:49 +0800 | [diff] [blame] | 428 | if (addr >= (void *)L2_START |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 429 | && addr < (void *)(L2_START + L2_LENGTH)) |
| 430 | return l2_sram_free(addr); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 431 | else |
Robin Getz | 5e95320 | 2008-10-08 17:22:49 +0800 | [diff] [blame] | 432 | #endif |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 433 | return -1; |
| 434 | } |
| 435 | EXPORT_SYMBOL(sram_free); |
| 436 | |
| 437 | void *l1_data_A_sram_alloc(size_t size) |
| 438 | { |
Mike Frysinger | 81c969a | 2009-07-01 15:42:13 +0000 | [diff] [blame] | 439 | #if L1_DATA_A_LENGTH != 0 |
Vegard Nossum | 226a6ec | 2008-08-28 17:28:46 +0800 | [diff] [blame] | 440 | unsigned long flags; |
Mike Frysinger | 81c969a | 2009-07-01 15:42:13 +0000 | [diff] [blame] | 441 | void *addr; |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 442 | unsigned int cpu; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 443 | |
Yi Li | 54536c5 | 2009-12-30 04:04:07 +0000 | [diff] [blame] | 444 | cpu = smp_processor_id(); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 445 | /* add mutex operation */ |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 446 | spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 447 | |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 448 | addr = _sram_alloc(size, &per_cpu(free_l1_data_A_sram_head, cpu), |
| 449 | &per_cpu(used_l1_data_A_sram_head, cpu)); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 450 | |
| 451 | /* add mutex operation */ |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 452 | spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 453 | |
| 454 | pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n", |
| 455 | (long unsigned int)addr, size); |
| 456 | |
| 457 | return addr; |
Mike Frysinger | 81c969a | 2009-07-01 15:42:13 +0000 | [diff] [blame] | 458 | #else |
| 459 | return NULL; |
| 460 | #endif |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 461 | } |
| 462 | EXPORT_SYMBOL(l1_data_A_sram_alloc); |
| 463 | |
| 464 | int l1_data_A_sram_free(const void *addr) |
| 465 | { |
Mike Frysinger | 81c969a | 2009-07-01 15:42:13 +0000 | [diff] [blame] | 466 | #if L1_DATA_A_LENGTH != 0 |
Vegard Nossum | 226a6ec | 2008-08-28 17:28:46 +0800 | [diff] [blame] | 467 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 468 | int ret; |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 469 | unsigned int cpu; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 470 | |
Yi Li | 54536c5 | 2009-12-30 04:04:07 +0000 | [diff] [blame] | 471 | cpu = smp_processor_id(); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 472 | /* add mutex operation */ |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 473 | spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 474 | |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 475 | ret = _sram_free(addr, &per_cpu(free_l1_data_A_sram_head, cpu), |
| 476 | &per_cpu(used_l1_data_A_sram_head, cpu)); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 477 | |
| 478 | /* add mutex operation */ |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 479 | spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 480 | |
| 481 | return ret; |
Mike Frysinger | 81c969a | 2009-07-01 15:42:13 +0000 | [diff] [blame] | 482 | #else |
| 483 | return -1; |
| 484 | #endif |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 485 | } |
| 486 | EXPORT_SYMBOL(l1_data_A_sram_free); |
| 487 | |
| 488 | void *l1_data_B_sram_alloc(size_t size) |
| 489 | { |
| 490 | #if L1_DATA_B_LENGTH != 0 |
Vegard Nossum | 226a6ec | 2008-08-28 17:28:46 +0800 | [diff] [blame] | 491 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 492 | void *addr; |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 493 | unsigned int cpu; |
| 494 | |
Yi Li | 54536c5 | 2009-12-30 04:04:07 +0000 | [diff] [blame] | 495 | cpu = smp_processor_id(); |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 496 | /* add mutex operation */ |
| 497 | spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags); |
| 498 | |
| 499 | addr = _sram_alloc(size, &per_cpu(free_l1_data_B_sram_head, cpu), |
| 500 | &per_cpu(used_l1_data_B_sram_head, cpu)); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 501 | |
| 502 | /* add mutex operation */ |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 503 | spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 504 | |
| 505 | pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n", |
| 506 | (long unsigned int)addr, size); |
| 507 | |
| 508 | return addr; |
| 509 | #else |
| 510 | return NULL; |
| 511 | #endif |
| 512 | } |
| 513 | EXPORT_SYMBOL(l1_data_B_sram_alloc); |
| 514 | |
| 515 | int l1_data_B_sram_free(const void *addr) |
| 516 | { |
| 517 | #if L1_DATA_B_LENGTH != 0 |
Vegard Nossum | 226a6ec | 2008-08-28 17:28:46 +0800 | [diff] [blame] | 518 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 519 | int ret; |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 520 | unsigned int cpu; |
| 521 | |
Yi Li | 54536c5 | 2009-12-30 04:04:07 +0000 | [diff] [blame] | 522 | cpu = smp_processor_id(); |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 523 | /* add mutex operation */ |
| 524 | spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags); |
| 525 | |
| 526 | ret = _sram_free(addr, &per_cpu(free_l1_data_B_sram_head, cpu), |
| 527 | &per_cpu(used_l1_data_B_sram_head, cpu)); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 528 | |
| 529 | /* add mutex operation */ |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 530 | spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 531 | |
| 532 | return ret; |
| 533 | #else |
| 534 | return -1; |
| 535 | #endif |
| 536 | } |
| 537 | EXPORT_SYMBOL(l1_data_B_sram_free); |
| 538 | |
| 539 | void *l1_data_sram_alloc(size_t size) |
| 540 | { |
| 541 | void *addr = l1_data_A_sram_alloc(size); |
| 542 | |
| 543 | if (!addr) |
| 544 | addr = l1_data_B_sram_alloc(size); |
| 545 | |
| 546 | return addr; |
| 547 | } |
| 548 | EXPORT_SYMBOL(l1_data_sram_alloc); |
| 549 | |
| 550 | void *l1_data_sram_zalloc(size_t size) |
| 551 | { |
| 552 | void *addr = l1_data_sram_alloc(size); |
| 553 | |
| 554 | if (addr) |
| 555 | memset(addr, 0x00, size); |
| 556 | |
| 557 | return addr; |
| 558 | } |
| 559 | EXPORT_SYMBOL(l1_data_sram_zalloc); |
| 560 | |
| 561 | int l1_data_sram_free(const void *addr) |
| 562 | { |
| 563 | int ret; |
| 564 | ret = l1_data_A_sram_free(addr); |
| 565 | if (ret == -1) |
| 566 | ret = l1_data_B_sram_free(addr); |
| 567 | return ret; |
| 568 | } |
| 569 | EXPORT_SYMBOL(l1_data_sram_free); |
| 570 | |
| 571 | void *l1_inst_sram_alloc(size_t size) |
| 572 | { |
Meihui Fan | c5b50df | 2008-04-23 08:55:26 +0800 | [diff] [blame] | 573 | #if L1_CODE_LENGTH != 0 |
Vegard Nossum | 226a6ec | 2008-08-28 17:28:46 +0800 | [diff] [blame] | 574 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 575 | void *addr; |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 576 | unsigned int cpu; |
| 577 | |
Yi Li | 54536c5 | 2009-12-30 04:04:07 +0000 | [diff] [blame] | 578 | cpu = smp_processor_id(); |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 579 | /* add mutex operation */ |
| 580 | spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags); |
| 581 | |
| 582 | addr = _sram_alloc(size, &per_cpu(free_l1_inst_sram_head, cpu), |
| 583 | &per_cpu(used_l1_inst_sram_head, cpu)); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 584 | |
| 585 | /* add mutex operation */ |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 586 | spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 587 | |
| 588 | pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n", |
| 589 | (long unsigned int)addr, size); |
| 590 | |
| 591 | return addr; |
| 592 | #else |
| 593 | return NULL; |
| 594 | #endif |
| 595 | } |
| 596 | EXPORT_SYMBOL(l1_inst_sram_alloc); |
| 597 | |
| 598 | int l1_inst_sram_free(const void *addr) |
| 599 | { |
| 600 | #if L1_CODE_LENGTH != 0 |
Vegard Nossum | 226a6ec | 2008-08-28 17:28:46 +0800 | [diff] [blame] | 601 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 602 | int ret; |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 603 | unsigned int cpu; |
| 604 | |
Yi Li | 54536c5 | 2009-12-30 04:04:07 +0000 | [diff] [blame] | 605 | cpu = smp_processor_id(); |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 606 | /* add mutex operation */ |
| 607 | spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags); |
| 608 | |
| 609 | ret = _sram_free(addr, &per_cpu(free_l1_inst_sram_head, cpu), |
| 610 | &per_cpu(used_l1_inst_sram_head, cpu)); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 611 | |
| 612 | /* add mutex operation */ |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 613 | spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 614 | |
| 615 | return ret; |
| 616 | #else |
| 617 | return -1; |
| 618 | #endif |
| 619 | } |
| 620 | EXPORT_SYMBOL(l1_inst_sram_free); |
| 621 | |
| 622 | /* L1 Scratchpad memory allocate function */ |
| 623 | void *l1sram_alloc(size_t size) |
| 624 | { |
Vegard Nossum | 226a6ec | 2008-08-28 17:28:46 +0800 | [diff] [blame] | 625 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 626 | void *addr; |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 627 | unsigned int cpu; |
| 628 | |
Yi Li | 54536c5 | 2009-12-30 04:04:07 +0000 | [diff] [blame] | 629 | cpu = smp_processor_id(); |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 630 | /* add mutex operation */ |
| 631 | spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags); |
| 632 | |
| 633 | addr = _sram_alloc(size, &per_cpu(free_l1_ssram_head, cpu), |
| 634 | &per_cpu(used_l1_ssram_head, cpu)); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 635 | |
| 636 | /* add mutex operation */ |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 637 | spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 638 | |
| 639 | return addr; |
| 640 | } |
| 641 | |
| 642 | /* L1 Scratchpad memory allocate function */ |
| 643 | void *l1sram_alloc_max(size_t *psize) |
| 644 | { |
Vegard Nossum | 226a6ec | 2008-08-28 17:28:46 +0800 | [diff] [blame] | 645 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 646 | void *addr; |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 647 | unsigned int cpu; |
| 648 | |
Yi Li | 54536c5 | 2009-12-30 04:04:07 +0000 | [diff] [blame] | 649 | cpu = smp_processor_id(); |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 650 | /* add mutex operation */ |
| 651 | spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags); |
| 652 | |
| 653 | addr = _sram_alloc_max(&per_cpu(free_l1_ssram_head, cpu), |
| 654 | &per_cpu(used_l1_ssram_head, cpu), psize); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 655 | |
| 656 | /* add mutex operation */ |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 657 | spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 658 | |
| 659 | return addr; |
| 660 | } |
| 661 | |
| 662 | /* L1 Scratchpad memory free function */ |
| 663 | int l1sram_free(const void *addr) |
| 664 | { |
Vegard Nossum | 226a6ec | 2008-08-28 17:28:46 +0800 | [diff] [blame] | 665 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 666 | int ret; |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 667 | unsigned int cpu; |
| 668 | |
Yi Li | 54536c5 | 2009-12-30 04:04:07 +0000 | [diff] [blame] | 669 | cpu = smp_processor_id(); |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 670 | /* add mutex operation */ |
| 671 | spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags); |
| 672 | |
| 673 | ret = _sram_free(addr, &per_cpu(free_l1_ssram_head, cpu), |
| 674 | &per_cpu(used_l1_ssram_head, cpu)); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 675 | |
| 676 | /* add mutex operation */ |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 677 | spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 678 | |
| 679 | return ret; |
| 680 | } |
| 681 | |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 682 | void *l2_sram_alloc(size_t size) |
| 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 | void *addr; |
| 687 | |
| 688 | /* add mutex operation */ |
| 689 | spin_lock_irqsave(&l2_sram_lock, flags); |
| 690 | |
| 691 | addr = _sram_alloc(size, &free_l2_sram_head, |
| 692 | &used_l2_sram_head); |
| 693 | |
| 694 | /* add mutex operation */ |
| 695 | spin_unlock_irqrestore(&l2_sram_lock, flags); |
| 696 | |
| 697 | pr_debug("Allocated address in l2_sram_alloc is 0x%lx+0x%lx\n", |
| 698 | (long unsigned int)addr, size); |
| 699 | |
| 700 | return addr; |
| 701 | #else |
| 702 | return NULL; |
| 703 | #endif |
| 704 | } |
| 705 | EXPORT_SYMBOL(l2_sram_alloc); |
| 706 | |
| 707 | void *l2_sram_zalloc(size_t size) |
| 708 | { |
| 709 | void *addr = l2_sram_alloc(size); |
| 710 | |
| 711 | if (addr) |
| 712 | memset(addr, 0x00, size); |
| 713 | |
| 714 | return addr; |
| 715 | } |
| 716 | EXPORT_SYMBOL(l2_sram_zalloc); |
| 717 | |
| 718 | int l2_sram_free(const void *addr) |
| 719 | { |
Mike Frysinger | 07aa7be | 2008-08-13 16:16:11 +0800 | [diff] [blame] | 720 | #if L2_LENGTH != 0 |
Vegard Nossum | 226a6ec | 2008-08-28 17:28:46 +0800 | [diff] [blame] | 721 | unsigned long flags; |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 722 | int ret; |
| 723 | |
| 724 | /* add mutex operation */ |
| 725 | spin_lock_irqsave(&l2_sram_lock, flags); |
| 726 | |
| 727 | ret = _sram_free(addr, &free_l2_sram_head, |
| 728 | &used_l2_sram_head); |
| 729 | |
| 730 | /* add mutex operation */ |
| 731 | spin_unlock_irqrestore(&l2_sram_lock, flags); |
| 732 | |
| 733 | return ret; |
| 734 | #else |
| 735 | return -1; |
| 736 | #endif |
| 737 | } |
| 738 | EXPORT_SYMBOL(l2_sram_free); |
| 739 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 740 | int sram_free_with_lsl(const void *addr) |
| 741 | { |
| 742 | struct sram_list_struct *lsl, **tmp; |
| 743 | struct mm_struct *mm = current->mm; |
Mike Frysinger | 25f3ff2 | 2010-12-06 21:12:23 +0000 | [diff] [blame] | 744 | int ret = -1; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 745 | |
| 746 | for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next) |
Mike Frysinger | 25f3ff2 | 2010-12-06 21:12:23 +0000 | [diff] [blame] | 747 | if ((*tmp)->addr == addr) { |
| 748 | lsl = *tmp; |
| 749 | ret = sram_free(addr); |
| 750 | *tmp = lsl->next; |
| 751 | kfree(lsl); |
| 752 | break; |
| 753 | } |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 754 | |
Mike Frysinger | 25f3ff2 | 2010-12-06 21:12:23 +0000 | [diff] [blame] | 755 | return ret; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 756 | } |
| 757 | EXPORT_SYMBOL(sram_free_with_lsl); |
| 758 | |
Mike Frysinger | f1db88d | 2009-06-02 08:46:35 +0000 | [diff] [blame] | 759 | /* Allocate memory and keep in L1 SRAM List (lsl) so that the resources are |
| 760 | * tracked. These are designed for userspace so that when a process exits, |
| 761 | * we can safely reap their resources. |
| 762 | */ |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 763 | void *sram_alloc_with_lsl(size_t size, unsigned long flags) |
| 764 | { |
| 765 | void *addr = NULL; |
| 766 | struct sram_list_struct *lsl = NULL; |
| 767 | struct mm_struct *mm = current->mm; |
| 768 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 769 | lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 770 | if (!lsl) |
| 771 | return NULL; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 772 | |
| 773 | if (flags & L1_INST_SRAM) |
| 774 | addr = l1_inst_sram_alloc(size); |
| 775 | |
| 776 | if (addr == NULL && (flags & L1_DATA_A_SRAM)) |
| 777 | addr = l1_data_A_sram_alloc(size); |
| 778 | |
| 779 | if (addr == NULL && (flags & L1_DATA_B_SRAM)) |
| 780 | addr = l1_data_B_sram_alloc(size); |
| 781 | |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 782 | if (addr == NULL && (flags & L2_SRAM)) |
| 783 | addr = l2_sram_alloc(size); |
| 784 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 785 | if (addr == NULL) { |
| 786 | kfree(lsl); |
| 787 | return NULL; |
| 788 | } |
| 789 | lsl->addr = addr; |
| 790 | lsl->length = size; |
| 791 | lsl->next = mm->context.sram_list; |
| 792 | mm->context.sram_list = lsl; |
| 793 | return addr; |
| 794 | } |
| 795 | EXPORT_SYMBOL(sram_alloc_with_lsl); |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 796 | |
| 797 | #ifdef CONFIG_PROC_FS |
| 798 | /* Once we get a real allocator, we'll throw all of this away. |
| 799 | * Until then, we need some sort of visibility into the L1 alloc. |
| 800 | */ |
Mike Frysinger | 260d5d3 | 2008-07-14 16:34:05 +0800 | [diff] [blame] | 801 | /* Need to keep line of output the same. Currently, that is 44 bytes |
| 802 | * (including newline). |
| 803 | */ |
Alexey Dobriyan | 934fe05b | 2011-05-14 19:51:54 +0300 | [diff] [blame] | 804 | static int _sram_proc_show(struct seq_file *m, const char *desc, |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 805 | struct sram_piece *pfree_head, |
| 806 | struct sram_piece *pused_head) |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 807 | { |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 808 | struct sram_piece *pslot; |
| 809 | |
| 810 | if (!pfree_head || !pused_head) |
| 811 | return -1; |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 812 | |
Alexey Dobriyan | 934fe05b | 2011-05-14 19:51:54 +0300 | [diff] [blame] | 813 | seq_printf(m, "--- SRAM %-14s Size PID State \n", desc); |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 814 | |
| 815 | /* search the relevant memory slot */ |
| 816 | pslot = pused_head->next; |
| 817 | |
| 818 | while (pslot != NULL) { |
Alexey Dobriyan | 934fe05b | 2011-05-14 19:51:54 +0300 | [diff] [blame] | 819 | seq_printf(m, "%p-%p %10i %5i %-10s\n", |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 820 | pslot->paddr, pslot->paddr + pslot->size, |
| 821 | pslot->size, pslot->pid, "ALLOCATED"); |
| 822 | |
| 823 | pslot = pslot->next; |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 824 | } |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 825 | |
| 826 | pslot = pfree_head->next; |
| 827 | |
| 828 | while (pslot != NULL) { |
Alexey Dobriyan | 934fe05b | 2011-05-14 19:51:54 +0300 | [diff] [blame] | 829 | seq_printf(m, "%p-%p %10i %5i %-10s\n", |
Sonic Zhang | 5d481f4 | 2008-07-19 14:51:31 +0800 | [diff] [blame] | 830 | pslot->paddr, pslot->paddr + pslot->size, |
| 831 | pslot->size, pslot->pid, "FREE"); |
| 832 | |
| 833 | pslot = pslot->next; |
| 834 | } |
| 835 | |
| 836 | return 0; |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 837 | } |
Alexey Dobriyan | 934fe05b | 2011-05-14 19:51:54 +0300 | [diff] [blame] | 838 | static int sram_proc_show(struct seq_file *m, void *v) |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 839 | { |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 840 | unsigned int cpu; |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 841 | |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 842 | for (cpu = 0; cpu < num_possible_cpus(); ++cpu) { |
Alexey Dobriyan | 934fe05b | 2011-05-14 19:51:54 +0300 | [diff] [blame] | 843 | if (_sram_proc_show(m, "Scratchpad", |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 844 | &per_cpu(free_l1_ssram_head, cpu), &per_cpu(used_l1_ssram_head, cpu))) |
| 845 | goto not_done; |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 846 | #if L1_DATA_A_LENGTH != 0 |
Alexey Dobriyan | 934fe05b | 2011-05-14 19:51:54 +0300 | [diff] [blame] | 847 | if (_sram_proc_show(m, "L1 Data A", |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 848 | &per_cpu(free_l1_data_A_sram_head, cpu), |
| 849 | &per_cpu(used_l1_data_A_sram_head, cpu))) |
| 850 | goto not_done; |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 851 | #endif |
| 852 | #if L1_DATA_B_LENGTH != 0 |
Alexey Dobriyan | 934fe05b | 2011-05-14 19:51:54 +0300 | [diff] [blame] | 853 | if (_sram_proc_show(m, "L1 Data B", |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 854 | &per_cpu(free_l1_data_B_sram_head, cpu), |
| 855 | &per_cpu(used_l1_data_B_sram_head, cpu))) |
| 856 | goto not_done; |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 857 | #endif |
| 858 | #if L1_CODE_LENGTH != 0 |
Alexey Dobriyan | 934fe05b | 2011-05-14 19:51:54 +0300 | [diff] [blame] | 859 | if (_sram_proc_show(m, "L1 Instruction", |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 860 | &per_cpu(free_l1_inst_sram_head, cpu), |
| 861 | &per_cpu(used_l1_inst_sram_head, cpu))) |
| 862 | goto not_done; |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 863 | #endif |
Graf Yang | 8f65873 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 864 | } |
Mike Frysinger | 07aa7be | 2008-08-13 16:16:11 +0800 | [diff] [blame] | 865 | #if L2_LENGTH != 0 |
Alexey Dobriyan | 934fe05b | 2011-05-14 19:51:54 +0300 | [diff] [blame] | 866 | 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] | 867 | goto not_done; |
| 868 | #endif |
Mike Frysinger | 260d5d3 | 2008-07-14 16:34:05 +0800 | [diff] [blame] | 869 | not_done: |
Alexey Dobriyan | 934fe05b | 2011-05-14 19:51:54 +0300 | [diff] [blame] | 870 | return 0; |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 871 | } |
| 872 | |
Alexey Dobriyan | 934fe05b | 2011-05-14 19:51:54 +0300 | [diff] [blame] | 873 | static int sram_proc_open(struct inode *inode, struct file *file) |
| 874 | { |
| 875 | return single_open(file, sram_proc_show, NULL); |
| 876 | } |
| 877 | |
| 878 | static const struct file_operations sram_proc_ops = { |
| 879 | .open = sram_proc_open, |
| 880 | .read = seq_read, |
| 881 | .llseek = seq_lseek, |
| 882 | .release = single_release, |
| 883 | }; |
| 884 | |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 885 | static int __init sram_proc_init(void) |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 886 | { |
| 887 | struct proc_dir_entry *ptr; |
Alexey Dobriyan | 934fe05b | 2011-05-14 19:51:54 +0300 | [diff] [blame] | 888 | |
| 889 | ptr = proc_create("sram", S_IRUGO, NULL, &sram_proc_ops); |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 890 | if (!ptr) { |
| 891 | printk(KERN_WARNING "unable to create /proc/sram\n"); |
| 892 | return -1; |
| 893 | } |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 894 | return 0; |
| 895 | } |
Sonic Zhang | 262c382 | 2008-07-19 15:42:41 +0800 | [diff] [blame] | 896 | late_initcall(sram_proc_init); |
Mike Frysinger | bc61b4e | 2007-06-14 13:21:08 +0800 | [diff] [blame] | 897 | #endif |