1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2009, Microsoft Corporation.
6 * Haiyang Zhang <haiyangz@microsoft.com>
7 * Hank Janssen <hjanssen@microsoft.com>
8 * K. Y. Srinivasan <kys@microsoft.com>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/interrupt.h>
16 #include <linux/sysctl.h>
17 #include <linux/slab.h>
18 #include <linux/acpi.h>
19 #include <linux/completion.h>
20 #include <linux/hyperv.h>
21 #include <linux/kernel_stat.h>
22 #include <linux/clockchips.h>
23 #include <linux/cpu.h>
24 #include <linux/sched/isolation.h>
25 #include <linux/sched/task_stack.h>
27 #include <linux/delay.h>
28 #include <linux/notifier.h>
29 #include <linux/panic_notifier.h>
30 #include <linux/ptrace.h>
31 #include <linux/screen_info.h>
32 #include <linux/kdebug.h>
33 #include <linux/efi.h>
34 #include <linux/random.h>
35 #include <linux/kernel.h>
36 #include <linux/syscore_ops.h>
37 #include <linux/dma-map-ops.h>
38 #include <linux/pci.h>
39 #include <clocksource/hyperv_timer.h>
40 #include "hyperv_vmbus.h"
43 struct list_head node;
44 struct hv_vmbus_device_id id;
47 static struct acpi_device *hv_acpi_dev;
49 static struct completion probe_event;
51 static int hyperv_cpuhp_online;
53 static void *hv_panic_page;
55 static long __percpu *vmbus_evt;
57 /* Values parsed from ACPI DSDT */
62 * Boolean to control whether to report panic messages over Hyper-V.
64 * It can be set via /proc/sys/kernel/hyperv_record_panic_msg
66 static int sysctl_record_panic_msg = 1;
68 static int hyperv_report_reg(void)
70 return !sysctl_record_panic_msg || !hv_panic_page;
73 static int hyperv_panic_event(struct notifier_block *nb, unsigned long val,
78 vmbus_initiate_unload(true);
81 * Hyper-V should be notified only once about a panic. If we will be
82 * doing hv_kmsg_dump() with kmsg data later, don't do the notification
85 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE
86 && hyperv_report_reg()) {
87 regs = current_pt_regs();
88 hyperv_report_panic(regs, val, false);
93 static int hyperv_die_event(struct notifier_block *nb, unsigned long val,
96 struct die_args *die = args;
97 struct pt_regs *regs = die->regs;
99 /* Don't notify Hyper-V if the die event is other than oops */
104 * Hyper-V should be notified only once about a panic. If we will be
105 * doing hv_kmsg_dump() with kmsg data later, don't do the notification
108 if (hyperv_report_reg())
109 hyperv_report_panic(regs, val, true);
113 static struct notifier_block hyperv_die_block = {
114 .notifier_call = hyperv_die_event,
116 static struct notifier_block hyperv_panic_block = {
117 .notifier_call = hyperv_panic_event,
120 static const char *fb_mmio_name = "fb_range";
121 static struct resource *fb_mmio;
122 static struct resource *hyperv_mmio;
123 static DEFINE_MUTEX(hyperv_mmio_lock);
125 static int vmbus_exists(void)
127 if (hv_acpi_dev == NULL)
133 static u8 channel_monitor_group(const struct vmbus_channel *channel)
135 return (u8)channel->offermsg.monitorid / 32;
138 static u8 channel_monitor_offset(const struct vmbus_channel *channel)
140 return (u8)channel->offermsg.monitorid % 32;
143 static u32 channel_pending(const struct vmbus_channel *channel,
144 const struct hv_monitor_page *monitor_page)
146 u8 monitor_group = channel_monitor_group(channel);
148 return monitor_page->trigger_group[monitor_group].pending;
151 static u32 channel_latency(const struct vmbus_channel *channel,
152 const struct hv_monitor_page *monitor_page)
154 u8 monitor_group = channel_monitor_group(channel);
155 u8 monitor_offset = channel_monitor_offset(channel);
157 return monitor_page->latency[monitor_group][monitor_offset];
160 static u32 channel_conn_id(struct vmbus_channel *channel,
161 struct hv_monitor_page *monitor_page)
163 u8 monitor_group = channel_monitor_group(channel);
164 u8 monitor_offset = channel_monitor_offset(channel);
166 return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
169 static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
172 struct hv_device *hv_dev = device_to_hv_device(dev);
174 if (!hv_dev->channel)
176 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
178 static DEVICE_ATTR_RO(id);
180 static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
183 struct hv_device *hv_dev = device_to_hv_device(dev);
185 if (!hv_dev->channel)
187 return sprintf(buf, "%d\n", hv_dev->channel->state);
189 static DEVICE_ATTR_RO(state);
191 static ssize_t monitor_id_show(struct device *dev,
192 struct device_attribute *dev_attr, char *buf)
194 struct hv_device *hv_dev = device_to_hv_device(dev);
196 if (!hv_dev->channel)
198 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
200 static DEVICE_ATTR_RO(monitor_id);
202 static ssize_t class_id_show(struct device *dev,
203 struct device_attribute *dev_attr, char *buf)
205 struct hv_device *hv_dev = device_to_hv_device(dev);
207 if (!hv_dev->channel)
209 return sprintf(buf, "{%pUl}\n",
210 &hv_dev->channel->offermsg.offer.if_type);
212 static DEVICE_ATTR_RO(class_id);
214 static ssize_t device_id_show(struct device *dev,
215 struct device_attribute *dev_attr, char *buf)
217 struct hv_device *hv_dev = device_to_hv_device(dev);
219 if (!hv_dev->channel)
221 return sprintf(buf, "{%pUl}\n",
222 &hv_dev->channel->offermsg.offer.if_instance);
224 static DEVICE_ATTR_RO(device_id);
226 static ssize_t modalias_show(struct device *dev,
227 struct device_attribute *dev_attr, char *buf)
229 struct hv_device *hv_dev = device_to_hv_device(dev);
231 return sprintf(buf, "vmbus:%*phN\n", UUID_SIZE, &hv_dev->dev_type);
233 static DEVICE_ATTR_RO(modalias);
236 static ssize_t numa_node_show(struct device *dev,
237 struct device_attribute *attr, char *buf)
239 struct hv_device *hv_dev = device_to_hv_device(dev);
241 if (!hv_dev->channel)
244 return sprintf(buf, "%d\n", cpu_to_node(hv_dev->channel->target_cpu));
246 static DEVICE_ATTR_RO(numa_node);
249 static ssize_t server_monitor_pending_show(struct device *dev,
250 struct device_attribute *dev_attr,
253 struct hv_device *hv_dev = device_to_hv_device(dev);
255 if (!hv_dev->channel)
257 return sprintf(buf, "%d\n",
258 channel_pending(hv_dev->channel,
259 vmbus_connection.monitor_pages[0]));
261 static DEVICE_ATTR_RO(server_monitor_pending);
263 static ssize_t client_monitor_pending_show(struct device *dev,
264 struct device_attribute *dev_attr,
267 struct hv_device *hv_dev = device_to_hv_device(dev);
269 if (!hv_dev->channel)
271 return sprintf(buf, "%d\n",
272 channel_pending(hv_dev->channel,
273 vmbus_connection.monitor_pages[1]));
275 static DEVICE_ATTR_RO(client_monitor_pending);
277 static ssize_t server_monitor_latency_show(struct device *dev,
278 struct device_attribute *dev_attr,
281 struct hv_device *hv_dev = device_to_hv_device(dev);
283 if (!hv_dev->channel)
285 return sprintf(buf, "%d\n",
286 channel_latency(hv_dev->channel,
287 vmbus_connection.monitor_pages[0]));
289 static DEVICE_ATTR_RO(server_monitor_latency);
291 static ssize_t client_monitor_latency_show(struct device *dev,
292 struct device_attribute *dev_attr,
295 struct hv_device *hv_dev = device_to_hv_device(dev);
297 if (!hv_dev->channel)
299 return sprintf(buf, "%d\n",
300 channel_latency(hv_dev->channel,
301 vmbus_connection.monitor_pages[1]));
303 static DEVICE_ATTR_RO(client_monitor_latency);
305 static ssize_t server_monitor_conn_id_show(struct device *dev,
306 struct device_attribute *dev_attr,
309 struct hv_device *hv_dev = device_to_hv_device(dev);
311 if (!hv_dev->channel)
313 return sprintf(buf, "%d\n",
314 channel_conn_id(hv_dev->channel,
315 vmbus_connection.monitor_pages[0]));
317 static DEVICE_ATTR_RO(server_monitor_conn_id);
319 static ssize_t client_monitor_conn_id_show(struct device *dev,
320 struct device_attribute *dev_attr,
323 struct hv_device *hv_dev = device_to_hv_device(dev);
325 if (!hv_dev->channel)
327 return sprintf(buf, "%d\n",
328 channel_conn_id(hv_dev->channel,
329 vmbus_connection.monitor_pages[1]));
331 static DEVICE_ATTR_RO(client_monitor_conn_id);
333 static ssize_t out_intr_mask_show(struct device *dev,
334 struct device_attribute *dev_attr, char *buf)
336 struct hv_device *hv_dev = device_to_hv_device(dev);
337 struct hv_ring_buffer_debug_info outbound;
340 if (!hv_dev->channel)
343 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
348 return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
350 static DEVICE_ATTR_RO(out_intr_mask);
352 static ssize_t out_read_index_show(struct device *dev,
353 struct device_attribute *dev_attr, char *buf)
355 struct hv_device *hv_dev = device_to_hv_device(dev);
356 struct hv_ring_buffer_debug_info outbound;
359 if (!hv_dev->channel)
362 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
366 return sprintf(buf, "%d\n", outbound.current_read_index);
368 static DEVICE_ATTR_RO(out_read_index);
370 static ssize_t out_write_index_show(struct device *dev,
371 struct device_attribute *dev_attr,
374 struct hv_device *hv_dev = device_to_hv_device(dev);
375 struct hv_ring_buffer_debug_info outbound;
378 if (!hv_dev->channel)
381 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
385 return sprintf(buf, "%d\n", outbound.current_write_index);
387 static DEVICE_ATTR_RO(out_write_index);
389 static ssize_t out_read_bytes_avail_show(struct device *dev,
390 struct device_attribute *dev_attr,
393 struct hv_device *hv_dev = device_to_hv_device(dev);
394 struct hv_ring_buffer_debug_info outbound;
397 if (!hv_dev->channel)
400 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
404 return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
406 static DEVICE_ATTR_RO(out_read_bytes_avail);
408 static ssize_t out_write_bytes_avail_show(struct device *dev,
409 struct device_attribute *dev_attr,
412 struct hv_device *hv_dev = device_to_hv_device(dev);
413 struct hv_ring_buffer_debug_info outbound;
416 if (!hv_dev->channel)
419 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
423 return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
425 static DEVICE_ATTR_RO(out_write_bytes_avail);
427 static ssize_t in_intr_mask_show(struct device *dev,
428 struct device_attribute *dev_attr, char *buf)
430 struct hv_device *hv_dev = device_to_hv_device(dev);
431 struct hv_ring_buffer_debug_info inbound;
434 if (!hv_dev->channel)
437 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
441 return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
443 static DEVICE_ATTR_RO(in_intr_mask);
445 static ssize_t in_read_index_show(struct device *dev,
446 struct device_attribute *dev_attr, char *buf)
448 struct hv_device *hv_dev = device_to_hv_device(dev);
449 struct hv_ring_buffer_debug_info inbound;
452 if (!hv_dev->channel)
455 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
459 return sprintf(buf, "%d\n", inbound.current_read_index);
461 static DEVICE_ATTR_RO(in_read_index);
463 static ssize_t in_write_index_show(struct device *dev,
464 struct device_attribute *dev_attr, char *buf)
466 struct hv_device *hv_dev = device_to_hv_device(dev);
467 struct hv_ring_buffer_debug_info inbound;
470 if (!hv_dev->channel)
473 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
477 return sprintf(buf, "%d\n", inbound.current_write_index);
479 static DEVICE_ATTR_RO(in_write_index);
481 static ssize_t in_read_bytes_avail_show(struct device *dev,
482 struct device_attribute *dev_attr,
485 struct hv_device *hv_dev = device_to_hv_device(dev);
486 struct hv_ring_buffer_debug_info inbound;
489 if (!hv_dev->channel)
492 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
496 return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
498 static DEVICE_ATTR_RO(in_read_bytes_avail);
500 static ssize_t in_write_bytes_avail_show(struct device *dev,
501 struct device_attribute *dev_attr,
504 struct hv_device *hv_dev = device_to_hv_device(dev);
505 struct hv_ring_buffer_debug_info inbound;
508 if (!hv_dev->channel)
511 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
515 return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
517 static DEVICE_ATTR_RO(in_write_bytes_avail);
519 static ssize_t channel_vp_mapping_show(struct device *dev,
520 struct device_attribute *dev_attr,
523 struct hv_device *hv_dev = device_to_hv_device(dev);
524 struct vmbus_channel *channel = hv_dev->channel, *cur_sc;
525 int buf_size = PAGE_SIZE, n_written, tot_written;
526 struct list_head *cur;
531 mutex_lock(&vmbus_connection.channel_mutex);
533 tot_written = snprintf(buf, buf_size, "%u:%u\n",
534 channel->offermsg.child_relid, channel->target_cpu);
536 list_for_each(cur, &channel->sc_list) {
537 if (tot_written >= buf_size - 1)
540 cur_sc = list_entry(cur, struct vmbus_channel, sc_list);
541 n_written = scnprintf(buf + tot_written,
542 buf_size - tot_written,
544 cur_sc->offermsg.child_relid,
546 tot_written += n_written;
549 mutex_unlock(&vmbus_connection.channel_mutex);
553 static DEVICE_ATTR_RO(channel_vp_mapping);
555 static ssize_t vendor_show(struct device *dev,
556 struct device_attribute *dev_attr,
559 struct hv_device *hv_dev = device_to_hv_device(dev);
561 return sprintf(buf, "0x%x\n", hv_dev->vendor_id);
563 static DEVICE_ATTR_RO(vendor);
565 static ssize_t device_show(struct device *dev,
566 struct device_attribute *dev_attr,
569 struct hv_device *hv_dev = device_to_hv_device(dev);
571 return sprintf(buf, "0x%x\n", hv_dev->device_id);
573 static DEVICE_ATTR_RO(device);
575 static ssize_t driver_override_store(struct device *dev,
576 struct device_attribute *attr,
577 const char *buf, size_t count)
579 struct hv_device *hv_dev = device_to_hv_device(dev);
582 ret = driver_set_override(dev, &hv_dev->driver_override, buf, count);
589 static ssize_t driver_override_show(struct device *dev,
590 struct device_attribute *attr, char *buf)
592 struct hv_device *hv_dev = device_to_hv_device(dev);
596 len = snprintf(buf, PAGE_SIZE, "%s\n", hv_dev->driver_override);
601 static DEVICE_ATTR_RW(driver_override);
603 /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
604 static struct attribute *vmbus_dev_attrs[] = {
606 &dev_attr_state.attr,
607 &dev_attr_monitor_id.attr,
608 &dev_attr_class_id.attr,
609 &dev_attr_device_id.attr,
610 &dev_attr_modalias.attr,
612 &dev_attr_numa_node.attr,
614 &dev_attr_server_monitor_pending.attr,
615 &dev_attr_client_monitor_pending.attr,
616 &dev_attr_server_monitor_latency.attr,
617 &dev_attr_client_monitor_latency.attr,
618 &dev_attr_server_monitor_conn_id.attr,
619 &dev_attr_client_monitor_conn_id.attr,
620 &dev_attr_out_intr_mask.attr,
621 &dev_attr_out_read_index.attr,
622 &dev_attr_out_write_index.attr,
623 &dev_attr_out_read_bytes_avail.attr,
624 &dev_attr_out_write_bytes_avail.attr,
625 &dev_attr_in_intr_mask.attr,
626 &dev_attr_in_read_index.attr,
627 &dev_attr_in_write_index.attr,
628 &dev_attr_in_read_bytes_avail.attr,
629 &dev_attr_in_write_bytes_avail.attr,
630 &dev_attr_channel_vp_mapping.attr,
631 &dev_attr_vendor.attr,
632 &dev_attr_device.attr,
633 &dev_attr_driver_override.attr,
638 * Device-level attribute_group callback function. Returns the permission for
639 * each attribute, and returns 0 if an attribute is not visible.
641 static umode_t vmbus_dev_attr_is_visible(struct kobject *kobj,
642 struct attribute *attr, int idx)
644 struct device *dev = kobj_to_dev(kobj);
645 const struct hv_device *hv_dev = device_to_hv_device(dev);
647 /* Hide the monitor attributes if the monitor mechanism is not used. */
648 if (!hv_dev->channel->offermsg.monitor_allocated &&
649 (attr == &dev_attr_monitor_id.attr ||
650 attr == &dev_attr_server_monitor_pending.attr ||
651 attr == &dev_attr_client_monitor_pending.attr ||
652 attr == &dev_attr_server_monitor_latency.attr ||
653 attr == &dev_attr_client_monitor_latency.attr ||
654 attr == &dev_attr_server_monitor_conn_id.attr ||
655 attr == &dev_attr_client_monitor_conn_id.attr))
661 static const struct attribute_group vmbus_dev_group = {
662 .attrs = vmbus_dev_attrs,
663 .is_visible = vmbus_dev_attr_is_visible
665 __ATTRIBUTE_GROUPS(vmbus_dev);
667 /* Set up the attribute for /sys/bus/vmbus/hibernation */
668 static ssize_t hibernation_show(struct bus_type *bus, char *buf)
670 return sprintf(buf, "%d\n", !!hv_is_hibernation_supported());
673 static BUS_ATTR_RO(hibernation);
675 static struct attribute *vmbus_bus_attrs[] = {
676 &bus_attr_hibernation.attr,
679 static const struct attribute_group vmbus_bus_group = {
680 .attrs = vmbus_bus_attrs,
682 __ATTRIBUTE_GROUPS(vmbus_bus);
685 * vmbus_uevent - add uevent for our device
687 * This routine is invoked when a device is added or removed on the vmbus to
688 * generate a uevent to udev in the userspace. The udev will then look at its
689 * rule and the uevent generated here to load the appropriate driver
691 * The alias string will be of the form vmbus:guid where guid is the string
692 * representation of the device guid (each byte of the guid will be
693 * represented with two hex characters.
695 static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
697 struct hv_device *dev = device_to_hv_device(device);
698 const char *format = "MODALIAS=vmbus:%*phN";
700 return add_uevent_var(env, format, UUID_SIZE, &dev->dev_type);
703 static const struct hv_vmbus_device_id *
704 hv_vmbus_dev_match(const struct hv_vmbus_device_id *id, const guid_t *guid)
707 return NULL; /* empty device table */
709 for (; !guid_is_null(&id->guid); id++)
710 if (guid_equal(&id->guid, guid))
716 static const struct hv_vmbus_device_id *
717 hv_vmbus_dynid_match(struct hv_driver *drv, const guid_t *guid)
719 const struct hv_vmbus_device_id *id = NULL;
720 struct vmbus_dynid *dynid;
722 spin_lock(&drv->dynids.lock);
723 list_for_each_entry(dynid, &drv->dynids.list, node) {
724 if (guid_equal(&dynid->id.guid, guid)) {
729 spin_unlock(&drv->dynids.lock);
734 static const struct hv_vmbus_device_id vmbus_device_null;
737 * Return a matching hv_vmbus_device_id pointer.
738 * If there is no match, return NULL.
740 static const struct hv_vmbus_device_id *hv_vmbus_get_id(struct hv_driver *drv,
741 struct hv_device *dev)
743 const guid_t *guid = &dev->dev_type;
744 const struct hv_vmbus_device_id *id;
746 /* When driver_override is set, only bind to the matching driver */
747 if (dev->driver_override && strcmp(dev->driver_override, drv->name))
750 /* Look at the dynamic ids first, before the static ones */
751 id = hv_vmbus_dynid_match(drv, guid);
753 id = hv_vmbus_dev_match(drv->id_table, guid);
755 /* driver_override will always match, send a dummy id */
756 if (!id && dev->driver_override)
757 id = &vmbus_device_null;
762 /* vmbus_add_dynid - add a new device ID to this driver and re-probe devices */
763 static int vmbus_add_dynid(struct hv_driver *drv, guid_t *guid)
765 struct vmbus_dynid *dynid;
767 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
771 dynid->id.guid = *guid;
773 spin_lock(&drv->dynids.lock);
774 list_add_tail(&dynid->node, &drv->dynids.list);
775 spin_unlock(&drv->dynids.lock);
777 return driver_attach(&drv->driver);
780 static void vmbus_free_dynids(struct hv_driver *drv)
782 struct vmbus_dynid *dynid, *n;
784 spin_lock(&drv->dynids.lock);
785 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
786 list_del(&dynid->node);
789 spin_unlock(&drv->dynids.lock);
793 * store_new_id - sysfs frontend to vmbus_add_dynid()
795 * Allow GUIDs to be added to an existing driver via sysfs.
797 static ssize_t new_id_store(struct device_driver *driver, const char *buf,
800 struct hv_driver *drv = drv_to_hv_drv(driver);
804 retval = guid_parse(buf, &guid);
808 if (hv_vmbus_dynid_match(drv, &guid))
811 retval = vmbus_add_dynid(drv, &guid);
816 static DRIVER_ATTR_WO(new_id);
819 * store_remove_id - remove a PCI device ID from this driver
821 * Removes a dynamic pci device ID to this driver.
823 static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
826 struct hv_driver *drv = drv_to_hv_drv(driver);
827 struct vmbus_dynid *dynid, *n;
831 retval = guid_parse(buf, &guid);
836 spin_lock(&drv->dynids.lock);
837 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
838 struct hv_vmbus_device_id *id = &dynid->id;
840 if (guid_equal(&id->guid, &guid)) {
841 list_del(&dynid->node);
847 spin_unlock(&drv->dynids.lock);
851 static DRIVER_ATTR_WO(remove_id);
853 static struct attribute *vmbus_drv_attrs[] = {
854 &driver_attr_new_id.attr,
855 &driver_attr_remove_id.attr,
858 ATTRIBUTE_GROUPS(vmbus_drv);
862 * vmbus_match - Attempt to match the specified device to the specified driver
864 static int vmbus_match(struct device *device, struct device_driver *driver)
866 struct hv_driver *drv = drv_to_hv_drv(driver);
867 struct hv_device *hv_dev = device_to_hv_device(device);
869 /* The hv_sock driver handles all hv_sock offers. */
870 if (is_hvsock_channel(hv_dev->channel))
873 if (hv_vmbus_get_id(drv, hv_dev))
880 * vmbus_probe - Add the new vmbus's child device
882 static int vmbus_probe(struct device *child_device)
885 struct hv_driver *drv =
886 drv_to_hv_drv(child_device->driver);
887 struct hv_device *dev = device_to_hv_device(child_device);
888 const struct hv_vmbus_device_id *dev_id;
890 dev_id = hv_vmbus_get_id(drv, dev);
892 ret = drv->probe(dev, dev_id);
894 pr_err("probe failed for device %s (%d)\n",
895 dev_name(child_device), ret);
898 pr_err("probe not set for driver %s\n",
899 dev_name(child_device));
906 * vmbus_dma_configure -- Configure DMA coherence for VMbus device
908 static int vmbus_dma_configure(struct device *child_device)
911 * On ARM64, propagate the DMA coherence setting from the top level
912 * VMbus ACPI device to the child VMbus device being added here.
913 * On x86/x64 coherence is assumed and these calls have no effect.
915 hv_setup_dma_ops(child_device,
916 device_get_dma_attr(&hv_acpi_dev->dev) == DEV_DMA_COHERENT);
921 * vmbus_remove - Remove a vmbus device
923 static void vmbus_remove(struct device *child_device)
925 struct hv_driver *drv;
926 struct hv_device *dev = device_to_hv_device(child_device);
928 if (child_device->driver) {
929 drv = drv_to_hv_drv(child_device->driver);
936 * vmbus_shutdown - Shutdown a vmbus device
938 static void vmbus_shutdown(struct device *child_device)
940 struct hv_driver *drv;
941 struct hv_device *dev = device_to_hv_device(child_device);
944 /* The device may not be attached yet */
945 if (!child_device->driver)
948 drv = drv_to_hv_drv(child_device->driver);
954 #ifdef CONFIG_PM_SLEEP
956 * vmbus_suspend - Suspend a vmbus device
958 static int vmbus_suspend(struct device *child_device)
960 struct hv_driver *drv;
961 struct hv_device *dev = device_to_hv_device(child_device);
963 /* The device may not be attached yet */
964 if (!child_device->driver)
967 drv = drv_to_hv_drv(child_device->driver);
971 return drv->suspend(dev);
975 * vmbus_resume - Resume a vmbus device
977 static int vmbus_resume(struct device *child_device)
979 struct hv_driver *drv;
980 struct hv_device *dev = device_to_hv_device(child_device);
982 /* The device may not be attached yet */
983 if (!child_device->driver)
986 drv = drv_to_hv_drv(child_device->driver);
990 return drv->resume(dev);
993 #define vmbus_suspend NULL
994 #define vmbus_resume NULL
995 #endif /* CONFIG_PM_SLEEP */
998 * vmbus_device_release - Final callback release of the vmbus child device
1000 static void vmbus_device_release(struct device *device)
1002 struct hv_device *hv_dev = device_to_hv_device(device);
1003 struct vmbus_channel *channel = hv_dev->channel;
1005 hv_debug_rm_dev_dir(hv_dev);
1007 mutex_lock(&vmbus_connection.channel_mutex);
1008 hv_process_channel_removal(channel);
1009 mutex_unlock(&vmbus_connection.channel_mutex);
1014 * Note: we must use the "noirq" ops: see the comment before vmbus_bus_pm.
1016 * suspend_noirq/resume_noirq are set to NULL to support Suspend-to-Idle: we
1017 * shouldn't suspend the vmbus devices upon Suspend-to-Idle, otherwise there
1018 * is no way to wake up a Generation-2 VM.
1020 * The other 4 ops are for hibernation.
1023 static const struct dev_pm_ops vmbus_pm = {
1024 .suspend_noirq = NULL,
1025 .resume_noirq = NULL,
1026 .freeze_noirq = vmbus_suspend,
1027 .thaw_noirq = vmbus_resume,
1028 .poweroff_noirq = vmbus_suspend,
1029 .restore_noirq = vmbus_resume,
1032 /* The one and only one */
1033 static struct bus_type hv_bus = {
1035 .match = vmbus_match,
1036 .shutdown = vmbus_shutdown,
1037 .remove = vmbus_remove,
1038 .probe = vmbus_probe,
1039 .uevent = vmbus_uevent,
1040 .dma_configure = vmbus_dma_configure,
1041 .dev_groups = vmbus_dev_groups,
1042 .drv_groups = vmbus_drv_groups,
1043 .bus_groups = vmbus_bus_groups,
1047 struct onmessage_work_context {
1048 struct work_struct work;
1050 struct hv_message_header header;
1055 static void vmbus_onmessage_work(struct work_struct *work)
1057 struct onmessage_work_context *ctx;
1059 /* Do not process messages if we're in DISCONNECTED state */
1060 if (vmbus_connection.conn_state == DISCONNECTED)
1063 ctx = container_of(work, struct onmessage_work_context,
1065 vmbus_onmessage((struct vmbus_channel_message_header *)
1070 void vmbus_on_msg_dpc(unsigned long data)
1072 struct hv_per_cpu_context *hv_cpu = (void *)data;
1073 void *page_addr = hv_cpu->synic_message_page;
1074 struct hv_message msg_copy, *msg = (struct hv_message *)page_addr +
1076 struct vmbus_channel_message_header *hdr;
1077 enum vmbus_channel_message_type msgtype;
1078 const struct vmbus_channel_message_table_entry *entry;
1079 struct onmessage_work_context *ctx;
1084 * 'enum vmbus_channel_message_type' is supposed to always be 'u32' as
1085 * it is being used in 'struct vmbus_channel_message_header' definition
1086 * which is supposed to match hypervisor ABI.
1088 BUILD_BUG_ON(sizeof(enum vmbus_channel_message_type) != sizeof(u32));
1091 * Since the message is in memory shared with the host, an erroneous or
1092 * malicious Hyper-V could modify the message while vmbus_on_msg_dpc()
1093 * or individual message handlers are executing; to prevent this, copy
1094 * the message into private memory.
1096 memcpy(&msg_copy, msg, sizeof(struct hv_message));
1098 message_type = msg_copy.header.message_type;
1099 if (message_type == HVMSG_NONE)
1103 hdr = (struct vmbus_channel_message_header *)msg_copy.u.payload;
1104 msgtype = hdr->msgtype;
1106 trace_vmbus_on_msg_dpc(hdr);
1108 if (msgtype >= CHANNELMSG_COUNT) {
1109 WARN_ONCE(1, "unknown msgtype=%d\n", msgtype);
1113 payload_size = msg_copy.header.payload_size;
1114 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT) {
1115 WARN_ONCE(1, "payload size is too large (%d)\n", payload_size);
1119 entry = &channel_message_table[msgtype];
1121 if (!entry->message_handler)
1124 if (payload_size < entry->min_payload_len) {
1125 WARN_ONCE(1, "message too short: msgtype=%d len=%d\n", msgtype, payload_size);
1129 if (entry->handler_type == VMHT_BLOCKING) {
1130 ctx = kmalloc(struct_size(ctx, msg.payload, payload_size), GFP_ATOMIC);
1134 INIT_WORK(&ctx->work, vmbus_onmessage_work);
1135 memcpy(&ctx->msg, &msg_copy, sizeof(msg->header) + payload_size);
1138 * The host can generate a rescind message while we
1139 * may still be handling the original offer. We deal with
1140 * this condition by relying on the synchronization provided
1141 * by offer_in_progress and by channel_mutex. See also the
1142 * inline comments in vmbus_onoffer_rescind().
1145 case CHANNELMSG_RESCIND_CHANNELOFFER:
1147 * If we are handling the rescind message;
1148 * schedule the work on the global work queue.
1150 * The OFFER message and the RESCIND message should
1151 * not be handled by the same serialized work queue,
1152 * because the OFFER handler may call vmbus_open(),
1153 * which tries to open the channel by sending an
1154 * OPEN_CHANNEL message to the host and waits for
1155 * the host's response; however, if the host has
1156 * rescinded the channel before it receives the
1157 * OPEN_CHANNEL message, the host just silently
1158 * ignores the OPEN_CHANNEL message; as a result,
1159 * the guest's OFFER handler hangs for ever, if we
1160 * handle the RESCIND message in the same serialized
1161 * work queue: the RESCIND handler can not start to
1162 * run before the OFFER handler finishes.
1164 if (vmbus_connection.ignore_any_offer_msg)
1166 queue_work(vmbus_connection.rescind_work_queue, &ctx->work);
1169 case CHANNELMSG_OFFERCHANNEL:
1171 * The host sends the offer message of a given channel
1172 * before sending the rescind message of the same
1173 * channel. These messages are sent to the guest's
1174 * connect CPU; the guest then starts processing them
1175 * in the tasklet handler on this CPU:
1179 * [vmbus_on_msg_dpc()]
1180 * atomic_inc() // CHANNELMSG_OFFERCHANNEL
1183 * [vmbus_on_msg_dpc()]
1184 * schedule_work() // CHANNELMSG_RESCIND_CHANNELOFFER
1186 * We rely on the memory-ordering properties of the
1187 * queue_work() and schedule_work() primitives, which
1188 * guarantee that the atomic increment will be visible
1189 * to the CPUs which will execute the offer & rescind
1190 * works by the time these works will start execution.
1192 if (vmbus_connection.ignore_any_offer_msg)
1194 atomic_inc(&vmbus_connection.offer_in_progress);
1198 queue_work(vmbus_connection.work_queue, &ctx->work);
1201 entry->message_handler(hdr);
1204 vmbus_signal_eom(msg, message_type);
1207 #ifdef CONFIG_PM_SLEEP
1209 * Fake RESCIND_CHANNEL messages to clean up hv_sock channels by force for
1210 * hibernation, because hv_sock connections can not persist across hibernation.
1212 static void vmbus_force_channel_rescinded(struct vmbus_channel *channel)
1214 struct onmessage_work_context *ctx;
1215 struct vmbus_channel_rescind_offer *rescind;
1217 WARN_ON(!is_hvsock_channel(channel));
1220 * Allocation size is small and the allocation should really not fail,
1221 * otherwise the state of the hv_sock connections ends up in limbo.
1223 ctx = kzalloc(sizeof(*ctx) + sizeof(*rescind),
1224 GFP_KERNEL | __GFP_NOFAIL);
1227 * So far, these are not really used by Linux. Just set them to the
1228 * reasonable values conforming to the definitions of the fields.
1230 ctx->msg.header.message_type = 1;
1231 ctx->msg.header.payload_size = sizeof(*rescind);
1233 /* These values are actually used by Linux. */
1234 rescind = (struct vmbus_channel_rescind_offer *)ctx->msg.payload;
1235 rescind->header.msgtype = CHANNELMSG_RESCIND_CHANNELOFFER;
1236 rescind->child_relid = channel->offermsg.child_relid;
1238 INIT_WORK(&ctx->work, vmbus_onmessage_work);
1240 queue_work(vmbus_connection.work_queue, &ctx->work);
1242 #endif /* CONFIG_PM_SLEEP */
1245 * Schedule all channels with events pending
1247 static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)
1249 unsigned long *recv_int_page;
1253 * The event page can be directly checked to get the id of
1254 * the channel that has the interrupt pending.
1256 void *page_addr = hv_cpu->synic_event_page;
1257 union hv_synic_event_flags *event
1258 = (union hv_synic_event_flags *)page_addr +
1261 maxbits = HV_EVENT_FLAGS_COUNT;
1262 recv_int_page = event->flags;
1264 if (unlikely(!recv_int_page))
1267 for_each_set_bit(relid, recv_int_page, maxbits) {
1268 void (*callback_fn)(void *context);
1269 struct vmbus_channel *channel;
1271 if (!sync_test_and_clear_bit(relid, recv_int_page))
1274 /* Special case - vmbus channel protocol msg */
1279 * Pairs with the kfree_rcu() in vmbus_chan_release().
1280 * Guarantees that the channel data structure doesn't
1281 * get freed while the channel pointer below is being
1286 /* Find channel based on relid */
1287 channel = relid2channel(relid);
1288 if (channel == NULL)
1289 goto sched_unlock_rcu;
1291 if (channel->rescind)
1292 goto sched_unlock_rcu;
1295 * Make sure that the ring buffer data structure doesn't get
1296 * freed while we dereference the ring buffer pointer. Test
1297 * for the channel's onchannel_callback being NULL within a
1298 * sched_lock critical section. See also the inline comments
1299 * in vmbus_reset_channel_cb().
1301 spin_lock(&channel->sched_lock);
1303 callback_fn = channel->onchannel_callback;
1304 if (unlikely(callback_fn == NULL))
1307 trace_vmbus_chan_sched(channel);
1309 ++channel->interrupts;
1311 switch (channel->callback_mode) {
1313 (*callback_fn)(channel->channel_callback_context);
1316 case HV_CALL_BATCHED:
1317 hv_begin_read(&channel->inbound);
1319 case HV_CALL_DIRECT:
1320 tasklet_schedule(&channel->callback_event);
1324 spin_unlock(&channel->sched_lock);
1330 static void vmbus_isr(void)
1332 struct hv_per_cpu_context *hv_cpu
1333 = this_cpu_ptr(hv_context.cpu_context);
1335 struct hv_message *msg;
1337 vmbus_chan_sched(hv_cpu);
1339 page_addr = hv_cpu->synic_message_page;
1340 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
1342 /* Check if there are actual msgs to be processed */
1343 if (msg->header.message_type != HVMSG_NONE) {
1344 if (msg->header.message_type == HVMSG_TIMER_EXPIRED) {
1346 vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED);
1348 tasklet_schedule(&hv_cpu->msg_dpc);
1351 add_interrupt_randomness(vmbus_interrupt);
1354 static irqreturn_t vmbus_percpu_isr(int irq, void *dev_id)
1361 * Callback from kmsg_dump. Grab as much as possible from the end of the kmsg
1362 * buffer and call into Hyper-V to transfer the data.
1364 static void hv_kmsg_dump(struct kmsg_dumper *dumper,
1365 enum kmsg_dump_reason reason)
1367 struct kmsg_dump_iter iter;
1368 size_t bytes_written;
1370 /* We are only interested in panics. */
1371 if ((reason != KMSG_DUMP_PANIC) || (!sysctl_record_panic_msg))
1375 * Write dump contents to the page. No need to synchronize; panic should
1376 * be single-threaded.
1378 kmsg_dump_rewind(&iter);
1379 kmsg_dump_get_buffer(&iter, false, hv_panic_page, HV_HYP_PAGE_SIZE,
1384 * P3 to contain the physical address of the panic page & P4 to
1385 * contain the size of the panic data in that page. Rest of the
1386 * registers are no-op when the NOTIFY_MSG flag is set.
1388 hv_set_register(HV_REGISTER_CRASH_P0, 0);
1389 hv_set_register(HV_REGISTER_CRASH_P1, 0);
1390 hv_set_register(HV_REGISTER_CRASH_P2, 0);
1391 hv_set_register(HV_REGISTER_CRASH_P3, virt_to_phys(hv_panic_page));
1392 hv_set_register(HV_REGISTER_CRASH_P4, bytes_written);
1395 * Let Hyper-V know there is crash data available along with
1396 * the panic message.
1398 hv_set_register(HV_REGISTER_CRASH_CTL,
1399 (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG));
1402 static struct kmsg_dumper hv_kmsg_dumper = {
1403 .dump = hv_kmsg_dump,
1406 static void hv_kmsg_dump_register(void)
1410 hv_panic_page = hv_alloc_hyperv_zeroed_page();
1411 if (!hv_panic_page) {
1412 pr_err("Hyper-V: panic message page memory allocation failed\n");
1416 ret = kmsg_dump_register(&hv_kmsg_dumper);
1418 pr_err("Hyper-V: kmsg dump register error 0x%x\n", ret);
1419 hv_free_hyperv_page((unsigned long)hv_panic_page);
1420 hv_panic_page = NULL;
1424 static struct ctl_table_header *hv_ctl_table_hdr;
1427 * sysctl option to allow the user to control whether kmsg data should be
1428 * reported to Hyper-V on panic.
1430 static struct ctl_table hv_ctl_table[] = {
1432 .procname = "hyperv_record_panic_msg",
1433 .data = &sysctl_record_panic_msg,
1434 .maxlen = sizeof(int),
1436 .proc_handler = proc_dointvec_minmax,
1437 .extra1 = SYSCTL_ZERO,
1438 .extra2 = SYSCTL_ONE
1443 static struct ctl_table hv_root_table[] = {
1445 .procname = "kernel",
1447 .child = hv_ctl_table
1453 * vmbus_bus_init -Main vmbus driver initialization routine.
1456 * - initialize the vmbus driver context
1457 * - invoke the vmbus hv main init routine
1458 * - retrieve the channel offers
1460 static int vmbus_bus_init(void)
1466 pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
1470 ret = bus_register(&hv_bus);
1475 * VMbus interrupts are best modeled as per-cpu interrupts. If
1476 * on an architecture with support for per-cpu IRQs (e.g. ARM64),
1477 * allocate a per-cpu IRQ using standard Linux kernel functionality.
1478 * If not on such an architecture (e.g., x86/x64), then rely on
1479 * code in the arch-specific portion of the code tree to connect
1480 * the VMbus interrupt handler.
1483 if (vmbus_irq == -1) {
1484 hv_setup_vmbus_handler(vmbus_isr);
1486 vmbus_evt = alloc_percpu(long);
1487 ret = request_percpu_irq(vmbus_irq, vmbus_percpu_isr,
1488 "Hyper-V VMbus", vmbus_evt);
1490 pr_err("Can't request Hyper-V VMbus IRQ %d, Err %d",
1492 free_percpu(vmbus_evt);
1497 ret = hv_synic_alloc();
1502 * Initialize the per-cpu interrupt state and stimer state.
1503 * Then connect to the host.
1505 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online",
1506 hv_synic_init, hv_synic_cleanup);
1509 hyperv_cpuhp_online = ret;
1511 ret = vmbus_connect();
1515 if (hv_is_isolation_supported())
1516 sysctl_record_panic_msg = 0;
1519 * Only register if the crash MSRs are available
1521 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
1522 u64 hyperv_crash_ctl;
1524 * Panic message recording (sysctl_record_panic_msg)
1525 * is enabled by default in non-isolated guests and
1526 * disabled by default in isolated guests; the panic
1527 * message recording won't be available in isolated
1528 * guests should the following registration fail.
1530 hv_ctl_table_hdr = register_sysctl_table(hv_root_table);
1531 if (!hv_ctl_table_hdr)
1532 pr_err("Hyper-V: sysctl table register error");
1535 * Register for panic kmsg callback only if the right
1536 * capability is supported by the hypervisor.
1538 hyperv_crash_ctl = hv_get_register(HV_REGISTER_CRASH_CTL);
1539 if (hyperv_crash_ctl & HV_CRASH_CTL_CRASH_NOTIFY_MSG)
1540 hv_kmsg_dump_register();
1542 register_die_notifier(&hyperv_die_block);
1546 * Always register the panic notifier because we need to unload
1547 * the VMbus channel connection to prevent any VMbus
1548 * activity after the VM panics.
1550 atomic_notifier_chain_register(&panic_notifier_list,
1551 &hyperv_panic_block);
1553 vmbus_request_offers();
1558 cpuhp_remove_state(hyperv_cpuhp_online);
1562 if (vmbus_irq == -1) {
1563 hv_remove_vmbus_handler();
1565 free_percpu_irq(vmbus_irq, vmbus_evt);
1566 free_percpu(vmbus_evt);
1569 bus_unregister(&hv_bus);
1570 unregister_sysctl_table(hv_ctl_table_hdr);
1571 hv_ctl_table_hdr = NULL;
1576 * __vmbus_child_driver_register() - Register a vmbus's driver
1577 * @hv_driver: Pointer to driver structure you want to register
1578 * @owner: owner module of the drv
1579 * @mod_name: module name string
1581 * Registers the given driver with Linux through the 'driver_register()' call
1582 * and sets up the hyper-v vmbus handling for this driver.
1583 * It will return the state of the 'driver_register()' call.
1586 int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
1590 pr_info("registering driver %s\n", hv_driver->name);
1592 ret = vmbus_exists();
1596 hv_driver->driver.name = hv_driver->name;
1597 hv_driver->driver.owner = owner;
1598 hv_driver->driver.mod_name = mod_name;
1599 hv_driver->driver.bus = &hv_bus;
1601 spin_lock_init(&hv_driver->dynids.lock);
1602 INIT_LIST_HEAD(&hv_driver->dynids.list);
1604 ret = driver_register(&hv_driver->driver);
1608 EXPORT_SYMBOL_GPL(__vmbus_driver_register);
1611 * vmbus_driver_unregister() - Unregister a vmbus's driver
1612 * @hv_driver: Pointer to driver structure you want to
1615 * Un-register the given driver that was previous registered with a call to
1616 * vmbus_driver_register()
1618 void vmbus_driver_unregister(struct hv_driver *hv_driver)
1620 pr_info("unregistering driver %s\n", hv_driver->name);
1622 if (!vmbus_exists()) {
1623 driver_unregister(&hv_driver->driver);
1624 vmbus_free_dynids(hv_driver);
1627 EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
1631 * Called when last reference to channel is gone.
1633 static void vmbus_chan_release(struct kobject *kobj)
1635 struct vmbus_channel *channel
1636 = container_of(kobj, struct vmbus_channel, kobj);
1638 kfree_rcu(channel, rcu);
1641 struct vmbus_chan_attribute {
1642 struct attribute attr;
1643 ssize_t (*show)(struct vmbus_channel *chan, char *buf);
1644 ssize_t (*store)(struct vmbus_channel *chan,
1645 const char *buf, size_t count);
1647 #define VMBUS_CHAN_ATTR(_name, _mode, _show, _store) \
1648 struct vmbus_chan_attribute chan_attr_##_name \
1649 = __ATTR(_name, _mode, _show, _store)
1650 #define VMBUS_CHAN_ATTR_RW(_name) \
1651 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RW(_name)
1652 #define VMBUS_CHAN_ATTR_RO(_name) \
1653 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RO(_name)
1654 #define VMBUS_CHAN_ATTR_WO(_name) \
1655 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_WO(_name)
1657 static ssize_t vmbus_chan_attr_show(struct kobject *kobj,
1658 struct attribute *attr, char *buf)
1660 const struct vmbus_chan_attribute *attribute
1661 = container_of(attr, struct vmbus_chan_attribute, attr);
1662 struct vmbus_channel *chan
1663 = container_of(kobj, struct vmbus_channel, kobj);
1665 if (!attribute->show)
1668 return attribute->show(chan, buf);
1671 static ssize_t vmbus_chan_attr_store(struct kobject *kobj,
1672 struct attribute *attr, const char *buf,
1675 const struct vmbus_chan_attribute *attribute
1676 = container_of(attr, struct vmbus_chan_attribute, attr);
1677 struct vmbus_channel *chan
1678 = container_of(kobj, struct vmbus_channel, kobj);
1680 if (!attribute->store)
1683 return attribute->store(chan, buf, count);
1686 static const struct sysfs_ops vmbus_chan_sysfs_ops = {
1687 .show = vmbus_chan_attr_show,
1688 .store = vmbus_chan_attr_store,
1691 static ssize_t out_mask_show(struct vmbus_channel *channel, char *buf)
1693 struct hv_ring_buffer_info *rbi = &channel->outbound;
1696 mutex_lock(&rbi->ring_buffer_mutex);
1697 if (!rbi->ring_buffer) {
1698 mutex_unlock(&rbi->ring_buffer_mutex);
1702 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1703 mutex_unlock(&rbi->ring_buffer_mutex);
1706 static VMBUS_CHAN_ATTR_RO(out_mask);
1708 static ssize_t in_mask_show(struct vmbus_channel *channel, char *buf)
1710 struct hv_ring_buffer_info *rbi = &channel->inbound;
1713 mutex_lock(&rbi->ring_buffer_mutex);
1714 if (!rbi->ring_buffer) {
1715 mutex_unlock(&rbi->ring_buffer_mutex);
1719 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1720 mutex_unlock(&rbi->ring_buffer_mutex);
1723 static VMBUS_CHAN_ATTR_RO(in_mask);
1725 static ssize_t read_avail_show(struct vmbus_channel *channel, char *buf)
1727 struct hv_ring_buffer_info *rbi = &channel->inbound;
1730 mutex_lock(&rbi->ring_buffer_mutex);
1731 if (!rbi->ring_buffer) {
1732 mutex_unlock(&rbi->ring_buffer_mutex);
1736 ret = sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi));
1737 mutex_unlock(&rbi->ring_buffer_mutex);
1740 static VMBUS_CHAN_ATTR_RO(read_avail);
1742 static ssize_t write_avail_show(struct vmbus_channel *channel, char *buf)
1744 struct hv_ring_buffer_info *rbi = &channel->outbound;
1747 mutex_lock(&rbi->ring_buffer_mutex);
1748 if (!rbi->ring_buffer) {
1749 mutex_unlock(&rbi->ring_buffer_mutex);
1753 ret = sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi));
1754 mutex_unlock(&rbi->ring_buffer_mutex);
1757 static VMBUS_CHAN_ATTR_RO(write_avail);
1759 static ssize_t target_cpu_show(struct vmbus_channel *channel, char *buf)
1761 return sprintf(buf, "%u\n", channel->target_cpu);
1763 static ssize_t target_cpu_store(struct vmbus_channel *channel,
1764 const char *buf, size_t count)
1766 u32 target_cpu, origin_cpu;
1767 ssize_t ret = count;
1769 if (vmbus_proto_version < VERSION_WIN10_V4_1)
1772 if (sscanf(buf, "%uu", &target_cpu) != 1)
1775 /* Validate target_cpu for the cpumask_test_cpu() operation below. */
1776 if (target_cpu >= nr_cpumask_bits)
1779 if (!cpumask_test_cpu(target_cpu, housekeeping_cpumask(HK_TYPE_MANAGED_IRQ)))
1782 /* No CPUs should come up or down during this. */
1785 if (!cpu_online(target_cpu)) {
1791 * Synchronizes target_cpu_store() and channel closure:
1793 * { Initially: state = CHANNEL_OPENED }
1797 * [target_cpu_store()] [vmbus_disconnect_ring()]
1799 * LOCK channel_mutex LOCK channel_mutex
1800 * LOAD r1 = state LOAD r2 = state
1801 * IF (r1 == CHANNEL_OPENED) IF (r2 == CHANNEL_OPENED)
1802 * SEND MODIFYCHANNEL STORE state = CHANNEL_OPEN
1803 * [...] SEND CLOSECHANNEL
1804 * UNLOCK channel_mutex UNLOCK channel_mutex
1806 * Forbids: r1 == r2 == CHANNEL_OPENED (i.e., CPU1's LOCK precedes
1807 * CPU2's LOCK) && CPU2's SEND precedes CPU1's SEND
1809 * Note. The host processes the channel messages "sequentially", in
1810 * the order in which they are received on a per-partition basis.
1812 mutex_lock(&vmbus_connection.channel_mutex);
1815 * Hyper-V will ignore MODIFYCHANNEL messages for "non-open" channels;
1816 * avoid sending the message and fail here for such channels.
1818 if (channel->state != CHANNEL_OPENED_STATE) {
1820 goto cpu_store_unlock;
1823 origin_cpu = channel->target_cpu;
1824 if (target_cpu == origin_cpu)
1825 goto cpu_store_unlock;
1827 if (vmbus_send_modifychannel(channel,
1828 hv_cpu_number_to_vp_number(target_cpu))) {
1830 goto cpu_store_unlock;
1834 * For version before VERSION_WIN10_V5_3, the following warning holds:
1836 * Warning. At this point, there is *no* guarantee that the host will
1837 * have successfully processed the vmbus_send_modifychannel() request.
1838 * See the header comment of vmbus_send_modifychannel() for more info.
1840 * Lags in the processing of the above vmbus_send_modifychannel() can
1841 * result in missed interrupts if the "old" target CPU is taken offline
1842 * before Hyper-V starts sending interrupts to the "new" target CPU.
1843 * But apart from this offlining scenario, the code tolerates such
1844 * lags. It will function correctly even if a channel interrupt comes
1845 * in on a CPU that is different from the channel target_cpu value.
1848 channel->target_cpu = target_cpu;
1850 /* See init_vp_index(). */
1851 if (hv_is_perf_channel(channel))
1852 hv_update_allocated_cpus(origin_cpu, target_cpu);
1854 /* Currently set only for storvsc channels. */
1855 if (channel->change_target_cpu_callback) {
1856 (*channel->change_target_cpu_callback)(channel,
1857 origin_cpu, target_cpu);
1861 mutex_unlock(&vmbus_connection.channel_mutex);
1865 static VMBUS_CHAN_ATTR(cpu, 0644, target_cpu_show, target_cpu_store);
1867 static ssize_t channel_pending_show(struct vmbus_channel *channel,
1870 return sprintf(buf, "%d\n",
1871 channel_pending(channel,
1872 vmbus_connection.monitor_pages[1]));
1874 static VMBUS_CHAN_ATTR(pending, 0444, channel_pending_show, NULL);
1876 static ssize_t channel_latency_show(struct vmbus_channel *channel,
1879 return sprintf(buf, "%d\n",
1880 channel_latency(channel,
1881 vmbus_connection.monitor_pages[1]));
1883 static VMBUS_CHAN_ATTR(latency, 0444, channel_latency_show, NULL);
1885 static ssize_t channel_interrupts_show(struct vmbus_channel *channel, char *buf)
1887 return sprintf(buf, "%llu\n", channel->interrupts);
1889 static VMBUS_CHAN_ATTR(interrupts, 0444, channel_interrupts_show, NULL);
1891 static ssize_t channel_events_show(struct vmbus_channel *channel, char *buf)
1893 return sprintf(buf, "%llu\n", channel->sig_events);
1895 static VMBUS_CHAN_ATTR(events, 0444, channel_events_show, NULL);
1897 static ssize_t channel_intr_in_full_show(struct vmbus_channel *channel,
1900 return sprintf(buf, "%llu\n",
1901 (unsigned long long)channel->intr_in_full);
1903 static VMBUS_CHAN_ATTR(intr_in_full, 0444, channel_intr_in_full_show, NULL);
1905 static ssize_t channel_intr_out_empty_show(struct vmbus_channel *channel,
1908 return sprintf(buf, "%llu\n",
1909 (unsigned long long)channel->intr_out_empty);
1911 static VMBUS_CHAN_ATTR(intr_out_empty, 0444, channel_intr_out_empty_show, NULL);
1913 static ssize_t channel_out_full_first_show(struct vmbus_channel *channel,
1916 return sprintf(buf, "%llu\n",
1917 (unsigned long long)channel->out_full_first);
1919 static VMBUS_CHAN_ATTR(out_full_first, 0444, channel_out_full_first_show, NULL);
1921 static ssize_t channel_out_full_total_show(struct vmbus_channel *channel,
1924 return sprintf(buf, "%llu\n",
1925 (unsigned long long)channel->out_full_total);
1927 static VMBUS_CHAN_ATTR(out_full_total, 0444, channel_out_full_total_show, NULL);
1929 static ssize_t subchannel_monitor_id_show(struct vmbus_channel *channel,
1932 return sprintf(buf, "%u\n", channel->offermsg.monitorid);
1934 static VMBUS_CHAN_ATTR(monitor_id, 0444, subchannel_monitor_id_show, NULL);
1936 static ssize_t subchannel_id_show(struct vmbus_channel *channel,
1939 return sprintf(buf, "%u\n",
1940 channel->offermsg.offer.sub_channel_index);
1942 static VMBUS_CHAN_ATTR_RO(subchannel_id);
1944 static struct attribute *vmbus_chan_attrs[] = {
1945 &chan_attr_out_mask.attr,
1946 &chan_attr_in_mask.attr,
1947 &chan_attr_read_avail.attr,
1948 &chan_attr_write_avail.attr,
1949 &chan_attr_cpu.attr,
1950 &chan_attr_pending.attr,
1951 &chan_attr_latency.attr,
1952 &chan_attr_interrupts.attr,
1953 &chan_attr_events.attr,
1954 &chan_attr_intr_in_full.attr,
1955 &chan_attr_intr_out_empty.attr,
1956 &chan_attr_out_full_first.attr,
1957 &chan_attr_out_full_total.attr,
1958 &chan_attr_monitor_id.attr,
1959 &chan_attr_subchannel_id.attr,
1964 * Channel-level attribute_group callback function. Returns the permission for
1965 * each attribute, and returns 0 if an attribute is not visible.
1967 static umode_t vmbus_chan_attr_is_visible(struct kobject *kobj,
1968 struct attribute *attr, int idx)
1970 const struct vmbus_channel *channel =
1971 container_of(kobj, struct vmbus_channel, kobj);
1973 /* Hide the monitor attributes if the monitor mechanism is not used. */
1974 if (!channel->offermsg.monitor_allocated &&
1975 (attr == &chan_attr_pending.attr ||
1976 attr == &chan_attr_latency.attr ||
1977 attr == &chan_attr_monitor_id.attr))
1983 static struct attribute_group vmbus_chan_group = {
1984 .attrs = vmbus_chan_attrs,
1985 .is_visible = vmbus_chan_attr_is_visible
1988 static struct kobj_type vmbus_chan_ktype = {
1989 .sysfs_ops = &vmbus_chan_sysfs_ops,
1990 .release = vmbus_chan_release,
1994 * vmbus_add_channel_kobj - setup a sub-directory under device/channels
1996 int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
1998 const struct device *device = &dev->device;
1999 struct kobject *kobj = &channel->kobj;
2000 u32 relid = channel->offermsg.child_relid;
2003 kobj->kset = dev->channels_kset;
2004 ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
2011 ret = sysfs_create_group(kobj, &vmbus_chan_group);
2015 * The calling functions' error handling paths will cleanup the
2016 * empty channel directory.
2019 dev_err(device, "Unable to set up channel sysfs files\n");
2023 kobject_uevent(kobj, KOBJ_ADD);
2029 * vmbus_remove_channel_attr_group - remove the channel's attribute group
2031 void vmbus_remove_channel_attr_group(struct vmbus_channel *channel)
2033 sysfs_remove_group(&channel->kobj, &vmbus_chan_group);
2037 * vmbus_device_create - Creates and registers a new child device
2040 struct hv_device *vmbus_device_create(const guid_t *type,
2041 const guid_t *instance,
2042 struct vmbus_channel *channel)
2044 struct hv_device *child_device_obj;
2046 child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
2047 if (!child_device_obj) {
2048 pr_err("Unable to allocate device object for child device\n");
2052 child_device_obj->channel = channel;
2053 guid_copy(&child_device_obj->dev_type, type);
2054 guid_copy(&child_device_obj->dev_instance, instance);
2055 child_device_obj->vendor_id = 0x1414; /* MSFT vendor ID */
2057 return child_device_obj;
2061 * vmbus_device_register - Register the child device
2063 int vmbus_device_register(struct hv_device *child_device_obj)
2065 struct kobject *kobj = &child_device_obj->device.kobj;
2068 dev_set_name(&child_device_obj->device, "%pUl",
2069 &child_device_obj->channel->offermsg.offer.if_instance);
2071 child_device_obj->device.bus = &hv_bus;
2072 child_device_obj->device.parent = &hv_acpi_dev->dev;
2073 child_device_obj->device.release = vmbus_device_release;
2075 child_device_obj->device.dma_parms = &child_device_obj->dma_parms;
2076 child_device_obj->device.dma_mask = &child_device_obj->dma_mask;
2077 dma_set_mask(&child_device_obj->device, DMA_BIT_MASK(64));
2080 * Register with the LDM. This will kick off the driver/device
2081 * binding...which will eventually call vmbus_match() and vmbus_probe()
2083 ret = device_register(&child_device_obj->device);
2085 pr_err("Unable to register child device\n");
2089 child_device_obj->channels_kset = kset_create_and_add("channels",
2091 if (!child_device_obj->channels_kset) {
2093 goto err_dev_unregister;
2096 ret = vmbus_add_channel_kobj(child_device_obj,
2097 child_device_obj->channel);
2099 pr_err("Unable to register primary channeln");
2100 goto err_kset_unregister;
2102 hv_debug_add_dev_dir(child_device_obj);
2106 err_kset_unregister:
2107 kset_unregister(child_device_obj->channels_kset);
2110 device_unregister(&child_device_obj->device);
2115 * vmbus_device_unregister - Remove the specified child device
2118 void vmbus_device_unregister(struct hv_device *device_obj)
2120 pr_debug("child device %s unregistered\n",
2121 dev_name(&device_obj->device));
2123 kset_unregister(device_obj->channels_kset);
2126 * Kick off the process of unregistering the device.
2127 * This will call vmbus_remove() and eventually vmbus_device_release()
2129 device_unregister(&device_obj->device);
2134 * VMBUS is an acpi enumerated device. Get the information we
2137 #define VTPM_BASE_ADDRESS 0xfed40000
2138 static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
2140 resource_size_t start = 0;
2141 resource_size_t end = 0;
2142 struct resource *new_res;
2143 struct resource **old_res = &hyperv_mmio;
2144 struct resource **prev_res = NULL;
2147 switch (res->type) {
2150 * "Address" descriptors are for bus windows. Ignore
2151 * "memory" descriptors, which are for registers on
2154 case ACPI_RESOURCE_TYPE_ADDRESS32:
2155 start = res->data.address32.address.minimum;
2156 end = res->data.address32.address.maximum;
2159 case ACPI_RESOURCE_TYPE_ADDRESS64:
2160 start = res->data.address64.address.minimum;
2161 end = res->data.address64.address.maximum;
2165 * The IRQ information is needed only on ARM64, which Hyper-V
2166 * sets up in the extended format. IRQ information is present
2167 * on x86/x64 in the non-extended format but it is not used by
2168 * Linux. So don't bother checking for the non-extended format.
2170 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
2171 if (!acpi_dev_resource_interrupt(res, 0, &r)) {
2172 pr_err("Unable to parse Hyper-V ACPI interrupt\n");
2175 /* ARM64 INTID for VMbus */
2176 vmbus_interrupt = res->data.extended_irq.interrupts[0];
2177 /* Linux IRQ number */
2178 vmbus_irq = r.start;
2182 /* Unused resource type */
2187 * Ignore ranges that are below 1MB, as they're not
2188 * necessary or useful here.
2193 new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC);
2195 return AE_NO_MEMORY;
2197 /* If this range overlaps the virtual TPM, truncate it. */
2198 if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS)
2199 end = VTPM_BASE_ADDRESS;
2201 new_res->name = "hyperv mmio";
2202 new_res->flags = IORESOURCE_MEM;
2203 new_res->start = start;
2207 * If two ranges are adjacent, merge them.
2215 if (((*old_res)->end + 1) == new_res->start) {
2216 (*old_res)->end = new_res->end;
2221 if ((*old_res)->start == new_res->end + 1) {
2222 (*old_res)->start = new_res->start;
2227 if ((*old_res)->start > new_res->end) {
2228 new_res->sibling = *old_res;
2230 (*prev_res)->sibling = new_res;
2236 old_res = &(*old_res)->sibling;
2243 static int vmbus_acpi_remove(struct acpi_device *device)
2245 struct resource *cur_res;
2246 struct resource *next_res;
2250 __release_region(hyperv_mmio, fb_mmio->start,
2251 resource_size(fb_mmio));
2255 for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) {
2256 next_res = cur_res->sibling;
2264 static void vmbus_reserve_fb(void)
2266 resource_size_t start = 0, size;
2267 struct pci_dev *pdev;
2269 if (efi_enabled(EFI_BOOT)) {
2270 /* Gen2 VM: get FB base from EFI framebuffer */
2271 start = screen_info.lfb_base;
2272 size = max_t(__u32, screen_info.lfb_size, 0x800000);
2274 /* Gen1 VM: get FB base from PCI */
2275 pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
2276 PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
2280 if (pdev->resource[0].flags & IORESOURCE_MEM) {
2281 start = pci_resource_start(pdev, 0);
2282 size = pci_resource_len(pdev, 0);
2286 * Release the PCI device so hyperv_drm or hyperv_fb driver can
2296 * Make a claim for the frame buffer in the resource tree under the
2297 * first node, which will be the one below 4GB. The length seems to
2298 * be underreported, particularly in a Generation 1 VM. So start out
2299 * reserving a larger area and make it smaller until it succeeds.
2301 for (; !fb_mmio && (size >= 0x100000); size >>= 1)
2302 fb_mmio = __request_region(hyperv_mmio, start, size, fb_mmio_name, 0);
2306 * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
2307 * @new: If successful, supplied a pointer to the
2308 * allocated MMIO space.
2309 * @device_obj: Identifies the caller
2310 * @min: Minimum guest physical address of the
2312 * @max: Maximum guest physical address
2313 * @size: Size of the range to be allocated
2314 * @align: Alignment of the range to be allocated
2315 * @fb_overlap_ok: Whether this allocation can be allowed
2316 * to overlap the video frame buffer.
2318 * This function walks the resources granted to VMBus by the
2319 * _CRS object in the ACPI namespace underneath the parent
2320 * "bridge" whether that's a root PCI bus in the Generation 1
2321 * case or a Module Device in the Generation 2 case. It then
2322 * attempts to allocate from the global MMIO pool in a way that
2323 * matches the constraints supplied in these parameters and by
2326 * Return: 0 on success, -errno on failure
2328 int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
2329 resource_size_t min, resource_size_t max,
2330 resource_size_t size, resource_size_t align,
2333 struct resource *iter, *shadow;
2334 resource_size_t range_min, range_max, start, end;
2335 const char *dev_n = dev_name(&device_obj->device);
2339 mutex_lock(&hyperv_mmio_lock);
2342 * If overlaps with frame buffers are allowed, then first attempt to
2343 * make the allocation from within the reserved region. Because it
2344 * is already reserved, no shadow allocation is necessary.
2346 if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) &&
2347 !(max < fb_mmio->start)) {
2349 range_min = fb_mmio->start;
2350 range_max = fb_mmio->end;
2351 start = (range_min + align - 1) & ~(align - 1);
2352 for (; start + size - 1 <= range_max; start += align) {
2353 *new = request_mem_region_exclusive(start, size, dev_n);
2361 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
2362 if ((iter->start >= max) || (iter->end <= min))
2365 range_min = iter->start;
2366 range_max = iter->end;
2367 start = (range_min + align - 1) & ~(align - 1);
2368 for (; start + size - 1 <= range_max; start += align) {
2369 end = start + size - 1;
2371 /* Skip the whole fb_mmio region if not fb_overlap_ok */
2372 if (!fb_overlap_ok && fb_mmio &&
2373 (((start >= fb_mmio->start) && (start <= fb_mmio->end)) ||
2374 ((end >= fb_mmio->start) && (end <= fb_mmio->end))))
2377 shadow = __request_region(iter, start, size, NULL,
2382 *new = request_mem_region_exclusive(start, size, dev_n);
2384 shadow->name = (char *)*new;
2389 __release_region(iter, start, size);
2394 mutex_unlock(&hyperv_mmio_lock);
2397 EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
2400 * vmbus_free_mmio() - Free a memory-mapped I/O range.
2401 * @start: Base address of region to release.
2402 * @size: Size of the range to be allocated
2404 * This function releases anything requested by
2405 * vmbus_mmio_allocate().
2407 void vmbus_free_mmio(resource_size_t start, resource_size_t size)
2409 struct resource *iter;
2411 mutex_lock(&hyperv_mmio_lock);
2412 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
2413 if ((iter->start >= start + size) || (iter->end <= start))
2416 __release_region(iter, start, size);
2418 release_mem_region(start, size);
2419 mutex_unlock(&hyperv_mmio_lock);
2422 EXPORT_SYMBOL_GPL(vmbus_free_mmio);
2424 static int vmbus_acpi_add(struct acpi_device *device)
2427 int ret_val = -ENODEV;
2428 struct acpi_device *ancestor;
2430 hv_acpi_dev = device;
2433 * Older versions of Hyper-V for ARM64 fail to include the _CCA
2434 * method on the top level VMbus device in the DSDT. But devices
2435 * are hardware coherent in all current Hyper-V use cases, so fix
2436 * up the ACPI device to behave as if _CCA is present and indicates
2437 * hardware coherence.
2439 ACPI_COMPANION_SET(&device->dev, device);
2440 if (IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED) &&
2441 device_get_dma_attr(&device->dev) == DEV_DMA_NOT_SUPPORTED) {
2442 pr_info("No ACPI _CCA found; assuming coherent device I/O\n");
2443 device->flags.cca_seen = true;
2444 device->flags.coherent_dma = true;
2447 result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
2448 vmbus_walk_resources, NULL);
2450 if (ACPI_FAILURE(result))
2453 * Some ancestor of the vmbus acpi device (Gen1 or Gen2
2454 * firmware) is the VMOD that has the mmio ranges. Get that.
2456 for (ancestor = acpi_dev_parent(device); ancestor;
2457 ancestor = acpi_dev_parent(ancestor)) {
2458 result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
2459 vmbus_walk_resources, NULL);
2461 if (ACPI_FAILURE(result))
2471 complete(&probe_event);
2473 vmbus_acpi_remove(device);
2477 #ifdef CONFIG_PM_SLEEP
2478 static int vmbus_bus_suspend(struct device *dev)
2480 struct hv_per_cpu_context *hv_cpu = per_cpu_ptr(
2481 hv_context.cpu_context, VMBUS_CONNECT_CPU);
2482 struct vmbus_channel *channel, *sc;
2484 tasklet_disable(&hv_cpu->msg_dpc);
2485 vmbus_connection.ignore_any_offer_msg = true;
2486 /* The tasklet_enable() takes care of providing a memory barrier */
2487 tasklet_enable(&hv_cpu->msg_dpc);
2489 /* Drain all the workqueues as we are in suspend */
2490 drain_workqueue(vmbus_connection.rescind_work_queue);
2491 drain_workqueue(vmbus_connection.work_queue);
2492 drain_workqueue(vmbus_connection.handle_primary_chan_wq);
2493 drain_workqueue(vmbus_connection.handle_sub_chan_wq);
2495 mutex_lock(&vmbus_connection.channel_mutex);
2496 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
2497 if (!is_hvsock_channel(channel))
2500 vmbus_force_channel_rescinded(channel);
2502 mutex_unlock(&vmbus_connection.channel_mutex);
2505 * Wait until all the sub-channels and hv_sock channels have been
2506 * cleaned up. Sub-channels should be destroyed upon suspend, otherwise
2507 * they would conflict with the new sub-channels that will be created
2508 * in the resume path. hv_sock channels should also be destroyed, but
2509 * a hv_sock channel of an established hv_sock connection can not be
2510 * really destroyed since it may still be referenced by the userspace
2511 * application, so we just force the hv_sock channel to be rescinded
2512 * by vmbus_force_channel_rescinded(), and the userspace application
2513 * will thoroughly destroy the channel after hibernation.
2515 * Note: the counter nr_chan_close_on_suspend may never go above 0 if
2516 * the VM has no sub-channel and hv_sock channel, e.g. a 1-vCPU VM.
2518 if (atomic_read(&vmbus_connection.nr_chan_close_on_suspend) > 0)
2519 wait_for_completion(&vmbus_connection.ready_for_suspend_event);
2521 if (atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) != 0) {
2522 pr_err("Can not suspend due to a previous failed resuming\n");
2526 mutex_lock(&vmbus_connection.channel_mutex);
2528 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
2530 * Remove the channel from the array of channels and invalidate
2531 * the channel's relid. Upon resume, vmbus_onoffer() will fix
2532 * up the relid (and other fields, if necessary) and add the
2533 * channel back to the array.
2535 vmbus_channel_unmap_relid(channel);
2536 channel->offermsg.child_relid = INVALID_RELID;
2538 if (is_hvsock_channel(channel)) {
2539 if (!channel->rescind) {
2540 pr_err("hv_sock channel not rescinded!\n");
2546 list_for_each_entry(sc, &channel->sc_list, sc_list) {
2547 pr_err("Sub-channel not deleted!\n");
2551 atomic_inc(&vmbus_connection.nr_chan_fixup_on_resume);
2554 mutex_unlock(&vmbus_connection.channel_mutex);
2556 vmbus_initiate_unload(false);
2558 /* Reset the event for the next resume. */
2559 reinit_completion(&vmbus_connection.ready_for_resume_event);
2564 static int vmbus_bus_resume(struct device *dev)
2566 struct vmbus_channel_msginfo *msginfo;
2570 vmbus_connection.ignore_any_offer_msg = false;
2573 * We only use the 'vmbus_proto_version', which was in use before
2574 * hibernation, to re-negotiate with the host.
2576 if (!vmbus_proto_version) {
2577 pr_err("Invalid proto version = 0x%x\n", vmbus_proto_version);
2581 msgsize = sizeof(*msginfo) +
2582 sizeof(struct vmbus_channel_initiate_contact);
2584 msginfo = kzalloc(msgsize, GFP_KERNEL);
2586 if (msginfo == NULL)
2589 ret = vmbus_negotiate_version(msginfo, vmbus_proto_version);
2596 WARN_ON(atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) == 0);
2598 vmbus_request_offers();
2600 if (wait_for_completion_timeout(
2601 &vmbus_connection.ready_for_resume_event, 10 * HZ) == 0)
2602 pr_err("Some vmbus device is missing after suspending?\n");
2604 /* Reset the event for the next suspend. */
2605 reinit_completion(&vmbus_connection.ready_for_suspend_event);
2610 #define vmbus_bus_suspend NULL
2611 #define vmbus_bus_resume NULL
2612 #endif /* CONFIG_PM_SLEEP */
2614 static const struct acpi_device_id vmbus_acpi_device_ids[] = {
2619 MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
2622 * Note: we must use the "no_irq" ops, otherwise hibernation can not work with
2623 * PCI device assignment, because "pci_dev_pm_ops" uses the "noirq" ops: in
2624 * the resume path, the pci "noirq" restore op runs before "non-noirq" op (see
2625 * resume_target_kernel() -> dpm_resume_start(), and hibernation_restore() ->
2626 * dpm_resume_end()). This means vmbus_bus_resume() and the pci-hyperv's
2627 * resume callback must also run via the "noirq" ops.
2629 * Set suspend_noirq/resume_noirq to NULL for Suspend-to-Idle: see the comment
2630 * earlier in this file before vmbus_pm.
2633 static const struct dev_pm_ops vmbus_bus_pm = {
2634 .suspend_noirq = NULL,
2635 .resume_noirq = NULL,
2636 .freeze_noirq = vmbus_bus_suspend,
2637 .thaw_noirq = vmbus_bus_resume,
2638 .poweroff_noirq = vmbus_bus_suspend,
2639 .restore_noirq = vmbus_bus_resume
2642 static struct acpi_driver vmbus_acpi_driver = {
2644 .ids = vmbus_acpi_device_ids,
2646 .add = vmbus_acpi_add,
2647 .remove = vmbus_acpi_remove,
2649 .drv.pm = &vmbus_bus_pm,
2652 static void hv_kexec_handler(void)
2654 hv_stimer_global_cleanup();
2655 vmbus_initiate_unload(false);
2656 /* Make sure conn_state is set as hv_synic_cleanup checks for it */
2658 cpuhp_remove_state(hyperv_cpuhp_online);
2661 static void hv_crash_handler(struct pt_regs *regs)
2665 vmbus_initiate_unload(true);
2667 * In crash handler we can't schedule synic cleanup for all CPUs,
2668 * doing the cleanup for current CPU only. This should be sufficient
2671 cpu = smp_processor_id();
2672 hv_stimer_cleanup(cpu);
2673 hv_synic_disable_regs(cpu);
2676 static int hv_synic_suspend(void)
2679 * When we reach here, all the non-boot CPUs have been offlined.
2680 * If we're in a legacy configuration where stimer Direct Mode is
2681 * not enabled, the stimers on the non-boot CPUs have been unbound
2682 * in hv_synic_cleanup() -> hv_stimer_legacy_cleanup() ->
2683 * hv_stimer_cleanup() -> clockevents_unbind_device().
2685 * hv_synic_suspend() only runs on CPU0 with interrupts disabled.
2686 * Here we do not call hv_stimer_legacy_cleanup() on CPU0 because:
2687 * 1) it's unnecessary as interrupts remain disabled between
2688 * syscore_suspend() and syscore_resume(): see create_image() and
2689 * resume_target_kernel()
2690 * 2) the stimer on CPU0 is automatically disabled later by
2691 * syscore_suspend() -> timekeeping_suspend() -> tick_suspend() -> ...
2692 * -> clockevents_shutdown() -> ... -> hv_ce_shutdown()
2693 * 3) a warning would be triggered if we call
2694 * clockevents_unbind_device(), which may sleep, in an
2695 * interrupts-disabled context.
2698 hv_synic_disable_regs(0);
2703 static void hv_synic_resume(void)
2705 hv_synic_enable_regs(0);
2708 * Note: we don't need to call hv_stimer_init(0), because the timer
2709 * on CPU0 is not unbound in hv_synic_suspend(), and the timer is
2710 * automatically re-enabled in timekeeping_resume().
2714 /* The callbacks run only on CPU0, with irqs_disabled. */
2715 static struct syscore_ops hv_synic_syscore_ops = {
2716 .suspend = hv_synic_suspend,
2717 .resume = hv_synic_resume,
2720 static int __init hv_acpi_init(void)
2724 if (!hv_is_hyperv_initialized())
2727 if (hv_root_partition)
2730 init_completion(&probe_event);
2733 * Get ACPI resources first.
2735 ret = acpi_bus_register_driver(&vmbus_acpi_driver);
2740 t = wait_for_completion_timeout(&probe_event, 5*HZ);
2747 * If we're on an architecture with a hardcoded hypervisor
2748 * vector (i.e. x86/x64), override the VMbus interrupt found
2749 * in the ACPI tables. Ensure vmbus_irq is not set since the
2750 * normal Linux IRQ mechanism is not used in this case.
2752 #ifdef HYPERVISOR_CALLBACK_VECTOR
2753 vmbus_interrupt = HYPERVISOR_CALLBACK_VECTOR;
2759 ret = vmbus_bus_init();
2763 hv_setup_kexec_handler(hv_kexec_handler);
2764 hv_setup_crash_handler(hv_crash_handler);
2766 register_syscore_ops(&hv_synic_syscore_ops);
2771 acpi_bus_unregister_driver(&vmbus_acpi_driver);
2776 static void __exit vmbus_exit(void)
2780 unregister_syscore_ops(&hv_synic_syscore_ops);
2782 hv_remove_kexec_handler();
2783 hv_remove_crash_handler();
2784 vmbus_connection.conn_state = DISCONNECTED;
2785 hv_stimer_global_cleanup();
2787 if (vmbus_irq == -1) {
2788 hv_remove_vmbus_handler();
2790 free_percpu_irq(vmbus_irq, vmbus_evt);
2791 free_percpu(vmbus_evt);
2793 for_each_online_cpu(cpu) {
2794 struct hv_per_cpu_context *hv_cpu
2795 = per_cpu_ptr(hv_context.cpu_context, cpu);
2797 tasklet_kill(&hv_cpu->msg_dpc);
2799 hv_debug_rm_all_dir();
2801 vmbus_free_channels();
2802 kfree(vmbus_connection.channels);
2804 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
2805 kmsg_dump_unregister(&hv_kmsg_dumper);
2806 unregister_die_notifier(&hyperv_die_block);
2810 * The panic notifier is always registered, hence we should
2811 * also unconditionally unregister it here as well.
2813 atomic_notifier_chain_unregister(&panic_notifier_list,
2814 &hyperv_panic_block);
2816 free_page((unsigned long)hv_panic_page);
2817 unregister_sysctl_table(hv_ctl_table_hdr);
2818 hv_ctl_table_hdr = NULL;
2819 bus_unregister(&hv_bus);
2821 cpuhp_remove_state(hyperv_cpuhp_online);
2823 acpi_bus_unregister_driver(&vmbus_acpi_driver);
2827 MODULE_LICENSE("GPL");
2828 MODULE_DESCRIPTION("Microsoft Hyper-V VMBus Driver");
2830 subsys_initcall(hv_acpi_init);
2831 module_exit(vmbus_exit);