1 /*	$NetBSD: device.h,v 1.9 2021/12/18 23:45:33 riastradh Exp $	*/
2 
3 /* SPDX-License-Identifier: MIT */
4 #ifndef __NVKM_DEVICE_H__
5 #define __NVKM_DEVICE_H__
6 #include <core/oclass.h>
7 #include <core/event.h>
8 
9 enum nvkm_devidx {
10 	NVKM_SUBDEV_PCI,
11 	NVKM_SUBDEV_VBIOS,
12 	NVKM_SUBDEV_DEVINIT,
13 	NVKM_SUBDEV_TOP,
14 	NVKM_SUBDEV_IBUS,
15 	NVKM_SUBDEV_GPIO,
16 	NVKM_SUBDEV_I2C,
17 	NVKM_SUBDEV_FUSE,
18 	NVKM_SUBDEV_MXM,
19 	NVKM_SUBDEV_MC,
20 	NVKM_SUBDEV_BUS,
21 	NVKM_SUBDEV_TIMER,
22 	NVKM_SUBDEV_INSTMEM,
23 	NVKM_SUBDEV_FB,
24 	NVKM_SUBDEV_LTC,
25 	NVKM_SUBDEV_MMU,
26 	NVKM_SUBDEV_BAR,
27 	NVKM_SUBDEV_FAULT,
28 	NVKM_SUBDEV_ACR,
29 	NVKM_SUBDEV_PMU,
30 	NVKM_SUBDEV_VOLT,
31 	NVKM_SUBDEV_ICCSENSE,
32 	NVKM_SUBDEV_THERM,
33 	NVKM_SUBDEV_CLK,
34 	NVKM_SUBDEV_GSP,
35 
36 	NVKM_ENGINE_BSP,
37 
38 	NVKM_ENGINE_CE0,
39 	NVKM_ENGINE_CE1,
40 	NVKM_ENGINE_CE2,
41 	NVKM_ENGINE_CE3,
42 	NVKM_ENGINE_CE4,
43 	NVKM_ENGINE_CE5,
44 	NVKM_ENGINE_CE6,
45 	NVKM_ENGINE_CE7,
46 	NVKM_ENGINE_CE8,
47 	NVKM_ENGINE_CE_LAST = NVKM_ENGINE_CE8,
48 
49 	NVKM_ENGINE_CIPHER,
50 	NVKM_ENGINE_DISP,
51 	NVKM_ENGINE_DMAOBJ,
52 	NVKM_ENGINE_FIFO,
53 	NVKM_ENGINE_GR,
54 	NVKM_ENGINE_IFB,
55 	NVKM_ENGINE_ME,
56 	NVKM_ENGINE_MPEG,
57 	NVKM_ENGINE_MSENC,
58 	NVKM_ENGINE_MSPDEC,
59 	NVKM_ENGINE_MSPPP,
60 	NVKM_ENGINE_MSVLD,
61 
62 	NVKM_ENGINE_NVENC0,
63 	NVKM_ENGINE_NVENC1,
64 	NVKM_ENGINE_NVENC2,
65 	NVKM_ENGINE_NVENC_LAST = NVKM_ENGINE_NVENC2,
66 
67 	NVKM_ENGINE_NVDEC0,
68 	NVKM_ENGINE_NVDEC1,
69 	NVKM_ENGINE_NVDEC2,
70 	NVKM_ENGINE_NVDEC_LAST = NVKM_ENGINE_NVDEC2,
71 
72 	NVKM_ENGINE_PM,
73 	NVKM_ENGINE_SEC,
74 	NVKM_ENGINE_SEC2,
75 	NVKM_ENGINE_SW,
76 	NVKM_ENGINE_VIC,
77 	NVKM_ENGINE_VP,
78 
79 	NVKM_SUBDEV_NR
80 };
81 
82 enum nvkm_device_type {
83 	NVKM_DEVICE_PCI,
84 	NVKM_DEVICE_AGP,
85 	NVKM_DEVICE_PCIE,
86 	NVKM_DEVICE_TEGRA,
87 };
88 
89 struct nvkm_device {
90 	const struct nvkm_device_func *func;
91 	const struct nvkm_device_quirk *quirk;
92 	struct device *dev;
93 	enum nvkm_device_type type;
94 	u64 handle;
95 	const char *name;
96 	const char *cfgopt;
97 	const char *dbgopt;
98 
99 	struct list_head head;
100 	struct mutex mutex;
101 	int refcount;
102 
103 #ifdef __NetBSD__
104 	bus_space_tag_t mmiot;
105 	bus_space_handle_t mmioh;
106 	bus_addr_t mmioaddr;
107 	bus_size_t mmiosz;
108 #else
109 	void __iomem *pri;
110 #endif
111 
112 	struct nvkm_event event;
113 
114 	u64 disable_mask;
115 	u32 debug;
116 
117 	const struct nvkm_device_chip *chip;
118 	enum {
119 		NV_04    = 0x04,
120 		NV_10    = 0x10,
121 		NV_11    = 0x11,
122 		NV_20    = 0x20,
123 		NV_30    = 0x30,
124 		NV_40    = 0x40,
125 		NV_50    = 0x50,
126 		NV_C0    = 0xc0,
127 		NV_E0    = 0xe0,
128 		GM100    = 0x110,
129 		GP100    = 0x130,
130 		GV100    = 0x140,
131 		TU100    = 0x160,
132 	} card_type;
133 	u32 chipset;
134 	u8  chiprev;
135 	u32 crystal;
136 
137 	struct {
138 		struct notifier_block nb;
139 	} acpi;
140 
141 	struct nvkm_acr *acr;
142 	struct nvkm_bar *bar;
143 	struct nvkm_bios *bios;
144 	struct nvkm_bus *bus;
145 	struct nvkm_clk *clk;
146 	struct nvkm_devinit *devinit;
147 	struct nvkm_fault *fault;
148 	struct nvkm_fb *fb;
149 	struct nvkm_fuse *fuse;
150 	struct nvkm_gpio *gpio;
151 	struct nvkm_gsp *gsp;
152 	struct nvkm_i2c *i2c;
153 	struct nvkm_subdev *ibus;
154 	struct nvkm_iccsense *iccsense;
155 	struct nvkm_instmem *imem;
156 	struct nvkm_ltc *ltc;
157 	struct nvkm_mc *mc;
158 	struct nvkm_mmu *mmu;
159 	struct nvkm_subdev *mxm;
160 	struct nvkm_pci *pci;
161 	struct nvkm_pmu *pmu;
162 	struct nvkm_therm *therm;
163 	struct nvkm_timer *timer;
164 	struct nvkm_top *top;
165 	struct nvkm_volt *volt;
166 
167 	struct nvkm_engine *bsp;
168 	struct nvkm_engine *ce[9];
169 	struct nvkm_engine *cipher;
170 	struct nvkm_disp *disp;
171 	struct nvkm_dma *dma;
172 	struct nvkm_fifo *fifo;
173 	struct nvkm_gr *gr;
174 	struct nvkm_engine *ifb;
175 	struct nvkm_engine *me;
176 	struct nvkm_engine *mpeg;
177 	struct nvkm_engine *msenc;
178 	struct nvkm_engine *mspdec;
179 	struct nvkm_engine *msppp;
180 	struct nvkm_engine *msvld;
181 	struct nvkm_nvenc *nvenc[3];
182 	struct nvkm_nvdec *nvdec[3];
183 	struct nvkm_pm *pm;
184 	struct nvkm_engine *sec;
185 	struct nvkm_sec2 *sec2;
186 	struct nvkm_sw *sw;
187 	struct nvkm_engine *vic;
188 	struct nvkm_engine *vp;
189 };
190 
191 struct nvkm_subdev *nvkm_device_subdev(struct nvkm_device *, int index);
192 struct nvkm_engine *nvkm_device_engine(struct nvkm_device *, int index);
193 
194 struct nvkm_device_func {
195 	struct nvkm_device_pci *(*pci)(struct nvkm_device *);
196 	struct nvkm_device_tegra *(*tegra)(struct nvkm_device *);
197 	void *(*dtor)(struct nvkm_device *);
198 	int (*preinit)(struct nvkm_device *);
199 	int (*init)(struct nvkm_device *);
200 	void (*fini)(struct nvkm_device *, bool suspend);
201 #ifdef __NetBSD__
202 	bus_dma_tag_t (*dma_tag)(struct nvkm_device *);
203 	bus_space_tag_t (*resource_tag)(struct nvkm_device *, unsigned bar);
204 #endif
205 	resource_size_t (*resource_addr)(struct nvkm_device *, unsigned bar);
206 	resource_size_t (*resource_size)(struct nvkm_device *, unsigned bar);
207 	bool cpu_coherent;
208 };
209 
210 struct nvkm_device_quirk {
211 	u8 tv_pin_mask;
212 	u8 tv_gpio;
213 };
214 
215 struct nvkm_device_chip {
216 	const char *name;
217 
218 	int (*acr     )(struct nvkm_device *, int idx, struct nvkm_acr **);
219 	int (*bar     )(struct nvkm_device *, int idx, struct nvkm_bar **);
220 	int (*bios    )(struct nvkm_device *, int idx, struct nvkm_bios **);
221 	int (*bus     )(struct nvkm_device *, int idx, struct nvkm_bus **);
222 	int (*clk     )(struct nvkm_device *, int idx, struct nvkm_clk **);
223 	int (*devinit )(struct nvkm_device *, int idx, struct nvkm_devinit **);
224 	int (*fault   )(struct nvkm_device *, int idx, struct nvkm_fault **);
225 	int (*fb      )(struct nvkm_device *, int idx, struct nvkm_fb **);
226 	int (*fuse    )(struct nvkm_device *, int idx, struct nvkm_fuse **);
227 	int (*gpio    )(struct nvkm_device *, int idx, struct nvkm_gpio **);
228 	int (*gsp     )(struct nvkm_device *, int idx, struct nvkm_gsp **);
229 	int (*i2c     )(struct nvkm_device *, int idx, struct nvkm_i2c **);
230 	int (*ibus    )(struct nvkm_device *, int idx, struct nvkm_subdev **);
231 	int (*iccsense)(struct nvkm_device *, int idx, struct nvkm_iccsense **);
232 	int (*imem    )(struct nvkm_device *, int idx, struct nvkm_instmem **);
233 	int (*ltc     )(struct nvkm_device *, int idx, struct nvkm_ltc **);
234 	int (*mc      )(struct nvkm_device *, int idx, struct nvkm_mc **);
235 	int (*mmu     )(struct nvkm_device *, int idx, struct nvkm_mmu **);
236 	int (*mxm     )(struct nvkm_device *, int idx, struct nvkm_subdev **);
237 	int (*pci     )(struct nvkm_device *, int idx, struct nvkm_pci **);
238 	int (*pmu     )(struct nvkm_device *, int idx, struct nvkm_pmu **);
239 	int (*therm   )(struct nvkm_device *, int idx, struct nvkm_therm **);
240 	int (*timer   )(struct nvkm_device *, int idx, struct nvkm_timer **);
241 	int (*top     )(struct nvkm_device *, int idx, struct nvkm_top **);
242 	int (*volt    )(struct nvkm_device *, int idx, struct nvkm_volt **);
243 
244 	int (*bsp     )(struct nvkm_device *, int idx, struct nvkm_engine **);
245 	int (*ce[9]   )(struct nvkm_device *, int idx, struct nvkm_engine **);
246 	int (*cipher  )(struct nvkm_device *, int idx, struct nvkm_engine **);
247 	int (*disp    )(struct nvkm_device *, int idx, struct nvkm_disp **);
248 	int (*dma     )(struct nvkm_device *, int idx, struct nvkm_dma **);
249 	int (*fifo    )(struct nvkm_device *, int idx, struct nvkm_fifo **);
250 	int (*gr      )(struct nvkm_device *, int idx, struct nvkm_gr **);
251 	int (*ifb     )(struct nvkm_device *, int idx, struct nvkm_engine **);
252 	int (*me      )(struct nvkm_device *, int idx, struct nvkm_engine **);
253 	int (*mpeg    )(struct nvkm_device *, int idx, struct nvkm_engine **);
254 	int (*msenc   )(struct nvkm_device *, int idx, struct nvkm_engine **);
255 	int (*mspdec  )(struct nvkm_device *, int idx, struct nvkm_engine **);
256 	int (*msppp   )(struct nvkm_device *, int idx, struct nvkm_engine **);
257 	int (*msvld   )(struct nvkm_device *, int idx, struct nvkm_engine **);
258 	int (*nvenc[3])(struct nvkm_device *, int idx, struct nvkm_nvenc **);
259 	int (*nvdec[3])(struct nvkm_device *, int idx, struct nvkm_nvdec **);
260 	int (*pm      )(struct nvkm_device *, int idx, struct nvkm_pm **);
261 	int (*sec     )(struct nvkm_device *, int idx, struct nvkm_engine **);
262 	int (*sec2    )(struct nvkm_device *, int idx, struct nvkm_sec2 **);
263 	int (*sw      )(struct nvkm_device *, int idx, struct nvkm_sw **);
264 	int (*vic     )(struct nvkm_device *, int idx, struct nvkm_engine **);
265 	int (*vp      )(struct nvkm_device *, int idx, struct nvkm_engine **);
266 };
267 
268 struct nvkm_device *nvkm_device_find(u64 name);
269 int nvkm_device_list(u64 *name, int size);
270 
271 #ifdef __NetBSD__
272 void	nvkm_devices_init(void);
273 void	nvkm_devices_fini(void);
274 #endif
275 
276 /* privileged register interface accessor macros */
277 #ifdef __NetBSD__
278 static inline uint8_t
nvkm_rd08(struct nvkm_device * d,bus_size_t a)279 nvkm_rd08(struct nvkm_device *d, bus_size_t a)
280 {
281 	return bus_space_read_1(d->mmiot, d->mmioh, a);
282 }
283 static inline uint16_t
nvkm_rd16(struct nvkm_device * d,bus_size_t a)284 nvkm_rd16(struct nvkm_device *d, bus_size_t a)
285 {
286 	return bus_space_read_stream_2(d->mmiot, d->mmioh, a);
287 }
288 static inline uint32_t
nvkm_rd32(struct nvkm_device * d,bus_size_t a)289 nvkm_rd32(struct nvkm_device *d, bus_size_t a)
290 {
291 	return bus_space_read_stream_4(d->mmiot, d->mmioh, a);
292 }
293 static inline void
nvkm_wr08(struct nvkm_device * d,bus_size_t a,uint8_t v)294 nvkm_wr08(struct nvkm_device *d, bus_size_t a, uint8_t v)
295 {
296 	bus_space_write_1(d->mmiot, d->mmioh, a, v);
297 }
298 static inline void
nvkm_wr16(struct nvkm_device * d,bus_size_t a,uint16_t v)299 nvkm_wr16(struct nvkm_device *d, bus_size_t a, uint16_t v)
300 {
301 	bus_space_write_stream_2(d->mmiot, d->mmioh, a, v);
302 }
303 static inline void
nvkm_wr32(struct nvkm_device * d,bus_size_t a,uint32_t v)304 nvkm_wr32(struct nvkm_device *d, bus_size_t a, uint32_t v)
305 {
306 	bus_space_write_stream_4(d->mmiot, d->mmioh, a, v);
307 }
308 #else
309 #define nvkm_rd08(d,a) ioread8((d)->pri + (a))
310 #define nvkm_rd16(d,a) ioread16_native((d)->pri + (a))
311 #define nvkm_rd32(d,a) ioread32_native((d)->pri + (a))
312 #define nvkm_wr08(d,a,v) iowrite8((v), (d)->pri + (a))
313 #define nvkm_wr16(d,a,v) iowrite16_native((v), (d)->pri + (a))
314 #define nvkm_wr32(d,a,v) iowrite32_native((v), (d)->pri + (a))
315 #endif
316 #define nvkm_mask(d,a,m,v) ({                                                  \
317 	struct nvkm_device *_device = (d);                                     \
318 	u32 _addr = (a), _temp = nvkm_rd32(_device, _addr);                    \
319 	nvkm_wr32(_device, _addr, (_temp & ~(m)) | (v));                       \
320 	_temp;                                                                 \
321 })
322 
323 void nvkm_device_del(struct nvkm_device **);
324 
325 struct nvkm_device_oclass {
326 	int (*ctor)(struct nvkm_device *, const struct nvkm_oclass *,
327 		    void *data, u32 size, struct nvkm_object **);
328 	struct nvkm_sclass base;
329 };
330 
331 extern const struct nvkm_sclass nvkm_udevice_sclass;
332 
333 /* device logging */
334 #define nvdev_printk_(d,l,p,f,a...) do {                                       \
335 	const struct nvkm_device *_device = (d);                               \
336 	if (_device->debug >= (l))                                             \
337 		dev_##p(_device->dev, f, ##a);                                 \
338 } while(0)
339 #define nvdev_printk(d,l,p,f,a...) nvdev_printk_((d), NV_DBG_##l, p, f, ##a)
340 #define nvdev_fatal(d,f,a...) nvdev_printk((d), FATAL,   crit, f, ##a)
341 #define nvdev_error(d,f,a...) nvdev_printk((d), ERROR,    err, f, ##a)
342 #define nvdev_warn(d,f,a...)  nvdev_printk((d),  WARN, notice, f, ##a)
343 #define nvdev_info(d,f,a...)  nvdev_printk((d),  INFO,   info, f, ##a)
344 #define nvdev_debug(d,f,a...) nvdev_printk((d), DEBUG,   info, f, ##a)
345 #define nvdev_trace(d,f,a...) nvdev_printk((d), TRACE,   info, f, ##a)
346 #define nvdev_spam(d,f,a...)  nvdev_printk((d),  SPAM,    dbg, f, ##a)
347 #endif
348