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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
/*
amddev.c
Driver for the AMD Device Exclusion Vector (DEV)
*/
#include <minix/driver.h>
#include <minix/config.h>
#include <minix/type.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <machine/vm.h>
#include <machine/vmparam.h>
#include <signal.h>
#include <minix/com.h>
#include <minix/const.h>
#include <minix/ipc.h>
#include <minix/syslib.h>
#include <minix/sysutil.h>
#include <minix/endpoint.h>
#include <machine/pci.h>
/* Offsets from capability pointer */
#define DEV_OP 4 /* Selects control/status register to access */
#define DEV_OP_FUNC_SHIFT 8 /* Function part in OP reg. */
#define DEV_DATA 8 /* Read/write to access reg. selected */
/* Functions */
#define DEVF_BASE_LO 0
#define DEVF_BASE_HI 1
#define DEVF_MAP 2
#define DEVF_CAP 3
#define DEVF_CAP_MAPS_MASK 0x00ff0000
#define DEVF_CAP_MAPS_SHIFT 16
#define DEVF_CAP_DOMS_MASK 0x0000ff00
#define DEVF_CAP_DOMS_SHIFT 8
#define DEVF_CAP_REV_MASK 0x000000ff
#define DEVF_CAP_REV_SHIFT 0
#define DEVF_CR 4
#define DEVF_ERR_STATUS 5
#define DEVF_ERR_ADDR_LO 6
#define DEVF_ERR_ADDR_HI 7
static int dev_devind;
static u8_t dev_capptr;
static u8_t *table;
static int find_dev(int *devindp, u8_t *capaddrp);
static u32_t read_reg(int function, int index);
static void write_reg(int function, int index, u32_t value);
static void init_domain(int index);
static void init_map(unsigned int ix);
static int do_add4pci(const message *m);
static void add_range(phys_bytes busaddr, phys_bytes size);
#if 0
static void del_range(phys_bytes busaddr, phys_bytes size);
static void sef_cb_signal_handler(int signo);
#endif
static void report_exceptions(void);
/* SEF functions and variables. */
static void sef_local_startup(void);
static int sef_cb_init_fresh(int type, sef_init_info_t *info);
int main(void)
{
int r;
message m;
int ipc_status;
/* SEF local startup. */
sef_local_startup();
for(;;)
{
report_exceptions();
r= driver_receive(ANY, &m, &ipc_status);
if (r != OK)
panic("driver_receive failed: %d", r);
if (m.m_type == IOMMU_MAP) {
r= do_add4pci(&m);
m.m_type= r;
ipc_send(m.m_source, &m);
continue;
}
printf("amddev: got message from %d\n", m.m_source);
}
}
/*===========================================================================*
* sef_local_startup *
*===========================================================================*/
static void sef_local_startup()
{
/* Register init callbacks. */
sef_setcb_init_fresh(sef_cb_init_fresh);
sef_setcb_init_lu(sef_cb_init_fresh);
sef_setcb_init_restart(sef_cb_init_fresh);
#if 0
/* Register signal callbacks. */
sef_setcb_signal_handler(sef_cb_signal_handler);
#endif
/* Let SEF perform startup. */
sef_startup();
}
/*===========================================================================*
* sef_cb_init_fresh *
*===========================================================================*/
static int sef_cb_init_fresh(int UNUSED(type), sef_init_info_t *UNUSED(info))
{
/* Initialize the amddev driver. */
int r, n_maps, n_domains, revision;
u16_t flags;
u32_t bits;
printf("amddev: starting\n");
r= find_dev(&dev_devind, &dev_capptr);
if (!r)
return r;
flags= pci_attr_r16(dev_devind, dev_capptr+CAP_SD_INFO);
printf("amddev`init: flags = 0x%x\n", flags);
bits= read_reg(DEVF_CAP, 0);
n_maps= ((bits & DEVF_CAP_MAPS_MASK) >> DEVF_CAP_MAPS_SHIFT);
n_domains= ((bits & DEVF_CAP_DOMS_MASK) >> DEVF_CAP_DOMS_SHIFT);
revision= ((bits & DEVF_CAP_REV_MASK) >> DEVF_CAP_REV_SHIFT);
printf("amddev`init: DEVF_CAP = 0x%x (%d maps, %d domains, rev 0x%x)\n",
bits, n_maps, n_domains, revision);
printf("status = 0x%x, addr-lo = 0x%x, addr-hi = 0x%x\n",
read_reg(DEVF_ERR_STATUS, 0),
read_reg(DEVF_ERR_ADDR_LO, 0),
read_reg(DEVF_ERR_ADDR_HI, 0));
init_domain(0);
init_map(0);
#if 0
init_domain(1);
#endif
write_reg(DEVF_CR, 0, 0x10 | 0x8 | 0x4 | 1);
printf("after write: DEVF_CR: 0x%x\n", read_reg(DEVF_CR, 0));
return(OK);
}
#if 0
/*===========================================================================*
* sef_cb_signal_handler *
*===========================================================================*/
static void sef_cb_signal_handler(int signo)
{
int r;
endpoint_t proc_e;
phys_bytes base, size;
/* Only check for termination signal, ignore anything else. */
if (signo != SIGTERM) return;
for (;;)
{
r= vm_getdma(&proc_e, &base, &size);
if (r == -1)
{
if (errno != -EAGAIN)
{
printf(
"amddev: vm_getdma failed: %d\n",
errno);
}
break;
}
printf(
"amddev: deleting 0x%lx@0x%lx for proc %d\n",
size, base, proc_e);
del_range(base, size);
r= vm_deldma(proc_e, base, size);
if (r == -1)
{
printf("amddev: vm_deldma failed: %d\n",
errno);
break;
}
}
}
#endif
/* Returns 0 if no device found, or 1 if a device is found. */
static int find_dev(devindp, capaddrp)
int *devindp;
u8_t *capaddrp;
{
int r, devind, first;
u8_t capptr, type, next, subtype;
u16_t vid, did, status;
pci_init();
first= 1;
for(;;)
{
if (first)
{
first= 0;
r= pci_first_dev(&devind, &vid, &did);
if (!r)
{
printf("amddev`find_dev: no first dev\n");
return 0;
}
}
else
{
r= pci_next_dev(&devind, &vid, &did);
if (!r)
{
printf("amddev`find_dev: no next dev\n");
return 0;
}
}
printf("amddev`find_dev: got devind %d, vid 0x%x, did 0x%x\n",
devind, vid, did);
/* Check capabilities bit in the device status register */
status= pci_attr_r16(devind, PCI_SR);
if (!(status & PSR_CAPPTR))
continue;
capptr= (pci_attr_r8(devind, PCI_CAPPTR) & PCI_CP_MASK);
while (capptr != 0)
{
type = pci_attr_r8(devind, capptr+CAP_TYPE);
next= (pci_attr_r8(devind, capptr+CAP_NEXT) &
PCI_CP_MASK);
if (type == CAP_T_SECURE_DEV)
{
printf(
"amddev`find_dev: found secure device\n");
subtype= (pci_attr_r8(devind, capptr+
CAP_SD_INFO) & CAP_SD_SUBTYPE_MASK);
if (subtype == CAP_T_SD_DEV)
{
printf("amddev`find_dev: AMD DEV\n");
pci_reserve(devind);
*devindp= devind;
*capaddrp= capptr;
return 1;
}
}
capptr= next;
}
}
return 0;
}
static u32_t read_reg(int function, int index)
{
pci_attr_w32(dev_devind, dev_capptr + DEV_OP, ((function <<
DEV_OP_FUNC_SHIFT) | index));
return pci_attr_r32(dev_devind, dev_capptr + DEV_DATA);
}
static void write_reg(int function, int index, u32_t value)
{
pci_attr_w32(dev_devind, dev_capptr + DEV_OP, ((function <<
DEV_OP_FUNC_SHIFT) | index));
pci_attr_w32(dev_devind, dev_capptr + DEV_DATA, value);
}
static void init_domain(int index)
{
size_t size, memsize;
phys_bytes busaddr;
size= 0x100000 / 8;
table= alloc_contig(size, AC_ALIGN4K, &busaddr);
if (table == NULL)
panic("malloc failed");
if (index == 0)
{
memset(table, 0, size);
memsize= 0x37000 / 8;
printf("memsize = 0x%x / 8\n", memsize*8);
memset(table, 0xff, memsize);
}
else
{
memset(table, 0xff, size);
memset(table, 0x00, size);
}
printf("init_domain: busaddr = 0x%lx\n", busaddr);
write_reg(DEVF_BASE_HI, index, 0);
write_reg(DEVF_BASE_LO, index, busaddr | 3);
printf("after write: DEVF_BASE_LO: 0x%x\n",
read_reg(DEVF_BASE_LO, index));
}
static void init_map(unsigned int ix)
{
u32_t v, dom, busno, unit0, unit1;
dom= 1;
busno= 7;
unit1= 9;
unit0= 9;
v= (dom << 26) | (dom << 20) | (busno << 12) |
(0 << 11) | (unit1 << 6) |
(0 << 5) | (unit0 << 0);
write_reg(DEVF_MAP, ix, v);
printf("after write: DEVF_MAP: 0x%x\n", read_reg(DEVF_MAP, ix));
}
#if 0
static int do_add(message *m)
{
int r;
endpoint_t proc;
vir_bytes start;
size_t size;
phys_bytes busaddr;
proc= m->m_source;
start= m->m2_l1;
size= m->m2_l2;
#if 0
printf("amddev`do_add: got request for 0x%x@0x%x from %d\n",
size, start, proc);
#endif
if (start % PAGE_SIZE)
{
printf("amddev`do_add: bad start 0x%x from proc %d\n",
start, proc);
return EINVAL;
}
if (size % PAGE_SIZE)
{
printf("amddev`do_add: bad size 0x%x from proc %d\n",
size, proc);
return EINVAL;
}
r= sys_umap_remote(proc, SELF, VM_D, (vir_bytes)start, size, &busaddr);
if (r != OK)
{
printf("amddev`do_add: umap failed for 0x%x@0x%x, proc %d\n",
size, start, proc);
return r;
}
add_range(busaddr, size);
}
#endif
static int do_add4pci(const message *m)
{
int r, pci_bus, pci_dev, pci_func;
endpoint_t proc;
vir_bytes start;
size_t size;
phys_bytes busaddr;
proc= m->m_source;
start= m->m2_l1;
size= m->m2_l2;
pci_bus= m->m1_i1;
pci_dev= m->m1_i2;
pci_func= m->m1_i3;
printf(
"amddev`do_add4pci: got request for 0x%x@0x%lx from %d for pci dev %u.%u.%u\n",
size, start, proc, pci_bus, pci_dev, pci_func);
if (start % PAGE_SIZE)
{
printf("amddev`do_add4pci: bad start 0x%lx from proc %d\n",
start, proc);
return EINVAL;
}
if (size % PAGE_SIZE)
{
printf("amddev`do_add4pci: bad size 0x%x from proc %d\n",
size, proc);
return EINVAL;
}
printf("amddev`do_add4pci: should check with PCI\n");
r= sys_umap_remote(proc, SELF, VM_D, (vir_bytes)start, size, &busaddr);
if (r != OK)
{
printf(
"amddev`do_add4pci: umap failed for 0x%x@0x%lx, proc %d: %d\n",
size, start, proc, r);
return r;
}
#if 0
r= vm_adddma(proc, start, size);
if (r != 0)
{
r= -errno;
printf("amddev`do_add4pci: vm_adddma failed for 0x%x@0x%lx, "
"proc %d: %d\n", size, start, proc, r);
return r;
}
#endif
add_range(busaddr, size);
return OK;
}
static void add_range(phys_bytes busaddr, phys_bytes size)
{
phys_bytes o;
#if 0
printf("add_range: mapping 0x%x@0x%x\n", size, busaddr);
#endif
for (o= 0; o<size; o += PAGE_SIZE)
{
u32_t bit= (busaddr+o)/PAGE_SIZE;
table[bit/8] &= ~(1U << (bit % 8));
}
}
#if 0
static void del_range(phys_bytes busaddr, phys_bytes size)
{
phys_bytes o;
#if 0
printf("del_range: mapping 0x%x@0x%x\n", size, busaddr);
#endif
for (o= 0; o<size; o += PAGE_SIZE)
{
u32_t bit= (busaddr+o)/PAGE_SIZE;
table[bit/8] |= (1 << (bit % 8));
}
}
#endif
static void report_exceptions(void)
{
u32_t status;
status= read_reg(DEVF_ERR_STATUS, 0);
if (!(status & 0x80000000))
return;
printf("amddev: status = 0x%x, addr-lo = 0x%x, addr-hi = 0x%x\n",
status, read_reg(DEVF_ERR_ADDR_LO, 0),
read_reg(DEVF_ERR_ADDR_HI, 0));
write_reg(DEVF_ERR_STATUS, 0, 0);
}
|