18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: MIT */
28c2ecf20Sopenharmony_ci#ifndef __NVIF_OBJECT_H__
38c2ecf20Sopenharmony_ci#define __NVIF_OBJECT_H__
48c2ecf20Sopenharmony_ci#include <nvif/os.h>
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_cistruct nvif_sclass {
78c2ecf20Sopenharmony_ci	s32 oclass;
88c2ecf20Sopenharmony_ci	int minver;
98c2ecf20Sopenharmony_ci	int maxver;
108c2ecf20Sopenharmony_ci};
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cistruct nvif_object {
138c2ecf20Sopenharmony_ci	struct nvif_parent *parent;
148c2ecf20Sopenharmony_ci	struct nvif_client *client;
158c2ecf20Sopenharmony_ci	const char *name;
168c2ecf20Sopenharmony_ci	u32 handle;
178c2ecf20Sopenharmony_ci	s32 oclass;
188c2ecf20Sopenharmony_ci	void *priv; /*XXX: hack */
198c2ecf20Sopenharmony_ci	struct {
208c2ecf20Sopenharmony_ci		void __iomem *ptr;
218c2ecf20Sopenharmony_ci		u64 size;
228c2ecf20Sopenharmony_ci	} map;
238c2ecf20Sopenharmony_ci};
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ciint  nvif_object_ctor(struct nvif_object *, const char *name, u32 handle,
268c2ecf20Sopenharmony_ci		      s32 oclass, void *, u32, struct nvif_object *);
278c2ecf20Sopenharmony_civoid nvif_object_dtor(struct nvif_object *);
288c2ecf20Sopenharmony_ciint  nvif_object_ioctl(struct nvif_object *, void *, u32, void **);
298c2ecf20Sopenharmony_ciint  nvif_object_sclass_get(struct nvif_object *, struct nvif_sclass **);
308c2ecf20Sopenharmony_civoid nvif_object_sclass_put(struct nvif_sclass **);
318c2ecf20Sopenharmony_ciu32  nvif_object_rd(struct nvif_object *, int, u64);
328c2ecf20Sopenharmony_civoid nvif_object_wr(struct nvif_object *, int, u64, u32);
338c2ecf20Sopenharmony_ciint  nvif_object_mthd(struct nvif_object *, u32, void *, u32);
348c2ecf20Sopenharmony_ciint  nvif_object_map_handle(struct nvif_object *, void *, u32,
358c2ecf20Sopenharmony_ci			    u64 *handle, u64 *length);
368c2ecf20Sopenharmony_civoid nvif_object_unmap_handle(struct nvif_object *);
378c2ecf20Sopenharmony_ciint  nvif_object_map(struct nvif_object *, void *, u32);
388c2ecf20Sopenharmony_civoid nvif_object_unmap(struct nvif_object *);
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#define nvif_handle(a) (unsigned long)(void *)(a)
418c2ecf20Sopenharmony_ci#define nvif_object(a) (a)->object
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#define nvif_rd(a,f,b,c) ({                                                    \
448c2ecf20Sopenharmony_ci	struct nvif_object *_object = (a);                                     \
458c2ecf20Sopenharmony_ci	u32 _data;                                                             \
468c2ecf20Sopenharmony_ci	if (likely(_object->map.ptr))                                          \
478c2ecf20Sopenharmony_ci		_data = f((u8 __iomem *)_object->map.ptr + (c));               \
488c2ecf20Sopenharmony_ci	else                                                                   \
498c2ecf20Sopenharmony_ci		_data = nvif_object_rd(_object, (b), (c));                     \
508c2ecf20Sopenharmony_ci	_data;                                                                 \
518c2ecf20Sopenharmony_ci})
528c2ecf20Sopenharmony_ci#define nvif_wr(a,f,b,c,d) ({                                                  \
538c2ecf20Sopenharmony_ci	struct nvif_object *_object = (a);                                     \
548c2ecf20Sopenharmony_ci	if (likely(_object->map.ptr))                                          \
558c2ecf20Sopenharmony_ci		f((d), (u8 __iomem *)_object->map.ptr + (c));                  \
568c2ecf20Sopenharmony_ci	else                                                                   \
578c2ecf20Sopenharmony_ci		nvif_object_wr(_object, (b), (c), (d));                        \
588c2ecf20Sopenharmony_ci})
598c2ecf20Sopenharmony_ci#define nvif_rd08(a,b) ({ ((u8)nvif_rd((a), ioread8, 1, (b))); })
608c2ecf20Sopenharmony_ci#define nvif_rd16(a,b) ({ ((u16)nvif_rd((a), ioread16_native, 2, (b))); })
618c2ecf20Sopenharmony_ci#define nvif_rd32(a,b) ({ ((u32)nvif_rd((a), ioread32_native, 4, (b))); })
628c2ecf20Sopenharmony_ci#define nvif_wr08(a,b,c) nvif_wr((a), iowrite8, 1, (b), (u8)(c))
638c2ecf20Sopenharmony_ci#define nvif_wr16(a,b,c) nvif_wr((a), iowrite16_native, 2, (b), (u16)(c))
648c2ecf20Sopenharmony_ci#define nvif_wr32(a,b,c) nvif_wr((a), iowrite32_native, 4, (b), (u32)(c))
658c2ecf20Sopenharmony_ci#define nvif_mask(a,b,c,d) ({                                                  \
668c2ecf20Sopenharmony_ci	struct nvif_object *__object = (a);                                    \
678c2ecf20Sopenharmony_ci	u32 _addr = (b), _data = nvif_rd32(__object, _addr);                   \
688c2ecf20Sopenharmony_ci	nvif_wr32(__object, _addr, (_data & ~(c)) | (d));                      \
698c2ecf20Sopenharmony_ci	_data;                                                                 \
708c2ecf20Sopenharmony_ci})
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci#define nvif_mthd(a,b,c,d) nvif_object_mthd((a), (b), (c), (d))
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistruct nvif_mclass {
758c2ecf20Sopenharmony_ci	s32 oclass;
768c2ecf20Sopenharmony_ci	int version;
778c2ecf20Sopenharmony_ci};
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci#define nvif_mclass(o,m) ({                                                    \
808c2ecf20Sopenharmony_ci	struct nvif_object *object = (o);                                      \
818c2ecf20Sopenharmony_ci	struct nvif_sclass *sclass;                                            \
828c2ecf20Sopenharmony_ci	typeof(m[0]) *mclass = (m);                                            \
838c2ecf20Sopenharmony_ci	int ret = -ENODEV;                                                     \
848c2ecf20Sopenharmony_ci	int cnt, i, j;                                                         \
858c2ecf20Sopenharmony_ci                                                                               \
868c2ecf20Sopenharmony_ci	cnt = nvif_object_sclass_get(object, &sclass);                         \
878c2ecf20Sopenharmony_ci	if (cnt >= 0) {                                                        \
888c2ecf20Sopenharmony_ci		for (i = 0; ret < 0 && mclass[i].oclass; i++) {                \
898c2ecf20Sopenharmony_ci			for (j = 0; j < cnt; j++) {                            \
908c2ecf20Sopenharmony_ci				if (mclass[i].oclass  == sclass[j].oclass &&   \
918c2ecf20Sopenharmony_ci				    mclass[i].version >= sclass[j].minver &&   \
928c2ecf20Sopenharmony_ci				    mclass[i].version <= sclass[j].maxver) {   \
938c2ecf20Sopenharmony_ci					ret = i;                               \
948c2ecf20Sopenharmony_ci					break;                                 \
958c2ecf20Sopenharmony_ci				}                                              \
968c2ecf20Sopenharmony_ci			}                                                      \
978c2ecf20Sopenharmony_ci		}                                                              \
988c2ecf20Sopenharmony_ci		nvif_object_sclass_put(&sclass);                               \
998c2ecf20Sopenharmony_ci	}                                                                      \
1008c2ecf20Sopenharmony_ci	ret;                                                                   \
1018c2ecf20Sopenharmony_ci})
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci#define nvif_sclass(o,m,u) ({                                                  \
1048c2ecf20Sopenharmony_ci	const typeof(m[0]) *_mclass = (m);                                     \
1058c2ecf20Sopenharmony_ci	s32 _oclass = (u);                                                     \
1068c2ecf20Sopenharmony_ci	int _cid;                                                              \
1078c2ecf20Sopenharmony_ci	if (_oclass) {                                                         \
1088c2ecf20Sopenharmony_ci		for (_cid = 0; _mclass[_cid].oclass; _cid++) {                 \
1098c2ecf20Sopenharmony_ci			if (_mclass[_cid].oclass == _oclass)                   \
1108c2ecf20Sopenharmony_ci				break;                                         \
1118c2ecf20Sopenharmony_ci		}                                                              \
1128c2ecf20Sopenharmony_ci		_cid = _mclass[_cid].oclass ? _cid : -ENOSYS;                  \
1138c2ecf20Sopenharmony_ci	} else {                                                               \
1148c2ecf20Sopenharmony_ci		_cid = nvif_mclass((o), _mclass);                              \
1158c2ecf20Sopenharmony_ci	}                                                                      \
1168c2ecf20Sopenharmony_ci	_cid;                                                                  \
1178c2ecf20Sopenharmony_ci})
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci#define NVIF_RD32_(p,o,dr)   nvif_rd32((p), (o) + (dr))
1208c2ecf20Sopenharmony_ci#define NVIF_WR32_(p,o,dr,f) nvif_wr32((p), (o) + (dr), (f))
1218c2ecf20Sopenharmony_ci#define NVIF_RD32(p,A...) DRF_RD(NVIF_RD32_,                  (p), 0, ##A)
1228c2ecf20Sopenharmony_ci#define NVIF_RV32(p,A...) DRF_RV(NVIF_RD32_,                  (p), 0, ##A)
1238c2ecf20Sopenharmony_ci#define NVIF_TV32(p,A...) DRF_TV(NVIF_RD32_,                  (p), 0, ##A)
1248c2ecf20Sopenharmony_ci#define NVIF_TD32(p,A...) DRF_TD(NVIF_RD32_,                  (p), 0, ##A)
1258c2ecf20Sopenharmony_ci#define NVIF_WR32(p,A...) DRF_WR(            NVIF_WR32_,      (p), 0, ##A)
1268c2ecf20Sopenharmony_ci#define NVIF_WV32(p,A...) DRF_WV(            NVIF_WR32_,      (p), 0, ##A)
1278c2ecf20Sopenharmony_ci#define NVIF_WD32(p,A...) DRF_WD(            NVIF_WR32_,      (p), 0, ##A)
1288c2ecf20Sopenharmony_ci#define NVIF_MR32(p,A...) DRF_MR(NVIF_RD32_, NVIF_WR32_, u32, (p), 0, ##A)
1298c2ecf20Sopenharmony_ci#define NVIF_MV32(p,A...) DRF_MV(NVIF_RD32_, NVIF_WR32_, u32, (p), 0, ##A)
1308c2ecf20Sopenharmony_ci#define NVIF_MD32(p,A...) DRF_MD(NVIF_RD32_, NVIF_WR32_, u32, (p), 0, ##A)
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci/*XXX*/
1338c2ecf20Sopenharmony_ci#include <core/object.h>
1348c2ecf20Sopenharmony_ci#define nvxx_object(a) ({                                                      \
1358c2ecf20Sopenharmony_ci	struct nvif_object *_object = (a);                                     \
1368c2ecf20Sopenharmony_ci	(struct nvkm_object *)_object->priv;                                   \
1378c2ecf20Sopenharmony_ci})
1388c2ecf20Sopenharmony_ci#endif
139