2 * Copyright 2012 Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
36 #include "libdrm_lists.h"
41 struct nouveau_bufref_priv {
42 struct nouveau_bufref base;
43 struct nouveau_bufref_priv *next;
44 struct nouveau_bufctx *bufctx;
47 struct nouveau_bufbin_priv {
48 struct nouveau_bufref_priv *list;
52 struct nouveau_bufctx_priv {
53 struct nouveau_bufctx base;
54 struct nouveau_bufref_priv *free;
56 struct nouveau_bufbin_priv bins[];
59 static inline struct nouveau_bufctx_priv *
60 nouveau_bufctx(struct nouveau_bufctx *bctx)
62 return (struct nouveau_bufctx_priv *)bctx;
66 nouveau_bufctx_new(struct nouveau_client *client, int bins,
67 struct nouveau_bufctx **pbctx)
69 struct nouveau_bufctx_priv *priv;
71 priv = calloc(1, sizeof(*priv) + sizeof(priv->bins[0]) * bins);
73 DRMINITLISTHEAD(&priv->base.head);
74 DRMINITLISTHEAD(&priv->base.pending);
75 DRMINITLISTHEAD(&priv->base.current);
76 priv->base.client = client;
86 nouveau_bufctx_del(struct nouveau_bufctx **pbctx)
88 struct nouveau_bufctx_priv *pctx = nouveau_bufctx(*pbctx);
89 struct nouveau_bufref_priv *pref;
91 while (pctx->nr_bins--)
92 nouveau_bufctx_reset(&pctx->base, pctx->nr_bins);
93 while ((pref = pctx->free)) {
94 pctx->free = pref->next;
103 nouveau_bufctx_reset(struct nouveau_bufctx *bctx, int bin)
105 struct nouveau_bufctx_priv *pctx = nouveau_bufctx(bctx);
106 struct nouveau_bufbin_priv *pbin = &pctx->bins[bin];
107 struct nouveau_bufref_priv *pref;
109 while ((pref = pbin->list)) {
110 DRMLISTDELINIT(&pref->base.thead);
111 pbin->list = pref->next;
112 pref->next = pctx->free;
116 bctx->relocs -= pbin->relocs;
120 drm_public struct nouveau_bufref *
121 nouveau_bufctx_refn(struct nouveau_bufctx *bctx, int bin,
122 struct nouveau_bo *bo, uint32_t flags)
124 struct nouveau_bufctx_priv *pctx = nouveau_bufctx(bctx);
125 struct nouveau_bufbin_priv *pbin = &pctx->bins[bin];
126 struct nouveau_bufref_priv *pref = pctx->free;
129 pref = malloc(sizeof(*pref));
131 pctx->free = pref->next;
135 pref->base.flags = flags;
136 pref->base.packet = 0;
138 DRMLISTADDTAIL(&pref->base.thead, &bctx->pending);
140 pref->next = pbin->list;
147 drm_public struct nouveau_bufref *
148 nouveau_bufctx_mthd(struct nouveau_bufctx *bctx, int bin, uint32_t packet,
149 struct nouveau_bo *bo, uint64_t data, uint32_t flags,
150 uint32_t vor, uint32_t tor)
152 struct nouveau_bufctx_priv *pctx = nouveau_bufctx(bctx);
153 struct nouveau_bufbin_priv *pbin = &pctx->bins[bin];
154 struct nouveau_bufref *bref = nouveau_bufctx_refn(bctx, bin, bo, flags);
156 bref->packet = packet;