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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
|
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/mm.h>
#include <linux/dma-mapping.h>
#include <linux/vmalloc.h>
#include <linux/spinlock.h>
#include <linux/ktime.h>
#include <linux/atomic.h>
#include "gsp_emulation.h"
#include "gsp_rpc_client.h"
#include "memory.h"
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Open-IOV");
MODULE_DESCRIPTION("GSP Emulation Unit Tests");
#define TEST_ALLOC_SIZE (4096)
#define TEST_MAP_SIZE (4096)
#define TEST_PIN_PAGES (1)
static struct gsp_emu_context *test_ctx = NULL;
static struct gsp_client *test_client = NULL;
static void *test_shared_mem = NULL;
static dma_addr_t test_shared_dma = 0;
static struct page **test_pages = NULL;
static int test_pages_count = 0;
static int test_seq_id = 1;
static int get_next_seq_id(void)
{
return test_seq_id++;
}
static void init_test_context(void)
{
test_shared_mem = dma_alloc_coherent(NULL, 4096, &test_shared_dma, GFP_KERNEL);
if (!test_shared_mem) {
pr_err("GSP_EMU_TEST: Failed to allocate shared memory\n");
return;
}
memset(test_shared_mem, 0, 4096);
test_pages_count = TEST_ALLOC_SIZE / PAGE_SIZE;
test_pages = kmalloc_array(test_pages_count, sizeof(struct page *), GFP_KERNEL);
if (!test_pages) {
pr_err("GSP_EMU_TEST: Failed to allocate page array\n");
return;
}
for (int i = 0; i < test_pages_count; i++) {
test_pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
if (!test_pages[i]) {
pr_err("GSP_EMU_TEST: Failed to allocate page %d\n", i);
test_pages_count = i;
break;
}
}
test_client = gsp_client_init(NULL);
if (!test_client) {
pr_err("GSP_EMU_TEST: Failed to init gsp_client\n");
return;
}
test_ctx = kzalloc(sizeof(struct gsp_emu_context), GFP_KERNEL);
if (!test_ctx) {
pr_err("GSP_EMU_TEST: Failed to allocate context\n");
return;
}
test_ctx->vgpu = NULL;
test_ctx->bar0_vaddr = test_shared_mem;
test_ctx->doorbell_reg = test_shared_mem + 0x110C00;
test_ctx->shared_mem = test_shared_mem + 0x1000;
test_ctx->rpc_client = test_client;
spin_lock_init(&test_ctx->lock);
test_ctx->running = true;
atomic_set(&test_ctx->processed_rpcs, 0);
atomic_set(&test_ctx->failed_rpcs, 0);
test_ctx->total_latency_ns = 0;
pr_info("GSP_EMU_TEST: Test context initialized\n");
}
static void cleanup_test_context(void)
{
if (test_ctx) {
test_ctx->running = false;
kfree(test_ctx);
test_ctx = NULL;
}
if (test_client) {
gsp_client_fini(test_client);
test_client = NULL;
}
if (test_pages) {
for (int i = 0; i < test_pages_count; i++) {
if (test_pages[i])
__free_page(test_pages[i]);
}
kfree(test_pages);
test_pages = NULL;
}
if (test_shared_mem) {
dma_free_coherent(NULL, 4096, test_shared_mem, test_shared_dma);
test_shared_mem = NULL;
}
pr_info("GSP_EMU_TEST: Test context cleaned up\n");
}
static int test_alloc_free(void)
{
struct gsp_rpc_alloc_request req = {0};
struct gsp_rpc_alloc_response resp = {0};
int ret;
pr_info("GSP_EMU_TEST: Testing ALLOC/FREE...\n");
req.header.type = GSP_RPC_ALLOC;
req.header.seq_id = get_next_seq_id();
req.header.flags = 0;
req.size = TEST_ALLOC_SIZE;
req.alignment = PAGE_SIZE;
req.flags = GSP_ALLOC_FLAG_VRAM;
ret = gsp_rpc_alloc_emulation(test_client, &req, &resp);
if (ret != 0) {
pr_err("GSP_EMU_TEST: ALLOC failed with ret=%d\n", ret);
return -1;
}
if (resp.status != 0) {
pr_err("GSP_EMU_TEST: ALLOC returned status=%d\n", resp.status);
return -1;
}
if (resp.handle == 0) {
pr_err("GSP_EMU_TEST: ALLOC returned zero handle\n");
return -1;
}
pr_info("GSP_EMU_TEST: ALLOC succeeded, handle=0x%llx\n", resp.handle);
struct gsp_rpc_free_request free_req = {0};
struct gsp_rpc_free_response free_resp = {0};
free_req.header.type = GSP_RPC_FREE;
free_req.header.seq_id = get_next_seq_id();
free_req.handle = resp.handle;
ret = gsp_rpc_free_emulation(test_client, &free_req, &free_resp);
if (ret != 0) {
pr_err("GSP_EMU_TEST: FREE failed with ret=%d\n", ret);
return -1;
}
if (free_resp.status != 0) {
pr_err("GSP_EMU_TEST: FREE returned status=%d\n", free_resp.status);
return -1;
}
pr_info("GSP_EMU_TEST: FREE succeeded\n");
return 0;
}
static int test_map_unmap(void)
{
struct gsp_rpc_alloc_request alloc_req = {0};
struct gsp_rpc_alloc_response alloc_resp = {0};
struct gsp_rpc_map_request map_req = {0};
struct gsp_rpc_map_response map_resp = {0};
struct gsp_rpc_unmap_request unmap_req = {0};
struct gsp_rpc_unmap_response unmap_resp = {0};
int ret;
pr_info("GSP_EMU_TEST: Testing MAP/UNMAP...\n");
alloc_req.header.type = GSP_RPC_ALLOC;
alloc_req.header.seq_id = get_next_seq_id();
alloc_req.size = TEST_MAP_SIZE;
alloc_req.alignment = PAGE_SIZE;
alloc_req.flags = GSP_ALLOC_FLAG_VRAM;
ret = gsp_rpc_alloc_emulation(test_client, &alloc_req, &alloc_resp);
if (ret != 0 || alloc_resp.status != 0) {
pr_err("GSP_EMU_TEST: Pre-alloc for MAP failed\n");
return -1;
}
map_req.header.type = GSP_RPC_MAP;
map_req.header.seq_id = get_next_seq_id();
map_req.handle = alloc_resp.handle;
map_req.offset = 0;
map_req.size = TEST_MAP_SIZE;
map_req.gpu_va = 0x100000000ULL;
map_req.flags = GSP_MAP_FLAG_READ | GSP_MAP_FLAG_WRITE;
ret = gsp_rpc_map_emulation(test_client, &map_req, &map_resp);
if (ret != 0) {
pr_err("GSP_EMU_TEST: MAP failed with ret=%d\n", ret);
return -1;
}
if (map_resp.status != 0) {
pr_err("GSP_EMU_TEST: MAP returned status=%d\n", map_resp.status);
return -1;
}
pr_info("GSP_EMU_TEST: MAP succeeded, gpu_va=0x%llx\n", map_resp.gpu_va);
unmap_req.header.type = GSP_RPC_UNMAP;
unmap_req.header.seq_id = get_next_seq_id();
unmap_req.gpu_va = map_resp.gpu_va;
unmap_req.size = TEST_MAP_SIZE;
ret = gsp_rpc_unmap_emulation(test_client, &unmap_req, &unmap_resp);
if (ret != 0) {
pr_err("GSP_EMU_TEST: UNMAP failed with ret=%d\n", ret);
return -1;
}
if (unmap_resp.status != 0) {
pr_err("GSP_EMU_TEST: UNMAP returned status=%d\n", unmap_resp.status);
return -1;
}
pr_info("GSP_EMU_TEST: UNMAP succeeded\n");
struct gsp_rpc_free_request free_req = {0};
struct gsp_rpc_free_response free_resp = {0};
free_req.header.type = GSP_RPC_FREE;
free_req.header.seq_id = get_next_seq_id();
free_req.handle = alloc_resp.handle;
gsp_rpc_free_emulation(test_client, &free_req, &free_resp);
return 0;
}
static int test_pin_unpin(void)
{
struct gsp_rpc_alloc_request alloc_req = {0};
struct gsp_rpc_alloc_response alloc_resp = {0};
struct gsp_rpc_map_request map_req = {0};
struct gsp_rpc_map_response map_resp = {0};
struct gsp_rpc_pin_request pin_req = {0};
struct gsp_rpc_pin_response pin_resp = {0};
struct gsp_rpc_unpin_request unpin_req = {0};
struct gsp_rpc_unpin_response unpin_resp = {0};
int ret;
pr_info("GSP_EMU_TEST: Testing PIN/UNPIN...\n");
alloc_req.header.type = GSP_RPC_ALLOC;
alloc_req.header.seq_id = get_next_seq_id();
alloc_req.size = TEST_ALLOC_SIZE;
alloc_req.alignment = PAGE_SIZE;
alloc_req.flags = GSP_ALLOC_FLAG_VRAM;
ret = gsp_rpc_alloc_emulation(test_client, &alloc_req, &alloc_resp);
if (ret != 0 || alloc_resp.status != 0) {
pr_err("GSP_EMU_TEST: Pre-alloc for PIN failed\n");
return -1;
}
map_req.header.type = GSP_RPC_MAP;
map_req.header.seq_id = get_next_seq_id();
map_req.handle = alloc_resp.handle;
map_req.offset = 0;
map_req.size = TEST_ALLOC_SIZE;
map_req.gpu_va = 0x200000000ULL;
map_req.flags = GSP_MAP_FLAG_READ | GSP_MAP_FLAG_WRITE;
ret = gsp_rpc_map_emulation(test_client, &map_req, &map_resp);
if (ret != 0 || map_resp.status != 0) {
pr_err("GSP_EMU_TEST: Pre-map for PIN failed\n");
return -1;
}
pin_req.header.type = GSP_RPC_PIN;
pin_req.header.seq_id = get_next_seq_id();
pin_req.gpu_va = map_resp.gpu_va;
pin_req.size = TEST_ALLOC_SIZE;
pin_req.flags = GSP_PIN_FLAG_DMA;
ret = gsp_rpc_pin_emulation(test_client, &pin_req, &pin_resp);
if (ret != 0) {
pr_err("GSP_EMU_TEST: PIN failed with ret=%d\n", ret);
return -1;
}
if (pin_resp.status != 0) {
pr_err("GSP_EMU_TEST: PIN returned status=%d\n", pin_resp.status);
return -1;
}
if (pin_resp.dma_addr == 0) {
pr_err("GSP_EMU_TEST: PIN returned zero DMA address\n");
return -1;
}
pr_info("GSP_EMU_TEST: PIN succeeded, dma_addr=0x%llx\n", pin_resp.dma_addr);
unpin_req.header.type = GSP_RPC_UNPIN;
unpin_req.header.seq_id = get_next_seq_id();
unpin_req.gpu_va = map_resp.gpu_va;
unpin_req.size = TEST_ALLOC_SIZE;
ret = gsp_rpc_unpin_emulation(test_client, &unpin_req, &unpin_resp);
if (ret != 0) {
pr_err("GSP_EMU_TEST: UNPIN failed with ret=%d\n", ret);
return -1;
}
if (unpin_resp.status != 0) {
pr_err("GSP_EMU_TEST: UNPIN returned status=%d\n", unpin_resp.status);
return -1;
}
pr_info("GSP_EMU_TEST: UNPIN succeeded\n");
struct gsp_rpc_unmap_request unmap_req = {0};
struct gsp_rpc_unmap_response unmap_resp = {0};
unmap_req.header.type = GSP_RPC_UNMAP;
unmap_req.header.seq_id = get_next_seq_id();
unmap_req.gpu_va = map_resp.gpu_va;
unmap_req.size = TEST_ALLOC_SIZE;
gsp_rpc_unmap_emulation(test_client, &unmap_req, &unmap_resp);
struct gsp_rpc_free_request free_req = {0};
struct gsp_rpc_free_response free_resp = {0};
free_req.header.type = GSP_RPC_FREE;
free_req.header.seq_id = get_next_seq_id();
free_req.handle = alloc_resp.handle;
gsp_rpc_free_emulation(test_client, &free_req, &free_resp);
return 0;
}
static int test_schedule_yield(void)
{
struct gsp_rpc_schedule_request req = {0};
struct gsp_rpc_schedule_response resp = {0};
struct gsp_rpc_yield_request yield_req = {0};
struct gsp_rpc_yield_response yield_resp = {0};
int ret;
pr_info("GSP_EMU_TEST: Testing SCHEDULE/YIELD...\n");
req.header.type = GSP_RPC_SCHEDULE;
req.header.seq_id = get_next_seq_id();
req.work_id = 0x1234;
req.priority = 10;
req.flags = 0;
ret = gsp_rpc_schedule_emulation(test_client, &req, &resp);
if (ret != 0) {
pr_err("GSP_EMU_TEST: SCHEDULE failed with ret=%d\n", ret);
return -1;
}
if (resp.status != 0) {
pr_err("GSP_EMU_TEST: SCHEDULE returned status=%d\n", resp.status);
return -1;
}
pr_info("GSP_EMU_TEST: SCHEDULE succeeded\n");
yield_req.header.type = GSP_RPC_YIELD;
yield_req.header.seq_id = get_next_seq_id();
yield_req.work_id = 0x1234;
ret = gsp_rpc_yield_emulation(test_client, &yield_req, &yield_resp);
if (ret != 0) {
pr_err("GSP_EMU_TEST: YIELD failed with ret=%d\n", ret);
return -1;
}
if (yield_resp.status != 0) {
pr_err("GSP_EMU_TEST: YIELD returned status=%d\n", yield_resp.status);
return -1;
}
pr_info("GSP_EMU_TEST: YIELD succeeded\n");
return 0;
}
static int test_bar_trap(void)
{
struct gsp_rpc_bar_trap_request req = {0};
struct gsp_rpc_bar_trap_response resp = {0};
int ret;
pr_info("GSP_EMU_TEST: Testing BAR_TRAP...\n");
req.header.type = GSP_RPC_BAR_TRAP;
req.header.seq_id = get_next_seq_id();
req.bar_index = 0;
req.offset = 0x1000;
req.size = 4;
req.is_write = false;
req.value = 0;
ret = gsp_rpc_bar_trap_emulation(test_client, &req, &resp);
if (ret != 0) {
pr_err("GSP_EMU_TEST: BAR_TRAP read failed with ret=%d\n", ret);
return -1;
}
if (resp.status != 0) {
pr_err("GSP_EMU_TEST: BAR_TRAP read returned status=%d\n", resp.status);
return -1;
}
pr_info("GSP_EMU_TEST: BAR_TRAP read succeeded, value=0x%llx\n", resp.value);
req.header.seq_id = get_next_seq_id();
req.is_write = true;
req.value = 0xDEADBEEF;
ret = gsp_rpc_bar_trap_emulation(test_client, &req, &resp);
if (ret != 0) {
pr_err("GSP_EMU_TEST: BAR_TRAP write failed with ret=%d\n", ret);
return -1;
}
if (resp.status != 0) {
pr_err("GSP_EMU_TEST: BAR_TRAP write returned status=%d\n", resp.status);
return -1;
}
pr_info("GSP_EMU_TEST: BAR_TRAP write succeeded\n");
req.header.seq_id = get_next_seq_id();
req.is_write = false;
req.value = 0;
ret = gsp_rpc_bar_trap_emulation(test_client, &req, &resp);
if (ret != 0) {
pr_err("GSP_EMU_TEST: BAR_TRAP read-back failed with ret=%d\n", ret);
return -1;
}
if (resp.value != 0xDEADBEEF) {
pr_err("GSP_EMU_TEST: BAR_TRAP read-back mismatch: expected 0xDEADBEEF, got 0x%llx\n", resp.value);
return -1;
}
pr_info("GSP_EMU_TEST: BAR_TRAP read-back verified\n");
return 0;
}
static int test_notify(void)
{
struct gsp_rpc_notify_request req = {0};
struct gsp_rpc_notify_response resp = {0};
int ret;
pr_info("GSP_EMU_TEST: Testing NOTIFY...\n");
req.header.type = GSP_RPC_NOTIFY;
req.header.seq_id = get_next_seq_id();
req.event_type = GSP_NOTIFY_EVENT_COMPLETION;
req.flags = GSP_NOTIFY_FLAG_ENABLE;
ret = gsp_rpc_notify_emulation(test_client, &req, &resp);
if (ret != 0) {
pr_err("GSP_EMU_TEST: NOTIFY register failed with ret=%d\n", ret);
return -1;
}
if (resp.status != 0) {
pr_err("GSP_EMU_TEST: NOTIFY register returned status=%d\n", resp.status);
return -1;
}
pr_info("GSP_EMU_TEST: NOTIFY register succeeded\n");
req.header.seq_id = get_next_seq_id();
req.flags = GSP_NOTIFY_FLAG_DISABLE;
ret = gsp_rpc_notify_emulation(test_client, &req, &resp);
if (ret != 0) {
pr_err("GSP_EMU_TEST: NOTIFY unregister failed with ret=%d\n", ret);
return -1;
}
if (resp.status != 0) {
pr_err("GSP_EMU_TEST: NOTIFY unregister returned status=%d\n", resp.status);
return -1;
}
pr_info("GSP_EMU_TEST: NOTIFY unregister succeeded\n");
return 0;
}
static int test_migrate(void)
{
struct gsp_rpc_migrate_request req = {0};
struct gsp_rpc_migrate_response resp = {0};
int ret;
pr_info("GSP_EMU_TEST: Testing MIGRATE...\n");
req.header.type = GSP_RPC_MIGRATE;
req.header.seq_id = get_next_seq_id();
req.phase = GSP_MIGRATE_QUERY;
req.flags = 0;
req.data = 0;
req.data_size = 0;
ret = gsp_rpc_migrate_emulation(test_client, &req, &resp);
if (ret != 0) {
pr_err("GSP_EMU_TEST: MIGRATE QUERY failed with ret=%d\n", ret);
return -1;
}
if (resp.status != 0) {
pr_err("GSP_EMU_TEST: MIGRATE QUERY returned status=%d\n", resp.status);
return -1;
}
pr_info("GSP_EMU_TEST: MIGRATE QUERY succeeded\n");
req.header.seq_id = get_next_seq_id();
req.phase = GSP_MIGRATE_PREPARE;
ret = gsp_rpc_migrate_emulation(test_client, &req, &resp);
if (ret != 0) {
pr_err("GSP_EMU_TEST: MIGRATE PREPARE failed with ret=%d\n", ret);
return -1;
}
req.header.seq_id = get_next_seq_id();
req.phase = GSP_MIGRATE_TRANSFER;
ret = gsp_rpc_migrate_emulation(test_client, &req, &resp);
if (ret != 0) {
pr_err("GSP_EMU_TEST: MIGRATE TRANSFER failed with ret=%d\n", ret);
return -1;
}
req.header.seq_id = get_next_seq_id();
req.phase = GSP_MIGRATE_COMMIT;
ret = gsp_rpc_migrate_emulation(test_client, &req, &resp);
if (ret != 0) {
pr_err("GSP_EMU_TEST: MIGRATE COMMIT failed with ret=%d\n", ret);
return -1;
}
req.header.seq_id = get_next_seq_id();
req.phase = GSP_MIGRATE_CLEANUP;
ret = gsp_rpc_migrate_emulation(test_client, &req, &resp);
if (ret != 0) {
pr_err("GSP_EMU_TEST: MIGRATE CLEANUP failed with ret=%d\n", ret);
return -1;
}
pr_info("GSP_EMU_TEST: MIGRATE 4-phase completed\n");
return 0;
}
static int test_debug(void)
{
struct gsp_rpc_debug_request req = {0};
struct gsp_rpc_debug_response resp = {0};
int ret;
pr_info("GSP_EMU_TEST: Testing DEBUG...\n");
req.header.type = GSP_RPC_DEBUG;
req.header.seq_id = get_next_seq_id();
req.sub_command = GSP_DEBUG_GET_VERSION;
req.data = 0;
req.data_size = 0;
ret = gsp_rpc_debug_emulation(test_client, &req, &resp);
if (ret != 0) {
pr_err("GSP_EMU_TEST: DEBUG failed with ret=%d\n", ret);
return -1;
}
if (resp.status != 0) {
pr_err("GSP_EMU_TEST: DEBUG returned status=%d\n", resp.status);
return -1;
}
pr_info("GSP_EMU_TEST: DEBUG succeeded, version=0x%llx\n", resp.data);
return 0;
}
static int test_concurrent_rpcs(void)
{
struct gsp_rpc_alloc_request req = {0};
struct gsp_rpc_alloc_response resp = {0};
int ret, passed = 0, failed = 0;
pr_info("GSP_EMU_TEST: Testing concurrent RPCs...\n");
for (int i = 0; i < 10; i++) {
req.header.type = GSP_RPC_ALLOC;
req.header.seq_id = get_next_seq_id();
req.size = 4096;
req.alignment = PAGE_SIZE;
req.flags = GSP_ALLOC_FLAG_VRAM;
ret = gsp_rpc_alloc_emulation(test_client, &req, &resp);
if (ret == 0 && resp.status == 0 && resp.handle != 0) {
passed++;
struct gsp_rpc_free_request free_req = {0};
struct gsp_rpc_free_response free_resp = {0};
free_req.header.type = GSP_RPC_FREE;
free_req.header.seq_id = get_next_seq_id();
free_req.handle = resp.handle;
gsp_rpc_free_emulation(test_client, &free_req, &free_resp);
} else {
failed++;
}
}
pr_info("GSP_EMU_TEST: Concurrent test: %d passed, %d failed\n", passed, failed);
return (failed == 0) ? 0 : -1;
}
static int test_error_handling(void)
{
struct gsp_rpc_alloc_request req = {0};
struct gsp_rpc_alloc_response resp = {0};
int ret;
pr_info("GSP_EMU_TEST: Testing error handling...\n");
req.header.type = GSP_RPC_ALLOC;
req.header.seq_id = 0;
req.size = 4096;
req.alignment = PAGE_SIZE;
req.flags = GSP_ALLOC_FLAG_VRAM;
ret = gsp_rpc_alloc_emulation(test_client, &req, &resp);
if (ret == 0 && resp.status != 0) {
pr_info("GSP_EMU_TEST: Zero seq_id correctly rejected\n");
} else {
pr_err("GSP_EMU_TEST: Zero seq_id not handled correctly\n");
return -1;
}
req.header.seq_id = get_next_seq_id();
req.size = 0;
ret = gsp_rpc_alloc_emulation(test_client, &req, &resp);
if (ret == 0 && resp.status != 0) {
pr_info("GSP_EMU_TEST: Zero size correctly rejected\n");
} else {
pr_err("GSP_EMU_TEST: Zero size not handled correctly\n");
return -1;
}
req.header.seq_id = get_next_seq_id();
req.size = 4096;
req.alignment = 123;
ret = gsp_rpc_alloc_emulation(test_client, &req, &resp);
if (ret == 0 && resp.status != 0) {
pr_info("GSP_EMU_TEST: Invalid alignment correctly rejected\n");
} else {
pr_err("GSP_EMU_TEST: Invalid alignment not handled correctly\n");
return -1;
}
return 0;
}
static int run_all_tests(void)
{
int failed = 0;
init_test_context();
if (!test_ctx || !test_client) {
pr_err("GSP_EMU_TEST: Failed to initialize test context\n");
return -1;
}
if (test_alloc_free() != 0) failed++;
if (test_map_unmap() != 0) failed++;
if (test_pin_unpin() != 0) failed++;
if (test_schedule_yield() != 0) failed++;
if (test_bar_trap() != 0) failed++;
if (test_notify() != 0) failed++;
if (test_migrate() != 0) failed++;
if (test_debug() != 0) failed++;
if (test_concurrent_rpcs() != 0) failed++;
if (test_error_handling() != 0) failed++;
cleanup_test_context();
if (failed == 0) {
pr_info("GSP_EMU_TEST: ALL TESTS PASSED\n");
return 0;
} else {
pr_err("GSP_EMU_TEST: %d TEST(S) FAILED\n", failed);
return -1;
}
}
static int __init gsp_emulation_test_init(void)
{
pr_info("GSP_EMU_TEST: Starting GSP emulation unit tests...\n");
return run_all_tests();
}
static void __exit gsp_emulation_test_exit(void)
{
pr_info("GSP_EMU_TEST: Module unloaded\n");
}
module_init(gsp_emulation_test_init);
module_exit(gsp_emulation_test_exit);
|