Home | History | Annotate | Line # | Download | only in nvif
      1  1.10  riastrad /*	$NetBSD: object.h,v 1.10 2021/12/19 10:51:56 riastradh Exp $	*/
      2   1.1  riastrad 
      3   1.9  riastrad /* SPDX-License-Identifier: MIT */
      4   1.1  riastrad #ifndef __NVIF_OBJECT_H__
      5   1.1  riastrad #define __NVIF_OBJECT_H__
      6   1.1  riastrad 
      7   1.1  riastrad #include <nvif/os.h>
      8   1.1  riastrad 
      9   1.1  riastrad struct nvif_sclass {
     10   1.1  riastrad 	s32 oclass;
     11   1.1  riastrad 	int minver;
     12   1.1  riastrad 	int maxver;
     13   1.1  riastrad };
     14   1.1  riastrad 
     15   1.3  riastrad #ifdef __NetBSD__
     16   1.3  riastrad #  define	__nvif_iomem	volatile
     17   1.3  riastrad #  define	__iomem		__nvif_iomem
     18   1.3  riastrad #endif
     19   1.3  riastrad 
     20   1.1  riastrad struct nvif_object {
     21   1.1  riastrad 	struct nvif_client *client;
     22   1.1  riastrad 	u32 handle;
     23   1.1  riastrad 	s32 oclass;
     24   1.1  riastrad 	void *priv; /*XXX: hack */
     25   1.1  riastrad 	struct {
     26   1.4  riastrad #ifdef __NetBSD__
     27   1.4  riastrad 		bus_space_tag_t tag;
     28   1.4  riastrad 		bus_space_handle_t handle;
     29   1.7  riastrad 		bus_addr_t addr;
     30   1.4  riastrad #endif
     31   1.1  riastrad 		void __iomem *ptr;
     32   1.9  riastrad 		u64 size;
     33   1.1  riastrad 	} map;
     34   1.1  riastrad };
     35   1.1  riastrad 
     36   1.5  riastrad #ifdef __NetBSD__
     37   1.5  riastrad #  undef	__iomem
     38   1.5  riastrad #endif
     39   1.5  riastrad 
     40   1.1  riastrad int  nvif_object_init(struct nvif_object *, u32 handle, s32 oclass, void *, u32,
     41   1.1  riastrad 		      struct nvif_object *);
     42   1.1  riastrad void nvif_object_fini(struct nvif_object *);
     43   1.1  riastrad int  nvif_object_ioctl(struct nvif_object *, void *, u32, void **);
     44   1.1  riastrad int  nvif_object_sclass_get(struct nvif_object *, struct nvif_sclass **);
     45   1.1  riastrad void nvif_object_sclass_put(struct nvif_sclass **);
     46   1.1  riastrad u32  nvif_object_rd(struct nvif_object *, int, u64);
     47   1.1  riastrad void nvif_object_wr(struct nvif_object *, int, u64, u32);
     48   1.1  riastrad int  nvif_object_mthd(struct nvif_object *, u32, void *, u32);
     49   1.9  riastrad int  nvif_object_map_handle(struct nvif_object *, void *, u32,
     50  1.10  riastrad #ifdef __NetBSD__
     51  1.10  riastrad 			    bus_space_tag_t *,
     52  1.10  riastrad #endif
     53   1.9  riastrad 			    u64 *handle, u64 *length);
     54   1.9  riastrad void nvif_object_unmap_handle(struct nvif_object *);
     55   1.9  riastrad int  nvif_object_map(struct nvif_object *, void *, u32);
     56   1.1  riastrad void nvif_object_unmap(struct nvif_object *);
     57   1.1  riastrad 
     58   1.1  riastrad #define nvif_handle(a) (unsigned long)(void *)(a)
     59   1.1  riastrad #define nvif_object(a) (a)->object
     60   1.1  riastrad 
     61   1.3  riastrad #ifdef __NetBSD__
     62   1.3  riastrad static inline uint8_t
     63   1.3  riastrad nvif_rd08(struct nvif_object *obj, uint64_t offset)
     64   1.3  riastrad {
     65   1.5  riastrad 	if (obj->map.ptr)
     66   1.5  riastrad 		return bus_space_read_1(obj->map.tag, obj->map.handle,
     67   1.5  riastrad 		    offset);
     68   1.5  riastrad 	else
     69   1.5  riastrad 		return nvif_object_rd(obj, 1, offset);
     70   1.3  riastrad }
     71   1.6  riastrad static inline uint16_t
     72   1.3  riastrad nvif_rd16(struct nvif_object *obj, uint64_t offset)
     73   1.3  riastrad {
     74   1.5  riastrad 	if (obj->map.ptr)
     75   1.5  riastrad 		return bus_space_read_stream_2(obj->map.tag, obj->map.handle,
     76   1.5  riastrad 		    offset);
     77   1.5  riastrad 	else
     78   1.5  riastrad 		return nvif_object_rd(obj, 2, offset);
     79   1.3  riastrad }
     80   1.6  riastrad static inline uint32_t
     81   1.3  riastrad nvif_rd32(struct nvif_object *obj, uint64_t offset)
     82   1.3  riastrad {
     83   1.5  riastrad 	if (obj->map.ptr)
     84   1.5  riastrad 		return bus_space_read_stream_4(obj->map.tag, obj->map.handle,
     85   1.5  riastrad 		    offset);
     86   1.5  riastrad 	else
     87   1.5  riastrad 		return nvif_object_rd(obj, 4, offset);
     88   1.3  riastrad }
     89   1.3  riastrad static inline void
     90   1.3  riastrad nvif_wr08(struct nvif_object *obj, uint64_t offset, uint8_t v)
     91   1.3  riastrad {
     92   1.5  riastrad 	if (obj->map.ptr)
     93   1.5  riastrad 		bus_space_write_1(obj->map.tag, obj->map.handle, offset, v);
     94   1.5  riastrad 	else
     95   1.5  riastrad 		nvif_object_wr(obj, 1, offset, v);
     96   1.3  riastrad }
     97   1.3  riastrad static inline void
     98   1.3  riastrad nvif_wr16(struct nvif_object *obj, uint64_t offset, uint16_t v)
     99   1.3  riastrad {
    100   1.5  riastrad 	if (obj->map.ptr)
    101   1.5  riastrad 		bus_space_write_stream_2(obj->map.tag, obj->map.handle, offset,
    102   1.5  riastrad 		    v);
    103   1.5  riastrad 	else
    104   1.5  riastrad 		nvif_object_wr(obj, 2, offset, v);
    105   1.3  riastrad }
    106   1.3  riastrad static inline void
    107   1.3  riastrad nvif_wr32(struct nvif_object *obj, uint64_t offset, uint32_t v)
    108   1.3  riastrad {
    109   1.5  riastrad 	if (obj->map.ptr)
    110   1.5  riastrad 		bus_space_write_stream_4(obj->map.tag, obj->map.handle, offset,
    111   1.5  riastrad 		    v);
    112   1.5  riastrad 	else
    113   1.5  riastrad 		nvif_object_wr(obj, 4, offset, v);
    114   1.3  riastrad }
    115   1.3  riastrad #else
    116   1.5  riastrad #define nvif_rd(a,f,b,c) ({                                                    \
    117   1.5  riastrad 	struct nvif_object *_object = (a);                                     \
    118   1.5  riastrad 	u32 _data;                                                             \
    119   1.5  riastrad 	if (likely(_object->map.ptr))                                          \
    120   1.5  riastrad 		_data = f((u8 __iomem *)_object->map.ptr + (c));               \
    121   1.5  riastrad 	else                                                                   \
    122   1.5  riastrad 		_data = nvif_object_rd(_object, (b), (c));                     \
    123   1.5  riastrad 	_data;                                                                 \
    124   1.5  riastrad })
    125   1.5  riastrad #define nvif_wr(a,f,b,c,d) ({                                                  \
    126   1.5  riastrad 	struct nvif_object *_object = (a);                                     \
    127   1.5  riastrad 	if (likely(_object->map.ptr))                                          \
    128   1.5  riastrad 		f((d), (u8 __iomem *)_object->map.ptr + (c));                  \
    129   1.5  riastrad 	else                                                                   \
    130   1.5  riastrad 		nvif_object_wr(_object, (b), (c), (d));                        \
    131   1.5  riastrad })
    132   1.1  riastrad #define nvif_rd08(a,b) ({ ((u8)nvif_rd((a), ioread8, 1, (b))); })
    133   1.1  riastrad #define nvif_rd16(a,b) ({ ((u16)nvif_rd((a), ioread16_native, 2, (b))); })
    134   1.1  riastrad #define nvif_rd32(a,b) ({ ((u32)nvif_rd((a), ioread32_native, 4, (b))); })
    135   1.1  riastrad #define nvif_wr08(a,b,c) nvif_wr((a), iowrite8, 1, (b), (u8)(c))
    136   1.1  riastrad #define nvif_wr16(a,b,c) nvif_wr((a), iowrite16_native, 2, (b), (u16)(c))
    137   1.1  riastrad #define nvif_wr32(a,b,c) nvif_wr((a), iowrite32_native, 4, (b), (u32)(c))
    138   1.3  riastrad #endif
    139   1.1  riastrad #define nvif_mask(a,b,c,d) ({                                                  \
    140   1.1  riastrad 	struct nvif_object *__object = (a);                                    \
    141   1.1  riastrad 	u32 _addr = (b), _data = nvif_rd32(__object, _addr);                   \
    142   1.1  riastrad 	nvif_wr32(__object, _addr, (_data & ~(c)) | (d));                      \
    143   1.1  riastrad 	_data;                                                                 \
    144   1.1  riastrad })
    145   1.1  riastrad 
    146   1.1  riastrad #define nvif_mthd(a,b,c,d) nvif_object_mthd((a), (b), (c), (d))
    147   1.1  riastrad 
    148   1.9  riastrad struct nvif_mclass {
    149   1.9  riastrad 	s32 oclass;
    150   1.9  riastrad 	int version;
    151   1.9  riastrad };
    152   1.9  riastrad 
    153   1.9  riastrad #define nvif_mclass(o,m) ({                                                    \
    154   1.9  riastrad 	struct nvif_object *object = (o);                                      \
    155   1.9  riastrad 	struct nvif_sclass *sclass;                                            \
    156   1.9  riastrad 	typeof(m[0]) *mclass = (m);                                            \
    157   1.9  riastrad 	int ret = -ENODEV;                                                     \
    158   1.9  riastrad 	int cnt, i, j;                                                         \
    159   1.9  riastrad                                                                                \
    160   1.9  riastrad 	cnt = nvif_object_sclass_get(object, &sclass);                         \
    161   1.9  riastrad 	if (cnt >= 0) {                                                        \
    162   1.9  riastrad 		for (i = 0; ret < 0 && mclass[i].oclass; i++) {                \
    163   1.9  riastrad 			for (j = 0; j < cnt; j++) {                            \
    164   1.9  riastrad 				if (mclass[i].oclass  == sclass[j].oclass &&   \
    165   1.9  riastrad 				    mclass[i].version >= sclass[j].minver &&   \
    166   1.9  riastrad 				    mclass[i].version <= sclass[j].maxver) {   \
    167   1.9  riastrad 					ret = i;                               \
    168   1.9  riastrad 					break;                                 \
    169   1.9  riastrad 				}                                              \
    170   1.9  riastrad 			}                                                      \
    171   1.9  riastrad 		}                                                              \
    172   1.9  riastrad 		nvif_object_sclass_put(&sclass);                               \
    173   1.9  riastrad 	}                                                                      \
    174   1.9  riastrad 	ret;                                                                   \
    175   1.9  riastrad })
    176   1.9  riastrad 
    177   1.9  riastrad #define nvif_sclass(o,m,u) ({                                                  \
    178   1.9  riastrad 	const typeof(m[0]) *_mclass = (m);                                     \
    179   1.9  riastrad 	s32 _oclass = (u);                                                     \
    180   1.9  riastrad 	int _cid;                                                              \
    181   1.9  riastrad 	if (_oclass) {                                                         \
    182   1.9  riastrad 		for (_cid = 0; _mclass[_cid].oclass; _cid++) {                 \
    183   1.9  riastrad 			if (_mclass[_cid].oclass == _oclass)                   \
    184   1.9  riastrad 				break;                                         \
    185   1.9  riastrad 		}                                                              \
    186   1.9  riastrad 		_cid = _mclass[_cid].oclass ? _cid : -ENOSYS;                  \
    187   1.9  riastrad 	} else {                                                               \
    188   1.9  riastrad 		_cid = nvif_mclass((o), _mclass);                              \
    189   1.9  riastrad 	}                                                                      \
    190   1.9  riastrad 	_cid;                                                                  \
    191   1.9  riastrad })
    192   1.9  riastrad 
    193   1.1  riastrad /*XXX*/
    194   1.1  riastrad #include <core/object.h>
    195   1.1  riastrad #define nvxx_object(a) ({                                                      \
    196   1.1  riastrad 	struct nvif_object *_object = (a);                                     \
    197   1.1  riastrad 	(struct nvkm_object *)_object->priv;                                   \
    198   1.1  riastrad })
    199   1.1  riastrad #endif
    200