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/device.h>
27 #include <core/client.h>
28 #include <core/option.h>
29 #include <nvif/unpack.h>
30 #include <nvif/class.h>
32 #include <subdev/fb.h>
33 #include <subdev/instmem.h>
38 static DEFINE_MUTEX(nv_devices_mutex);
39 static LIST_HEAD(nv_devices);
41 struct nouveau_device *
42 nouveau_device_find(u64 name)
44 struct nouveau_device *device, *match = NULL;
45 mutex_lock(&nv_devices_mutex);
46 list_for_each_entry(device, &nv_devices, head) {
47 if (device->handle == name) {
52 mutex_unlock(&nv_devices_mutex);
57 nouveau_device_list(u64 *name, int size)
59 struct nouveau_device *device;
61 mutex_lock(&nv_devices_mutex);
62 list_for_each_entry(device, &nv_devices, head) {
64 name[nr - 1] = device->handle;
66 mutex_unlock(&nv_devices_mutex);
70 /******************************************************************************
71 * nouveau_devobj (0x0080): class implementation
72 *****************************************************************************/
74 struct nouveau_devobj {
75 struct nouveau_parent base;
76 struct nouveau_object *subdev[NVDEV_SUBDEV_NR];
80 nouveau_devobj_info(struct nouveau_object *object, void *data, u32 size)
82 struct nouveau_device *device = nv_device(object);
83 struct nouveau_fb *pfb = nouveau_fb(device);
84 struct nouveau_instmem *imem = nouveau_instmem(device);
86 struct nv_device_info_v0 v0;
90 nv_ioctl(object, "device info size %d\n", size);
91 if (nvif_unpack(args->v0, 0, 0, false)) {
92 nv_ioctl(object, "device info vers %d\n", args->v0.version);
96 switch (device->chipset) {
107 args->v0.platform = NV_DEVICE_INFO_V0_IGP;
111 if (pci_find_capability(device->pdev, PCI_CAP_ID_AGP))
112 args->v0.platform = NV_DEVICE_INFO_V0_AGP;
114 if (pci_is_pcie(device->pdev))
115 args->v0.platform = NV_DEVICE_INFO_V0_PCIE;
117 args->v0.platform = NV_DEVICE_INFO_V0_PCI;
119 args->v0.platform = NV_DEVICE_INFO_V0_SOC;
124 switch (device->card_type) {
125 case NV_04: args->v0.family = NV_DEVICE_INFO_V0_TNT; break;
127 case NV_11: args->v0.family = NV_DEVICE_INFO_V0_CELSIUS; break;
128 case NV_20: args->v0.family = NV_DEVICE_INFO_V0_KELVIN; break;
129 case NV_30: args->v0.family = NV_DEVICE_INFO_V0_RANKINE; break;
130 case NV_40: args->v0.family = NV_DEVICE_INFO_V0_CURIE; break;
131 case NV_50: args->v0.family = NV_DEVICE_INFO_V0_TESLA; break;
132 case NV_C0: args->v0.family = NV_DEVICE_INFO_V0_FERMI; break;
133 case NV_E0: args->v0.family = NV_DEVICE_INFO_V0_KEPLER; break;
134 case GM100: args->v0.family = NV_DEVICE_INFO_V0_MAXWELL; break;
140 args->v0.chipset = device->chipset;
141 args->v0.revision = device->chipset >= 0x10 ? nv_rd32(device, 0) : 0x00;
142 if (pfb) args->v0.ram_size = args->v0.ram_user = pfb->ram->size;
143 else args->v0.ram_size = args->v0.ram_user = 0;
144 if (imem) args->v0.ram_user = args->v0.ram_user - imem->reserved;
149 nouveau_devobj_mthd(struct nouveau_object *object, u32 mthd,
150 void *data, u32 size)
153 case NV_DEVICE_V0_INFO:
154 return nouveau_devobj_info(object, data, size);
162 nouveau_devobj_rd08(struct nouveau_object *object, u64 addr)
164 return nv_rd08(object->engine, addr);
168 nouveau_devobj_rd16(struct nouveau_object *object, u64 addr)
170 return nv_rd16(object->engine, addr);
174 nouveau_devobj_rd32(struct nouveau_object *object, u64 addr)
176 return nv_rd32(object->engine, addr);
180 nouveau_devobj_wr08(struct nouveau_object *object, u64 addr, u8 data)
182 nv_wr08(object->engine, addr, data);
186 nouveau_devobj_wr16(struct nouveau_object *object, u64 addr, u16 data)
188 nv_wr16(object->engine, addr, data);
192 nouveau_devobj_wr32(struct nouveau_object *object, u64 addr, u32 data)
194 nv_wr32(object->engine, addr, data);
198 nouveau_devobj_map(struct nouveau_object *object, u64 *addr, u32 *size)
200 struct nouveau_device *device = nv_device(object);
201 *addr = nv_device_resource_start(device, 0);
202 *size = nv_device_resource_len(device, 0);
206 static const u64 disable_map[] = {
207 [NVDEV_SUBDEV_VBIOS] = NV_DEVICE_V0_DISABLE_VBIOS,
208 [NVDEV_SUBDEV_DEVINIT] = NV_DEVICE_V0_DISABLE_CORE,
209 [NVDEV_SUBDEV_GPIO] = NV_DEVICE_V0_DISABLE_CORE,
210 [NVDEV_SUBDEV_I2C] = NV_DEVICE_V0_DISABLE_CORE,
211 [NVDEV_SUBDEV_CLOCK] = NV_DEVICE_V0_DISABLE_CORE,
212 [NVDEV_SUBDEV_MXM] = NV_DEVICE_V0_DISABLE_CORE,
213 [NVDEV_SUBDEV_MC] = NV_DEVICE_V0_DISABLE_CORE,
214 [NVDEV_SUBDEV_BUS] = NV_DEVICE_V0_DISABLE_CORE,
215 [NVDEV_SUBDEV_TIMER] = NV_DEVICE_V0_DISABLE_CORE,
216 [NVDEV_SUBDEV_FB] = NV_DEVICE_V0_DISABLE_CORE,
217 [NVDEV_SUBDEV_LTC] = NV_DEVICE_V0_DISABLE_CORE,
218 [NVDEV_SUBDEV_IBUS] = NV_DEVICE_V0_DISABLE_CORE,
219 [NVDEV_SUBDEV_INSTMEM] = NV_DEVICE_V0_DISABLE_CORE,
220 [NVDEV_SUBDEV_VM] = NV_DEVICE_V0_DISABLE_CORE,
221 [NVDEV_SUBDEV_BAR] = NV_DEVICE_V0_DISABLE_CORE,
222 [NVDEV_SUBDEV_VOLT] = NV_DEVICE_V0_DISABLE_CORE,
223 [NVDEV_SUBDEV_THERM] = NV_DEVICE_V0_DISABLE_CORE,
224 [NVDEV_SUBDEV_PWR] = NV_DEVICE_V0_DISABLE_CORE,
225 [NVDEV_ENGINE_DMAOBJ] = NV_DEVICE_V0_DISABLE_CORE,
226 [NVDEV_ENGINE_PERFMON] = NV_DEVICE_V0_DISABLE_CORE,
227 [NVDEV_ENGINE_FIFO] = NV_DEVICE_V0_DISABLE_FIFO,
228 [NVDEV_ENGINE_SW] = NV_DEVICE_V0_DISABLE_FIFO,
229 [NVDEV_ENGINE_GR] = NV_DEVICE_V0_DISABLE_GRAPH,
230 [NVDEV_ENGINE_MPEG] = NV_DEVICE_V0_DISABLE_MPEG,
231 [NVDEV_ENGINE_ME] = NV_DEVICE_V0_DISABLE_ME,
232 [NVDEV_ENGINE_VP] = NV_DEVICE_V0_DISABLE_VP,
233 [NVDEV_ENGINE_CRYPT] = NV_DEVICE_V0_DISABLE_CRYPT,
234 [NVDEV_ENGINE_BSP] = NV_DEVICE_V0_DISABLE_BSP,
235 [NVDEV_ENGINE_PPP] = NV_DEVICE_V0_DISABLE_PPP,
236 [NVDEV_ENGINE_COPY0] = NV_DEVICE_V0_DISABLE_COPY0,
237 [NVDEV_ENGINE_COPY1] = NV_DEVICE_V0_DISABLE_COPY1,
238 [NVDEV_ENGINE_VIC] = NV_DEVICE_V0_DISABLE_VIC,
239 [NVDEV_ENGINE_VENC] = NV_DEVICE_V0_DISABLE_VENC,
240 [NVDEV_ENGINE_DISP] = NV_DEVICE_V0_DISABLE_DISP,
241 [NVDEV_SUBDEV_NR] = 0,
245 nouveau_devobj_dtor(struct nouveau_object *object)
247 struct nouveau_devobj *devobj = (void *)object;
250 for (i = NVDEV_SUBDEV_NR - 1; i >= 0; i--)
251 nouveau_object_ref(NULL, &devobj->subdev[i]);
253 nouveau_parent_destroy(&devobj->base);
256 static struct nouveau_oclass
257 nouveau_devobj_oclass_super = {
259 .ofuncs = &(struct nouveau_ofuncs) {
260 .dtor = nouveau_devobj_dtor,
261 .init = _nouveau_parent_init,
262 .fini = _nouveau_parent_fini,
263 .mthd = nouveau_devobj_mthd,
264 .map = nouveau_devobj_map,
265 .rd08 = nouveau_devobj_rd08,
266 .rd16 = nouveau_devobj_rd16,
267 .rd32 = nouveau_devobj_rd32,
268 .wr08 = nouveau_devobj_wr08,
269 .wr16 = nouveau_devobj_wr16,
270 .wr32 = nouveau_devobj_wr32,
275 nouveau_devobj_ctor(struct nouveau_object *parent,
276 struct nouveau_object *engine,
277 struct nouveau_oclass *oclass, void *data, u32 size,
278 struct nouveau_object **pobject)
281 struct nv_device_v0 v0;
283 struct nouveau_client *client = nv_client(parent);
284 struct nouveau_device *device;
285 struct nouveau_devobj *devobj;
287 u64 disable, mmio_base, mmio_size;
291 nv_ioctl(parent, "create device size %d\n", size);
292 if (nvif_unpack(args->v0, 0, 0, false)) {
293 nv_ioctl(parent, "create device v%d device %016llx "
294 "disable %016llx debug0 %016llx\n",
295 args->v0.version, args->v0.device,
296 args->v0.disable, args->v0.debug0);
300 /* give priviledged clients register access */
302 oclass = &nouveau_devobj_oclass_super;
304 /* find the device subdev that matches what the client requested */
305 device = nv_device(client->device);
306 if (args->v0.device != ~0) {
307 device = nouveau_device_find(args->v0.device);
312 ret = nouveau_parent_create(parent, nv_object(device), oclass, 0,
313 nouveau_control_oclass,
314 (1ULL << NVDEV_ENGINE_DMAOBJ) |
315 (1ULL << NVDEV_ENGINE_FIFO) |
316 (1ULL << NVDEV_ENGINE_DISP) |
317 (1ULL << NVDEV_ENGINE_PERFMON), &devobj);
318 *pobject = nv_object(devobj);
322 mmio_base = nv_device_resource_start(device, 0);
323 mmio_size = nv_device_resource_len(device, 0);
325 /* translate api disable mask into internal mapping */
326 disable = args->v0.debug0;
327 for (i = 0; i < NVDEV_SUBDEV_NR; i++) {
328 if (args->v0.disable & disable_map[i])
329 disable |= (1ULL << i);
332 /* identify the chipset, and determine classes of subdev/engines */
333 if (!(args->v0.disable & NV_DEVICE_V0_DISABLE_IDENTIFY) &&
334 !device->card_type) {
335 map = ioremap(mmio_base, 0x102000);
339 /* switch mmio to cpu's native endianness */
341 if (ioread32_native(map + 0x000004) != 0x00000000)
343 if (ioread32_native(map + 0x000004) == 0x00000000)
345 iowrite32_native(0x01000001, map + 0x000004);
347 /* read boot0 and strapping information */
348 boot0 = ioread32_native(map + 0x000000);
349 strap = ioread32_native(map + 0x101000);
352 /* determine chipset and derive architecture from it */
353 if ((boot0 & 0x1f000000) > 0) {
354 device->chipset = (boot0 & 0x1ff00000) >> 20;
355 switch (device->chipset & 0x1f0) {
357 if (0x461 & (1 << (device->chipset & 0xf)))
358 device->card_type = NV_10;
360 device->card_type = NV_11;
363 case 0x020: device->card_type = NV_20; break;
364 case 0x030: device->card_type = NV_30; break;
366 case 0x060: device->card_type = NV_40; break;
370 case 0x0a0: device->card_type = NV_50; break;
372 case 0x0d0: device->card_type = NV_C0; break;
375 case 0x100: device->card_type = NV_E0; break;
376 case 0x110: device->card_type = GM100; break;
381 if ((boot0 & 0xff00fff0) == 0x20004000) {
382 if (boot0 & 0x00f00000)
383 device->chipset = 0x05;
385 device->chipset = 0x04;
386 device->card_type = NV_04;
389 switch (device->card_type) {
390 case NV_04: ret = nv04_identify(device); break;
392 case NV_11: ret = nv10_identify(device); break;
393 case NV_20: ret = nv20_identify(device); break;
394 case NV_30: ret = nv30_identify(device); break;
395 case NV_40: ret = nv40_identify(device); break;
396 case NV_50: ret = nv50_identify(device); break;
397 case NV_C0: ret = nvc0_identify(device); break;
398 case NV_E0: ret = nve0_identify(device); break;
399 case GM100: ret = gm100_identify(device); break;
406 nv_error(device, "unknown chipset, 0x%08x\n", boot0);
410 nv_info(device, "BOOT0 : 0x%08x\n", boot0);
411 nv_info(device, "Chipset: %s (NV%02X)\n",
412 device->cname, device->chipset);
413 nv_info(device, "Family : NV%02X\n", device->card_type);
415 /* determine frequency of timing crystal */
416 if ( device->card_type <= NV_10 || device->chipset < 0x17 ||
417 (device->chipset >= 0x20 && device->chipset < 0x25))
423 case 0x00000000: device->crystal = 13500; break;
424 case 0x00000040: device->crystal = 14318; break;
425 case 0x00400000: device->crystal = 27000; break;
426 case 0x00400040: device->crystal = 25000; break;
429 nv_debug(device, "crystal freq: %dKHz\n", device->crystal);
432 if (!(args->v0.disable & NV_DEVICE_V0_DISABLE_MMIO) &&
433 !nv_subdev(device)->mmio) {
434 nv_subdev(device)->mmio = ioremap(mmio_base, mmio_size);
435 if (!nv_subdev(device)->mmio) {
436 nv_error(device, "unable to map device registers\n");
441 /* ensure requested subsystems are available for use */
442 for (i = 1, c = 1; i < NVDEV_SUBDEV_NR; i++) {
443 if (!(oclass = device->oclass[i]) || (disable & (1ULL << i)))
446 if (device->subdev[i]) {
447 nouveau_object_ref(device->subdev[i],
452 ret = nouveau_object_ctor(nv_object(device), NULL,
460 device->subdev[i] = devobj->subdev[i];
462 /* note: can't init *any* subdevs until devinit has been run
463 * due to not knowing exactly what the vbios init tables will
464 * mess with. devinit also can't be run until all of its
465 * dependencies have been created.
467 * this code delays init of any subdev until all of devinit's
468 * dependencies have been created, and then initialises each
469 * subdev in turn as they're created.
471 while (i >= NVDEV_SUBDEV_DEVINIT_LAST && c <= i) {
472 struct nouveau_object *subdev = devobj->subdev[c++];
473 if (subdev && !nv_iclass(subdev, NV_ENGINE_CLASS)) {
474 ret = nouveau_object_inc(subdev);
477 atomic_dec(&nv_object(device)->usecount);
480 nouveau_subdev_reset(subdev);
488 static struct nouveau_ofuncs
489 nouveau_devobj_ofuncs = {
490 .ctor = nouveau_devobj_ctor,
491 .dtor = nouveau_devobj_dtor,
492 .init = _nouveau_parent_init,
493 .fini = _nouveau_parent_fini,
494 .mthd = nouveau_devobj_mthd,
497 /******************************************************************************
498 * nouveau_device: engine functions
499 *****************************************************************************/
501 static struct nouveau_oclass
502 nouveau_device_sclass[] = {
503 { 0x0080, &nouveau_devobj_ofuncs },
508 nouveau_device_event_ctor(void *data, u32 size, struct nvkm_notify *notify)
510 if (!WARN_ON(size != 0)) {
519 static const struct nvkm_event_func
520 nouveau_device_event_func = {
521 .ctor = nouveau_device_event_ctor,
525 nouveau_device_fini(struct nouveau_object *object, bool suspend)
527 struct nouveau_device *device = (void *)object;
528 struct nouveau_object *subdev;
531 for (i = NVDEV_SUBDEV_NR - 1; i >= 0; i--) {
532 if ((subdev = device->subdev[i])) {
533 if (!nv_iclass(subdev, NV_ENGINE_CLASS)) {
534 ret = nouveau_object_dec(subdev, suspend);
541 ret = nvkm_acpi_fini(device, suspend);
543 for (; ret && i < NVDEV_SUBDEV_NR; i++) {
544 if ((subdev = device->subdev[i])) {
545 if (!nv_iclass(subdev, NV_ENGINE_CLASS)) {
546 ret = nouveau_object_inc(subdev);
558 nouveau_device_init(struct nouveau_object *object)
560 struct nouveau_device *device = (void *)object;
561 struct nouveau_object *subdev;
564 ret = nvkm_acpi_init(device);
568 for (i = 0; i < NVDEV_SUBDEV_NR; i++) {
569 if ((subdev = device->subdev[i])) {
570 if (!nv_iclass(subdev, NV_ENGINE_CLASS)) {
571 ret = nouveau_object_inc(subdev);
575 nouveau_subdev_reset(subdev);
582 for (--i; ret && i >= 0; i--) {
583 if ((subdev = device->subdev[i])) {
584 if (!nv_iclass(subdev, NV_ENGINE_CLASS))
585 nouveau_object_dec(subdev, false);
590 nvkm_acpi_fini(device, false);
595 nouveau_device_dtor(struct nouveau_object *object)
597 struct nouveau_device *device = (void *)object;
599 nvkm_event_fini(&device->event);
601 mutex_lock(&nv_devices_mutex);
602 list_del(&device->head);
603 mutex_unlock(&nv_devices_mutex);
605 if (nv_subdev(device)->mmio)
606 iounmap(nv_subdev(device)->mmio);
608 nouveau_engine_destroy(&device->base);
612 nv_device_resource_start(struct nouveau_device *device, unsigned int bar)
614 if (nv_device_is_pci(device)) {
615 return pci_resource_start(device->pdev, bar);
617 struct resource *res;
618 res = platform_get_resource(device->platformdev,
619 IORESOURCE_MEM, bar);
627 nv_device_resource_len(struct nouveau_device *device, unsigned int bar)
629 if (nv_device_is_pci(device)) {
630 return pci_resource_len(device->pdev, bar);
632 struct resource *res;
633 res = platform_get_resource(device->platformdev,
634 IORESOURCE_MEM, bar);
637 return resource_size(res);
642 nv_device_get_irq(struct nouveau_device *device, bool stall)
644 if (nv_device_is_pci(device)) {
645 return device->pdev->irq;
647 return platform_get_irq_byname(device->platformdev,
648 stall ? "stall" : "nonstall");
652 static struct nouveau_oclass
653 nouveau_device_oclass = {
654 .handle = NV_ENGINE(DEVICE, 0x00),
655 .ofuncs = &(struct nouveau_ofuncs) {
656 .dtor = nouveau_device_dtor,
657 .init = nouveau_device_init,
658 .fini = nouveau_device_fini,
663 nouveau_device_create_(void *dev, enum nv_bus_type type, u64 name,
664 const char *sname, const char *cfg, const char *dbg,
665 int length, void **pobject)
667 struct nouveau_device *device;
670 mutex_lock(&nv_devices_mutex);
671 list_for_each_entry(device, &nv_devices, head) {
672 if (device->handle == name)
676 ret = nouveau_engine_create_(NULL, NULL, &nouveau_device_oclass, true,
677 "DEVICE", "device", length, pobject);
683 case NOUVEAU_BUS_PCI:
686 case NOUVEAU_BUS_PLATFORM:
687 device->platformdev = dev;
690 device->handle = name;
691 device->cfgopt = cfg;
692 device->dbgopt = dbg;
693 device->name = sname;
695 nv_subdev(device)->debug = nouveau_dbgopt(device->dbgopt, "DEVICE");
696 nv_engine(device)->sclass = nouveau_device_sclass;
697 list_add(&device->head, &nv_devices);
699 ret = nvkm_event_init(&nouveau_device_event_func, 1, 1,
702 mutex_unlock(&nv_devices_mutex);