-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdxgadapter.c
More file actions
1367 lines (1183 loc) · 36.7 KB
/
dxgadapter.c
File metadata and controls
1367 lines (1183 loc) · 36.7 KB
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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2022, Microsoft Corporation.
*
* Author:
* Iouri Tarassov <iourit@linux.microsoft.com>
*
* Dxgkrnl Graphics Driver
* Implementation of dxgadapter and its objects
*
*/
#include <linux/module.h>
#include "hyperv.h"
#include <linux/pagemap.h>
#include <linux/eventfd.h>
#include "dxgkrnl.h"
#undef dev_fmt
#define dev_fmt(fmt) "dxgk: " fmt
int dxgadapter_set_vmbus(struct dxgadapter *adapter, struct hv_device *hdev)
{
int ret;
guid_to_luid(&hdev->channel->offermsg.offer.if_instance,
&adapter->luid);
DXG_TRACE("%x:%x %p %pUb",
adapter->luid.b, adapter->luid.a, hdev->channel,
&hdev->channel->offermsg.offer.if_instance);
ret = dxgvmbuschannel_init(&adapter->channel, hdev);
if (ret)
goto cleanup;
adapter->channel.adapter = adapter;
adapter->hv_dev = hdev;
ret = dxgvmb_send_open_adapter(adapter);
if (ret < 0) {
DXG_ERR("dxgvmb_send_open_adapter failed: %d", ret);
goto cleanup;
}
ret = dxgvmb_send_get_internal_adapter_info(adapter);
cleanup:
if (ret)
DXG_ERR("Failed to set vmbus: %d", ret);
return ret;
}
void dxgadapter_start(struct dxgadapter *adapter)
{
struct dxgvgpuchannel *ch = NULL;
struct dxgvgpuchannel *entry;
int ret;
struct dxgglobal *dxgglobal = dxggbl();
DXG_TRACE("%x-%x", adapter->luid.a, adapter->luid.b);
/* Find the corresponding vGPU vm bus channel */
list_for_each_entry(entry, &dxgglobal->vgpu_ch_list_head,
vgpu_ch_list_entry) {
if (memcmp(&adapter->luid,
&entry->adapter_luid,
sizeof(struct winluid)) == 0) {
ch = entry;
break;
}
}
if (ch == NULL) {
DXG_TRACE("vGPU chanel is not ready");
return;
}
/* The global channel is initialized when the first adapter starts */
if (!dxgglobal->global_channel_initialized) {
ret = dxgglobal_init_global_channel();
if (ret) {
dxgglobal_destroy_global_channel();
return;
}
dxgglobal->global_channel_initialized = true;
}
/* Initialize vGPU vm bus channel */
ret = dxgadapter_set_vmbus(adapter, ch->hdev);
if (ret) {
DXG_ERR("Failed to start adapter %p", adapter);
adapter->adapter_state = DXGADAPTER_STATE_STOPPED;
return;
}
adapter->adapter_state = DXGADAPTER_STATE_ACTIVE;
DXG_TRACE("Adapter started %p", adapter);
}
void dxgadapter_stop(struct dxgadapter *adapter)
{
struct dxgprocess_adapter *entry;
bool adapter_stopped = false;
down_write(&adapter->core_lock);
if (!adapter->stopping_adapter)
adapter->stopping_adapter = true;
else
adapter_stopped = true;
up_write(&adapter->core_lock);
if (adapter_stopped)
return;
dxgglobal_acquire_process_adapter_lock();
list_for_each_entry(entry, &adapter->adapter_process_list_head,
adapter_process_list_entry) {
dxgprocess_adapter_stop(entry);
}
dxgglobal_release_process_adapter_lock();
if (dxgadapter_acquire_lock_exclusive(adapter) == 0) {
dxgvmb_send_close_adapter(adapter);
dxgadapter_release_lock_exclusive(adapter);
}
dxgvmbuschannel_destroy(&adapter->channel);
adapter->adapter_state = DXGADAPTER_STATE_STOPPED;
}
void dxgadapter_release(struct kref *refcount)
{
struct dxgadapter *adapter;
adapter = container_of(refcount, struct dxgadapter, adapter_kref);
DXG_TRACE("Destroying adapter: %px", adapter);
kfree(adapter);
}
bool dxgadapter_is_active(struct dxgadapter *adapter)
{
return adapter->adapter_state == DXGADAPTER_STATE_ACTIVE;
}
/* Protected by dxgglobal_acquire_process_adapter_lock */
void dxgadapter_add_process(struct dxgadapter *adapter,
struct dxgprocess_adapter *process_info)
{
DXG_TRACE("%p %p", adapter, process_info);
list_add_tail(&process_info->adapter_process_list_entry,
&adapter->adapter_process_list_head);
}
void dxgadapter_remove_process(struct dxgprocess_adapter *process_info)
{
DXG_TRACE("%p %p", process_info->adapter, process_info);
list_del(&process_info->adapter_process_list_entry);
}
void dxgadapter_remove_shared_resource(struct dxgadapter *adapter,
struct dxgsharedresource *object)
{
down_write(&adapter->shared_resource_list_lock);
if (object->shared_resource_list_entry.next) {
list_del(&object->shared_resource_list_entry);
object->shared_resource_list_entry.next = NULL;
}
up_write(&adapter->shared_resource_list_lock);
}
void dxgadapter_add_shared_syncobj(struct dxgadapter *adapter,
struct dxgsharedsyncobject *object)
{
down_write(&adapter->shared_resource_list_lock);
list_add_tail(&object->adapter_shared_syncobj_list_entry,
&adapter->adapter_shared_syncobj_list_head);
up_write(&adapter->shared_resource_list_lock);
}
void dxgadapter_remove_shared_syncobj(struct dxgadapter *adapter,
struct dxgsharedsyncobject *object)
{
down_write(&adapter->shared_resource_list_lock);
if (object->adapter_shared_syncobj_list_entry.next) {
list_del(&object->adapter_shared_syncobj_list_entry);
object->adapter_shared_syncobj_list_entry.next = NULL;
}
up_write(&adapter->shared_resource_list_lock);
}
void dxgadapter_add_syncobj(struct dxgadapter *adapter,
struct dxgsyncobject *object)
{
down_write(&adapter->shared_resource_list_lock);
list_add_tail(&object->syncobj_list_entry, &adapter->syncobj_list_head);
up_write(&adapter->shared_resource_list_lock);
}
void dxgadapter_remove_syncobj(struct dxgsyncobject *object)
{
down_write(&object->adapter->shared_resource_list_lock);
if (object->syncobj_list_entry.next) {
list_del(&object->syncobj_list_entry);
object->syncobj_list_entry.next = NULL;
}
up_write(&object->adapter->shared_resource_list_lock);
}
int dxgadapter_acquire_lock_exclusive(struct dxgadapter *adapter)
{
down_write(&adapter->core_lock);
if (adapter->adapter_state != DXGADAPTER_STATE_ACTIVE) {
dxgadapter_release_lock_exclusive(adapter);
return -ENODEV;
}
return 0;
}
void dxgadapter_acquire_lock_forced(struct dxgadapter *adapter)
{
down_write(&adapter->core_lock);
}
void dxgadapter_release_lock_exclusive(struct dxgadapter *adapter)
{
up_write(&adapter->core_lock);
}
int dxgadapter_acquire_lock_shared(struct dxgadapter *adapter)
{
down_read(&adapter->core_lock);
if (adapter->adapter_state == DXGADAPTER_STATE_ACTIVE)
return 0;
dxgadapter_release_lock_shared(adapter);
return -ENODEV;
}
void dxgadapter_release_lock_shared(struct dxgadapter *adapter)
{
up_read(&adapter->core_lock);
}
struct dxgdevice *dxgdevice_create(struct dxgadapter *adapter,
struct dxgprocess *process)
{
struct dxgdevice *device;
int ret;
device = kzalloc(sizeof(struct dxgdevice), GFP_KERNEL);
if (device) {
kref_init(&device->device_kref);
device->adapter = adapter;
device->process = process;
kref_get(&adapter->adapter_kref);
INIT_LIST_HEAD(&device->context_list_head);
INIT_LIST_HEAD(&device->alloc_list_head);
INIT_LIST_HEAD(&device->resource_list_head);
init_rwsem(&device->device_lock);
init_rwsem(&device->context_list_lock);
init_rwsem(&device->alloc_list_lock);
INIT_LIST_HEAD(&device->pqueue_list_head);
INIT_LIST_HEAD(&device->syncobj_list_head);
device->object_state = DXGOBJECTSTATE_CREATED;
device->execution_state = _D3DKMT_DEVICEEXECUTION_ACTIVE;
ret = dxgprocess_adapter_add_device(process, adapter, device);
if (ret < 0) {
kref_put(&device->device_kref, dxgdevice_release);
device = NULL;
} else {
DXG_TRACE("dxgdevice created: %px", device);
}
}
return device;
}
void dxgdevice_stop(struct dxgdevice *device)
{
struct dxgallocation *alloc;
struct dxgpagingqueue *pqueue;
struct dxgsyncobject *syncobj;
DXG_TRACE("Stopping device: %p", device);
dxgdevice_acquire_alloc_list_lock(device);
list_for_each_entry(alloc, &device->alloc_list_head, alloc_list_entry) {
dxgallocation_stop(alloc);
}
dxgdevice_release_alloc_list_lock(device);
hmgrtable_lock(&device->process->handle_table, DXGLOCK_EXCL);
list_for_each_entry(pqueue, &device->pqueue_list_head,
pqueue_list_entry) {
dxgpagingqueue_stop(pqueue);
}
list_for_each_entry(syncobj, &device->syncobj_list_head,
syncobj_list_entry) {
dxgsyncobject_stop(syncobj);
}
hmgrtable_unlock(&device->process->handle_table, DXGLOCK_EXCL);
DXG_TRACE("Device stopped: %p", device);
}
void dxgdevice_mark_destroyed(struct dxgdevice *device)
{
down_write(&device->device_lock);
device->object_state = DXGOBJECTSTATE_DESTROYED;
up_write(&device->device_lock);
}
void dxgdevice_destroy(struct dxgdevice *device)
{
struct dxgprocess *process = device->process;
struct dxgadapter *adapter = device->adapter;
struct d3dkmthandle device_handle = {};
DXG_TRACE("Destroying device: %p", device);
down_write(&device->device_lock);
if (device->object_state != DXGOBJECTSTATE_ACTIVE)
goto cleanup;
device->object_state = DXGOBJECTSTATE_DESTROYED;
dxgdevice_stop(device);
dxgdevice_acquire_alloc_list_lock(device);
while (!list_empty(&device->syncobj_list_head)) {
struct dxgsyncobject *syncobj =
list_first_entry(&device->syncobj_list_head,
struct dxgsyncobject,
syncobj_list_entry);
list_del(&syncobj->syncobj_list_entry);
syncobj->syncobj_list_entry.next = NULL;
dxgdevice_release_alloc_list_lock(device);
dxgsyncobject_destroy(process, syncobj);
dxgdevice_acquire_alloc_list_lock(device);
}
{
struct dxgallocation *alloc;
struct dxgallocation *tmp;
DXG_TRACE("destroying allocations");
list_for_each_entry_safe(alloc, tmp, &device->alloc_list_head,
alloc_list_entry) {
dxgallocation_destroy(alloc);
}
}
{
struct dxgresource *resource;
struct dxgresource *tmp;
DXG_TRACE("destroying resources");
list_for_each_entry_safe(resource, tmp,
&device->resource_list_head,
resource_list_entry) {
dxgresource_destroy(resource);
}
}
dxgdevice_release_alloc_list_lock(device);
{
struct dxgcontext *context;
struct dxgcontext *tmp;
DXG_TRACE("destroying contexts");
dxgdevice_acquire_context_list_lock(device);
list_for_each_entry_safe(context, tmp,
&device->context_list_head,
context_list_entry) {
dxgcontext_destroy(process, context);
}
dxgdevice_release_context_list_lock(device);
}
{
struct dxgpagingqueue *tmp;
struct dxgpagingqueue *pqueue;
DXG_TRACE("destroying paging queues");
list_for_each_entry_safe(pqueue, tmp, &device->pqueue_list_head,
pqueue_list_entry) {
dxgpagingqueue_destroy(pqueue);
}
}
/* Guest handles need to be released before the host handles */
hmgrtable_lock(&process->handle_table, DXGLOCK_EXCL);
if (device->handle_valid) {
hmgrtable_free_handle(&process->handle_table,
HMGRENTRY_TYPE_DXGDEVICE, device->handle);
device_handle = device->handle;
device->handle_valid = 0;
}
hmgrtable_unlock(&process->handle_table, DXGLOCK_EXCL);
if (device_handle.v) {
up_write(&device->device_lock);
if (dxgadapter_acquire_lock_shared(adapter) == 0) {
dxgvmb_send_destroy_device(adapter, process,
device_handle);
dxgadapter_release_lock_shared(adapter);
}
down_write(&device->device_lock);
}
cleanup:
if (device->adapter)
dxgprocess_adapter_remove_device(device);
up_write(&device->device_lock);
kref_put(&device->device_kref, dxgdevice_release);
DXG_TRACE("Device destroyed");
}
int dxgdevice_acquire_lock_shared(struct dxgdevice *device)
{
down_read(&device->device_lock);
if (!dxgdevice_is_active(device)) {
up_read(&device->device_lock);
return -ENODEV;
}
return 0;
}
void dxgdevice_release_lock_shared(struct dxgdevice *device)
{
up_read(&device->device_lock);
}
bool dxgdevice_is_active(struct dxgdevice *device)
{
return device->object_state == DXGOBJECTSTATE_ACTIVE;
}
void dxgdevice_acquire_context_list_lock(struct dxgdevice *device)
{
down_write(&device->context_list_lock);
}
void dxgdevice_release_context_list_lock(struct dxgdevice *device)
{
up_write(&device->context_list_lock);
}
void dxgdevice_acquire_alloc_list_lock(struct dxgdevice *device)
{
down_write(&device->alloc_list_lock);
}
void dxgdevice_release_alloc_list_lock(struct dxgdevice *device)
{
up_write(&device->alloc_list_lock);
}
void dxgdevice_acquire_alloc_list_lock_shared(struct dxgdevice *device)
{
down_read(&device->alloc_list_lock);
}
void dxgdevice_release_alloc_list_lock_shared(struct dxgdevice *device)
{
up_read(&device->alloc_list_lock);
}
void dxgdevice_add_context(struct dxgdevice *device, struct dxgcontext *context)
{
down_write(&device->context_list_lock);
list_add_tail(&context->context_list_entry, &device->context_list_head);
up_write(&device->context_list_lock);
}
void dxgdevice_remove_context(struct dxgdevice *device,
struct dxgcontext *context)
{
if (context->context_list_entry.next) {
list_del(&context->context_list_entry);
context->context_list_entry.next = NULL;
}
}
void dxgdevice_add_alloc(struct dxgdevice *device, struct dxgallocation *alloc)
{
dxgdevice_acquire_alloc_list_lock(device);
list_add_tail(&alloc->alloc_list_entry, &device->alloc_list_head);
kref_get(&device->device_kref);
alloc->owner.device = device;
dxgdevice_release_alloc_list_lock(device);
}
void dxgdevice_remove_alloc(struct dxgdevice *device,
struct dxgallocation *alloc)
{
if (alloc->alloc_list_entry.next) {
list_del(&alloc->alloc_list_entry);
alloc->alloc_list_entry.next = NULL;
kref_put(&device->device_kref, dxgdevice_release);
}
}
void dxgdevice_remove_alloc_safe(struct dxgdevice *device,
struct dxgallocation *alloc)
{
dxgdevice_acquire_alloc_list_lock(device);
dxgdevice_remove_alloc(device, alloc);
dxgdevice_release_alloc_list_lock(device);
}
void dxgdevice_add_resource(struct dxgdevice *device, struct dxgresource *res)
{
dxgdevice_acquire_alloc_list_lock(device);
list_add_tail(&res->resource_list_entry, &device->resource_list_head);
kref_get(&device->device_kref);
dxgdevice_release_alloc_list_lock(device);
}
void dxgdevice_remove_resource(struct dxgdevice *device,
struct dxgresource *res)
{
if (res->resource_list_entry.next) {
list_del(&res->resource_list_entry);
res->resource_list_entry.next = NULL;
kref_put(&device->device_kref, dxgdevice_release);
}
}
struct dxgsharedresource *dxgsharedresource_create(struct dxgadapter *adapter)
{
struct dxgsharedresource *resource;
resource = kzalloc(sizeof(*resource), GFP_KERNEL);
if (resource) {
INIT_LIST_HEAD(&resource->resource_list_head);
kref_init(&resource->sresource_kref);
mutex_init(&resource->fd_mutex);
resource->adapter = adapter;
}
return resource;
}
void dxgsharedresource_destroy(struct kref *refcount)
{
struct dxgsharedresource *resource;
resource = container_of(refcount, struct dxgsharedresource,
sresource_kref);
if (resource->runtime_private_data)
vfree(resource->runtime_private_data);
if (resource->resource_private_data)
vfree(resource->resource_private_data);
if (resource->alloc_info)
vfree(resource->alloc_info);
if (resource->alloc_private_data)
vfree(resource->alloc_private_data);
kfree(resource);
}
void dxgsharedresource_add_resource(struct dxgsharedresource *shared_resource,
struct dxgresource *resource)
{
down_write(&shared_resource->adapter->shared_resource_list_lock);
DXG_TRACE("Adding resource: %p %p", shared_resource, resource);
list_add_tail(&resource->shared_resource_list_entry,
&shared_resource->resource_list_head);
kref_get(&shared_resource->sresource_kref);
kref_get(&resource->resource_kref);
resource->shared_owner = shared_resource;
up_write(&shared_resource->adapter->shared_resource_list_lock);
}
void dxgsharedresource_remove_resource(struct dxgsharedresource
*shared_resource,
struct dxgresource *resource)
{
struct dxgadapter *adapter = shared_resource->adapter;
down_write(&adapter->shared_resource_list_lock);
DXG_TRACE("Removing resource: %p %p", shared_resource, resource);
if (resource->shared_resource_list_entry.next) {
list_del(&resource->shared_resource_list_entry);
resource->shared_resource_list_entry.next = NULL;
kref_put(&shared_resource->sresource_kref,
dxgsharedresource_destroy);
resource->shared_owner = NULL;
kref_put(&resource->resource_kref, dxgresource_release);
}
up_write(&adapter->shared_resource_list_lock);
}
struct dxgresource *dxgresource_create(struct dxgdevice *device)
{
struct dxgresource *resource;
resource = kzalloc(sizeof(struct dxgresource), GFP_KERNEL);
if (resource) {
kref_init(&resource->resource_kref);
resource->device = device;
resource->process = device->process;
resource->object_state = DXGOBJECTSTATE_ACTIVE;
mutex_init(&resource->resource_mutex);
INIT_LIST_HEAD(&resource->alloc_list_head);
dxgdevice_add_resource(device, resource);
}
return resource;
}
void dxgresource_free_handle(struct dxgresource *resource)
{
struct dxgallocation *alloc;
struct dxgprocess *process;
if (resource->handle_valid) {
process = resource->device->process;
hmgrtable_free_handle_safe(&process->handle_table,
HMGRENTRY_TYPE_DXGRESOURCE,
resource->handle);
resource->handle_valid = 0;
}
list_for_each_entry(alloc, &resource->alloc_list_head,
alloc_list_entry) {
dxgallocation_free_handle(alloc);
}
}
void dxgresource_destroy(struct dxgresource *resource)
{
/* device->alloc_list_lock is held */
struct dxgallocation *alloc;
struct dxgallocation *tmp;
struct d3dkmt_destroyallocation2 args = { };
int destroyed = test_and_set_bit(0, &resource->flags);
struct dxgdevice *device = resource->device;
struct dxgsharedresource *shared_resource;
if (!destroyed) {
dxgresource_free_handle(resource);
if (resource->handle.v) {
args.device = device->handle;
args.resource = resource->handle;
dxgvmb_send_destroy_allocation(device->process,
device, &args, NULL);
resource->handle.v = 0;
}
list_for_each_entry_safe(alloc, tmp, &resource->alloc_list_head,
alloc_list_entry) {
dxgallocation_destroy(alloc);
}
dxgdevice_remove_resource(device, resource);
shared_resource = resource->shared_owner;
if (shared_resource) {
dxgsharedresource_remove_resource(shared_resource,
resource);
resource->shared_owner = NULL;
}
}
kref_put(&resource->resource_kref, dxgresource_release);
}
void dxgresource_release(struct kref *refcount)
{
struct dxgresource *resource;
resource = container_of(refcount, struct dxgresource, resource_kref);
kfree(resource);
}
bool dxgresource_is_active(struct dxgresource *resource)
{
return resource->object_state == DXGOBJECTSTATE_ACTIVE;
}
int dxgresource_add_alloc(struct dxgresource *resource,
struct dxgallocation *alloc)
{
int ret = -ENODEV;
struct dxgdevice *device = resource->device;
dxgdevice_acquire_alloc_list_lock(device);
if (dxgresource_is_active(resource)) {
list_add_tail(&alloc->alloc_list_entry,
&resource->alloc_list_head);
alloc->owner.resource = resource;
ret = 0;
}
alloc->resource_owner = 1;
dxgdevice_release_alloc_list_lock(device);
return ret;
}
void dxgresource_remove_alloc(struct dxgresource *resource,
struct dxgallocation *alloc)
{
if (alloc->alloc_list_entry.next) {
list_del(&alloc->alloc_list_entry);
alloc->alloc_list_entry.next = NULL;
}
}
void dxgresource_remove_alloc_safe(struct dxgresource *resource,
struct dxgallocation *alloc)
{
dxgdevice_acquire_alloc_list_lock(resource->device);
dxgresource_remove_alloc(resource, alloc);
dxgdevice_release_alloc_list_lock(resource->device);
}
void dxgdevice_release(struct kref *refcount)
{
struct dxgdevice *device;
device = container_of(refcount, struct dxgdevice, device_kref);
DXG_TRACE("Destroying device: %px", device);
kref_put(&device->adapter->adapter_kref, dxgadapter_release);
kfree(device);
}
void dxgdevice_add_paging_queue(struct dxgdevice *device,
struct dxgpagingqueue *entry)
{
dxgdevice_acquire_alloc_list_lock(device);
list_add_tail(&entry->pqueue_list_entry, &device->pqueue_list_head);
dxgdevice_release_alloc_list_lock(device);
}
void dxgdevice_remove_paging_queue(struct dxgpagingqueue *pqueue)
{
struct dxgdevice *device = pqueue->device;
dxgdevice_acquire_alloc_list_lock(device);
if (pqueue->pqueue_list_entry.next) {
list_del(&pqueue->pqueue_list_entry);
pqueue->pqueue_list_entry.next = NULL;
}
dxgdevice_release_alloc_list_lock(device);
}
void dxgdevice_add_syncobj(struct dxgdevice *device,
struct dxgsyncobject *syncobj)
{
dxgdevice_acquire_alloc_list_lock(device);
list_add_tail(&syncobj->syncobj_list_entry, &device->syncobj_list_head);
kref_get(&syncobj->syncobj_kref);
dxgdevice_release_alloc_list_lock(device);
}
void dxgdevice_remove_syncobj(struct dxgsyncobject *entry)
{
struct dxgdevice *device = entry->device;
dxgdevice_acquire_alloc_list_lock(device);
if (entry->syncobj_list_entry.next) {
list_del(&entry->syncobj_list_entry);
entry->syncobj_list_entry.next = NULL;
kref_put(&entry->syncobj_kref, dxgsyncobject_release);
}
dxgdevice_release_alloc_list_lock(device);
kref_put(&device->device_kref, dxgdevice_release);
entry->device = NULL;
}
struct dxgcontext *dxgcontext_create(struct dxgdevice *device)
{
struct dxgcontext *context;
context = kzalloc(sizeof(struct dxgcontext), GFP_KERNEL);
if (context) {
kref_init(&context->context_kref);
context->device = device;
context->process = device->process;
context->device_handle = device->handle;
kref_get(&device->device_kref);
INIT_LIST_HEAD(&context->hwqueue_list_head);
init_rwsem(&context->hwqueue_list_lock);
dxgdevice_add_context(device, context);
context->object_state = DXGOBJECTSTATE_ACTIVE;
}
return context;
}
/*
* Called when the device context list lock is held
*/
void dxgcontext_destroy(struct dxgprocess *process, struct dxgcontext *context)
{
struct dxghwqueue *hwqueue;
struct dxghwqueue *tmp;
DXG_TRACE("Destroying context %p", context);
context->object_state = DXGOBJECTSTATE_DESTROYED;
if (context->device) {
if (context->handle.v) {
hmgrtable_free_handle_safe(&process->handle_table,
HMGRENTRY_TYPE_DXGCONTEXT,
context->handle);
}
dxgdevice_remove_context(context->device, context);
kref_put(&context->device->device_kref, dxgdevice_release);
}
list_for_each_entry_safe(hwqueue, tmp, &context->hwqueue_list_head,
hwqueue_list_entry) {
dxghwqueue_destroy(process, hwqueue);
}
kref_put(&context->context_kref, dxgcontext_release);
}
void dxgcontext_destroy_safe(struct dxgprocess *process,
struct dxgcontext *context)
{
struct dxgdevice *device = context->device;
dxgdevice_acquire_context_list_lock(device);
dxgcontext_destroy(process, context);
dxgdevice_release_context_list_lock(device);
}
bool dxgcontext_is_active(struct dxgcontext *context)
{
return context->object_state == DXGOBJECTSTATE_ACTIVE;
}
void dxgcontext_release(struct kref *refcount)
{
struct dxgcontext *context;
context = container_of(refcount, struct dxgcontext, context_kref);
kfree(context);
}
int dxgcontext_add_hwqueue(struct dxgcontext *context,
struct dxghwqueue *hwqueue)
{
int ret = 0;
down_write(&context->hwqueue_list_lock);
if (dxgcontext_is_active(context))
list_add_tail(&hwqueue->hwqueue_list_entry,
&context->hwqueue_list_head);
else
ret = -ENODEV;
up_write(&context->hwqueue_list_lock);
return ret;
}
void dxgcontext_remove_hwqueue(struct dxgcontext *context,
struct dxghwqueue *hwqueue)
{
if (hwqueue->hwqueue_list_entry.next) {
list_del(&hwqueue->hwqueue_list_entry);
hwqueue->hwqueue_list_entry.next = NULL;
}
}
void dxgcontext_remove_hwqueue_safe(struct dxgcontext *context,
struct dxghwqueue *hwqueue)
{
down_write(&context->hwqueue_list_lock);
dxgcontext_remove_hwqueue(context, hwqueue);
up_write(&context->hwqueue_list_lock);
}
struct dxgallocation *dxgallocation_create(struct dxgprocess *process)
{
struct dxgallocation *alloc;
alloc = kzalloc(sizeof(struct dxgallocation), GFP_KERNEL);
if (alloc)
alloc->process = process;
return alloc;
}
void dxgallocation_stop(struct dxgallocation *alloc)
{
if (alloc->pages) {
unpin_user_pages(alloc->pages, alloc->num_pages);
vfree(alloc->pages);
alloc->pages = NULL;
}
dxgprocess_ht_lock_exclusive_down(alloc->process);
if (alloc->cpu_address_mapped) {
dxg_unmap_iospace(alloc->cpu_address,
alloc->num_pages << PAGE_SHIFT);
alloc->cpu_address_mapped = false;
alloc->cpu_address = NULL;
alloc->cpu_address_refcount = 0;
}
dxgprocess_ht_lock_exclusive_up(alloc->process);
}
void dxgallocation_free_handle(struct dxgallocation *alloc)
{
dxgprocess_ht_lock_exclusive_down(alloc->process);
if (alloc->handle_valid) {
hmgrtable_free_handle(&alloc->process->handle_table,
HMGRENTRY_TYPE_DXGALLOCATION,
alloc->alloc_handle);
alloc->handle_valid = 0;
}
dxgprocess_ht_lock_exclusive_up(alloc->process);
}
void dxgallocation_destroy(struct dxgallocation *alloc)
{
struct dxgprocess *process = alloc->process;
struct d3dkmt_destroyallocation2 args = { };
dxgallocation_stop(alloc);
if (alloc->resource_owner)
dxgresource_remove_alloc(alloc->owner.resource, alloc);
else if (alloc->owner.device)
dxgdevice_remove_alloc(alloc->owner.device, alloc);
dxgallocation_free_handle(alloc);
if (alloc->alloc_handle.v && !alloc->resource_owner) {
args.device = alloc->owner.device->handle;
args.alloc_count = 1;
dxgvmb_send_destroy_allocation(process,
alloc->owner.device,
&args, &alloc->alloc_handle);
}
if (alloc->gpadl.gpadl_handle) {
DXG_TRACE("Teardown gpadl %d", alloc->gpadl.gpadl_handle);
vmbus_teardown_gpadl(dxgglobal_get_vmbus(), &alloc->gpadl);
alloc->gpadl.gpadl_handle = 0;
}
if (alloc->priv_drv_data)
vfree(alloc->priv_drv_data);
if (alloc->cpu_address_mapped)
DXG_ERR("Alloc IO space is mapped: %p", alloc);
kfree(alloc);
}
struct dxgpagingqueue *dxgpagingqueue_create(struct dxgdevice *device)
{
struct dxgpagingqueue *pqueue;
pqueue = kzalloc(sizeof(*pqueue), GFP_KERNEL);
if (pqueue) {
pqueue->device = device;
pqueue->process = device->process;
pqueue->device_handle = device->handle;
dxgdevice_add_paging_queue(device, pqueue);
}
return pqueue;
}
void dxgpagingqueue_stop(struct dxgpagingqueue *pqueue)
{
int ret;
if (pqueue->mapped_address) {
ret = dxg_unmap_iospace(pqueue->mapped_address, PAGE_SIZE);
DXG_TRACE("fence is unmapped %d %p",
ret, pqueue->mapped_address);
pqueue->mapped_address = NULL;
}
}
void dxgpagingqueue_destroy(struct dxgpagingqueue *pqueue)
{
struct dxgprocess *process = pqueue->process;
DXG_TRACE("Destroying pqueue %p %x", pqueue, pqueue->handle.v);
dxgpagingqueue_stop(pqueue);
hmgrtable_lock(&process->handle_table, DXGLOCK_EXCL);
if (pqueue->handle.v) {
hmgrtable_free_handle(&process->handle_table,
HMGRENTRY_TYPE_DXGPAGINGQUEUE,
pqueue->handle);
pqueue->handle.v = 0;
}
if (pqueue->syncobj_handle.v) {
hmgrtable_free_handle(&process->handle_table,
HMGRENTRY_TYPE_MONITOREDFENCE,
pqueue->syncobj_handle);
pqueue->syncobj_handle.v = 0;
}
hmgrtable_unlock(&process->handle_table, DXGLOCK_EXCL);
if (pqueue->device)
dxgdevice_remove_paging_queue(pqueue);
kfree(pqueue);
}
/*
* Process_adapter_mutex is held.
*/
struct dxgprocess_adapter *dxgprocess_adapter_create(struct dxgprocess *process,
struct dxgadapter *adapter)
{