18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2013 Ilia Mirkin
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
128c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
158c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
168c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
178c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
188c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
198c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
208c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ci#include <engine/xtensa.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include <core/gpuobj.h>
258c2ecf20Sopenharmony_ci#include <engine/fifo.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic int
288c2ecf20Sopenharmony_cinvkm_xtensa_oclass_get(struct nvkm_oclass *oclass, int index)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	struct nvkm_xtensa *xtensa = nvkm_xtensa(oclass->engine);
318c2ecf20Sopenharmony_ci	int c = 0;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	while (xtensa->func->sclass[c].oclass) {
348c2ecf20Sopenharmony_ci		if (c++ == index) {
358c2ecf20Sopenharmony_ci			oclass->base = xtensa->func->sclass[index];
368c2ecf20Sopenharmony_ci			return index;
378c2ecf20Sopenharmony_ci		}
388c2ecf20Sopenharmony_ci	}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	return c;
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic int
448c2ecf20Sopenharmony_cinvkm_xtensa_cclass_bind(struct nvkm_object *object, struct nvkm_gpuobj *parent,
458c2ecf20Sopenharmony_ci			int align, struct nvkm_gpuobj **pgpuobj)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	return nvkm_gpuobj_new(object->engine->subdev.device, 0x10000, align,
488c2ecf20Sopenharmony_ci			       true, parent, pgpuobj);
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic const struct nvkm_object_func
528c2ecf20Sopenharmony_cinvkm_xtensa_cclass = {
538c2ecf20Sopenharmony_ci	.bind = nvkm_xtensa_cclass_bind,
548c2ecf20Sopenharmony_ci};
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic void
578c2ecf20Sopenharmony_cinvkm_xtensa_intr(struct nvkm_engine *engine)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	struct nvkm_xtensa *xtensa = nvkm_xtensa(engine);
608c2ecf20Sopenharmony_ci	struct nvkm_subdev *subdev = &xtensa->engine.subdev;
618c2ecf20Sopenharmony_ci	struct nvkm_device *device = subdev->device;
628c2ecf20Sopenharmony_ci	const u32 base = xtensa->addr;
638c2ecf20Sopenharmony_ci	u32 unk104 = nvkm_rd32(device, base + 0xd04);
648c2ecf20Sopenharmony_ci	u32 intr = nvkm_rd32(device, base + 0xc20);
658c2ecf20Sopenharmony_ci	u32 chan = nvkm_rd32(device, base + 0xc28);
668c2ecf20Sopenharmony_ci	u32 unk10c = nvkm_rd32(device, base + 0xd0c);
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	if (intr & 0x10)
698c2ecf20Sopenharmony_ci		nvkm_warn(subdev, "Watchdog interrupt, engine hung.\n");
708c2ecf20Sopenharmony_ci	nvkm_wr32(device, base + 0xc20, intr);
718c2ecf20Sopenharmony_ci	intr = nvkm_rd32(device, base + 0xc20);
728c2ecf20Sopenharmony_ci	if (unk104 == 0x10001 && unk10c == 0x200 && chan && !intr) {
738c2ecf20Sopenharmony_ci		nvkm_debug(subdev, "Enabling FIFO_CTRL\n");
748c2ecf20Sopenharmony_ci		nvkm_mask(device, xtensa->addr + 0xd94, 0, xtensa->func->fifo_val);
758c2ecf20Sopenharmony_ci	}
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic int
798c2ecf20Sopenharmony_cinvkm_xtensa_fini(struct nvkm_engine *engine, bool suspend)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	struct nvkm_xtensa *xtensa = nvkm_xtensa(engine);
828c2ecf20Sopenharmony_ci	struct nvkm_device *device = xtensa->engine.subdev.device;
838c2ecf20Sopenharmony_ci	const u32 base = xtensa->addr;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	nvkm_wr32(device, base + 0xd84, 0); /* INTR_EN */
868c2ecf20Sopenharmony_ci	nvkm_wr32(device, base + 0xd94, 0); /* FIFO_CTRL */
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	if (!suspend)
898c2ecf20Sopenharmony_ci		nvkm_memory_unref(&xtensa->gpu_fw);
908c2ecf20Sopenharmony_ci	return 0;
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic int
948c2ecf20Sopenharmony_cinvkm_xtensa_init(struct nvkm_engine *engine)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	struct nvkm_xtensa *xtensa = nvkm_xtensa(engine);
978c2ecf20Sopenharmony_ci	struct nvkm_subdev *subdev = &xtensa->engine.subdev;
988c2ecf20Sopenharmony_ci	struct nvkm_device *device = subdev->device;
998c2ecf20Sopenharmony_ci	const u32 base = xtensa->addr;
1008c2ecf20Sopenharmony_ci	const struct firmware *fw;
1018c2ecf20Sopenharmony_ci	char name[32];
1028c2ecf20Sopenharmony_ci	int i, ret;
1038c2ecf20Sopenharmony_ci	u64 addr, size;
1048c2ecf20Sopenharmony_ci	u32 tmp;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	if (!xtensa->gpu_fw) {
1078c2ecf20Sopenharmony_ci		snprintf(name, sizeof(name), "nouveau/nv84_xuc%03x",
1088c2ecf20Sopenharmony_ci			 xtensa->addr >> 12);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci		ret = request_firmware(&fw, name, device->dev);
1118c2ecf20Sopenharmony_ci		if (ret) {
1128c2ecf20Sopenharmony_ci			nvkm_warn(subdev, "unable to load firmware %s\n", name);
1138c2ecf20Sopenharmony_ci			return ret;
1148c2ecf20Sopenharmony_ci		}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci		if (fw->size > 0x40000) {
1178c2ecf20Sopenharmony_ci			nvkm_warn(subdev, "firmware %s too large\n", name);
1188c2ecf20Sopenharmony_ci			release_firmware(fw);
1198c2ecf20Sopenharmony_ci			return -EINVAL;
1208c2ecf20Sopenharmony_ci		}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci		ret = nvkm_memory_new(device, NVKM_MEM_TARGET_INST,
1238c2ecf20Sopenharmony_ci				      0x40000, 0x1000, false,
1248c2ecf20Sopenharmony_ci				      &xtensa->gpu_fw);
1258c2ecf20Sopenharmony_ci		if (ret) {
1268c2ecf20Sopenharmony_ci			release_firmware(fw);
1278c2ecf20Sopenharmony_ci			return ret;
1288c2ecf20Sopenharmony_ci		}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci		nvkm_kmap(xtensa->gpu_fw);
1318c2ecf20Sopenharmony_ci		for (i = 0; i < fw->size / 4; i++)
1328c2ecf20Sopenharmony_ci			nvkm_wo32(xtensa->gpu_fw, i * 4, *((u32 *)fw->data + i));
1338c2ecf20Sopenharmony_ci		nvkm_done(xtensa->gpu_fw);
1348c2ecf20Sopenharmony_ci		release_firmware(fw);
1358c2ecf20Sopenharmony_ci	}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	addr = nvkm_memory_addr(xtensa->gpu_fw);
1388c2ecf20Sopenharmony_ci	size = nvkm_memory_size(xtensa->gpu_fw);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	nvkm_wr32(device, base + 0xd10, 0x1fffffff); /* ?? */
1418c2ecf20Sopenharmony_ci	nvkm_wr32(device, base + 0xd08, 0x0fffffff); /* ?? */
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	nvkm_wr32(device, base + 0xd28, xtensa->func->unkd28); /* ?? */
1448c2ecf20Sopenharmony_ci	nvkm_wr32(device, base + 0xc20, 0x3f); /* INTR */
1458c2ecf20Sopenharmony_ci	nvkm_wr32(device, base + 0xd84, 0x3f); /* INTR_EN */
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	nvkm_wr32(device, base + 0xcc0, addr >> 8); /* XT_REGION_BASE */
1488c2ecf20Sopenharmony_ci	nvkm_wr32(device, base + 0xcc4, 0x1c); /* XT_REGION_SETUP */
1498c2ecf20Sopenharmony_ci	nvkm_wr32(device, base + 0xcc8, size >> 8); /* XT_REGION_LIMIT */
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	tmp = nvkm_rd32(device, 0x0);
1528c2ecf20Sopenharmony_ci	nvkm_wr32(device, base + 0xde0, tmp); /* SCRATCH_H2X */
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	nvkm_wr32(device, base + 0xce8, 0xf); /* XT_REGION_SETUP */
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	nvkm_wr32(device, base + 0xc20, 0x3f); /* INTR */
1578c2ecf20Sopenharmony_ci	nvkm_wr32(device, base + 0xd84, 0x3f); /* INTR_EN */
1588c2ecf20Sopenharmony_ci	return 0;
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic void *
1628c2ecf20Sopenharmony_cinvkm_xtensa_dtor(struct nvkm_engine *engine)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	return nvkm_xtensa(engine);
1658c2ecf20Sopenharmony_ci}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic const struct nvkm_engine_func
1688c2ecf20Sopenharmony_cinvkm_xtensa = {
1698c2ecf20Sopenharmony_ci	.dtor = nvkm_xtensa_dtor,
1708c2ecf20Sopenharmony_ci	.init = nvkm_xtensa_init,
1718c2ecf20Sopenharmony_ci	.fini = nvkm_xtensa_fini,
1728c2ecf20Sopenharmony_ci	.intr = nvkm_xtensa_intr,
1738c2ecf20Sopenharmony_ci	.fifo.sclass = nvkm_xtensa_oclass_get,
1748c2ecf20Sopenharmony_ci	.cclass = &nvkm_xtensa_cclass,
1758c2ecf20Sopenharmony_ci};
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ciint
1788c2ecf20Sopenharmony_cinvkm_xtensa_new_(const struct nvkm_xtensa_func *func,
1798c2ecf20Sopenharmony_ci		 struct nvkm_device *device, int index, bool enable,
1808c2ecf20Sopenharmony_ci		 u32 addr, struct nvkm_engine **pengine)
1818c2ecf20Sopenharmony_ci{
1828c2ecf20Sopenharmony_ci	struct nvkm_xtensa *xtensa;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	if (!(xtensa = kzalloc(sizeof(*xtensa), GFP_KERNEL)))
1858c2ecf20Sopenharmony_ci		return -ENOMEM;
1868c2ecf20Sopenharmony_ci	xtensa->func = func;
1878c2ecf20Sopenharmony_ci	xtensa->addr = addr;
1888c2ecf20Sopenharmony_ci	*pengine = &xtensa->engine;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	return nvkm_engine_ctor(&nvkm_xtensa, device, index,
1918c2ecf20Sopenharmony_ci				enable, &xtensa->engine);
1928c2ecf20Sopenharmony_ci}
193