1 /* SPDX-License-Identifier: MIT */
2 #ifndef __NVKM_DEVICE_H__
3 #define __NVKM_DEVICE_H__
4 #include <core/oclass.h>
5 #include <core/intr.h>
6 enum nvkm_subdev_type;
7 
8 enum nvkm_device_type {
9 	NVKM_DEVICE_PCI,
10 	NVKM_DEVICE_AGP,
11 	NVKM_DEVICE_PCIE,
12 	NVKM_DEVICE_TEGRA,
13 };
14 
15 struct nvkm_device {
16 	const struct nvkm_device_func *func;
17 	const struct nvkm_device_quirk *quirk;
18 	struct device *dev;
19 	enum nvkm_device_type type;
20 	u64 handle;
21 	const char *name;
22 	const char *cfgopt;
23 	const char *dbgopt;
24 
25 	struct list_head head;
26 	struct mutex mutex;
27 	int refcount;
28 
29 	void __iomem *pri;
30 
31 	u32 debug;
32 
33 	const struct nvkm_device_chip *chip;
34 	enum {
35 		NV_04    = 0x04,
36 		NV_10    = 0x10,
37 		NV_11    = 0x11,
38 		NV_20    = 0x20,
39 		NV_30    = 0x30,
40 		NV_40    = 0x40,
41 		NV_50    = 0x50,
42 		NV_C0    = 0xc0,
43 		NV_E0    = 0xe0,
44 		GM100    = 0x110,
45 		GP100    = 0x130,
46 		GV100    = 0x140,
47 		TU100    = 0x160,
48 		GA100    = 0x170,
49 		AD100    = 0x190,
50 	} card_type;
51 	u32 chipset;
52 	u8  chiprev;
53 	u32 crystal;
54 
55 	struct {
56 		struct notifier_block nb;
57 	} acpi;
58 
59 #define NVKM_LAYOUT_ONCE(type,data,ptr) data *ptr;
60 #define NVKM_LAYOUT_INST(type,data,ptr,cnt) data *ptr[cnt];
61 #include <core/layout.h>
62 #undef NVKM_LAYOUT_INST
63 #undef NVKM_LAYOUT_ONCE
64 	struct list_head subdev;
65 
66 	struct {
67 		struct list_head intr;
68 		struct list_head prio[NVKM_INTR_PRIO_NR];
69 		spinlock_t lock;
70 		int irq;
71 		bool alloc;
72 		bool armed;
73 		bool legacy_done;
74 	} intr;
75 };
76 
77 struct nvkm_subdev *nvkm_device_subdev(struct nvkm_device *, int type, int inst);
78 struct nvkm_engine *nvkm_device_engine(struct nvkm_device *, int type, int inst);
79 
80 struct nvkm_device_func {
81 	struct nvkm_device_pci *(*pci)(struct nvkm_device *);
82 	struct nvkm_device_tegra *(*tegra)(struct nvkm_device *);
83 	void *(*dtor)(struct nvkm_device *);
84 	int (*preinit)(struct nvkm_device *);
85 	int (*init)(struct nvkm_device *);
86 	void (*fini)(struct nvkm_device *, bool suspend);
87 	int (*irq)(struct nvkm_device *);
88 	resource_size_t (*resource_addr)(struct nvkm_device *, unsigned bar);
89 	resource_size_t (*resource_size)(struct nvkm_device *, unsigned bar);
90 	bool cpu_coherent;
91 };
92 
93 struct nvkm_device_quirk {
94 	u8 tv_pin_mask;
95 	u8 tv_gpio;
96 };
97 
98 struct nvkm_device_chip {
99 	const char *name;
100 #define NVKM_LAYOUT_ONCE(type,data,ptr,...)                                                  \
101 	struct {                                                                             \
102 		u32 inst;                                                                    \
103 		int (*ctor)(struct nvkm_device *, enum nvkm_subdev_type, int inst, data **); \
104 	} ptr;
105 #define NVKM_LAYOUT_INST(A...) NVKM_LAYOUT_ONCE(A)
106 #include <core/layout.h>
107 #undef NVKM_LAYOUT_INST
108 #undef NVKM_LAYOUT_ONCE
109 };
110 
111 struct nvkm_device *nvkm_device_find(u64 name);
112 int nvkm_device_list(u64 *name, int size);
113 
114 /* privileged register interface accessor macros */
115 #define nvkm_rd08(d,a) ioread8((d)->pri + (a))
116 #define nvkm_rd16(d,a) ioread16_native((d)->pri + (a))
117 #define nvkm_rd32(d,a) ioread32_native((d)->pri + (a))
118 #define nvkm_wr08(d,a,v) iowrite8((v), (d)->pri + (a))
119 #define nvkm_wr16(d,a,v) iowrite16_native((v), (d)->pri + (a))
120 #define nvkm_wr32(d,a,v) iowrite32_native((v), (d)->pri + (a))
121 #define nvkm_mask(d,a,m,v) ({                                                  \
122 	struct nvkm_device *_device = (d);                                     \
123 	u32 _addr = (a), _temp = nvkm_rd32(_device, _addr);                    \
124 	nvkm_wr32(_device, _addr, (_temp & ~(m)) | (v));                       \
125 	_temp;                                                                 \
126 })
127 
128 void nvkm_device_del(struct nvkm_device **);
129 
130 struct nvkm_device_oclass {
131 	int (*ctor)(struct nvkm_device *, const struct nvkm_oclass *,
132 		    void *data, u32 size, struct nvkm_object **);
133 	struct nvkm_sclass base;
134 };
135 
136 extern const struct nvkm_sclass nvkm_udevice_sclass;
137 
138 /* device logging */
139 #define nvdev_printk_(d,l,p,f,a...) do {                                       \
140 	const struct nvkm_device *_device = (d);                               \
141 	if (_device->debug >= (l))                                             \
142 		dev_##p(_device->dev, f, ##a);                                 \
143 } while(0)
144 #define nvdev_printk(d,l,p,f,a...) nvdev_printk_((d), NV_DBG_##l, p, f, ##a)
145 #define nvdev_fatal(d,f,a...) nvdev_printk((d), FATAL,   crit, f, ##a)
146 #define nvdev_error(d,f,a...) nvdev_printk((d), ERROR,    err, f, ##a)
147 #define nvdev_warn(d,f,a...)  nvdev_printk((d),  WARN, notice, f, ##a)
148 #define nvdev_info(d,f,a...)  nvdev_printk((d),  INFO,   info, f, ##a)
149 #define nvdev_debug(d,f,a...) nvdev_printk((d), DEBUG,   info, f, ##a)
150 #define nvdev_trace(d,f,a...) nvdev_printk((d), TRACE,   info, f, ##a)
151 #define nvdev_spam(d,f,a...)  nvdev_printk((d),  SPAM,    dbg, f, ##a)
152 #endif
153