18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * arch/powerpc/oprofile/op_model_7450.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Freescale 745x/744x oprofile support, based on fsl_booke support 68c2ecf20Sopenharmony_ci * Copyright (C) 2004 Anton Blanchard <anton@au.ibm.com>, IBM 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Copyright (c) 2004 Freescale Semiconductor, Inc 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci * Author: Andy Fleming 118c2ecf20Sopenharmony_ci * Maintainer: Kumar Gala <galak@kernel.crashing.org> 128c2ecf20Sopenharmony_ci */ 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include <linux/oprofile.h> 158c2ecf20Sopenharmony_ci#include <linux/smp.h> 168c2ecf20Sopenharmony_ci#include <asm/ptrace.h> 178c2ecf20Sopenharmony_ci#include <asm/processor.h> 188c2ecf20Sopenharmony_ci#include <asm/cputable.h> 198c2ecf20Sopenharmony_ci#include <asm/page.h> 208c2ecf20Sopenharmony_ci#include <asm/pmc.h> 218c2ecf20Sopenharmony_ci#include <asm/oprofile_impl.h> 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_cistatic unsigned long reset_value[OP_MAX_COUNTER]; 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_cistatic int oprofile_running; 268c2ecf20Sopenharmony_cistatic u32 mmcr0_val, mmcr1_val, mmcr2_val, num_pmcs; 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci#define MMCR0_PMC1_SHIFT 6 298c2ecf20Sopenharmony_ci#define MMCR0_PMC2_SHIFT 0 308c2ecf20Sopenharmony_ci#define MMCR1_PMC3_SHIFT 27 318c2ecf20Sopenharmony_ci#define MMCR1_PMC4_SHIFT 22 328c2ecf20Sopenharmony_ci#define MMCR1_PMC5_SHIFT 17 338c2ecf20Sopenharmony_ci#define MMCR1_PMC6_SHIFT 11 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci#define mmcr0_event1(event) \ 368c2ecf20Sopenharmony_ci ((event << MMCR0_PMC1_SHIFT) & MMCR0_PMC1SEL) 378c2ecf20Sopenharmony_ci#define mmcr0_event2(event) \ 388c2ecf20Sopenharmony_ci ((event << MMCR0_PMC2_SHIFT) & MMCR0_PMC2SEL) 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci#define mmcr1_event3(event) \ 418c2ecf20Sopenharmony_ci ((event << MMCR1_PMC3_SHIFT) & MMCR1_PMC3SEL) 428c2ecf20Sopenharmony_ci#define mmcr1_event4(event) \ 438c2ecf20Sopenharmony_ci ((event << MMCR1_PMC4_SHIFT) & MMCR1_PMC4SEL) 448c2ecf20Sopenharmony_ci#define mmcr1_event5(event) \ 458c2ecf20Sopenharmony_ci ((event << MMCR1_PMC5_SHIFT) & MMCR1_PMC5SEL) 468c2ecf20Sopenharmony_ci#define mmcr1_event6(event) \ 478c2ecf20Sopenharmony_ci ((event << MMCR1_PMC6_SHIFT) & MMCR1_PMC6SEL) 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci#define MMCR0_INIT (MMCR0_FC | MMCR0_FCS | MMCR0_FCP | MMCR0_FCM1 | MMCR0_FCM0) 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci/* Unfreezes the counters on this CPU, enables the interrupt, 528c2ecf20Sopenharmony_ci * enables the counters to trigger the interrupt, and sets the 538c2ecf20Sopenharmony_ci * counters to only count when the mark bit is not set. 548c2ecf20Sopenharmony_ci */ 558c2ecf20Sopenharmony_cistatic void pmc_start_ctrs(void) 568c2ecf20Sopenharmony_ci{ 578c2ecf20Sopenharmony_ci u32 mmcr0 = mfspr(SPRN_MMCR0); 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci mmcr0 &= ~(MMCR0_FC | MMCR0_FCM0); 608c2ecf20Sopenharmony_ci mmcr0 |= (MMCR0_FCECE | MMCR0_PMC1CE | MMCR0_PMCnCE | MMCR0_PMXE); 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci mtspr(SPRN_MMCR0, mmcr0); 638c2ecf20Sopenharmony_ci} 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci/* Disables the counters on this CPU, and freezes them */ 668c2ecf20Sopenharmony_cistatic void pmc_stop_ctrs(void) 678c2ecf20Sopenharmony_ci{ 688c2ecf20Sopenharmony_ci u32 mmcr0 = mfspr(SPRN_MMCR0); 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci mmcr0 |= MMCR0_FC; 718c2ecf20Sopenharmony_ci mmcr0 &= ~(MMCR0_FCECE | MMCR0_PMC1CE | MMCR0_PMCnCE | MMCR0_PMXE); 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci mtspr(SPRN_MMCR0, mmcr0); 748c2ecf20Sopenharmony_ci} 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci/* Configures the counters on this CPU based on the global 778c2ecf20Sopenharmony_ci * settings */ 788c2ecf20Sopenharmony_cistatic int fsl7450_cpu_setup(struct op_counter_config *ctr) 798c2ecf20Sopenharmony_ci{ 808c2ecf20Sopenharmony_ci /* freeze all counters */ 818c2ecf20Sopenharmony_ci pmc_stop_ctrs(); 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci mtspr(SPRN_MMCR0, mmcr0_val); 848c2ecf20Sopenharmony_ci mtspr(SPRN_MMCR1, mmcr1_val); 858c2ecf20Sopenharmony_ci if (num_pmcs > 4) 868c2ecf20Sopenharmony_ci mtspr(SPRN_MMCR2, mmcr2_val); 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci return 0; 898c2ecf20Sopenharmony_ci} 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci/* Configures the global settings for the countes on all CPUs. */ 928c2ecf20Sopenharmony_cistatic int fsl7450_reg_setup(struct op_counter_config *ctr, 938c2ecf20Sopenharmony_ci struct op_system_config *sys, 948c2ecf20Sopenharmony_ci int num_ctrs) 958c2ecf20Sopenharmony_ci{ 968c2ecf20Sopenharmony_ci int i; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci num_pmcs = num_ctrs; 998c2ecf20Sopenharmony_ci /* Our counters count up, and "count" refers to 1008c2ecf20Sopenharmony_ci * how much before the next interrupt, and we interrupt 1018c2ecf20Sopenharmony_ci * on overflow. So we calculate the starting value 1028c2ecf20Sopenharmony_ci * which will give us "count" until overflow. 1038c2ecf20Sopenharmony_ci * Then we set the events on the enabled counters */ 1048c2ecf20Sopenharmony_ci for (i = 0; i < num_ctrs; ++i) 1058c2ecf20Sopenharmony_ci reset_value[i] = 0x80000000UL - ctr[i].count; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci /* Set events for Counters 1 & 2 */ 1088c2ecf20Sopenharmony_ci mmcr0_val = MMCR0_INIT | mmcr0_event1(ctr[0].event) 1098c2ecf20Sopenharmony_ci | mmcr0_event2(ctr[1].event); 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci /* Setup user/kernel bits */ 1128c2ecf20Sopenharmony_ci if (sys->enable_kernel) 1138c2ecf20Sopenharmony_ci mmcr0_val &= ~(MMCR0_FCS); 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci if (sys->enable_user) 1168c2ecf20Sopenharmony_ci mmcr0_val &= ~(MMCR0_FCP); 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci /* Set events for Counters 3-6 */ 1198c2ecf20Sopenharmony_ci mmcr1_val = mmcr1_event3(ctr[2].event) 1208c2ecf20Sopenharmony_ci | mmcr1_event4(ctr[3].event); 1218c2ecf20Sopenharmony_ci if (num_ctrs > 4) 1228c2ecf20Sopenharmony_ci mmcr1_val |= mmcr1_event5(ctr[4].event) 1238c2ecf20Sopenharmony_ci | mmcr1_event6(ctr[5].event); 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci mmcr2_val = 0; 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci return 0; 1288c2ecf20Sopenharmony_ci} 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci/* Sets the counters on this CPU to the chosen values, and starts them */ 1318c2ecf20Sopenharmony_cistatic int fsl7450_start(struct op_counter_config *ctr) 1328c2ecf20Sopenharmony_ci{ 1338c2ecf20Sopenharmony_ci int i; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci mtmsr(mfmsr() | MSR_PMM); 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci for (i = 0; i < num_pmcs; ++i) { 1388c2ecf20Sopenharmony_ci if (ctr[i].enabled) 1398c2ecf20Sopenharmony_ci classic_ctr_write(i, reset_value[i]); 1408c2ecf20Sopenharmony_ci else 1418c2ecf20Sopenharmony_ci classic_ctr_write(i, 0); 1428c2ecf20Sopenharmony_ci } 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci /* Clear the freeze bit, and enable the interrupt. 1458c2ecf20Sopenharmony_ci * The counters won't actually start until the rfi clears 1468c2ecf20Sopenharmony_ci * the PMM bit */ 1478c2ecf20Sopenharmony_ci pmc_start_ctrs(); 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci oprofile_running = 1; 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci return 0; 1528c2ecf20Sopenharmony_ci} 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci/* Stop the counters on this CPU */ 1558c2ecf20Sopenharmony_cistatic void fsl7450_stop(void) 1568c2ecf20Sopenharmony_ci{ 1578c2ecf20Sopenharmony_ci /* freeze counters */ 1588c2ecf20Sopenharmony_ci pmc_stop_ctrs(); 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci oprofile_running = 0; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci mb(); 1638c2ecf20Sopenharmony_ci} 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci/* Handle the interrupt on this CPU, and log a sample for each 1678c2ecf20Sopenharmony_ci * event that triggered the interrupt */ 1688c2ecf20Sopenharmony_cistatic void fsl7450_handle_interrupt(struct pt_regs *regs, 1698c2ecf20Sopenharmony_ci struct op_counter_config *ctr) 1708c2ecf20Sopenharmony_ci{ 1718c2ecf20Sopenharmony_ci unsigned long pc; 1728c2ecf20Sopenharmony_ci int is_kernel; 1738c2ecf20Sopenharmony_ci int val; 1748c2ecf20Sopenharmony_ci int i; 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci /* set the PMM bit (see comment below) */ 1778c2ecf20Sopenharmony_ci mtmsr(mfmsr() | MSR_PMM); 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci pc = mfspr(SPRN_SIAR); 1808c2ecf20Sopenharmony_ci is_kernel = is_kernel_addr(pc); 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci for (i = 0; i < num_pmcs; ++i) { 1838c2ecf20Sopenharmony_ci val = classic_ctr_read(i); 1848c2ecf20Sopenharmony_ci if (val < 0) { 1858c2ecf20Sopenharmony_ci if (oprofile_running && ctr[i].enabled) { 1868c2ecf20Sopenharmony_ci oprofile_add_ext_sample(pc, regs, i, is_kernel); 1878c2ecf20Sopenharmony_ci classic_ctr_write(i, reset_value[i]); 1888c2ecf20Sopenharmony_ci } else { 1898c2ecf20Sopenharmony_ci classic_ctr_write(i, 0); 1908c2ecf20Sopenharmony_ci } 1918c2ecf20Sopenharmony_ci } 1928c2ecf20Sopenharmony_ci } 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci /* The freeze bit was set by the interrupt. */ 1958c2ecf20Sopenharmony_ci /* Clear the freeze bit, and reenable the interrupt. 1968c2ecf20Sopenharmony_ci * The counters won't actually start until the rfi clears 1978c2ecf20Sopenharmony_ci * the PM/M bit */ 1988c2ecf20Sopenharmony_ci pmc_start_ctrs(); 1998c2ecf20Sopenharmony_ci} 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_cistruct op_powerpc_model op_model_7450= { 2028c2ecf20Sopenharmony_ci .reg_setup = fsl7450_reg_setup, 2038c2ecf20Sopenharmony_ci .cpu_setup = fsl7450_cpu_setup, 2048c2ecf20Sopenharmony_ci .start = fsl7450_start, 2058c2ecf20Sopenharmony_ci .stop = fsl7450_stop, 2068c2ecf20Sopenharmony_ci .handle_interrupt = fsl7450_handle_interrupt, 2078c2ecf20Sopenharmony_ci}; 208