1 /* SPDX-License-Identifier: MIT */
2 #ifndef __NVKM_MM_H__
3 #define __NVKM_MM_H__
4 #include <core/os.h>
5 
6 struct nvkm_mm_node {
7 	struct list_head nl_entry;
8 	struct list_head fl_entry;
9 	struct nvkm_mm_node *next;
10 
11 #define NVKM_MM_HEAP_ANY 0x00
12 	u8  heap;
13 #define NVKM_MM_TYPE_NONE 0x00
14 #define NVKM_MM_TYPE_HOLE 0xff
15 	u8  type;
16 	u32 offset;
17 	u32 length;
18 };
19 
20 struct nvkm_mm {
21 	struct list_head nodes;
22 	struct list_head free;
23 
24 	u32 block_size;
25 	int heap_nodes;
26 };
27 
28 static inline bool
nvkm_mm_initialised(struct nvkm_mm * mm)29 nvkm_mm_initialised(struct nvkm_mm *mm)
30 {
31 	return mm->heap_nodes;
32 }
33 
34 int  nvkm_mm_init(struct nvkm_mm *, u8 heap, u32 offset, u32 length, u32 block);
35 int  nvkm_mm_fini(struct nvkm_mm *);
36 int  nvkm_mm_head(struct nvkm_mm *, u8 heap, u8 type, u32 size_max,
37 		  u32 size_min, u32 align, struct nvkm_mm_node **);
38 int  nvkm_mm_tail(struct nvkm_mm *, u8 heap, u8 type, u32 size_max,
39 		  u32 size_min, u32 align, struct nvkm_mm_node **);
40 void nvkm_mm_free(struct nvkm_mm *, struct nvkm_mm_node **);
41 void nvkm_mm_dump(struct nvkm_mm *, const char *);
42 
43 static inline u32
nvkm_mm_heap_size(struct nvkm_mm * mm,u8 heap)44 nvkm_mm_heap_size(struct nvkm_mm *mm, u8 heap)
45 {
46 	struct nvkm_mm_node *node;
47 	u32 size = 0;
48 	list_for_each_entry(node, &mm->nodes, nl_entry) {
49 		if (node->heap == heap)
50 			size += node->length;
51 	}
52 	return size;
53 }
54 
55 static inline bool
nvkm_mm_contiguous(struct nvkm_mm_node * node)56 nvkm_mm_contiguous(struct nvkm_mm_node *node)
57 {
58 	return !node->next;
59 }
60 
61 static inline u32
nvkm_mm_addr(struct nvkm_mm_node * node)62 nvkm_mm_addr(struct nvkm_mm_node *node)
63 {
64 	if (WARN_ON(!nvkm_mm_contiguous(node)))
65 		return 0;
66 	return node->offset;
67 }
68 
69 static inline u32
nvkm_mm_size(struct nvkm_mm_node * node)70 nvkm_mm_size(struct nvkm_mm_node *node)
71 {
72 	u32 size = 0;
73 	do {
74 		size += node->length;
75 	} while ((node = node->next));
76 	return size;
77 }
78 #endif
79