Path: blob/21.2-virgl/src/gallium/drivers/freedreno/a2xx/fd2_query.c
4574 views
/*1* Copyright (C) 2018 Jonathan Marek <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*22* Authors:23* Jonathan Marek <[email protected]>24* Rob Clark <[email protected]>25*/2627/* NOTE: perfcntrs are 48-bits but we only have 32-bit accumulate (?)28* so we work with 32-bits only. we accumulate start/stop separately,29* which differs from a5xx but works with only accumulate (no add/neg)30*/3132#include "freedreno_query_acc.h"33#include "freedreno_resource.h"3435#include "fd2_context.h"36#include "fd2_query.h"3738struct PACKED fd2_query_sample {39uint32_t start;40uint32_t stop;41};4243/* offset of a single field of an array of fd2_query_sample: */44#define query_sample_idx(aq, idx, field) \45fd_resource((aq)->prsc)->bo, \46(idx * sizeof(struct fd2_query_sample)) + \47offsetof(struct fd2_query_sample, field), \480, 04950/* offset of a single field of fd2_query_sample: */51#define query_sample(aq, field) query_sample_idx(aq, 0, field)5253/*54* Performance Counter (batch) queries:55*56* Only one of these is active at a time, per design of the gallium57* batch_query API design. On perfcntr query tracks N query_types,58* each of which has a 'fd_batch_query_entry' that maps it back to59* the associated group and counter.60*/6162struct fd_batch_query_entry {63uint8_t gid; /* group-id */64uint8_t cid; /* countable-id within the group */65};6667struct fd_batch_query_data {68struct fd_screen *screen;69unsigned num_query_entries;70struct fd_batch_query_entry query_entries[];71};7273static void74perfcntr_resume(struct fd_acc_query *aq, struct fd_batch *batch) assert_dt75{76struct fd_batch_query_data *data = aq->query_data;77struct fd_screen *screen = data->screen;78struct fd_ringbuffer *ring = batch->draw;7980unsigned counters_per_group[screen->num_perfcntr_groups];81memset(counters_per_group, 0, sizeof(counters_per_group));8283fd_wfi(batch, ring);8485/* configure performance counters for the requested queries: */86for (unsigned i = 0; i < data->num_query_entries; i++) {87struct fd_batch_query_entry *entry = &data->query_entries[i];88const struct fd_perfcntr_group *g = &screen->perfcntr_groups[entry->gid];89unsigned counter_idx = counters_per_group[entry->gid]++;9091debug_assert(counter_idx < g->num_counters);9293OUT_PKT0(ring, g->counters[counter_idx].select_reg, 1);94OUT_RING(ring, g->countables[entry->cid].selector);95}9697memset(counters_per_group, 0, sizeof(counters_per_group));9899/* and snapshot the start values */100for (unsigned i = 0; i < data->num_query_entries; i++) {101struct fd_batch_query_entry *entry = &data->query_entries[i];102const struct fd_perfcntr_group *g = &screen->perfcntr_groups[entry->gid];103unsigned counter_idx = counters_per_group[entry->gid]++;104const struct fd_perfcntr_counter *counter = &g->counters[counter_idx];105106OUT_PKT3(ring, CP_REG_TO_MEM, 2);107OUT_RING(ring, counter->counter_reg_lo | CP_REG_TO_MEM_0_ACCUMULATE);108OUT_RELOC(ring, query_sample_idx(aq, i, start));109}110}111112static void113perfcntr_pause(struct fd_acc_query *aq, struct fd_batch *batch) assert_dt114{115struct fd_batch_query_data *data = aq->query_data;116struct fd_screen *screen = data->screen;117struct fd_ringbuffer *ring = batch->draw;118119unsigned counters_per_group[screen->num_perfcntr_groups];120memset(counters_per_group, 0, sizeof(counters_per_group));121122fd_wfi(batch, ring);123124/* TODO do we need to bother to turn anything off? */125126/* snapshot the end values: */127for (unsigned i = 0; i < data->num_query_entries; i++) {128struct fd_batch_query_entry *entry = &data->query_entries[i];129const struct fd_perfcntr_group *g = &screen->perfcntr_groups[entry->gid];130unsigned counter_idx = counters_per_group[entry->gid]++;131const struct fd_perfcntr_counter *counter = &g->counters[counter_idx];132133OUT_PKT3(ring, CP_REG_TO_MEM, 2);134OUT_RING(ring, counter->counter_reg_lo | CP_REG_TO_MEM_0_ACCUMULATE);135OUT_RELOC(ring, query_sample_idx(aq, i, stop));136}137}138139static void140perfcntr_accumulate_result(struct fd_acc_query *aq, void *buf,141union pipe_query_result *result)142{143struct fd_batch_query_data *data = aq->query_data;144struct fd2_query_sample *sp = buf;145146for (unsigned i = 0; i < data->num_query_entries; i++)147result->batch[i].u64 = sp[i].stop - sp[i].start;148}149150static const struct fd_acc_sample_provider perfcntr = {151.query_type = FD_QUERY_FIRST_PERFCNTR,152.always = true,153.resume = perfcntr_resume,154.pause = perfcntr_pause,155.result = perfcntr_accumulate_result,156};157158static struct pipe_query *159fd2_create_batch_query(struct pipe_context *pctx, unsigned num_queries,160unsigned *query_types)161{162struct fd_context *ctx = fd_context(pctx);163struct fd_screen *screen = ctx->screen;164struct fd_query *q;165struct fd_acc_query *aq;166struct fd_batch_query_data *data;167168data = CALLOC_VARIANT_LENGTH_STRUCT(169fd_batch_query_data, num_queries * sizeof(data->query_entries[0]));170171data->screen = screen;172data->num_query_entries = num_queries;173174/* validate the requested query_types and ensure we don't try175* to request more query_types of a given group than we have176* counters:177*/178unsigned counters_per_group[screen->num_perfcntr_groups];179memset(counters_per_group, 0, sizeof(counters_per_group));180181for (unsigned i = 0; i < num_queries; i++) {182unsigned idx = query_types[i] - FD_QUERY_FIRST_PERFCNTR;183184/* verify valid query_type, ie. is it actually a perfcntr? */185if ((query_types[i] < FD_QUERY_FIRST_PERFCNTR) ||186(idx >= screen->num_perfcntr_queries)) {187mesa_loge("invalid batch query query_type: %u", query_types[i]);188goto error;189}190191struct fd_batch_query_entry *entry = &data->query_entries[i];192struct pipe_driver_query_info *pq = &screen->perfcntr_queries[idx];193194entry->gid = pq->group_id;195196/* the perfcntr_queries[] table flattens all the countables197* for each group in series, ie:198*199* (G0,C0), .., (G0,Cn), (G1,C0), .., (G1,Cm), ...200*201* So to find the countable index just step back through the202* table to find the first entry with the same group-id.203*/204while (pq > screen->perfcntr_queries) {205pq--;206if (pq->group_id == entry->gid)207entry->cid++;208}209210if (counters_per_group[entry->gid] >=211screen->perfcntr_groups[entry->gid].num_counters) {212mesa_loge("too many counters for group %u", entry->gid);213goto error;214}215216counters_per_group[entry->gid]++;217}218219q = fd_acc_create_query2(ctx, 0, 0, &perfcntr);220aq = fd_acc_query(q);221222/* sample buffer size is based on # of queries: */223aq->size = num_queries * sizeof(struct fd2_query_sample);224aq->query_data = data;225226return (struct pipe_query *)q;227228error:229free(data);230return NULL;231}232233void234fd2_query_context_init(struct pipe_context *pctx) disable_thread_safety_analysis235{236struct fd_context *ctx = fd_context(pctx);237238ctx->create_query = fd_acc_create_query;239ctx->query_update_batch = fd_acc_query_update_batch;240241pctx->create_batch_query = fd2_create_batch_query;242}243244245