xref: /linux/drivers/gpu/drm/nouveau/include/nvkm/subdev/gsp.h (revision c5dbf04160005e07e8ca7232a7faa77ab1547ae0)
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 #define GSP_PAGE_SHIFT 12
9 #define GSP_PAGE_SIZE  BIT(GSP_PAGE_SHIFT)
10 
11 struct nvkm_gsp_mem {
12 	u32 size;
13 	void *data;
14 	dma_addr_t addr;
15 };
16 
17 struct nvkm_gsp_radix3 {
18 	struct nvkm_gsp_mem mem[3];
19 };
20 
21 int nvkm_gsp_sg(struct nvkm_device *, u64 size, struct sg_table *);
22 void nvkm_gsp_sg_free(struct nvkm_device *, struct sg_table *);
23 
24 typedef int (*nvkm_gsp_msg_ntfy_func)(void *priv, u32 fn, void *repv, u32 repc);
25 
26 struct nvkm_gsp_event;
27 typedef void (*nvkm_gsp_event_func)(struct nvkm_gsp_event *, void *repv, u32 repc);
28 
29 struct nvkm_gsp {
30 	const struct nvkm_gsp_func *func;
31 	struct nvkm_subdev subdev;
32 
33 	struct nvkm_falcon falcon;
34 
35 	struct {
36 		struct {
37 			const struct firmware *load;
38 			const struct firmware *unload;
39 		} booter;
40 		const struct firmware *bl;
41 		const struct firmware *rm;
42 	} fws;
43 
44 	struct nvkm_firmware fw;
45 	struct nvkm_gsp_mem sig;
46 	struct nvkm_gsp_radix3 radix3;
47 
48 	struct {
49 		struct {
50 			struct {
51 				u64 addr;
52 				u64 size;
53 			} vga_workspace;
54 			u64 addr;
55 			u64 size;
56 		} bios;
57 		struct {
58 			struct {
59 				u64 addr;
60 				u64 size;
61 			} frts, boot, elf, heap;
62 			u64 addr;
63 			u64 size;
64 		} wpr2;
65 		struct {
66 			u64 addr;
67 			u64 size;
68 		} heap;
69 		u64 addr;
70 		u64 size;
71 
72 		struct {
73 			u64 addr;
74 			u64 size;
75 		} region[16];
76 		int region_nr;
77 		u32 rsvd_size;
78 	} fb;
79 
80 	struct {
81 		struct nvkm_falcon_fw load;
82 		struct nvkm_falcon_fw unload;
83 	} booter;
84 
85 	struct {
86 		struct nvkm_gsp_mem fw;
87 		u32 code_offset;
88 		u32 data_offset;
89 		u32 manifest_offset;
90 		u32 app_version;
91 	} boot;
92 
93 	struct nvkm_gsp_mem libos;
94 	struct nvkm_gsp_mem loginit;
95 	struct nvkm_gsp_mem logintr;
96 	struct nvkm_gsp_mem logrm;
97 	struct nvkm_gsp_mem rmargs;
98 
99 	struct nvkm_gsp_mem wpr_meta;
100 
101 	struct {
102 		struct sg_table sgt;
103 		struct nvkm_gsp_radix3 radix3;
104 		struct nvkm_gsp_mem meta;
105 	} sr;
106 
107 	struct {
108 		struct nvkm_gsp_mem mem;
109 
110 		struct {
111 			int   nr;
112 			u32 size;
113 			u64 *ptr;
114 		} ptes;
115 
116 		struct {
117 			u32  size;
118 			void *ptr;
119 		} cmdq, msgq;
120 	} shm;
121 
122 	struct nvkm_gsp_cmdq {
123 		struct mutex mutex;
124 		u32 cnt;
125 		u32 seq;
126 		u32 *wptr;
127 		u32 *rptr;
128 	} cmdq;
129 
130 	struct nvkm_gsp_msgq {
131 		struct mutex mutex;
132 		u32 cnt;
133 		u32 *wptr;
134 		u32 *rptr;
135 		struct nvkm_gsp_msgq_ntfy {
136 			u32 fn;
137 			nvkm_gsp_msg_ntfy_func func;
138 			void *priv;
139 		} ntfy[16];
140 		int ntfy_nr;
141 		struct work_struct work;
142 	} msgq;
143 
144 	bool running;
145 
146 	/* Internal GSP-RM control handles. */
147 	struct {
148 		struct nvkm_gsp_client {
149 			struct nvkm_gsp_object {
150 				struct nvkm_gsp_client *client;
151 				struct nvkm_gsp_object *parent;
152 				u32 handle;
153 			} object;
154 
155 			struct nvkm_gsp *gsp;
156 
157 			struct list_head events;
158 		} client;
159 
160 		struct nvkm_gsp_device {
161 			struct nvkm_gsp_object object;
162 			struct nvkm_gsp_object subdevice;
163 		} device;
164 	} internal;
165 
166 	struct {
167 		enum nvkm_subdev_type type;
168 		int inst;
169 		u32 stall;
170 		u32 nonstall;
171 	} intr[32];
172 	int intr_nr;
173 
174 	struct {
175 		u64 rm_bar1_pdb;
176 		u64 rm_bar2_pdb;
177 	} bar;
178 
179 	struct {
180 		u8 gpcs;
181 		u8 tpcs;
182 	} gr;
183 
184 	const struct nvkm_gsp_rm {
185 		void *(*rpc_get)(struct nvkm_gsp *, u32 fn, u32 argc);
186 		void *(*rpc_push)(struct nvkm_gsp *, void *argv, bool wait, u32 repc);
187 		void (*rpc_done)(struct nvkm_gsp *gsp, void *repv);
188 
189 		void *(*rm_ctrl_get)(struct nvkm_gsp_object *, u32 cmd, u32 argc);
190 		void *(*rm_ctrl_push)(struct nvkm_gsp_object *, void *argv, u32 repc);
191 		void (*rm_ctrl_done)(struct nvkm_gsp_object *, void *repv);
192 
193 		void *(*rm_alloc_get)(struct nvkm_gsp_object *, u32 oclass, u32 argc);
194 		void *(*rm_alloc_push)(struct nvkm_gsp_object *, void *argv, u32 repc);
195 		void (*rm_alloc_done)(struct nvkm_gsp_object *, void *repv);
196 
197 		int (*rm_free)(struct nvkm_gsp_object *);
198 
199 		int (*client_ctor)(struct nvkm_gsp *, struct nvkm_gsp_client *);
200 		void (*client_dtor)(struct nvkm_gsp_client *);
201 
202 		int (*device_ctor)(struct nvkm_gsp_client *, struct nvkm_gsp_device *);
203 		void (*device_dtor)(struct nvkm_gsp_device *);
204 
205 		int (*event_ctor)(struct nvkm_gsp_device *, u32 handle, u32 id,
206 				  nvkm_gsp_event_func, struct nvkm_gsp_event *);
207 		void (*event_dtor)(struct nvkm_gsp_event *);
208 	} *rm;
209 
210 	struct {
211 		struct mutex mutex;;
212 		struct idr idr;
213 	} client_id;
214 };
215 
216 static inline bool
217 nvkm_gsp_rm(struct nvkm_gsp *gsp)
218 {
219 	return gsp && (gsp->fws.rm || gsp->fw.img);
220 }
221 
222 static inline void *
223 nvkm_gsp_rpc_get(struct nvkm_gsp *gsp, u32 fn, u32 argc)
224 {
225 	return gsp->rm->rpc_get(gsp, fn, argc);
226 }
227 
228 static inline void *
229 nvkm_gsp_rpc_push(struct nvkm_gsp *gsp, void *argv, bool wait, u32 repc)
230 {
231 	return gsp->rm->rpc_push(gsp, argv, wait, repc);
232 }
233 
234 static inline void *
235 nvkm_gsp_rpc_rd(struct nvkm_gsp *gsp, u32 fn, u32 argc)
236 {
237 	void *argv = nvkm_gsp_rpc_get(gsp, fn, argc);
238 
239 	if (IS_ERR_OR_NULL(argv))
240 		return argv;
241 
242 	return nvkm_gsp_rpc_push(gsp, argv, true, argc);
243 }
244 
245 static inline int
246 nvkm_gsp_rpc_wr(struct nvkm_gsp *gsp, void *argv, bool wait)
247 {
248 	void *repv = nvkm_gsp_rpc_push(gsp, argv, wait, 0);
249 
250 	if (IS_ERR(repv))
251 		return PTR_ERR(repv);
252 
253 	return 0;
254 }
255 
256 static inline void
257 nvkm_gsp_rpc_done(struct nvkm_gsp *gsp, void *repv)
258 {
259 	gsp->rm->rpc_done(gsp, repv);
260 }
261 
262 static inline void *
263 nvkm_gsp_rm_ctrl_get(struct nvkm_gsp_object *object, u32 cmd, u32 argc)
264 {
265 	return object->client->gsp->rm->rm_ctrl_get(object, cmd, argc);
266 }
267 
268 static inline void *
269 nvkm_gsp_rm_ctrl_push(struct nvkm_gsp_object *object, void *argv, u32 repc)
270 {
271 	return object->client->gsp->rm->rm_ctrl_push(object, argv, repc);
272 }
273 
274 static inline void *
275 nvkm_gsp_rm_ctrl_rd(struct nvkm_gsp_object *object, u32 cmd, u32 repc)
276 {
277 	void *argv = nvkm_gsp_rm_ctrl_get(object, cmd, repc);
278 
279 	if (IS_ERR(argv))
280 		return argv;
281 
282 	return nvkm_gsp_rm_ctrl_push(object, argv, repc);
283 }
284 
285 static inline int
286 nvkm_gsp_rm_ctrl_wr(struct nvkm_gsp_object *object, void *argv)
287 {
288 	void *repv = nvkm_gsp_rm_ctrl_push(object, argv, 0);
289 
290 	if (IS_ERR(repv))
291 		return PTR_ERR(repv);
292 
293 	return 0;
294 }
295 
296 static inline void
297 nvkm_gsp_rm_ctrl_done(struct nvkm_gsp_object *object, void *repv)
298 {
299 	object->client->gsp->rm->rm_ctrl_done(object, repv);
300 }
301 
302 static inline void *
303 nvkm_gsp_rm_alloc_get(struct nvkm_gsp_object *parent, u32 handle, u32 oclass, u32 argc,
304 		      struct nvkm_gsp_object *object)
305 {
306 	struct nvkm_gsp_client *client = parent->client;
307 	struct nvkm_gsp *gsp = client->gsp;
308 	void *argv;
309 
310 	object->client = parent->client;
311 	object->parent = parent;
312 	object->handle = handle;
313 
314 	argv = gsp->rm->rm_alloc_get(object, oclass, argc);
315 	if (IS_ERR_OR_NULL(argv)) {
316 		object->client = NULL;
317 		return argv;
318 	}
319 
320 	return argv;
321 }
322 
323 static inline void *
324 nvkm_gsp_rm_alloc_push(struct nvkm_gsp_object *object, void *argv, u32 repc)
325 {
326 	void *repv = object->client->gsp->rm->rm_alloc_push(object, argv, repc);
327 
328 	if (IS_ERR(repv))
329 		object->client = NULL;
330 
331 	return repv;
332 }
333 
334 static inline int
335 nvkm_gsp_rm_alloc_wr(struct nvkm_gsp_object *object, void *argv)
336 {
337 	void *repv = nvkm_gsp_rm_alloc_push(object, argv, 0);
338 
339 	if (IS_ERR(repv))
340 		return PTR_ERR(repv);
341 
342 	return 0;
343 }
344 
345 static inline void
346 nvkm_gsp_rm_alloc_done(struct nvkm_gsp_object *object, void *repv)
347 {
348 	object->client->gsp->rm->rm_alloc_done(object, repv);
349 }
350 
351 static inline int
352 nvkm_gsp_rm_alloc(struct nvkm_gsp_object *parent, u32 handle, u32 oclass, u32 argc,
353 		  struct nvkm_gsp_object *object)
354 {
355 	void *argv = nvkm_gsp_rm_alloc_get(parent, handle, oclass, argc, object);
356 
357 	if (IS_ERR_OR_NULL(argv))
358 		return argv ? PTR_ERR(argv) : -EIO;
359 
360 	return nvkm_gsp_rm_alloc_wr(object, argv);
361 }
362 
363 static inline int
364 nvkm_gsp_rm_free(struct nvkm_gsp_object *object)
365 {
366 	if (object->client)
367 		return object->client->gsp->rm->rm_free(object);
368 
369 	return 0;
370 }
371 
372 static inline int
373 nvkm_gsp_client_ctor(struct nvkm_gsp *gsp, struct nvkm_gsp_client *client)
374 {
375 	if (WARN_ON(!gsp->rm))
376 		return -ENOSYS;
377 
378 	return gsp->rm->client_ctor(gsp, client);
379 }
380 
381 static inline void
382 nvkm_gsp_client_dtor(struct nvkm_gsp_client *client)
383 {
384 	if (client->gsp)
385 		client->gsp->rm->client_dtor(client);
386 }
387 
388 static inline int
389 nvkm_gsp_device_ctor(struct nvkm_gsp_client *client, struct nvkm_gsp_device *device)
390 {
391 	return client->gsp->rm->device_ctor(client, device);
392 }
393 
394 static inline void
395 nvkm_gsp_device_dtor(struct nvkm_gsp_device *device)
396 {
397 	if (device->object.client)
398 		device->object.client->gsp->rm->device_dtor(device);
399 }
400 
401 static inline int
402 nvkm_gsp_client_device_ctor(struct nvkm_gsp *gsp,
403 			    struct nvkm_gsp_client *client, struct nvkm_gsp_device *device)
404 {
405 	int ret = nvkm_gsp_client_ctor(gsp, client);
406 
407 	if (ret == 0) {
408 		ret = nvkm_gsp_device_ctor(client, device);
409 		if (ret)
410 			nvkm_gsp_client_dtor(client);
411 	}
412 
413 	return ret;
414 }
415 
416 struct nvkm_gsp_event {
417 	struct nvkm_gsp_device *device;
418 	u32 id;
419 	nvkm_gsp_event_func func;
420 
421 	struct nvkm_gsp_object object;
422 
423 	struct list_head head;
424 };
425 
426 static inline int
427 nvkm_gsp_device_event_ctor(struct nvkm_gsp_device *device, u32 handle, u32 id,
428 			   nvkm_gsp_event_func func, struct nvkm_gsp_event *event)
429 {
430 	return device->object.client->gsp->rm->event_ctor(device, handle, id, func, event);
431 }
432 
433 static inline void
434 nvkm_gsp_event_dtor(struct nvkm_gsp_event *event)
435 {
436 	struct nvkm_gsp_device *device = event->device;
437 
438 	if (device)
439 		device->object.client->gsp->rm->event_dtor(event);
440 }
441 
442 int nvkm_gsp_intr_stall(struct nvkm_gsp *, enum nvkm_subdev_type, int);
443 int nvkm_gsp_intr_nonstall(struct nvkm_gsp *, enum nvkm_subdev_type, int);
444 
445 int gv100_gsp_new(struct nvkm_device *, enum nvkm_subdev_type, int, struct nvkm_gsp **);
446 int tu102_gsp_new(struct nvkm_device *, enum nvkm_subdev_type, int, struct nvkm_gsp **);
447 int tu116_gsp_new(struct nvkm_device *, enum nvkm_subdev_type, int, struct nvkm_gsp **);
448 int ga100_gsp_new(struct nvkm_device *, enum nvkm_subdev_type, int, struct nvkm_gsp **);
449 int ga102_gsp_new(struct nvkm_device *, enum nvkm_subdev_type, int, struct nvkm_gsp **);
450 int ad102_gsp_new(struct nvkm_device *, enum nvkm_subdev_type, int, struct nvkm_gsp **);
451 #endif
452