xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/nouveau/include/nvif/object.h (revision e670fd5c413e99c2f6a37901bb21c537fcd322d2)
1 /*	$NetBSD: object.h,v 1.8 2018/08/27 14:48:21 riastradh Exp $	*/
2 
3 #ifndef __NVIF_OBJECT_H__
4 #define __NVIF_OBJECT_H__
5 
6 #include <nvif/os.h>
7 
8 struct nvif_sclass {
9 	s32 oclass;
10 	int minver;
11 	int maxver;
12 };
13 
14 #ifdef __NetBSD__
15 #  define	__nvif_iomem	volatile
16 #  define	__iomem		__nvif_iomem
17 #endif
18 
19 struct nvif_object {
20 	struct nvif_client *client;
21 	u32 handle;
22 	s32 oclass;
23 	void *priv; /*XXX: hack */
24 	struct {
25 #ifdef __NetBSD__
26 		bus_space_tag_t tag;
27 		bus_space_handle_t handle;
28 		bus_addr_t addr;
29 #endif
30 		void __iomem *ptr;
31 		u32 size;
32 	} map;
33 };
34 
35 #ifdef __NetBSD__
36 #  undef	__iomem
37 #endif
38 
39 int  nvif_object_init(struct nvif_object *, u32 handle, s32 oclass, void *, u32,
40 		      struct nvif_object *);
41 void nvif_object_fini(struct nvif_object *);
42 int  nvif_object_ioctl(struct nvif_object *, void *, u32, void **);
43 int  nvif_object_sclass_get(struct nvif_object *, struct nvif_sclass **);
44 void nvif_object_sclass_put(struct nvif_sclass **);
45 u32  nvif_object_rd(struct nvif_object *, int, u64);
46 void nvif_object_wr(struct nvif_object *, int, u64, u32);
47 int  nvif_object_mthd(struct nvif_object *, u32, void *, u32);
48 int  nvif_object_map(struct nvif_object *) __must_check;
49 void nvif_object_unmap(struct nvif_object *);
50 
51 #define nvif_handle(a) (unsigned long)(void *)(a)
52 #define nvif_object(a) (a)->object
53 
54 #ifdef __NetBSD__
55 static inline uint8_t
56 nvif_rd08(struct nvif_object *obj, uint64_t offset)
57 {
58 	if (obj->map.ptr)
59 		return bus_space_read_1(obj->map.tag, obj->map.handle,
60 		    offset);
61 	else
62 		return nvif_object_rd(obj, 1, offset);
63 }
64 static inline uint16_t
65 nvif_rd16(struct nvif_object *obj, uint64_t offset)
66 {
67 	if (obj->map.ptr)
68 		return bus_space_read_stream_2(obj->map.tag, obj->map.handle,
69 		    offset);
70 	else
71 		return nvif_object_rd(obj, 2, offset);
72 }
73 static inline uint32_t
74 nvif_rd32(struct nvif_object *obj, uint64_t offset)
75 {
76 	if (obj->map.ptr)
77 		return bus_space_read_stream_4(obj->map.tag, obj->map.handle,
78 		    offset);
79 	else
80 		return nvif_object_rd(obj, 4, offset);
81 }
82 static inline void
83 nvif_wr08(struct nvif_object *obj, uint64_t offset, uint8_t v)
84 {
85 	if (obj->map.ptr)
86 		bus_space_write_1(obj->map.tag, obj->map.handle, offset, v);
87 	else
88 		nvif_object_wr(obj, 1, offset, v);
89 }
90 static inline void
91 nvif_wr16(struct nvif_object *obj, uint64_t offset, uint16_t v)
92 {
93 	if (obj->map.ptr)
94 		bus_space_write_stream_2(obj->map.tag, obj->map.handle, offset,
95 		    v);
96 	else
97 		nvif_object_wr(obj, 2, offset, v);
98 }
99 static inline void
100 nvif_wr32(struct nvif_object *obj, uint64_t offset, uint32_t v)
101 {
102 	if (obj->map.ptr)
103 		bus_space_write_stream_4(obj->map.tag, obj->map.handle, offset,
104 		    v);
105 	else
106 		nvif_object_wr(obj, 4, offset, v);
107 }
108 #else
109 #define nvif_rd(a,f,b,c) ({                                                    \
110 	struct nvif_object *_object = (a);                                     \
111 	u32 _data;                                                             \
112 	if (likely(_object->map.ptr))                                          \
113 		_data = f((u8 __iomem *)_object->map.ptr + (c));               \
114 	else                                                                   \
115 		_data = nvif_object_rd(_object, (b), (c));                     \
116 	_data;                                                                 \
117 })
118 #define nvif_wr(a,f,b,c,d) ({                                                  \
119 	struct nvif_object *_object = (a);                                     \
120 	if (likely(_object->map.ptr))                                          \
121 		f((d), (u8 __iomem *)_object->map.ptr + (c));                  \
122 	else                                                                   \
123 		nvif_object_wr(_object, (b), (c), (d));                        \
124 })
125 #define nvif_rd08(a,b) ({ ((u8)nvif_rd((a), ioread8, 1, (b))); })
126 #define nvif_rd16(a,b) ({ ((u16)nvif_rd((a), ioread16_native, 2, (b))); })
127 #define nvif_rd32(a,b) ({ ((u32)nvif_rd((a), ioread32_native, 4, (b))); })
128 #define nvif_wr08(a,b,c) nvif_wr((a), iowrite8, 1, (b), (u8)(c))
129 #define nvif_wr16(a,b,c) nvif_wr((a), iowrite16_native, 2, (b), (u16)(c))
130 #define nvif_wr32(a,b,c) nvif_wr((a), iowrite32_native, 4, (b), (u32)(c))
131 #endif
132 #define nvif_mask(a,b,c,d) ({                                                  \
133 	struct nvif_object *__object = (a);                                    \
134 	u32 _addr = (b), _data = nvif_rd32(__object, _addr);                   \
135 	nvif_wr32(__object, _addr, (_data & ~(c)) | (d));                      \
136 	_data;                                                                 \
137 })
138 
139 #define nvif_mthd(a,b,c,d) nvif_object_mthd((a), (b), (c), (d))
140 
141 /*XXX*/
142 #include <core/object.h>
143 #define nvxx_object(a) ({                                                      \
144 	struct nvif_object *_object = (a);                                     \
145 	(struct nvkm_object *)_object->priv;                                   \
146 })
147 #endif
148