2 * Copyright 2012 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
25 #include <core/client.h>
26 #include <core/object.h>
27 #include <core/handle.h>
28 #include <core/event.h>
29 #include <nvif/unpack.h>
30 #include <nvif/class.h>
31 #include <nvif/event.h>
33 #include <engine/dmaobj.h>
34 #include <engine/fifo.h>
37 nouveau_fifo_event_ctor(void *data, u32 size, struct nvkm_notify *notify)
48 static const struct nvkm_event_func
49 nouveau_fifo_event_func = {
50 .ctor = nouveau_fifo_event_ctor,
54 nouveau_fifo_channel_create_(struct nouveau_object *parent,
55 struct nouveau_object *engine,
56 struct nouveau_oclass *oclass,
57 int bar, u32 addr, u32 size, u32 pushbuf,
58 u64 engmask, int len, void **ptr)
60 struct nouveau_device *device = nv_device(engine);
61 struct nouveau_fifo *priv = (void *)engine;
62 struct nouveau_fifo_chan *chan;
63 struct nouveau_dmaeng *dmaeng;
67 /* create base object class */
68 ret = nouveau_namedb_create_(parent, engine, oclass, 0, NULL,
74 /* validate dma object representing push buffer */
75 chan->pushdma = (void *)nouveau_handle_ref(parent, pushbuf);
79 dmaeng = (void *)chan->pushdma->base.engine;
80 switch (chan->pushdma->base.oclass->handle) {
81 case NV_DMA_FROM_MEMORY:
82 case NV_DMA_IN_MEMORY:
88 ret = dmaeng->bind(chan->pushdma, parent, &chan->pushgpu);
92 /* find a free fifo channel */
93 spin_lock_irqsave(&priv->lock, flags);
94 for (chan->chid = priv->min; chan->chid < priv->max; chan->chid++) {
95 if (!priv->channel[chan->chid]) {
96 priv->channel[chan->chid] = nv_object(chan);
100 spin_unlock_irqrestore(&priv->lock, flags);
102 if (chan->chid == priv->max) {
103 nv_error(priv, "no free channels\n");
107 chan->addr = nv_device_resource_start(device, bar) +
108 addr + size * chan->chid;
110 nvkm_event_send(&priv->cevent, 1, 0, NULL, 0);
115 nouveau_fifo_channel_destroy(struct nouveau_fifo_chan *chan)
117 struct nouveau_fifo *priv = (void *)nv_object(chan)->engine;
123 spin_lock_irqsave(&priv->lock, flags);
124 priv->channel[chan->chid] = NULL;
125 spin_unlock_irqrestore(&priv->lock, flags);
127 nouveau_gpuobj_ref(NULL, &chan->pushgpu);
128 nouveau_object_ref(NULL, (struct nouveau_object **)&chan->pushdma);
129 nouveau_namedb_destroy(&chan->base);
133 _nouveau_fifo_channel_dtor(struct nouveau_object *object)
135 struct nouveau_fifo_chan *chan = (void *)object;
136 nouveau_fifo_channel_destroy(chan);
140 _nouveau_fifo_channel_map(struct nouveau_object *object, u64 *addr, u32 *size)
142 struct nouveau_fifo_chan *chan = (void *)object;
149 _nouveau_fifo_channel_rd32(struct nouveau_object *object, u64 addr)
151 struct nouveau_fifo_chan *chan = (void *)object;
152 if (unlikely(!chan->user)) {
153 chan->user = ioremap(chan->addr, chan->size);
154 if (WARN_ON_ONCE(chan->user == NULL))
157 return ioread32_native(chan->user + addr);
161 _nouveau_fifo_channel_wr32(struct nouveau_object *object, u64 addr, u32 data)
163 struct nouveau_fifo_chan *chan = (void *)object;
164 if (unlikely(!chan->user)) {
165 chan->user = ioremap(chan->addr, chan->size);
166 if (WARN_ON_ONCE(chan->user == NULL))
169 iowrite32_native(data, chan->user + addr);
173 nouveau_fifo_uevent_ctor(void *data, u32 size, struct nvkm_notify *notify)
176 struct nvif_notify_uevent_req none;
180 if (nvif_unvers(req->none)) {
181 notify->size = sizeof(struct nvif_notify_uevent_rep);
190 nouveau_fifo_uevent(struct nouveau_fifo *fifo)
192 struct nvif_notify_uevent_rep rep = {
194 nvkm_event_send(&fifo->uevent, 1, 0, &rep, sizeof(rep));
198 _nouveau_fifo_channel_ntfy(struct nouveau_object *object, u32 type,
199 struct nvkm_event **event)
201 struct nouveau_fifo *fifo = (void *)object->engine;
203 case G82_CHANNEL_DMA_V0_NTFY_UEVENT:
204 if (nv_mclass(object) >= G82_CHANNEL_DMA) {
205 *event = &fifo->uevent;
216 nouveau_fifo_chid(struct nouveau_fifo *priv, struct nouveau_object *object)
218 int engidx = nv_hclass(priv) & 0xff;
220 while (object && object->parent) {
221 if ( nv_iclass(object->parent, NV_ENGCTX_CLASS) &&
222 (nv_hclass(object->parent) & 0xff) == engidx)
223 return nouveau_fifo_chan(object)->chid;
224 object = object->parent;
231 nouveau_client_name_for_fifo_chid(struct nouveau_fifo *fifo, u32 chid)
233 struct nouveau_fifo_chan *chan = NULL;
236 spin_lock_irqsave(&fifo->lock, flags);
237 if (chid >= fifo->min && chid <= fifo->max)
238 chan = (void *)fifo->channel[chid];
239 spin_unlock_irqrestore(&fifo->lock, flags);
241 return nouveau_client_name(chan);
245 nouveau_fifo_destroy(struct nouveau_fifo *priv)
247 kfree(priv->channel);
248 nvkm_event_fini(&priv->uevent);
249 nvkm_event_fini(&priv->cevent);
250 nouveau_engine_destroy(&priv->base);
254 nouveau_fifo_create_(struct nouveau_object *parent,
255 struct nouveau_object *engine,
256 struct nouveau_oclass *oclass,
257 int min, int max, int length, void **pobject)
259 struct nouveau_fifo *priv;
262 ret = nouveau_engine_create_(parent, engine, oclass, true, "PFIFO",
263 "fifo", length, pobject);
270 priv->channel = kzalloc(sizeof(*priv->channel) * (max + 1), GFP_KERNEL);
274 ret = nvkm_event_init(&nouveau_fifo_event_func, 1, 1, &priv->cevent);
278 priv->chid = nouveau_fifo_chid;
279 spin_lock_init(&priv->lock);