xref: /linux/drivers/gpu/drm/nouveau/nouveau_drv.h (revision 2c1ed907520c50326b8f604907a8478b27881a2e)
1 /* SPDX-License-Identifier: MIT */
2 #ifndef __NOUVEAU_DRV_H__
3 #define __NOUVEAU_DRV_H__
4 
5 #define DRIVER_AUTHOR		"Nouveau Project"
6 #define DRIVER_EMAIL		"nouveau@lists.freedesktop.org"
7 
8 #define DRIVER_NAME		"nouveau"
9 #define DRIVER_DESC		"nVidia Riva/TNT/GeForce/Quadro/Tesla/Tegra K1+"
10 
11 #define DRIVER_MAJOR		1
12 #define DRIVER_MINOR		4
13 #define DRIVER_PATCHLEVEL	0
14 
15 /*
16  * 1.1.1:
17  * 	- added support for tiled system memory buffer objects
18  *      - added support for NOUVEAU_GETPARAM_GRAPH_UNITS on [nvc0,nve0].
19  *      - added support for compressed memory storage types on [nvc0,nve0].
20  *      - added support for software methods 0x600,0x644,0x6ac on nvc0
21  *        to control registers on the MPs to enable performance counters,
22  *        and to control the warp error enable mask (OpenGL requires out of
23  *        bounds access to local memory to be silently ignored / return 0).
24  * 1.1.2:
25  *      - fixes multiple bugs in flip completion events and timestamping
26  * 1.2.0:
27  * 	- object api exposed to userspace
28  * 	- fermi,kepler,maxwell zbc
29  * 1.2.1:
30  *      - allow concurrent access to bo's mapped read/write.
31  * 1.2.2:
32  *      - add NOUVEAU_GEM_DOMAIN_COHERENT flag
33  * 1.3.0:
34  *      - NVIF ABI modified, safe because only (current) users are test
35  *        programs that get directly linked with NVKM.
36  * 1.3.1:
37  *      - implemented limited ABI16/NVIF interop
38  */
39 
40 #include <linux/notifier.h>
41 
42 #include <nvif/client.h>
43 #include <nvif/device.h>
44 #include <nvif/ioctl.h>
45 #include <nvif/mmu.h>
46 #include <nvif/vmm.h>
47 
48 #include <drm/drm_connector.h>
49 #include <drm/drm_device.h>
50 #include <drm/drm_drv.h>
51 #include <drm/drm_file.h>
52 
53 #include <drm/ttm/ttm_bo.h>
54 #include <drm/ttm/ttm_placement.h>
55 
56 #include <drm/drm_audio_component.h>
57 
58 #include "uapi/drm/nouveau_drm.h"
59 
60 struct nouveau_channel;
61 struct platform_device;
62 
63 #include "nouveau_fence.h"
64 #include "nouveau_bios.h"
65 #include "nouveau_sched.h"
66 #include "nouveau_vmm.h"
67 #include "nouveau_uvmm.h"
68 
69 struct nouveau_drm_tile {
70 	struct nouveau_fence *fence;
71 	bool used;
72 };
73 
74 enum nouveau_drm_object_route {
75 	NVDRM_OBJECT_NVIF = NVIF_IOCTL_V0_OWNER_NVIF,
76 	NVDRM_OBJECT_USIF,
77 	NVDRM_OBJECT_ABI16,
78 	NVDRM_OBJECT_ANY = NVIF_IOCTL_V0_OWNER_ANY,
79 };
80 
81 enum nouveau_drm_handle {
82 	NVDRM_CHAN    = 0xcccc0000, /* |= client chid */
83 	NVDRM_NVSW    = 0x55550000,
84 };
85 
86 struct nouveau_cli {
87 	struct nvif_client base;
88 	struct nouveau_drm *drm;
89 	struct mutex mutex;
90 
91 	struct nvif_device device;
92 	struct nvif_mmu mmu;
93 	struct nouveau_vmm vmm;
94 	struct nouveau_vmm svm;
95 	struct {
96 		struct nouveau_uvmm *ptr;
97 		bool disabled;
98 	} uvmm;
99 
100 	struct nouveau_sched *sched;
101 
102 	const struct nvif_mclass *mem;
103 
104 	struct list_head head;
105 	void *abi16;
106 	struct list_head objects;
107 	char name[32];
108 
109 	struct work_struct work;
110 	struct list_head worker;
111 	struct mutex lock;
112 };
113 
114 struct nouveau_cli_work {
115 	void (*func)(struct nouveau_cli_work *);
116 	struct nouveau_cli *cli;
117 	struct list_head head;
118 
119 	struct dma_fence *fence;
120 	struct dma_fence_cb cb;
121 };
122 
123 static inline struct nouveau_uvmm *
nouveau_cli_uvmm(struct nouveau_cli * cli)124 nouveau_cli_uvmm(struct nouveau_cli *cli)
125 {
126 	return cli ? cli->uvmm.ptr : NULL;
127 }
128 
129 static inline struct nouveau_uvmm *
nouveau_cli_uvmm_locked(struct nouveau_cli * cli)130 nouveau_cli_uvmm_locked(struct nouveau_cli *cli)
131 {
132 	struct nouveau_uvmm *uvmm;
133 
134 	mutex_lock(&cli->mutex);
135 	uvmm = nouveau_cli_uvmm(cli);
136 	mutex_unlock(&cli->mutex);
137 
138 	return uvmm;
139 }
140 
141 static inline struct nouveau_vmm *
nouveau_cli_vmm(struct nouveau_cli * cli)142 nouveau_cli_vmm(struct nouveau_cli *cli)
143 {
144 	struct nouveau_uvmm *uvmm;
145 
146 	uvmm = nouveau_cli_uvmm(cli);
147 	if (uvmm)
148 		return &uvmm->vmm;
149 
150 	if (cli->svm.cli)
151 		return &cli->svm;
152 
153 	return &cli->vmm;
154 }
155 
156 static inline void
__nouveau_cli_disable_uvmm_noinit(struct nouveau_cli * cli)157 __nouveau_cli_disable_uvmm_noinit(struct nouveau_cli *cli)
158 {
159 	struct nouveau_uvmm *uvmm = nouveau_cli_uvmm(cli);
160 
161 	if (!uvmm)
162 		cli->uvmm.disabled = true;
163 }
164 
165 static inline void
nouveau_cli_disable_uvmm_noinit(struct nouveau_cli * cli)166 nouveau_cli_disable_uvmm_noinit(struct nouveau_cli *cli)
167 {
168 	mutex_lock(&cli->mutex);
169 	__nouveau_cli_disable_uvmm_noinit(cli);
170 	mutex_unlock(&cli->mutex);
171 }
172 
173 void nouveau_cli_work_queue(struct nouveau_cli *, struct dma_fence *,
174 			    struct nouveau_cli_work *);
175 
176 static inline struct nouveau_cli *
nouveau_cli(struct drm_file * fpriv)177 nouveau_cli(struct drm_file *fpriv)
178 {
179 	return fpriv ? fpriv->driver_priv : NULL;
180 }
181 
182 static inline void
u_free(void * addr)183 u_free(void *addr)
184 {
185 	kvfree(addr);
186 }
187 
188 static inline void *
u_memcpya(uint64_t user,unsigned int nmemb,unsigned int size)189 u_memcpya(uint64_t user, unsigned int nmemb, unsigned int size)
190 {
191 	void __user *userptr = u64_to_user_ptr(user);
192 	size_t bytes;
193 
194 	if (unlikely(check_mul_overflow(nmemb, size, &bytes)))
195 		return ERR_PTR(-EOVERFLOW);
196 	return vmemdup_user(userptr, bytes);
197 }
198 
199 #include <nvif/object.h>
200 #include <nvif/parent.h>
201 
202 struct nouveau_drm {
203 	struct nvkm_device *nvkm;
204 	struct nvif_parent parent;
205 	struct mutex client_mutex;
206 	struct nvif_client _client;
207 	struct nvif_device device;
208 	struct nvif_mmu mmu;
209 
210 	struct nouveau_cli client;
211 	struct drm_device *dev;
212 
213 	struct list_head clients;
214 
215 	/**
216 	 * @clients_lock: Protects access to the @clients list of &struct nouveau_cli.
217 	 */
218 	struct mutex clients_lock;
219 
220 	u8 old_pm_cap;
221 
222 	struct {
223 		struct agp_bridge_data *bridge;
224 		u32 base;
225 		u32 size;
226 		bool cma;
227 	} agp;
228 
229 	/* TTM interface support */
230 	struct {
231 		struct ttm_device bdev;
232 		atomic_t validate_sequence;
233 		int (*move)(struct nouveau_channel *,
234 			    struct ttm_buffer_object *,
235 			    struct ttm_resource *, struct ttm_resource *);
236 		struct nouveau_channel *chan;
237 		struct nvif_object copy;
238 		int mtrr;
239 		int type_vram;
240 		int type_host[2];
241 		int type_ncoh[2];
242 		struct mutex io_reserve_mutex;
243 		struct list_head io_reserve_lru;
244 	} ttm;
245 
246 	/* GEM interface support */
247 	struct {
248 		u64 vram_available;
249 		u64 gart_available;
250 	} gem;
251 
252 	/* synchronisation */
253 	void *fence;
254 
255 	/* Global channel management. */
256 	int chan_total; /* Number of channels across all runlists. */
257 	int chan_nr;	/* 0 if per-runlist CHIDs. */
258 	int runl_nr;
259 	struct {
260 		int chan_nr;
261 		int chan_id_base;
262 		u64 context_base;
263 	} *runl;
264 
265 	/* Workqueue used for channel schedulers. */
266 	struct workqueue_struct *sched_wq;
267 
268 	/* context for accelerated drm-internal operations */
269 	struct nouveau_channel *cechan;
270 	struct nouveau_channel *channel;
271 	struct nvkm_gpuobj *notify;
272 	struct nvif_object ntfy;
273 
274 	/* nv10-nv40 tiling regions */
275 	struct {
276 		struct nouveau_drm_tile reg[15];
277 		spinlock_t lock;
278 	} tile;
279 
280 	/* modesetting */
281 	struct nvbios vbios;
282 	struct nouveau_display *display;
283 	bool headless;
284 	struct work_struct hpd_work;
285 	spinlock_t hpd_lock;
286 	u32 hpd_pending;
287 #ifdef CONFIG_ACPI
288 	struct notifier_block acpi_nb;
289 #endif
290 
291 	/* power management */
292 	struct nouveau_hwmon *hwmon;
293 	struct nouveau_debugfs *debugfs;
294 
295 	/* led management */
296 	struct nouveau_led *led;
297 
298 	struct dev_pm_domain vga_pm_domain;
299 
300 	struct nouveau_svm *svm;
301 
302 	struct nouveau_dmem *dmem;
303 
304 	struct {
305 		struct drm_audio_component *component;
306 		struct mutex lock;
307 		bool component_registered;
308 	} audio;
309 };
310 
311 static inline struct nouveau_drm *
nouveau_drm(struct drm_device * dev)312 nouveau_drm(struct drm_device *dev)
313 {
314 	return dev->dev_private;
315 }
316 
317 static inline bool
nouveau_drm_use_coherent_gpu_mapping(struct nouveau_drm * drm)318 nouveau_drm_use_coherent_gpu_mapping(struct nouveau_drm *drm)
319 {
320 	struct nvif_mmu *mmu = &drm->client.mmu;
321 	return !(mmu->type[drm->ttm.type_host[0]].type & NVIF_MEM_UNCACHED);
322 }
323 
324 int nouveau_pmops_suspend(struct device *);
325 int nouveau_pmops_resume(struct device *);
326 bool nouveau_pmops_runtime(void);
327 
328 #include <nvkm/core/tegra.h>
329 
330 struct drm_device *
331 nouveau_platform_device_create(const struct nvkm_device_tegra_func *,
332 			       struct platform_device *, struct nvkm_device **);
333 void nouveau_drm_device_remove(struct nouveau_drm *);
334 
335 #define NV_PRINTK(l,c,f,a...) do {                                             \
336 	struct nouveau_cli *_cli = (c);                                        \
337 	dev_##l(_cli->drm->dev->dev, "%s: "f, _cli->name, ##a);                \
338 } while(0)
339 
340 #define NV_PRINTK_(l,drm,f,a...) do {             \
341 	dev_##l((drm)->nvkm->dev, "drm: "f, ##a); \
342 } while(0)
343 #define NV_FATAL(drm,f,a...) NV_PRINTK_(crit, (drm), f, ##a)
344 #define NV_ERROR(drm,f,a...) NV_PRINTK_(err, (drm), f, ##a)
345 #define NV_WARN(drm,f,a...) NV_PRINTK_(warn, (drm), f, ##a)
346 #define NV_INFO(drm,f,a...) NV_PRINTK_(info, (drm), f, ##a)
347 
348 #define NV_DEBUG(drm,f,a...) do {                                              \
349 	if (drm_debug_enabled(DRM_UT_DRIVER))                                  \
350 		NV_PRINTK_(info, (drm), f, ##a);                               \
351 } while(0)
352 #define NV_ATOMIC(drm,f,a...) do {                                             \
353 	if (drm_debug_enabled(DRM_UT_ATOMIC))                                  \
354 		NV_PRINTK_(info, (drm), f, ##a);                               \
355 } while(0)
356 
357 #define NV_PRINTK_ONCE(l,c,f,a...) NV_PRINTK(l##_once,c,f, ##a)
358 
359 #define NV_ERROR_ONCE(drm,f,a...) NV_PRINTK_ONCE(err, &(drm)->client, f, ##a)
360 #define NV_WARN_ONCE(drm,f,a...) NV_PRINTK_ONCE(warn, &(drm)->client, f, ##a)
361 #define NV_INFO_ONCE(drm,f,a...) NV_PRINTK_ONCE(info, &(drm)->client, f, ##a)
362 
363 extern int nouveau_modeset;
364 
365 /*XXX: Don't use these in new code.
366  *
367  * These accessors are used in a few places (mostly older code paths)
368  * to get direct access to NVKM structures, where a more well-defined
369  * interface doesn't exist.  Outside of the current use, these should
370  * not be relied on, and instead be implemented as NVIF.
371  *
372  * This is especially important when considering GSP-RM, as a lot the
373  * modules don't exist, or are "stub" implementations that just allow
374  * the GSP-RM paths to be bootstrapped.
375  */
376 #include <subdev/bios.h>
377 #include <subdev/fb.h>
378 #include <subdev/gpio.h>
379 #include <subdev/clk.h>
380 #include <subdev/i2c.h>
381 #include <subdev/timer.h>
382 #include <subdev/therm.h>
383 
384 static inline struct nvkm_device *
nvxx_device(struct nouveau_drm * drm)385 nvxx_device(struct nouveau_drm *drm)
386 {
387 	return drm->nvkm;
388 }
389 
390 #define nvxx_bios(a) nvxx_device(a)->bios
391 #define nvxx_fb(a) nvxx_device(a)->fb
392 #define nvxx_gpio(a) nvxx_device(a)->gpio
393 #define nvxx_clk(a) nvxx_device(a)->clk
394 #define nvxx_i2c(a) nvxx_device(a)->i2c
395 #define nvxx_iccsense(a) nvxx_device(a)->iccsense
396 #define nvxx_therm(a) nvxx_device(a)->therm
397 #define nvxx_volt(a) nvxx_device(a)->volt
398 
399 #include <engine/gr.h>
400 
401 #define nvxx_gr(a) nvxx_device(a)->gr
402 #endif
403