1 /* 2 * 3 * Copyright (c) 2011, Microsoft Corporation. 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms and conditions of the GNU General Public License, 7 * version 2, as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 * more details. 13 * 14 * You should have received a copy of the GNU General Public License along with 15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple 16 * Place - Suite 330, Boston, MA 02111-1307 USA. 17 * 18 * Authors: 19 * Haiyang Zhang <haiyangz@microsoft.com> 20 * Hank Janssen <hjanssen@microsoft.com> 21 * K. Y. Srinivasan <kys@microsoft.com> 22 * 23 */ 24 25 #ifndef _HYPERV_VMBUS_H 26 #define _HYPERV_VMBUS_H 27 28 #include <linux/list.h> 29 #include <asm/sync_bitops.h> 30 #include <linux/atomic.h> 31 #include <linux/hyperv.h> 32 33 /* 34 * The below CPUID leaves are present if VersionAndFeatures.HypervisorPresent 35 * is set by CPUID(HVCPUID_VERSION_FEATURES). 36 */ 37 enum hv_cpuid_function { 38 HVCPUID_VERSION_FEATURES = 0x00000001, 39 HVCPUID_VENDOR_MAXFUNCTION = 0x40000000, 40 HVCPUID_INTERFACE = 0x40000001, 41 42 /* 43 * The remaining functions depend on the value of 44 * HVCPUID_INTERFACE 45 */ 46 HVCPUID_VERSION = 0x40000002, 47 HVCPUID_FEATURES = 0x40000003, 48 HVCPUID_ENLIGHTENMENT_INFO = 0x40000004, 49 HVCPUID_IMPLEMENTATION_LIMITS = 0x40000005, 50 }; 51 52 /* Define version of the synthetic interrupt controller. */ 53 #define HV_SYNIC_VERSION (1) 54 55 /* Define the expected SynIC version. */ 56 #define HV_SYNIC_VERSION_1 (0x1) 57 58 /* Define synthetic interrupt controller message constants. */ 59 #define HV_MESSAGE_SIZE (256) 60 #define HV_MESSAGE_PAYLOAD_BYTE_COUNT (240) 61 #define HV_MESSAGE_PAYLOAD_QWORD_COUNT (30) 62 #define HV_ANY_VP (0xFFFFFFFF) 63 64 /* Define synthetic interrupt controller flag constants. */ 65 #define HV_EVENT_FLAGS_COUNT (256 * 8) 66 #define HV_EVENT_FLAGS_BYTE_COUNT (256) 67 #define HV_EVENT_FLAGS_DWORD_COUNT (256 / sizeof(u32)) 68 69 /* Define hypervisor message types. */ 70 enum hv_message_type { 71 HVMSG_NONE = 0x00000000, 72 73 /* Memory access messages. */ 74 HVMSG_UNMAPPED_GPA = 0x80000000, 75 HVMSG_GPA_INTERCEPT = 0x80000001, 76 77 /* Timer notification messages. */ 78 HVMSG_TIMER_EXPIRED = 0x80000010, 79 80 /* Error messages. */ 81 HVMSG_INVALID_VP_REGISTER_VALUE = 0x80000020, 82 HVMSG_UNRECOVERABLE_EXCEPTION = 0x80000021, 83 HVMSG_UNSUPPORTED_FEATURE = 0x80000022, 84 85 /* Trace buffer complete messages. */ 86 HVMSG_EVENTLOG_BUFFERCOMPLETE = 0x80000040, 87 88 /* Platform-specific processor intercept messages. */ 89 HVMSG_X64_IOPORT_INTERCEPT = 0x80010000, 90 HVMSG_X64_MSR_INTERCEPT = 0x80010001, 91 HVMSG_X64_CPUID_INTERCEPT = 0x80010002, 92 HVMSG_X64_EXCEPTION_INTERCEPT = 0x80010003, 93 HVMSG_X64_APIC_EOI = 0x80010004, 94 HVMSG_X64_LEGACY_FP_ERROR = 0x80010005 95 }; 96 97 /* Define the number of synthetic interrupt sources. */ 98 #define HV_SYNIC_SINT_COUNT (16) 99 #define HV_SYNIC_STIMER_COUNT (4) 100 101 /* Define invalid partition identifier. */ 102 #define HV_PARTITION_ID_INVALID ((u64)0x0) 103 104 /* Define port identifier type. */ 105 union hv_port_id { 106 u32 asu32; 107 struct { 108 u32 id:24; 109 u32 reserved:8; 110 } u ; 111 }; 112 113 /* Define port type. */ 114 enum hv_port_type { 115 HVPORT_MSG = 1, 116 HVPORT_EVENT = 2, 117 HVPORT_MONITOR = 3 118 }; 119 120 /* Define port information structure. */ 121 struct hv_port_info { 122 enum hv_port_type port_type; 123 u32 padding; 124 union { 125 struct { 126 u32 target_sint; 127 u32 target_vp; 128 u64 rsvdz; 129 } message_port_info; 130 struct { 131 u32 target_sint; 132 u32 target_vp; 133 u16 base_flag_bumber; 134 u16 flag_count; 135 u32 rsvdz; 136 } event_port_info; 137 struct { 138 u64 monitor_address; 139 u64 rsvdz; 140 } monitor_port_info; 141 }; 142 }; 143 144 struct hv_connection_info { 145 enum hv_port_type port_type; 146 u32 padding; 147 union { 148 struct { 149 u64 rsvdz; 150 } message_connection_info; 151 struct { 152 u64 rsvdz; 153 } event_connection_info; 154 struct { 155 u64 monitor_address; 156 } monitor_connection_info; 157 }; 158 }; 159 160 /* Define synthetic interrupt controller message flags. */ 161 union hv_message_flags { 162 u8 asu8; 163 struct { 164 u8 msg_pending:1; 165 u8 reserved:7; 166 }; 167 }; 168 169 /* Define synthetic interrupt controller message header. */ 170 struct hv_message_header { 171 enum hv_message_type message_type; 172 u8 payload_size; 173 union hv_message_flags message_flags; 174 u8 reserved[2]; 175 union { 176 u64 sender; 177 union hv_port_id port; 178 }; 179 }; 180 181 /* 182 * Timer configuration register. 183 */ 184 union hv_timer_config { 185 u64 as_uint64; 186 struct { 187 u64 enable:1; 188 u64 periodic:1; 189 u64 lazy:1; 190 u64 auto_enable:1; 191 u64 reserved_z0:12; 192 u64 sintx:4; 193 u64 reserved_z1:44; 194 }; 195 }; 196 197 198 /* Define timer message payload structure. */ 199 struct hv_timer_message_payload { 200 u32 timer_index; 201 u32 reserved; 202 u64 expiration_time; /* When the timer expired */ 203 u64 delivery_time; /* When the message was delivered */ 204 }; 205 206 /* Define synthetic interrupt controller message format. */ 207 struct hv_message { 208 struct hv_message_header header; 209 union { 210 u64 payload[HV_MESSAGE_PAYLOAD_QWORD_COUNT]; 211 } u ; 212 }; 213 214 /* Define the number of message buffers associated with each port. */ 215 #define HV_PORT_MESSAGE_BUFFER_COUNT (16) 216 217 /* Define the synthetic interrupt message page layout. */ 218 struct hv_message_page { 219 struct hv_message sint_message[HV_SYNIC_SINT_COUNT]; 220 }; 221 222 /* Define the synthetic interrupt controller event flags format. */ 223 union hv_synic_event_flags { 224 u8 flags8[HV_EVENT_FLAGS_BYTE_COUNT]; 225 u32 flags32[HV_EVENT_FLAGS_DWORD_COUNT]; 226 }; 227 228 /* Define the synthetic interrupt flags page layout. */ 229 struct hv_synic_event_flags_page { 230 union hv_synic_event_flags sintevent_flags[HV_SYNIC_SINT_COUNT]; 231 }; 232 233 /* Define SynIC control register. */ 234 union hv_synic_scontrol { 235 u64 as_uint64; 236 struct { 237 u64 enable:1; 238 u64 reserved:63; 239 }; 240 }; 241 242 /* Define synthetic interrupt source. */ 243 union hv_synic_sint { 244 u64 as_uint64; 245 struct { 246 u64 vector:8; 247 u64 reserved1:8; 248 u64 masked:1; 249 u64 auto_eoi:1; 250 u64 reserved2:46; 251 }; 252 }; 253 254 /* Define the format of the SIMP register */ 255 union hv_synic_simp { 256 u64 as_uint64; 257 struct { 258 u64 simp_enabled:1; 259 u64 preserved:11; 260 u64 base_simp_gpa:52; 261 }; 262 }; 263 264 /* Define the format of the SIEFP register */ 265 union hv_synic_siefp { 266 u64 as_uint64; 267 struct { 268 u64 siefp_enabled:1; 269 u64 preserved:11; 270 u64 base_siefp_gpa:52; 271 }; 272 }; 273 274 /* Definitions for the monitored notification facility */ 275 union hv_monitor_trigger_group { 276 u64 as_uint64; 277 struct { 278 u32 pending; 279 u32 armed; 280 }; 281 }; 282 283 struct hv_monitor_parameter { 284 union hv_connection_id connectionid; 285 u16 flagnumber; 286 u16 rsvdz; 287 }; 288 289 union hv_monitor_trigger_state { 290 u32 asu32; 291 292 struct { 293 u32 group_enable:4; 294 u32 rsvdz:28; 295 }; 296 }; 297 298 /* struct hv_monitor_page Layout */ 299 /* ------------------------------------------------------ */ 300 /* | 0 | TriggerState (4 bytes) | Rsvd1 (4 bytes) | */ 301 /* | 8 | TriggerGroup[0] | */ 302 /* | 10 | TriggerGroup[1] | */ 303 /* | 18 | TriggerGroup[2] | */ 304 /* | 20 | TriggerGroup[3] | */ 305 /* | 28 | Rsvd2[0] | */ 306 /* | 30 | Rsvd2[1] | */ 307 /* | 38 | Rsvd2[2] | */ 308 /* | 40 | NextCheckTime[0][0] | NextCheckTime[0][1] | */ 309 /* | ... | */ 310 /* | 240 | Latency[0][0..3] | */ 311 /* | 340 | Rsvz3[0] | */ 312 /* | 440 | Parameter[0][0] | */ 313 /* | 448 | Parameter[0][1] | */ 314 /* | ... | */ 315 /* | 840 | Rsvd4[0] | */ 316 /* ------------------------------------------------------ */ 317 struct hv_monitor_page { 318 union hv_monitor_trigger_state trigger_state; 319 u32 rsvdz1; 320 321 union hv_monitor_trigger_group trigger_group[4]; 322 u64 rsvdz2[3]; 323 324 s32 next_checktime[4][32]; 325 326 u16 latency[4][32]; 327 u64 rsvdz3[32]; 328 329 struct hv_monitor_parameter parameter[4][32]; 330 331 u8 rsvdz4[1984]; 332 }; 333 334 /* Declare the various hypercall operations. */ 335 enum hv_call_code { 336 HVCALL_POST_MESSAGE = 0x005c, 337 HVCALL_SIGNAL_EVENT = 0x005d, 338 }; 339 340 /* Definition of the hv_post_message hypercall input structure. */ 341 struct hv_input_post_message { 342 union hv_connection_id connectionid; 343 u32 reserved; 344 enum hv_message_type message_type; 345 u32 payload_size; 346 u64 payload[HV_MESSAGE_PAYLOAD_QWORD_COUNT]; 347 }; 348 349 /* 350 * Versioning definitions used for guests reporting themselves to the 351 * hypervisor, and visa versa. 352 */ 353 354 /* Version info reported by guest OS's */ 355 enum hv_guest_os_vendor { 356 HVGUESTOS_VENDOR_MICROSOFT = 0x0001 357 }; 358 359 enum hv_guest_os_microsoft_ids { 360 HVGUESTOS_MICROSOFT_UNDEFINED = 0x00, 361 HVGUESTOS_MICROSOFT_MSDOS = 0x01, 362 HVGUESTOS_MICROSOFT_WINDOWS3X = 0x02, 363 HVGUESTOS_MICROSOFT_WINDOWS9X = 0x03, 364 HVGUESTOS_MICROSOFT_WINDOWSNT = 0x04, 365 HVGUESTOS_MICROSOFT_WINDOWSCE = 0x05 366 }; 367 368 /* 369 * Declare the MSR used to identify the guest OS. 370 */ 371 #define HV_X64_MSR_GUEST_OS_ID 0x40000000 372 373 union hv_x64_msr_guest_os_id_contents { 374 u64 as_uint64; 375 struct { 376 u64 build_number:16; 377 u64 service_version:8; /* Service Pack, etc. */ 378 u64 minor_version:8; 379 u64 major_version:8; 380 u64 os_id:8; /* enum hv_guest_os_microsoft_ids (if Vendor=MS) */ 381 u64 vendor_id:16; /* enum hv_guest_os_vendor */ 382 }; 383 }; 384 385 /* 386 * Declare the MSR used to setup pages used to communicate with the hypervisor. 387 */ 388 #define HV_X64_MSR_HYPERCALL 0x40000001 389 390 union hv_x64_msr_hypercall_contents { 391 u64 as_uint64; 392 struct { 393 u64 enable:1; 394 u64 reserved:11; 395 u64 guest_physical_address:52; 396 }; 397 }; 398 399 400 enum { 401 VMBUS_MESSAGE_CONNECTION_ID = 1, 402 VMBUS_MESSAGE_PORT_ID = 1, 403 VMBUS_EVENT_CONNECTION_ID = 2, 404 VMBUS_EVENT_PORT_ID = 2, 405 VMBUS_MONITOR_CONNECTION_ID = 3, 406 VMBUS_MONITOR_PORT_ID = 3, 407 VMBUS_MESSAGE_SINT = 2, 408 }; 409 410 /* #defines */ 411 412 #define HV_PRESENT_BIT 0x80000000 413 414 /* 415 * The guest OS needs to register the guest ID with the hypervisor. 416 * The guest ID is a 64 bit entity and the structure of this ID is 417 * specified in the Hyper-V specification: 418 * 419 * http://msdn.microsoft.com/en-us/library/windows/hardware/ff542653%28v=vs.85%29.aspx 420 * 421 * While the current guideline does not specify how Linux guest ID(s) 422 * need to be generated, our plan is to publish the guidelines for 423 * Linux and other guest operating systems that currently are hosted 424 * on Hyper-V. The implementation here conforms to this yet 425 * unpublished guidelines. 426 * 427 * 428 * Bit(s) 429 * 63 - Indicates if the OS is Open Source or not; 1 is Open Source 430 * 62:56 - Os Type; Linux is 0x100 431 * 55:48 - Distro specific identification 432 * 47:16 - Linux kernel version number 433 * 15:0 - Distro specific identification 434 * 435 * 436 */ 437 438 #define HV_LINUX_VENDOR_ID 0x8100 439 440 /* 441 * Generate the guest ID based on the guideline described above. 442 */ 443 444 static inline __u64 generate_guest_id(__u8 d_info1, __u32 kernel_version, 445 __u16 d_info2) 446 { 447 __u64 guest_id = 0; 448 449 guest_id = (((__u64)HV_LINUX_VENDOR_ID) << 48); 450 guest_id |= (((__u64)(d_info1)) << 48); 451 guest_id |= (((__u64)(kernel_version)) << 16); 452 guest_id |= ((__u64)(d_info2)); 453 454 return guest_id; 455 } 456 457 458 #define HV_CPU_POWER_MANAGEMENT (1 << 0) 459 #define HV_RECOMMENDATIONS_MAX 4 460 461 #define HV_X64_MAX 5 462 #define HV_CAPS_MAX 8 463 464 465 #define HV_HYPERCALL_PARAM_ALIGN sizeof(u64) 466 467 468 /* Service definitions */ 469 470 #define HV_SERVICE_PARENT_PORT (0) 471 #define HV_SERVICE_PARENT_CONNECTION (0) 472 473 #define HV_SERVICE_CONNECT_RESPONSE_SUCCESS (0) 474 #define HV_SERVICE_CONNECT_RESPONSE_INVALID_PARAMETER (1) 475 #define HV_SERVICE_CONNECT_RESPONSE_UNKNOWN_SERVICE (2) 476 #define HV_SERVICE_CONNECT_RESPONSE_CONNECTION_REJECTED (3) 477 478 #define HV_SERVICE_CONNECT_REQUEST_MESSAGE_ID (1) 479 #define HV_SERVICE_CONNECT_RESPONSE_MESSAGE_ID (2) 480 #define HV_SERVICE_DISCONNECT_REQUEST_MESSAGE_ID (3) 481 #define HV_SERVICE_DISCONNECT_RESPONSE_MESSAGE_ID (4) 482 #define HV_SERVICE_MAX_MESSAGE_ID (4) 483 484 #define HV_SERVICE_PROTOCOL_VERSION (0x0010) 485 #define HV_CONNECT_PAYLOAD_BYTE_COUNT 64 486 487 /* #define VMBUS_REVISION_NUMBER 6 */ 488 489 /* Our local vmbus's port and connection id. Anything >0 is fine */ 490 /* #define VMBUS_PORT_ID 11 */ 491 492 /* 628180B8-308D-4c5e-B7DB-1BEB62E62EF4 */ 493 static const uuid_le VMBUS_SERVICE_ID = { 494 .b = { 495 0xb8, 0x80, 0x81, 0x62, 0x8d, 0x30, 0x5e, 0x4c, 496 0xb7, 0xdb, 0x1b, 0xeb, 0x62, 0xe6, 0x2e, 0xf4 497 }, 498 }; 499 500 501 502 struct hv_context { 503 /* We only support running on top of Hyper-V 504 * So at this point this really can only contain the Hyper-V ID 505 */ 506 u64 guestid; 507 508 void *hypercall_page; 509 510 bool synic_initialized; 511 512 void *synic_message_page[NR_CPUS]; 513 void *synic_event_page[NR_CPUS]; 514 /* 515 * Hypervisor's notion of virtual processor ID is different from 516 * Linux' notion of CPU ID. This information can only be retrieved 517 * in the context of the calling CPU. Setup a map for easy access 518 * to this information: 519 * 520 * vp_index[a] is the Hyper-V's processor ID corresponding to 521 * Linux cpuid 'a'. 522 */ 523 u32 vp_index[NR_CPUS]; 524 /* 525 * Starting with win8, we can take channel interrupts on any CPU; 526 * we will manage the tasklet that handles events on a per CPU 527 * basis. 528 */ 529 struct tasklet_struct *event_dpc[NR_CPUS]; 530 /* 531 * To optimize the mapping of relid to channel, maintain 532 * per-cpu list of the channels based on their CPU affinity. 533 */ 534 struct list_head percpu_list[NR_CPUS]; 535 /* 536 * buffer to post messages to the host. 537 */ 538 void *post_msg_page[NR_CPUS]; 539 /* 540 * Support PV clockevent device. 541 */ 542 struct clock_event_device *clk_evt[NR_CPUS]; 543 }; 544 545 extern struct hv_context hv_context; 546 547 struct hv_ring_buffer_debug_info { 548 u32 current_interrupt_mask; 549 u32 current_read_index; 550 u32 current_write_index; 551 u32 bytes_avail_toread; 552 u32 bytes_avail_towrite; 553 }; 554 555 /* Hv Interface */ 556 557 extern int hv_init(void); 558 559 extern void hv_cleanup(void); 560 561 extern int hv_post_message(union hv_connection_id connection_id, 562 enum hv_message_type message_type, 563 void *payload, size_t payload_size); 564 565 extern u16 hv_signal_event(void *con_id); 566 567 extern int hv_synic_alloc(void); 568 569 extern void hv_synic_free(void); 570 571 extern void hv_synic_init(void *irqarg); 572 573 extern void hv_synic_cleanup(void *arg); 574 575 /* 576 * Host version information. 577 */ 578 extern unsigned int host_info_eax; 579 extern unsigned int host_info_ebx; 580 extern unsigned int host_info_ecx; 581 extern unsigned int host_info_edx; 582 583 /* Interface */ 584 585 586 int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info, void *buffer, 587 u32 buflen); 588 589 void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info); 590 591 int hv_ringbuffer_write(struct hv_ring_buffer_info *ring_info, 592 struct kvec *kv_list, 593 u32 kv_count, bool *signal); 594 595 int hv_ringbuffer_peek(struct hv_ring_buffer_info *ring_info, void *buffer, 596 u32 buflen); 597 598 int hv_ringbuffer_read(struct hv_ring_buffer_info *ring_info, 599 void *buffer, 600 u32 buflen, 601 u32 offset, bool *signal); 602 603 604 void hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info, 605 struct hv_ring_buffer_debug_info *debug_info); 606 607 void hv_begin_read(struct hv_ring_buffer_info *rbi); 608 609 u32 hv_end_read(struct hv_ring_buffer_info *rbi); 610 611 /* 612 * Maximum channels is determined by the size of the interrupt page 613 * which is PAGE_SIZE. 1/2 of PAGE_SIZE is for send endpoint interrupt 614 * and the other is receive endpoint interrupt 615 */ 616 #define MAX_NUM_CHANNELS ((PAGE_SIZE >> 1) << 3) /* 16348 channels */ 617 618 /* The value here must be in multiple of 32 */ 619 /* TODO: Need to make this configurable */ 620 #define MAX_NUM_CHANNELS_SUPPORTED 256 621 622 623 enum vmbus_connect_state { 624 DISCONNECTED, 625 CONNECTING, 626 CONNECTED, 627 DISCONNECTING 628 }; 629 630 #define MAX_SIZE_CHANNEL_MESSAGE HV_MESSAGE_PAYLOAD_BYTE_COUNT 631 632 struct vmbus_connection { 633 enum vmbus_connect_state conn_state; 634 635 atomic_t next_gpadl_handle; 636 637 /* 638 * Represents channel interrupts. Each bit position represents a 639 * channel. When a channel sends an interrupt via VMBUS, it finds its 640 * bit in the sendInterruptPage, set it and calls Hv to generate a port 641 * event. The other end receives the port event and parse the 642 * recvInterruptPage to see which bit is set 643 */ 644 void *int_page; 645 void *send_int_page; 646 void *recv_int_page; 647 648 /* 649 * 2 pages - 1st page for parent->child notification and 2nd 650 * is child->parent notification 651 */ 652 struct hv_monitor_page *monitor_pages[2]; 653 struct list_head chn_msg_list; 654 spinlock_t channelmsg_lock; 655 656 /* List of channels */ 657 struct list_head chn_list; 658 spinlock_t channel_lock; 659 660 struct workqueue_struct *work_queue; 661 }; 662 663 664 struct vmbus_msginfo { 665 /* Bookkeeping stuff */ 666 struct list_head msglist_entry; 667 668 /* The message itself */ 669 unsigned char msg[0]; 670 }; 671 672 673 extern struct vmbus_connection vmbus_connection; 674 675 /* General vmbus interface */ 676 677 struct hv_device *vmbus_device_create(const uuid_le *type, 678 const uuid_le *instance, 679 struct vmbus_channel *channel); 680 681 int vmbus_device_register(struct hv_device *child_device_obj); 682 void vmbus_device_unregister(struct hv_device *device_obj); 683 684 /* static void */ 685 /* VmbusChildDeviceDestroy( */ 686 /* struct hv_device *); */ 687 688 struct vmbus_channel *relid2channel(u32 relid); 689 690 void vmbus_free_channels(void); 691 692 /* Connection interface */ 693 694 int vmbus_connect(void); 695 696 int vmbus_post_msg(void *buffer, size_t buflen); 697 698 int vmbus_set_event(struct vmbus_channel *channel); 699 700 void vmbus_on_event(unsigned long data); 701 702 int hv_fcopy_init(struct hv_util_service *); 703 void hv_fcopy_deinit(void); 704 void hv_fcopy_onchannelcallback(void *); 705 706 707 #endif /* _HYPERV_VMBUS_H */ 708