| @@ -220,6 +220,143 @@ static int cs_etm__map_trace_id(u8 trace_chan_id, u64 *cpu_metadata) · 219 unmodified lines |
| 220 | 220 | return 0; |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | +static int cs_etm__metadata_get_trace_id(u8 *trace_chan_id, u64 *cpu_metadata) |
| 224 | +{ |
| 225 | + u64 cs_etm_magic = cpu_metadata[CS_ETM_MAGIC]; |
| 226 | + |
| 227 | + switch (cs_etm_magic) { |
| 228 | + case __perf_cs_etmv3_magic: |
| 229 | + *trace_chan_id = (u8)(cpu_metadata[CS_ETM_ETMTRACEIDR] & |
| 230 | + CORESIGHT_TRACE_ID_VAL_MASK); |
| 231 | + break; |
| 232 | + case __perf_cs_etmv4_magic: |
| 233 | + case __perf_cs_ete_magic: |
| 234 | + *trace_chan_id = (u8)(cpu_metadata[CS_ETMV4_TRCTRACEIDR] & |
| 235 | + CORESIGHT_TRACE_ID_VAL_MASK); |
| 236 | + break; |
| 237 | + default: |
| 238 | + return -EINVAL; |
| 239 | + } |
| 240 | + return 0; |
| 241 | +} |
| 242 | + |
| 243 | +/* |
| 244 | + * update metadata trace ID from the value found in the AUX_HW_INFO packet. |
| 245 | + * This will also clear the CORESIGHT_TRACE_ID_UNUSED_FLAG flag if present. |
| 246 | + */ |
| 247 | +static int cs_etm__metadata_set_trace_id(u8 trace_chan_id, u64 *cpu_metadata) |
| 248 | +{ |
| 249 | + u64 cs_etm_magic = cpu_metadata[CS_ETM_MAGIC]; |
| 250 | + |
| 251 | + switch (cs_etm_magic) { |
| 252 | + case __perf_cs_etmv3_magic: |
| 253 | + cpu_metadata[CS_ETM_ETMTRACEIDR] = trace_chan_id; |
| 254 | + break; |
| 255 | + case __perf_cs_etmv4_magic: |
| 256 | + case __perf_cs_ete_magic: |
| 257 | + cpu_metadata[CS_ETMV4_TRCTRACEIDR] = trace_chan_id; |
| 258 | + break; |
| 259 | + |
| 260 | + default: |
| 261 | + return -EINVAL; |
| 262 | + } |
| 263 | + return 0; |
| 264 | +} |
| 265 | + |
| 266 | +/* |
| 267 | + * FIELD_GET (linux/bitfield.h) not available outside kernel code, |
| 268 | + * and the header contains too many dependencies to just copy over, |
| 269 | + * so roll our own based on the original |
| 270 | + */ |
| 271 | +#define __bf_shf(x) (__builtin_ffsll(x) - 1) |
| 272 | +#define FIELD_GET(_mask, _reg) \ |
| 273 | + ({ \ |
| 274 | + (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \ |
| 275 | + }) |
| 276 | + |
| 277 | +/* |
| 278 | + * Handle the PERF_RECORD_AUX_OUTPUT_HW_ID event. |
| 279 | + * |
| 280 | + * The payload associates the Trace ID and the CPU. |
| 281 | + * The routine is tolerant of seeing multiple packets with the same association, |
| 282 | + * but a CPU / Trace ID association changing during a session is an error. |
| 283 | + */ |
| 284 | +static int cs_etm__process_aux_output_hw_id(struct perf_session *session, |
| 285 | + union perf_event *event) |
| 286 | +{ |
| 287 | + struct cs_etm_auxtrace *etm; |
| 288 | + struct perf_sample sample; |
| 289 | + struct int_node *inode; |
| 290 | + struct evsel *evsel; |
| 291 | + u64 *cpu_data; |
| 292 | + u64 hw_id; |
| 293 | + int cpu, version, err; |
| 294 | + u8 trace_chan_id, curr_chan_id; |
| 295 | + |
| 296 | + /* extract and parse the HW ID */ |
| 297 | + hw_id = event->aux_output_hw_id.hw_id; |
| 298 | + version = FIELD_GET(CS_AUX_HW_ID_VERSION_MASK, hw_id); |
| 299 | + trace_chan_id = FIELD_GET(CS_AUX_HW_ID_TRACE_ID_MASK, hw_id); |
| 300 | + |
| 301 | + /* check that we can handle this version */ |
| 302 | + if (version > CS_AUX_HW_ID_CURR_VERSION) |
| 303 | + return -EINVAL; |
| 304 | + |
| 305 | + /* get access to the etm metadata */ |
| 306 | + etm = container_of(session->auxtrace, struct cs_etm_auxtrace, auxtrace); |
| 307 | + if (!etm || !etm->metadata) |
| 308 | + return -EINVAL; |
| 309 | + |
| 310 | + /* parse the sample to get the CPU */ |
| 311 | + evsel = evlist__event2evsel(session->evlist, event); |
| 312 | + if (!evsel) |
| 313 | + return -EINVAL; |
| 314 | + err = evsel__parse_sample(evsel, event, &sample); |
| 315 | + if (err) |
| 316 | + return err; |
| 317 | + cpu = sample.cpu; |
| 318 | + if (cpu == -1) { |
| 319 | + /* no CPU in the sample - possibly recorded with an old version of perf */ |
| 320 | + pr_err("CS_ETM: no CPU AUX_OUTPUT_HW_ID sample. Use compatible perf to record."); |
| 321 | + return -EINVAL; |
| 322 | + } |
| 323 | + |
| 324 | + /* See if the ID is mapped to a CPU, and it matches the current CPU */ |
| 325 | + inode = intlist__find(traceid_list, trace_chan_id); |
| 326 | + if (inode) { |
| 327 | + cpu_data = inode->priv; |
| 328 | + if ((int)cpu_data[CS_ETM_CPU] != cpu) { |
| 329 | + pr_err("CS_ETM: map mismatch between HW_ID packet CPU and Trace ID\n"); |
| 330 | + return -EINVAL; |
| 331 | + } |
| 332 | + |
| 333 | + /* check that the mapped ID matches */ |
| 334 | + err = cs_etm__metadata_get_trace_id(&curr_chan_id, cpu_data); |
| 335 | + if (err) |
| 336 | + return err; |
| 337 | + if (curr_chan_id != trace_chan_id) { |
| 338 | + pr_err("CS_ETM: mismatch between CPU trace ID and HW_ID packet ID\n"); |
| 339 | + return -EINVAL; |
| 340 | + } |
| 341 | + |
| 342 | + /* mapped and matched - return OK */ |
| 343 | + return 0; |
| 344 | + } |
| 345 | + |
| 346 | + /* not one we've seen before - lets map it */ |
| 347 | + cpu_data = etm->metadata[cpu]; |
| 348 | + err = cs_etm__map_trace_id(trace_chan_id, cpu_data); |
| 349 | + if (err) |
| 350 | + return err; |
| 351 | + |
| 352 | + /* |
| 353 | + * if we are picking up the association from the packet, need to plug |
| 354 | + * the correct trace ID into the metadata for setting up decoders later. |
| 355 | + */ |
| 356 | + err = cs_etm__metadata_set_trace_id(trace_chan_id, cpu_data); |
| 357 | + return err; |
| 358 | +} |
| 359 | + |
| 223 | 360 | void cs_etm__etmq_set_traceid_queue_timestamp(struct cs_etm_queue *etmq, |
| 224 | 361 | u8 trace_chan_id) |
| 225 | 362 | { |
| @@ -2668,11 +2805,16 @@ static int cs_etm__queue_aux_fragment(struct perf_session *session, off_t file_o · 2442 unmodified lines |
| 2668 | 2805 | } |
| 2669 | 2806 | |
| 2670 | 2807 | /* |
| 2671 | | − * In per-thread mode, CPU is set to -1, but TID will be set instead. See |
| 2672 | | − * auxtrace_mmap_params__set_idx(). Return 'not found' if neither CPU nor TID match. |
| 2808 | + * In per-thread mode, auxtrace CPU is set to -1, but TID will be set instead. See |
| 2809 | + * auxtrace_mmap_params__set_idx(). However, the sample AUX event will contain a |
| 2810 | + * CPU as we set this always for the AUX_OUTPUT_HW_ID event. |
| 2811 | + * So now compare only TIDs if auxtrace CPU is -1, and CPUs if auxtrace CPU is not -1. |
| 2812 | + * Return 'not found' if mismatch. |
| 2673 | 2813 | */ |
| 2674 | | − if ((auxtrace_event->cpu == (__u32) -1 && auxtrace_event->tid != sample->tid) || |
| 2675 | | − auxtrace_event->cpu != sample->cpu) |
| 2814 | + if (auxtrace_event->cpu == (__u32) -1) { |
| 2815 | + if (auxtrace_event->tid != sample->tid) |
| 2816 | + return 1; |
| 2817 | + } else if (auxtrace_event->cpu != sample->cpu) |
| 2676 | 2818 | return 1; |
| 2677 | 2819 | |
| 2678 | 2820 | if (aux_event->flags & PERF_AUX_FLAG_OVERWRITE) { |
| @@ -2721,6 +2863,17 @@ static int cs_etm__queue_aux_fragment(struct perf_session *session, off_t file_o · 42 unmodified lines |
| 2721 | 2863 | return 1; |
| 2722 | 2864 | } |
| 2723 | 2865 | |
| 2866 | +static int cs_etm__process_aux_hw_id_cb(struct perf_session *session, union perf_event *event, |
| 2867 | + u64 offset __maybe_unused, void *data __maybe_unused) |
| 2868 | +{ |
| 2869 | + /* look to handle PERF_RECORD_AUX_OUTPUT_HW_ID early to ensure decoders can be set up */ |
| 2870 | + if (event->header.type == PERF_RECORD_AUX_OUTPUT_HW_ID) { |
| 2871 | + (*(int *)data)++; /* increment found count */ |
| 2872 | + return cs_etm__process_aux_output_hw_id(session, event); |
| 2873 | + } |
| 2874 | + return 0; |
| 2875 | +} |
| 2876 | + |
| 2724 | 2877 | static int cs_etm__queue_aux_records_cb(struct perf_session *session, union perf_event *event, |
| 2725 | 2878 | u64 offset __maybe_unused, void *data __maybe_unused) |
| 2726 | 2879 | { |
| @@ -2839,13 +2992,13 @@ static int cs_etm__map_trace_ids_metadata(int num_cpu, u64 **metadata) · 112 unmodified lines |
| 2839 | 2992 | cs_etm_magic = metadata[i][CS_ETM_MAGIC]; |
| 2840 | 2993 | switch (cs_etm_magic) { |
| 2841 | 2994 | case __perf_cs_etmv3_magic: |
| 2842 | | − trace_chan_id = (u8)((metadata[i][CS_ETM_ETMTRACEIDR]) & |
| 2843 | | − CORESIGHT_TRACE_ID_VAL_MASK); |
| 2995 | + metadata[i][CS_ETM_ETMTRACEIDR] &= CORESIGHT_TRACE_ID_VAL_MASK; |
| 2996 | + trace_chan_id = (u8)(metadata[i][CS_ETM_ETMTRACEIDR]); |
| 2844 | 2997 | break; |
| 2845 | 2998 | case __perf_cs_etmv4_magic: |
| 2846 | 2999 | case __perf_cs_ete_magic: |
| 2847 | | − trace_chan_id = (u8)((metadata[i][CS_ETMV4_TRCTRACEIDR]) & |
| 2848 | | − CORESIGHT_TRACE_ID_VAL_MASK); |
| 3000 | + metadata[i][CS_ETMV4_TRCTRACEIDR] &= CORESIGHT_TRACE_ID_VAL_MASK; |
| 3001 | + trace_chan_id = (u8)(metadata[i][CS_ETMV4_TRCTRACEIDR]); |
| 2849 | 3002 | break; |
| 2850 | 3003 | default: |
| 2851 | 3004 | /* unknown magic number */ |
| @@ -2858,6 +3011,35 @@ static int cs_etm__map_trace_ids_metadata(int num_cpu, u64 **metadata) · 6 unmodified lines |
| 2858 | 3011 | return 0; |
| 2859 | 3012 | } |
| 2860 | 3013 | |
| 3014 | +/* |
| 3015 | + * If we found AUX_HW_ID packets, then set any metadata marked as unused to the |
| 3016 | + * unused value to reduce the number of unneeded decoders created. |
| 3017 | + */ |
| 3018 | +static int cs_etm__clear_unused_trace_ids_metadata(int num_cpu, u64 **metadata) |
| 3019 | +{ |
| 3020 | + u64 cs_etm_magic; |
| 3021 | + int i; |
| 3022 | + |
| 3023 | + for (i = 0; i < num_cpu; i++) { |
| 3024 | + cs_etm_magic = metadata[i][CS_ETM_MAGIC]; |
| 3025 | + switch (cs_etm_magic) { |
| 3026 | + case __perf_cs_etmv3_magic: |
| 3027 | + if (metadata[i][CS_ETM_ETMTRACEIDR] & CORESIGHT_TRACE_ID_UNUSED_FLAG) |
| 3028 | + metadata[i][CS_ETM_ETMTRACEIDR] = CORESIGHT_TRACE_ID_UNUSED_VAL; |
| 3029 | + break; |
| 3030 | + case __perf_cs_etmv4_magic: |
| 3031 | + case __perf_cs_ete_magic: |
| 3032 | + if (metadata[i][CS_ETMV4_TRCTRACEIDR] & CORESIGHT_TRACE_ID_UNUSED_FLAG) |
| 3033 | + metadata[i][CS_ETMV4_TRCTRACEIDR] = CORESIGHT_TRACE_ID_UNUSED_VAL; |
| 3034 | + break; |
| 3035 | + default: |
| 3036 | + /* unknown magic number */ |
| 3037 | + return -EINVAL; |
| 3038 | + } |
| 3039 | + } |
| 3040 | + return 0; |
| 3041 | +} |
| 3042 | + |
| 2861 | 3043 | int cs_etm__process_auxtrace_info_full(union perf_event *event, |
| 2862 | 3044 | struct perf_session *session) |
| 2863 | 3045 | { |
| @@ -2869,6 +3051,7 @@ int cs_etm__process_auxtrace_info_full(union perf_event *event, · 5 unmodified lines |
| 2869 | 3051 | int priv_size = 0; |
| 2870 | 3052 | int num_cpu; |
| 2871 | 3053 | int err = 0; |
| 3054 | + int aux_hw_id_found; |
| 2872 | 3055 | int i, j; |
| 2873 | 3056 | u64 *ptr = NULL; |
| 2874 | 3057 | u64 **metadata = NULL; |
| @@ -3017,8 +3200,43 @@ int cs_etm__process_auxtrace_info_full(union perf_event *event, · 142 unmodified lines |
| 3017 | 3200 | if (err) |
| 3018 | 3201 | goto err_delete_thread; |
| 3019 | 3202 | |
| 3020 | | − /* before aux records are queued, need to map metadata to trace IDs */ |
| 3021 | | − err = cs_etm__map_trace_ids_metadata(num_cpu, metadata); |
| 3203 | + /* |
| 3204 | + * Map Trace ID values to CPU metadata. |
| 3205 | + * |
| 3206 | + * Trace metadata will always contain Trace ID values from the legacy algorithm. If the |
| 3207 | + * files has been recorded by a "new" perf updated to handle AUX_HW_ID then the metadata |
| 3208 | + * ID value will also have the CORESIGHT_TRACE_ID_UNUSED_FLAG set. |
| 3209 | + * |
| 3210 | + * The updated kernel drivers that use AUX_HW_ID to sent Trace IDs will attempt to use |
| 3211 | + * the same IDs as the old algorithm as far as is possible, unless there are clashes |
| 3212 | + * in which case a different value will be used. This means an older perf may still |
| 3213 | + * be able to record and read files generate on a newer system. |
| 3214 | + * |
| 3215 | + * For a perf able to interpret AUX_HW_ID packets we first check for the presence of |
| 3216 | + * those packets. If they are there then the values will be mapped and plugged into |
| 3217 | + * the metadata. We then set any remaining metadata values with the used flag to a |
| 3218 | + * value CORESIGHT_TRACE_ID_UNUSED_VAL - which indicates no decoder is required. |
| 3219 | + * |
| 3220 | + * If no AUX_HW_ID packets are present - which means a file recorded on an old kernel |
| 3221 | + * then we map Trace ID values to CPU directly from the metadata - clearing any unused |
| 3222 | + * flags if present. |
| 3223 | + */ |
| 3224 | + |
| 3225 | + /* first scan for AUX_OUTPUT_HW_ID records to map trace ID values to CPU metadata */ |
| 3226 | + aux_hw_id_found = 0; |
| 3227 | + err = perf_session__peek_events(session, session->header.data_offset, |
| 3228 | + session->header.data_size, |
| 3229 | + cs_etm__process_aux_hw_id_cb, &aux_hw_id_found); |
| 3230 | + if (err) |
| 3231 | + goto err_delete_thread; |
| 3232 | + |
| 3233 | + /* if HW ID found then clear any unused metadata ID values */ |
| 3234 | + if (aux_hw_id_found) |
| 3235 | + err = cs_etm__clear_unused_trace_ids_metadata(num_cpu, metadata); |
| 3236 | + /* otherwise, this is a file with metadata values only, map from metadata */ |
| 3237 | + else |
| 3238 | + err = cs_etm__map_trace_ids_metadata(num_cpu, metadata); |
| 3239 | + |
| 3022 | 3240 | if (err) |
| 3023 | 3241 | goto err_delete_thread; |
| 3024 | 3242 | |
| @@ -3027,14 +3245,6 @@ int cs_etm__process_auxtrace_info_full(union perf_event *event, · 2 unmodified lines |
| 3027 | 3245 | goto err_delete_thread; |
| 3028 | 3246 | |
| 3029 | 3247 | etm->data_queued = etm->queues.populated; |
| 3030 | | − /* |
| 3031 | | − * Print warning in pipe mode, see cs_etm__process_auxtrace_event() and |
| 3032 | | − * cs_etm__queue_aux_fragment() for details relating to limitations. |
| 3033 | | − */ |
| 3034 | | − if (!etm->data_queued) |
| 3035 | | − pr_warning("CS ETM warning: Coresight decode and TRBE support requires random file access.\n" |
| 3036 | | − "Continuing with best effort decoding in piped mode.\n\n"); |
| 3037 | | − |
| 3038 | 3248 | return 0; |
| 3039 | 3249 | |
| 3040 | 3250 | err_delete_thread: |