1 /* SPDX-License-Identifier: MIT */
2 #ifndef __NVKM_MEMORY_H__
3 #define __NVKM_MEMORY_H__
4 #include <core/os.h>
5 struct nvkm_device;
6 struct nvkm_vma;
7 struct nvkm_vmm;
8 
9 struct nvkm_tags {
10 	struct nvkm_mm_node *mn;
11 	refcount_t refcount;
12 };
13 
14 enum nvkm_memory_target {
15 	NVKM_MEM_TARGET_INST_SR_LOST, /* instance memory - not preserved across suspend */
16 	NVKM_MEM_TARGET_INST, /* instance memory */
17 	NVKM_MEM_TARGET_VRAM, /* video memory */
18 	NVKM_MEM_TARGET_HOST, /* coherent system memory */
19 	NVKM_MEM_TARGET_NCOH, /* non-coherent system memory */
20 };
21 
22 struct nvkm_memory {
23 	const struct nvkm_memory_func *func;
24 	const struct nvkm_memory_ptrs *ptrs;
25 	struct kref kref;
26 	struct nvkm_tags *tags;
27 };
28 
29 struct nvkm_memory_func {
30 	void *(*dtor)(struct nvkm_memory *);
31 	enum nvkm_memory_target (*target)(struct nvkm_memory *);
32 	u8 (*page)(struct nvkm_memory *);
33 	u64 (*bar2)(struct nvkm_memory *);
34 	u64 (*addr)(struct nvkm_memory *);
35 	u64 (*size)(struct nvkm_memory *);
36 	void (*boot)(struct nvkm_memory *, struct nvkm_vmm *);
37 	void __iomem *(*acquire)(struct nvkm_memory *);
38 	void (*release)(struct nvkm_memory *);
39 	int (*map)(struct nvkm_memory *, u64 offset, struct nvkm_vmm *,
40 		   struct nvkm_vma *, void *argv, u32 argc);
41 	int (*kmap)(struct nvkm_memory *, struct nvkm_memory **);
42 };
43 
44 struct nvkm_memory_ptrs {
45 	u32 (*rd32)(struct nvkm_memory *, u64 offset);
46 	void (*wr32)(struct nvkm_memory *, u64 offset, u32 data);
47 };
48 
49 void nvkm_memory_ctor(const struct nvkm_memory_func *, struct nvkm_memory *);
50 int nvkm_memory_new(struct nvkm_device *, enum nvkm_memory_target,
51 		    u64 size, u32 align, bool zero, struct nvkm_memory **);
52 struct nvkm_memory *nvkm_memory_ref(struct nvkm_memory *);
53 void nvkm_memory_unref(struct nvkm_memory **);
54 int nvkm_memory_tags_get(struct nvkm_memory *, struct nvkm_device *, u32 tags,
55 			 void (*clear)(struct nvkm_device *, u32, u32),
56 			 struct nvkm_tags **);
57 void nvkm_memory_tags_put(struct nvkm_memory *, struct nvkm_device *,
58 			  struct nvkm_tags **);
59 
60 #define nvkm_memory_target(p) (p)->func->target(p)
61 #define nvkm_memory_page(p) (p)->func->page(p)
62 #define nvkm_memory_bar2(p) (p)->func->bar2(p)
63 #define nvkm_memory_addr(p) (p)->func->addr(p)
64 #define nvkm_memory_size(p) (p)->func->size(p)
65 #define nvkm_memory_boot(p,v) (p)->func->boot((p),(v))
66 #define nvkm_memory_map(p,o,vm,va,av,ac)                                       \
67 	(p)->func->map((p),(o),(vm),(va),(av),(ac))
68 #define nvkm_memory_kmap(p,i) ((p)->func->kmap ? (p)->func->kmap((p), (i)) : -ENOSYS)
69 
70 /* accessor macros - kmap()/done() must bracket use of the other accessor
71  * macros to guarantee correct behaviour across all chipsets
72  */
73 #define nvkm_kmap(o)     (o)->func->acquire(o)
74 #define nvkm_done(o)     (o)->func->release(o)
75 
76 #define nvkm_ro32(o,a)   (o)->ptrs->rd32((o), (a))
77 #define nvkm_wo32(o,a,d) (o)->ptrs->wr32((o), (a), (d))
78 #define nvkm_mo32(o,a,m,d) ({                                                  \
79 	u32 _addr = (a), _data = nvkm_ro32((o), _addr);                        \
80 	nvkm_wo32((o), _addr, (_data & ~(m)) | (d));                           \
81 	_data;                                                                 \
82 })
83 
84 #define nvkm_wo64(o,a,d) do {                                                  \
85 	u64 __a = (a), __d = (d);                                              \
86 	nvkm_wo32((o), __a + 0, lower_32_bits(__d));                           \
87 	nvkm_wo32((o), __a + 4, upper_32_bits(__d));                           \
88 } while(0)
89 
90 #define nvkm_robj(o,a,p,s) do {                                                \
91 	u32 _addr = (a), _size = (s) >> 2, *_data = (void *)(p);               \
92 	while (_size--) {                                                      \
93 		*(_data++) = nvkm_ro32((o), _addr);                            \
94 		_addr += 4;                                                    \
95 	}                                                                      \
96 } while(0)
97 
98 #define nvkm_wobj(o,a,p,s) do {                                                \
99 	u32 _addr = (a), _size = (s) >> 2, *_data = (void *)(p);               \
100 	while (_size--) {                                                      \
101 		nvkm_wo32((o), _addr, *(_data++));                             \
102 		_addr += 4;                                                    \
103 	}                                                                      \
104 } while(0)
105 
106 #define nvkm_fill(t,s,o,a,d,c) do {                                            \
107 	u64 _a = (a), _c = (c), _d = (d), _o = _a >> s, _s = _c << s;          \
108 	u##t __iomem *_m = nvkm_kmap(o);                                       \
109 	if (likely(_m)) {                                                      \
110 		if (_d) {                                                      \
111 			while (_c--)                                           \
112 				iowrite##t##_native(_d, &_m[_o++]);            \
113 		} else {                                                       \
114 			memset_io(&_m[_o], _d, _s);                            \
115 		}                                                              \
116 	} else {                                                               \
117 		for (; _c; _c--, _a += BIT(s))                                 \
118 			nvkm_wo##t((o), _a, _d);                               \
119 	}                                                                      \
120 	nvkm_done(o);                                                          \
121 } while(0)
122 #define nvkm_fo32(o,a,d,c) nvkm_fill(32, 2, (o), (a), (d), (c))
123 #define nvkm_fo64(o,a,d,c) nvkm_fill(64, 3, (o), (a), (d), (c))
124 #endif
125