GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / gpu / drm / nouveau / include / nvif / object.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NVIF_OBJECT_H__
3 #define __NVIF_OBJECT_H__
4
5 #include <nvif/os.h>
6
7 struct nvif_sclass {
8         s32 oclass;
9         int minver;
10         int maxver;
11 };
12
13 struct nvif_object {
14         struct nvif_client *client;
15         u32 handle;
16         s32 oclass;
17         void *priv; /*XXX: hack */
18         struct {
19                 void __iomem *ptr;
20                 u32 size;
21         } map;
22 };
23
24 int  nvif_object_init(struct nvif_object *, u32 handle, s32 oclass, void *, u32,
25                       struct nvif_object *);
26 void nvif_object_fini(struct nvif_object *);
27 int  nvif_object_ioctl(struct nvif_object *, void *, u32, void **);
28 int  nvif_object_sclass_get(struct nvif_object *, struct nvif_sclass **);
29 void nvif_object_sclass_put(struct nvif_sclass **);
30 u32  nvif_object_rd(struct nvif_object *, int, u64);
31 void nvif_object_wr(struct nvif_object *, int, u64, u32);
32 int  nvif_object_mthd(struct nvif_object *, u32, void *, u32);
33 int  nvif_object_map(struct nvif_object *);
34 void nvif_object_unmap(struct nvif_object *);
35
36 #define nvif_handle(a) (unsigned long)(void *)(a)
37 #define nvif_object(a) (a)->object
38
39 #define nvif_rd(a,f,b,c) ({                                                    \
40         struct nvif_object *_object = (a);                                     \
41         u32 _data;                                                             \
42         if (likely(_object->map.ptr))                                          \
43                 _data = f((u8 __iomem *)_object->map.ptr + (c));               \
44         else                                                                   \
45                 _data = nvif_object_rd(_object, (b), (c));                     \
46         _data;                                                                 \
47 })
48 #define nvif_wr(a,f,b,c,d) ({                                                  \
49         struct nvif_object *_object = (a);                                     \
50         if (likely(_object->map.ptr))                                          \
51                 f((d), (u8 __iomem *)_object->map.ptr + (c));                  \
52         else                                                                   \
53                 nvif_object_wr(_object, (b), (c), (d));                        \
54 })
55 #define nvif_rd08(a,b) ({ ((u8)nvif_rd((a), ioread8, 1, (b))); })
56 #define nvif_rd16(a,b) ({ ((u16)nvif_rd((a), ioread16_native, 2, (b))); })
57 #define nvif_rd32(a,b) ({ ((u32)nvif_rd((a), ioread32_native, 4, (b))); })
58 #define nvif_wr08(a,b,c) nvif_wr((a), iowrite8, 1, (b), (u8)(c))
59 #define nvif_wr16(a,b,c) nvif_wr((a), iowrite16_native, 2, (b), (u16)(c))
60 #define nvif_wr32(a,b,c) nvif_wr((a), iowrite32_native, 4, (b), (u32)(c))
61 #define nvif_mask(a,b,c,d) ({                                                  \
62         struct nvif_object *__object = (a);                                    \
63         u32 _addr = (b), _data = nvif_rd32(__object, _addr);                   \
64         nvif_wr32(__object, _addr, (_data & ~(c)) | (d));                      \
65         _data;                                                                 \
66 })
67
68 #define nvif_mthd(a,b,c,d) nvif_object_mthd((a), (b), (c), (d))
69
70 struct nvif_mclass {
71         s32 oclass;
72         int version;
73 };
74
75 #define nvif_mclass(o,m) ({                                                    \
76         struct nvif_object *object = (o);                                      \
77         struct nvif_sclass *sclass;                                            \
78         const typeof(m[0]) *mclass = (m);                                      \
79         int ret = -ENODEV;                                                     \
80         int cnt, i, j;                                                         \
81                                                                                \
82         cnt = nvif_object_sclass_get(object, &sclass);                         \
83         if (cnt >= 0) {                                                        \
84                 for (i = 0; ret < 0 && mclass[i].oclass; i++) {                \
85                         for (j = 0; j < cnt; j++) {                            \
86                                 if (mclass[i].oclass  == sclass[j].oclass &&   \
87                                     mclass[i].version >= sclass[j].minver &&   \
88                                     mclass[i].version <= sclass[j].maxver) {   \
89                                         ret = i;                               \
90                                         break;                                 \
91                                 }                                              \
92                         }                                                      \
93                 }                                                              \
94                 nvif_object_sclass_put(&sclass);                               \
95         }                                                                      \
96         ret;                                                                   \
97 })
98
99 /*XXX*/
100 #include <core/object.h>
101 #define nvxx_object(a) ({                                                      \
102         struct nvif_object *_object = (a);                                     \
103         (struct nvkm_object *)_object->priv;                                   \
104 })
105 #endif