base.c 21.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright 2012 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Ben Skeggs
 */
24 25
#include "priv.h"
#include "acpi.h"
26 27 28

#include <core/client.h>
#include <core/option.h>
29 30
#include <core/notify.h>
#include <core/parent.h>
31
#include <subdev/bios.h>
32 33 34
#include <subdev/fb.h>
#include <subdev/instmem.h>

35 36
#include <nvif/class.h>
#include <nvif/unpack.h>
37 38 39 40

static DEFINE_MUTEX(nv_devices_mutex);
static LIST_HEAD(nv_devices);

41 42
struct nvkm_device *
nvkm_device_find(u64 name)
43
{
44
	struct nvkm_device *device, *match = NULL;
45 46 47 48 49 50 51 52 53 54 55
	mutex_lock(&nv_devices_mutex);
	list_for_each_entry(device, &nv_devices, head) {
		if (device->handle == name) {
			match = device;
			break;
		}
	}
	mutex_unlock(&nv_devices_mutex);
	return match;
}

56
int
57
nvkm_device_list(u64 *name, int size)
58
{
59
	struct nvkm_device *device;
60 61 62 63 64 65 66 67 68 69
	int nr = 0;
	mutex_lock(&nv_devices_mutex);
	list_for_each_entry(device, &nv_devices, head) {
		if (nr++ < size)
			name[nr - 1] = device->handle;
	}
	mutex_unlock(&nv_devices_mutex);
	return nr;
}

70
/******************************************************************************
71
 * nvkm_devobj (0x0080): class implementation
72
 *****************************************************************************/
73

74 75 76
struct nvkm_devobj {
	struct nvkm_parent base;
	struct nvkm_object *subdev[NVDEV_SUBDEV_NR];
77 78
};

79
static int
80
nvkm_devobj_info(struct nvkm_object *object, void *data, u32 size)
81
{
82 83 84
	struct nvkm_device *device = nv_device(object);
	struct nvkm_fb *pfb = nvkm_fb(device);
	struct nvkm_instmem *imem = nvkm_instmem(device);
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	union {
		struct nv_device_info_v0 v0;
	} *args = data;
	int ret;

	nv_ioctl(object, "device info size %d\n", size);
	if (nvif_unpack(args->v0, 0, 0, false)) {
		nv_ioctl(object, "device info vers %d\n", args->v0.version);
	} else
		return ret;

	switch (device->chipset) {
	case 0x01a:
	case 0x01f:
	case 0x04c:
	case 0x04e:
	case 0x063:
	case 0x067:
	case 0x068:
	case 0x0aa:
	case 0x0ac:
	case 0x0af:
		args->v0.platform = NV_DEVICE_INFO_V0_IGP;
		break;
	default:
		if (device->pdev) {
			if (pci_find_capability(device->pdev, PCI_CAP_ID_AGP))
				args->v0.platform = NV_DEVICE_INFO_V0_AGP;
			else
			if (pci_is_pcie(device->pdev))
				args->v0.platform = NV_DEVICE_INFO_V0_PCIE;
			else
				args->v0.platform = NV_DEVICE_INFO_V0_PCI;
		} else {
			args->v0.platform = NV_DEVICE_INFO_V0_SOC;
		}
		break;
	}

	switch (device->card_type) {
	case NV_04: args->v0.family = NV_DEVICE_INFO_V0_TNT; break;
	case NV_10:
	case NV_11: args->v0.family = NV_DEVICE_INFO_V0_CELSIUS; break;
	case NV_20: args->v0.family = NV_DEVICE_INFO_V0_KELVIN; break;
	case NV_30: args->v0.family = NV_DEVICE_INFO_V0_RANKINE; break;
	case NV_40: args->v0.family = NV_DEVICE_INFO_V0_CURIE; break;
	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 134 135 136 137 138 139 140
	case NV_E0: args->v0.family = NV_DEVICE_INFO_V0_KEPLER; break;
	case GM100: args->v0.family = NV_DEVICE_INFO_V0_MAXWELL; break;
	default:
		args->v0.family = 0;
		break;
	}

	args->v0.chipset  = device->chipset;
B
Ben Skeggs 已提交
141
	args->v0.revision = device->chiprev;
A
Alexandre Courbot 已提交
142 143 144 145 146 147 148
	if (pfb && pfb->ram)
		args->v0.ram_size = args->v0.ram_user = pfb->ram->size;
	else
		args->v0.ram_size = args->v0.ram_user = 0;
	if (imem && args->v0.ram_size > 0)
		args->v0.ram_user = args->v0.ram_user - imem->reserved;

149 150 151 152
	return 0;
}

static int
153
nvkm_devobj_mthd(struct nvkm_object *object, u32 mthd, void *data, u32 size)
154 155 156
{
	switch (mthd) {
	case NV_DEVICE_V0_INFO:
157
		return nvkm_devobj_info(object, data, size);
158 159 160 161 162 163 164
	default:
		break;
	}
	return -EINVAL;
}

static u8
165
nvkm_devobj_rd08(struct nvkm_object *object, u64 addr)
166 167 168 169 170
{
	return nv_rd08(object->engine, addr);
}

static u16
171
nvkm_devobj_rd16(struct nvkm_object *object, u64 addr)
172 173 174 175 176
{
	return nv_rd16(object->engine, addr);
}

static u32
177
nvkm_devobj_rd32(struct nvkm_object *object, u64 addr)
178 179 180 181 182
{
	return nv_rd32(object->engine, addr);
}

static void
183
nvkm_devobj_wr08(struct nvkm_object *object, u64 addr, u8 data)
184 185 186 187 188
{
	nv_wr08(object->engine, addr, data);
}

static void
189
nvkm_devobj_wr16(struct nvkm_object *object, u64 addr, u16 data)
190 191 192 193 194
{
	nv_wr16(object->engine, addr, data);
}

static void
195
nvkm_devobj_wr32(struct nvkm_object *object, u64 addr, u32 data)
196 197 198 199
{
	nv_wr32(object->engine, addr, data);
}

200
static int
201
nvkm_devobj_map(struct nvkm_object *object, u64 *addr, u32 *size)
202
{
203
	struct nvkm_device *device = nv_device(object);
204 205 206 207 208
	*addr = nv_device_resource_start(device, 0);
	*size = nv_device_resource_len(device, 0);
	return 0;
}

209
static const u64 disable_map[] = {
210 211 212 213
	[NVDEV_SUBDEV_VBIOS]	= NV_DEVICE_V0_DISABLE_VBIOS,
	[NVDEV_SUBDEV_DEVINIT]	= NV_DEVICE_V0_DISABLE_CORE,
	[NVDEV_SUBDEV_GPIO]	= NV_DEVICE_V0_DISABLE_CORE,
	[NVDEV_SUBDEV_I2C]	= NV_DEVICE_V0_DISABLE_CORE,
214
	[NVDEV_SUBDEV_CLK  ]	= NV_DEVICE_V0_DISABLE_CORE,
215 216 217 218 219
	[NVDEV_SUBDEV_MXM]	= NV_DEVICE_V0_DISABLE_CORE,
	[NVDEV_SUBDEV_MC]	= NV_DEVICE_V0_DISABLE_CORE,
	[NVDEV_SUBDEV_BUS]	= NV_DEVICE_V0_DISABLE_CORE,
	[NVDEV_SUBDEV_TIMER]	= NV_DEVICE_V0_DISABLE_CORE,
	[NVDEV_SUBDEV_FB]	= NV_DEVICE_V0_DISABLE_CORE,
220
	[NVDEV_SUBDEV_LTC]	= NV_DEVICE_V0_DISABLE_CORE,
221 222
	[NVDEV_SUBDEV_IBUS]	= NV_DEVICE_V0_DISABLE_CORE,
	[NVDEV_SUBDEV_INSTMEM]	= NV_DEVICE_V0_DISABLE_CORE,
223
	[NVDEV_SUBDEV_MMU]	= NV_DEVICE_V0_DISABLE_CORE,
224 225 226
	[NVDEV_SUBDEV_BAR]	= NV_DEVICE_V0_DISABLE_CORE,
	[NVDEV_SUBDEV_VOLT]	= NV_DEVICE_V0_DISABLE_CORE,
	[NVDEV_SUBDEV_THERM]	= NV_DEVICE_V0_DISABLE_CORE,
227
	[NVDEV_SUBDEV_PMU]	= NV_DEVICE_V0_DISABLE_CORE,
228
	[NVDEV_SUBDEV_FUSE]	= NV_DEVICE_V0_DISABLE_CORE,
229
	[NVDEV_ENGINE_DMAOBJ]	= NV_DEVICE_V0_DISABLE_CORE,
230
	[NVDEV_ENGINE_PM     ]  = NV_DEVICE_V0_DISABLE_CORE,
231 232
	[NVDEV_ENGINE_FIFO]	= NV_DEVICE_V0_DISABLE_FIFO,
	[NVDEV_ENGINE_SW]	= NV_DEVICE_V0_DISABLE_FIFO,
233
	[NVDEV_ENGINE_GR]	= NV_DEVICE_V0_DISABLE_GR,
234 235 236
	[NVDEV_ENGINE_MPEG]	= NV_DEVICE_V0_DISABLE_MPEG,
	[NVDEV_ENGINE_ME]	= NV_DEVICE_V0_DISABLE_ME,
	[NVDEV_ENGINE_VP]	= NV_DEVICE_V0_DISABLE_VP,
237
	[NVDEV_ENGINE_CIPHER]	= NV_DEVICE_V0_DISABLE_CIPHER,
238
	[NVDEV_ENGINE_BSP]	= NV_DEVICE_V0_DISABLE_BSP,
239
	[NVDEV_ENGINE_MSPPP]	= NV_DEVICE_V0_DISABLE_MSPPP,
240 241 242
	[NVDEV_ENGINE_CE0]	= NV_DEVICE_V0_DISABLE_CE0,
	[NVDEV_ENGINE_CE1]	= NV_DEVICE_V0_DISABLE_CE1,
	[NVDEV_ENGINE_CE2]	= NV_DEVICE_V0_DISABLE_CE2,
243
	[NVDEV_ENGINE_VIC]	= NV_DEVICE_V0_DISABLE_VIC,
244
	[NVDEV_ENGINE_MSENC]	= NV_DEVICE_V0_DISABLE_MSENC,
245
	[NVDEV_ENGINE_DISP]	= NV_DEVICE_V0_DISABLE_DISP,
246
	[NVDEV_ENGINE_MSVLD]	= NV_DEVICE_V0_DISABLE_MSVLD,
247
	[NVDEV_ENGINE_SEC]	= NV_DEVICE_V0_DISABLE_SEC,
248 249 250
	[NVDEV_SUBDEV_NR]	= 0,
};

251
static void
252
nvkm_devobj_dtor(struct nvkm_object *object)
253
{
254
	struct nvkm_devobj *devobj = (void *)object;
255 256 257
	int i;

	for (i = NVDEV_SUBDEV_NR - 1; i >= 0; i--)
258
		nvkm_object_ref(NULL, &devobj->subdev[i]);
259

260
	nvkm_parent_destroy(&devobj->base);
261 262
}

263 264
static struct nvkm_oclass
nvkm_devobj_oclass_super = {
265
	.handle = NV_DEVICE,
266 267 268 269 270 271 272 273 274 275 276 277
	.ofuncs = &(struct nvkm_ofuncs) {
		.dtor = nvkm_devobj_dtor,
		.init = _nvkm_parent_init,
		.fini = _nvkm_parent_fini,
		.mthd = nvkm_devobj_mthd,
		.map  = nvkm_devobj_map,
		.rd08 = nvkm_devobj_rd08,
		.rd16 = nvkm_devobj_rd16,
		.rd32 = nvkm_devobj_rd32,
		.wr08 = nvkm_devobj_wr08,
		.wr16 = nvkm_devobj_wr16,
		.wr32 = nvkm_devobj_wr32,
278 279 280
	}
};

281
static int
282 283 284
nvkm_devobj_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
		 struct nvkm_oclass *oclass, void *data, u32 size,
		 struct nvkm_object **pobject)
285
{
286 287 288
	union {
		struct nv_device_v0 v0;
	} *args = data;
289 290 291
	struct nvkm_client *client = nv_client(parent);
	struct nvkm_device *device;
	struct nvkm_devobj *devobj;
292 293
	u32 boot0, strap;
	u64 disable, mmio_base, mmio_size;
294
	void __iomem *map;
295
	int ret, i, c;
296

297 298 299 300 301 302 303 304 305 306 307
	nv_ioctl(parent, "create device size %d\n", size);
	if (nvif_unpack(args->v0, 0, 0, false)) {
		nv_ioctl(parent, "create device v%d device %016llx "
				 "disable %016llx debug0 %016llx\n",
			 args->v0.version, args->v0.device,
			 args->v0.disable, args->v0.debug0);
	} else
		return ret;

	/* give priviledged clients register access */
	if (client->super)
308
		oclass = &nvkm_devobj_oclass_super;
309 310 311

	/* find the device subdev that matches what the client requested */
	device = nv_device(client->device);
312
	if (args->v0.device != ~0) {
313
		device = nvkm_device_find(args->v0.device);
314 315 316 317
		if (!device)
			return -ENODEV;
	}

318 319 320 321 322 323
	ret = nvkm_parent_create(parent, nv_object(device), oclass, 0,
				 nvkm_control_oclass,
				 (1ULL << NVDEV_ENGINE_DMAOBJ) |
				 (1ULL << NVDEV_ENGINE_FIFO) |
				 (1ULL << NVDEV_ENGINE_DISP) |
				 (1ULL << NVDEV_ENGINE_PM), &devobj);
324 325 326 327
	*pobject = nv_object(devobj);
	if (ret)
		return ret;

A
Alexandre Courbot 已提交
328 329
	mmio_base = nv_device_resource_start(device, 0);
	mmio_size = nv_device_resource_len(device, 0);
330 331

	/* translate api disable mask into internal mapping */
332
	disable = args->v0.debug0;
333
	for (i = 0; i < NVDEV_SUBDEV_NR; i++) {
334
		if (args->v0.disable & disable_map[i])
335 336 337 338
			disable |= (1ULL << i);
	}

	/* identify the chipset, and determine classes of subdev/engines */
339
	if (!(args->v0.disable & NV_DEVICE_V0_DISABLE_IDENTIFY) &&
340 341
	    !device->card_type) {
		map = ioremap(mmio_base, 0x102000);
342 343
		if (map == NULL)
			return -ENOMEM;
344 345 346

		/* switch mmio to cpu's native endianness */
#ifndef __BIG_ENDIAN
347
		if (ioread32_native(map + 0x000004) != 0x00000000) {
348
#else
349
		if (ioread32_native(map + 0x000004) == 0x00000000) {
350 351
#endif
			iowrite32_native(0x01000001, map + 0x000004);
352 353
			ioread32_native(map);
		}
354 355 356 357 358 359 360

		/* read boot0 and strapping information */
		boot0 = ioread32_native(map + 0x000000);
		strap = ioread32_native(map + 0x101000);
		iounmap(map);

		/* determine chipset and derive architecture from it */
361 362
		if ((boot0 & 0x1f000000) > 0) {
			device->chipset = (boot0 & 0x1ff00000) >> 20;
B
Ben Skeggs 已提交
363
			device->chiprev = (boot0 & 0x000000ff);
364
			switch (device->chipset & 0x1f0) {
B
Ben Skeggs 已提交
365
			case 0x010: {
366 367 368 369
				if (0x461 & (1 << (device->chipset & 0xf)))
					device->card_type = NV_10;
				else
					device->card_type = NV_11;
B
Ben Skeggs 已提交
370
				device->chiprev = 0x00;
371 372
				break;
			}
B
Ben Skeggs 已提交
373 374 375 376 377 378 379 380
			case 0x020: device->card_type = NV_20; break;
			case 0x030: device->card_type = NV_30; break;
			case 0x040:
			case 0x060: device->card_type = NV_40; break;
			case 0x050:
			case 0x080:
			case 0x090:
			case 0x0a0: device->card_type = NV_50; break;
381 382
			case 0x0c0:
			case 0x0d0: device->card_type = NV_C0; break;
B
Ben Skeggs 已提交
383 384 385
			case 0x0e0:
			case 0x0f0:
			case 0x100: device->card_type = NV_E0; break;
B
Ben Skeggs 已提交
386 387
			case 0x110:
			case 0x120: device->card_type = GM100; break;
388 389 390 391 392 393 394 395 396 397 398 399 400 401
			default:
				break;
			}
		} else
		if ((boot0 & 0xff00fff0) == 0x20004000) {
			if (boot0 & 0x00f00000)
				device->chipset = 0x05;
			else
				device->chipset = 0x04;
			device->card_type = NV_04;
		}

		switch (device->card_type) {
		case NV_04: ret = nv04_identify(device); break;
402 403
		case NV_10:
		case NV_11: ret = nv10_identify(device); break;
404 405 406 407
		case NV_20: ret = nv20_identify(device); break;
		case NV_30: ret = nv30_identify(device); break;
		case NV_40: ret = nv40_identify(device); break;
		case NV_50: ret = nv50_identify(device); break;
408 409
		case NV_C0: ret = gf100_identify(device); break;
		case NV_E0: ret = gk104_identify(device); break;
B
Ben Skeggs 已提交
410
		case GM100: ret = gm100_identify(device); break;
411 412 413 414 415 416 417 418 419 420 421
		default:
			ret = -EINVAL;
			break;
		}

		if (ret) {
			nv_error(device, "unknown chipset, 0x%08x\n", boot0);
			return ret;
		}

		nv_info(device, "BOOT0  : 0x%08x\n", boot0);
422 423
		nv_info(device, "Chipset: %s (NV%02X)\n",
			device->cname, device->chipset);
424 425 426
		nv_info(device, "Family : NV%02X\n", device->card_type);

		/* determine frequency of timing crystal */
427
		if ( device->card_type <= NV_10 || device->chipset < 0x17 ||
428
		    (device->chipset >= 0x20 && device->chipset < 0x25))
429 430 431 432 433 434 435 436 437 438 439 440
			strap &= 0x00000040;
		else
			strap &= 0x00400040;

		switch (strap) {
		case 0x00000000: device->crystal = 13500; break;
		case 0x00000040: device->crystal = 14318; break;
		case 0x00400000: device->crystal = 27000; break;
		case 0x00400040: device->crystal = 25000; break;
		}

		nv_debug(device, "crystal freq: %dKHz\n", device->crystal);
441 442 443
	} else
	if ( (args->v0.disable & NV_DEVICE_V0_DISABLE_IDENTIFY)) {
		device->cname = "NULL";
444
		device->oclass[NVDEV_SUBDEV_VBIOS] = &nvkm_bios_oclass;
445 446
	}

447
	if (!(args->v0.disable & NV_DEVICE_V0_DISABLE_MMIO) &&
448 449 450 451
	    !nv_subdev(device)->mmio) {
		nv_subdev(device)->mmio  = ioremap(mmio_base, mmio_size);
		if (!nv_subdev(device)->mmio) {
			nv_error(device, "unable to map device registers\n");
452
			return -ENOMEM;
453 454 455 456
		}
	}

	/* ensure requested subsystems are available for use */
457
	for (i = 1, c = 1; i < NVDEV_SUBDEV_NR; i++) {
458 459 460
		if (!(oclass = device->oclass[i]) || (disable & (1ULL << i)))
			continue;

461
		if (device->subdev[i]) {
462
			nvkm_object_ref(device->subdev[i], &devobj->subdev[i]);
463
			continue;
464 465
		}

466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
#define _(s,m) case s:                                                         \
		ret = nvkm_object_ctor(nv_object(device), NULL, oclass, NULL,  \
				       (s), (struct nvkm_object **)&device->m);\
		if (ret == -ENODEV)                                            \
			continue;                                              \
		if (ret)                                                       \
			return ret;                                            \
		devobj->subdev[s] = (struct nvkm_object *)device->m;           \
		device->subdev[s] = devobj->subdev[s];                         \
		break

		switch (i) {
		_(NVDEV_SUBDEV_BAR    ,     bar);
		_(NVDEV_SUBDEV_VBIOS  ,    bios);
		_(NVDEV_SUBDEV_BUS    ,     bus);
		_(NVDEV_SUBDEV_CLK    ,     clk);
		_(NVDEV_SUBDEV_DEVINIT, devinit);
		_(NVDEV_SUBDEV_FB     ,      fb);
		_(NVDEV_SUBDEV_FUSE   ,    fuse);
		_(NVDEV_SUBDEV_GPIO   ,    gpio);
		_(NVDEV_SUBDEV_I2C    ,     i2c);
		_(NVDEV_SUBDEV_IBUS   ,    ibus);
		_(NVDEV_SUBDEV_INSTMEM,    imem);
		_(NVDEV_SUBDEV_LTC    ,     ltc);
		_(NVDEV_SUBDEV_MC     ,      mc);
		_(NVDEV_SUBDEV_MMU    ,     mmu);
		_(NVDEV_SUBDEV_MXM    ,     mxm);
		_(NVDEV_SUBDEV_PMU    ,     pmu);
		_(NVDEV_SUBDEV_THERM  ,   therm);
		_(NVDEV_SUBDEV_TIMER  ,   timer);
		_(NVDEV_SUBDEV_VOLT   ,    volt);
		_(NVDEV_ENGINE_BSP    ,     bsp);
		_(NVDEV_ENGINE_CE0    ,   ce[0]);
		_(NVDEV_ENGINE_CE1    ,   ce[1]);
		_(NVDEV_ENGINE_CE2    ,   ce[2]);
		_(NVDEV_ENGINE_CIPHER ,  cipher);
		_(NVDEV_ENGINE_DISP   ,    disp);
		_(NVDEV_ENGINE_DMAOBJ ,     dma);
		_(NVDEV_ENGINE_FIFO   ,    fifo);
		_(NVDEV_ENGINE_GR     ,      gr);
		_(NVDEV_ENGINE_IFB    ,     ifb);
		_(NVDEV_ENGINE_ME     ,      me);
		_(NVDEV_ENGINE_MPEG   ,    mpeg);
		_(NVDEV_ENGINE_MSENC  ,   msenc);
		_(NVDEV_ENGINE_MSPDEC ,  mspdec);
		_(NVDEV_ENGINE_MSPPP  ,   msppp);
		_(NVDEV_ENGINE_MSVLD  ,   msvld);
		_(NVDEV_ENGINE_PM     ,      pm);
		_(NVDEV_ENGINE_SEC    ,     sec);
		_(NVDEV_ENGINE_SW     ,      sw);
		_(NVDEV_ENGINE_VIC    ,     vic);
		_(NVDEV_ENGINE_VP     ,      vp);
		default:
			WARN_ON(1);
520
			continue;
521 522
		}
#undef _
523

524 525 526 527 528 529 530 531 532 533
		/* note: can't init *any* subdevs until devinit has been run
		 * due to not knowing exactly what the vbios init tables will
		 * mess with.  devinit also can't be run until all of its
		 * dependencies have been created.
		 *
		 * this code delays init of any subdev until all of devinit's
		 * dependencies have been created, and then initialises each
		 * subdev in turn as they're created.
		 */
		while (i >= NVDEV_SUBDEV_DEVINIT_LAST && c <= i) {
534
			struct nvkm_object *subdev = devobj->subdev[c++];
535
			if (subdev && !nv_iclass(subdev, NV_ENGINE_CLASS)) {
536
				ret = nvkm_object_inc(subdev);
537 538
				if (ret)
					return ret;
539 540 541
				atomic_dec(&nv_object(device)->usecount);
			} else
			if (subdev) {
542
				nvkm_subdev_reset(subdev);
543 544 545 546 547 548 549
			}
		}
	}

	return 0;
}

550 551 552 553 554 555 556
static struct nvkm_ofuncs
nvkm_devobj_ofuncs = {
	.ctor = nvkm_devobj_ctor,
	.dtor = nvkm_devobj_dtor,
	.init = _nvkm_parent_init,
	.fini = _nvkm_parent_fini,
	.mthd = nvkm_devobj_mthd,
557 558 559
};

/******************************************************************************
560
 * nvkm_device: engine functions
561
 *****************************************************************************/
562

563
struct nvkm_device *
564 565
nv_device(void *obj)
{
566
	struct nvkm_object *device = nv_object(obj);
567 568 569 570
	if (device->engine == NULL) {
		while (device && device->parent)
			device = device->parent;
	} else {
571
		device = &nv_object(obj)->engine->subdev.object;
572 573
		if (device && device->parent)
			device = device->parent;
574
	}
575 576 577
#if CONFIG_NOUVEAU_DEBUG >= NV_DBG_PARANOIA
	if (unlikely(!device))
		nv_assert("BAD CAST -> NvDevice, 0x%08x\n", nv_hclass(obj));
578 579 580 581
#endif
	return (void *)device;
}

582 583 584
static struct nvkm_oclass
nvkm_device_sclass[] = {
	{ 0x0080, &nvkm_devobj_ofuncs },
585 586 587
	{}
};

588
static int
589 590
nvkm_device_event_ctor(struct nvkm_object *object, void *data, u32 size,
		       struct nvkm_notify *notify)
591 592 593 594 595 596 597 598 599 600 601
{
	if (!WARN_ON(size != 0)) {
		notify->size  = 0;
		notify->types = 1;
		notify->index = 0;
		return 0;
	}
	return -EINVAL;
}

static const struct nvkm_event_func
602 603
nvkm_device_event_func = {
	.ctor = nvkm_device_event_ctor,
604 605
};

606
static int
607
nvkm_device_fini(struct nvkm_object *object, bool suspend)
608
{
609 610
	struct nvkm_device *device = (void *)object;
	struct nvkm_object *subdev;
611 612 613 614 615
	int ret, i;

	for (i = NVDEV_SUBDEV_NR - 1; i >= 0; i--) {
		if ((subdev = device->subdev[i])) {
			if (!nv_iclass(subdev, NV_ENGINE_CLASS)) {
616
				ret = nvkm_object_dec(subdev, suspend);
617 618 619 620 621 622
				if (ret && suspend)
					goto fail;
			}
		}
	}

623
	ret = nvkm_acpi_fini(device, suspend);
624 625 626 627
fail:
	for (; ret && i < NVDEV_SUBDEV_NR; i++) {
		if ((subdev = device->subdev[i])) {
			if (!nv_iclass(subdev, NV_ENGINE_CLASS)) {
628
				ret = nvkm_object_inc(subdev);
629 630 631 632 633 634 635 636
				if (ret) {
					/* XXX */
				}
			}
		}
	}

	return ret;
637 638 639
}

static int
640
nvkm_device_init(struct nvkm_object *object)
641
{
642 643
	struct nvkm_device *device = (void *)object;
	struct nvkm_object *subdev;
644 645 646 647 648
	int ret, i = 0;

	ret = nvkm_acpi_init(device);
	if (ret)
		goto fail;
649 650 651 652

	for (i = 0; i < NVDEV_SUBDEV_NR; i++) {
		if ((subdev = device->subdev[i])) {
			if (!nv_iclass(subdev, NV_ENGINE_CLASS)) {
653
				ret = nvkm_object_inc(subdev);
654 655 656
				if (ret)
					goto fail;
			} else {
657
				nvkm_subdev_reset(subdev);
658 659 660 661 662 663 664 665 666
			}
		}
	}

	ret = 0;
fail:
	for (--i; ret && i >= 0; i--) {
		if ((subdev = device->subdev[i])) {
			if (!nv_iclass(subdev, NV_ENGINE_CLASS))
667
				nvkm_object_dec(subdev, false);
668 669 670
		}
	}

671 672
	if (ret)
		nvkm_acpi_fini(device, false);
673
	return ret;
674 675
}

676
static void
677
nvkm_device_dtor(struct nvkm_object *object)
678
{
679
	struct nvkm_device *device = (void *)object;
680

681
	nvkm_event_fini(&device->event);
682

683 684 685 686
	mutex_lock(&nv_devices_mutex);
	list_del(&device->head);
	mutex_unlock(&nv_devices_mutex);

687 688
	if (nv_subdev(device)->mmio)
		iounmap(nv_subdev(device)->mmio);
689

690
	nvkm_engine_destroy(&device->engine);
691 692
}

A
Alexandre Courbot 已提交
693
resource_size_t
694
nv_device_resource_start(struct nvkm_device *device, unsigned int bar)
A
Alexandre Courbot 已提交
695 696 697 698 699 700 701 702 703 704 705 706 707 708
{
	if (nv_device_is_pci(device)) {
		return pci_resource_start(device->pdev, bar);
	} else {
		struct resource *res;
		res = platform_get_resource(device->platformdev,
					    IORESOURCE_MEM, bar);
		if (!res)
			return 0;
		return res->start;
	}
}

resource_size_t
709
nv_device_resource_len(struct nvkm_device *device, unsigned int bar)
A
Alexandre Courbot 已提交
710 711 712 713 714 715 716 717 718 719 720 721 722 723
{
	if (nv_device_is_pci(device)) {
		return pci_resource_len(device->pdev, bar);
	} else {
		struct resource *res;
		res = platform_get_resource(device->platformdev,
					    IORESOURCE_MEM, bar);
		if (!res)
			return 0;
		return resource_size(res);
	}
}

int
724
nv_device_get_irq(struct nvkm_device *device, bool stall)
A
Alexandre Courbot 已提交
725 726 727 728 729 730 731 732 733
{
	if (nv_device_is_pci(device)) {
		return device->pdev->irq;
	} else {
		return platform_get_irq_byname(device->platformdev,
					       stall ? "stall" : "nonstall");
	}
}

734 735
static struct nvkm_oclass
nvkm_device_oclass = {
736
	.handle = NV_ENGINE(DEVICE, 0x00),
737 738 739 740
	.ofuncs = &(struct nvkm_ofuncs) {
		.dtor = nvkm_device_dtor,
		.init = nvkm_device_init,
		.fini = nvkm_device_fini,
741 742 743 744
	},
};

int
745 746 747
nvkm_device_create_(void *dev, enum nv_bus_type type, u64 name,
		    const char *sname, const char *cfg, const char *dbg,
		    int length, void **pobject)
748
{
749
	struct nvkm_device *device;
750 751 752 753 754 755 756 757
	int ret = -EEXIST;

	mutex_lock(&nv_devices_mutex);
	list_for_each_entry(device, &nv_devices, head) {
		if (device->handle == name)
			goto done;
	}

758 759
	ret = nvkm_engine_create_(NULL, NULL, &nvkm_device_oclass, true,
				  "DEVICE", "device", length, pobject);
760 761 762 763
	device = *pobject;
	if (ret)
		goto done;

A
Alexandre Courbot 已提交
764
	switch (type) {
765
	case NVKM_BUS_PCI:
A
Alexandre Courbot 已提交
766 767
		device->pdev = dev;
		break;
768
	case NVKM_BUS_PLATFORM:
A
Alexandre Courbot 已提交
769 770 771
		device->platformdev = dev;
		break;
	}
772 773 774 775 776
	device->handle = name;
	device->cfgopt = cfg;
	device->dbgopt = dbg;
	device->name = sname;

777 778
	nv_subdev(device)->debug = nvkm_dbgopt(device->dbgopt, "DEVICE");
	nv_engine(device)->sclass = nvkm_device_sclass;
B
Ben Skeggs 已提交
779
	list_add_tail(&device->head, &nv_devices);
780

781
	ret = nvkm_event_init(&nvkm_device_event_func, 1, 1, &device->event);
782 783 784 785
done:
	mutex_unlock(&nv_devices_mutex);
	return ret;
}