blob: 0057a7accfb17467600eac646a7c489a9deb7be3 [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
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020027#include <linux/module.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 <linux/kallsyms.h>
33#include <asm/pgtable.h>
34#include <linux/mmiotrace.h>
35#include <asm/e820.h> /* for ISA_START_ADDRESS */
Arun Sharma600634972011-07-26 16:09:06 -070036#include <linux/atomic.h>
Pekka Paalanenf5136382008-05-12 21:20:57 +020037#include <linux/percpu.h>
Pekka Paalanen7423d112008-05-12 21:21:02 +020038#include <linux/cpu.h>
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020039
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020040#include "pf_in.h"
41
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020042struct trap_reason {
43 unsigned long addr;
44 unsigned long ip;
45 enum reason_type type;
46 int active_traces;
47};
48
Pekka Paalanend61fc442008-05-12 21:20:57 +020049struct remap_trace {
50 struct list_head list;
51 struct kmmio_probe probe;
Pekka Paalanendee310d2008-05-12 21:21:03 +020052 resource_size_t phys;
Pekka Paalanend61fc442008-05-12 21:20:57 +020053 unsigned long id;
54};
55
Pekka Paalanenfe1ffaf2008-05-12 21:20:56 +020056/* Accessed per-cpu. */
Pekka Paalanenf5136382008-05-12 21:20:57 +020057static DEFINE_PER_CPU(struct trap_reason, pf_reason);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +020058static DEFINE_PER_CPU(struct mmiotrace_rw, cpu_trace);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020059
Pekka Paalanend61fc442008-05-12 21:20:57 +020060static DEFINE_MUTEX(mmiotrace_mutex);
61static DEFINE_SPINLOCK(trace_lock);
62static atomic_t mmiotrace_enabled;
63static LIST_HEAD(trace_list); /* struct remap_trace */
Pekka Paalanend61fc442008-05-12 21:20:57 +020064
65/*
66 * Locking in this file:
67 * - mmiotrace_mutex enforces enable/disable_mmiotrace() critical sections.
68 * - mmiotrace_enabled may be modified only when holding mmiotrace_mutex
69 * and trace_lock.
70 * - Routines depending on is_enabled() must take trace_lock.
71 * - trace_list users must hold trace_lock.
Pekka Paalanen9e57fb32008-09-16 22:00:34 +030072 * - is_enabled() guarantees that mmio_trace_{rw,mapping} are allowed.
Pekka Paalanend61fc442008-05-12 21:20:57 +020073 * - pre/post callbacks assume the effect of is_enabled() being true.
74 */
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020075
76/* module parameters */
Pekka Paalanend61fc442008-05-12 21:20:57 +020077static unsigned long filter_offset;
Rusty Russell476bc002012-01-13 09:32:18 +103078static bool nommiotrace;
79static bool trace_pc;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020080
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020081module_param(filter_offset, ulong, 0);
82module_param(nommiotrace, bool, 0);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020083module_param(trace_pc, bool, 0);
84
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020085MODULE_PARM_DESC(filter_offset, "Start address of traced mappings.");
86MODULE_PARM_DESC(nommiotrace, "Disable actual MMIO tracing.");
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020087MODULE_PARM_DESC(trace_pc, "Record address of faulting instructions.");
Pekka Paalanend61fc442008-05-12 21:20:57 +020088
89static bool is_enabled(void)
90{
91 return atomic_read(&mmiotrace_enabled);
92}
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020093
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020094static void print_pte(unsigned long address)
95{
Pekka Paalanen790e2a22008-05-12 21:21:14 +020096 unsigned int level;
Pekka Paalanen75bb8832008-05-12 21:20:56 +020097 pte_t *pte = lookup_address(address, &level);
98
99 if (!pte) {
Joe Perches3a0340b2009-12-09 10:45:37 -0800100 pr_err("Error in %s: no pte for page 0x%08lx\n",
101 __func__, address);
Pekka Paalanen75bb8832008-05-12 21:20:56 +0200102 return;
103 }
104
105 if (level == PG_LEVEL_2M) {
Joe Perches3a0340b2009-12-09 10:45:37 -0800106 pr_emerg("4MB pages are not currently supported: 0x%08lx\n",
107 address);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200108 BUG();
109 }
Joe Perches3a0340b2009-12-09 10:45:37 -0800110 pr_info("pte for 0x%lx: 0x%llx 0x%llx\n",
111 address,
Randy Dunlap0663bb62008-05-12 21:20:59 +0200112 (unsigned long long)pte_val(*pte),
113 (unsigned long long)pte_val(*pte) & _PAGE_PRESENT);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200114}
115
116/*
117 * For some reason the pre/post pairs have been called in an
118 * unmatched order. Report and die.
119 */
120static void die_kmmio_nesting_error(struct pt_regs *regs, unsigned long addr)
121{
Pekka Paalanenf5136382008-05-12 21:20:57 +0200122 const struct trap_reason *my_reason = &get_cpu_var(pf_reason);
Joe Perches3a0340b2009-12-09 10:45:37 -0800123 pr_emerg("unexpected fault for address: 0x%08lx, last fault for address: 0x%08lx\n",
124 addr, my_reason->addr);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200125 print_pte(addr);
Pekka Paalanend61fc442008-05-12 21:20:57 +0200126 print_symbol(KERN_EMERG "faulting IP is at %s\n", regs->ip);
127 print_symbol(KERN_EMERG "last faulting IP was at %s\n", my_reason->ip);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200128#ifdef __i386__
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200129 pr_emerg("eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n",
Joe Perches3a0340b2009-12-09 10:45:37 -0800130 regs->ax, regs->bx, regs->cx, regs->dx);
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200131 pr_emerg("esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n",
Joe Perches3a0340b2009-12-09 10:45:37 -0800132 regs->si, regs->di, regs->bp, regs->sp);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200133#else
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200134 pr_emerg("rax: %016lx rcx: %016lx rdx: %016lx\n",
Joe Perches3a0340b2009-12-09 10:45:37 -0800135 regs->ax, regs->cx, regs->dx);
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200136 pr_emerg("rsi: %016lx rdi: %016lx rbp: %016lx rsp: %016lx\n",
Joe Perches3a0340b2009-12-09 10:45:37 -0800137 regs->si, regs->di, regs->bp, regs->sp);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200138#endif
Pekka Paalanenf5136382008-05-12 21:20:57 +0200139 put_cpu_var(pf_reason);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200140 BUG();
141}
142
143static void pre(struct kmmio_probe *p, struct pt_regs *regs,
144 unsigned long addr)
145{
Pekka Paalanenf5136382008-05-12 21:20:57 +0200146 struct trap_reason *my_reason = &get_cpu_var(pf_reason);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200147 struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200148 const unsigned long instptr = instruction_pointer(regs);
149 const enum reason_type type = get_ins_type(instptr);
Pekka Paalanena50445d2008-05-12 21:21:03 +0200150 struct remap_trace *trace = p->private;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200151
152 /* it doesn't make sense to have more than one active trace per cpu */
Pekka Paalanenf5136382008-05-12 21:20:57 +0200153 if (my_reason->active_traces)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200154 die_kmmio_nesting_error(regs, addr);
155 else
Pekka Paalanenf5136382008-05-12 21:20:57 +0200156 my_reason->active_traces++;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200157
Pekka Paalanenf5136382008-05-12 21:20:57 +0200158 my_reason->type = type;
159 my_reason->addr = addr;
160 my_reason->ip = instptr;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200161
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200162 my_trace->phys = addr - trace->probe.addr + trace->phys;
163 my_trace->map_id = trace->id;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200164
165 /*
166 * Only record the program counter when requested.
167 * It may taint clean-room reverse engineering.
168 */
169 if (trace_pc)
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200170 my_trace->pc = instptr;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200171 else
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200172 my_trace->pc = 0;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200173
Pekka Paalanenf984b512008-05-12 21:20:57 +0200174 /*
175 * XXX: the timestamp recorded will be *after* the tracing has been
176 * done, not at the time we hit the instruction. SMP implications
177 * on event ordering?
178 */
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200179
180 switch (type) {
181 case REG_READ:
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200182 my_trace->opcode = MMIO_READ;
183 my_trace->width = get_ins_mem_width(instptr);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200184 break;
185 case REG_WRITE:
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200186 my_trace->opcode = MMIO_WRITE;
187 my_trace->width = get_ins_mem_width(instptr);
188 my_trace->value = get_ins_reg_val(instptr, regs);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200189 break;
190 case IMM_WRITE:
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200191 my_trace->opcode = MMIO_WRITE;
192 my_trace->width = get_ins_mem_width(instptr);
193 my_trace->value = get_ins_imm_val(instptr);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200194 break;
195 default:
196 {
197 unsigned char *ip = (unsigned char *)instptr;
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200198 my_trace->opcode = MMIO_UNKNOWN_OP;
199 my_trace->width = 0;
200 my_trace->value = (*ip) << 16 | *(ip + 1) << 8 |
Pekka Paalanenf5136382008-05-12 21:20:57 +0200201 *(ip + 2);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200202 }
203 }
Pekka Paalanenf5136382008-05-12 21:20:57 +0200204 put_cpu_var(cpu_trace);
205 put_cpu_var(pf_reason);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200206}
207
208static void post(struct kmmio_probe *p, unsigned long condition,
209 struct pt_regs *regs)
210{
Pekka Paalanenf5136382008-05-12 21:20:57 +0200211 struct trap_reason *my_reason = &get_cpu_var(pf_reason);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200212 struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200213
214 /* this should always return the active_trace count to 0 */
Pekka Paalanenf5136382008-05-12 21:20:57 +0200215 my_reason->active_traces--;
216 if (my_reason->active_traces) {
Joe Perches3a0340b2009-12-09 10:45:37 -0800217 pr_emerg("unexpected post handler");
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200218 BUG();
219 }
220
Pekka Paalanenf5136382008-05-12 21:20:57 +0200221 switch (my_reason->type) {
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200222 case REG_READ:
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200223 my_trace->value = get_ins_reg_val(my_reason->ip, regs);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200224 break;
225 default:
226 break;
227 }
Pekka Paalanenf984b512008-05-12 21:20:57 +0200228
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200229 mmio_trace_rw(my_trace);
Pekka Paalanenf5136382008-05-12 21:20:57 +0200230 put_cpu_var(cpu_trace);
231 put_cpu_var(pf_reason);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200232}
233
Pekka Paalanendee310d2008-05-12 21:21:03 +0200234static void ioremap_trace_core(resource_size_t offset, unsigned long size,
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200235 void __iomem *addr)
236{
Pekka Paalanend61fc442008-05-12 21:20:57 +0200237 static atomic_t next_id;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200238 struct remap_trace *trace = kmalloc(sizeof(*trace), GFP_KERNEL);
Pekka Paalanen87e547f2008-05-12 21:21:03 +0200239 /* These are page-unaligned. */
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200240 struct mmiotrace_map map = {
241 .phys = offset,
242 .virt = (unsigned long)addr,
243 .len = size,
244 .opcode = MMIO_PROBE
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200245 };
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200246
Pekka Paalanend61fc442008-05-12 21:20:57 +0200247 if (!trace) {
Joe Perches3a0340b2009-12-09 10:45:37 -0800248 pr_err("kmalloc failed in ioremap\n");
Pekka Paalanend61fc442008-05-12 21:20:57 +0200249 return;
250 }
251
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200252 *trace = (struct remap_trace) {
253 .probe = {
254 .addr = (unsigned long)addr,
255 .len = size,
256 .pre_handler = pre,
257 .post_handler = post,
Pekka Paalanena50445d2008-05-12 21:21:03 +0200258 .private = trace
Pekka Paalanend61fc442008-05-12 21:20:57 +0200259 },
260 .phys = offset,
261 .id = atomic_inc_return(&next_id)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200262 };
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200263 map.map_id = trace->id;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200264
Pekka Paalanend61fc442008-05-12 21:20:57 +0200265 spin_lock_irq(&trace_lock);
Pekka Paalanenbbe5c782008-09-16 21:54:16 +0300266 if (!is_enabled()) {
267 kfree(trace);
Pekka Paalanend61fc442008-05-12 21:20:57 +0200268 goto not_enabled;
Pekka Paalanenbbe5c782008-09-16 21:54:16 +0300269 }
Pekka Paalanend61fc442008-05-12 21:20:57 +0200270
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200271 mmio_trace_mapping(&map);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200272 list_add_tail(&trace->list, &trace_list);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200273 if (!nommiotrace)
274 register_kmmio_probe(&trace->probe);
Pekka Paalanend61fc442008-05-12 21:20:57 +0200275
276not_enabled:
277 spin_unlock_irq(&trace_lock);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200278}
279
Pekka Paalanendee310d2008-05-12 21:21:03 +0200280void mmiotrace_ioremap(resource_size_t offset, unsigned long size,
281 void __iomem *addr)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200282{
Pekka Paalanend61fc442008-05-12 21:20:57 +0200283 if (!is_enabled()) /* recheck and proper locking in *_core() */
284 return;
285
Joe Perches3a0340b2009-12-09 10:45:37 -0800286 pr_debug("ioremap_*(0x%llx, 0x%lx) = %p\n",
287 (unsigned long long)offset, size, addr);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200288 if ((filter_offset) && (offset != filter_offset))
289 return;
Pekka Paalanend61fc442008-05-12 21:20:57 +0200290 ioremap_trace_core(offset, size, addr);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200291}
292
Pekka Paalanend61fc442008-05-12 21:20:57 +0200293static void iounmap_trace_core(volatile void __iomem *addr)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200294{
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200295 struct mmiotrace_map map = {
296 .phys = 0,
297 .virt = (unsigned long)addr,
298 .len = 0,
299 .opcode = MMIO_UNPROBE
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200300 };
301 struct remap_trace *trace;
302 struct remap_trace *tmp;
Pekka Paalanend61fc442008-05-12 21:20:57 +0200303 struct remap_trace *found_trace = NULL;
304
Joe Perches3a0340b2009-12-09 10:45:37 -0800305 pr_debug("Unmapping %p.\n", addr);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200306
Pekka Paalanend61fc442008-05-12 21:20:57 +0200307 spin_lock_irq(&trace_lock);
308 if (!is_enabled())
309 goto not_enabled;
310
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200311 list_for_each_entry_safe(trace, tmp, &trace_list, list) {
312 if ((unsigned long)addr == trace->probe.addr) {
313 if (!nommiotrace)
314 unregister_kmmio_probe(&trace->probe);
315 list_del(&trace->list);
Pekka Paalanend61fc442008-05-12 21:20:57 +0200316 found_trace = trace;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200317 break;
318 }
319 }
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200320 map.map_id = (found_trace) ? found_trace->id : -1;
321 mmio_trace_mapping(&map);
Pekka Paalanend61fc442008-05-12 21:20:57 +0200322
323not_enabled:
324 spin_unlock_irq(&trace_lock);
325 if (found_trace) {
326 synchronize_rcu(); /* unregister_kmmio_probe() requirement */
327 kfree(found_trace);
328 }
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200329}
Pekka Paalanend61fc442008-05-12 21:20:57 +0200330
331void mmiotrace_iounmap(volatile void __iomem *addr)
332{
333 might_sleep();
334 if (is_enabled()) /* recheck and proper locking in *_core() */
335 iounmap_trace_core(addr);
336}
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200337
Pekka Paalanen9e57fb32008-09-16 22:00:34 +0300338int mmiotrace_printk(const char *fmt, ...)
339{
340 int ret = 0;
341 va_list args;
342 unsigned long flags;
343 va_start(args, fmt);
344
345 spin_lock_irqsave(&trace_lock, flags);
346 if (is_enabled())
347 ret = mmio_trace_printk(fmt, args);
348 spin_unlock_irqrestore(&trace_lock, flags);
349
350 va_end(args);
351 return ret;
352}
353EXPORT_SYMBOL(mmiotrace_printk);
354
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200355static void clear_trace_list(void)
356{
357 struct remap_trace *trace;
358 struct remap_trace *tmp;
359
Pekka Paalanend61fc442008-05-12 21:20:57 +0200360 /*
361 * No locking required, because the caller ensures we are in a
362 * critical section via mutex, and is_enabled() is false,
363 * i.e. nothing can traverse or modify this list.
364 * Caller also ensures is_enabled() cannot change.
365 */
366 list_for_each_entry(trace, &trace_list, list) {
Joe Perches3a0340b2009-12-09 10:45:37 -0800367 pr_notice("purging non-iounmapped trace @0x%08lx, size 0x%lx.\n",
368 trace->probe.addr, trace->probe.len);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200369 if (!nommiotrace)
370 unregister_kmmio_probe(&trace->probe);
Pekka Paalanend61fc442008-05-12 21:20:57 +0200371 }
372 synchronize_rcu(); /* unregister_kmmio_probe() requirement */
373
374 list_for_each_entry_safe(trace, tmp, &trace_list, list) {
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200375 list_del(&trace->list);
376 kfree(trace);
Pekka Paalanend61fc442008-05-12 21:20:57 +0200377 }
378}
379
Pekka Paalanen7423d112008-05-12 21:21:02 +0200380#ifdef CONFIG_HOTPLUG_CPU
Rusty Russellc38da562009-03-11 16:33:55 +1030381static cpumask_var_t downed_cpus;
Pekka Paalanen7423d112008-05-12 21:21:02 +0200382
383static void enter_uniprocessor(void)
384{
385 int cpu;
386 int err;
387
Rusty Russellc38da562009-03-11 16:33:55 +1030388 if (downed_cpus == NULL &&
389 !alloc_cpumask_var(&downed_cpus, GFP_KERNEL)) {
Joe Perches3a0340b2009-12-09 10:45:37 -0800390 pr_notice("Failed to allocate mask\n");
Rusty Russellc38da562009-03-11 16:33:55 +1030391 goto out;
392 }
393
Pekka Paalanen7423d112008-05-12 21:21:02 +0200394 get_online_cpus();
Rusty Russellc38da562009-03-11 16:33:55 +1030395 cpumask_copy(downed_cpus, cpu_online_mask);
396 cpumask_clear_cpu(cpumask_first(cpu_online_mask), downed_cpus);
Pekka Paalanen7423d112008-05-12 21:21:02 +0200397 if (num_online_cpus() > 1)
Joe Perches3a0340b2009-12-09 10:45:37 -0800398 pr_notice("Disabling non-boot CPUs...\n");
Pekka Paalanen7423d112008-05-12 21:21:02 +0200399 put_online_cpus();
400
Rusty Russellc38da562009-03-11 16:33:55 +1030401 for_each_cpu(cpu, downed_cpus) {
Pekka Paalanen7423d112008-05-12 21:21:02 +0200402 err = cpu_down(cpu);
Pekka Paalanen970e6fa2008-05-12 21:21:03 +0200403 if (!err)
Joe Perches3a0340b2009-12-09 10:45:37 -0800404 pr_info("CPU%d is down.\n", cpu);
Pekka Paalanen970e6fa2008-05-12 21:21:03 +0200405 else
Joe Perches3a0340b2009-12-09 10:45:37 -0800406 pr_err("Error taking CPU%d down: %d\n", cpu, err);
Pekka Paalanen7423d112008-05-12 21:21:02 +0200407 }
Rusty Russellc38da562009-03-11 16:33:55 +1030408out:
Pekka Paalanen7423d112008-05-12 21:21:02 +0200409 if (num_online_cpus() > 1)
Joe Perches3a0340b2009-12-09 10:45:37 -0800410 pr_warning("multiple CPUs still online, may miss events.\n");
Pekka Paalanen7423d112008-05-12 21:21:02 +0200411}
412
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400413static void leave_uniprocessor(void)
Pekka Paalanen7423d112008-05-12 21:21:02 +0200414{
415 int cpu;
416 int err;
417
Rusty Russellc38da562009-03-11 16:33:55 +1030418 if (downed_cpus == NULL || cpumask_weight(downed_cpus) == 0)
Pekka Paalanen7423d112008-05-12 21:21:02 +0200419 return;
Joe Perches3a0340b2009-12-09 10:45:37 -0800420 pr_notice("Re-enabling CPUs...\n");
Rusty Russellc38da562009-03-11 16:33:55 +1030421 for_each_cpu(cpu, downed_cpus) {
Pekka Paalanen7423d112008-05-12 21:21:02 +0200422 err = cpu_up(cpu);
423 if (!err)
Joe Perches3a0340b2009-12-09 10:45:37 -0800424 pr_info("enabled CPU%d.\n", cpu);
Pekka Paalanen7423d112008-05-12 21:21:02 +0200425 else
Joe Perches3a0340b2009-12-09 10:45:37 -0800426 pr_err("cannot re-enable CPU%d: %d\n", cpu, err);
Pekka Paalanen7423d112008-05-12 21:21:02 +0200427 }
428}
429
430#else /* !CONFIG_HOTPLUG_CPU */
431static void enter_uniprocessor(void)
432{
433 if (num_online_cpus() > 1)
Joe Perches3a0340b2009-12-09 10:45:37 -0800434 pr_warning("multiple CPUs are online, may miss events. "
435 "Suggest booting with maxcpus=1 kernel argument.\n");
Pekka Paalanen7423d112008-05-12 21:21:02 +0200436}
437
438static void leave_uniprocessor(void)
439{
440}
441#endif
442
Pekka Paalanenf984b512008-05-12 21:20:57 +0200443void enable_mmiotrace(void)
Pekka Paalanend61fc442008-05-12 21:20:57 +0200444{
445 mutex_lock(&mmiotrace_mutex);
446 if (is_enabled())
447 goto out;
448
Pekka Paalanend61fc442008-05-12 21:20:57 +0200449 if (nommiotrace)
Joe Perches3a0340b2009-12-09 10:45:37 -0800450 pr_info("MMIO tracing disabled.\n");
Stuart Bennett0f9a6232009-04-28 20:17:51 +0100451 kmmio_init();
Pekka Paalanen7423d112008-05-12 21:21:02 +0200452 enter_uniprocessor();
Pekka Paalanend61fc442008-05-12 21:20:57 +0200453 spin_lock_irq(&trace_lock);
454 atomic_inc(&mmiotrace_enabled);
455 spin_unlock_irq(&trace_lock);
Joe Perches3a0340b2009-12-09 10:45:37 -0800456 pr_info("enabled.\n");
Pekka Paalanend61fc442008-05-12 21:20:57 +0200457out:
458 mutex_unlock(&mmiotrace_mutex);
459}
460
Pekka Paalanenf984b512008-05-12 21:20:57 +0200461void disable_mmiotrace(void)
Pekka Paalanend61fc442008-05-12 21:20:57 +0200462{
463 mutex_lock(&mmiotrace_mutex);
464 if (!is_enabled())
465 goto out;
466
467 spin_lock_irq(&trace_lock);
468 atomic_dec(&mmiotrace_enabled);
469 BUG_ON(is_enabled());
470 spin_unlock_irq(&trace_lock);
471
472 clear_trace_list(); /* guarantees: no more kmmio callbacks */
Pekka Paalanen7423d112008-05-12 21:21:02 +0200473 leave_uniprocessor();
Stuart Bennett0f9a6232009-04-28 20:17:51 +0100474 kmmio_cleanup();
Joe Perches3a0340b2009-12-09 10:45:37 -0800475 pr_info("disabled.\n");
Pekka Paalanend61fc442008-05-12 21:20:57 +0200476out:
477 mutex_unlock(&mmiotrace_mutex);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200478}