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