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/object.h>
26 #include <core/handle.h>
27 #include <core/class.h>
29 #include <engine/dmaobj.h>
30 #include <engine/fifo.h>
33 nouveau_fifo_channel_create_(struct nouveau_object *parent,
34 struct nouveau_object *engine,
35 struct nouveau_oclass *oclass,
36 int bar, u32 addr, u32 size, u32 pushbuf,
37 u32 engmask, int len, void **ptr)
39 struct nouveau_device *device = nv_device(engine);
40 struct nouveau_fifo *priv = (void *)engine;
41 struct nouveau_fifo_chan *chan;
42 struct nouveau_dmaeng *dmaeng;
46 /* create base object class */
47 ret = nouveau_namedb_create_(parent, engine, oclass, 0, NULL,
53 /* validate dma object representing push buffer */
54 chan->pushdma = (void *)nouveau_handle_ref(parent, pushbuf);
58 dmaeng = (void *)chan->pushdma->base.engine;
59 switch (chan->pushdma->base.oclass->handle) {
60 case NV_DMA_FROM_MEMORY_CLASS:
61 case NV_DMA_IN_MEMORY_CLASS:
68 ret = dmaeng->bind(dmaeng, parent, chan->pushdma,
74 /* find a free fifo channel */
75 spin_lock_irqsave(&priv->lock, flags);
76 for (chan->chid = priv->min; chan->chid < priv->max; chan->chid++) {
77 if (!priv->channel[chan->chid]) {
78 priv->channel[chan->chid] = nv_object(chan);
82 spin_unlock_irqrestore(&priv->lock, flags);
84 if (chan->chid == priv->max) {
85 nv_error(priv, "no free channels\n");
89 /* map fifo control registers */
90 chan->user = ioremap(pci_resource_start(device->pdev, bar) + addr +
91 (chan->chid * size), size);
100 nouveau_fifo_channel_destroy(struct nouveau_fifo_chan *chan)
102 struct nouveau_fifo *priv = (void *)nv_object(chan)->engine;
107 spin_lock_irqsave(&priv->lock, flags);
108 priv->channel[chan->chid] = NULL;
109 spin_unlock_irqrestore(&priv->lock, flags);
111 nouveau_gpuobj_ref(NULL, &chan->pushgpu);
112 nouveau_object_ref(NULL, (struct nouveau_object **)&chan->pushdma);
113 nouveau_namedb_destroy(&chan->base);
117 _nouveau_fifo_channel_dtor(struct nouveau_object *object)
119 struct nouveau_fifo_chan *chan = (void *)object;
120 nouveau_fifo_channel_destroy(chan);
124 _nouveau_fifo_channel_rd32(struct nouveau_object *object, u64 addr)
126 struct nouveau_fifo_chan *chan = (void *)object;
127 return ioread32_native(chan->user + addr);
131 _nouveau_fifo_channel_wr32(struct nouveau_object *object, u64 addr, u32 data)
133 struct nouveau_fifo_chan *chan = (void *)object;
134 iowrite32_native(data, chan->user + addr);
138 nouveau_fifo_chid(struct nouveau_fifo *priv, struct nouveau_object *object)
140 int engidx = nv_hclass(priv) & 0xff;
142 while (object && object->parent) {
143 if ( nv_iclass(object->parent, NV_ENGCTX_CLASS) &&
144 (nv_hclass(object->parent) & 0xff) == engidx)
145 return nouveau_fifo_chan(object)->chid;
146 object = object->parent;
153 nouveau_fifo_destroy(struct nouveau_fifo *priv)
155 kfree(priv->channel);
156 nouveau_engine_destroy(&priv->base);
160 nouveau_fifo_create_(struct nouveau_object *parent,
161 struct nouveau_object *engine,
162 struct nouveau_oclass *oclass,
163 int min, int max, int length, void **pobject)
165 struct nouveau_fifo *priv;
168 ret = nouveau_engine_create_(parent, engine, oclass, true, "PFIFO",
169 "fifo", length, pobject);
176 priv->channel = kzalloc(sizeof(*priv->channel) * (max + 1), GFP_KERNEL);
180 priv->chid = nouveau_fifo_chid;
181 spin_lock_init(&priv->lock);