Merge branch 'for-linus' of git://git.kernel.dk/linux-block
[pandora-kernel.git] / drivers / staging / hv / hyperv_vmbus.h
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
32 #include "hyperv.h"
33
34 /*
35  * The below CPUID leaves are present if VersionAndFeatures.HypervisorPresent
36  * is set by CPUID(HVCPUID_VERSION_FEATURES).
37  */
38 enum hv_cpuid_function {
39         HVCPUID_VERSION_FEATURES                = 0x00000001,
40         HVCPUID_VENDOR_MAXFUNCTION              = 0x40000000,
41         HVCPUID_INTERFACE                       = 0x40000001,
42
43         /*
44          * The remaining functions depend on the value of
45          * HVCPUID_INTERFACE
46          */
47         HVCPUID_VERSION                 = 0x40000002,
48         HVCPUID_FEATURES                        = 0x40000003,
49         HVCPUID_ENLIGHTENMENT_INFO      = 0x40000004,
50         HVCPUID_IMPLEMENTATION_LIMITS           = 0x40000005,
51 };
52
53 /* Define version of the synthetic interrupt controller. */
54 #define HV_SYNIC_VERSION                (1)
55
56 /* Define the expected SynIC version. */
57 #define HV_SYNIC_VERSION_1              (0x1)
58
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)
64
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))
69
70 /* Define hypervisor message types. */
71 enum hv_message_type {
72         HVMSG_NONE                      = 0x00000000,
73
74         /* Memory access messages. */
75         HVMSG_UNMAPPED_GPA              = 0x80000000,
76         HVMSG_GPA_INTERCEPT             = 0x80000001,
77
78         /* Timer notification messages. */
79         HVMSG_TIMER_EXPIRED                     = 0x80000010,
80
81         /* Error messages. */
82         HVMSG_INVALID_VP_REGISTER_VALUE = 0x80000020,
83         HVMSG_UNRECOVERABLE_EXCEPTION   = 0x80000021,
84         HVMSG_UNSUPPORTED_FEATURE               = 0x80000022,
85
86         /* Trace buffer complete messages. */
87         HVMSG_EVENTLOG_BUFFERCOMPLETE   = 0x80000040,
88
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
96 };
97
98 /* Define the number of synthetic interrupt sources. */
99 #define HV_SYNIC_SINT_COUNT             (16)
100 #define HV_SYNIC_STIMER_COUNT           (4)
101
102 /* Define invalid partition identifier. */
103 #define HV_PARTITION_ID_INVALID         ((u64)0x0)
104
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;
112 };
113
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 ;
121 };
122
123 /* Define port type. */
124 enum hv_port_type {
125         HVPORT_MSG      = 1,
126         HVPORT_EVENT            = 2,
127         HVPORT_MONITOR  = 3
128 };
129
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;
151         };
152 };
153
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;
167         };
168 };
169
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;
176         };
177 };
178
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;
188         };
189 };
190
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 */
197 };
198
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 ;
205 };
206
207 /* Define the number of message buffers associated with each port. */
208 #define HV_PORT_MESSAGE_BUFFER_COUNT    (16)
209
210 /* Define the synthetic interrupt message page layout. */
211 struct hv_message_page {
212         struct hv_message sint_message[HV_SYNIC_SINT_COUNT];
213 };
214
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];
219 };
220
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];
224 };
225
226 /* Define SynIC control register. */
227 union hv_synic_scontrol {
228         u64 as_uint64;
229         struct {
230                 u64 enable:1;
231                 u64 reserved:63;
232         };
233 };
234
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;
244         };
245 };
246
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;
254         };
255 };
256
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;
264         };
265 };
266
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;
273         };
274 };
275
276 struct hv_monitor_parameter {
277         union hv_connection_id connectionid;
278         u16 flagnumber;
279         u16 rsvdz;
280 };
281
282 union hv_monitor_trigger_state {
283         u32 asu32;
284
285         struct {
286                 u32 group_enable:4;
287                 u32 rsvdz:28;
288         };
289 };
290
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;
313
314         union hv_monitor_trigger_group trigger_group[4];
315         u64 rsvdz2[3];
316
317         s32 next_checktime[4][32];
318
319         u16 latency[4][32];
320         u64 rsvdz3[32];
321
322         struct hv_monitor_parameter parameter[4][32];
323
324         u8 rsvdz4[1984];
325 };
326
327 /* Declare the various hypercall operations. */
328 enum hv_call_code {
329         HVCALL_POST_MESSAGE     = 0x005c,
330         HVCALL_SIGNAL_EVENT     = 0x005d,
331 };
332
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];
340 };
341
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;
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 #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)
418
419 #define HV_CPU_POWER_MANAGEMENT         (1 << 0)
420 #define HV_RECOMMENDATIONS_MAX          4
421
422 #define HV_X64_MAX                      5
423 #define HV_CAPS_MAX                     8
424
425
426 #define HV_HYPERCALL_PARAM_ALIGN        sizeof(u64)
427
428
429 /* Service definitions */
430
431 #define HV_SERVICE_PARENT_PORT                          (0)
432 #define HV_SERVICE_PARENT_CONNECTION                    (0)
433
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)
438
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)
444
445 #define HV_SERVICE_PROTOCOL_VERSION (0x0010)
446 #define HV_CONNECT_PAYLOAD_BYTE_COUNT 64
447
448 /* #define VMBUS_REVISION_NUMBER        6 */
449
450 /* Our local vmbus's port and connection id. Anything >0 is fine */
451 /* #define VMBUS_PORT_ID                11 */
452
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
458         },
459 };
460
461 #define MAX_NUM_CPUS    32
462
463
464 struct hv_input_signal_event_buffer {
465         u64 align8;
466         struct hv_input_signal_event event;
467 };
468
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
472         */
473         u64 guestid;
474
475         void *hypercall_page;
476
477         bool synic_initialized;
478
479         /*
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;
486
487         void *synic_message_page[MAX_NUM_CPUS];
488         void *synic_event_page[MAX_NUM_CPUS];
489 };
490
491 extern struct hv_context hv_context;
492
493
494 /* Hv Interface */
495
496 extern int hv_init(void);
497
498 extern void hv_cleanup(void);
499
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);
503
504 extern u16 hv_signal_event(void);
505
506 extern void hv_synic_init(void *irqarg);
507
508 extern void hv_synic_cleanup(void *arg);
509
510
511 /* Interface */
512
513
514 int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info, void *buffer,
515                    u32 buflen);
516
517 void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info);
518
519 int hv_ringbuffer_write(struct hv_ring_buffer_info *ring_info,
520                     struct scatterlist *sglist,
521                     u32 sgcount);
522
523 int hv_ringbuffer_peek(struct hv_ring_buffer_info *ring_info, void *buffer,
524                    u32 buflen);
525
526 int hv_ringbuffer_read(struct hv_ring_buffer_info *ring_info,
527                    void *buffer,
528                    u32 buflen,
529                    u32 offset);
530
531 u32 hv_get_ringbuffer_interrupt_mask(struct hv_ring_buffer_info *ring_info);
532
533 void hv_dump_ring_info(struct hv_ring_buffer_info *ring_info, char *prefix);
534
535 void hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
536                             struct hv_ring_buffer_debug_info *debug_info);
537
538 /*
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
542  */
543 #define MAX_NUM_CHANNELS        ((PAGE_SIZE >> 1) << 3) /* 16348 channels */
544
545 /* The value here must be in multiple of 32 */
546 /* TODO: Need to make this configurable */
547 #define MAX_NUM_CHANNELS_SUPPORTED      256
548
549
550 enum vmbus_connect_state {
551         DISCONNECTED,
552         CONNECTING,
553         CONNECTED,
554         DISCONNECTING
555 };
556
557 #define MAX_SIZE_CHANNEL_MESSAGE        HV_MESSAGE_PAYLOAD_BYTE_COUNT
558
559 struct vmbus_connection {
560         enum vmbus_connect_state conn_state;
561
562         atomic_t next_gpadl_handle;
563
564         /*
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
570          */
571         void *int_page;
572         void *send_int_page;
573         void *recv_int_page;
574
575         /*
576          * 2 pages - 1st page for parent->child notification and 2nd
577          * is child->parent notification
578          */
579         void *monitor_pages;
580         struct list_head chn_msg_list;
581         spinlock_t channelmsg_lock;
582
583         /* List of channels */
584         struct list_head chn_list;
585         spinlock_t channel_lock;
586
587         struct workqueue_struct *work_queue;
588 };
589
590
591 struct vmbus_msginfo {
592         /* Bookkeeping stuff */
593         struct list_head msglist_entry;
594
595         /* The message itself */
596         unsigned char msg[0];
597 };
598
599
600 extern struct vmbus_connection vmbus_connection;
601
602 /* General vmbus interface */
603
604 struct hv_device *vmbus_child_device_create(struct hv_guid *type,
605                                          struct hv_guid *instance,
606                                          struct vmbus_channel *channel);
607
608 int vmbus_child_device_register(struct hv_device *child_device_obj);
609 void vmbus_child_device_unregister(struct hv_device *device_obj);
610
611 /* static void */
612 /* VmbusChildDeviceDestroy( */
613 /* struct hv_device *); */
614
615 struct vmbus_channel *relid2channel(u32 relid);
616
617
618 /* Connection interface */
619
620 int vmbus_connect(void);
621
622 int vmbus_post_msg(void *buffer, size_t buflen);
623
624 int vmbus_set_event(u32 child_relid);
625
626 void vmbus_on_event(unsigned long data);
627
628
629 #endif /* _HYPERV_VMBUS_H */