Back to index

libdrm  2.4.37
bufctx.c
Go to the documentation of this file.
00001 /*
00002  * Copyright 2012 Red Hat Inc.
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a
00005  * copy of this software and associated documentation files (the "Software"),
00006  * to deal in the Software without restriction, including without limitation
00007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008  * and/or sell copies of the Software, and to permit persons to whom the
00009  * Software is furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
00017  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
00018  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020  * OTHER DEALINGS IN THE SOFTWARE.
00021  *
00022  * Authors: Ben Skeggs
00023  */
00024 
00025 #ifdef HAVE_CONFIG_H
00026 #include <config.h>
00027 #endif
00028 
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <stdint.h>
00032 #include <stdbool.h>
00033 #include <assert.h>
00034 #include <errno.h>
00035 
00036 #include "libdrm_lists.h"
00037 
00038 #include "nouveau.h"
00039 #include "private.h"
00040 
00041 struct nouveau_bufref_priv {
00042        struct nouveau_bufref base;
00043        struct nouveau_bufref_priv *next;
00044        struct nouveau_bufctx *bufctx;
00045 };
00046 
00047 static inline struct nouveau_bufref_priv *
00048 nouveau_bufref(struct nouveau_bufref *bctx)
00049 {
00050        return (struct nouveau_bufref_priv *)bctx;
00051 }
00052 
00053 struct nouveau_bufbin_priv {
00054        struct nouveau_bufref_priv *list;
00055        int relocs;
00056 };
00057 
00058 struct nouveau_bufctx_priv {
00059        struct nouveau_bufctx base;
00060        struct nouveau_bufref_priv *free;
00061        int nr_bins;
00062        struct nouveau_bufbin_priv bins[];
00063 };
00064 
00065 static inline struct nouveau_bufctx_priv *
00066 nouveau_bufctx(struct nouveau_bufctx *bctx)
00067 {
00068        return (struct nouveau_bufctx_priv *)bctx;
00069 }
00070 
00071 int
00072 nouveau_bufctx_new(struct nouveau_client *client, int bins,
00073                  struct nouveau_bufctx **pbctx)
00074 {
00075        struct nouveau_bufctx_priv *priv;
00076 
00077        priv = calloc(1, sizeof(*priv) + sizeof(priv->bins[0]) * bins);
00078        if (priv) {
00079               DRMINITLISTHEAD(&priv->base.head);
00080               DRMINITLISTHEAD(&priv->base.pending);
00081               DRMINITLISTHEAD(&priv->base.current);
00082               priv->base.client = client;
00083               priv->nr_bins = bins;
00084               *pbctx = &priv->base;
00085               return 0;
00086        }
00087 
00088        return -ENOMEM;
00089 }
00090 
00091 void
00092 nouveau_bufctx_del(struct nouveau_bufctx **pbctx)
00093 {
00094        struct nouveau_bufctx_priv *pctx = nouveau_bufctx(*pbctx);
00095        struct nouveau_bufref_priv *pref;
00096        if (pctx) {
00097               while (pctx->nr_bins--)
00098                      nouveau_bufctx_reset(&pctx->base, pctx->nr_bins);
00099               while ((pref = pctx->free)) {
00100                      pctx->free = pref->next;
00101                      free(pref);
00102               }
00103               free(pctx);
00104               *pbctx = NULL;
00105        }
00106 }
00107 
00108 void
00109 nouveau_bufctx_reset(struct nouveau_bufctx *bctx, int bin)
00110 {
00111        struct nouveau_bufctx_priv *pctx = nouveau_bufctx(bctx);
00112        struct nouveau_bufbin_priv *pbin = &pctx->bins[bin];
00113        struct nouveau_bufref_priv *pref;
00114 
00115        while ((pref = pbin->list)) {
00116               DRMLISTDELINIT(&pref->base.thead);
00117               pbin->list = pref->next;
00118               pref->next = pctx->free;
00119               pctx->free = pref;
00120        }
00121 
00122        bctx->relocs -= pbin->relocs;
00123        pbin->relocs  = 0;
00124 }
00125 
00126 struct nouveau_bufref *
00127 nouveau_bufctx_refn(struct nouveau_bufctx *bctx, int bin,
00128                   struct nouveau_bo *bo, uint32_t flags)
00129 {
00130        struct nouveau_bufctx_priv *pctx = nouveau_bufctx(bctx);
00131        struct nouveau_bufbin_priv *pbin = &pctx->bins[bin];
00132        struct nouveau_bufref_priv *pref = pctx->free;
00133 
00134        if (!pref)
00135               pref = malloc(sizeof(*pref));
00136        else
00137               pctx->free = pref->next;
00138 
00139        if (pref) {
00140               pref->base.bo = bo;
00141               pref->base.flags = flags;
00142               pref->base.packet = 0;
00143 
00144               DRMLISTADDTAIL(&pref->base.thead, &bctx->pending);
00145               pref->bufctx = bctx;
00146               pref->next = pbin->list;
00147               pbin->list = pref;
00148        }
00149 
00150        return &pref->base;
00151 }
00152 
00153 struct nouveau_bufref *
00154 nouveau_bufctx_mthd(struct nouveau_bufctx *bctx, int bin, uint32_t packet,
00155                   struct nouveau_bo *bo, uint64_t data, uint32_t flags,
00156                   uint32_t vor, uint32_t tor)
00157 {
00158        struct nouveau_bufctx_priv *pctx = nouveau_bufctx(bctx);
00159        struct nouveau_bufbin_priv *pbin = &pctx->bins[bin];
00160        struct nouveau_bufref *bref = nouveau_bufctx_refn(bctx, bin, bo, flags);
00161        if (bref) {
00162               bref->packet = packet;
00163               bref->data = data;
00164               bref->vor = vor;
00165               bref->tor = tor;
00166               pbin->relocs++;
00167               bctx->relocs++;
00168        }
00169        return bref;
00170 }