Path: blob/21.2-virgl/src/gallium/drivers/nouveau/nv30/nv30_query.c
4574 views
/*1* Copyright 2012 Red Hat Inc.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 shall be included in11* all copies or substantial portions of the Software.12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR17* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,18* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR19* OTHER DEALINGS IN THE SOFTWARE.20*21* Authors: Ben Skeggs22*23*/2425#include "nv_object.xml.h"26#include "nv30/nv30-40_3d.xml.h"27#include "nv30/nv30_screen.h"28#include "nv30/nv30_context.h"2930#define LIST_FIRST_ENTRY(__type, __item, __field) \31LIST_ENTRY(__type, (__item)->next, __field)3233struct nv30_query_object {34struct list_head list;35struct nouveau_heap *hw;36};3738static volatile void *39nv30_ntfy(struct nv30_screen *screen, struct nv30_query_object *qo)40{41struct nv04_notify *query = screen->query->data;42struct nouveau_bo *notify = screen->notify;43volatile void *ntfy = NULL;4445if (qo && qo->hw)46ntfy = (char *)notify->map + query->offset + qo->hw->start;4748return ntfy;49}5051static void52nv30_query_object_del(struct nv30_screen *screen, struct nv30_query_object **po)53{54struct nv30_query_object *qo = *po; *po = NULL;55if (qo) {56volatile uint32_t *ntfy = nv30_ntfy(screen, qo);57while (ntfy[3] & 0xff000000) {58}59nouveau_heap_free(&qo->hw);60list_del(&qo->list);61FREE(qo);62}63}6465static struct nv30_query_object *66nv30_query_object_new(struct nv30_screen *screen)67{68struct nv30_query_object *oq, *qo = CALLOC_STRUCT(nv30_query_object);69volatile uint32_t *ntfy;7071if (!qo)72return NULL;7374/* allocate a new hw query object, if no hw objects left we need to75* spin waiting for one to become free76*/77while (nouveau_heap_alloc(screen->query_heap, 32, NULL, &qo->hw)) {78oq = LIST_FIRST_ENTRY(struct nv30_query_object, &screen->queries, list);79nv30_query_object_del(screen, &oq);80}8182list_addtail(&qo->list, &screen->queries);8384ntfy = nv30_ntfy(screen, qo);85ntfy[0] = 0x00000000;86ntfy[1] = 0x00000000;87ntfy[2] = 0x00000000;88ntfy[3] = 0x01000000;89return qo;90}9192struct nv30_query {93struct nv30_query_object *qo[2];94unsigned type;95uint32_t report;96uint32_t enable;97uint64_t result;98};99100static inline struct nv30_query *101nv30_query(struct pipe_query *pipe)102{103return (struct nv30_query *)pipe;104}105106static struct pipe_query *107nv30_query_create(struct pipe_context *pipe, unsigned type, unsigned index)108{109struct nv30_query *q = CALLOC_STRUCT(nv30_query);110if (!q)111return NULL;112113q->type = type;114115switch (q->type) {116case PIPE_QUERY_TIMESTAMP:117case PIPE_QUERY_TIME_ELAPSED:118q->enable = 0x0000;119q->report = 1;120break;121case PIPE_QUERY_OCCLUSION_COUNTER:122case PIPE_QUERY_OCCLUSION_PREDICATE:123case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:124q->enable = NV30_3D_QUERY_ENABLE;125q->report = 1;126break;127case NV30_QUERY_ZCULL_0:128case NV30_QUERY_ZCULL_1:129case NV30_QUERY_ZCULL_2:130case NV30_QUERY_ZCULL_3:131q->enable = 0x1804;132q->report = 2 + (q->type - NV30_QUERY_ZCULL_0);133break;134default:135FREE(q);136return NULL;137}138139return (struct pipe_query *)q;140}141142static void143nv30_query_destroy(struct pipe_context *pipe, struct pipe_query *pq)144{145FREE(pq);146}147148static bool149nv30_query_begin(struct pipe_context *pipe, struct pipe_query *pq)150{151struct nv30_context *nv30 = nv30_context(pipe);152struct nv30_query *q = nv30_query(pq);153struct nouveau_pushbuf *push = nv30->base.pushbuf;154155switch (q->type) {156case PIPE_QUERY_TIME_ELAPSED:157q->qo[0] = nv30_query_object_new(nv30->screen);158if (q->qo[0]) {159BEGIN_NV04(push, NV30_3D(QUERY_GET), 1);160PUSH_DATA (push, (q->report << 24) | q->qo[0]->hw->start);161}162break;163case PIPE_QUERY_TIMESTAMP:164return true;165default:166BEGIN_NV04(push, NV30_3D(QUERY_RESET), 1);167PUSH_DATA (push, q->report);168break;169}170171if (q->enable) {172BEGIN_NV04(push, SUBC_3D(q->enable), 1);173PUSH_DATA (push, 1);174}175return true;176}177178static bool179nv30_query_end(struct pipe_context *pipe, struct pipe_query *pq)180{181struct nv30_context *nv30 = nv30_context(pipe);182struct nv30_screen *screen = nv30->screen;183struct nv30_query *q = nv30_query(pq);184struct nouveau_pushbuf *push = nv30->base.pushbuf;185186q->qo[1] = nv30_query_object_new(screen);187if (q->qo[1]) {188BEGIN_NV04(push, NV30_3D(QUERY_GET), 1);189PUSH_DATA (push, (q->report << 24) | q->qo[1]->hw->start);190}191192if (q->enable) {193BEGIN_NV04(push, SUBC_3D(q->enable), 1);194PUSH_DATA (push, 0);195}196PUSH_KICK (push);197return true;198}199200static bool201nv30_query_result(struct pipe_context *pipe, struct pipe_query *pq,202bool wait, union pipe_query_result *result)203{204struct nv30_screen *screen = nv30_screen(pipe->screen);205struct nv30_query *q = nv30_query(pq);206volatile uint32_t *ntfy0 = nv30_ntfy(screen, q->qo[0]);207volatile uint32_t *ntfy1 = nv30_ntfy(screen, q->qo[1]);208209if (ntfy1) {210while (ntfy1[3] & 0xff000000) {211if (!wait)212return false;213}214215switch (q->type) {216case PIPE_QUERY_TIMESTAMP:217q->result = *(uint64_t *)&ntfy1[0];218break;219case PIPE_QUERY_TIME_ELAPSED:220q->result = *(uint64_t *)&ntfy1[0] - *(uint64_t *)&ntfy0[0];221break;222default:223q->result = ntfy1[2];224break;225}226227nv30_query_object_del(screen, &q->qo[0]);228nv30_query_object_del(screen, &q->qo[1]);229}230231if (q->type == PIPE_QUERY_OCCLUSION_PREDICATE ||232q->type == PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE)233result->b = !!q->result;234else235result->u64 = q->result;236return true;237}238239static void240nv40_query_render_condition(struct pipe_context *pipe,241struct pipe_query *pq,242bool condition, enum pipe_render_cond_flag mode)243{244struct nv30_context *nv30 = nv30_context(pipe);245struct nv30_query *q = nv30_query(pq);246struct nouveau_pushbuf *push = nv30->base.pushbuf;247248nv30->render_cond_query = pq;249nv30->render_cond_mode = mode;250nv30->render_cond_cond = condition;251252if (!pq) {253BEGIN_NV04(push, SUBC_3D(0x1e98), 1);254PUSH_DATA (push, 0x01000000);255return;256}257258if (mode == PIPE_RENDER_COND_WAIT ||259mode == PIPE_RENDER_COND_BY_REGION_WAIT) {260BEGIN_NV04(push, SUBC_3D(0x0110), 1);261PUSH_DATA (push, 0);262}263264BEGIN_NV04(push, SUBC_3D(0x1e98), 1);265PUSH_DATA (push, 0x02000000 | q->qo[1]->hw->start);266}267268static void269nv30_set_active_query_state(struct pipe_context *pipe, bool enable)270{271}272273void274nv30_query_init(struct pipe_context *pipe)275{276struct nouveau_object *eng3d = nv30_context(pipe)->screen->eng3d;277278pipe->create_query = nv30_query_create;279pipe->destroy_query = nv30_query_destroy;280pipe->begin_query = nv30_query_begin;281pipe->end_query = nv30_query_end;282pipe->get_query_result = nv30_query_result;283pipe->set_active_query_state = nv30_set_active_query_state;284if (eng3d->oclass >= NV40_3D_CLASS)285pipe->render_condition = nv40_query_render_condition;286}287288289