162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright 2022 Red Hat Inc.
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 "priv.h"
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#include <subdev/mc.h>
2562306a36Sopenharmony_ci#include <subdev/timer.h>
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_cistatic bool
2862306a36Sopenharmony_ciga102_flcn_dma_done(struct nvkm_falcon *falcon)
2962306a36Sopenharmony_ci{
3062306a36Sopenharmony_ci	return !!(nvkm_falcon_rd32(falcon, 0x118) & 0x00000002);
3162306a36Sopenharmony_ci}
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_cistatic void
3462306a36Sopenharmony_ciga102_flcn_dma_xfer(struct nvkm_falcon *falcon, u32 mem_base, u32 dma_base, u32 cmd)
3562306a36Sopenharmony_ci{
3662306a36Sopenharmony_ci	nvkm_falcon_wr32(falcon, 0x114, mem_base);
3762306a36Sopenharmony_ci	nvkm_falcon_wr32(falcon, 0x11c, dma_base);
3862306a36Sopenharmony_ci	nvkm_falcon_wr32(falcon, 0x118, cmd);
3962306a36Sopenharmony_ci}
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_cistatic int
4262306a36Sopenharmony_ciga102_flcn_dma_init(struct nvkm_falcon *falcon, u64 dma_addr, int xfer_len,
4362306a36Sopenharmony_ci		    enum nvkm_falcon_mem mem_type, bool sec, u32 *cmd)
4462306a36Sopenharmony_ci{
4562306a36Sopenharmony_ci	*cmd = (ilog2(xfer_len) - 2) << 8;
4662306a36Sopenharmony_ci	if (mem_type == IMEM)
4762306a36Sopenharmony_ci		*cmd |= 0x00000010;
4862306a36Sopenharmony_ci	if (sec)
4962306a36Sopenharmony_ci		*cmd |= 0x00000004;
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci	nvkm_falcon_wr32(falcon, 0x110, dma_addr >> 8);
5262306a36Sopenharmony_ci	nvkm_falcon_wr32(falcon, 0x128, 0x00000000);
5362306a36Sopenharmony_ci	return 0;
5462306a36Sopenharmony_ci}
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ciconst struct nvkm_falcon_func_dma
5762306a36Sopenharmony_ciga102_flcn_dma = {
5862306a36Sopenharmony_ci	.init = ga102_flcn_dma_init,
5962306a36Sopenharmony_ci	.xfer = ga102_flcn_dma_xfer,
6062306a36Sopenharmony_ci	.done = ga102_flcn_dma_done,
6162306a36Sopenharmony_ci};
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ciint
6462306a36Sopenharmony_ciga102_flcn_reset_wait_mem_scrubbing(struct nvkm_falcon *falcon)
6562306a36Sopenharmony_ci{
6662306a36Sopenharmony_ci	nvkm_falcon_mask(falcon, 0x040, 0x00000000, 0x00000000);
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	if (nvkm_msec(falcon->owner->device, 20,
6962306a36Sopenharmony_ci		if (!(nvkm_falcon_rd32(falcon, 0x0f4) & 0x00001000))
7062306a36Sopenharmony_ci			break;
7162306a36Sopenharmony_ci	) < 0)
7262306a36Sopenharmony_ci		return -ETIMEDOUT;
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci	return 0;
7562306a36Sopenharmony_ci}
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ciint
7862306a36Sopenharmony_ciga102_flcn_reset_prep(struct nvkm_falcon *falcon)
7962306a36Sopenharmony_ci{
8062306a36Sopenharmony_ci	nvkm_falcon_rd32(falcon, 0x0f4);
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci	nvkm_usec(falcon->owner->device, 150,
8362306a36Sopenharmony_ci		if (nvkm_falcon_rd32(falcon, 0x0f4) & 0x80000000)
8462306a36Sopenharmony_ci			break;
8562306a36Sopenharmony_ci		_warn = false;
8662306a36Sopenharmony_ci	);
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	return 0;
8962306a36Sopenharmony_ci}
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ciint
9262306a36Sopenharmony_ciga102_flcn_select(struct nvkm_falcon *falcon)
9362306a36Sopenharmony_ci{
9462306a36Sopenharmony_ci	if ((nvkm_falcon_rd32(falcon, falcon->addr2 + 0x668) & 0x00000010) != 0x00000000) {
9562306a36Sopenharmony_ci		nvkm_falcon_wr32(falcon, falcon->addr2 + 0x668, 0x00000000);
9662306a36Sopenharmony_ci		if (nvkm_msec(falcon->owner->device, 10,
9762306a36Sopenharmony_ci			if (nvkm_falcon_rd32(falcon, falcon->addr2 + 0x668) & 0x00000001)
9862306a36Sopenharmony_ci				break;
9962306a36Sopenharmony_ci		) < 0)
10062306a36Sopenharmony_ci			return -ETIMEDOUT;
10162306a36Sopenharmony_ci	}
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	return 0;
10462306a36Sopenharmony_ci}
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ciint
10762306a36Sopenharmony_ciga102_flcn_fw_boot(struct nvkm_falcon_fw *fw, u32 *mbox0, u32 *mbox1, u32 mbox0_ok, u32 irqsclr)
10862306a36Sopenharmony_ci{
10962306a36Sopenharmony_ci	struct nvkm_falcon *falcon = fw->falcon;
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci	nvkm_falcon_wr32(falcon, falcon->addr2 + 0x210, fw->dmem_sign);
11262306a36Sopenharmony_ci	nvkm_falcon_wr32(falcon, falcon->addr2 + 0x19c, fw->engine_id);
11362306a36Sopenharmony_ci	nvkm_falcon_wr32(falcon, falcon->addr2 + 0x198, fw->ucode_id);
11462306a36Sopenharmony_ci	nvkm_falcon_wr32(falcon, falcon->addr2 + 0x180, 0x00000001);
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	return gm200_flcn_fw_boot(fw, mbox0, mbox1, mbox0_ok, irqsclr);
11762306a36Sopenharmony_ci}
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ciint
12062306a36Sopenharmony_ciga102_flcn_fw_load(struct nvkm_falcon_fw *fw)
12162306a36Sopenharmony_ci{
12262306a36Sopenharmony_ci	struct nvkm_falcon *falcon = fw->falcon;
12362306a36Sopenharmony_ci	int ret = 0;
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	nvkm_falcon_mask(falcon, 0x624, 0x00000080, 0x00000080);
12662306a36Sopenharmony_ci	nvkm_falcon_wr32(falcon, 0x10c, 0x00000000);
12762306a36Sopenharmony_ci	nvkm_falcon_mask(falcon, 0x600, 0x00010007, (0 << 16) | (1 << 2) | 1);
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	ret = nvkm_falcon_dma_wr(falcon, fw->fw.img, fw->fw.phys, fw->imem_base_img,
13062306a36Sopenharmony_ci				 IMEM, fw->imem_base, fw->imem_size, true);
13162306a36Sopenharmony_ci	if (ret)
13262306a36Sopenharmony_ci		return ret;
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci	ret = nvkm_falcon_dma_wr(falcon, fw->fw.img, fw->fw.phys, fw->dmem_base_img,
13562306a36Sopenharmony_ci				 DMEM, fw->dmem_base, fw->dmem_size, false);
13662306a36Sopenharmony_ci	if (ret)
13762306a36Sopenharmony_ci		return ret;
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	return 0;
14062306a36Sopenharmony_ci}
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ciconst struct nvkm_falcon_fw_func
14362306a36Sopenharmony_ciga102_flcn_fw = {
14462306a36Sopenharmony_ci	.signature = ga100_flcn_fw_signature,
14562306a36Sopenharmony_ci	.reset = gm200_flcn_fw_reset,
14662306a36Sopenharmony_ci	.load = ga102_flcn_fw_load,
14762306a36Sopenharmony_ci	.boot = ga102_flcn_fw_boot,
14862306a36Sopenharmony_ci};
149