1#ifndef __NOUVEAU_OBJECT_H__
2#define __NOUVEAU_OBJECT_H__
3
4#include <core/os.h>
5#include <core/printk.h>
6
7#define NV_PARENT_CLASS 0x80000000
8#define NV_NAMEDB_CLASS 0x40000000
9#define NV_CLIENT_CLASS 0x20000000
10#define NV_SUBDEV_CLASS 0x10000000
11#define NV_ENGINE_CLASS 0x08000000
12#define NV_MEMOBJ_CLASS 0x04000000
13#define NV_GPUOBJ_CLASS 0x02000000
14#define NV_ENGCTX_CLASS 0x01000000
15#define NV_OBJECT_CLASS 0x0000ffff
16
17struct nouveau_object {
18 struct nouveau_oclass *oclass;
19 struct nouveau_object *parent;
20 struct nouveau_object *engine;
21 atomic_t refcount;
22 atomic_t usecount;
23#if CONFIG_NOUVEAU_DEBUG >= NV_DBG_PARANOIA
24#define NOUVEAU_OBJECT_MAGIC 0x75ef0bad
25 struct list_head list;
26 u32 _magic;
27#endif
28};
29
30static inline struct nouveau_object *
31nv_object(void *obj)
32{
33#if CONFIG_NOUVEAU_DEBUG >= NV_DBG_PARANOIA
34 if (likely(obj)) {
35 struct nouveau_object *object = obj;
36 if (unlikely(object->_magic != NOUVEAU_OBJECT_MAGIC))
37 nv_assert("BAD CAST -> NvObject, invalid magic");
38 }
39#endif
40 return obj;
41}
42
43#ifdef __NetBSD__
44void nouveau_objects_init(void);
45void nouveau_objects_fini(void);
46#endif
47
48#define nouveau_object_create(p,e,c,s,d) \
49 nouveau_object_create_((p), (e), (c), (s), sizeof(**d), (void **)d)
50int nouveau_object_create_(struct nouveau_object *, struct nouveau_object *,
51 struct nouveau_oclass *, u32, int size, void **);
52void nouveau_object_destroy(struct nouveau_object *);
53int nouveau_object_init(struct nouveau_object *);
54int nouveau_object_fini(struct nouveau_object *, bool suspend);
55
56extern struct nouveau_ofuncs nouveau_object_ofuncs;
57
58/* Don't allocate dynamically, because lockdep needs lock_class_keys to be in
59 * ".data". */
60struct nouveau_oclass {
61 u32 handle;
62 struct nouveau_ofuncs * const ofuncs;
63 struct nouveau_omthds * const omthds;
64 struct lock_class_key lock_class_key;
65};
66
67#define nv_oclass(o) nv_object(o)->oclass
68#define nv_hclass(o) nv_oclass(o)->handle
69#define nv_iclass(o,i) (nv_hclass(o) & (i))
70#define nv_mclass(o) nv_iclass(o, NV_OBJECT_CLASS)
71
72static inline struct nouveau_object *
73nv_pclass(struct nouveau_object *parent, u32 oclass)
74{
75 while (parent && !nv_iclass(parent, oclass))
76 parent = parent->parent;
77 return parent;
78}
79
80struct nouveau_omthds {
81 u32 start;
82 u32 limit;
83 int (*call)(struct nouveau_object *, u32, void *, u32);
84};
85
86struct nouveau_ofuncs {
87 int (*ctor)(struct nouveau_object *, struct nouveau_object *,
88 struct nouveau_oclass *, void *data, u32 size,
89 struct nouveau_object **);
90 void (*dtor)(struct nouveau_object *);
91 int (*init)(struct nouveau_object *);
92 int (*fini)(struct nouveau_object *, bool suspend);
93 u8 (*rd08)(struct nouveau_object *, u64 offset);
94 u16 (*rd16)(struct nouveau_object *, u64 offset);
95 u32 (*rd32)(struct nouveau_object *, u64 offset);
96 void (*wr08)(struct nouveau_object *, u64 offset, u8 data);
97 void (*wr16)(struct nouveau_object *, u64 offset, u16 data);
98 void (*wr32)(struct nouveau_object *, u64 offset, u32 data);
99};
100
101static inline struct nouveau_ofuncs *
102nv_ofuncs(void *obj)
103{
104 return nv_oclass(obj)->ofuncs;
105}
106
107int nouveau_object_ctor(struct nouveau_object *, struct nouveau_object *,
108 struct nouveau_oclass *, void *, u32,
109 struct nouveau_object **);
110void nouveau_object_ref(struct nouveau_object *, struct nouveau_object **);
111int nouveau_object_inc(struct nouveau_object *);
112int nouveau_object_dec(struct nouveau_object *, bool suspend);
113
114int nouveau_object_new(struct nouveau_object *, u32 parent, u32 handle,
115 u16 oclass, void *data, u32 size,
116 struct nouveau_object **);
117int nouveau_object_del(struct nouveau_object *, u32 parent, u32 handle);
118void nouveau_object_debug(void);
119
120static inline int
121nv_exec(void *obj, u32 mthd, void *data, u32 size)
122{
123 struct nouveau_omthds *method = nv_oclass(obj)->omthds;
124
125 while (method && method->call) {
126 if (mthd >= method->start && mthd <= method->limit)
127 return method->call(obj, mthd, data, size);
128 method++;
129 }
130
131 return -EINVAL;
132}
133
134static inline int
135nv_call(void *obj, u32 mthd, u32 data)
136{
137 return nv_exec(obj, mthd, &data, sizeof(data));
138}
139
140static inline u8
141nv_ro08(void *obj, u64 addr)
142{
143 u8 data = nv_ofuncs(obj)->rd08(obj, addr);
144 nv_spam(obj, "nv_ro08 0x%08"PRIx64" 0x%02x\n", addr, data);
145 return data;
146}
147
148static inline u16
149nv_ro16(void *obj, u64 addr)
150{
151 u16 data = nv_ofuncs(obj)->rd16(obj, addr);
152 nv_spam(obj, "nv_ro16 0x%08"PRIx64" 0x%04x\n", addr, data);
153 return data;
154}
155
156static inline u32
157nv_ro32(void *obj, u64 addr)
158{
159 u32 data = nv_ofuncs(obj)->rd32(obj, addr);
160 nv_spam(obj, "nv_ro32 0x%08"PRIx64" 0x%08x\n", addr, data);
161 return data;
162}
163
164static inline void
165nv_wo08(void *obj, u64 addr, u8 data)
166{
167 nv_spam(obj, "nv_wo08 0x%08"PRIx64" 0x%02x\n", addr, data);
168 nv_ofuncs(obj)->wr08(obj, addr, data);
169}
170
171static inline void
172nv_wo16(void *obj, u64 addr, u16 data)
173{
174 nv_spam(obj, "nv_wo16 0x%08"PRIx64" 0x%04x\n", addr, data);
175 nv_ofuncs(obj)->wr16(obj, addr, data);
176}
177
178static inline void
179nv_wo32(void *obj, u64 addr, u32 data)
180{
181 nv_spam(obj, "nv_wo32 0x%08"PRIx64" 0x%08x\n", addr, data);
182 nv_ofuncs(obj)->wr32(obj, addr, data);
183}
184
185static inline u32
186nv_mo32(void *obj, u64 addr, u32 mask, u32 data)
187{
188 u32 temp = nv_ro32(obj, addr);
189 nv_wo32(obj, addr, (temp & ~mask) | data);
190 return temp;
191}
192
193static inline int
194nv_memcmp(void *obj, u32 addr, const char *str, u32 len)
195{
196 unsigned char c1, c2;
197
198 while (len--) {
199 c1 = nv_ro08(obj, addr++);
200 c2 = *(str++);
201 if (c1 != c2)
202 return c1 - c2;
203 }
204 return 0;
205}
206
207#endif
208