123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- /*
- * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 and
- * only version 2 as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- */
- #include <linux/atomic.h>
- #include <linux/export.h>
- #include <linux/kernel.h>
- #include <linux/memory_alloc.h>
- #include <linux/module.h>
- #include <linux/mod_devicetable.h>
- #include <linux/platform_device.h>
- #include <linux/sched.h>
- #include <linux/slab.h>
- #include <linux/string.h>
- #include <linux/atomic.h>
- #include <linux/of.h>
- #if defined(CONFIG_MSM_RTB_TIMESTAMP)
- #include <linux/io.h>
- #else
- #include <asm/io.h>
- #endif
- #include <asm-generic/sizes.h>
- #include <mach/memory.h>
- #include <mach/msm_rtb.h>
- #include <mach/system.h>
- #define SENTINEL_BYTE_1 0xFF
- #define SENTINEL_BYTE_2 0xAA
- #define SENTINEL_BYTE_3 0xFF
- #define RTB_COMPAT_STR "qcom,msm-rtb"
- /* If enalbe "CONFIG_MSM_RTB_TIMESTAMP"
- * Write
- * 1) 3 bytes sentinel
- * 2) 1 bytes of log type
- * 3) 8 bytes of where the caller came from
- * 4) 4 bytes index
- * 4) 8 bytes extra data from the caller
- * 5) 8 bytes for timestamp
- *
- * Total = 32 bytes.
- * If disable "CONFIG_MSM_RTB_TIMESTAMP"
- * Write
- * 1) 3 bytes sentinel
- * 2) 1 bytes of log type
- * 3) 4 bytes of where the caller came from
- * 4) 4 bytes index
- * 4) 4 bytes extra data from the caller
- *
- * Total = 16 bytes.
- */
- struct msm_rtb_layout {
- unsigned char sentinel[3];
- unsigned char log_type;
- #if defined(CONFIG_MSM_RTB_TIMESTAMP)
- uint32_t idx;
- uint64_t caller;
- uint64_t data;
- uint64_t timestamp;
- #else
- void *caller;
- unsigned long idx;
- void *data;
- #endif
- } __attribute__ ((__packed__));
- struct msm_rtb_state {
- struct msm_rtb_layout *rtb;
- phys_addr_t phys;
- int nentries;
- int size;
- int enabled;
- int initialized;
- uint32_t filter;
- int step_size;
- };
- #if defined(CONFIG_MSM_RTB_SEPARATE_CPUS)
- DEFINE_PER_CPU(atomic_t, msm_rtb_idx_cpu);
- #else
- static atomic_t msm_rtb_idx;
- #endif
- #if defined(CONFIG_MSM_RTB_TIMESTAMP)
- static struct msm_rtb_state msm_rtb = {
- .filter = 1 << LOGK_LOGBUF,
- .enabled = 0,
- };
- #else
- struct msm_rtb_state msm_rtb = {
- .filter = 1 << LOGK_LOGBUF,
- .enabled = 0,
- };
- #endif
- module_param_named(filter, msm_rtb.filter, uint, 0644);
- module_param_named(enable, msm_rtb.enabled, int, 0644);
- static int msm_rtb_panic_notifier(struct notifier_block *this,
- unsigned long event, void *ptr)
- {
- msm_rtb.enabled = 0;
- return NOTIFY_DONE;
- }
- static struct notifier_block msm_rtb_panic_blk = {
- .notifier_call = msm_rtb_panic_notifier,
- };
- int notrace msm_rtb_event_should_log(enum logk_event_type log_type)
- {
- return msm_rtb.initialized && msm_rtb.enabled &&
- ((1 << (log_type & ~LOGTYPE_NOPC)) & msm_rtb.filter);
- }
- EXPORT_SYMBOL(msm_rtb_event_should_log);
- static void msm_rtb_emit_sentinel(struct msm_rtb_layout *start)
- {
- start->sentinel[0] = SENTINEL_BYTE_1;
- start->sentinel[1] = SENTINEL_BYTE_2;
- start->sentinel[2] = SENTINEL_BYTE_3;
- }
- static void msm_rtb_write_type(enum logk_event_type log_type,
- struct msm_rtb_layout *start)
- {
- start->log_type = (char)log_type;
- }
- #if defined(CONFIG_MSM_RTB_TIMESTAMP)
- static void msm_rtb_write_caller(uint64_t caller, struct msm_rtb_layout *start)
- #else
- static void msm_rtb_write_caller(void *caller, struct msm_rtb_layout *start)
- #endif
- {
- start->caller = caller;
- }
- #if defined(CONFIG_MSM_RTB_TIMESTAMP)
- static void msm_rtb_write_idx(uint32_t idx, struct msm_rtb_layout *start)
- #else
- static void msm_rtb_write_idx(unsigned long idx, struct msm_rtb_layout *start)
- #endif
- {
- start->idx = idx;
- }
- #if defined(CONFIG_MSM_RTB_TIMESTAMP)
- static void msm_rtb_write_data(uint64_t data, struct msm_rtb_layout *start)
- #else
- static void msm_rtb_write_data(void *data, struct msm_rtb_layout *start)
- #endif
- {
- start->data = data;
- }
- #if defined(CONFIG_MSM_RTB_TIMESTAMP)
- void msm_rtb_write_timestamp(struct msm_rtb_layout *start)
- {
- start->timestamp = sched_clock();
- }
- #endif
- #if defined(CONFIG_MSM_RTB_TIMESTAMP)
- static void uncached_logk_pc_idx(enum logk_event_type log_type, uint64_t caller,
- uint64_t data, int idx)
- #else
- static void uncached_logk_pc_idx(enum logk_event_type log_type, void *caller,
- void *data, int idx)
- #endif
- {
- struct msm_rtb_layout *start;
- start = &msm_rtb.rtb[idx & (msm_rtb.nentries - 1)];
- msm_rtb_emit_sentinel(start);
- msm_rtb_write_type(log_type, start);
- msm_rtb_write_caller(caller, start);
- msm_rtb_write_idx(idx, start);
- msm_rtb_write_data(data, start);
- #if defined(CONFIG_MSM_RTB_TIMESTAMP)
- msm_rtb_write_timestamp(start);
- #endif
- mb();
- return;
- }
- static void uncached_logk_timestamp(int idx)
- {
- unsigned long long timestamp;
- #if !defined(CONFIG_MSM_RTB_TIMESTAMP)
- void *timestamp_upper, *timestamp_lower;
- #endif
- timestamp = sched_clock();
- #if defined(CONFIG_MSM_RTB_TIMESTAMP)
- uncached_logk_pc_idx(LOGK_TIMESTAMP|LOGTYPE_NOPC,
- (uint64_t)lower_32_bits(timestamp),
- (uint64_t)upper_32_bits(timestamp), idx);
- #else
- timestamp_lower = (void *)lower_32_bits(timestamp);
- timestamp_upper = (void *)upper_32_bits(timestamp);
- uncached_logk_pc_idx(LOGK_TIMESTAMP|LOGTYPE_NOPC, timestamp_lower,
- timestamp_upper, idx);
- #endif
- }
- #if defined(CONFIG_MSM_RTB_SEPARATE_CPUS)
- static int msm_rtb_get_idx(void)
- {
- int cpu, i, offset;
- atomic_t *index;
- /*
- * ideally we would use get_cpu but this is a close enough
- * approximation for our purposes.
- */
- cpu = raw_smp_processor_id();
- index = &per_cpu(msm_rtb_idx_cpu, cpu);
- i = atomic_add_return(msm_rtb.step_size, index);
- i -= msm_rtb.step_size;
- /* Check if index has wrapped around */
- offset = (i & (msm_rtb.nentries - 1)) -
- ((i - msm_rtb.step_size) & (msm_rtb.nentries - 1));
- if (offset < 0) {
- uncached_logk_timestamp(i);
- i = atomic_add_return(msm_rtb.step_size, index);
- i -= msm_rtb.step_size;
- }
- return i;
- }
- #else
- static int msm_rtb_get_idx(void)
- {
- int i, offset;
- i = atomic_inc_return(&msm_rtb_idx);
- i--;
- /* Check if index has wrapped around */
- offset = (i & (msm_rtb.nentries - 1)) -
- ((i - 1) & (msm_rtb.nentries - 1));
- if (offset < 0) {
- uncached_logk_timestamp(i);
- i = atomic_inc_return(&msm_rtb_idx);
- i--;
- }
- return i;
- }
- #endif
- int notrace uncached_logk_pc(enum logk_event_type log_type, void *caller,
- void *data)
- {
- int i;
- if (!msm_rtb_event_should_log(log_type))
- return 0;
- i = msm_rtb_get_idx();
- #if defined(CONFIG_MSM_RTB_TIMESTAMP)
- uncached_logk_pc_idx(log_type, (uint64_t)((unsigned long) caller),
- (uint64_t)((unsigned long) data), i);
- #else
- uncached_logk_pc_idx(log_type, caller, data, i);
- #endif
- return 1;
- }
- EXPORT_SYMBOL(uncached_logk_pc);
- noinline int notrace uncached_logk(enum logk_event_type log_type, void *data)
- {
- return uncached_logk_pc(log_type, __builtin_return_address(0), data);
- }
- EXPORT_SYMBOL(uncached_logk);
- #if defined(CONFIG_MSM_RTB_TIMESTAMP)
- static int msm_rtb_probe(struct platform_device *pdev)
- #else
- int msm_rtb_probe(struct platform_device *pdev)
- #endif
- {
- struct msm_rtb_platform_data *d = pdev->dev.platform_data;
- #if defined(CONFIG_MSM_RTB_SEPARATE_CPUS)
- unsigned int cpu;
- #endif
- int ret;
- if (!pdev->dev.of_node) {
- msm_rtb.size = d->size;
- } else {
- int size;
- ret = of_property_read_u32((&pdev->dev)->of_node,
- "qcom,memory-reservation-size",
- &size);
- if (ret < 0)
- return ret;
- msm_rtb.size = size;
- }
- if (msm_rtb.size <= 0 || msm_rtb.size > SZ_1M)
- return -EINVAL;
- /*
- * The ioremap call is made separately to store the physical
- * address of the buffer. This is necessary for cases where
- * the only way to access the buffer is a physical address.
- */
- msm_rtb.phys = allocate_contiguous_ebi_nomap(msm_rtb.size, SZ_4K);
- if (!msm_rtb.phys)
- return -ENOMEM;
- msm_rtb.rtb = ioremap(msm_rtb.phys, msm_rtb.size);
- if (!msm_rtb.rtb) {
- free_contiguous_memory_by_paddr(msm_rtb.phys);
- return -ENOMEM;
- }
- msm_rtb.nentries = msm_rtb.size / sizeof(struct msm_rtb_layout);
- /* Round this down to a power of 2 */
- msm_rtb.nentries = __rounddown_pow_of_two(msm_rtb.nentries);
- memset(msm_rtb.rtb, 0, msm_rtb.size);
- #if defined(CONFIG_MSM_RTB_SEPARATE_CPUS)
- for_each_possible_cpu(cpu) {
- atomic_t *a = &per_cpu(msm_rtb_idx_cpu, cpu);
- atomic_set(a, cpu);
- }
- msm_rtb.step_size = num_possible_cpus();
- #else
- atomic_set(&msm_rtb_idx, 0);
- msm_rtb.step_size = 1;
- #endif
- atomic_notifier_chain_register(&panic_notifier_list,
- &msm_rtb_panic_blk);
- #ifdef CONFIG_ARCH_MSM8226
- /* Adding msm_rtb.filter" here, as removed from Board kernel cmdline */
- msm_rtb.filter = 0x37;
- #endif
- msm_rtb.initialized = 1;
- return 0;
- }
- static struct of_device_id msm_match_table[] = {
- {.compatible = RTB_COMPAT_STR},
- {},
- };
- #if !defined(CONFIG_MSM_RTB_TIMESTAMP)
- EXPORT_COMPAT(RTB_COMPAT_STR);
- #endif
- static struct platform_driver msm_rtb_driver = {
- .driver = {
- .name = "msm_rtb",
- .owner = THIS_MODULE,
- .of_match_table = msm_match_table
- },
- };
- static int __init msm_rtb_init(void)
- {
- return platform_driver_probe(&msm_rtb_driver, msm_rtb_probe);
- }
- static void __exit msm_rtb_exit(void)
- {
- platform_driver_unregister(&msm_rtb_driver);
- }
- module_init(msm_rtb_init)
- module_exit(msm_rtb_exit)
|