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