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 static inline struct nouveau_bufref_priv *
48 nouveau_bufref(struct nouveau_bufref *bctx)
50 return (struct nouveau_bufref_priv *)bctx;
53 struct nouveau_bufbin_priv {
54 struct nouveau_bufref_priv *list;
58 struct nouveau_bufctx_priv {
59 struct nouveau_bufctx base;
60 struct nouveau_bufref_priv *free;
62 struct nouveau_bufbin_priv bins[];
65 static inline struct nouveau_bufctx_priv *
66 nouveau_bufctx(struct nouveau_bufctx *bctx)
68 return (struct nouveau_bufctx_priv *)bctx;
72 nouveau_bufctx_new(struct nouveau_client *client, int bins,
73 struct nouveau_bufctx **pbctx)
75 struct nouveau_bufctx_priv *priv;
77 priv = calloc(1, sizeof(*priv) + sizeof(priv->bins[0]) * bins);
79 DRMINITLISTHEAD(&priv->base.head);
80 DRMINITLISTHEAD(&priv->base.pending);
81 DRMINITLISTHEAD(&priv->base.current);
82 priv->base.client = client;
92 nouveau_bufctx_del(struct nouveau_bufctx **pbctx)
94 struct nouveau_bufctx_priv *pctx = nouveau_bufctx(*pbctx);
95 struct nouveau_bufref_priv *pref;
97 while (pctx->nr_bins--)
98 nouveau_bufctx_reset(&pctx->base, pctx->nr_bins);
99 while ((pref = pctx->free)) {
100 pctx->free = pref->next;
109 nouveau_bufctx_reset(struct nouveau_bufctx *bctx, int bin)
111 struct nouveau_bufctx_priv *pctx = nouveau_bufctx(bctx);
112 struct nouveau_bufbin_priv *pbin = &pctx->bins[bin];
113 struct nouveau_bufref_priv *pref;
115 while ((pref = pbin->list)) {
116 DRMLISTDELINIT(&pref->base.thead);
117 pbin->list = pref->next;
118 pref->next = pctx->free;
122 bctx->relocs -= pbin->relocs;
126 drm_public struct nouveau_bufref *
127 nouveau_bufctx_refn(struct nouveau_bufctx *bctx, int bin,
128 struct nouveau_bo *bo, uint32_t flags)
130 struct nouveau_bufctx_priv *pctx = nouveau_bufctx(bctx);
131 struct nouveau_bufbin_priv *pbin = &pctx->bins[bin];
132 struct nouveau_bufref_priv *pref = pctx->free;
135 pref = malloc(sizeof(*pref));
137 pctx->free = pref->next;
141 pref->base.flags = flags;
142 pref->base.packet = 0;
144 DRMLISTADDTAIL(&pref->base.thead, &bctx->pending);
146 pref->next = pbin->list;
153 drm_public struct nouveau_bufref *
154 nouveau_bufctx_mthd(struct nouveau_bufctx *bctx, int bin, uint32_t packet,
155 struct nouveau_bo *bo, uint64_t data, uint32_t flags,
156 uint32_t vor, uint32_t tor)
158 struct nouveau_bufctx_priv *pctx = nouveau_bufctx(bctx);
159 struct nouveau_bufbin_priv *pbin = &pctx->bins[bin];
160 struct nouveau_bufref *bref = nouveau_bufctx_refn(bctx, bin, bo, flags);
162 bref->packet = packet;