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
|
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
typedef struct IOMap IOMap;
struct IOMap
{
IOMap *next;
ulong start;
ulong end;
char reserved;
char tag[13];
};
static struct
{
Lock;
IOMap *m;
IOMap *free;
IOMap maps[32]; /* some initial free maps */
ulong mask;
} iomap;
static void
insert(IOMap **l, ulong start, ulong end, char *tag, int reserved)
{
IOMap *m;
m = iomap.free;
if(m != nil)
iomap.free = m->next;
else
m = malloc(sizeof(IOMap));
if(m == nil)
panic("ioalloc: out of memory");
m->next = *l;
m->start = start;
m->end = end;
m->reserved = reserved;
strncpy(m->tag, tag, sizeof(m->tag)-1);
m->tag[sizeof(m->tag)-1] = 0;
*l = m;
}
/*
* Reserve a range to be ioalloced later.
* If port is -1, look for a free range above 0x400.
*/
int
ioreserve(ulong port, ulong size, ulong align, char *tag)
{
if(port == -1)
return ioreservewin(0x400, -0x400 & iomap.mask, size, align, tag);
else
return ioreservewin(port, size, size, align, tag);
}
/*
* Find a free region of length "size" within the window [port, port+win)
* and reserve it to be ioalloced later.
*/
int
ioreservewin(ulong port, ulong win, ulong size, ulong align, char *tag)
{
IOMap *m, **l;
if(win == 0 || port & ~iomap.mask || (~port & iomap.mask) < (win-1 & iomap.mask))
return -1;
size = (size + ~iomap.mask) & iomap.mask;
if(size == 0 || size > win)
return -1;
if(align){
if((align & (align-1)) != 0)
return -1;
align--;
}
win += port;
port = (port+align) & ~align;
if(port >= win || win - port < size)
return -1;
lock(&iomap);
for(l = &iomap.m; (m = *l) != nil; l = &m->next){
if(m->end <= port)
continue;
if(m->start > port && m->start - port >= size)
break;
port = m->end;
port = (port+align) & ~align;
if(port >= win || win - port < size){
unlock(&iomap);
return -1;
}
}
insert(l, port, port + size, tag, 1);
unlock(&iomap);
return port;
}
/*
* Alloc some io port space and remember who it was
* alloced to. If port == -1, find a free region.
*/
int
ioalloc(ulong port, ulong size, ulong align, char *tag)
{
IOMap *m, **l;
if(port == -1)
port = ioreserve(port, size, align, tag);
size = (size + ~iomap.mask) & iomap.mask;
if(size == 0 || port & ~iomap.mask || (~port & iomap.mask) < (size-1 & iomap.mask))
return -1;
lock(&iomap);
for(l = &iomap.m; (m = *l) != nil; l = &m->next){
if(m->end <= port)
continue;
if(m->start > port && m->start - port >= size)
break;
if(m->reserved && m->start <= port && m->end - port >= size){
if(m->end - port > size)
insert(&m->next, port + size, m->end, m->tag, 1);
if(m->start < port){
insert(l, m->start, port, m->tag, 1);
l = &(*l)->next;
}
*l = m->next;
m->next = iomap.free;
iomap.free = m;
break;
}
print("ioalloc: %lux - %lux %s: clashes with: %lux - %lux %s\n",
port, port+size-1, tag,
m->start, m->end-1, m->tag);
unlock(&iomap);
return -1;
}
insert(l, port, port + size, tag, 0);
unlock(&iomap);
return port;
}
void
iofree(ulong port)
{
IOMap *m, **l;
if(port & ~iomap.mask)
return;
lock(&iomap);
for(l = &iomap.m; (m = *l) != nil; l = &m->next){
if(m->start == port){
*l = m->next;
m->next = iomap.free;
iomap.free = m;
break;
}
if(m->start > port)
break;
}
unlock(&iomap);
}
int
iounused(ulong start, ulong end)
{
IOMap *m;
if(start & ~iomap.mask || end < start)
return 0;
for(m = iomap.m; m != nil; m = m->next){
if(m->end <= start)
continue;
if(m->start >= end)
break;
if(!m->reserved)
return 0;
}
return 1;
}
static long
iomapread(Chan*, void *a, long n, vlong offset)
{
char buf[32];
IOMap *m;
int i;
lock(&iomap);
i = 0;
for(m = iomap.m; m != nil; m = m->next){
i = snprint(buf, sizeof(buf), "%8lux %8lux %-12.12s\n",
m->start, m->end-1, m->tag);
offset -= i;
if(offset < 0)
break;
}
unlock(&iomap);
if(offset >= 0)
return 0;
if(n > -offset)
n = -offset;
offset += i;
memmove(a, buf+offset, n);
return n;
}
/*
* Initialize the io space port map.
* The mask argument defines the valid bits of
* a port address, so different architectures
* might have different sizes and alignments.
*/
void
iomapinit(ulong mask)
{
int i;
assert(mask != 0 && (mask >> 31) == 0);
for(i = 0; i < nelem(iomap.maps)-1; i++)
iomap.maps[i].next = &iomap.maps[i+1];
iomap.maps[i].next = nil;
iomap.free = iomap.maps;
iomap.mask = mask;
addarchfile("ioalloc", 0444, iomapread, nil);
}
|