1 /* SPDX-License-Identifier: MIT */
2 #ifndef __NVIF_OBJECT_H__
3 #define __NVIF_OBJECT_H__
4 #include <nvif/os.h>
5 
6 struct nvif_sclass {
7 	s32 oclass;
8 	int minver;
9 	int maxver;
10 };
11 
12 struct nvif_object {
13 	struct nvif_parent *parent;
14 	struct nvif_client *client;
15 	const char *name;
16 	u32 handle;
17 	s32 oclass;
18 	void *priv; /*XXX: hack */
19 	struct {
20 		void __iomem *ptr;
21 		u64 size;
22 	} map;
23 };
24 
25 static inline bool
26 nvif_object_constructed(struct nvif_object *object)
27 {
28 	return object->client != NULL;
29 }
30 
31 int  nvif_object_ctor(struct nvif_object *, const char *name, u32 handle,
32 		      s32 oclass, void *, u32, struct nvif_object *);
33 void nvif_object_dtor(struct nvif_object *);
34 int  nvif_object_ioctl(struct nvif_object *, void *, u32, void **);
35 int  nvif_object_sclass_get(struct nvif_object *, struct nvif_sclass **);
36 void nvif_object_sclass_put(struct nvif_sclass **);
37 u32  nvif_object_rd(struct nvif_object *, int, u64);
38 void nvif_object_wr(struct nvif_object *, int, u64, u32);
39 int  nvif_object_mthd(struct nvif_object *, u32, void *, u32);
40 int  nvif_object_map_handle(struct nvif_object *, void *, u32,
41 			    u64 *handle, u64 *length);
42 void nvif_object_unmap_handle(struct nvif_object *);
43 int  nvif_object_map(struct nvif_object *, void *, u32);
44 void nvif_object_unmap(struct nvif_object *);
45 
46 #define nvif_handle(a) (unsigned long)(void *)(a)
47 #define nvif_object(a) (a)->object
48 
49 #define nvif_rd(a,f,b,c) ({                                                    \
50 	struct nvif_object *_object = (a);                                     \
51 	u32 _data;                                                             \
52 	if (likely(_object->map.ptr))                                          \
53 		_data = f((u8 __iomem *)_object->map.ptr + (c));               \
54 	else                                                                   \
55 		_data = nvif_object_rd(_object, (b), (c));                     \
56 	_data;                                                                 \
57 })
58 #define nvif_wr(a,f,b,c,d) ({                                                  \
59 	struct nvif_object *_object = (a);                                     \
60 	if (likely(_object->map.ptr))                                          \
61 		f((d), (u8 __iomem *)_object->map.ptr + (c));                  \
62 	else                                                                   \
63 		nvif_object_wr(_object, (b), (c), (d));                        \
64 })
65 #define nvif_rd08(a,b) ({ ((u8)nvif_rd((a), ioread8, 1, (b))); })
66 #define nvif_rd16(a,b) ({ ((u16)nvif_rd((a), ioread16_native, 2, (b))); })
67 #define nvif_rd32(a,b) ({ ((u32)nvif_rd((a), ioread32_native, 4, (b))); })
68 #define nvif_wr08(a,b,c) nvif_wr((a), iowrite8, 1, (b), (u8)(c))
69 #define nvif_wr16(a,b,c) nvif_wr((a), iowrite16_native, 2, (b), (u16)(c))
70 #define nvif_wr32(a,b,c) nvif_wr((a), iowrite32_native, 4, (b), (u32)(c))
71 #define nvif_mask(a,b,c,d) ({                                                  \
72 	struct nvif_object *__object = (a);                                    \
73 	u32 _addr = (b), _data = nvif_rd32(__object, _addr);                   \
74 	nvif_wr32(__object, _addr, (_data & ~(c)) | (d));                      \
75 	_data;                                                                 \
76 })
77 
78 #define nvif_mthd(a,b,c,d) nvif_object_mthd((a), (b), (c), (d))
79 
80 struct nvif_mclass {
81 	s32 oclass;
82 	int version;
83 };
84 
85 #define nvif_mclass(o,m) ({                                                    \
86 	struct nvif_object *object = (o);                                      \
87 	struct nvif_sclass *sclass;                                            \
88 	typeof(m[0]) *mclass = (m);                                            \
89 	int ret = -ENODEV;                                                     \
90 	int cnt, i, j;                                                         \
91                                                                                \
92 	cnt = nvif_object_sclass_get(object, &sclass);                         \
93 	if (cnt >= 0) {                                                        \
94 		for (i = 0; ret < 0 && mclass[i].oclass; i++) {                \
95 			for (j = 0; j < cnt; j++) {                            \
96 				if (mclass[i].oclass  == sclass[j].oclass &&   \
97 				    mclass[i].version >= sclass[j].minver &&   \
98 				    mclass[i].version <= sclass[j].maxver) {   \
99 					ret = i;                               \
100 					break;                                 \
101 				}                                              \
102 			}                                                      \
103 		}                                                              \
104 		nvif_object_sclass_put(&sclass);                               \
105 	}                                                                      \
106 	ret;                                                                   \
107 })
108 
109 #define nvif_sclass(o,m,u) ({                                                  \
110 	const typeof(m[0]) *_mclass = (m);                                     \
111 	s32 _oclass = (u);                                                     \
112 	int _cid;                                                              \
113 	if (_oclass) {                                                         \
114 		for (_cid = 0; _mclass[_cid].oclass; _cid++) {                 \
115 			if (_mclass[_cid].oclass == _oclass)                   \
116 				break;                                         \
117 		}                                                              \
118 		_cid = _mclass[_cid].oclass ? _cid : -ENOSYS;                  \
119 	} else {                                                               \
120 		_cid = nvif_mclass((o), _mclass);                              \
121 	}                                                                      \
122 	_cid;                                                                  \
123 })
124 
125 #define NVIF_RD32_(p,o,dr)   nvif_rd32((p), (o) + (dr))
126 #define NVIF_WR32_(p,o,dr,f) nvif_wr32((p), (o) + (dr), (f))
127 #define NVIF_RD32(p,A...) DRF_RD(NVIF_RD32_,                  (p), 0, ##A)
128 #define NVIF_RV32(p,A...) DRF_RV(NVIF_RD32_,                  (p), 0, ##A)
129 #define NVIF_TV32(p,A...) DRF_TV(NVIF_RD32_,                  (p), 0, ##A)
130 #define NVIF_TD32(p,A...) DRF_TD(NVIF_RD32_,                  (p), 0, ##A)
131 #define NVIF_WR32(p,A...) DRF_WR(            NVIF_WR32_,      (p), 0, ##A)
132 #define NVIF_WV32(p,A...) DRF_WV(            NVIF_WR32_,      (p), 0, ##A)
133 #define NVIF_WD32(p,A...) DRF_WD(            NVIF_WR32_,      (p), 0, ##A)
134 #define NVIF_MR32(p,A...) DRF_MR(NVIF_RD32_, NVIF_WR32_, u32, (p), 0, ##A)
135 #define NVIF_MV32(p,A...) DRF_MV(NVIF_RD32_, NVIF_WR32_, u32, (p), 0, ##A)
136 #define NVIF_MD32(p,A...) DRF_MD(NVIF_RD32_, NVIF_WR32_, u32, (p), 0, ##A)
137 
138 /*XXX*/
139 #include <core/object.h>
140 #define nvxx_object(a) ({                                                      \
141 	struct nvif_object *_object = (a);                                     \
142 	(struct nvkm_object *)_object->priv;                                   \
143 })
144 #endif
145