Skip to content

Commit d593d64

Browse files
Prasad Sodagudiarndb
authored andcommitted
lib: Add register read/write tracing support
Generic MMIO read/write i.e., __raw_{read,write}{b,l,w,q} accessors are typically used to read/write from/to memory mapped registers and can cause hangs or some undefined behaviour in following few cases, * If the access to the register space is unclocked, for example: if there is an access to multimedia(MM) block registers without MM clocks. * If the register space is protected and not set to be accessible from non-secure world, for example: only EL3 (EL: Exception level) access is allowed and any EL2/EL1 access is forbidden. * If xPU(memory/register protection units) is controlling access to certain memory/register space for specific clients. and more... Such cases usually results in instant reboot/SErrors/NOC or interconnect hangs and tracing these register accesses can be very helpful to debug such issues during initial development stages and also in later stages. So use ftrace trace events to log such MMIO register accesses which provides rich feature set such as early enablement of trace events, filtering capability, dumping ftrace logs on console and many more. Sample output: rwmmio_write: __qcom_geni_serial_console_write+0x160/0x1e0 width=32 val=0xa0d5d addr=0xfffffbfffdbff700 rwmmio_post_write: __qcom_geni_serial_console_write+0x160/0x1e0 width=32 val=0xa0d5d addr=0xfffffbfffdbff700 rwmmio_read: qcom_geni_serial_poll_bit+0x94/0x138 width=32 addr=0xfffffbfffdbff610 rwmmio_post_read: qcom_geni_serial_poll_bit+0x94/0x138 width=32 val=0x0 addr=0xfffffbfffdbff610 Co-developed-by: Sai Prakash Ranjan <[email protected]> Signed-off-by: Prasad Sodagudi <[email protected]> Signed-off-by: Sai Prakash Ranjan <[email protected]> Acked-by: Steven Rostedt (Google) <[email protected]> Signed-off-by: Arnd Bergmann <[email protected]>
1 parent 98692f5 commit d593d64

File tree

6 files changed

+157
-0
lines changed

6 files changed

+157
-0
lines changed

arch/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,9 @@ config ARCH_HAS_ELFCORE_COMPAT
13961396
config ARCH_HAS_PARANOID_L1D_FLUSH
13971397
bool
13981398

1399+
config ARCH_HAVE_TRACE_MMIO_ACCESS
1400+
bool
1401+
13991402
config DYNAMIC_SIGFRAME
14001403
bool
14011404

arch/arm64/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ config ARM64
4949
select ARCH_HAS_ZONE_DMA_SET if EXPERT
5050
select ARCH_HAVE_ELF_PROT
5151
select ARCH_HAVE_NMI_SAFE_CMPXCHG
52+
select ARCH_HAVE_TRACE_MMIO_ACCESS
5253
select ARCH_INLINE_READ_LOCK if !PREEMPTION
5354
select ARCH_INLINE_READ_LOCK_BH if !PREEMPTION
5455
select ARCH_INLINE_READ_LOCK_IRQ if !PREEMPTION

include/trace/events/rwmmio.h

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
4+
*/
5+
#undef TRACE_SYSTEM
6+
#define TRACE_SYSTEM rwmmio
7+
8+
#if !defined(_TRACE_RWMMIO_H) || defined(TRACE_HEADER_MULTI_READ)
9+
#define _TRACE_RWMMIO_H
10+
11+
#include <linux/tracepoint.h>
12+
13+
DECLARE_EVENT_CLASS(rwmmio_rw_template,
14+
15+
TP_PROTO(unsigned long caller, u64 val, u8 width, volatile void __iomem *addr),
16+
17+
TP_ARGS(caller, val, width, addr),
18+
19+
TP_STRUCT__entry(
20+
__field(unsigned long, caller)
21+
__field(unsigned long, addr)
22+
__field(u64, val)
23+
__field(u8, width)
24+
),
25+
26+
TP_fast_assign(
27+
__entry->caller = caller;
28+
__entry->val = val;
29+
__entry->addr = (unsigned long)addr;
30+
__entry->width = width;
31+
),
32+
33+
TP_printk("%pS width=%d val=%#llx addr=%#lx",
34+
(void *)__entry->caller, __entry->width,
35+
__entry->val, __entry->addr)
36+
);
37+
38+
DEFINE_EVENT(rwmmio_rw_template, rwmmio_write,
39+
TP_PROTO(unsigned long caller, u64 val, u8 width, volatile void __iomem *addr),
40+
TP_ARGS(caller, val, width, addr)
41+
);
42+
43+
DEFINE_EVENT(rwmmio_rw_template, rwmmio_post_write,
44+
TP_PROTO(unsigned long caller, u64 val, u8 width, volatile void __iomem *addr),
45+
TP_ARGS(caller, val, width, addr)
46+
);
47+
48+
TRACE_EVENT(rwmmio_read,
49+
50+
TP_PROTO(unsigned long caller, u8 width, const volatile void __iomem *addr),
51+
52+
TP_ARGS(caller, width, addr),
53+
54+
TP_STRUCT__entry(
55+
__field(unsigned long, caller)
56+
__field(unsigned long, addr)
57+
__field(u8, width)
58+
),
59+
60+
TP_fast_assign(
61+
__entry->caller = caller;
62+
__entry->addr = (unsigned long)addr;
63+
__entry->width = width;
64+
),
65+
66+
TP_printk("%pS width=%d addr=%#lx",
67+
(void *)__entry->caller, __entry->width, __entry->addr)
68+
);
69+
70+
TRACE_EVENT(rwmmio_post_read,
71+
72+
TP_PROTO(unsigned long caller, u64 val, u8 width, const volatile void __iomem *addr),
73+
74+
TP_ARGS(caller, val, width, addr),
75+
76+
TP_STRUCT__entry(
77+
__field(unsigned long, caller)
78+
__field(unsigned long, addr)
79+
__field(u64, val)
80+
__field(u8, width)
81+
),
82+
83+
TP_fast_assign(
84+
__entry->caller = caller;
85+
__entry->val = val;
86+
__entry->addr = (unsigned long)addr;
87+
__entry->width = width;
88+
),
89+
90+
TP_printk("%pS width=%d val=%#llx addr=%#lx",
91+
(void *)__entry->caller, __entry->width,
92+
__entry->val, __entry->addr)
93+
);
94+
95+
#endif /* _TRACE_RWMMIO_H */
96+
97+
#include <trace/define_trace.h>

lib/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ config INDIRECT_IOMEM_FALLBACK
118118
mmio accesses when the IO memory address is not a registered
119119
emulated region.
120120

121+
config TRACE_MMIO_ACCESS
122+
bool "Register read/write tracing"
123+
depends on TRACING && ARCH_HAVE_TRACE_MMIO_ACCESS
124+
help
125+
Create tracepoints for MMIO read/write operations. These trace events
126+
can be used for logging all MMIO read/write operations.
127+
121128
source "lib/crypto/Kconfig"
122129

123130
config CRC_CCITT

lib/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ lib-y += logic_pio.o
151151

152152
lib-$(CONFIG_INDIRECT_IOMEM) += logic_iomem.o
153153

154+
obj-$(CONFIG_TRACE_MMIO_ACCESS) += trace_readwrite.o
155+
154156
obj-$(CONFIG_GENERIC_HWEIGHT) += hweight.o
155157

156158
obj-$(CONFIG_BTREE) += btree.o

lib/trace_readwrite.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Register read and write tracepoints
4+
*
5+
* Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
6+
*/
7+
8+
#include <linux/ftrace.h>
9+
#include <linux/module.h>
10+
#include <asm-generic/io.h>
11+
12+
#define CREATE_TRACE_POINTS
13+
#include <trace/events/rwmmio.h>
14+
15+
#ifdef CONFIG_TRACE_MMIO_ACCESS
16+
void log_write_mmio(u64 val, u8 width, volatile void __iomem *addr,
17+
unsigned long caller_addr)
18+
{
19+
trace_rwmmio_write(caller_addr, val, width, addr);
20+
}
21+
EXPORT_SYMBOL_GPL(log_write_mmio);
22+
EXPORT_TRACEPOINT_SYMBOL_GPL(rwmmio_write);
23+
24+
void log_post_write_mmio(u64 val, u8 width, volatile void __iomem *addr,
25+
unsigned long caller_addr)
26+
{
27+
trace_rwmmio_post_write(caller_addr, val, width, addr);
28+
}
29+
EXPORT_SYMBOL_GPL(log_post_write_mmio);
30+
EXPORT_TRACEPOINT_SYMBOL_GPL(rwmmio_post_write);
31+
32+
void log_read_mmio(u8 width, const volatile void __iomem *addr,
33+
unsigned long caller_addr)
34+
{
35+
trace_rwmmio_read(caller_addr, width, addr);
36+
}
37+
EXPORT_SYMBOL_GPL(log_read_mmio);
38+
EXPORT_TRACEPOINT_SYMBOL_GPL(rwmmio_read);
39+
40+
void log_post_read_mmio(u64 val, u8 width, const volatile void __iomem *addr,
41+
unsigned long caller_addr)
42+
{
43+
trace_rwmmio_post_read(caller_addr, val, width, addr);
44+
}
45+
EXPORT_SYMBOL_GPL(log_post_read_mmio);
46+
EXPORT_TRACEPOINT_SYMBOL_GPL(rwmmio_post_read);
47+
#endif /* CONFIG_TRACE_MMIO_ACCESS */

0 commit comments

Comments
 (0)