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