162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright 2013 Ilia Mirkin
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
562306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
662306a36Sopenharmony_ci * to deal in the Software without restriction, including without limitation
762306a36Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
862306a36Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
962306a36Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
1262306a36Sopenharmony_ci * all copies or substantial portions of the Software.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1562306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1662306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1762306a36Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
1862306a36Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1962306a36Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2062306a36Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
2162306a36Sopenharmony_ci */
2262306a36Sopenharmony_ci#include <engine/xtensa.h>
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#include <core/gpuobj.h>
2562306a36Sopenharmony_ci#include <engine/fifo.h>
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_cistatic int
2862306a36Sopenharmony_cinvkm_xtensa_oclass_get(struct nvkm_oclass *oclass, int index)
2962306a36Sopenharmony_ci{
3062306a36Sopenharmony_ci	struct nvkm_xtensa *xtensa = nvkm_xtensa(oclass->engine);
3162306a36Sopenharmony_ci	int c = 0;
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci	while (xtensa->func->sclass[c].oclass) {
3462306a36Sopenharmony_ci		if (c++ == index) {
3562306a36Sopenharmony_ci			oclass->base = xtensa->func->sclass[index];
3662306a36Sopenharmony_ci			return index;
3762306a36Sopenharmony_ci		}
3862306a36Sopenharmony_ci	}
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci	return c;
4162306a36Sopenharmony_ci}
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cistatic int
4462306a36Sopenharmony_cinvkm_xtensa_cclass_bind(struct nvkm_object *object, struct nvkm_gpuobj *parent,
4562306a36Sopenharmony_ci			int align, struct nvkm_gpuobj **pgpuobj)
4662306a36Sopenharmony_ci{
4762306a36Sopenharmony_ci	return nvkm_gpuobj_new(object->engine->subdev.device, 0x10000, align,
4862306a36Sopenharmony_ci			       true, parent, pgpuobj);
4962306a36Sopenharmony_ci}
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_cistatic const struct nvkm_object_func
5262306a36Sopenharmony_cinvkm_xtensa_cclass = {
5362306a36Sopenharmony_ci	.bind = nvkm_xtensa_cclass_bind,
5462306a36Sopenharmony_ci};
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_cistatic void
5762306a36Sopenharmony_cinvkm_xtensa_intr(struct nvkm_engine *engine)
5862306a36Sopenharmony_ci{
5962306a36Sopenharmony_ci	struct nvkm_xtensa *xtensa = nvkm_xtensa(engine);
6062306a36Sopenharmony_ci	struct nvkm_subdev *subdev = &xtensa->engine.subdev;
6162306a36Sopenharmony_ci	struct nvkm_device *device = subdev->device;
6262306a36Sopenharmony_ci	const u32 base = xtensa->addr;
6362306a36Sopenharmony_ci	u32 unk104 = nvkm_rd32(device, base + 0xd04);
6462306a36Sopenharmony_ci	u32 intr = nvkm_rd32(device, base + 0xc20);
6562306a36Sopenharmony_ci	u32 chan = nvkm_rd32(device, base + 0xc28);
6662306a36Sopenharmony_ci	u32 unk10c = nvkm_rd32(device, base + 0xd0c);
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	if (intr & 0x10)
6962306a36Sopenharmony_ci		nvkm_warn(subdev, "Watchdog interrupt, engine hung.\n");
7062306a36Sopenharmony_ci	nvkm_wr32(device, base + 0xc20, intr);
7162306a36Sopenharmony_ci	intr = nvkm_rd32(device, base + 0xc20);
7262306a36Sopenharmony_ci	if (unk104 == 0x10001 && unk10c == 0x200 && chan && !intr) {
7362306a36Sopenharmony_ci		nvkm_debug(subdev, "Enabling FIFO_CTRL\n");
7462306a36Sopenharmony_ci		nvkm_mask(device, xtensa->addr + 0xd94, 0, xtensa->func->fifo_val);
7562306a36Sopenharmony_ci	}
7662306a36Sopenharmony_ci}
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_cistatic int
7962306a36Sopenharmony_cinvkm_xtensa_fini(struct nvkm_engine *engine, bool suspend)
8062306a36Sopenharmony_ci{
8162306a36Sopenharmony_ci	struct nvkm_xtensa *xtensa = nvkm_xtensa(engine);
8262306a36Sopenharmony_ci	struct nvkm_device *device = xtensa->engine.subdev.device;
8362306a36Sopenharmony_ci	const u32 base = xtensa->addr;
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci	nvkm_wr32(device, base + 0xd84, 0); /* INTR_EN */
8662306a36Sopenharmony_ci	nvkm_wr32(device, base + 0xd94, 0); /* FIFO_CTRL */
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	if (!suspend)
8962306a36Sopenharmony_ci		nvkm_memory_unref(&xtensa->gpu_fw);
9062306a36Sopenharmony_ci	return 0;
9162306a36Sopenharmony_ci}
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_cistatic int
9462306a36Sopenharmony_cinvkm_xtensa_init(struct nvkm_engine *engine)
9562306a36Sopenharmony_ci{
9662306a36Sopenharmony_ci	struct nvkm_xtensa *xtensa = nvkm_xtensa(engine);
9762306a36Sopenharmony_ci	struct nvkm_subdev *subdev = &xtensa->engine.subdev;
9862306a36Sopenharmony_ci	struct nvkm_device *device = subdev->device;
9962306a36Sopenharmony_ci	const u32 base = xtensa->addr;
10062306a36Sopenharmony_ci	const struct firmware *fw;
10162306a36Sopenharmony_ci	char name[32];
10262306a36Sopenharmony_ci	int i, ret;
10362306a36Sopenharmony_ci	u64 addr, size;
10462306a36Sopenharmony_ci	u32 tmp;
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	if (!xtensa->gpu_fw) {
10762306a36Sopenharmony_ci		snprintf(name, sizeof(name), "nouveau/nv84_xuc%03x",
10862306a36Sopenharmony_ci			 xtensa->addr >> 12);
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci		ret = request_firmware(&fw, name, device->dev);
11162306a36Sopenharmony_ci		if (ret) {
11262306a36Sopenharmony_ci			nvkm_warn(subdev, "unable to load firmware %s\n", name);
11362306a36Sopenharmony_ci			return ret;
11462306a36Sopenharmony_ci		}
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci		if (fw->size > 0x40000) {
11762306a36Sopenharmony_ci			nvkm_warn(subdev, "firmware %s too large\n", name);
11862306a36Sopenharmony_ci			release_firmware(fw);
11962306a36Sopenharmony_ci			return -EINVAL;
12062306a36Sopenharmony_ci		}
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci		ret = nvkm_memory_new(device, NVKM_MEM_TARGET_INST,
12362306a36Sopenharmony_ci				      0x40000, 0x1000, false,
12462306a36Sopenharmony_ci				      &xtensa->gpu_fw);
12562306a36Sopenharmony_ci		if (ret) {
12662306a36Sopenharmony_ci			release_firmware(fw);
12762306a36Sopenharmony_ci			return ret;
12862306a36Sopenharmony_ci		}
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci		nvkm_kmap(xtensa->gpu_fw);
13162306a36Sopenharmony_ci		for (i = 0; i < fw->size / 4; i++)
13262306a36Sopenharmony_ci			nvkm_wo32(xtensa->gpu_fw, i * 4, *((u32 *)fw->data + i));
13362306a36Sopenharmony_ci		nvkm_done(xtensa->gpu_fw);
13462306a36Sopenharmony_ci		release_firmware(fw);
13562306a36Sopenharmony_ci	}
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci	addr = nvkm_memory_addr(xtensa->gpu_fw);
13862306a36Sopenharmony_ci	size = nvkm_memory_size(xtensa->gpu_fw);
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	nvkm_wr32(device, base + 0xd10, 0x1fffffff); /* ?? */
14162306a36Sopenharmony_ci	nvkm_wr32(device, base + 0xd08, 0x0fffffff); /* ?? */
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	nvkm_wr32(device, base + 0xd28, xtensa->func->unkd28); /* ?? */
14462306a36Sopenharmony_ci	nvkm_wr32(device, base + 0xc20, 0x3f); /* INTR */
14562306a36Sopenharmony_ci	nvkm_wr32(device, base + 0xd84, 0x3f); /* INTR_EN */
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci	nvkm_wr32(device, base + 0xcc0, addr >> 8); /* XT_REGION_BASE */
14862306a36Sopenharmony_ci	nvkm_wr32(device, base + 0xcc4, 0x1c); /* XT_REGION_SETUP */
14962306a36Sopenharmony_ci	nvkm_wr32(device, base + 0xcc8, size >> 8); /* XT_REGION_LIMIT */
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci	tmp = nvkm_rd32(device, 0x0);
15262306a36Sopenharmony_ci	nvkm_wr32(device, base + 0xde0, tmp); /* SCRATCH_H2X */
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci	nvkm_wr32(device, base + 0xce8, 0xf); /* XT_REGION_SETUP */
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci	nvkm_wr32(device, base + 0xc20, 0x3f); /* INTR */
15762306a36Sopenharmony_ci	nvkm_wr32(device, base + 0xd84, 0x3f); /* INTR_EN */
15862306a36Sopenharmony_ci	return 0;
15962306a36Sopenharmony_ci}
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_cistatic void *
16262306a36Sopenharmony_cinvkm_xtensa_dtor(struct nvkm_engine *engine)
16362306a36Sopenharmony_ci{
16462306a36Sopenharmony_ci	return nvkm_xtensa(engine);
16562306a36Sopenharmony_ci}
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_cistatic const struct nvkm_engine_func
16862306a36Sopenharmony_cinvkm_xtensa = {
16962306a36Sopenharmony_ci	.dtor = nvkm_xtensa_dtor,
17062306a36Sopenharmony_ci	.init = nvkm_xtensa_init,
17162306a36Sopenharmony_ci	.fini = nvkm_xtensa_fini,
17262306a36Sopenharmony_ci	.intr = nvkm_xtensa_intr,
17362306a36Sopenharmony_ci	.fifo.sclass = nvkm_xtensa_oclass_get,
17462306a36Sopenharmony_ci	.cclass = &nvkm_xtensa_cclass,
17562306a36Sopenharmony_ci};
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ciint
17862306a36Sopenharmony_cinvkm_xtensa_new_(const struct nvkm_xtensa_func *func, struct nvkm_device *device,
17962306a36Sopenharmony_ci		 enum nvkm_subdev_type type, int inst, bool enable, u32 addr,
18062306a36Sopenharmony_ci		 struct nvkm_engine **pengine)
18162306a36Sopenharmony_ci{
18262306a36Sopenharmony_ci	struct nvkm_xtensa *xtensa;
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci	if (!(xtensa = kzalloc(sizeof(*xtensa), GFP_KERNEL)))
18562306a36Sopenharmony_ci		return -ENOMEM;
18662306a36Sopenharmony_ci	xtensa->func = func;
18762306a36Sopenharmony_ci	xtensa->addr = addr;
18862306a36Sopenharmony_ci	*pengine = &xtensa->engine;
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci	return nvkm_engine_ctor(&nvkm_xtensa, device, type, inst, enable, &xtensa->engine);
19162306a36Sopenharmony_ci}
192