]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/hyperv/vmbus/hv_vmbus_priv.h
hyperv/vmbus: Avoid two unnecessary protocol checks on isr handling path
[FreeBSD/FreeBSD.git] / sys / dev / hyperv / vmbus / hv_vmbus_priv.h
1 /*-
2  * Copyright (c) 2009-2012,2016 Microsoft Corp.
3  * Copyright (c) 2012 NetApp Inc.
4  * Copyright (c) 2012 Citrix Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30
31 #ifndef __HYPERV_PRIV_H__
32 #define __HYPERV_PRIV_H__
33
34 #include <sys/param.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/sema.h>
38
39 #include <dev/hyperv/include/hyperv.h>
40
41
42 /*
43  *  Status codes for hypervisor operations.
44  */
45
46 typedef uint16_t hv_vmbus_status;
47
48 #define HV_MESSAGE_SIZE                 (256)
49 #define HV_MESSAGE_PAYLOAD_BYTE_COUNT   (240)
50 #define HV_MESSAGE_PAYLOAD_QWORD_COUNT  (30)
51 #define HV_ANY_VP                       (0xFFFFFFFF)
52
53 /*
54  * Synthetic interrupt controller flag constants.
55  */
56
57 #define HV_EVENT_FLAGS_COUNT        (256 * 8)
58 #define HV_EVENT_FLAGS_BYTE_COUNT   (256)
59 #define HV_EVENT_FLAGS_DWORD_COUNT  (256 / sizeof(uint32_t))
60 #define HV_EVENT_FLAGS_ULONG_COUNT  (256 / sizeof(unsigned long))
61
62 /**
63  * max channel count <== event_flags_dword_count * bit_of_dword
64  */
65 #ifdef __LP64__
66 #define HV_CHANNEL_ULONG_LEN        (64)
67 #define HV_CHANNEL_ULONG_SHIFT      (6)
68 #else
69 #define HV_CHANNEL_ULONG_LEN        (32)
70 #define HV_CHANNEL_ULONG_SHIFT      (5)
71 #endif
72 #define HV_CHANNEL_DWORD_LEN        (32)
73 #define HV_CHANNEL_MAX_COUNT        \
74         ((HV_EVENT_FLAGS_DWORD_COUNT) * HV_CHANNEL_DWORD_LEN)
75 /*
76  * MessageId: HV_STATUS_INSUFFICIENT_BUFFERS
77  * MessageText:
78  *    You did not supply enough message buffers to send a message.
79  */
80
81 #define HV_STATUS_SUCCESS                ((uint16_t)0)
82 #define HV_STATUS_INSUFFICIENT_BUFFERS   ((uint16_t)0x0013)
83
84 typedef void (*hv_vmbus_channel_callback)(void *context);
85
86 typedef struct {
87         void*           data;
88         uint32_t        length;
89 } hv_vmbus_sg_buffer_list;
90
91 typedef struct {
92         uint32_t        current_interrupt_mask;
93         uint32_t        current_read_index;
94         uint32_t        current_write_index;
95         uint32_t        bytes_avail_to_read;
96         uint32_t        bytes_avail_to_write;
97 } hv_vmbus_ring_buffer_debug_info;
98
99 typedef struct {
100         uint32_t                rel_id;
101         hv_vmbus_channel_state  state;
102         hv_guid                 interface_type;
103         hv_guid                 interface_instance;
104         uint32_t                monitor_id;
105         uint32_t                server_monitor_pending;
106         uint32_t                server_monitor_latency;
107         uint32_t                server_monitor_connection_id;
108         uint32_t                client_monitor_pending;
109         uint32_t                client_monitor_latency;
110         uint32_t                client_monitor_connection_id;
111         hv_vmbus_ring_buffer_debug_info inbound;
112         hv_vmbus_ring_buffer_debug_info outbound;
113 } hv_vmbus_channel_debug_info;
114
115 typedef union {
116         hv_vmbus_channel_version_supported      version_supported;
117         hv_vmbus_channel_open_result            open_result;
118         hv_vmbus_channel_gpadl_torndown         gpadl_torndown;
119         hv_vmbus_channel_gpadl_created          gpadl_created;
120         hv_vmbus_channel_version_response       version_response;
121 } hv_vmbus_channel_msg_response;
122
123 /*
124  * Represents each channel msg on the vmbus connection
125  * This is a variable-size data structure depending on
126  * the msg type itself
127  */
128 typedef struct hv_vmbus_channel_msg_info {
129         /*
130          * Bookkeeping stuff
131          */
132         TAILQ_ENTRY(hv_vmbus_channel_msg_info)  msg_list_entry;
133         /*
134          * So far, this is only used to handle
135          * gpadl body message
136          */
137         TAILQ_HEAD(, hv_vmbus_channel_msg_info) sub_msg_list_anchor;
138         /*
139          * Synchronize the request/response if
140          * needed.
141          * KYS: Use a semaphore for now.
142          * Not perf critical.
143          */
144         struct sema                             wait_sema;
145         hv_vmbus_channel_msg_response           response;
146         uint32_t                                message_size;
147         /**
148          * The channel message that goes out on
149          *  the "wire". It will contain at
150          *  minimum the
151          *  hv_vmbus_channel_msg_header
152          * header.
153          */
154         unsigned char                           msg[0];
155 } hv_vmbus_channel_msg_info;
156
157 /*
158  * The format must be the same as hv_vm_data_gpa_direct
159  */
160 typedef struct hv_vmbus_channel_packet_page_buffer {
161         uint16_t                type;
162         uint16_t                data_offset8;
163         uint16_t                length8;
164         uint16_t                flags;
165         uint64_t                transaction_id;
166         uint32_t                reserved;
167         uint32_t                range_count;
168         hv_vmbus_page_buffer    range[HV_MAX_PAGE_BUFFER_COUNT];
169 } __packed hv_vmbus_channel_packet_page_buffer;
170
171 /*
172  * The format must be the same as hv_vm_data_gpa_direct
173  */
174 typedef struct hv_vmbus_channel_packet_multipage_buffer {
175         uint16_t                        type;
176         uint16_t                        data_offset8;
177         uint16_t                        length8;
178         uint16_t                        flags;
179         uint64_t                        transaction_id;
180         uint32_t                        reserved;
181         uint32_t                        range_count; /* Always 1 in this case */
182         hv_vmbus_multipage_buffer       range;
183 } __packed hv_vmbus_channel_packet_multipage_buffer;
184
185 enum {
186         HV_VMBUS_MESSAGE_CONNECTION_ID  = 1,
187         HV_VMBUS_MESSAGE_PORT_ID        = 1,
188         HV_VMBUS_EVENT_CONNECTION_ID    = 2,
189         HV_VMBUS_EVENT_PORT_ID          = 2,
190         HV_VMBUS_MONITOR_CONNECTION_ID  = 3,
191         HV_VMBUS_MONITOR_PORT_ID        = 3,
192         HV_VMBUS_MESSAGE_SINT           = 2,
193         HV_VMBUS_TIMER_SINT             = 4,
194 };
195
196 #define HV_PRESENT_BIT          0x80000000
197
198 #define HV_HYPERCALL_PARAM_ALIGN sizeof(uint64_t)
199
200 typedef struct {
201         uint64_t        guest_id;
202         void*           hypercall_page;
203         hv_bool_uint8_t syn_ic_initialized;
204
205         hv_vmbus_handle syn_ic_msg_page[MAXCPU];
206         hv_vmbus_handle syn_ic_event_page[MAXCPU];
207         /*
208          * For FreeBSD cpuid to Hyper-V vcpuid mapping.
209          */
210         uint32_t        hv_vcpu_index[MAXCPU];
211         /*
212          * Each cpu has its own software interrupt handler for channel
213          * event and msg handling.
214          */
215         struct taskqueue                *hv_event_queue[MAXCPU];
216         struct taskqueue                *hv_msg_tq[MAXCPU];
217         struct task                     hv_msg_task[MAXCPU];
218         /*
219          * Host use this vector to interrupt guest for vmbus channel
220          * event and msg.
221          */
222         int                             hv_cb_vector;
223 } hv_vmbus_context;
224
225 /*
226  * Define hypervisor message types
227  */
228 typedef enum {
229
230         HV_MESSAGE_TYPE_NONE                            = 0x00000000,
231
232         /*
233          * Memory access messages
234          */
235         HV_MESSAGE_TYPE_UNMAPPED_GPA                    = 0x80000000,
236         HV_MESSAGE_TYPE_GPA_INTERCEPT                   = 0x80000001,
237
238         /*
239          * Timer notification messages
240          */
241         HV_MESSAGE_TIMER_EXPIRED                        = 0x80000010,
242
243         /*
244          * Error messages
245          */
246         HV_MESSAGE_TYPE_INVALID_VP_REGISTER_VALUE       = 0x80000020,
247         HV_MESSAGE_TYPE_UNRECOVERABLE_EXCEPTION         = 0x80000021,
248         HV_MESSAGE_TYPE_UNSUPPORTED_FEATURE             = 0x80000022,
249
250         /*
251          * Trace buffer complete messages
252          */
253         HV_MESSAGE_TYPE_EVENT_LOG_BUFFER_COMPLETE       = 0x80000040,
254
255         /*
256          * Platform-specific processor intercept messages
257          */
258         HV_MESSAGE_TYPE_X64_IO_PORT_INTERCEPT           = 0x80010000,
259         HV_MESSAGE_TYPE_X64_MSR_INTERCEPT               = 0x80010001,
260         HV_MESSAGE_TYPE_X64_CPU_INTERCEPT               = 0x80010002,
261         HV_MESSAGE_TYPE_X64_EXCEPTION_INTERCEPT         = 0x80010003,
262         HV_MESSAGE_TYPE_X64_APIC_EOI                    = 0x80010004,
263         HV_MESSAGE_TYPE_X64_LEGACY_FP_ERROR             = 0x80010005
264
265 } hv_vmbus_msg_type;
266
267 /*
268  * Define port identifier type
269  */
270 typedef union _hv_vmbus_port_id {
271         uint32_t        as_uint32_t;
272         struct {
273                 uint32_t        id:24;
274                 uint32_t        reserved:8;
275         } u ;
276 } hv_vmbus_port_id;
277
278 /*
279  * Define synthetic interrupt controller message flag
280  */
281 typedef union {
282         uint8_t as_uint8_t;
283         struct {
284                 uint8_t message_pending:1;
285                 uint8_t reserved:7;
286         } u;
287 } hv_vmbus_msg_flags;
288
289 typedef uint64_t hv_vmbus_partition_id;
290
291 /*
292  * Define synthetic interrupt controller message header
293  */
294 typedef struct {
295         hv_vmbus_msg_type       message_type;
296         uint8_t                 payload_size;
297         hv_vmbus_msg_flags      message_flags;
298         uint8_t                 reserved[2];
299         union {
300                 hv_vmbus_partition_id   sender;
301                 hv_vmbus_port_id        port;
302         } u;
303 } hv_vmbus_msg_header;
304
305 /*
306  *  Define synthetic interrupt controller message format
307  */
308 typedef struct {
309         hv_vmbus_msg_header     header;
310         union {
311                 uint64_t        payload[HV_MESSAGE_PAYLOAD_QWORD_COUNT];
312         } u ;
313 } hv_vmbus_message;
314
315 /*
316  *  Maximum channels is determined by the size of the interrupt
317  *  page which is PAGE_SIZE. 1/2 of PAGE_SIZE is for
318  *  send endpoint interrupt and the other is receive
319  *  endpoint interrupt.
320  *
321  *   Note: (PAGE_SIZE >> 1) << 3 allocates 16348 channels
322  */
323 #define HV_MAX_NUM_CHANNELS                     (PAGE_SIZE >> 1) << 3
324
325 /*
326  * (The value here must be in multiple of 32)
327  */
328 #define HV_MAX_NUM_CHANNELS_SUPPORTED           256
329
330 /*
331  * VM Bus connection states
332  */
333 typedef enum {
334         HV_DISCONNECTED,
335         HV_CONNECTING,
336         HV_CONNECTED,
337         HV_DISCONNECTING
338 } hv_vmbus_connect_state;
339
340 #define HV_MAX_SIZE_CHANNEL_MESSAGE     HV_MESSAGE_PAYLOAD_BYTE_COUNT
341
342
343 typedef struct {
344         hv_vmbus_connect_state                  connect_state;
345         uint32_t                                next_gpadl_handle;
346         /**
347          * Represents channel interrupts. Each bit position
348          * represents a channel.
349          * When a channel sends an interrupt via VMBUS, it
350          * finds its bit in the send_interrupt_page, set it and
351          * calls Hv to generate a port event. The other end
352          * receives the port event and parse the
353          * recv_interrupt_page to see which bit is set
354          */
355         void                                    *interrupt_page;
356         void                                    *send_interrupt_page;
357         void                                    *recv_interrupt_page;
358         /*
359          * 2 pages - 1st page for parent->child
360          * notification and 2nd is child->parent
361          * notification
362          */
363         void                                    *monitor_page_1;
364         void                                    *monitor_page_2;
365         TAILQ_HEAD(, hv_vmbus_channel_msg_info) channel_msg_anchor;
366         struct mtx                              channel_msg_lock;
367         /**
368          * List of primary channels. Sub channels will be linked
369          * under their primary channel.
370          */
371         TAILQ_HEAD(, hv_vmbus_channel)          channel_anchor;
372         struct mtx                              channel_lock;
373
374         /**
375          * channel table for fast lookup through id.
376         */
377         hv_vmbus_channel                        **channels;
378 } hv_vmbus_connection;
379
380 typedef union {
381         uint64_t as_uint64_t;
382         struct {
383                 uint64_t build_number           : 16;
384                 uint64_t service_version        : 8; /* Service Pack, etc. */
385                 uint64_t minor_version          : 8;
386                 uint64_t major_version          : 8;
387                 /*
388                  * HV_GUEST_OS_MICROSOFT_IDS (If Vendor=MS)
389                  * HV_GUEST_OS_VENDOR
390                  */
391                 uint64_t os_id                  : 8;
392                 uint64_t vendor_id              : 16;
393         } u;
394 } hv_vmbus_x64_msr_guest_os_id_contents;
395
396
397 typedef union {
398         uint64_t as_uint64_t;
399         struct {
400                 uint64_t enable :1;
401                 uint64_t reserved :11;
402                 uint64_t guest_physical_address :52;
403         } u;
404 } hv_vmbus_x64_msr_hypercall_contents;
405
406 typedef union {
407         uint32_t as_uint32_t;
408         struct {
409                 uint32_t group_enable :4;
410                 uint32_t rsvd_z :28;
411         } u;
412 } hv_vmbus_monitor_trigger_state;
413
414 typedef union {
415         uint64_t as_uint64_t;
416         struct {
417                 uint32_t pending;
418                 uint32_t armed;
419         } u;
420 } hv_vmbus_monitor_trigger_group;
421
422 typedef struct {
423         hv_vmbus_connection_id  connection_id;
424         uint16_t                flag_number;
425         uint16_t                rsvd_z;
426 } hv_vmbus_monitor_parameter;
427
428 /*
429  * hv_vmbus_monitor_page Layout
430  * ------------------------------------------------------
431  * | 0   | trigger_state (4 bytes) | Rsvd1 (4 bytes)     |
432  * | 8   | trigger_group[0]                              |
433  * | 10  | trigger_group[1]                              |
434  * | 18  | trigger_group[2]                              |
435  * | 20  | trigger_group[3]                              |
436  * | 28  | Rsvd2[0]                                      |
437  * | 30  | Rsvd2[1]                                      |
438  * | 38  | Rsvd2[2]                                      |
439  * | 40  | next_check_time[0][0] | next_check_time[0][1] |
440  * | ...                                                 |
441  * | 240 | latency[0][0..3]                              |
442  * | 340 | Rsvz3[0]                                      |
443  * | 440 | parameter[0][0]                               |
444  * | 448 | parameter[0][1]                               |
445  * | ...                                                 |
446  * | 840 | Rsvd4[0]                                      |
447  * ------------------------------------------------------
448  */
449
450 typedef struct {
451         hv_vmbus_monitor_trigger_state  trigger_state;
452         uint32_t                        rsvd_z1;
453
454         hv_vmbus_monitor_trigger_group  trigger_group[4];
455         uint64_t                        rsvd_z2[3];
456
457         int32_t                         next_check_time[4][32];
458
459         uint16_t                        latency[4][32];
460         uint64_t                        rsvd_z3[32];
461
462         hv_vmbus_monitor_parameter      parameter[4][32];
463
464         uint8_t                         rsvd_z4[1984];
465 } hv_vmbus_monitor_page;
466
467 /*
468  * The below CPUID leaves are present if VersionAndFeatures.HypervisorPresent
469  * is set by CPUID(HV_CPU_ID_FUNCTION_VERSION_AND_FEATURES).
470  */
471 typedef enum {
472         HV_CPU_ID_FUNCTION_VERSION_AND_FEATURES                 = 0x00000001,
473         HV_CPU_ID_FUNCTION_HV_VENDOR_AND_MAX_FUNCTION           = 0x40000000,
474         HV_CPU_ID_FUNCTION_HV_INTERFACE                         = 0x40000001,
475         /*
476          * The remaining functions depend on the value
477          * of hv_cpu_id_function_interface
478          */
479         HV_CPU_ID_FUNCTION_MS_HV_VERSION                        = 0x40000002,
480         HV_CPU_ID_FUNCTION_MS_HV_FEATURES                       = 0x40000003,
481         HV_CPU_ID_FUNCTION_MS_HV_ENLIGHTENMENT_INFORMATION      = 0x40000004,
482         HV_CPU_ID_FUNCTION_MS_HV_IMPLEMENTATION_LIMITS          = 0x40000005,
483         HV_CPU_ID_FUNCTION_MS_HV_HARDWARE_FEATURE               = 0x40000006
484 } hv_vmbus_cpuid_function;
485
486 #define HV_FEATURE_MSR_TIME_REFCNT      0x0002  /* MSR_TIME_REF_COUNT */
487 #define HV_FEATURE_MSR_SYNIC            0x0004  /* MSRs for SynIC */
488 #define HV_FEATURE_MSR_SYNTIMER         0x0008  /* MSRs for SynTimer */
489 #define HV_FEATURE_MSR_APIC             0x0010  /* MSR_{EOI,ICR,TPR} */
490 #define HV_FEATURE_MSR_HYPERCALL        0x0020  /* MSR_{GUEST_OS_ID,HYPERCALL} */
491 #define HV_FEATURE_MSR_GUEST_IDLE       0x0400  /* MSR_GUEST_IDLE */
492
493 #define HV_PM_FEATURE_CSTATE_MASK       0x000f
494 #define HV_PM_FEATURE_C3_HPET           0x0010  /* C3 requires HPET */
495 #define HV_PM_FEATURE_CSTATE(f)         ((f) & HV_PM_FEATURE_CSTATE_MASK)
496
497 #define HV_FEATURE3_MWAIT               0x0001  /* MWAIT */
498 #define HV_FEATURE3_XMM_HYPERCALL       0x0010  /* hypercall input through XMM regs */
499 #define HV_FEATURE3_GUEST_IDLE          0x0020  /* guest idle support */
500 #define HV_FEATURE3_NUMA                0x0080  /* NUMA distance query support */
501 #define HV_FEATURE3_TIME_FREQ           0x0100  /* timer frequency query (TSC, LAPIC) */
502 #define HV_FEATURE3_MSR_CRASH           0x0400  /* MSRs for guest crash */
503
504 /*
505  * Define the format of the SIMP register
506  */
507 typedef union {
508         uint64_t as_uint64_t;
509         struct {
510                 uint64_t simp_enabled   : 1;
511                 uint64_t preserved      : 11;
512                 uint64_t base_simp_gpa  : 52;
513         } u;
514 } hv_vmbus_synic_simp;
515
516 /*
517  * Define the format of the SIEFP register
518  */
519 typedef union {
520         uint64_t as_uint64_t;
521         struct {
522                 uint64_t siefp_enabled  : 1;
523                 uint64_t preserved      : 11;
524                 uint64_t base_siefp_gpa : 52;
525         } u;
526 } hv_vmbus_synic_siefp;
527
528 /*
529  * Define synthetic interrupt source
530  */
531 typedef union {
532         uint64_t as_uint64_t;
533         struct {
534                 uint64_t vector         : 8;
535                 uint64_t reserved1      : 8;
536                 uint64_t masked         : 1;
537                 uint64_t auto_eoi       : 1;
538                 uint64_t reserved2      : 46;
539         } u;
540 } hv_vmbus_synic_sint;
541
542 /*
543  * Timer configuration register.
544  */
545 union hv_timer_config {
546         uint64_t as_uint64;
547         struct {
548                 uint64_t enable:1;
549                 uint64_t periodic:1;
550                 uint64_t lazy:1;
551                 uint64_t auto_enable:1;
552                 uint64_t reserved_z0:12;
553                 uint64_t sintx:4;
554                 uint64_t reserved_z1:44;
555         };
556 };
557
558 /*
559  * Define syn_ic control register
560  */
561 typedef union _hv_vmbus_synic_scontrol {
562     uint64_t as_uint64_t;
563     struct {
564         uint64_t enable         : 1;
565         uint64_t reserved       : 63;
566     } u;
567 } hv_vmbus_synic_scontrol;
568
569 /*
570  *  Define the hv_vmbus_post_message hypercall input structure
571  */
572 typedef struct {
573         hv_vmbus_connection_id  connection_id;
574         uint32_t                reserved;
575         hv_vmbus_msg_type       message_type;
576         uint32_t                payload_size;
577         uint64_t                payload[HV_MESSAGE_PAYLOAD_QWORD_COUNT];
578 } hv_vmbus_input_post_message;
579
580 /*
581  * Define the synthetic interrupt controller event flags format
582  */
583 typedef union {
584         uint8_t         flags8[HV_EVENT_FLAGS_BYTE_COUNT];
585         uint32_t        flags32[HV_EVENT_FLAGS_DWORD_COUNT];
586         unsigned long   flagsul[HV_EVENT_FLAGS_ULONG_COUNT];
587 } hv_vmbus_synic_event_flags;
588 CTASSERT(sizeof(hv_vmbus_synic_event_flags) == HV_EVENT_FLAGS_BYTE_COUNT);
589
590 #define HV_X64_CPUID_MIN        (0x40000005)
591 #define HV_X64_CPUID_MAX        (0x4000ffff)
592
593 /*
594  * Declare the MSR used to identify the guest OS
595  */
596 #define HV_X64_MSR_GUEST_OS_ID  (0x40000000)
597 /*
598  *  Declare the MSR used to setup pages used to communicate with the hypervisor
599  */
600 #define HV_X64_MSR_HYPERCALL    (0x40000001)
601 /* MSR used to provide vcpu index */
602 #define HV_X64_MSR_VP_INDEX     (0x40000002)
603
604 #define HV_X64_MSR_TIME_REF_COUNT      (0x40000020)
605
606 /*
607  * Define synthetic interrupt controller model specific registers
608  */
609 #define HV_X64_MSR_SCONTROL   (0x40000080)
610 #define HV_X64_MSR_SVERSION   (0x40000081)
611 #define HV_X64_MSR_SIEFP      (0x40000082)
612 #define HV_X64_MSR_SIMP       (0x40000083)
613 #define HV_X64_MSR_EOM        (0x40000084)
614
615 #define HV_X64_MSR_SINT0      (0x40000090)
616 #define HV_X64_MSR_SINT1      (0x40000091)
617 #define HV_X64_MSR_SINT2      (0x40000092)
618 #define HV_X64_MSR_SINT3      (0x40000093)
619 #define HV_X64_MSR_SINT4      (0x40000094)
620 #define HV_X64_MSR_SINT5      (0x40000095)
621 #define HV_X64_MSR_SINT6      (0x40000096)
622 #define HV_X64_MSR_SINT7      (0x40000097)
623 #define HV_X64_MSR_SINT8      (0x40000098)
624 #define HV_X64_MSR_SINT9      (0x40000099)
625 #define HV_X64_MSR_SINT10     (0x4000009A)
626 #define HV_X64_MSR_SINT11     (0x4000009B)
627 #define HV_X64_MSR_SINT12     (0x4000009C)
628 #define HV_X64_MSR_SINT13     (0x4000009D)
629 #define HV_X64_MSR_SINT14     (0x4000009E)
630 #define HV_X64_MSR_SINT15     (0x4000009F)
631
632 /*
633  * Synthetic Timer MSRs. Four timers per vcpu.
634  */
635 #define HV_X64_MSR_STIMER0_CONFIG               0x400000B0
636 #define HV_X64_MSR_STIMER0_COUNT                0x400000B1
637 #define HV_X64_MSR_STIMER1_CONFIG               0x400000B2
638 #define HV_X64_MSR_STIMER1_COUNT                0x400000B3
639 #define HV_X64_MSR_STIMER2_CONFIG               0x400000B4
640 #define HV_X64_MSR_STIMER2_COUNT                0x400000B5
641 #define HV_X64_MSR_STIMER3_CONFIG               0x400000B6
642 #define HV_X64_MSR_STIMER3_COUNT                0x400000B7
643
644 /*
645  * Declare the various hypercall operations
646  */
647 typedef enum {
648         HV_CALL_POST_MESSAGE    = 0x005c,
649         HV_CALL_SIGNAL_EVENT    = 0x005d,
650 } hv_vmbus_call_code;
651
652 /**
653  * Global variables
654  */
655
656 extern hv_vmbus_context         hv_vmbus_g_context;
657 extern hv_vmbus_connection      hv_vmbus_g_connection;
658
659 extern u_int                    hyperv_features;
660 extern u_int                    hyperv_recommends;
661
662 typedef void (*vmbus_msg_handler)(hv_vmbus_channel_msg_header *msg);
663
664 typedef struct hv_vmbus_channel_msg_table_entry {
665         hv_vmbus_channel_msg_type    messageType;
666
667         vmbus_msg_handler   messageHandler;
668 } hv_vmbus_channel_msg_table_entry;
669
670 extern hv_vmbus_channel_msg_table_entry g_channel_message_table[];
671
672 /*
673  * Private, VM Bus functions
674  */
675 struct sysctl_ctx_list;
676 struct sysctl_oid_list;
677
678 void                    hv_ring_buffer_stat(
679                                 struct sysctl_ctx_list          *ctx,
680                                 struct sysctl_oid_list          *tree_node,
681                                 hv_vmbus_ring_buffer_info       *rbi,
682                                 const char                      *desc);
683
684 int                     hv_vmbus_ring_buffer_init(
685                                 hv_vmbus_ring_buffer_info       *ring_info,
686                                 void                            *buffer,
687                                 uint32_t                        buffer_len);
688
689 void                    hv_ring_buffer_cleanup(
690                                 hv_vmbus_ring_buffer_info       *ring_info);
691
692 int                     hv_ring_buffer_write(
693                                 hv_vmbus_ring_buffer_info       *ring_info,
694                                 hv_vmbus_sg_buffer_list         sg_buffers[],
695                                 uint32_t                        sg_buff_count,
696                                 boolean_t                       *need_sig);
697
698 int                     hv_ring_buffer_peek(
699                                 hv_vmbus_ring_buffer_info       *ring_info,
700                                 void                            *buffer,
701                                 uint32_t                        buffer_len);
702
703 int                     hv_ring_buffer_read(
704                                 hv_vmbus_ring_buffer_info       *ring_info,
705                                 void                            *buffer,
706                                 uint32_t                        buffer_len,
707                                 uint32_t                        offset);
708
709 uint32_t                hv_vmbus_get_ring_buffer_interrupt_mask(
710                                 hv_vmbus_ring_buffer_info       *ring_info);
711
712 void                    hv_vmbus_dump_ring_info(
713                                 hv_vmbus_ring_buffer_info       *ring_info,
714                                 char                            *prefix);
715
716 void                    hv_ring_buffer_read_begin(
717                                 hv_vmbus_ring_buffer_info       *ring_info);
718
719 uint32_t                hv_ring_buffer_read_end(
720                                 hv_vmbus_ring_buffer_info       *ring_info);
721
722 hv_vmbus_channel*       hv_vmbus_allocate_channel(void);
723 void                    hv_vmbus_free_vmbus_channel(hv_vmbus_channel *channel);
724 int                     hv_vmbus_request_channel_offers(void);
725 void                    hv_vmbus_release_unattached_channels(void);
726 int                     hv_vmbus_init(void);
727 void                    hv_vmbus_cleanup(void);
728
729 uint16_t                hv_vmbus_post_msg_via_msg_ipc(
730                                 hv_vmbus_connection_id  connection_id,
731                                 hv_vmbus_msg_type       message_type,
732                                 void                    *payload,
733                                 size_t                  payload_size);
734
735 uint16_t                hv_vmbus_signal_event(void *con_id);
736 void                    hv_vmbus_synic_init(void *irq_arg);
737 void                    hv_vmbus_synic_cleanup(void *arg);
738
739 struct hv_device*       hv_vmbus_child_device_create(
740                                 hv_guid                 device_type,
741                                 hv_guid                 device_instance,
742                                 hv_vmbus_channel        *channel);
743
744 int                     hv_vmbus_child_device_register(
745                                         struct hv_device *child_dev);
746 int                     hv_vmbus_child_device_unregister(
747                                         struct hv_device *child_dev);
748
749 /**
750  * Connection interfaces
751  */
752 int                     hv_vmbus_connect(void);
753 int                     hv_vmbus_disconnect(void);
754 int                     hv_vmbus_post_message(void *buffer, size_t buf_size);
755 int                     hv_vmbus_set_event(hv_vmbus_channel *channel);
756
757 /**
758  * Event Timer interfaces
759  */
760 void                    hv_et_init(void);
761 void                    hv_et_intr(struct trapframe*);
762
763 /* Wait for device creation */
764 void                    vmbus_scan(void);
765
766 /*
767  * The guest OS needs to register the guest ID with the hypervisor.
768  * The guest ID is a 64 bit entity and the structure of this ID is
769  * specified in the Hyper-V specification:
770  *
771  * http://msdn.microsoft.com/en-us/library/windows/
772  * hardware/ff542653%28v=vs.85%29.aspx
773  *
774  * While the current guideline does not specify how FreeBSD guest ID(s)
775  * need to be generated, our plan is to publish the guidelines for
776  * FreeBSD and other guest operating systems that currently are hosted
777  * on Hyper-V. The implementation here conforms to this yet
778  * unpublished guidelines.
779  *
780  * Bit(s)
781  * 63 - Indicates if the OS is Open Source or not; 1 is Open Source
782  * 62:56 - Os Type; Linux is 0x100, FreeBSD is 0x200
783  * 55:48 - Distro specific identification
784  * 47:16 - FreeBSD kernel version number
785  * 15:0  - Distro specific identification
786  *
787  */
788
789 #define HV_FREEBSD_VENDOR_ID    0x8200
790 #define HV_FREEBSD_GUEST_ID     hv_generate_guest_id(0,0)
791
792 static inline  uint64_t hv_generate_guest_id(
793         uint8_t distro_id_part1,
794         uint16_t distro_id_part2)
795 {
796         uint64_t guest_id;
797         guest_id =  (((uint64_t)HV_FREEBSD_VENDOR_ID) << 48);
798         guest_id |= (((uint64_t)(distro_id_part1)) << 48);
799         guest_id |= (((uint64_t)(__FreeBSD_version)) << 16); /* in param.h */
800         guest_id |= ((uint64_t)(distro_id_part2));
801         return guest_id;
802 }
803
804 typedef struct {
805         unsigned int    vector;
806         void            *page_buffers[2 * MAXCPU];
807 } hv_setup_args;
808
809 #endif  /* __HYPERV_PRIV_H__ */