| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 1 | /* | 
 | 2 |  * PPC 64 oprofile support: | 
 | 3 |  * Copyright (C) 2004 Anton Blanchard <anton@au.ibm.com>, IBM | 
 | 4 |  * PPC 32 oprofile support: (based on PPC 64 support) | 
 | 5 |  * Copyright (C) Freescale Semiconductor, Inc 2004 | 
 | 6 |  *	Author: Andy Fleming | 
 | 7 |  * | 
 | 8 |  * Based on alpha version. | 
 | 9 |  * | 
 | 10 |  * This program is free software; you can redistribute it and/or | 
 | 11 |  * modify it under the terms of the GNU General Public License | 
 | 12 |  * as published by the Free Software Foundation; either version | 
 | 13 |  * 2 of the License, or (at your option) any later version. | 
 | 14 |  */ | 
 | 15 |  | 
 | 16 | #include <linux/oprofile.h> | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 17 | #include <linux/init.h> | 
 | 18 | #include <linux/smp.h> | 
 | 19 | #include <linux/errno.h> | 
 | 20 | #include <asm/ptrace.h> | 
 | 21 | #include <asm/system.h> | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 22 | #include <asm/pmc.h> | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 23 | #include <asm/cputable.h> | 
 | 24 | #include <asm/oprofile_impl.h> | 
| Kelly Daly | 2191fe3 | 2006-06-21 13:52:55 +1000 | [diff] [blame] | 25 | #include <asm/firmware.h> | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 26 |  | 
 | 27 | static struct op_powerpc_model *model; | 
 | 28 |  | 
 | 29 | static struct op_counter_config ctr[OP_MAX_COUNTER]; | 
 | 30 | static struct op_system_config sys; | 
 | 31 |  | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 32 | static void op_handle_interrupt(struct pt_regs *regs) | 
 | 33 | { | 
 | 34 | 	model->handle_interrupt(regs, ctr); | 
 | 35 | } | 
 | 36 |  | 
| Andy Fleming | dd6c89f | 2006-10-27 15:06:32 -0500 | [diff] [blame] | 37 | static void op_powerpc_cpu_setup(void *dummy) | 
 | 38 | { | 
 | 39 | 	model->cpu_setup(ctr); | 
 | 40 | } | 
 | 41 |  | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 42 | static int op_powerpc_setup(void) | 
 | 43 | { | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 44 | 	int err; | 
 | 45 |  | 
 | 46 | 	/* Grab the hardware */ | 
 | 47 | 	err = reserve_pmc_hardware(op_handle_interrupt); | 
 | 48 | 	if (err) | 
 | 49 | 		return err; | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 50 |  | 
 | 51 | 	/* Pre-compute the values to stuff in the hardware registers.  */ | 
 | 52 | 	model->reg_setup(ctr, &sys, model->num_counters); | 
 | 53 |  | 
 | 54 | 	/* Configure the registers on all cpus.  */ | 
| Andy Fleming | dd6c89f | 2006-10-27 15:06:32 -0500 | [diff] [blame] | 55 | 	on_each_cpu(op_powerpc_cpu_setup, NULL, 0, 1); | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 56 |  | 
 | 57 | 	return 0; | 
 | 58 | } | 
 | 59 |  | 
 | 60 | static void op_powerpc_shutdown(void) | 
 | 61 | { | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 62 | 	release_pmc_hardware(); | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 63 | } | 
 | 64 |  | 
 | 65 | static void op_powerpc_cpu_start(void *dummy) | 
 | 66 | { | 
 | 67 | 	model->start(ctr); | 
 | 68 | } | 
 | 69 |  | 
 | 70 | static int op_powerpc_start(void) | 
 | 71 | { | 
 | 72 | 	on_each_cpu(op_powerpc_cpu_start, NULL, 0, 1); | 
 | 73 | 	return 0; | 
 | 74 | } | 
 | 75 |  | 
 | 76 | static inline void op_powerpc_cpu_stop(void *dummy) | 
 | 77 | { | 
 | 78 | 	model->stop(); | 
 | 79 | } | 
 | 80 |  | 
 | 81 | static void op_powerpc_stop(void) | 
 | 82 | { | 
 | 83 | 	on_each_cpu(op_powerpc_cpu_stop, NULL, 0, 1); | 
 | 84 | } | 
 | 85 |  | 
 | 86 | static int op_powerpc_create_files(struct super_block *sb, struct dentry *root) | 
 | 87 | { | 
 | 88 | 	int i; | 
 | 89 |  | 
| Andy Fleming | 555d97a | 2005-12-15 20:02:04 -0600 | [diff] [blame] | 90 | #ifdef CONFIG_PPC64 | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 91 | 	/* | 
 | 92 | 	 * There is one mmcr0, mmcr1 and mmcra for setting the events for | 
 | 93 | 	 * all of the counters. | 
 | 94 | 	 */ | 
 | 95 | 	oprofilefs_create_ulong(sb, root, "mmcr0", &sys.mmcr0); | 
 | 96 | 	oprofilefs_create_ulong(sb, root, "mmcr1", &sys.mmcr1); | 
 | 97 | 	oprofilefs_create_ulong(sb, root, "mmcra", &sys.mmcra); | 
| Andy Fleming | 555d97a | 2005-12-15 20:02:04 -0600 | [diff] [blame] | 98 | #endif | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 99 |  | 
 | 100 | 	for (i = 0; i < model->num_counters; ++i) { | 
 | 101 | 		struct dentry *dir; | 
| Markus Armbruster | 0c6856f | 2006-06-26 00:24:34 -0700 | [diff] [blame] | 102 | 		char buf[4]; | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 103 |  | 
 | 104 | 		snprintf(buf, sizeof buf, "%d", i); | 
 | 105 | 		dir = oprofilefs_mkdir(sb, root, buf); | 
 | 106 |  | 
 | 107 | 		oprofilefs_create_ulong(sb, dir, "enabled", &ctr[i].enabled); | 
 | 108 | 		oprofilefs_create_ulong(sb, dir, "event", &ctr[i].event); | 
 | 109 | 		oprofilefs_create_ulong(sb, dir, "count", &ctr[i].count); | 
| Andy Fleming | 555d97a | 2005-12-15 20:02:04 -0600 | [diff] [blame] | 110 |  | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 111 | 		/* | 
| Andy Fleming | 555d97a | 2005-12-15 20:02:04 -0600 | [diff] [blame] | 112 | 		 * Classic PowerPC doesn't support per-counter | 
 | 113 | 		 * control like this, but the options are | 
 | 114 | 		 * expected, so they remain.  For Freescale | 
 | 115 | 		 * Book-E style performance monitors, we do | 
 | 116 | 		 * support them. | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 117 | 		 */ | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 118 | 		oprofilefs_create_ulong(sb, dir, "kernel", &ctr[i].kernel); | 
 | 119 | 		oprofilefs_create_ulong(sb, dir, "user", &ctr[i].user); | 
 | 120 |  | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 121 | 		oprofilefs_create_ulong(sb, dir, "unit_mask", &ctr[i].unit_mask); | 
 | 122 | 	} | 
 | 123 |  | 
 | 124 | 	oprofilefs_create_ulong(sb, root, "enable_kernel", &sys.enable_kernel); | 
 | 125 | 	oprofilefs_create_ulong(sb, root, "enable_user", &sys.enable_user); | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 126 |  | 
 | 127 | 	/* Default to tracing both kernel and user */ | 
 | 128 | 	sys.enable_kernel = 1; | 
 | 129 | 	sys.enable_user = 1; | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 130 |  | 
 | 131 | 	return 0; | 
 | 132 | } | 
 | 133 |  | 
 | 134 | int __init oprofile_arch_init(struct oprofile_operations *ops) | 
 | 135 | { | 
| Anton Blanchard | 32a3399 | 2006-01-09 15:41:31 +1100 | [diff] [blame] | 136 | 	if (!cur_cpu_spec->oprofile_cpu_type) | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 137 | 		return -ENODEV; | 
| Anton Blanchard | 32a3399 | 2006-01-09 15:41:31 +1100 | [diff] [blame] | 138 |  | 
| Kelly Daly | 2191fe3 | 2006-06-21 13:52:55 +1000 | [diff] [blame] | 139 | 	if (firmware_has_feature(FW_FEATURE_ISERIES)) | 
 | 140 | 		return -ENODEV; | 
 | 141 |  | 
| Anton Blanchard | 32a3399 | 2006-01-09 15:41:31 +1100 | [diff] [blame] | 142 | 	switch (cur_cpu_spec->oprofile_type) { | 
 | 143 | #ifdef CONFIG_PPC64 | 
| Andy Whitcroft | 7a45fb1 | 2006-01-13 12:35:49 +0000 | [diff] [blame] | 144 | 		case PPC_OPROFILE_RS64: | 
| Anton Blanchard | 32a3399 | 2006-01-09 15:41:31 +1100 | [diff] [blame] | 145 | 			model = &op_model_rs64; | 
 | 146 | 			break; | 
| Andy Whitcroft | 7a45fb1 | 2006-01-13 12:35:49 +0000 | [diff] [blame] | 147 | 		case PPC_OPROFILE_POWER4: | 
| Anton Blanchard | 32a3399 | 2006-01-09 15:41:31 +1100 | [diff] [blame] | 148 | 			model = &op_model_power4; | 
 | 149 | 			break; | 
| Andy Fleming | dd6c89f | 2006-10-27 15:06:32 -0500 | [diff] [blame] | 150 | #endif | 
 | 151 | #ifdef CONFIG_6xx | 
| Andy Whitcroft | 7a45fb1 | 2006-01-13 12:35:49 +0000 | [diff] [blame] | 152 | 		case PPC_OPROFILE_G4: | 
| Anton Blanchard | 32a3399 | 2006-01-09 15:41:31 +1100 | [diff] [blame] | 153 | 			model = &op_model_7450; | 
 | 154 | 			break; | 
 | 155 | #endif | 
 | 156 | #ifdef CONFIG_FSL_BOOKE | 
| Andy Whitcroft | 7a45fb1 | 2006-01-13 12:35:49 +0000 | [diff] [blame] | 157 | 		case PPC_OPROFILE_BOOKE: | 
| Anton Blanchard | 32a3399 | 2006-01-09 15:41:31 +1100 | [diff] [blame] | 158 | 			model = &op_model_fsl_booke; | 
 | 159 | 			break; | 
 | 160 | #endif | 
 | 161 | 		default: | 
 | 162 | 			return -ENODEV; | 
 | 163 | 	} | 
 | 164 |  | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 165 | 	model->num_counters = cur_cpu_spec->num_pmcs; | 
 | 166 |  | 
 | 167 | 	ops->cpu_type = cur_cpu_spec->oprofile_cpu_type; | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 168 | 	ops->create_files = op_powerpc_create_files; | 
 | 169 | 	ops->setup = op_powerpc_setup; | 
 | 170 | 	ops->shutdown = op_powerpc_shutdown; | 
 | 171 | 	ops->start = op_powerpc_start; | 
 | 172 | 	ops->stop = op_powerpc_stop; | 
| Brian Rogan | 6c6bd75 | 2006-03-27 11:57:01 +1100 | [diff] [blame] | 173 | 	ops->backtrace = op_powerpc_backtrace; | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 174 |  | 
| Olof Johansson | 5e1415c | 2006-04-12 15:29:00 -0500 | [diff] [blame] | 175 | 	printk(KERN_DEBUG "oprofile: using %s performance monitoring.\n", | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 176 | 	       ops->cpu_type); | 
 | 177 |  | 
 | 178 | 	return 0; | 
 | 179 | } | 
 | 180 |  | 
 | 181 | void oprofile_arch_exit(void) | 
 | 182 | { | 
| Stephen Rothwell | 86a5cdd | 2005-09-19 23:24:08 +1000 | [diff] [blame] | 183 | } |