3 * Copyright (c) 2011, Microsoft Corporation.
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.
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
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.
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
21 * K. Y. Srinivasan <kys@microsoft.com>
25 #ifndef _HYPERV_VMBUS_H
26 #define _HYPERV_VMBUS_H
28 #include <linux/list.h>
29 #include <asm/sync_bitops.h>
30 #include <linux/atomic.h>
31 #include <linux/hyperv.h>
34 * The below CPUID leaves are present if VersionAndFeatures.HypervisorPresent
35 * is set by CPUID(HVCPUID_VERSION_FEATURES).
37 enum hv_cpuid_function
{
38 HVCPUID_VERSION_FEATURES
= 0x00000001,
39 HVCPUID_VENDOR_MAXFUNCTION
= 0x40000000,
40 HVCPUID_INTERFACE
= 0x40000001,
43 * The remaining functions depend on the value of
46 HVCPUID_VERSION
= 0x40000002,
47 HVCPUID_FEATURES
= 0x40000003,
48 HVCPUID_ENLIGHTENMENT_INFO
= 0x40000004,
49 HVCPUID_IMPLEMENTATION_LIMITS
= 0x40000005,
52 /* Define version of the synthetic interrupt controller. */
53 #define HV_SYNIC_VERSION (1)
55 /* Define the expected SynIC version. */
56 #define HV_SYNIC_VERSION_1 (0x1)
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)
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))
69 /* Define hypervisor message types. */
70 enum hv_message_type
{
71 HVMSG_NONE
= 0x00000000,
73 /* Memory access messages. */
74 HVMSG_UNMAPPED_GPA
= 0x80000000,
75 HVMSG_GPA_INTERCEPT
= 0x80000001,
77 /* Timer notification messages. */
78 HVMSG_TIMER_EXPIRED
= 0x80000010,
81 HVMSG_INVALID_VP_REGISTER_VALUE
= 0x80000020,
82 HVMSG_UNRECOVERABLE_EXCEPTION
= 0x80000021,
83 HVMSG_UNSUPPORTED_FEATURE
= 0x80000022,
85 /* Trace buffer complete messages. */
86 HVMSG_EVENTLOG_BUFFERCOMPLETE
= 0x80000040,
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
97 /* Define the number of synthetic interrupt sources. */
98 #define HV_SYNIC_SINT_COUNT (16)
99 #define HV_SYNIC_STIMER_COUNT (4)
101 /* Define invalid partition identifier. */
102 #define HV_PARTITION_ID_INVALID ((u64)0x0)
104 /* Define connection identifier type. */
105 union hv_connection_id
{
113 /* Define port identifier type. */
122 /* Define port type. */
129 /* Define port information structure. */
130 struct hv_port_info
{
131 enum hv_port_type port_type
;
142 u16 base_flag_bumber
;
153 struct hv_connection_info
{
154 enum hv_port_type port_type
;
159 } message_connection_info
;
162 } event_connection_info
;
165 } monitor_connection_info
;
169 /* Define synthetic interrupt controller message flags. */
170 union hv_message_flags
{
178 /* Define synthetic interrupt controller message header. */
179 struct hv_message_header
{
180 enum hv_message_type message_type
;
182 union hv_message_flags message_flags
;
186 union hv_port_id port
;
190 /* Define timer message payload structure. */
191 struct hv_timer_message_payload
{
194 u64 expiration_time
; /* When the timer expired */
195 u64 delivery_time
; /* When the message was delivered */
198 /* Define synthetic interrupt controller message format. */
200 struct hv_message_header header
;
202 u64 payload
[HV_MESSAGE_PAYLOAD_QWORD_COUNT
];
206 /* Define the number of message buffers associated with each port. */
207 #define HV_PORT_MESSAGE_BUFFER_COUNT (16)
209 /* Define the synthetic interrupt message page layout. */
210 struct hv_message_page
{
211 struct hv_message sint_message
[HV_SYNIC_SINT_COUNT
];
214 /* Define the synthetic interrupt controller event flags format. */
215 union hv_synic_event_flags
{
216 u8 flags8
[HV_EVENT_FLAGS_BYTE_COUNT
];
217 u32 flags32
[HV_EVENT_FLAGS_DWORD_COUNT
];
220 /* Define the synthetic interrupt flags page layout. */
221 struct hv_synic_event_flags_page
{
222 union hv_synic_event_flags sintevent_flags
[HV_SYNIC_SINT_COUNT
];
225 /* Define SynIC control register. */
226 union hv_synic_scontrol
{
234 /* Define synthetic interrupt source. */
235 union hv_synic_sint
{
246 /* Define the format of the SIMP register */
247 union hv_synic_simp
{
252 u64 base_simp_gpa
:52;
256 /* Define the format of the SIEFP register */
257 union hv_synic_siefp
{
262 u64 base_siefp_gpa
:52;
266 /* Definitions for the monitored notification facility */
267 union hv_monitor_trigger_group
{
275 struct hv_monitor_parameter
{
276 union hv_connection_id connectionid
;
281 union hv_monitor_trigger_state
{
290 /* struct hv_monitor_page Layout */
291 /* ------------------------------------------------------ */
292 /* | 0 | TriggerState (4 bytes) | Rsvd1 (4 bytes) | */
293 /* | 8 | TriggerGroup[0] | */
294 /* | 10 | TriggerGroup[1] | */
295 /* | 18 | TriggerGroup[2] | */
296 /* | 20 | TriggerGroup[3] | */
297 /* | 28 | Rsvd2[0] | */
298 /* | 30 | Rsvd2[1] | */
299 /* | 38 | Rsvd2[2] | */
300 /* | 40 | NextCheckTime[0][0] | NextCheckTime[0][1] | */
302 /* | 240 | Latency[0][0..3] | */
303 /* | 340 | Rsvz3[0] | */
304 /* | 440 | Parameter[0][0] | */
305 /* | 448 | Parameter[0][1] | */
307 /* | 840 | Rsvd4[0] | */
308 /* ------------------------------------------------------ */
309 struct hv_monitor_page
{
310 union hv_monitor_trigger_state trigger_state
;
313 union hv_monitor_trigger_group trigger_group
[4];
316 s32 next_checktime
[4][32];
321 struct hv_monitor_parameter parameter
[4][32];
326 /* Declare the various hypercall operations. */
328 HVCALL_POST_MESSAGE
= 0x005c,
329 HVCALL_SIGNAL_EVENT
= 0x005d,
332 /* Definition of the hv_post_message hypercall input structure. */
333 struct hv_input_post_message
{
334 union hv_connection_id connectionid
;
336 enum hv_message_type message_type
;
338 u64 payload
[HV_MESSAGE_PAYLOAD_QWORD_COUNT
];
341 /* Definition of the hv_signal_event hypercall input structure. */
342 struct hv_input_signal_event
{
343 union hv_connection_id connectionid
;
349 * Versioning definitions used for guests reporting themselves to the
350 * hypervisor, and visa versa.
353 /* Version info reported by guest OS's */
354 enum hv_guest_os_vendor
{
355 HVGUESTOS_VENDOR_MICROSOFT
= 0x0001
358 enum hv_guest_os_microsoft_ids
{
359 HVGUESTOS_MICROSOFT_UNDEFINED
= 0x00,
360 HVGUESTOS_MICROSOFT_MSDOS
= 0x01,
361 HVGUESTOS_MICROSOFT_WINDOWS3X
= 0x02,
362 HVGUESTOS_MICROSOFT_WINDOWS9X
= 0x03,
363 HVGUESTOS_MICROSOFT_WINDOWSNT
= 0x04,
364 HVGUESTOS_MICROSOFT_WINDOWSCE
= 0x05
368 * Declare the MSR used to identify the guest OS.
370 #define HV_X64_MSR_GUEST_OS_ID 0x40000000
372 union hv_x64_msr_guest_os_id_contents
{
376 u64 service_version
:8; /* Service Pack, etc. */
379 u64 os_id
:8; /* enum hv_guest_os_microsoft_ids (if Vendor=MS) */
380 u64 vendor_id
:16; /* enum hv_guest_os_vendor */
385 * Declare the MSR used to setup pages used to communicate with the hypervisor.
387 #define HV_X64_MSR_HYPERCALL 0x40000001
389 union hv_x64_msr_hypercall_contents
{
394 u64 guest_physical_address
:52;
400 VMBUS_MESSAGE_CONNECTION_ID
= 1,
401 VMBUS_MESSAGE_PORT_ID
= 1,
402 VMBUS_EVENT_CONNECTION_ID
= 2,
403 VMBUS_EVENT_PORT_ID
= 2,
404 VMBUS_MONITOR_CONNECTION_ID
= 3,
405 VMBUS_MONITOR_PORT_ID
= 3,
406 VMBUS_MESSAGE_SINT
= 2,
411 #define HV_PRESENT_BIT 0x80000000
413 #define HV_LINUX_GUEST_ID_LO 0x00000000
414 #define HV_LINUX_GUEST_ID_HI 0xB16B00B5
415 #define HV_LINUX_GUEST_ID (((u64)HV_LINUX_GUEST_ID_HI << 32) | \
416 HV_LINUX_GUEST_ID_LO)
418 #define HV_CPU_POWER_MANAGEMENT (1 << 0)
419 #define HV_RECOMMENDATIONS_MAX 4
422 #define HV_CAPS_MAX 8
425 #define HV_HYPERCALL_PARAM_ALIGN sizeof(u64)
428 /* Service definitions */
430 #define HV_SERVICE_PARENT_PORT (0)
431 #define HV_SERVICE_PARENT_CONNECTION (0)
433 #define HV_SERVICE_CONNECT_RESPONSE_SUCCESS (0)
434 #define HV_SERVICE_CONNECT_RESPONSE_INVALID_PARAMETER (1)
435 #define HV_SERVICE_CONNECT_RESPONSE_UNKNOWN_SERVICE (2)
436 #define HV_SERVICE_CONNECT_RESPONSE_CONNECTION_REJECTED (3)
438 #define HV_SERVICE_CONNECT_REQUEST_MESSAGE_ID (1)
439 #define HV_SERVICE_CONNECT_RESPONSE_MESSAGE_ID (2)
440 #define HV_SERVICE_DISCONNECT_REQUEST_MESSAGE_ID (3)
441 #define HV_SERVICE_DISCONNECT_RESPONSE_MESSAGE_ID (4)
442 #define HV_SERVICE_MAX_MESSAGE_ID (4)
444 #define HV_SERVICE_PROTOCOL_VERSION (0x0010)
445 #define HV_CONNECT_PAYLOAD_BYTE_COUNT 64
447 /* #define VMBUS_REVISION_NUMBER 6 */
449 /* Our local vmbus's port and connection id. Anything >0 is fine */
450 /* #define VMBUS_PORT_ID 11 */
452 /* 628180B8-308D-4c5e-B7DB-1BEB62E62EF4 */
453 static const uuid_le VMBUS_SERVICE_ID
= {
455 0xb8, 0x80, 0x81, 0x62, 0x8d, 0x30, 0x5e, 0x4c,
456 0xb7, 0xdb, 0x1b, 0xeb, 0x62, 0xe6, 0x2e, 0xf4
460 #define MAX_NUM_CPUS 32
463 struct hv_input_signal_event_buffer
{
465 struct hv_input_signal_event event
;
469 /* We only support running on top of Hyper-V
470 * So at this point this really can only contain the Hyper-V ID
474 void *hypercall_page
;
476 bool synic_initialized
;
479 * This is used as an input param to HvCallSignalEvent hypercall. The
480 * input param is immutable in our usage and must be dynamic mem (vs
481 * stack or global). */
482 struct hv_input_signal_event_buffer
*signal_event_buffer
;
483 /* 8-bytes aligned of the buffer above */
484 struct hv_input_signal_event
*signal_event_param
;
486 void *synic_message_page
[MAX_NUM_CPUS
];
487 void *synic_event_page
[MAX_NUM_CPUS
];
489 * buffer to post messages to the host.
491 void *post_msg_page
[MAX_NUM_CPUS
];
494 extern struct hv_context hv_context
;
499 extern int hv_init(void);
501 extern void hv_cleanup(void);
503 extern u16
hv_post_message(union hv_connection_id connection_id
,
504 enum hv_message_type message_type
,
505 void *payload
, size_t payload_size
);
507 extern u16
hv_signal_event(void);
509 extern void hv_synic_init(void *irqarg
);
511 extern void hv_synic_cleanup(void *arg
);
517 int hv_ringbuffer_init(struct hv_ring_buffer_info
*ring_info
, void *buffer
,
520 void hv_ringbuffer_cleanup(struct hv_ring_buffer_info
*ring_info
);
522 int hv_ringbuffer_write(struct hv_ring_buffer_info
*ring_info
,
523 struct scatterlist
*sglist
,
526 int hv_ringbuffer_peek(struct hv_ring_buffer_info
*ring_info
, void *buffer
,
529 int hv_ringbuffer_read(struct hv_ring_buffer_info
*ring_info
,
534 u32
hv_get_ringbuffer_interrupt_mask(struct hv_ring_buffer_info
*ring_info
);
536 void hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info
*ring_info
,
537 struct hv_ring_buffer_debug_info
*debug_info
);
540 * Maximum channels is determined by the size of the interrupt page
541 * which is PAGE_SIZE. 1/2 of PAGE_SIZE is for send endpoint interrupt
542 * and the other is receive endpoint interrupt
544 #define MAX_NUM_CHANNELS ((PAGE_SIZE >> 1) << 3) /* 16348 channels */
546 /* The value here must be in multiple of 32 */
547 /* TODO: Need to make this configurable */
548 #define MAX_NUM_CHANNELS_SUPPORTED 256
551 enum vmbus_connect_state
{
558 #define MAX_SIZE_CHANNEL_MESSAGE HV_MESSAGE_PAYLOAD_BYTE_COUNT
560 struct vmbus_connection
{
561 enum vmbus_connect_state conn_state
;
563 atomic_t next_gpadl_handle
;
566 * Represents channel interrupts. Each bit position represents a
567 * channel. When a channel sends an interrupt via VMBUS, it finds its
568 * bit in the sendInterruptPage, set it and calls Hv to generate a port
569 * event. The other end receives the port event and parse the
570 * recvInterruptPage to see which bit is set
577 * 2 pages - 1st page for parent->child notification and 2nd
578 * is child->parent notification
581 struct list_head chn_msg_list
;
582 spinlock_t channelmsg_lock
;
584 /* List of channels */
585 struct list_head chn_list
;
586 spinlock_t channel_lock
;
588 struct workqueue_struct
*work_queue
;
592 struct vmbus_msginfo
{
593 /* Bookkeeping stuff */
594 struct list_head msglist_entry
;
596 /* The message itself */
597 unsigned char msg
[0];
601 extern struct vmbus_connection vmbus_connection
;
603 /* General vmbus interface */
605 struct hv_device
*vmbus_device_create(uuid_le
*type
,
607 struct vmbus_channel
*channel
);
609 int vmbus_device_register(struct hv_device
*child_device_obj
);
610 void vmbus_device_unregister(struct hv_device
*device_obj
);
613 /* VmbusChildDeviceDestroy( */
614 /* struct hv_device *); */
616 struct vmbus_channel
*relid2channel(u32 relid
);
619 /* Connection interface */
621 int vmbus_connect(void);
623 int vmbus_post_msg(void *buffer
, size_t buflen
);
625 int vmbus_set_event(u32 child_relid
);
627 void vmbus_on_event(unsigned long data
);
630 #endif /* _HYPERV_VMBUS_H */