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