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