1 #ifndef __NVKM_GSP_H__
2 #define __NVKM_GSP_H__
3 #define nvkm_gsp(p) container_of((p), struct nvkm_gsp, subdev)
4 #include <core/subdev.h>
5 #include <core/falcon.h>
6 #include <core/firmware.h>
7
8 #include <linux/debugfs.h>
9
10 #define GSP_PAGE_SHIFT 12
11 #define GSP_PAGE_SIZE BIT(GSP_PAGE_SHIFT)
12
13 struct nvkm_gsp_mem {
14 struct device *dev;
15 size_t size;
16 void *data;
17 dma_addr_t addr;
18 };
19
20 int nvkm_gsp_mem_ctor(struct nvkm_gsp *, size_t size, struct nvkm_gsp_mem *);
21 void nvkm_gsp_mem_dtor(struct nvkm_gsp_mem *);
22
23 struct nvkm_gsp_radix3 {
24 struct nvkm_gsp_mem lvl0;
25 struct nvkm_gsp_mem lvl1;
26 struct sg_table lvl2;
27 };
28
29 int nvkm_gsp_sg(struct nvkm_device *, u64 size, struct sg_table *);
30 void nvkm_gsp_sg_free(struct nvkm_device *, struct sg_table *);
31
32 typedef int (*nvkm_gsp_msg_ntfy_func)(void *priv, u32 fn, void *repv, u32 repc);
33
34 struct nvkm_gsp_event;
35 typedef void (*nvkm_gsp_event_func)(struct nvkm_gsp_event *, void *repv, u32 repc);
36
37 /**
38 * DOC: GSP message handling policy
39 *
40 * When sending a GSP RPC command, there can be multiple cases of handling
41 * the GSP RPC messages, which are the reply of GSP RPC commands, according
42 * to the requirement of the callers and the nature of the GSP RPC commands.
43 *
44 * NVKM_GSP_RPC_REPLY_NOWAIT - If specified, immediately return to the
45 * caller after the GSP RPC command is issued.
46 *
47 * NVKM_GSP_RPC_REPLY_RECV - If specified, wait and receive the entire GSP
48 * RPC message after the GSP RPC command is issued.
49 *
50 * NVKM_GSP_RPC_REPLY_POLL - If specified, wait for the specific reply and
51 * discard the reply before returning to the caller.
52 *
53 */
54 enum nvkm_gsp_rpc_reply_policy {
55 NVKM_GSP_RPC_REPLY_NOWAIT = 0,
56 NVKM_GSP_RPC_REPLY_RECV,
57 NVKM_GSP_RPC_REPLY_POLL,
58 };
59
60 struct nvkm_gsp {
61 const struct nvkm_gsp_func *func;
62 struct nvkm_subdev subdev;
63
64 struct nvkm_falcon falcon;
65
66 struct {
67 struct {
68 const struct firmware *load;
69 const struct firmware *unload;
70 } booter;
71
72 const struct firmware *fmc;
73
74 const struct firmware *bl;
75 const struct firmware *rm;
76 } fws;
77
78 struct nvkm_firmware fw;
79 struct nvkm_gsp_mem sig;
80 struct nvkm_gsp_radix3 radix3;
81
82 struct {
83 struct {
84 struct {
85 u64 addr;
86 u64 size;
87 } vga_workspace;
88 u64 addr;
89 u64 size;
90 } bios;
91 struct {
92 struct {
93 u64 addr;
94 u64 size;
95 } frts, boot, elf, heap;
96 u64 addr;
97 u64 size;
98 } wpr2;
99 struct {
100 u64 addr;
101 u64 size;
102 } heap;
103 u64 addr;
104 u64 size;
105
106 struct {
107 u64 addr;
108 u64 size;
109 } region[16];
110 int region_nr;
111 u32 rsvd_size;
112 } fb;
113
114 struct {
115 struct nvkm_falcon_fw load;
116 struct nvkm_falcon_fw unload;
117 } booter;
118
119 struct {
120 struct nvkm_gsp_mem fw;
121 u8 *hash;
122 u8 *pkey;
123 u8 *sig;
124
125 struct nvkm_gsp_mem args;
126 } fmc;
127
128 struct {
129 struct nvkm_gsp_mem fw;
130 u32 code_offset;
131 u32 data_offset;
132 u32 manifest_offset;
133 u32 app_version;
134 } boot;
135
136 struct nvkm_gsp_mem libos;
137 struct nvkm_gsp_mem loginit;
138 struct nvkm_gsp_mem logintr;
139 struct nvkm_gsp_mem logrm;
140 struct nvkm_gsp_mem rmargs;
141
142 struct nvkm_gsp_mem wpr_meta;
143
144 struct {
145 struct sg_table sgt;
146 struct nvkm_gsp_radix3 radix3;
147 struct nvkm_gsp_mem meta;
148 struct sg_table fbsr;
149 } sr;
150
151 struct {
152 struct nvkm_gsp_mem mem;
153
154 struct {
155 int nr;
156 u32 size;
157 u64 *ptr;
158 } ptes;
159
160 struct {
161 u32 size;
162 void *ptr;
163 } cmdq, msgq;
164 } shm;
165
166 struct nvkm_gsp_cmdq {
167 struct mutex mutex;
168 u32 cnt;
169 u32 seq;
170 u32 *wptr;
171 u32 *rptr;
172 } cmdq;
173
174 struct nvkm_gsp_msgq {
175 struct mutex mutex;
176 u32 cnt;
177 u32 *wptr;
178 u32 *rptr;
179 struct nvkm_gsp_msgq_ntfy {
180 u32 fn;
181 nvkm_gsp_msg_ntfy_func func;
182 void *priv;
183 } ntfy[16];
184 int ntfy_nr;
185 struct work_struct work;
186 } msgq;
187
188 bool running;
189
190 /* Internal GSP-RM control handles. */
191 struct {
192 struct nvkm_gsp_client {
193 struct nvkm_gsp_object {
194 struct nvkm_gsp_client *client;
195 struct nvkm_gsp_object *parent;
196 u32 handle;
197 } object;
198
199 struct nvkm_gsp *gsp;
200
201 struct list_head events;
202 } client;
203
204 struct nvkm_gsp_device {
205 struct nvkm_gsp_object object;
206 struct nvkm_gsp_object subdevice;
207 } device;
208 } internal;
209
210 struct {
211 enum nvkm_subdev_type type;
212 int inst;
213 u32 stall;
214 u32 nonstall;
215 } intr[32];
216 int intr_nr;
217
218 struct {
219 u64 rm_bar1_pdb;
220 u64 rm_bar2_pdb;
221 } bar;
222
223 struct {
224 u8 gpcs;
225 u8 tpcs;
226 } gr;
227
228 struct nvkm_rm *rm;
229
230 struct {
231 struct mutex mutex;
232 struct idr idr;
233 } client_id;
234
235 /* A linked list of registry items. The registry RPC will be built from it. */
236 struct list_head registry_list;
237
238 /* The size of the registry RPC */
239 size_t registry_rpc_size;
240
241 #ifdef CONFIG_DEBUG_FS
242 /*
243 * Logging buffers in debugfs. The wrapper objects need to remain
244 * in memory until the dentry is deleted.
245 */
246 struct {
247 struct dentry *parent;
248 struct dentry *init;
249 struct dentry *rm;
250 struct dentry *intr;
251 struct dentry *pmu;
252 } debugfs;
253 struct debugfs_blob_wrapper blob_init;
254 struct debugfs_blob_wrapper blob_intr;
255 struct debugfs_blob_wrapper blob_rm;
256 struct debugfs_blob_wrapper blob_pmu;
257 #endif
258 };
259
260 static inline bool
nvkm_gsp_rm(struct nvkm_gsp * gsp)261 nvkm_gsp_rm(struct nvkm_gsp *gsp)
262 {
263 return gsp && (gsp->fws.rm || gsp->fw.img);
264 }
265
266 #include <rm/rm.h>
267
268 static inline void *
nvkm_gsp_rpc_get(struct nvkm_gsp * gsp,u32 fn,u32 argc)269 nvkm_gsp_rpc_get(struct nvkm_gsp *gsp, u32 fn, u32 argc)
270 {
271 return gsp->rm->api->rpc->get(gsp, fn, argc);
272 }
273
274 static inline void *
nvkm_gsp_rpc_push(struct nvkm_gsp * gsp,void * argv,enum nvkm_gsp_rpc_reply_policy policy,u32 repc)275 nvkm_gsp_rpc_push(struct nvkm_gsp *gsp, void *argv,
276 enum nvkm_gsp_rpc_reply_policy policy, u32 repc)
277 {
278 return gsp->rm->api->rpc->push(gsp, argv, policy, repc);
279 }
280
281 static inline void *
nvkm_gsp_rpc_rd(struct nvkm_gsp * gsp,u32 fn,u32 argc)282 nvkm_gsp_rpc_rd(struct nvkm_gsp *gsp, u32 fn, u32 argc)
283 {
284 void *argv = nvkm_gsp_rpc_get(gsp, fn, argc);
285
286 if (IS_ERR_OR_NULL(argv))
287 return argv;
288
289 return nvkm_gsp_rpc_push(gsp, argv, NVKM_GSP_RPC_REPLY_RECV, argc);
290 }
291
292 static inline int
nvkm_gsp_rpc_wr(struct nvkm_gsp * gsp,void * argv,enum nvkm_gsp_rpc_reply_policy policy)293 nvkm_gsp_rpc_wr(struct nvkm_gsp *gsp, void *argv,
294 enum nvkm_gsp_rpc_reply_policy policy)
295 {
296 void *repv = nvkm_gsp_rpc_push(gsp, argv, policy, 0);
297
298 if (IS_ERR(repv))
299 return PTR_ERR(repv);
300
301 return 0;
302 }
303
304 static inline void
nvkm_gsp_rpc_done(struct nvkm_gsp * gsp,void * repv)305 nvkm_gsp_rpc_done(struct nvkm_gsp *gsp, void *repv)
306 {
307 gsp->rm->api->rpc->done(gsp, repv);
308 }
309
310 static inline void *
nvkm_gsp_rm_ctrl_get(struct nvkm_gsp_object * object,u32 cmd,u32 argc)311 nvkm_gsp_rm_ctrl_get(struct nvkm_gsp_object *object, u32 cmd, u32 argc)
312 {
313 return object->client->gsp->rm->api->ctrl->get(object, cmd, argc);
314 }
315
316 static inline int
nvkm_gsp_rm_ctrl_push(struct nvkm_gsp_object * object,void * argv,u32 repc)317 nvkm_gsp_rm_ctrl_push(struct nvkm_gsp_object *object, void *argv, u32 repc)
318 {
319 return object->client->gsp->rm->api->ctrl->push(object, argv, repc);
320 }
321
322 static inline void *
nvkm_gsp_rm_ctrl_rd(struct nvkm_gsp_object * object,u32 cmd,u32 repc)323 nvkm_gsp_rm_ctrl_rd(struct nvkm_gsp_object *object, u32 cmd, u32 repc)
324 {
325 void *argv = nvkm_gsp_rm_ctrl_get(object, cmd, repc);
326 int ret;
327
328 if (IS_ERR(argv))
329 return argv;
330
331 ret = nvkm_gsp_rm_ctrl_push(object, &argv, repc);
332 if (ret)
333 return ERR_PTR(ret);
334 return argv;
335 }
336
337 static inline int
nvkm_gsp_rm_ctrl_wr(struct nvkm_gsp_object * object,void * argv)338 nvkm_gsp_rm_ctrl_wr(struct nvkm_gsp_object *object, void *argv)
339 {
340 int ret = nvkm_gsp_rm_ctrl_push(object, &argv, 0);
341
342 if (ret)
343 return ret;
344 return 0;
345 }
346
347 static inline void
nvkm_gsp_rm_ctrl_done(struct nvkm_gsp_object * object,void * repv)348 nvkm_gsp_rm_ctrl_done(struct nvkm_gsp_object *object, void *repv)
349 {
350 object->client->gsp->rm->api->ctrl->done(object, repv);
351 }
352
353 static inline void *
nvkm_gsp_rm_alloc_get(struct nvkm_gsp_object * parent,u32 handle,u32 oclass,u32 argc,struct nvkm_gsp_object * object)354 nvkm_gsp_rm_alloc_get(struct nvkm_gsp_object *parent, u32 handle, u32 oclass, u32 argc,
355 struct nvkm_gsp_object *object)
356 {
357 struct nvkm_gsp_client *client = parent->client;
358 struct nvkm_gsp *gsp = client->gsp;
359 void *argv;
360
361 object->client = parent->client;
362 object->parent = parent;
363 object->handle = handle;
364
365 argv = gsp->rm->api->alloc->get(object, oclass, argc);
366 if (IS_ERR_OR_NULL(argv)) {
367 object->client = NULL;
368 return argv;
369 }
370
371 return argv;
372 }
373
374 static inline void *
nvkm_gsp_rm_alloc_push(struct nvkm_gsp_object * object,void * argv)375 nvkm_gsp_rm_alloc_push(struct nvkm_gsp_object *object, void *argv)
376 {
377 void *repv = object->client->gsp->rm->api->alloc->push(object, argv);
378
379 if (IS_ERR(repv))
380 object->client = NULL;
381
382 return repv;
383 }
384
385 static inline int
nvkm_gsp_rm_alloc_wr(struct nvkm_gsp_object * object,void * argv)386 nvkm_gsp_rm_alloc_wr(struct nvkm_gsp_object *object, void *argv)
387 {
388 void *repv = nvkm_gsp_rm_alloc_push(object, argv);
389
390 if (IS_ERR(repv))
391 return PTR_ERR(repv);
392
393 return 0;
394 }
395
396 static inline void
nvkm_gsp_rm_alloc_done(struct nvkm_gsp_object * object,void * repv)397 nvkm_gsp_rm_alloc_done(struct nvkm_gsp_object *object, void *repv)
398 {
399 object->client->gsp->rm->api->alloc->done(object, repv);
400 }
401
402 static inline int
nvkm_gsp_rm_alloc(struct nvkm_gsp_object * parent,u32 handle,u32 oclass,u32 argc,struct nvkm_gsp_object * object)403 nvkm_gsp_rm_alloc(struct nvkm_gsp_object *parent, u32 handle, u32 oclass, u32 argc,
404 struct nvkm_gsp_object *object)
405 {
406 void *argv = nvkm_gsp_rm_alloc_get(parent, handle, oclass, argc, object);
407
408 if (IS_ERR_OR_NULL(argv))
409 return argv ? PTR_ERR(argv) : -EIO;
410
411 return nvkm_gsp_rm_alloc_wr(object, argv);
412 }
413
414 static inline int
nvkm_gsp_rm_free(struct nvkm_gsp_object * object)415 nvkm_gsp_rm_free(struct nvkm_gsp_object *object)
416 {
417 if (object->client) {
418 int ret = object->client->gsp->rm->api->alloc->free(object);
419 object->client = NULL;
420 return ret;
421 }
422
423 return 0;
424 }
425
426 int nvkm_gsp_client_ctor(struct nvkm_gsp *, struct nvkm_gsp_client *);
427 void nvkm_gsp_client_dtor(struct nvkm_gsp_client *);
428
429 static inline int
nvkm_gsp_device_ctor(struct nvkm_gsp_client * client,struct nvkm_gsp_device * device)430 nvkm_gsp_device_ctor(struct nvkm_gsp_client *client, struct nvkm_gsp_device *device)
431 {
432 return client->gsp->rm->api->device->ctor(client, device);
433 }
434
435 static inline void
nvkm_gsp_device_dtor(struct nvkm_gsp_device * device)436 nvkm_gsp_device_dtor(struct nvkm_gsp_device *device)
437 {
438 if (device->object.client)
439 device->object.client->gsp->rm->api->device->dtor(device);
440 }
441
442 static inline int
nvkm_gsp_client_device_ctor(struct nvkm_gsp * gsp,struct nvkm_gsp_client * client,struct nvkm_gsp_device * device)443 nvkm_gsp_client_device_ctor(struct nvkm_gsp *gsp,
444 struct nvkm_gsp_client *client, struct nvkm_gsp_device *device)
445 {
446 int ret = nvkm_gsp_client_ctor(gsp, client);
447
448 if (ret == 0) {
449 ret = nvkm_gsp_device_ctor(client, device);
450 if (ret)
451 nvkm_gsp_client_dtor(client);
452 }
453
454 return ret;
455 }
456
457 struct nvkm_gsp_event {
458 struct nvkm_gsp_device *device;
459 u32 id;
460 nvkm_gsp_event_func func;
461
462 struct nvkm_gsp_object object;
463
464 struct list_head head;
465 };
466
467 static inline int
nvkm_gsp_device_event_ctor(struct nvkm_gsp_device * device,u32 handle,u32 id,nvkm_gsp_event_func func,struct nvkm_gsp_event * event)468 nvkm_gsp_device_event_ctor(struct nvkm_gsp_device *device, u32 handle, u32 id,
469 nvkm_gsp_event_func func, struct nvkm_gsp_event *event)
470 {
471 struct nvkm_rm *rm = device->object.client->gsp->rm;
472
473 return rm->api->device->event.ctor(device, handle, id, func, event);
474 }
475
476 static inline void
nvkm_gsp_event_dtor(struct nvkm_gsp_event * event)477 nvkm_gsp_event_dtor(struct nvkm_gsp_event *event)
478 {
479 struct nvkm_gsp_device *device = event->device;
480
481 if (device)
482 device->object.client->gsp->rm->api->device->event.dtor(event);
483 }
484
485 int nvkm_gsp_intr_stall(struct nvkm_gsp *, enum nvkm_subdev_type, int);
486 int nvkm_gsp_intr_nonstall(struct nvkm_gsp *, enum nvkm_subdev_type, int);
487
488 int gv100_gsp_new(struct nvkm_device *, enum nvkm_subdev_type, int, struct nvkm_gsp **);
489 int tu102_gsp_new(struct nvkm_device *, enum nvkm_subdev_type, int, struct nvkm_gsp **);
490 int tu116_gsp_new(struct nvkm_device *, enum nvkm_subdev_type, int, struct nvkm_gsp **);
491 int ga100_gsp_new(struct nvkm_device *, enum nvkm_subdev_type, int, struct nvkm_gsp **);
492 int ga102_gsp_new(struct nvkm_device *, enum nvkm_subdev_type, int, struct nvkm_gsp **);
493 int gh100_gsp_new(struct nvkm_device *, enum nvkm_subdev_type, int, struct nvkm_gsp **);
494 int ad102_gsp_new(struct nvkm_device *, enum nvkm_subdev_type, int, struct nvkm_gsp **);
495 int gb100_gsp_new(struct nvkm_device *, enum nvkm_subdev_type, int, struct nvkm_gsp **);
496 int gb202_gsp_new(struct nvkm_device *, enum nvkm_subdev_type, int, struct nvkm_gsp **);
497 #endif
498