forked from ovis-hpc/gpcd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpcd_lib.c
executable file
·579 lines (444 loc) · 10.1 KB
/
gpcd_lib.c
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/*
* The Gemini Performance Counter driver.A
* Userspace interface library
*
* Copyright 2011 Cray Inc. All Rights Reserved.
*
*/
/* config file generated by top-level configure.in,
* defines CRAY_GNI_ARCH_GEM and CRAY_GNI_ARCH_ARI. */
//#include <cray-gni-config.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include "gpcd_lib.h"
#define CRAY_GNI_ARCH_ARI 1
#if CRAY_GNI_ARCH_GEM
#include "gpcd_lib_gem_names.h"
#elif CRAY_GNI_ARCH_ARI
#include "gpcd_lib_ari_names.h"
#else
#error Only Gemini and Aries are supported
#endif
int gpcd_print_valid_nic_mmrs()
{
const gpcd_mmr_desc_t *mmrd;
printf("Valid NIC MMRS for GPCD:\n");
for ( mmrd = &valid_mmrs[0];
mmrd->addr != 0;
mmrd++) {
printf("%-49s 0x%lx\n", mmrd->name, mmrd->addr);
}
return 0;
}
int gpcd_print_valid_tile_filtering_mmrs()
{
const gpcd_mmr_desc_t *mmrd;
for ( mmrd = &valid_tile_fpc_mmrs[0];
mmrd->addr != 0;
mmrd++) {
printf("%-49s 0x%lx\n", mmrd->name, mmrd->addr);
}
return 0;
}
int gpcd_print_valid_tile_static_mmrs()
{
const gpcd_mmr_desc_t *mmrd;
for( mmrd = valid_tile_spc_mmrs;
mmrd->addr != 0;
mmrd++){
printf("%-49s 0x%lx\n", mmrd->name, mmrd->addr);
}
return 0;
}
int gpcd_print_valid_tile_mmrs()
{
gpcd_print_valid_tile_filtering_mmrs();
printf("----------------\n");
gpcd_print_valid_tile_static_mmrs();
return 0;
}
int gpcd_read_mmr_val_byname(char* mmr_name, uint64_t *val, uint32_t nic_addr)
{
const gpcd_mmr_desc_t *mmrd;
/*uint64_t addr, retval;*/
int error;
if ((!mmr_name) || (!val)){
printf("gpcd_read_mmr_val_byname 1\n");
return -EINVAL;
}
error = strncmp(mmr_name, "GM_", 3);
if (error != 0){
printf("gpcd_read_mmr_val_byname 2\n");
return -EINVAL;
}
for(mmrd = &valid_mmrs[0]; mmrd->addr != 0; mmrd++){
error = strncmp(mmr_name, mmrd->name, 40);
if (!error){
break;
}
}
if (mmrd->addr == 0){
printf("gpcd_read_mmr_val_byname 3\n");
return -ENOENT;
}
error = gpcd_read_mmr_val(mmrd, val, nic_addr);
if(error){
printf("gpcd_read_mmr_val_byname 4\n");
return error;
}
return 0;
}
int gpcd_read_mmr_val(const gpcd_mmr_desc_t *mmrd, uint64_t *val,
uint32_t nic_addr)
{
int error, fd, req;
gpcd_read_args_t req_data;
uint64_t ret_data = 0;
fd = open(GPCD_DEV_PATH, O_RDWR);
if (fd < 1){
printf("gpcd_read_mmr_val open failed.\n");
return -ENOENT;
}
req = GPC_IOC_READ_REG;
req_data.nic_addr = nic_addr;
req_data.num_mmrs = 1;
req_data.mmr_addrs = (uint64_t*)&mmrd->addr;
req_data.mmr_data = &ret_data;
error = ioctl(fd, req, &req_data);
if (error == -1){
error = 0 - errno;
close(fd);
printf("gpcd_read_mmr_val ioctl error %d\n", error);
return error;
}
*val = ret_data;
close(fd);
return 0;
}
int gpcd_write_mmr_val_byname(char* mmr_name, uint64_t *val, uint32_t nic_addr)
{
const gpcd_mmr_desc_t *mmrd;
int error;
if ((!mmr_name) || (!val)){
printf("gpcd_write_mmr_val_byname 1\n");
return -EINVAL;
}
error = strncmp(mmr_name, "GM_", 3);
if (error != 0){
printf("gpcd_write_mmr_val_byname 2\n");
return -EINVAL;
}
for (mmrd = &valid_mmrs[0]; mmrd->addr != 0; mmrd++){
error = strncmp(mmr_name, mmrd->name, 40);
if (!error){
break;
}
}
if (mmrd->addr == 0){
printf("gpcd_write_mmr_val_byname 3\n");
return -ENOENT;
}
error = gpcd_write_mmr_val(mmrd, val, nic_addr);
if (error){
printf("gpcd_write_mmr_val_byname 4\n");
return error;
}
return 0;
}
int gpcd_write_mmr_val(const gpcd_mmr_desc_t *mmrd, uint64_t *val,
uint32_t nic_addr)
{
int error, fd;
gpcd_write_args_t req_data;
uint64_t data = *val;
fd = open(GPCD_DEV_PATH, O_RDWR);
if (fd < 1){
printf("gpcd_write_mmr_val open failed.\n");
return -ENOENT;
}
req_data.mmr_data = &data;
req_data.nic_addr = nic_addr;
req_data.num_mmrs = 1;
req_data.mmr_addrs = (uint64_t*)&mmrd->addr;
error = ioctl(fd, GPC_IOC_WRITE_REG, &req_data);
if (error == -1){
error = 0 - errno;
close(fd);
printf("gpcd_write_mmr_val ioctl error %d\n", error);
return error;
}
close(fd);
return 0;
}
int gpcd_add_perms_entry(uint64_t job_id,
uint64_t perms)
{
int error, fd;
gpcd_perms_t pdata;
pdata.perms = perms;
pdata.job_id = job_id;
fd = open(GPCD_DEV_PATH, O_RDWR);
if (fd < 1){
printf("gpcd_add_perms_entry open failed.\n");
return -ENOENT;
}
error = ioctl(fd, GPC_IOC_SET_PERMS, &pdata);
if (error == -1){
error = 0 - errno;
close(fd);
printf("gpcd_add_perms_entry ioctl error %d\n", error);
return error;
}
close(fd);
return 0;
}
/*must be root to disable perms checking.*/
int gpcd_disable_perms()
{
int error, fd;
fd = open(GPCD_DEV_PATH, O_RDWR);
if (fd < 1){
printf("gpcd_disable_perms open failed.\n");
return -ENOENT;
}
error = ioctl(fd, GPC_IOC_IGNORE_PERMS, NULL);
if (error == -1){
error = 0 - errno;
close(fd);
printf("gpcd_disable_perms ioctl error %d\n", error);
return error;
}
close(fd);
return 0;
}
/* i == 1 : verbose on
* i == -1: verbose off */
int gpcd_verbose_debug(int i)
{
int error, fd;
fd = open(GPCD_DEV_PATH, O_RDWR);
if (fd < 1){
printf("gpcd_verbose_debug open failed.\n");
return -ENOENT;
}
error = ioctl(fd, GPC_IOC_VERBOSE, &i);
if (error == -1){
error = 0 - errno;
close(fd);
printf("gpcd_verbose_debug ioctl error %d\n", error);
return error;
}
close(fd);
return 0;
}
gpcd_context_t* gpcd_create_context(){
gpcd_context_t *gcp;
gcp = (gpcd_context_t*)malloc(sizeof(gpcd_context_t));
if(gcp){
gcp->list = NULL;
gcp->count = 0;
gcp->nic_addr = 0;
}
return gcp;
}
void gpcd_context_set_nic_addr(gpcd_context_t *cp, uint32_t nic_addr){
cp->nic_addr = nic_addr;
}
int gpcd_remove_context(gpcd_context_t *context)
{
gpcd_mmr_list_t *listp;
if (!context)
return -EINVAL;
while(context->list){
listp = context->list;
context->list = listp->next;
context->count--;
free(listp);
}
if(context->count > 0){
free(context);
return -E2BIG;
}
free(context);
return 0;
}
const gpcd_mmr_desc_t *gpcd_lookup_mmr_byname(char* mmr_name)
{
const gpcd_mmr_desc_t *mmrp;
int error;
if (!mmr_name)
return NULL;
if (!gpcd_mmr_name_sane(mmr_name))
return NULL;
for (mmrp = &valid_mmrs[0]; mmrp->addr != 0; mmrp++){
error = strncmp(mmr_name, mmrp->name, 40);
if(error == 0)
return mmrp;
}
for (mmrp = &valid_tile_fpc_mmrs[0]; mmrp->addr != 0; mmrp++){
error = strncmp(mmr_name, mmrp->name, 40);
if(error == 0)
return mmrp;
}
for (mmrp = &valid_tile_spc_mmrs[0]; mmrp->addr != 0; mmrp++){
error = strncmp(mmr_name, mmrp->name, 40);
if(error == 0)
return mmrp;
}
for (mmrp = &valid_tile_fpc_setup_mmrs[0]; mmrp->addr != 0; mmrp++){
error = strncmp(mmr_name, mmrp->name, 40);
if(error == 0)
return mmrp;
}
return NULL;
}
int gpcd_context_add_mmr(gpcd_context_t *context, gpcd_mmr_desc_t *item)
{
gpcd_mmr_list_t *listp;
if ((!context) || (!item))
return -EINVAL;
if (context->count >= 1024)
return -E2BIG;
listp = (gpcd_mmr_list_t*)malloc(sizeof(gpcd_mmr_list_t));
if (!listp)
return -ENOMEM;
listp->item = item;
listp->next = context->list;
listp->value = 0;
context->list = listp;
context->count++;
return 0;
}
gpcd_mmr_list_t* gpcd_context_find_list_byname(
gpcd_context_t *context,
char* mmr_name)
{
gpcd_mmr_list_t *listp;
int error;
for (listp = context->list; listp != NULL; listp = listp->next){
error = strncmp(mmr_name, listp->item->name, 40);
if (error == 0) {
return listp;
}
}
return NULL;
}
int gpcd_context_remove_mmr(gpcd_context_t *context, gpcd_mmr_desc_t *item)
{
gpcd_mmr_list_t *listp1, *listp2;
int i = 1;
if ((!context) || (!item))
return -EINVAL;
if (!context->list)
return -ENOENT;
listp1 = context->list;
if (listp1->item == item){
context->list = listp1->next;
context->count--;
free(listp1);
return 0;
}
while(listp1->next != NULL){
if (i > context->count)
return -E2BIG;
listp2 = listp1;
listp1 = listp1->next;
i++;
if (listp1->item == item){
listp2->next = listp1->next;
context->count--;
free(listp1);
return 0;
}
}
return -ENOENT;
}
int gpcd_context_print(gpcd_context_t *context)
{
gpcd_mmr_list_t *listp;
int i=0;
if (!context)
return -EINVAL;
if (!context->list)
return -ENOENT;
for(listp = context->list; listp; listp = listp->next)
printf("GPCD Context print %3d : %s : %lu\n",
i++, listp->item->name, listp->value);
return 0;
}
int gpcd_clone_context(
gpcd_context_t *in,
gpcd_context_t *out)
{
gpcd_mmr_list_t *mmrl;
int error;
out = (gpcd_context_t*)malloc(sizeof(gpcd_context_t));
if (!out)
return -ENOMEM;
out->count = 0;
out->list = NULL;
out->nic_addr = in->nic_addr;
for(mmrl = in->list; mmrl; mmrl = mmrl->next){
error = gpcd_context_add_mmr(out, mmrl->item);
if (error){
gpcd_remove_context(out);
return -ENOMEM;
}
}
return 0;
}
int gpcd_context_read_mmr_vals(gpcd_context_t *context)
{
int i = 0, n = 0, fd, error;
gpcd_read_args_t req_data;
uint64_t *ret_data;
uint64_t *addr_list = NULL;
gpcd_mmr_list_t *listp1, *listp2;
fd = open(GPCD_DEV_PATH, O_RDWR);
if (fd < 1)
return -ENOENT;
ret_data = (uint64_t*)malloc(context->count * sizeof(uint64_t));
if (!ret_data){
error = -ENOMEM;
goto out;
}
bzero((void*)ret_data,context->count * sizeof(uint64_t));
addr_list = (uint64_t*)malloc(context->count * sizeof(uint64_t));
if (!addr_list){
error = -ENOMEM;
goto out;
}
bzero((void*)addr_list,context->count * sizeof(uint64_t));
req_data.mmr_data = ret_data;
req_data.mmr_addrs = addr_list;
req_data.num_mmrs = 0;
req_data.nic_addr = context->nic_addr;
listp2 = context->list;
for(listp1 = context->list; listp1 != NULL; listp1 = listp1->next){
req_data.mmr_addrs[n] = listp1->item->addr;
req_data.num_mmrs = ++n;
}
error = ioctl(fd, GPC_IOC_READ_REG, &req_data);
if (error == -1){
error = 0 - errno;
goto out;
}
i = 0;
for (listp2 = context->list; listp2 != NULL; listp2 = listp2->next){
listp2->value = ret_data[i++];
}
error = 0;
out:
if (ret_data) free(ret_data);
if (addr_list) free(addr_list);
close(fd);
return error;
}