msm_rtb.c 9.2 KB

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