Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 2 | /* |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) IBM Corporation, 2005 |
| 5 | * Jeff Muizelaar, 2006, 2007 |
| 6 | * Pekka Paalanen, 2008 <pq@iki.fi> |
| 7 | * |
| 8 | * Derived from the read-mod example from relay-examples by Tom Zanussi. |
| 9 | */ |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 10 | |
Joe Perches | eba11d6 | 2009-12-13 23:24:03 -0800 | [diff] [blame] | 11 | #define pr_fmt(fmt) "mmiotrace: " fmt |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 12 | |
Paul Gortmaker | 4b599fe | 2016-07-13 20:18:55 -0400 | [diff] [blame] | 13 | #include <linux/moduleparam.h> |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 14 | #include <linux/debugfs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Pekka Paalanen | f984b51 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 16 | #include <linux/uaccess.h> |
Pekka Paalanen | 970e6fa | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 17 | #include <linux/io.h> |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 18 | #include <linux/mmiotrace.h> |
Mike Rapoport | 65fddcf | 2020-06-08 21:32:42 -0700 | [diff] [blame] | 19 | #include <linux/pgtable.h> |
Ingo Molnar | 66441bd | 2017-01-27 10:27:10 +0100 | [diff] [blame] | 20 | #include <asm/e820/api.h> /* for ISA_START_ADDRESS */ |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 21 | #include <linux/atomic.h> |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 22 | #include <linux/percpu.h> |
Pekka Paalanen | 7423d11 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 23 | #include <linux/cpu.h> |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 24 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 25 | #include "pf_in.h" |
| 26 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 27 | struct trap_reason { |
| 28 | unsigned long addr; |
| 29 | unsigned long ip; |
| 30 | enum reason_type type; |
| 31 | int active_traces; |
| 32 | }; |
| 33 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 34 | struct remap_trace { |
| 35 | struct list_head list; |
| 36 | struct kmmio_probe probe; |
Pekka Paalanen | dee310d | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 37 | resource_size_t phys; |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 38 | unsigned long id; |
| 39 | }; |
| 40 | |
Pekka Paalanen | fe1ffaf | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 41 | /* Accessed per-cpu. */ |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 42 | static DEFINE_PER_CPU(struct trap_reason, pf_reason); |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 43 | static DEFINE_PER_CPU(struct mmiotrace_rw, cpu_trace); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 44 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 45 | static DEFINE_MUTEX(mmiotrace_mutex); |
| 46 | static DEFINE_SPINLOCK(trace_lock); |
| 47 | static atomic_t mmiotrace_enabled; |
| 48 | static LIST_HEAD(trace_list); /* struct remap_trace */ |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 49 | |
| 50 | /* |
| 51 | * Locking in this file: |
| 52 | * - mmiotrace_mutex enforces enable/disable_mmiotrace() critical sections. |
| 53 | * - mmiotrace_enabled may be modified only when holding mmiotrace_mutex |
| 54 | * and trace_lock. |
| 55 | * - Routines depending on is_enabled() must take trace_lock. |
| 56 | * - trace_list users must hold trace_lock. |
Pekka Paalanen | 9e57fb3 | 2008-09-16 22:00:34 +0300 | [diff] [blame] | 57 | * - is_enabled() guarantees that mmio_trace_{rw,mapping} are allowed. |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 58 | * - pre/post callbacks assume the effect of is_enabled() being true. |
| 59 | */ |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 60 | |
| 61 | /* module parameters */ |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 62 | static unsigned long filter_offset; |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 63 | static bool nommiotrace; |
| 64 | static bool trace_pc; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 65 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 66 | module_param(filter_offset, ulong, 0); |
| 67 | module_param(nommiotrace, bool, 0); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 68 | module_param(trace_pc, bool, 0); |
| 69 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 70 | MODULE_PARM_DESC(filter_offset, "Start address of traced mappings."); |
| 71 | MODULE_PARM_DESC(nommiotrace, "Disable actual MMIO tracing."); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 72 | MODULE_PARM_DESC(trace_pc, "Record address of faulting instructions."); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 73 | |
| 74 | static bool is_enabled(void) |
| 75 | { |
| 76 | return atomic_read(&mmiotrace_enabled); |
| 77 | } |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 78 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 79 | static void print_pte(unsigned long address) |
| 80 | { |
Pekka Paalanen | 790e2a2 | 2008-05-12 21:21:14 +0200 | [diff] [blame] | 81 | unsigned int level; |
Pekka Paalanen | 75bb883 | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 82 | pte_t *pte = lookup_address(address, &level); |
| 83 | |
| 84 | if (!pte) { |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 85 | pr_err("Error in %s: no pte for page 0x%08lx\n", |
| 86 | __func__, address); |
Pekka Paalanen | 75bb883 | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 87 | return; |
| 88 | } |
| 89 | |
| 90 | if (level == PG_LEVEL_2M) { |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 91 | pr_emerg("4MB pages are not currently supported: 0x%08lx\n", |
| 92 | address); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 93 | BUG(); |
| 94 | } |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 95 | pr_info("pte for 0x%lx: 0x%llx 0x%llx\n", |
| 96 | address, |
Randy Dunlap | 0663bb6 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 97 | (unsigned long long)pte_val(*pte), |
| 98 | (unsigned long long)pte_val(*pte) & _PAGE_PRESENT); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* |
| 102 | * For some reason the pre/post pairs have been called in an |
| 103 | * unmatched order. Report and die. |
| 104 | */ |
| 105 | static void die_kmmio_nesting_error(struct pt_regs *regs, unsigned long addr) |
| 106 | { |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 107 | const struct trap_reason *my_reason = &get_cpu_var(pf_reason); |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 108 | pr_emerg("unexpected fault for address: 0x%08lx, last fault for address: 0x%08lx\n", |
| 109 | addr, my_reason->addr); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 110 | print_pte(addr); |
Sergey Senozhatsky | 7b606162 | 2017-12-11 21:50:20 +0900 | [diff] [blame] | 111 | pr_emerg("faulting IP is at %pS\n", (void *)regs->ip); |
| 112 | pr_emerg("last faulting IP was at %pS\n", (void *)my_reason->ip); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 113 | #ifdef __i386__ |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 114 | pr_emerg("eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n", |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 115 | regs->ax, regs->bx, regs->cx, regs->dx); |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 116 | pr_emerg("esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n", |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 117 | regs->si, regs->di, regs->bp, regs->sp); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 118 | #else |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 119 | pr_emerg("rax: %016lx rcx: %016lx rdx: %016lx\n", |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 120 | regs->ax, regs->cx, regs->dx); |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 121 | pr_emerg("rsi: %016lx rdi: %016lx rbp: %016lx rsp: %016lx\n", |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 122 | regs->si, regs->di, regs->bp, regs->sp); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 123 | #endif |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 124 | put_cpu_var(pf_reason); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 125 | BUG(); |
| 126 | } |
| 127 | |
| 128 | static void pre(struct kmmio_probe *p, struct pt_regs *regs, |
| 129 | unsigned long addr) |
| 130 | { |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 131 | struct trap_reason *my_reason = &get_cpu_var(pf_reason); |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 132 | struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 133 | const unsigned long instptr = instruction_pointer(regs); |
| 134 | const enum reason_type type = get_ins_type(instptr); |
Pekka Paalanen | a50445d | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 135 | struct remap_trace *trace = p->private; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 136 | |
| 137 | /* it doesn't make sense to have more than one active trace per cpu */ |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 138 | if (my_reason->active_traces) |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 139 | die_kmmio_nesting_error(regs, addr); |
| 140 | else |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 141 | my_reason->active_traces++; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 142 | |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 143 | my_reason->type = type; |
| 144 | my_reason->addr = addr; |
| 145 | my_reason->ip = instptr; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 146 | |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 147 | my_trace->phys = addr - trace->probe.addr + trace->phys; |
| 148 | my_trace->map_id = trace->id; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 149 | |
| 150 | /* |
| 151 | * Only record the program counter when requested. |
| 152 | * It may taint clean-room reverse engineering. |
| 153 | */ |
| 154 | if (trace_pc) |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 155 | my_trace->pc = instptr; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 156 | else |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 157 | my_trace->pc = 0; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 158 | |
Pekka Paalanen | f984b51 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 159 | /* |
| 160 | * XXX: the timestamp recorded will be *after* the tracing has been |
| 161 | * done, not at the time we hit the instruction. SMP implications |
| 162 | * on event ordering? |
| 163 | */ |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 164 | |
| 165 | switch (type) { |
| 166 | case REG_READ: |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 167 | my_trace->opcode = MMIO_READ; |
| 168 | my_trace->width = get_ins_mem_width(instptr); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 169 | break; |
| 170 | case REG_WRITE: |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 171 | my_trace->opcode = MMIO_WRITE; |
| 172 | my_trace->width = get_ins_mem_width(instptr); |
| 173 | my_trace->value = get_ins_reg_val(instptr, regs); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 174 | break; |
| 175 | case IMM_WRITE: |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 176 | my_trace->opcode = MMIO_WRITE; |
| 177 | my_trace->width = get_ins_mem_width(instptr); |
| 178 | my_trace->value = get_ins_imm_val(instptr); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 179 | break; |
| 180 | default: |
| 181 | { |
| 182 | unsigned char *ip = (unsigned char *)instptr; |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 183 | my_trace->opcode = MMIO_UNKNOWN_OP; |
| 184 | my_trace->width = 0; |
| 185 | my_trace->value = (*ip) << 16 | *(ip + 1) << 8 | |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 186 | *(ip + 2); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 187 | } |
| 188 | } |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 189 | put_cpu_var(cpu_trace); |
| 190 | put_cpu_var(pf_reason); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static void post(struct kmmio_probe *p, unsigned long condition, |
| 194 | struct pt_regs *regs) |
| 195 | { |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 196 | struct trap_reason *my_reason = &get_cpu_var(pf_reason); |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 197 | struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 198 | |
| 199 | /* this should always return the active_trace count to 0 */ |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 200 | my_reason->active_traces--; |
| 201 | if (my_reason->active_traces) { |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 202 | pr_emerg("unexpected post handler"); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 203 | BUG(); |
| 204 | } |
| 205 | |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 206 | switch (my_reason->type) { |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 207 | case REG_READ: |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 208 | my_trace->value = get_ins_reg_val(my_reason->ip, regs); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 209 | break; |
| 210 | default: |
| 211 | break; |
| 212 | } |
Pekka Paalanen | f984b51 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 213 | |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 214 | mmio_trace_rw(my_trace); |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 215 | put_cpu_var(cpu_trace); |
| 216 | put_cpu_var(pf_reason); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 217 | } |
| 218 | |
Pekka Paalanen | dee310d | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 219 | static void ioremap_trace_core(resource_size_t offset, unsigned long size, |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 220 | void __iomem *addr) |
| 221 | { |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 222 | static atomic_t next_id; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 223 | struct remap_trace *trace = kmalloc(sizeof(*trace), GFP_KERNEL); |
Pekka Paalanen | 87e547f | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 224 | /* These are page-unaligned. */ |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 225 | struct mmiotrace_map map = { |
| 226 | .phys = offset, |
| 227 | .virt = (unsigned long)addr, |
| 228 | .len = size, |
| 229 | .opcode = MMIO_PROBE |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 230 | }; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 231 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 232 | if (!trace) { |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 233 | pr_err("kmalloc failed in ioremap\n"); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 234 | return; |
| 235 | } |
| 236 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 237 | *trace = (struct remap_trace) { |
| 238 | .probe = { |
| 239 | .addr = (unsigned long)addr, |
| 240 | .len = size, |
| 241 | .pre_handler = pre, |
| 242 | .post_handler = post, |
Pekka Paalanen | a50445d | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 243 | .private = trace |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 244 | }, |
| 245 | .phys = offset, |
| 246 | .id = atomic_inc_return(&next_id) |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 247 | }; |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 248 | map.map_id = trace->id; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 249 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 250 | spin_lock_irq(&trace_lock); |
Pekka Paalanen | bbe5c78 | 2008-09-16 21:54:16 +0300 | [diff] [blame] | 251 | if (!is_enabled()) { |
| 252 | kfree(trace); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 253 | goto not_enabled; |
Pekka Paalanen | bbe5c78 | 2008-09-16 21:54:16 +0300 | [diff] [blame] | 254 | } |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 255 | |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 256 | mmio_trace_mapping(&map); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 257 | list_add_tail(&trace->list, &trace_list); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 258 | if (!nommiotrace) |
| 259 | register_kmmio_probe(&trace->probe); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 260 | |
| 261 | not_enabled: |
| 262 | spin_unlock_irq(&trace_lock); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 263 | } |
| 264 | |
Pekka Paalanen | dee310d | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 265 | void mmiotrace_ioremap(resource_size_t offset, unsigned long size, |
| 266 | void __iomem *addr) |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 267 | { |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 268 | if (!is_enabled()) /* recheck and proper locking in *_core() */ |
| 269 | return; |
| 270 | |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 271 | pr_debug("ioremap_*(0x%llx, 0x%lx) = %p\n", |
| 272 | (unsigned long long)offset, size, addr); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 273 | if ((filter_offset) && (offset != filter_offset)) |
| 274 | return; |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 275 | ioremap_trace_core(offset, size, addr); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 276 | } |
| 277 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 278 | static void iounmap_trace_core(volatile void __iomem *addr) |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 279 | { |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 280 | struct mmiotrace_map map = { |
| 281 | .phys = 0, |
| 282 | .virt = (unsigned long)addr, |
| 283 | .len = 0, |
| 284 | .opcode = MMIO_UNPROBE |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 285 | }; |
| 286 | struct remap_trace *trace; |
| 287 | struct remap_trace *tmp; |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 288 | struct remap_trace *found_trace = NULL; |
| 289 | |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 290 | pr_debug("Unmapping %p.\n", addr); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 291 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 292 | spin_lock_irq(&trace_lock); |
| 293 | if (!is_enabled()) |
| 294 | goto not_enabled; |
| 295 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 296 | list_for_each_entry_safe(trace, tmp, &trace_list, list) { |
| 297 | if ((unsigned long)addr == trace->probe.addr) { |
| 298 | if (!nommiotrace) |
| 299 | unregister_kmmio_probe(&trace->probe); |
| 300 | list_del(&trace->list); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 301 | found_trace = trace; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 302 | break; |
| 303 | } |
| 304 | } |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 305 | map.map_id = (found_trace) ? found_trace->id : -1; |
| 306 | mmio_trace_mapping(&map); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 307 | |
| 308 | not_enabled: |
| 309 | spin_unlock_irq(&trace_lock); |
| 310 | if (found_trace) { |
| 311 | synchronize_rcu(); /* unregister_kmmio_probe() requirement */ |
| 312 | kfree(found_trace); |
| 313 | } |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 314 | } |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 315 | |
| 316 | void mmiotrace_iounmap(volatile void __iomem *addr) |
| 317 | { |
| 318 | might_sleep(); |
| 319 | if (is_enabled()) /* recheck and proper locking in *_core() */ |
| 320 | iounmap_trace_core(addr); |
| 321 | } |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 322 | |
Pekka Paalanen | 9e57fb3 | 2008-09-16 22:00:34 +0300 | [diff] [blame] | 323 | int mmiotrace_printk(const char *fmt, ...) |
| 324 | { |
| 325 | int ret = 0; |
| 326 | va_list args; |
| 327 | unsigned long flags; |
| 328 | va_start(args, fmt); |
| 329 | |
| 330 | spin_lock_irqsave(&trace_lock, flags); |
| 331 | if (is_enabled()) |
| 332 | ret = mmio_trace_printk(fmt, args); |
| 333 | spin_unlock_irqrestore(&trace_lock, flags); |
| 334 | |
| 335 | va_end(args); |
| 336 | return ret; |
| 337 | } |
| 338 | EXPORT_SYMBOL(mmiotrace_printk); |
| 339 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 340 | static void clear_trace_list(void) |
| 341 | { |
| 342 | struct remap_trace *trace; |
| 343 | struct remap_trace *tmp; |
| 344 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 345 | /* |
| 346 | * No locking required, because the caller ensures we are in a |
| 347 | * critical section via mutex, and is_enabled() is false, |
| 348 | * i.e. nothing can traverse or modify this list. |
| 349 | * Caller also ensures is_enabled() cannot change. |
| 350 | */ |
| 351 | list_for_each_entry(trace, &trace_list, list) { |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 352 | pr_notice("purging non-iounmapped trace @0x%08lx, size 0x%lx.\n", |
| 353 | trace->probe.addr, trace->probe.len); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 354 | if (!nommiotrace) |
| 355 | unregister_kmmio_probe(&trace->probe); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 356 | } |
| 357 | synchronize_rcu(); /* unregister_kmmio_probe() requirement */ |
| 358 | |
| 359 | list_for_each_entry_safe(trace, tmp, &trace_list, list) { |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 360 | list_del(&trace->list); |
| 361 | kfree(trace); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
Pekka Paalanen | 7423d11 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 365 | #ifdef CONFIG_HOTPLUG_CPU |
Rusty Russell | c38da56 | 2009-03-11 16:33:55 +1030 | [diff] [blame] | 366 | static cpumask_var_t downed_cpus; |
Pekka Paalanen | 7423d11 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 367 | |
| 368 | static void enter_uniprocessor(void) |
| 369 | { |
| 370 | int cpu; |
| 371 | int err; |
| 372 | |
Nathan Chancellor | d7110a2 | 2020-04-08 13:53:23 -0700 | [diff] [blame] | 373 | if (!cpumask_available(downed_cpus) && |
Rusty Russell | c38da56 | 2009-03-11 16:33:55 +1030 | [diff] [blame] | 374 | !alloc_cpumask_var(&downed_cpus, GFP_KERNEL)) { |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 375 | pr_notice("Failed to allocate mask\n"); |
Rusty Russell | c38da56 | 2009-03-11 16:33:55 +1030 | [diff] [blame] | 376 | goto out; |
| 377 | } |
| 378 | |
Sebastian Andrzej Siewior | 77ad320 | 2021-08-03 16:15:49 +0200 | [diff] [blame] | 379 | cpus_read_lock(); |
Rusty Russell | c38da56 | 2009-03-11 16:33:55 +1030 | [diff] [blame] | 380 | cpumask_copy(downed_cpus, cpu_online_mask); |
| 381 | cpumask_clear_cpu(cpumask_first(cpu_online_mask), downed_cpus); |
Pekka Paalanen | 7423d11 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 382 | if (num_online_cpus() > 1) |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 383 | pr_notice("Disabling non-boot CPUs...\n"); |
Sebastian Andrzej Siewior | 77ad320 | 2021-08-03 16:15:49 +0200 | [diff] [blame] | 384 | cpus_read_unlock(); |
Pekka Paalanen | 7423d11 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 385 | |
Rusty Russell | c38da56 | 2009-03-11 16:33:55 +1030 | [diff] [blame] | 386 | for_each_cpu(cpu, downed_cpus) { |
Qais Yousef | af7aa04 | 2020-03-23 13:51:02 +0000 | [diff] [blame] | 387 | err = remove_cpu(cpu); |
Pekka Paalanen | 970e6fa | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 388 | if (!err) |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 389 | pr_info("CPU%d is down.\n", cpu); |
Pekka Paalanen | 970e6fa | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 390 | else |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 391 | pr_err("Error taking CPU%d down: %d\n", cpu, err); |
Pekka Paalanen | 7423d11 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 392 | } |
Rusty Russell | c38da56 | 2009-03-11 16:33:55 +1030 | [diff] [blame] | 393 | out: |
Pekka Paalanen | 7423d11 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 394 | if (num_online_cpus() > 1) |
Kefeng Wang | 8d3bcc4 | 2019-10-18 11:18:24 +0800 | [diff] [blame] | 395 | pr_warn("multiple CPUs still online, may miss events.\n"); |
Pekka Paalanen | 7423d11 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 396 | } |
| 397 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 398 | static void leave_uniprocessor(void) |
Pekka Paalanen | 7423d11 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 399 | { |
| 400 | int cpu; |
| 401 | int err; |
| 402 | |
Nathan Chancellor | d7110a2 | 2020-04-08 13:53:23 -0700 | [diff] [blame] | 403 | if (!cpumask_available(downed_cpus) || cpumask_weight(downed_cpus) == 0) |
Pekka Paalanen | 7423d11 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 404 | return; |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 405 | pr_notice("Re-enabling CPUs...\n"); |
Rusty Russell | c38da56 | 2009-03-11 16:33:55 +1030 | [diff] [blame] | 406 | for_each_cpu(cpu, downed_cpus) { |
Qais Yousef | af7aa04 | 2020-03-23 13:51:02 +0000 | [diff] [blame] | 407 | err = add_cpu(cpu); |
Pekka Paalanen | 7423d11 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 408 | if (!err) |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 409 | pr_info("enabled CPU%d.\n", cpu); |
Pekka Paalanen | 7423d11 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 410 | else |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 411 | pr_err("cannot re-enable CPU%d: %d\n", cpu, err); |
Pekka Paalanen | 7423d11 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
| 415 | #else /* !CONFIG_HOTPLUG_CPU */ |
| 416 | static void enter_uniprocessor(void) |
| 417 | { |
| 418 | if (num_online_cpus() > 1) |
Kefeng Wang | 8d3bcc4 | 2019-10-18 11:18:24 +0800 | [diff] [blame] | 419 | pr_warn("multiple CPUs are online, may miss events. " |
| 420 | "Suggest booting with maxcpus=1 kernel argument.\n"); |
Pekka Paalanen | 7423d11 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | static void leave_uniprocessor(void) |
| 424 | { |
| 425 | } |
| 426 | #endif |
| 427 | |
Pekka Paalanen | f984b51 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 428 | void enable_mmiotrace(void) |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 429 | { |
| 430 | mutex_lock(&mmiotrace_mutex); |
| 431 | if (is_enabled()) |
| 432 | goto out; |
| 433 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 434 | if (nommiotrace) |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 435 | pr_info("MMIO tracing disabled.\n"); |
Stuart Bennett | 0f9a623 | 2009-04-28 20:17:51 +0100 | [diff] [blame] | 436 | kmmio_init(); |
Pekka Paalanen | 7423d11 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 437 | enter_uniprocessor(); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 438 | spin_lock_irq(&trace_lock); |
| 439 | atomic_inc(&mmiotrace_enabled); |
| 440 | spin_unlock_irq(&trace_lock); |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 441 | pr_info("enabled.\n"); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 442 | out: |
| 443 | mutex_unlock(&mmiotrace_mutex); |
| 444 | } |
| 445 | |
Pekka Paalanen | f984b51 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 446 | void disable_mmiotrace(void) |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 447 | { |
| 448 | mutex_lock(&mmiotrace_mutex); |
| 449 | if (!is_enabled()) |
| 450 | goto out; |
| 451 | |
| 452 | spin_lock_irq(&trace_lock); |
| 453 | atomic_dec(&mmiotrace_enabled); |
| 454 | BUG_ON(is_enabled()); |
| 455 | spin_unlock_irq(&trace_lock); |
| 456 | |
| 457 | clear_trace_list(); /* guarantees: no more kmmio callbacks */ |
Pekka Paalanen | 7423d11 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 458 | leave_uniprocessor(); |
Stuart Bennett | 0f9a623 | 2009-04-28 20:17:51 +0100 | [diff] [blame] | 459 | kmmio_cleanup(); |
Joe Perches | 3a0340b | 2009-12-09 10:45:37 -0800 | [diff] [blame] | 460 | pr_info("disabled.\n"); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 461 | out: |
| 462 | mutex_unlock(&mmiotrace_mutex); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 463 | } |