1/* $NetBSD: nouveau_engine_software_nv50.c,v 1.1.1.1 2014/08/06 12:36:27 riastradh Exp $ */
2
3/*
4 * Copyright 2012 Red Hat Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Ben Skeggs
25 */
26
27#include <sys/cdefs.h>
28__KERNEL_RCSID(0, "$NetBSD: nouveau_engine_software_nv50.c,v 1.1.1.1 2014/08/06 12:36:27 riastradh Exp $");
29
30#include <core/os.h>
31#include <core/class.h>
32#include <core/engctx.h>
33#include <core/namedb.h>
34#include <core/handle.h>
35#include <core/gpuobj.h>
36#include <core/event.h>
37
38#include <subdev/bar.h>
39
40#include <engine/disp.h>
41
42#include "nv50.h"
43
44/*******************************************************************************
45 * software object classes
46 ******************************************************************************/
47
48static int
49nv50_software_mthd_dma_vblsem(struct nouveau_object *object, u32 mthd,
50 void *args, u32 size)
51{
52 struct nv50_software_chan *chan = (void *)nv_engctx(object->parent);
53 struct nouveau_fifo_chan *fifo = (void *)nv_object(chan)->parent;
54 struct nouveau_handle *handle;
55 int ret = -EINVAL;
56
57 handle = nouveau_namedb_get(nv_namedb(fifo), *(u32 *)args);
58 if (!handle)
59 return -ENOENT;
60
61 if (nv_iclass(handle->object, NV_GPUOBJ_CLASS)) {
62 struct nouveau_gpuobj *gpuobj = nv_gpuobj(handle->object);
63 chan->vblank.ctxdma = gpuobj->node->offset >> 4;
64 ret = 0;
65 }
66 nouveau_namedb_put(handle);
67 return ret;
68}
69
70static int
71nv50_software_mthd_vblsem_offset(struct nouveau_object *object, u32 mthd,
72 void *args, u32 size)
73{
74 struct nv50_software_chan *chan = (void *)nv_engctx(object->parent);
75 chan->vblank.offset = *(u32 *)args;
76 return 0;
77}
78
79int
80nv50_software_mthd_vblsem_value(struct nouveau_object *object, u32 mthd,
81 void *args, u32 size)
82{
83 struct nv50_software_chan *chan = (void *)nv_engctx(object->parent);
84 chan->vblank.value = *(u32 *)args;
85 return 0;
86}
87
88int
89nv50_software_mthd_vblsem_release(struct nouveau_object *object, u32 mthd,
90 void *args, u32 size)
91{
92 struct nv50_software_chan *chan = (void *)nv_engctx(object->parent);
93 u32 head = *(u32 *)args;
94 if (head >= chan->vblank.nr_event)
95 return -EINVAL;
96
97 nouveau_event_get(chan->vblank.event[head]);
98 return 0;
99}
100
101int
102nv50_software_mthd_flip(struct nouveau_object *object, u32 mthd,
103 void *args, u32 size)
104{
105 struct nv50_software_chan *chan = (void *)nv_engctx(object->parent);
106 if (chan->base.flip)
107 return chan->base.flip(chan->base.flip_data);
108 return -EINVAL;
109}
110
111static struct nouveau_omthds
112nv50_software_omthds[] = {
113 { 0x018c, 0x018c, nv50_software_mthd_dma_vblsem },
114 { 0x0400, 0x0400, nv50_software_mthd_vblsem_offset },
115 { 0x0404, 0x0404, nv50_software_mthd_vblsem_value },
116 { 0x0408, 0x0408, nv50_software_mthd_vblsem_release },
117 { 0x0500, 0x0500, nv50_software_mthd_flip },
118 {}
119};
120
121static struct nouveau_oclass
122nv50_software_sclass[] = {
123 { 0x506e, &nouveau_object_ofuncs, nv50_software_omthds },
124 {}
125};
126
127/*******************************************************************************
128 * software context
129 ******************************************************************************/
130
131static int
132nv50_software_vblsem_release(void *data, int head)
133{
134 struct nv50_software_chan *chan = data;
135 struct nv50_software_priv *priv = (void *)nv_object(chan)->engine;
136 struct nouveau_bar *bar = nouveau_bar(priv);
137
138 nv_wr32(priv, 0x001704, chan->vblank.channel);
139 nv_wr32(priv, 0x001710, 0x80000000 | chan->vblank.ctxdma);
140 bar->flush(bar);
141
142 if (nv_device(priv)->chipset == 0x50) {
143 nv_wr32(priv, 0x001570, chan->vblank.offset);
144 nv_wr32(priv, 0x001574, chan->vblank.value);
145 } else {
146 nv_wr32(priv, 0x060010, chan->vblank.offset);
147 nv_wr32(priv, 0x060014, chan->vblank.value);
148 }
149
150 return NVKM_EVENT_DROP;
151}
152
153void
154nv50_software_context_dtor(struct nouveau_object *object)
155{
156 struct nv50_software_chan *chan = (void *)object;
157 int i;
158
159 if (chan->vblank.event) {
160 for (i = 0; i < chan->vblank.nr_event; i++)
161 nouveau_event_ref(NULL, &chan->vblank.event[i]);
162 kfree(chan->vblank.event);
163 }
164
165 nouveau_software_context_destroy(&chan->base);
166}
167
168int
169nv50_software_context_ctor(struct nouveau_object *parent,
170 struct nouveau_object *engine,
171 struct nouveau_oclass *oclass, void *data, u32 size,
172 struct nouveau_object **pobject)
173{
174 struct nouveau_disp *pdisp = nouveau_disp(parent);
175 struct nv50_software_cclass *pclass = (void *)oclass;
176 struct nv50_software_chan *chan;
177 int ret, i;
178
179 ret = nouveau_software_context_create(parent, engine, oclass, &chan);
180 *pobject = nv_object(chan);
181 if (ret)
182 return ret;
183
184 chan->vblank.nr_event = pdisp ? pdisp->vblank->index_nr : 0;
185 chan->vblank.event = kzalloc(chan->vblank.nr_event *
186 sizeof(*chan->vblank.event), GFP_KERNEL);
187 if (!chan->vblank.event)
188 return -ENOMEM;
189
190 for (i = 0; i < chan->vblank.nr_event; i++) {
191 ret = nouveau_event_new(pdisp->vblank, i, pclass->vblank,
192 chan, &chan->vblank.event[i]);
193 if (ret)
194 return ret;
195 }
196
197 chan->vblank.channel = nv_gpuobj(parent->parent)->addr >> 12;
198 return 0;
199}
200
201static struct nv50_software_cclass
202nv50_software_cclass = {
203 .base.handle = NV_ENGCTX(SW, 0x50),
204 .base.ofuncs = &(struct nouveau_ofuncs) {
205 .ctor = nv50_software_context_ctor,
206 .dtor = _nouveau_software_context_dtor,
207 .init = _nouveau_software_context_init,
208 .fini = _nouveau_software_context_fini,
209 },
210 .vblank = nv50_software_vblsem_release,
211};
212
213/*******************************************************************************
214 * software engine/subdev functions
215 ******************************************************************************/
216
217int
218nv50_software_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
219 struct nouveau_oclass *oclass, void *data, u32 size,
220 struct nouveau_object **pobject)
221{
222 struct nv50_software_oclass *pclass = (void *)oclass;
223 struct nv50_software_priv *priv;
224 int ret;
225
226 ret = nouveau_software_create(parent, engine, oclass, &priv);
227 *pobject = nv_object(priv);
228 if (ret)
229 return ret;
230
231 nv_engine(priv)->cclass = pclass->cclass;
232 nv_engine(priv)->sclass = pclass->sclass;
233 nv_subdev(priv)->intr = nv04_software_intr;
234 return 0;
235}
236
237struct nouveau_oclass *
238nv50_software_oclass = &(struct nv50_software_oclass) {
239 .base.handle = NV_ENGINE(SW, 0x50),
240 .base.ofuncs = &(struct nouveau_ofuncs) {
241 .ctor = nv50_software_ctor,
242 .dtor = _nouveau_software_dtor,
243 .init = _nouveau_software_init,
244 .fini = _nouveau_software_fini,
245 },
246 .cclass = &nv50_software_cclass.base,
247 .sclass = nv50_software_sclass,
248}.base;
249