base.c 12.6 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
#include <core/notify.h>
28
#include <core/option.h>
29

30
#include <subdev/bios.h>
31 32 33 34

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

35 36
struct nvkm_device *
nvkm_device_find(u64 name)
37
{
38
	struct nvkm_device *device, *match = NULL;
39 40 41 42 43 44 45 46 47 48 49
	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;
}

50
int
51
nvkm_device_list(u64 *name, int size)
52
{
53
	struct nvkm_device *device;
54 55 56 57 58 59 60 61 62 63
	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;
}

64 65
#include <core/parent.h>

66
struct nvkm_device *
67 68
nv_device(void *obj)
{
69
	struct nvkm_object *device = nv_object(obj);
70

71
	if (device->engine == NULL) {
72 73 74 75 76 77 78 79
		while (device && device->parent) {
			if (nv_mclass(device) == 0x0080) {
				struct {
					struct nvkm_parent base;
					struct nvkm_device *device;
				} *udevice = (void *)device;
				return udevice->device;
			}
80
			device = device->parent;
81
		}
82
	} else {
83
		device = &nv_object(obj)->engine->subdev.object;
84 85
		if (device && device->parent)
			device = device->parent;
86
	}
87
#if CONFIG_NOUVEAU_DEBUG >= NV_DBG_PARANOIA
88
	BUG_ON(!device);
89 90 91 92
#endif
	return (void *)device;
}

93
static int
94 95
nvkm_device_event_ctor(struct nvkm_object *object, void *data, u32 size,
		       struct nvkm_notify *notify)
96 97 98 99 100 101 102 103 104 105 106
{
	if (!WARN_ON(size != 0)) {
		notify->size  = 0;
		notify->types = 1;
		notify->index = 0;
		return 0;
	}
	return -EINVAL;
}

static const struct nvkm_event_func
107 108
nvkm_device_event_func = {
	.ctor = nvkm_device_event_ctor,
109 110
};

111 112
int
nvkm_device_fini(struct nvkm_device *device, bool suspend)
113
{
114
	struct nvkm_object *subdev;
115 116 117 118 119
	int ret, i;

	for (i = NVDEV_SUBDEV_NR - 1; i >= 0; i--) {
		if ((subdev = device->subdev[i])) {
			if (!nv_iclass(subdev, NV_ENGINE_CLASS)) {
120
				ret = nvkm_object_dec(subdev, suspend);
121 122 123 124 125 126
				if (ret && suspend)
					goto fail;
			}
		}
	}

127
	ret = nvkm_acpi_fini(device, suspend);
128 129 130 131
fail:
	for (; ret && i < NVDEV_SUBDEV_NR; i++) {
		if ((subdev = device->subdev[i])) {
			if (!nv_iclass(subdev, NV_ENGINE_CLASS)) {
132
				ret = nvkm_object_inc(subdev);
133 134 135 136 137 138 139 140
				if (ret) {
					/* XXX */
				}
			}
		}
	}

	return ret;
141 142
}

143 144
int
nvkm_device_init(struct nvkm_device *device)
145
{
146
	struct nvkm_object *subdev;
147
	int ret, i = 0, c;
148 149 150 151

	ret = nvkm_acpi_init(device);
	if (ret)
		goto fail;
152

153
	for (i = 0, c = 0; i < NVDEV_SUBDEV_NR; i++) {
154
#define _(s,m) case s: if (device->oclass[s] && !device->subdev[s]) {          \
155
		ret = nvkm_object_old(nv_object(device), NULL,                \
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
				       device->oclass[s], NULL,  (s),          \
				       (struct nvkm_object **)&device->m);     \
		if (ret == -ENODEV) {                                          \
			device->oclass[s] = NULL;                              \
			continue;                                              \
		}                                                              \
		if (ret)                                                       \
			goto fail;                                             \
		device->subdev[s] = (struct nvkm_object *)device->m;           \
} 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);
			continue;
		}
#undef _

		/* 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) {
			struct nvkm_object *subdev = device->subdev[c++];
			if (subdev && !nv_iclass(subdev, NV_ENGINE_CLASS)) {
225
				ret = nvkm_object_inc(subdev);
226 227
				if (ret)
					goto fail;
228 229
			} else
			if (subdev) {
230
				nvkm_subdev_reset(subdev);
231 232 233 234 235 236 237 238 239
			}
		}
	}

	ret = 0;
fail:
	for (--i; ret && i >= 0; i--) {
		if ((subdev = device->subdev[i])) {
			if (!nv_iclass(subdev, NV_ENGINE_CLASS))
240
				nvkm_object_dec(subdev, false);
241 242 243
		}
	}

244 245
	if (ret)
		nvkm_acpi_fini(device, false);
246
	return ret;
247 248
}

A
Alexandre Courbot 已提交
249
resource_size_t
250
nv_device_resource_start(struct nvkm_device *device, unsigned int bar)
A
Alexandre Courbot 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264
{
	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
265
nv_device_resource_len(struct nvkm_device *device, unsigned int bar)
A
Alexandre Courbot 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279
{
	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
280
nv_device_get_irq(struct nvkm_device *device, bool stall)
A
Alexandre Courbot 已提交
281 282 283 284 285 286 287 288 289
{
	if (nv_device_is_pci(device)) {
		return device->pdev->irq;
	} else {
		return platform_get_irq_byname(device->platformdev,
					       stall ? "stall" : "nonstall");
	}
}

290 291 292
static struct nvkm_oclass
nvkm_device_oclass = {
	.ofuncs = &(struct nvkm_ofuncs) {
293 294 295
	},
};

296 297 298 299
void
nvkm_device_del(struct nvkm_device **pdevice)
{
	struct nvkm_device *device = *pdevice;
300
	int i;
301 302
	if (device) {
		mutex_lock(&nv_devices_mutex);
303 304 305 306
		for (i = NVDEV_SUBDEV_NR - 1; i >= 0; i--)
			nvkm_object_ref(NULL, &device->subdev[i]);

		nvkm_event_fini(&device->event);
307 308 309

		if (device->pri)
			iounmap(device->pri);
310 311
		list_del(&device->head);
		mutex_unlock(&nv_devices_mutex);
312 313 314 315 316 317

		nvkm_engine_destroy(&device->engine);
		*pdevice = NULL;
	}
}

318
int
319 320
nvkm_device_new(void *dev, enum nv_bus_type type, u64 name,
		const char *sname, const char *cfg, const char *dbg,
321
		bool detect, bool mmio, u64 subdev_mask,
322
		struct nvkm_device **pdevice)
323
{
324
	struct nvkm_device *device;
325 326 327
	u64 mmio_base, mmio_size;
	u32 boot0, strap;
	void __iomem *map;
328
	int ret = -EEXIST;
329
	int i;
330 331 332 333 334 335 336

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

337 338 339
	ret = nvkm_engine_create(NULL, NULL, &nvkm_device_oclass, true,
				 "DEVICE", "device", &device);
	*pdevice = device;
340 341 342
	if (ret)
		goto done;

A
Alexandre Courbot 已提交
343
	switch (type) {
344
	case NVKM_BUS_PCI:
A
Alexandre Courbot 已提交
345
		device->pdev = dev;
346
		device->dev = &device->pdev->dev;
A
Alexandre Courbot 已提交
347
		break;
348
	case NVKM_BUS_PLATFORM:
A
Alexandre Courbot 已提交
349
		device->platformdev = dev;
350
		device->dev = &device->platformdev->dev;
A
Alexandre Courbot 已提交
351 352
		break;
	}
353 354 355 356 357
	device->handle = name;
	device->cfgopt = cfg;
	device->dbgopt = dbg;
	device->name = sname;

358
	nv_subdev(device)->debug = nvkm_dbgopt(device->dbgopt, "DEVICE");
B
Ben Skeggs 已提交
359
	list_add_tail(&device->head, &nv_devices);
360

361
	ret = nvkm_event_init(&nvkm_device_event_func, 1, 1, &device->event);
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
	if (ret)
		goto done;

	mmio_base = nv_device_resource_start(device, 0);
	mmio_size = nv_device_resource_len(device, 0);

	/* identify the chipset, and determine classes of subdev/engines */
	if (detect) {
		map = ioremap(mmio_base, 0x102000);
		if (ret = -ENOMEM, map == NULL)
			goto done;

		/* switch mmio to cpu's native endianness */
#ifndef __BIG_ENDIAN
		if (ioread32_native(map + 0x000004) != 0x00000000) {
#else
		if (ioread32_native(map + 0x000004) == 0x00000000) {
#endif
			iowrite32_native(0x01000001, map + 0x000004);
			ioread32_native(map);
		}

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

		/* determine chipset and derive architecture from it */
		if ((boot0 & 0x1f000000) > 0) {
			device->chipset = (boot0 & 0x1ff00000) >> 20;
			device->chiprev = (boot0 & 0x000000ff);
			switch (device->chipset & 0x1f0) {
			case 0x010: {
				if (0x461 & (1 << (device->chipset & 0xf)))
					device->card_type = NV_10;
				else
					device->card_type = NV_11;
				device->chiprev = 0x00;
				break;
			}
			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;
			case 0x0c0:
			case 0x0d0: device->card_type = NV_C0; break;
			case 0x0e0:
			case 0x0f0:
			case 0x100: device->card_type = NV_E0; break;
			case 0x110:
			case 0x120: device->card_type = GM100; break;
			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;
		case NV_10:
		case NV_11: ret = nv10_identify(device); break;
		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;
		case NV_C0: ret = gf100_identify(device); break;
		case NV_E0: ret = gk104_identify(device); break;
		case GM100: ret = gm100_identify(device); break;
		default:
			ret = -EINVAL;
			break;
		}

		if (ret) {
			nvdev_error(device, "unknown chipset (%08x)\n", boot0);
			goto done;
		}

		nvdev_info(device, "NVIDIA %s (%08x)\n", device->cname, boot0);

		/* determine frequency of timing crystal */
		if ( device->card_type <= NV_10 || device->chipset < 0x17 ||
		    (device->chipset >= 0x20 && device->chipset < 0x25))
			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;
		}
	} else {
		device->cname = "NULL";
		device->oclass[NVDEV_SUBDEV_VBIOS] = &nvkm_bios_oclass;
	}

	if (mmio) {
		device->pri = ioremap(mmio_base, mmio_size);
		if (!device->pri) {
			nvdev_error(device, "unable to map PRI\n");
			return -ENOMEM;
		}
	}

	/* disable subdevs that aren't required (used by tools) */
	for (i = 0; i < NVDEV_SUBDEV_NR; i++) {
		if (!(subdev_mask & (1ULL << i)))
			device->oclass[i] = NULL;
	}

484 485
	atomic_set(&device->engine.subdev.object.usecount, 2);
	mutex_init(&device->mutex);
486 487 488 489
done:
	mutex_unlock(&nv_devices_mutex);
	return ret;
}