Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/upstream-linus
[linux-btrfs-devel.git] / drivers / staging / hv / hyperv_vmbus.h
blob349ad80ce32846c72f3b57c64ff824d01be794d5
1 /*
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
12 * more details.
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.
18 * Authors:
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>
32 #include "hyperv.h"
35 * The below CPUID leaves are present if VersionAndFeatures.HypervisorPresent
36 * is set by CPUID(HVCPUID_VERSION_FEATURES).
38 enum hv_cpuid_function {
39 HVCPUID_VERSION_FEATURES = 0x00000001,
40 HVCPUID_VENDOR_MAXFUNCTION = 0x40000000,
41 HVCPUID_INTERFACE = 0x40000001,
44 * The remaining functions depend on the value of
45 * HVCPUID_INTERFACE
47 HVCPUID_VERSION = 0x40000002,
48 HVCPUID_FEATURES = 0x40000003,
49 HVCPUID_ENLIGHTENMENT_INFO = 0x40000004,
50 HVCPUID_IMPLEMENTATION_LIMITS = 0x40000005,
53 /* Define version of the synthetic interrupt controller. */
54 #define HV_SYNIC_VERSION (1)
56 /* Define the expected SynIC version. */
57 #define HV_SYNIC_VERSION_1 (0x1)
59 /* Define synthetic interrupt controller message constants. */
60 #define HV_MESSAGE_SIZE (256)
61 #define HV_MESSAGE_PAYLOAD_BYTE_COUNT (240)
62 #define HV_MESSAGE_PAYLOAD_QWORD_COUNT (30)
63 #define HV_ANY_VP (0xFFFFFFFF)
65 /* Define synthetic interrupt controller flag constants. */
66 #define HV_EVENT_FLAGS_COUNT (256 * 8)
67 #define HV_EVENT_FLAGS_BYTE_COUNT (256)
68 #define HV_EVENT_FLAGS_DWORD_COUNT (256 / sizeof(u32))
70 /* Define hypervisor message types. */
71 enum hv_message_type {
72 HVMSG_NONE = 0x00000000,
74 /* Memory access messages. */
75 HVMSG_UNMAPPED_GPA = 0x80000000,
76 HVMSG_GPA_INTERCEPT = 0x80000001,
78 /* Timer notification messages. */
79 HVMSG_TIMER_EXPIRED = 0x80000010,
81 /* Error messages. */
82 HVMSG_INVALID_VP_REGISTER_VALUE = 0x80000020,
83 HVMSG_UNRECOVERABLE_EXCEPTION = 0x80000021,
84 HVMSG_UNSUPPORTED_FEATURE = 0x80000022,
86 /* Trace buffer complete messages. */
87 HVMSG_EVENTLOG_BUFFERCOMPLETE = 0x80000040,
89 /* Platform-specific processor intercept messages. */
90 HVMSG_X64_IOPORT_INTERCEPT = 0x80010000,
91 HVMSG_X64_MSR_INTERCEPT = 0x80010001,
92 HVMSG_X64_CPUID_INTERCEPT = 0x80010002,
93 HVMSG_X64_EXCEPTION_INTERCEPT = 0x80010003,
94 HVMSG_X64_APIC_EOI = 0x80010004,
95 HVMSG_X64_LEGACY_FP_ERROR = 0x80010005
98 /* Define the number of synthetic interrupt sources. */
99 #define HV_SYNIC_SINT_COUNT (16)
100 #define HV_SYNIC_STIMER_COUNT (4)
102 /* Define invalid partition identifier. */
103 #define HV_PARTITION_ID_INVALID ((u64)0x0)
105 /* Define connection identifier type. */
106 union hv_connection_id {
107 u32 asu32;
108 struct {
109 u32 id:24;
110 u32 reserved:8;
111 } u;
114 /* Define port identifier type. */
115 union hv_port_id {
116 u32 asu32;
117 struct {
118 u32 id:24;
119 u32 reserved:8;
120 } u ;
123 /* Define port type. */
124 enum hv_port_type {
125 HVPORT_MSG = 1,
126 HVPORT_EVENT = 2,
127 HVPORT_MONITOR = 3
130 /* Define port information structure. */
131 struct hv_port_info {
132 enum hv_port_type port_type;
133 u32 padding;
134 union {
135 struct {
136 u32 target_sint;
137 u32 target_vp;
138 u64 rsvdz;
139 } message_port_info;
140 struct {
141 u32 target_sint;
142 u32 target_vp;
143 u16 base_flag_bumber;
144 u16 flag_count;
145 u32 rsvdz;
146 } event_port_info;
147 struct {
148 u64 monitor_address;
149 u64 rsvdz;
150 } monitor_port_info;
154 struct hv_connection_info {
155 enum hv_port_type port_type;
156 u32 padding;
157 union {
158 struct {
159 u64 rsvdz;
160 } message_connection_info;
161 struct {
162 u64 rsvdz;
163 } event_connection_info;
164 struct {
165 u64 monitor_address;
166 } monitor_connection_info;
170 /* Define synthetic interrupt controller message flags. */
171 union hv_message_flags {
172 u8 asu8;
173 struct {
174 u8 msg_pending:1;
175 u8 reserved:7;
179 /* Define synthetic interrupt controller message header. */
180 struct hv_message_header {
181 enum hv_message_type message_type;
182 u8 payload_size;
183 union hv_message_flags message_flags;
184 u8 reserved[2];
185 union {
186 u64 sender;
187 union hv_port_id port;
191 /* Define timer message payload structure. */
192 struct hv_timer_message_payload {
193 u32 timer_index;
194 u32 reserved;
195 u64 expiration_time; /* When the timer expired */
196 u64 delivery_time; /* When the message was delivered */
199 /* Define synthetic interrupt controller message format. */
200 struct hv_message {
201 struct hv_message_header header;
202 union {
203 u64 payload[HV_MESSAGE_PAYLOAD_QWORD_COUNT];
204 } u ;
207 /* Define the number of message buffers associated with each port. */
208 #define HV_PORT_MESSAGE_BUFFER_COUNT (16)
210 /* Define the synthetic interrupt message page layout. */
211 struct hv_message_page {
212 struct hv_message sint_message[HV_SYNIC_SINT_COUNT];
215 /* Define the synthetic interrupt controller event flags format. */
216 union hv_synic_event_flags {
217 u8 flags8[HV_EVENT_FLAGS_BYTE_COUNT];
218 u32 flags32[HV_EVENT_FLAGS_DWORD_COUNT];
221 /* Define the synthetic interrupt flags page layout. */
222 struct hv_synic_event_flags_page {
223 union hv_synic_event_flags sintevent_flags[HV_SYNIC_SINT_COUNT];
226 /* Define SynIC control register. */
227 union hv_synic_scontrol {
228 u64 as_uint64;
229 struct {
230 u64 enable:1;
231 u64 reserved:63;
235 /* Define synthetic interrupt source. */
236 union hv_synic_sint {
237 u64 as_uint64;
238 struct {
239 u64 vector:8;
240 u64 reserved1:8;
241 u64 masked:1;
242 u64 auto_eoi:1;
243 u64 reserved2:46;
247 /* Define the format of the SIMP register */
248 union hv_synic_simp {
249 u64 as_uint64;
250 struct {
251 u64 simp_enabled:1;
252 u64 preserved:11;
253 u64 base_simp_gpa:52;
257 /* Define the format of the SIEFP register */
258 union hv_synic_siefp {
259 u64 as_uint64;
260 struct {
261 u64 siefp_enabled:1;
262 u64 preserved:11;
263 u64 base_siefp_gpa:52;
267 /* Definitions for the monitored notification facility */
268 union hv_monitor_trigger_group {
269 u64 as_uint64;
270 struct {
271 u32 pending;
272 u32 armed;
276 struct hv_monitor_parameter {
277 union hv_connection_id connectionid;
278 u16 flagnumber;
279 u16 rsvdz;
282 union hv_monitor_trigger_state {
283 u32 asu32;
285 struct {
286 u32 group_enable:4;
287 u32 rsvdz:28;
291 /* struct hv_monitor_page Layout */
292 /* ------------------------------------------------------ */
293 /* | 0 | TriggerState (4 bytes) | Rsvd1 (4 bytes) | */
294 /* | 8 | TriggerGroup[0] | */
295 /* | 10 | TriggerGroup[1] | */
296 /* | 18 | TriggerGroup[2] | */
297 /* | 20 | TriggerGroup[3] | */
298 /* | 28 | Rsvd2[0] | */
299 /* | 30 | Rsvd2[1] | */
300 /* | 38 | Rsvd2[2] | */
301 /* | 40 | NextCheckTime[0][0] | NextCheckTime[0][1] | */
302 /* | ... | */
303 /* | 240 | Latency[0][0..3] | */
304 /* | 340 | Rsvz3[0] | */
305 /* | 440 | Parameter[0][0] | */
306 /* | 448 | Parameter[0][1] | */
307 /* | ... | */
308 /* | 840 | Rsvd4[0] | */
309 /* ------------------------------------------------------ */
310 struct hv_monitor_page {
311 union hv_monitor_trigger_state trigger_state;
312 u32 rsvdz1;
314 union hv_monitor_trigger_group trigger_group[4];
315 u64 rsvdz2[3];
317 s32 next_checktime[4][32];
319 u16 latency[4][32];
320 u64 rsvdz3[32];
322 struct hv_monitor_parameter parameter[4][32];
324 u8 rsvdz4[1984];
327 /* Declare the various hypercall operations. */
328 enum hv_call_code {
329 HVCALL_POST_MESSAGE = 0x005c,
330 HVCALL_SIGNAL_EVENT = 0x005d,
333 /* Definition of the hv_post_message hypercall input structure. */
334 struct hv_input_post_message {
335 union hv_connection_id connectionid;
336 u32 reserved;
337 enum hv_message_type message_type;
338 u32 payload_size;
339 u64 payload[HV_MESSAGE_PAYLOAD_QWORD_COUNT];
342 /* Definition of the hv_signal_event hypercall input structure. */
343 struct hv_input_signal_event {
344 union hv_connection_id connectionid;
345 u16 flag_number;
346 u16 rsvdz;
350 * Versioning definitions used for guests reporting themselves to the
351 * hypervisor, and visa versa.
354 /* Version info reported by guest OS's */
355 enum hv_guest_os_vendor {
356 HVGUESTOS_VENDOR_MICROSOFT = 0x0001
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
369 * Declare the MSR used to identify the guest OS.
371 #define HV_X64_MSR_GUEST_OS_ID 0x40000000
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 */
386 * Declare the MSR used to setup pages used to communicate with the hypervisor.
388 #define HV_X64_MSR_HYPERCALL 0x40000001
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;
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,
410 /* #defines */
412 #define HV_PRESENT_BIT 0x80000000
414 #define HV_LINUX_GUEST_ID_LO 0x00000000
415 #define HV_LINUX_GUEST_ID_HI 0xB16B00B5
416 #define HV_LINUX_GUEST_ID (((u64)HV_LINUX_GUEST_ID_HI << 32) | \
417 HV_LINUX_GUEST_ID_LO)
419 #define HV_CPU_POWER_MANAGEMENT (1 << 0)
420 #define HV_RECOMMENDATIONS_MAX 4
422 #define HV_X64_MAX 5
423 #define HV_CAPS_MAX 8
426 #define HV_HYPERCALL_PARAM_ALIGN sizeof(u64)
429 /* Service definitions */
431 #define HV_SERVICE_PARENT_PORT (0)
432 #define HV_SERVICE_PARENT_CONNECTION (0)
434 #define HV_SERVICE_CONNECT_RESPONSE_SUCCESS (0)
435 #define HV_SERVICE_CONNECT_RESPONSE_INVALID_PARAMETER (1)
436 #define HV_SERVICE_CONNECT_RESPONSE_UNKNOWN_SERVICE (2)
437 #define HV_SERVICE_CONNECT_RESPONSE_CONNECTION_REJECTED (3)
439 #define HV_SERVICE_CONNECT_REQUEST_MESSAGE_ID (1)
440 #define HV_SERVICE_CONNECT_RESPONSE_MESSAGE_ID (2)
441 #define HV_SERVICE_DISCONNECT_REQUEST_MESSAGE_ID (3)
442 #define HV_SERVICE_DISCONNECT_RESPONSE_MESSAGE_ID (4)
443 #define HV_SERVICE_MAX_MESSAGE_ID (4)
445 #define HV_SERVICE_PROTOCOL_VERSION (0x0010)
446 #define HV_CONNECT_PAYLOAD_BYTE_COUNT 64
448 /* #define VMBUS_REVISION_NUMBER 6 */
450 /* Our local vmbus's port and connection id. Anything >0 is fine */
451 /* #define VMBUS_PORT_ID 11 */
453 /* 628180B8-308D-4c5e-B7DB-1BEB62E62EF4 */
454 static const struct hv_guid VMBUS_SERVICE_ID = {
455 .data = {
456 0xb8, 0x80, 0x81, 0x62, 0x8d, 0x30, 0x5e, 0x4c,
457 0xb7, 0xdb, 0x1b, 0xeb, 0x62, 0xe6, 0x2e, 0xf4
461 #define MAX_NUM_CPUS 32
464 struct hv_input_signal_event_buffer {
465 u64 align8;
466 struct hv_input_signal_event event;
469 struct hv_context {
470 /* We only support running on top of Hyper-V
471 * So at this point this really can only contain the Hyper-V ID
473 u64 guestid;
475 void *hypercall_page;
477 bool synic_initialized;
480 * This is used as an input param to HvCallSignalEvent hypercall. The
481 * input param is immutable in our usage and must be dynamic mem (vs
482 * stack or global). */
483 struct hv_input_signal_event_buffer *signal_event_buffer;
484 /* 8-bytes aligned of the buffer above */
485 struct hv_input_signal_event *signal_event_param;
487 void *synic_message_page[MAX_NUM_CPUS];
488 void *synic_event_page[MAX_NUM_CPUS];
491 extern struct hv_context hv_context;
494 /* Hv Interface */
496 extern int hv_init(void);
498 extern void hv_cleanup(void);
500 extern u16 hv_post_message(union hv_connection_id connection_id,
501 enum hv_message_type message_type,
502 void *payload, size_t payload_size);
504 extern u16 hv_signal_event(void);
506 extern void hv_synic_init(void *irqarg);
508 extern void hv_synic_cleanup(void *arg);
511 /* Interface */
514 int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info, void *buffer,
515 u32 buflen);
517 void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info);
519 int hv_ringbuffer_write(struct hv_ring_buffer_info *ring_info,
520 struct scatterlist *sglist,
521 u32 sgcount);
523 int hv_ringbuffer_peek(struct hv_ring_buffer_info *ring_info, void *buffer,
524 u32 buflen);
526 int hv_ringbuffer_read(struct hv_ring_buffer_info *ring_info,
527 void *buffer,
528 u32 buflen,
529 u32 offset);
531 u32 hv_get_ringbuffer_interrupt_mask(struct hv_ring_buffer_info *ring_info);
533 void hv_dump_ring_info(struct hv_ring_buffer_info *ring_info, char *prefix);
535 void hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
536 struct hv_ring_buffer_debug_info *debug_info);
539 * Maximum channels is determined by the size of the interrupt page
540 * which is PAGE_SIZE. 1/2 of PAGE_SIZE is for send endpoint interrupt
541 * and the other is receive endpoint interrupt
543 #define MAX_NUM_CHANNELS ((PAGE_SIZE >> 1) << 3) /* 16348 channels */
545 /* The value here must be in multiple of 32 */
546 /* TODO: Need to make this configurable */
547 #define MAX_NUM_CHANNELS_SUPPORTED 256
550 enum vmbus_connect_state {
551 DISCONNECTED,
552 CONNECTING,
553 CONNECTED,
554 DISCONNECTING
557 #define MAX_SIZE_CHANNEL_MESSAGE HV_MESSAGE_PAYLOAD_BYTE_COUNT
559 struct vmbus_connection {
560 enum vmbus_connect_state conn_state;
562 atomic_t next_gpadl_handle;
565 * Represents channel interrupts. Each bit position represents a
566 * channel. When a channel sends an interrupt via VMBUS, it finds its
567 * bit in the sendInterruptPage, set it and calls Hv to generate a port
568 * event. The other end receives the port event and parse the
569 * recvInterruptPage to see which bit is set
571 void *int_page;
572 void *send_int_page;
573 void *recv_int_page;
576 * 2 pages - 1st page for parent->child notification and 2nd
577 * is child->parent notification
579 void *monitor_pages;
580 struct list_head chn_msg_list;
581 spinlock_t channelmsg_lock;
583 /* List of channels */
584 struct list_head chn_list;
585 spinlock_t channel_lock;
587 struct workqueue_struct *work_queue;
591 struct vmbus_msginfo {
592 /* Bookkeeping stuff */
593 struct list_head msglist_entry;
595 /* The message itself */
596 unsigned char msg[0];
600 extern struct vmbus_connection vmbus_connection;
602 /* General vmbus interface */
604 struct hv_device *vmbus_child_device_create(struct hv_guid *type,
605 struct hv_guid *instance,
606 struct vmbus_channel *channel);
608 int vmbus_child_device_register(struct hv_device *child_device_obj);
609 void vmbus_child_device_unregister(struct hv_device *device_obj);
611 /* static void */
612 /* VmbusChildDeviceDestroy( */
613 /* struct hv_device *); */
615 struct vmbus_channel *relid2channel(u32 relid);
618 /* Connection interface */
620 int vmbus_connect(void);
622 int vmbus_post_msg(void *buffer, size_t buflen);
624 int vmbus_set_event(u32 child_relid);
626 void vmbus_on_event(unsigned long data);
629 #endif /* _HYPERV_VMBUS_H */