Home | History | Annotate | Line # | Download | only in nouveau
      1  1.1  riastrad /*	$NetBSD: nouveau_nv17_fence.c,v 1.3 2021/12/18 23:45:32 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2012 Red Hat Inc.
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      8  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      9  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     11  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     12  1.1  riastrad  *
     13  1.1  riastrad  * The above copyright notice and this permission notice shall be included in
     14  1.1  riastrad  * all copies or substantial portions of the Software.
     15  1.1  riastrad  *
     16  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  1.1  riastrad  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     23  1.1  riastrad  *
     24  1.1  riastrad  * Authors: Ben Skeggs <bskeggs (at) redhat.com>
     25  1.1  riastrad  */
     26  1.1  riastrad 
     27  1.1  riastrad #include <sys/cdefs.h>
     28  1.1  riastrad __KERNEL_RCSID(0, "$NetBSD: nouveau_nv17_fence.c,v 1.3 2021/12/18 23:45:32 riastradh Exp $");
     29  1.1  riastrad 
     30  1.2  riastrad #include <nvif/os.h>
     31  1.2  riastrad #include <nvif/class.h>
     32  1.3  riastrad #include <nvif/cl0002.h>
     33  1.1  riastrad 
     34  1.3  riastrad #include "nouveau_drv.h"
     35  1.1  riastrad #include "nouveau_dma.h"
     36  1.1  riastrad #include "nv10_fence.h"
     37  1.1  riastrad 
     38  1.1  riastrad int
     39  1.1  riastrad nv17_fence_sync(struct nouveau_fence *fence,
     40  1.1  riastrad 		struct nouveau_channel *prev, struct nouveau_channel *chan)
     41  1.1  riastrad {
     42  1.2  riastrad 	struct nouveau_cli *cli = (void *)prev->user.client;
     43  1.1  riastrad 	struct nv10_fence_priv *priv = chan->drm->fence;
     44  1.2  riastrad 	struct nv10_fence_chan *fctx = chan->fence;
     45  1.1  riastrad 	u32 value;
     46  1.1  riastrad 	int ret;
     47  1.1  riastrad 
     48  1.2  riastrad 	if (!mutex_trylock(&cli->mutex))
     49  1.1  riastrad 		return -EBUSY;
     50  1.1  riastrad 
     51  1.1  riastrad 	spin_lock(&priv->lock);
     52  1.1  riastrad 	value = priv->sequence;
     53  1.1  riastrad 	priv->sequence += 2;
     54  1.1  riastrad 	spin_unlock(&priv->lock);
     55  1.1  riastrad 
     56  1.1  riastrad 	ret = RING_SPACE(prev, 5);
     57  1.1  riastrad 	if (!ret) {
     58  1.1  riastrad 		BEGIN_NV04(prev, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 4);
     59  1.2  riastrad 		OUT_RING  (prev, fctx->sema.handle);
     60  1.1  riastrad 		OUT_RING  (prev, 0);
     61  1.1  riastrad 		OUT_RING  (prev, value + 0);
     62  1.1  riastrad 		OUT_RING  (prev, value + 1);
     63  1.1  riastrad 		FIRE_RING (prev);
     64  1.1  riastrad 	}
     65  1.1  riastrad 
     66  1.1  riastrad 	if (!ret && !(ret = RING_SPACE(chan, 5))) {
     67  1.1  riastrad 		BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 4);
     68  1.2  riastrad 		OUT_RING  (chan, fctx->sema.handle);
     69  1.1  riastrad 		OUT_RING  (chan, 0);
     70  1.1  riastrad 		OUT_RING  (chan, value + 1);
     71  1.1  riastrad 		OUT_RING  (chan, value + 2);
     72  1.1  riastrad 		FIRE_RING (chan);
     73  1.1  riastrad 	}
     74  1.1  riastrad 
     75  1.2  riastrad 	mutex_unlock(&cli->mutex);
     76  1.1  riastrad 	return 0;
     77  1.1  riastrad }
     78  1.1  riastrad 
     79  1.1  riastrad static int
     80  1.1  riastrad nv17_fence_context_new(struct nouveau_channel *chan)
     81  1.1  riastrad {
     82  1.1  riastrad 	struct nv10_fence_priv *priv = chan->drm->fence;
     83  1.1  riastrad 	struct nv10_fence_chan *fctx;
     84  1.3  riastrad 	struct ttm_mem_reg *reg = &priv->bo->bo.mem;
     85  1.3  riastrad 	u32 start = reg->start * PAGE_SIZE;
     86  1.3  riastrad 	u32 limit = start + reg->size - 1;
     87  1.1  riastrad 	int ret = 0;
     88  1.1  riastrad 
     89  1.1  riastrad 	fctx = chan->fence = kzalloc(sizeof(*fctx), GFP_KERNEL);
     90  1.1  riastrad 	if (!fctx)
     91  1.1  riastrad 		return -ENOMEM;
     92  1.1  riastrad 
     93  1.2  riastrad 	nouveau_fence_context_new(chan, &fctx->base);
     94  1.1  riastrad 	fctx->base.emit = nv10_fence_emit;
     95  1.1  riastrad 	fctx->base.read = nv10_fence_read;
     96  1.1  riastrad 	fctx->base.sync = nv17_fence_sync;
     97  1.1  riastrad 
     98  1.2  riastrad 	ret = nvif_object_init(&chan->user, NvSema, NV_DMA_FROM_MEMORY,
     99  1.2  riastrad 			       &(struct nv_dma_v0) {
    100  1.2  riastrad 					.target = NV_DMA_V0_TARGET_VRAM,
    101  1.2  riastrad 					.access = NV_DMA_V0_ACCESS_RDWR,
    102  1.1  riastrad 					.start = start,
    103  1.1  riastrad 					.limit = limit,
    104  1.2  riastrad 			       }, sizeof(struct nv_dma_v0),
    105  1.2  riastrad 			       &fctx->sema);
    106  1.1  riastrad 	if (ret)
    107  1.1  riastrad 		nv10_fence_context_del(chan);
    108  1.1  riastrad 	return ret;
    109  1.1  riastrad }
    110  1.1  riastrad 
    111  1.1  riastrad void
    112  1.1  riastrad nv17_fence_resume(struct nouveau_drm *drm)
    113  1.1  riastrad {
    114  1.1  riastrad 	struct nv10_fence_priv *priv = drm->fence;
    115  1.1  riastrad 
    116  1.1  riastrad 	nouveau_bo_wr32(priv->bo, 0, priv->sequence);
    117  1.1  riastrad }
    118  1.1  riastrad 
    119  1.1  riastrad int
    120  1.1  riastrad nv17_fence_create(struct nouveau_drm *drm)
    121  1.1  riastrad {
    122  1.1  riastrad 	struct nv10_fence_priv *priv;
    123  1.1  riastrad 	int ret = 0;
    124  1.1  riastrad 
    125  1.1  riastrad 	priv = drm->fence = kzalloc(sizeof(*priv), GFP_KERNEL);
    126  1.1  riastrad 	if (!priv)
    127  1.1  riastrad 		return -ENOMEM;
    128  1.1  riastrad 
    129  1.1  riastrad 	priv->base.dtor = nv10_fence_destroy;
    130  1.1  riastrad 	priv->base.resume = nv17_fence_resume;
    131  1.1  riastrad 	priv->base.context_new = nv17_fence_context_new;
    132  1.1  riastrad 	priv->base.context_del = nv10_fence_context_del;
    133  1.1  riastrad 	spin_lock_init(&priv->lock);
    134  1.1  riastrad 
    135  1.3  riastrad 	ret = nouveau_bo_new(&drm->client, 4096, 0x1000, TTM_PL_FLAG_VRAM,
    136  1.2  riastrad 			     0, 0x0000, NULL, NULL, &priv->bo);
    137  1.1  riastrad 	if (!ret) {
    138  1.2  riastrad 		ret = nouveau_bo_pin(priv->bo, TTM_PL_FLAG_VRAM, false);
    139  1.1  riastrad 		if (!ret) {
    140  1.1  riastrad 			ret = nouveau_bo_map(priv->bo);
    141  1.1  riastrad 			if (ret)
    142  1.1  riastrad 				nouveau_bo_unpin(priv->bo);
    143  1.1  riastrad 		}
    144  1.1  riastrad 		if (ret)
    145  1.1  riastrad 			nouveau_bo_ref(NULL, &priv->bo);
    146  1.1  riastrad 	}
    147  1.1  riastrad 
    148  1.1  riastrad 	if (ret) {
    149  1.1  riastrad 		nv10_fence_destroy(drm);
    150  1.1  riastrad 		return ret;
    151  1.1  riastrad 	}
    152  1.1  riastrad 
    153  1.1  riastrad 	nouveau_bo_wr32(priv->bo, 0x000, 0x00000000);
    154  1.1  riastrad 	return ret;
    155  1.1  riastrad }
    156