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
|
/*
* nvidia-xconfig: A tool for manipulating X config files,
* specifically for use by the NVIDIA Linux graphics driver.
*
* Copyright (C) 2005 NVIDIA Corporation
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the:
*
* Free Software Foundation, Inc.
* 59 Temple Place - Suite 330
* Boston, MA 02111-1307, USA
*
*
* multiple_screens.c
*/
#include "nvidia-xconfig.h"
#include "xf86Parser.h"
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
static int enable_separate_x_screens(Options *op, XConfigPtr config,
XConfigLayoutPtr layout);
static int disable_separate_x_screens(Options *op, XConfigPtr config,
XConfigLayoutPtr layout);
static int set_xinerama(int xinerama_enabled, XConfigLayoutPtr layout);
static XConfigDisplayPtr clone_display_list(XConfigDisplayPtr display0);
static XConfigDevicePtr clone_device(XConfigDevicePtr device0);
static XConfigScreenPtr clone_screen(XConfigScreenPtr screen0);
static void create_adjacencies(Options *op, XConfigPtr config,
XConfigLayoutPtr layout);
static int enable_all_gpus(Options *op, XConfigPtr config,
XConfigLayoutPtr layout);
static void free_unused_devices(Options *op, XConfigPtr config);
static void free_unused_monitors(Options *op, XConfigPtr config);
static int only_one_screen(Options *op, XConfigPtr config,
XConfigLayoutPtr layout);
/*
* apply_multi_screen_options() - there are 4 options that can affect
* multiple X screens:
*
* - add X screens for all GPUS in the system
* - separate X screens on one GPU (turned on or off)
* - only one X screen
* - Xinerama
*
* apply these options in that order
*/
int apply_multi_screen_options(Options *op, XConfigPtr config,
XConfigLayoutPtr layout)
{
if (op->enable_all_gpus) {
if (!enable_all_gpus(op, config, layout)) return FALSE;
}
if (GET_BOOL_OPTION(op->boolean_options,
SEPARATE_X_SCREENS_BOOL_OPTION)) {
if (GET_BOOL_OPTION(op->boolean_option_values,
SEPARATE_X_SCREENS_BOOL_OPTION)) {
if (!enable_separate_x_screens(op, config, layout)) return FALSE;
} else {
if (!disable_separate_x_screens(op, config, layout)) return FALSE;
}
}
if (GET_BOOL_OPTION(op->boolean_options,
XINERAMA_BOOL_OPTION)) {
if (!set_xinerama(GET_BOOL_OPTION(op->boolean_option_values,
XINERAMA_BOOL_OPTION),
layout)) return FALSE;
}
if (op->only_one_screen) {
if (!only_one_screen(op, config, layout)) return FALSE;
}
return TRUE;
} /* apply_multi_screen_options() */
/*
* find_devices() - dlopen the nvidia-cfg library and query the
* available information about the GPUs in the system.
*/
DevicesPtr find_devices(Options *op)
{
DevicesPtr pDevices = NULL;
DisplayDevicePtr pDisplayDevice;
int i, j, n, count = 0;
unsigned int mask, bit;
DeviceRec tmpDevice;
NvCfgPciDevice *devs = NULL;
NvCfgBool is_primary_device;
NvCfgBool ret;
char *lib_path;
void *lib_handle;
NvCfgBool (*__getDevices)(int *n, NvCfgDevice **devs);
NvCfgBool (*__openDevice)(int bus, int slot, NvCfgDeviceHandle *handle);
NvCfgBool (*__getPciDevices)(int *n, NvCfgPciDevice **devs);
NvCfgBool (*__openPciDevice)(int domain, int bus, int slot, int function,
NvCfgDeviceHandle *handle);
NvCfgBool (*__getNumCRTCs)(NvCfgDeviceHandle handle, int *crtcs);
NvCfgBool (*__getProductName)(NvCfgDeviceHandle handle, char **name);
NvCfgBool (*__getDisplayDevices)(NvCfgDeviceHandle handle,
unsigned int *display_device_mask);
NvCfgBool (*__getEDID)(NvCfgDeviceHandle handle,
unsigned int display_device,
NvCfgDisplayDeviceInformation *info);
NvCfgBool (*__isPrimaryDevice)(NvCfgDeviceHandle handle,
NvCfgBool *is_primary_device);
NvCfgBool (*__closeDevice)(NvCfgDeviceHandle handle);
/* dlopen() the nvidia-cfg library */
#define __LIB_NAME "libnvidia-cfg.so.1"
if (op->nvidia_cfg_path) {
lib_path = nvstrcat(op->nvidia_cfg_path, "/", __LIB_NAME, NULL);
} else {
lib_path = strdup(__LIB_NAME);
}
lib_handle = dlopen(lib_path, RTLD_NOW);
nvfree(lib_path);
if (!lib_handle) {
fmtwarn("error opening %s: %s.", __LIB_NAME, dlerror());
return NULL;
}
#define __GET_FUNC(proc, name) \
(proc) = dlsym(lib_handle, (name)); \
if (!(proc)) { \
fmtwarn("error retrieving symbol %s from %s: %s", \
(name), __LIB_NAME, dlerror()); \
dlclose(lib_handle); \
return NULL; \
}
/* required functions */
__GET_FUNC(__getDevices, "nvCfgGetDevices");
__GET_FUNC(__openDevice, "nvCfgOpenDevice");
__GET_FUNC(__getPciDevices, "nvCfgGetPciDevices");
__GET_FUNC(__openPciDevice, "nvCfgOpenPciDevice");
__GET_FUNC(__getNumCRTCs, "nvCfgGetNumCRTCs");
__GET_FUNC(__getProductName, "nvCfgGetProductName");
__GET_FUNC(__getDisplayDevices, "nvCfgGetDisplayDevices");
__GET_FUNC(__getEDID, "nvCfgGetEDID");
__GET_FUNC(__closeDevice, "nvCfgCloseDevice");
/* optional functions */
__isPrimaryDevice = dlsym(lib_handle, "nvCfgIsPrimaryDevice");
if (__getPciDevices(&count, &devs) != NVCFG_TRUE) {
return NULL;
}
if (count == 0) return NULL;
pDevices = nvalloc(sizeof(DevicesRec));
pDevices->devices = nvalloc(sizeof(DeviceRec) * count);
pDevices->nDevices = count;
for (i = 0; i < count; i++) {
pDevices->devices[i].dev = devs[i];
if (__openPciDevice(devs[i].domain, devs[i].bus, devs[i].slot, 0,
&(pDevices->devices[i].handle)) != NVCFG_TRUE) {
goto fail;
}
if (__getNumCRTCs(pDevices->devices[i].handle,
&pDevices->devices[i].crtcs) != NVCFG_TRUE) {
goto fail;
}
if (__getProductName(pDevices->devices[i].handle,
&pDevices->devices[i].name) != NVCFG_TRUE) {
goto fail;
}
if (__getDisplayDevices(pDevices->devices[i].handle, &mask) !=
NVCFG_TRUE) {
goto fail;
}
pDevices->devices[i].displayDeviceMask = mask;
/* count the number of display devices */
for (n = j = 0; j < 32; j++) {
if (mask & (1 << j)) n++;
}
pDevices->devices[i].nDisplayDevices = n;
if (n) {
/* allocate the info array of the right size */
pDevices->devices[i].displayDevices =
nvalloc(sizeof(DisplayDeviceRec) * n);
/* fill in the info array */
for (n = j = 0; j < 32; j++) {
bit = 1 << j;
if (!(bit & mask)) continue;
pDisplayDevice = &pDevices->devices[i].displayDevices[n];
pDisplayDevice->mask = bit;
if (__getEDID(pDevices->devices[i].handle, bit,
&pDisplayDevice->info) != NVCFG_TRUE) {
pDisplayDevice->info_valid = FALSE;
} else {
pDisplayDevice->info_valid = TRUE;
}
n++;
}
} else {
pDevices->devices[i].displayDevices = NULL;
}
if ((i != 0) && (__isPrimaryDevice != NULL) &&
(__isPrimaryDevice(pDevices->devices[i].handle,
&is_primary_device) == NVCFG_TRUE) &&
(is_primary_device == NVCFG_TRUE)) {
memcpy(&tmpDevice, &pDevices->devices[0], sizeof(DeviceRec));
memcpy(&pDevices->devices[0], &pDevices->devices[i], sizeof(DeviceRec));
memcpy(&pDevices->devices[i], &tmpDevice, sizeof(DeviceRec));
}
ret = __closeDevice(pDevices->devices[i].handle);
pDevices->devices[i].handle = NULL;
if (ret != NVCFG_TRUE) {
goto fail;
}
}
goto done;
fail:
fmtwarn("Unable to use the nvidia-cfg library to query NVIDIA "
"hardware.");
for (i = 0; i < pDevices->nDevices; i++) {
/* close the opened device */
if (pDevices->devices[i].handle) {
__closeDevice(pDevices->devices[i].handle);
}
}
free_devices(pDevices);
pDevices = NULL;
/* fall through */
done:
if (devs) free(devs);
return pDevices;
} /* find_devices() */
/*
* free_devices()
*/
void free_devices(DevicesPtr pDevices)
{
int i;
if (!pDevices) return;
for (i = 0; i < pDevices->nDevices; i++) {
if (pDevices->devices[i].displayDevices) {
nvfree(pDevices->devices[i].displayDevices);
}
}
if (pDevices->devices) {
nvfree(pDevices->devices);
}
nvfree(pDevices);
} /* free_devices() */
/*
* set_xinerama() - This makes sure there is a ServerLayout
* section and sets the "Xinerama" option
*/
static int set_xinerama(int xinerama_enabled, XConfigLayoutPtr layout)
{
xconfigAddNewOption(&(layout->options),
"Xinerama",
(xinerama_enabled ? "1" : "0"));
return TRUE;
} /* set_xinerama() */
/*
* enable_separate_x_screens() - this effectively clones each screen
* that is on a unique GPU.
*
* Algorithm:
*
* step 1: build a list of screens to be cloned
*
* step 2: assign a busID to every screen that is in the list (if
* BusIDs are not already assigned)
*
* step 3: for every candidate screen, check if it is already one of
* multiple screens on a gpu; if so, then it is not eligible for
* cloning. Note that this has to check all screens in the adjacency
* list, not just the ones in the candidate list.
*
* step 4: clone each eligible screen
*
* step 5: update adjacency list (just wipe the list and restart)
*
* XXX we need to check that there are actually 2 CRTCs on this GPU
*/
static int enable_separate_x_screens(Options *op, XConfigPtr config,
XConfigLayoutPtr layout)
{
XConfigScreenPtr screen, *screenlist = NULL;
XConfigAdjacencyPtr adj;
int i, nscreens = 0;
int have_busids;
/* build the list of screens that are candidate to be cloned */
if (op->screen) {
screen = xconfigFindScreen(op->screen, config->screens);
if (!screen) {
fmterr("Unable to find screen '%s'.", op->screen);
return FALSE;
}
screenlist = nvalloc(sizeof(XConfigScreenPtr));
screenlist[0] = screen;
nscreens = 1;
} else {
for (adj = layout->adjacencies; adj; adj = adj->next) {
nscreens++;
screenlist = realloc(screenlist,
sizeof(XConfigScreenPtr) * nscreens);
screenlist[nscreens-1] = adj->screen;
}
}
if (!nscreens) return FALSE;
/* do all screens in the list have a bus ID? */
have_busids = TRUE;
for (i = 0; i < nscreens; i++) {
if (screenlist[i] &&
screenlist[i]->device &&
screenlist[i]->device->busid) {
// this screen has a busid
} else {
have_busids = FALSE;
break;
}
}
/*
* if not everyone has a bus id, then let's assign bus ids to all
* the screens; XXX what if _some_ already have busids? Too bad,
* we're going to reassign all of them.
*/
if (!have_busids) {
DevicesPtr pDevices;
pDevices = find_devices(op);
if (!pDevices) {
fmterr("Unable to determine number or location of "
"GPUs in system; cannot "
"honor '--separate-x-screens' option.");
return FALSE;
}
for (i = 0; i < nscreens; i++) {
if (i >= pDevices->nDevices) {
/*
* we have more screens than GPUs, this screen is no
* longer a candidate
*/
screenlist[i] = NULL;
continue;
}
screenlist[i]->device->busid = nvalloc(32);
xconfigFormatPciBusString(screenlist[i]->device->busid, 32,
pDevices->devices[i].dev.domain,
pDevices->devices[i].dev.bus,
pDevices->devices[i].dev.slot, 0);
screenlist[i]->device->board = nvstrdup(pDevices->devices[i].name);
}
free_devices(pDevices);
}
/*
* step 3: for every candidate screen, check if it is already one
* of multiple screens on a gpu; if so, then it is not eligible
* for cloning. Note that this has to check all screens in the
* adjacency list, not just the ones in the candidate list
*/
for (i = 0; i < nscreens; i++) {
int bus0, bus1, slot0, slot1, scratch;
if (!screenlist[i]) continue;
/* parse the bus id for this candidate screen */
if (!xconfigParsePciBusString(screenlist[i]->device->busid,
&bus0, &slot0, &scratch)) {
/* parsing failed; this screen is no longer a candidate */
screenlist[i] = NULL;
continue;
}
/*
* scan through all the screens; if any screen, other than
* *this* screen, have the same busid, then this screen is no
* longer a candidate
*/
for (screen = config->screens; screen; screen = screen->next) {
if (screen == screenlist[i]) continue;
if (!screen->device) continue;
if (!screen->device->busid) continue;
if (!xconfigParsePciBusString(screen->device->busid,
&bus1, &slot1, &scratch)) continue;
if ((bus0 == bus1) && (slot0 == slot1)) {
screenlist[i] = NULL; /* no longer a candidate */
break;
}
}
}
/* clone each eligible screen */
for (i = 0; i < nscreens; i++) {
if (!screenlist[i]) continue;
clone_screen(screenlist[i]);
}
/*
* wipe the existing adjacencies and recreate them
*
* XXX we should really only use the screens in the current
* adjacency list, plus the new cloned screens, when building the
* new adjacencies
*/
xconfigFreeAdjacencyList(&layout->adjacencies);
create_adjacencies(op, config, layout);
/* free stuff */
free(screenlist);
return TRUE;
} /* enable_separate_x_screens() */
/*
* disable_separate_x_screens() - remove multiple screens that are
* configured for the same GPU.
*
* Algorithm:
*
* step 1: find which screens need to be "de-cloned" (either
* op->screen or all screens in the layout)
*
* step 2: narrow that list down to screens that have a busid
* specified
*
* step 3: find all other screens that have the same busid and remove
* them
*
* step 3: recompute the adjacency list
*/
static int disable_separate_x_screens(Options *op, XConfigPtr config,
XConfigLayoutPtr layout)
{
XConfigScreenPtr screen, prev, next, *screenlist = NULL;
XConfigAdjacencyPtr adj;
int i, j, nscreens = 0;
int *bus, *slot, scratch;
/* build the list of screens that are candidate to be de-cloned */
if (op->screen) {
screen = xconfigFindScreen(op->screen, config->screens);
if (!screen) {
fmterr("Unable to find screen '%s'.", op->screen);
return FALSE;
}
screenlist = nvalloc(sizeof(XConfigScreenPtr));
screenlist[0] = screen;
nscreens = 1;
} else {
for (adj = layout->adjacencies; adj; adj = adj->next) {
nscreens++;
screenlist = realloc(screenlist,
sizeof(XConfigScreenPtr) * nscreens);
screenlist[nscreens-1] = adj->screen;
}
}
/*
* limit the list to screens that have a BusID; parse the busIDs
* while we're at it
*/
bus = nvalloc(sizeof(int) * nscreens);
slot = nvalloc(sizeof(int) * nscreens);
for (i = 0; i < nscreens; i++) {
if (screenlist[i] &&
screenlist[i]->device &&
screenlist[i]->device->busid &&
xconfigParsePciBusString(screenlist[i]->device->busid,
&bus[i], &slot[i], &scratch)) {
// this screen has a valid busid
} else {
screenlist[i] = NULL;
}
}
/* trim out duplicates */
for (i = 0; i < nscreens; i++) {
if (!screenlist[i]) continue;
for (j = i+1; j < nscreens; j++) {
if (!screenlist[j]) continue;
if ((bus[i] == bus[j]) && (slot[i] == slot[j])) {
screenlist[j] = NULL;
}
}
}
/*
* for every screen in the de-clone list, scan through all
* screens; if any screen, other than *this* screen has the same
* busid, remove it
*/
for (i = 0; i < nscreens; i++) {
int bus0, slot0;
if (!screenlist[i]) continue;
screen = config->screens;
prev = NULL;
while (screen) {
if (screenlist[i] == screen) goto next_screen;
if (!screen->device) goto next_screen;
if (!screen->device->busid) goto next_screen;
if (!xconfigParsePciBusString(screen->device->busid,
&bus0, &slot0, &scratch))
goto next_screen;
if ((bus0 == bus[i]) && (slot0 == slot[i])) {
if (prev) {
prev->next = screen->next;
} else {
config->screens = screen->next;
}
next = screen->next;
screen->next = NULL;
xconfigFreeScreenList(&screen);
screen = next;
} else {
next_screen:
prev = screen;
screen = screen->next;
}
}
screenlist[i]->device->screen = -1;
}
/* wipe the existing adjacencies and recreate them */
xconfigFreeAdjacencyList(&layout->adjacencies);
create_adjacencies(op, config, layout);
/* free unused device and monitor sections */
free_unused_devices(op, config);
free_unused_monitors(op, config);
/* free stuff */
free(screenlist);
free(bus);
free(slot);
return TRUE;
} /* disable_separate_x_screens() */
/*
* clone_display_list() - create a duplicate of the specified display
* subsection.
*/
static XConfigDisplayPtr clone_display_list(XConfigDisplayPtr display0)
{
XConfigDisplayPtr d = NULL, prev = NULL, head = NULL;
while (display0) {
d = nvalloc(sizeof(XConfigDisplayRec));
memcpy(d, display0, sizeof(XConfigDisplayRec));
if (display0->visual) d->visual = nvstrdup(display0->visual);
if (display0->comment) d->comment = nvstrdup(display0->comment);
d->options = xconfigOptionListDup(display0->options);
d->next = NULL;
if (prev) prev->next = d;
if (!head) head = d;
prev = d;
display0 = display0->next;
}
return head;
} /* clone_display_list() */
/*
* clone_device() - duplicate the specified device section, updating
* the screen indices as approprate for multiple X screens on one GPU
*/
static XConfigDevicePtr clone_device(XConfigDevicePtr device0)
{
XConfigDevicePtr device;
device = nvalloc(sizeof(XConfigDeviceRec));
device->identifier = nvstrcat(device0->identifier, " (2nd)", NULL);
if (device0->vendor) device->vendor = nvstrdup(device0->vendor);
if (device0->board) device->board = nvstrdup(device0->board);
if (device0->chipset) device->chipset = nvstrdup(device0->chipset);
if (device0->busid) device->busid = nvstrdup(device0->busid);
if (device0->card) device->card = nvstrdup(device0->card);
if (device0->driver) device->driver = nvstrdup(device0->driver);
if (device0->ramdac) device->ramdac = nvstrdup(device0->ramdac);
if (device0->comment) device->comment = nvstrdup(device0->comment);
/* these are needed for multiple X screens on one GPU */
device->screen = 1;
device0->screen = 0;
device->chipid = -1;
device->chiprev = -1;
device->irq = -1;
device->options = xconfigOptionListDup(device0->options);
/* insert the new device after the original device */
device->next = device0->next;
device0->next = device;
return device;
} /* clone_device() */
/*
* clone_screen() - duplicate the given screen, for use as the second
* X screen on one GPU
*/
static XConfigScreenPtr clone_screen(XConfigScreenPtr screen0)
{
XConfigScreenPtr screen = nvalloc(sizeof(XConfigScreenRec));
screen->identifier = nvstrcat(screen0->identifier, " (2nd)", NULL);
screen->device = clone_device(screen0->device);
screen->device_name = nvstrdup(screen->device->identifier);
screen->monitor = screen0->monitor;
screen->monitor_name = nvstrdup(screen0->monitor_name);
screen->defaultdepth = screen0->defaultdepth;
screen->displays = clone_display_list(screen0->displays);
screen->options = xconfigOptionListDup(screen0->options);
if (screen0->comment) screen->comment = nvstrdup(screen0->comment);
/* insert the new screen after the original screen */
screen->next = screen0->next;
screen0->next = screen;
return screen;
} /* clone_screen() */
/*
* create_adjacencies() - loop through all the screens in the config,
* and add an adjacency section to the layout; this assumes that there
* are no existing adjacencies in the layout
*/
static void create_adjacencies(Options *op, XConfigPtr config,
XConfigLayoutPtr layout)
{
XConfigAdjacencyPtr adj, prev_adj;
XConfigScreenPtr screen, prev;
int i;
prev = NULL;
i = 0;
prev_adj = NULL;
for (screen = config->screens; screen; screen = screen->next) {
adj = nvalloc(sizeof(XConfigAdjacencyRec));
adj->scrnum = i;
adj->screen_name = nvstrdup(screen->identifier);
adj->screen = screen;
if (prev_adj) {
prev_adj->next = adj;
} else {
layout->adjacencies = adj;
}
prev_adj = adj;
prev = screen;
i++;
}
xconfigGenerateAssignScreenAdjacencies(layout);
} /* create_adjacencies() */
/*
* enable_all_gpus() - get information for every GPU in the system,
* and create a screen section for each
*
* XXX do we add new screens with reasonable defaults, or do we clone
* the first existing X screen N times? For now, we'll just add all
* new X screens
*/
static int enable_all_gpus(Options *op, XConfigPtr config,
XConfigLayoutPtr layout)
{
DevicesPtr pDevices;
int i;
pDevices = find_devices(op);
if (!pDevices) {
fmterr("Unable to determine number of GPUs in system; cannot "
"honor '--enable-all-gpus' option.");
return FALSE;
}
/* free all existing X screens, monitors, devices, and adjacencies */
xconfigFreeScreenList(&config->screens);
xconfigFreeDeviceList(&config->devices);
xconfigFreeMonitorList(&config->monitors);
xconfigFreeAdjacencyList(&layout->adjacencies);
/* add N new screens; this will also add device and monitor sections */
for (i = 0; i < pDevices->nDevices; i++) {
xconfigGenerateAddScreen(config,
pDevices->devices[i].dev.bus,
pDevices->devices[i].dev.domain,
pDevices->devices[i].dev.slot,
pDevices->devices[i].name, i);
}
free_devices(pDevices);
/* create adjacencies for the layout */
create_adjacencies(op, config, layout);
return TRUE;
} /* enable_all_gpus() */
/*
* free_unused_devices() - free unused device sections
*/
static void free_unused_devices(Options *op, XConfigPtr config)
{
XConfigDevicePtr device, prev, next;
XConfigScreenPtr screen;
int found;
/* free any unused device sections */
device = config->devices;
prev = NULL;
while (device) {
found = FALSE;
for (screen = config->screens; screen; screen = screen->next) {
if (device == screen->device) {
found = TRUE;
break;
}
}
if (!found) {
if (prev) {
prev->next = device->next;
} else {
config->devices = device->next;
}
next = device->next;
device->next = NULL;
xconfigFreeDeviceList(&device);
device = next;
} else {
prev = device;
device = device->next;
}
}
} /* free_unused_devices() */
/*
* free_unused_monitors() - free unused monitor sections
*/
static void free_unused_monitors(Options *op, XConfigPtr config)
{
XConfigMonitorPtr monitor, prev, next;
XConfigScreenPtr screen;
int found;
/* free any unused monitor sections */
monitor = config->monitors;
prev = NULL;
while (monitor) {
found = FALSE;
for (screen = config->screens; screen; screen = screen->next) {
if (monitor == screen->monitor) {
found = TRUE;
break;
}
}
if (!found) {
if (prev) {
prev->next = monitor->next;
} else {
config->monitors = monitor->next;
}
next = monitor->next;
monitor->next = NULL;
xconfigFreeMonitorList(&monitor);
monitor = next;
} else {
prev = monitor;
monitor = monitor->next;
}
}
} /* free_unused_monitors() */
/*
* only_one_screen() - delete all screens after the first one
*/
static int only_one_screen(Options *op, XConfigPtr config,
XConfigLayoutPtr layout)
{
if (!config->screens) return FALSE;
/* free all existing X screens after the first */
xconfigFreeScreenList(&config->screens->next);
/* free all adjacencies */
xconfigFreeAdjacencyList(&layout->adjacencies);
/* add new adjacency */
create_adjacencies(op, config, layout);
/* removed unused device and monitor sections */
free_unused_devices(op, config);
free_unused_monitors(op, config);
return TRUE;
} /* only_one_screen() */
|