Home | History | Annotate | Line # | Download | only in core
      1 /*	$NetBSD: device.h,v 1.10 2024/04/16 14:34:02 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 #ifdef __NetBSD__
     96 	struct acpi_devnode *acpidev;
     97 #endif
     98 	const char *name;
     99 	const char *cfgopt;
    100 	const char *dbgopt;
    101 
    102 	struct list_head head;
    103 	struct mutex mutex;
    104 	int refcount;
    105 
    106 #ifdef __NetBSD__
    107 	bus_space_tag_t mmiot;
    108 	bus_space_handle_t mmioh;
    109 	bus_addr_t mmioaddr;
    110 	bus_size_t mmiosz;
    111 #else
    112 	void __iomem *pri;
    113 #endif
    114 
    115 	struct nvkm_event event;
    116 
    117 	u64 disable_mask;
    118 	u32 debug;
    119 
    120 	const struct nvkm_device_chip *chip;
    121 	enum {
    122 		NV_04    = 0x04,
    123 		NV_10    = 0x10,
    124 		NV_11    = 0x11,
    125 		NV_20    = 0x20,
    126 		NV_30    = 0x30,
    127 		NV_40    = 0x40,
    128 		NV_50    = 0x50,
    129 		NV_C0    = 0xc0,
    130 		NV_E0    = 0xe0,
    131 		GM100    = 0x110,
    132 		GP100    = 0x130,
    133 		GV100    = 0x140,
    134 		TU100    = 0x160,
    135 	} card_type;
    136 	u32 chipset;
    137 	u8  chiprev;
    138 	u32 crystal;
    139 
    140 	struct {
    141 		struct notifier_block nb;
    142 	} acpi;
    143 
    144 	struct nvkm_acr *acr;
    145 	struct nvkm_bar *bar;
    146 	struct nvkm_bios *bios;
    147 	struct nvkm_bus *bus;
    148 	struct nvkm_clk *clk;
    149 	struct nvkm_devinit *devinit;
    150 	struct nvkm_fault *fault;
    151 	struct nvkm_fb *fb;
    152 	struct nvkm_fuse *fuse;
    153 	struct nvkm_gpio *gpio;
    154 	struct nvkm_gsp *gsp;
    155 	struct nvkm_i2c *i2c;
    156 	struct nvkm_subdev *ibus;
    157 	struct nvkm_iccsense *iccsense;
    158 	struct nvkm_instmem *imem;
    159 	struct nvkm_ltc *ltc;
    160 	struct nvkm_mc *mc;
    161 	struct nvkm_mmu *mmu;
    162 	struct nvkm_subdev *mxm;
    163 	struct nvkm_pci *pci;
    164 	struct nvkm_pmu *pmu;
    165 	struct nvkm_therm *therm;
    166 	struct nvkm_timer *timer;
    167 	struct nvkm_top *top;
    168 	struct nvkm_volt *volt;
    169 
    170 	struct nvkm_engine *bsp;
    171 	struct nvkm_engine *ce[9];
    172 	struct nvkm_engine *cipher;
    173 	struct nvkm_disp *disp;
    174 	struct nvkm_dma *dma;
    175 	struct nvkm_fifo *fifo;
    176 	struct nvkm_gr *gr;
    177 	struct nvkm_engine *ifb;
    178 	struct nvkm_engine *me;
    179 	struct nvkm_engine *mpeg;
    180 	struct nvkm_engine *msenc;
    181 	struct nvkm_engine *mspdec;
    182 	struct nvkm_engine *msppp;
    183 	struct nvkm_engine *msvld;
    184 	struct nvkm_nvenc *nvenc[3];
    185 	struct nvkm_nvdec *nvdec[3];
    186 	struct nvkm_pm *pm;
    187 	struct nvkm_engine *sec;
    188 	struct nvkm_sec2 *sec2;
    189 	struct nvkm_sw *sw;
    190 	struct nvkm_engine *vic;
    191 	struct nvkm_engine *vp;
    192 };
    193 
    194 struct nvkm_subdev *nvkm_device_subdev(struct nvkm_device *, int index);
    195 struct nvkm_engine *nvkm_device_engine(struct nvkm_device *, int index);
    196 
    197 struct nvkm_device_func {
    198 	struct nvkm_device_pci *(*pci)(struct nvkm_device *);
    199 	struct nvkm_device_tegra *(*tegra)(struct nvkm_device *);
    200 	void *(*dtor)(struct nvkm_device *);
    201 	int (*preinit)(struct nvkm_device *);
    202 	int (*init)(struct nvkm_device *);
    203 	void (*fini)(struct nvkm_device *, bool suspend);
    204 #ifdef __NetBSD__
    205 	bus_dma_tag_t (*dma_tag)(struct nvkm_device *);
    206 	bus_space_tag_t (*resource_tag)(struct nvkm_device *, unsigned bar);
    207 #endif
    208 	resource_size_t (*resource_addr)(struct nvkm_device *, unsigned bar);
    209 	resource_size_t (*resource_size)(struct nvkm_device *, unsigned bar);
    210 	bool cpu_coherent;
    211 };
    212 
    213 struct nvkm_device_quirk {
    214 	u8 tv_pin_mask;
    215 	u8 tv_gpio;
    216 };
    217 
    218 struct nvkm_device_chip {
    219 	const char *name;
    220 
    221 	int (*acr     )(struct nvkm_device *, int idx, struct nvkm_acr **);
    222 	int (*bar     )(struct nvkm_device *, int idx, struct nvkm_bar **);
    223 	int (*bios    )(struct nvkm_device *, int idx, struct nvkm_bios **);
    224 	int (*bus     )(struct nvkm_device *, int idx, struct nvkm_bus **);
    225 	int (*clk     )(struct nvkm_device *, int idx, struct nvkm_clk **);
    226 	int (*devinit )(struct nvkm_device *, int idx, struct nvkm_devinit **);
    227 	int (*fault   )(struct nvkm_device *, int idx, struct nvkm_fault **);
    228 	int (*fb      )(struct nvkm_device *, int idx, struct nvkm_fb **);
    229 	int (*fuse    )(struct nvkm_device *, int idx, struct nvkm_fuse **);
    230 	int (*gpio    )(struct nvkm_device *, int idx, struct nvkm_gpio **);
    231 	int (*gsp     )(struct nvkm_device *, int idx, struct nvkm_gsp **);
    232 	int (*i2c     )(struct nvkm_device *, int idx, struct nvkm_i2c **);
    233 	int (*ibus    )(struct nvkm_device *, int idx, struct nvkm_subdev **);
    234 	int (*iccsense)(struct nvkm_device *, int idx, struct nvkm_iccsense **);
    235 	int (*imem    )(struct nvkm_device *, int idx, struct nvkm_instmem **);
    236 	int (*ltc     )(struct nvkm_device *, int idx, struct nvkm_ltc **);
    237 	int (*mc      )(struct nvkm_device *, int idx, struct nvkm_mc **);
    238 	int (*mmu     )(struct nvkm_device *, int idx, struct nvkm_mmu **);
    239 	int (*mxm     )(struct nvkm_device *, int idx, struct nvkm_subdev **);
    240 	int (*pci     )(struct nvkm_device *, int idx, struct nvkm_pci **);
    241 	int (*pmu     )(struct nvkm_device *, int idx, struct nvkm_pmu **);
    242 	int (*therm   )(struct nvkm_device *, int idx, struct nvkm_therm **);
    243 	int (*timer   )(struct nvkm_device *, int idx, struct nvkm_timer **);
    244 	int (*top     )(struct nvkm_device *, int idx, struct nvkm_top **);
    245 	int (*volt    )(struct nvkm_device *, int idx, struct nvkm_volt **);
    246 
    247 	int (*bsp     )(struct nvkm_device *, int idx, struct nvkm_engine **);
    248 	int (*ce[9]   )(struct nvkm_device *, int idx, struct nvkm_engine **);
    249 	int (*cipher  )(struct nvkm_device *, int idx, struct nvkm_engine **);
    250 	int (*disp    )(struct nvkm_device *, int idx, struct nvkm_disp **);
    251 	int (*dma     )(struct nvkm_device *, int idx, struct nvkm_dma **);
    252 	int (*fifo    )(struct nvkm_device *, int idx, struct nvkm_fifo **);
    253 	int (*gr      )(struct nvkm_device *, int idx, struct nvkm_gr **);
    254 	int (*ifb     )(struct nvkm_device *, int idx, struct nvkm_engine **);
    255 	int (*me      )(struct nvkm_device *, int idx, struct nvkm_engine **);
    256 	int (*mpeg    )(struct nvkm_device *, int idx, struct nvkm_engine **);
    257 	int (*msenc   )(struct nvkm_device *, int idx, struct nvkm_engine **);
    258 	int (*mspdec  )(struct nvkm_device *, int idx, struct nvkm_engine **);
    259 	int (*msppp   )(struct nvkm_device *, int idx, struct nvkm_engine **);
    260 	int (*msvld   )(struct nvkm_device *, int idx, struct nvkm_engine **);
    261 	int (*nvenc[3])(struct nvkm_device *, int idx, struct nvkm_nvenc **);
    262 	int (*nvdec[3])(struct nvkm_device *, int idx, struct nvkm_nvdec **);
    263 	int (*pm      )(struct nvkm_device *, int idx, struct nvkm_pm **);
    264 	int (*sec     )(struct nvkm_device *, int idx, struct nvkm_engine **);
    265 	int (*sec2    )(struct nvkm_device *, int idx, struct nvkm_sec2 **);
    266 	int (*sw      )(struct nvkm_device *, int idx, struct nvkm_sw **);
    267 	int (*vic     )(struct nvkm_device *, int idx, struct nvkm_engine **);
    268 	int (*vp      )(struct nvkm_device *, int idx, struct nvkm_engine **);
    269 };
    270 
    271 struct nvkm_device *nvkm_device_find(u64 name);
    272 int nvkm_device_list(u64 *name, int size);
    273 
    274 #ifdef __NetBSD__
    275 void	nvkm_devices_init(void);
    276 void	nvkm_devices_fini(void);
    277 #endif
    278 
    279 /* privileged register interface accessor macros */
    280 #ifdef __NetBSD__
    281 static inline uint8_t
    282 nvkm_rd08(struct nvkm_device *d, bus_size_t a)
    283 {
    284 	return bus_space_read_1(d->mmiot, d->mmioh, a);
    285 }
    286 static inline uint16_t
    287 nvkm_rd16(struct nvkm_device *d, bus_size_t a)
    288 {
    289 	return bus_space_read_stream_2(d->mmiot, d->mmioh, a);
    290 }
    291 static inline uint32_t
    292 nvkm_rd32(struct nvkm_device *d, bus_size_t a)
    293 {
    294 	return bus_space_read_stream_4(d->mmiot, d->mmioh, a);
    295 }
    296 static inline void
    297 nvkm_wr08(struct nvkm_device *d, bus_size_t a, uint8_t v)
    298 {
    299 	bus_space_write_1(d->mmiot, d->mmioh, a, v);
    300 }
    301 static inline void
    302 nvkm_wr16(struct nvkm_device *d, bus_size_t a, uint16_t v)
    303 {
    304 	bus_space_write_stream_2(d->mmiot, d->mmioh, a, v);
    305 }
    306 static inline void
    307 nvkm_wr32(struct nvkm_device *d, bus_size_t a, uint32_t v)
    308 {
    309 	bus_space_write_stream_4(d->mmiot, d->mmioh, a, v);
    310 }
    311 #else
    312 #define nvkm_rd08(d,a) ioread8((d)->pri + (a))
    313 #define nvkm_rd16(d,a) ioread16_native((d)->pri + (a))
    314 #define nvkm_rd32(d,a) ioread32_native((d)->pri + (a))
    315 #define nvkm_wr08(d,a,v) iowrite8((v), (d)->pri + (a))
    316 #define nvkm_wr16(d,a,v) iowrite16_native((v), (d)->pri + (a))
    317 #define nvkm_wr32(d,a,v) iowrite32_native((v), (d)->pri + (a))
    318 #endif
    319 #define nvkm_mask(d,a,m,v) ({                                                  \
    320 	struct nvkm_device *_device = (d);                                     \
    321 	u32 _addr = (a), _temp = nvkm_rd32(_device, _addr);                    \
    322 	nvkm_wr32(_device, _addr, (_temp & ~(m)) | (v));                       \
    323 	_temp;                                                                 \
    324 })
    325 
    326 void nvkm_device_del(struct nvkm_device **);
    327 
    328 struct nvkm_device_oclass {
    329 	int (*ctor)(struct nvkm_device *, const struct nvkm_oclass *,
    330 		    void *data, u32 size, struct nvkm_object **);
    331 	struct nvkm_sclass base;
    332 };
    333 
    334 extern const struct nvkm_sclass nvkm_udevice_sclass;
    335 
    336 /* device logging */
    337 #define nvdev_printk_(d,l,p,f,a...) do {                                       \
    338 	const struct nvkm_device *_device = (d);                               \
    339 	if (_device->debug >= (l))                                             \
    340 		dev_##p(_device->dev, f, ##a);                                 \
    341 } while(0)
    342 #define nvdev_printk(d,l,p,f,a...) nvdev_printk_((d), NV_DBG_##l, p, f, ##a)
    343 #define nvdev_fatal(d,f,a...) nvdev_printk((d), FATAL,   crit, f, ##a)
    344 #define nvdev_error(d,f,a...) nvdev_printk((d), ERROR,    err, f, ##a)
    345 #define nvdev_warn(d,f,a...)  nvdev_printk((d),  WARN, notice, f, ##a)
    346 #define nvdev_info(d,f,a...)  nvdev_printk((d),  INFO,   info, f, ##a)
    347 #define nvdev_debug(d,f,a...) nvdev_printk((d), DEBUG,   info, f, ##a)
    348 #define nvdev_trace(d,f,a...) nvdev_printk((d), TRACE,   info, f, ##a)
    349 #define nvdev_spam(d,f,a...)  nvdev_printk((d),  SPAM,    dbg, f, ##a)
    350 #endif
    351