hyperv_vmbus.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. #ifndef _HYPERV_VMBUS_H
  25. #define _HYPERV_VMBUS_H
  26. #include <linux/list.h>
  27. #include <asm/sync_bitops.h>
  28. #include <linux/atomic.h>
  29. #include <linux/hyperv.h>
  30. /*
  31. * The below CPUID leaves are present if VersionAndFeatures.HypervisorPresent
  32. * is set by CPUID(HVCPUID_VERSION_FEATURES).
  33. */
  34. enum hv_cpuid_function {
  35. HVCPUID_VERSION_FEATURES = 0x00000001,
  36. HVCPUID_VENDOR_MAXFUNCTION = 0x40000000,
  37. HVCPUID_INTERFACE = 0x40000001,
  38. /*
  39. * The remaining functions depend on the value of
  40. * HVCPUID_INTERFACE
  41. */
  42. HVCPUID_VERSION = 0x40000002,
  43. HVCPUID_FEATURES = 0x40000003,
  44. HVCPUID_ENLIGHTENMENT_INFO = 0x40000004,
  45. HVCPUID_IMPLEMENTATION_LIMITS = 0x40000005,
  46. };
  47. /* Define version of the synthetic interrupt controller. */
  48. #define HV_SYNIC_VERSION (1)
  49. /* Define the expected SynIC version. */
  50. #define HV_SYNIC_VERSION_1 (0x1)
  51. /* Define synthetic interrupt controller message constants. */
  52. #define HV_MESSAGE_SIZE (256)
  53. #define HV_MESSAGE_PAYLOAD_BYTE_COUNT (240)
  54. #define HV_MESSAGE_PAYLOAD_QWORD_COUNT (30)
  55. #define HV_ANY_VP (0xFFFFFFFF)
  56. /* Define synthetic interrupt controller flag constants. */
  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(u32))
  60. /* Define hypervisor message types. */
  61. enum hv_message_type {
  62. HVMSG_NONE = 0x00000000,
  63. /* Memory access messages. */
  64. HVMSG_UNMAPPED_GPA = 0x80000000,
  65. HVMSG_GPA_INTERCEPT = 0x80000001,
  66. /* Timer notification messages. */
  67. HVMSG_TIMER_EXPIRED = 0x80000010,
  68. /* Error messages. */
  69. HVMSG_INVALID_VP_REGISTER_VALUE = 0x80000020,
  70. HVMSG_UNRECOVERABLE_EXCEPTION = 0x80000021,
  71. HVMSG_UNSUPPORTED_FEATURE = 0x80000022,
  72. /* Trace buffer complete messages. */
  73. HVMSG_EVENTLOG_BUFFERCOMPLETE = 0x80000040,
  74. /* Platform-specific processor intercept messages. */
  75. HVMSG_X64_IOPORT_INTERCEPT = 0x80010000,
  76. HVMSG_X64_MSR_INTERCEPT = 0x80010001,
  77. HVMSG_X64_CPUID_INTERCEPT = 0x80010002,
  78. HVMSG_X64_EXCEPTION_INTERCEPT = 0x80010003,
  79. HVMSG_X64_APIC_EOI = 0x80010004,
  80. HVMSG_X64_LEGACY_FP_ERROR = 0x80010005
  81. };
  82. /* Define the number of synthetic interrupt sources. */
  83. #define HV_SYNIC_SINT_COUNT (16)
  84. #define HV_SYNIC_STIMER_COUNT (4)
  85. /* Define invalid partition identifier. */
  86. #define HV_PARTITION_ID_INVALID ((u64)0x0)
  87. /* Define connection identifier type. */
  88. union hv_connection_id {
  89. u32 asu32;
  90. struct {
  91. u32 id:24;
  92. u32 reserved:8;
  93. } u;
  94. };
  95. /* Define port identifier type. */
  96. union hv_port_id {
  97. u32 asu32;
  98. struct {
  99. u32 id:24;
  100. u32 reserved:8;
  101. } u ;
  102. };
  103. /* Define port type. */
  104. enum hv_port_type {
  105. HVPORT_MSG = 1,
  106. HVPORT_EVENT = 2,
  107. HVPORT_MONITOR = 3
  108. };
  109. /* Define port information structure. */
  110. struct hv_port_info {
  111. enum hv_port_type port_type;
  112. u32 padding;
  113. union {
  114. struct {
  115. u32 target_sint;
  116. u32 target_vp;
  117. u64 rsvdz;
  118. } message_port_info;
  119. struct {
  120. u32 target_sint;
  121. u32 target_vp;
  122. u16 base_flag_bumber;
  123. u16 flag_count;
  124. u32 rsvdz;
  125. } event_port_info;
  126. struct {
  127. u64 monitor_address;
  128. u64 rsvdz;
  129. } monitor_port_info;
  130. };
  131. };
  132. struct hv_connection_info {
  133. enum hv_port_type port_type;
  134. u32 padding;
  135. union {
  136. struct {
  137. u64 rsvdz;
  138. } message_connection_info;
  139. struct {
  140. u64 rsvdz;
  141. } event_connection_info;
  142. struct {
  143. u64 monitor_address;
  144. } monitor_connection_info;
  145. };
  146. };
  147. /* Define synthetic interrupt controller message flags. */
  148. union hv_message_flags {
  149. u8 asu8;
  150. struct {
  151. u8 msg_pending:1;
  152. u8 reserved:7;
  153. };
  154. };
  155. /* Define synthetic interrupt controller message header. */
  156. struct hv_message_header {
  157. enum hv_message_type message_type;
  158. u8 payload_size;
  159. union hv_message_flags message_flags;
  160. u8 reserved[2];
  161. union {
  162. u64 sender;
  163. union hv_port_id port;
  164. };
  165. };
  166. /* Define timer message payload structure. */
  167. struct hv_timer_message_payload {
  168. u32 timer_index;
  169. u32 reserved;
  170. u64 expiration_time; /* When the timer expired */
  171. u64 delivery_time; /* When the message was delivered */
  172. };
  173. /* Define synthetic interrupt controller message format. */
  174. struct hv_message {
  175. struct hv_message_header header;
  176. union {
  177. u64 payload[HV_MESSAGE_PAYLOAD_QWORD_COUNT];
  178. } u ;
  179. };
  180. /* Define the number of message buffers associated with each port. */
  181. #define HV_PORT_MESSAGE_BUFFER_COUNT (16)
  182. /* Define the synthetic interrupt message page layout. */
  183. struct hv_message_page {
  184. struct hv_message sint_message[HV_SYNIC_SINT_COUNT];
  185. };
  186. /* Define the synthetic interrupt controller event flags format. */
  187. union hv_synic_event_flags {
  188. u8 flags8[HV_EVENT_FLAGS_BYTE_COUNT];
  189. u32 flags32[HV_EVENT_FLAGS_DWORD_COUNT];
  190. };
  191. /* Define the synthetic interrupt flags page layout. */
  192. struct hv_synic_event_flags_page {
  193. union hv_synic_event_flags sintevent_flags[HV_SYNIC_SINT_COUNT];
  194. };
  195. /* Define SynIC control register. */
  196. union hv_synic_scontrol {
  197. u64 as_uint64;
  198. struct {
  199. u64 enable:1;
  200. u64 reserved:63;
  201. };
  202. };
  203. /* Define synthetic interrupt source. */
  204. union hv_synic_sint {
  205. u64 as_uint64;
  206. struct {
  207. u64 vector:8;
  208. u64 reserved1:8;
  209. u64 masked:1;
  210. u64 auto_eoi:1;
  211. u64 reserved2:46;
  212. };
  213. };
  214. /* Define the format of the SIMP register */
  215. union hv_synic_simp {
  216. u64 as_uint64;
  217. struct {
  218. u64 simp_enabled:1;
  219. u64 preserved:11;
  220. u64 base_simp_gpa:52;
  221. };
  222. };
  223. /* Define the format of the SIEFP register */
  224. union hv_synic_siefp {
  225. u64 as_uint64;
  226. struct {
  227. u64 siefp_enabled:1;
  228. u64 preserved:11;
  229. u64 base_siefp_gpa:52;
  230. };
  231. };
  232. /* Definitions for the monitored notification facility */
  233. union hv_monitor_trigger_group {
  234. u64 as_uint64;
  235. struct {
  236. u32 pending;
  237. u32 armed;
  238. };
  239. };
  240. struct hv_monitor_parameter {
  241. union hv_connection_id connectionid;
  242. u16 flagnumber;
  243. u16 rsvdz;
  244. };
  245. union hv_monitor_trigger_state {
  246. u32 asu32;
  247. struct {
  248. u32 group_enable:4;
  249. u32 rsvdz:28;
  250. };
  251. };
  252. /* struct hv_monitor_page Layout */
  253. /* ------------------------------------------------------ */
  254. /* | 0 | TriggerState (4 bytes) | Rsvd1 (4 bytes) | */
  255. /* | 8 | TriggerGroup[0] | */
  256. /* | 10 | TriggerGroup[1] | */
  257. /* | 18 | TriggerGroup[2] | */
  258. /* | 20 | TriggerGroup[3] | */
  259. /* | 28 | Rsvd2[0] | */
  260. /* | 30 | Rsvd2[1] | */
  261. /* | 38 | Rsvd2[2] | */
  262. /* | 40 | NextCheckTime[0][0] | NextCheckTime[0][1] | */
  263. /* | ... | */
  264. /* | 240 | Latency[0][0..3] | */
  265. /* | 340 | Rsvz3[0] | */
  266. /* | 440 | Parameter[0][0] | */
  267. /* | 448 | Parameter[0][1] | */
  268. /* | ... | */
  269. /* | 840 | Rsvd4[0] | */
  270. /* ------------------------------------------------------ */
  271. struct hv_monitor_page {
  272. union hv_monitor_trigger_state trigger_state;
  273. u32 rsvdz1;
  274. union hv_monitor_trigger_group trigger_group[4];
  275. u64 rsvdz2[3];
  276. s32 next_checktime[4][32];
  277. u16 latency[4][32];
  278. u64 rsvdz3[32];
  279. struct hv_monitor_parameter parameter[4][32];
  280. u8 rsvdz4[1984];
  281. };
  282. /* Declare the various hypercall operations. */
  283. enum hv_call_code {
  284. HVCALL_POST_MESSAGE = 0x005c,
  285. HVCALL_SIGNAL_EVENT = 0x005d,
  286. };
  287. /* Definition of the hv_post_message hypercall input structure. */
  288. struct hv_input_post_message {
  289. union hv_connection_id connectionid;
  290. u32 reserved;
  291. enum hv_message_type message_type;
  292. u32 payload_size;
  293. u64 payload[HV_MESSAGE_PAYLOAD_QWORD_COUNT];
  294. };
  295. /* Definition of the hv_signal_event hypercall input structure. */
  296. struct hv_input_signal_event {
  297. union hv_connection_id connectionid;
  298. u16 flag_number;
  299. u16 rsvdz;
  300. };
  301. /*
  302. * Versioning definitions used for guests reporting themselves to the
  303. * hypervisor, and visa versa.
  304. */
  305. /* Version info reported by guest OS's */
  306. enum hv_guest_os_vendor {
  307. HVGUESTOS_VENDOR_MICROSOFT = 0x0001
  308. };
  309. enum hv_guest_os_microsoft_ids {
  310. HVGUESTOS_MICROSOFT_UNDEFINED = 0x00,
  311. HVGUESTOS_MICROSOFT_MSDOS = 0x01,
  312. HVGUESTOS_MICROSOFT_WINDOWS3X = 0x02,
  313. HVGUESTOS_MICROSOFT_WINDOWS9X = 0x03,
  314. HVGUESTOS_MICROSOFT_WINDOWSNT = 0x04,
  315. HVGUESTOS_MICROSOFT_WINDOWSCE = 0x05
  316. };
  317. /*
  318. * Declare the MSR used to identify the guest OS.
  319. */
  320. #define HV_X64_MSR_GUEST_OS_ID 0x40000000
  321. union hv_x64_msr_guest_os_id_contents {
  322. u64 as_uint64;
  323. struct {
  324. u64 build_number:16;
  325. u64 service_version:8; /* Service Pack, etc. */
  326. u64 minor_version:8;
  327. u64 major_version:8;
  328. u64 os_id:8; /* enum hv_guest_os_microsoft_ids (if Vendor=MS) */
  329. u64 vendor_id:16; /* enum hv_guest_os_vendor */
  330. };
  331. };
  332. /*
  333. * Declare the MSR used to setup pages used to communicate with the hypervisor.
  334. */
  335. #define HV_X64_MSR_HYPERCALL 0x40000001
  336. union hv_x64_msr_hypercall_contents {
  337. u64 as_uint64;
  338. struct {
  339. u64 enable:1;
  340. u64 reserved:11;
  341. u64 guest_physical_address:52;
  342. };
  343. };
  344. enum {
  345. VMBUS_MESSAGE_CONNECTION_ID = 1,
  346. VMBUS_MESSAGE_PORT_ID = 1,
  347. VMBUS_EVENT_CONNECTION_ID = 2,
  348. VMBUS_EVENT_PORT_ID = 2,
  349. VMBUS_MONITOR_CONNECTION_ID = 3,
  350. VMBUS_MONITOR_PORT_ID = 3,
  351. VMBUS_MESSAGE_SINT = 2,
  352. };
  353. /* #defines */
  354. #define HV_PRESENT_BIT 0x80000000
  355. #define HV_LINUX_GUEST_ID_LO 0x00000000
  356. #define HV_LINUX_GUEST_ID_HI 0xB16B00B5
  357. #define HV_LINUX_GUEST_ID (((u64)HV_LINUX_GUEST_ID_HI << 32) | \
  358. HV_LINUX_GUEST_ID_LO)
  359. #define HV_CPU_POWER_MANAGEMENT (1 << 0)
  360. #define HV_RECOMMENDATIONS_MAX 4
  361. #define HV_X64_MAX 5
  362. #define HV_CAPS_MAX 8
  363. #define HV_HYPERCALL_PARAM_ALIGN sizeof(u64)
  364. /* Service definitions */
  365. #define HV_SERVICE_PARENT_PORT (0)
  366. #define HV_SERVICE_PARENT_CONNECTION (0)
  367. #define HV_SERVICE_CONNECT_RESPONSE_SUCCESS (0)
  368. #define HV_SERVICE_CONNECT_RESPONSE_INVALID_PARAMETER (1)
  369. #define HV_SERVICE_CONNECT_RESPONSE_UNKNOWN_SERVICE (2)
  370. #define HV_SERVICE_CONNECT_RESPONSE_CONNECTION_REJECTED (3)
  371. #define HV_SERVICE_CONNECT_REQUEST_MESSAGE_ID (1)
  372. #define HV_SERVICE_CONNECT_RESPONSE_MESSAGE_ID (2)
  373. #define HV_SERVICE_DISCONNECT_REQUEST_MESSAGE_ID (3)
  374. #define HV_SERVICE_DISCONNECT_RESPONSE_MESSAGE_ID (4)
  375. #define HV_SERVICE_MAX_MESSAGE_ID (4)
  376. #define HV_SERVICE_PROTOCOL_VERSION (0x0010)
  377. #define HV_CONNECT_PAYLOAD_BYTE_COUNT 64
  378. /* #define VMBUS_REVISION_NUMBER 6 */
  379. /* Our local vmbus's port and connection id. Anything >0 is fine */
  380. /* #define VMBUS_PORT_ID 11 */
  381. /* 628180B8-308D-4c5e-B7DB-1BEB62E62EF4 */
  382. static const uuid_le VMBUS_SERVICE_ID = {
  383. .b = {
  384. 0xb8, 0x80, 0x81, 0x62, 0x8d, 0x30, 0x5e, 0x4c,
  385. 0xb7, 0xdb, 0x1b, 0xeb, 0x62, 0xe6, 0x2e, 0xf4
  386. },
  387. };
  388. struct hv_input_signal_event_buffer {
  389. u64 align8;
  390. struct hv_input_signal_event event;
  391. };
  392. struct hv_context {
  393. /* We only support running on top of Hyper-V
  394. * So at this point this really can only contain the Hyper-V ID
  395. */
  396. u64 guestid;
  397. void *hypercall_page;
  398. bool synic_initialized;
  399. /*
  400. * This is used as an input param to HvCallSignalEvent hypercall. The
  401. * input param is immutable in our usage and must be dynamic mem (vs
  402. * stack or global). */
  403. struct hv_input_signal_event_buffer *signal_event_buffer;
  404. /* 8-bytes aligned of the buffer above */
  405. struct hv_input_signal_event *signal_event_param;
  406. void *synic_message_page[NR_CPUS];
  407. void *synic_event_page[NR_CPUS];
  408. };
  409. extern struct hv_context hv_context;
  410. /* Hv Interface */
  411. extern int hv_init(void);
  412. extern void hv_cleanup(void);
  413. extern u16 hv_post_message(union hv_connection_id connection_id,
  414. enum hv_message_type message_type,
  415. void *payload, size_t payload_size);
  416. extern u16 hv_signal_event(void);
  417. extern void hv_synic_init(void *irqarg);
  418. extern void hv_synic_cleanup(void *arg);
  419. /* Interface */
  420. int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info, void *buffer,
  421. u32 buflen);
  422. void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info);
  423. int hv_ringbuffer_write(struct hv_ring_buffer_info *ring_info,
  424. struct scatterlist *sglist,
  425. u32 sgcount);
  426. int hv_ringbuffer_peek(struct hv_ring_buffer_info *ring_info, void *buffer,
  427. u32 buflen);
  428. int hv_ringbuffer_read(struct hv_ring_buffer_info *ring_info,
  429. void *buffer,
  430. u32 buflen,
  431. u32 offset);
  432. u32 hv_get_ringbuffer_interrupt_mask(struct hv_ring_buffer_info *ring_info);
  433. void hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
  434. struct hv_ring_buffer_debug_info *debug_info);
  435. /*
  436. * Maximum channels is determined by the size of the interrupt page
  437. * which is PAGE_SIZE. 1/2 of PAGE_SIZE is for send endpoint interrupt
  438. * and the other is receive endpoint interrupt
  439. */
  440. #define MAX_NUM_CHANNELS ((PAGE_SIZE >> 1) << 3) /* 16348 channels */
  441. /* The value here must be in multiple of 32 */
  442. /* TODO: Need to make this configurable */
  443. #define MAX_NUM_CHANNELS_SUPPORTED 256
  444. enum vmbus_connect_state {
  445. DISCONNECTED,
  446. CONNECTING,
  447. CONNECTED,
  448. DISCONNECTING
  449. };
  450. #define MAX_SIZE_CHANNEL_MESSAGE HV_MESSAGE_PAYLOAD_BYTE_COUNT
  451. struct vmbus_connection {
  452. enum vmbus_connect_state conn_state;
  453. atomic_t next_gpadl_handle;
  454. /*
  455. * Represents channel interrupts. Each bit position represents a
  456. * channel. When a channel sends an interrupt via VMBUS, it finds its
  457. * bit in the sendInterruptPage, set it and calls Hv to generate a port
  458. * event. The other end receives the port event and parse the
  459. * recvInterruptPage to see which bit is set
  460. */
  461. void *int_page;
  462. void *send_int_page;
  463. void *recv_int_page;
  464. /*
  465. * 2 pages - 1st page for parent->child notification and 2nd
  466. * is child->parent notification
  467. */
  468. void *monitor_pages;
  469. struct list_head chn_msg_list;
  470. spinlock_t channelmsg_lock;
  471. /* List of channels */
  472. struct list_head chn_list;
  473. spinlock_t channel_lock;
  474. struct workqueue_struct *work_queue;
  475. };
  476. struct vmbus_msginfo {
  477. /* Bookkeeping stuff */
  478. struct list_head msglist_entry;
  479. /* The message itself */
  480. unsigned char msg[0];
  481. };
  482. extern struct vmbus_connection vmbus_connection;
  483. /* General vmbus interface */
  484. struct hv_device *vmbus_device_create(uuid_le *type,
  485. uuid_le *instance,
  486. struct vmbus_channel *channel);
  487. int vmbus_device_register(struct hv_device *child_device_obj);
  488. void vmbus_device_unregister(struct hv_device *device_obj);
  489. /* static void */
  490. /* VmbusChildDeviceDestroy( */
  491. /* struct hv_device *); */
  492. struct vmbus_channel *relid2channel(u32 relid);
  493. void vmbus_free_channels(void);
  494. /* Connection interface */
  495. int vmbus_connect(void);
  496. int vmbus_post_msg(void *buffer, size_t buflen);
  497. int vmbus_set_event(u32 child_relid);
  498. void vmbus_on_event(unsigned long data);
  499. #endif /* _HYPERV_VMBUS_H */