blob: c3acf5b70d9ec8d39db1f0066b122701ce8ed4cd [file] [log] [blame]
Ben Skeggs3863c9b2012-07-14 19:09:17 +10001/*
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/gpuobj.h>
26
27#include <subdev/timer.h>
28#include <subdev/bar.h>
29#include <subdev/fb.h>
30#include <subdev/vm.h>
31
32struct nv50_bar_priv {
33 struct nouveau_bar base;
34 spinlock_t lock;
35 struct nouveau_gpuobj *mem;
36 struct nouveau_gpuobj *pad;
37 struct nouveau_gpuobj *pgd;
38 struct nouveau_vm *bar1_vm;
39 struct nouveau_gpuobj *bar1;
40 struct nouveau_vm *bar3_vm;
41 struct nouveau_gpuobj *bar3;
42};
43
44static int
45nv50_bar_kmap(struct nouveau_bar *bar, struct nouveau_mem *mem,
46 u32 flags, struct nouveau_vma *vma)
47{
48 struct nv50_bar_priv *priv = (void *)bar;
49 int ret;
50
51 ret = nouveau_vm_get(priv->bar3_vm, mem->size << 12, 12, flags, vma);
52 if (ret)
53 return ret;
54
55 nouveau_vm_map(vma, mem);
56 nv50_vm_flush_engine(nv_subdev(bar), 6);
57 return 0;
58}
59
60static int
61nv50_bar_umap(struct nouveau_bar *bar, struct nouveau_mem *mem,
62 u32 flags, struct nouveau_vma *vma)
63{
64 struct nv50_bar_priv *priv = (void *)bar;
65 int ret;
66
67 ret = nouveau_vm_get(priv->bar1_vm, mem->size << 12, 12, flags, vma);
68 if (ret)
69 return ret;
70
71 nouveau_vm_map(vma, mem);
72 nv50_vm_flush_engine(nv_subdev(bar), 6);
73 return 0;
74}
75
76static void
77nv50_bar_unmap(struct nouveau_bar *bar, struct nouveau_vma *vma)
78{
79 nouveau_vm_unmap(vma);
80 nv50_vm_flush_engine(nv_subdev(bar), 6);
81 nouveau_vm_put(vma);
82}
83
84static void
85nv50_bar_flush(struct nouveau_bar *bar)
86{
87 struct nv50_bar_priv *priv = (void *)bar;
88 unsigned long flags;
89 spin_lock_irqsave(&priv->lock, flags);
90 nv_wr32(priv, 0x00330c, 0x00000001);
91 if (!nv_wait(priv, 0x00330c, 0x00000002, 0x00000000))
92 nv_warn(priv, "flush timeout\n");
93 spin_unlock_irqrestore(&priv->lock, flags);
94}
95
96void
97nv84_bar_flush(struct nouveau_bar *bar)
98{
99 struct nv50_bar_priv *priv = (void *)bar;
100 unsigned long flags;
101 spin_lock_irqsave(&priv->lock, flags);
102 nv_wr32(bar, 0x070000, 0x00000001);
103 if (!nv_wait(priv, 0x070000, 0x00000002, 0x00000000))
104 nv_warn(priv, "flush timeout\n");
105 spin_unlock_irqrestore(&priv->lock, flags);
106}
107
108static int
109nv50_bar_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
110 struct nouveau_oclass *oclass, void *data, u32 size,
111 struct nouveau_object **pobject)
112{
113 struct nouveau_device *device = nv_device(parent);
114 struct nouveau_object *heap;
115 struct nouveau_vm *vm;
116 struct nv50_bar_priv *priv;
117 u64 start, limit;
118 int ret;
119
120 ret = nouveau_bar_create(parent, engine, oclass, &priv);
121 *pobject = nv_object(priv);
122 if (ret)
123 return ret;
124
125 ret = nouveau_gpuobj_new(parent, NULL, 0x20000, 0, NVOBJ_FLAG_HEAP,
126 &priv->mem);
127 heap = nv_object(priv->mem);
128 if (ret)
129 return ret;
130
131 ret = nouveau_gpuobj_new(parent, heap, (device->chipset == 0x50) ?
132 0x1400 : 0x0200, 0, 0, &priv->pad);
133 if (ret)
134 return ret;
135
136 ret = nouveau_gpuobj_new(parent, heap, 0x4000, 0, 0, &priv->pgd);
137 if (ret)
138 return ret;
139
140 /* BAR3 */
141 start = 0x0100000000ULL;
142 limit = start + pci_resource_len(device->pdev, 3);
143
144 ret = nouveau_vm_new(device, start, limit, start, &vm);
145 if (ret)
146 return ret;
147
148 ret = nouveau_gpuobj_new(parent, heap, ((limit-- - start) >> 12) * 8,
149 0x1000, NVOBJ_FLAG_ZERO_ALLOC,
150 &vm->pgt[0].obj[0]);
151 vm->pgt[0].refcount[0] = 1;
152 if (ret)
153 return ret;
154
155 ret = nouveau_vm_ref(vm, &priv->bar3_vm, priv->pgd);
156 nouveau_vm_ref(NULL, &vm, NULL);
157 if (ret)
158 return ret;
159
160 ret = nouveau_gpuobj_new(parent, heap, 24, 16, 0, &priv->bar3);
161 if (ret)
162 return ret;
163
164 nv_wo32(priv->bar3, 0x00, 0x7fc00000);
165 nv_wo32(priv->bar3, 0x04, lower_32_bits(limit));
166 nv_wo32(priv->bar3, 0x08, lower_32_bits(start));
167 nv_wo32(priv->bar3, 0x0c, upper_32_bits(limit) << 24 |
168 upper_32_bits(start));
169 nv_wo32(priv->bar3, 0x10, 0x00000000);
170 nv_wo32(priv->bar3, 0x14, 0x00000000);
171
172 /* BAR1 */
173 start = 0x0000000000ULL;
174 limit = start + pci_resource_len(device->pdev, 1);
175
176 ret = nouveau_vm_new(device, start, limit--, start, &vm);
177 if (ret)
178 return ret;
179
180 ret = nouveau_vm_ref(vm, &priv->bar1_vm, priv->pgd);
181 nouveau_vm_ref(NULL, &vm, NULL);
182 if (ret)
183 return ret;
184
185 ret = nouveau_gpuobj_new(parent, heap, 24, 16, 0, &priv->bar1);
186 if (ret)
187 return ret;
188
189 nv_wo32(priv->bar1, 0x00, 0x7fc00000);
190 nv_wo32(priv->bar1, 0x04, lower_32_bits(limit));
191 nv_wo32(priv->bar1, 0x08, lower_32_bits(start));
192 nv_wo32(priv->bar1, 0x0c, upper_32_bits(limit) << 24 |
193 upper_32_bits(start));
194 nv_wo32(priv->bar1, 0x10, 0x00000000);
195 nv_wo32(priv->bar1, 0x14, 0x00000000);
196
197 priv->base.alloc = nouveau_bar_alloc;
198 priv->base.kmap = nv50_bar_kmap;
199 priv->base.umap = nv50_bar_umap;
200 priv->base.unmap = nv50_bar_unmap;
201 if (device->chipset == 0x50)
202 priv->base.flush = nv50_bar_flush;
203 else
204 priv->base.flush = nv84_bar_flush;
205 spin_lock_init(&priv->lock);
206 return 0;
207}
208
209static void
210nv50_bar_dtor(struct nouveau_object *object)
211{
212 struct nv50_bar_priv *priv = (void *)object;
213 nouveau_gpuobj_ref(NULL, &priv->bar1);
214 nouveau_vm_ref(NULL, &priv->bar1_vm, priv->pgd);
215 nouveau_gpuobj_ref(NULL, &priv->bar3);
216 if (priv->bar3_vm) {
217 nouveau_gpuobj_ref(NULL, &priv->bar3_vm->pgt[0].obj[0]);
218 nouveau_vm_ref(NULL, &priv->bar3_vm, priv->pgd);
219 }
220 nouveau_gpuobj_ref(NULL, &priv->pgd);
221 nouveau_gpuobj_ref(NULL, &priv->pad);
222 nouveau_gpuobj_ref(NULL, &priv->mem);
223 nouveau_bar_destroy(&priv->base);
224}
225
226static int
227nv50_bar_init(struct nouveau_object *object)
228{
229 struct nv50_bar_priv *priv = (void *)object;
230 int ret;
231
232 ret = nouveau_bar_init(&priv->base);
233 if (ret)
234 return ret;
235
236 nv_mask(priv, 0x000200, 0x00000100, 0x00000000);
237 nv_mask(priv, 0x000200, 0x00000100, 0x00000100);
238 nv50_vm_flush_engine(nv_subdev(priv), 6);
239
240 nv_wr32(priv, 0x001704, 0x00000000 | priv->mem->addr >> 12);
241 nv_wr32(priv, 0x001704, 0x40000000 | priv->mem->addr >> 12);
242 nv_wr32(priv, 0x001708, 0x80000000 | priv->bar1->node->offset >> 4);
243 nv_wr32(priv, 0x00170c, 0x80000000 | priv->bar3->node->offset >> 4);
244 return 0;
245}
246
247static int
248nv50_bar_fini(struct nouveau_object *object, bool suspend)
249{
250 struct nv50_bar_priv *priv = (void *)object;
251 return nouveau_bar_fini(&priv->base, suspend);
252}
253
254struct nouveau_oclass
255nv50_bar_oclass = {
256 .handle = NV_SUBDEV(BAR, 0x50),
257 .ofuncs = &(struct nouveau_ofuncs) {
258 .ctor = nv50_bar_ctor,
259 .dtor = nv50_bar_dtor,
260 .init = nv50_bar_init,
261 .fini = nv50_bar_fini,
262 },
263};