drm/nouveau: share fence structures between nv10+ and nv50 implementations
[linux-2.6-block.git] / drivers / gpu / drm / nouveau / core / subdev / device / base.c
CommitLineData
9274f4a9
BS
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/device.h>
27#include <core/client.h>
9274f4a9
BS
28#include <core/option.h>
29
30#include <core/class.h>
31
32#include <subdev/device.h>
33
34static DEFINE_MUTEX(nv_devices_mutex);
35static LIST_HEAD(nv_devices);
36
37struct nouveau_device *
38nouveau_device_find(u64 name)
39{
40 struct nouveau_device *device, *match = NULL;
41 mutex_lock(&nv_devices_mutex);
42 list_for_each_entry(device, &nv_devices, head) {
43 if (device->handle == name) {
44 match = device;
45 break;
46 }
47 }
48 mutex_unlock(&nv_devices_mutex);
49 return match;
50}
51
52/******************************************************************************
53 * nouveau_devobj (0x0080): class implementation
54 *****************************************************************************/
55struct nouveau_devobj {
56 struct nouveau_parent base;
57 struct nouveau_object *subdev[NVDEV_SUBDEV_NR];
58 bool created;
59};
60
61static const u64 disable_map[] = {
62 [NVDEV_SUBDEV_VBIOS] = NV_DEVICE_DISABLE_VBIOS,
206c38a9 63 [NVDEV_SUBDEV_DEVINIT] = NV_DEVICE_DISABLE_CORE,
9274f4a9
BS
64 [NVDEV_SUBDEV_GPIO] = NV_DEVICE_DISABLE_CORE,
65 [NVDEV_SUBDEV_I2C] = NV_DEVICE_DISABLE_CORE,
206c38a9
BS
66 [NVDEV_SUBDEV_CLOCK] = NV_DEVICE_DISABLE_CORE,
67 [NVDEV_SUBDEV_MXM] = NV_DEVICE_DISABLE_CORE,
9274f4a9 68 [NVDEV_SUBDEV_MC] = NV_DEVICE_DISABLE_CORE,
a10220bb 69 [NVDEV_SUBDEV_BUS] = NV_DEVICE_DISABLE_CORE,
9274f4a9
BS
70 [NVDEV_SUBDEV_TIMER] = NV_DEVICE_DISABLE_CORE,
71 [NVDEV_SUBDEV_FB] = NV_DEVICE_DISABLE_CORE,
206c38a9
BS
72 [NVDEV_SUBDEV_LTCG] = NV_DEVICE_DISABLE_CORE,
73 [NVDEV_SUBDEV_IBUS] = NV_DEVICE_DISABLE_CORE,
9274f4a9 74 [NVDEV_SUBDEV_INSTMEM] = NV_DEVICE_DISABLE_CORE,
206c38a9 75 [NVDEV_SUBDEV_VM] = NV_DEVICE_DISABLE_CORE,
9274f4a9
BS
76 [NVDEV_SUBDEV_BAR] = NV_DEVICE_DISABLE_CORE,
77 [NVDEV_SUBDEV_VOLT] = NV_DEVICE_DISABLE_CORE,
9274f4a9
BS
78 [NVDEV_SUBDEV_THERM] = NV_DEVICE_DISABLE_CORE,
79 [NVDEV_ENGINE_DMAOBJ] = NV_DEVICE_DISABLE_CORE,
206c38a9
BS
80 [NVDEV_ENGINE_FIFO] = NV_DEVICE_DISABLE_FIFO,
81 [NVDEV_ENGINE_SW] = NV_DEVICE_DISABLE_FIFO,
9274f4a9
BS
82 [NVDEV_ENGINE_GR] = NV_DEVICE_DISABLE_GRAPH,
83 [NVDEV_ENGINE_MPEG] = NV_DEVICE_DISABLE_MPEG,
84 [NVDEV_ENGINE_ME] = NV_DEVICE_DISABLE_ME,
85 [NVDEV_ENGINE_VP] = NV_DEVICE_DISABLE_VP,
86 [NVDEV_ENGINE_CRYPT] = NV_DEVICE_DISABLE_CRYPT,
87 [NVDEV_ENGINE_BSP] = NV_DEVICE_DISABLE_BSP,
88 [NVDEV_ENGINE_PPP] = NV_DEVICE_DISABLE_PPP,
89 [NVDEV_ENGINE_COPY0] = NV_DEVICE_DISABLE_COPY0,
90 [NVDEV_ENGINE_COPY1] = NV_DEVICE_DISABLE_COPY1,
91 [NVDEV_ENGINE_UNK1C1] = NV_DEVICE_DISABLE_UNK1C1,
206c38a9 92 [NVDEV_ENGINE_VENC] = NV_DEVICE_DISABLE_VENC,
9274f4a9
BS
93 [NVDEV_ENGINE_DISP] = NV_DEVICE_DISABLE_DISP,
94 [NVDEV_SUBDEV_NR] = 0,
95};
96
97static int
98nouveau_devobj_ctor(struct nouveau_object *parent,
99 struct nouveau_object *engine,
100 struct nouveau_oclass *oclass, void *data, u32 size,
101 struct nouveau_object **pobject)
102{
103 struct nouveau_client *client = nv_client(parent);
9274f4a9
BS
104 struct nouveau_device *device;
105 struct nouveau_devobj *devobj;
106 struct nv_device_class *args = data;
107 u64 disable, boot0, strap;
108 u64 mmio_base, mmio_size;
109 void __iomem *map;
7234d023 110 int ret, i, c;
9274f4a9
BS
111
112 if (size < sizeof(struct nv_device_class))
113 return -EINVAL;
114
115 /* find the device subdev that matches what the client requested */
116 device = nv_device(client->device);
117 if (args->device != ~0) {
118 device = nouveau_device_find(args->device);
119 if (!device)
120 return -ENODEV;
121 }
122
123 ret = nouveau_parent_create(parent, nv_object(device), oclass, 0, NULL,
124 (1ULL << NVDEV_ENGINE_DMAOBJ) |
125 (1ULL << NVDEV_ENGINE_FIFO) |
126 (1ULL << NVDEV_ENGINE_DISP), &devobj);
127 *pobject = nv_object(devobj);
128 if (ret)
129 return ret;
130
131 mmio_base = pci_resource_start(device->pdev, 0);
132 mmio_size = pci_resource_len(device->pdev, 0);
133
134 /* translate api disable mask into internal mapping */
135 disable = args->debug0;
136 for (i = 0; i < NVDEV_SUBDEV_NR; i++) {
137 if (args->disable & disable_map[i])
138 disable |= (1ULL << i);
139 }
140
141 /* identify the chipset, and determine classes of subdev/engines */
142 if (!(args->disable & NV_DEVICE_DISABLE_IDENTIFY) &&
143 !device->card_type) {
144 map = ioremap(mmio_base, 0x102000);
43b1e9c9
BS
145 if (map == NULL)
146 return -ENOMEM;
9274f4a9
BS
147
148 /* switch mmio to cpu's native endianness */
149#ifndef __BIG_ENDIAN
150 if (ioread32_native(map + 0x000004) != 0x00000000)
151#else
152 if (ioread32_native(map + 0x000004) == 0x00000000)
153#endif
154 iowrite32_native(0x01000001, map + 0x000004);
155
156 /* read boot0 and strapping information */
157 boot0 = ioread32_native(map + 0x000000);
158 strap = ioread32_native(map + 0x101000);
159 iounmap(map);
160
161 /* determine chipset and derive architecture from it */
162 if ((boot0 & 0x0f000000) > 0) {
163 device->chipset = (boot0 & 0xff00000) >> 20;
164 switch (device->chipset & 0xf0) {
165 case 0x10: device->card_type = NV_10; break;
166 case 0x20: device->card_type = NV_20; break;
167 case 0x30: device->card_type = NV_30; break;
168 case 0x40:
169 case 0x60: device->card_type = NV_40; break;
170 case 0x50:
171 case 0x80:
172 case 0x90:
173 case 0xa0: device->card_type = NV_50; break;
174 case 0xc0: device->card_type = NV_C0; break;
175 case 0xd0: device->card_type = NV_D0; break;
176 case 0xe0: device->card_type = NV_E0; break;
177 default:
178 break;
179 }
180 } else
181 if ((boot0 & 0xff00fff0) == 0x20004000) {
182 if (boot0 & 0x00f00000)
183 device->chipset = 0x05;
184 else
185 device->chipset = 0x04;
186 device->card_type = NV_04;
187 }
188
189 switch (device->card_type) {
190 case NV_04: ret = nv04_identify(device); break;
191 case NV_10: ret = nv10_identify(device); break;
192 case NV_20: ret = nv20_identify(device); break;
193 case NV_30: ret = nv30_identify(device); break;
194 case NV_40: ret = nv40_identify(device); break;
195 case NV_50: ret = nv50_identify(device); break;
196 case NV_C0:
197 case NV_D0: ret = nvc0_identify(device); break;
198 case NV_E0: ret = nve0_identify(device); break;
199 default:
200 ret = -EINVAL;
201 break;
202 }
203
204 if (ret) {
205 nv_error(device, "unknown chipset, 0x%08x\n", boot0);
206 return ret;
207 }
208
209 nv_info(device, "BOOT0 : 0x%08x\n", boot0);
2094dd82
BS
210 nv_info(device, "Chipset: %s (NV%02X)\n",
211 device->cname, device->chipset);
9274f4a9
BS
212 nv_info(device, "Family : NV%02X\n", device->card_type);
213
214 /* determine frequency of timing crystal */
215 if ( device->chipset < 0x17 ||
1f2285d4 216 (device->chipset >= 0x20 && device->chipset < 0x25))
9274f4a9
BS
217 strap &= 0x00000040;
218 else
219 strap &= 0x00400040;
220
221 switch (strap) {
222 case 0x00000000: device->crystal = 13500; break;
223 case 0x00000040: device->crystal = 14318; break;
224 case 0x00400000: device->crystal = 27000; break;
225 case 0x00400040: device->crystal = 25000; break;
226 }
227
228 nv_debug(device, "crystal freq: %dKHz\n", device->crystal);
229 }
230
231 if (!(args->disable & NV_DEVICE_DISABLE_MMIO) &&
232 !nv_subdev(device)->mmio) {
233 nv_subdev(device)->mmio = ioremap(mmio_base, mmio_size);
234 if (!nv_subdev(device)->mmio) {
235 nv_error(device, "unable to map device registers\n");
43b1e9c9 236 return -ENOMEM;
9274f4a9
BS
237 }
238 }
239
240 /* ensure requested subsystems are available for use */
7234d023 241 for (i = 0, c = 0; i < NVDEV_SUBDEV_NR; i++) {
9274f4a9
BS
242 if (!(oclass = device->oclass[i]) || (disable & (1ULL << i)))
243 continue;
244
245 if (!device->subdev[i]) {
246 ret = nouveau_object_ctor(nv_object(device), NULL,
7234d023
BS
247 oclass, NULL, i,
248 &devobj->subdev[i]);
ebb945a9
BS
249 if (ret == -ENODEV)
250 continue;
9274f4a9
BS
251 if (ret)
252 return ret;
253
7234d023
BS
254 if (nv_iclass(devobj->subdev[i], NV_ENGINE_CLASS))
255 nouveau_subdev_reset(devobj->subdev[i]);
9274f4a9 256 } else {
7234d023
BS
257 nouveau_object_ref(device->subdev[i],
258 &devobj->subdev[i]);
9274f4a9
BS
259 }
260
7234d023
BS
261 /* note: can't init *any* subdevs until devinit has been run
262 * due to not knowing exactly what the vbios init tables will
263 * mess with. devinit also can't be run until all of its
264 * dependencies have been created.
265 *
266 * this code delays init of any subdev until all of devinit's
267 * dependencies have been created, and then initialises each
268 * subdev in turn as they're created.
269 */
270 while (i >= NVDEV_SUBDEV_DEVINIT_LAST && c <= i) {
271 struct nouveau_object *subdev = devobj->subdev[c++];
272 if (subdev && !nv_iclass(subdev, NV_ENGINE_CLASS)) {
273 ret = nouveau_object_inc(subdev);
274 if (ret)
275 return ret;
9274f4a9
BS
276 }
277 }
9274f4a9
BS
278 }
279
280 return 0;
281}
282
283static void
284nouveau_devobj_dtor(struct nouveau_object *object)
285{
286 struct nouveau_devobj *devobj = (void *)object;
287 int i;
288
289 for (i = NVDEV_SUBDEV_NR - 1; i >= 0; i--)
290 nouveau_object_ref(NULL, &devobj->subdev[i]);
291
292 nouveau_parent_destroy(&devobj->base);
293}
294
295static int
296nouveau_devobj_init(struct nouveau_object *object)
297{
298 struct nouveau_devobj *devobj = (void *)object;
299 struct nouveau_object *subdev;
300 int ret, i;
301
302 ret = nouveau_parent_init(&devobj->base);
303 if (ret)
304 return ret;
305
306 for (i = 0; devobj->created && i < NVDEV_SUBDEV_NR; i++) {
307 if ((subdev = devobj->subdev[i])) {
308 if (!nv_iclass(subdev, NV_ENGINE_CLASS)) {
309 ret = nouveau_object_inc(subdev);
310 if (ret)
311 goto fail;
312 }
313 }
314 }
315
316 devobj->created = true;
317 return 0;
318
319fail:
320 for (--i; i >= 0; i--) {
321 if ((subdev = devobj->subdev[i])) {
322 if (!nv_iclass(subdev, NV_ENGINE_CLASS))
323 nouveau_object_dec(subdev, false);
324 }
325 }
326
327 return ret;
328}
329
330static int
331nouveau_devobj_fini(struct nouveau_object *object, bool suspend)
332{
333 struct nouveau_devobj *devobj = (void *)object;
334 struct nouveau_object *subdev;
335 int ret, i;
336
337 for (i = NVDEV_SUBDEV_NR - 1; i >= 0; i--) {
338 if ((subdev = devobj->subdev[i])) {
339 if (!nv_iclass(subdev, NV_ENGINE_CLASS)) {
340 ret = nouveau_object_dec(subdev, suspend);
341 if (ret && suspend)
342 goto fail;
343 }
344 }
345 }
346
347 ret = nouveau_parent_fini(&devobj->base, suspend);
348fail:
349 for (; ret && suspend && i < NVDEV_SUBDEV_NR; i++) {
350 if ((subdev = devobj->subdev[i])) {
351 if (!nv_iclass(subdev, NV_ENGINE_CLASS)) {
352 ret = nouveau_object_inc(subdev);
353 if (ret) {
354 /* XXX */
355 }
356 }
357 }
358 }
359
360 return ret;
361}
362
363static u8
0a32241d 364nouveau_devobj_rd08(struct nouveau_object *object, u64 addr)
9274f4a9
BS
365{
366 return nv_rd08(object->engine, addr);
367}
368
369static u16
0a32241d 370nouveau_devobj_rd16(struct nouveau_object *object, u64 addr)
9274f4a9
BS
371{
372 return nv_rd16(object->engine, addr);
373}
374
375static u32
0a32241d 376nouveau_devobj_rd32(struct nouveau_object *object, u64 addr)
9274f4a9
BS
377{
378 return nv_rd32(object->engine, addr);
379}
380
381static void
0a32241d 382nouveau_devobj_wr08(struct nouveau_object *object, u64 addr, u8 data)
9274f4a9
BS
383{
384 nv_wr08(object->engine, addr, data);
385}
386
387static void
0a32241d 388nouveau_devobj_wr16(struct nouveau_object *object, u64 addr, u16 data)
9274f4a9
BS
389{
390 nv_wr16(object->engine, addr, data);
391}
392
393static void
0a32241d 394nouveau_devobj_wr32(struct nouveau_object *object, u64 addr, u32 data)
9274f4a9
BS
395{
396 nv_wr32(object->engine, addr, data);
397}
398
399static struct nouveau_ofuncs
400nouveau_devobj_ofuncs = {
401 .ctor = nouveau_devobj_ctor,
402 .dtor = nouveau_devobj_dtor,
403 .init = nouveau_devobj_init,
404 .fini = nouveau_devobj_fini,
405 .rd08 = nouveau_devobj_rd08,
406 .rd16 = nouveau_devobj_rd16,
407 .rd32 = nouveau_devobj_rd32,
408 .wr08 = nouveau_devobj_wr08,
409 .wr16 = nouveau_devobj_wr16,
410 .wr32 = nouveau_devobj_wr32,
411};
412
413/******************************************************************************
414 * nouveau_device: engine functions
415 *****************************************************************************/
416struct nouveau_oclass
417nouveau_device_sclass[] = {
418 { 0x0080, &nouveau_devobj_ofuncs },
419 {}
420};
421
ebb945a9
BS
422static void
423nouveau_device_dtor(struct nouveau_object *object)
424{
425 struct nouveau_device *device = (void *)object;
426
427 mutex_lock(&nv_devices_mutex);
428 list_del(&device->head);
429 mutex_unlock(&nv_devices_mutex);
430
431 if (device->base.mmio)
432 iounmap(device->base.mmio);
433
434 nouveau_subdev_destroy(&device->base);
435}
436
9274f4a9
BS
437static struct nouveau_oclass
438nouveau_device_oclass = {
439 .handle = NV_SUBDEV(DEVICE, 0x00),
440 .ofuncs = &(struct nouveau_ofuncs) {
ebb945a9 441 .dtor = nouveau_device_dtor,
9274f4a9
BS
442 },
443};
444
445int
446nouveau_device_create_(struct pci_dev *pdev, u64 name, const char *sname,
447 const char *cfg, const char *dbg,
448 int length, void **pobject)
449{
450 struct nouveau_device *device;
451 int ret = -EEXIST;
452
453 mutex_lock(&nv_devices_mutex);
454 list_for_each_entry(device, &nv_devices, head) {
455 if (device->handle == name)
456 goto done;
457 }
458
459 ret = nouveau_subdev_create_(NULL, NULL, &nouveau_device_oclass, 0,
460 "DEVICE", "device", length, pobject);
461 device = *pobject;
462 if (ret)
463 goto done;
464
465 atomic_set(&nv_object(device)->usecount, 2);
466 device->pdev = pdev;
467 device->handle = name;
468 device->cfgopt = cfg;
469 device->dbgopt = dbg;
470 device->name = sname;
471
472 nv_subdev(device)->debug = nouveau_dbgopt(device->dbgopt, "DEVICE");
473 list_add(&device->head, &nv_devices);
474done:
475 mutex_unlock(&nv_devices_mutex);
476 return ret;
477}