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