aboutsummaryrefslogtreecommitdiffstats
path: root/tmk_core/common/command.c
blob: f79d5a257b9cfdf9294cc61ed72828085d0dd2ab (plain)
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
/*
Copyright 2011 Jun Wako <wakojun@gmail.com>

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, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <stdbool.h>
#include "wait.h"
#include "keycode.h"
#include "host.h"
#include "keymap.h"
#include "print.h"
#include "debug.h"
#include "util.h"
#include "timer.h"
#include "keyboard.h"
#include "bootloader.h"
#include "action_layer.h"
#include "action_util.h"
#include "eeconfig.h"
#include "sleep_led.h"
#include "led.h"
#include "command.h"
#include "backlight.h"
#include "quantum.h"
#include "version.h"

#ifdef MOUSEKEY_ENABLE
#include "mousekey.h"
#endif

#ifdef PROTOCOL_PJRC
	#include "usb_keyboard.h"
		#ifdef EXTRAKEY_ENABLE
		#include "usb_extra.h"
	#endif
#endif

#ifdef PROTOCOL_VUSB
	#include "usbdrv.h"
#endif

#ifdef AUDIO_ENABLE
    #include "audio.h"
#endif /* AUDIO_ENABLE */


static bool command_common(uint8_t code);
static void command_common_help(void);
static void print_version(void);
static void print_status(void);
static bool command_console(uint8_t code);
static void command_console_help(void);
#ifdef MOUSEKEY_ENABLE
static bool mousekey_console(uint8_t code);
static void mousekey_console_help(void);
#endif

static void switch_default_layer(uint8_t layer);


command_state_t command_state = ONESHOT;


bool command_proc(uint8_t code)
{
    switch (command_state) {
        case ONESHOT:
            if (!IS_COMMAND())
                return false;
            return (command_extra(code) || command_common(code));
            break;
        case CONSOLE:
            if (IS_COMMAND())
                return (command_extra(code) || command_common(code));
            else
                return (command_console_extra(code) || command_console(code));
            break;
#ifdef MOUSEKEY_ENABLE
        case MOUSEKEY:
            mousekey_console(code);
            break;
#endif
        default:
            command_state = ONESHOT;
            return false;
    }
    return true;
}

/* TODO: Refactoring is needed. */
/* This allows to define extra commands. return false when not processed. */
bool command_extra(uint8_t code) __attribute__ ((weak));
bool command_extra(uint8_t code)
{
    (void)code;
    return false;
}

bool command_console_extra(uint8_t code) __attribute__ ((weak));
bool command_console_extra(uint8_t code)
{
    (void)code;
    return false;
}


/***********************************************************
 * Command common
 ***********************************************************/
static void command_common_help(void)
{
	print(                            "\n\t- Magic -\n"
		STR(MAGIC_KEY_DEBUG       ) ":	Debug Message Toggle\n"
		STR(MAGIC_KEY_DEBUG_MATRIX) ":	Matrix Debug Mode Toggle - Show keypresses in matrix grid\n"
		STR(MAGIC_KEY_DEBUG_KBD   ) ":	Keyboard Debug Toggle - Show keypress report\n"
		STR(MAGIC_KEY_DEBUG_MOUSE ) ":	Debug Mouse Toggle\n"
		STR(MAGIC_KEY_VERSION     ) ":	Version\n"
		STR(MAGIC_KEY_STATUS      ) ":	Status\n"
		STR(MAGIC_KEY_CONSOLE     ) ":	Activate Console Mode\n"

#if MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM
		STR(MAGIC_KEY_LAYER0      ) ":	Switch to Layer 0\n"
		STR(MAGIC_KEY_LAYER1      ) ":	Switch to Layer 1\n"
		STR(MAGIC_KEY_LAYER2      ) ":	Switch to Layer 2\n"
		STR(MAGIC_KEY_LAYER3      ) ":	Switch to Layer 3\n"
		STR(MAGIC_KEY_LAYER4      ) ":	Switch to Layer 4\n"
		STR(MAGIC_KEY_LAYER5      ) ":	Switch to Layer 5\n"
		STR(MAGIC_KEY_LAYER6      ) ":	Switch to Layer 6\n"
		STR(MAGIC_KEY_LAYER7      ) ":	Switch to Layer 7\n"
		STR(MAGIC_KEY_LAYER8      ) ":	Switch to Layer 8\n"
		STR(MAGIC_KEY_LAYER9      ) ":	Switch to Layer 9\n"
#endif

#if MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS
		                            "F1-F10:	Switch to Layer 0-9 (F10 = L0)\n"
#endif

#if MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS
		                            "0-9:	Switch to Layer 0-9\n"
#endif

		STR(MAGIC_KEY_LAYER0_ALT1 ) ":	Switch to Layer 0 (alternate key 1)\n"
		STR(MAGIC_KEY_LAYER0_ALT2 ) ":	Switch to Layer 0 (alternate key 2)\n"
		STR(MAGIC_KEY_BOOTLOADER  ) ":	Jump to Bootloader (Reset)\n"

#ifdef KEYBOARD_LOCK_ENABLE
		STR(MAGIC_KEY_LOCK        ) ":	Lock\n"
#endif

#ifdef BOOTMAGIC_ENABLE
		STR(MAGIC_KEY_EEPROM      ) ":	Print EEPROM Settings\n"
#endif

#ifdef NKRO_ENABLE
		STR(MAGIC_KEY_NKRO        ) ":	NKRO Toggle\n"
#endif

#ifdef SLEEP_LED_ENABLE
		STR(MAGIC_KEY_SLEEP_LED   ) ":	Sleep LED Test\n"
#endif
    );
}

static void print_version(void)
{
	// print version & information
    print("\n\t- Version -\n");
    print("DESC: " STR(DESCRIPTION) "\n");
    print("VID: " STR(VENDOR_ID) "(" STR(MANUFACTURER) ") "
          "PID: " STR(PRODUCT_ID) "(" STR(PRODUCT) ") "
          "VER: " STR(DEVICE_VER) "\n");
    print("BUILD: " STR(QMK_VERSION) " (" __TIME__ " " __DATE__ ")\n");

    /* build options */
    print("OPTIONS:"

#ifdef PROTOCOL_PJRC
	    " PJRC"
#endif
#ifdef PROTOCOL_LUFA
	    " LUFA"
#endif
#ifdef PROTOCOL_VUSB
	    " VUSB"
#endif
#ifdef BOOTMAGIC_ENABLE
	    " BOOTMAGIC"
#endif
#ifdef MOUSEKEY_ENABLE
	    " MOUSEKEY"
#endif
#ifdef EXTRAKEY_ENABLE
	    " EXTRAKEY"
#endif
#ifdef CONSOLE_ENABLE
	    " CONSOLE"
#endif
#ifdef COMMAND_ENABLE
	    " COMMAND"
#endif
#ifdef NKRO_ENABLE
	    " NKRO"
#endif
#ifdef KEYMAP_SECTION_ENABLE
	    " KEYMAP_SECTION"
#endif

	    " " STR(BOOTLOADER_SIZE) "\n");

    print("GCC: " STR(__GNUC__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__)
#if defined(__AVR__)
          " AVR-LIBC: " __AVR_LIBC_VERSION_STRING__
          " AVR_ARCH: avr" STR(__AVR_ARCH__)
#endif
		  "\n");

	return;
}

static void print_status(void)
{

    print("\n\t- Status -\n");

    print_val_hex8(host_keyboard_leds());
#ifndef PROTOCOL_VUSB
    // these aren't set on the V-USB protocol, so we just ignore them for now
    print_val_hex8(keyboard_protocol);
    print_val_hex8(keyboard_idle);
#endif
#ifdef NKRO_ENABLE
    print_val_hex8(keymap_config.nkro);
#endif
    print_val_hex32(timer_read32());

#ifdef PROTOCOL_PJRC
    print_val_hex8(UDCON);
    print_val_hex8(UDIEN);
    print_val_hex8(UDINT);
    print_val_hex8(usb_keyboard_leds);
    print_val_hex8(usb_keyboard_idle_count);
#endif

#ifdef PROTOCOL_PJRC
#   if USB_COUNT_SOF
    print_val_hex8(usbSofCount);
#   endif
#endif
	return;
}

#ifdef BOOTMAGIC_ENABLE
static void print_eeconfig(void)
{

// Print these variables if NO_PRINT or USER_PRINT are not defined.
#if !defined(NO_PRINT) && !defined(USER_PRINT)

    print("default_layer: "); print_dec(eeconfig_read_default_layer()); print("\n");

    debug_config_t dc;
    dc.raw = eeconfig_read_debug();
    print("debug_config.raw: "); print_hex8(dc.raw); print("\n");
    print(".enable: "); print_dec(dc.enable); print("\n");
    print(".matrix: "); print_dec(dc.matrix); print("\n");
    print(".keyboard: "); print_dec(dc.keyboard); print("\n");
    print(".mouse: "); print_dec(dc.mouse); print("\n");

    keymap_config_t kc;
    kc.raw = eeconfig_read_keymap();
    print("keymap_config.raw: "); print_hex8(kc.raw); print("\n");
    print(".swap_control_capslock: "); print_dec(kc.swap_control_capslock); print("\n");
    print(".capslock_to_control: "); print_dec(kc.capslock_to_control); print("\n");
    print(".swap_lalt_lgui: "); print_dec(kc.swap_lalt_lgui); print("\n");
    print(".swap_ralt_rgui: "); print_dec(kc.swap_ralt_rgui); print("\n");
    print(".no_gui: "); print_dec(kc.no_gui); print("\n");
    print(".swap_grave_esc: "); print_dec(kc.swap_grave_esc); print("\n");
    print(".swap_backslash_backspace: "); print_dec(kc.swap_backslash_backspace); print("\n");
    print(".nkro: "); print_dec(kc.nkro); print("\n");

#ifdef BACKLIGHT_ENABLE
    backlight_config_t bc;
    bc.raw = eeconfig_read_backlight();
    print("backlight_config.raw: "); print_hex8(bc.raw); print("\n");
    print(".enable: "); print_dec(bc.enable); print("\n");
    print(".level: "); print_dec(bc.level); print("\n");
#endif /* BACKLIGHT_ENABLE */

#endif /* !NO_PRINT */

}
#endif /* BOOTMAGIC_ENABLE */

static bool command_common(uint8_t code)
{

#ifdef KEYBOARD_LOCK_ENABLE
    static host_driver_t *host_driver = 0;
#endif

    switch (code) {

#ifdef SLEEP_LED_ENABLE

		// test breathing sleep LED
        case MAGIC_KC(MAGIC_KEY_SLEEP_LED):
            print("Sleep LED Test\n");
            sleep_led_toggle();
            led_set(host_keyboard_leds());
            break;
#endif

#ifdef BOOTMAGIC_ENABLE

		// print stored eeprom config
        case MAGIC_KC(MAGIC_KEY_EEPROM):
            print("eeconfig:\n");
            print_eeconfig();
            break;
#endif

#ifdef KEYBOARD_LOCK_ENABLE

		// lock/unlock keyboard
        case MAGIC_KC(MAGIC_KEY_LOCK):
            if (host_get_driver()) {
                host_driver = host_get_driver();
                clear_keyboard();
                host_set_driver(0);
                print("Locked.\n");
            } else {
                host_set_driver(host_driver);
                print("Unlocked.\n");
            }
            break;
#endif

		// print help
        case MAGIC_KC(MAGIC_KEY_HELP1):
        case MAGIC_KC(MAGIC_KEY_HELP2):
            command_common_help();
            break;

		// activate console
        case MAGIC_KC(MAGIC_KEY_CONSOLE):
            debug_matrix   = false;
            debug_keyboard = false;
            debug_mouse    = false;
            debug_enable   = false;
            command_console_help();
            print("C> ");
            command_state = CONSOLE;
            break;

        // jump to bootloader
        case MAGIC_KC(MAGIC_KEY_BOOTLOADER):
            clear_keyboard(); // clear to prevent stuck keys
            print("\n\nJumping to bootloader... ");
            #ifdef AUDIO_ENABLE
	            stop_all_notes();
                shutdown_user();
            #else
	            wait_ms(1000);
            #endif
            bootloader_jump(); // not return
            break;

        // debug toggle
        case MAGIC_KC(MAGIC_KEY_DEBUG):
            debug_enable = !debug_enable;
            if (debug_enable) {
                print("\ndebug: on\n");
            } else {
                print("\ndebug: off\n");
                debug_matrix   = false;
                debug_keyboard = false;
                debug_mouse    = false;
            }
            break;

        // debug matrix toggle
        case MAGIC_KC(MAGIC_KEY_DEBUG_MATRIX):
            debug_matrix = !debug_matrix;
            if (debug_matrix) {
                print("\nmatrix: on\n");
                debug_enable = true;
            } else {
                print("\nmatrix: off\n");
            }
            break;

        // debug keyboard toggle
        case MAGIC_KC(MAGIC_KEY_DEBUG_KBD):
            debug_keyboard = !debug_keyboard;
            if (debug_keyboard) {
                print("\nkeyboard: on\n");
                debug_enable = true;
            } else {
                print("\nkeyboard: off\n");
            }
            break;

        // debug mouse toggle
        case MAGIC_KC(MAGIC_KEY_DEBUG_MOUSE):
            debug_mouse = !debug_mouse;
            if (debug_mouse) {
                print("\nmouse: on\n");
                debug_enable = true;
            } else {
				print("\nmouse: off\n");
            }
            break;

		// print version
        case MAGIC_KC(MAGIC_KEY_VERSION):
        	print_version();
		    break;

		// print status
		case MAGIC_KC(MAGIC_KEY_STATUS):
			print_status();
            break;

#ifdef NKRO_ENABLE

		// NKRO toggle
        case MAGIC_KC(MAGIC_KEY_NKRO):
            clear_keyboard(); // clear to prevent stuck keys
            keymap_config.nkro = !keymap_config.nkro;
            if (keymap_config.nkro) {
                print("NKRO: on\n");
            } else {
                print("NKRO: off\n");
            }
            break;
#endif

		// switch layers

		case MAGIC_KC(MAGIC_KEY_LAYER0_ALT1):
		case MAGIC_KC(MAGIC_KEY_LAYER0_ALT2):
            switch_default_layer(0);
            break;

#if MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM

		case MAGIC_KC(MAGIC_KEY_LAYER0):
            switch_default_layer(0);
            break;

		case MAGIC_KC(MAGIC_KEY_LAYER1):
            switch_default_layer(1);
            break;

		case MAGIC_KC(MAGIC_KEY_LAYER2):
            switch_default_layer(2);
            break;

		case MAGIC_KC(MAGIC_KEY_LAYER3):
            switch_default_layer(3);
            break;

		case MAGIC_KC(MAGIC_KEY_LAYER4):
            switch_default_layer(4);
            break;

		case MAGIC_KC(MAGIC_KEY_LAYER5):
            switch_default_layer(5);
            break;

		case MAGIC_KC(MAGIC_KEY_LAYER6):
            switch_default_layer(6);
            break;

		case MAGIC_KC(MAGIC_KEY_LAYER7):
            switch_default_layer(7);
            break;

		case MAGIC_KC(MAGIC_KEY_LAYER8):
            switch_default_layer(8);
            break;

		case MAGIC_KC(MAGIC_KEY_LAYER9):
            switch_default_layer(9);
            break;
#endif


#if MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS

        case KC_F1 ... KC_F9:
            switch_default_layer((code - KC_F1) + 1);
            break;
        case KC_F10:
            switch_default_layer(0);
            break;
#endif

#if MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS

        case KC_1 ... KC_9:
            switch_default_layer((code - KC_1) + 1);
            break;
        case KC_0:
            switch_default_layer(0);
            break;
#endif

        default:
            print("?");
            return false;
    }
    return true;
}


/***********************************************************
 * Command console
 ***********************************************************/
static void command_console_help(void)
{
    print("\n\t- Console -\n"
          "ESC/q:	quit\n"
#ifdef MOUSEKEY_ENABLE
          "m:	mousekey\n"
#endif
    );
}

static bool command_console(uint8_t code)
{
    switch (code) {
        case KC_H:
        case KC_SLASH: /* ? */
            command_console_help();
            break;
        case KC_Q:
        case KC_ESC:
            command_state = ONESHOT;
            return false;
#ifdef MOUSEKEY_ENABLE
        case KC_M:
            mousekey_console_help();
            print("M> ");
            command_state = MOUSEKEY;
            return true;
#endif
        default:
            print("?");
            return false;
    }
    print("C> ");
    return true;
}


#ifdef MOUSEKEY_ENABLE
/***********************************************************
 * Mousekey console
 ***********************************************************/
static uint8_t mousekey_param = 0;

static void mousekey_param_print(void)
{
// Print these variables if NO_PRINT or USER_PRINT are not defined.
#if !defined(NO_PRINT) && !defined(USER_PRINT)
    print("\n\t- Values -\n");
    print("1: delay(*10ms): "); pdec(mk_delay); print("\n");
    print("2: interval(ms): "); pdec(mk_interval); print("\n");
    print("3: max_speed: "); pdec(mk_max_speed); print("\n");
    print("4: time_to_max: "); pdec(mk_time_to_max); print("\n");
    print("5: wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
    print("6: wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
#endif /* !NO_PRINT */

}

//#define PRINT_SET_VAL(v)  print(#v " = "); print_dec(v); print("\n");
#define PRINT_SET_VAL(v)  xprintf(#v " = %d\n", (v))
static void mousekey_param_inc(uint8_t param, uint8_t inc)
{
    switch (param) {
        case 1:
            if (mk_delay + inc < UINT8_MAX)
                mk_delay += inc;
            else
                mk_delay = UINT8_MAX;
            PRINT_SET_VAL(mk_delay);
            break;
        case 2:
            if (mk_interval + inc < UINT8_MAX)
                mk_interval += inc;
            else
                mk_interval = UINT8_MAX;
            PRINT_SET_VAL(mk_interval);
            break;
        case 3:
            if (mk_max_speed + inc < UINT8_MAX)
                mk_max_speed += inc;
            else
                mk_max_speed = UINT8_MAX;
            PRINT_SET_VAL(mk_max_speed);
            break;
        case 4:
            if (mk_time_to_max + inc < UINT8_MAX)
                mk_time_to_max += inc;
            else
                mk_time_to_max = UINT8_MAX;
            PRINT_SET_VAL(mk_time_to_max);
            break;
        case 5:
            if (mk_wheel_max_speed + inc < UINT8_MAX)
                mk_wheel_max_speed += inc;
            else
                mk_wheel_max_speed = UINT8_MAX;
            PRINT_SET_VAL(mk_wheel_max_speed);
            break;
        case 6:
            if (mk_wheel_time_to_max + inc < UINT8_MAX)
                mk_wheel_time_to_max += inc;
            else
                mk_wheel_time_to_max = UINT8_MAX;
            PRINT_SET_VAL(mk_wheel_time_to_max);
            break;
    }
}

static void mousekey_param_dec(uint8_t param, uint8_t dec)
{
    switch (param) {
        case 1:
            if (mk_delay > dec)
                mk_delay -= dec;
            else
                mk_delay = 0;
            PRINT_SET_VAL(mk_delay);
            break;
        case 2:
            if (mk_interval > dec)
                mk_interval -= dec;
            else
                mk_interval = 0;
            PRINT_SET_VAL(mk_interval);
            break;
        case 3:
            if (mk_max_speed > dec)
                mk_max_speed -= dec;
            else
                mk_max_speed = 0;
            PRINT_SET_VAL(mk_max_speed);
            break;
        case 4:
            if (mk_time_to_max > dec)
                mk_time_to_max -= dec;
            else
                mk_time_to_max = 0;
            PRINT_SET_VAL(mk_time_to_max);
            break;
        case 5:
            if (mk_wheel_max_speed > dec)
                mk_wheel_max_speed -= dec;
            else
                mk_wheel_max_speed = 0;
            PRINT_SET_VAL(mk_wheel_max_speed);
            break;
        case 6:
            if (mk_wheel_time_to_max > dec)
                mk_wheel_time_to_max -= dec;
            else
                mk_wheel_time_to_max = 0;
            PRINT_SET_VAL(mk_wheel_time_to_max);
            break;
    }
}

static void mousekey_console_help(void)
{
    print("\n\t- Mousekey -\n"
          "ESC/q:	quit\n"
          "1:	delay(*10ms)\n"
          "2:	interval(ms)\n"
          "3:	max_speed\n"
          "4:	time_to_max\n"
          "5:	wheel_max_speed\n"
          "6:	wheel_time_to_max\n"
          "\n"
          "p:	print values\n"
          "d:	set defaults\n"
          "up:	+1\n"
          "down:	-1\n"
          "pgup:	+10\n"
          "pgdown:	-10\n"
          "\n"
          "speed = delta * max_speed * (repeat / time_to_max)\n");
    xprintf("where delta: cursor=%d, wheel=%d\n"
            "See http://en.wikipedia.org/wiki/Mouse_keys\n", MOUSEKEY_MOVE_DELTA,  MOUSEKEY_WHEEL_DELTA);
}

static bool mousekey_console(uint8_t code)
{
    switch (code) {
        case KC_H:
        case KC_SLASH: /* ? */
            mousekey_console_help();
            break;
        case KC_Q:
        case KC_ESC:
            if (mousekey_param) {
                mousekey_param = 0;
            } else {
                print("C> ");
                command_state = CONSOLE;
                return false;
            }
            break;
        case KC_P:
            mousekey_param_print();
            break;
        case KC_1:
        case KC_2:
        case KC_3:
        case KC_4:
        case KC_5:
        case KC_6:
            mousekey_param = numkey2num(code);
            break;
        case KC_UP:
            mousekey_param_inc(mousekey_param, 1);
            break;
        case KC_DOWN:
            mousekey_param_dec(mousekey_param, 1);
            break;
        case KC_PGUP:
            mousekey_param_inc(mousekey_param, 10);
            break;
        case KC_PGDN:
            mousekey_param_dec(mousekey_param, 10);
            break;
        case KC_D:
            mk_delay = MOUSEKEY_DELAY/10;
            mk_interval = MOUSEKEY_INTERVAL;
            mk_max_speed = MOUSEKEY_MAX_SPEED;
            mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
            mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
            mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
            print("set default\n");
            break;
        default:
            print("?");
            return false;
    }
    if (mousekey_param) {
        xprintf("M%d> ", mousekey_param);
    } else {
        print("M>" );
    }
    return true;
}
#endif


/***********************************************************
 * Utilities
 ***********************************************************/
uint8_t numkey2num(uint8_t code)
{
    switch (code) {
        case KC_1: return 1;
        case KC_2: return 2;
        case KC_3: return 3;
        case KC_4: return 4;
        case KC_5: return 5;
        case KC_6: return 6;
        case KC_7: return 7;
        case KC_8: return 8;
        case KC_9: return 9;
        case KC_0: return 0;
    }
    return 0;
}

static void switch_default_layer(uint8_t layer)
{
    xprintf("L%d\n", layer);
    default_layer_set(1UL<<layer);
    clear_keyboard();
}
n> irq_vector = xmalloc_array(u8, nr_irqs_gsi); if ( !irq_desc || !irq_cfg || !irq_status ||! irq_vector ) return -ENOMEM; memset(irq_desc, 0, nr_irqs * sizeof(*irq_desc)); memset(irq_cfg, 0, nr_irqs * sizeof(*irq_cfg)); memset(irq_status, 0, nr_irqs * sizeof(*irq_status)); memset(irq_vector, 0, nr_irqs_gsi * sizeof(*irq_vector)); for (irq = 0; irq < nr_irqs; irq++) { desc = irq_to_desc(irq); cfg = irq_cfg(irq); desc->irq = irq; desc->chip_data = cfg; init_one_irq_desc(desc); init_one_irq_cfg(cfg); init_one_irq_status(irq); } /* Never allocate the hypercall vector or Linux/BSD fast-trap vector. */ set_bit(LEGACY_SYSCALL_VECTOR, used_vectors); set_bit(HYPERCALL_VECTOR, used_vectors); /* IRQ_MOVE_CLEANUP_VECTOR used for clean up vectors */ set_bit(IRQ_MOVE_CLEANUP_VECTOR, used_vectors); return 0; } static void __do_IRQ_guest(int vector); void no_action(int cpl, void *dev_id, struct cpu_user_regs *regs) { } static void enable_none(unsigned int vector) { } static void end_none(unsigned int irq, u8 vector) { } static unsigned int startup_none(unsigned int vector) { return 0; } static void disable_none(unsigned int vector) { } static void ack_none(unsigned int irq) { ack_bad_irq(irq); } #define shutdown_none disable_none hw_irq_controller no_irq_type = { "none", startup_none, shutdown_none, enable_none, disable_none, ack_none, end_none }; static vmask_t *irq_get_used_vector_mask(int irq) { vmask_t *ret = NULL; if ( opt_irq_vector_map == OPT_IRQ_VECTOR_MAP_GLOBAL ) { struct irq_desc *desc = irq_to_desc(irq); ret = &global_used_vector_map; if ( desc->chip_data->used_vectors ) { printk(XENLOG_INFO "%s: Strange, unassigned irq %d already has used_vectors!\n", __func__, irq); } else { int vector; vector = irq_to_vector(irq); if ( vector > 0 ) { printk(XENLOG_INFO "%s: Strange, irq %d already assigned vector %d!\n", __func__, irq, vector); ASSERT(!test_bit(vector, ret)); set_bit(vector, ret); } } } else if ( IO_APIC_IRQ(irq) && opt_irq_vector_map != OPT_IRQ_VECTOR_MAP_NONE ) { ret = io_apic_get_used_vector_map(irq); } return ret; } int __assign_irq_vector(int irq, struct irq_cfg *cfg, const cpumask_t *mask) { /* * NOTE! The local APIC isn't very good at handling * multiple interrupts at the same interrupt level. * As the interrupt level is determined by taking the * vector number and shifting that right by 4, we * want to spread these out a bit so that they don't * all fall in the same interrupt level. * * Also, we've got to be careful not to trash gate * 0x80, because int 0x80 is hm, kind of importantish. ;) */ static int current_vector = FIRST_DYNAMIC_VECTOR, current_offset = 0; unsigned int old_vector; int cpu, err; unsigned long flags; cpumask_t tmp_mask; vmask_t *irq_used_vectors = NULL; old_vector = irq_to_vector(irq); if (old_vector) { cpus_and(tmp_mask, *mask, cpu_online_map); if (cpus_intersects(tmp_mask, cfg->cpu_mask)) { cfg->vector = old_vector; return 0; } } if ((cfg->move_in_progress) || cfg->move_cleanup_count) return -EAGAIN; err = -ENOSPC; /* This is the only place normal IRQs are ever marked * as "in use". If they're not in use yet, check to see * if we need to assign a global vector mask. */ if ( irq_status[irq] == IRQ_USED ) { irq_used_vectors = cfg->used_vectors; } else irq_used_vectors = irq_get_used_vector_mask(irq); for_each_cpu_mask(cpu, *mask) { int new_cpu; int vector, offset; /* Only try and allocate irqs on cpus that are present. */ if (!cpu_online(cpu)) continue; cpus_and(tmp_mask, *vector_allocation_cpumask(cpu), cpu_online_map); vector = current_vector; offset = current_offset; next: vector += 8; if (vector > LAST_DYNAMIC_VECTOR) { /* If out of vectors on large boxen, must share them. */ offset = (offset + 1) % 8; vector = FIRST_DYNAMIC_VECTOR + offset; } if (unlikely(current_vector == vector)) continue; if (test_bit(vector, used_vectors)) goto next; if (irq_used_vectors && test_bit(vector, irq_used_vectors) ) goto next; for_each_cpu_mask(new_cpu, tmp_mask) if (per_cpu(vector_irq, new_cpu)[vector] != -1) goto next; /* Found one! */ current_vector = vector; current_offset = offset; local_irq_save(flags); if (old_vector) { cfg->move_in_progress = 1; cpus_copy(cfg->old_cpu_mask, cfg->cpu_mask); } for_each_cpu_mask(new_cpu, tmp_mask) per_cpu(vector_irq, new_cpu)[vector] = irq; cfg->vector = vector; cpus_copy(cfg->cpu_mask, tmp_mask); irq_status[irq] = IRQ_USED; ASSERT((cfg->used_vectors == NULL) || (cfg->used_vectors == irq_used_vectors)); cfg->used_vectors = irq_used_vectors; if (IO_APIC_IRQ(irq)) irq_vector[irq] = vector; if ( cfg->used_vectors ) { ASSERT(!test_bit(vector, cfg->used_vectors)); set_bit(vector, cfg->used_vectors); } err = 0; local_irq_restore(flags); break; } return err; } int assign_irq_vector(int irq) { int ret; unsigned long flags; struct irq_cfg *cfg = &irq_cfg[irq]; struct irq_desc *desc = irq_to_desc(irq); BUG_ON(irq >= nr_irqs || irq <0); spin_lock_irqsave(&vector_lock, flags); ret = __assign_irq_vector(irq, cfg, TARGET_CPUS); if (!ret) { ret = cfg->vector; cpus_copy(desc->affinity, cfg->cpu_mask); } spin_unlock_irqrestore(&vector_lock, flags); return ret; } /* * Initialize vector_irq on a new cpu. This function must be called * with vector_lock held. */ void __setup_vector_irq(int cpu) { int irq, vector; struct irq_cfg *cfg; /* Clear vector_irq */ for (vector = 0; vector < NR_VECTORS; ++vector) per_cpu(vector_irq, cpu)[vector] = -1; /* Mark the inuse vectors */ for (irq = 0; irq < nr_irqs; ++irq) { cfg = irq_cfg(irq); if (!cpu_isset(cpu, cfg->cpu_mask)) continue; vector = irq_to_vector(irq); per_cpu(vector_irq, cpu)[vector] = irq; } } void move_masked_irq(int irq) { struct irq_desc *desc = irq_to_desc(irq); if (likely(!(desc->status & IRQ_MOVE_PENDING))) return; desc->status &= ~IRQ_MOVE_PENDING; if (unlikely(cpus_empty(desc->pending_mask))) return; if (!desc->handler->set_affinity) return; /* * If there was a valid mask to work with, please do the disable, * re-program, enable sequence. This is *not* particularly important for * level triggered but in a edge trigger case, we might be setting rte when * an active trigger is comming in. This could cause some ioapics to * mal-function. Being paranoid i guess! * * For correct operation this depends on the caller masking the irqs. */ if (likely(cpus_intersects(desc->pending_mask, cpu_online_map))) desc->handler->set_affinity(irq, desc->pending_mask); cpus_clear(desc->pending_mask); } void move_native_irq(int irq) { struct irq_desc *desc = irq_to_desc(irq); if (likely(!(desc->status & IRQ_MOVE_PENDING))) return; if (unlikely(desc->status & IRQ_DISABLED)) return; desc->handler->disable(irq); move_masked_irq(irq); desc->handler->enable(irq); } /* For re-setting irq interrupt affinity for specific irq */ void irq_set_affinity(struct irq_desc *desc, const cpumask_t *mask) { if (!desc->handler->set_affinity) return; ASSERT(spin_is_locked(&desc->lock)); desc->status &= ~IRQ_MOVE_PENDING; wmb(); cpus_copy(desc->pending_mask, *mask); wmb(); desc->status |= IRQ_MOVE_PENDING; } void pirq_set_affinity(struct domain *d, int pirq, const cpumask_t *mask) { unsigned long flags; struct irq_desc *desc = domain_spin_lock_irq_desc(d, pirq, &flags); if ( !desc ) return; irq_set_affinity(desc, mask); spin_unlock_irqrestore(&desc->lock, flags); } DEFINE_PER_CPU(unsigned int, irq_count); void do_IRQ(struct cpu_user_regs *regs) { struct irqaction *action; uint32_t tsc_in; struct irq_desc *desc; unsigned int vector = regs->entry_vector; int irq = __get_cpu_var(vector_irq[vector]); struct cpu_user_regs *old_regs = set_irq_regs(regs); perfc_incr(irqs); this_cpu(irq_count)++; if (irq < 0) { ack_APIC_irq(); printk("%s: %d.%d No irq handler for vector (irq %d)\n", __func__, smp_processor_id(), vector, irq); set_irq_regs(old_regs); return; } irq_enter(); desc = irq_to_desc(irq); spin_lock(&desc->lock); desc->handler->ack(irq); if ( likely(desc->status & IRQ_GUEST) ) { if ( irq_ratelimit_timer.function && /* irq rate limiting enabled? */ unlikely(desc->rl_cnt++ >= irq_ratelimit_threshold) ) { s_time_t now = NOW(); if ( now < (desc->rl_quantum_start + MILLISECS(10)) ) { desc->handler->disable(irq); /* * If handler->disable doesn't actually mask the interrupt, a * disabled irq still can fire. This check also avoids possible * deadlocks if ratelimit_timer_fn runs at the same time. */ if ( likely(list_empty(&desc->rl_link)) ) { spin_lock(&irq_ratelimit_lock); if ( list_empty(&irq_ratelimit_list) ) set_timer(&irq_ratelimit_timer, now + MILLISECS(10)); list_add(&desc->rl_link, &irq_ratelimit_list); spin_unlock(&irq_ratelimit_lock); } goto out; } desc->rl_cnt = 0; desc->rl_quantum_start = now; } tsc_in = tb_init_done ? get_cycles() : 0; __do_IRQ_guest(irq); TRACE_3D(TRC_TRACE_IRQ, irq, tsc_in, get_cycles()); goto out_no_end; } desc->status &= ~IRQ_REPLAY; desc->status |= IRQ_PENDING; /* * Since we set PENDING, if another processor is handling a different * instance of this same irq, the other processor will take care of it. */ if ( desc->status & (IRQ_DISABLED | IRQ_INPROGRESS) ) goto out; desc->status |= IRQ_INPROGRESS; action = desc->action; while ( desc->status & IRQ_PENDING ) { desc->status &= ~IRQ_PENDING; spin_unlock_irq(&desc->lock); tsc_in = tb_init_done ? get_cycles() : 0; action->handler(irq, action->dev_id, regs); TRACE_3D(TRC_TRACE_IRQ, irq, tsc_in, get_cycles()); spin_lock_irq(&desc->lock); } desc->status &= ~IRQ_INPROGRESS; out: desc->handler->end(irq, regs->entry_vector); out_no_end: spin_unlock(&desc->lock); irq_exit(); set_irq_regs(old_regs); } static void irq_ratelimit_timer_fn(void *data) { struct irq_desc *desc, *tmp; unsigned long flags; spin_lock_irqsave(&irq_ratelimit_lock, flags); list_for_each_entry_safe ( desc, tmp, &irq_ratelimit_list, rl_link ) { spin_lock(&desc->lock); desc->handler->enable(desc->irq); list_del(&desc->rl_link); INIT_LIST_HEAD(&desc->rl_link); spin_unlock(&desc->lock); } spin_unlock_irqrestore(&irq_ratelimit_lock, flags); } static int __init irq_ratelimit_init(void) { if ( irq_ratelimit_threshold ) init_timer(&irq_ratelimit_timer, irq_ratelimit_timer_fn, NULL, 0); return 0; } __initcall(irq_ratelimit_init); int request_irq(unsigned int irq, void (*handler)(int, void *, struct cpu_user_regs *), unsigned long irqflags, const char * devname, void *dev_id) { struct irqaction * action; int retval; /* * Sanity-check: shared interrupts must pass in a real dev-ID, * otherwise we'll have trouble later trying to figure out * which interrupt is which (messes up the interrupt freeing * logic etc). */ if (irq >= nr_irqs) return -EINVAL; if (!handler) return -EINVAL; action = xmalloc(struct irqaction); if (!action) return -ENOMEM; action->handler = handler; action->name = devname; action->dev_id = dev_id; action->free_on_release = 1; retval = setup_irq(irq, action); if (retval) xfree(action); return retval; } void release_irq(unsigned int irq) { struct irq_desc *desc; unsigned long flags; struct irqaction *action; desc = irq_to_desc(irq); spin_lock_irqsave(&desc->lock,flags); action = desc->action; desc->action = NULL; desc->depth = 1; desc->status |= IRQ_DISABLED; desc->handler->shutdown(irq); spin_unlock_irqrestore(&desc->lock,flags); /* Wait to make sure it's not being used on another CPU */ do { smp_mb(); } while ( desc->status & IRQ_INPROGRESS ); if (action && action->free_on_release) xfree(action); } int setup_irq(unsigned int irq, struct irqaction *new) { struct irq_desc *desc; unsigned long flags; desc = irq_to_desc(irq); spin_lock_irqsave(&desc->lock,flags); if ( desc->action != NULL ) { spin_unlock_irqrestore(&desc->lock,flags); return -EBUSY; } desc->action = new; desc->depth = 0; desc->status &= ~IRQ_DISABLED; desc->handler->startup(irq); spin_unlock_irqrestore(&desc->lock,flags); return 0; } /* * HANDLING OF GUEST-BOUND PHYSICAL IRQS */ #define IRQ_MAX_GUESTS 7 typedef struct { u8 nr_guests; u8 in_flight; u8 shareable; u8 ack_type; #define ACKTYPE_NONE 0 /* No final acknowledgement is required */ #define ACKTYPE_UNMASK 1 /* Unmask PIC hardware (from any CPU) */ #define ACKTYPE_EOI 2 /* EOI on the CPU that was interrupted */ cpumask_t cpu_eoi_map; /* CPUs that need to EOI this interrupt */ struct timer eoi_timer; struct domain *guest[IRQ_MAX_GUESTS]; } irq_guest_action_t; /* * Stack of interrupts awaiting EOI on each CPU. These must be popped in * order, as only the current highest-priority pending irq can be EOIed. */ struct pending_eoi { u32 ready:1; /* Ready for EOI now? */ u32 irq:23; /* irq of the vector */ u32 vector:8; /* vector awaiting EOI */ }; static DEFINE_PER_CPU(struct pending_eoi, pending_eoi[NR_DYNAMIC_VECTORS]); #define pending_eoi_sp(p) ((p)[NR_DYNAMIC_VECTORS-1].vector) bool_t cpu_has_pending_apic_eoi(void) { return (pending_eoi_sp(this_cpu(pending_eoi)) != 0); } static inline void set_pirq_eoi(struct domain *d, unsigned int irq) { if ( d->arch.pirq_eoi_map ) set_bit(irq, d->arch.pirq_eoi_map); } static inline void clear_pirq_eoi(struct domain *d, unsigned int irq) { if ( d->arch.pirq_eoi_map ) clear_bit(irq, d->arch.pirq_eoi_map); } static void _irq_guest_eoi(struct irq_desc *desc) { irq_guest_action_t *action = (irq_guest_action_t *)desc->action; unsigned int i, irq = desc - irq_desc; if ( !(desc->status & IRQ_GUEST_EOI_PENDING) ) return; for ( i = 0; i < action->nr_guests; ++i ) clear_pirq_eoi(action->guest[i], domain_irq_to_pirq(action->guest[i], irq)); desc->status &= ~(IRQ_INPROGRESS|IRQ_GUEST_EOI_PENDING); desc->handler->enable(irq); } static void set_eoi_ready(void *data); static void irq_guest_eoi_timer_fn(void *data) { struct irq_desc *desc = data; unsigned int irq = desc - irq_desc; irq_guest_action_t *action; cpumask_t cpu_eoi_map; unsigned long flags; spin_lock_irqsave(&desc->lock, flags); if ( !(desc->status & IRQ_GUEST) ) goto out; action = (irq_guest_action_t *)desc->action; if ( action->ack_type != ACKTYPE_NONE ) { unsigned int i; for ( i = 0; i < action->nr_guests; i++ ) { struct domain *d = action->guest[i]; unsigned int pirq = domain_irq_to_pirq(d, irq); if ( test_and_clear_bit(pirq, d->pirq_mask) ) action->in_flight--; } } if ( action->in_flight != 0 ) goto out; switch ( action->ack_type ) { case ACKTYPE_UNMASK: desc->handler->end(irq, 0); break; case ACKTYPE_EOI: cpu_eoi_map = action->cpu_eoi_map; spin_unlock_irq(&desc->lock); on_selected_cpus(&cpu_eoi_map, set_eoi_ready, desc, 0); spin_lock_irq(&desc->lock); break; case ACKTYPE_NONE: _irq_guest_eoi(desc); break; } out: spin_unlock_irqrestore(&desc->lock, flags); } static void __do_IRQ_guest(int irq) { struct irq_desc *desc = irq_to_desc(irq); irq_guest_action_t *action = (irq_guest_action_t *)desc->action; struct domain *d; int i, sp, already_pending = 0; struct pending_eoi *peoi = this_cpu(pending_eoi); int vector = get_irq_regs()->entry_vector; if ( unlikely(action->nr_guests == 0) ) { /* An interrupt may slip through while freeing an ACKTYPE_EOI irq. */ ASSERT(action->ack_type == ACKTYPE_EOI); ASSERT(desc->status & IRQ_DISABLED); desc->handler->end(irq, vector); return; } if ( action->ack_type == ACKTYPE_EOI ) { sp = pending_eoi_sp(peoi); ASSERT((sp == 0) || (peoi[sp-1].vector < vector)); ASSERT(sp < (NR_DYNAMIC_VECTORS-1)); peoi[sp].irq = irq; peoi[sp].vector = vector; peoi[sp].ready = 0; pending_eoi_sp(peoi) = sp+1; cpu_set(smp_processor_id(), action->cpu_eoi_map); } for ( i = 0; i < action->nr_guests; i++ ) { unsigned int pirq; d = action->guest[i]; pirq = domain_irq_to_pirq(d, irq); if ( (action->ack_type != ACKTYPE_NONE) && !test_and_set_bit(pirq, d->pirq_mask) ) action->in_flight++; if ( hvm_do_IRQ_dpci(d, pirq) ) { if ( action->ack_type == ACKTYPE_NONE ) { already_pending += !!(desc->status & IRQ_INPROGRESS); desc->status |= IRQ_INPROGRESS; /* cleared during hvm eoi */ } } else if ( send_guest_pirq(d, pirq) && (action->ack_type == ACKTYPE_NONE) ) { already_pending++; } } stop_timer(&action->eoi_timer); if ( (action->ack_type == ACKTYPE_NONE) && (already_pending == action->nr_guests) ) { desc->handler->disable(irq); desc->status |= IRQ_GUEST_EOI_PENDING; for ( i = 0; i < already_pending; ++i ) { d = action->guest[i]; set_pirq_eoi(d, domain_irq_to_pirq(d, irq)); /* * Could check here whether the guest unmasked the event by now * (or perhaps just re-issue the send_guest_pirq()), and if it * can now accept the event, * - clear all the pirq_eoi bits we already set, * - re-enable the vector, and * - skip the timer setup below. */ } } migrate_timer(&action->eoi_timer, smp_processor_id()); set_timer(&action->eoi_timer, NOW() + MILLISECS(1)); } /* * Retrieve Xen irq-descriptor corresponding to a domain-specific irq. * The descriptor is returned locked. This function is safe against changes * to the per-domain irq-to-vector mapping. */ struct irq_desc *domain_spin_lock_irq_desc( struct domain *d, int pirq, unsigned long *pflags) { int irq; unsigned long flags; struct irq_desc *desc; for ( ; ; ) { irq = domain_pirq_to_irq(d, pirq); if ( irq <= 0 ) return NULL; desc = irq_to_desc(irq); spin_lock_irqsave(&desc->lock, flags); if ( irq == domain_pirq_to_irq(d, pirq) ) break; spin_unlock_irqrestore(&desc->lock, flags); } if ( pflags != NULL ) *pflags = flags; return desc; } /* Flush all ready EOIs from the top of this CPU's pending-EOI stack. */ static void flush_ready_eoi(void) { struct pending_eoi *peoi = this_cpu(pending_eoi); struct irq_desc *desc; int irq, sp; ASSERT(!local_irq_is_enabled()); sp = pending_eoi_sp(peoi); while ( (--sp >= 0) && peoi[sp].ready ) { irq = peoi[sp].irq; ASSERT(irq > 0); desc = irq_to_desc(irq); spin_lock(&desc->lock); desc->handler->end(irq, peoi[sp].vector); spin_unlock(&desc->lock); } pending_eoi_sp(peoi) = sp+1; } static void __set_eoi_ready(struct irq_desc *desc) { irq_guest_action_t *action = (irq_guest_action_t *)desc->action; struct pending_eoi *peoi = this_cpu(pending_eoi); int irq, sp; irq = desc - irq_desc; if ( !(desc->status & IRQ_GUEST) || (action->in_flight != 0) || !cpu_test_and_clear(smp_processor_id(), action->cpu_eoi_map) ) return; sp = pending_eoi_sp(peoi); do { ASSERT(sp > 0); } while ( peoi[--sp].irq != irq ); ASSERT(!peoi[sp].ready); peoi[sp].ready = 1; } /* Mark specified IRQ as ready-for-EOI (if it really is) and attempt to EOI. */ static void set_eoi_ready(void *data) { struct irq_desc *desc = data; ASSERT(!local_irq_is_enabled()); spin_lock(&desc->lock); __set_eoi_ready(desc); spin_unlock(&desc->lock); flush_ready_eoi(); } static void __pirq_guest_eoi(struct domain *d, int pirq) { struct irq_desc *desc; irq_guest_action_t *action; cpumask_t cpu_eoi_map; int irq; ASSERT(local_irq_is_enabled()); desc = domain_spin_lock_irq_desc(d, pirq, NULL); if ( desc == NULL ) return; if ( !(desc->status & IRQ_GUEST) ) { spin_unlock_irq(&desc->lock); return; } action = (irq_guest_action_t *)desc->action; irq = desc - irq_desc; if ( action->ack_type == ACKTYPE_NONE ) { ASSERT(!test_bit(pirq, d->pirq_mask)); stop_timer(&action->eoi_timer); _irq_guest_eoi(desc); } if ( unlikely(!test_and_clear_bit(pirq, d->pirq_mask)) || unlikely(--action->in_flight != 0) ) { spin_unlock_irq(&desc->lock); return; } if ( action->ack_type == ACKTYPE_UNMASK ) { ASSERT(cpus_empty(action->cpu_eoi_map)); desc->handler->end(irq, 0); spin_unlock_irq(&desc->lock); return; } ASSERT(action->ack_type == ACKTYPE_EOI); cpu_eoi_map = action->cpu_eoi_map; if ( cpu_test_and_clear(smp_processor_id(), cpu_eoi_map) ) { __set_eoi_ready(desc); spin_unlock(&desc->lock); flush_ready_eoi(); local_irq_enable(); } else { spin_unlock_irq(&desc->lock); } if ( !cpus_empty(cpu_eoi_map) ) on_selected_cpus(&cpu_eoi_map, set_eoi_ready, desc, 0); } int pirq_guest_eoi(struct domain *d, int irq) { if ( (irq < 0) || (irq >= d->nr_pirqs) ) return -EINVAL; __pirq_guest_eoi(d, irq); return 0; } int pirq_guest_unmask(struct domain *d) { unsigned int irq, nr = d->nr_pirqs; for ( irq = find_first_bit(d->pirq_mask, nr); irq < nr; irq = find_next_bit(d->pirq_mask, nr, irq+1) ) { if ( !test_bit(d->pirq_to_evtchn[irq], &shared_info(d, evtchn_mask)) ) __pirq_guest_eoi(d, irq); } return 0; } extern int ioapic_ack_new; static int pirq_acktype(struct domain *d, int pirq) { struct irq_desc *desc; int irq; irq = domain_pirq_to_irq(d, pirq); if ( irq <= 0 ) return ACKTYPE_NONE; desc = irq_to_desc(irq); if ( desc->handler == &no_irq_type ) return ACKTYPE_NONE; /* * Edge-triggered IO-APIC and LAPIC interrupts need no final * acknowledgement: we ACK early during interrupt processing. */ if ( !strcmp(desc->handler->typename, "IO-APIC-edge") || !strcmp(desc->handler->typename, "local-APIC-edge") ) return ACKTYPE_NONE; /* * MSIs are treated as edge-triggered interrupts, except * when there is no proper way to mask them. */ if ( desc->handler == &pci_msi_type ) return msi_maskable_irq(desc->msi_desc) ? ACKTYPE_NONE : ACKTYPE_EOI; /* * Level-triggered IO-APIC interrupts need to be acknowledged on the CPU * on which they were received. This is because we tickle the LAPIC to EOI. */ if ( !strcmp(desc->handler->typename, "IO-APIC-level") ) return ioapic_ack_new ? ACKTYPE_EOI : ACKTYPE_UNMASK; /* Legacy PIC interrupts can be acknowledged from any CPU. */ if ( !strcmp(desc->handler->typename, "XT-PIC") ) return ACKTYPE_UNMASK; printk("Unknown PIC type '%s' for IRQ %d\n", desc->handler->typename, irq); BUG(); return 0; } int pirq_shared(struct domain *d, int pirq) { struct irq_desc *desc; irq_guest_action_t *action; unsigned long flags; int shared; desc = domain_spin_lock_irq_desc(d, pirq, &flags); if ( desc == NULL ) return 0; action = (irq_guest_action_t *)desc->action; shared = ((desc->status & IRQ_GUEST) && (action->nr_guests > 1)); spin_unlock_irqrestore(&desc->lock, flags); return shared; } int pirq_guest_bind(struct vcpu *v, int pirq, int will_share) { unsigned int irq; struct irq_desc *desc; irq_guest_action_t *action, *newaction = NULL; int rc = 0; cpumask_t cpumask = CPU_MASK_NONE; WARN_ON(!spin_is_locked(&v->domain->event_lock)); BUG_ON(!local_irq_is_enabled()); retry: desc = domain_spin_lock_irq_desc(v->domain, pirq, NULL); if ( desc == NULL ) { rc = -EINVAL; goto out; } action = (irq_guest_action_t *)desc->action; irq = desc - irq_desc; if ( !(desc->status & IRQ_GUEST) ) { if ( desc->action != NULL ) { gdprintk(XENLOG_INFO, "Cannot bind IRQ %d to guest. In use by '%s'.\n", pirq, desc->action->name); rc = -EBUSY; goto unlock_out; } if ( newaction == NULL ) { spin_unlock_irq(&desc->lock); if ( (newaction = xmalloc(irq_guest_action_t)) != NULL ) goto retry; gdprintk(XENLOG_INFO, "Cannot bind IRQ %d to guest. Out of memory.\n", pirq); rc = -ENOMEM; goto out; } action = newaction; desc->action = (struct irqaction *)action; newaction = NULL; action->nr_guests = 0; action->in_flight = 0; action->shareable = will_share; action->ack_type = pirq_acktype(v->domain, pirq); cpus_clear(action->cpu_eoi_map); init_timer(&action->eoi_timer, irq_guest_eoi_timer_fn, desc, 0); desc->depth = 0; desc->status |= IRQ_GUEST; desc->status &= ~IRQ_DISABLED; desc->handler->startup(irq); /* Attempt to bind the interrupt target to the correct CPU. */ cpu_set(v->processor, cpumask); if ( !opt_noirqbalance && (desc->handler->set_affinity != NULL) ) desc->handler->set_affinity(irq, cpumask); } else if ( !will_share || !action->shareable ) { gdprintk(XENLOG_INFO, "Cannot bind IRQ %d to guest. %s.\n", pirq, will_share ? "Others do not share" : "Will not share with others"); rc = -EBUSY; goto unlock_out; } else if ( action->nr_guests == 0 ) { /* * Indicates that an ACKTYPE_EOI interrupt is being released. * Wait for that to happen before continuing. */ ASSERT(action->ack_type == ACKTYPE_EOI); ASSERT(desc->status & IRQ_DISABLED); spin_unlock_irq(&desc->lock); cpu_relax(); goto retry; } if ( action->nr_guests == IRQ_MAX_GUESTS ) { gdprintk(XENLOG_INFO, "Cannot bind IRQ %d to guest. " "Already at max share.\n", pirq); rc = -EBUSY; goto unlock_out; } action->guest[action->nr_guests++] = v->domain; if ( action->ack_type != ACKTYPE_NONE ) set_pirq_eoi(v->domain, pirq); else clear_pirq_eoi(v->domain, pirq); unlock_out: spin_unlock_irq(&desc->lock); out: if ( newaction != NULL ) xfree(newaction); return rc; } static irq_guest_action_t *__pirq_guest_unbind( struct domain *d, int pirq, struct irq_desc *desc) { unsigned int irq; irq_guest_action_t *action; cpumask_t cpu_eoi_map; int i; action = (irq_guest_action_t *)desc->action; irq = desc - irq_desc; if ( unlikely(action == NULL) ) { dprintk(XENLOG_G_WARNING, "dom%d: pirq %d: desc->action is NULL!\n", d->domain_id, pirq); return NULL; } BUG_ON(!(desc->status & IRQ_GUEST)); for ( i = 0; (i < action->nr_guests) && (action->guest[i] != d); i++ ) continue; BUG_ON(i == action->nr_guests); memmove(&action->guest[i], &action->guest[i+1], (action->nr_guests-i-1) * sizeof(action->guest[0])); action->nr_guests--; switch ( action->ack_type ) { case ACKTYPE_UNMASK: if ( test_and_clear_bit(pirq, d->pirq_mask) && (--action->in_flight == 0) ) desc->handler->end(irq, 0); break; case ACKTYPE_EOI: /* NB. If #guests == 0 then we clear the eoi_map later on. */ if ( test_and_clear_bit(pirq, d->pirq_mask) && (--action->in_flight == 0) && (action->nr_guests != 0) ) { cpu_eoi_map = action->cpu_eoi_map; spin_unlock_irq(&desc->lock); on_selected_cpus(&cpu_eoi_map, set_eoi_ready, desc, 0); spin_lock_irq(&desc->lock); } break; case ACKTYPE_NONE: stop_timer(&action->eoi_timer); _irq_guest_eoi(desc); break; } /* * The guest cannot re-bind to this IRQ until this function returns. So, * when we have flushed this IRQ from pirq_mask, it should remain flushed. */ BUG_ON(test_bit(pirq, d->pirq_mask)); if ( action->nr_guests != 0 ) return NULL; BUG_ON(action->in_flight != 0); /* Disabling IRQ before releasing the desc_lock avoids an IRQ storm. */ desc->depth = 1; desc->status |= IRQ_DISABLED; desc->handler->disable(irq); /* * Mark any remaining pending EOIs as ready to flush. * NOTE: We will need to make this a stronger barrier if in future we allow * an interrupt vectors to be re-bound to a different PIC. In that case we * would need to flush all ready EOIs before returning as otherwise the * desc->handler could change and we would call the wrong 'end' hook. */ cpu_eoi_map = action->cpu_eoi_map; if ( !cpus_empty(cpu_eoi_map) ) { BUG_ON(action->ack_type != ACKTYPE_EOI); spin_unlock_irq(&desc->lock); on_selected_cpus(&cpu_eoi_map, set_eoi_ready, desc, 1); spin_lock_irq(&desc->lock); } BUG_ON(!cpus_empty(action->cpu_eoi_map)); desc->action = NULL; desc->status &= ~(IRQ_GUEST|IRQ_GUEST_EOI_PENDING|IRQ_INPROGRESS); desc->handler->shutdown(irq); /* Caller frees the old guest descriptor block. */ return action; } void pirq_guest_unbind(struct domain *d, int pirq) { irq_guest_action_t *oldaction = NULL; struct irq_desc *desc; int irq; WARN_ON(!spin_is_locked(&d->event_lock)); BUG_ON(!local_irq_is_enabled()); desc = domain_spin_lock_irq_desc(d, pirq, NULL); if ( desc == NULL ) { irq = -domain_pirq_to_irq(d, pirq); BUG_ON(irq <= 0); desc = irq_to_desc(irq); spin_lock_irq(&desc->lock); d->arch.pirq_irq[pirq] = d->arch.irq_pirq[irq] = 0; } else { oldaction = __pirq_guest_unbind(d, pirq, desc); } spin_unlock_irq(&desc->lock); if ( oldaction != NULL ) { kill_timer(&oldaction->eoi_timer); xfree(oldaction); } } static int pirq_guest_force_unbind(struct domain *d, int irq) { struct irq_desc *desc; irq_guest_action_t *action, *oldaction = NULL; int i, bound = 0; WARN_ON(!spin_is_locked(&d->event_lock)); BUG_ON(!local_irq_is_enabled()); desc = domain_spin_lock_irq_desc(d, irq, NULL); BUG_ON(desc == NULL); if ( !(desc->status & IRQ_GUEST) ) goto out; action = (irq_guest_action_t *)desc->action; if ( unlikely(action == NULL) ) { dprintk(XENLOG_G_WARNING, "dom%d: pirq %d: desc->action is NULL!\n", d->domain_id, irq); goto out; } for ( i = 0; (i < action->nr_guests) && (action->guest[i] != d); i++ ) continue; if ( i == action->nr_guests ) goto out; bound = 1; oldaction = __pirq_guest_unbind(d, irq, desc); out: spin_unlock_irq(&desc->lock); if ( oldaction != NULL ) { kill_timer(&oldaction->eoi_timer); xfree(oldaction); } return bound; } int get_free_pirq(struct domain *d, int type, int index) { int i; ASSERT(spin_is_locked(&d->event_lock)); if ( type == MAP_PIRQ_TYPE_GSI ) { for ( i = 16; i < nr_irqs_gsi; i++ ) if ( !d->arch.pirq_irq[i] ) { if ( !is_hvm_domain(d) || d->arch.pirq_emuirq[i] == IRQ_UNBOUND ) return i; } } for ( i = d->nr_pirqs - 1; i >= nr_irqs_gsi; i-- ) if ( !d->arch.pirq_irq[i] ) { if ( !is_hvm_domain(d) || d->arch.pirq_emuirq[i] == IRQ_UNBOUND ) break; } if ( i < nr_irqs_gsi ) return -ENOSPC; return i; } int map_domain_pirq( struct domain *d, int pirq, int irq, int type, void *data) { int ret = 0; int old_irq, old_pirq; struct irq_desc *desc; unsigned long flags; struct msi_desc *msi_desc; struct pci_dev *pdev = NULL; ASSERT(spin_is_locked(&pcidevs_lock)); ASSERT(spin_is_locked(&d->event_lock)); if ( !IS_PRIV(current->domain) && !(IS_PRIV_FOR(current->domain, d) && irq_access_permitted(current->domain, irq))) return -EPERM; if ( pirq < 0 || pirq >= d->nr_pirqs || irq < 0 || irq >= nr_irqs ) { dprintk(XENLOG_G_ERR, "dom%d: invalid pirq %d or irq %d\n", d->domain_id, pirq, irq); return -EINVAL; } old_irq = domain_pirq_to_irq(d, pirq); old_pirq = domain_irq_to_pirq(d, irq); if ( (old_irq > 0 && (old_irq != irq) ) || (old_pirq && (old_pirq != pirq)) ) { dprintk(XENLOG_G_WARNING, "dom%d: pirq %d or irq %d already mapped\n", d->domain_id, pirq, irq); return 0; } ret = irq_permit_access(d, irq); if ( ret ) { printk(XENLOG_G_ERR "dom%d: could not permit access to IRQ%d (pirq %d)\n", d->domain_id, irq, pirq); return ret; } desc = irq_to_desc(irq); if ( type == MAP_PIRQ_TYPE_MSI ) { struct msi_info *msi = (struct msi_info *)data; ret = -ENODEV; if ( !cpu_has_apic ) goto done; pdev = pci_get_pdev(msi->bus, msi->devfn); ret = pci_enable_msi(msi, &msi_desc); if ( ret ) goto done; spin_lock_irqsave(&desc->lock, flags); if ( desc->handler != &no_irq_type ) { spin_unlock_irqrestore(&desc->lock, flags); dprintk(XENLOG_G_ERR, "dom%d: irq %d in use\n", d->domain_id, irq); pci_disable_msi(msi_desc); ret = -EBUSY; goto done; } desc->handler = &pci_msi_type; if ( opt_irq_vector_map == OPT_IRQ_VECTOR_MAP_PERDEV && !desc->chip_data->used_vectors ) { desc->chip_data->used_vectors = &pdev->info.used_vectors; if ( desc->chip_data->vector != IRQ_VECTOR_UNASSIGNED ) { int vector = desc->chip_data->vector; ASSERT(!test_bit(vector, desc->chip_data->used_vectors)); set_bit(vector, desc->chip_data->used_vectors); } } d->arch.pirq_irq[pirq] = irq; d->arch.irq_pirq[irq] = pirq; setup_msi_irq(pdev, msi_desc, irq); spin_unlock_irqrestore(&desc->lock, flags); } else { spin_lock_irqsave(&desc->lock, flags); d->arch.pirq_irq[pirq] = irq; d->arch.irq_pirq[irq] = pirq; spin_unlock_irqrestore(&desc->lock, flags); } done: if ( ret && irq_deny_access(d, irq) ) printk(XENLOG_G_ERR "dom%d: could not revoke access to IRQ%d (pirq %d)\n", d->domain_id, irq, pirq); return ret; } /* The pirq should have been unbound before this call. */ int unmap_domain_pirq(struct domain *d, int pirq) { unsigned long flags; struct irq_desc *desc; int irq, ret = 0; bool_t forced_unbind; struct msi_desc *msi_desc = NULL; if ( (pirq < 0) || (pirq >= d->nr_pirqs) ) return -EINVAL; ASSERT(spin_is_locked(&pcidevs_lock)); ASSERT(spin_is_locked(&d->event_lock)); irq = domain_pirq_to_irq(d, pirq); if ( irq <= 0 ) { dprintk(XENLOG_G_ERR, "dom%d: pirq %d not mapped\n", d->domain_id, pirq); ret = -EINVAL; goto done; } forced_unbind = pirq_guest_force_unbind(d, pirq); if ( forced_unbind ) dprintk(XENLOG_G_WARNING, "dom%d: forcing unbind of pirq %d\n", d->domain_id, pirq); desc = irq_to_desc(irq); if ( (msi_desc = desc->msi_desc) != NULL ) pci_disable_msi(msi_desc); spin_lock_irqsave(&desc->lock, flags); BUG_ON(irq != domain_pirq_to_irq(d, pirq)); if ( !forced_unbind ) { d->arch.pirq_irq[pirq] = 0; d->arch.irq_pirq[irq] = 0; } else { d->arch.pirq_irq[pirq] = -irq; d->arch.irq_pirq[irq] = -pirq; } spin_unlock_irqrestore(&desc->lock, flags); if (msi_desc) msi_free_irq(msi_desc); ret = irq_deny_access(d, irq); if ( ret ) printk(XENLOG_G_ERR "dom%d: could not deny access to IRQ%d (pirq %d)\n", d->domain_id, irq, pirq); if ( desc->handler == &pci_msi_type ) desc->handler = &no_irq_type; done: return ret; } void free_domain_pirqs(struct domain *d) { int i; spin_lock(&pcidevs_lock); spin_lock(&d->event_lock); for ( i = 0; i < d->nr_pirqs; i++ ) if ( d->arch.pirq_irq[i] > 0 ) unmap_domain_pirq(d, i); spin_unlock(&d->event_lock); spin_unlock(&pcidevs_lock); } extern void dump_ioapic_irq_info(void); static void dump_irqs(unsigned char key) { int i, irq, pirq; struct irq_desc *desc; struct irq_cfg *cfg; irq_guest_action_t *action; struct domain *d; unsigned long flags; printk("Guest interrupt information:\n"); for ( irq = 0; irq < nr_irqs; irq++ ) { desc = irq_to_desc(irq); cfg = desc->chip_data; if ( !desc->handler || desc->handler == &no_irq_type ) continue; spin_lock_irqsave(&desc->lock, flags); cpumask_scnprintf(keyhandler_scratch, sizeof(keyhandler_scratch), desc->affinity); printk(" IRQ:%4d affinity:%s vec:%02x type=%-15s" " status=%08x ", irq, keyhandler_scratch, cfg->vector, desc->handler->typename, desc->status); if ( !(desc->status & IRQ_GUEST) ) printk("mapped, unbound\n"); else { action = (irq_guest_action_t *)desc->action; printk("in-flight=%d domain-list=", action->in_flight); for ( i = 0; i < action->nr_guests; i++ ) { d = action->guest[i]; pirq = domain_irq_to_pirq(d, irq); printk("%u:%3d(%c%c%c%c)", d->domain_id, pirq, (test_bit(d->pirq_to_evtchn[pirq], &shared_info(d, evtchn_pending)) ? 'P' : '-'), (test_bit(d->pirq_to_evtchn[pirq] / BITS_PER_EVTCHN_WORD(d), &vcpu_info(d->vcpu[0], evtchn_pending_sel)) ? 'S' : '-'), (test_bit(d->pirq_to_evtchn[pirq], &shared_info(d, evtchn_mask)) ? 'M' : '-'), (test_bit(pirq, d->pirq_mask) ? 'M' : '-')); if ( i != action->nr_guests ) printk(","); } printk("\n"); } spin_unlock_irqrestore(&desc->lock, flags); } dump_ioapic_irq_info(); } static struct keyhandler dump_irqs_keyhandler = { .diagnostic = 1, .u.fn = dump_irqs, .desc = "dump interrupt bindings" }; static int __init setup_dump_irqs(void) { register_keyhandler('i', &dump_irqs_keyhandler); return 0; } __initcall(setup_dump_irqs); /* A cpu has been removed from cpu_online_mask. Re-set irq affinities. */ void fixup_irqs(void) { unsigned int irq, sp; static int warned; struct irq_desc *desc; irq_guest_action_t *action; struct pending_eoi *peoi; for ( irq = 0; irq < nr_irqs; irq++ ) { int break_affinity = 0; int set_affinity = 1; cpumask_t affinity; if ( irq == 2 ) continue; desc = irq_to_desc(irq); spin_lock(&desc->lock); affinity = desc->affinity; if ( !desc->action || cpus_subset(affinity, cpu_online_map) ) { spin_unlock(&desc->lock); continue; } cpus_and(affinity, affinity, cpu_online_map); if ( cpus_empty(affinity) ) { break_affinity = 1; affinity = cpu_online_map; } if ( desc->handler->disable ) desc->handler->disable(irq); if ( desc->handler->set_affinity ) desc->handler->set_affinity(irq, affinity); else if ( !(warned++) ) set_affinity = 0; if ( desc->handler->enable ) desc->handler->enable(irq); spin_unlock(&desc->lock); if ( break_affinity && set_affinity ) printk("Broke affinity for irq %i\n", irq); else if ( !set_affinity ) printk("Cannot set affinity for irq %i\n", irq); } /* That doesn't seem sufficient. Give it 1ms. */ local_irq_enable(); mdelay(1); local_irq_disable(); /* Clean up cpu_eoi_map of every interrupt to exclude this CPU. */ for ( irq = 0; irq < nr_irqs; irq++ ) { desc = irq_to_desc(irq); if ( !(desc->status & IRQ_GUEST) ) continue; action = (irq_guest_action_t *)desc->action; cpu_clear(smp_processor_id(), action->cpu_eoi_map); } /* Flush the interrupt EOI stack. */ peoi = this_cpu(pending_eoi); for ( sp = 0; sp < pending_eoi_sp(peoi); sp++ ) peoi[sp].ready = 1; flush_ready_eoi(); } int map_domain_emuirq_pirq(struct domain *d, int pirq, int emuirq) { int old_emuirq = IRQ_UNBOUND, old_pirq = IRQ_UNBOUND; ASSERT(spin_is_locked(&d->event_lock)); if ( !is_hvm_domain(d) ) return -EINVAL; if ( pirq < 0 || pirq >= d->nr_pirqs || emuirq == IRQ_UNBOUND || emuirq >= (int) nr_irqs ) { dprintk(XENLOG_G_ERR, "dom%d: invalid pirq %d or emuirq %d\n", d->domain_id, pirq, emuirq); return -EINVAL; } old_emuirq = domain_pirq_to_emuirq(d, pirq); if ( emuirq != IRQ_PT ) old_pirq = domain_emuirq_to_pirq(d, emuirq); if ( (old_emuirq != IRQ_UNBOUND && (old_emuirq != emuirq) ) || (old_pirq != IRQ_UNBOUND && (old_pirq != pirq)) ) { dprintk(XENLOG_G_WARNING, "dom%d: pirq %d or emuirq %d already mapped\n", d->domain_id, pirq, emuirq); return 0; } d->arch.pirq_emuirq[pirq] = emuirq; /* do not store emuirq mappings for pt devices */ if ( emuirq != IRQ_PT ) d->arch.emuirq_pirq[emuirq] = pirq; return 0; } int unmap_domain_pirq_emuirq(struct domain *d, int pirq) { int emuirq, ret = 0; if ( !is_hvm_domain(d) ) return -EINVAL; if ( (pirq < 0) || (pirq >= d->nr_pirqs) ) return -EINVAL; ASSERT(spin_is_locked(&d->event_lock)); emuirq = domain_pirq_to_emuirq(d, pirq); if ( emuirq == IRQ_UNBOUND ) { dprintk(XENLOG_G_ERR, "dom%d: pirq %d not mapped\n", d->domain_id, pirq); ret = -EINVAL; goto done; } d->arch.pirq_emuirq[pirq] = IRQ_UNBOUND; if ( emuirq != IRQ_PT ) d->arch.emuirq_pirq[emuirq] = IRQ_UNBOUND; done: return ret; } int hvm_domain_use_pirq(struct domain *d, int pirq) { int emuirq; if ( !is_hvm_domain(d) || pirq < 0 ) return 0; emuirq = domain_pirq_to_emuirq(d, pirq); if ( emuirq != IRQ_UNBOUND ) return 1; else return 0; }