Home | History | Annotate | Line # | Download | only in arch
      1 /* $NetBSD: tprof_armv7.c,v 1.1 2018/07/15 23:50:53 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 #include <sys/types.h>
     31 
     32 #include <err.h>
     33 #include <stdio.h>
     34 #include <stdint.h>
     35 #include <stdlib.h>
     36 #include <string.h>
     37 
     38 #include <dev/tprof/tprof_ioctl.h>
     39 #include "../tprof.h"
     40 
     41 struct pmu_event {
     42 	uint16_t		event;
     43 	const char		*name;
     44 };
     45 
     46 struct pmu_event_table {
     47 	const char		*name;
     48 	struct pmu_event	*events;
     49 	u_int			nevents;
     50 	struct pmu_event_table	*next;
     51 };
     52 
     53 /*
     54  * Common event numbers, from ARM ARM.
     55  */
     56 static struct pmu_event pmu_armv7_common_events[] = {
     57 	{ 0x00,	"SW_INCR" },
     58 	{ 0x01,	"L1I_CACHE_REFILL" },
     59 	{ 0x02,	"L1I_TLB_REFILL" },
     60 	{ 0x03,	"L1D_CACHE_REFILL" },
     61 	{ 0x04,	"L1D_CACHE" },
     62 	{ 0x05,	"L1D_TLB_REFILL" },
     63 	{ 0x06,	"LD_RETIRED" },
     64 	{ 0x07,	"ST_RETIRED" },
     65 	{ 0x08,	"INST_RETIRED" },
     66 	{ 0x09,	"EXC_TAKEN" },
     67 	{ 0x0a,	"EXC_RETURN" },
     68 	{ 0x0b,	"CID_WRITE_RETIRED" },
     69 	{ 0x0c,	"PC_WRITE_RETIRED" },
     70 	{ 0x0d,	"BR_IMMED_RETIRED" },
     71 	{ 0x0e,	"BR_RETURN_RETIRED" },
     72 	{ 0x0f,	"UNALIGNED_LDST_RETIRED" },
     73 	{ 0x10,	"BR_MIS_PRED" },
     74 	{ 0x11,	"CPU_CYCLES" },
     75 	{ 0x12,	"BR_PRED" },
     76 	{ 0x13,	"MEM_ACCESS" },
     77 	{ 0x14,	"L1I_CACHE" },
     78 	{ 0x15,	"L1D_CACHE_WB" },
     79 	{ 0x16,	"L2D_CACHE" },
     80 	{ 0x17,	"L2D_CACHE_REFILL" },
     81 	{ 0x18,	"L2D_CACHE_WB" },
     82 	{ 0x19,	"BUS_ACCESS" },
     83 	{ 0x1a,	"MEMORY_ERROR" },
     84 	{ 0x1b,	"INST_SPEC" },
     85 	{ 0x1c,	"TTBR_WRITE_RETIRED" },
     86 	{ 0x1d,	"BUS_CYCLES" },
     87 };
     88 
     89 static struct pmu_event_table pmu_armv7 = {
     90 	.name = "ARMv7 common architectural and microarchitectural events",
     91 	.events = pmu_armv7_common_events,
     92 	.nevents = __arraycount(pmu_armv7_common_events),
     93 	.next = NULL
     94 };
     95 
     96 static struct pmu_event_table *events = NULL;
     97 
     98 int
     99 tprof_event_init(uint32_t ident)
    100 {
    101 	if (ident != TPROF_IDENT_ARMV7_GENERIC)
    102 		return -1;
    103 
    104 	events = &pmu_armv7;
    105 
    106 	return 0;
    107 }
    108 
    109 static void
    110 tprof_event_list_table(struct pmu_event_table *tbl)
    111 {
    112 	u_int n;
    113 
    114 	printf("%s:\n", tbl->name);
    115 	for (n = 0; n < tbl->nevents; n++) {
    116 		printf("\t%s\n", tbl->events[n].name);
    117 	}
    118 
    119 	if (tbl->next)
    120 		tprof_event_list_table(tbl->next);
    121 }
    122 
    123 void
    124 tprof_event_list(void)
    125 {
    126 	tprof_event_list_table(events);
    127 }
    128 
    129 static void
    130 tprof_event_lookup_table(const char *name, struct tprof_param *param,
    131     struct pmu_event_table *tbl)
    132 {
    133 	u_int n;
    134 
    135 	for (n = 0; n < tbl->nevents; n++) {
    136 		if (strcmp(tbl->events[n].name, name) == 0) {
    137 			param->p_event = tbl->events[n].event;
    138 			param->p_unit = 0;
    139 			return;
    140 		}
    141 	}
    142 
    143 	if (tbl->next)
    144 		tprof_event_lookup_table(name, param, tbl->next);
    145 	else
    146 		errx(EXIT_FAILURE, "event '%s' unknown", name);
    147 }
    148 
    149 void
    150 tprof_event_lookup(const char *name, struct tprof_param *param)
    151 {
    152 	tprof_event_lookup_table(name, param, events);
    153 }
    154