gf100.c 47.2 KB
Newer Older
1
/*
2
 * Copyright 2012 Red Hat Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * 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 26 27 28 29 30 31 32 33 34 35 36 37
#include "gf100.h"
#include "ctxgf100.h"
#include "fuc/os.h"

#include <core/client.h>
#include <core/handle.h>
#include <core/option.h>
#include <engine/fifo.h>
#include <subdev/fb.h>
#include <subdev/mc.h>
#include <subdev/timer.h>

#include <nvif/class.h>
#include <nvif/unpack.h>
38

39 40 41 42 43
/*******************************************************************************
 * Zero Bandwidth Clear
 ******************************************************************************/

static void
B
Ben Skeggs 已提交
44
gf100_gr_zbc_clear_color(struct gf100_gr *gr, int zbc)
45
{
46
	struct nvkm_device *device = gr->base.engine.subdev.device;
B
Ben Skeggs 已提交
47
	if (gr->zbc_color[zbc].format) {
48 49 50 51 52 53 54 55
		nvkm_wr32(device, 0x405804, gr->zbc_color[zbc].ds[0]);
		nvkm_wr32(device, 0x405808, gr->zbc_color[zbc].ds[1]);
		nvkm_wr32(device, 0x40580c, gr->zbc_color[zbc].ds[2]);
		nvkm_wr32(device, 0x405810, gr->zbc_color[zbc].ds[3]);
	}
	nvkm_wr32(device, 0x405814, gr->zbc_color[zbc].format);
	nvkm_wr32(device, 0x405820, zbc);
	nvkm_wr32(device, 0x405824, 0x00000004); /* TRIGGER | WRITE | COLOR */
56 57 58
}

static int
B
Ben Skeggs 已提交
59
gf100_gr_zbc_color_get(struct gf100_gr *gr, int format,
60
		       const u32 ds[4], const u32 l2[4])
61
{
B
Ben Skeggs 已提交
62
	struct nvkm_ltc *ltc = nvkm_ltc(gr);
63 64 65
	int zbc = -ENOSPC, i;

	for (i = ltc->zbc_min; i <= ltc->zbc_max; i++) {
B
Ben Skeggs 已提交
66 67
		if (gr->zbc_color[i].format) {
			if (gr->zbc_color[i].format != format)
68
				continue;
B
Ben Skeggs 已提交
69 70
			if (memcmp(gr->zbc_color[i].ds, ds, sizeof(
				   gr->zbc_color[i].ds)))
71
				continue;
B
Ben Skeggs 已提交
72 73
			if (memcmp(gr->zbc_color[i].l2, l2, sizeof(
				   gr->zbc_color[i].l2))) {
74 75 76 77 78 79 80 81 82
				WARN_ON(1);
				return -EINVAL;
			}
			return i;
		} else {
			zbc = (zbc < 0) ? i : zbc;
		}
	}

83 84 85
	if (zbc < 0)
		return zbc;

B
Ben Skeggs 已提交
86 87 88
	memcpy(gr->zbc_color[zbc].ds, ds, sizeof(gr->zbc_color[zbc].ds));
	memcpy(gr->zbc_color[zbc].l2, l2, sizeof(gr->zbc_color[zbc].l2));
	gr->zbc_color[zbc].format = format;
89
	ltc->zbc_color_get(ltc, zbc, l2);
B
Ben Skeggs 已提交
90
	gf100_gr_zbc_clear_color(gr, zbc);
91 92 93 94
	return zbc;
}

static void
B
Ben Skeggs 已提交
95
gf100_gr_zbc_clear_depth(struct gf100_gr *gr, int zbc)
96
{
97
	struct nvkm_device *device = gr->base.engine.subdev.device;
B
Ben Skeggs 已提交
98
	if (gr->zbc_depth[zbc].format)
99 100 101 102
		nvkm_wr32(device, 0x405818, gr->zbc_depth[zbc].ds);
	nvkm_wr32(device, 0x40581c, gr->zbc_depth[zbc].format);
	nvkm_wr32(device, 0x405820, zbc);
	nvkm_wr32(device, 0x405824, 0x00000005); /* TRIGGER | WRITE | DEPTH */
103 104 105
}

static int
B
Ben Skeggs 已提交
106
gf100_gr_zbc_depth_get(struct gf100_gr *gr, int format,
107
		       const u32 ds, const u32 l2)
108
{
B
Ben Skeggs 已提交
109
	struct nvkm_ltc *ltc = nvkm_ltc(gr);
110 111 112
	int zbc = -ENOSPC, i;

	for (i = ltc->zbc_min; i <= ltc->zbc_max; i++) {
B
Ben Skeggs 已提交
113 114
		if (gr->zbc_depth[i].format) {
			if (gr->zbc_depth[i].format != format)
115
				continue;
B
Ben Skeggs 已提交
116
			if (gr->zbc_depth[i].ds != ds)
117
				continue;
B
Ben Skeggs 已提交
118
			if (gr->zbc_depth[i].l2 != l2) {
119 120 121 122 123 124 125 126 127
				WARN_ON(1);
				return -EINVAL;
			}
			return i;
		} else {
			zbc = (zbc < 0) ? i : zbc;
		}
	}

128 129 130
	if (zbc < 0)
		return zbc;

B
Ben Skeggs 已提交
131 132 133
	gr->zbc_depth[zbc].format = format;
	gr->zbc_depth[zbc].ds = ds;
	gr->zbc_depth[zbc].l2 = l2;
134
	ltc->zbc_depth_get(ltc, zbc, l2);
B
Ben Skeggs 已提交
135
	gf100_gr_zbc_clear_depth(gr, zbc);
136 137 138
	return zbc;
}

139 140 141 142
/*******************************************************************************
 * Graphics object classes
 ******************************************************************************/

143
static int
144
gf100_fermi_mthd_zbc_color(struct nvkm_object *object, void *data, u32 size)
145
{
B
Ben Skeggs 已提交
146
	struct gf100_gr *gr = (void *)object->engine;
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
	union {
		struct fermi_a_zbc_color_v0 v0;
	} *args = data;
	int ret;

	if (nvif_unpack(args->v0, 0, 0, false)) {
		switch (args->v0.format) {
		case FERMI_A_ZBC_COLOR_V0_FMT_ZERO:
		case FERMI_A_ZBC_COLOR_V0_FMT_UNORM_ONE:
		case FERMI_A_ZBC_COLOR_V0_FMT_RF32_GF32_BF32_AF32:
		case FERMI_A_ZBC_COLOR_V0_FMT_R16_G16_B16_A16:
		case FERMI_A_ZBC_COLOR_V0_FMT_RN16_GN16_BN16_AN16:
		case FERMI_A_ZBC_COLOR_V0_FMT_RS16_GS16_BS16_AS16:
		case FERMI_A_ZBC_COLOR_V0_FMT_RU16_GU16_BU16_AU16:
		case FERMI_A_ZBC_COLOR_V0_FMT_RF16_GF16_BF16_AF16:
		case FERMI_A_ZBC_COLOR_V0_FMT_A8R8G8B8:
		case FERMI_A_ZBC_COLOR_V0_FMT_A8RL8GL8BL8:
		case FERMI_A_ZBC_COLOR_V0_FMT_A2B10G10R10:
		case FERMI_A_ZBC_COLOR_V0_FMT_AU2BU10GU10RU10:
		case FERMI_A_ZBC_COLOR_V0_FMT_A8B8G8R8:
		case FERMI_A_ZBC_COLOR_V0_FMT_A8BL8GL8RL8:
		case FERMI_A_ZBC_COLOR_V0_FMT_AN8BN8GN8RN8:
		case FERMI_A_ZBC_COLOR_V0_FMT_AS8BS8GS8RS8:
		case FERMI_A_ZBC_COLOR_V0_FMT_AU8BU8GU8RU8:
		case FERMI_A_ZBC_COLOR_V0_FMT_A2R10G10B10:
		case FERMI_A_ZBC_COLOR_V0_FMT_BF10GF11RF11:
B
Ben Skeggs 已提交
173
			ret = gf100_gr_zbc_color_get(gr, args->v0.format,
174 175
							   args->v0.ds,
							   args->v0.l2);
176 177 178 179 180 181 182 183 184 185 186 187 188 189
			if (ret >= 0) {
				args->v0.index = ret;
				return 0;
			}
			break;
		default:
			return -EINVAL;
		}
	}

	return ret;
}

static int
190
gf100_fermi_mthd_zbc_depth(struct nvkm_object *object, void *data, u32 size)
191
{
B
Ben Skeggs 已提交
192
	struct gf100_gr *gr = (void *)object->engine;
193 194 195 196 197 198 199 200
	union {
		struct fermi_a_zbc_depth_v0 v0;
	} *args = data;
	int ret;

	if (nvif_unpack(args->v0, 0, 0, false)) {
		switch (args->v0.format) {
		case FERMI_A_ZBC_DEPTH_V0_FMT_FP32:
B
Ben Skeggs 已提交
201
			ret = gf100_gr_zbc_depth_get(gr, args->v0.format,
202 203
							   args->v0.ds,
							   args->v0.l2);
204 205 206 207 208 209 210 211 212 213
			return (ret >= 0) ? 0 : -ENOSPC;
		default:
			return -EINVAL;
		}
	}

	return ret;
}

static int
214
gf100_fermi_mthd(struct nvkm_object *object, u32 mthd, void *data, u32 size)
215 216 217
{
	switch (mthd) {
	case FERMI_A_ZBC_COLOR:
218
		return gf100_fermi_mthd_zbc_color(object, data, size);
219
	case FERMI_A_ZBC_DEPTH:
220
		return gf100_fermi_mthd_zbc_depth(object, data, size);
221 222 223 224 225 226
	default:
		break;
	}
	return -EINVAL;
}

227 228 229 230 231 232 233
struct nvkm_ofuncs
gf100_fermi_ofuncs = {
	.ctor = _nvkm_object_ctor,
	.dtor = nvkm_object_destroy,
	.init = nvkm_object_init,
	.fini = nvkm_object_fini,
	.mthd = gf100_fermi_mthd,
234 235
};

236
static int
237 238
gf100_gr_set_shader_exceptions(struct nvkm_object *object, u32 mthd,
			       void *pdata, u32 size)
239
{
B
Ben Skeggs 已提交
240
	struct gf100_gr *gr = (void *)object->engine;
241
	struct nvkm_device *device = gr->base.engine.subdev.device;
242 243
	if (size >= sizeof(u32)) {
		u32 data = *(u32 *)pdata ? 0xffffffff : 0x00000000;
244 245
		nvkm_wr32(device, 0x419e44, data);
		nvkm_wr32(device, 0x419e4c, data);
246 247 248 249 250
		return 0;
	}
	return -EINVAL;
}

251 252 253
struct nvkm_omthds
gf100_gr_9097_omthds[] = {
	{ 0x1528, 0x1528, gf100_gr_set_shader_exceptions },
254 255 256
	{}
};

257 258 259
struct nvkm_omthds
gf100_gr_90c0_omthds[] = {
	{ 0x1528, 0x1528, gf100_gr_set_shader_exceptions },
260 261 262
	{}
};

263 264
struct nvkm_oclass
gf100_gr_sclass[] = {
265 266
	{ FERMI_TWOD_A, &nvkm_object_ofuncs },
	{ FERMI_MEMORY_TO_MEMORY_FORMAT_A, &nvkm_object_ofuncs },
267 268
	{ FERMI_A, &gf100_fermi_ofuncs, gf100_gr_9097_omthds },
	{ FERMI_COMPUTE_A, &nvkm_object_ofuncs, gf100_gr_90c0_omthds },
269 270 271 272 273 274
	{}
};

/*******************************************************************************
 * PGRAPH context
 ******************************************************************************/
275

276
int
277 278 279
gf100_gr_context_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
		      struct nvkm_oclass *oclass, void *args, u32 size,
		      struct nvkm_object **pobject)
280
{
281
	struct nvkm_vm *vm = nvkm_client(parent)->vm;
B
Ben Skeggs 已提交
282 283 284
	struct gf100_gr *gr = (void *)engine;
	struct gf100_gr_data *data = gr->mmio_data;
	struct gf100_gr_mmio *mmio = gr->mmio_list;
285
	struct gf100_gr_chan *chan;
286 287
	int ret, i;

288
	/* allocate memory for context, and fill with default values */
289
	ret = nvkm_gr_context_create(parent, engine, oclass, NULL,
B
Ben Skeggs 已提交
290
				     gr->size, 0x100,
291
				     NVOBJ_FLAG_ZERO_ALLOC, &chan);
292
	*pobject = nv_object(chan);
293 294 295
	if (ret)
		return ret;

296 297 298 299
	/* allocate memory for a "mmio list" buffer that's used by the HUB
	 * fuc to modify some per-context register settings on first load
	 * of the context.
	 */
300 301
	ret = nvkm_gpuobj_new(nv_object(chan), NULL, 0x1000, 0x100, 0,
			      &chan->mmio);
302 303 304
	if (ret)
		return ret;

305 306 307
	ret = nvkm_gpuobj_map_vm(nv_gpuobj(chan->mmio), vm,
				 NV_MEM_ACCESS_RW | NV_MEM_ACCESS_SYS,
				 &chan->mmio_vma);
308 309 310
	if (ret)
		return ret;

311
	/* allocate buffers referenced by mmio list */
B
Ben Skeggs 已提交
312
	for (i = 0; data->size && i < ARRAY_SIZE(gr->mmio_data); i++) {
313 314
		ret = nvkm_gpuobj_new(nv_object(chan), NULL, data->size,
				      data->align, 0, &chan->data[i].mem);
315 316
		if (ret)
			return ret;
317

318 319
		ret = nvkm_gpuobj_map_vm(chan->data[i].mem, vm, data->access,
					 &chan->data[i].vma);
320 321
		if (ret)
			return ret;
322

323
		data++;
324 325
	}

326
	/* finally, fill in the mmio list and point the context at it */
B
Ben Skeggs 已提交
327
	for (i = 0; mmio->addr && i < ARRAY_SIZE(gr->mmio_list); i++) {
328 329
		u32 addr = mmio->addr;
		u32 data = mmio->data;
330

331
		if (mmio->buffer >= 0) {
332
			u64 info = chan->data[mmio->buffer].vma.offset;
333 334
			data |= info >> mmio->shift;
		}
335

336 337
		nv_wo32(chan->mmio, chan->mmio_nr++ * 4, addr);
		nv_wo32(chan->mmio, chan->mmio_nr++ * 4, data);
338 339
		mmio++;
	}
340

B
Ben Skeggs 已提交
341 342
	for (i = 0; i < gr->size; i += 4)
		nv_wo32(chan, i, gr->data[i / 4]);
343

B
Ben Skeggs 已提交
344
	if (!gr->firmware) {
345 346
		nv_wo32(chan, 0x00, chan->mmio_nr / 2);
		nv_wo32(chan, 0x04, chan->mmio_vma.offset >> 8);
347
	} else {
348 349 350 351 352 353 354 355 356
		nv_wo32(chan, 0xf4, 0);
		nv_wo32(chan, 0xf8, 0);
		nv_wo32(chan, 0x10, chan->mmio_nr / 2);
		nv_wo32(chan, 0x14, lower_32_bits(chan->mmio_vma.offset));
		nv_wo32(chan, 0x18, upper_32_bits(chan->mmio_vma.offset));
		nv_wo32(chan, 0x1c, 1);
		nv_wo32(chan, 0x20, 0);
		nv_wo32(chan, 0x28, 0);
		nv_wo32(chan, 0x2c, 0);
357
	}
358

359
	return 0;
360 361
}

362
void
363
gf100_gr_context_dtor(struct nvkm_object *object)
364
{
365
	struct gf100_gr_chan *chan = (void *)object;
366 367
	int i;

368
	for (i = 0; i < ARRAY_SIZE(chan->data); i++) {
369 370
		nvkm_gpuobj_unmap(&chan->data[i].vma);
		nvkm_gpuobj_ref(NULL, &chan->data[i].mem);
371
	}
372

373 374
	nvkm_gpuobj_unmap(&chan->mmio_vma);
	nvkm_gpuobj_ref(NULL, &chan->mmio);
375

376
	nvkm_gr_context_destroy(&chan->base);
377 378
}

379
/*******************************************************************************
380
 * PGRAPH register lists
381 382
 ******************************************************************************/

383 384
const struct gf100_gr_init
gf100_gr_init_main_0[] = {
385 386 387 388 389 390 391 392 393 394 395 396 397 398
	{ 0x400080,   1, 0x04, 0x003083c2 },
	{ 0x400088,   1, 0x04, 0x00006fe7 },
	{ 0x40008c,   1, 0x04, 0x00000000 },
	{ 0x400090,   1, 0x04, 0x00000030 },
	{ 0x40013c,   1, 0x04, 0x013901f7 },
	{ 0x400140,   1, 0x04, 0x00000100 },
	{ 0x400144,   1, 0x04, 0x00000000 },
	{ 0x400148,   1, 0x04, 0x00000110 },
	{ 0x400138,   1, 0x04, 0x00000000 },
	{ 0x400130,   2, 0x04, 0x00000000 },
	{ 0x400124,   1, 0x04, 0x00000002 },
	{}
};

399 400
const struct gf100_gr_init
gf100_gr_init_fe_0[] = {
401 402 403 404 405
	{ 0x40415c,   1, 0x04, 0x00000000 },
	{ 0x404170,   1, 0x04, 0x00000000 },
	{}
};

406 407
const struct gf100_gr_init
gf100_gr_init_pri_0[] = {
408 409 410 411
	{ 0x404488,   2, 0x04, 0x00000000 },
	{}
};

412 413
const struct gf100_gr_init
gf100_gr_init_rstr2d_0[] = {
414 415 416 417
	{ 0x407808,   1, 0x04, 0x00000000 },
	{}
};

418 419
const struct gf100_gr_init
gf100_gr_init_pd_0[] = {
420 421 422 423
	{ 0x406024,   1, 0x04, 0x00000000 },
	{}
};

424 425
const struct gf100_gr_init
gf100_gr_init_ds_0[] = {
426 427 428 429 430 431
	{ 0x405844,   1, 0x04, 0x00ffffff },
	{ 0x405850,   1, 0x04, 0x00000000 },
	{ 0x405908,   1, 0x04, 0x00000000 },
	{}
};

432 433
const struct gf100_gr_init
gf100_gr_init_scc_0[] = {
434 435 436 437
	{ 0x40803c,   1, 0x04, 0x00000000 },
	{}
};

438 439
const struct gf100_gr_init
gf100_gr_init_prop_0[] = {
440
	{ 0x4184a0,   1, 0x04, 0x00000000 },
441 442 443
	{}
};

444 445
const struct gf100_gr_init
gf100_gr_init_gpc_unk_0[] = {
446 447 448 449
	{ 0x418604,   1, 0x04, 0x00000000 },
	{ 0x418680,   1, 0x04, 0x00000000 },
	{ 0x418714,   1, 0x04, 0x80000000 },
	{ 0x418384,   1, 0x04, 0x00000000 },
450 451 452
	{}
};

453 454
const struct gf100_gr_init
gf100_gr_init_setup_0[] = {
455
	{ 0x418814,   3, 0x04, 0x00000000 },
456 457 458
	{}
};

459 460
const struct gf100_gr_init
gf100_gr_init_crstr_0[] = {
461
	{ 0x418b04,   1, 0x04, 0x00000000 },
462 463 464
	{}
};

465 466
const struct gf100_gr_init
gf100_gr_init_setup_1[] = {
467 468 469 470
	{ 0x4188c8,   1, 0x04, 0x80000000 },
	{ 0x4188cc,   1, 0x04, 0x00000000 },
	{ 0x4188d0,   1, 0x04, 0x00010000 },
	{ 0x4188d4,   1, 0x04, 0x00000001 },
471 472 473
	{}
};

474 475
const struct gf100_gr_init
gf100_gr_init_zcull_0[] = {
476 477 478 479 480
	{ 0x418910,   1, 0x04, 0x00010001 },
	{ 0x418914,   1, 0x04, 0x00000301 },
	{ 0x418918,   1, 0x04, 0x00800000 },
	{ 0x418980,   1, 0x04, 0x77777770 },
	{ 0x418984,   3, 0x04, 0x77777777 },
481 482 483
	{}
};

484 485
const struct gf100_gr_init
gf100_gr_init_gpm_0[] = {
486 487
	{ 0x418c04,   1, 0x04, 0x00000000 },
	{ 0x418c88,   1, 0x04, 0x00000000 },
488 489 490
	{}
};

491 492
const struct gf100_gr_init
gf100_gr_init_gpc_unk_1[] = {
493 494 495 496
	{ 0x418d00,   1, 0x04, 0x00000000 },
	{ 0x418f08,   1, 0x04, 0x00000000 },
	{ 0x418e00,   1, 0x04, 0x00000050 },
	{ 0x418e08,   1, 0x04, 0x00000000 },
497 498 499
	{}
};

500 501
const struct gf100_gr_init
gf100_gr_init_gcc_0[] = {
502 503 504 505 506
	{ 0x41900c,   1, 0x04, 0x00000000 },
	{ 0x419018,   1, 0x04, 0x00000000 },
	{}
};

507 508
const struct gf100_gr_init
gf100_gr_init_tpccs_0[] = {
509 510
	{ 0x419d08,   2, 0x04, 0x00000000 },
	{ 0x419d10,   1, 0x04, 0x00000014 },
511 512 513
	{}
};

514 515
const struct gf100_gr_init
gf100_gr_init_tex_0[] = {
516 517 518
	{ 0x419ab0,   1, 0x04, 0x00000000 },
	{ 0x419ab8,   1, 0x04, 0x000000e7 },
	{ 0x419abc,   2, 0x04, 0x00000000 },
519 520 521
	{}
};

522 523
const struct gf100_gr_init
gf100_gr_init_pe_0[] = {
524 525 526 527
	{ 0x41980c,   3, 0x04, 0x00000000 },
	{ 0x419844,   1, 0x04, 0x00000000 },
	{ 0x41984c,   1, 0x04, 0x00005bc5 },
	{ 0x419850,   4, 0x04, 0x00000000 },
528 529 530
	{}
};

531 532
const struct gf100_gr_init
gf100_gr_init_l1c_0[] = {
533 534 535 536 537 538
	{ 0x419c98,   1, 0x04, 0x00000000 },
	{ 0x419ca8,   1, 0x04, 0x80000000 },
	{ 0x419cb4,   1, 0x04, 0x00000000 },
	{ 0x419cb8,   1, 0x04, 0x00008bf4 },
	{ 0x419cbc,   1, 0x04, 0x28137606 },
	{ 0x419cc0,   2, 0x04, 0x00000000 },
539 540 541
	{}
};

542 543
const struct gf100_gr_init
gf100_gr_init_wwdx_0[] = {
544 545
	{ 0x419bd4,   1, 0x04, 0x00800000 },
	{ 0x419bdc,   1, 0x04, 0x00000000 },
546 547 548
	{}
};

549 550
const struct gf100_gr_init
gf100_gr_init_tpccs_1[] = {
551
	{ 0x419d2c,   1, 0x04, 0x00000000 },
552 553 554
	{}
};

555 556
const struct gf100_gr_init
gf100_gr_init_mpc_0[] = {
557
	{ 0x419c0c,   1, 0x04, 0x00000000 },
558 559 560
	{}
};

561 562
static const struct gf100_gr_init
gf100_gr_init_sm_0[] = {
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
	{ 0x419e00,   1, 0x04, 0x00000000 },
	{ 0x419ea0,   1, 0x04, 0x00000000 },
	{ 0x419ea4,   1, 0x04, 0x00000100 },
	{ 0x419ea8,   1, 0x04, 0x00001100 },
	{ 0x419eac,   1, 0x04, 0x11100702 },
	{ 0x419eb0,   1, 0x04, 0x00000003 },
	{ 0x419eb4,   4, 0x04, 0x00000000 },
	{ 0x419ec8,   1, 0x04, 0x06060618 },
	{ 0x419ed0,   1, 0x04, 0x0eff0e38 },
	{ 0x419ed4,   1, 0x04, 0x011104f1 },
	{ 0x419edc,   1, 0x04, 0x00000000 },
	{ 0x419f00,   1, 0x04, 0x00000000 },
	{ 0x419f2c,   1, 0x04, 0x00000000 },
	{}
};

579 580
const struct gf100_gr_init
gf100_gr_init_be_0[] = {
581 582 583 584 585 586 587 588 589 590
	{ 0x40880c,   1, 0x04, 0x00000000 },
	{ 0x408910,   9, 0x04, 0x00000000 },
	{ 0x408950,   1, 0x04, 0x00000000 },
	{ 0x408954,   1, 0x04, 0x0000ffff },
	{ 0x408984,   1, 0x04, 0x00000000 },
	{ 0x408988,   1, 0x04, 0x08040201 },
	{ 0x40898c,   1, 0x04, 0x80402010 },
	{}
};

591 592
const struct gf100_gr_init
gf100_gr_init_fe_1[] = {
593 594 595 596
	{ 0x4040f0,   1, 0x04, 0x00000000 },
	{}
};

597 598
const struct gf100_gr_init
gf100_gr_init_pe_1[] = {
599 600 601 602
	{ 0x419880,   1, 0x04, 0x00000002 },
	{}
};

603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
static const struct gf100_gr_pack
gf100_gr_pack_mmio[] = {
	{ gf100_gr_init_main_0 },
	{ gf100_gr_init_fe_0 },
	{ gf100_gr_init_pri_0 },
	{ gf100_gr_init_rstr2d_0 },
	{ gf100_gr_init_pd_0 },
	{ gf100_gr_init_ds_0 },
	{ gf100_gr_init_scc_0 },
	{ gf100_gr_init_prop_0 },
	{ gf100_gr_init_gpc_unk_0 },
	{ gf100_gr_init_setup_0 },
	{ gf100_gr_init_crstr_0 },
	{ gf100_gr_init_setup_1 },
	{ gf100_gr_init_zcull_0 },
	{ gf100_gr_init_gpm_0 },
	{ gf100_gr_init_gpc_unk_1 },
	{ gf100_gr_init_gcc_0 },
	{ gf100_gr_init_tpccs_0 },
	{ gf100_gr_init_tex_0 },
	{ gf100_gr_init_pe_0 },
	{ gf100_gr_init_l1c_0 },
	{ gf100_gr_init_wwdx_0 },
	{ gf100_gr_init_tpccs_1 },
	{ gf100_gr_init_mpc_0 },
	{ gf100_gr_init_sm_0 },
	{ gf100_gr_init_be_0 },
	{ gf100_gr_init_fe_1 },
	{ gf100_gr_init_pe_1 },
M
Maarten Lankhorst 已提交
632 633 634
	{}
};

635 636 637 638
/*******************************************************************************
 * PGRAPH engine/subdev functions
 ******************************************************************************/

639
void
B
Ben Skeggs 已提交
640
gf100_gr_zbc_init(struct gf100_gr *gr)
641 642 643 644 645 646 647 648 649
{
	const u32  zero[] = { 0x00000000, 0x00000000, 0x00000000, 0x00000000,
			      0x00000000, 0x00000000, 0x00000000, 0x00000000 };
	const u32   one[] = { 0x3f800000, 0x3f800000, 0x3f800000, 0x3f800000,
			      0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
	const u32 f32_0[] = { 0x00000000, 0x00000000, 0x00000000, 0x00000000,
			      0x00000000, 0x00000000, 0x00000000, 0x00000000 };
	const u32 f32_1[] = { 0x3f800000, 0x3f800000, 0x3f800000, 0x3f800000,
			      0x3f800000, 0x3f800000, 0x3f800000, 0x3f800000 };
B
Ben Skeggs 已提交
650
	struct nvkm_ltc *ltc = nvkm_ltc(gr);
651 652
	int index;

B
Ben Skeggs 已提交
653 654 655 656 657 658 659
	if (!gr->zbc_color[0].format) {
		gf100_gr_zbc_color_get(gr, 1,  & zero[0],   &zero[4]);
		gf100_gr_zbc_color_get(gr, 2,  &  one[0],    &one[4]);
		gf100_gr_zbc_color_get(gr, 4,  &f32_0[0],  &f32_0[4]);
		gf100_gr_zbc_color_get(gr, 4,  &f32_1[0],  &f32_1[4]);
		gf100_gr_zbc_depth_get(gr, 1, 0x00000000, 0x00000000);
		gf100_gr_zbc_depth_get(gr, 1, 0x3f800000, 0x3f800000);
660 661 662
	}

	for (index = ltc->zbc_min; index <= ltc->zbc_max; index++)
B
Ben Skeggs 已提交
663
		gf100_gr_zbc_clear_color(gr, index);
664
	for (index = ltc->zbc_min; index <= ltc->zbc_max; index++)
B
Ben Skeggs 已提交
665
		gf100_gr_zbc_clear_depth(gr, index);
666 667
}

668 669 670 671 672 673
/**
 * Wait until GR goes idle. GR is considered idle if it is disabled by the
 * MC (0x200) register, or GR is not busy and a context switch is not in
 * progress.
 */
int
B
Ben Skeggs 已提交
674
gf100_gr_wait_idle(struct gf100_gr *gr)
675
{
676
	struct nvkm_device *device = gr->base.engine.subdev.device;
677 678 679 680 681 682 683 684
	unsigned long end_jiffies = jiffies + msecs_to_jiffies(2000);
	bool gr_enabled, ctxsw_active, gr_busy;

	do {
		/*
		 * required to make sure FIFO_ENGINE_STATUS (0x2640) is
		 * up-to-date
		 */
685
		nvkm_rd32(device, 0x400700);
686

687 688 689
		gr_enabled = nvkm_rd32(device, 0x200) & 0x1000;
		ctxsw_active = nvkm_rd32(device, 0x2640) & 0x8000;
		gr_busy = nvkm_rd32(device, 0x40060c) & 0x1;
690 691 692 693 694

		if (!gr_enabled || (!gr_busy && !ctxsw_active))
			return 0;
	} while (time_before(jiffies, end_jiffies));

B
Ben Skeggs 已提交
695
	nv_error(gr, "wait for idle timeout (en: %d, ctxsw: %d, busy: %d)\n",
696 697 698 699
		 gr_enabled, ctxsw_active, gr_busy);
	return -EAGAIN;
}

700
void
B
Ben Skeggs 已提交
701
gf100_gr_mmio(struct gf100_gr *gr, const struct gf100_gr_pack *p)
702
{
703
	struct nvkm_device *device = gr->base.engine.subdev.device;
704 705
	const struct gf100_gr_pack *pack;
	const struct gf100_gr_init *init;
706 707 708 709 710

	pack_for_each_init(init, pack, p) {
		u32 next = init->addr + init->count * init->pitch;
		u32 addr = init->addr;
		while (addr < next) {
711
			nvkm_wr32(device, addr, init->data);
712 713 714
			addr += init->pitch;
		}
	}
715 716 717
}

void
B
Ben Skeggs 已提交
718
gf100_gr_icmd(struct gf100_gr *gr, const struct gf100_gr_pack *p)
719
{
720
	struct nvkm_device *device = gr->base.engine.subdev.device;
721 722
	const struct gf100_gr_pack *pack;
	const struct gf100_gr_init *init;
723
	u32 data = 0;
724

725
	nvkm_wr32(device, 0x400208, 0x80000000);
726 727 728 729 730 731

	pack_for_each_init(init, pack, p) {
		u32 next = init->addr + init->count * init->pitch;
		u32 addr = init->addr;

		if ((pack == p && init == p->init) || data != init->data) {
732
			nvkm_wr32(device, 0x400204, init->data);
733 734
			data = init->data;
		}
735

736
		while (addr < next) {
737
			nvkm_wr32(device, 0x400200, addr);
738 739 740 741 742
			/**
			 * Wait for GR to go idle after submitting a
			 * GO_IDLE bundle
			 */
			if ((addr & 0xffff) == 0xe100)
B
Ben Skeggs 已提交
743 744
				gf100_gr_wait_idle(gr);
			nv_wait(gr, 0x400700, 0x00000004, 0x00000000);
745 746 747
			addr += init->pitch;
		}
	}
748

749
	nvkm_wr32(device, 0x400208, 0x00000000);
750 751 752
}

void
B
Ben Skeggs 已提交
753
gf100_gr_mthd(struct gf100_gr *gr, const struct gf100_gr_pack *p)
754
{
755
	struct nvkm_device *device = gr->base.engine.subdev.device;
756 757
	const struct gf100_gr_pack *pack;
	const struct gf100_gr_init *init;
758
	u32 data = 0;
759

760 761 762 763 764 765
	pack_for_each_init(init, pack, p) {
		u32 ctrl = 0x80000000 | pack->type;
		u32 next = init->addr + init->count * init->pitch;
		u32 addr = init->addr;

		if ((pack == p && init == p->init) || data != init->data) {
766
			nvkm_wr32(device, 0x40448c, init->data);
767 768 769 770
			data = init->data;
		}

		while (addr < next) {
771
			nvkm_wr32(device, 0x404488, ctrl | (addr << 14));
772
			addr += init->pitch;
773 774 775 776 777
		}
	}
}

u64
B
Ben Skeggs 已提交
778
gf100_gr_units(struct nvkm_gr *obj)
779
{
B
Ben Skeggs 已提交
780
	struct gf100_gr *gr = container_of(obj, typeof(*gr), base);
781 782
	u64 cfg;

B
Ben Skeggs 已提交
783 784 785
	cfg  = (u32)gr->gpc_nr;
	cfg |= (u32)gr->tpc_total << 8;
	cfg |= (u64)gr->rop_nr << 32;
786 787

	return cfg;
788 789
}

790
static const struct nvkm_enum gk104_sked_error[] = {
791 792 793 794 795 796 797 798 799 800 801 802 803 804
	{ 7, "CONSTANT_BUFFER_SIZE" },
	{ 9, "LOCAL_MEMORY_SIZE_POS" },
	{ 10, "LOCAL_MEMORY_SIZE_NEG" },
	{ 11, "WARP_CSTACK_SIZE" },
	{ 12, "TOTAL_TEMP_SIZE" },
	{ 13, "REGISTER_COUNT" },
	{ 18, "TOTAL_THREADS" },
	{ 20, "PROGRAM_OFFSET" },
	{ 21, "SHARED_MEMORY_SIZE" },
	{ 25, "SHARED_CONFIG_TOO_SMALL" },
	{ 26, "TOTAL_REGISTER_COUNT" },
	{}
};

805
static const struct nvkm_enum gf100_gpc_rop_error[] = {
806 807 808 809 810 811 812 813 814
	{ 1, "RT_PITCH_OVERRUN" },
	{ 4, "RT_WIDTH_OVERRUN" },
	{ 5, "RT_HEIGHT_OVERRUN" },
	{ 7, "ZETA_STORAGE_TYPE_MISMATCH" },
	{ 8, "RT_STORAGE_TYPE_MISMATCH" },
	{ 10, "RT_LINEAR_MISMATCH" },
	{}
};

815
static void
B
Ben Skeggs 已提交
816
gf100_gr_trap_gpc_rop(struct gf100_gr *gr, int gpc)
817
{
818
	struct nvkm_device *device = gr->base.engine.subdev.device;
819 820
	u32 trap[4];
	int i;
821

822 823 824 825
	trap[0] = nvkm_rd32(device, GPC_UNIT(gpc, 0x0420));
	trap[1] = nvkm_rd32(device, GPC_UNIT(gpc, 0x0434));
	trap[2] = nvkm_rd32(device, GPC_UNIT(gpc, 0x0438));
	trap[3] = nvkm_rd32(device, GPC_UNIT(gpc, 0x043c));
826

B
Ben Skeggs 已提交
827
	nv_error(gr, "GPC%d/PROP trap:", gpc);
828 829 830 831
	for (i = 0; i <= 29; ++i) {
		if (!(trap[0] & (1 << i)))
			continue;
		pr_cont(" ");
832
		nvkm_enum_print(gf100_gpc_rop_error, i);
833 834 835
	}
	pr_cont("\n");

B
Ben Skeggs 已提交
836
	nv_error(gr, "x = %u, y = %u, format = %x, storage type = %x\n",
837 838
		 trap[1] & 0xffff, trap[1] >> 16, (trap[2] >> 8) & 0x3f,
		 trap[3] & 0xff);
839
	nvkm_wr32(device, GPC_UNIT(gpc, 0x0420), 0xc0000000);
840 841
}

842
static const struct nvkm_enum gf100_mp_warp_error[] = {
843 844 845 846 847 848 849 850 851 852 853 854
	{ 0x00, "NO_ERROR" },
	{ 0x01, "STACK_MISMATCH" },
	{ 0x05, "MISALIGNED_PC" },
	{ 0x08, "MISALIGNED_GPR" },
	{ 0x09, "INVALID_OPCODE" },
	{ 0x0d, "GPR_OUT_OF_BOUNDS" },
	{ 0x0e, "MEM_OUT_OF_BOUNDS" },
	{ 0x0f, "UNALIGNED_MEM_ACCESS" },
	{ 0x11, "INVALID_PARAM" },
	{}
};

855
static const struct nvkm_bitfield gf100_mp_global_error[] = {
856 857 858 859 860 861
	{ 0x00000004, "MULTIPLE_WARP_ERRORS" },
	{ 0x00000008, "OUT_OF_STACK_SPACE" },
	{}
};

static void
B
Ben Skeggs 已提交
862
gf100_gr_trap_mp(struct gf100_gr *gr, int gpc, int tpc)
863
{
864 865 866
	struct nvkm_device *device = gr->base.engine.subdev.device;
	u32 werr = nvkm_rd32(device, TPC_UNIT(gpc, tpc, 0x648));
	u32 gerr = nvkm_rd32(device, TPC_UNIT(gpc, tpc, 0x650));
867

B
Ben Skeggs 已提交
868
	nv_error(gr, "GPC%i/TPC%i/MP trap:", gpc, tpc);
869
	nvkm_bitfield_print(gf100_mp_global_error, gerr);
870 871
	if (werr) {
		pr_cont(" ");
872
		nvkm_enum_print(gf100_mp_warp_error, werr & 0xffff);
873 874 875
	}
	pr_cont("\n");

876 877
	nvkm_wr32(device, TPC_UNIT(gpc, tpc, 0x648), 0x00000000);
	nvkm_wr32(device, TPC_UNIT(gpc, tpc, 0x650), gerr);
878 879
}

880
static void
B
Ben Skeggs 已提交
881
gf100_gr_trap_tpc(struct gf100_gr *gr, int gpc, int tpc)
882
{
883 884
	struct nvkm_device *device = gr->base.engine.subdev.device;
	u32 stat = nvkm_rd32(device, TPC_UNIT(gpc, tpc, 0x0508));
885 886

	if (stat & 0x00000001) {
887
		u32 trap = nvkm_rd32(device, TPC_UNIT(gpc, tpc, 0x0224));
B
Ben Skeggs 已提交
888
		nv_error(gr, "GPC%d/TPC%d/TEX: 0x%08x\n", gpc, tpc, trap);
889
		nvkm_wr32(device, TPC_UNIT(gpc, tpc, 0x0224), 0xc0000000);
890 891 892 893
		stat &= ~0x00000001;
	}

	if (stat & 0x00000002) {
B
Ben Skeggs 已提交
894
		gf100_gr_trap_mp(gr, gpc, tpc);
895 896 897 898
		stat &= ~0x00000002;
	}

	if (stat & 0x00000004) {
899
		u32 trap = nvkm_rd32(device, TPC_UNIT(gpc, tpc, 0x0084));
B
Ben Skeggs 已提交
900
		nv_error(gr, "GPC%d/TPC%d/POLY: 0x%08x\n", gpc, tpc, trap);
901
		nvkm_wr32(device, TPC_UNIT(gpc, tpc, 0x0084), 0xc0000000);
902 903 904 905
		stat &= ~0x00000004;
	}

	if (stat & 0x00000008) {
906
		u32 trap = nvkm_rd32(device, TPC_UNIT(gpc, tpc, 0x048c));
B
Ben Skeggs 已提交
907
		nv_error(gr, "GPC%d/TPC%d/L1C: 0x%08x\n", gpc, tpc, trap);
908
		nvkm_wr32(device, TPC_UNIT(gpc, tpc, 0x048c), 0xc0000000);
909 910 911 912
		stat &= ~0x00000008;
	}

	if (stat) {
B
Ben Skeggs 已提交
913
		nv_error(gr, "GPC%d/TPC%d/0x%08x: unknown\n", gpc, tpc, stat);
914 915 916 917
	}
}

static void
B
Ben Skeggs 已提交
918
gf100_gr_trap_gpc(struct gf100_gr *gr, int gpc)
919
{
920 921
	struct nvkm_device *device = gr->base.engine.subdev.device;
	u32 stat = nvkm_rd32(device, GPC_UNIT(gpc, 0x2c90));
922 923 924
	int tpc;

	if (stat & 0x00000001) {
B
Ben Skeggs 已提交
925
		gf100_gr_trap_gpc_rop(gr, gpc);
926 927 928 929
		stat &= ~0x00000001;
	}

	if (stat & 0x00000002) {
930
		u32 trap = nvkm_rd32(device, GPC_UNIT(gpc, 0x0900));
B
Ben Skeggs 已提交
931
		nv_error(gr, "GPC%d/ZCULL: 0x%08x\n", gpc, trap);
932
		nvkm_wr32(device, GPC_UNIT(gpc, 0x0900), 0xc0000000);
933 934 935 936
		stat &= ~0x00000002;
	}

	if (stat & 0x00000004) {
937
		u32 trap = nvkm_rd32(device, GPC_UNIT(gpc, 0x1028));
B
Ben Skeggs 已提交
938
		nv_error(gr, "GPC%d/CCACHE: 0x%08x\n", gpc, trap);
939
		nvkm_wr32(device, GPC_UNIT(gpc, 0x1028), 0xc0000000);
940 941 942 943
		stat &= ~0x00000004;
	}

	if (stat & 0x00000008) {
944
		u32 trap = nvkm_rd32(device, GPC_UNIT(gpc, 0x0824));
B
Ben Skeggs 已提交
945
		nv_error(gr, "GPC%d/ESETUP: 0x%08x\n", gpc, trap);
946
		nvkm_wr32(device, GPC_UNIT(gpc, 0x0824), 0xc0000000);
947 948 949
		stat &= ~0x00000009;
	}

B
Ben Skeggs 已提交
950
	for (tpc = 0; tpc < gr->tpc_nr[gpc]; tpc++) {
951 952
		u32 mask = 0x00010000 << tpc;
		if (stat & mask) {
B
Ben Skeggs 已提交
953
			gf100_gr_trap_tpc(gr, gpc, tpc);
954
			nvkm_wr32(device, GPC_UNIT(gpc, 0x2c90), mask);
955 956 957 958 959
			stat &= ~mask;
		}
	}

	if (stat) {
B
Ben Skeggs 已提交
960
		nv_error(gr, "GPC%d/0x%08x: unknown\n", gpc, stat);
961 962 963 964
	}
}

static void
B
Ben Skeggs 已提交
965
gf100_gr_trap_intr(struct gf100_gr *gr)
966
{
967 968
	struct nvkm_device *device = gr->base.engine.subdev.device;
	u32 trap = nvkm_rd32(device, 0x400108);
969
	int rop, gpc, i;
970 971

	if (trap & 0x00000001) {
972
		u32 stat = nvkm_rd32(device, 0x404000);
B
Ben Skeggs 已提交
973
		nv_error(gr, "DISPATCH 0x%08x\n", stat);
974 975
		nvkm_wr32(device, 0x404000, 0xc0000000);
		nvkm_wr32(device, 0x400108, 0x00000001);
976 977 978 979
		trap &= ~0x00000001;
	}

	if (trap & 0x00000002) {
980
		u32 stat = nvkm_rd32(device, 0x404600);
B
Ben Skeggs 已提交
981
		nv_error(gr, "M2MF 0x%08x\n", stat);
982 983
		nvkm_wr32(device, 0x404600, 0xc0000000);
		nvkm_wr32(device, 0x400108, 0x00000002);
984 985 986 987
		trap &= ~0x00000002;
	}

	if (trap & 0x00000008) {
988
		u32 stat = nvkm_rd32(device, 0x408030);
B
Ben Skeggs 已提交
989
		nv_error(gr, "CCACHE 0x%08x\n", stat);
990 991
		nvkm_wr32(device, 0x408030, 0xc0000000);
		nvkm_wr32(device, 0x400108, 0x00000008);
992 993 994 995
		trap &= ~0x00000008;
	}

	if (trap & 0x00000010) {
996
		u32 stat = nvkm_rd32(device, 0x405840);
B
Ben Skeggs 已提交
997
		nv_error(gr, "SHADER 0x%08x\n", stat);
998 999
		nvkm_wr32(device, 0x405840, 0xc0000000);
		nvkm_wr32(device, 0x400108, 0x00000010);
1000 1001 1002 1003
		trap &= ~0x00000010;
	}

	if (trap & 0x00000040) {
1004
		u32 stat = nvkm_rd32(device, 0x40601c);
B
Ben Skeggs 已提交
1005
		nv_error(gr, "UNK6 0x%08x\n", stat);
1006 1007
		nvkm_wr32(device, 0x40601c, 0xc0000000);
		nvkm_wr32(device, 0x400108, 0x00000040);
1008 1009 1010 1011
		trap &= ~0x00000040;
	}

	if (trap & 0x00000080) {
1012
		u32 stat = nvkm_rd32(device, 0x404490);
B
Ben Skeggs 已提交
1013
		nv_error(gr, "MACRO 0x%08x\n", stat);
1014 1015
		nvkm_wr32(device, 0x404490, 0xc0000000);
		nvkm_wr32(device, 0x400108, 0x00000080);
1016 1017 1018
		trap &= ~0x00000080;
	}

1019
	if (trap & 0x00000100) {
1020
		u32 stat = nvkm_rd32(device, 0x407020);
1021

B
Ben Skeggs 已提交
1022
		nv_error(gr, "SKED:");
1023 1024 1025 1026
		for (i = 0; i <= 29; ++i) {
			if (!(stat & (1 << i)))
				continue;
			pr_cont(" ");
1027
			nvkm_enum_print(gk104_sked_error, i);
1028 1029 1030 1031
		}
		pr_cont("\n");

		if (stat & 0x3fffffff)
1032 1033
			nvkm_wr32(device, 0x407020, 0x40000000);
		nvkm_wr32(device, 0x400108, 0x00000100);
1034 1035 1036
		trap &= ~0x00000100;
	}

1037
	if (trap & 0x01000000) {
1038
		u32 stat = nvkm_rd32(device, 0x400118);
B
Ben Skeggs 已提交
1039
		for (gpc = 0; stat && gpc < gr->gpc_nr; gpc++) {
1040 1041
			u32 mask = 0x00000001 << gpc;
			if (stat & mask) {
B
Ben Skeggs 已提交
1042
				gf100_gr_trap_gpc(gr, gpc);
1043
				nvkm_wr32(device, 0x400118, mask);
1044 1045 1046
				stat &= ~mask;
			}
		}
1047
		nvkm_wr32(device, 0x400108, 0x01000000);
1048 1049 1050 1051
		trap &= ~0x01000000;
	}

	if (trap & 0x02000000) {
B
Ben Skeggs 已提交
1052
		for (rop = 0; rop < gr->rop_nr; rop++) {
1053 1054
			u32 statz = nvkm_rd32(device, ROP_UNIT(rop, 0x070));
			u32 statc = nvkm_rd32(device, ROP_UNIT(rop, 0x144));
B
Ben Skeggs 已提交
1055
			nv_error(gr, "ROP%d 0x%08x 0x%08x\n",
1056
				 rop, statz, statc);
1057 1058
			nvkm_wr32(device, ROP_UNIT(rop, 0x070), 0xc0000000);
			nvkm_wr32(device, ROP_UNIT(rop, 0x144), 0xc0000000);
1059
		}
1060
		nvkm_wr32(device, 0x400108, 0x02000000);
1061 1062 1063 1064
		trap &= ~0x02000000;
	}

	if (trap) {
B
Ben Skeggs 已提交
1065
		nv_error(gr, "TRAP UNHANDLED 0x%08x\n", trap);
1066
		nvkm_wr32(device, 0x400108, trap);
1067 1068 1069
	}
}

1070
static void
B
Ben Skeggs 已提交
1071
gf100_gr_ctxctl_debug_unit(struct gf100_gr *gr, u32 base)
1072
{
1073
	struct nvkm_device *device = gr->base.engine.subdev.device;
B
Ben Skeggs 已提交
1074
	nv_error(gr, "%06x - done 0x%08x\n", base,
1075
		 nvkm_rd32(device, base + 0x400));
B
Ben Skeggs 已提交
1076
	nv_error(gr, "%06x - stat 0x%08x 0x%08x 0x%08x 0x%08x\n", base,
1077 1078
		 nvkm_rd32(device, base + 0x800), nvkm_rd32(device, base + 0x804),
		 nvkm_rd32(device, base + 0x808), nvkm_rd32(device, base + 0x80c));
B
Ben Skeggs 已提交
1079
	nv_error(gr, "%06x - stat 0x%08x 0x%08x 0x%08x 0x%08x\n", base,
1080 1081
		 nvkm_rd32(device, base + 0x810), nvkm_rd32(device, base + 0x814),
		 nvkm_rd32(device, base + 0x818), nvkm_rd32(device, base + 0x81c));
1082 1083 1084
}

void
B
Ben Skeggs 已提交
1085
gf100_gr_ctxctl_debug(struct gf100_gr *gr)
1086
{
1087 1088
	struct nvkm_device *device = gr->base.engine.subdev.device;
	u32 gpcnr = nvkm_rd32(device, 0x409604) & 0xffff;
1089 1090
	u32 gpc;

B
Ben Skeggs 已提交
1091
	gf100_gr_ctxctl_debug_unit(gr, 0x409000);
1092
	for (gpc = 0; gpc < gpcnr; gpc++)
B
Ben Skeggs 已提交
1093
		gf100_gr_ctxctl_debug_unit(gr, 0x502000 + (gpc * 0x8000));
1094 1095 1096
}

static void
B
Ben Skeggs 已提交
1097
gf100_gr_ctxctl_isr(struct gf100_gr *gr)
1098
{
1099 1100
	struct nvkm_device *device = gr->base.engine.subdev.device;
	u32 stat = nvkm_rd32(device, 0x409c18);
1101

1102
	if (stat & 0x00000001) {
1103
		u32 code = nvkm_rd32(device, 0x409814);
1104
		if (code == E_BAD_FWMTHD) {
1105 1106
			u32 class = nvkm_rd32(device, 0x409808);
			u32  addr = nvkm_rd32(device, 0x40980c);
1107 1108
			u32  subc = (addr & 0x00070000) >> 16;
			u32  mthd = (addr & 0x00003ffc);
1109
			u32  data = nvkm_rd32(device, 0x409810);
1110

B
Ben Skeggs 已提交
1111
			nv_error(gr, "FECS MTHD subc %d class 0x%04x "
1112 1113 1114
				       "mthd 0x%04x data 0x%08x\n",
				 subc, class, mthd, data);

1115
			nvkm_wr32(device, 0x409c20, 0x00000001);
1116 1117
			stat &= ~0x00000001;
		} else {
B
Ben Skeggs 已提交
1118
			nv_error(gr, "FECS ucode error %d\n", code);
1119 1120
		}
	}
1121

1122
	if (stat & 0x00080000) {
B
Ben Skeggs 已提交
1123 1124
		nv_error(gr, "FECS watchdog timeout\n");
		gf100_gr_ctxctl_debug(gr);
1125
		nvkm_wr32(device, 0x409c20, 0x00080000);
1126 1127 1128 1129
		stat &= ~0x00080000;
	}

	if (stat) {
B
Ben Skeggs 已提交
1130 1131
		nv_error(gr, "FECS 0x%08x\n", stat);
		gf100_gr_ctxctl_debug(gr);
1132
		nvkm_wr32(device, 0x409c20, stat);
1133
	}
1134 1135
}

1136
static void
1137
gf100_gr_intr(struct nvkm_subdev *subdev)
1138
{
1139 1140 1141
	struct gf100_gr *gr = (void *)subdev;
	struct nvkm_device *device = gr->base.engine.subdev.device;
	struct nvkm_fifo *fifo = device->fifo;
1142 1143 1144
	struct nvkm_engine *engine = nv_engine(subdev);
	struct nvkm_object *engctx;
	struct nvkm_handle *handle;
1145 1146 1147
	u64 inst = nvkm_rd32(device, 0x409b00) & 0x0fffffff;
	u32 stat = nvkm_rd32(device, 0x400100);
	u32 addr = nvkm_rd32(device, 0x400704);
1148 1149
	u32 mthd = (addr & 0x00003ffc);
	u32 subc = (addr & 0x00070000) >> 16;
1150 1151
	u32 data = nvkm_rd32(device, 0x400708);
	u32 code = nvkm_rd32(device, 0x400110);
1152
	u32 class;
1153 1154
	int chid;

B
Ben Skeggs 已提交
1155
	if (nv_device(gr)->card_type < NV_E0 || subc < 4)
1156
		class = nvkm_rd32(device, 0x404200 + (subc * 4));
1157 1158 1159
	else
		class = 0x0000;

1160
	engctx = nvkm_engctx_get(engine, inst);
B
Ben Skeggs 已提交
1161
	chid   = fifo->chid(fifo, engctx);
1162

1163 1164 1165 1166 1167
	if (stat & 0x00000001) {
		/*
		 * notifier interrupt, only needed for cyclestats
		 * can be safely ignored
		 */
1168
		nvkm_wr32(device, 0x400100, 0x00000001);
1169 1170 1171
		stat &= ~0x00000001;
	}

1172
	if (stat & 0x00000010) {
1173
		handle = nvkm_handle_get_class(engctx, class);
1174
		if (!handle || nv_call(handle->object, mthd, data)) {
B
Ben Skeggs 已提交
1175
			nv_error(gr,
1176
				 "ILLEGAL_MTHD ch %d [0x%010llx %s] subc %d class 0x%04x mthd 0x%04x data 0x%08x\n",
1177
				 chid, inst << 12, nvkm_client_name(engctx),
1178
				 subc, class, mthd, data);
1179
		}
1180
		nvkm_handle_put(handle);
1181
		nvkm_wr32(device, 0x400100, 0x00000010);
1182 1183 1184 1185
		stat &= ~0x00000010;
	}

	if (stat & 0x00000020) {
B
Ben Skeggs 已提交
1186
		nv_error(gr,
1187
			 "ILLEGAL_CLASS ch %d [0x%010llx %s] subc %d class 0x%04x mthd 0x%04x data 0x%08x\n",
1188
			 chid, inst << 12, nvkm_client_name(engctx), subc,
1189
			 class, mthd, data);
1190
		nvkm_wr32(device, 0x400100, 0x00000020);
1191 1192 1193 1194
		stat &= ~0x00000020;
	}

	if (stat & 0x00100000) {
B
Ben Skeggs 已提交
1195
		nv_error(gr, "DATA_ERROR [");
1196
		nvkm_enum_print(nv50_data_error_names, code);
1197
		pr_cont("] ch %d [0x%010llx %s] subc %d class 0x%04x mthd 0x%04x data 0x%08x\n",
1198
			chid, inst << 12, nvkm_client_name(engctx), subc,
1199
			class, mthd, data);
1200
		nvkm_wr32(device, 0x400100, 0x00100000);
1201 1202 1203 1204
		stat &= ~0x00100000;
	}

	if (stat & 0x00200000) {
B
Ben Skeggs 已提交
1205
		nv_error(gr, "TRAP ch %d [0x%010llx %s]\n", chid, inst << 12,
1206
			 nvkm_client_name(engctx));
B
Ben Skeggs 已提交
1207
		gf100_gr_trap_intr(gr);
1208
		nvkm_wr32(device, 0x400100, 0x00200000);
1209 1210 1211 1212
		stat &= ~0x00200000;
	}

	if (stat & 0x00080000) {
B
Ben Skeggs 已提交
1213
		gf100_gr_ctxctl_isr(gr);
1214
		nvkm_wr32(device, 0x400100, 0x00080000);
1215 1216 1217 1218
		stat &= ~0x00080000;
	}

	if (stat) {
B
Ben Skeggs 已提交
1219
		nv_error(gr, "unknown stat 0x%08x\n", stat);
1220
		nvkm_wr32(device, 0x400100, stat);
1221 1222
	}

1223
	nvkm_wr32(device, 0x400500, 0x00010001);
1224
	nvkm_engctx_put(engctx);
1225 1226
}

1227
void
B
Ben Skeggs 已提交
1228
gf100_gr_init_fw(struct gf100_gr *gr, u32 fuc_base,
1229
		 struct gf100_gr_fuc *code, struct gf100_gr_fuc *data)
1230
{
1231
	struct nvkm_device *device = gr->base.engine.subdev.device;
1232
	int i;
1233

1234
	nvkm_wr32(device, fuc_base + 0x01c0, 0x01000000);
1235
	for (i = 0; i < data->size / 4; i++)
1236
		nvkm_wr32(device, fuc_base + 0x01c4, data->data[i]);
1237

1238
	nvkm_wr32(device, fuc_base + 0x0180, 0x01000000);
1239 1240
	for (i = 0; i < code->size / 4; i++) {
		if ((i & 0x3f) == 0)
1241 1242
			nvkm_wr32(device, fuc_base + 0x0188, i >> 6);
		nvkm_wr32(device, fuc_base + 0x0184, code->data[i]);
1243
	}
1244 1245 1246

	/* code must be padded to 0x40 words */
	for (; i & 0x3f; i++)
1247
		nvkm_wr32(device, fuc_base + 0x0184, 0);
1248 1249
}

1250
static void
B
Ben Skeggs 已提交
1251
gf100_gr_init_csdata(struct gf100_gr *gr,
1252 1253
		     const struct gf100_gr_pack *pack,
		     u32 falcon, u32 starstar, u32 base)
1254
{
1255
	struct nvkm_device *device = gr->base.engine.subdev.device;
1256 1257
	const struct gf100_gr_pack *iter;
	const struct gf100_gr_init *init;
1258
	u32 addr = ~0, prev = ~0, xfer = 0;
1259 1260
	u32 star, temp;

1261 1262 1263
	nvkm_wr32(device, falcon + 0x01c0, 0x02000000 + starstar);
	star = nvkm_rd32(device, falcon + 0x01c4);
	temp = nvkm_rd32(device, falcon + 0x01c4);
1264 1265
	if (temp > star)
		star = temp;
1266
	nvkm_wr32(device, falcon + 0x01c0, 0x01000000 + star);
1267

1268 1269 1270 1271 1272 1273 1274
	pack_for_each_init(init, iter, pack) {
		u32 head = init->addr - base;
		u32 tail = head + init->count * init->pitch;
		while (head < tail) {
			if (head != prev + 4 || xfer >= 32) {
				if (xfer) {
					u32 data = ((--xfer << 26) | addr);
1275
					nvkm_wr32(device, falcon + 0x01c4, data);
1276 1277 1278 1279
					star += 4;
				}
				addr = head;
				xfer = 0;
1280
			}
1281 1282 1283
			prev = head;
			xfer = xfer + 1;
			head = head + init->pitch;
1284
		}
1285
	}
1286

1287 1288 1289
	nvkm_wr32(device, falcon + 0x01c4, (--xfer << 26) | addr);
	nvkm_wr32(device, falcon + 0x01c0, 0x01000004 + starstar);
	nvkm_wr32(device, falcon + 0x01c4, star + 4);
1290 1291
}

1292
int
B
Ben Skeggs 已提交
1293
gf100_gr_init_ctxctl(struct gf100_gr *gr)
1294
{
1295
	struct nvkm_device *device = gr->base.engine.subdev.device;
B
Ben Skeggs 已提交
1296 1297
	struct gf100_gr_oclass *oclass = (void *)nv_object(gr)->oclass;
	struct gf100_grctx_oclass *cclass = (void *)nv_engine(gr)->cclass;
1298
	int i;
1299

B
Ben Skeggs 已提交
1300
	if (gr->firmware) {
1301
		/* load fuc microcode */
B
Ben Skeggs 已提交
1302 1303 1304 1305 1306 1307
		nvkm_mc(gr)->unk260(nvkm_mc(gr), 0);
		gf100_gr_init_fw(gr, 0x409000, &gr->fuc409c,
						 &gr->fuc409d);
		gf100_gr_init_fw(gr, 0x41a000, &gr->fuc41ac,
						 &gr->fuc41ad);
		nvkm_mc(gr)->unk260(nvkm_mc(gr), 1);
1308

1309
		/* start both of them running */
1310 1311 1312 1313 1314
		nvkm_wr32(device, 0x409840, 0xffffffff);
		nvkm_wr32(device, 0x41a10c, 0x00000000);
		nvkm_wr32(device, 0x40910c, 0x00000000);
		nvkm_wr32(device, 0x41a100, 0x00000002);
		nvkm_wr32(device, 0x409100, 0x00000002);
B
Ben Skeggs 已提交
1315 1316 1317
		if (!nv_wait(gr, 0x409800, 0x00000001, 0x00000001))
			nv_warn(gr, "0x409800 wait failed\n");

1318 1319 1320
		nvkm_wr32(device, 0x409840, 0xffffffff);
		nvkm_wr32(device, 0x409500, 0x7fffffff);
		nvkm_wr32(device, 0x409504, 0x00000021);
B
Ben Skeggs 已提交
1321

1322 1323 1324
		nvkm_wr32(device, 0x409840, 0xffffffff);
		nvkm_wr32(device, 0x409500, 0x00000000);
		nvkm_wr32(device, 0x409504, 0x00000010);
B
Ben Skeggs 已提交
1325 1326
		if (!nv_wait_ne(gr, 0x409800, 0xffffffff, 0x00000000)) {
			nv_error(gr, "fuc09 req 0x10 timeout\n");
1327 1328
			return -EBUSY;
		}
1329
		gr->size = nvkm_rd32(device, 0x409800);
1330

1331 1332 1333
		nvkm_wr32(device, 0x409840, 0xffffffff);
		nvkm_wr32(device, 0x409500, 0x00000000);
		nvkm_wr32(device, 0x409504, 0x00000016);
B
Ben Skeggs 已提交
1334 1335
		if (!nv_wait_ne(gr, 0x409800, 0xffffffff, 0x00000000)) {
			nv_error(gr, "fuc09 req 0x16 timeout\n");
1336 1337 1338
			return -EBUSY;
		}

1339 1340 1341
		nvkm_wr32(device, 0x409840, 0xffffffff);
		nvkm_wr32(device, 0x409500, 0x00000000);
		nvkm_wr32(device, 0x409504, 0x00000025);
B
Ben Skeggs 已提交
1342 1343
		if (!nv_wait_ne(gr, 0x409800, 0xffffffff, 0x00000000)) {
			nv_error(gr, "fuc09 req 0x25 timeout\n");
1344 1345 1346
			return -EBUSY;
		}

B
Ben Skeggs 已提交
1347
		if (nv_device(gr)->chipset >= 0xe0) {
1348 1349 1350
			nvkm_wr32(device, 0x409800, 0x00000000);
			nvkm_wr32(device, 0x409500, 0x00000001);
			nvkm_wr32(device, 0x409504, 0x00000030);
B
Ben Skeggs 已提交
1351 1352
			if (!nv_wait_ne(gr, 0x409800, 0xffffffff, 0x00000000)) {
				nv_error(gr, "fuc09 req 0x30 timeout\n");
1353 1354 1355
				return -EBUSY;
			}

1356 1357 1358 1359
			nvkm_wr32(device, 0x409810, 0xb00095c8);
			nvkm_wr32(device, 0x409800, 0x00000000);
			nvkm_wr32(device, 0x409500, 0x00000001);
			nvkm_wr32(device, 0x409504, 0x00000031);
B
Ben Skeggs 已提交
1360 1361
			if (!nv_wait_ne(gr, 0x409800, 0xffffffff, 0x00000000)) {
				nv_error(gr, "fuc09 req 0x31 timeout\n");
1362 1363 1364
				return -EBUSY;
			}

1365 1366 1367 1368
			nvkm_wr32(device, 0x409810, 0x00080420);
			nvkm_wr32(device, 0x409800, 0x00000000);
			nvkm_wr32(device, 0x409500, 0x00000001);
			nvkm_wr32(device, 0x409504, 0x00000032);
B
Ben Skeggs 已提交
1369 1370
			if (!nv_wait_ne(gr, 0x409800, 0xffffffff, 0x00000000)) {
				nv_error(gr, "fuc09 req 0x32 timeout\n");
1371 1372 1373
				return -EBUSY;
			}

1374 1375 1376
			nvkm_wr32(device, 0x409614, 0x00000070);
			nvkm_wr32(device, 0x409614, 0x00000770);
			nvkm_wr32(device, 0x40802c, 0x00000001);
1377 1378
		}

B
Ben Skeggs 已提交
1379 1380
		if (gr->data == NULL) {
			int ret = gf100_grctx_generate(gr);
1381
			if (ret) {
B
Ben Skeggs 已提交
1382
				nv_error(gr, "failed to construct context\n");
1383 1384 1385 1386 1387
				return ret;
			}
		}

		return 0;
1388 1389 1390
	} else
	if (!oclass->fecs.ucode) {
		return -ENOSYS;
1391
	}
1392

1393
	/* load HUB microcode */
B
Ben Skeggs 已提交
1394
	nvkm_mc(gr)->unk260(nvkm_mc(gr), 0);
1395
	nvkm_wr32(device, 0x4091c0, 0x01000000);
1396
	for (i = 0; i < oclass->fecs.ucode->data.size / 4; i++)
1397
		nvkm_wr32(device, 0x4091c4, oclass->fecs.ucode->data.data[i]);
1398

1399
	nvkm_wr32(device, 0x409180, 0x01000000);
1400
	for (i = 0; i < oclass->fecs.ucode->code.size / 4; i++) {
1401
		if ((i & 0x3f) == 0)
1402 1403
			nvkm_wr32(device, 0x409188, i >> 6);
		nvkm_wr32(device, 0x409184, oclass->fecs.ucode->code.data[i]);
1404 1405 1406
	}

	/* load GPC microcode */
1407
	nvkm_wr32(device, 0x41a1c0, 0x01000000);
1408
	for (i = 0; i < oclass->gpccs.ucode->data.size / 4; i++)
1409
		nvkm_wr32(device, 0x41a1c4, oclass->gpccs.ucode->data.data[i]);
1410

1411
	nvkm_wr32(device, 0x41a180, 0x01000000);
1412
	for (i = 0; i < oclass->gpccs.ucode->code.size / 4; i++) {
1413
		if ((i & 0x3f) == 0)
1414 1415
			nvkm_wr32(device, 0x41a188, i >> 6);
		nvkm_wr32(device, 0x41a184, oclass->gpccs.ucode->code.data[i]);
1416
	}
B
Ben Skeggs 已提交
1417
	nvkm_mc(gr)->unk260(nvkm_mc(gr), 1);
1418

1419
	/* load register lists */
B
Ben Skeggs 已提交
1420 1421 1422 1423
	gf100_gr_init_csdata(gr, cclass->hub, 0x409000, 0x000, 0x000000);
	gf100_gr_init_csdata(gr, cclass->gpc, 0x41a000, 0x000, 0x418000);
	gf100_gr_init_csdata(gr, cclass->tpc, 0x41a000, 0x004, 0x419800);
	gf100_gr_init_csdata(gr, cclass->ppc, 0x41a000, 0x008, 0x41be00);
1424

1425
	/* start HUB ucode running, it'll init the GPCs */
1426 1427
	nvkm_wr32(device, 0x40910c, 0x00000000);
	nvkm_wr32(device, 0x409100, 0x00000002);
B
Ben Skeggs 已提交
1428 1429 1430
	if (!nv_wait(gr, 0x409800, 0x80000000, 0x80000000)) {
		nv_error(gr, "HUB_INIT timed out\n");
		gf100_gr_ctxctl_debug(gr);
1431 1432 1433
		return -EBUSY;
	}

1434
	gr->size = nvkm_rd32(device, 0x409804);
B
Ben Skeggs 已提交
1435 1436
	if (gr->data == NULL) {
		int ret = gf100_grctx_generate(gr);
1437
		if (ret) {
B
Ben Skeggs 已提交
1438
			nv_error(gr, "failed to construct context\n");
1439 1440
			return ret;
		}
1441 1442 1443
	}

	return 0;
1444 1445
}

1446
int
1447
gf100_gr_init(struct nvkm_object *object)
1448
{
B
Ben Skeggs 已提交
1449
	struct gf100_gr *gr = (void *)object;
1450 1451
	struct nvkm_device *device = gr->base.engine.subdev.device;
	struct gf100_gr_oclass *oclass = (void *)object->oclass;
B
Ben Skeggs 已提交
1452
	const u32 magicgpc918 = DIV_ROUND_UP(0x00800000, gr->tpc_total);
1453 1454 1455 1456
	u32 data[TPC_MAX / 8] = {};
	u8  tpcnr[GPC_MAX];
	int gpc, tpc, rop;
	int ret, i;
1457

B
Ben Skeggs 已提交
1458
	ret = nvkm_gr_init(&gr->base);
1459 1460 1461
	if (ret)
		return ret;

1462 1463 1464 1465 1466 1467 1468 1469
	nvkm_wr32(device, GPC_BCAST(0x0880), 0x00000000);
	nvkm_wr32(device, GPC_BCAST(0x08a4), 0x00000000);
	nvkm_wr32(device, GPC_BCAST(0x0888), 0x00000000);
	nvkm_wr32(device, GPC_BCAST(0x088c), 0x00000000);
	nvkm_wr32(device, GPC_BCAST(0x0890), 0x00000000);
	nvkm_wr32(device, GPC_BCAST(0x0894), 0x00000000);
	nvkm_wr32(device, GPC_BCAST(0x08b4), gr->unk4188b4->addr >> 8);
	nvkm_wr32(device, GPC_BCAST(0x08b8), gr->unk4188b8->addr >> 8);
1470

B
Ben Skeggs 已提交
1471
	gf100_gr_mmio(gr, oclass->mmio);
1472

B
Ben Skeggs 已提交
1473 1474
	memcpy(tpcnr, gr->tpc_nr, sizeof(gr->tpc_nr));
	for (i = 0, gpc = -1; i < gr->tpc_total; i++) {
1475
		do {
B
Ben Skeggs 已提交
1476
			gpc = (gpc + 1) % gr->gpc_nr;
1477
		} while (!tpcnr[gpc]);
B
Ben Skeggs 已提交
1478
		tpc = gr->tpc_nr[gpc] - tpcnr[gpc]--;
1479 1480 1481 1482

		data[i / 8] |= tpc << ((i % 8) * 4);
	}

1483 1484 1485 1486
	nvkm_wr32(device, GPC_BCAST(0x0980), data[0]);
	nvkm_wr32(device, GPC_BCAST(0x0984), data[1]);
	nvkm_wr32(device, GPC_BCAST(0x0988), data[2]);
	nvkm_wr32(device, GPC_BCAST(0x098c), data[3]);
1487

B
Ben Skeggs 已提交
1488
	for (gpc = 0; gpc < gr->gpc_nr; gpc++) {
1489
		nvkm_wr32(device, GPC_UNIT(gpc, 0x0914),
B
Ben Skeggs 已提交
1490
			gr->magic_not_rop_nr << 8 | gr->tpc_nr[gpc]);
1491
		nvkm_wr32(device, GPC_UNIT(gpc, 0x0910), 0x00040000 |
B
Ben Skeggs 已提交
1492
			gr->tpc_total);
1493
		nvkm_wr32(device, GPC_UNIT(gpc, 0x0918), magicgpc918);
1494 1495
	}

B
Ben Skeggs 已提交
1496
	if (nv_device(gr)->chipset != 0xd7)
1497
		nvkm_wr32(device, GPC_BCAST(0x1bd4), magicgpc918);
M
Maarten Lankhorst 已提交
1498
	else
1499
		nvkm_wr32(device, GPC_BCAST(0x3fd4), magicgpc918);
B
Ben Skeggs 已提交
1500

1501
	nvkm_wr32(device, GPC_BCAST(0x08ac), nvkm_rd32(device, 0x100800));
B
Ben Skeggs 已提交
1502

1503
	nvkm_wr32(device, 0x400500, 0x00010001);
B
Ben Skeggs 已提交
1504

1505 1506
	nvkm_wr32(device, 0x400100, 0xffffffff);
	nvkm_wr32(device, 0x40013c, 0xffffffff);
B
Ben Skeggs 已提交
1507

1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
	nvkm_wr32(device, 0x409c24, 0x000f0000);
	nvkm_wr32(device, 0x404000, 0xc0000000);
	nvkm_wr32(device, 0x404600, 0xc0000000);
	nvkm_wr32(device, 0x408030, 0xc0000000);
	nvkm_wr32(device, 0x40601c, 0xc0000000);
	nvkm_wr32(device, 0x404490, 0xc0000000);
	nvkm_wr32(device, 0x406018, 0xc0000000);
	nvkm_wr32(device, 0x405840, 0xc0000000);
	nvkm_wr32(device, 0x405844, 0x00ffffff);
	nvkm_mask(device, 0x419cc0, 0x00000008, 0x00000008);
	nvkm_mask(device, 0x419eb4, 0x00001000, 0x00001000);
B
Ben Skeggs 已提交
1519 1520

	for (gpc = 0; gpc < gr->gpc_nr; gpc++) {
1521 1522 1523 1524
		nvkm_wr32(device, GPC_UNIT(gpc, 0x0420), 0xc0000000);
		nvkm_wr32(device, GPC_UNIT(gpc, 0x0900), 0xc0000000);
		nvkm_wr32(device, GPC_UNIT(gpc, 0x1028), 0xc0000000);
		nvkm_wr32(device, GPC_UNIT(gpc, 0x0824), 0xc0000000);
B
Ben Skeggs 已提交
1525
		for (tpc = 0; tpc < gr->tpc_nr[gpc]; tpc++) {
1526 1527 1528 1529 1530 1531 1532
			nvkm_wr32(device, TPC_UNIT(gpc, tpc, 0x508), 0xffffffff);
			nvkm_wr32(device, TPC_UNIT(gpc, tpc, 0x50c), 0xffffffff);
			nvkm_wr32(device, TPC_UNIT(gpc, tpc, 0x224), 0xc0000000);
			nvkm_wr32(device, TPC_UNIT(gpc, tpc, 0x48c), 0xc0000000);
			nvkm_wr32(device, TPC_UNIT(gpc, tpc, 0x084), 0xc0000000);
			nvkm_wr32(device, TPC_UNIT(gpc, tpc, 0x644), 0x001ffffe);
			nvkm_wr32(device, TPC_UNIT(gpc, tpc, 0x64c), 0x0000000f);
1533
		}
1534 1535
		nvkm_wr32(device, GPC_UNIT(gpc, 0x2c90), 0xffffffff);
		nvkm_wr32(device, GPC_UNIT(gpc, 0x2c94), 0xffffffff);
1536 1537
	}

B
Ben Skeggs 已提交
1538
	for (rop = 0; rop < gr->rop_nr; rop++) {
1539 1540 1541 1542
		nvkm_wr32(device, ROP_UNIT(rop, 0x144), 0xc0000000);
		nvkm_wr32(device, ROP_UNIT(rop, 0x070), 0xc0000000);
		nvkm_wr32(device, ROP_UNIT(rop, 0x204), 0xffffffff);
		nvkm_wr32(device, ROP_UNIT(rop, 0x208), 0xffffffff);
1543
	}
1544

1545 1546 1547 1548 1549 1550
	nvkm_wr32(device, 0x400108, 0xffffffff);
	nvkm_wr32(device, 0x400138, 0xffffffff);
	nvkm_wr32(device, 0x400118, 0xffffffff);
	nvkm_wr32(device, 0x400130, 0xffffffff);
	nvkm_wr32(device, 0x40011c, 0xffffffff);
	nvkm_wr32(device, 0x400134, 0xffffffff);
1551

1552
	nvkm_wr32(device, 0x400054, 0x34ce3464);
1553

B
Ben Skeggs 已提交
1554
	gf100_gr_zbc_init(gr);
1555

B
Ben Skeggs 已提交
1556
	return gf100_gr_init_ctxctl(gr);
1557 1558
}

1559
void
1560
gf100_gr_dtor_fw(struct gf100_gr_fuc *fuc)
1561 1562 1563 1564 1565 1566
{
	kfree(fuc->data);
	fuc->data = NULL;
}

int
B
Ben Skeggs 已提交
1567
gf100_gr_ctor_fw(struct gf100_gr *gr, const char *fwname,
1568
		 struct gf100_gr_fuc *fuc)
1569
{
B
Ben Skeggs 已提交
1570
	struct nvkm_device *device = nv_device(gr);
1571
	const struct firmware *fw;
1572 1573
	char f[64];
	char cname[16];
1574
	int ret;
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
	int i;

	/* Convert device name to lowercase */
	strncpy(cname, device->cname, sizeof(cname));
	cname[sizeof(cname) - 1] = '\0';
	i = strlen(cname);
	while (i) {
		--i;
		cname[i] = tolower(cname[i]);
	}
1585

1586
	snprintf(f, sizeof(f), "nvidia/%s/%s.bin", cname, fwname);
A
Alexandre Courbot 已提交
1587
	ret = request_firmware(&fw, f, nv_device_base(device));
1588
	if (ret) {
B
Ben Skeggs 已提交
1589
		nv_error(gr, "failed to load %s\n", fwname);
1590
		return ret;
1591 1592 1593 1594 1595 1596 1597 1598 1599
	}

	fuc->size = fw->size;
	fuc->data = kmemdup(fw->data, fuc->size, GFP_KERNEL);
	release_firmware(fw);
	return (fuc->data != NULL) ? 0 : -ENOMEM;
}

void
1600
gf100_gr_dtor(struct nvkm_object *object)
1601
{
B
Ben Skeggs 已提交
1602
	struct gf100_gr *gr = (void *)object;
1603

B
Ben Skeggs 已提交
1604
	kfree(gr->data);
1605

B
Ben Skeggs 已提交
1606 1607 1608 1609
	gf100_gr_dtor_fw(&gr->fuc409c);
	gf100_gr_dtor_fw(&gr->fuc409d);
	gf100_gr_dtor_fw(&gr->fuc41ac);
	gf100_gr_dtor_fw(&gr->fuc41ad);
1610

B
Ben Skeggs 已提交
1611 1612
	nvkm_gpuobj_ref(NULL, &gr->unk4188b8);
	nvkm_gpuobj_ref(NULL, &gr->unk4188b4);
1613

B
Ben Skeggs 已提交
1614
	nvkm_gr_destroy(&gr->base);
1615 1616 1617
}

int
1618 1619 1620
gf100_gr_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
	      struct nvkm_oclass *bclass, void *data, u32 size,
	      struct nvkm_object **pobject)
1621
{
1622 1623
	struct gf100_gr_oclass *oclass = (void *)bclass;
	struct nvkm_device *device = nv_device(parent);
B
Ben Skeggs 已提交
1624
	struct gf100_gr *gr;
1625
	bool use_ext_fw, enable;
1626
	int ret, i, j;
1627

1628 1629
	use_ext_fw = nvkm_boolopt(device->cfgopt, "NvGrUseFW",
				  oclass->fecs.ucode == NULL);
1630 1631
	enable = use_ext_fw || oclass->fecs.ucode != NULL;

B
Ben Skeggs 已提交
1632 1633
	ret = nvkm_gr_create(parent, engine, bclass, enable, &gr);
	*pobject = nv_object(gr);
1634 1635 1636
	if (ret)
		return ret;

B
Ben Skeggs 已提交
1637 1638
	nv_subdev(gr)->unit = 0x08001000;
	nv_subdev(gr)->intr = gf100_gr_intr;
1639

B
Ben Skeggs 已提交
1640
	gr->base.units = gf100_gr_units;
1641

1642
	if (use_ext_fw) {
B
Ben Skeggs 已提交
1643 1644 1645 1646 1647
		nv_info(gr, "using external firmware\n");
		if (gf100_gr_ctor_fw(gr, "fecs_inst", &gr->fuc409c) ||
		    gf100_gr_ctor_fw(gr, "fecs_data", &gr->fuc409d) ||
		    gf100_gr_ctor_fw(gr, "gpccs_inst", &gr->fuc41ac) ||
		    gf100_gr_ctor_fw(gr, "gpccs_data", &gr->fuc41ad))
1648
			return -ENODEV;
B
Ben Skeggs 已提交
1649
		gr->firmware = true;
1650 1651
	}

B
Ben Skeggs 已提交
1652 1653
	ret = nvkm_gpuobj_new(nv_object(gr), NULL, 0x1000, 256, 0,
			      &gr->unk4188b4);
1654 1655
	if (ret)
		return ret;
1656

B
Ben Skeggs 已提交
1657 1658
	ret = nvkm_gpuobj_new(nv_object(gr), NULL, 0x1000, 256, 0,
			      &gr->unk4188b8);
1659
	if (ret)
1660 1661
		return ret;

1662
	for (i = 0; i < 0x1000; i += 4) {
B
Ben Skeggs 已提交
1663 1664 1665 1666
		nv_wo32(gr->unk4188b4, i, 0x00000010);
		nv_wo32(gr->unk4188b8, i, 0x00000010);
	}

1667 1668
	gr->rop_nr = (nvkm_rd32(device, 0x409604) & 0x001f0000) >> 16;
	gr->gpc_nr =  nvkm_rd32(device, 0x409604) & 0x0000001f;
B
Ben Skeggs 已提交
1669
	for (i = 0; i < gr->gpc_nr; i++) {
1670
		gr->tpc_nr[i]  = nvkm_rd32(device, GPC_UNIT(i, 0x2608));
B
Ben Skeggs 已提交
1671 1672 1673
		gr->tpc_total += gr->tpc_nr[i];
		gr->ppc_nr[i]  = oclass->ppc_nr;
		for (j = 0; j < gr->ppc_nr[i]; j++) {
1674
			u8 mask = nvkm_rd32(device, GPC_UNIT(i, 0x0c30 + (j * 4)));
B
Ben Skeggs 已提交
1675
			gr->ppc_tpc_nr[i][j] = hweight8(mask);
1676
		}
1677 1678 1679
	}

	/*XXX: these need figuring out... though it might not even matter */
B
Ben Skeggs 已提交
1680
	switch (nv_device(gr)->chipset) {
1681
	case 0xc0:
B
Ben Skeggs 已提交
1682 1683
		if (gr->tpc_total == 11) { /* 465, 3/4/4/0, 4 */
			gr->magic_not_rop_nr = 0x07;
1684
		} else
B
Ben Skeggs 已提交
1685 1686
		if (gr->tpc_total == 14) { /* 470, 3/3/4/4, 5 */
			gr->magic_not_rop_nr = 0x05;
1687
		} else
B
Ben Skeggs 已提交
1688 1689
		if (gr->tpc_total == 15) { /* 480, 3/4/4/4, 6 */
			gr->magic_not_rop_nr = 0x06;
1690 1691 1692
		}
		break;
	case 0xc3: /* 450, 4/0/0/0, 2 */
B
Ben Skeggs 已提交
1693
		gr->magic_not_rop_nr = 0x03;
1694 1695
		break;
	case 0xc4: /* 460, 3/4/0/0, 4 */
B
Ben Skeggs 已提交
1696
		gr->magic_not_rop_nr = 0x01;
1697 1698
		break;
	case 0xc1: /* 2/0/0/0, 1 */
B
Ben Skeggs 已提交
1699
		gr->magic_not_rop_nr = 0x01;
1700 1701
		break;
	case 0xc8: /* 4/4/3/4, 5 */
B
Ben Skeggs 已提交
1702
		gr->magic_not_rop_nr = 0x06;
1703 1704
		break;
	case 0xce: /* 4/4/0/0, 4 */
B
Ben Skeggs 已提交
1705
		gr->magic_not_rop_nr = 0x03;
1706 1707
		break;
	case 0xcf: /* 4/0/0/0, 3 */
B
Ben Skeggs 已提交
1708
		gr->magic_not_rop_nr = 0x03;
1709
		break;
M
Maarten Lankhorst 已提交
1710
	case 0xd7:
1711
	case 0xd9: /* 1/0/0/0, 1 */
1712
	case 0xea: /* gk20a */
1713
	case 0x12b: /* gm20b */
B
Ben Skeggs 已提交
1714
		gr->magic_not_rop_nr = 0x01;
1715 1716 1717
		break;
	}

B
Ben Skeggs 已提交
1718 1719
	nv_engine(gr)->cclass = *oclass->cclass;
	nv_engine(gr)->sclass =  oclass->sclass;
1720 1721 1722
	return 0;
}

1723
#include "fuc/hubgf100.fuc3.h"
1724

1725 1726 1727 1728 1729 1730
struct gf100_gr_ucode
gf100_gr_fecs_ucode = {
	.code.data = gf100_grhub_code,
	.code.size = sizeof(gf100_grhub_code),
	.data.data = gf100_grhub_data,
	.data.size = sizeof(gf100_grhub_data),
1731 1732
};

1733
#include "fuc/gpcgf100.fuc3.h"
1734

1735 1736 1737 1738 1739 1740
struct gf100_gr_ucode
gf100_gr_gpccs_ucode = {
	.code.data = gf100_grgpc_code,
	.code.size = sizeof(gf100_grgpc_code),
	.data.data = gf100_grgpc_data,
	.data.size = sizeof(gf100_grgpc_data),
1741 1742
};

1743 1744
struct nvkm_oclass *
gf100_gr_oclass = &(struct gf100_gr_oclass) {
1745
	.base.handle = NV_ENGINE(GR, 0xc0),
1746 1747 1748 1749 1750
	.base.ofuncs = &(struct nvkm_ofuncs) {
		.ctor = gf100_gr_ctor,
		.dtor = gf100_gr_dtor,
		.init = gf100_gr_init,
		.fini = _nvkm_gr_fini,
1751
	},
1752 1753 1754 1755 1756
	.cclass = &gf100_grctx_oclass,
	.sclass =  gf100_gr_sclass,
	.mmio = gf100_gr_pack_mmio,
	.fecs.ucode = &gf100_gr_fecs_ucode,
	.gpccs.ucode = &gf100_gr_gpccs_ucode,
1757
}.base;