1 #ifndef __NVKM_FALCON_H__
2 #define __NVKM_FALCON_H__
3 #include <core/firmware.h>
4 #include <engine/falcon.h>
5
6 enum nvkm_falcon_mem {
7 IMEM,
8 DMEM,
9 EMEM,
10 };
11
12 static inline const char *
nvkm_falcon_mem(enum nvkm_falcon_mem mem)13 nvkm_falcon_mem(enum nvkm_falcon_mem mem)
14 {
15 switch (mem) {
16 case IMEM: return "imem";
17 case DMEM: return "dmem";
18 case EMEM: return "emem";
19 default:
20 WARN_ON(1);
21 return "?mem";
22 }
23 }
24
25 struct nvkm_falcon_func_pio {
26 int min;
27 int max;
28 void (*wr_init)(struct nvkm_falcon *, u8 port, bool sec, u32 mem_base);
29 void (*wr)(struct nvkm_falcon *, u8 port, const u8 *img, int len, u16 tag);
30 void (*rd_init)(struct nvkm_falcon *, u8 port, u32 mem_base);
31 void (*rd)(struct nvkm_falcon *, u8 port, const u8 *img, int len);
32 };
33
34 struct nvkm_falcon_func_dma {
35 int (*init)(struct nvkm_falcon *, u64 dma_addr, int xfer_len,
36 enum nvkm_falcon_mem, bool sec, u32 *cmd);
37 void (*xfer)(struct nvkm_falcon *, u32 mem_base, u32 dma_base, u32 cmd);
38 bool (*done)(struct nvkm_falcon *);
39 };
40
41 int nvkm_falcon_ctor(const struct nvkm_falcon_func *, struct nvkm_subdev *owner,
42 const char *name, u32 addr, struct nvkm_falcon *);
43 void nvkm_falcon_dtor(struct nvkm_falcon *);
44 int nvkm_falcon_reset(struct nvkm_falcon *);
45 int nvkm_falcon_pio_wr(struct nvkm_falcon *, const u8 *img, u32 img_base, u8 port,
46 enum nvkm_falcon_mem mem_type, u32 mem_base, int len, u16 tag, bool sec);
47 int nvkm_falcon_pio_rd(struct nvkm_falcon *, u8 port, enum nvkm_falcon_mem type, u32 mem_base,
48 const u8 *img, u32 img_base, int len);
49 int nvkm_falcon_dma_wr(struct nvkm_falcon *, const u8 *img, u64 dma_addr, u32 dma_base,
50 enum nvkm_falcon_mem mem_type, u32 mem_base, int len, bool sec);
51
52 int gm200_flcn_reset_wait_mem_scrubbing(struct nvkm_falcon *);
53 int gm200_flcn_disable(struct nvkm_falcon *);
54 int gm200_flcn_enable(struct nvkm_falcon *);
55 void gm200_flcn_bind_inst(struct nvkm_falcon *, int, u64);
56 int gm200_flcn_bind_stat(struct nvkm_falcon *, bool);
57 extern const struct nvkm_falcon_func_pio gm200_flcn_imem_pio;
58 extern const struct nvkm_falcon_func_pio gm200_flcn_dmem_pio;
59 void gm200_flcn_tracepc(struct nvkm_falcon *);
60
61 int gp102_flcn_reset_eng(struct nvkm_falcon *);
62 extern const struct nvkm_falcon_func_pio gp102_flcn_emem_pio;
63
64 int ga102_flcn_select(struct nvkm_falcon *);
65 int ga102_flcn_reset_prep(struct nvkm_falcon *);
66 int ga102_flcn_reset_wait_mem_scrubbing(struct nvkm_falcon *);
67 extern const struct nvkm_falcon_func_dma ga102_flcn_dma;
68
69 void nvkm_falcon_v1_load_imem(struct nvkm_falcon *,
70 void *, u32, u32, u16, u8, bool);
71 void nvkm_falcon_v1_load_dmem(struct nvkm_falcon *, void *, u32, u32, u8);
72 void nvkm_falcon_v1_start(struct nvkm_falcon *);
73
74 #define FLCN_PRINTK(f,l,p,fmt,a...) ({ \
75 if ((f)->owner->name != (f)->name) \
76 nvkm_printk___((f)->owner, (f)->user, NV_DBG_##l, p, "%s:"fmt, (f)->name, ##a); \
77 else \
78 nvkm_printk___((f)->owner, (f)->user, NV_DBG_##l, p, fmt, ##a); \
79 })
80 #define FLCN_DBG(f,fmt,a...) FLCN_PRINTK((f), DEBUG, info, " "fmt"\n", ##a)
81 #define FLCN_ERR(f,fmt,a...) FLCN_PRINTK((f), ERROR, err, " "fmt"\n", ##a)
82 #define FLCN_ERRON(f,c,fmt,a...) \
83 ({ bool _cond = (c); _cond ? FLCN_ERR(f, fmt, ##a) : FLCN_DBG(f, fmt, ##a); _cond; })
84
85
86 struct nvkm_falcon_fw {
87 const struct nvkm_falcon_fw_func {
88 int (*signature)(struct nvkm_falcon_fw *, u32 *sig_base_src);
89 int (*reset)(struct nvkm_falcon_fw *);
90 int (*setup)(struct nvkm_falcon_fw *);
91 int (*load)(struct nvkm_falcon_fw *);
92 int (*load_bld)(struct nvkm_falcon_fw *);
93 int (*boot)(struct nvkm_falcon_fw *,
94 u32 *mbox0, u32 *mbox1, u32 mbox0_ok, u32 irqsclr);
95 } *func;
96 struct nvkm_firmware fw;
97
98 u32 sig_base_prd;
99 u32 sig_base_dbg;
100 u32 sig_base_img;
101 u32 sig_size;
102 int sig_nr;
103 u8 *sigs;
104 u32 fuse_ver;
105 u32 engine_id;
106 u32 ucode_id;
107
108 u32 nmem_base_img;
109 u32 nmem_base;
110 u32 nmem_size;
111
112 u32 imem_base_img;
113 u32 imem_base;
114 u32 imem_size;
115
116 u32 dmem_base_img;
117 u32 dmem_base;
118 u32 dmem_size;
119 u32 dmem_sign;
120
121 u8 *boot;
122 u32 boot_size;
123 u32 boot_addr;
124
125 struct nvkm_falcon *falcon;
126 struct nvkm_memory *inst;
127 struct nvkm_vmm *vmm;
128 struct nvkm_vma *vma;
129 };
130
131 int nvkm_falcon_fw_ctor(const struct nvkm_falcon_fw_func *, const char *name, struct nvkm_device *,
132 bool bl, const void *src, u32 len, struct nvkm_falcon *,
133 struct nvkm_falcon_fw *);
134 int nvkm_falcon_fw_ctor_hs(const struct nvkm_falcon_fw_func *, const char *name,
135 struct nvkm_subdev *, const char *bl, const char *img, int ver,
136 struct nvkm_falcon *falcon, struct nvkm_falcon_fw *fw);
137 int nvkm_falcon_fw_ctor_hs_v2(const struct nvkm_falcon_fw_func *, const char *name,
138 struct nvkm_subdev *, const char *img, int ver, struct nvkm_falcon *,
139 struct nvkm_falcon_fw *);
140 int nvkm_falcon_fw_sign(struct nvkm_falcon_fw *, u32 sig_base_img, u32 sig_size, const u8 *sigs,
141 int sig_nr_prd, u32 sig_base_prd, int sig_nr_dbg, u32 sig_base_dbg);
142 int nvkm_falcon_fw_patch(struct nvkm_falcon_fw *);
143 void nvkm_falcon_fw_dtor(struct nvkm_falcon_fw *);
144 int nvkm_falcon_fw_oneinit(struct nvkm_falcon_fw *, struct nvkm_falcon *, struct nvkm_vmm *,
145 struct nvkm_memory *inst);
146 int nvkm_falcon_fw_boot(struct nvkm_falcon_fw *, struct nvkm_subdev *user,
147 bool release, u32 *pmbox0, u32 *pmbox1, u32 mbox0_ok, u32 irqsclr);
148
149 extern const struct nvkm_falcon_fw_func gm200_flcn_fw;
150 int gm200_flcn_fw_signature(struct nvkm_falcon_fw *, u32 *);
151 int gm200_flcn_fw_reset(struct nvkm_falcon_fw *);
152 int gm200_flcn_fw_load(struct nvkm_falcon_fw *);
153 int gm200_flcn_fw_boot(struct nvkm_falcon_fw *, u32 *, u32 *, u32, u32);
154
155 int ga100_flcn_fw_signature(struct nvkm_falcon_fw *, u32 *);
156
157 extern const struct nvkm_falcon_fw_func ga102_flcn_fw;
158 int ga102_flcn_fw_load(struct nvkm_falcon_fw *);
159 int ga102_flcn_fw_boot(struct nvkm_falcon_fw *, u32 *, u32 *, u32, u32);
160
161 #define FLCNFW_PRINTK(f,l,p,fmt,a...) FLCN_PRINTK((f)->falcon, l, p, "%s: "fmt, (f)->fw.name, ##a)
162 #define FLCNFW_DBG(f,fmt,a...) FLCNFW_PRINTK((f), DEBUG, info, fmt"\n", ##a)
163 #define FLCNFW_ERR(f,fmt,a...) FLCNFW_PRINTK((f), ERROR, err, fmt"\n", ##a)
164
165 /**
166 * struct nvfw_falcon_msg - header for all messages
167 *
168 * @unit_id: id of firmware process that sent the message
169 * @size: total size of message
170 * @ctrl_flags: control flags
171 * @seq_id: used to match a message from its corresponding command
172 */
173 struct nvfw_falcon_msg {
174 u8 unit_id;
175 u8 size;
176 u8 ctrl_flags;
177 u8 seq_id;
178 };
179
180 #define nvfw_falcon_cmd nvfw_falcon_msg
181 #define NV_FALCON_CMD_UNIT_ID_REWIND 0x00
182
183 struct nvkm_falcon_qmgr;
184 int nvkm_falcon_qmgr_new(struct nvkm_falcon *, struct nvkm_falcon_qmgr **);
185 void nvkm_falcon_qmgr_del(struct nvkm_falcon_qmgr **);
186
187 typedef int
188 (*nvkm_falcon_qmgr_callback)(void *priv, struct nvfw_falcon_msg *);
189
190 struct nvkm_falcon_cmdq;
191 int nvkm_falcon_cmdq_new(struct nvkm_falcon_qmgr *, const char *name,
192 struct nvkm_falcon_cmdq **);
193 void nvkm_falcon_cmdq_del(struct nvkm_falcon_cmdq **);
194 void nvkm_falcon_cmdq_init(struct nvkm_falcon_cmdq *,
195 u32 index, u32 offset, u32 size);
196 void nvkm_falcon_cmdq_fini(struct nvkm_falcon_cmdq *);
197 int nvkm_falcon_cmdq_send(struct nvkm_falcon_cmdq *, struct nvfw_falcon_cmd *,
198 nvkm_falcon_qmgr_callback, void *priv,
199 unsigned long timeout_jiffies);
200
201 struct nvkm_falcon_msgq;
202 int nvkm_falcon_msgq_new(struct nvkm_falcon_qmgr *, const char *name,
203 struct nvkm_falcon_msgq **);
204 void nvkm_falcon_msgq_del(struct nvkm_falcon_msgq **);
205 void nvkm_falcon_msgq_init(struct nvkm_falcon_msgq *,
206 u32 index, u32 offset, u32 size);
207 bool nvkm_falcon_msgq_empty(struct nvkm_falcon_msgq *);
208 int nvkm_falcon_msgq_recv_initmsg(struct nvkm_falcon_msgq *, void *, u32 size);
209 void nvkm_falcon_msgq_recv(struct nvkm_falcon_msgq *);
210 #endif
211