Path: blob/21.2-virgl/src/gallium/drivers/vc4/vc4_query.c
4570 views
/*1* Copyright © 2014 Broadcom2*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, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223/**24* Expose V3D HW perf counters.25*26* We also have code to fake support for occlusion queries.27* Since we expose support for GL 2.0, we have to expose occlusion queries,28* but the spec allows you to expose 0 query counter bits, so we just return 029* as the result of all our queries.30*/31#include "vc4_context.h"3233struct vc4_query34{35unsigned num_queries;36struct vc4_hwperfmon *hwperfmon;37};3839static const char *v3d_counter_names[] = {40"FEP-valid-primitives-no-rendered-pixels",41"FEP-valid-primitives-rendered-pixels",42"FEP-clipped-quads",43"FEP-valid-quads",44"TLB-quads-not-passing-stencil-test",45"TLB-quads-not-passing-z-and-stencil-test",46"TLB-quads-passing-z-and-stencil-test",47"TLB-quads-with-zero-coverage",48"TLB-quads-with-non-zero-coverage",49"TLB-quads-written-to-color-buffer",50"PTB-primitives-discarded-outside-viewport",51"PTB-primitives-need-clipping",52"PTB-primitives-discared-reversed",53"QPU-total-idle-clk-cycles",54"QPU-total-clk-cycles-vertex-coord-shading",55"QPU-total-clk-cycles-fragment-shading",56"QPU-total-clk-cycles-executing-valid-instr",57"QPU-total-clk-cycles-waiting-TMU",58"QPU-total-clk-cycles-waiting-scoreboard",59"QPU-total-clk-cycles-waiting-varyings",60"QPU-total-instr-cache-hit",61"QPU-total-instr-cache-miss",62"QPU-total-uniform-cache-hit",63"QPU-total-uniform-cache-miss",64"TMU-total-text-quads-processed",65"TMU-total-text-cache-miss",66"VPM-total-clk-cycles-VDW-stalled",67"VPM-total-clk-cycles-VCD-stalled",68"L2C-total-cache-hit",69"L2C-total-cache-miss",70};7172int vc4_get_driver_query_group_info(struct pipe_screen *pscreen,73unsigned index,74struct pipe_driver_query_group_info *info)75{76struct vc4_screen *screen = vc4_screen(pscreen);7778if (!screen->has_perfmon_ioctl)79return 0;8081if (!info)82return 1;8384if (index > 0)85return 0;8687info->name = "V3D counters";88info->max_active_queries = DRM_VC4_MAX_PERF_COUNTERS;89info->num_queries = ARRAY_SIZE(v3d_counter_names);90return 1;91}9293int vc4_get_driver_query_info(struct pipe_screen *pscreen, unsigned index,94struct pipe_driver_query_info *info)95{96struct vc4_screen *screen = vc4_screen(pscreen);9798if (!screen->has_perfmon_ioctl)99return 0;100101if (!info)102return ARRAY_SIZE(v3d_counter_names);103104if (index >= ARRAY_SIZE(v3d_counter_names))105return 0;106107info->group_id = 0;108info->name = v3d_counter_names[index];109info->query_type = PIPE_QUERY_DRIVER_SPECIFIC + index;110info->result_type = PIPE_DRIVER_QUERY_RESULT_TYPE_CUMULATIVE;111info->type = PIPE_DRIVER_QUERY_TYPE_UINT64;112info->flags = PIPE_DRIVER_QUERY_FLAG_BATCH;113return 1;114}115116static struct pipe_query *117vc4_create_batch_query(struct pipe_context *pctx, unsigned num_queries,118unsigned *query_types)119{120struct vc4_query *query = calloc(1, sizeof(*query));121struct vc4_hwperfmon *hwperfmon;122unsigned i, nhwqueries = 0;123124if (!query)125return NULL;126127for (i = 0; i < num_queries; i++) {128if (query_types[i] >= PIPE_QUERY_DRIVER_SPECIFIC)129nhwqueries++;130}131132/* We can't mix HW and non-HW queries. */133if (nhwqueries && nhwqueries != num_queries)134goto err_free_query;135136if (!nhwqueries)137return (struct pipe_query *)query;138139hwperfmon = calloc(1, sizeof(*hwperfmon));140if (!hwperfmon)141goto err_free_query;142143for (i = 0; i < num_queries; i++)144hwperfmon->events[i] = query_types[i] -145PIPE_QUERY_DRIVER_SPECIFIC;146147query->hwperfmon = hwperfmon;148query->num_queries = num_queries;149150/* Note that struct pipe_query isn't actually defined anywhere. */151return (struct pipe_query *)query;152153err_free_query:154free(query);155156return NULL;157}158159static struct pipe_query *160vc4_create_query(struct pipe_context *ctx, unsigned query_type, unsigned index)161{162return vc4_create_batch_query(ctx, 1, &query_type);163}164165static void166vc4_destroy_query(struct pipe_context *pctx, struct pipe_query *pquery)167{168struct vc4_context *ctx = vc4_context(pctx);169struct vc4_query *query = (struct vc4_query *)pquery;170171if (query->hwperfmon && query->hwperfmon->id) {172if (query->hwperfmon->id) {173struct drm_vc4_perfmon_destroy req = { };174175req.id = query->hwperfmon->id;176vc4_ioctl(ctx->fd, DRM_IOCTL_VC4_PERFMON_DESTROY,177&req);178}179180free(query->hwperfmon);181}182183free(query);184}185186static bool187vc4_begin_query(struct pipe_context *pctx, struct pipe_query *pquery)188{189struct vc4_query *query = (struct vc4_query *)pquery;190struct vc4_context *ctx = vc4_context(pctx);191struct drm_vc4_perfmon_create req = { };192unsigned i;193int ret;194195if (!query->hwperfmon)196return true;197198/* Only one perfmon can be activated per context. */199if (ctx->perfmon)200return false;201202/* Reset the counters by destroying the previously allocated perfmon */203if (query->hwperfmon->id) {204struct drm_vc4_perfmon_destroy destroyreq = { };205206destroyreq.id = query->hwperfmon->id;207vc4_ioctl(ctx->fd, DRM_IOCTL_VC4_PERFMON_DESTROY, &destroyreq);208}209210for (i = 0; i < query->num_queries; i++)211req.events[i] = query->hwperfmon->events[i];212213req.ncounters = query->num_queries;214ret = vc4_ioctl(ctx->fd, DRM_IOCTL_VC4_PERFMON_CREATE, &req);215if (ret)216return false;217218query->hwperfmon->id = req.id;219220/* Make sure all pendings jobs are flushed before activating the221* perfmon.222*/223vc4_flush(pctx);224ctx->perfmon = query->hwperfmon;225return true;226}227228static bool229vc4_end_query(struct pipe_context *pctx, struct pipe_query *pquery)230{231struct vc4_query *query = (struct vc4_query *)pquery;232struct vc4_context *ctx = vc4_context(pctx);233234if (!query->hwperfmon)235return true;236237if (ctx->perfmon != query->hwperfmon)238return false;239240/* Make sure all pendings jobs are flushed before deactivating the241* perfmon.242*/243vc4_flush(pctx);244ctx->perfmon = NULL;245return true;246}247248static bool249vc4_get_query_result(struct pipe_context *pctx, struct pipe_query *pquery,250bool wait, union pipe_query_result *vresult)251{252struct vc4_context *ctx = vc4_context(pctx);253struct vc4_query *query = (struct vc4_query *)pquery;254struct drm_vc4_perfmon_get_values req;255unsigned i;256int ret;257258if (!query->hwperfmon) {259vresult->u64 = 0;260return true;261}262263if (!vc4_wait_seqno(ctx->screen, query->hwperfmon->last_seqno,264wait ? PIPE_TIMEOUT_INFINITE : 0, "perfmon"))265return false;266267req.id = query->hwperfmon->id;268req.values_ptr = (uintptr_t)query->hwperfmon->counters;269ret = vc4_ioctl(ctx->fd, DRM_IOCTL_VC4_PERFMON_GET_VALUES, &req);270if (ret)271return false;272273for (i = 0; i < query->num_queries; i++)274vresult->batch[i].u64 = query->hwperfmon->counters[i];275276return true;277}278279static void280vc4_set_active_query_state(struct pipe_context *pctx, bool enable)281{282}283284void285vc4_query_init(struct pipe_context *pctx)286{287pctx->create_query = vc4_create_query;288pctx->create_batch_query = vc4_create_batch_query;289pctx->destroy_query = vc4_destroy_query;290pctx->begin_query = vc4_begin_query;291pctx->end_query = vc4_end_query;292pctx->get_query_result = vc4_get_query_result;293pctx->set_active_query_state = vc4_set_active_query_state;294}295296297