]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/nouveau/core/engine/fifo/base.c
ca4050c6ea5fa278c90f7dffd728dd908fad799a
[karo-tx-linux.git] / drivers / gpu / drm / nouveau / core / engine / fifo / base.c
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/handle.h>
27 #include <core/class.h>
28
29 #include <engine/dmaobj.h>
30 #include <engine/fifo.h>
31
32 int
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)
38 {
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;
43         unsigned long flags;
44         int ret;
45
46         /* create base object class */
47         ret = nouveau_namedb_create_(parent, engine, oclass, 0, NULL,
48                                      engmask, len, ptr);
49         chan = *ptr;
50         if (ret)
51                 return ret;
52
53         /* validate dma object representing push buffer */
54         chan->pushdma = (void *)nouveau_handle_ref(parent, pushbuf);
55         if (!chan->pushdma)
56                 return -ENOENT;
57
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:
62                 break;
63         default:
64                 return -EINVAL;
65         }
66
67         if (dmaeng->bind) {
68                 ret = dmaeng->bind(dmaeng, parent, chan->pushdma,
69                                   &chan->pushgpu);
70                 if (ret)
71                         return ret;
72         }
73
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);
79                         break;
80                 }
81         }
82         spin_unlock_irqrestore(&priv->lock, flags);
83
84         if (chan->chid == priv->max) {
85                 nv_error(priv, "no free channels\n");
86                 return -ENOSPC;
87         }
88
89         /* map fifo control registers */
90         chan->user = ioremap(pci_resource_start(device->pdev, bar) + addr +
91                              (chan->chid * size), size);
92         if (!chan->user)
93                 return -EFAULT;
94
95         chan->size = size;
96         return 0;
97 }
98
99 void
100 nouveau_fifo_channel_destroy(struct nouveau_fifo_chan *chan)
101 {
102         struct nouveau_fifo *priv = (void *)nv_object(chan)->engine;
103         unsigned long flags;
104
105         iounmap(chan->user);
106
107         spin_lock_irqsave(&priv->lock, flags);
108         priv->channel[chan->chid] = NULL;
109         spin_unlock_irqrestore(&priv->lock, flags);
110
111         nouveau_gpuobj_ref(NULL, &chan->pushgpu);
112         nouveau_object_ref(NULL, (struct nouveau_object **)&chan->pushdma);
113         nouveau_namedb_destroy(&chan->base);
114 }
115
116 void
117 _nouveau_fifo_channel_dtor(struct nouveau_object *object)
118 {
119         struct nouveau_fifo_chan *chan = (void *)object;
120         nouveau_fifo_channel_destroy(chan);
121 }
122
123 u32
124 _nouveau_fifo_channel_rd32(struct nouveau_object *object, u64 addr)
125 {
126         struct nouveau_fifo_chan *chan = (void *)object;
127         return ioread32_native(chan->user + addr);
128 }
129
130 void
131 _nouveau_fifo_channel_wr32(struct nouveau_object *object, u64 addr, u32 data)
132 {
133         struct nouveau_fifo_chan *chan = (void *)object;
134         iowrite32_native(data, chan->user + addr);
135 }
136
137 static int
138 nouveau_fifo_chid(struct nouveau_fifo *priv, struct nouveau_object *object)
139 {
140         int engidx = nv_hclass(priv) & 0xff;
141
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;
147         }
148
149         return -1;
150 }
151
152 void
153 nouveau_fifo_destroy(struct nouveau_fifo *priv)
154 {
155         kfree(priv->channel);
156         nouveau_engine_destroy(&priv->base);
157 }
158
159 int
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)
164 {
165         struct nouveau_fifo *priv;
166         int ret;
167
168         ret = nouveau_engine_create_(parent, engine, oclass, true, "PFIFO",
169                                      "fifo", length, pobject);
170         priv = *pobject;
171         if (ret)
172                 return ret;
173
174         priv->min = min;
175         priv->max = max;
176         priv->channel = kzalloc(sizeof(*priv->channel) * (max + 1), GFP_KERNEL);
177         if (!priv->channel)
178                 return -ENOMEM;
179
180         priv->chid = nouveau_fifo_chid;
181         spin_lock_init(&priv->lock);
182         return 0;
183 }