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, /* instance memory */
16 	NVKM_MEM_TARGET_VRAM, /* video memory */
17 	NVKM_MEM_TARGET_HOST, /* coherent system memory */
18 	NVKM_MEM_TARGET_NCOH, /* non-coherent system memory */
19 };
20 
21 struct nvkm_memory {
22 	const struct nvkm_memory_func *func;
23 	const struct nvkm_memory_ptrs *ptrs;
24 	struct kref kref;
25 	struct nvkm_tags *tags;
26 };
27 
28 struct nvkm_memory_func {
29 	void *(*dtor)(struct nvkm_memory *);
30 	enum nvkm_memory_target (*target)(struct nvkm_memory *);
31 	u8 (*page)(struct nvkm_memory *);
32 	u64 (*bar2)(struct nvkm_memory *);
33 	u64 (*addr)(struct nvkm_memory *);
34 	u64 (*size)(struct nvkm_memory *);
35 	void (*boot)(struct nvkm_memory *, struct nvkm_vmm *);
36 	void __iomem *(*acquire)(struct nvkm_memory *);
37 	void (*release)(struct nvkm_memory *);
38 	int (*map)(struct nvkm_memory *, u64 offset, struct nvkm_vmm *,
39 		   struct nvkm_vma *, void *argv, u32 argc);
40 };
41 
42 struct nvkm_memory_ptrs {
43 	u32 (*rd32)(struct nvkm_memory *, u64 offset);
44 	void (*wr32)(struct nvkm_memory *, u64 offset, u32 data);
45 };
46 
47 void nvkm_memory_ctor(const struct nvkm_memory_func *, struct nvkm_memory *);
48 int nvkm_memory_new(struct nvkm_device *, enum nvkm_memory_target,
49 		    u64 size, u32 align, bool zero, struct nvkm_memory **);
50 struct nvkm_memory *nvkm_memory_ref(struct nvkm_memory *);
51 void nvkm_memory_unref(struct nvkm_memory **);
52 int nvkm_memory_tags_get(struct nvkm_memory *, struct nvkm_device *, u32 tags,
53 			 void (*clear)(struct nvkm_device *, u32, u32),
54 			 struct nvkm_tags **);
55 void nvkm_memory_tags_put(struct nvkm_memory *, struct nvkm_device *,
56 			  struct nvkm_tags **);
57 
58 #define nvkm_memory_target(p) (p)->func->target(p)
59 #define nvkm_memory_page(p) (p)->func->page(p)
60 #define nvkm_memory_bar2(p) (p)->func->bar2(p)
61 #define nvkm_memory_addr(p) (p)->func->addr(p)
62 #define nvkm_memory_size(p) (p)->func->size(p)
63 #define nvkm_memory_boot(p,v) (p)->func->boot((p),(v))
64 #define nvkm_memory_map(p,o,vm,va,av,ac)                                       \
65 	(p)->func->map((p),(o),(vm),(va),(av),(ac))
66 
67 /* accessor macros - kmap()/done() must bracket use of the other accessor
68  * macros to guarantee correct behaviour across all chipsets
69  */
70 #define nvkm_kmap(o)     (o)->func->acquire(o)
71 #define nvkm_done(o)     (o)->func->release(o)
72 
73 #define nvkm_ro32(o,a)   (o)->ptrs->rd32((o), (a))
74 #define nvkm_wo32(o,a,d) (o)->ptrs->wr32((o), (a), (d))
75 #define nvkm_mo32(o,a,m,d) ({                                                  \
76 	u32 _addr = (a), _data = nvkm_ro32((o), _addr);                        \
77 	nvkm_wo32((o), _addr, (_data & ~(m)) | (d));                           \
78 	_data;                                                                 \
79 })
80 
81 #define nvkm_wo64(o,a,d) do {                                                  \
82 	u64 __a = (a), __d = (d);                                              \
83 	nvkm_wo32((o), __a + 0, lower_32_bits(__d));                           \
84 	nvkm_wo32((o), __a + 4, upper_32_bits(__d));                           \
85 } while(0)
86 
87 #define nvkm_fill(t,s,o,a,d,c) do {                                            \
88 	u64 _a = (a), _c = (c), _d = (d), _o = _a >> s, _s = _c << s;          \
89 	u##t __iomem *_m = nvkm_kmap(o);                                       \
90 	if (likely(_m)) {                                                      \
91 		if (_d) {                                                      \
92 			while (_c--)                                           \
93 				iowrite##t##_native(_d, &_m[_o++]);            \
94 		} else {                                                       \
95 			memset_io(&_m[_o], _d, _s);                            \
96 		}                                                              \
97 	} else {                                                               \
98 		for (; _c; _c--, _a += BIT(s))                                 \
99 			nvkm_wo##t((o), _a, _d);                               \
100 	}                                                                      \
101 	nvkm_done(o);                                                          \
102 } while(0)
103 #define nvkm_fo32(o,a,d,c) nvkm_fill(32, 2, (o), (a), (d), (c))
104 #define nvkm_fo64(o,a,d,c) nvkm_fill(64, 3, (o), (a), (d), (c))
105 #endif
106