PAPI 7.1.0.0
Loading...
Searching...
No Matches
pfmlib_coreduo.c
Go to the documentation of this file.
1/*
2 * pfmlib_coreduo.c : Intel Core Duo/Solo
3 *
4 * Copyright (c) 2009 Google, Inc
5 * Contributed by Stephane Eranian <eranian@gmail.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
11 * of the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
19 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
22 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 *
25 * This file implements support for Intel Core Duo/Solor PMU as specified in the
26 * following document:
27 * "IA-32 Intel Architecture Software Developer's Manual - Volume 3B: System
28 * Programming Guide"
29 *
30 * Core Dup/Solo PMU = architectural perfmon v1 + model specific events
31 */
32#include <sys/types.h>
33#include <ctype.h>
34#include <string.h>
35#include <stdlib.h>
36#include <stdio.h>
37
38/* public headers */
40
41/* private headers */
42#include "pfmlib_priv.h"
43#include "pfmlib_coreduo_priv.h"
44
45#include "coreduo_events.h"
46
47/* let's define some handy shortcuts! */
48#define sel_event_select perfevtsel.sel_event_select
49#define sel_unit_mask perfevtsel.sel_unit_mask
50#define sel_usr perfevtsel.sel_usr
51#define sel_os perfevtsel.sel_os
52#define sel_edge perfevtsel.sel_edge
53#define sel_pc perfevtsel.sel_pc
54#define sel_int perfevtsel.sel_int
55#define sel_en perfevtsel.sel_en
56#define sel_inv perfevtsel.sel_inv
57#define sel_cnt_mask perfevtsel.sel_cnt_mask
58
59/*
60 * Description of the PMC register mappings:
61 *
62 * 0 -> PMC0 -> PERFEVTSEL0
63 * 1 -> PMC1 -> PERFEVTSEL1
64 * 16 -> PMC16 -> FIXED_CTR_CTRL
65 * 17 -> PMC17 -> PEBS_ENABLED
66 *
67 * Description of the PMD register mapping:
68 *
69 * 0 -> PMD0 -> PMC0
70 * 1 -> PMD1 -> PMC1
71 * 16 -> PMD2 -> FIXED_CTR0
72 * 17 -> PMD3 -> FIXED_CTR1
73 * 18 -> PMD4 -> FIXED_CTR2
74 */
75#define COREDUO_SEL_BASE 0x186
76#define COREDUO_CTR_BASE 0xc1
77
78#define PFMLIB_COREDUO_ALL_FLAGS \
79 (PFM_COREDUO_SEL_INV|PFM_COREDUO_SEL_EDGE)
80
82static int highest_counter;
83
84static int
86{
87 char buffer[128];
88 int family, model;
89 int ret;
90
91 ret = __pfm_getcpuinfo_attr("vendor_id", buffer, sizeof(buffer));
92 if (ret == -1)
93 return PFMLIB_ERR_NOTSUPP;
94
95 if (strcmp(buffer, "GenuineIntel"))
96 return PFMLIB_ERR_NOTSUPP;
97
98 ret = __pfm_getcpuinfo_attr("cpu family", buffer, sizeof(buffer));
99 if (ret == -1)
100 return PFMLIB_ERR_NOTSUPP;
101
102 family = atoi(buffer);
103
104 ret = __pfm_getcpuinfo_attr("model", buffer, sizeof(buffer));
105 if (ret == -1)
106 return PFMLIB_ERR_NOTSUPP;
107
108 model = atoi(buffer);
109
110 return family == 6 && model == 14 ? PFMLIB_SUCCESS : PFMLIB_ERR_NOTSUPP;
111}
112
113static int
115{
118
121
122 highest_counter = 1;
123
124 return PFMLIB_SUCCESS;
125}
126
127/*
128 * IMPORTANT: the interface guarantees that pfp_pmds[] elements are returned in the order the events
129 * were submitted.
130 */
131static int
133{
134#define HAS_OPTIONS(x) (cntrs && (cntrs[x].flags || cntrs[x].cnt_mask))
135
139 pfmlib_reg_t *pc, *pd;
140 pfmlib_regmask_t *r_pmcs;
141 uint64_t val;
142 unsigned long plm;
143 unsigned int npc, npmc0, npmc1, nf2;
144 unsigned int i, n, k, ucode;
145 unsigned int assign_pc[PMU_COREDUO_NUM_COUNTERS];
146 unsigned int next_gen, last_gen;
147
148 npc = npmc0 = npmc1 = nf2 = 0;
149
150 e = inp->pfp_events;
151 pc = outp->pfp_pmcs;
152 pd = outp->pfp_pmds;
153 n = inp->pfp_event_count;
154
155 r_pmcs = &inp->pfp_unavail_pmcs;
156 cntrs = param ? param->pfp_coreduo_counters : NULL;
157
159 return PFMLIB_ERR_TOOMANY;
160
161 /*
162 * initilize to empty
163 */
164 for(i=0; i < PMU_COREDUO_NUM_COUNTERS; i++)
165 assign_pc[i] = -1;
166
167 /*
168 * error checking
169 */
170 for(i=0; i < n; i++) {
171 /*
172 * only supports two priv levels for perf counters
173 */
174 if (e[i].plm & (PFM_PLM1|PFM_PLM2))
175 return PFMLIB_ERR_INVAL;
176
177 /*
178 * check for valid flags
179 */
180 if (cntrs && cntrs[i].flags & ~PFMLIB_COREDUO_ALL_FLAGS)
181 return PFMLIB_ERR_INVAL;
182
183 /*
184 * check event-level single register constraint (PMC0, PMC1, FIXED_CTR2)
185 * fail if more than two events requested for the same counter
186 */
187 if (coreduo_pe[e[i].event].pme_flags & PFMLIB_COREDUO_PMC0) {
188 if (++npmc0 > 1) {
189 DPRINT("two events compete for a PMC0\n");
190 return PFMLIB_ERR_NOASSIGN;
191 }
192 }
193 /*
194 * check if PMC1 is available and if only one event is dependent on it
195 */
196 if (coreduo_pe[e[i].event].pme_flags & PFMLIB_COREDUO_PMC1) {
197 if (++npmc1 > 1) {
198 DPRINT("two events compete for a PMC1\n");
199 return PFMLIB_ERR_NOASSIGN;
200 }
201 }
202 }
203
204 next_gen = 0; /* first generic counter */
205 last_gen = 1; /* last generic counter */
206
207 /*
208 * strongest constraint first: works only in IA32_PMC0, IA32_PMC1
209 */
210 for(i=0; i < n; i++) {
211 if ((coreduo_pe[e[i].event].pme_flags & PFMLIB_COREDUO_PMC0)) {
212 if (pfm_regmask_isset(r_pmcs, 0))
213 return PFMLIB_ERR_NOASSIGN;
214
215 assign_pc[i] = 0;
216
217 next_gen++;
218 }
219 if (coreduo_pe[e[i].event].pme_flags & PFMLIB_COREDUO_PMC1) {
220 if (pfm_regmask_isset(r_pmcs, 1))
221 return PFMLIB_ERR_NOASSIGN;
222
223 assign_pc[i] = 1;
224
225 next_gen = (next_gen+1) % PMU_COREDUO_NUM_COUNTERS;
226 }
227 }
228 /*
229 * assign what is left
230 */
231 for(i=0; i < n; i++) {
232 if (assign_pc[i] == -1) {
233 for(; next_gen <= last_gen; next_gen++) {
234DPRINT("i=%d next_gen=%d last=%d isset=%d\n", i, next_gen, last_gen, pfm_regmask_isset(r_pmcs, next_gen));
235 if (!pfm_regmask_isset(r_pmcs, next_gen))
236 break;
237 }
238 if (next_gen <= last_gen)
239 assign_pc[i] = next_gen++;
240 else {
241 DPRINT("cannot assign generic counters\n");
242 return PFMLIB_ERR_NOASSIGN;
243 }
244 }
245 }
246
247 for (i=0; i < n ; i++ ) {
248 reg.val = 0; /* assume reserved bits are zerooed */
249
250 /* if plm is 0, then assume not specified per-event and use default */
251 plm = e[i].plm ? e[i].plm : inp->pfp_dfl_plm;
252
253 val = coreduo_pe[e[i].event].pme_code;
254
255 reg.sel_event_select = val & 0xff;
256
257 ucode = (val >> 8) & 0xff;
258
259 for(k=0; k < e[i].num_masks; k++) {
260 ucode |= coreduo_pe[e[i].event].pme_umasks[e[i].unit_masks[k]].pme_ucode;
261 }
262
263 /*
264 * for events supporting Core specificity (self, both), a value
265 * of 0 for bits 15:14 (7:6 in our umask) is reserved, therefore we
266 * force to SELF if user did not specify anything
267 */
268 if ((coreduo_pe[e[i].event].pme_flags & PFMLIB_COREDUO_CSPEC)
269 && ((ucode & (0x3 << 6)) == 0)) {
270 ucode |= 1 << 6;
271 }
272 /*
273 * for events supporting MESI, a value
274 * of 0 for bits 11:8 (0-3 in our umask) means nothing will be
275 * counted. Therefore, we force a default of 0xf (M,E,S,I).
276 */
277 if ((coreduo_pe[e[i].event].pme_flags & PFMLIB_COREDUO_MESI)
278 && ((ucode & 0xf) == 0)) {
279 ucode |= 0xf;
280 }
281
282 val |= ucode << 8;
283
284 reg.sel_unit_mask = ucode;
285 reg.sel_usr = plm & PFM_PLM3 ? 1 : 0;
286 reg.sel_os = plm & PFM_PLM0 ? 1 : 0;
287 reg.sel_en = 1; /* force enable bit to 1 */
288 reg.sel_int = 1; /* force APIC int to 1 */
289
290 reg.sel_cnt_mask = val >>24;
291 reg.sel_inv = val >> 23;
292 reg.sel_edge = val >> 18;
293
294 if (cntrs) {
295 if (!reg.sel_cnt_mask) {
296 /*
297 * counter mask is 8-bit wide, do not silently
298 * wrap-around
299 */
300 if (cntrs[i].cnt_mask > 255)
301 return PFMLIB_ERR_INVAL;
302 reg.sel_cnt_mask = cntrs[i].cnt_mask;
303 }
304
305 if (!reg.sel_edge)
306 reg.sel_edge = cntrs[i].flags & PFM_COREDUO_SEL_EDGE ? 1 : 0;
307 if (!reg.sel_inv)
308 reg.sel_inv = cntrs[i].flags & PFM_COREDUO_SEL_INV ? 1 : 0;
309 }
310
311 pc[npc].reg_num = assign_pc[i];
312 pc[npc].reg_value = reg.val;
313 pc[npc].reg_addr = COREDUO_SEL_BASE+assign_pc[i];
314 pc[npc].reg_alt_addr= COREDUO_SEL_BASE+assign_pc[i];
315
316 __pfm_vbprintf("[PERFEVTSEL%u(pmc%u)=0x%"PRIx64" event_sel=0x%x umask=0x%x os=%d usr=%d en=%d int=%d inv=%d edge=%d cnt_mask=%d] %s\n",
317 pc[npc].reg_num,
318 pc[npc].reg_num,
319 reg.val,
321 reg.sel_unit_mask,
322 reg.sel_os,
323 reg.sel_usr,
324 reg.sel_en,
325 reg.sel_int,
326 reg.sel_inv,
327 reg.sel_edge,
328 reg.sel_cnt_mask,
330
331 __pfm_vbprintf("[PMC%u(pmd%u)]\n",
332 pc[npc].reg_num,
333 pc[npc].reg_num);
334
335 npc++;
336 }
337 /*
338 * setup pmds: must be in the same order as the events
339 */
340 for (i=0; i < n ; i++) {
341 pd[i].reg_num = assign_pc[i];
342 pd[i].reg_addr = COREDUO_CTR_BASE+assign_pc[i];
343 /* index to use with RDPMC */
344 pd[i].reg_alt_addr = assign_pc[i];
345 }
346 outp->pfp_pmd_count = i;
347 outp->pfp_pmc_count = npc;
348
349 return PFMLIB_SUCCESS;
350}
351static int
352pfm_coreduo_dispatch_events(pfmlib_input_param_t *inp, void *model_in, pfmlib_output_param_t *outp, void *model_out)
353{
355
356 if (inp->pfp_dfl_plm & (PFM_PLM1|PFM_PLM2)) {
357 DPRINT("invalid plm=%x\n", inp->pfp_dfl_plm);
358 return PFMLIB_ERR_INVAL;
359 }
360 return pfm_coreduo_dispatch_counters(inp, mod_in, outp);
361}
362
363static int
364pfm_coreduo_get_event_code(unsigned int i, unsigned int cnt, int *code)
365{
366 if (cnt != PFMLIB_CNT_FIRST
367 && (cnt > highest_counter ||
369 return PFMLIB_ERR_INVAL;
370
371 *code = coreduo_pe[i].pme_code;
372
373 return PFMLIB_SUCCESS;
374}
375
376static void
378{
379 memset(counters, 0, sizeof(*counters));
380
381 pfm_regmask_set(counters, 0);
382 pfm_regmask_set(counters, 1);
383
384 if (coreduo_pe[j].pme_flags & PFMLIB_COREDUO_PMC0)
385 pfm_regmask_clr(counters, 1);
386 if (coreduo_pe[j].pme_flags & PFMLIB_COREDUO_PMC1)
387 pfm_regmask_clr(counters, 0);
388}
389
390static void
392{
393 *impl_pmcs = coreduo_impl_pmcs;
394}
395
396static void
398{
399 *impl_pmds = coreduo_impl_pmds;
400}
401
402static void
404{
405 /* all pmds are counters */
406 *impl_counters = coreduo_impl_pmds;
407}
408
409/*
410 * Even though, CPUID 0xa returns in eax the actual counter
411 * width, the architecture specifies that writes are limited
412 * to lower 32-bits. As such, only the lower 32-bit have full
413 * degree of freedom. That is the "useable" counter width.
414 */
415static void
417{
418 /*
419 * Even though, CPUID 0xa returns in eax the actual counter
420 * width, the architecture specifies that writes are limited
421 * to lower 32-bits. As such, only the lower 31 bits have full
422 * degree of freedom. That is the "useable" counter width.
423 */
424 *width = 32;
425}
426
427static char *
429{
430 return coreduo_pe[i].pme_name;
431}
432
433static int
434pfm_coreduo_get_event_description(unsigned int ev, char **str)
435{
436 char *s;
437 s = coreduo_pe[ev].pme_desc;
438 if (s) {
439 *str = strdup(s);
440 } else {
441 *str = NULL;
442 }
443 return PFMLIB_SUCCESS;
444}
445
446static char *
447pfm_coreduo_get_event_mask_name(unsigned int ev, unsigned int midx)
448{
449 return coreduo_pe[ev].pme_umasks[midx].pme_uname;
450}
451
452static int
453pfm_coreduo_get_event_mask_desc(unsigned int ev, unsigned int midx, char **str)
454{
455 char *s;
456
457 s = coreduo_pe[ev].pme_umasks[midx].pme_udesc;
458 if (s) {
459 *str = strdup(s);
460 } else {
461 *str = NULL;
462 }
463 return PFMLIB_SUCCESS;
464}
465
466static unsigned int
468{
469 return coreduo_pe[ev].pme_numasks;
470}
471
472static int
473pfm_coreduo_get_event_mask_code(unsigned int ev, unsigned int midx, unsigned int *code)
474{
475 *code = coreduo_pe[ev].pme_umasks[midx].pme_ucode;
476 return PFMLIB_SUCCESS;
477}
478
479static int
481{
483 return PFMLIB_SUCCESS;
484}
485
486static int
488{
490 return PFMLIB_SUCCESS;
491}
492
494 .pmu_name = "Intel Core Duo/Solo",
495 .pmu_type = PFMLIB_COREDUO_PMU,
496 .pme_count = PME_COREDUO_EVENT_COUNT,
497 .pmc_count = 2,
498 .pmd_count = 2,
499 .num_cnt = 2,
500 .get_event_code = pfm_coreduo_get_event_code,
501 .get_event_name = pfm_coreduo_get_event_name,
502 .get_event_counters = pfm_coreduo_get_event_counters,
503 .dispatch_events = pfm_coreduo_dispatch_events,
504 .pmu_detect = pfm_coreduo_detect,
505 .pmu_init = pfm_coreduo_init,
506 .get_impl_pmcs = pfm_coreduo_get_impl_pmcs,
507 .get_impl_pmds = pfm_coreduo_get_impl_pmds,
508 .get_impl_counters = pfm_coreduo_get_impl_counters,
509 .get_hw_counter_width = pfm_coreduo_get_hw_counter_width,
510 .get_event_desc = pfm_coreduo_get_event_description,
511 .get_num_event_masks = pfm_coreduo_get_num_event_masks,
512 .get_event_mask_name = pfm_coreduo_get_event_mask_name,
513 .get_event_mask_code = pfm_coreduo_get_event_mask_code,
514 .get_event_mask_desc = pfm_coreduo_get_event_mask_desc,
515 .get_cycle_event = pfm_coreduo_get_cycle_event,
516 .get_inst_retired_event = pfm_coreduo_get_inst_retired
517};
int i
double s
Definition: byte_profile.c:36
#define PME_COREDUO_UNHALTED_CORE_CYCLES
#define PME_COREDUO_INSTRUCTIONS_RETIRED
static pme_coreduo_entry_t coreduo_pe[]
#define PME_COREDUO_EVENT_COUNT
#define PFM_PLM2
Definition: pfmlib.h:52
static int pfm_regmask_set(pfmlib_regmask_t *h, unsigned int b)
Definition: pfmlib.h:321
#define PFMLIB_SUCCESS
Definition: pfmlib.h:283
#define PFM_PLM3
Definition: pfmlib.h:53
#define PFMLIB_ERR_INVAL
Definition: pfmlib.h:285
static int pfm_regmask_clr(pfmlib_regmask_t *h, unsigned int b)
Definition: pfmlib.h:332
#define PFMLIB_ERR_TOOMANY
Definition: pfmlib.h:295
#define PFM_PLM0
Definition: pfmlib.h:50
static int pfm_regmask_isset(pfmlib_regmask_t *h, unsigned int b)
Definition: pfmlib.h:313
#define PFMLIB_ERR_NOASSIGN
Definition: pfmlib.h:288
#define PFM_PLM1
Definition: pfmlib.h:51
#define PFMLIB_ERR_NOTSUPP
Definition: pfmlib.h:284
#define PFMLIB_COREDUO_PMU
Definition: pfmlib.h:231
int model
Definition: pfmlib_amd64.c:86
int family
Definition: pfmlib_amd64.c:85
static int pfm_coreduo_dispatch_events(pfmlib_input_param_t *inp, void *model_in, pfmlib_output_param_t *outp, void *model_out)
static pfmlib_regmask_t coreduo_impl_pmds
static int pfm_coreduo_get_event_mask_desc(unsigned int ev, unsigned int midx, char **str)
static void pfm_coreduo_get_impl_counters(pfmlib_regmask_t *impl_counters)
static int pfm_coreduo_detect(void)
static char * pfm_coreduo_get_event_name(unsigned int i)
static int pfm_coreduo_get_cycle_event(pfmlib_event_t *e)
static int pfm_coreduo_get_event_description(unsigned int ev, char **str)
#define PFMLIB_COREDUO_ALL_FLAGS
static void pfm_coreduo_get_hw_counter_width(unsigned int *width)
static char * pfm_coreduo_get_event_mask_name(unsigned int ev, unsigned int midx)
static int highest_counter
static int pfm_coreduo_get_event_mask_code(unsigned int ev, unsigned int midx, unsigned int *code)
static void pfm_coreduo_get_impl_pmcs(pfmlib_regmask_t *impl_pmcs)
#define COREDUO_SEL_BASE
static int pfm_coreduo_get_event_code(unsigned int i, unsigned int cnt, int *code)
static int pfm_coreduo_dispatch_counters(pfmlib_input_param_t *inp, pfmlib_coreduo_input_param_t *param, pfmlib_output_param_t *outp)
static void pfm_coreduo_get_impl_pmds(pfmlib_regmask_t *impl_pmds)
static void pfm_coreduo_get_event_counters(unsigned int j, pfmlib_regmask_t *counters)
static pfmlib_regmask_t coreduo_impl_pmcs
static unsigned int pfm_coreduo_get_num_event_masks(unsigned int ev)
static int pfm_coreduo_get_inst_retired(pfmlib_event_t *e)
static int pfm_coreduo_init(void)
#define COREDUO_CTR_BASE
pfm_pmu_support_t coreduo_support
#define PFM_COREDUO_SEL_EDGE
#define PFM_COREDUO_SEL_INV
#define PMU_COREDUO_NUM_COUNTERS
#define PFMLIB_COREDUO_CSPEC
#define PFMLIB_COREDUO_PMC1
#define PFMLIB_COREDUO_PMC0
#define PFMLIB_COREDUO_MESI
int __pfm_getcpuinfo_attr(const char *attr, char *ret_buf, size_t maxlen)
void __pfm_vbprintf(const char *fmt,...)
Definition: pfmlib_priv.c:52
#define DPRINT(fmt, a...)
Definition: pfmlib_priv.h:90
#define PFMLIB_CNT_FIRST
Definition: pfmlib_priv.h:62
unsigned long cnt_mask
pfm_coreduo_counter_t pfp_coreduo_counters[PMU_COREDUO_NUM_COUNTERS]
unsigned int num_masks
Definition: pfmlib.h:90
unsigned int plm
Definition: pfmlib.h:87
unsigned int unit_masks[PFMLIB_MAX_MASKS_PER_EVENT]
Definition: pfmlib.h:89
unsigned int event
Definition: pfmlib.h:86
unsigned int pfp_dfl_plm
Definition: pfmlib.h:110
pfmlib_regmask_t pfp_unavail_pmcs
Definition: pfmlib.h:114
pfmlib_event_t pfp_events[PFMLIB_MAX_PMCS]
Definition: pfmlib.h:113
unsigned int pfp_event_count
Definition: pfmlib.h:109
pfmlib_reg_t pfp_pmds[PFMLIB_MAX_PMDS]
Definition: pfmlib.h:130
pfmlib_reg_t pfp_pmcs[PFMLIB_MAX_PMCS]
Definition: pfmlib.h:129
unsigned int pfp_pmc_count
Definition: pfmlib.h:127
unsigned int pfp_pmd_count
Definition: pfmlib.h:128
unsigned long long reg_value
Definition: pfmlib.h:98
unsigned int reg_num
Definition: pfmlib.h:100
unsigned long reg_alt_addr
Definition: pfmlib.h:102
unsigned long long reg_addr
Definition: pfmlib.h:99
char * pme_desc
unsigned int pme_numasks
unsigned int pme_code
pme_coreduo_umask_t pme_umasks[PFMLIB_COREDUO_MAX_UMASK]
char * pme_name
unsigned long sel_usr
unsigned long sel_cnt_mask
unsigned long sel_en
unsigned long long val
unsigned long sel_int
unsigned long sel_os
unsigned long sel_event_select
unsigned long sel_edge
unsigned long sel_inv
unsigned long sel_unit_mask