1 #ifndef __NOUVEAU_SUBDEV_H__
2 #define __NOUVEAU_SUBDEV_H__
3 
4 #include <core/object.h>
5 
6 #define NV_SUBDEV_(sub,var) (NV_SUBDEV_CLASS | ((var) << 8) | (sub))
7 #define NV_SUBDEV(name,var)  NV_SUBDEV_(NVDEV_SUBDEV_##name, (var))
8 
9 struct nouveau_subdev {
10 	struct nouveau_object base;
11 	struct mutex mutex;
12 	const char *name;
13 #ifdef __NetBSD__
14 	bus_space_tag_t mmiot;
15 	bus_space_handle_t mmioh;
16 	bus_size_t mmiosz;
17 #else
18 	void __iomem *mmio;
19 #endif
20 	u32 debug;
21 	u32 unit;
22 
23 	void (*intr)(struct nouveau_subdev *);
24 };
25 
26 static inline struct nouveau_subdev *
nv_subdev(void * obj)27 nv_subdev(void *obj)
28 {
29 #if CONFIG_NOUVEAU_DEBUG >= NV_DBG_PARANOIA
30 	if (unlikely(!nv_iclass(obj, NV_SUBDEV_CLASS)))
31 		nv_assert("BAD CAST -> NvSubDev, %08x", nv_hclass(obj));
32 #endif
33 	return obj;
34 }
35 
36 static inline int
nv_subidx(struct nouveau_object * object)37 nv_subidx(struct nouveau_object *object)
38 {
39 	return nv_hclass(nv_subdev(object)) & 0xff;
40 }
41 
42 #define nouveau_subdev_create(p,e,o,v,s,f,d)                                   \
43 	nouveau_subdev_create_((p), (e), (o), (v), (s), (f),                   \
44 			       sizeof(**d),(void **)d)
45 
46 int  nouveau_subdev_create_(struct nouveau_object *, struct nouveau_object *,
47 			    struct nouveau_oclass *, u32 pclass,
48 			    const char *sname, const char *fname,
49 			    int size, void **);
50 void nouveau_subdev_destroy(struct nouveau_subdev *);
51 int  nouveau_subdev_init(struct nouveau_subdev *);
52 int  nouveau_subdev_fini(struct nouveau_subdev *, bool suspend);
53 void nouveau_subdev_reset(struct nouveau_object *);
54 
55 void _nouveau_subdev_dtor(struct nouveau_object *);
56 int  _nouveau_subdev_init(struct nouveau_object *);
57 int  _nouveau_subdev_fini(struct nouveau_object *, bool suspend);
58 
59 #define s_printk(s,l,f,a...) do {                                              \
60 	if ((s)->debug >= OS_DBG_##l) {                                        \
61 		nv_printk((s)->base.parent, (s)->name, l, f, ##a);             \
62 	}                                                                      \
63 } while(0)
64 
65 static inline u8
nv_rd08(void * obj,u32 addr)66 nv_rd08(void *obj, u32 addr)
67 {
68 	struct nouveau_subdev *subdev = nv_subdev(obj);
69 #ifdef __NetBSD__
70 	u8 data = bus_space_read_stream_1(subdev->mmiot, subdev->mmioh, addr);
71 #else
72 	u8 data = ioread8(subdev->mmio + addr);
73 #endif
74 	nv_spam(subdev, "nv_rd08 0x%06x 0x%02x\n", addr, data);
75 	return data;
76 }
77 
78 static inline u16
nv_rd16(void * obj,u32 addr)79 nv_rd16(void *obj, u32 addr)
80 {
81 	struct nouveau_subdev *subdev = nv_subdev(obj);
82 #ifdef __NetBSD__
83 	u16 data = bus_space_read_stream_2(subdev->mmiot, subdev->mmioh, addr);
84 #else
85 	u16 data = ioread16_native(subdev->mmio + addr);
86 #endif
87 	nv_spam(subdev, "nv_rd16 0x%06x 0x%04x\n", addr, data);
88 	return data;
89 }
90 
91 static inline u32
nv_rd32(void * obj,u32 addr)92 nv_rd32(void *obj, u32 addr)
93 {
94 	struct nouveau_subdev *subdev = nv_subdev(obj);
95 #ifdef __NetBSD__
96 	u32 data = bus_space_read_stream_4(subdev->mmiot, subdev->mmioh, addr);
97 #else
98 	u32 data = ioread32_native(subdev->mmio + addr);
99 #endif
100 	nv_spam(subdev, "nv_rd32 0x%06x 0x%08x\n", addr, data);
101 	return data;
102 }
103 
104 static inline void
nv_wr08(void * obj,u32 addr,u8 data)105 nv_wr08(void *obj, u32 addr, u8 data)
106 {
107 	struct nouveau_subdev *subdev = nv_subdev(obj);
108 	nv_spam(subdev, "nv_wr08 0x%06x 0x%02x\n", addr, data);
109 #ifdef __NetBSD__
110 	bus_space_write_stream_1(subdev->mmiot, subdev->mmioh, addr, data);
111 #else
112 	iowrite8(data, subdev->mmio + addr);
113 #endif
114 }
115 
116 static inline void
nv_wr16(void * obj,u32 addr,u16 data)117 nv_wr16(void *obj, u32 addr, u16 data)
118 {
119 	struct nouveau_subdev *subdev = nv_subdev(obj);
120 	nv_spam(subdev, "nv_wr16 0x%06x 0x%04x\n", addr, data);
121 #ifdef __NetBSD__
122 	bus_space_write_stream_2(subdev->mmiot, subdev->mmioh, addr, data);
123 #else
124 	iowrite16_native(data, subdev->mmio + addr);
125 #endif
126 }
127 
128 static inline void
nv_wr32(void * obj,u32 addr,u32 data)129 nv_wr32(void *obj, u32 addr, u32 data)
130 {
131 	struct nouveau_subdev *subdev = nv_subdev(obj);
132 	nv_spam(subdev, "nv_wr32 0x%06x 0x%08x\n", addr, data);
133 #ifdef __NetBSD__
134 	bus_space_write_stream_4(subdev->mmiot, subdev->mmioh, addr, data);
135 #else
136 	iowrite32_native(data, subdev->mmio + addr);
137 #endif
138 }
139 
140 static inline u32
nv_mask(void * obj,u32 addr,u32 mask,u32 data)141 nv_mask(void *obj, u32 addr, u32 mask, u32 data)
142 {
143 	u32 temp = nv_rd32(obj, addr);
144 	nv_wr32(obj, addr, (temp & ~mask) | data);
145 	return temp;
146 }
147 
148 #endif
149