Path: blob/master/arch/mips/sibyte/common/sb_tbprof.c
10820 views
/*1* This program is free software; you can redistribute it and/or2* modify it under the terms of the GNU General Public License3* as published by the Free Software Foundation; either version 24* of the License, or (at your option) any later version.5*6* This program is distributed in the hope that it will be useful,7* but WITHOUT ANY WARRANTY; without even the implied warranty of8* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9* GNU General Public License for more details.10*11* You should have received a copy of the GNU General Public License12* along with this program; if not, write to the Free Software13* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.14*15* Copyright (C) 2001, 2002, 2003 Broadcom Corporation16* Copyright (C) 2007 Ralf Baechle <[email protected]>17* Copyright (C) 2007 MIPS Technologies, Inc.18* written by Ralf Baechle <[email protected]>19*/2021#undef DEBUG2223#include <linux/device.h>24#include <linux/module.h>25#include <linux/kernel.h>26#include <linux/types.h>27#include <linux/init.h>28#include <linux/interrupt.h>29#include <linux/vmalloc.h>30#include <linux/fs.h>31#include <linux/errno.h>32#include <linux/wait.h>33#include <asm/io.h>34#include <asm/sibyte/sb1250.h>3536#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)37#include <asm/sibyte/bcm1480_regs.h>38#include <asm/sibyte/bcm1480_scd.h>39#include <asm/sibyte/bcm1480_int.h>40#elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)41#include <asm/sibyte/sb1250_regs.h>42#include <asm/sibyte/sb1250_scd.h>43#include <asm/sibyte/sb1250_int.h>44#else45#error invalid SiByte UART configuration46#endif4748#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)49#undef K_INT_TRACE_FREEZE50#define K_INT_TRACE_FREEZE K_BCM1480_INT_TRACE_FREEZE51#undef K_INT_PERF_CNT52#define K_INT_PERF_CNT K_BCM1480_INT_PERF_CNT53#endif5455#include <asm/system.h>56#include <asm/uaccess.h>5758#define SBPROF_TB_MAJOR 2405960typedef u64 tb_sample_t[6*256];6162enum open_status {63SB_CLOSED,64SB_OPENING,65SB_OPEN66};6768struct sbprof_tb {69wait_queue_head_t tb_sync;70wait_queue_head_t tb_read;71struct mutex lock;72enum open_status open;73tb_sample_t *sbprof_tbbuf;74int next_tb_sample;7576volatile int tb_enable;77volatile int tb_armed;7879};8081static struct sbprof_tb sbp;8283#define MAX_SAMPLE_BYTES (24*1024*1024)84#define MAX_TBSAMPLE_BYTES (12*1024*1024)8586#define MAX_SAMPLES (MAX_SAMPLE_BYTES/sizeof(u_int32_t))87#define TB_SAMPLE_SIZE (sizeof(tb_sample_t))88#define MAX_TB_SAMPLES (MAX_TBSAMPLE_BYTES/TB_SAMPLE_SIZE)8990/* ioctls */91#define SBPROF_ZBSTART _IOW('s', 0, int)92#define SBPROF_ZBSTOP _IOW('s', 1, int)93#define SBPROF_ZBWAITFULL _IOW('s', 2, int)9495/*96* Routines for using 40-bit SCD cycle counter97*98* Client responsible for either handling interrupts or making sure99* the cycles counter never saturates, e.g., by doing100* zclk_timer_init(0) at least every 2^40 - 1 ZCLKs.101*/102103/*104* Configures SCD counter 0 to count ZCLKs starting from val;105* Configures SCD counters1,2,3 to count nothing.106* Must not be called while gathering ZBbus profiles.107*/108109#define zclk_timer_init(val) \110__asm__ __volatile__ (".set push;" \111".set mips64;" \112"la $8, 0xb00204c0;" /* SCD perf_cnt_cfg */ \113"sd %0, 0x10($8);" /* write val to counter0 */ \114"sd %1, 0($8);" /* config counter0 for zclks*/ \115".set pop" \116: /* no outputs */ \117/* enable, counter0 */ \118: /* inputs */ "r"(val), "r" ((1ULL << 33) | 1ULL) \119: /* modifies */ "$8" )120121122/* Reads SCD counter 0 and puts result in value123unsigned long long val; */124#define zclk_get(val) \125__asm__ __volatile__ (".set push;" \126".set mips64;" \127"la $8, 0xb00204c0;" /* SCD perf_cnt_cfg */ \128"ld %0, 0x10($8);" /* write val to counter0 */ \129".set pop" \130: /* outputs */ "=r"(val) \131: /* inputs */ \132: /* modifies */ "$8" )133134#define DEVNAME "sb_tbprof"135136#define TB_FULL (sbp.next_tb_sample == MAX_TB_SAMPLES)137138/*139* Support for ZBbus sampling using the trace buffer140*141* We use the SCD performance counter interrupt, caused by a Zclk counter142* overflow, to trigger the start of tracing.143*144* We set the trace buffer to sample everything and freeze on145* overflow.146*147* We map the interrupt for trace_buffer_freeze to handle it on CPU 0.148*149*/150151static u64 tb_period;152153static void arm_tb(void)154{155u64 scdperfcnt;156u64 next = (1ULL << 40) - tb_period;157u64 tb_options = M_SCD_TRACE_CFG_FREEZE_FULL;158159/*160* Generate an SCD_PERFCNT interrupt in TB_PERIOD Zclks to161* trigger start of trace. XXX vary sampling period162*/163__raw_writeq(0, IOADDR(A_SCD_PERF_CNT_1));164scdperfcnt = __raw_readq(IOADDR(A_SCD_PERF_CNT_CFG));165166/*167* Unfortunately, in Pass 2 we must clear all counters to knock down168* a previous interrupt request. This means that bus profiling169* requires ALL of the SCD perf counters.170*/171#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)172__raw_writeq((scdperfcnt & ~M_SPC_CFG_SRC1) |173/* keep counters 0,2,3,4,5,6,7 as is */174V_SPC_CFG_SRC1(1), /* counter 1 counts cycles */175IOADDR(A_BCM1480_SCD_PERF_CNT_CFG0));176__raw_writeq(177M_SPC_CFG_ENABLE | /* enable counting */178M_SPC_CFG_CLEAR | /* clear all counters */179V_SPC_CFG_SRC1(1), /* counter 1 counts cycles */180IOADDR(A_BCM1480_SCD_PERF_CNT_CFG1));181#else182__raw_writeq((scdperfcnt & ~M_SPC_CFG_SRC1) |183/* keep counters 0,2,3 as is */184M_SPC_CFG_ENABLE | /* enable counting */185M_SPC_CFG_CLEAR | /* clear all counters */186V_SPC_CFG_SRC1(1), /* counter 1 counts cycles */187IOADDR(A_SCD_PERF_CNT_CFG));188#endif189__raw_writeq(next, IOADDR(A_SCD_PERF_CNT_1));190/* Reset the trace buffer */191__raw_writeq(M_SCD_TRACE_CFG_RESET, IOADDR(A_SCD_TRACE_CFG));192#if 0 && defined(M_SCD_TRACE_CFG_FORCECNT)193/* XXXKW may want to expose control to the data-collector */194tb_options |= M_SCD_TRACE_CFG_FORCECNT;195#endif196__raw_writeq(tb_options, IOADDR(A_SCD_TRACE_CFG));197sbp.tb_armed = 1;198}199200static irqreturn_t sbprof_tb_intr(int irq, void *dev_id)201{202int i;203204pr_debug(DEVNAME ": tb_intr\n");205206if (sbp.next_tb_sample < MAX_TB_SAMPLES) {207/* XXX should use XKPHYS to make writes bypass L2 */208u64 *p = sbp.sbprof_tbbuf[sbp.next_tb_sample++];209/* Read out trace */210__raw_writeq(M_SCD_TRACE_CFG_START_READ,211IOADDR(A_SCD_TRACE_CFG));212__asm__ __volatile__ ("sync" : : : "memory");213/* Loop runs backwards because bundles are read out in reverse order */214for (i = 256 * 6; i > 0; i -= 6) {215/* Subscripts decrease to put bundle in the order */216/* t0 lo, t0 hi, t1 lo, t1 hi, t2 lo, t2 hi */217p[i - 1] = __raw_readq(IOADDR(A_SCD_TRACE_READ));218/* read t2 hi */219p[i - 2] = __raw_readq(IOADDR(A_SCD_TRACE_READ));220/* read t2 lo */221p[i - 3] = __raw_readq(IOADDR(A_SCD_TRACE_READ));222/* read t1 hi */223p[i - 4] = __raw_readq(IOADDR(A_SCD_TRACE_READ));224/* read t1 lo */225p[i - 5] = __raw_readq(IOADDR(A_SCD_TRACE_READ));226/* read t0 hi */227p[i - 6] = __raw_readq(IOADDR(A_SCD_TRACE_READ));228/* read t0 lo */229}230if (!sbp.tb_enable) {231pr_debug(DEVNAME ": tb_intr shutdown\n");232__raw_writeq(M_SCD_TRACE_CFG_RESET,233IOADDR(A_SCD_TRACE_CFG));234sbp.tb_armed = 0;235wake_up_interruptible(&sbp.tb_sync);236} else {237/* knock down current interrupt and get another one later */238arm_tb();239}240} else {241/* No more trace buffer samples */242pr_debug(DEVNAME ": tb_intr full\n");243__raw_writeq(M_SCD_TRACE_CFG_RESET, IOADDR(A_SCD_TRACE_CFG));244sbp.tb_armed = 0;245if (!sbp.tb_enable)246wake_up_interruptible(&sbp.tb_sync);247wake_up_interruptible(&sbp.tb_read);248}249return IRQ_HANDLED;250}251252static irqreturn_t sbprof_pc_intr(int irq, void *dev_id)253{254printk(DEVNAME ": unexpected pc_intr");255return IRQ_NONE;256}257258/*259* Requires: Already called zclk_timer_init with a value that won't260* saturate 40 bits. No subsequent use of SCD performance counters261* or trace buffer.262*/263264static int sbprof_zbprof_start(struct file *filp)265{266u64 scdperfcnt;267int err;268269if (xchg(&sbp.tb_enable, 1))270return -EBUSY;271272pr_debug(DEVNAME ": starting\n");273274sbp.next_tb_sample = 0;275filp->f_pos = 0;276277err = request_irq(K_INT_TRACE_FREEZE, sbprof_tb_intr, 0,278DEVNAME " trace freeze", &sbp);279if (err)280return -EBUSY;281282/* Make sure there isn't a perf-cnt interrupt waiting */283scdperfcnt = __raw_readq(IOADDR(A_SCD_PERF_CNT_CFG));284/* Disable and clear counters, override SRC_1 */285__raw_writeq((scdperfcnt & ~(M_SPC_CFG_SRC1 | M_SPC_CFG_ENABLE)) |286M_SPC_CFG_ENABLE | M_SPC_CFG_CLEAR | V_SPC_CFG_SRC1(1),287IOADDR(A_SCD_PERF_CNT_CFG));288289/*290* We grab this interrupt to prevent others from trying to use291* it, even though we don't want to service the interrupts292* (they only feed into the trace-on-interrupt mechanism)293*/294if (request_irq(K_INT_PERF_CNT, sbprof_pc_intr, 0, DEVNAME " scd perfcnt", &sbp)) {295free_irq(K_INT_TRACE_FREEZE, &sbp);296return -EBUSY;297}298299/*300* I need the core to mask these, but the interrupt mapper to301* pass them through. I am exploiting my knowledge that302* cp0_status masks out IP[5]. krw303*/304#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)305__raw_writeq(K_BCM1480_INT_MAP_I3,306IOADDR(A_BCM1480_IMR_REGISTER(0, R_BCM1480_IMR_INTERRUPT_MAP_BASE_L) +307((K_BCM1480_INT_PERF_CNT & 0x3f) << 3)));308#else309__raw_writeq(K_INT_MAP_I3,310IOADDR(A_IMR_REGISTER(0, R_IMR_INTERRUPT_MAP_BASE) +311(K_INT_PERF_CNT << 3)));312#endif313314/* Initialize address traps */315__raw_writeq(0, IOADDR(A_ADDR_TRAP_UP_0));316__raw_writeq(0, IOADDR(A_ADDR_TRAP_UP_1));317__raw_writeq(0, IOADDR(A_ADDR_TRAP_UP_2));318__raw_writeq(0, IOADDR(A_ADDR_TRAP_UP_3));319320__raw_writeq(0, IOADDR(A_ADDR_TRAP_DOWN_0));321__raw_writeq(0, IOADDR(A_ADDR_TRAP_DOWN_1));322__raw_writeq(0, IOADDR(A_ADDR_TRAP_DOWN_2));323__raw_writeq(0, IOADDR(A_ADDR_TRAP_DOWN_3));324325__raw_writeq(0, IOADDR(A_ADDR_TRAP_CFG_0));326__raw_writeq(0, IOADDR(A_ADDR_TRAP_CFG_1));327__raw_writeq(0, IOADDR(A_ADDR_TRAP_CFG_2));328__raw_writeq(0, IOADDR(A_ADDR_TRAP_CFG_3));329330/* Initialize Trace Event 0-7 */331/* when interrupt */332__raw_writeq(M_SCD_TREVT_INTERRUPT, IOADDR(A_SCD_TRACE_EVENT_0));333__raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_1));334__raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_2));335__raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_3));336__raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_4));337__raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_5));338__raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_6));339__raw_writeq(0, IOADDR(A_SCD_TRACE_EVENT_7));340341/* Initialize Trace Sequence 0-7 */342/* Start on event 0 (interrupt) */343__raw_writeq(V_SCD_TRSEQ_FUNC_START | 0x0fff,344IOADDR(A_SCD_TRACE_SEQUENCE_0));345/* dsamp when d used | asamp when a used */346__raw_writeq(M_SCD_TRSEQ_ASAMPLE | M_SCD_TRSEQ_DSAMPLE |347K_SCD_TRSEQ_TRIGGER_ALL,348IOADDR(A_SCD_TRACE_SEQUENCE_1));349__raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_2));350__raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_3));351__raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_4));352__raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_5));353__raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_6));354__raw_writeq(0, IOADDR(A_SCD_TRACE_SEQUENCE_7));355356/* Now indicate the PERF_CNT interrupt as a trace-relevant interrupt */357#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)358__raw_writeq(1ULL << (K_BCM1480_INT_PERF_CNT & 0x3f),359IOADDR(A_BCM1480_IMR_REGISTER(0, R_BCM1480_IMR_INTERRUPT_TRACE_L)));360#else361__raw_writeq(1ULL << K_INT_PERF_CNT,362IOADDR(A_IMR_REGISTER(0, R_IMR_INTERRUPT_TRACE)));363#endif364arm_tb();365366pr_debug(DEVNAME ": done starting\n");367368return 0;369}370371static int sbprof_zbprof_stop(void)372{373int err = 0;374375pr_debug(DEVNAME ": stopping\n");376377if (sbp.tb_enable) {378/*379* XXXKW there is a window here where the intr handler may run,380* see the disable, and do the wake_up before this sleep381* happens.382*/383pr_debug(DEVNAME ": wait for disarm\n");384err = wait_event_interruptible(sbp.tb_sync, !sbp.tb_armed);385pr_debug(DEVNAME ": disarm complete, stat %d\n", err);386387if (err)388return err;389390sbp.tb_enable = 0;391free_irq(K_INT_TRACE_FREEZE, &sbp);392free_irq(K_INT_PERF_CNT, &sbp);393}394395pr_debug(DEVNAME ": done stopping\n");396397return err;398}399400static int sbprof_tb_open(struct inode *inode, struct file *filp)401{402int minor;403404minor = iminor(inode);405if (minor != 0)406return -ENODEV;407408if (xchg(&sbp.open, SB_OPENING) != SB_CLOSED)409return -EBUSY;410411memset(&sbp, 0, sizeof(struct sbprof_tb));412sbp.sbprof_tbbuf = vzalloc(MAX_TBSAMPLE_BYTES);413if (!sbp.sbprof_tbbuf) {414sbp.open = SB_CLOSED;415wmb();416return -ENOMEM;417}418419init_waitqueue_head(&sbp.tb_sync);420init_waitqueue_head(&sbp.tb_read);421mutex_init(&sbp.lock);422423sbp.open = SB_OPEN;424wmb();425426return 0;427}428429static int sbprof_tb_release(struct inode *inode, struct file *filp)430{431int minor;432433minor = iminor(inode);434if (minor != 0 || sbp.open != SB_CLOSED)435return -ENODEV;436437mutex_lock(&sbp.lock);438439if (sbp.tb_armed || sbp.tb_enable)440sbprof_zbprof_stop();441442vfree(sbp.sbprof_tbbuf);443sbp.open = SB_CLOSED;444wmb();445446mutex_unlock(&sbp.lock);447448return 0;449}450451static ssize_t sbprof_tb_read(struct file *filp, char *buf,452size_t size, loff_t *offp)453{454int cur_sample, sample_off, cur_count, sample_left;455char *src;456int count = 0;457char *dest = buf;458long cur_off = *offp;459460if (!access_ok(VERIFY_WRITE, buf, size))461return -EFAULT;462463mutex_lock(&sbp.lock);464465count = 0;466cur_sample = cur_off / TB_SAMPLE_SIZE;467sample_off = cur_off % TB_SAMPLE_SIZE;468sample_left = TB_SAMPLE_SIZE - sample_off;469470while (size && (cur_sample < sbp.next_tb_sample)) {471int err;472473cur_count = size < sample_left ? size : sample_left;474src = (char *)(((long)sbp.sbprof_tbbuf[cur_sample])+sample_off);475err = __copy_to_user(dest, src, cur_count);476if (err) {477*offp = cur_off + cur_count - err;478mutex_unlock(&sbp.lock);479return err;480}481pr_debug(DEVNAME ": read from sample %d, %d bytes\n",482cur_sample, cur_count);483size -= cur_count;484sample_left -= cur_count;485if (!sample_left) {486cur_sample++;487sample_off = 0;488sample_left = TB_SAMPLE_SIZE;489} else {490sample_off += cur_count;491}492cur_off += cur_count;493dest += cur_count;494count += cur_count;495}496*offp = cur_off;497mutex_unlock(&sbp.lock);498499return count;500}501502static long sbprof_tb_ioctl(struct file *filp,503unsigned int command,504unsigned long arg)505{506int err = 0;507508switch (command) {509case SBPROF_ZBSTART:510mutex_lock(&sbp.lock);511err = sbprof_zbprof_start(filp);512mutex_unlock(&sbp.lock);513break;514515case SBPROF_ZBSTOP:516mutex_lock(&sbp.lock);517err = sbprof_zbprof_stop();518mutex_unlock(&sbp.lock);519break;520521case SBPROF_ZBWAITFULL: {522err = wait_event_interruptible(sbp.tb_read, TB_FULL);523if (err)524break;525526err = put_user(TB_FULL, (int *) arg);527break;528}529530default:531err = -EINVAL;532break;533}534535return err;536}537538static const struct file_operations sbprof_tb_fops = {539.owner = THIS_MODULE,540.open = sbprof_tb_open,541.release = sbprof_tb_release,542.read = sbprof_tb_read,543.unlocked_ioctl = sbprof_tb_ioctl,544.compat_ioctl = sbprof_tb_ioctl,545.mmap = NULL,546.llseek = default_llseek,547};548549static struct class *tb_class;550static struct device *tb_dev;551552static int __init sbprof_tb_init(void)553{554struct device *dev;555struct class *tbc;556int err;557558if (register_chrdev(SBPROF_TB_MAJOR, DEVNAME, &sbprof_tb_fops)) {559printk(KERN_WARNING DEVNAME ": initialization failed (dev %d)\n",560SBPROF_TB_MAJOR);561return -EIO;562}563564tbc = class_create(THIS_MODULE, "sb_tracebuffer");565if (IS_ERR(tbc)) {566err = PTR_ERR(tbc);567goto out_chrdev;568}569570tb_class = tbc;571572dev = device_create(tbc, NULL, MKDEV(SBPROF_TB_MAJOR, 0), NULL, "tb");573if (IS_ERR(dev)) {574err = PTR_ERR(dev);575goto out_class;576}577tb_dev = dev;578579sbp.open = SB_CLOSED;580wmb();581tb_period = zbbus_mhz * 10000LL;582pr_info(DEVNAME ": initialized - tb_period = %lld\n",583(long long) tb_period);584return 0;585586out_class:587class_destroy(tb_class);588out_chrdev:589unregister_chrdev(SBPROF_TB_MAJOR, DEVNAME);590591return err;592}593594static void __exit sbprof_tb_cleanup(void)595{596device_destroy(tb_class, MKDEV(SBPROF_TB_MAJOR, 0));597unregister_chrdev(SBPROF_TB_MAJOR, DEVNAME);598class_destroy(tb_class);599}600601module_init(sbprof_tb_init);602module_exit(sbprof_tb_cleanup);603604MODULE_ALIAS_CHARDEV_MAJOR(SBPROF_TB_MAJOR);605MODULE_AUTHOR("Ralf Baechle <[email protected]>");606MODULE_LICENSE("GPL");607608609