33 *
44 * OProfile support for SH7750/SH7750S Performance Counters
55 *
6- * Copyright (C) 2003, 2004 Paul Mundt
6+ * Copyright (C) 2003 - 2008 Paul Mundt
77 *
88 * This file is subject to the terms and conditions of the GNU General Public
99 * License. See the file "COPYING" in the main directory of this archive
1515#include <linux/init.h>
1616#include <linux/errno.h>
1717#include <linux/interrupt.h>
18+ #include <linux/io.h>
1819#include <linux/fs.h>
19- #include <asm/uaccess.h>
20- #include <asm/io.h>
20+ #include "op_impl.h"
2121
2222#define PM_CR_BASE 0xff000084 /* 16-bit */
2323#define PM_CTR_BASE 0xff100004 /* 32-bit */
2424
25- #define PMCR1 (PM_CR_BASE + 0x00)
26- #define PMCR2 (PM_CR_BASE + 0x04)
27- #define PMCTR1H (PM_CTR_BASE + 0x00)
28- #define PMCTR1L (PM_CTR_BASE + 0x04)
29- #define PMCTR2H (PM_CTR_BASE + 0x08)
30- #define PMCTR2L (PM_CTR_BASE + 0x0c)
25+ #define PMCR (n ) (PM_CR_BASE + ((n) * 0x04))
26+ #define PMCTRH (n ) (PM_CTR_BASE + 0x00 + ((n) * 0x08))
27+ #define PMCTRL (n ) (PM_CTR_BASE + 0x04 + ((n) * 0x08))
3128
3229#define PMCR_PMM_MASK 0x0000003f
3330
3633#define PMCR_PMST 0x00004000
3734#define PMCR_PMEN 0x00008000
3835
39- #define PMCR_ENABLE (PMCR_PMST | PMCR_PMEN)
36+ struct op_sh_model op_model_sh7750_ops ;
4037
41- /*
42- * SH7750/SH7750S have 2 perf counters
43- */
4438#define NR_CNTRS 2
4539
46- struct op_counter_config {
47- unsigned long enabled ;
48- unsigned long event ;
49- unsigned long count ;
50-
51- /* Dummy values for userspace tool compliance */
52- unsigned long kernel ;
53- unsigned long user ;
54- unsigned long unit_mask ;
55- };
56-
57- static struct op_counter_config ctr [NR_CNTRS ];
40+ static struct sh7750_ppc_register_config {
41+ unsigned int ctrl ;
42+ unsigned long cnt_hi ;
43+ unsigned long cnt_lo ;
44+ } regcache [NR_CNTRS ];
5845
5946/*
6047 * There are a number of events supported by each counter (33 in total).
@@ -116,12 +103,8 @@ static int sh7750_timer_notify(struct pt_regs *regs)
116103
117104static u64 sh7750_read_counter (int counter )
118105{
119- u32 hi , lo ;
120-
121- hi = (counter == 0 ) ? ctrl_inl (PMCTR1H ) : ctrl_inl (PMCTR2H );
122- lo = (counter == 0 ) ? ctrl_inl (PMCTR1L ) : ctrl_inl (PMCTR2L );
123-
124- return (u64 )((u64 )(hi & 0xffff ) << 32 ) | lo ;
106+ return (u64 )((u64 )(__raw_readl (PMCTRH (counter )) & 0xffff ) << 32 ) |
107+ __raw_readl (PMCTRL (counter ));
125108}
126109
127110/*
@@ -170,11 +153,7 @@ static ssize_t sh7750_write_count(struct file *file, const char __user *buf,
170153 */
171154 WARN_ON (val != 0 );
172155
173- if (counter == 0 ) {
174- ctrl_outw (ctrl_inw (PMCR1 ) | PMCR_PMCLR , PMCR1 );
175- } else {
176- ctrl_outw (ctrl_inw (PMCR2 ) | PMCR_PMCLR , PMCR2 );
177- }
156+ __raw_writew (__raw_readw (PMCR (counter )) | PMCR_PMCLR , PMCR (counter ));
178157
179158 return count ;
180159}
@@ -184,88 +163,93 @@ static const struct file_operations count_fops = {
184163 .write = sh7750_write_count ,
185164};
186165
187- static int sh7750_perf_counter_create_files (struct super_block * sb , struct dentry * root )
166+ static int sh7750_ppc_create_files (struct super_block * sb , struct dentry * dir )
188167{
189- int i ;
168+ return oprofilefs_create_file (sb , dir , "count" , & count_fops );
169+ }
190170
191- for (i = 0 ; i < NR_CNTRS ; i ++ ) {
192- struct dentry * dir ;
193- char buf [4 ];
171+ static void sh7750_ppc_reg_setup (struct op_counter_config * ctr )
172+ {
173+ unsigned int counters = op_model_sh7750_ops .num_counters ;
174+ int i ;
194175
195- snprintf (buf , sizeof (buf ), "%d" , i );
196- dir = oprofilefs_mkdir (sb , root , buf );
176+ for (i = 0 ; i < counters ; i ++ ) {
177+ regcache [i ].ctrl = 0 ;
178+ regcache [i ].cnt_hi = 0 ;
179+ regcache [i ].cnt_lo = 0 ;
197180
198- oprofilefs_create_ulong (sb , dir , "enabled" , & ctr [i ].enabled );
199- oprofilefs_create_ulong (sb , dir , "event" , & ctr [i ].event );
200- oprofilefs_create_file (sb , dir , "count" , & count_fops );
181+ if (!ctr [i ].enabled )
182+ continue ;
201183
202- /* Dummy entries */
203- oprofilefs_create_ulong (sb , dir , "kernel" , & ctr [i ].kernel );
204- oprofilefs_create_ulong (sb , dir , "user" , & ctr [i ].user );
205- oprofilefs_create_ulong (sb , dir , "unit_mask" , & ctr [i ].unit_mask );
184+ regcache [i ].ctrl |= ctr [i ].event | PMCR_PMEN | PMCR_PMST ;
185+ regcache [i ].cnt_hi = (unsigned long )((ctr -> count >> 32 ) & 0xffff );
186+ regcache [i ].cnt_lo = (unsigned long )(ctr -> count & 0xffffffff );
206187 }
207-
208- return 0 ;
209188}
210189
211- static int sh7750_perf_counter_start (void )
190+ static void sh7750_ppc_cpu_setup (void * args )
212191{
213- u16 pmcr ;
214-
215- /* Enable counter 1 */
216- if (ctr [0 ].enabled ) {
217- pmcr = ctrl_inw (PMCR1 );
218- WARN_ON (pmcr & PMCR_PMEN );
219-
220- pmcr &= ~PMCR_PMM_MASK ;
221- pmcr |= ctr [0 ].event ;
222- ctrl_outw (pmcr | PMCR_ENABLE , PMCR1 );
223- }
224-
225- /* Enable counter 2 */
226- if (ctr [1 ].enabled ) {
227- pmcr = ctrl_inw (PMCR2 );
228- WARN_ON (pmcr & PMCR_PMEN );
192+ unsigned int counters = op_model_sh7750_ops .num_counters ;
193+ int i ;
229194
230- pmcr &= ~PMCR_PMM_MASK ;
231- pmcr |= ctr [1 ].event ;
232- ctrl_outw (pmcr | PMCR_ENABLE , PMCR2 );
195+ for (i = 0 ; i < counters ; i ++ ) {
196+ __raw_writew (0 , PMCR (i ));
197+ __raw_writel (regcache [i ].cnt_hi , PMCTRH (i ));
198+ __raw_writel (regcache [i ].cnt_lo , PMCTRL (i ));
233199 }
234-
235- return register_timer_hook (sh7750_timer_notify );
236200}
237201
238- static void sh7750_perf_counter_stop (void )
202+ static void sh7750_ppc_cpu_start (void * args )
239203{
240- ctrl_outw ( ctrl_inw ( PMCR1 ) & ~ PMCR_PMEN , PMCR1 ) ;
241- ctrl_outw ( ctrl_inw ( PMCR2 ) & ~ PMCR_PMEN , PMCR2 ) ;
204+ unsigned int counters = op_model_sh7750_ops . num_counters ;
205+ int i ;
242206
243- unregister_timer_hook (sh7750_timer_notify );
207+ for (i = 0 ; i < counters ; i ++ )
208+ __raw_writew (regcache [i ].ctrl , PMCR (i ));
244209}
245210
246- static struct oprofile_operations sh7750_perf_counter_ops = {
247- .create_files = sh7750_perf_counter_create_files ,
248- .start = sh7750_perf_counter_start ,
249- .stop = sh7750_perf_counter_stop ,
250- };
251-
252- int __init oprofile_arch_init (struct oprofile_operations * ops )
211+ static void sh7750_ppc_cpu_stop (void * args )
253212{
254- if (!( current_cpu_data . flags & CPU_HAS_PERF_COUNTER ))
255- return - ENODEV ;
213+ unsigned int counters = op_model_sh7750_ops . num_counters ;
214+ int i ;
256215
257- ops = & sh7750_perf_counter_ops ;
258- ops -> cpu_type = "sh/sh7750" ;
216+ /* Disable the counters */
217+ for (i = 0 ; i < counters ; i ++ )
218+ __raw_writew (__raw_readw (PMCR (i )) & ~PMCR_PMEN , PMCR (i ));
219+ }
259220
260- printk (KERN_INFO "oprofile: using SH-4 performance monitoring.\n" );
221+ static inline void sh7750_ppc_reset (void )
222+ {
223+ unsigned int counters = op_model_sh7750_ops .num_counters ;
224+ int i ;
261225
262226 /* Clear the counters */
263- ctrl_outw (ctrl_inw (PMCR1 ) | PMCR_PMCLR , PMCR1 );
264- ctrl_outw (ctrl_inw (PMCR2 ) | PMCR_PMCLR , PMCR2 );
227+ for (i = 0 ; i < counters ; i ++ )
228+ __raw_writew (__raw_readw (PMCR (i )) | PMCR_PMCLR , PMCR (i ));
229+ }
265230
266- return 0 ;
231+ static int sh7750_ppc_init (void )
232+ {
233+ sh7750_ppc_reset ();
234+
235+ return register_timer_hook (sh7750_timer_notify );
267236}
268237
269- void oprofile_arch_exit (void )
238+ static void sh7750_ppc_exit (void )
270239{
240+ unregister_timer_hook (sh7750_timer_notify );
241+
242+ sh7750_ppc_reset ();
271243}
244+
245+ struct op_sh_model op_model_sh7750_ops = {
246+ .cpu_type = "sh/sh7750" ,
247+ .num_counters = NR_CNTRS ,
248+ .reg_setup = sh7750_ppc_reg_setup ,
249+ .cpu_setup = sh7750_ppc_cpu_setup ,
250+ .cpu_start = sh7750_ppc_cpu_start ,
251+ .cpu_stop = sh7750_ppc_cpu_stop ,
252+ .init = sh7750_ppc_init ,
253+ .exit = sh7750_ppc_exit ,
254+ .create_files = sh7750_ppc_create_files ,
255+ };
0 commit comments