nvc0_fence.c revision e18c080fb8695d038f69c26c248f5ecbd9e8aa77
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#include <core/object.h>
26#include <core/client.h>
27#include <core/class.h>
28
29#include <engine/fifo.h>
30
31#include "nouveau_drm.h"
32#include "nouveau_dma.h"
33#include "nouveau_fence.h"
34
35#include "nv50_display.h"
36
37struct nvc0_fence_priv {
38	struct nouveau_fence_priv base;
39	struct nouveau_bo *bo;
40	u32 *suspend;
41};
42
43struct nvc0_fence_chan {
44	struct nouveau_fence_chan base;
45	struct nouveau_vma vma;
46	struct nouveau_vma dispc_vma[4];
47};
48
49u64
50nvc0_fence_crtc(struct nouveau_channel *chan, int crtc)
51{
52	struct nvc0_fence_chan *fctx = chan->fence;
53	return fctx->dispc_vma[crtc].offset;
54}
55
56static int
57nvc0_fence_emit(struct nouveau_fence *fence)
58{
59	struct nouveau_channel *chan = fence->channel;
60	struct nvc0_fence_chan *fctx = chan->fence;
61	struct nouveau_fifo_chan *fifo = (void *)chan->object;
62	u64 addr = fctx->vma.offset + fifo->chid * 16;
63	int ret;
64
65	ret = RING_SPACE(chan, 6);
66	if (ret == 0) {
67		BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 5);
68		OUT_RING  (chan, upper_32_bits(addr));
69		OUT_RING  (chan, lower_32_bits(addr));
70		OUT_RING  (chan, fence->sequence);
71		OUT_RING  (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG);
72		OUT_RING  (chan, 0x00000000);
73		FIRE_RING (chan);
74	}
75
76	return ret;
77}
78
79static int
80nvc0_fence_sync(struct nouveau_fence *fence,
81		struct nouveau_channel *prev, struct nouveau_channel *chan)
82{
83	struct nvc0_fence_chan *fctx = chan->fence;
84	struct nouveau_fifo_chan *fifo = (void *)prev->object;
85	u64 addr = fctx->vma.offset + fifo->chid * 16;
86	int ret;
87
88	ret = RING_SPACE(chan, 5);
89	if (ret == 0) {
90		BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
91		OUT_RING  (chan, upper_32_bits(addr));
92		OUT_RING  (chan, lower_32_bits(addr));
93		OUT_RING  (chan, fence->sequence);
94		OUT_RING  (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_GEQUAL |
95				 NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
96		FIRE_RING (chan);
97	}
98
99	return ret;
100}
101
102static u32
103nvc0_fence_read(struct nouveau_channel *chan)
104{
105	struct nouveau_fifo_chan *fifo = (void *)chan->object;
106	struct nvc0_fence_priv *priv = chan->drm->fence;
107	return nouveau_bo_rd32(priv->bo, fifo->chid * 16/4);
108}
109
110static void
111nvc0_fence_context_del(struct nouveau_channel *chan)
112{
113	struct drm_device *dev = chan->drm->dev;
114	struct nvc0_fence_priv *priv = chan->drm->fence;
115	struct nvc0_fence_chan *fctx = chan->fence;
116	int i;
117
118	for (i = 0; i < dev->mode_config.num_crtc; i++) {
119		struct nouveau_bo *bo = nv50_display_crtc_sema(dev, i);
120		nouveau_bo_vma_del(bo, &fctx->dispc_vma[i]);
121	}
122
123	nouveau_bo_vma_del(priv->bo, &fctx->vma);
124	nouveau_fence_context_del(&fctx->base);
125	chan->fence = NULL;
126	kfree(fctx);
127}
128
129static int
130nvc0_fence_context_new(struct nouveau_channel *chan)
131{
132	struct nouveau_fifo_chan *fifo = (void *)chan->object;
133	struct nouveau_client *client = nouveau_client(fifo);
134	struct nvc0_fence_priv *priv = chan->drm->fence;
135	struct nvc0_fence_chan *fctx;
136	int ret, i;
137
138	fctx = chan->fence = kzalloc(sizeof(*fctx), GFP_KERNEL);
139	if (!fctx)
140		return -ENOMEM;
141
142	nouveau_fence_context_new(&fctx->base);
143
144	ret = nouveau_bo_vma_add(priv->bo, client->vm, &fctx->vma);
145	if (ret)
146		nvc0_fence_context_del(chan);
147
148	/* map display semaphore buffers into channel's vm */
149	for (i = 0; !ret && i < chan->drm->dev->mode_config.num_crtc; i++) {
150		struct nouveau_bo *bo = nv50_display_crtc_sema(chan->drm->dev, i);
151		ret = nouveau_bo_vma_add(bo, client->vm, &fctx->dispc_vma[i]);
152	}
153
154	nouveau_bo_wr32(priv->bo, fifo->chid * 16/4, 0x00000000);
155	return ret;
156}
157
158static bool
159nvc0_fence_suspend(struct nouveau_drm *drm)
160{
161	struct nouveau_fifo *pfifo = nouveau_fifo(drm->device);
162	struct nvc0_fence_priv *priv = drm->fence;
163	int i;
164
165	priv->suspend = vmalloc((pfifo->max + 1) * sizeof(u32));
166	if (priv->suspend) {
167		for (i = 0; i <= pfifo->max; i++)
168			priv->suspend[i] = nouveau_bo_rd32(priv->bo, i);
169	}
170
171	return priv->suspend != NULL;
172}
173
174static void
175nvc0_fence_resume(struct nouveau_drm *drm)
176{
177	struct nouveau_fifo *pfifo = nouveau_fifo(drm->device);
178	struct nvc0_fence_priv *priv = drm->fence;
179	int i;
180
181	if (priv->suspend) {
182		for (i = 0; i <= pfifo->max; i++)
183			nouveau_bo_wr32(priv->bo, i, priv->suspend[i]);
184		vfree(priv->suspend);
185		priv->suspend = NULL;
186	}
187}
188
189static void
190nvc0_fence_destroy(struct nouveau_drm *drm)
191{
192	struct nvc0_fence_priv *priv = drm->fence;
193	nouveau_bo_unmap(priv->bo);
194	if (priv->bo)
195		nouveau_bo_unpin(priv->bo);
196	nouveau_bo_ref(NULL, &priv->bo);
197	drm->fence = NULL;
198	kfree(priv);
199}
200
201int
202nvc0_fence_create(struct nouveau_drm *drm)
203{
204	struct nouveau_fifo *pfifo = nouveau_fifo(drm->device);
205	struct nvc0_fence_priv *priv;
206	int ret;
207
208	priv = drm->fence = kzalloc(sizeof(*priv), GFP_KERNEL);
209	if (!priv)
210		return -ENOMEM;
211
212	priv->base.dtor = nvc0_fence_destroy;
213	priv->base.suspend = nvc0_fence_suspend;
214	priv->base.resume = nvc0_fence_resume;
215	priv->base.context_new = nvc0_fence_context_new;
216	priv->base.context_del = nvc0_fence_context_del;
217	priv->base.emit = nvc0_fence_emit;
218	priv->base.sync = nvc0_fence_sync;
219	priv->base.read = nvc0_fence_read;
220
221	init_waitqueue_head(&priv->base.waiting);
222	priv->base.uevent = true;
223
224	ret = nouveau_bo_new(drm->dev, 16 * (pfifo->max + 1), 0,
225			     TTM_PL_FLAG_VRAM, 0, 0, NULL, &priv->bo);
226	if (ret == 0) {
227		ret = nouveau_bo_pin(priv->bo, TTM_PL_FLAG_VRAM);
228		if (ret == 0) {
229			ret = nouveau_bo_map(priv->bo);
230			if (ret)
231				nouveau_bo_unpin(priv->bo);
232		}
233		if (ret)
234			nouveau_bo_ref(NULL, &priv->bo);
235	}
236
237	if (ret)
238		nvc0_fence_destroy(drm);
239	return ret;
240}
241