xref: /openbmc/linux/drivers/gpu/drm/nouveau/nvkm/falcon/ga102.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
1a51c69eeSBen Skeggs /*
2a51c69eeSBen Skeggs  * Copyright 2022 Red Hat Inc.
3a51c69eeSBen Skeggs  *
4a51c69eeSBen Skeggs  * Permission is hereby granted, free of charge, to any person obtaining a
5a51c69eeSBen Skeggs  * copy of this software and associated documentation files (the "Software"),
6a51c69eeSBen Skeggs  * to deal in the Software without restriction, including without limitation
7a51c69eeSBen Skeggs  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8a51c69eeSBen Skeggs  * and/or sell copies of the Software, and to permit persons to whom the
9a51c69eeSBen Skeggs  * Software is furnished to do so, subject to the following conditions:
10a51c69eeSBen Skeggs  *
11a51c69eeSBen Skeggs  * The above copyright notice and this permission notice shall be included in
12a51c69eeSBen Skeggs  * all copies or substantial portions of the Software.
13a51c69eeSBen Skeggs  *
14a51c69eeSBen Skeggs  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15a51c69eeSBen Skeggs  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16a51c69eeSBen Skeggs  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17a51c69eeSBen Skeggs  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18a51c69eeSBen Skeggs  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19a51c69eeSBen Skeggs  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20a51c69eeSBen Skeggs  * OTHER DEALINGS IN THE SOFTWARE.
21a51c69eeSBen Skeggs  */
22a51c69eeSBen Skeggs #include "priv.h"
23a51c69eeSBen Skeggs 
24a51c69eeSBen Skeggs #include <subdev/mc.h>
25a51c69eeSBen Skeggs #include <subdev/timer.h>
26a51c69eeSBen Skeggs 
27a51c69eeSBen Skeggs static bool
ga102_flcn_dma_done(struct nvkm_falcon * falcon)28a51c69eeSBen Skeggs ga102_flcn_dma_done(struct nvkm_falcon *falcon)
29a51c69eeSBen Skeggs {
30a51c69eeSBen Skeggs 	return !!(nvkm_falcon_rd32(falcon, 0x118) & 0x00000002);
31a51c69eeSBen Skeggs }
32a51c69eeSBen Skeggs 
33a51c69eeSBen Skeggs static void
ga102_flcn_dma_xfer(struct nvkm_falcon * falcon,u32 mem_base,u32 dma_base,u32 cmd)34a51c69eeSBen Skeggs ga102_flcn_dma_xfer(struct nvkm_falcon *falcon, u32 mem_base, u32 dma_base, u32 cmd)
35a51c69eeSBen Skeggs {
36a51c69eeSBen Skeggs 	nvkm_falcon_wr32(falcon, 0x114, mem_base);
37a51c69eeSBen Skeggs 	nvkm_falcon_wr32(falcon, 0x11c, dma_base);
38a51c69eeSBen Skeggs 	nvkm_falcon_wr32(falcon, 0x118, cmd);
39a51c69eeSBen Skeggs }
40a51c69eeSBen Skeggs 
41a51c69eeSBen Skeggs static int
ga102_flcn_dma_init(struct nvkm_falcon * falcon,u64 dma_addr,int xfer_len,enum nvkm_falcon_mem mem_type,bool sec,u32 * cmd)42a51c69eeSBen Skeggs ga102_flcn_dma_init(struct nvkm_falcon *falcon, u64 dma_addr, int xfer_len,
43a51c69eeSBen Skeggs 		    enum nvkm_falcon_mem mem_type, bool sec, u32 *cmd)
44a51c69eeSBen Skeggs {
45a51c69eeSBen Skeggs 	*cmd = (ilog2(xfer_len) - 2) << 8;
46a51c69eeSBen Skeggs 	if (mem_type == IMEM)
47a51c69eeSBen Skeggs 		*cmd |= 0x00000010;
48a51c69eeSBen Skeggs 	if (sec)
49a51c69eeSBen Skeggs 		*cmd |= 0x00000004;
50a51c69eeSBen Skeggs 
51a51c69eeSBen Skeggs 	nvkm_falcon_wr32(falcon, 0x110, dma_addr >> 8);
52a51c69eeSBen Skeggs 	nvkm_falcon_wr32(falcon, 0x128, 0x00000000);
53a51c69eeSBen Skeggs 	return 0;
54a51c69eeSBen Skeggs }
55a51c69eeSBen Skeggs 
56a51c69eeSBen Skeggs const struct nvkm_falcon_func_dma
57a51c69eeSBen Skeggs ga102_flcn_dma = {
58a51c69eeSBen Skeggs 	.init = ga102_flcn_dma_init,
59a51c69eeSBen Skeggs 	.xfer = ga102_flcn_dma_xfer,
60a51c69eeSBen Skeggs 	.done = ga102_flcn_dma_done,
61a51c69eeSBen Skeggs };
62a51c69eeSBen Skeggs 
63a51c69eeSBen Skeggs int
ga102_flcn_reset_wait_mem_scrubbing(struct nvkm_falcon * falcon)64a51c69eeSBen Skeggs ga102_flcn_reset_wait_mem_scrubbing(struct nvkm_falcon *falcon)
65a51c69eeSBen Skeggs {
66a51c69eeSBen Skeggs 	nvkm_falcon_mask(falcon, 0x040, 0x00000000, 0x00000000);
67a51c69eeSBen Skeggs 
68a51c69eeSBen Skeggs 	if (nvkm_msec(falcon->owner->device, 20,
69a51c69eeSBen Skeggs 		if (!(nvkm_falcon_rd32(falcon, 0x0f4) & 0x00001000))
70a51c69eeSBen Skeggs 			break;
71a51c69eeSBen Skeggs 	) < 0)
72a51c69eeSBen Skeggs 		return -ETIMEDOUT;
73a51c69eeSBen Skeggs 
74a51c69eeSBen Skeggs 	return 0;
75a51c69eeSBen Skeggs }
76a51c69eeSBen Skeggs 
77a51c69eeSBen Skeggs int
ga102_flcn_reset_prep(struct nvkm_falcon * falcon)78a51c69eeSBen Skeggs ga102_flcn_reset_prep(struct nvkm_falcon *falcon)
79a51c69eeSBen Skeggs {
80*4b569dedSBen Skeggs 	nvkm_falcon_rd32(falcon, 0x0f4);
81a51c69eeSBen Skeggs 
82*4b569dedSBen Skeggs 	nvkm_usec(falcon->owner->device, 150,
83*4b569dedSBen Skeggs 		if (nvkm_falcon_rd32(falcon, 0x0f4) & 0x80000000)
84*4b569dedSBen Skeggs 			break;
85*4b569dedSBen Skeggs 		_warn = false;
86*4b569dedSBen Skeggs 	);
87*4b569dedSBen Skeggs 
88*4b569dedSBen Skeggs 	return 0;
89*4b569dedSBen Skeggs }
90*4b569dedSBen Skeggs 
91*4b569dedSBen Skeggs int
ga102_flcn_select(struct nvkm_falcon * falcon)92*4b569dedSBen Skeggs ga102_flcn_select(struct nvkm_falcon *falcon)
93*4b569dedSBen Skeggs {
94*4b569dedSBen Skeggs 	if ((nvkm_falcon_rd32(falcon, falcon->addr2 + 0x668) & 0x00000010) != 0x00000000) {
95*4b569dedSBen Skeggs 		nvkm_falcon_wr32(falcon, falcon->addr2 + 0x668, 0x00000000);
96a51c69eeSBen Skeggs 		if (nvkm_msec(falcon->owner->device, 10,
97*4b569dedSBen Skeggs 			if (nvkm_falcon_rd32(falcon, falcon->addr2 + 0x668) & 0x00000001)
98a51c69eeSBen Skeggs 				break;
99a51c69eeSBen Skeggs 		) < 0)
100a51c69eeSBen Skeggs 			return -ETIMEDOUT;
101*4b569dedSBen Skeggs 	}
102a51c69eeSBen Skeggs 
103a51c69eeSBen Skeggs 	return 0;
104a51c69eeSBen Skeggs }
105a51c69eeSBen Skeggs 
106a51c69eeSBen Skeggs int
ga102_flcn_fw_boot(struct nvkm_falcon_fw * fw,u32 * mbox0,u32 * mbox1,u32 mbox0_ok,u32 irqsclr)107a51c69eeSBen Skeggs ga102_flcn_fw_boot(struct nvkm_falcon_fw *fw, u32 *mbox0, u32 *mbox1, u32 mbox0_ok, u32 irqsclr)
108a51c69eeSBen Skeggs {
109a51c69eeSBen Skeggs 	struct nvkm_falcon *falcon = fw->falcon;
110a51c69eeSBen Skeggs 
111a51c69eeSBen Skeggs 	nvkm_falcon_wr32(falcon, falcon->addr2 + 0x210, fw->dmem_sign);
112a51c69eeSBen Skeggs 	nvkm_falcon_wr32(falcon, falcon->addr2 + 0x19c, fw->engine_id);
113a51c69eeSBen Skeggs 	nvkm_falcon_wr32(falcon, falcon->addr2 + 0x198, fw->ucode_id);
114a51c69eeSBen Skeggs 	nvkm_falcon_wr32(falcon, falcon->addr2 + 0x180, 0x00000001);
115a51c69eeSBen Skeggs 
116a51c69eeSBen Skeggs 	return gm200_flcn_fw_boot(fw, mbox0, mbox1, mbox0_ok, irqsclr);
117a51c69eeSBen Skeggs }
118a51c69eeSBen Skeggs 
119a51c69eeSBen Skeggs int
ga102_flcn_fw_load(struct nvkm_falcon_fw * fw)120a51c69eeSBen Skeggs ga102_flcn_fw_load(struct nvkm_falcon_fw *fw)
121a51c69eeSBen Skeggs {
122a51c69eeSBen Skeggs 	struct nvkm_falcon *falcon = fw->falcon;
123a51c69eeSBen Skeggs 	int ret = 0;
124a51c69eeSBen Skeggs 
125a51c69eeSBen Skeggs 	nvkm_falcon_mask(falcon, 0x624, 0x00000080, 0x00000080);
126a51c69eeSBen Skeggs 	nvkm_falcon_wr32(falcon, 0x10c, 0x00000000);
127a51c69eeSBen Skeggs 	nvkm_falcon_mask(falcon, 0x600, 0x00010007, (0 << 16) | (1 << 2) | 1);
128a51c69eeSBen Skeggs 
129a51c69eeSBen Skeggs 	ret = nvkm_falcon_dma_wr(falcon, fw->fw.img, fw->fw.phys, fw->imem_base_img,
130a51c69eeSBen Skeggs 				 IMEM, fw->imem_base, fw->imem_size, true);
131a51c69eeSBen Skeggs 	if (ret)
132a51c69eeSBen Skeggs 		return ret;
133a51c69eeSBen Skeggs 
134a51c69eeSBen Skeggs 	ret = nvkm_falcon_dma_wr(falcon, fw->fw.img, fw->fw.phys, fw->dmem_base_img,
135a51c69eeSBen Skeggs 				 DMEM, fw->dmem_base, fw->dmem_size, false);
136a51c69eeSBen Skeggs 	if (ret)
137a51c69eeSBen Skeggs 		return ret;
138a51c69eeSBen Skeggs 
139a51c69eeSBen Skeggs 	return 0;
140a51c69eeSBen Skeggs }
141a51c69eeSBen Skeggs 
142a51c69eeSBen Skeggs const struct nvkm_falcon_fw_func
143a51c69eeSBen Skeggs ga102_flcn_fw = {
144a51c69eeSBen Skeggs 	.signature = ga100_flcn_fw_signature,
145a51c69eeSBen Skeggs 	.reset = gm200_flcn_fw_reset,
146a51c69eeSBen Skeggs 	.load = ga102_flcn_fw_load,
147a51c69eeSBen Skeggs 	.boot = ga102_flcn_fw_boot,
148a51c69eeSBen Skeggs };
149