nv50.c 27.8 KB
Newer Older
1
/*
2
 * Copyright 2012 Red Hat Inc.
3
 *
4 5 6 7 8 9
 * 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:
10
 *
11 12
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
13
 *
14 15 16 17 18 19 20
 * 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.
21
 *
22
 * Authors: Ben Skeggs
23 24
 */

25
#include <core/os.h>
26
#include <core/client.h>
27 28 29
#include <core/handle.h>
#include <core/engctx.h>
#include <core/enum.h>
30

31
#include <subdev/fb.h>
32
#include <subdev/mmu.h>
33
#include <subdev/timer.h>
34

35
#include <engine/fifo.h>
36
#include <engine/graph.h>
37

38
#include "nv50.h"
39

40 41 42 43 44
struct nv50_graph_priv {
	struct nouveau_graph base;
	spinlock_t lock;
	u32 size;
};
45

46 47 48
struct nv50_graph_chan {
	struct nouveau_graph_chan base;
};
49

50 51 52 53 54 55 56 57
static u64
nv50_graph_units(struct nouveau_graph *graph)
{
	struct nv50_graph_priv *priv = (void *)graph;

	return nv_rd32(priv, 0x1540);
}

58 59 60
/*******************************************************************************
 * Graphics object classes
 ******************************************************************************/
61

62 63 64 65 66 67 68 69
static int
nv50_graph_object_ctor(struct nouveau_object *parent,
		       struct nouveau_object *engine,
		       struct nouveau_oclass *oclass, void *data, u32 size,
		       struct nouveau_object **pobject)
{
	struct nouveau_gpuobj *obj;
	int ret;
70

71 72 73 74 75
	ret = nouveau_gpuobj_create(parent, engine, oclass, 0, parent,
				    16, 16, 0, &obj);
	*pobject = nv_object(obj);
	if (ret)
		return ret;
76

77 78 79 80
	nv_wo32(obj, 0x00, nv_mclass(obj));
	nv_wo32(obj, 0x04, 0x00000000);
	nv_wo32(obj, 0x08, 0x00000000);
	nv_wo32(obj, 0x0c, 0x00000000);
81 82 83
	return 0;
}

84
static struct nouveau_ofuncs
85 86 87 88 89 90 91 92
nv50_graph_ofuncs = {
	.ctor = nv50_graph_object_ctor,
	.dtor = _nouveau_gpuobj_dtor,
	.init = _nouveau_gpuobj_init,
	.fini = _nouveau_gpuobj_fini,
	.rd32 = _nouveau_gpuobj_rd32,
	.wr32 = _nouveau_gpuobj_wr32,
};
93

94 95 96 97 98 99 100 101 102
static struct nouveau_oclass
nv50_graph_sclass[] = {
	{ 0x0030, &nv50_graph_ofuncs },
	{ 0x502d, &nv50_graph_ofuncs },
	{ 0x5039, &nv50_graph_ofuncs },
	{ 0x5097, &nv50_graph_ofuncs },
	{ 0x50c0, &nv50_graph_ofuncs },
	{}
};
103

104 105 106 107 108 109 110 111 112
static struct nouveau_oclass
nv84_graph_sclass[] = {
	{ 0x0030, &nv50_graph_ofuncs },
	{ 0x502d, &nv50_graph_ofuncs },
	{ 0x5039, &nv50_graph_ofuncs },
	{ 0x50c0, &nv50_graph_ofuncs },
	{ 0x8297, &nv50_graph_ofuncs },
	{}
};
113

114 115 116 117 118 119 120 121 122
static struct nouveau_oclass
nva0_graph_sclass[] = {
	{ 0x0030, &nv50_graph_ofuncs },
	{ 0x502d, &nv50_graph_ofuncs },
	{ 0x5039, &nv50_graph_ofuncs },
	{ 0x50c0, &nv50_graph_ofuncs },
	{ 0x8397, &nv50_graph_ofuncs },
	{}
};
123

124 125 126 127 128 129 130 131 132 133
static struct nouveau_oclass
nva3_graph_sclass[] = {
	{ 0x0030, &nv50_graph_ofuncs },
	{ 0x502d, &nv50_graph_ofuncs },
	{ 0x5039, &nv50_graph_ofuncs },
	{ 0x50c0, &nv50_graph_ofuncs },
	{ 0x8597, &nv50_graph_ofuncs },
	{ 0x85c0, &nv50_graph_ofuncs },
	{}
};
134

135 136 137 138 139 140 141 142 143 144
static struct nouveau_oclass
nvaf_graph_sclass[] = {
	{ 0x0030, &nv50_graph_ofuncs },
	{ 0x502d, &nv50_graph_ofuncs },
	{ 0x5039, &nv50_graph_ofuncs },
	{ 0x50c0, &nv50_graph_ofuncs },
	{ 0x85c0, &nv50_graph_ofuncs },
	{ 0x8697, &nv50_graph_ofuncs },
	{}
};
145

146 147 148
/*******************************************************************************
 * PGRAPH context
 ******************************************************************************/
149 150

static int
151 152 153 154
nv50_graph_context_ctor(struct nouveau_object *parent,
			struct nouveau_object *engine,
			struct nouveau_oclass *oclass, void *data, u32 size,
			struct nouveau_object **pobject)
155
{
156 157
	struct nv50_graph_priv *priv = (void *)engine;
	struct nv50_graph_chan *chan;
158 159
	int ret;

160 161 162 163
	ret = nouveau_graph_context_create(parent, engine, oclass, NULL,
					   priv->size, 0,
					   NVOBJ_FLAG_ZERO_ALLOC, &chan);
	*pobject = nv_object(chan);
164 165 166
	if (ret)
		return ret;

167 168
	nv50_grctx_fill(nv_device(priv), nv_gpuobj(chan));
	return 0;
169 170
}

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
static struct nouveau_oclass
nv50_graph_cclass = {
	.handle = NV_ENGCTX(GR, 0x50),
	.ofuncs = &(struct nouveau_ofuncs) {
		.ctor = nv50_graph_context_ctor,
		.dtor = _nouveau_graph_context_dtor,
		.init = _nouveau_graph_context_init,
		.fini = _nouveau_graph_context_fini,
		.rd32 = _nouveau_graph_context_rd32,
		.wr32 = _nouveau_graph_context_wr32,
	},
};

/*******************************************************************************
 * PGRAPH engine/subdev functions
 ******************************************************************************/

188 189 190 191 192 193 194 195 196 197 198
static const struct nouveau_bitfield nv50_pgraph_status[] = {
	{ 0x00000001, "BUSY" }, /* set when any bit is set */
	{ 0x00000002, "DISPATCH" },
	{ 0x00000004, "UNK2" },
	{ 0x00000008, "UNK3" },
	{ 0x00000010, "UNK4" },
	{ 0x00000020, "UNK5" },
	{ 0x00000040, "M2MF" },
	{ 0x00000080, "UNK7" },
	{ 0x00000100, "CTXPROG" },
	{ 0x00000200, "VFETCH" },
199 200 201 202 203
	{ 0x00000400, "CCACHE_PREGEOM" },
	{ 0x00000800, "STRMOUT_VATTR_POSTGEOM" },
	{ 0x00001000, "VCLIP" },
	{ 0x00002000, "RATTR_APLANE" },
	{ 0x00004000, "TRAST" },
204 205 206
	{ 0x00008000, "CLIPID" },
	{ 0x00010000, "ZCULL" },
	{ 0x00020000, "ENG2D" },
207 208 209 210 211 212
	{ 0x00040000, "RMASK" },
	{ 0x00080000, "TPC_RAST" },
	{ 0x00100000, "TPC_PROP" },
	{ 0x00200000, "TPC_TEX" },
	{ 0x00400000, "TPC_GEOM" },
	{ 0x00800000, "TPC_MP" },
213 214 215 216 217
	{ 0x01000000, "ROP" },
	{}
};

static const char *const nv50_pgraph_vstatus_0[] = {
218 219
	"VFETCH", "CCACHE", "PREGEOM", "POSTGEOM", "VATTR", "STRMOUT", "VCLIP",
	NULL
220 221 222
};

static const char *const nv50_pgraph_vstatus_1[] = {
223
	"TPC_RAST", "TPC_PROP", "TPC_TEX", "TPC_GEOM", "TPC_MP", NULL
224 225 226
};

static const char *const nv50_pgraph_vstatus_2[] = {
227
	"RATTR", "APLANE", "TRAST", "CLIPID", "ZCULL", "ENG2D", "RMASK",
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
	"ROP", NULL
};

static void nouveau_pgraph_vstatus_print(struct nv50_graph_priv *priv, int r,
		const char *const units[], u32 status)
{
	int i;

	nv_error(priv, "PGRAPH_VSTATUS%d: 0x%08x", r, status);

	for (i = 0; units[i] && status; i++) {
		if ((status & 7) == 1)
			pr_cont(" %s", units[i]);
		status >>= 3;
	}
	if (status)
		pr_cont(" (invalid: 0x%x)", status);
	pr_cont("\n");
}

248 249
static int
nv84_graph_tlb_flush(struct nouveau_engine *engine)
250
{
251 252
	struct nouveau_timer *ptimer = nouveau_timer(engine);
	struct nv50_graph_priv *priv = (void *)engine;
253 254 255 256 257
	bool idle, timeout = false;
	unsigned long flags;
	u64 start;
	u32 tmp;

258 259
	spin_lock_irqsave(&priv->lock, flags);
	nv_mask(priv, 0x400500, 0x00000001, 0x00000000);
260

261
	start = ptimer->read(ptimer);
262 263 264
	do {
		idle = true;

265
		for (tmp = nv_rd32(priv, 0x400380); tmp && idle; tmp >>= 3) {
266 267 268 269
			if ((tmp & 7) == 1)
				idle = false;
		}

270
		for (tmp = nv_rd32(priv, 0x400384); tmp && idle; tmp >>= 3) {
271 272 273 274
			if ((tmp & 7) == 1)
				idle = false;
		}

275
		for (tmp = nv_rd32(priv, 0x400388); tmp && idle; tmp >>= 3) {
276 277 278
			if ((tmp & 7) == 1)
				idle = false;
		}
279 280
	} while (!idle &&
		 !(timeout = ptimer->read(ptimer) - start > 2000000000));
281 282

	if (timeout) {
283 284 285 286 287 288 289 290 291 292 293 294 295
		nv_error(priv, "PGRAPH TLB flush idle timeout fail\n");

		tmp = nv_rd32(priv, 0x400700);
		nv_error(priv, "PGRAPH_STATUS  : 0x%08x", tmp);
		nouveau_bitfield_print(nv50_pgraph_status, tmp);
		pr_cont("\n");

		nouveau_pgraph_vstatus_print(priv, 0, nv50_pgraph_vstatus_0,
				nv_rd32(priv, 0x400380));
		nouveau_pgraph_vstatus_print(priv, 1, nv50_pgraph_vstatus_1,
				nv_rd32(priv, 0x400384));
		nouveau_pgraph_vstatus_print(priv, 2, nv50_pgraph_vstatus_2,
				nv_rd32(priv, 0x400388));
296 297 298
	}


299 300 301
	nv_wr32(priv, 0x100c80, 0x00000001);
	if (!nv_wait(priv, 0x100c80, 0x00000001, 0x00000000))
		nv_error(priv, "vm flush timeout\n");
302 303 304
	nv_mask(priv, 0x400500, 0x00000001, 0x00000001);
	spin_unlock_irqrestore(&priv->lock, flags);
	return timeout ? -EBUSY : 0;
305
}
306

307 308 309 310 311 312 313 314
static const struct nouveau_bitfield nv50_mp_exec_errors[] = {
	{ 0x01, "STACK_UNDERFLOW" },
	{ 0x02, "STACK_MISMATCH" },
	{ 0x04, "QUADON_ACTIVE" },
	{ 0x08, "TIMEOUT" },
	{ 0x10, "INVALID_OPCODE" },
	{ 0x20, "PM_OVERFLOW" },
	{ 0x40, "BREAKPOINT" },
315 316 317
	{}
};

318 319 320 321 322 323 324 325 326 327 328 329 330 331
static const struct nouveau_bitfield nv50_mpc_traps[] = {
	{ 0x0000001, "LOCAL_LIMIT_READ" },
	{ 0x0000010, "LOCAL_LIMIT_WRITE" },
	{ 0x0000040, "STACK_LIMIT" },
	{ 0x0000100, "GLOBAL_LIMIT_READ" },
	{ 0x0001000, "GLOBAL_LIMIT_WRITE" },
	{ 0x0010000, "MP0" },
	{ 0x0020000, "MP1" },
	{ 0x0040000, "GLOBAL_LIMIT_RED" },
	{ 0x0400000, "GLOBAL_LIMIT_ATOM" },
	{ 0x4000000, "MP2" },
	{}
};

332 333 334 335 336 337 338 339 340
static const struct nouveau_bitfield nv50_tex_traps[] = {
	{ 0x00000001, "" }, /* any bit set? */
	{ 0x00000002, "FAULT" },
	{ 0x00000004, "STORAGE_TYPE_MISMATCH" },
	{ 0x00000008, "LINEAR_MISMATCH" },
	{ 0x00000020, "WRONG_MEMTYPE" },
	{}
};

341
static const struct nouveau_bitfield nv50_graph_trap_m2mf[] = {
342 343 344 345 346 347
	{ 0x00000001, "NOTIFY" },
	{ 0x00000002, "IN" },
	{ 0x00000004, "OUT" },
	{}
};

348
static const struct nouveau_bitfield nv50_graph_trap_vfetch[] = {
349 350 351 352
	{ 0x00000001, "FAULT" },
	{}
};

353
static const struct nouveau_bitfield nv50_graph_trap_strmout[] = {
354 355 356 357
	{ 0x00000001, "FAULT" },
	{}
};

358
static const struct nouveau_bitfield nv50_graph_trap_ccache[] = {
359 360 361 362 363
	{ 0x00000001, "FAULT" },
	{}
};

/* There must be a *lot* of these. Will take some time to gather them up. */
364
const struct nouveau_enum nv50_data_error_names[] = {
365
	{ 0x00000003, "INVALID_OPERATION", NULL },
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
	{ 0x00000004, "INVALID_VALUE", NULL },
	{ 0x00000005, "INVALID_ENUM", NULL },
	{ 0x00000008, "INVALID_OBJECT", NULL },
	{ 0x00000009, "READ_ONLY_OBJECT", NULL },
	{ 0x0000000a, "SUPERVISOR_OBJECT", NULL },
	{ 0x0000000b, "INVALID_ADDRESS_ALIGNMENT", NULL },
	{ 0x0000000c, "INVALID_BITFIELD", NULL },
	{ 0x0000000d, "BEGIN_END_ACTIVE", NULL },
	{ 0x0000000e, "SEMANTIC_COLOR_BACK_OVER_LIMIT", NULL },
	{ 0x0000000f, "VIEWPORT_ID_NEEDS_GP", NULL },
	{ 0x00000010, "RT_DOUBLE_BIND", NULL },
	{ 0x00000011, "RT_TYPES_MISMATCH", NULL },
	{ 0x00000012, "RT_LINEAR_WITH_ZETA", NULL },
	{ 0x00000015, "FP_TOO_FEW_REGS", NULL },
	{ 0x00000016, "ZETA_FORMAT_CSAA_MISMATCH", NULL },
	{ 0x00000017, "RT_LINEAR_WITH_MSAA", NULL },
	{ 0x00000018, "FP_INTERPOLANT_START_OVER_LIMIT", NULL },
	{ 0x00000019, "SEMANTIC_LAYER_OVER_LIMIT", NULL },
	{ 0x0000001a, "RT_INVALID_ALIGNMENT", NULL },
	{ 0x0000001b, "SAMPLER_OVER_LIMIT", NULL },
	{ 0x0000001c, "TEXTURE_OVER_LIMIT", NULL },
	{ 0x0000001e, "GP_TOO_MANY_OUTPUTS", NULL },
	{ 0x0000001f, "RT_BPP128_WITH_MS8", NULL },
	{ 0x00000021, "Z_OUT_OF_BOUNDS", NULL },
	{ 0x00000023, "XY_OUT_OF_BOUNDS", NULL },
391
	{ 0x00000024, "VP_ZERO_INPUTS", NULL },
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
	{ 0x00000027, "CP_MORE_PARAMS_THAN_SHARED", NULL },
	{ 0x00000028, "CP_NO_REG_SPACE_STRIPED", NULL },
	{ 0x00000029, "CP_NO_REG_SPACE_PACKED", NULL },
	{ 0x0000002a, "CP_NOT_ENOUGH_WARPS", NULL },
	{ 0x0000002b, "CP_BLOCK_SIZE_MISMATCH", NULL },
	{ 0x0000002c, "CP_NOT_ENOUGH_LOCAL_WARPS", NULL },
	{ 0x0000002d, "CP_NOT_ENOUGH_STACK_WARPS", NULL },
	{ 0x0000002e, "CP_NO_BLOCKDIM_LATCH", NULL },
	{ 0x00000031, "ENG2D_FORMAT_MISMATCH", NULL },
	{ 0x0000003f, "PRIMITIVE_ID_NEEDS_GP", NULL },
	{ 0x00000044, "SEMANTIC_VIEWPORT_OVER_LIMIT", NULL },
	{ 0x00000045, "SEMANTIC_COLOR_FRONT_OVER_LIMIT", NULL },
	{ 0x00000046, "LAYER_ID_NEEDS_GP", NULL },
	{ 0x00000047, "SEMANTIC_CLIP_OVER_LIMIT", NULL },
	{ 0x00000048, "SEMANTIC_PTSZ_OVER_LIMIT", NULL },
407 408 409
	{}
};

410
static const struct nouveau_bitfield nv50_graph_intr_name[] = {
411 412 413 414 415 416 417 418 419 420 421 422 423
	{ 0x00000001, "NOTIFY" },
	{ 0x00000002, "COMPUTE_QUERY" },
	{ 0x00000010, "ILLEGAL_MTHD" },
	{ 0x00000020, "ILLEGAL_CLASS" },
	{ 0x00000040, "DOUBLE_NOTIFY" },
	{ 0x00001000, "CONTEXT_SWITCH" },
	{ 0x00010000, "BUFFER_NOTIFY" },
	{ 0x00100000, "DATA_ERROR" },
	{ 0x00200000, "TRAP" },
	{ 0x01000000, "SINGLE_STEP" },
	{}
};

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
static const struct nouveau_bitfield nv50_graph_trap_prop[] = {
	{ 0x00000004, "SURF_WIDTH_OVERRUN" },
	{ 0x00000008, "SURF_HEIGHT_OVERRUN" },
	{ 0x00000010, "DST2D_FAULT" },
	{ 0x00000020, "ZETA_FAULT" },
	{ 0x00000040, "RT_FAULT" },
	{ 0x00000080, "CUDA_FAULT" },
	{ 0x00000100, "DST2D_STORAGE_TYPE_MISMATCH" },
	{ 0x00000200, "ZETA_STORAGE_TYPE_MISMATCH" },
	{ 0x00000400, "RT_STORAGE_TYPE_MISMATCH" },
	{ 0x00000800, "DST2D_LINEAR_MISMATCH" },
	{ 0x00001000, "RT_LINEAR_MISMATCH" },
	{}
};

static void
nv50_priv_prop_trap(struct nv50_graph_priv *priv,
		    u32 ustatus_addr, u32 ustatus, u32 tp)
{
	u32 e0c = nv_rd32(priv, ustatus_addr + 0x04);
	u32 e10 = nv_rd32(priv, ustatus_addr + 0x08);
	u32 e14 = nv_rd32(priv, ustatus_addr + 0x0c);
	u32 e18 = nv_rd32(priv, ustatus_addr + 0x10);
	u32 e1c = nv_rd32(priv, ustatus_addr + 0x14);
	u32 e20 = nv_rd32(priv, ustatus_addr + 0x18);
	u32 e24 = nv_rd32(priv, ustatus_addr + 0x1c);

	/* CUDA memory: l[], g[] or stack. */
	if (ustatus & 0x00000080) {
		if (e18 & 0x80000000) {
			/* g[] read fault? */
			nv_error(priv, "TRAP_PROP - TP %d - CUDA_FAULT - Global read fault at address %02x%08x\n",
					 tp, e14, e10 | ((e18 >> 24) & 0x1f));
			e18 &= ~0x1f000000;
		} else if (e18 & 0xc) {
			/* g[] write fault? */
			nv_error(priv, "TRAP_PROP - TP %d - CUDA_FAULT - Global write fault at address %02x%08x\n",
				 tp, e14, e10 | ((e18 >> 7) & 0x1f));
			e18 &= ~0x00000f80;
		} else {
			nv_error(priv, "TRAP_PROP - TP %d - Unknown CUDA fault at address %02x%08x\n",
				 tp, e14, e10);
		}
		ustatus &= ~0x00000080;
	}
	if (ustatus) {
		nv_error(priv, "TRAP_PROP - TP %d -", tp);
		nouveau_bitfield_print(nv50_graph_trap_prop, ustatus);
		pr_cont(" - Address %02x%08x\n", e14, e10);
	}
	nv_error(priv, "TRAP_PROP - TP %d - e0c: %08x, e18: %08x, e1c: %08x, e20: %08x, e24: %08x\n",
		 tp, e0c, e18, e1c, e20, e24);
}

478
static void
479
nv50_priv_mp_trap(struct nv50_graph_priv *priv, int tpid, int display)
480
{
481 482
	u32 units = nv_rd32(priv, 0x1540);
	u32 addr, mp10, status, pc, oplow, ophigh;
483 484 485 486 487
	int i;
	int mps = 0;
	for (i = 0; i < 4; i++) {
		if (!(units & 1 << (i+24)))
			continue;
488
		if (nv_device(priv)->chipset < 0xa0)
489 490 491
			addr = 0x408200 + (tpid << 12) + (i << 7);
		else
			addr = 0x408100 + (tpid << 11) + (i << 7);
492 493
		mp10 = nv_rd32(priv, addr + 0x10);
		status = nv_rd32(priv, addr + 0x14);
494 495 496
		if (!status)
			continue;
		if (display) {
497 498 499 500 501
			nv_rd32(priv, addr + 0x20);
			pc = nv_rd32(priv, addr + 0x24);
			oplow = nv_rd32(priv, addr + 0x70);
			ophigh = nv_rd32(priv, addr + 0x74);
			nv_error(priv, "TRAP_MP_EXEC - "
502 503
					"TP %d MP %d:", tpid, i);
			nouveau_bitfield_print(nv50_mp_exec_errors, status);
M
Marcin Slusarz 已提交
504
			pr_cont(" at %06x warp %d, opcode %08x %08x\n",
505 506 507
					pc&0xffffff, pc >> 24,
					oplow, ophigh);
		}
508 509
		nv_wr32(priv, addr + 0x10, mp10);
		nv_wr32(priv, addr + 0x14, 0);
510 511 512
		mps++;
	}
	if (!mps && display)
513
		nv_error(priv, "TRAP_MP_EXEC - TP %d: "
514 515 516 517
				"No MPs claiming errors?\n", tpid);
}

static void
518 519
nv50_priv_tp_trap(struct nv50_graph_priv *priv, int type, u32 ustatus_old,
		u32 ustatus_new, int display, const char *name)
520 521
{
	int tps = 0;
522
	u32 units = nv_rd32(priv, 0x1540);
523
	int i, r;
524
	u32 ustatus_addr, ustatus;
525 526 527
	for (i = 0; i < 16; i++) {
		if (!(units & (1 << i)))
			continue;
528
		if (nv_device(priv)->chipset < 0xa0)
529 530 531
			ustatus_addr = ustatus_old + (i << 12);
		else
			ustatus_addr = ustatus_new + (i << 11);
532
		ustatus = nv_rd32(priv, ustatus_addr) & 0x7fffffff;
533 534 535 536 537 538
		if (!ustatus)
			continue;
		tps++;
		switch (type) {
		case 6: /* texture error... unknown for now */
			if (display) {
539
				nv_error(priv, "magic set %d:\n", i);
540
				for (r = ustatus_addr + 4; r <= ustatus_addr + 0x10; r += 4)
541 542
					nv_error(priv, "\t0x%08x: 0x%08x\n", r,
						nv_rd32(priv, r));
543 544 545 546 547 548 549
				if (ustatus) {
					nv_error(priv, "%s - TP%d:", name, i);
					nouveau_bitfield_print(nv50_tex_traps,
							       ustatus);
					pr_cont("\n");
					ustatus = 0;
				}
550 551 552
			}
			break;
		case 7: /* MP error */
553
			if (ustatus & 0x04030000) {
554
				nv50_priv_mp_trap(priv, i, display);
555
				ustatus &= ~0x04030000;
556
			}
557
			if (ustatus && display) {
558
				nv_error(priv, "%s - TP%d:", name, i);
559 560 561 562
				nouveau_bitfield_print(nv50_mpc_traps, ustatus);
				pr_cont("\n");
				ustatus = 0;
			}
563
			break;
564 565 566 567 568
		case 8: /* PROP error */
			if (display)
				nv50_priv_prop_trap(
						priv, ustatus_addr, ustatus, i);
			ustatus = 0;
569 570 571 572
			break;
		}
		if (ustatus) {
			if (display)
573
				nv_error(priv, "%s - TP%d: Unhandled ustatus 0x%08x\n", name, i, ustatus);
574
		}
575
		nv_wr32(priv, ustatus_addr, 0xc0000000);
576 577 578
	}

	if (!tps && display)
579
		nv_warn(priv, "%s - No TPs claiming errors?\n", name);
580 581 582
}

static int
583
nv50_graph_trap_handler(struct nv50_graph_priv *priv, u32 display,
584
			int chid, u64 inst, struct nouveau_object *engctx)
585
{
586
	u32 status = nv_rd32(priv, 0x400108);
587 588 589
	u32 ustatus;

	if (!status && display) {
590
		nv_error(priv, "TRAP: no units reporting traps?\n");
591 592 593 594 595 596 597
		return 1;
	}

	/* DISPATCH: Relays commands to other units and handles NOTIFY,
	 * COND, QUERY. If you get a trap from it, the command is still stuck
	 * in DISPATCH and you need to do something about it. */
	if (status & 0x001) {
598
		ustatus = nv_rd32(priv, 0x400804) & 0x7fffffff;
599
		if (!ustatus && display) {
600
			nv_error(priv, "TRAP_DISPATCH - no ustatus?\n");
601 602
		}

603
		nv_wr32(priv, 0x400500, 0x00000000);
604 605 606

		/* Known to be triggered by screwed up NOTIFY and COND... */
		if (ustatus & 0x00000001) {
607
			u32 addr = nv_rd32(priv, 0x400808);
608 609
			u32 subc = (addr & 0x00070000) >> 16;
			u32 mthd = (addr & 0x00001ffc);
610 611 612 613
			u32 datal = nv_rd32(priv, 0x40080c);
			u32 datah = nv_rd32(priv, 0x400810);
			u32 class = nv_rd32(priv, 0x400814);
			u32 r848 = nv_rd32(priv, 0x400848);
614

615
			nv_error(priv, "TRAP DISPATCH_FAULT\n");
616
			if (display && (addr & 0x80000000)) {
617 618 619 620 621
				nv_error(priv,
					 "ch %d [0x%010llx %s] subc %d class 0x%04x mthd 0x%04x data 0x%08x%08x 400808 0x%08x 400848 0x%08x\n",
					 chid, inst,
					 nouveau_client_name(engctx), subc,
					 class, mthd, datah, datal, addr, r848);
622 623
			} else
			if (display) {
624
				nv_error(priv, "no stuck command?\n");
625 626
			}

627 628 629
			nv_wr32(priv, 0x400808, 0);
			nv_wr32(priv, 0x4008e8, nv_rd32(priv, 0x4008e8) & 3);
			nv_wr32(priv, 0x400848, 0);
630 631 632 633
			ustatus &= ~0x00000001;
		}

		if (ustatus & 0x00000002) {
634
			u32 addr = nv_rd32(priv, 0x40084c);
635 636
			u32 subc = (addr & 0x00070000) >> 16;
			u32 mthd = (addr & 0x00001ffc);
637 638
			u32 data = nv_rd32(priv, 0x40085c);
			u32 class = nv_rd32(priv, 0x400814);
639

640
			nv_error(priv, "TRAP DISPATCH_QUERY\n");
641
			if (display && (addr & 0x80000000)) {
642 643 644 645 646
				nv_error(priv,
					 "ch %d [0x%010llx %s] subc %d class 0x%04x mthd 0x%04x data 0x%08x 40084c 0x%08x\n",
					 chid, inst,
					 nouveau_client_name(engctx), subc,
					 class, mthd, data, addr);
647 648
			} else
			if (display) {
649
				nv_error(priv, "no stuck command?\n");
650 651
			}

652
			nv_wr32(priv, 0x40084c, 0);
653 654 655 656
			ustatus &= ~0x00000002;
		}

		if (ustatus && display) {
657
			nv_error(priv, "TRAP_DISPATCH (unknown "
658 659 660
				      "0x%08x)\n", ustatus);
		}

661 662
		nv_wr32(priv, 0x400804, 0xc0000000);
		nv_wr32(priv, 0x400108, 0x001);
663 664 665 666 667 668 669
		status &= ~0x001;
		if (!status)
			return 0;
	}

	/* M2MF: Memory to memory copy engine. */
	if (status & 0x002) {
670
		u32 ustatus = nv_rd32(priv, 0x406800) & 0x7fffffff;
671
		if (display) {
672
			nv_error(priv, "TRAP_M2MF");
673
			nouveau_bitfield_print(nv50_graph_trap_m2mf, ustatus);
M
Marcin Slusarz 已提交
674
			pr_cont("\n");
675 676 677
			nv_error(priv, "TRAP_M2MF %08x %08x %08x %08x\n",
				nv_rd32(priv, 0x406804), nv_rd32(priv, 0x406808),
				nv_rd32(priv, 0x40680c), nv_rd32(priv, 0x406810));
678 679 680 681

		}

		/* No sane way found yet -- just reset the bugger. */
682 683 684 685
		nv_wr32(priv, 0x400040, 2);
		nv_wr32(priv, 0x400040, 0);
		nv_wr32(priv, 0x406800, 0xc0000000);
		nv_wr32(priv, 0x400108, 0x002);
686 687 688 689 690
		status &= ~0x002;
	}

	/* VFETCH: Fetches data from vertex buffers. */
	if (status & 0x004) {
691
		u32 ustatus = nv_rd32(priv, 0x400c04) & 0x7fffffff;
692
		if (display) {
693
			nv_error(priv, "TRAP_VFETCH");
694
			nouveau_bitfield_print(nv50_graph_trap_vfetch, ustatus);
M
Marcin Slusarz 已提交
695
			pr_cont("\n");
696 697 698
			nv_error(priv, "TRAP_VFETCH %08x %08x %08x %08x\n",
				nv_rd32(priv, 0x400c00), nv_rd32(priv, 0x400c08),
				nv_rd32(priv, 0x400c0c), nv_rd32(priv, 0x400c10));
699 700
		}

701 702
		nv_wr32(priv, 0x400c04, 0xc0000000);
		nv_wr32(priv, 0x400108, 0x004);
703 704 705 706 707
		status &= ~0x004;
	}

	/* STRMOUT: DirectX streamout / OpenGL transform feedback. */
	if (status & 0x008) {
708
		ustatus = nv_rd32(priv, 0x401800) & 0x7fffffff;
709
		if (display) {
710
			nv_error(priv, "TRAP_STRMOUT");
711
			nouveau_bitfield_print(nv50_graph_trap_strmout, ustatus);
M
Marcin Slusarz 已提交
712
			pr_cont("\n");
713 714 715
			nv_error(priv, "TRAP_STRMOUT %08x %08x %08x %08x\n",
				nv_rd32(priv, 0x401804), nv_rd32(priv, 0x401808),
				nv_rd32(priv, 0x40180c), nv_rd32(priv, 0x401810));
716 717 718 719

		}

		/* No sane way found yet -- just reset the bugger. */
720 721 722 723
		nv_wr32(priv, 0x400040, 0x80);
		nv_wr32(priv, 0x400040, 0);
		nv_wr32(priv, 0x401800, 0xc0000000);
		nv_wr32(priv, 0x400108, 0x008);
724 725 726 727 728
		status &= ~0x008;
	}

	/* CCACHE: Handles code and c[] caches and fills them. */
	if (status & 0x010) {
729
		ustatus = nv_rd32(priv, 0x405018) & 0x7fffffff;
730
		if (display) {
731
			nv_error(priv, "TRAP_CCACHE");
732
			nouveau_bitfield_print(nv50_graph_trap_ccache, ustatus);
M
Marcin Slusarz 已提交
733
			pr_cont("\n");
734
			nv_error(priv, "TRAP_CCACHE %08x %08x %08x %08x"
735
				     " %08x %08x %08x\n",
736 737 738 739
				nv_rd32(priv, 0x405000), nv_rd32(priv, 0x405004),
				nv_rd32(priv, 0x405008), nv_rd32(priv, 0x40500c),
				nv_rd32(priv, 0x405010), nv_rd32(priv, 0x405014),
				nv_rd32(priv, 0x40501c));
740 741 742

		}

743 744
		nv_wr32(priv, 0x405018, 0xc0000000);
		nv_wr32(priv, 0x400108, 0x010);
745 746 747 748 749 750 751
		status &= ~0x010;
	}

	/* Unknown, not seen yet... 0x402000 is the only trap status reg
	 * remaining, so try to handle it anyway. Perhaps related to that
	 * unknown DMA slot on tesla? */
	if (status & 0x20) {
752
		ustatus = nv_rd32(priv, 0x402000) & 0x7fffffff;
753
		if (display)
754 755
			nv_error(priv, "TRAP_UNKC04 0x%08x\n", ustatus);
		nv_wr32(priv, 0x402000, 0xc0000000);
756 757 758 759 760
		/* no status modifiction on purpose */
	}

	/* TEXTURE: CUDA texturing units */
	if (status & 0x040) {
761 762 763
		nv50_priv_tp_trap(priv, 6, 0x408900, 0x408600, display,
				    "TRAP_TEXTURE");
		nv_wr32(priv, 0x400108, 0x040);
764 765 766 767 768
		status &= ~0x040;
	}

	/* MP: CUDA execution engines. */
	if (status & 0x080) {
769 770 771
		nv50_priv_tp_trap(priv, 7, 0x408314, 0x40831c, display,
				    "TRAP_MP");
		nv_wr32(priv, 0x400108, 0x080);
772 773 774
		status &= ~0x080;
	}

775
	/* PROP:  Handles TP-initiated uncached memory accesses:
776 777
	 * l[], g[], stack, 2d surfaces, render targets. */
	if (status & 0x100) {
778
		nv50_priv_tp_trap(priv, 8, 0x408e08, 0x408708, display,
779
				    "TRAP_PROP");
780
		nv_wr32(priv, 0x400108, 0x100);
781 782 783 784 785
		status &= ~0x100;
	}

	if (status) {
		if (display)
786 787
			nv_error(priv, "TRAP: unknown 0x%08x\n", status);
		nv_wr32(priv, 0x400108, status);
788 789 790 791 792
	}

	return 1;
}

793 794
static void
nv50_graph_intr(struct nouveau_subdev *subdev)
795
{
796
	struct nouveau_fifo *pfifo = nouveau_fifo(subdev);
797
	struct nouveau_engine *engine = nv_engine(subdev);
798
	struct nouveau_object *engctx;
799
	struct nouveau_handle *handle = NULL;
800
	struct nv50_graph_priv *priv = (void *)subdev;
801
	u32 stat = nv_rd32(priv, 0x400100);
802
	u32 inst = nv_rd32(priv, 0x40032c) & 0x0fffffff;
803 804 805 806 807
	u32 addr = nv_rd32(priv, 0x400704);
	u32 subc = (addr & 0x00070000) >> 16;
	u32 mthd = (addr & 0x00001ffc);
	u32 data = nv_rd32(priv, 0x400708);
	u32 class = nv_rd32(priv, 0x400814);
808
	u32 show = stat, show_bitfield = stat;
809 810 811 812
	int chid;

	engctx = nouveau_engctx_get(engine, inst);
	chid   = pfifo->chid(pfifo, engctx);
813 814

	if (stat & 0x00000010) {
815
		handle = nouveau_handle_get_class(engctx, class);
816 817
		if (handle && !nv_call(handle->object, mthd, data))
			show &= ~0x00000010;
818
		nouveau_handle_put(handle);
819
	}
820

821 822 823 824
	if (show & 0x00100000) {
		u32 ecode = nv_rd32(priv, 0x400110);
		nv_error(priv, "DATA_ERROR ");
		nouveau_enum_print(nv50_data_error_names, ecode);
M
Marcin Slusarz 已提交
825
		pr_cont("\n");
826
		show_bitfield &= ~0x00100000;
827
	}
828

829
	if (stat & 0x00200000) {
830 831
		if (!nv50_graph_trap_handler(priv, show, chid, (u64)inst << 12,
				engctx))
832
			show &= ~0x00200000;
833
		show_bitfield &= ~0x00200000;
834 835 836 837 838 839
	}

	nv_wr32(priv, 0x400100, stat);
	nv_wr32(priv, 0x400500, 0x00010001);

	if (show) {
840 841 842 843 844 845
		show &= show_bitfield;
		if (show) {
			nv_error(priv, "%s", "");
			nouveau_bitfield_print(nv50_graph_intr_name, show);
			pr_cont("\n");
		}
846 847 848 849
		nv_error(priv,
			 "ch %d [0x%010llx %s] subc %d class 0x%04x mthd 0x%04x data 0x%08x\n",
			 chid, (u64)inst << 12, nouveau_client_name(engctx),
			 subc, class, mthd, data);
850
	}
851 852 853

	if (nv_rd32(priv, 0x400824) & (1 << 31))
		nv_wr32(priv, 0x400824, nv_rd32(priv, 0x400824) & ~(1 << 31));
854 855

	nouveau_engctx_put(engctx);
856 857
}

858 859 860 861
static int
nv50_graph_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
	       struct nouveau_oclass *oclass, void *data, u32 size,
	       struct nouveau_object **pobject)
862
{
863 864
	struct nv50_graph_priv *priv;
	int ret;
865

866 867 868 869
	ret = nouveau_graph_create(parent, engine, oclass, true, &priv);
	*pobject = nv_object(priv);
	if (ret)
		return ret;
870

871 872 873
	nv_subdev(priv)->unit = 0x00201000;
	nv_subdev(priv)->intr = nv50_graph_intr;
	nv_engine(priv)->cclass = &nv50_graph_cclass;
874

875 876
	priv->base.units = nv50_graph_units;

877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
	switch (nv_device(priv)->chipset) {
	case 0x50:
		nv_engine(priv)->sclass = nv50_graph_sclass;
		break;
	case 0x84:
	case 0x86:
	case 0x92:
	case 0x94:
	case 0x96:
	case 0x98:
		nv_engine(priv)->sclass = nv84_graph_sclass;
		break;
	case 0xa0:
	case 0xaa:
	case 0xac:
		nv_engine(priv)->sclass = nva0_graph_sclass;
		break;
	case 0xa3:
	case 0xa5:
	case 0xa8:
		nv_engine(priv)->sclass = nva3_graph_sclass;
		break;
	case 0xaf:
		nv_engine(priv)->sclass = nvaf_graph_sclass;
		break;
902

D
Damien Lespiau 已提交
903
	}
904

905 906 907
	/* unfortunate hw bug workaround... */
	if (nv_device(priv)->chipset != 0x50 &&
	    nv_device(priv)->chipset != 0xac)
908
		nv_engine(priv)->tlb_flush = nv84_graph_tlb_flush;
909

910 911
	spin_lock_init(&priv->lock);
	return 0;
912
}
913

914 915
static int
nv50_graph_init(struct nouveau_object *object)
916
{
917 918
	struct nv50_graph_priv *priv = (void *)object;
	int ret, units, i;
919

920 921 922
	ret = nouveau_graph_init(&priv->base);
	if (ret)
		return ret;
923

924 925
	/* NV_PGRAPH_DEBUG_3_HW_CTX_SWITCH_ENABLED */
	nv_wr32(priv, 0x40008c, 0x00000004);
926

927 928 929 930 931 932 933 934 935 936 937 938
	/* reset/enable traps and interrupts */
	nv_wr32(priv, 0x400804, 0xc0000000);
	nv_wr32(priv, 0x406800, 0xc0000000);
	nv_wr32(priv, 0x400c04, 0xc0000000);
	nv_wr32(priv, 0x401800, 0xc0000000);
	nv_wr32(priv, 0x405018, 0xc0000000);
	nv_wr32(priv, 0x402000, 0xc0000000);

	units = nv_rd32(priv, 0x001540);
	for (i = 0; i < 16; i++) {
		if (!(units & (1 << i)))
			continue;
939

940 941 942 943 944 945 946 947 948
		if (nv_device(priv)->chipset < 0xa0) {
			nv_wr32(priv, 0x408900 + (i << 12), 0xc0000000);
			nv_wr32(priv, 0x408e08 + (i << 12), 0xc0000000);
			nv_wr32(priv, 0x408314 + (i << 12), 0xc0000000);
		} else {
			nv_wr32(priv, 0x408600 + (i << 11), 0xc0000000);
			nv_wr32(priv, 0x408708 + (i << 11), 0xc0000000);
			nv_wr32(priv, 0x40831c + (i << 11), 0xc0000000);
		}
949 950
	}

951 952 953 954 955
	nv_wr32(priv, 0x400108, 0xffffffff);
	nv_wr32(priv, 0x400138, 0xffffffff);
	nv_wr32(priv, 0x400100, 0xffffffff);
	nv_wr32(priv, 0x40013c, 0xffffffff);
	nv_wr32(priv, 0x400500, 0x00010001);
956

957 958 959 960
	/* upload context program, initialise ctxctl defaults */
	ret = nv50_grctx_init(nv_device(priv), &priv->size);
	if (ret)
		return ret;
961

962 963 964 965 966
	nv_wr32(priv, 0x400824, 0x00000000);
	nv_wr32(priv, 0x400828, 0x00000000);
	nv_wr32(priv, 0x40082c, 0x00000000);
	nv_wr32(priv, 0x400830, 0x00000000);
	nv_wr32(priv, 0x40032c, 0x00000000);
967
	nv_wr32(priv, 0x400330, 0x00000000);
968

969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
	/* some unknown zcull magic */
	switch (nv_device(priv)->chipset & 0xf0) {
	case 0x50:
	case 0x80:
	case 0x90:
		nv_wr32(priv, 0x402ca8, 0x00000800);
		break;
	case 0xa0:
	default:
		if (nv_device(priv)->chipset == 0xa0 ||
		    nv_device(priv)->chipset == 0xaa ||
		    nv_device(priv)->chipset == 0xac) {
			nv_wr32(priv, 0x402ca8, 0x00000802);
		} else {
			nv_wr32(priv, 0x402cc0, 0x00000000);
			nv_wr32(priv, 0x402ca8, 0x00000002);
985 986
		}

987 988
		break;
	}
989

990 991
	/* zero out zcull regions */
	for (i = 0; i < 8; i++) {
992 993 994 995
		nv_wr32(priv, 0x402c20 + (i * 0x10), 0x00000000);
		nv_wr32(priv, 0x402c24 + (i * 0x10), 0x00000000);
		nv_wr32(priv, 0x402c28 + (i * 0x10), 0x00000000);
		nv_wr32(priv, 0x402c2c + (i * 0x10), 0x00000000);
996
	}
997 998
	return 0;
}
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

struct nouveau_oclass
nv50_graph_oclass = {
	.handle = NV_ENGINE(GR, 0x50),
	.ofuncs = &(struct nouveau_ofuncs) {
		.ctor = nv50_graph_ctor,
		.dtor = _nouveau_graph_dtor,
		.init = nv50_graph_init,
		.fini = _nouveau_graph_fini,
	},
};