| .. | .. |
|---|
| 1 | +/* SPDX-License-Identifier: GPL-2.0-only */ |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * |
|---|
| 3 | 4 | * 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 | 5 | * |
|---|
| 18 | 6 | * Authors: |
|---|
| 19 | 7 | * Haiyang Zhang <haiyangz@microsoft.com> |
|---|
| 20 | 8 | * Hank Janssen <hjanssen@microsoft.com> |
|---|
| 21 | 9 | * K. Y. Srinivasan <kys@microsoft.com> |
|---|
| 22 | | - * |
|---|
| 23 | 10 | */ |
|---|
| 24 | 11 | |
|---|
| 25 | 12 | #ifndef _HYPERV_VMBUS_H |
|---|
| .. | .. |
|---|
| 45 | 32 | */ |
|---|
| 46 | 33 | #define HV_UTIL_NEGO_TIMEOUT 55 |
|---|
| 47 | 34 | |
|---|
| 48 | | -/* Define synthetic interrupt controller flag constants. */ |
|---|
| 49 | | -#define HV_EVENT_FLAGS_COUNT (256 * 8) |
|---|
| 50 | | -#define HV_EVENT_FLAGS_LONG_COUNT (256 / sizeof(unsigned long)) |
|---|
| 51 | | - |
|---|
| 52 | | -/* |
|---|
| 53 | | - * Timer configuration register. |
|---|
| 54 | | - */ |
|---|
| 55 | | -union hv_timer_config { |
|---|
| 56 | | - u64 as_uint64; |
|---|
| 57 | | - struct { |
|---|
| 58 | | - u64 enable:1; |
|---|
| 59 | | - u64 periodic:1; |
|---|
| 60 | | - u64 lazy:1; |
|---|
| 61 | | - u64 auto_enable:1; |
|---|
| 62 | | - u64 apic_vector:8; |
|---|
| 63 | | - u64 direct_mode:1; |
|---|
| 64 | | - u64 reserved_z0:3; |
|---|
| 65 | | - u64 sintx:4; |
|---|
| 66 | | - u64 reserved_z1:44; |
|---|
| 67 | | - }; |
|---|
| 68 | | -}; |
|---|
| 69 | | - |
|---|
| 70 | | - |
|---|
| 71 | | -/* Define the synthetic interrupt controller event flags format. */ |
|---|
| 72 | | -union hv_synic_event_flags { |
|---|
| 73 | | - unsigned long flags[HV_EVENT_FLAGS_LONG_COUNT]; |
|---|
| 74 | | -}; |
|---|
| 75 | | - |
|---|
| 76 | | -/* Define SynIC control register. */ |
|---|
| 77 | | -union hv_synic_scontrol { |
|---|
| 78 | | - u64 as_uint64; |
|---|
| 79 | | - struct { |
|---|
| 80 | | - u64 enable:1; |
|---|
| 81 | | - u64 reserved:63; |
|---|
| 82 | | - }; |
|---|
| 83 | | -}; |
|---|
| 84 | | - |
|---|
| 85 | | -/* Define synthetic interrupt source. */ |
|---|
| 86 | | -union hv_synic_sint { |
|---|
| 87 | | - u64 as_uint64; |
|---|
| 88 | | - struct { |
|---|
| 89 | | - u64 vector:8; |
|---|
| 90 | | - u64 reserved1:8; |
|---|
| 91 | | - u64 masked:1; |
|---|
| 92 | | - u64 auto_eoi:1; |
|---|
| 93 | | - u64 reserved2:46; |
|---|
| 94 | | - }; |
|---|
| 95 | | -}; |
|---|
| 96 | | - |
|---|
| 97 | | -/* Define the format of the SIMP register */ |
|---|
| 98 | | -union hv_synic_simp { |
|---|
| 99 | | - u64 as_uint64; |
|---|
| 100 | | - struct { |
|---|
| 101 | | - u64 simp_enabled:1; |
|---|
| 102 | | - u64 preserved:11; |
|---|
| 103 | | - u64 base_simp_gpa:52; |
|---|
| 104 | | - }; |
|---|
| 105 | | -}; |
|---|
| 106 | | - |
|---|
| 107 | | -/* Define the format of the SIEFP register */ |
|---|
| 108 | | -union hv_synic_siefp { |
|---|
| 109 | | - u64 as_uint64; |
|---|
| 110 | | - struct { |
|---|
| 111 | | - u64 siefp_enabled:1; |
|---|
| 112 | | - u64 preserved:11; |
|---|
| 113 | | - u64 base_siefp_gpa:52; |
|---|
| 114 | | - }; |
|---|
| 115 | | -}; |
|---|
| 116 | 35 | |
|---|
| 117 | 36 | /* Definitions for the monitored notification facility */ |
|---|
| 118 | 37 | union hv_monitor_trigger_group { |
|---|
| .. | .. |
|---|
| 214 | 133 | * basis. |
|---|
| 215 | 134 | */ |
|---|
| 216 | 135 | struct tasklet_struct msg_dpc; |
|---|
| 217 | | - |
|---|
| 218 | | - /* |
|---|
| 219 | | - * To optimize the mapping of relid to channel, maintain |
|---|
| 220 | | - * per-cpu list of the channels based on their CPU affinity. |
|---|
| 221 | | - */ |
|---|
| 222 | | - struct list_head chan_list; |
|---|
| 223 | | - struct clock_event_device *clk_evt; |
|---|
| 224 | 136 | }; |
|---|
| 225 | 137 | |
|---|
| 226 | 138 | struct hv_context { |
|---|
| .. | .. |
|---|
| 228 | 140 | * So at this point this really can only contain the Hyper-V ID |
|---|
| 229 | 141 | */ |
|---|
| 230 | 142 | u64 guestid; |
|---|
| 231 | | - |
|---|
| 232 | | - void *tsc_page; |
|---|
| 233 | | - |
|---|
| 234 | | - bool synic_initialized; |
|---|
| 235 | 143 | |
|---|
| 236 | 144 | struct hv_per_cpu_context __percpu *cpu_context; |
|---|
| 237 | 145 | |
|---|
| .. | .. |
|---|
| 256 | 164 | |
|---|
| 257 | 165 | extern void hv_synic_free(void); |
|---|
| 258 | 166 | |
|---|
| 167 | +extern void hv_synic_enable_regs(unsigned int cpu); |
|---|
| 259 | 168 | extern int hv_synic_init(unsigned int cpu); |
|---|
| 260 | 169 | |
|---|
| 170 | +extern void hv_synic_disable_regs(unsigned int cpu); |
|---|
| 261 | 171 | extern int hv_synic_cleanup(unsigned int cpu); |
|---|
| 262 | | - |
|---|
| 263 | | -extern void hv_synic_clockevents_cleanup(void); |
|---|
| 264 | 172 | |
|---|
| 265 | 173 | /* Interface */ |
|---|
| 266 | 174 | |
|---|
| 175 | +void hv_ringbuffer_pre_init(struct vmbus_channel *channel); |
|---|
| 267 | 176 | |
|---|
| 268 | 177 | int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info, |
|---|
| 269 | 178 | struct page *pages, u32 pagecnt); |
|---|
| .. | .. |
|---|
| 278 | 187 | u64 *requestid, bool raw); |
|---|
| 279 | 188 | |
|---|
| 280 | 189 | /* |
|---|
| 281 | | - * Maximum channels is determined by the size of the interrupt page |
|---|
| 282 | | - * which is PAGE_SIZE. 1/2 of PAGE_SIZE is for send endpoint interrupt |
|---|
| 283 | | - * and the other is receive endpoint interrupt |
|---|
| 190 | + * The Maximum number of channels (16348) is determined by the size of the |
|---|
| 191 | + * interrupt page, which is HV_HYP_PAGE_SIZE. 1/2 of HV_HYP_PAGE_SIZE is to |
|---|
| 192 | + * send endpoint interrupts, and the other is to receive endpoint interrupts. |
|---|
| 284 | 193 | */ |
|---|
| 285 | | -#define MAX_NUM_CHANNELS ((PAGE_SIZE >> 1) << 3) /* 16348 channels */ |
|---|
| 194 | +#define MAX_NUM_CHANNELS ((HV_HYP_PAGE_SIZE >> 1) << 3) |
|---|
| 286 | 195 | |
|---|
| 287 | 196 | /* The value here must be in multiple of 32 */ |
|---|
| 288 | 197 | /* TODO: Need to make this configurable */ |
|---|
| 289 | 198 | #define MAX_NUM_CHANNELS_SUPPORTED 256 |
|---|
| 290 | 199 | |
|---|
| 200 | +#define MAX_CHANNEL_RELIDS \ |
|---|
| 201 | + max(MAX_NUM_CHANNELS_SUPPORTED, HV_EVENT_FLAGS_COUNT) |
|---|
| 291 | 202 | |
|---|
| 292 | 203 | enum vmbus_connect_state { |
|---|
| 293 | 204 | DISCONNECTED, |
|---|
| .. | .. |
|---|
| 298 | 209 | |
|---|
| 299 | 210 | #define MAX_SIZE_CHANNEL_MESSAGE HV_MESSAGE_PAYLOAD_BYTE_COUNT |
|---|
| 300 | 211 | |
|---|
| 301 | | -struct vmbus_connection { |
|---|
| 302 | | - /* |
|---|
| 303 | | - * CPU on which the initial host contact was made. |
|---|
| 304 | | - */ |
|---|
| 305 | | - int connect_cpu; |
|---|
| 212 | +/* |
|---|
| 213 | + * The CPU that Hyper-V will interrupt for VMBUS messages, such as |
|---|
| 214 | + * CHANNELMSG_OFFERCHANNEL and CHANNELMSG_RESCIND_CHANNELOFFER. |
|---|
| 215 | + */ |
|---|
| 216 | +#define VMBUS_CONNECT_CPU 0 |
|---|
| 306 | 217 | |
|---|
| 218 | +struct vmbus_connection { |
|---|
| 307 | 219 | u32 msg_conn_id; |
|---|
| 308 | 220 | |
|---|
| 309 | 221 | atomic_t offer_in_progress; |
|---|
| .. | .. |
|---|
| 336 | 248 | struct list_head chn_list; |
|---|
| 337 | 249 | struct mutex channel_mutex; |
|---|
| 338 | 250 | |
|---|
| 251 | + /* Array of channels */ |
|---|
| 252 | + struct vmbus_channel **channels; |
|---|
| 253 | + |
|---|
| 339 | 254 | /* |
|---|
| 340 | 255 | * An offer message is handled first on the work_queue, and then |
|---|
| 341 | 256 | * is further handled on handle_primary_chan_wq or |
|---|
| .. | .. |
|---|
| 344 | 259 | struct workqueue_struct *work_queue; |
|---|
| 345 | 260 | struct workqueue_struct *handle_primary_chan_wq; |
|---|
| 346 | 261 | struct workqueue_struct *handle_sub_chan_wq; |
|---|
| 262 | + |
|---|
| 263 | + /* |
|---|
| 264 | + * The number of sub-channels and hv_sock channels that should be |
|---|
| 265 | + * cleaned up upon suspend: sub-channels will be re-created upon |
|---|
| 266 | + * resume, and hv_sock channels should not survive suspend. |
|---|
| 267 | + */ |
|---|
| 268 | + atomic_t nr_chan_close_on_suspend; |
|---|
| 269 | + /* |
|---|
| 270 | + * vmbus_bus_suspend() waits for "nr_chan_close_on_suspend" to |
|---|
| 271 | + * drop to zero. |
|---|
| 272 | + */ |
|---|
| 273 | + struct completion ready_for_suspend_event; |
|---|
| 274 | + |
|---|
| 275 | + /* |
|---|
| 276 | + * The number of primary channels that should be "fixed up" |
|---|
| 277 | + * upon resume: these channels are re-offered upon resume, and some |
|---|
| 278 | + * fields of the channel offers (i.e. child_relid and connection_id) |
|---|
| 279 | + * can change, so the old offermsg must be fixed up, before the resume |
|---|
| 280 | + * callbacks of the VSC drivers start to further touch the channels. |
|---|
| 281 | + */ |
|---|
| 282 | + atomic_t nr_chan_fixup_on_resume; |
|---|
| 283 | + /* |
|---|
| 284 | + * vmbus_bus_resume() waits for "nr_chan_fixup_on_resume" to |
|---|
| 285 | + * drop to zero. |
|---|
| 286 | + */ |
|---|
| 287 | + struct completion ready_for_resume_event; |
|---|
| 347 | 288 | }; |
|---|
| 348 | 289 | |
|---|
| 349 | 290 | |
|---|
| .. | .. |
|---|
| 352 | 293 | struct list_head msglist_entry; |
|---|
| 353 | 294 | |
|---|
| 354 | 295 | /* The message itself */ |
|---|
| 355 | | - unsigned char msg[0]; |
|---|
| 296 | + unsigned char msg[]; |
|---|
| 356 | 297 | }; |
|---|
| 357 | 298 | |
|---|
| 358 | 299 | |
|---|
| 359 | 300 | extern struct vmbus_connection vmbus_connection; |
|---|
| 301 | + |
|---|
| 302 | +int vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo, u32 version); |
|---|
| 360 | 303 | |
|---|
| 361 | 304 | static inline void vmbus_send_interrupt(u32 relid) |
|---|
| 362 | 305 | { |
|---|
| .. | .. |
|---|
| 375 | 318 | enum vmbus_channel_message_type message_type; |
|---|
| 376 | 319 | enum vmbus_message_handler_type handler_type; |
|---|
| 377 | 320 | void (*message_handler)(struct vmbus_channel_message_header *msg); |
|---|
| 321 | + u32 min_payload_len; |
|---|
| 378 | 322 | }; |
|---|
| 379 | 323 | |
|---|
| 380 | 324 | extern const struct vmbus_channel_message_table_entry |
|---|
| .. | .. |
|---|
| 383 | 327 | |
|---|
| 384 | 328 | /* General vmbus interface */ |
|---|
| 385 | 329 | |
|---|
| 386 | | -struct hv_device *vmbus_device_create(const uuid_le *type, |
|---|
| 387 | | - const uuid_le *instance, |
|---|
| 330 | +struct hv_device *vmbus_device_create(const guid_t *type, |
|---|
| 331 | + const guid_t *instance, |
|---|
| 388 | 332 | struct vmbus_channel *channel); |
|---|
| 389 | 333 | |
|---|
| 390 | 334 | int vmbus_device_register(struct hv_device *child_device_obj); |
|---|
| .. | .. |
|---|
| 393 | 337 | struct vmbus_channel *channel); |
|---|
| 394 | 338 | |
|---|
| 395 | 339 | void vmbus_remove_channel_attr_group(struct vmbus_channel *channel); |
|---|
| 340 | + |
|---|
| 341 | +void vmbus_channel_map_relid(struct vmbus_channel *channel); |
|---|
| 342 | +void vmbus_channel_unmap_relid(struct vmbus_channel *channel); |
|---|
| 396 | 343 | |
|---|
| 397 | 344 | struct vmbus_channel *relid2channel(u32 relid); |
|---|
| 398 | 345 | |
|---|
| .. | .. |
|---|
| 410 | 357 | |
|---|
| 411 | 358 | int hv_kvp_init(struct hv_util_service *srv); |
|---|
| 412 | 359 | void hv_kvp_deinit(void); |
|---|
| 360 | +int hv_kvp_pre_suspend(void); |
|---|
| 361 | +int hv_kvp_pre_resume(void); |
|---|
| 413 | 362 | void hv_kvp_onchannelcallback(void *context); |
|---|
| 414 | 363 | |
|---|
| 415 | 364 | int hv_vss_init(struct hv_util_service *srv); |
|---|
| 416 | 365 | void hv_vss_deinit(void); |
|---|
| 366 | +int hv_vss_pre_suspend(void); |
|---|
| 367 | +int hv_vss_pre_resume(void); |
|---|
| 417 | 368 | void hv_vss_onchannelcallback(void *context); |
|---|
| 418 | 369 | |
|---|
| 419 | 370 | int hv_fcopy_init(struct hv_util_service *srv); |
|---|
| 420 | 371 | void hv_fcopy_deinit(void); |
|---|
| 372 | +int hv_fcopy_pre_suspend(void); |
|---|
| 373 | +int hv_fcopy_pre_resume(void); |
|---|
| 421 | 374 | void hv_fcopy_onchannelcallback(void *context); |
|---|
| 422 | 375 | void vmbus_initiate_unload(bool crash); |
|---|
| 423 | 376 | |
|---|
| .. | .. |
|---|
| 426 | 379 | { |
|---|
| 427 | 380 | if (!channel) |
|---|
| 428 | 381 | return; |
|---|
| 429 | | - |
|---|
| 430 | | - if (in_interrupt() && (channel->target_cpu == smp_processor_id())) { |
|---|
| 431 | | - cb(channel); |
|---|
| 432 | | - return; |
|---|
| 433 | | - } |
|---|
| 434 | | - smp_call_function_single(channel->target_cpu, cb, channel, true); |
|---|
| 382 | + cb(channel); |
|---|
| 435 | 383 | } |
|---|
| 436 | 384 | |
|---|
| 437 | 385 | enum hvutil_device_state { |
|---|
| .. | .. |
|---|
| 443 | 391 | HVUTIL_DEVICE_DYING, /* driver unload is in progress */ |
|---|
| 444 | 392 | }; |
|---|
| 445 | 393 | |
|---|
| 394 | +enum delay { |
|---|
| 395 | + INTERRUPT_DELAY = 0, |
|---|
| 396 | + MESSAGE_DELAY = 1, |
|---|
| 397 | +}; |
|---|
| 398 | + |
|---|
| 399 | +extern const struct vmbus_device vmbus_devs[]; |
|---|
| 400 | + |
|---|
| 401 | +static inline bool hv_is_perf_channel(struct vmbus_channel *channel) |
|---|
| 402 | +{ |
|---|
| 403 | + return vmbus_devs[channel->device_id].perf_device; |
|---|
| 404 | +} |
|---|
| 405 | + |
|---|
| 406 | +static inline bool hv_is_alloced_cpu(unsigned int cpu) |
|---|
| 407 | +{ |
|---|
| 408 | + struct vmbus_channel *channel, *sc; |
|---|
| 409 | + |
|---|
| 410 | + lockdep_assert_held(&vmbus_connection.channel_mutex); |
|---|
| 411 | + /* |
|---|
| 412 | + * List additions/deletions as well as updates of the target CPUs are |
|---|
| 413 | + * protected by channel_mutex. |
|---|
| 414 | + */ |
|---|
| 415 | + list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) { |
|---|
| 416 | + if (!hv_is_perf_channel(channel)) |
|---|
| 417 | + continue; |
|---|
| 418 | + if (channel->target_cpu == cpu) |
|---|
| 419 | + return true; |
|---|
| 420 | + list_for_each_entry(sc, &channel->sc_list, sc_list) { |
|---|
| 421 | + if (sc->target_cpu == cpu) |
|---|
| 422 | + return true; |
|---|
| 423 | + } |
|---|
| 424 | + } |
|---|
| 425 | + return false; |
|---|
| 426 | +} |
|---|
| 427 | + |
|---|
| 428 | +static inline void hv_set_alloced_cpu(unsigned int cpu) |
|---|
| 429 | +{ |
|---|
| 430 | + cpumask_set_cpu(cpu, &hv_context.hv_numa_map[cpu_to_node(cpu)]); |
|---|
| 431 | +} |
|---|
| 432 | + |
|---|
| 433 | +static inline void hv_clear_alloced_cpu(unsigned int cpu) |
|---|
| 434 | +{ |
|---|
| 435 | + if (hv_is_alloced_cpu(cpu)) |
|---|
| 436 | + return; |
|---|
| 437 | + cpumask_clear_cpu(cpu, &hv_context.hv_numa_map[cpu_to_node(cpu)]); |
|---|
| 438 | +} |
|---|
| 439 | + |
|---|
| 440 | +static inline void hv_update_alloced_cpus(unsigned int old_cpu, |
|---|
| 441 | + unsigned int new_cpu) |
|---|
| 442 | +{ |
|---|
| 443 | + hv_set_alloced_cpu(new_cpu); |
|---|
| 444 | + hv_clear_alloced_cpu(old_cpu); |
|---|
| 445 | +} |
|---|
| 446 | + |
|---|
| 447 | +#ifdef CONFIG_HYPERV_TESTING |
|---|
| 448 | + |
|---|
| 449 | +int hv_debug_add_dev_dir(struct hv_device *dev); |
|---|
| 450 | +void hv_debug_rm_dev_dir(struct hv_device *dev); |
|---|
| 451 | +void hv_debug_rm_all_dir(void); |
|---|
| 452 | +int hv_debug_init(void); |
|---|
| 453 | +void hv_debug_delay_test(struct vmbus_channel *channel, enum delay delay_type); |
|---|
| 454 | + |
|---|
| 455 | +#else /* CONFIG_HYPERV_TESTING */ |
|---|
| 456 | + |
|---|
| 457 | +static inline void hv_debug_rm_dev_dir(struct hv_device *dev) {}; |
|---|
| 458 | +static inline void hv_debug_rm_all_dir(void) {}; |
|---|
| 459 | +static inline void hv_debug_delay_test(struct vmbus_channel *channel, |
|---|
| 460 | + enum delay delay_type) {}; |
|---|
| 461 | +static inline int hv_debug_init(void) |
|---|
| 462 | +{ |
|---|
| 463 | + return -1; |
|---|
| 464 | +} |
|---|
| 465 | + |
|---|
| 466 | +static inline int hv_debug_add_dev_dir(struct hv_device *dev) |
|---|
| 467 | +{ |
|---|
| 468 | + return -1; |
|---|
| 469 | +} |
|---|
| 470 | + |
|---|
| 471 | +#endif /* CONFIG_HYPERV_TESTING */ |
|---|
| 472 | + |
|---|
| 446 | 473 | #endif /* _HYPERV_VMBUS_H */ |
|---|