blob: 2c1ecf4763c4dc4d019c663b5b919e8a9b7901e8 [file] [log] [blame]
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +02001/*
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 Perches3a0340b2009-12-09 10:45:37 -080022
Joe Percheseba11d62009-12-13 23:24:03 -080023#define pr_fmt(fmt) "mmiotrace: " fmt
Joe Perches3a0340b2009-12-09 10:45:37 -080024
Pekka Paalanend61fc442008-05-12 21:20:57 +020025#define DEBUG 1
26
Paul Gortmaker4b599fe2016-07-13 20:18:55 -040027#include <linux/moduleparam.h>
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020028#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Pekka Paalanenf984b512008-05-12 21:20:57 +020030#include <linux/uaccess.h>
Pekka Paalanen970e6fa2008-05-12 21:21:03 +020031#include <linux/io.h>
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020032#include <asm/pgtable.h>
33#include <linux/mmiotrace.h>
Ingo Molnar66441bd2017-01-27 10:27:10 +010034#include <asm/e820/api.h> /* for ISA_START_ADDRESS */
Arun Sharma600634972011-07-26 16:09:06 -070035#include <linux/atomic.h>
Pekka Paalanenf5136382008-05-12 21:20:57 +020036#include <linux/percpu.h>
Pekka Paalanen7423d112008-05-12 21:21:02 +020037#include <linux/cpu.h>
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020038
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020039#include "pf_in.h"
40
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020041struct trap_reason {
42 unsigned long addr;
43 unsigned long ip;
44 enum reason_type type;
45 int active_traces;
46};
47
Pekka Paalanend61fc442008-05-12 21:20:57 +020048struct remap_trace {
49 struct list_head list;
50 struct kmmio_probe probe;
Pekka Paalanendee310d2008-05-12 21:21:03 +020051 resource_size_t phys;
Pekka Paalanend61fc442008-05-12 21:20:57 +020052 unsigned long id;
53};
54
Pekka Paalanenfe1ffaf2008-05-12 21:20:56 +020055/* Accessed per-cpu. */
Pekka Paalanenf5136382008-05-12 21:20:57 +020056static DEFINE_PER_CPU(struct trap_reason, pf_reason);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +020057static DEFINE_PER_CPU(struct mmiotrace_rw, cpu_trace);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020058
Pekka Paalanend61fc442008-05-12 21:20:57 +020059static DEFINE_MUTEX(mmiotrace_mutex);
60static DEFINE_SPINLOCK(trace_lock);
61static atomic_t mmiotrace_enabled;
62static LIST_HEAD(trace_list); /* struct remap_trace */
Pekka Paalanend61fc442008-05-12 21:20:57 +020063
64/*
65 * Locking in this file:
66 * - mmiotrace_mutex enforces enable/disable_mmiotrace() critical sections.
67 * - mmiotrace_enabled may be modified only when holding mmiotrace_mutex
68 * and trace_lock.
69 * - Routines depending on is_enabled() must take trace_lock.
70 * - trace_list users must hold trace_lock.
Pekka Paalanen9e57fb32008-09-16 22:00:34 +030071 * - is_enabled() guarantees that mmio_trace_{rw,mapping} are allowed.
Pekka Paalanend61fc442008-05-12 21:20:57 +020072 * - pre/post callbacks assume the effect of is_enabled() being true.
73 */
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020074
75/* module parameters */
Pekka Paalanend61fc442008-05-12 21:20:57 +020076static unsigned long filter_offset;
Rusty Russell476bc002012-01-13 09:32:18 +103077static bool nommiotrace;
78static bool trace_pc;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020079
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020080module_param(filter_offset, ulong, 0);
81module_param(nommiotrace, bool, 0);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020082module_param(trace_pc, bool, 0);
83
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020084MODULE_PARM_DESC(filter_offset, "Start address of traced mappings.");
85MODULE_PARM_DESC(nommiotrace, "Disable actual MMIO tracing.");
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020086MODULE_PARM_DESC(trace_pc, "Record address of faulting instructions.");
Pekka Paalanend61fc442008-05-12 21:20:57 +020087
88static bool is_enabled(void)
89{
90 return atomic_read(&mmiotrace_enabled);
91}
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020092
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020093static void print_pte(unsigned long address)
94{
Pekka Paalanen790e2a22008-05-12 21:21:14 +020095 unsigned int level;
Pekka Paalanen75bb8832008-05-12 21:20:56 +020096 pte_t *pte = lookup_address(address, &level);
97
98 if (!pte) {
Joe Perches3a0340b2009-12-09 10:45:37 -080099 pr_err("Error in %s: no pte for page 0x%08lx\n",
100 __func__, address);
Pekka Paalanen75bb8832008-05-12 21:20:56 +0200101 return;
102 }
103
104 if (level == PG_LEVEL_2M) {
Joe Perches3a0340b2009-12-09 10:45:37 -0800105 pr_emerg("4MB pages are not currently supported: 0x%08lx\n",
106 address);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200107 BUG();
108 }
Joe Perches3a0340b2009-12-09 10:45:37 -0800109 pr_info("pte for 0x%lx: 0x%llx 0x%llx\n",
110 address,
Randy Dunlap0663bb62008-05-12 21:20:59 +0200111 (unsigned long long)pte_val(*pte),
112 (unsigned long long)pte_val(*pte) & _PAGE_PRESENT);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200113}
114
115/*
116 * For some reason the pre/post pairs have been called in an
117 * unmatched order. Report and die.
118 */
119static void die_kmmio_nesting_error(struct pt_regs *regs, unsigned long addr)
120{
Pekka Paalanenf5136382008-05-12 21:20:57 +0200121 const struct trap_reason *my_reason = &get_cpu_var(pf_reason);
Joe Perches3a0340b2009-12-09 10:45:37 -0800122 pr_emerg("unexpected fault for address: 0x%08lx, last fault for address: 0x%08lx\n",
123 addr, my_reason->addr);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200124 print_pte(addr);
Sergey Senozhatsky7b6061622017-12-11 21:50:20 +0900125 pr_emerg("faulting IP is at %pS\n", (void *)regs->ip);
126 pr_emerg("last faulting IP was at %pS\n", (void *)my_reason->ip);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200127#ifdef __i386__
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200128 pr_emerg("eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n",
Joe Perches3a0340b2009-12-09 10:45:37 -0800129 regs->ax, regs->bx, regs->cx, regs->dx);
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200130 pr_emerg("esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n",
Joe Perches3a0340b2009-12-09 10:45:37 -0800131 regs->si, regs->di, regs->bp, regs->sp);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200132#else
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200133 pr_emerg("rax: %016lx rcx: %016lx rdx: %016lx\n",
Joe Perches3a0340b2009-12-09 10:45:37 -0800134 regs->ax, regs->cx, regs->dx);
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200135 pr_emerg("rsi: %016lx rdi: %016lx rbp: %016lx rsp: %016lx\n",
Joe Perches3a0340b2009-12-09 10:45:37 -0800136 regs->si, regs->di, regs->bp, regs->sp);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200137#endif
Pekka Paalanenf5136382008-05-12 21:20:57 +0200138 put_cpu_var(pf_reason);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200139 BUG();
140}
141
142static void pre(struct kmmio_probe *p, struct pt_regs *regs,
143 unsigned long addr)
144{
Pekka Paalanenf5136382008-05-12 21:20:57 +0200145 struct trap_reason *my_reason = &get_cpu_var(pf_reason);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200146 struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200147 const unsigned long instptr = instruction_pointer(regs);
148 const enum reason_type type = get_ins_type(instptr);
Pekka Paalanena50445d2008-05-12 21:21:03 +0200149 struct remap_trace *trace = p->private;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200150
151 /* it doesn't make sense to have more than one active trace per cpu */
Pekka Paalanenf5136382008-05-12 21:20:57 +0200152 if (my_reason->active_traces)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200153 die_kmmio_nesting_error(regs, addr);
154 else
Pekka Paalanenf5136382008-05-12 21:20:57 +0200155 my_reason->active_traces++;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200156
Pekka Paalanenf5136382008-05-12 21:20:57 +0200157 my_reason->type = type;
158 my_reason->addr = addr;
159 my_reason->ip = instptr;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200160
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200161 my_trace->phys = addr - trace->probe.addr + trace->phys;
162 my_trace->map_id = trace->id;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200163
164 /*
165 * Only record the program counter when requested.
166 * It may taint clean-room reverse engineering.
167 */
168 if (trace_pc)
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200169 my_trace->pc = instptr;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200170 else
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200171 my_trace->pc = 0;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200172
Pekka Paalanenf984b512008-05-12 21:20:57 +0200173 /*
174 * XXX: the timestamp recorded will be *after* the tracing has been
175 * done, not at the time we hit the instruction. SMP implications
176 * on event ordering?
177 */
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200178
179 switch (type) {
180 case REG_READ:
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200181 my_trace->opcode = MMIO_READ;
182 my_trace->width = get_ins_mem_width(instptr);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200183 break;
184 case REG_WRITE:
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200185 my_trace->opcode = MMIO_WRITE;
186 my_trace->width = get_ins_mem_width(instptr);
187 my_trace->value = get_ins_reg_val(instptr, regs);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200188 break;
189 case IMM_WRITE:
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200190 my_trace->opcode = MMIO_WRITE;
191 my_trace->width = get_ins_mem_width(instptr);
192 my_trace->value = get_ins_imm_val(instptr);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200193 break;
194 default:
195 {
196 unsigned char *ip = (unsigned char *)instptr;
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200197 my_trace->opcode = MMIO_UNKNOWN_OP;
198 my_trace->width = 0;
199 my_trace->value = (*ip) << 16 | *(ip + 1) << 8 |
Pekka Paalanenf5136382008-05-12 21:20:57 +0200200 *(ip + 2);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200201 }
202 }
Pekka Paalanenf5136382008-05-12 21:20:57 +0200203 put_cpu_var(cpu_trace);
204 put_cpu_var(pf_reason);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200205}
206
207static void post(struct kmmio_probe *p, unsigned long condition,
208 struct pt_regs *regs)
209{
Pekka Paalanenf5136382008-05-12 21:20:57 +0200210 struct trap_reason *my_reason = &get_cpu_var(pf_reason);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200211 struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200212
213 /* this should always return the active_trace count to 0 */
Pekka Paalanenf5136382008-05-12 21:20:57 +0200214 my_reason->active_traces--;
215 if (my_reason->active_traces) {
Joe Perches3a0340b2009-12-09 10:45:37 -0800216 pr_emerg("unexpected post handler");
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200217 BUG();
218 }
219
Pekka Paalanenf5136382008-05-12 21:20:57 +0200220 switch (my_reason->type) {
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200221 case REG_READ:
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200222 my_trace->value = get_ins_reg_val(my_reason->ip, regs);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200223 break;
224 default:
225 break;
226 }
Pekka Paalanenf984b512008-05-12 21:20:57 +0200227
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200228 mmio_trace_rw(my_trace);
Pekka Paalanenf5136382008-05-12 21:20:57 +0200229 put_cpu_var(cpu_trace);
230 put_cpu_var(pf_reason);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200231}
232
Pekka Paalanendee310d2008-05-12 21:21:03 +0200233static void ioremap_trace_core(resource_size_t offset, unsigned long size,
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200234 void __iomem *addr)
235{
Pekka Paalanend61fc442008-05-12 21:20:57 +0200236 static atomic_t next_id;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200237 struct remap_trace *trace = kmalloc(sizeof(*trace), GFP_KERNEL);
Pekka Paalanen87e547f2008-05-12 21:21:03 +0200238 /* These are page-unaligned. */
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200239 struct mmiotrace_map map = {
240 .phys = offset,
241 .virt = (unsigned long)addr,
242 .len = size,
243 .opcode = MMIO_PROBE
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200244 };
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200245
Pekka Paalanend61fc442008-05-12 21:20:57 +0200246 if (!trace) {
Joe Perches3a0340b2009-12-09 10:45:37 -0800247 pr_err("kmalloc failed in ioremap\n");
Pekka Paalanend61fc442008-05-12 21:20:57 +0200248 return;
249 }
250
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200251 *trace = (struct remap_trace) {
252 .probe = {
253 .addr = (unsigned long)addr,
254 .len = size,
255 .pre_handler = pre,
256 .post_handler = post,
Pekka Paalanena50445d2008-05-12 21:21:03 +0200257 .private = trace
Pekka Paalanend61fc442008-05-12 21:20:57 +0200258 },
259 .phys = offset,
260 .id = atomic_inc_return(&next_id)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200261 };
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200262 map.map_id = trace->id;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200263
Pekka Paalanend61fc442008-05-12 21:20:57 +0200264 spin_lock_irq(&trace_lock);
Pekka Paalanenbbe5c782008-09-16 21:54:16 +0300265 if (!is_enabled()) {
266 kfree(trace);
Pekka Paalanend61fc442008-05-12 21:20:57 +0200267 goto not_enabled;
Pekka Paalanenbbe5c782008-09-16 21:54:16 +0300268 }
Pekka Paalanend61fc442008-05-12 21:20:57 +0200269
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200270 mmio_trace_mapping(&map);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200271 list_add_tail(&trace->list, &trace_list);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200272 if (!nommiotrace)
273 register_kmmio_probe(&trace->probe);
Pekka Paalanend61fc442008-05-12 21:20:57 +0200274
275not_enabled:
276 spin_unlock_irq(&trace_lock);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200277}
278
Pekka Paalanendee310d2008-05-12 21:21:03 +0200279void mmiotrace_ioremap(resource_size_t offset, unsigned long size,
280 void __iomem *addr)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200281{
Pekka Paalanend61fc442008-05-12 21:20:57 +0200282 if (!is_enabled()) /* recheck and proper locking in *_core() */
283 return;
284
Joe Perches3a0340b2009-12-09 10:45:37 -0800285 pr_debug("ioremap_*(0x%llx, 0x%lx) = %p\n",
286 (unsigned long long)offset, size, addr);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200287 if ((filter_offset) && (offset != filter_offset))
288 return;
Pekka Paalanend61fc442008-05-12 21:20:57 +0200289 ioremap_trace_core(offset, size, addr);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200290}
291
Pekka Paalanend61fc442008-05-12 21:20:57 +0200292static void iounmap_trace_core(volatile void __iomem *addr)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200293{
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200294 struct mmiotrace_map map = {
295 .phys = 0,
296 .virt = (unsigned long)addr,
297 .len = 0,
298 .opcode = MMIO_UNPROBE
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200299 };
300 struct remap_trace *trace;
301 struct remap_trace *tmp;
Pekka Paalanend61fc442008-05-12 21:20:57 +0200302 struct remap_trace *found_trace = NULL;
303
Joe Perches3a0340b2009-12-09 10:45:37 -0800304 pr_debug("Unmapping %p.\n", addr);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200305
Pekka Paalanend61fc442008-05-12 21:20:57 +0200306 spin_lock_irq(&trace_lock);
307 if (!is_enabled())
308 goto not_enabled;
309
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200310 list_for_each_entry_safe(trace, tmp, &trace_list, list) {
311 if ((unsigned long)addr == trace->probe.addr) {
312 if (!nommiotrace)
313 unregister_kmmio_probe(&trace->probe);
314 list_del(&trace->list);
Pekka Paalanend61fc442008-05-12 21:20:57 +0200315 found_trace = trace;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200316 break;
317 }
318 }
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200319 map.map_id = (found_trace) ? found_trace->id : -1;
320 mmio_trace_mapping(&map);
Pekka Paalanend61fc442008-05-12 21:20:57 +0200321
322not_enabled:
323 spin_unlock_irq(&trace_lock);
324 if (found_trace) {
325 synchronize_rcu(); /* unregister_kmmio_probe() requirement */
326 kfree(found_trace);
327 }
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200328}
Pekka Paalanend61fc442008-05-12 21:20:57 +0200329
330void mmiotrace_iounmap(volatile void __iomem *addr)
331{
332 might_sleep();
333 if (is_enabled()) /* recheck and proper locking in *_core() */
334 iounmap_trace_core(addr);
335}
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200336
Pekka Paalanen9e57fb32008-09-16 22:00:34 +0300337int mmiotrace_printk(const char *fmt, ...)
338{
339 int ret = 0;
340 va_list args;
341 unsigned long flags;
342 va_start(args, fmt);
343
344 spin_lock_irqsave(&trace_lock, flags);
345 if (is_enabled())
346 ret = mmio_trace_printk(fmt, args);
347 spin_unlock_irqrestore(&trace_lock, flags);
348
349 va_end(args);
350 return ret;
351}
352EXPORT_SYMBOL(mmiotrace_printk);
353
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200354static void clear_trace_list(void)
355{
356 struct remap_trace *trace;
357 struct remap_trace *tmp;
358
Pekka Paalanend61fc442008-05-12 21:20:57 +0200359 /*
360 * No locking required, because the caller ensures we are in a
361 * critical section via mutex, and is_enabled() is false,
362 * i.e. nothing can traverse or modify this list.
363 * Caller also ensures is_enabled() cannot change.
364 */
365 list_for_each_entry(trace, &trace_list, list) {
Joe Perches3a0340b2009-12-09 10:45:37 -0800366 pr_notice("purging non-iounmapped trace @0x%08lx, size 0x%lx.\n",
367 trace->probe.addr, trace->probe.len);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200368 if (!nommiotrace)
369 unregister_kmmio_probe(&trace->probe);
Pekka Paalanend61fc442008-05-12 21:20:57 +0200370 }
371 synchronize_rcu(); /* unregister_kmmio_probe() requirement */
372
373 list_for_each_entry_safe(trace, tmp, &trace_list, list) {
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200374 list_del(&trace->list);
375 kfree(trace);
Pekka Paalanend61fc442008-05-12 21:20:57 +0200376 }
377}
378
Pekka Paalanen7423d112008-05-12 21:21:02 +0200379#ifdef CONFIG_HOTPLUG_CPU
Rusty Russellc38da562009-03-11 16:33:55 +1030380static cpumask_var_t downed_cpus;
Pekka Paalanen7423d112008-05-12 21:21:02 +0200381
382static void enter_uniprocessor(void)
383{
384 int cpu;
385 int err;
386
Rusty Russellc38da562009-03-11 16:33:55 +1030387 if (downed_cpus == NULL &&
388 !alloc_cpumask_var(&downed_cpus, GFP_KERNEL)) {
Joe Perches3a0340b2009-12-09 10:45:37 -0800389 pr_notice("Failed to allocate mask\n");
Rusty Russellc38da562009-03-11 16:33:55 +1030390 goto out;
391 }
392
Pekka Paalanen7423d112008-05-12 21:21:02 +0200393 get_online_cpus();
Rusty Russellc38da562009-03-11 16:33:55 +1030394 cpumask_copy(downed_cpus, cpu_online_mask);
395 cpumask_clear_cpu(cpumask_first(cpu_online_mask), downed_cpus);
Pekka Paalanen7423d112008-05-12 21:21:02 +0200396 if (num_online_cpus() > 1)
Joe Perches3a0340b2009-12-09 10:45:37 -0800397 pr_notice("Disabling non-boot CPUs...\n");
Pekka Paalanen7423d112008-05-12 21:21:02 +0200398 put_online_cpus();
399
Rusty Russellc38da562009-03-11 16:33:55 +1030400 for_each_cpu(cpu, downed_cpus) {
Pekka Paalanen7423d112008-05-12 21:21:02 +0200401 err = cpu_down(cpu);
Pekka Paalanen970e6fa2008-05-12 21:21:03 +0200402 if (!err)
Joe Perches3a0340b2009-12-09 10:45:37 -0800403 pr_info("CPU%d is down.\n", cpu);
Pekka Paalanen970e6fa2008-05-12 21:21:03 +0200404 else
Joe Perches3a0340b2009-12-09 10:45:37 -0800405 pr_err("Error taking CPU%d down: %d\n", cpu, err);
Pekka Paalanen7423d112008-05-12 21:21:02 +0200406 }
Rusty Russellc38da562009-03-11 16:33:55 +1030407out:
Pekka Paalanen7423d112008-05-12 21:21:02 +0200408 if (num_online_cpus() > 1)
Joe Perches3a0340b2009-12-09 10:45:37 -0800409 pr_warning("multiple CPUs still online, may miss events.\n");
Pekka Paalanen7423d112008-05-12 21:21:02 +0200410}
411
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400412static void leave_uniprocessor(void)
Pekka Paalanen7423d112008-05-12 21:21:02 +0200413{
414 int cpu;
415 int err;
416
Rusty Russellc38da562009-03-11 16:33:55 +1030417 if (downed_cpus == NULL || cpumask_weight(downed_cpus) == 0)
Pekka Paalanen7423d112008-05-12 21:21:02 +0200418 return;
Joe Perches3a0340b2009-12-09 10:45:37 -0800419 pr_notice("Re-enabling CPUs...\n");
Rusty Russellc38da562009-03-11 16:33:55 +1030420 for_each_cpu(cpu, downed_cpus) {
Pekka Paalanen7423d112008-05-12 21:21:02 +0200421 err = cpu_up(cpu);
422 if (!err)
Joe Perches3a0340b2009-12-09 10:45:37 -0800423 pr_info("enabled CPU%d.\n", cpu);
Pekka Paalanen7423d112008-05-12 21:21:02 +0200424 else
Joe Perches3a0340b2009-12-09 10:45:37 -0800425 pr_err("cannot re-enable CPU%d: %d\n", cpu, err);
Pekka Paalanen7423d112008-05-12 21:21:02 +0200426 }
427}
428
429#else /* !CONFIG_HOTPLUG_CPU */
430static void enter_uniprocessor(void)
431{
432 if (num_online_cpus() > 1)
Joe Perches3a0340b2009-12-09 10:45:37 -0800433 pr_warning("multiple CPUs are online, may miss events. "
434 "Suggest booting with maxcpus=1 kernel argument.\n");
Pekka Paalanen7423d112008-05-12 21:21:02 +0200435}
436
437static void leave_uniprocessor(void)
438{
439}
440#endif
441
Pekka Paalanenf984b512008-05-12 21:20:57 +0200442void enable_mmiotrace(void)
Pekka Paalanend61fc442008-05-12 21:20:57 +0200443{
444 mutex_lock(&mmiotrace_mutex);
445 if (is_enabled())
446 goto out;
447
Pekka Paalanend61fc442008-05-12 21:20:57 +0200448 if (nommiotrace)
Joe Perches3a0340b2009-12-09 10:45:37 -0800449 pr_info("MMIO tracing disabled.\n");
Stuart Bennett0f9a6232009-04-28 20:17:51 +0100450 kmmio_init();
Pekka Paalanen7423d112008-05-12 21:21:02 +0200451 enter_uniprocessor();
Pekka Paalanend61fc442008-05-12 21:20:57 +0200452 spin_lock_irq(&trace_lock);
453 atomic_inc(&mmiotrace_enabled);
454 spin_unlock_irq(&trace_lock);
Joe Perches3a0340b2009-12-09 10:45:37 -0800455 pr_info("enabled.\n");
Pekka Paalanend61fc442008-05-12 21:20:57 +0200456out:
457 mutex_unlock(&mmiotrace_mutex);
458}
459
Pekka Paalanenf984b512008-05-12 21:20:57 +0200460void disable_mmiotrace(void)
Pekka Paalanend61fc442008-05-12 21:20:57 +0200461{
462 mutex_lock(&mmiotrace_mutex);
463 if (!is_enabled())
464 goto out;
465
466 spin_lock_irq(&trace_lock);
467 atomic_dec(&mmiotrace_enabled);
468 BUG_ON(is_enabled());
469 spin_unlock_irq(&trace_lock);
470
471 clear_trace_list(); /* guarantees: no more kmmio callbacks */
Pekka Paalanen7423d112008-05-12 21:21:02 +0200472 leave_uniprocessor();
Stuart Bennett0f9a6232009-04-28 20:17:51 +0100473 kmmio_cleanup();
Joe Perches3a0340b2009-12-09 10:45:37 -0800474 pr_info("disabled.\n");
Pekka Paalanend61fc442008-05-12 21:20:57 +0200475out:
476 mutex_unlock(&mmiotrace_mutex);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200477}