xref: /aosp_15_r20/external/libdrm/nouveau/bufctx.c (revision 7688df22e49036ff52a766b7101da3a49edadb8c)
1*7688df22SAndroid Build Coastguard Worker /*
2*7688df22SAndroid Build Coastguard Worker  * Copyright 2012 Red Hat Inc.
3*7688df22SAndroid Build Coastguard Worker  *
4*7688df22SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a
5*7688df22SAndroid Build Coastguard Worker  * copy of this software and associated documentation files (the "Software"),
6*7688df22SAndroid Build Coastguard Worker  * to deal in the Software without restriction, including without limitation
7*7688df22SAndroid Build Coastguard Worker  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*7688df22SAndroid Build Coastguard Worker  * and/or sell copies of the Software, and to permit persons to whom the
9*7688df22SAndroid Build Coastguard Worker  * Software is furnished to do so, subject to the following conditions:
10*7688df22SAndroid Build Coastguard Worker  *
11*7688df22SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice shall be included in
12*7688df22SAndroid Build Coastguard Worker  * all copies or substantial portions of the Software.
13*7688df22SAndroid Build Coastguard Worker  *
14*7688df22SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*7688df22SAndroid Build Coastguard Worker  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*7688df22SAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17*7688df22SAndroid Build Coastguard Worker  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*7688df22SAndroid Build Coastguard Worker  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*7688df22SAndroid Build Coastguard Worker  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*7688df22SAndroid Build Coastguard Worker  * OTHER DEALINGS IN THE SOFTWARE.
21*7688df22SAndroid Build Coastguard Worker  *
22*7688df22SAndroid Build Coastguard Worker  * Authors: Ben Skeggs
23*7688df22SAndroid Build Coastguard Worker  */
24*7688df22SAndroid Build Coastguard Worker 
25*7688df22SAndroid Build Coastguard Worker #include <stdio.h>
26*7688df22SAndroid Build Coastguard Worker #include <stdlib.h>
27*7688df22SAndroid Build Coastguard Worker #include <stdint.h>
28*7688df22SAndroid Build Coastguard Worker #include <stdbool.h>
29*7688df22SAndroid Build Coastguard Worker #include <assert.h>
30*7688df22SAndroid Build Coastguard Worker #include <errno.h>
31*7688df22SAndroid Build Coastguard Worker 
32*7688df22SAndroid Build Coastguard Worker #include "libdrm_lists.h"
33*7688df22SAndroid Build Coastguard Worker 
34*7688df22SAndroid Build Coastguard Worker #include "nouveau.h"
35*7688df22SAndroid Build Coastguard Worker #include "private.h"
36*7688df22SAndroid Build Coastguard Worker 
37*7688df22SAndroid Build Coastguard Worker struct nouveau_bufref_priv {
38*7688df22SAndroid Build Coastguard Worker 	struct nouveau_bufref base;
39*7688df22SAndroid Build Coastguard Worker 	struct nouveau_bufref_priv *next;
40*7688df22SAndroid Build Coastguard Worker 	struct nouveau_bufctx *bufctx;
41*7688df22SAndroid Build Coastguard Worker };
42*7688df22SAndroid Build Coastguard Worker 
43*7688df22SAndroid Build Coastguard Worker struct nouveau_bufbin_priv {
44*7688df22SAndroid Build Coastguard Worker 	struct nouveau_bufref_priv *list;
45*7688df22SAndroid Build Coastguard Worker 	int relocs;
46*7688df22SAndroid Build Coastguard Worker };
47*7688df22SAndroid Build Coastguard Worker 
48*7688df22SAndroid Build Coastguard Worker struct nouveau_bufctx_priv {
49*7688df22SAndroid Build Coastguard Worker 	struct nouveau_bufctx base;
50*7688df22SAndroid Build Coastguard Worker 	struct nouveau_bufref_priv *free;
51*7688df22SAndroid Build Coastguard Worker 	int nr_bins;
52*7688df22SAndroid Build Coastguard Worker 	struct nouveau_bufbin_priv bins[];
53*7688df22SAndroid Build Coastguard Worker };
54*7688df22SAndroid Build Coastguard Worker 
55*7688df22SAndroid Build Coastguard Worker static inline struct nouveau_bufctx_priv *
nouveau_bufctx(struct nouveau_bufctx * bctx)56*7688df22SAndroid Build Coastguard Worker nouveau_bufctx(struct nouveau_bufctx *bctx)
57*7688df22SAndroid Build Coastguard Worker {
58*7688df22SAndroid Build Coastguard Worker 	return (struct nouveau_bufctx_priv *)bctx;
59*7688df22SAndroid Build Coastguard Worker }
60*7688df22SAndroid Build Coastguard Worker 
61*7688df22SAndroid Build Coastguard Worker drm_public int
nouveau_bufctx_new(struct nouveau_client * client,int bins,struct nouveau_bufctx ** pbctx)62*7688df22SAndroid Build Coastguard Worker nouveau_bufctx_new(struct nouveau_client *client, int bins,
63*7688df22SAndroid Build Coastguard Worker 		   struct nouveau_bufctx **pbctx)
64*7688df22SAndroid Build Coastguard Worker {
65*7688df22SAndroid Build Coastguard Worker 	struct nouveau_bufctx_priv *priv;
66*7688df22SAndroid Build Coastguard Worker 
67*7688df22SAndroid Build Coastguard Worker 	priv = calloc(1, sizeof(*priv) + sizeof(priv->bins[0]) * bins);
68*7688df22SAndroid Build Coastguard Worker 	if (priv) {
69*7688df22SAndroid Build Coastguard Worker 		DRMINITLISTHEAD(&priv->base.head);
70*7688df22SAndroid Build Coastguard Worker 		DRMINITLISTHEAD(&priv->base.pending);
71*7688df22SAndroid Build Coastguard Worker 		DRMINITLISTHEAD(&priv->base.current);
72*7688df22SAndroid Build Coastguard Worker 		priv->base.client = client;
73*7688df22SAndroid Build Coastguard Worker 		priv->nr_bins = bins;
74*7688df22SAndroid Build Coastguard Worker 		*pbctx = &priv->base;
75*7688df22SAndroid Build Coastguard Worker 		return 0;
76*7688df22SAndroid Build Coastguard Worker 	}
77*7688df22SAndroid Build Coastguard Worker 
78*7688df22SAndroid Build Coastguard Worker 	return -ENOMEM;
79*7688df22SAndroid Build Coastguard Worker }
80*7688df22SAndroid Build Coastguard Worker 
81*7688df22SAndroid Build Coastguard Worker drm_public void
nouveau_bufctx_del(struct nouveau_bufctx ** pbctx)82*7688df22SAndroid Build Coastguard Worker nouveau_bufctx_del(struct nouveau_bufctx **pbctx)
83*7688df22SAndroid Build Coastguard Worker {
84*7688df22SAndroid Build Coastguard Worker 	struct nouveau_bufctx_priv *pctx = nouveau_bufctx(*pbctx);
85*7688df22SAndroid Build Coastguard Worker 	struct nouveau_bufref_priv *pref;
86*7688df22SAndroid Build Coastguard Worker 	if (pctx) {
87*7688df22SAndroid Build Coastguard Worker 		while (pctx->nr_bins--)
88*7688df22SAndroid Build Coastguard Worker 			nouveau_bufctx_reset(&pctx->base, pctx->nr_bins);
89*7688df22SAndroid Build Coastguard Worker 		while ((pref = pctx->free)) {
90*7688df22SAndroid Build Coastguard Worker 			pctx->free = pref->next;
91*7688df22SAndroid Build Coastguard Worker 			free(pref);
92*7688df22SAndroid Build Coastguard Worker 		}
93*7688df22SAndroid Build Coastguard Worker 		free(pctx);
94*7688df22SAndroid Build Coastguard Worker 		*pbctx = NULL;
95*7688df22SAndroid Build Coastguard Worker 	}
96*7688df22SAndroid Build Coastguard Worker }
97*7688df22SAndroid Build Coastguard Worker 
98*7688df22SAndroid Build Coastguard Worker drm_public void
nouveau_bufctx_reset(struct nouveau_bufctx * bctx,int bin)99*7688df22SAndroid Build Coastguard Worker nouveau_bufctx_reset(struct nouveau_bufctx *bctx, int bin)
100*7688df22SAndroid Build Coastguard Worker {
101*7688df22SAndroid Build Coastguard Worker 	struct nouveau_bufctx_priv *pctx = nouveau_bufctx(bctx);
102*7688df22SAndroid Build Coastguard Worker 	struct nouveau_bufbin_priv *pbin = &pctx->bins[bin];
103*7688df22SAndroid Build Coastguard Worker 	struct nouveau_bufref_priv *pref;
104*7688df22SAndroid Build Coastguard Worker 
105*7688df22SAndroid Build Coastguard Worker 	while ((pref = pbin->list)) {
106*7688df22SAndroid Build Coastguard Worker 		DRMLISTDELINIT(&pref->base.thead);
107*7688df22SAndroid Build Coastguard Worker 		pbin->list = pref->next;
108*7688df22SAndroid Build Coastguard Worker 		pref->next = pctx->free;
109*7688df22SAndroid Build Coastguard Worker 		pctx->free = pref;
110*7688df22SAndroid Build Coastguard Worker 	}
111*7688df22SAndroid Build Coastguard Worker 
112*7688df22SAndroid Build Coastguard Worker 	bctx->relocs -= pbin->relocs;
113*7688df22SAndroid Build Coastguard Worker 	pbin->relocs  = 0;
114*7688df22SAndroid Build Coastguard Worker }
115*7688df22SAndroid Build Coastguard Worker 
116*7688df22SAndroid Build Coastguard Worker drm_public struct nouveau_bufref *
nouveau_bufctx_refn(struct nouveau_bufctx * bctx,int bin,struct nouveau_bo * bo,uint32_t flags)117*7688df22SAndroid Build Coastguard Worker nouveau_bufctx_refn(struct nouveau_bufctx *bctx, int bin,
118*7688df22SAndroid Build Coastguard Worker 		    struct nouveau_bo *bo, uint32_t flags)
119*7688df22SAndroid Build Coastguard Worker {
120*7688df22SAndroid Build Coastguard Worker 	struct nouveau_bufctx_priv *pctx = nouveau_bufctx(bctx);
121*7688df22SAndroid Build Coastguard Worker 	struct nouveau_bufbin_priv *pbin = &pctx->bins[bin];
122*7688df22SAndroid Build Coastguard Worker 	struct nouveau_bufref_priv *pref = pctx->free;
123*7688df22SAndroid Build Coastguard Worker 
124*7688df22SAndroid Build Coastguard Worker 	if (!pref)
125*7688df22SAndroid Build Coastguard Worker 		pref = malloc(sizeof(*pref));
126*7688df22SAndroid Build Coastguard Worker 	else
127*7688df22SAndroid Build Coastguard Worker 		pctx->free = pref->next;
128*7688df22SAndroid Build Coastguard Worker 
129*7688df22SAndroid Build Coastguard Worker 	if (pref) {
130*7688df22SAndroid Build Coastguard Worker 		pref->base.bo = bo;
131*7688df22SAndroid Build Coastguard Worker 		pref->base.flags = flags;
132*7688df22SAndroid Build Coastguard Worker 		pref->base.packet = 0;
133*7688df22SAndroid Build Coastguard Worker 
134*7688df22SAndroid Build Coastguard Worker 		DRMLISTADDTAIL(&pref->base.thead, &bctx->pending);
135*7688df22SAndroid Build Coastguard Worker 		pref->bufctx = bctx;
136*7688df22SAndroid Build Coastguard Worker 		pref->next = pbin->list;
137*7688df22SAndroid Build Coastguard Worker 		pbin->list = pref;
138*7688df22SAndroid Build Coastguard Worker 	}
139*7688df22SAndroid Build Coastguard Worker 
140*7688df22SAndroid Build Coastguard Worker 	return &pref->base;
141*7688df22SAndroid Build Coastguard Worker }
142*7688df22SAndroid Build Coastguard Worker 
143*7688df22SAndroid Build Coastguard Worker drm_public struct nouveau_bufref *
nouveau_bufctx_mthd(struct nouveau_bufctx * bctx,int bin,uint32_t packet,struct nouveau_bo * bo,uint64_t data,uint32_t flags,uint32_t vor,uint32_t tor)144*7688df22SAndroid Build Coastguard Worker nouveau_bufctx_mthd(struct nouveau_bufctx *bctx, int bin, uint32_t packet,
145*7688df22SAndroid Build Coastguard Worker 		    struct nouveau_bo *bo, uint64_t data, uint32_t flags,
146*7688df22SAndroid Build Coastguard Worker 		    uint32_t vor, uint32_t tor)
147*7688df22SAndroid Build Coastguard Worker {
148*7688df22SAndroid Build Coastguard Worker 	struct nouveau_bufctx_priv *pctx = nouveau_bufctx(bctx);
149*7688df22SAndroid Build Coastguard Worker 	struct nouveau_bufbin_priv *pbin = &pctx->bins[bin];
150*7688df22SAndroid Build Coastguard Worker 	struct nouveau_bufref *bref = nouveau_bufctx_refn(bctx, bin, bo, flags);
151*7688df22SAndroid Build Coastguard Worker 	if (bref) {
152*7688df22SAndroid Build Coastguard Worker 		bref->packet = packet;
153*7688df22SAndroid Build Coastguard Worker 		bref->data = data;
154*7688df22SAndroid Build Coastguard Worker 		bref->vor = vor;
155*7688df22SAndroid Build Coastguard Worker 		bref->tor = tor;
156*7688df22SAndroid Build Coastguard Worker 		pbin->relocs++;
157*7688df22SAndroid Build Coastguard Worker 		bctx->relocs++;
158*7688df22SAndroid Build Coastguard Worker 	}
159*7688df22SAndroid Build Coastguard Worker 	return bref;
160*7688df22SAndroid Build Coastguard Worker }
161