Home | History | Annotate | Download | only in nouveau
      1 /*
      2  * Copyright 2012 Red Hat Inc.
      3  *
      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:
     10  *
     11  * The above copyright notice and this permission notice shall be included in
     12  * all copies or substantial portions of the Software.
     13  *
     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.
     21  *
     22  * Authors: Ben Skeggs
     23  */
     24 
     25 #ifdef HAVE_CONFIG_H
     26 #include <config.h>
     27 #endif
     28 
     29 #include <stdio.h>
     30 #include <stdlib.h>
     31 #include <stdint.h>
     32 #include <stdbool.h>
     33 #include <assert.h>
     34 #include <errno.h>
     35 
     36 #include "libdrm_lists.h"
     37 
     38 #include "nouveau.h"
     39 #include "private.h"
     40 
     41 struct nouveau_bufref_priv {
     42 	struct nouveau_bufref base;
     43 	struct nouveau_bufref_priv *next;
     44 	struct nouveau_bufctx *bufctx;
     45 };
     46 
     47 static inline struct nouveau_bufref_priv *
     48 nouveau_bufref(struct nouveau_bufref *bctx)
     49 {
     50 	return (struct nouveau_bufref_priv *)bctx;
     51 }
     52 
     53 struct nouveau_bufbin_priv {
     54 	struct nouveau_bufref_priv *list;
     55 	int relocs;
     56 };
     57 
     58 struct nouveau_bufctx_priv {
     59 	struct nouveau_bufctx base;
     60 	struct nouveau_bufref_priv *free;
     61 	int nr_bins;
     62 	struct nouveau_bufbin_priv bins[];
     63 };
     64 
     65 static inline struct nouveau_bufctx_priv *
     66 nouveau_bufctx(struct nouveau_bufctx *bctx)
     67 {
     68 	return (struct nouveau_bufctx_priv *)bctx;
     69 }
     70 
     71 drm_public int
     72 nouveau_bufctx_new(struct nouveau_client *client, int bins,
     73 		   struct nouveau_bufctx **pbctx)
     74 {
     75 	struct nouveau_bufctx_priv *priv;
     76 
     77 	priv = calloc(1, sizeof(*priv) + sizeof(priv->bins[0]) * bins);
     78 	if (priv) {
     79 		DRMINITLISTHEAD(&priv->base.head);
     80 		DRMINITLISTHEAD(&priv->base.pending);
     81 		DRMINITLISTHEAD(&priv->base.current);
     82 		priv->base.client = client;
     83 		priv->nr_bins = bins;
     84 		*pbctx = &priv->base;
     85 		return 0;
     86 	}
     87 
     88 	return -ENOMEM;
     89 }
     90 
     91 drm_public void
     92 nouveau_bufctx_del(struct nouveau_bufctx **pbctx)
     93 {
     94 	struct nouveau_bufctx_priv *pctx = nouveau_bufctx(*pbctx);
     95 	struct nouveau_bufref_priv *pref;
     96 	if (pctx) {
     97 		while (pctx->nr_bins--)
     98 			nouveau_bufctx_reset(&pctx->base, pctx->nr_bins);
     99 		while ((pref = pctx->free)) {
    100 			pctx->free = pref->next;
    101 			free(pref);
    102 		}
    103 		free(pctx);
    104 		*pbctx = NULL;
    105 	}
    106 }
    107 
    108 drm_public void
    109 nouveau_bufctx_reset(struct nouveau_bufctx *bctx, int bin)
    110 {
    111 	struct nouveau_bufctx_priv *pctx = nouveau_bufctx(bctx);
    112 	struct nouveau_bufbin_priv *pbin = &pctx->bins[bin];
    113 	struct nouveau_bufref_priv *pref;
    114 
    115 	while ((pref = pbin->list)) {
    116 		DRMLISTDELINIT(&pref->base.thead);
    117 		pbin->list = pref->next;
    118 		pref->next = pctx->free;
    119 		pctx->free = pref;
    120 	}
    121 
    122 	bctx->relocs -= pbin->relocs;
    123 	pbin->relocs  = 0;
    124 }
    125 
    126 drm_public struct nouveau_bufref *
    127 nouveau_bufctx_refn(struct nouveau_bufctx *bctx, int bin,
    128 		    struct nouveau_bo *bo, uint32_t flags)
    129 {
    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;
    133 
    134 	if (!pref)
    135 		pref = malloc(sizeof(*pref));
    136 	else
    137 		pctx->free = pref->next;
    138 
    139 	if (pref) {
    140 		pref->base.bo = bo;
    141 		pref->base.flags = flags;
    142 		pref->base.packet = 0;
    143 
    144 		DRMLISTADDTAIL(&pref->base.thead, &bctx->pending);
    145 		pref->bufctx = bctx;
    146 		pref->next = pbin->list;
    147 		pbin->list = pref;
    148 	}
    149 
    150 	return &pref->base;
    151 }
    152 
    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)
    157 {
    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);
    161 	if (bref) {
    162 		bref->packet = packet;
    163 		bref->data = data;
    164 		bref->vor = vor;
    165 		bref->tor = tor;
    166 		pbin->relocs++;
    167 		bctx->relocs++;
    168 	}
    169 	return bref;
    170 }
    171