Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 1 | /* |
Sarang Joshi | 31a7724 | 2013-08-28 12:57:00 -0700 | [diff] [blame] | 2 | * Copyright (c) 2013, The Linux Foundation. All rights reserved. |
Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/atomic.h> |
| 15 | #include <linux/export.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/memory_alloc.h> |
| 18 | #include <linux/module.h> |
Laura Abbott | f7e4404 | 2012-06-22 12:50:32 -0700 | [diff] [blame] | 19 | #include <linux/mod_devicetable.h> |
Laura Abbott | f8c03b9 | 2012-02-16 14:57:58 -0800 | [diff] [blame] | 20 | #include <linux/platform_device.h> |
Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 21 | #include <linux/sched.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/string.h> |
Jeff Ohlstein | 4e24908 | 2012-03-21 15:12:20 -0700 | [diff] [blame] | 24 | #include <linux/atomic.h> |
Laura Abbott | f7e4404 | 2012-06-22 12:50:32 -0700 | [diff] [blame] | 25 | #include <linux/of.h> |
Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 26 | #include <asm/io.h> |
| 27 | #include <asm-generic/sizes.h> |
| 28 | #include <mach/memory.h> |
| 29 | #include <mach/msm_rtb.h> |
| 30 | #include <mach/system.h> |
| 31 | |
| 32 | #define SENTINEL_BYTE_1 0xFF |
| 33 | #define SENTINEL_BYTE_2 0xAA |
| 34 | #define SENTINEL_BYTE_3 0xFF |
| 35 | |
Laura Abbott | f7e4404 | 2012-06-22 12:50:32 -0700 | [diff] [blame] | 36 | #define RTB_COMPAT_STR "qcom,msm-rtb" |
| 37 | |
Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 38 | /* Write |
| 39 | * 1) 3 bytes sentinel |
| 40 | * 2) 1 bytes of log type |
| 41 | * 3) 4 bytes of where the caller came from |
| 42 | * 4) 4 bytes index |
| 43 | * 4) 4 bytes extra data from the caller |
| 44 | * |
| 45 | * Total = 16 bytes. |
| 46 | */ |
| 47 | struct msm_rtb_layout { |
| 48 | unsigned char sentinel[3]; |
| 49 | unsigned char log_type; |
| 50 | void *caller; |
| 51 | unsigned long idx; |
| 52 | void *data; |
| 53 | } __attribute__ ((__packed__)); |
| 54 | |
| 55 | |
| 56 | struct msm_rtb_state { |
| 57 | struct msm_rtb_layout *rtb; |
Laura Abbott | 8d08716 | 2013-03-12 16:43:39 -0700 | [diff] [blame] | 58 | phys_addr_t phys; |
Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 59 | int nentries; |
| 60 | int size; |
| 61 | int enabled; |
Laura Abbott | aaa34a1 | 2012-02-14 15:48:47 -0800 | [diff] [blame] | 62 | int initialized; |
Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 63 | uint32_t filter; |
| 64 | int step_size; |
| 65 | }; |
| 66 | |
| 67 | #if defined(CONFIG_MSM_RTB_SEPARATE_CPUS) |
| 68 | DEFINE_PER_CPU(atomic_t, msm_rtb_idx_cpu); |
| 69 | #else |
| 70 | static atomic_t msm_rtb_idx; |
| 71 | #endif |
| 72 | |
| 73 | struct msm_rtb_state msm_rtb = { |
Laura Abbott | aa9b60b | 2012-01-23 13:06:20 -0800 | [diff] [blame] | 74 | .filter = 1 << LOGK_LOGBUF, |
Laura Abbott | aaa34a1 | 2012-02-14 15:48:47 -0800 | [diff] [blame] | 75 | .enabled = 1, |
Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | module_param_named(filter, msm_rtb.filter, uint, 0644); |
| 79 | module_param_named(enable, msm_rtb.enabled, int, 0644); |
| 80 | |
Laura Abbott | a29d731 | 2012-04-09 13:10:01 -0700 | [diff] [blame] | 81 | static int msm_rtb_panic_notifier(struct notifier_block *this, |
| 82 | unsigned long event, void *ptr) |
| 83 | { |
| 84 | msm_rtb.enabled = 0; |
| 85 | return NOTIFY_DONE; |
| 86 | } |
| 87 | |
| 88 | static struct notifier_block msm_rtb_panic_blk = { |
| 89 | .notifier_call = msm_rtb_panic_notifier, |
| 90 | }; |
| 91 | |
Sarang Joshi | 31a7724 | 2013-08-28 12:57:00 -0700 | [diff] [blame] | 92 | int notrace msm_rtb_event_should_log(enum logk_event_type log_type) |
Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 93 | { |
Laura Abbott | aaa34a1 | 2012-02-14 15:48:47 -0800 | [diff] [blame] | 94 | return msm_rtb.initialized && msm_rtb.enabled && |
Jeff Ohlstein | 4e24908 | 2012-03-21 15:12:20 -0700 | [diff] [blame] | 95 | ((1 << (log_type & ~LOGTYPE_NOPC)) & msm_rtb.filter); |
Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 96 | } |
| 97 | EXPORT_SYMBOL(msm_rtb_event_should_log); |
| 98 | |
| 99 | static void msm_rtb_emit_sentinel(struct msm_rtb_layout *start) |
| 100 | { |
| 101 | start->sentinel[0] = SENTINEL_BYTE_1; |
| 102 | start->sentinel[1] = SENTINEL_BYTE_2; |
| 103 | start->sentinel[2] = SENTINEL_BYTE_3; |
| 104 | } |
| 105 | |
| 106 | static void msm_rtb_write_type(enum logk_event_type log_type, |
| 107 | struct msm_rtb_layout *start) |
| 108 | { |
| 109 | start->log_type = (char)log_type; |
| 110 | } |
| 111 | |
| 112 | static void msm_rtb_write_caller(void *caller, struct msm_rtb_layout *start) |
| 113 | { |
| 114 | start->caller = caller; |
| 115 | } |
| 116 | |
| 117 | static void msm_rtb_write_idx(unsigned long idx, |
| 118 | struct msm_rtb_layout *start) |
| 119 | { |
| 120 | start->idx = idx; |
| 121 | } |
| 122 | |
| 123 | static void msm_rtb_write_data(void *data, struct msm_rtb_layout *start) |
| 124 | { |
| 125 | start->data = data; |
| 126 | } |
| 127 | |
Jeff Ohlstein | 4e24908 | 2012-03-21 15:12:20 -0700 | [diff] [blame] | 128 | static void uncached_logk_pc_idx(enum logk_event_type log_type, void *caller, |
| 129 | void *data, int idx) |
| 130 | { |
| 131 | struct msm_rtb_layout *start; |
| 132 | |
| 133 | start = &msm_rtb.rtb[idx & (msm_rtb.nentries - 1)]; |
| 134 | |
| 135 | msm_rtb_emit_sentinel(start); |
| 136 | msm_rtb_write_type(log_type, start); |
| 137 | msm_rtb_write_caller(caller, start); |
| 138 | msm_rtb_write_idx(idx, start); |
| 139 | msm_rtb_write_data(data, start); |
| 140 | mb(); |
| 141 | |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | static void uncached_logk_timestamp(int idx) |
| 146 | { |
| 147 | unsigned long long timestamp; |
| 148 | void *timestamp_upper, *timestamp_lower; |
| 149 | timestamp = sched_clock(); |
| 150 | timestamp_lower = (void *)lower_32_bits(timestamp); |
| 151 | timestamp_upper = (void *)upper_32_bits(timestamp); |
| 152 | |
| 153 | uncached_logk_pc_idx(LOGK_TIMESTAMP|LOGTYPE_NOPC, timestamp_lower, |
| 154 | timestamp_upper, idx); |
| 155 | } |
| 156 | |
Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 157 | #if defined(CONFIG_MSM_RTB_SEPARATE_CPUS) |
| 158 | static int msm_rtb_get_idx(void) |
| 159 | { |
Jeff Ohlstein | 4e24908 | 2012-03-21 15:12:20 -0700 | [diff] [blame] | 160 | int cpu, i, offset; |
Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 161 | atomic_t *index; |
| 162 | |
| 163 | /* |
| 164 | * ideally we would use get_cpu but this is a close enough |
| 165 | * approximation for our purposes. |
| 166 | */ |
| 167 | cpu = raw_smp_processor_id(); |
| 168 | |
| 169 | index = &per_cpu(msm_rtb_idx_cpu, cpu); |
| 170 | |
| 171 | i = atomic_add_return(msm_rtb.step_size, index); |
| 172 | i -= msm_rtb.step_size; |
| 173 | |
Jeff Ohlstein | 4e24908 | 2012-03-21 15:12:20 -0700 | [diff] [blame] | 174 | /* Check if index has wrapped around */ |
| 175 | offset = (i & (msm_rtb.nentries - 1)) - |
| 176 | ((i - msm_rtb.step_size) & (msm_rtb.nentries - 1)); |
| 177 | if (offset < 0) { |
| 178 | uncached_logk_timestamp(i); |
| 179 | i = atomic_add_return(msm_rtb.step_size, index); |
| 180 | i -= msm_rtb.step_size; |
| 181 | } |
| 182 | |
Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 183 | return i; |
| 184 | } |
| 185 | #else |
| 186 | static int msm_rtb_get_idx(void) |
| 187 | { |
Jeff Ohlstein | 4e24908 | 2012-03-21 15:12:20 -0700 | [diff] [blame] | 188 | int i, offset; |
Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 189 | |
| 190 | i = atomic_inc_return(&msm_rtb_idx); |
| 191 | i--; |
| 192 | |
Jeff Ohlstein | 4e24908 | 2012-03-21 15:12:20 -0700 | [diff] [blame] | 193 | /* Check if index has wrapped around */ |
| 194 | offset = (i & (msm_rtb.nentries - 1)) - |
| 195 | ((i - 1) & (msm_rtb.nentries - 1)); |
| 196 | if (offset < 0) { |
| 197 | uncached_logk_timestamp(i); |
| 198 | i = atomic_inc_return(&msm_rtb_idx); |
| 199 | i--; |
| 200 | } |
| 201 | |
Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 202 | return i; |
| 203 | } |
| 204 | #endif |
| 205 | |
Sarang Joshi | 31a7724 | 2013-08-28 12:57:00 -0700 | [diff] [blame] | 206 | int notrace uncached_logk_pc(enum logk_event_type log_type, void *caller, |
Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 207 | void *data) |
| 208 | { |
| 209 | int i; |
Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 210 | |
| 211 | if (!msm_rtb_event_should_log(log_type)) |
| 212 | return 0; |
| 213 | |
| 214 | i = msm_rtb_get_idx(); |
| 215 | |
Jeff Ohlstein | 4e24908 | 2012-03-21 15:12:20 -0700 | [diff] [blame] | 216 | uncached_logk_pc_idx(log_type, caller, data, i); |
Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 217 | |
| 218 | return 1; |
| 219 | } |
| 220 | EXPORT_SYMBOL(uncached_logk_pc); |
| 221 | |
Sarang Joshi | 31a7724 | 2013-08-28 12:57:00 -0700 | [diff] [blame] | 222 | noinline int notrace uncached_logk(enum logk_event_type log_type, void *data) |
Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 223 | { |
| 224 | return uncached_logk_pc(log_type, __builtin_return_address(0), data); |
| 225 | } |
| 226 | EXPORT_SYMBOL(uncached_logk); |
| 227 | |
Laura Abbott | f8c03b9 | 2012-02-16 14:57:58 -0800 | [diff] [blame] | 228 | int msm_rtb_probe(struct platform_device *pdev) |
Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 229 | { |
Laura Abbott | f8c03b9 | 2012-02-16 14:57:58 -0800 | [diff] [blame] | 230 | struct msm_rtb_platform_data *d = pdev->dev.platform_data; |
Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 231 | #if defined(CONFIG_MSM_RTB_SEPARATE_CPUS) |
| 232 | unsigned int cpu; |
| 233 | #endif |
Laura Abbott | f7e4404 | 2012-06-22 12:50:32 -0700 | [diff] [blame] | 234 | int ret; |
Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 235 | |
Laura Abbott | f7e4404 | 2012-06-22 12:50:32 -0700 | [diff] [blame] | 236 | if (!pdev->dev.of_node) { |
| 237 | msm_rtb.size = d->size; |
| 238 | } else { |
| 239 | int size; |
| 240 | |
| 241 | ret = of_property_read_u32((&pdev->dev)->of_node, |
| 242 | "qcom,memory-reservation-size", |
| 243 | &size); |
| 244 | |
| 245 | if (ret < 0) |
| 246 | return ret; |
| 247 | |
| 248 | msm_rtb.size = size; |
| 249 | } |
Laura Abbott | f8c03b9 | 2012-02-16 14:57:58 -0800 | [diff] [blame] | 250 | |
Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 251 | if (msm_rtb.size <= 0 || msm_rtb.size > SZ_1M) |
| 252 | return -EINVAL; |
| 253 | |
| 254 | /* |
| 255 | * The ioremap call is made separately to store the physical |
| 256 | * address of the buffer. This is necessary for cases where |
| 257 | * the only way to access the buffer is a physical address. |
| 258 | */ |
| 259 | msm_rtb.phys = allocate_contiguous_ebi_nomap(msm_rtb.size, SZ_4K); |
| 260 | |
| 261 | if (!msm_rtb.phys) |
| 262 | return -ENOMEM; |
| 263 | |
| 264 | msm_rtb.rtb = ioremap(msm_rtb.phys, msm_rtb.size); |
| 265 | |
| 266 | if (!msm_rtb.rtb) { |
| 267 | free_contiguous_memory_by_paddr(msm_rtb.phys); |
| 268 | return -ENOMEM; |
| 269 | } |
| 270 | |
| 271 | msm_rtb.nentries = msm_rtb.size / sizeof(struct msm_rtb_layout); |
| 272 | |
| 273 | /* Round this down to a power of 2 */ |
| 274 | msm_rtb.nentries = __rounddown_pow_of_two(msm_rtb.nentries); |
| 275 | |
| 276 | memset(msm_rtb.rtb, 0, msm_rtb.size); |
| 277 | |
| 278 | |
| 279 | #if defined(CONFIG_MSM_RTB_SEPARATE_CPUS) |
| 280 | for_each_possible_cpu(cpu) { |
| 281 | atomic_t *a = &per_cpu(msm_rtb_idx_cpu, cpu); |
| 282 | atomic_set(a, cpu); |
| 283 | } |
| 284 | msm_rtb.step_size = num_possible_cpus(); |
| 285 | #else |
| 286 | atomic_set(&msm_rtb_idx, 0); |
| 287 | msm_rtb.step_size = 1; |
| 288 | #endif |
| 289 | |
Laura Abbott | a29d731 | 2012-04-09 13:10:01 -0700 | [diff] [blame] | 290 | atomic_notifier_chain_register(&panic_notifier_list, |
| 291 | &msm_rtb_panic_blk); |
Laura Abbott | aaa34a1 | 2012-02-14 15:48:47 -0800 | [diff] [blame] | 292 | msm_rtb.initialized = 1; |
Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 293 | return 0; |
| 294 | } |
Laura Abbott | f8c03b9 | 2012-02-16 14:57:58 -0800 | [diff] [blame] | 295 | |
Laura Abbott | f7e4404 | 2012-06-22 12:50:32 -0700 | [diff] [blame] | 296 | static struct of_device_id msm_match_table[] = { |
| 297 | {.compatible = RTB_COMPAT_STR}, |
| 298 | {}, |
| 299 | }; |
| 300 | EXPORT_COMPAT(RTB_COMPAT_STR); |
| 301 | |
Laura Abbott | f8c03b9 | 2012-02-16 14:57:58 -0800 | [diff] [blame] | 302 | static struct platform_driver msm_rtb_driver = { |
| 303 | .driver = { |
| 304 | .name = "msm_rtb", |
Laura Abbott | f7e4404 | 2012-06-22 12:50:32 -0700 | [diff] [blame] | 305 | .owner = THIS_MODULE, |
| 306 | .of_match_table = msm_match_table |
Laura Abbott | f8c03b9 | 2012-02-16 14:57:58 -0800 | [diff] [blame] | 307 | }, |
| 308 | }; |
| 309 | |
| 310 | static int __init msm_rtb_init(void) |
| 311 | { |
| 312 | return platform_driver_probe(&msm_rtb_driver, msm_rtb_probe); |
| 313 | } |
| 314 | |
| 315 | static void __exit msm_rtb_exit(void) |
| 316 | { |
| 317 | platform_driver_unregister(&msm_rtb_driver); |
| 318 | } |
Laura Abbott | ad340ff | 2012-01-04 14:23:48 -0800 | [diff] [blame] | 319 | module_init(msm_rtb_init) |
Laura Abbott | f8c03b9 | 2012-02-16 14:57:58 -0800 | [diff] [blame] | 320 | module_exit(msm_rtb_exit) |