Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 1 | /* |
| 2 | * arch/arm/mm/cache-l2x0.c - L210/L220 cache controller support |
| 3 | * |
| 4 | * Copyright (C) 2007 ARM Limited |
Taniya Das | 38a8c6e | 2012-05-09 20:34:39 +0530 | [diff] [blame] | 5 | * Copyright (c) 2009, 2011-2012, Code Aurora Forum. All rights reserved. |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
Rob Herring | 8c36926 | 2011-08-03 18:12:05 +0100 | [diff] [blame] | 20 | #include <linux/err.h> |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 21 | #include <linux/init.h> |
Catalin Marinas | 0762097 | 2007-07-20 11:42:40 +0100 | [diff] [blame] | 22 | #include <linux/spinlock.h> |
Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 23 | #include <linux/io.h> |
Rob Herring | 8c36926 | 2011-08-03 18:12:05 +0100 | [diff] [blame] | 24 | #include <linux/of.h> |
| 25 | #include <linux/of_address.h> |
Mark Rutland | 29081e1 | 2012-11-21 17:02:51 -0500 | [diff] [blame] | 26 | #include <linux/platform_device.h> |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 27 | |
| 28 | #include <asm/cacheflush.h> |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 29 | #include <asm/hardware/cache-l2x0.h> |
Mark Rutland | 29081e1 | 2012-11-21 17:02:51 -0500 | [diff] [blame] | 30 | #include <asm/irq_regs.h> |
| 31 | #include <asm/pmu.h> |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 32 | |
| 33 | #define CACHE_LINE_SIZE 32 |
| 34 | |
| 35 | static void __iomem *l2x0_base; |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 36 | static DEFINE_RAW_SPINLOCK(l2x0_lock); |
Russell King | 3e175ca | 2011-09-18 11:27:30 +0100 | [diff] [blame] | 37 | static u32 l2x0_way_mask; /* Bitmask of active ways */ |
| 38 | static u32 l2x0_size; |
Colin Cross | 74b6cdd | 2011-09-14 15:59:50 -0700 | [diff] [blame] | 39 | static u32 l2x0_cache_id; |
| 40 | static unsigned int l2x0_sets; |
| 41 | static unsigned int l2x0_ways; |
Will Deacon | f154fe9 | 2012-04-20 17:21:08 +0100 | [diff] [blame] | 42 | static unsigned long sync_reg_offset = L2X0_CACHE_SYNC; |
Taniya Das | 38a8c6e | 2012-05-09 20:34:39 +0530 | [diff] [blame] | 43 | static void pl310_save(void); |
Girish Mahadevan | 74934a3 | 2012-10-11 10:44:47 -0600 | [diff] [blame] | 44 | static void pl310_resume(void); |
| 45 | static void l2x0_resume(void); |
Colin Cross | 74b6cdd | 2011-09-14 15:59:50 -0700 | [diff] [blame] | 46 | |
| 47 | static inline bool is_pl310_rev(int rev) |
| 48 | { |
| 49 | return (l2x0_cache_id & |
| 50 | (L2X0_CACHE_ID_PART_MASK | L2X0_CACHE_ID_REV_MASK)) == |
| 51 | (L2X0_CACHE_ID_PART_L310 | rev); |
| 52 | } |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 53 | |
Barry Song | 91c2ebb | 2011-09-30 14:43:12 +0100 | [diff] [blame] | 54 | struct l2x0_regs l2x0_saved_regs; |
| 55 | |
| 56 | struct l2x0_of_data { |
Russell King | 3e175ca | 2011-09-18 11:27:30 +0100 | [diff] [blame] | 57 | void (*setup)(const struct device_node *, u32 *, u32 *); |
Barry Song | 91c2ebb | 2011-09-30 14:43:12 +0100 | [diff] [blame] | 58 | void (*save)(void); |
| 59 | void (*resume)(void); |
| 60 | }; |
| 61 | |
Catalin Marinas | 9a6655e | 2010-08-31 13:05:22 +0100 | [diff] [blame] | 62 | static inline void cache_wait_way(void __iomem *reg, unsigned long mask) |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 63 | { |
Catalin Marinas | 9a6655e | 2010-08-31 13:05:22 +0100 | [diff] [blame] | 64 | /* wait for cache operation by line or way to complete */ |
Catalin Marinas | 6775a55 | 2010-07-28 22:01:25 +0100 | [diff] [blame] | 65 | while (readl_relaxed(reg) & mask) |
Barry Song | 1caf309 | 2011-09-09 10:30:34 +0100 | [diff] [blame] | 66 | cpu_relax(); |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Catalin Marinas | 9a6655e | 2010-08-31 13:05:22 +0100 | [diff] [blame] | 69 | #ifdef CONFIG_CACHE_PL310 |
| 70 | static inline void cache_wait(void __iomem *reg, unsigned long mask) |
| 71 | { |
| 72 | /* cache operations by line are atomic on PL310 */ |
| 73 | } |
| 74 | #else |
| 75 | #define cache_wait cache_wait_way |
| 76 | #endif |
| 77 | |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 78 | static inline void cache_sync(void) |
| 79 | { |
Russell King | 3d10743 | 2009-11-19 11:41:09 +0000 | [diff] [blame] | 80 | void __iomem *base = l2x0_base; |
Srinidhi Kasagar | 885028e | 2011-02-17 07:03:51 +0100 | [diff] [blame] | 81 | |
Will Deacon | f154fe9 | 2012-04-20 17:21:08 +0100 | [diff] [blame] | 82 | writel_relaxed(0, base + sync_reg_offset); |
Russell King | 3d10743 | 2009-11-19 11:41:09 +0000 | [diff] [blame] | 83 | cache_wait(base + L2X0_CACHE_SYNC, 1); |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Santosh Shilimkar | 424d6b1 | 2010-02-04 19:35:06 +0100 | [diff] [blame] | 86 | static inline void l2x0_clean_line(unsigned long addr) |
| 87 | { |
| 88 | void __iomem *base = l2x0_base; |
| 89 | cache_wait(base + L2X0_CLEAN_LINE_PA, 1); |
Catalin Marinas | 6775a55 | 2010-07-28 22:01:25 +0100 | [diff] [blame] | 90 | writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA); |
Santosh Shilimkar | 424d6b1 | 2010-02-04 19:35:06 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static inline void l2x0_inv_line(unsigned long addr) |
| 94 | { |
| 95 | void __iomem *base = l2x0_base; |
| 96 | cache_wait(base + L2X0_INV_LINE_PA, 1); |
Catalin Marinas | 6775a55 | 2010-07-28 22:01:25 +0100 | [diff] [blame] | 97 | writel_relaxed(addr, base + L2X0_INV_LINE_PA); |
Santosh Shilimkar | 424d6b1 | 2010-02-04 19:35:06 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Santosh Shilimkar | 2839e06 | 2011-03-08 06:59:54 +0100 | [diff] [blame] | 100 | #if defined(CONFIG_PL310_ERRATA_588369) || defined(CONFIG_PL310_ERRATA_727915) |
Will Deacon | ab4d536 | 2012-04-20 17:22:11 +0100 | [diff] [blame] | 101 | static inline void debug_writel(unsigned long val) |
| 102 | { |
| 103 | if (outer_cache.set_debug) |
| 104 | outer_cache.set_debug(val); |
| 105 | } |
Santosh Shilimkar | 9e65582 | 2010-02-04 19:42:42 +0100 | [diff] [blame] | 106 | |
Will Deacon | ab4d536 | 2012-04-20 17:22:11 +0100 | [diff] [blame] | 107 | static void pl310_set_debug(unsigned long val) |
Santosh Shilimkar | 2839e06 | 2011-03-08 06:59:54 +0100 | [diff] [blame] | 108 | { |
| 109 | writel_relaxed(val, l2x0_base + L2X0_DEBUG_CTRL); |
| 110 | } |
| 111 | #else |
| 112 | /* Optimised out for non-errata case */ |
| 113 | static inline void debug_writel(unsigned long val) |
| 114 | { |
Santosh Shilimkar | 9e65582 | 2010-02-04 19:42:42 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Will Deacon | ab4d536 | 2012-04-20 17:22:11 +0100 | [diff] [blame] | 117 | #define pl310_set_debug NULL |
Santosh Shilimkar | 2839e06 | 2011-03-08 06:59:54 +0100 | [diff] [blame] | 118 | #endif |
| 119 | |
| 120 | #ifdef CONFIG_PL310_ERRATA_588369 |
Santosh Shilimkar | 9e65582 | 2010-02-04 19:42:42 +0100 | [diff] [blame] | 121 | static inline void l2x0_flush_line(unsigned long addr) |
| 122 | { |
| 123 | void __iomem *base = l2x0_base; |
| 124 | |
| 125 | /* Clean by PA followed by Invalidate by PA */ |
| 126 | cache_wait(base + L2X0_CLEAN_LINE_PA, 1); |
Catalin Marinas | 6775a55 | 2010-07-28 22:01:25 +0100 | [diff] [blame] | 127 | writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA); |
Santosh Shilimkar | 9e65582 | 2010-02-04 19:42:42 +0100 | [diff] [blame] | 128 | cache_wait(base + L2X0_INV_LINE_PA, 1); |
Catalin Marinas | 6775a55 | 2010-07-28 22:01:25 +0100 | [diff] [blame] | 129 | writel_relaxed(addr, base + L2X0_INV_LINE_PA); |
Santosh Shilimkar | 9e65582 | 2010-02-04 19:42:42 +0100 | [diff] [blame] | 130 | } |
| 131 | #else |
| 132 | |
Santosh Shilimkar | 424d6b1 | 2010-02-04 19:35:06 +0100 | [diff] [blame] | 133 | static inline void l2x0_flush_line(unsigned long addr) |
| 134 | { |
| 135 | void __iomem *base = l2x0_base; |
| 136 | cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1); |
Catalin Marinas | 6775a55 | 2010-07-28 22:01:25 +0100 | [diff] [blame] | 137 | writel_relaxed(addr, base + L2X0_CLEAN_INV_LINE_PA); |
Santosh Shilimkar | 424d6b1 | 2010-02-04 19:35:06 +0100 | [diff] [blame] | 138 | } |
Santosh Shilimkar | 9e65582 | 2010-02-04 19:42:42 +0100 | [diff] [blame] | 139 | #endif |
Santosh Shilimkar | 424d6b1 | 2010-02-04 19:35:06 +0100 | [diff] [blame] | 140 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 141 | void l2x0_cache_sync(void) |
Catalin Marinas | 23107c5 | 2010-03-24 16:48:53 +0100 | [diff] [blame] | 142 | { |
| 143 | unsigned long flags; |
| 144 | |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 145 | raw_spin_lock_irqsave(&l2x0_lock, flags); |
Catalin Marinas | 23107c5 | 2010-03-24 16:48:53 +0100 | [diff] [blame] | 146 | cache_sync(); |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 147 | raw_spin_unlock_irqrestore(&l2x0_lock, flags); |
Catalin Marinas | 23107c5 | 2010-03-24 16:48:53 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Colin Cross | 74b6cdd | 2011-09-14 15:59:50 -0700 | [diff] [blame] | 150 | #ifdef CONFIG_PL310_ERRATA_727915 |
| 151 | static void l2x0_for_each_set_way(void __iomem *reg) |
| 152 | { |
| 153 | int set; |
| 154 | int way; |
| 155 | unsigned long flags; |
| 156 | |
| 157 | for (way = 0; way < l2x0_ways; way++) { |
| 158 | raw_spin_lock_irqsave(&l2x0_lock, flags); |
| 159 | for (set = 0; set < l2x0_sets; set++) |
| 160 | writel_relaxed((way << 28) | (set << 5), reg); |
| 161 | cache_sync(); |
| 162 | raw_spin_unlock_irqrestore(&l2x0_lock, flags); |
| 163 | } |
| 164 | } |
| 165 | #endif |
| 166 | |
Will Deacon | 38a8914 | 2011-07-01 14:36:19 +0100 | [diff] [blame] | 167 | static void __l2x0_flush_all(void) |
| 168 | { |
| 169 | debug_writel(0x03); |
| 170 | writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_INV_WAY); |
| 171 | cache_wait_way(l2x0_base + L2X0_CLEAN_INV_WAY, l2x0_way_mask); |
| 172 | cache_sync(); |
| 173 | debug_writel(0x00); |
| 174 | } |
| 175 | |
Thomas Gleixner | 2fd8658 | 2010-07-31 21:05:24 +0530 | [diff] [blame] | 176 | static void l2x0_flush_all(void) |
| 177 | { |
| 178 | unsigned long flags; |
| 179 | |
Colin Cross | 74b6cdd | 2011-09-14 15:59:50 -0700 | [diff] [blame] | 180 | #ifdef CONFIG_PL310_ERRATA_727915 |
| 181 | if (is_pl310_rev(REV_PL310_R2P0)) { |
| 182 | l2x0_for_each_set_way(l2x0_base + L2X0_CLEAN_INV_LINE_IDX); |
| 183 | return; |
| 184 | } |
| 185 | #endif |
| 186 | |
Thomas Gleixner | 2fd8658 | 2010-07-31 21:05:24 +0530 | [diff] [blame] | 187 | /* clean all ways */ |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 188 | raw_spin_lock_irqsave(&l2x0_lock, flags); |
Will Deacon | 38a8914 | 2011-07-01 14:36:19 +0100 | [diff] [blame] | 189 | __l2x0_flush_all(); |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 190 | raw_spin_unlock_irqrestore(&l2x0_lock, flags); |
Thomas Gleixner | 2fd8658 | 2010-07-31 21:05:24 +0530 | [diff] [blame] | 191 | } |
| 192 | |
Santosh Shilimkar | 444457c | 2010-07-11 14:58:41 +0530 | [diff] [blame] | 193 | static void l2x0_clean_all(void) |
| 194 | { |
| 195 | unsigned long flags; |
| 196 | |
Colin Cross | 74b6cdd | 2011-09-14 15:59:50 -0700 | [diff] [blame] | 197 | #ifdef CONFIG_PL310_ERRATA_727915 |
| 198 | if (is_pl310_rev(REV_PL310_R2P0)) { |
| 199 | l2x0_for_each_set_way(l2x0_base + L2X0_CLEAN_LINE_IDX); |
| 200 | return; |
| 201 | } |
| 202 | #endif |
| 203 | |
Santosh Shilimkar | 444457c | 2010-07-11 14:58:41 +0530 | [diff] [blame] | 204 | /* clean all ways */ |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 205 | raw_spin_lock_irqsave(&l2x0_lock, flags); |
Colin Cross | 74b6cdd | 2011-09-14 15:59:50 -0700 | [diff] [blame] | 206 | debug_writel(0x03); |
Santosh Shilimkar | 444457c | 2010-07-11 14:58:41 +0530 | [diff] [blame] | 207 | writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_WAY); |
| 208 | cache_wait_way(l2x0_base + L2X0_CLEAN_WAY, l2x0_way_mask); |
| 209 | cache_sync(); |
Colin Cross | 74b6cdd | 2011-09-14 15:59:50 -0700 | [diff] [blame] | 210 | debug_writel(0x00); |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 211 | raw_spin_unlock_irqrestore(&l2x0_lock, flags); |
Santosh Shilimkar | 444457c | 2010-07-11 14:58:41 +0530 | [diff] [blame] | 212 | } |
| 213 | |
Thomas Gleixner | 2fd8658 | 2010-07-31 21:05:24 +0530 | [diff] [blame] | 214 | static void l2x0_inv_all(void) |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 215 | { |
Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 216 | unsigned long flags; |
| 217 | |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 218 | /* invalidate all ways */ |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 219 | raw_spin_lock_irqsave(&l2x0_lock, flags); |
Thomas Gleixner | 2fd8658 | 2010-07-31 21:05:24 +0530 | [diff] [blame] | 220 | /* Invalidating when L2 is enabled is a nono */ |
| 221 | BUG_ON(readl(l2x0_base + L2X0_CTRL) & 1); |
Catalin Marinas | 6775a55 | 2010-07-28 22:01:25 +0100 | [diff] [blame] | 222 | writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_INV_WAY); |
Catalin Marinas | 9a6655e | 2010-08-31 13:05:22 +0100 | [diff] [blame] | 223 | cache_wait_way(l2x0_base + L2X0_INV_WAY, l2x0_way_mask); |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 224 | cache_sync(); |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 225 | raw_spin_unlock_irqrestore(&l2x0_lock, flags); |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static void l2x0_inv_range(unsigned long start, unsigned long end) |
| 229 | { |
Russell King | 3d10743 | 2009-11-19 11:41:09 +0000 | [diff] [blame] | 230 | void __iomem *base = l2x0_base; |
Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 231 | unsigned long flags; |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 232 | |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 233 | raw_spin_lock_irqsave(&l2x0_lock, flags); |
Rui Sousa | 4f6627a | 2007-09-15 00:56:19 +0100 | [diff] [blame] | 234 | if (start & (CACHE_LINE_SIZE - 1)) { |
| 235 | start &= ~(CACHE_LINE_SIZE - 1); |
Santosh Shilimkar | 9e65582 | 2010-02-04 19:42:42 +0100 | [diff] [blame] | 236 | debug_writel(0x03); |
Santosh Shilimkar | 424d6b1 | 2010-02-04 19:35:06 +0100 | [diff] [blame] | 237 | l2x0_flush_line(start); |
Santosh Shilimkar | 9e65582 | 2010-02-04 19:42:42 +0100 | [diff] [blame] | 238 | debug_writel(0x00); |
Rui Sousa | 4f6627a | 2007-09-15 00:56:19 +0100 | [diff] [blame] | 239 | start += CACHE_LINE_SIZE; |
| 240 | } |
| 241 | |
| 242 | if (end & (CACHE_LINE_SIZE - 1)) { |
| 243 | end &= ~(CACHE_LINE_SIZE - 1); |
Santosh Shilimkar | 9e65582 | 2010-02-04 19:42:42 +0100 | [diff] [blame] | 244 | debug_writel(0x03); |
Santosh Shilimkar | 424d6b1 | 2010-02-04 19:35:06 +0100 | [diff] [blame] | 245 | l2x0_flush_line(end); |
Santosh Shilimkar | 9e65582 | 2010-02-04 19:42:42 +0100 | [diff] [blame] | 246 | debug_writel(0x00); |
Rui Sousa | 4f6627a | 2007-09-15 00:56:19 +0100 | [diff] [blame] | 247 | } |
| 248 | |
Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 249 | while (start < end) { |
| 250 | unsigned long blk_end = start + min(end - start, 4096UL); |
| 251 | |
| 252 | while (start < blk_end) { |
Santosh Shilimkar | 424d6b1 | 2010-02-04 19:35:06 +0100 | [diff] [blame] | 253 | l2x0_inv_line(start); |
Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 254 | start += CACHE_LINE_SIZE; |
| 255 | } |
| 256 | |
| 257 | if (blk_end < end) { |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 258 | raw_spin_unlock_irqrestore(&l2x0_lock, flags); |
| 259 | raw_spin_lock_irqsave(&l2x0_lock, flags); |
Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 260 | } |
| 261 | } |
Russell King | 3d10743 | 2009-11-19 11:41:09 +0000 | [diff] [blame] | 262 | cache_wait(base + L2X0_INV_LINE_PA, 1); |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 263 | cache_sync(); |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 264 | raw_spin_unlock_irqrestore(&l2x0_lock, flags); |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | static void l2x0_clean_range(unsigned long start, unsigned long end) |
| 268 | { |
Russell King | 3d10743 | 2009-11-19 11:41:09 +0000 | [diff] [blame] | 269 | void __iomem *base = l2x0_base; |
Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 270 | unsigned long flags; |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 271 | |
Santosh Shilimkar | 444457c | 2010-07-11 14:58:41 +0530 | [diff] [blame] | 272 | if ((end - start) >= l2x0_size) { |
| 273 | l2x0_clean_all(); |
| 274 | return; |
| 275 | } |
| 276 | |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 277 | raw_spin_lock_irqsave(&l2x0_lock, flags); |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 278 | start &= ~(CACHE_LINE_SIZE - 1); |
Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 279 | while (start < end) { |
| 280 | unsigned long blk_end = start + min(end - start, 4096UL); |
| 281 | |
| 282 | while (start < blk_end) { |
Santosh Shilimkar | 424d6b1 | 2010-02-04 19:35:06 +0100 | [diff] [blame] | 283 | l2x0_clean_line(start); |
Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 284 | start += CACHE_LINE_SIZE; |
| 285 | } |
| 286 | |
| 287 | if (blk_end < end) { |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 288 | raw_spin_unlock_irqrestore(&l2x0_lock, flags); |
| 289 | raw_spin_lock_irqsave(&l2x0_lock, flags); |
Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
Russell King | 3d10743 | 2009-11-19 11:41:09 +0000 | [diff] [blame] | 292 | cache_wait(base + L2X0_CLEAN_LINE_PA, 1); |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 293 | cache_sync(); |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 294 | raw_spin_unlock_irqrestore(&l2x0_lock, flags); |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | static void l2x0_flush_range(unsigned long start, unsigned long end) |
| 298 | { |
Russell King | 3d10743 | 2009-11-19 11:41:09 +0000 | [diff] [blame] | 299 | void __iomem *base = l2x0_base; |
Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 300 | unsigned long flags; |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 301 | |
Santosh Shilimkar | 444457c | 2010-07-11 14:58:41 +0530 | [diff] [blame] | 302 | if ((end - start) >= l2x0_size) { |
| 303 | l2x0_flush_all(); |
| 304 | return; |
| 305 | } |
| 306 | |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 307 | raw_spin_lock_irqsave(&l2x0_lock, flags); |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 308 | start &= ~(CACHE_LINE_SIZE - 1); |
Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 309 | while (start < end) { |
| 310 | unsigned long blk_end = start + min(end - start, 4096UL); |
| 311 | |
Santosh Shilimkar | 9e65582 | 2010-02-04 19:42:42 +0100 | [diff] [blame] | 312 | debug_writel(0x03); |
Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 313 | while (start < blk_end) { |
Santosh Shilimkar | 424d6b1 | 2010-02-04 19:35:06 +0100 | [diff] [blame] | 314 | l2x0_flush_line(start); |
Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 315 | start += CACHE_LINE_SIZE; |
| 316 | } |
Santosh Shilimkar | 9e65582 | 2010-02-04 19:42:42 +0100 | [diff] [blame] | 317 | debug_writel(0x00); |
Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 318 | |
| 319 | if (blk_end < end) { |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 320 | raw_spin_unlock_irqrestore(&l2x0_lock, flags); |
| 321 | raw_spin_lock_irqsave(&l2x0_lock, flags); |
Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
Russell King | 3d10743 | 2009-11-19 11:41:09 +0000 | [diff] [blame] | 324 | cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1); |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 325 | cache_sync(); |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 326 | raw_spin_unlock_irqrestore(&l2x0_lock, flags); |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 327 | } |
| 328 | |
Thomas Gleixner | 2fd8658 | 2010-07-31 21:05:24 +0530 | [diff] [blame] | 329 | static void l2x0_disable(void) |
| 330 | { |
| 331 | unsigned long flags; |
| 332 | |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 333 | raw_spin_lock_irqsave(&l2x0_lock, flags); |
Will Deacon | 38a8914 | 2011-07-01 14:36:19 +0100 | [diff] [blame] | 334 | __l2x0_flush_all(); |
| 335 | writel_relaxed(0, l2x0_base + L2X0_CTRL); |
| 336 | dsb(); |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 337 | raw_spin_unlock_irqrestore(&l2x0_lock, flags); |
Thomas Gleixner | 2fd8658 | 2010-07-31 21:05:24 +0530 | [diff] [blame] | 338 | } |
| 339 | |
Russell King | 3e175ca | 2011-09-18 11:27:30 +0100 | [diff] [blame] | 340 | static void l2x0_unlock(u32 cache_id) |
Linus Walleij | bac7e6e | 2011-09-06 07:45:46 +0100 | [diff] [blame] | 341 | { |
| 342 | int lockregs; |
| 343 | int i; |
| 344 | |
| 345 | if (cache_id == L2X0_CACHE_ID_PART_L310) |
| 346 | lockregs = 8; |
| 347 | else |
| 348 | /* L210 and unknown types */ |
| 349 | lockregs = 1; |
| 350 | |
| 351 | for (i = 0; i < lockregs; i++) { |
| 352 | writel_relaxed(0x0, l2x0_base + L2X0_LOCKDOWN_WAY_D_BASE + |
| 353 | i * L2X0_LOCKDOWN_STRIDE); |
| 354 | writel_relaxed(0x0, l2x0_base + L2X0_LOCKDOWN_WAY_I_BASE + |
| 355 | i * L2X0_LOCKDOWN_STRIDE); |
| 356 | } |
| 357 | } |
| 358 | |
Russell King | 3e175ca | 2011-09-18 11:27:30 +0100 | [diff] [blame] | 359 | void __init l2x0_init(void __iomem *base, u32 aux_val, u32 aux_mask) |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 360 | { |
Russell King | 3e175ca | 2011-09-18 11:27:30 +0100 | [diff] [blame] | 361 | u32 aux; |
Russell King | 3e175ca | 2011-09-18 11:27:30 +0100 | [diff] [blame] | 362 | u32 way_size = 0; |
Jason McMullan | 64039be | 2010-05-05 18:59:37 +0100 | [diff] [blame] | 363 | const char *type; |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 364 | |
| 365 | l2x0_base = base; |
| 366 | |
Colin Cross | 74b6cdd | 2011-09-14 15:59:50 -0700 | [diff] [blame] | 367 | l2x0_cache_id = readl_relaxed(l2x0_base + L2X0_CACHE_ID); |
Catalin Marinas | 6775a55 | 2010-07-28 22:01:25 +0100 | [diff] [blame] | 368 | aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL); |
Jason McMullan | 64039be | 2010-05-05 18:59:37 +0100 | [diff] [blame] | 369 | |
Sascha Hauer | 4082cfa | 2010-07-08 08:36:21 +0100 | [diff] [blame] | 370 | aux &= aux_mask; |
| 371 | aux |= aux_val; |
| 372 | |
Jason McMullan | 64039be | 2010-05-05 18:59:37 +0100 | [diff] [blame] | 373 | /* Determine the number of ways */ |
Colin Cross | 74b6cdd | 2011-09-14 15:59:50 -0700 | [diff] [blame] | 374 | switch (l2x0_cache_id & L2X0_CACHE_ID_PART_MASK) { |
Jason McMullan | 64039be | 2010-05-05 18:59:37 +0100 | [diff] [blame] | 375 | case L2X0_CACHE_ID_PART_L310: |
| 376 | if (aux & (1 << 16)) |
Colin Cross | 74b6cdd | 2011-09-14 15:59:50 -0700 | [diff] [blame] | 377 | l2x0_ways = 16; |
Jason McMullan | 64039be | 2010-05-05 18:59:37 +0100 | [diff] [blame] | 378 | else |
Colin Cross | 74b6cdd | 2011-09-14 15:59:50 -0700 | [diff] [blame] | 379 | l2x0_ways = 8; |
Jason McMullan | 64039be | 2010-05-05 18:59:37 +0100 | [diff] [blame] | 380 | type = "L310"; |
Will Deacon | f154fe9 | 2012-04-20 17:21:08 +0100 | [diff] [blame] | 381 | #ifdef CONFIG_PL310_ERRATA_753970 |
| 382 | /* Unmapped register. */ |
| 383 | sync_reg_offset = L2X0_DUMMY_REG; |
| 384 | #endif |
Will Deacon | ab4d536 | 2012-04-20 17:22:11 +0100 | [diff] [blame] | 385 | outer_cache.set_debug = pl310_set_debug; |
Girish Mahadevan | 74934a3 | 2012-10-11 10:44:47 -0600 | [diff] [blame] | 386 | outer_cache.resume = pl310_resume; |
Jason McMullan | 64039be | 2010-05-05 18:59:37 +0100 | [diff] [blame] | 387 | break; |
| 388 | case L2X0_CACHE_ID_PART_L210: |
Colin Cross | 74b6cdd | 2011-09-14 15:59:50 -0700 | [diff] [blame] | 389 | l2x0_ways = (aux >> 13) & 0xf; |
Jason McMullan | 64039be | 2010-05-05 18:59:37 +0100 | [diff] [blame] | 390 | type = "L210"; |
Girish Mahadevan | 74934a3 | 2012-10-11 10:44:47 -0600 | [diff] [blame] | 391 | outer_cache.resume = l2x0_resume; |
Jason McMullan | 64039be | 2010-05-05 18:59:37 +0100 | [diff] [blame] | 392 | break; |
| 393 | default: |
| 394 | /* Assume unknown chips have 8 ways */ |
Colin Cross | 74b6cdd | 2011-09-14 15:59:50 -0700 | [diff] [blame] | 395 | l2x0_ways = 8; |
Jason McMullan | 64039be | 2010-05-05 18:59:37 +0100 | [diff] [blame] | 396 | type = "L2x0 series"; |
Girish Mahadevan | 74934a3 | 2012-10-11 10:44:47 -0600 | [diff] [blame] | 397 | outer_cache.resume = l2x0_resume; |
Jason McMullan | 64039be | 2010-05-05 18:59:37 +0100 | [diff] [blame] | 398 | break; |
| 399 | } |
| 400 | |
Colin Cross | 74b6cdd | 2011-09-14 15:59:50 -0700 | [diff] [blame] | 401 | l2x0_way_mask = (1 << l2x0_ways) - 1; |
Jason McMullan | 64039be | 2010-05-05 18:59:37 +0100 | [diff] [blame] | 402 | |
Srinidhi Kasagar | 48371cd | 2009-12-02 06:18:03 +0100 | [diff] [blame] | 403 | /* |
Santosh Shilimkar | 5ba7037 | 2010-07-11 14:35:37 +0530 | [diff] [blame] | 404 | * L2 cache Size = Way size * Number of ways |
| 405 | */ |
| 406 | way_size = (aux & L2X0_AUX_CTRL_WAY_SIZE_MASK) >> 17; |
Colin Cross | 74b6cdd | 2011-09-14 15:59:50 -0700 | [diff] [blame] | 407 | way_size = SZ_1K << (way_size + 3); |
| 408 | l2x0_size = l2x0_ways * way_size; |
| 409 | l2x0_sets = way_size / CACHE_LINE_SIZE; |
Santosh Shilimkar | 5ba7037 | 2010-07-11 14:35:37 +0530 | [diff] [blame] | 410 | |
| 411 | /* |
Srinidhi Kasagar | 48371cd | 2009-12-02 06:18:03 +0100 | [diff] [blame] | 412 | * Check if l2x0 controller is already enabled. |
| 413 | * If you are booting from non-secure mode |
| 414 | * accessing the below registers will fault. |
| 415 | */ |
Catalin Marinas | 6775a55 | 2010-07-28 22:01:25 +0100 | [diff] [blame] | 416 | if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) { |
Linus Walleij | bac7e6e | 2011-09-06 07:45:46 +0100 | [diff] [blame] | 417 | /* Make sure that I&D is not locked down when starting */ |
Colin Cross | 74b6cdd | 2011-09-14 15:59:50 -0700 | [diff] [blame] | 418 | l2x0_unlock(l2x0_cache_id); |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 419 | |
Srinidhi Kasagar | 48371cd | 2009-12-02 06:18:03 +0100 | [diff] [blame] | 420 | /* l2x0 controller is disabled */ |
Catalin Marinas | 6775a55 | 2010-07-28 22:01:25 +0100 | [diff] [blame] | 421 | writel_relaxed(aux, l2x0_base + L2X0_AUX_CTRL); |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 422 | |
Barry Song | 91c2ebb | 2011-09-30 14:43:12 +0100 | [diff] [blame] | 423 | l2x0_saved_regs.aux_ctrl = aux; |
| 424 | |
Srinidhi Kasagar | 48371cd | 2009-12-02 06:18:03 +0100 | [diff] [blame] | 425 | l2x0_inv_all(); |
| 426 | |
| 427 | /* enable L2X0 */ |
Catalin Marinas | 6775a55 | 2010-07-28 22:01:25 +0100 | [diff] [blame] | 428 | writel_relaxed(1, l2x0_base + L2X0_CTRL); |
Srinidhi Kasagar | 48371cd | 2009-12-02 06:18:03 +0100 | [diff] [blame] | 429 | } |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 430 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 431 | outer_cache.inv_range = l2x0_inv_range; |
| 432 | outer_cache.clean_range = l2x0_clean_range; |
| 433 | outer_cache.flush_range = l2x0_flush_range; |
Catalin Marinas | 23107c5 | 2010-03-24 16:48:53 +0100 | [diff] [blame] | 434 | outer_cache.sync = l2x0_cache_sync; |
Thomas Gleixner | 2fd8658 | 2010-07-31 21:05:24 +0530 | [diff] [blame] | 435 | outer_cache.flush_all = l2x0_flush_all; |
| 436 | outer_cache.inv_all = l2x0_inv_all; |
| 437 | outer_cache.disable = l2x0_disable; |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 438 | |
Jason McMullan | 64039be | 2010-05-05 18:59:37 +0100 | [diff] [blame] | 439 | printk(KERN_INFO "%s cache controller enabled\n", type); |
Santosh Shilimkar | 5ba7037 | 2010-07-11 14:35:37 +0530 | [diff] [blame] | 440 | printk(KERN_INFO "l2x0: %d ways, CACHE_ID 0x%08x, AUX_CTRL 0x%08x, Cache size: %d B\n", |
Colin Cross | 74b6cdd | 2011-09-14 15:59:50 -0700 | [diff] [blame] | 441 | l2x0_ways, l2x0_cache_id, aux, l2x0_size); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 442 | |
Taniya Das | 38a8c6e | 2012-05-09 20:34:39 +0530 | [diff] [blame] | 443 | /* Save the L2X0 contents, as they are not modified else where */ |
| 444 | pl310_save(); |
Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 445 | } |
Rob Herring | 8c36926 | 2011-08-03 18:12:05 +0100 | [diff] [blame] | 446 | |
| 447 | #ifdef CONFIG_OF |
| 448 | static void __init l2x0_of_setup(const struct device_node *np, |
Russell King | 3e175ca | 2011-09-18 11:27:30 +0100 | [diff] [blame] | 449 | u32 *aux_val, u32 *aux_mask) |
Rob Herring | 8c36926 | 2011-08-03 18:12:05 +0100 | [diff] [blame] | 450 | { |
| 451 | u32 data[2] = { 0, 0 }; |
| 452 | u32 tag = 0; |
| 453 | u32 dirty = 0; |
| 454 | u32 val = 0, mask = 0; |
| 455 | |
| 456 | of_property_read_u32(np, "arm,tag-latency", &tag); |
| 457 | if (tag) { |
| 458 | mask |= L2X0_AUX_CTRL_TAG_LATENCY_MASK; |
| 459 | val |= (tag - 1) << L2X0_AUX_CTRL_TAG_LATENCY_SHIFT; |
| 460 | } |
| 461 | |
| 462 | of_property_read_u32_array(np, "arm,data-latency", |
| 463 | data, ARRAY_SIZE(data)); |
| 464 | if (data[0] && data[1]) { |
| 465 | mask |= L2X0_AUX_CTRL_DATA_RD_LATENCY_MASK | |
| 466 | L2X0_AUX_CTRL_DATA_WR_LATENCY_MASK; |
| 467 | val |= ((data[0] - 1) << L2X0_AUX_CTRL_DATA_RD_LATENCY_SHIFT) | |
| 468 | ((data[1] - 1) << L2X0_AUX_CTRL_DATA_WR_LATENCY_SHIFT); |
| 469 | } |
| 470 | |
| 471 | of_property_read_u32(np, "arm,dirty-latency", &dirty); |
| 472 | if (dirty) { |
| 473 | mask |= L2X0_AUX_CTRL_DIRTY_LATENCY_MASK; |
| 474 | val |= (dirty - 1) << L2X0_AUX_CTRL_DIRTY_LATENCY_SHIFT; |
| 475 | } |
| 476 | |
| 477 | *aux_val &= ~mask; |
| 478 | *aux_val |= val; |
| 479 | *aux_mask &= ~mask; |
| 480 | } |
| 481 | |
| 482 | static void __init pl310_of_setup(const struct device_node *np, |
Russell King | 3e175ca | 2011-09-18 11:27:30 +0100 | [diff] [blame] | 483 | u32 *aux_val, u32 *aux_mask) |
Rob Herring | 8c36926 | 2011-08-03 18:12:05 +0100 | [diff] [blame] | 484 | { |
| 485 | u32 data[3] = { 0, 0, 0 }; |
| 486 | u32 tag[3] = { 0, 0, 0 }; |
| 487 | u32 filter[2] = { 0, 0 }; |
| 488 | |
| 489 | of_property_read_u32_array(np, "arm,tag-latency", tag, ARRAY_SIZE(tag)); |
| 490 | if (tag[0] && tag[1] && tag[2]) |
| 491 | writel_relaxed( |
| 492 | ((tag[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) | |
| 493 | ((tag[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) | |
| 494 | ((tag[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT), |
| 495 | l2x0_base + L2X0_TAG_LATENCY_CTRL); |
| 496 | |
| 497 | of_property_read_u32_array(np, "arm,data-latency", |
| 498 | data, ARRAY_SIZE(data)); |
| 499 | if (data[0] && data[1] && data[2]) |
| 500 | writel_relaxed( |
| 501 | ((data[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) | |
| 502 | ((data[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) | |
| 503 | ((data[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT), |
| 504 | l2x0_base + L2X0_DATA_LATENCY_CTRL); |
| 505 | |
| 506 | of_property_read_u32_array(np, "arm,filter-ranges", |
| 507 | filter, ARRAY_SIZE(filter)); |
Barry Song | 74d41f3 | 2011-09-14 03:20:01 +0100 | [diff] [blame] | 508 | if (filter[1]) { |
Rob Herring | 8c36926 | 2011-08-03 18:12:05 +0100 | [diff] [blame] | 509 | writel_relaxed(ALIGN(filter[0] + filter[1], SZ_1M), |
| 510 | l2x0_base + L2X0_ADDR_FILTER_END); |
| 511 | writel_relaxed((filter[0] & ~(SZ_1M - 1)) | L2X0_ADDR_FILTER_EN, |
| 512 | l2x0_base + L2X0_ADDR_FILTER_START); |
| 513 | } |
| 514 | } |
Taniya Das | 38a8c6e | 2012-05-09 20:34:39 +0530 | [diff] [blame] | 515 | #endif |
Rob Herring | 8c36926 | 2011-08-03 18:12:05 +0100 | [diff] [blame] | 516 | |
Stephen Boyd | 22ab934 | 2012-04-25 11:42:14 -0700 | [diff] [blame] | 517 | static void pl310_save(void) |
Barry Song | 91c2ebb | 2011-09-30 14:43:12 +0100 | [diff] [blame] | 518 | { |
| 519 | u32 l2x0_revision = readl_relaxed(l2x0_base + L2X0_CACHE_ID) & |
| 520 | L2X0_CACHE_ID_RTL_MASK; |
| 521 | |
| 522 | l2x0_saved_regs.tag_latency = readl_relaxed(l2x0_base + |
| 523 | L2X0_TAG_LATENCY_CTRL); |
| 524 | l2x0_saved_regs.data_latency = readl_relaxed(l2x0_base + |
| 525 | L2X0_DATA_LATENCY_CTRL); |
| 526 | l2x0_saved_regs.filter_end = readl_relaxed(l2x0_base + |
| 527 | L2X0_ADDR_FILTER_END); |
| 528 | l2x0_saved_regs.filter_start = readl_relaxed(l2x0_base + |
| 529 | L2X0_ADDR_FILTER_START); |
| 530 | |
| 531 | if (l2x0_revision >= L2X0_CACHE_ID_RTL_R2P0) { |
| 532 | /* |
| 533 | * From r2p0, there is Prefetch offset/control register |
| 534 | */ |
| 535 | l2x0_saved_regs.prefetch_ctrl = readl_relaxed(l2x0_base + |
| 536 | L2X0_PREFETCH_CTRL); |
| 537 | /* |
| 538 | * From r3p0, there is Power control register |
| 539 | */ |
| 540 | if (l2x0_revision >= L2X0_CACHE_ID_RTL_R3P0) |
| 541 | l2x0_saved_regs.pwr_ctrl = readl_relaxed(l2x0_base + |
| 542 | L2X0_POWER_CTRL); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | static void l2x0_resume(void) |
| 547 | { |
| 548 | if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) { |
| 549 | /* restore aux ctrl and enable l2 */ |
| 550 | l2x0_unlock(readl_relaxed(l2x0_base + L2X0_CACHE_ID)); |
| 551 | |
| 552 | writel_relaxed(l2x0_saved_regs.aux_ctrl, l2x0_base + |
| 553 | L2X0_AUX_CTRL); |
| 554 | |
| 555 | l2x0_inv_all(); |
| 556 | |
| 557 | writel_relaxed(1, l2x0_base + L2X0_CTRL); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | static void pl310_resume(void) |
| 562 | { |
| 563 | u32 l2x0_revision; |
| 564 | |
| 565 | if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) { |
| 566 | /* restore pl310 setup */ |
| 567 | writel_relaxed(l2x0_saved_regs.tag_latency, |
| 568 | l2x0_base + L2X0_TAG_LATENCY_CTRL); |
| 569 | writel_relaxed(l2x0_saved_regs.data_latency, |
| 570 | l2x0_base + L2X0_DATA_LATENCY_CTRL); |
| 571 | writel_relaxed(l2x0_saved_regs.filter_end, |
| 572 | l2x0_base + L2X0_ADDR_FILTER_END); |
| 573 | writel_relaxed(l2x0_saved_regs.filter_start, |
| 574 | l2x0_base + L2X0_ADDR_FILTER_START); |
| 575 | |
| 576 | l2x0_revision = readl_relaxed(l2x0_base + L2X0_CACHE_ID) & |
| 577 | L2X0_CACHE_ID_RTL_MASK; |
| 578 | |
| 579 | if (l2x0_revision >= L2X0_CACHE_ID_RTL_R2P0) { |
| 580 | writel_relaxed(l2x0_saved_regs.prefetch_ctrl, |
| 581 | l2x0_base + L2X0_PREFETCH_CTRL); |
| 582 | if (l2x0_revision >= L2X0_CACHE_ID_RTL_R3P0) |
| 583 | writel_relaxed(l2x0_saved_regs.pwr_ctrl, |
| 584 | l2x0_base + L2X0_POWER_CTRL); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | l2x0_resume(); |
| 589 | } |
| 590 | |
Taniya Das | 38a8c6e | 2012-05-09 20:34:39 +0530 | [diff] [blame] | 591 | #ifdef CONFIG_OF |
Barry Song | 91c2ebb | 2011-09-30 14:43:12 +0100 | [diff] [blame] | 592 | static const struct l2x0_of_data pl310_data = { |
| 593 | pl310_of_setup, |
| 594 | pl310_save, |
| 595 | pl310_resume, |
| 596 | }; |
| 597 | |
| 598 | static const struct l2x0_of_data l2x0_data = { |
| 599 | l2x0_of_setup, |
| 600 | NULL, |
| 601 | l2x0_resume, |
| 602 | }; |
| 603 | |
Rob Herring | 8c36926 | 2011-08-03 18:12:05 +0100 | [diff] [blame] | 604 | static const struct of_device_id l2x0_ids[] __initconst = { |
Barry Song | 91c2ebb | 2011-09-30 14:43:12 +0100 | [diff] [blame] | 605 | { .compatible = "arm,pl310-cache", .data = (void *)&pl310_data }, |
| 606 | { .compatible = "arm,l220-cache", .data = (void *)&l2x0_data }, |
| 607 | { .compatible = "arm,l210-cache", .data = (void *)&l2x0_data }, |
Rob Herring | 8c36926 | 2011-08-03 18:12:05 +0100 | [diff] [blame] | 608 | {} |
| 609 | }; |
| 610 | |
Russell King | 3e175ca | 2011-09-18 11:27:30 +0100 | [diff] [blame] | 611 | int __init l2x0_of_init(u32 aux_val, u32 aux_mask) |
Rob Herring | 8c36926 | 2011-08-03 18:12:05 +0100 | [diff] [blame] | 612 | { |
| 613 | struct device_node *np; |
Barry Song | 91c2ebb | 2011-09-30 14:43:12 +0100 | [diff] [blame] | 614 | struct l2x0_of_data *data; |
| 615 | struct resource res; |
Rob Herring | 8c36926 | 2011-08-03 18:12:05 +0100 | [diff] [blame] | 616 | |
| 617 | np = of_find_matching_node(NULL, l2x0_ids); |
| 618 | if (!np) |
| 619 | return -ENODEV; |
Barry Song | 91c2ebb | 2011-09-30 14:43:12 +0100 | [diff] [blame] | 620 | |
| 621 | if (of_address_to_resource(np, 0, &res)) |
| 622 | return -ENODEV; |
| 623 | |
| 624 | l2x0_base = ioremap(res.start, resource_size(&res)); |
Rob Herring | 8c36926 | 2011-08-03 18:12:05 +0100 | [diff] [blame] | 625 | if (!l2x0_base) |
| 626 | return -ENOMEM; |
| 627 | |
Barry Song | 91c2ebb | 2011-09-30 14:43:12 +0100 | [diff] [blame] | 628 | l2x0_saved_regs.phy_base = res.start; |
| 629 | |
| 630 | data = of_match_node(l2x0_ids, np)->data; |
| 631 | |
Rob Herring | 8c36926 | 2011-08-03 18:12:05 +0100 | [diff] [blame] | 632 | /* L2 configuration can only be changed if the cache is disabled */ |
| 633 | if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) { |
Barry Song | 91c2ebb | 2011-09-30 14:43:12 +0100 | [diff] [blame] | 634 | if (data->setup) |
| 635 | data->setup(np, &aux_val, &aux_mask); |
Rob Herring | 8c36926 | 2011-08-03 18:12:05 +0100 | [diff] [blame] | 636 | } |
Barry Song | 91c2ebb | 2011-09-30 14:43:12 +0100 | [diff] [blame] | 637 | |
| 638 | if (data->save) |
| 639 | data->save(); |
| 640 | |
Rob Herring | 8c36926 | 2011-08-03 18:12:05 +0100 | [diff] [blame] | 641 | l2x0_init(l2x0_base, aux_val, aux_mask); |
Barry Song | 91c2ebb | 2011-09-30 14:43:12 +0100 | [diff] [blame] | 642 | |
| 643 | outer_cache.resume = data->resume; |
Rob Herring | 8c36926 | 2011-08-03 18:12:05 +0100 | [diff] [blame] | 644 | return 0; |
| 645 | } |
| 646 | #endif |
Taniya Das | 38a8c6e | 2012-05-09 20:34:39 +0530 | [diff] [blame] | 647 | |
| 648 | void l2cc_suspend(void) |
| 649 | { |
| 650 | l2x0_disable(); |
| 651 | dmb(); |
| 652 | } |
| 653 | |
| 654 | void l2cc_resume(void) |
| 655 | { |
| 656 | pl310_resume(); |
| 657 | dmb(); |
| 658 | } |
Mark Rutland | 29081e1 | 2012-11-21 17:02:51 -0500 | [diff] [blame] | 659 | |
| 660 | #ifdef CONFIG_HW_PERF_EVENTS |
| 661 | /* |
| 662 | * L220/PL310 PMU-specific functionality. |
| 663 | * TODO: Put this in a separate file and get the l2x0 driver to register |
| 664 | * the PMU from l2x0_{of}_init. |
| 665 | */ |
| 666 | |
| 667 | static struct arm_pmu l2x0_pmu; |
| 668 | |
| 669 | static u64 l2x0pmu_max_event_id; |
| 670 | |
| 671 | static struct perf_event *events[2]; |
| 672 | static unsigned long used_mask[BITS_TO_LONGS(2)]; |
| 673 | static struct pmu_hw_events hw_events = { |
| 674 | .events = events, |
| 675 | .used_mask = used_mask, |
| 676 | .pmu_lock = __RAW_SPIN_LOCK_UNLOCKED(l2x0pmu_hw_events.pmu_lock), |
| 677 | }; |
| 678 | |
| 679 | #define COUNTER_CFG_ADDR(idx) (l2x0_base + L2X0_EVENT_CNT0_CFG - 4*idx) |
| 680 | |
| 681 | #define COUNTER_CTRL_ADDR (l2x0_base + L2X0_EVENT_CNT_CTRL) |
| 682 | |
| 683 | #define COUNTER_ADDR(idx) (l2x0_base + L2X0_EVENT_CNT0_VAL - 4*idx) |
| 684 | |
| 685 | static u32 l2x0_read_intr_mask(void) |
| 686 | { |
| 687 | return readl_relaxed(l2x0_base + L2X0_INTR_MASK); |
| 688 | } |
| 689 | |
| 690 | static void l2x0_write_intr_mask(u32 val) |
| 691 | { |
| 692 | writel_relaxed(val, l2x0_base + L2X0_INTR_MASK); |
| 693 | } |
| 694 | |
| 695 | static void l2x0_enable_counter_interrupt(void) |
| 696 | { |
| 697 | u32 intr_mask = l2x0_read_intr_mask(); |
| 698 | intr_mask |= L2X0_INTR_MASK_ECNTR; |
| 699 | l2x0_write_intr_mask(intr_mask); |
| 700 | } |
| 701 | |
| 702 | static void l2x0_disable_counter_interrupt(void) |
| 703 | { |
| 704 | u32 intr_mask = l2x0_read_intr_mask(); |
| 705 | intr_mask &= ~L2X0_INTR_MASK_ECNTR; |
| 706 | l2x0_write_intr_mask(intr_mask); |
| 707 | } |
| 708 | |
| 709 | static void l2x0_clear_interrupts(u32 flags) |
| 710 | { |
| 711 | writel_relaxed(flags, l2x0_base + L2X0_INTR_CLEAR); |
| 712 | } |
| 713 | |
| 714 | static struct pmu_hw_events *l2x0pmu_get_hw_events(void) |
| 715 | { |
| 716 | return &hw_events; |
| 717 | } |
| 718 | |
| 719 | static u32 l2x0pmu_read_ctrl(void) |
| 720 | { |
| 721 | return readl_relaxed(COUNTER_CTRL_ADDR); |
| 722 | } |
| 723 | |
| 724 | static void l2x0pmu_write_ctrl(u32 val) |
| 725 | { |
| 726 | writel_relaxed(val, COUNTER_CTRL_ADDR); |
| 727 | } |
| 728 | |
| 729 | static u32 l2x0pmu_read_cfg(int idx) |
| 730 | { |
| 731 | return readl_relaxed(COUNTER_CFG_ADDR(idx)); |
| 732 | } |
| 733 | |
| 734 | static void l2x0pmu_write_cfg(u32 val, int idx) |
| 735 | { |
| 736 | writel_relaxed(val, COUNTER_CFG_ADDR(idx)); |
| 737 | } |
| 738 | |
| 739 | static void l2x0pmu_enable_counter(u32 cfg, int idx) |
| 740 | { |
| 741 | cfg |= L2X0_EVENT_CNT_CFG_INTR_OVERFLOW; |
| 742 | l2x0pmu_write_cfg(cfg, idx); |
| 743 | } |
| 744 | |
| 745 | static u32 l2x0pmu_disable_counter(int idx) |
| 746 | { |
| 747 | u32 cfg, oldcfg; |
| 748 | |
| 749 | cfg = oldcfg = l2x0pmu_read_cfg(idx); |
| 750 | cfg &= ~L2X0_EVENT_CNT_CFG_MASK; |
| 751 | cfg &= ~L2X0_EVENT_CNT_CFG_INTR_MASK; |
| 752 | l2x0pmu_write_cfg(cfg, idx); |
| 753 | |
| 754 | return oldcfg; |
| 755 | } |
| 756 | |
| 757 | static u32 l2x0pmu_read_counter(int idx) |
| 758 | { |
| 759 | return readl_relaxed(COUNTER_ADDR(idx)); |
| 760 | } |
| 761 | |
| 762 | static void l2x0pmu_write_counter(int idx, u32 val) |
| 763 | { |
| 764 | /* |
| 765 | * L2X0 counters can only be written to when they are disabled. |
| 766 | * As perf core does not disable counters before writing to them |
| 767 | * under interrupts, we must do so here. |
| 768 | */ |
| 769 | u32 cfg = l2x0pmu_disable_counter(idx); |
| 770 | writel_relaxed(val, COUNTER_ADDR(idx)); |
| 771 | l2x0pmu_write_cfg(cfg, idx); |
| 772 | } |
| 773 | |
| 774 | static int counter_is_saturated(int idx) |
| 775 | { |
| 776 | return l2x0pmu_read_counter(idx) == 0xFFFFFFFF; |
| 777 | } |
| 778 | |
| 779 | static void l2x0pmu_start(void) |
| 780 | { |
| 781 | unsigned long flags; |
| 782 | u32 val; |
| 783 | |
| 784 | raw_spin_lock_irqsave(&hw_events.pmu_lock, flags); |
| 785 | |
| 786 | l2x0_enable_counter_interrupt(); |
| 787 | |
| 788 | val = l2x0pmu_read_ctrl(); |
| 789 | val |= L2X0_EVENT_CNT_ENABLE; |
| 790 | l2x0pmu_write_ctrl(val); |
| 791 | |
| 792 | raw_spin_unlock_irqrestore(&hw_events.pmu_lock, flags); |
| 793 | } |
| 794 | |
| 795 | static void l2x0pmu_stop(void) |
| 796 | { |
| 797 | unsigned long flags; |
| 798 | u32 val; |
| 799 | |
| 800 | raw_spin_lock_irqsave(&hw_events.pmu_lock, flags); |
| 801 | |
| 802 | val = l2x0pmu_read_ctrl(); |
| 803 | val &= ~L2X0_EVENT_CNT_ENABLE_MASK; |
| 804 | l2x0pmu_write_ctrl(val); |
| 805 | |
| 806 | l2x0_disable_counter_interrupt(); |
| 807 | |
| 808 | raw_spin_unlock_irqrestore(&hw_events.pmu_lock, flags); |
| 809 | } |
| 810 | |
| 811 | static void l2x0pmu_enable(struct hw_perf_event *event, int idx, int cpu) |
| 812 | { |
| 813 | unsigned long flags; |
| 814 | u32 cfg; |
| 815 | |
| 816 | raw_spin_lock_irqsave(&hw_events.pmu_lock, flags); |
| 817 | |
| 818 | cfg = (event->config_base << L2X0_EVENT_CNT_CFG_SHIFT) & |
| 819 | L2X0_EVENT_CNT_CFG_MASK; |
| 820 | l2x0pmu_enable_counter(cfg, idx); |
| 821 | |
| 822 | raw_spin_unlock_irqrestore(&hw_events.pmu_lock, flags); |
| 823 | } |
| 824 | |
| 825 | static void l2x0pmu_disable(struct hw_perf_event *event, int idx) |
| 826 | { |
| 827 | unsigned long flags; |
| 828 | |
| 829 | raw_spin_lock_irqsave(&hw_events.pmu_lock, flags); |
| 830 | l2x0pmu_disable_counter(idx); |
| 831 | raw_spin_unlock_irqrestore(&hw_events.pmu_lock, flags); |
| 832 | } |
| 833 | |
| 834 | static int l2x0pmu_get_event_idx(struct pmu_hw_events *events, |
| 835 | struct hw_perf_event *hwc) |
| 836 | { |
| 837 | int idx; |
| 838 | |
| 839 | /* Counters are identical. Just grab a free one. */ |
| 840 | for (idx = 0; idx < L2X0_NUM_COUNTERS; ++idx) { |
| 841 | if (!test_and_set_bit(idx, hw_events.used_mask)) |
| 842 | return idx; |
| 843 | } |
| 844 | |
| 845 | return -EAGAIN; |
| 846 | } |
| 847 | |
| 848 | /* |
| 849 | * As System PMUs are affine to CPU0, the fact that interrupts are disabled |
| 850 | * during interrupt handling is enough to serialise our actions and make this |
| 851 | * safe. We do not need to grab our pmu_lock here. |
| 852 | */ |
| 853 | static irqreturn_t l2x0pmu_handle_irq(int irq, void *dev) |
| 854 | { |
| 855 | irqreturn_t status = IRQ_NONE; |
| 856 | struct perf_sample_data data; |
| 857 | struct pt_regs *regs; |
| 858 | int idx; |
| 859 | |
| 860 | regs = get_irq_regs(); |
| 861 | |
| 862 | for (idx = 0; idx < L2X0_NUM_COUNTERS; ++idx) { |
| 863 | struct perf_event *event = hw_events.events[idx]; |
| 864 | struct hw_perf_event *hwc; |
| 865 | |
| 866 | if (!counter_is_saturated(idx)) |
| 867 | continue; |
| 868 | |
| 869 | status = IRQ_HANDLED; |
| 870 | |
| 871 | hwc = &event->hw; |
| 872 | |
| 873 | /* |
| 874 | * The armpmu_* functions expect counters to overflow, but |
| 875 | * L220/PL310 counters saturate instead. Fake the overflow |
| 876 | * here so the hardware is in sync with what the framework |
| 877 | * expects. |
| 878 | */ |
| 879 | l2x0pmu_write_counter(idx, 0); |
| 880 | |
| 881 | armpmu_event_update(event, hwc, idx); |
| 882 | data.period = event->hw.last_period; |
| 883 | |
| 884 | if (!armpmu_event_set_period(event, hwc, idx)) |
| 885 | continue; |
| 886 | |
| 887 | if (perf_event_overflow(event, &data, regs)) |
| 888 | l2x0pmu_disable_counter(idx); |
| 889 | } |
| 890 | |
| 891 | l2x0_clear_interrupts(L2X0_INTR_MASK_ECNTR); |
| 892 | |
| 893 | irq_work_run(); |
| 894 | |
| 895 | return status; |
| 896 | } |
| 897 | |
| 898 | static int map_l2x0_raw_event(u64 config) |
| 899 | { |
| 900 | return (config <= l2x0pmu_max_event_id) ? config : -ENOENT; |
| 901 | } |
| 902 | |
| 903 | static int l2x0pmu_map_event(struct perf_event *event) |
| 904 | { |
| 905 | u64 config = event->attr.config; |
| 906 | u64 supported_samples = (PERF_SAMPLE_TIME | |
| 907 | PERF_SAMPLE_ID | |
| 908 | PERF_SAMPLE_PERIOD | |
| 909 | PERF_SAMPLE_STREAM_ID | |
| 910 | PERF_SAMPLE_RAW); |
| 911 | |
| 912 | if (event->attr.type != l2x0_pmu.pmu.type) |
| 913 | return -ENOENT; |
| 914 | |
| 915 | /* |
| 916 | * L2x0 counters are global across CPUs. |
| 917 | * If userspace ask perf to monitor from multiple CPUs, each CPU will |
| 918 | * report the shared total. When summed, this will be the actual value |
| 919 | * multiplied by the number of CPUs. We limit monitoring to a single |
| 920 | * CPU (0) to prevent confusion stemming from this. |
| 921 | */ |
| 922 | if (event->cpu != 0) |
| 923 | return -ENOENT; |
| 924 | |
| 925 | if (event->attr.sample_type & ~supported_samples) |
| 926 | return -ENOENT; |
| 927 | |
| 928 | return map_l2x0_raw_event(config); |
| 929 | } |
| 930 | |
| 931 | static struct arm_pmu l2x0_pmu = { |
| 932 | .id = ARM_PERF_PMU_ID_L2X0, |
| 933 | .type = ARM_PMU_DEVICE_L2CC, |
| 934 | .name = "ARM L220/PL310 L2 Cache controller", |
| 935 | .start = l2x0pmu_start, |
| 936 | .stop = l2x0pmu_stop, |
| 937 | .handle_irq = l2x0pmu_handle_irq, |
| 938 | .enable = l2x0pmu_enable, |
| 939 | .disable = l2x0pmu_disable, |
| 940 | .get_event_idx = l2x0pmu_get_event_idx, |
| 941 | .read_counter = l2x0pmu_read_counter, |
| 942 | .write_counter = l2x0pmu_write_counter, |
| 943 | .map_event = l2x0pmu_map_event, |
| 944 | .num_events = 2, |
| 945 | .max_period = 0xFFFFFFFF, |
| 946 | .get_hw_events = l2x0pmu_get_hw_events, |
| 947 | }; |
| 948 | |
| 949 | static int __devinit l2x0pmu_device_probe(struct platform_device *pdev) |
| 950 | { |
| 951 | l2x0_pmu.plat_device = pdev; |
| 952 | /* FIXME: return code? */ |
| 953 | armpmu_register(&l2x0_pmu, "l2x0", -1); |
| 954 | return 0; |
| 955 | } |
| 956 | |
| 957 | static struct platform_driver l2x0pmu_driver = { |
| 958 | .driver = { |
| 959 | .name = "l2x0-pmu", |
| 960 | }, |
| 961 | .probe = l2x0pmu_device_probe, |
| 962 | }; |
| 963 | |
| 964 | static int __init register_pmu_driver(void) |
| 965 | { |
| 966 | return platform_driver_register(&l2x0pmu_driver); |
| 967 | } |
| 968 | device_initcall(register_pmu_driver); |
| 969 | |
| 970 | #endif /* CONFIG_HW_PERF_EVENTS */ |