xref: /OK3568_Linux_fs/kernel/drivers/rknpu/rknpu_mm.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) Rockchip Electronics Co.Ltd
4  * Author: Felix Zeng <felix.zeng@rock-chips.com>
5  */
6 
7 #include "rknpu_debugger.h"
8 #include "rknpu_mm.h"
9 
rknpu_mm_create(unsigned int mem_size,unsigned int chunk_size,struct rknpu_mm ** mm)10 int rknpu_mm_create(unsigned int mem_size, unsigned int chunk_size,
11 		    struct rknpu_mm **mm)
12 {
13 	unsigned int num_of_longs;
14 	int ret = -EINVAL;
15 
16 	if (WARN_ON(mem_size < chunk_size))
17 		return -EINVAL;
18 	if (WARN_ON(mem_size == 0))
19 		return -EINVAL;
20 	if (WARN_ON(chunk_size == 0))
21 		return -EINVAL;
22 
23 	*mm = kzalloc(sizeof(struct rknpu_mm), GFP_KERNEL);
24 	if (!(*mm))
25 		return -ENOMEM;
26 
27 	(*mm)->chunk_size = chunk_size;
28 	(*mm)->total_chunks = mem_size / chunk_size;
29 	(*mm)->free_chunks = (*mm)->total_chunks;
30 
31 	num_of_longs =
32 		((*mm)->total_chunks + BITS_PER_LONG - 1) / BITS_PER_LONG;
33 
34 	(*mm)->bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
35 	if (!(*mm)->bitmap) {
36 		ret = -ENOMEM;
37 		goto free_mm;
38 	}
39 
40 	mutex_init(&(*mm)->lock);
41 
42 	LOG_DEBUG("total_chunks: %d, bitmap: %p\n", (*mm)->total_chunks,
43 		  (*mm)->bitmap);
44 
45 	return 0;
46 
47 free_mm:
48 	kfree(mm);
49 	return ret;
50 }
51 
rknpu_mm_destroy(struct rknpu_mm * mm)52 void rknpu_mm_destroy(struct rknpu_mm *mm)
53 {
54 	if (mm != NULL) {
55 		mutex_destroy(&mm->lock);
56 		kfree(mm->bitmap);
57 		kfree(mm);
58 	}
59 }
60 
rknpu_mm_alloc(struct rknpu_mm * mm,unsigned int size,struct rknpu_mm_obj ** mm_obj)61 int rknpu_mm_alloc(struct rknpu_mm *mm, unsigned int size,
62 		   struct rknpu_mm_obj **mm_obj)
63 {
64 	unsigned int found, start_search, cur_size;
65 
66 	if (size == 0)
67 		return -EINVAL;
68 
69 	if (size > mm->total_chunks * mm->chunk_size)
70 		return -ENOMEM;
71 
72 	*mm_obj = kzalloc(sizeof(struct rknpu_mm_obj), GFP_KERNEL);
73 	if (!(*mm_obj))
74 		return -ENOMEM;
75 
76 	start_search = 0;
77 
78 	mutex_lock(&mm->lock);
79 
80 mm_restart_search:
81 	/* Find the first chunk that is free */
82 	found = find_next_zero_bit(mm->bitmap, mm->total_chunks, start_search);
83 
84 	/* If there wasn't any free chunk, bail out */
85 	if (found == mm->total_chunks)
86 		goto mm_no_free_chunk;
87 
88 	/* Update fields of mm_obj */
89 	(*mm_obj)->range_start = found;
90 	(*mm_obj)->range_end = found;
91 
92 	/* If we need only one chunk, mark it as allocated and get out */
93 	if (size <= mm->chunk_size) {
94 		set_bit(found, mm->bitmap);
95 		goto mm_out;
96 	}
97 
98 	/* Otherwise, try to see if we have enough contiguous chunks */
99 	cur_size = size - mm->chunk_size;
100 	do {
101 		(*mm_obj)->range_end = find_next_zero_bit(
102 			mm->bitmap, mm->total_chunks, ++found);
103 		/*
104 		 * If next free chunk is not contiguous than we need to
105 		 * restart our search from the last free chunk we found (which
106 		 * wasn't contiguous to the previous ones
107 		 */
108 		if ((*mm_obj)->range_end != found) {
109 			start_search = found;
110 			goto mm_restart_search;
111 		}
112 
113 		/*
114 		 * If we reached end of buffer, bail out with error
115 		 */
116 		if (found == mm->total_chunks)
117 			goto mm_no_free_chunk;
118 
119 		/* Check if we don't need another chunk */
120 		if (cur_size <= mm->chunk_size)
121 			cur_size = 0;
122 		else
123 			cur_size -= mm->chunk_size;
124 
125 	} while (cur_size > 0);
126 
127 	/* Mark the chunks as allocated */
128 	for (found = (*mm_obj)->range_start; found <= (*mm_obj)->range_end;
129 	     found++)
130 		set_bit(found, mm->bitmap);
131 
132 mm_out:
133 	mm->free_chunks -= ((*mm_obj)->range_end - (*mm_obj)->range_start + 1);
134 	mutex_unlock(&mm->lock);
135 
136 	LOG_DEBUG("mm allocate, mm_obj: %p, range_start: %d, range_end: %d\n",
137 		  *mm_obj, (*mm_obj)->range_start, (*mm_obj)->range_end);
138 
139 	return 0;
140 
141 mm_no_free_chunk:
142 	mutex_unlock(&mm->lock);
143 	kfree(*mm_obj);
144 
145 	return -ENOMEM;
146 }
147 
rknpu_mm_free(struct rknpu_mm * mm,struct rknpu_mm_obj * mm_obj)148 int rknpu_mm_free(struct rknpu_mm *mm, struct rknpu_mm_obj *mm_obj)
149 {
150 	unsigned int bit;
151 
152 	/* Act like kfree when trying to free a NULL object */
153 	if (!mm_obj)
154 		return 0;
155 
156 	LOG_DEBUG("mm free, mem_obj: %p, range_start: %d, range_end: %d\n",
157 		  mm_obj, mm_obj->range_start, mm_obj->range_end);
158 
159 	mutex_lock(&mm->lock);
160 
161 	/* Mark the chunks as free */
162 	for (bit = mm_obj->range_start; bit <= mm_obj->range_end; bit++)
163 		clear_bit(bit, mm->bitmap);
164 
165 	mm->free_chunks += (mm_obj->range_end - mm_obj->range_start + 1);
166 
167 	mutex_unlock(&mm->lock);
168 
169 	kfree(mm_obj);
170 
171 	return 0;
172 }
173 
rknpu_mm_dump(struct seq_file * m,void * data)174 int rknpu_mm_dump(struct seq_file *m, void *data)
175 {
176 	struct rknpu_debugger_node *node = m->private;
177 	struct rknpu_debugger *debugger = node->debugger;
178 	struct rknpu_device *rknpu_dev =
179 		container_of(debugger, struct rknpu_device, debugger);
180 	struct rknpu_mm *mm = NULL;
181 	int cur = 0, rbot = 0, rtop = 0;
182 	size_t ret = 0;
183 	char buf[64];
184 	size_t size = sizeof(buf);
185 	int seg_chunks = 32, seg_id = 0;
186 	int free_size = 0;
187 	int i = 0;
188 
189 	mm = rknpu_dev->sram_mm;
190 	if (mm == NULL)
191 		return 0;
192 
193 	seq_printf(m, "SRAM bitmap: \"*\" - used, \".\" - free (1bit = %dKB)\n",
194 		   mm->chunk_size / 1024);
195 
196 	rbot = cur = find_first_bit(mm->bitmap, mm->total_chunks);
197 	for (i = 0; i < cur; ++i) {
198 		ret += scnprintf(buf + ret, size - ret, ".");
199 		if (ret >= seg_chunks) {
200 			seq_printf(m, "[%03d] [%s]\n", seg_id++, buf);
201 			ret = 0;
202 		}
203 	}
204 	while (cur < mm->total_chunks) {
205 		rtop = cur;
206 		cur = find_next_bit(mm->bitmap, mm->total_chunks, cur + 1);
207 		if (cur < mm->total_chunks && cur <= rtop + 1)
208 			continue;
209 
210 		for (i = rbot; i <= rtop; ++i) {
211 			ret += scnprintf(buf + ret, size - ret, "*");
212 			if (ret >= seg_chunks) {
213 				seq_printf(m, "[%03d] [%s]\n", seg_id++, buf);
214 				ret = 0;
215 			}
216 		}
217 
218 		for (i = rtop + 1; i < cur; ++i) {
219 			ret += scnprintf(buf + ret, size - ret, ".");
220 			if (ret >= seg_chunks) {
221 				seq_printf(m, "[%03d] [%s]\n", seg_id++, buf);
222 				ret = 0;
223 			}
224 		}
225 
226 		rbot = cur;
227 	}
228 
229 	if (ret > 0)
230 		seq_printf(m, "[%03d] [%s]\n", seg_id++, buf);
231 
232 	free_size = mm->free_chunks * mm->chunk_size;
233 	seq_printf(m, "SRAM total size: %d, used: %d, free: %d\n",
234 		   rknpu_dev->sram_size, rknpu_dev->sram_size - free_size,
235 		   free_size);
236 
237 	return 0;
238 }
239