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