1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2009, Microsoft Corporation. 4 * 5 * Authors: 6 * Haiyang Zhang <haiyangz@microsoft.com> 7 * Hank Janssen <hjanssen@microsoft.com> 8 * K. Y. Srinivasan <kys@microsoft.com> 9 */ 10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 11 12 #include <linux/init.h> 13 #include <linux/module.h> 14 #include <linux/device.h> 15 #include <linux/interrupt.h> 16 #include <linux/sysctl.h> 17 #include <linux/slab.h> 18 #include <linux/acpi.h> 19 #include <linux/completion.h> 20 #include <linux/hyperv.h> 21 #include <linux/kernel_stat.h> 22 #include <linux/clockchips.h> 23 #include <linux/cpu.h> 24 #include <linux/sched/isolation.h> 25 #include <linux/sched/task_stack.h> 26 27 #include <linux/delay.h> 28 #include <linux/notifier.h> 29 #include <linux/panic_notifier.h> 30 #include <linux/ptrace.h> 31 #include <linux/screen_info.h> 32 #include <linux/kdebug.h> 33 #include <linux/efi.h> 34 #include <linux/random.h> 35 #include <linux/kernel.h> 36 #include <linux/syscore_ops.h> 37 #include <linux/dma-map-ops.h> 38 #include <linux/pci.h> 39 #include <clocksource/hyperv_timer.h> 40 #include "hyperv_vmbus.h" 41 42 struct vmbus_dynid { 43 struct list_head node; 44 struct hv_vmbus_device_id id; 45 }; 46 47 static struct acpi_device *hv_acpi_dev; 48 49 static struct completion probe_event; 50 51 static int hyperv_cpuhp_online; 52 53 static void *hv_panic_page; 54 55 static long __percpu *vmbus_evt; 56 57 /* Values parsed from ACPI DSDT */ 58 int vmbus_irq; 59 int vmbus_interrupt; 60 61 /* 62 * Boolean to control whether to report panic messages over Hyper-V. 63 * 64 * It can be set via /proc/sys/kernel/hyperv_record_panic_msg 65 */ 66 static int sysctl_record_panic_msg = 1; 67 68 static int hyperv_report_reg(void) 69 { 70 return !sysctl_record_panic_msg || !hv_panic_page; 71 } 72 73 static int hyperv_panic_event(struct notifier_block *nb, unsigned long val, 74 void *args) 75 { 76 struct pt_regs *regs; 77 78 vmbus_initiate_unload(true); 79 80 /* 81 * Hyper-V should be notified only once about a panic. If we will be 82 * doing hv_kmsg_dump() with kmsg data later, don't do the notification 83 * here. 84 */ 85 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE 86 && hyperv_report_reg()) { 87 regs = current_pt_regs(); 88 hyperv_report_panic(regs, val, false); 89 } 90 return NOTIFY_DONE; 91 } 92 93 static int hyperv_die_event(struct notifier_block *nb, unsigned long val, 94 void *args) 95 { 96 struct die_args *die = args; 97 struct pt_regs *regs = die->regs; 98 99 /* Don't notify Hyper-V if the die event is other than oops */ 100 if (val != DIE_OOPS) 101 return NOTIFY_DONE; 102 103 /* 104 * Hyper-V should be notified only once about a panic. If we will be 105 * doing hv_kmsg_dump() with kmsg data later, don't do the notification 106 * here. 107 */ 108 if (hyperv_report_reg()) 109 hyperv_report_panic(regs, val, true); 110 return NOTIFY_DONE; 111 } 112 113 static struct notifier_block hyperv_die_block = { 114 .notifier_call = hyperv_die_event, 115 }; 116 static struct notifier_block hyperv_panic_block = { 117 .notifier_call = hyperv_panic_event, 118 }; 119 120 static const char *fb_mmio_name = "fb_range"; 121 static struct resource *fb_mmio; 122 static struct resource *hyperv_mmio; 123 static DEFINE_MUTEX(hyperv_mmio_lock); 124 125 static int vmbus_exists(void) 126 { 127 if (hv_acpi_dev == NULL) 128 return -ENODEV; 129 130 return 0; 131 } 132 133 static u8 channel_monitor_group(const struct vmbus_channel *channel) 134 { 135 return (u8)channel->offermsg.monitorid / 32; 136 } 137 138 static u8 channel_monitor_offset(const struct vmbus_channel *channel) 139 { 140 return (u8)channel->offermsg.monitorid % 32; 141 } 142 143 static u32 channel_pending(const struct vmbus_channel *channel, 144 const struct hv_monitor_page *monitor_page) 145 { 146 u8 monitor_group = channel_monitor_group(channel); 147 148 return monitor_page->trigger_group[monitor_group].pending; 149 } 150 151 static u32 channel_latency(const struct vmbus_channel *channel, 152 const struct hv_monitor_page *monitor_page) 153 { 154 u8 monitor_group = channel_monitor_group(channel); 155 u8 monitor_offset = channel_monitor_offset(channel); 156 157 return monitor_page->latency[monitor_group][monitor_offset]; 158 } 159 160 static u32 channel_conn_id(struct vmbus_channel *channel, 161 struct hv_monitor_page *monitor_page) 162 { 163 u8 monitor_group = channel_monitor_group(channel); 164 u8 monitor_offset = channel_monitor_offset(channel); 165 166 return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id; 167 } 168 169 static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr, 170 char *buf) 171 { 172 struct hv_device *hv_dev = device_to_hv_device(dev); 173 174 if (!hv_dev->channel) 175 return -ENODEV; 176 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid); 177 } 178 static DEVICE_ATTR_RO(id); 179 180 static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr, 181 char *buf) 182 { 183 struct hv_device *hv_dev = device_to_hv_device(dev); 184 185 if (!hv_dev->channel) 186 return -ENODEV; 187 return sprintf(buf, "%d\n", hv_dev->channel->state); 188 } 189 static DEVICE_ATTR_RO(state); 190 191 static ssize_t monitor_id_show(struct device *dev, 192 struct device_attribute *dev_attr, char *buf) 193 { 194 struct hv_device *hv_dev = device_to_hv_device(dev); 195 196 if (!hv_dev->channel) 197 return -ENODEV; 198 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid); 199 } 200 static DEVICE_ATTR_RO(monitor_id); 201 202 static ssize_t class_id_show(struct device *dev, 203 struct device_attribute *dev_attr, char *buf) 204 { 205 struct hv_device *hv_dev = device_to_hv_device(dev); 206 207 if (!hv_dev->channel) 208 return -ENODEV; 209 return sprintf(buf, "{%pUl}\n", 210 &hv_dev->channel->offermsg.offer.if_type); 211 } 212 static DEVICE_ATTR_RO(class_id); 213 214 static ssize_t device_id_show(struct device *dev, 215 struct device_attribute *dev_attr, char *buf) 216 { 217 struct hv_device *hv_dev = device_to_hv_device(dev); 218 219 if (!hv_dev->channel) 220 return -ENODEV; 221 return sprintf(buf, "{%pUl}\n", 222 &hv_dev->channel->offermsg.offer.if_instance); 223 } 224 static DEVICE_ATTR_RO(device_id); 225 226 static ssize_t modalias_show(struct device *dev, 227 struct device_attribute *dev_attr, char *buf) 228 { 229 struct hv_device *hv_dev = device_to_hv_device(dev); 230 231 return sprintf(buf, "vmbus:%*phN\n", UUID_SIZE, &hv_dev->dev_type); 232 } 233 static DEVICE_ATTR_RO(modalias); 234 235 #ifdef CONFIG_NUMA 236 static ssize_t numa_node_show(struct device *dev, 237 struct device_attribute *attr, char *buf) 238 { 239 struct hv_device *hv_dev = device_to_hv_device(dev); 240 241 if (!hv_dev->channel) 242 return -ENODEV; 243 244 return sprintf(buf, "%d\n", cpu_to_node(hv_dev->channel->target_cpu)); 245 } 246 static DEVICE_ATTR_RO(numa_node); 247 #endif 248 249 static ssize_t server_monitor_pending_show(struct device *dev, 250 struct device_attribute *dev_attr, 251 char *buf) 252 { 253 struct hv_device *hv_dev = device_to_hv_device(dev); 254 255 if (!hv_dev->channel) 256 return -ENODEV; 257 return sprintf(buf, "%d\n", 258 channel_pending(hv_dev->channel, 259 vmbus_connection.monitor_pages[0])); 260 } 261 static DEVICE_ATTR_RO(server_monitor_pending); 262 263 static ssize_t client_monitor_pending_show(struct device *dev, 264 struct device_attribute *dev_attr, 265 char *buf) 266 { 267 struct hv_device *hv_dev = device_to_hv_device(dev); 268 269 if (!hv_dev->channel) 270 return -ENODEV; 271 return sprintf(buf, "%d\n", 272 channel_pending(hv_dev->channel, 273 vmbus_connection.monitor_pages[1])); 274 } 275 static DEVICE_ATTR_RO(client_monitor_pending); 276 277 static ssize_t server_monitor_latency_show(struct device *dev, 278 struct device_attribute *dev_attr, 279 char *buf) 280 { 281 struct hv_device *hv_dev = device_to_hv_device(dev); 282 283 if (!hv_dev->channel) 284 return -ENODEV; 285 return sprintf(buf, "%d\n", 286 channel_latency(hv_dev->channel, 287 vmbus_connection.monitor_pages[0])); 288 } 289 static DEVICE_ATTR_RO(server_monitor_latency); 290 291 static ssize_t client_monitor_latency_show(struct device *dev, 292 struct device_attribute *dev_attr, 293 char *buf) 294 { 295 struct hv_device *hv_dev = device_to_hv_device(dev); 296 297 if (!hv_dev->channel) 298 return -ENODEV; 299 return sprintf(buf, "%d\n", 300 channel_latency(hv_dev->channel, 301 vmbus_connection.monitor_pages[1])); 302 } 303 static DEVICE_ATTR_RO(client_monitor_latency); 304 305 static ssize_t server_monitor_conn_id_show(struct device *dev, 306 struct device_attribute *dev_attr, 307 char *buf) 308 { 309 struct hv_device *hv_dev = device_to_hv_device(dev); 310 311 if (!hv_dev->channel) 312 return -ENODEV; 313 return sprintf(buf, "%d\n", 314 channel_conn_id(hv_dev->channel, 315 vmbus_connection.monitor_pages[0])); 316 } 317 static DEVICE_ATTR_RO(server_monitor_conn_id); 318 319 static ssize_t client_monitor_conn_id_show(struct device *dev, 320 struct device_attribute *dev_attr, 321 char *buf) 322 { 323 struct hv_device *hv_dev = device_to_hv_device(dev); 324 325 if (!hv_dev->channel) 326 return -ENODEV; 327 return sprintf(buf, "%d\n", 328 channel_conn_id(hv_dev->channel, 329 vmbus_connection.monitor_pages[1])); 330 } 331 static DEVICE_ATTR_RO(client_monitor_conn_id); 332 333 static ssize_t out_intr_mask_show(struct device *dev, 334 struct device_attribute *dev_attr, char *buf) 335 { 336 struct hv_device *hv_dev = device_to_hv_device(dev); 337 struct hv_ring_buffer_debug_info outbound; 338 int ret; 339 340 if (!hv_dev->channel) 341 return -ENODEV; 342 343 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, 344 &outbound); 345 if (ret < 0) 346 return ret; 347 348 return sprintf(buf, "%d\n", outbound.current_interrupt_mask); 349 } 350 static DEVICE_ATTR_RO(out_intr_mask); 351 352 static ssize_t out_read_index_show(struct device *dev, 353 struct device_attribute *dev_attr, char *buf) 354 { 355 struct hv_device *hv_dev = device_to_hv_device(dev); 356 struct hv_ring_buffer_debug_info outbound; 357 int ret; 358 359 if (!hv_dev->channel) 360 return -ENODEV; 361 362 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, 363 &outbound); 364 if (ret < 0) 365 return ret; 366 return sprintf(buf, "%d\n", outbound.current_read_index); 367 } 368 static DEVICE_ATTR_RO(out_read_index); 369 370 static ssize_t out_write_index_show(struct device *dev, 371 struct device_attribute *dev_attr, 372 char *buf) 373 { 374 struct hv_device *hv_dev = device_to_hv_device(dev); 375 struct hv_ring_buffer_debug_info outbound; 376 int ret; 377 378 if (!hv_dev->channel) 379 return -ENODEV; 380 381 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, 382 &outbound); 383 if (ret < 0) 384 return ret; 385 return sprintf(buf, "%d\n", outbound.current_write_index); 386 } 387 static DEVICE_ATTR_RO(out_write_index); 388 389 static ssize_t out_read_bytes_avail_show(struct device *dev, 390 struct device_attribute *dev_attr, 391 char *buf) 392 { 393 struct hv_device *hv_dev = device_to_hv_device(dev); 394 struct hv_ring_buffer_debug_info outbound; 395 int ret; 396 397 if (!hv_dev->channel) 398 return -ENODEV; 399 400 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, 401 &outbound); 402 if (ret < 0) 403 return ret; 404 return sprintf(buf, "%d\n", outbound.bytes_avail_toread); 405 } 406 static DEVICE_ATTR_RO(out_read_bytes_avail); 407 408 static ssize_t out_write_bytes_avail_show(struct device *dev, 409 struct device_attribute *dev_attr, 410 char *buf) 411 { 412 struct hv_device *hv_dev = device_to_hv_device(dev); 413 struct hv_ring_buffer_debug_info outbound; 414 int ret; 415 416 if (!hv_dev->channel) 417 return -ENODEV; 418 419 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, 420 &outbound); 421 if (ret < 0) 422 return ret; 423 return sprintf(buf, "%d\n", outbound.bytes_avail_towrite); 424 } 425 static DEVICE_ATTR_RO(out_write_bytes_avail); 426 427 static ssize_t in_intr_mask_show(struct device *dev, 428 struct device_attribute *dev_attr, char *buf) 429 { 430 struct hv_device *hv_dev = device_to_hv_device(dev); 431 struct hv_ring_buffer_debug_info inbound; 432 int ret; 433 434 if (!hv_dev->channel) 435 return -ENODEV; 436 437 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound); 438 if (ret < 0) 439 return ret; 440 441 return sprintf(buf, "%d\n", inbound.current_interrupt_mask); 442 } 443 static DEVICE_ATTR_RO(in_intr_mask); 444 445 static ssize_t in_read_index_show(struct device *dev, 446 struct device_attribute *dev_attr, char *buf) 447 { 448 struct hv_device *hv_dev = device_to_hv_device(dev); 449 struct hv_ring_buffer_debug_info inbound; 450 int ret; 451 452 if (!hv_dev->channel) 453 return -ENODEV; 454 455 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound); 456 if (ret < 0) 457 return ret; 458 459 return sprintf(buf, "%d\n", inbound.current_read_index); 460 } 461 static DEVICE_ATTR_RO(in_read_index); 462 463 static ssize_t in_write_index_show(struct device *dev, 464 struct device_attribute *dev_attr, char *buf) 465 { 466 struct hv_device *hv_dev = device_to_hv_device(dev); 467 struct hv_ring_buffer_debug_info inbound; 468 int ret; 469 470 if (!hv_dev->channel) 471 return -ENODEV; 472 473 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound); 474 if (ret < 0) 475 return ret; 476 477 return sprintf(buf, "%d\n", inbound.current_write_index); 478 } 479 static DEVICE_ATTR_RO(in_write_index); 480 481 static ssize_t in_read_bytes_avail_show(struct device *dev, 482 struct device_attribute *dev_attr, 483 char *buf) 484 { 485 struct hv_device *hv_dev = device_to_hv_device(dev); 486 struct hv_ring_buffer_debug_info inbound; 487 int ret; 488 489 if (!hv_dev->channel) 490 return -ENODEV; 491 492 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound); 493 if (ret < 0) 494 return ret; 495 496 return sprintf(buf, "%d\n", inbound.bytes_avail_toread); 497 } 498 static DEVICE_ATTR_RO(in_read_bytes_avail); 499 500 static ssize_t in_write_bytes_avail_show(struct device *dev, 501 struct device_attribute *dev_attr, 502 char *buf) 503 { 504 struct hv_device *hv_dev = device_to_hv_device(dev); 505 struct hv_ring_buffer_debug_info inbound; 506 int ret; 507 508 if (!hv_dev->channel) 509 return -ENODEV; 510 511 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound); 512 if (ret < 0) 513 return ret; 514 515 return sprintf(buf, "%d\n", inbound.bytes_avail_towrite); 516 } 517 static DEVICE_ATTR_RO(in_write_bytes_avail); 518 519 static ssize_t channel_vp_mapping_show(struct device *dev, 520 struct device_attribute *dev_attr, 521 char *buf) 522 { 523 struct hv_device *hv_dev = device_to_hv_device(dev); 524 struct vmbus_channel *channel = hv_dev->channel, *cur_sc; 525 int buf_size = PAGE_SIZE, n_written, tot_written; 526 struct list_head *cur; 527 528 if (!channel) 529 return -ENODEV; 530 531 mutex_lock(&vmbus_connection.channel_mutex); 532 533 tot_written = snprintf(buf, buf_size, "%u:%u\n", 534 channel->offermsg.child_relid, channel->target_cpu); 535 536 list_for_each(cur, &channel->sc_list) { 537 if (tot_written >= buf_size - 1) 538 break; 539 540 cur_sc = list_entry(cur, struct vmbus_channel, sc_list); 541 n_written = scnprintf(buf + tot_written, 542 buf_size - tot_written, 543 "%u:%u\n", 544 cur_sc->offermsg.child_relid, 545 cur_sc->target_cpu); 546 tot_written += n_written; 547 } 548 549 mutex_unlock(&vmbus_connection.channel_mutex); 550 551 return tot_written; 552 } 553 static DEVICE_ATTR_RO(channel_vp_mapping); 554 555 static ssize_t vendor_show(struct device *dev, 556 struct device_attribute *dev_attr, 557 char *buf) 558 { 559 struct hv_device *hv_dev = device_to_hv_device(dev); 560 561 return sprintf(buf, "0x%x\n", hv_dev->vendor_id); 562 } 563 static DEVICE_ATTR_RO(vendor); 564 565 static ssize_t device_show(struct device *dev, 566 struct device_attribute *dev_attr, 567 char *buf) 568 { 569 struct hv_device *hv_dev = device_to_hv_device(dev); 570 571 return sprintf(buf, "0x%x\n", hv_dev->device_id); 572 } 573 static DEVICE_ATTR_RO(device); 574 575 static ssize_t driver_override_store(struct device *dev, 576 struct device_attribute *attr, 577 const char *buf, size_t count) 578 { 579 struct hv_device *hv_dev = device_to_hv_device(dev); 580 int ret; 581 582 ret = driver_set_override(dev, &hv_dev->driver_override, buf, count); 583 if (ret) 584 return ret; 585 586 return count; 587 } 588 589 static ssize_t driver_override_show(struct device *dev, 590 struct device_attribute *attr, char *buf) 591 { 592 struct hv_device *hv_dev = device_to_hv_device(dev); 593 ssize_t len; 594 595 device_lock(dev); 596 len = snprintf(buf, PAGE_SIZE, "%s\n", hv_dev->driver_override); 597 device_unlock(dev); 598 599 return len; 600 } 601 static DEVICE_ATTR_RW(driver_override); 602 603 /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */ 604 static struct attribute *vmbus_dev_attrs[] = { 605 &dev_attr_id.attr, 606 &dev_attr_state.attr, 607 &dev_attr_monitor_id.attr, 608 &dev_attr_class_id.attr, 609 &dev_attr_device_id.attr, 610 &dev_attr_modalias.attr, 611 #ifdef CONFIG_NUMA 612 &dev_attr_numa_node.attr, 613 #endif 614 &dev_attr_server_monitor_pending.attr, 615 &dev_attr_client_monitor_pending.attr, 616 &dev_attr_server_monitor_latency.attr, 617 &dev_attr_client_monitor_latency.attr, 618 &dev_attr_server_monitor_conn_id.attr, 619 &dev_attr_client_monitor_conn_id.attr, 620 &dev_attr_out_intr_mask.attr, 621 &dev_attr_out_read_index.attr, 622 &dev_attr_out_write_index.attr, 623 &dev_attr_out_read_bytes_avail.attr, 624 &dev_attr_out_write_bytes_avail.attr, 625 &dev_attr_in_intr_mask.attr, 626 &dev_attr_in_read_index.attr, 627 &dev_attr_in_write_index.attr, 628 &dev_attr_in_read_bytes_avail.attr, 629 &dev_attr_in_write_bytes_avail.attr, 630 &dev_attr_channel_vp_mapping.attr, 631 &dev_attr_vendor.attr, 632 &dev_attr_device.attr, 633 &dev_attr_driver_override.attr, 634 NULL, 635 }; 636 637 /* 638 * Device-level attribute_group callback function. Returns the permission for 639 * each attribute, and returns 0 if an attribute is not visible. 640 */ 641 static umode_t vmbus_dev_attr_is_visible(struct kobject *kobj, 642 struct attribute *attr, int idx) 643 { 644 struct device *dev = kobj_to_dev(kobj); 645 const struct hv_device *hv_dev = device_to_hv_device(dev); 646 647 /* Hide the monitor attributes if the monitor mechanism is not used. */ 648 if (!hv_dev->channel->offermsg.monitor_allocated && 649 (attr == &dev_attr_monitor_id.attr || 650 attr == &dev_attr_server_monitor_pending.attr || 651 attr == &dev_attr_client_monitor_pending.attr || 652 attr == &dev_attr_server_monitor_latency.attr || 653 attr == &dev_attr_client_monitor_latency.attr || 654 attr == &dev_attr_server_monitor_conn_id.attr || 655 attr == &dev_attr_client_monitor_conn_id.attr)) 656 return 0; 657 658 return attr->mode; 659 } 660 661 static const struct attribute_group vmbus_dev_group = { 662 .attrs = vmbus_dev_attrs, 663 .is_visible = vmbus_dev_attr_is_visible 664 }; 665 __ATTRIBUTE_GROUPS(vmbus_dev); 666 667 /* Set up the attribute for /sys/bus/vmbus/hibernation */ 668 static ssize_t hibernation_show(struct bus_type *bus, char *buf) 669 { 670 return sprintf(buf, "%d\n", !!hv_is_hibernation_supported()); 671 } 672 673 static BUS_ATTR_RO(hibernation); 674 675 static struct attribute *vmbus_bus_attrs[] = { 676 &bus_attr_hibernation.attr, 677 NULL, 678 }; 679 static const struct attribute_group vmbus_bus_group = { 680 .attrs = vmbus_bus_attrs, 681 }; 682 __ATTRIBUTE_GROUPS(vmbus_bus); 683 684 /* 685 * vmbus_uevent - add uevent for our device 686 * 687 * This routine is invoked when a device is added or removed on the vmbus to 688 * generate a uevent to udev in the userspace. The udev will then look at its 689 * rule and the uevent generated here to load the appropriate driver 690 * 691 * The alias string will be of the form vmbus:guid where guid is the string 692 * representation of the device guid (each byte of the guid will be 693 * represented with two hex characters. 694 */ 695 static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env) 696 { 697 struct hv_device *dev = device_to_hv_device(device); 698 const char *format = "MODALIAS=vmbus:%*phN"; 699 700 return add_uevent_var(env, format, UUID_SIZE, &dev->dev_type); 701 } 702 703 static const struct hv_vmbus_device_id * 704 hv_vmbus_dev_match(const struct hv_vmbus_device_id *id, const guid_t *guid) 705 { 706 if (id == NULL) 707 return NULL; /* empty device table */ 708 709 for (; !guid_is_null(&id->guid); id++) 710 if (guid_equal(&id->guid, guid)) 711 return id; 712 713 return NULL; 714 } 715 716 static const struct hv_vmbus_device_id * 717 hv_vmbus_dynid_match(struct hv_driver *drv, const guid_t *guid) 718 { 719 const struct hv_vmbus_device_id *id = NULL; 720 struct vmbus_dynid *dynid; 721 722 spin_lock(&drv->dynids.lock); 723 list_for_each_entry(dynid, &drv->dynids.list, node) { 724 if (guid_equal(&dynid->id.guid, guid)) { 725 id = &dynid->id; 726 break; 727 } 728 } 729 spin_unlock(&drv->dynids.lock); 730 731 return id; 732 } 733 734 static const struct hv_vmbus_device_id vmbus_device_null; 735 736 /* 737 * Return a matching hv_vmbus_device_id pointer. 738 * If there is no match, return NULL. 739 */ 740 static const struct hv_vmbus_device_id *hv_vmbus_get_id(struct hv_driver *drv, 741 struct hv_device *dev) 742 { 743 const guid_t *guid = &dev->dev_type; 744 const struct hv_vmbus_device_id *id; 745 746 /* When driver_override is set, only bind to the matching driver */ 747 if (dev->driver_override && strcmp(dev->driver_override, drv->name)) 748 return NULL; 749 750 /* Look at the dynamic ids first, before the static ones */ 751 id = hv_vmbus_dynid_match(drv, guid); 752 if (!id) 753 id = hv_vmbus_dev_match(drv->id_table, guid); 754 755 /* driver_override will always match, send a dummy id */ 756 if (!id && dev->driver_override) 757 id = &vmbus_device_null; 758 759 return id; 760 } 761 762 /* vmbus_add_dynid - add a new device ID to this driver and re-probe devices */ 763 static int vmbus_add_dynid(struct hv_driver *drv, guid_t *guid) 764 { 765 struct vmbus_dynid *dynid; 766 767 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL); 768 if (!dynid) 769 return -ENOMEM; 770 771 dynid->id.guid = *guid; 772 773 spin_lock(&drv->dynids.lock); 774 list_add_tail(&dynid->node, &drv->dynids.list); 775 spin_unlock(&drv->dynids.lock); 776 777 return driver_attach(&drv->driver); 778 } 779 780 static void vmbus_free_dynids(struct hv_driver *drv) 781 { 782 struct vmbus_dynid *dynid, *n; 783 784 spin_lock(&drv->dynids.lock); 785 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) { 786 list_del(&dynid->node); 787 kfree(dynid); 788 } 789 spin_unlock(&drv->dynids.lock); 790 } 791 792 /* 793 * store_new_id - sysfs frontend to vmbus_add_dynid() 794 * 795 * Allow GUIDs to be added to an existing driver via sysfs. 796 */ 797 static ssize_t new_id_store(struct device_driver *driver, const char *buf, 798 size_t count) 799 { 800 struct hv_driver *drv = drv_to_hv_drv(driver); 801 guid_t guid; 802 ssize_t retval; 803 804 retval = guid_parse(buf, &guid); 805 if (retval) 806 return retval; 807 808 if (hv_vmbus_dynid_match(drv, &guid)) 809 return -EEXIST; 810 811 retval = vmbus_add_dynid(drv, &guid); 812 if (retval) 813 return retval; 814 return count; 815 } 816 static DRIVER_ATTR_WO(new_id); 817 818 /* 819 * store_remove_id - remove a PCI device ID from this driver 820 * 821 * Removes a dynamic pci device ID to this driver. 822 */ 823 static ssize_t remove_id_store(struct device_driver *driver, const char *buf, 824 size_t count) 825 { 826 struct hv_driver *drv = drv_to_hv_drv(driver); 827 struct vmbus_dynid *dynid, *n; 828 guid_t guid; 829 ssize_t retval; 830 831 retval = guid_parse(buf, &guid); 832 if (retval) 833 return retval; 834 835 retval = -ENODEV; 836 spin_lock(&drv->dynids.lock); 837 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) { 838 struct hv_vmbus_device_id *id = &dynid->id; 839 840 if (guid_equal(&id->guid, &guid)) { 841 list_del(&dynid->node); 842 kfree(dynid); 843 retval = count; 844 break; 845 } 846 } 847 spin_unlock(&drv->dynids.lock); 848 849 return retval; 850 } 851 static DRIVER_ATTR_WO(remove_id); 852 853 static struct attribute *vmbus_drv_attrs[] = { 854 &driver_attr_new_id.attr, 855 &driver_attr_remove_id.attr, 856 NULL, 857 }; 858 ATTRIBUTE_GROUPS(vmbus_drv); 859 860 861 /* 862 * vmbus_match - Attempt to match the specified device to the specified driver 863 */ 864 static int vmbus_match(struct device *device, struct device_driver *driver) 865 { 866 struct hv_driver *drv = drv_to_hv_drv(driver); 867 struct hv_device *hv_dev = device_to_hv_device(device); 868 869 /* The hv_sock driver handles all hv_sock offers. */ 870 if (is_hvsock_channel(hv_dev->channel)) 871 return drv->hvsock; 872 873 if (hv_vmbus_get_id(drv, hv_dev)) 874 return 1; 875 876 return 0; 877 } 878 879 /* 880 * vmbus_probe - Add the new vmbus's child device 881 */ 882 static int vmbus_probe(struct device *child_device) 883 { 884 int ret = 0; 885 struct hv_driver *drv = 886 drv_to_hv_drv(child_device->driver); 887 struct hv_device *dev = device_to_hv_device(child_device); 888 const struct hv_vmbus_device_id *dev_id; 889 890 dev_id = hv_vmbus_get_id(drv, dev); 891 if (drv->probe) { 892 ret = drv->probe(dev, dev_id); 893 if (ret != 0) 894 pr_err("probe failed for device %s (%d)\n", 895 dev_name(child_device), ret); 896 897 } else { 898 pr_err("probe not set for driver %s\n", 899 dev_name(child_device)); 900 ret = -ENODEV; 901 } 902 return ret; 903 } 904 905 /* 906 * vmbus_dma_configure -- Configure DMA coherence for VMbus device 907 */ 908 static int vmbus_dma_configure(struct device *child_device) 909 { 910 /* 911 * On ARM64, propagate the DMA coherence setting from the top level 912 * VMbus ACPI device to the child VMbus device being added here. 913 * On x86/x64 coherence is assumed and these calls have no effect. 914 */ 915 hv_setup_dma_ops(child_device, 916 device_get_dma_attr(&hv_acpi_dev->dev) == DEV_DMA_COHERENT); 917 return 0; 918 } 919 920 /* 921 * vmbus_remove - Remove a vmbus device 922 */ 923 static void vmbus_remove(struct device *child_device) 924 { 925 struct hv_driver *drv; 926 struct hv_device *dev = device_to_hv_device(child_device); 927 928 if (child_device->driver) { 929 drv = drv_to_hv_drv(child_device->driver); 930 if (drv->remove) 931 drv->remove(dev); 932 } 933 } 934 935 /* 936 * vmbus_shutdown - Shutdown a vmbus device 937 */ 938 static void vmbus_shutdown(struct device *child_device) 939 { 940 struct hv_driver *drv; 941 struct hv_device *dev = device_to_hv_device(child_device); 942 943 944 /* The device may not be attached yet */ 945 if (!child_device->driver) 946 return; 947 948 drv = drv_to_hv_drv(child_device->driver); 949 950 if (drv->shutdown) 951 drv->shutdown(dev); 952 } 953 954 #ifdef CONFIG_PM_SLEEP 955 /* 956 * vmbus_suspend - Suspend a vmbus device 957 */ 958 static int vmbus_suspend(struct device *child_device) 959 { 960 struct hv_driver *drv; 961 struct hv_device *dev = device_to_hv_device(child_device); 962 963 /* The device may not be attached yet */ 964 if (!child_device->driver) 965 return 0; 966 967 drv = drv_to_hv_drv(child_device->driver); 968 if (!drv->suspend) 969 return -EOPNOTSUPP; 970 971 return drv->suspend(dev); 972 } 973 974 /* 975 * vmbus_resume - Resume a vmbus device 976 */ 977 static int vmbus_resume(struct device *child_device) 978 { 979 struct hv_driver *drv; 980 struct hv_device *dev = device_to_hv_device(child_device); 981 982 /* The device may not be attached yet */ 983 if (!child_device->driver) 984 return 0; 985 986 drv = drv_to_hv_drv(child_device->driver); 987 if (!drv->resume) 988 return -EOPNOTSUPP; 989 990 return drv->resume(dev); 991 } 992 #else 993 #define vmbus_suspend NULL 994 #define vmbus_resume NULL 995 #endif /* CONFIG_PM_SLEEP */ 996 997 /* 998 * vmbus_device_release - Final callback release of the vmbus child device 999 */ 1000 static void vmbus_device_release(struct device *device) 1001 { 1002 struct hv_device *hv_dev = device_to_hv_device(device); 1003 struct vmbus_channel *channel = hv_dev->channel; 1004 1005 hv_debug_rm_dev_dir(hv_dev); 1006 1007 mutex_lock(&vmbus_connection.channel_mutex); 1008 hv_process_channel_removal(channel); 1009 mutex_unlock(&vmbus_connection.channel_mutex); 1010 kfree(hv_dev); 1011 } 1012 1013 /* 1014 * Note: we must use the "noirq" ops: see the comment before vmbus_bus_pm. 1015 * 1016 * suspend_noirq/resume_noirq are set to NULL to support Suspend-to-Idle: we 1017 * shouldn't suspend the vmbus devices upon Suspend-to-Idle, otherwise there 1018 * is no way to wake up a Generation-2 VM. 1019 * 1020 * The other 4 ops are for hibernation. 1021 */ 1022 1023 static const struct dev_pm_ops vmbus_pm = { 1024 .suspend_noirq = NULL, 1025 .resume_noirq = NULL, 1026 .freeze_noirq = vmbus_suspend, 1027 .thaw_noirq = vmbus_resume, 1028 .poweroff_noirq = vmbus_suspend, 1029 .restore_noirq = vmbus_resume, 1030 }; 1031 1032 /* The one and only one */ 1033 static struct bus_type hv_bus = { 1034 .name = "vmbus", 1035 .match = vmbus_match, 1036 .shutdown = vmbus_shutdown, 1037 .remove = vmbus_remove, 1038 .probe = vmbus_probe, 1039 .uevent = vmbus_uevent, 1040 .dma_configure = vmbus_dma_configure, 1041 .dev_groups = vmbus_dev_groups, 1042 .drv_groups = vmbus_drv_groups, 1043 .bus_groups = vmbus_bus_groups, 1044 .pm = &vmbus_pm, 1045 }; 1046 1047 struct onmessage_work_context { 1048 struct work_struct work; 1049 struct { 1050 struct hv_message_header header; 1051 u8 payload[]; 1052 } msg; 1053 }; 1054 1055 static void vmbus_onmessage_work(struct work_struct *work) 1056 { 1057 struct onmessage_work_context *ctx; 1058 1059 /* Do not process messages if we're in DISCONNECTED state */ 1060 if (vmbus_connection.conn_state == DISCONNECTED) 1061 return; 1062 1063 ctx = container_of(work, struct onmessage_work_context, 1064 work); 1065 vmbus_onmessage((struct vmbus_channel_message_header *) 1066 &ctx->msg.payload); 1067 kfree(ctx); 1068 } 1069 1070 void vmbus_on_msg_dpc(unsigned long data) 1071 { 1072 struct hv_per_cpu_context *hv_cpu = (void *)data; 1073 void *page_addr = hv_cpu->synic_message_page; 1074 struct hv_message msg_copy, *msg = (struct hv_message *)page_addr + 1075 VMBUS_MESSAGE_SINT; 1076 struct vmbus_channel_message_header *hdr; 1077 enum vmbus_channel_message_type msgtype; 1078 const struct vmbus_channel_message_table_entry *entry; 1079 struct onmessage_work_context *ctx; 1080 __u8 payload_size; 1081 u32 message_type; 1082 1083 /* 1084 * 'enum vmbus_channel_message_type' is supposed to always be 'u32' as 1085 * it is being used in 'struct vmbus_channel_message_header' definition 1086 * which is supposed to match hypervisor ABI. 1087 */ 1088 BUILD_BUG_ON(sizeof(enum vmbus_channel_message_type) != sizeof(u32)); 1089 1090 /* 1091 * Since the message is in memory shared with the host, an erroneous or 1092 * malicious Hyper-V could modify the message while vmbus_on_msg_dpc() 1093 * or individual message handlers are executing; to prevent this, copy 1094 * the message into private memory. 1095 */ 1096 memcpy(&msg_copy, msg, sizeof(struct hv_message)); 1097 1098 message_type = msg_copy.header.message_type; 1099 if (message_type == HVMSG_NONE) 1100 /* no msg */ 1101 return; 1102 1103 hdr = (struct vmbus_channel_message_header *)msg_copy.u.payload; 1104 msgtype = hdr->msgtype; 1105 1106 trace_vmbus_on_msg_dpc(hdr); 1107 1108 if (msgtype >= CHANNELMSG_COUNT) { 1109 WARN_ONCE(1, "unknown msgtype=%d\n", msgtype); 1110 goto msg_handled; 1111 } 1112 1113 payload_size = msg_copy.header.payload_size; 1114 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT) { 1115 WARN_ONCE(1, "payload size is too large (%d)\n", payload_size); 1116 goto msg_handled; 1117 } 1118 1119 entry = &channel_message_table[msgtype]; 1120 1121 if (!entry->message_handler) 1122 goto msg_handled; 1123 1124 if (payload_size < entry->min_payload_len) { 1125 WARN_ONCE(1, "message too short: msgtype=%d len=%d\n", msgtype, payload_size); 1126 goto msg_handled; 1127 } 1128 1129 if (entry->handler_type == VMHT_BLOCKING) { 1130 ctx = kmalloc(struct_size(ctx, msg.payload, payload_size), GFP_ATOMIC); 1131 if (ctx == NULL) 1132 return; 1133 1134 INIT_WORK(&ctx->work, vmbus_onmessage_work); 1135 memcpy(&ctx->msg, &msg_copy, sizeof(msg->header) + payload_size); 1136 1137 /* 1138 * The host can generate a rescind message while we 1139 * may still be handling the original offer. We deal with 1140 * this condition by relying on the synchronization provided 1141 * by offer_in_progress and by channel_mutex. See also the 1142 * inline comments in vmbus_onoffer_rescind(). 1143 */ 1144 switch (msgtype) { 1145 case CHANNELMSG_RESCIND_CHANNELOFFER: 1146 /* 1147 * If we are handling the rescind message; 1148 * schedule the work on the global work queue. 1149 * 1150 * The OFFER message and the RESCIND message should 1151 * not be handled by the same serialized work queue, 1152 * because the OFFER handler may call vmbus_open(), 1153 * which tries to open the channel by sending an 1154 * OPEN_CHANNEL message to the host and waits for 1155 * the host's response; however, if the host has 1156 * rescinded the channel before it receives the 1157 * OPEN_CHANNEL message, the host just silently 1158 * ignores the OPEN_CHANNEL message; as a result, 1159 * the guest's OFFER handler hangs for ever, if we 1160 * handle the RESCIND message in the same serialized 1161 * work queue: the RESCIND handler can not start to 1162 * run before the OFFER handler finishes. 1163 */ 1164 if (vmbus_connection.ignore_any_offer_msg) 1165 break; 1166 queue_work(vmbus_connection.rescind_work_queue, &ctx->work); 1167 break; 1168 1169 case CHANNELMSG_OFFERCHANNEL: 1170 /* 1171 * The host sends the offer message of a given channel 1172 * before sending the rescind message of the same 1173 * channel. These messages are sent to the guest's 1174 * connect CPU; the guest then starts processing them 1175 * in the tasklet handler on this CPU: 1176 * 1177 * VMBUS_CONNECT_CPU 1178 * 1179 * [vmbus_on_msg_dpc()] 1180 * atomic_inc() // CHANNELMSG_OFFERCHANNEL 1181 * queue_work() 1182 * ... 1183 * [vmbus_on_msg_dpc()] 1184 * schedule_work() // CHANNELMSG_RESCIND_CHANNELOFFER 1185 * 1186 * We rely on the memory-ordering properties of the 1187 * queue_work() and schedule_work() primitives, which 1188 * guarantee that the atomic increment will be visible 1189 * to the CPUs which will execute the offer & rescind 1190 * works by the time these works will start execution. 1191 */ 1192 if (vmbus_connection.ignore_any_offer_msg) 1193 break; 1194 atomic_inc(&vmbus_connection.offer_in_progress); 1195 fallthrough; 1196 1197 default: 1198 queue_work(vmbus_connection.work_queue, &ctx->work); 1199 } 1200 } else 1201 entry->message_handler(hdr); 1202 1203 msg_handled: 1204 vmbus_signal_eom(msg, message_type); 1205 } 1206 1207 #ifdef CONFIG_PM_SLEEP 1208 /* 1209 * Fake RESCIND_CHANNEL messages to clean up hv_sock channels by force for 1210 * hibernation, because hv_sock connections can not persist across hibernation. 1211 */ 1212 static void vmbus_force_channel_rescinded(struct vmbus_channel *channel) 1213 { 1214 struct onmessage_work_context *ctx; 1215 struct vmbus_channel_rescind_offer *rescind; 1216 1217 WARN_ON(!is_hvsock_channel(channel)); 1218 1219 /* 1220 * Allocation size is small and the allocation should really not fail, 1221 * otherwise the state of the hv_sock connections ends up in limbo. 1222 */ 1223 ctx = kzalloc(sizeof(*ctx) + sizeof(*rescind), 1224 GFP_KERNEL | __GFP_NOFAIL); 1225 1226 /* 1227 * So far, these are not really used by Linux. Just set them to the 1228 * reasonable values conforming to the definitions of the fields. 1229 */ 1230 ctx->msg.header.message_type = 1; 1231 ctx->msg.header.payload_size = sizeof(*rescind); 1232 1233 /* These values are actually used by Linux. */ 1234 rescind = (struct vmbus_channel_rescind_offer *)ctx->msg.payload; 1235 rescind->header.msgtype = CHANNELMSG_RESCIND_CHANNELOFFER; 1236 rescind->child_relid = channel->offermsg.child_relid; 1237 1238 INIT_WORK(&ctx->work, vmbus_onmessage_work); 1239 1240 queue_work(vmbus_connection.work_queue, &ctx->work); 1241 } 1242 #endif /* CONFIG_PM_SLEEP */ 1243 1244 /* 1245 * Schedule all channels with events pending 1246 */ 1247 static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu) 1248 { 1249 unsigned long *recv_int_page; 1250 u32 maxbits, relid; 1251 1252 /* 1253 * The event page can be directly checked to get the id of 1254 * the channel that has the interrupt pending. 1255 */ 1256 void *page_addr = hv_cpu->synic_event_page; 1257 union hv_synic_event_flags *event 1258 = (union hv_synic_event_flags *)page_addr + 1259 VMBUS_MESSAGE_SINT; 1260 1261 maxbits = HV_EVENT_FLAGS_COUNT; 1262 recv_int_page = event->flags; 1263 1264 if (unlikely(!recv_int_page)) 1265 return; 1266 1267 for_each_set_bit(relid, recv_int_page, maxbits) { 1268 void (*callback_fn)(void *context); 1269 struct vmbus_channel *channel; 1270 1271 if (!sync_test_and_clear_bit(relid, recv_int_page)) 1272 continue; 1273 1274 /* Special case - vmbus channel protocol msg */ 1275 if (relid == 0) 1276 continue; 1277 1278 /* 1279 * Pairs with the kfree_rcu() in vmbus_chan_release(). 1280 * Guarantees that the channel data structure doesn't 1281 * get freed while the channel pointer below is being 1282 * dereferenced. 1283 */ 1284 rcu_read_lock(); 1285 1286 /* Find channel based on relid */ 1287 channel = relid2channel(relid); 1288 if (channel == NULL) 1289 goto sched_unlock_rcu; 1290 1291 if (channel->rescind) 1292 goto sched_unlock_rcu; 1293 1294 /* 1295 * Make sure that the ring buffer data structure doesn't get 1296 * freed while we dereference the ring buffer pointer. Test 1297 * for the channel's onchannel_callback being NULL within a 1298 * sched_lock critical section. See also the inline comments 1299 * in vmbus_reset_channel_cb(). 1300 */ 1301 spin_lock(&channel->sched_lock); 1302 1303 callback_fn = channel->onchannel_callback; 1304 if (unlikely(callback_fn == NULL)) 1305 goto sched_unlock; 1306 1307 trace_vmbus_chan_sched(channel); 1308 1309 ++channel->interrupts; 1310 1311 switch (channel->callback_mode) { 1312 case HV_CALL_ISR: 1313 (*callback_fn)(channel->channel_callback_context); 1314 break; 1315 1316 case HV_CALL_BATCHED: 1317 hv_begin_read(&channel->inbound); 1318 fallthrough; 1319 case HV_CALL_DIRECT: 1320 tasklet_schedule(&channel->callback_event); 1321 } 1322 1323 sched_unlock: 1324 spin_unlock(&channel->sched_lock); 1325 sched_unlock_rcu: 1326 rcu_read_unlock(); 1327 } 1328 } 1329 1330 static void vmbus_isr(void) 1331 { 1332 struct hv_per_cpu_context *hv_cpu 1333 = this_cpu_ptr(hv_context.cpu_context); 1334 void *page_addr; 1335 struct hv_message *msg; 1336 1337 vmbus_chan_sched(hv_cpu); 1338 1339 page_addr = hv_cpu->synic_message_page; 1340 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT; 1341 1342 /* Check if there are actual msgs to be processed */ 1343 if (msg->header.message_type != HVMSG_NONE) { 1344 if (msg->header.message_type == HVMSG_TIMER_EXPIRED) { 1345 hv_stimer0_isr(); 1346 vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED); 1347 } else 1348 tasklet_schedule(&hv_cpu->msg_dpc); 1349 } 1350 1351 add_interrupt_randomness(vmbus_interrupt); 1352 } 1353 1354 static irqreturn_t vmbus_percpu_isr(int irq, void *dev_id) 1355 { 1356 vmbus_isr(); 1357 return IRQ_HANDLED; 1358 } 1359 1360 /* 1361 * Callback from kmsg_dump. Grab as much as possible from the end of the kmsg 1362 * buffer and call into Hyper-V to transfer the data. 1363 */ 1364 static void hv_kmsg_dump(struct kmsg_dumper *dumper, 1365 enum kmsg_dump_reason reason) 1366 { 1367 struct kmsg_dump_iter iter; 1368 size_t bytes_written; 1369 1370 /* We are only interested in panics. */ 1371 if ((reason != KMSG_DUMP_PANIC) || (!sysctl_record_panic_msg)) 1372 return; 1373 1374 /* 1375 * Write dump contents to the page. No need to synchronize; panic should 1376 * be single-threaded. 1377 */ 1378 kmsg_dump_rewind(&iter); 1379 kmsg_dump_get_buffer(&iter, false, hv_panic_page, HV_HYP_PAGE_SIZE, 1380 &bytes_written); 1381 if (!bytes_written) 1382 return; 1383 /* 1384 * P3 to contain the physical address of the panic page & P4 to 1385 * contain the size of the panic data in that page. Rest of the 1386 * registers are no-op when the NOTIFY_MSG flag is set. 1387 */ 1388 hv_set_register(HV_REGISTER_CRASH_P0, 0); 1389 hv_set_register(HV_REGISTER_CRASH_P1, 0); 1390 hv_set_register(HV_REGISTER_CRASH_P2, 0); 1391 hv_set_register(HV_REGISTER_CRASH_P3, virt_to_phys(hv_panic_page)); 1392 hv_set_register(HV_REGISTER_CRASH_P4, bytes_written); 1393 1394 /* 1395 * Let Hyper-V know there is crash data available along with 1396 * the panic message. 1397 */ 1398 hv_set_register(HV_REGISTER_CRASH_CTL, 1399 (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG)); 1400 } 1401 1402 static struct kmsg_dumper hv_kmsg_dumper = { 1403 .dump = hv_kmsg_dump, 1404 }; 1405 1406 static void hv_kmsg_dump_register(void) 1407 { 1408 int ret; 1409 1410 hv_panic_page = hv_alloc_hyperv_zeroed_page(); 1411 if (!hv_panic_page) { 1412 pr_err("Hyper-V: panic message page memory allocation failed\n"); 1413 return; 1414 } 1415 1416 ret = kmsg_dump_register(&hv_kmsg_dumper); 1417 if (ret) { 1418 pr_err("Hyper-V: kmsg dump register error 0x%x\n", ret); 1419 hv_free_hyperv_page((unsigned long)hv_panic_page); 1420 hv_panic_page = NULL; 1421 } 1422 } 1423 1424 static struct ctl_table_header *hv_ctl_table_hdr; 1425 1426 /* 1427 * sysctl option to allow the user to control whether kmsg data should be 1428 * reported to Hyper-V on panic. 1429 */ 1430 static struct ctl_table hv_ctl_table[] = { 1431 { 1432 .procname = "hyperv_record_panic_msg", 1433 .data = &sysctl_record_panic_msg, 1434 .maxlen = sizeof(int), 1435 .mode = 0644, 1436 .proc_handler = proc_dointvec_minmax, 1437 .extra1 = SYSCTL_ZERO, 1438 .extra2 = SYSCTL_ONE 1439 }, 1440 {} 1441 }; 1442 1443 static struct ctl_table hv_root_table[] = { 1444 { 1445 .procname = "kernel", 1446 .mode = 0555, 1447 .child = hv_ctl_table 1448 }, 1449 {} 1450 }; 1451 1452 /* 1453 * vmbus_bus_init -Main vmbus driver initialization routine. 1454 * 1455 * Here, we 1456 * - initialize the vmbus driver context 1457 * - invoke the vmbus hv main init routine 1458 * - retrieve the channel offers 1459 */ 1460 static int vmbus_bus_init(void) 1461 { 1462 int ret; 1463 1464 ret = hv_init(); 1465 if (ret != 0) { 1466 pr_err("Unable to initialize the hypervisor - 0x%x\n", ret); 1467 return ret; 1468 } 1469 1470 ret = bus_register(&hv_bus); 1471 if (ret) 1472 return ret; 1473 1474 /* 1475 * VMbus interrupts are best modeled as per-cpu interrupts. If 1476 * on an architecture with support for per-cpu IRQs (e.g. ARM64), 1477 * allocate a per-cpu IRQ using standard Linux kernel functionality. 1478 * If not on such an architecture (e.g., x86/x64), then rely on 1479 * code in the arch-specific portion of the code tree to connect 1480 * the VMbus interrupt handler. 1481 */ 1482 1483 if (vmbus_irq == -1) { 1484 hv_setup_vmbus_handler(vmbus_isr); 1485 } else { 1486 vmbus_evt = alloc_percpu(long); 1487 ret = request_percpu_irq(vmbus_irq, vmbus_percpu_isr, 1488 "Hyper-V VMbus", vmbus_evt); 1489 if (ret) { 1490 pr_err("Can't request Hyper-V VMbus IRQ %d, Err %d", 1491 vmbus_irq, ret); 1492 free_percpu(vmbus_evt); 1493 goto err_setup; 1494 } 1495 } 1496 1497 ret = hv_synic_alloc(); 1498 if (ret) 1499 goto err_alloc; 1500 1501 /* 1502 * Initialize the per-cpu interrupt state and stimer state. 1503 * Then connect to the host. 1504 */ 1505 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online", 1506 hv_synic_init, hv_synic_cleanup); 1507 if (ret < 0) 1508 goto err_cpuhp; 1509 hyperv_cpuhp_online = ret; 1510 1511 ret = vmbus_connect(); 1512 if (ret) 1513 goto err_connect; 1514 1515 if (hv_is_isolation_supported()) 1516 sysctl_record_panic_msg = 0; 1517 1518 /* 1519 * Only register if the crash MSRs are available 1520 */ 1521 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) { 1522 u64 hyperv_crash_ctl; 1523 /* 1524 * Panic message recording (sysctl_record_panic_msg) 1525 * is enabled by default in non-isolated guests and 1526 * disabled by default in isolated guests; the panic 1527 * message recording won't be available in isolated 1528 * guests should the following registration fail. 1529 */ 1530 hv_ctl_table_hdr = register_sysctl_table(hv_root_table); 1531 if (!hv_ctl_table_hdr) 1532 pr_err("Hyper-V: sysctl table register error"); 1533 1534 /* 1535 * Register for panic kmsg callback only if the right 1536 * capability is supported by the hypervisor. 1537 */ 1538 hyperv_crash_ctl = hv_get_register(HV_REGISTER_CRASH_CTL); 1539 if (hyperv_crash_ctl & HV_CRASH_CTL_CRASH_NOTIFY_MSG) 1540 hv_kmsg_dump_register(); 1541 1542 register_die_notifier(&hyperv_die_block); 1543 } 1544 1545 /* 1546 * Always register the panic notifier because we need to unload 1547 * the VMbus channel connection to prevent any VMbus 1548 * activity after the VM panics. 1549 */ 1550 atomic_notifier_chain_register(&panic_notifier_list, 1551 &hyperv_panic_block); 1552 1553 vmbus_request_offers(); 1554 1555 return 0; 1556 1557 err_connect: 1558 cpuhp_remove_state(hyperv_cpuhp_online); 1559 err_cpuhp: 1560 hv_synic_free(); 1561 err_alloc: 1562 if (vmbus_irq == -1) { 1563 hv_remove_vmbus_handler(); 1564 } else { 1565 free_percpu_irq(vmbus_irq, vmbus_evt); 1566 free_percpu(vmbus_evt); 1567 } 1568 err_setup: 1569 bus_unregister(&hv_bus); 1570 unregister_sysctl_table(hv_ctl_table_hdr); 1571 hv_ctl_table_hdr = NULL; 1572 return ret; 1573 } 1574 1575 /** 1576 * __vmbus_child_driver_register() - Register a vmbus's driver 1577 * @hv_driver: Pointer to driver structure you want to register 1578 * @owner: owner module of the drv 1579 * @mod_name: module name string 1580 * 1581 * Registers the given driver with Linux through the 'driver_register()' call 1582 * and sets up the hyper-v vmbus handling for this driver. 1583 * It will return the state of the 'driver_register()' call. 1584 * 1585 */ 1586 int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name) 1587 { 1588 int ret; 1589 1590 pr_info("registering driver %s\n", hv_driver->name); 1591 1592 ret = vmbus_exists(); 1593 if (ret < 0) 1594 return ret; 1595 1596 hv_driver->driver.name = hv_driver->name; 1597 hv_driver->driver.owner = owner; 1598 hv_driver->driver.mod_name = mod_name; 1599 hv_driver->driver.bus = &hv_bus; 1600 1601 spin_lock_init(&hv_driver->dynids.lock); 1602 INIT_LIST_HEAD(&hv_driver->dynids.list); 1603 1604 ret = driver_register(&hv_driver->driver); 1605 1606 return ret; 1607 } 1608 EXPORT_SYMBOL_GPL(__vmbus_driver_register); 1609 1610 /** 1611 * vmbus_driver_unregister() - Unregister a vmbus's driver 1612 * @hv_driver: Pointer to driver structure you want to 1613 * un-register 1614 * 1615 * Un-register the given driver that was previous registered with a call to 1616 * vmbus_driver_register() 1617 */ 1618 void vmbus_driver_unregister(struct hv_driver *hv_driver) 1619 { 1620 pr_info("unregistering driver %s\n", hv_driver->name); 1621 1622 if (!vmbus_exists()) { 1623 driver_unregister(&hv_driver->driver); 1624 vmbus_free_dynids(hv_driver); 1625 } 1626 } 1627 EXPORT_SYMBOL_GPL(vmbus_driver_unregister); 1628 1629 1630 /* 1631 * Called when last reference to channel is gone. 1632 */ 1633 static void vmbus_chan_release(struct kobject *kobj) 1634 { 1635 struct vmbus_channel *channel 1636 = container_of(kobj, struct vmbus_channel, kobj); 1637 1638 kfree_rcu(channel, rcu); 1639 } 1640 1641 struct vmbus_chan_attribute { 1642 struct attribute attr; 1643 ssize_t (*show)(struct vmbus_channel *chan, char *buf); 1644 ssize_t (*store)(struct vmbus_channel *chan, 1645 const char *buf, size_t count); 1646 }; 1647 #define VMBUS_CHAN_ATTR(_name, _mode, _show, _store) \ 1648 struct vmbus_chan_attribute chan_attr_##_name \ 1649 = __ATTR(_name, _mode, _show, _store) 1650 #define VMBUS_CHAN_ATTR_RW(_name) \ 1651 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RW(_name) 1652 #define VMBUS_CHAN_ATTR_RO(_name) \ 1653 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RO(_name) 1654 #define VMBUS_CHAN_ATTR_WO(_name) \ 1655 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_WO(_name) 1656 1657 static ssize_t vmbus_chan_attr_show(struct kobject *kobj, 1658 struct attribute *attr, char *buf) 1659 { 1660 const struct vmbus_chan_attribute *attribute 1661 = container_of(attr, struct vmbus_chan_attribute, attr); 1662 struct vmbus_channel *chan 1663 = container_of(kobj, struct vmbus_channel, kobj); 1664 1665 if (!attribute->show) 1666 return -EIO; 1667 1668 return attribute->show(chan, buf); 1669 } 1670 1671 static ssize_t vmbus_chan_attr_store(struct kobject *kobj, 1672 struct attribute *attr, const char *buf, 1673 size_t count) 1674 { 1675 const struct vmbus_chan_attribute *attribute 1676 = container_of(attr, struct vmbus_chan_attribute, attr); 1677 struct vmbus_channel *chan 1678 = container_of(kobj, struct vmbus_channel, kobj); 1679 1680 if (!attribute->store) 1681 return -EIO; 1682 1683 return attribute->store(chan, buf, count); 1684 } 1685 1686 static const struct sysfs_ops vmbus_chan_sysfs_ops = { 1687 .show = vmbus_chan_attr_show, 1688 .store = vmbus_chan_attr_store, 1689 }; 1690 1691 static ssize_t out_mask_show(struct vmbus_channel *channel, char *buf) 1692 { 1693 struct hv_ring_buffer_info *rbi = &channel->outbound; 1694 ssize_t ret; 1695 1696 mutex_lock(&rbi->ring_buffer_mutex); 1697 if (!rbi->ring_buffer) { 1698 mutex_unlock(&rbi->ring_buffer_mutex); 1699 return -EINVAL; 1700 } 1701 1702 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask); 1703 mutex_unlock(&rbi->ring_buffer_mutex); 1704 return ret; 1705 } 1706 static VMBUS_CHAN_ATTR_RO(out_mask); 1707 1708 static ssize_t in_mask_show(struct vmbus_channel *channel, char *buf) 1709 { 1710 struct hv_ring_buffer_info *rbi = &channel->inbound; 1711 ssize_t ret; 1712 1713 mutex_lock(&rbi->ring_buffer_mutex); 1714 if (!rbi->ring_buffer) { 1715 mutex_unlock(&rbi->ring_buffer_mutex); 1716 return -EINVAL; 1717 } 1718 1719 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask); 1720 mutex_unlock(&rbi->ring_buffer_mutex); 1721 return ret; 1722 } 1723 static VMBUS_CHAN_ATTR_RO(in_mask); 1724 1725 static ssize_t read_avail_show(struct vmbus_channel *channel, char *buf) 1726 { 1727 struct hv_ring_buffer_info *rbi = &channel->inbound; 1728 ssize_t ret; 1729 1730 mutex_lock(&rbi->ring_buffer_mutex); 1731 if (!rbi->ring_buffer) { 1732 mutex_unlock(&rbi->ring_buffer_mutex); 1733 return -EINVAL; 1734 } 1735 1736 ret = sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi)); 1737 mutex_unlock(&rbi->ring_buffer_mutex); 1738 return ret; 1739 } 1740 static VMBUS_CHAN_ATTR_RO(read_avail); 1741 1742 static ssize_t write_avail_show(struct vmbus_channel *channel, char *buf) 1743 { 1744 struct hv_ring_buffer_info *rbi = &channel->outbound; 1745 ssize_t ret; 1746 1747 mutex_lock(&rbi->ring_buffer_mutex); 1748 if (!rbi->ring_buffer) { 1749 mutex_unlock(&rbi->ring_buffer_mutex); 1750 return -EINVAL; 1751 } 1752 1753 ret = sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi)); 1754 mutex_unlock(&rbi->ring_buffer_mutex); 1755 return ret; 1756 } 1757 static VMBUS_CHAN_ATTR_RO(write_avail); 1758 1759 static ssize_t target_cpu_show(struct vmbus_channel *channel, char *buf) 1760 { 1761 return sprintf(buf, "%u\n", channel->target_cpu); 1762 } 1763 static ssize_t target_cpu_store(struct vmbus_channel *channel, 1764 const char *buf, size_t count) 1765 { 1766 u32 target_cpu, origin_cpu; 1767 ssize_t ret = count; 1768 1769 if (vmbus_proto_version < VERSION_WIN10_V4_1) 1770 return -EIO; 1771 1772 if (sscanf(buf, "%uu", &target_cpu) != 1) 1773 return -EIO; 1774 1775 /* Validate target_cpu for the cpumask_test_cpu() operation below. */ 1776 if (target_cpu >= nr_cpumask_bits) 1777 return -EINVAL; 1778 1779 if (!cpumask_test_cpu(target_cpu, housekeeping_cpumask(HK_TYPE_MANAGED_IRQ))) 1780 return -EINVAL; 1781 1782 /* No CPUs should come up or down during this. */ 1783 cpus_read_lock(); 1784 1785 if (!cpu_online(target_cpu)) { 1786 cpus_read_unlock(); 1787 return -EINVAL; 1788 } 1789 1790 /* 1791 * Synchronizes target_cpu_store() and channel closure: 1792 * 1793 * { Initially: state = CHANNEL_OPENED } 1794 * 1795 * CPU1 CPU2 1796 * 1797 * [target_cpu_store()] [vmbus_disconnect_ring()] 1798 * 1799 * LOCK channel_mutex LOCK channel_mutex 1800 * LOAD r1 = state LOAD r2 = state 1801 * IF (r1 == CHANNEL_OPENED) IF (r2 == CHANNEL_OPENED) 1802 * SEND MODIFYCHANNEL STORE state = CHANNEL_OPEN 1803 * [...] SEND CLOSECHANNEL 1804 * UNLOCK channel_mutex UNLOCK channel_mutex 1805 * 1806 * Forbids: r1 == r2 == CHANNEL_OPENED (i.e., CPU1's LOCK precedes 1807 * CPU2's LOCK) && CPU2's SEND precedes CPU1's SEND 1808 * 1809 * Note. The host processes the channel messages "sequentially", in 1810 * the order in which they are received on a per-partition basis. 1811 */ 1812 mutex_lock(&vmbus_connection.channel_mutex); 1813 1814 /* 1815 * Hyper-V will ignore MODIFYCHANNEL messages for "non-open" channels; 1816 * avoid sending the message and fail here for such channels. 1817 */ 1818 if (channel->state != CHANNEL_OPENED_STATE) { 1819 ret = -EIO; 1820 goto cpu_store_unlock; 1821 } 1822 1823 origin_cpu = channel->target_cpu; 1824 if (target_cpu == origin_cpu) 1825 goto cpu_store_unlock; 1826 1827 if (vmbus_send_modifychannel(channel, 1828 hv_cpu_number_to_vp_number(target_cpu))) { 1829 ret = -EIO; 1830 goto cpu_store_unlock; 1831 } 1832 1833 /* 1834 * For version before VERSION_WIN10_V5_3, the following warning holds: 1835 * 1836 * Warning. At this point, there is *no* guarantee that the host will 1837 * have successfully processed the vmbus_send_modifychannel() request. 1838 * See the header comment of vmbus_send_modifychannel() for more info. 1839 * 1840 * Lags in the processing of the above vmbus_send_modifychannel() can 1841 * result in missed interrupts if the "old" target CPU is taken offline 1842 * before Hyper-V starts sending interrupts to the "new" target CPU. 1843 * But apart from this offlining scenario, the code tolerates such 1844 * lags. It will function correctly even if a channel interrupt comes 1845 * in on a CPU that is different from the channel target_cpu value. 1846 */ 1847 1848 channel->target_cpu = target_cpu; 1849 1850 /* See init_vp_index(). */ 1851 if (hv_is_perf_channel(channel)) 1852 hv_update_allocated_cpus(origin_cpu, target_cpu); 1853 1854 /* Currently set only for storvsc channels. */ 1855 if (channel->change_target_cpu_callback) { 1856 (*channel->change_target_cpu_callback)(channel, 1857 origin_cpu, target_cpu); 1858 } 1859 1860 cpu_store_unlock: 1861 mutex_unlock(&vmbus_connection.channel_mutex); 1862 cpus_read_unlock(); 1863 return ret; 1864 } 1865 static VMBUS_CHAN_ATTR(cpu, 0644, target_cpu_show, target_cpu_store); 1866 1867 static ssize_t channel_pending_show(struct vmbus_channel *channel, 1868 char *buf) 1869 { 1870 return sprintf(buf, "%d\n", 1871 channel_pending(channel, 1872 vmbus_connection.monitor_pages[1])); 1873 } 1874 static VMBUS_CHAN_ATTR(pending, 0444, channel_pending_show, NULL); 1875 1876 static ssize_t channel_latency_show(struct vmbus_channel *channel, 1877 char *buf) 1878 { 1879 return sprintf(buf, "%d\n", 1880 channel_latency(channel, 1881 vmbus_connection.monitor_pages[1])); 1882 } 1883 static VMBUS_CHAN_ATTR(latency, 0444, channel_latency_show, NULL); 1884 1885 static ssize_t channel_interrupts_show(struct vmbus_channel *channel, char *buf) 1886 { 1887 return sprintf(buf, "%llu\n", channel->interrupts); 1888 } 1889 static VMBUS_CHAN_ATTR(interrupts, 0444, channel_interrupts_show, NULL); 1890 1891 static ssize_t channel_events_show(struct vmbus_channel *channel, char *buf) 1892 { 1893 return sprintf(buf, "%llu\n", channel->sig_events); 1894 } 1895 static VMBUS_CHAN_ATTR(events, 0444, channel_events_show, NULL); 1896 1897 static ssize_t channel_intr_in_full_show(struct vmbus_channel *channel, 1898 char *buf) 1899 { 1900 return sprintf(buf, "%llu\n", 1901 (unsigned long long)channel->intr_in_full); 1902 } 1903 static VMBUS_CHAN_ATTR(intr_in_full, 0444, channel_intr_in_full_show, NULL); 1904 1905 static ssize_t channel_intr_out_empty_show(struct vmbus_channel *channel, 1906 char *buf) 1907 { 1908 return sprintf(buf, "%llu\n", 1909 (unsigned long long)channel->intr_out_empty); 1910 } 1911 static VMBUS_CHAN_ATTR(intr_out_empty, 0444, channel_intr_out_empty_show, NULL); 1912 1913 static ssize_t channel_out_full_first_show(struct vmbus_channel *channel, 1914 char *buf) 1915 { 1916 return sprintf(buf, "%llu\n", 1917 (unsigned long long)channel->out_full_first); 1918 } 1919 static VMBUS_CHAN_ATTR(out_full_first, 0444, channel_out_full_first_show, NULL); 1920 1921 static ssize_t channel_out_full_total_show(struct vmbus_channel *channel, 1922 char *buf) 1923 { 1924 return sprintf(buf, "%llu\n", 1925 (unsigned long long)channel->out_full_total); 1926 } 1927 static VMBUS_CHAN_ATTR(out_full_total, 0444, channel_out_full_total_show, NULL); 1928 1929 static ssize_t subchannel_monitor_id_show(struct vmbus_channel *channel, 1930 char *buf) 1931 { 1932 return sprintf(buf, "%u\n", channel->offermsg.monitorid); 1933 } 1934 static VMBUS_CHAN_ATTR(monitor_id, 0444, subchannel_monitor_id_show, NULL); 1935 1936 static ssize_t subchannel_id_show(struct vmbus_channel *channel, 1937 char *buf) 1938 { 1939 return sprintf(buf, "%u\n", 1940 channel->offermsg.offer.sub_channel_index); 1941 } 1942 static VMBUS_CHAN_ATTR_RO(subchannel_id); 1943 1944 static struct attribute *vmbus_chan_attrs[] = { 1945 &chan_attr_out_mask.attr, 1946 &chan_attr_in_mask.attr, 1947 &chan_attr_read_avail.attr, 1948 &chan_attr_write_avail.attr, 1949 &chan_attr_cpu.attr, 1950 &chan_attr_pending.attr, 1951 &chan_attr_latency.attr, 1952 &chan_attr_interrupts.attr, 1953 &chan_attr_events.attr, 1954 &chan_attr_intr_in_full.attr, 1955 &chan_attr_intr_out_empty.attr, 1956 &chan_attr_out_full_first.attr, 1957 &chan_attr_out_full_total.attr, 1958 &chan_attr_monitor_id.attr, 1959 &chan_attr_subchannel_id.attr, 1960 NULL 1961 }; 1962 1963 /* 1964 * Channel-level attribute_group callback function. Returns the permission for 1965 * each attribute, and returns 0 if an attribute is not visible. 1966 */ 1967 static umode_t vmbus_chan_attr_is_visible(struct kobject *kobj, 1968 struct attribute *attr, int idx) 1969 { 1970 const struct vmbus_channel *channel = 1971 container_of(kobj, struct vmbus_channel, kobj); 1972 1973 /* Hide the monitor attributes if the monitor mechanism is not used. */ 1974 if (!channel->offermsg.monitor_allocated && 1975 (attr == &chan_attr_pending.attr || 1976 attr == &chan_attr_latency.attr || 1977 attr == &chan_attr_monitor_id.attr)) 1978 return 0; 1979 1980 return attr->mode; 1981 } 1982 1983 static struct attribute_group vmbus_chan_group = { 1984 .attrs = vmbus_chan_attrs, 1985 .is_visible = vmbus_chan_attr_is_visible 1986 }; 1987 1988 static struct kobj_type vmbus_chan_ktype = { 1989 .sysfs_ops = &vmbus_chan_sysfs_ops, 1990 .release = vmbus_chan_release, 1991 }; 1992 1993 /* 1994 * vmbus_add_channel_kobj - setup a sub-directory under device/channels 1995 */ 1996 int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel) 1997 { 1998 const struct device *device = &dev->device; 1999 struct kobject *kobj = &channel->kobj; 2000 u32 relid = channel->offermsg.child_relid; 2001 int ret; 2002 2003 kobj->kset = dev->channels_kset; 2004 ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL, 2005 "%u", relid); 2006 if (ret) { 2007 kobject_put(kobj); 2008 return ret; 2009 } 2010 2011 ret = sysfs_create_group(kobj, &vmbus_chan_group); 2012 2013 if (ret) { 2014 /* 2015 * The calling functions' error handling paths will cleanup the 2016 * empty channel directory. 2017 */ 2018 kobject_put(kobj); 2019 dev_err(device, "Unable to set up channel sysfs files\n"); 2020 return ret; 2021 } 2022 2023 kobject_uevent(kobj, KOBJ_ADD); 2024 2025 return 0; 2026 } 2027 2028 /* 2029 * vmbus_remove_channel_attr_group - remove the channel's attribute group 2030 */ 2031 void vmbus_remove_channel_attr_group(struct vmbus_channel *channel) 2032 { 2033 sysfs_remove_group(&channel->kobj, &vmbus_chan_group); 2034 } 2035 2036 /* 2037 * vmbus_device_create - Creates and registers a new child device 2038 * on the vmbus. 2039 */ 2040 struct hv_device *vmbus_device_create(const guid_t *type, 2041 const guid_t *instance, 2042 struct vmbus_channel *channel) 2043 { 2044 struct hv_device *child_device_obj; 2045 2046 child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL); 2047 if (!child_device_obj) { 2048 pr_err("Unable to allocate device object for child device\n"); 2049 return NULL; 2050 } 2051 2052 child_device_obj->channel = channel; 2053 guid_copy(&child_device_obj->dev_type, type); 2054 guid_copy(&child_device_obj->dev_instance, instance); 2055 child_device_obj->vendor_id = 0x1414; /* MSFT vendor ID */ 2056 2057 return child_device_obj; 2058 } 2059 2060 /* 2061 * vmbus_device_register - Register the child device 2062 */ 2063 int vmbus_device_register(struct hv_device *child_device_obj) 2064 { 2065 struct kobject *kobj = &child_device_obj->device.kobj; 2066 int ret; 2067 2068 dev_set_name(&child_device_obj->device, "%pUl", 2069 &child_device_obj->channel->offermsg.offer.if_instance); 2070 2071 child_device_obj->device.bus = &hv_bus; 2072 child_device_obj->device.parent = &hv_acpi_dev->dev; 2073 child_device_obj->device.release = vmbus_device_release; 2074 2075 child_device_obj->device.dma_parms = &child_device_obj->dma_parms; 2076 child_device_obj->device.dma_mask = &child_device_obj->dma_mask; 2077 dma_set_mask(&child_device_obj->device, DMA_BIT_MASK(64)); 2078 2079 /* 2080 * Register with the LDM. This will kick off the driver/device 2081 * binding...which will eventually call vmbus_match() and vmbus_probe() 2082 */ 2083 ret = device_register(&child_device_obj->device); 2084 if (ret) { 2085 pr_err("Unable to register child device\n"); 2086 return ret; 2087 } 2088 2089 child_device_obj->channels_kset = kset_create_and_add("channels", 2090 NULL, kobj); 2091 if (!child_device_obj->channels_kset) { 2092 ret = -ENOMEM; 2093 goto err_dev_unregister; 2094 } 2095 2096 ret = vmbus_add_channel_kobj(child_device_obj, 2097 child_device_obj->channel); 2098 if (ret) { 2099 pr_err("Unable to register primary channeln"); 2100 goto err_kset_unregister; 2101 } 2102 hv_debug_add_dev_dir(child_device_obj); 2103 2104 return 0; 2105 2106 err_kset_unregister: 2107 kset_unregister(child_device_obj->channels_kset); 2108 2109 err_dev_unregister: 2110 device_unregister(&child_device_obj->device); 2111 return ret; 2112 } 2113 2114 /* 2115 * vmbus_device_unregister - Remove the specified child device 2116 * from the vmbus. 2117 */ 2118 void vmbus_device_unregister(struct hv_device *device_obj) 2119 { 2120 pr_debug("child device %s unregistered\n", 2121 dev_name(&device_obj->device)); 2122 2123 kset_unregister(device_obj->channels_kset); 2124 2125 /* 2126 * Kick off the process of unregistering the device. 2127 * This will call vmbus_remove() and eventually vmbus_device_release() 2128 */ 2129 device_unregister(&device_obj->device); 2130 } 2131 2132 2133 /* 2134 * VMBUS is an acpi enumerated device. Get the information we 2135 * need from DSDT. 2136 */ 2137 #define VTPM_BASE_ADDRESS 0xfed40000 2138 static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx) 2139 { 2140 resource_size_t start = 0; 2141 resource_size_t end = 0; 2142 struct resource *new_res; 2143 struct resource **old_res = &hyperv_mmio; 2144 struct resource **prev_res = NULL; 2145 struct resource r; 2146 2147 switch (res->type) { 2148 2149 /* 2150 * "Address" descriptors are for bus windows. Ignore 2151 * "memory" descriptors, which are for registers on 2152 * devices. 2153 */ 2154 case ACPI_RESOURCE_TYPE_ADDRESS32: 2155 start = res->data.address32.address.minimum; 2156 end = res->data.address32.address.maximum; 2157 break; 2158 2159 case ACPI_RESOURCE_TYPE_ADDRESS64: 2160 start = res->data.address64.address.minimum; 2161 end = res->data.address64.address.maximum; 2162 break; 2163 2164 /* 2165 * The IRQ information is needed only on ARM64, which Hyper-V 2166 * sets up in the extended format. IRQ information is present 2167 * on x86/x64 in the non-extended format but it is not used by 2168 * Linux. So don't bother checking for the non-extended format. 2169 */ 2170 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 2171 if (!acpi_dev_resource_interrupt(res, 0, &r)) { 2172 pr_err("Unable to parse Hyper-V ACPI interrupt\n"); 2173 return AE_ERROR; 2174 } 2175 /* ARM64 INTID for VMbus */ 2176 vmbus_interrupt = res->data.extended_irq.interrupts[0]; 2177 /* Linux IRQ number */ 2178 vmbus_irq = r.start; 2179 return AE_OK; 2180 2181 default: 2182 /* Unused resource type */ 2183 return AE_OK; 2184 2185 } 2186 /* 2187 * Ignore ranges that are below 1MB, as they're not 2188 * necessary or useful here. 2189 */ 2190 if (end < 0x100000) 2191 return AE_OK; 2192 2193 new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC); 2194 if (!new_res) 2195 return AE_NO_MEMORY; 2196 2197 /* If this range overlaps the virtual TPM, truncate it. */ 2198 if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS) 2199 end = VTPM_BASE_ADDRESS; 2200 2201 new_res->name = "hyperv mmio"; 2202 new_res->flags = IORESOURCE_MEM; 2203 new_res->start = start; 2204 new_res->end = end; 2205 2206 /* 2207 * If two ranges are adjacent, merge them. 2208 */ 2209 do { 2210 if (!*old_res) { 2211 *old_res = new_res; 2212 break; 2213 } 2214 2215 if (((*old_res)->end + 1) == new_res->start) { 2216 (*old_res)->end = new_res->end; 2217 kfree(new_res); 2218 break; 2219 } 2220 2221 if ((*old_res)->start == new_res->end + 1) { 2222 (*old_res)->start = new_res->start; 2223 kfree(new_res); 2224 break; 2225 } 2226 2227 if ((*old_res)->start > new_res->end) { 2228 new_res->sibling = *old_res; 2229 if (prev_res) 2230 (*prev_res)->sibling = new_res; 2231 *old_res = new_res; 2232 break; 2233 } 2234 2235 prev_res = old_res; 2236 old_res = &(*old_res)->sibling; 2237 2238 } while (1); 2239 2240 return AE_OK; 2241 } 2242 2243 static int vmbus_acpi_remove(struct acpi_device *device) 2244 { 2245 struct resource *cur_res; 2246 struct resource *next_res; 2247 2248 if (hyperv_mmio) { 2249 if (fb_mmio) { 2250 __release_region(hyperv_mmio, fb_mmio->start, 2251 resource_size(fb_mmio)); 2252 fb_mmio = NULL; 2253 } 2254 2255 for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) { 2256 next_res = cur_res->sibling; 2257 kfree(cur_res); 2258 } 2259 } 2260 2261 return 0; 2262 } 2263 2264 static void vmbus_reserve_fb(void) 2265 { 2266 resource_size_t start = 0, size; 2267 struct pci_dev *pdev; 2268 2269 if (efi_enabled(EFI_BOOT)) { 2270 /* Gen2 VM: get FB base from EFI framebuffer */ 2271 start = screen_info.lfb_base; 2272 size = max_t(__u32, screen_info.lfb_size, 0x800000); 2273 } else { 2274 /* Gen1 VM: get FB base from PCI */ 2275 pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT, 2276 PCI_DEVICE_ID_HYPERV_VIDEO, NULL); 2277 if (!pdev) 2278 return; 2279 2280 if (pdev->resource[0].flags & IORESOURCE_MEM) { 2281 start = pci_resource_start(pdev, 0); 2282 size = pci_resource_len(pdev, 0); 2283 } 2284 2285 /* 2286 * Release the PCI device so hyperv_drm or hyperv_fb driver can 2287 * grab it later. 2288 */ 2289 pci_dev_put(pdev); 2290 } 2291 2292 if (!start) 2293 return; 2294 2295 /* 2296 * Make a claim for the frame buffer in the resource tree under the 2297 * first node, which will be the one below 4GB. The length seems to 2298 * be underreported, particularly in a Generation 1 VM. So start out 2299 * reserving a larger area and make it smaller until it succeeds. 2300 */ 2301 for (; !fb_mmio && (size >= 0x100000); size >>= 1) 2302 fb_mmio = __request_region(hyperv_mmio, start, size, fb_mmio_name, 0); 2303 } 2304 2305 /** 2306 * vmbus_allocate_mmio() - Pick a memory-mapped I/O range. 2307 * @new: If successful, supplied a pointer to the 2308 * allocated MMIO space. 2309 * @device_obj: Identifies the caller 2310 * @min: Minimum guest physical address of the 2311 * allocation 2312 * @max: Maximum guest physical address 2313 * @size: Size of the range to be allocated 2314 * @align: Alignment of the range to be allocated 2315 * @fb_overlap_ok: Whether this allocation can be allowed 2316 * to overlap the video frame buffer. 2317 * 2318 * This function walks the resources granted to VMBus by the 2319 * _CRS object in the ACPI namespace underneath the parent 2320 * "bridge" whether that's a root PCI bus in the Generation 1 2321 * case or a Module Device in the Generation 2 case. It then 2322 * attempts to allocate from the global MMIO pool in a way that 2323 * matches the constraints supplied in these parameters and by 2324 * that _CRS. 2325 * 2326 * Return: 0 on success, -errno on failure 2327 */ 2328 int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj, 2329 resource_size_t min, resource_size_t max, 2330 resource_size_t size, resource_size_t align, 2331 bool fb_overlap_ok) 2332 { 2333 struct resource *iter, *shadow; 2334 resource_size_t range_min, range_max, start, end; 2335 const char *dev_n = dev_name(&device_obj->device); 2336 int retval; 2337 2338 retval = -ENXIO; 2339 mutex_lock(&hyperv_mmio_lock); 2340 2341 /* 2342 * If overlaps with frame buffers are allowed, then first attempt to 2343 * make the allocation from within the reserved region. Because it 2344 * is already reserved, no shadow allocation is necessary. 2345 */ 2346 if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) && 2347 !(max < fb_mmio->start)) { 2348 2349 range_min = fb_mmio->start; 2350 range_max = fb_mmio->end; 2351 start = (range_min + align - 1) & ~(align - 1); 2352 for (; start + size - 1 <= range_max; start += align) { 2353 *new = request_mem_region_exclusive(start, size, dev_n); 2354 if (*new) { 2355 retval = 0; 2356 goto exit; 2357 } 2358 } 2359 } 2360 2361 for (iter = hyperv_mmio; iter; iter = iter->sibling) { 2362 if ((iter->start >= max) || (iter->end <= min)) 2363 continue; 2364 2365 range_min = iter->start; 2366 range_max = iter->end; 2367 start = (range_min + align - 1) & ~(align - 1); 2368 for (; start + size - 1 <= range_max; start += align) { 2369 end = start + size - 1; 2370 2371 /* Skip the whole fb_mmio region if not fb_overlap_ok */ 2372 if (!fb_overlap_ok && fb_mmio && 2373 (((start >= fb_mmio->start) && (start <= fb_mmio->end)) || 2374 ((end >= fb_mmio->start) && (end <= fb_mmio->end)))) 2375 continue; 2376 2377 shadow = __request_region(iter, start, size, NULL, 2378 IORESOURCE_BUSY); 2379 if (!shadow) 2380 continue; 2381 2382 *new = request_mem_region_exclusive(start, size, dev_n); 2383 if (*new) { 2384 shadow->name = (char *)*new; 2385 retval = 0; 2386 goto exit; 2387 } 2388 2389 __release_region(iter, start, size); 2390 } 2391 } 2392 2393 exit: 2394 mutex_unlock(&hyperv_mmio_lock); 2395 return retval; 2396 } 2397 EXPORT_SYMBOL_GPL(vmbus_allocate_mmio); 2398 2399 /** 2400 * vmbus_free_mmio() - Free a memory-mapped I/O range. 2401 * @start: Base address of region to release. 2402 * @size: Size of the range to be allocated 2403 * 2404 * This function releases anything requested by 2405 * vmbus_mmio_allocate(). 2406 */ 2407 void vmbus_free_mmio(resource_size_t start, resource_size_t size) 2408 { 2409 struct resource *iter; 2410 2411 mutex_lock(&hyperv_mmio_lock); 2412 for (iter = hyperv_mmio; iter; iter = iter->sibling) { 2413 if ((iter->start >= start + size) || (iter->end <= start)) 2414 continue; 2415 2416 __release_region(iter, start, size); 2417 } 2418 release_mem_region(start, size); 2419 mutex_unlock(&hyperv_mmio_lock); 2420 2421 } 2422 EXPORT_SYMBOL_GPL(vmbus_free_mmio); 2423 2424 static int vmbus_acpi_add(struct acpi_device *device) 2425 { 2426 acpi_status result; 2427 int ret_val = -ENODEV; 2428 struct acpi_device *ancestor; 2429 2430 hv_acpi_dev = device; 2431 2432 /* 2433 * Older versions of Hyper-V for ARM64 fail to include the _CCA 2434 * method on the top level VMbus device in the DSDT. But devices 2435 * are hardware coherent in all current Hyper-V use cases, so fix 2436 * up the ACPI device to behave as if _CCA is present and indicates 2437 * hardware coherence. 2438 */ 2439 ACPI_COMPANION_SET(&device->dev, device); 2440 if (IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED) && 2441 device_get_dma_attr(&device->dev) == DEV_DMA_NOT_SUPPORTED) { 2442 pr_info("No ACPI _CCA found; assuming coherent device I/O\n"); 2443 device->flags.cca_seen = true; 2444 device->flags.coherent_dma = true; 2445 } 2446 2447 result = acpi_walk_resources(device->handle, METHOD_NAME__CRS, 2448 vmbus_walk_resources, NULL); 2449 2450 if (ACPI_FAILURE(result)) 2451 goto acpi_walk_err; 2452 /* 2453 * Some ancestor of the vmbus acpi device (Gen1 or Gen2 2454 * firmware) is the VMOD that has the mmio ranges. Get that. 2455 */ 2456 for (ancestor = device->parent; ancestor; ancestor = ancestor->parent) { 2457 result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS, 2458 vmbus_walk_resources, NULL); 2459 2460 if (ACPI_FAILURE(result)) 2461 continue; 2462 if (hyperv_mmio) { 2463 vmbus_reserve_fb(); 2464 break; 2465 } 2466 } 2467 ret_val = 0; 2468 2469 acpi_walk_err: 2470 complete(&probe_event); 2471 if (ret_val) 2472 vmbus_acpi_remove(device); 2473 return ret_val; 2474 } 2475 2476 #ifdef CONFIG_PM_SLEEP 2477 static int vmbus_bus_suspend(struct device *dev) 2478 { 2479 struct hv_per_cpu_context *hv_cpu = per_cpu_ptr( 2480 hv_context.cpu_context, VMBUS_CONNECT_CPU); 2481 struct vmbus_channel *channel, *sc; 2482 2483 tasklet_disable(&hv_cpu->msg_dpc); 2484 vmbus_connection.ignore_any_offer_msg = true; 2485 /* The tasklet_enable() takes care of providing a memory barrier */ 2486 tasklet_enable(&hv_cpu->msg_dpc); 2487 2488 /* Drain all the workqueues as we are in suspend */ 2489 drain_workqueue(vmbus_connection.rescind_work_queue); 2490 drain_workqueue(vmbus_connection.work_queue); 2491 drain_workqueue(vmbus_connection.handle_primary_chan_wq); 2492 drain_workqueue(vmbus_connection.handle_sub_chan_wq); 2493 2494 mutex_lock(&vmbus_connection.channel_mutex); 2495 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) { 2496 if (!is_hvsock_channel(channel)) 2497 continue; 2498 2499 vmbus_force_channel_rescinded(channel); 2500 } 2501 mutex_unlock(&vmbus_connection.channel_mutex); 2502 2503 /* 2504 * Wait until all the sub-channels and hv_sock channels have been 2505 * cleaned up. Sub-channels should be destroyed upon suspend, otherwise 2506 * they would conflict with the new sub-channels that will be created 2507 * in the resume path. hv_sock channels should also be destroyed, but 2508 * a hv_sock channel of an established hv_sock connection can not be 2509 * really destroyed since it may still be referenced by the userspace 2510 * application, so we just force the hv_sock channel to be rescinded 2511 * by vmbus_force_channel_rescinded(), and the userspace application 2512 * will thoroughly destroy the channel after hibernation. 2513 * 2514 * Note: the counter nr_chan_close_on_suspend may never go above 0 if 2515 * the VM has no sub-channel and hv_sock channel, e.g. a 1-vCPU VM. 2516 */ 2517 if (atomic_read(&vmbus_connection.nr_chan_close_on_suspend) > 0) 2518 wait_for_completion(&vmbus_connection.ready_for_suspend_event); 2519 2520 if (atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) != 0) { 2521 pr_err("Can not suspend due to a previous failed resuming\n"); 2522 return -EBUSY; 2523 } 2524 2525 mutex_lock(&vmbus_connection.channel_mutex); 2526 2527 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) { 2528 /* 2529 * Remove the channel from the array of channels and invalidate 2530 * the channel's relid. Upon resume, vmbus_onoffer() will fix 2531 * up the relid (and other fields, if necessary) and add the 2532 * channel back to the array. 2533 */ 2534 vmbus_channel_unmap_relid(channel); 2535 channel->offermsg.child_relid = INVALID_RELID; 2536 2537 if (is_hvsock_channel(channel)) { 2538 if (!channel->rescind) { 2539 pr_err("hv_sock channel not rescinded!\n"); 2540 WARN_ON_ONCE(1); 2541 } 2542 continue; 2543 } 2544 2545 list_for_each_entry(sc, &channel->sc_list, sc_list) { 2546 pr_err("Sub-channel not deleted!\n"); 2547 WARN_ON_ONCE(1); 2548 } 2549 2550 atomic_inc(&vmbus_connection.nr_chan_fixup_on_resume); 2551 } 2552 2553 mutex_unlock(&vmbus_connection.channel_mutex); 2554 2555 vmbus_initiate_unload(false); 2556 2557 /* Reset the event for the next resume. */ 2558 reinit_completion(&vmbus_connection.ready_for_resume_event); 2559 2560 return 0; 2561 } 2562 2563 static int vmbus_bus_resume(struct device *dev) 2564 { 2565 struct vmbus_channel_msginfo *msginfo; 2566 size_t msgsize; 2567 int ret; 2568 2569 vmbus_connection.ignore_any_offer_msg = false; 2570 2571 /* 2572 * We only use the 'vmbus_proto_version', which was in use before 2573 * hibernation, to re-negotiate with the host. 2574 */ 2575 if (!vmbus_proto_version) { 2576 pr_err("Invalid proto version = 0x%x\n", vmbus_proto_version); 2577 return -EINVAL; 2578 } 2579 2580 msgsize = sizeof(*msginfo) + 2581 sizeof(struct vmbus_channel_initiate_contact); 2582 2583 msginfo = kzalloc(msgsize, GFP_KERNEL); 2584 2585 if (msginfo == NULL) 2586 return -ENOMEM; 2587 2588 ret = vmbus_negotiate_version(msginfo, vmbus_proto_version); 2589 2590 kfree(msginfo); 2591 2592 if (ret != 0) 2593 return ret; 2594 2595 WARN_ON(atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) == 0); 2596 2597 vmbus_request_offers(); 2598 2599 if (wait_for_completion_timeout( 2600 &vmbus_connection.ready_for_resume_event, 10 * HZ) == 0) 2601 pr_err("Some vmbus device is missing after suspending?\n"); 2602 2603 /* Reset the event for the next suspend. */ 2604 reinit_completion(&vmbus_connection.ready_for_suspend_event); 2605 2606 return 0; 2607 } 2608 #else 2609 #define vmbus_bus_suspend NULL 2610 #define vmbus_bus_resume NULL 2611 #endif /* CONFIG_PM_SLEEP */ 2612 2613 static const struct acpi_device_id vmbus_acpi_device_ids[] = { 2614 {"VMBUS", 0}, 2615 {"VMBus", 0}, 2616 {"", 0}, 2617 }; 2618 MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids); 2619 2620 /* 2621 * Note: we must use the "no_irq" ops, otherwise hibernation can not work with 2622 * PCI device assignment, because "pci_dev_pm_ops" uses the "noirq" ops: in 2623 * the resume path, the pci "noirq" restore op runs before "non-noirq" op (see 2624 * resume_target_kernel() -> dpm_resume_start(), and hibernation_restore() -> 2625 * dpm_resume_end()). This means vmbus_bus_resume() and the pci-hyperv's 2626 * resume callback must also run via the "noirq" ops. 2627 * 2628 * Set suspend_noirq/resume_noirq to NULL for Suspend-to-Idle: see the comment 2629 * earlier in this file before vmbus_pm. 2630 */ 2631 2632 static const struct dev_pm_ops vmbus_bus_pm = { 2633 .suspend_noirq = NULL, 2634 .resume_noirq = NULL, 2635 .freeze_noirq = vmbus_bus_suspend, 2636 .thaw_noirq = vmbus_bus_resume, 2637 .poweroff_noirq = vmbus_bus_suspend, 2638 .restore_noirq = vmbus_bus_resume 2639 }; 2640 2641 static struct acpi_driver vmbus_acpi_driver = { 2642 .name = "vmbus", 2643 .ids = vmbus_acpi_device_ids, 2644 .ops = { 2645 .add = vmbus_acpi_add, 2646 .remove = vmbus_acpi_remove, 2647 }, 2648 .drv.pm = &vmbus_bus_pm, 2649 }; 2650 2651 static void hv_kexec_handler(void) 2652 { 2653 hv_stimer_global_cleanup(); 2654 vmbus_initiate_unload(false); 2655 /* Make sure conn_state is set as hv_synic_cleanup checks for it */ 2656 mb(); 2657 cpuhp_remove_state(hyperv_cpuhp_online); 2658 }; 2659 2660 static void hv_crash_handler(struct pt_regs *regs) 2661 { 2662 int cpu; 2663 2664 vmbus_initiate_unload(true); 2665 /* 2666 * In crash handler we can't schedule synic cleanup for all CPUs, 2667 * doing the cleanup for current CPU only. This should be sufficient 2668 * for kdump. 2669 */ 2670 cpu = smp_processor_id(); 2671 hv_stimer_cleanup(cpu); 2672 hv_synic_disable_regs(cpu); 2673 }; 2674 2675 static int hv_synic_suspend(void) 2676 { 2677 /* 2678 * When we reach here, all the non-boot CPUs have been offlined. 2679 * If we're in a legacy configuration where stimer Direct Mode is 2680 * not enabled, the stimers on the non-boot CPUs have been unbound 2681 * in hv_synic_cleanup() -> hv_stimer_legacy_cleanup() -> 2682 * hv_stimer_cleanup() -> clockevents_unbind_device(). 2683 * 2684 * hv_synic_suspend() only runs on CPU0 with interrupts disabled. 2685 * Here we do not call hv_stimer_legacy_cleanup() on CPU0 because: 2686 * 1) it's unnecessary as interrupts remain disabled between 2687 * syscore_suspend() and syscore_resume(): see create_image() and 2688 * resume_target_kernel() 2689 * 2) the stimer on CPU0 is automatically disabled later by 2690 * syscore_suspend() -> timekeeping_suspend() -> tick_suspend() -> ... 2691 * -> clockevents_shutdown() -> ... -> hv_ce_shutdown() 2692 * 3) a warning would be triggered if we call 2693 * clockevents_unbind_device(), which may sleep, in an 2694 * interrupts-disabled context. 2695 */ 2696 2697 hv_synic_disable_regs(0); 2698 2699 return 0; 2700 } 2701 2702 static void hv_synic_resume(void) 2703 { 2704 hv_synic_enable_regs(0); 2705 2706 /* 2707 * Note: we don't need to call hv_stimer_init(0), because the timer 2708 * on CPU0 is not unbound in hv_synic_suspend(), and the timer is 2709 * automatically re-enabled in timekeeping_resume(). 2710 */ 2711 } 2712 2713 /* The callbacks run only on CPU0, with irqs_disabled. */ 2714 static struct syscore_ops hv_synic_syscore_ops = { 2715 .suspend = hv_synic_suspend, 2716 .resume = hv_synic_resume, 2717 }; 2718 2719 static int __init hv_acpi_init(void) 2720 { 2721 int ret, t; 2722 2723 if (!hv_is_hyperv_initialized()) 2724 return -ENODEV; 2725 2726 if (hv_root_partition) 2727 return 0; 2728 2729 init_completion(&probe_event); 2730 2731 /* 2732 * Get ACPI resources first. 2733 */ 2734 ret = acpi_bus_register_driver(&vmbus_acpi_driver); 2735 2736 if (ret) 2737 return ret; 2738 2739 t = wait_for_completion_timeout(&probe_event, 5*HZ); 2740 if (t == 0) { 2741 ret = -ETIMEDOUT; 2742 goto cleanup; 2743 } 2744 2745 /* 2746 * If we're on an architecture with a hardcoded hypervisor 2747 * vector (i.e. x86/x64), override the VMbus interrupt found 2748 * in the ACPI tables. Ensure vmbus_irq is not set since the 2749 * normal Linux IRQ mechanism is not used in this case. 2750 */ 2751 #ifdef HYPERVISOR_CALLBACK_VECTOR 2752 vmbus_interrupt = HYPERVISOR_CALLBACK_VECTOR; 2753 vmbus_irq = -1; 2754 #endif 2755 2756 hv_debug_init(); 2757 2758 ret = vmbus_bus_init(); 2759 if (ret) 2760 goto cleanup; 2761 2762 hv_setup_kexec_handler(hv_kexec_handler); 2763 hv_setup_crash_handler(hv_crash_handler); 2764 2765 register_syscore_ops(&hv_synic_syscore_ops); 2766 2767 return 0; 2768 2769 cleanup: 2770 acpi_bus_unregister_driver(&vmbus_acpi_driver); 2771 hv_acpi_dev = NULL; 2772 return ret; 2773 } 2774 2775 static void __exit vmbus_exit(void) 2776 { 2777 int cpu; 2778 2779 unregister_syscore_ops(&hv_synic_syscore_ops); 2780 2781 hv_remove_kexec_handler(); 2782 hv_remove_crash_handler(); 2783 vmbus_connection.conn_state = DISCONNECTED; 2784 hv_stimer_global_cleanup(); 2785 vmbus_disconnect(); 2786 if (vmbus_irq == -1) { 2787 hv_remove_vmbus_handler(); 2788 } else { 2789 free_percpu_irq(vmbus_irq, vmbus_evt); 2790 free_percpu(vmbus_evt); 2791 } 2792 for_each_online_cpu(cpu) { 2793 struct hv_per_cpu_context *hv_cpu 2794 = per_cpu_ptr(hv_context.cpu_context, cpu); 2795 2796 tasklet_kill(&hv_cpu->msg_dpc); 2797 } 2798 hv_debug_rm_all_dir(); 2799 2800 vmbus_free_channels(); 2801 kfree(vmbus_connection.channels); 2802 2803 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) { 2804 kmsg_dump_unregister(&hv_kmsg_dumper); 2805 unregister_die_notifier(&hyperv_die_block); 2806 } 2807 2808 /* 2809 * The panic notifier is always registered, hence we should 2810 * also unconditionally unregister it here as well. 2811 */ 2812 atomic_notifier_chain_unregister(&panic_notifier_list, 2813 &hyperv_panic_block); 2814 2815 free_page((unsigned long)hv_panic_page); 2816 unregister_sysctl_table(hv_ctl_table_hdr); 2817 hv_ctl_table_hdr = NULL; 2818 bus_unregister(&hv_bus); 2819 2820 cpuhp_remove_state(hyperv_cpuhp_online); 2821 hv_synic_free(); 2822 acpi_bus_unregister_driver(&vmbus_acpi_driver); 2823 } 2824 2825 2826 MODULE_LICENSE("GPL"); 2827 MODULE_DESCRIPTION("Microsoft Hyper-V VMBus Driver"); 2828 2829 subsys_initcall(hv_acpi_init); 2830 module_exit(vmbus_exit); 2831