~funderscore blog cgit wiki get in touch
aboutsummaryrefslogtreecommitdiff
blob: 2ac20ea25b9ed4ec8ac7aee71f2015f5a9ed0d0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2018-2022 Marvell International Ltd.
 */

#include <log.h>
#include <time.h>
#include <linux/delay.h>

#include <mach/cvmx-regs.h>
#include <mach/cvmx-csr.h>
#include <mach/cvmx-bootmem.h>
#include <mach/octeon-model.h>
#include <mach/cvmx-fuse.h>
#include <mach/octeon-feature.h>
#include <mach/cvmx-qlm.h>
#include <mach/octeon_qlm.h>
#include <mach/cvmx-pcie.h>
#include <mach/cvmx-coremask.h>

#include <mach/cvmx-global-resources.h>

#include <mach/cvmx-pki.h>
#include <mach/cvmx-helper.h>
#include <mach/cvmx-helper-board.h>
#include <mach/cvmx-helper-cfg.h>

#include <mach/cvmx-range.h>

#define CVMX_RANGE_AVAILABLE ((u64)-88)
#define addr_of_element(base, index)					\
	(1ull << 63 | ((base) + sizeof(u64) + (index) * sizeof(u64)))
#define addr_of_size(base) (1ull << 63 | (base))

static const int debug;

int cvmx_range_memory_size(int nelements)
{
	return sizeof(u64) * (nelements + 1);
}

int cvmx_range_init(u64 range_addr, int size)
{
	u64 lsize = size;
	u64 i;

	cvmx_write64_uint64(addr_of_size(range_addr), lsize);
	for (i = 0; i < lsize; i++) {
		cvmx_write64_uint64(addr_of_element(range_addr, i),
				    CVMX_RANGE_AVAILABLE);
	}
	return 0;
}

static int64_t cvmx_range_find_next_available(u64 range_addr, u64 index,
					      int align)
{
	u64 size = cvmx_read64_uint64(addr_of_size(range_addr));
	u64 i;

	while ((index % align) != 0)
		index++;

	for (i = index; i < size; i += align) {
		u64 r_owner = cvmx_read64_uint64(addr_of_element(range_addr, i));

		if (debug)
			debug("%s: index=%d owner=%llx\n", __func__, (int)i,
			      (unsigned long long)r_owner);
		if (r_owner == CVMX_RANGE_AVAILABLE)
			return i;
	}
	return -1;
}

static int64_t cvmx_range_find_last_available(u64 range_addr, u64 index,
					      u64 align)
{
	u64 size = cvmx_read64_uint64(addr_of_size(range_addr));
	u64 i;

	if (index == 0)
		index = size - 1;

	while ((index % align) != 0)
		index++;

	for (i = index; i > align; i -= align) {
		u64 r_owner = cvmx_read64_uint64(addr_of_element(range_addr, i));

		if (debug)
			debug("%s: index=%d owner=%llx\n", __func__, (int)i,
			      (unsigned long long)r_owner);
		if (r_owner == CVMX_RANGE_AVAILABLE)
			return i;
	}
	return -1;
}

int cvmx_range_alloc_ordered(u64 range_addr, u64 owner, u64 cnt,
			     int align, int reverse)
{
	u64 i = 0, size;
	s64 first_available;

	if (debug)
		debug("%s: range_addr=%llx  owner=%llx cnt=%d\n", __func__,
		      (unsigned long long)range_addr,
		      (unsigned long long)owner, (int)cnt);

	size = cvmx_read64_uint64(addr_of_size(range_addr));
	while (i < size) {
		u64 available_cnt = 0;

		if (reverse)
			first_available = cvmx_range_find_last_available(range_addr, i, align);
		else
			first_available = cvmx_range_find_next_available(range_addr, i, align);
		if (first_available == -1)
			return -1;
		i = first_available;

		if (debug)
			debug("%s: first_available=%d\n", __func__, (int)first_available);
		while ((available_cnt != cnt) && (i < size)) {
			u64 r_owner = cvmx_read64_uint64(addr_of_element(range_addr, i));

			if (r_owner == CVMX_RANGE_AVAILABLE)
				available_cnt++;
			i++;
		}
		if (available_cnt == cnt) {
			u64 j;

			if (debug)
				debug("%s: first_available=%d available=%d\n",
				      __func__,
				      (int)first_available, (int)available_cnt);

			for (j = first_available; j < first_available + cnt;
			     j++) {
				u64 a = addr_of_element(range_addr, j);

				cvmx_write64_uint64(a, owner);
			}
			return first_available;
		}
	}

	if (debug) {
		debug("ERROR: %s: failed to allocate range cnt=%d\n",
		      __func__, (int)cnt);
		cvmx_range_show(range_addr);
	}

	return -1;
}

int cvmx_range_alloc(u64 range_addr, u64 owner, u64 cnt, int align)
{
	return cvmx_range_alloc_ordered(range_addr, owner, cnt, align, 0);
}

int cvmx_range_reserve(u64 range_addr, u64 owner, u64 base,
		       u64 cnt)
{
	u64 i, size, r_owner;
	u64 up = base + cnt;

	size = cvmx_read64_uint64(addr_of_size(range_addr));
	if (up > size) {
		debug("ERROR: %s: invalid base or cnt. range_addr=0x%llx, owner=0x%llx, size=%d base+cnt=%d\n",
		      __func__, (unsigned long long)range_addr,
		      (unsigned long long)owner,
		      (int)size, (int)up);
		return -1;
	}
	for (i = base; i < up; i++) {
		r_owner = cvmx_read64_uint64(addr_of_element(range_addr, i));
		if (debug)
			debug("%s: %d: %llx\n",
			      __func__, (int)i, (unsigned long long)r_owner);
		if (r_owner != CVMX_RANGE_AVAILABLE) {
			if (debug) {
				debug("%s: resource already reserved base+cnt=%d %llu %llu %llx %llx %llx\n",
				      __func__, (int)i, (unsigned long long)cnt,
				      (unsigned long long)base,
				      (unsigned long long)r_owner,
				      (unsigned long long)range_addr,
				      (unsigned long long)owner);
			}
			return -1;
		}
	}
	for (i = base; i < up; i++)
		cvmx_write64_uint64(addr_of_element(range_addr, i), owner);
	return base;
}

int __cvmx_range_is_allocated(u64 range_addr, int bases[], int count)
{
	u64 i, cnt, size;
	u64 r_owner;

	cnt = count;
	size = cvmx_read64_uint64(addr_of_size(range_addr));
	for (i = 0; i < cnt; i++) {
		u64 base = bases[i];

		if (base >= size) {
			debug("ERROR: %s: invalid base or cnt size=%d base=%d\n",
			      __func__, (int)size, (int)base);
			return 0;
		}
		r_owner = cvmx_read64_uint64(addr_of_element(range_addr, base));
		if (r_owner == CVMX_RANGE_AVAILABLE) {
			if (debug) {
				debug("%s: i=%d:base=%d is available\n",
				      __func__, (int)i, (int)base);
			}
			return 0;
		}
	}
	return 1;
}

int cvmx_range_free_mutiple(u64 range_addr, int bases[], int count)
{
	u64 i, cnt;

	cnt = count;
	if (__cvmx_range_is_allocated(range_addr, bases, count) != 1)
		return -1;
	for (i = 0; i < cnt; i++) {
		u64 base = bases[i];

		cvmx_write64_uint64(addr_of_element(range_addr, base),
				    CVMX_RANGE_AVAILABLE);
	}
	return 0;
}

int cvmx_range_free_with_base(u64 range_addr, int base, int cnt)
{
	u64 i, size;
	u64 up = base + cnt;

	size = cvmx_read64_uint64(addr_of_size(range_addr));
	if (up > size) {
		debug("ERROR: %s: invalid base or cnt size=%d base+cnt=%d\n",
		      __func__, (int)size, (int)up);
		return -1;
	}
	for (i = base; i < up; i++) {
		cvmx_write64_uint64(addr_of_element(range_addr, i),
				    CVMX_RANGE_AVAILABLE);
	}
	return 0;
}