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/task_stack.h>
26 #include <asm/mshyperv.h>
27 #include <linux/notifier.h>
28 #include <linux/ptrace.h>
29 #include <linux/screen_info.h>
30 #include <linux/kdebug.h>
31 #include <linux/efi.h>
32 #include <linux/random.h>
33 #include <clocksource/hyperv_timer.h>
34 #include "hyperv_vmbus.h"
37 struct list_head node;
38 struct hv_vmbus_device_id id;
41 static struct acpi_device *hv_acpi_dev;
43 static struct completion probe_event;
45 static int hyperv_cpuhp_online;
47 static void *hv_panic_page;
49 static int hyperv_panic_event(struct notifier_block *nb, unsigned long val,
54 regs = current_pt_regs();
56 hyperv_report_panic(regs, val);
60 static int hyperv_die_event(struct notifier_block *nb, unsigned long val,
63 struct die_args *die = (struct die_args *)args;
64 struct pt_regs *regs = die->regs;
66 hyperv_report_panic(regs, val);
70 static struct notifier_block hyperv_die_block = {
71 .notifier_call = hyperv_die_event,
73 static struct notifier_block hyperv_panic_block = {
74 .notifier_call = hyperv_panic_event,
77 static const char *fb_mmio_name = "fb_range";
78 static struct resource *fb_mmio;
79 static struct resource *hyperv_mmio;
80 static DEFINE_SEMAPHORE(hyperv_mmio_lock);
82 static int vmbus_exists(void)
84 if (hv_acpi_dev == NULL)
90 #define VMBUS_ALIAS_LEN ((sizeof((struct hv_vmbus_device_id *)0)->guid) * 2)
91 static void print_alias_name(struct hv_device *hv_dev, char *alias_name)
94 for (i = 0; i < VMBUS_ALIAS_LEN; i += 2)
95 sprintf(&alias_name[i], "%02x", hv_dev->dev_type.b[i/2]);
98 static u8 channel_monitor_group(const struct vmbus_channel *channel)
100 return (u8)channel->offermsg.monitorid / 32;
103 static u8 channel_monitor_offset(const struct vmbus_channel *channel)
105 return (u8)channel->offermsg.monitorid % 32;
108 static u32 channel_pending(const struct vmbus_channel *channel,
109 const struct hv_monitor_page *monitor_page)
111 u8 monitor_group = channel_monitor_group(channel);
113 return monitor_page->trigger_group[monitor_group].pending;
116 static u32 channel_latency(const struct vmbus_channel *channel,
117 const struct hv_monitor_page *monitor_page)
119 u8 monitor_group = channel_monitor_group(channel);
120 u8 monitor_offset = channel_monitor_offset(channel);
122 return monitor_page->latency[monitor_group][monitor_offset];
125 static u32 channel_conn_id(struct vmbus_channel *channel,
126 struct hv_monitor_page *monitor_page)
128 u8 monitor_group = channel_monitor_group(channel);
129 u8 monitor_offset = channel_monitor_offset(channel);
130 return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
133 static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
136 struct hv_device *hv_dev = device_to_hv_device(dev);
138 if (!hv_dev->channel)
140 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
142 static DEVICE_ATTR_RO(id);
144 static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
147 struct hv_device *hv_dev = device_to_hv_device(dev);
149 if (!hv_dev->channel)
151 return sprintf(buf, "%d\n", hv_dev->channel->state);
153 static DEVICE_ATTR_RO(state);
155 static ssize_t monitor_id_show(struct device *dev,
156 struct device_attribute *dev_attr, char *buf)
158 struct hv_device *hv_dev = device_to_hv_device(dev);
160 if (!hv_dev->channel)
162 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
164 static DEVICE_ATTR_RO(monitor_id);
166 static ssize_t class_id_show(struct device *dev,
167 struct device_attribute *dev_attr, char *buf)
169 struct hv_device *hv_dev = device_to_hv_device(dev);
171 if (!hv_dev->channel)
173 return sprintf(buf, "{%pUl}\n",
174 hv_dev->channel->offermsg.offer.if_type.b);
176 static DEVICE_ATTR_RO(class_id);
178 static ssize_t device_id_show(struct device *dev,
179 struct device_attribute *dev_attr, char *buf)
181 struct hv_device *hv_dev = device_to_hv_device(dev);
183 if (!hv_dev->channel)
185 return sprintf(buf, "{%pUl}\n",
186 hv_dev->channel->offermsg.offer.if_instance.b);
188 static DEVICE_ATTR_RO(device_id);
190 static ssize_t modalias_show(struct device *dev,
191 struct device_attribute *dev_attr, char *buf)
193 struct hv_device *hv_dev = device_to_hv_device(dev);
194 char alias_name[VMBUS_ALIAS_LEN + 1];
196 print_alias_name(hv_dev, alias_name);
197 return sprintf(buf, "vmbus:%s\n", alias_name);
199 static DEVICE_ATTR_RO(modalias);
202 static ssize_t numa_node_show(struct device *dev,
203 struct device_attribute *attr, char *buf)
205 struct hv_device *hv_dev = device_to_hv_device(dev);
207 if (!hv_dev->channel)
210 return sprintf(buf, "%d\n", hv_dev->channel->numa_node);
212 static DEVICE_ATTR_RO(numa_node);
215 static ssize_t server_monitor_pending_show(struct device *dev,
216 struct device_attribute *dev_attr,
219 struct hv_device *hv_dev = device_to_hv_device(dev);
221 if (!hv_dev->channel)
223 return sprintf(buf, "%d\n",
224 channel_pending(hv_dev->channel,
225 vmbus_connection.monitor_pages[0]));
227 static DEVICE_ATTR_RO(server_monitor_pending);
229 static ssize_t client_monitor_pending_show(struct device *dev,
230 struct device_attribute *dev_attr,
233 struct hv_device *hv_dev = device_to_hv_device(dev);
235 if (!hv_dev->channel)
237 return sprintf(buf, "%d\n",
238 channel_pending(hv_dev->channel,
239 vmbus_connection.monitor_pages[1]));
241 static DEVICE_ATTR_RO(client_monitor_pending);
243 static ssize_t server_monitor_latency_show(struct device *dev,
244 struct device_attribute *dev_attr,
247 struct hv_device *hv_dev = device_to_hv_device(dev);
249 if (!hv_dev->channel)
251 return sprintf(buf, "%d\n",
252 channel_latency(hv_dev->channel,
253 vmbus_connection.monitor_pages[0]));
255 static DEVICE_ATTR_RO(server_monitor_latency);
257 static ssize_t client_monitor_latency_show(struct device *dev,
258 struct device_attribute *dev_attr,
261 struct hv_device *hv_dev = device_to_hv_device(dev);
263 if (!hv_dev->channel)
265 return sprintf(buf, "%d\n",
266 channel_latency(hv_dev->channel,
267 vmbus_connection.monitor_pages[1]));
269 static DEVICE_ATTR_RO(client_monitor_latency);
271 static ssize_t server_monitor_conn_id_show(struct device *dev,
272 struct device_attribute *dev_attr,
275 struct hv_device *hv_dev = device_to_hv_device(dev);
277 if (!hv_dev->channel)
279 return sprintf(buf, "%d\n",
280 channel_conn_id(hv_dev->channel,
281 vmbus_connection.monitor_pages[0]));
283 static DEVICE_ATTR_RO(server_monitor_conn_id);
285 static ssize_t client_monitor_conn_id_show(struct device *dev,
286 struct device_attribute *dev_attr,
289 struct hv_device *hv_dev = device_to_hv_device(dev);
291 if (!hv_dev->channel)
293 return sprintf(buf, "%d\n",
294 channel_conn_id(hv_dev->channel,
295 vmbus_connection.monitor_pages[1]));
297 static DEVICE_ATTR_RO(client_monitor_conn_id);
299 static ssize_t out_intr_mask_show(struct device *dev,
300 struct device_attribute *dev_attr, char *buf)
302 struct hv_device *hv_dev = device_to_hv_device(dev);
303 struct hv_ring_buffer_debug_info outbound;
306 if (!hv_dev->channel)
309 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
314 return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
316 static DEVICE_ATTR_RO(out_intr_mask);
318 static ssize_t out_read_index_show(struct device *dev,
319 struct device_attribute *dev_attr, char *buf)
321 struct hv_device *hv_dev = device_to_hv_device(dev);
322 struct hv_ring_buffer_debug_info outbound;
325 if (!hv_dev->channel)
328 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
332 return sprintf(buf, "%d\n", outbound.current_read_index);
334 static DEVICE_ATTR_RO(out_read_index);
336 static ssize_t out_write_index_show(struct device *dev,
337 struct device_attribute *dev_attr,
340 struct hv_device *hv_dev = device_to_hv_device(dev);
341 struct hv_ring_buffer_debug_info outbound;
344 if (!hv_dev->channel)
347 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
351 return sprintf(buf, "%d\n", outbound.current_write_index);
353 static DEVICE_ATTR_RO(out_write_index);
355 static ssize_t out_read_bytes_avail_show(struct device *dev,
356 struct device_attribute *dev_attr,
359 struct hv_device *hv_dev = device_to_hv_device(dev);
360 struct hv_ring_buffer_debug_info outbound;
363 if (!hv_dev->channel)
366 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
370 return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
372 static DEVICE_ATTR_RO(out_read_bytes_avail);
374 static ssize_t out_write_bytes_avail_show(struct device *dev,
375 struct device_attribute *dev_attr,
378 struct hv_device *hv_dev = device_to_hv_device(dev);
379 struct hv_ring_buffer_debug_info outbound;
382 if (!hv_dev->channel)
385 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
389 return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
391 static DEVICE_ATTR_RO(out_write_bytes_avail);
393 static ssize_t in_intr_mask_show(struct device *dev,
394 struct device_attribute *dev_attr, char *buf)
396 struct hv_device *hv_dev = device_to_hv_device(dev);
397 struct hv_ring_buffer_debug_info inbound;
400 if (!hv_dev->channel)
403 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
407 return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
409 static DEVICE_ATTR_RO(in_intr_mask);
411 static ssize_t in_read_index_show(struct device *dev,
412 struct device_attribute *dev_attr, char *buf)
414 struct hv_device *hv_dev = device_to_hv_device(dev);
415 struct hv_ring_buffer_debug_info inbound;
418 if (!hv_dev->channel)
421 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
425 return sprintf(buf, "%d\n", inbound.current_read_index);
427 static DEVICE_ATTR_RO(in_read_index);
429 static ssize_t in_write_index_show(struct device *dev,
430 struct device_attribute *dev_attr, char *buf)
432 struct hv_device *hv_dev = device_to_hv_device(dev);
433 struct hv_ring_buffer_debug_info inbound;
436 if (!hv_dev->channel)
439 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
443 return sprintf(buf, "%d\n", inbound.current_write_index);
445 static DEVICE_ATTR_RO(in_write_index);
447 static ssize_t in_read_bytes_avail_show(struct device *dev,
448 struct device_attribute *dev_attr,
451 struct hv_device *hv_dev = device_to_hv_device(dev);
452 struct hv_ring_buffer_debug_info inbound;
455 if (!hv_dev->channel)
458 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
462 return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
464 static DEVICE_ATTR_RO(in_read_bytes_avail);
466 static ssize_t in_write_bytes_avail_show(struct device *dev,
467 struct device_attribute *dev_attr,
470 struct hv_device *hv_dev = device_to_hv_device(dev);
471 struct hv_ring_buffer_debug_info inbound;
474 if (!hv_dev->channel)
477 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
481 return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
483 static DEVICE_ATTR_RO(in_write_bytes_avail);
485 static ssize_t channel_vp_mapping_show(struct device *dev,
486 struct device_attribute *dev_attr,
489 struct hv_device *hv_dev = device_to_hv_device(dev);
490 struct vmbus_channel *channel = hv_dev->channel, *cur_sc;
492 int buf_size = PAGE_SIZE, n_written, tot_written;
493 struct list_head *cur;
498 tot_written = snprintf(buf, buf_size, "%u:%u\n",
499 channel->offermsg.child_relid, channel->target_cpu);
501 spin_lock_irqsave(&channel->lock, flags);
503 list_for_each(cur, &channel->sc_list) {
504 if (tot_written >= buf_size - 1)
507 cur_sc = list_entry(cur, struct vmbus_channel, sc_list);
508 n_written = scnprintf(buf + tot_written,
509 buf_size - tot_written,
511 cur_sc->offermsg.child_relid,
513 tot_written += n_written;
516 spin_unlock_irqrestore(&channel->lock, flags);
520 static DEVICE_ATTR_RO(channel_vp_mapping);
522 static ssize_t vendor_show(struct device *dev,
523 struct device_attribute *dev_attr,
526 struct hv_device *hv_dev = device_to_hv_device(dev);
527 return sprintf(buf, "0x%x\n", hv_dev->vendor_id);
529 static DEVICE_ATTR_RO(vendor);
531 static ssize_t device_show(struct device *dev,
532 struct device_attribute *dev_attr,
535 struct hv_device *hv_dev = device_to_hv_device(dev);
536 return sprintf(buf, "0x%x\n", hv_dev->device_id);
538 static DEVICE_ATTR_RO(device);
540 static ssize_t driver_override_store(struct device *dev,
541 struct device_attribute *attr,
542 const char *buf, size_t count)
544 struct hv_device *hv_dev = device_to_hv_device(dev);
545 char *driver_override, *old, *cp;
547 /* We need to keep extra room for a newline */
548 if (count >= (PAGE_SIZE - 1))
551 driver_override = kstrndup(buf, count, GFP_KERNEL);
552 if (!driver_override)
555 cp = strchr(driver_override, '\n');
560 old = hv_dev->driver_override;
561 if (strlen(driver_override)) {
562 hv_dev->driver_override = driver_override;
564 kfree(driver_override);
565 hv_dev->driver_override = NULL;
574 static ssize_t driver_override_show(struct device *dev,
575 struct device_attribute *attr, char *buf)
577 struct hv_device *hv_dev = device_to_hv_device(dev);
581 len = snprintf(buf, PAGE_SIZE, "%s\n", hv_dev->driver_override);
586 static DEVICE_ATTR_RW(driver_override);
588 /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
589 static struct attribute *vmbus_dev_attrs[] = {
591 &dev_attr_state.attr,
592 &dev_attr_monitor_id.attr,
593 &dev_attr_class_id.attr,
594 &dev_attr_device_id.attr,
595 &dev_attr_modalias.attr,
597 &dev_attr_numa_node.attr,
599 &dev_attr_server_monitor_pending.attr,
600 &dev_attr_client_monitor_pending.attr,
601 &dev_attr_server_monitor_latency.attr,
602 &dev_attr_client_monitor_latency.attr,
603 &dev_attr_server_monitor_conn_id.attr,
604 &dev_attr_client_monitor_conn_id.attr,
605 &dev_attr_out_intr_mask.attr,
606 &dev_attr_out_read_index.attr,
607 &dev_attr_out_write_index.attr,
608 &dev_attr_out_read_bytes_avail.attr,
609 &dev_attr_out_write_bytes_avail.attr,
610 &dev_attr_in_intr_mask.attr,
611 &dev_attr_in_read_index.attr,
612 &dev_attr_in_write_index.attr,
613 &dev_attr_in_read_bytes_avail.attr,
614 &dev_attr_in_write_bytes_avail.attr,
615 &dev_attr_channel_vp_mapping.attr,
616 &dev_attr_vendor.attr,
617 &dev_attr_device.attr,
618 &dev_attr_driver_override.attr,
623 * Device-level attribute_group callback function. Returns the permission for
624 * each attribute, and returns 0 if an attribute is not visible.
626 static umode_t vmbus_dev_attr_is_visible(struct kobject *kobj,
627 struct attribute *attr, int idx)
629 struct device *dev = kobj_to_dev(kobj);
630 const struct hv_device *hv_dev = device_to_hv_device(dev);
632 /* Hide the monitor attributes if the monitor mechanism is not used. */
633 if (!hv_dev->channel->offermsg.monitor_allocated &&
634 (attr == &dev_attr_monitor_id.attr ||
635 attr == &dev_attr_server_monitor_pending.attr ||
636 attr == &dev_attr_client_monitor_pending.attr ||
637 attr == &dev_attr_server_monitor_latency.attr ||
638 attr == &dev_attr_client_monitor_latency.attr ||
639 attr == &dev_attr_server_monitor_conn_id.attr ||
640 attr == &dev_attr_client_monitor_conn_id.attr))
646 static const struct attribute_group vmbus_dev_group = {
647 .attrs = vmbus_dev_attrs,
648 .is_visible = vmbus_dev_attr_is_visible
650 __ATTRIBUTE_GROUPS(vmbus_dev);
653 * vmbus_uevent - add uevent for our device
655 * This routine is invoked when a device is added or removed on the vmbus to
656 * generate a uevent to udev in the userspace. The udev will then look at its
657 * rule and the uevent generated here to load the appropriate driver
659 * The alias string will be of the form vmbus:guid where guid is the string
660 * representation of the device guid (each byte of the guid will be
661 * represented with two hex characters.
663 static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
665 struct hv_device *dev = device_to_hv_device(device);
667 char alias_name[VMBUS_ALIAS_LEN + 1];
669 print_alias_name(dev, alias_name);
670 ret = add_uevent_var(env, "MODALIAS=vmbus:%s", alias_name);
674 static const struct hv_vmbus_device_id *
675 hv_vmbus_dev_match(const struct hv_vmbus_device_id *id, const guid_t *guid)
678 return NULL; /* empty device table */
680 for (; !guid_is_null(&id->guid); id++)
681 if (guid_equal(&id->guid, guid))
687 static const struct hv_vmbus_device_id *
688 hv_vmbus_dynid_match(struct hv_driver *drv, const guid_t *guid)
690 const struct hv_vmbus_device_id *id = NULL;
691 struct vmbus_dynid *dynid;
693 spin_lock(&drv->dynids.lock);
694 list_for_each_entry(dynid, &drv->dynids.list, node) {
695 if (guid_equal(&dynid->id.guid, guid)) {
700 spin_unlock(&drv->dynids.lock);
705 static const struct hv_vmbus_device_id vmbus_device_null;
708 * Return a matching hv_vmbus_device_id pointer.
709 * If there is no match, return NULL.
711 static const struct hv_vmbus_device_id *hv_vmbus_get_id(struct hv_driver *drv,
712 struct hv_device *dev)
714 const guid_t *guid = &dev->dev_type;
715 const struct hv_vmbus_device_id *id;
717 /* When driver_override is set, only bind to the matching driver */
718 if (dev->driver_override && strcmp(dev->driver_override, drv->name))
721 /* Look at the dynamic ids first, before the static ones */
722 id = hv_vmbus_dynid_match(drv, guid);
724 id = hv_vmbus_dev_match(drv->id_table, guid);
726 /* driver_override will always match, send a dummy id */
727 if (!id && dev->driver_override)
728 id = &vmbus_device_null;
733 /* vmbus_add_dynid - add a new device ID to this driver and re-probe devices */
734 static int vmbus_add_dynid(struct hv_driver *drv, guid_t *guid)
736 struct vmbus_dynid *dynid;
738 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
742 dynid->id.guid = *guid;
744 spin_lock(&drv->dynids.lock);
745 list_add_tail(&dynid->node, &drv->dynids.list);
746 spin_unlock(&drv->dynids.lock);
748 return driver_attach(&drv->driver);
751 static void vmbus_free_dynids(struct hv_driver *drv)
753 struct vmbus_dynid *dynid, *n;
755 spin_lock(&drv->dynids.lock);
756 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
757 list_del(&dynid->node);
760 spin_unlock(&drv->dynids.lock);
764 * store_new_id - sysfs frontend to vmbus_add_dynid()
766 * Allow GUIDs to be added to an existing driver via sysfs.
768 static ssize_t new_id_store(struct device_driver *driver, const char *buf,
771 struct hv_driver *drv = drv_to_hv_drv(driver);
775 retval = guid_parse(buf, &guid);
779 if (hv_vmbus_dynid_match(drv, &guid))
782 retval = vmbus_add_dynid(drv, &guid);
787 static DRIVER_ATTR_WO(new_id);
790 * store_remove_id - remove a PCI device ID from this driver
792 * Removes a dynamic pci device ID to this driver.
794 static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
797 struct hv_driver *drv = drv_to_hv_drv(driver);
798 struct vmbus_dynid *dynid, *n;
802 retval = guid_parse(buf, &guid);
807 spin_lock(&drv->dynids.lock);
808 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
809 struct hv_vmbus_device_id *id = &dynid->id;
811 if (guid_equal(&id->guid, &guid)) {
812 list_del(&dynid->node);
818 spin_unlock(&drv->dynids.lock);
822 static DRIVER_ATTR_WO(remove_id);
824 static struct attribute *vmbus_drv_attrs[] = {
825 &driver_attr_new_id.attr,
826 &driver_attr_remove_id.attr,
829 ATTRIBUTE_GROUPS(vmbus_drv);
833 * vmbus_match - Attempt to match the specified device to the specified driver
835 static int vmbus_match(struct device *device, struct device_driver *driver)
837 struct hv_driver *drv = drv_to_hv_drv(driver);
838 struct hv_device *hv_dev = device_to_hv_device(device);
840 /* The hv_sock driver handles all hv_sock offers. */
841 if (is_hvsock_channel(hv_dev->channel))
844 if (hv_vmbus_get_id(drv, hv_dev))
851 * vmbus_probe - Add the new vmbus's child device
853 static int vmbus_probe(struct device *child_device)
856 struct hv_driver *drv =
857 drv_to_hv_drv(child_device->driver);
858 struct hv_device *dev = device_to_hv_device(child_device);
859 const struct hv_vmbus_device_id *dev_id;
861 dev_id = hv_vmbus_get_id(drv, dev);
863 ret = drv->probe(dev, dev_id);
865 pr_err("probe failed for device %s (%d)\n",
866 dev_name(child_device), ret);
869 pr_err("probe not set for driver %s\n",
870 dev_name(child_device));
877 * vmbus_remove - Remove a vmbus device
879 static int vmbus_remove(struct device *child_device)
881 struct hv_driver *drv;
882 struct hv_device *dev = device_to_hv_device(child_device);
884 if (child_device->driver) {
885 drv = drv_to_hv_drv(child_device->driver);
895 * vmbus_shutdown - Shutdown a vmbus device
897 static void vmbus_shutdown(struct device *child_device)
899 struct hv_driver *drv;
900 struct hv_device *dev = device_to_hv_device(child_device);
903 /* The device may not be attached yet */
904 if (!child_device->driver)
907 drv = drv_to_hv_drv(child_device->driver);
915 * vmbus_device_release - Final callback release of the vmbus child device
917 static void vmbus_device_release(struct device *device)
919 struct hv_device *hv_dev = device_to_hv_device(device);
920 struct vmbus_channel *channel = hv_dev->channel;
922 mutex_lock(&vmbus_connection.channel_mutex);
923 hv_process_channel_removal(channel);
924 mutex_unlock(&vmbus_connection.channel_mutex);
928 /* The one and only one */
929 static struct bus_type hv_bus = {
931 .match = vmbus_match,
932 .shutdown = vmbus_shutdown,
933 .remove = vmbus_remove,
934 .probe = vmbus_probe,
935 .uevent = vmbus_uevent,
936 .dev_groups = vmbus_dev_groups,
937 .drv_groups = vmbus_drv_groups,
940 struct onmessage_work_context {
941 struct work_struct work;
942 struct hv_message msg;
945 static void vmbus_onmessage_work(struct work_struct *work)
947 struct onmessage_work_context *ctx;
949 /* Do not process messages if we're in DISCONNECTED state */
950 if (vmbus_connection.conn_state == DISCONNECTED)
953 ctx = container_of(work, struct onmessage_work_context,
955 vmbus_onmessage(&ctx->msg);
959 void vmbus_on_msg_dpc(unsigned long data)
961 struct hv_per_cpu_context *hv_cpu = (void *)data;
962 void *page_addr = hv_cpu->synic_message_page;
963 struct hv_message *msg = (struct hv_message *)page_addr +
965 struct vmbus_channel_message_header *hdr;
966 const struct vmbus_channel_message_table_entry *entry;
967 struct onmessage_work_context *ctx;
968 u32 message_type = msg->header.message_type;
970 if (message_type == HVMSG_NONE)
974 hdr = (struct vmbus_channel_message_header *)msg->u.payload;
976 trace_vmbus_on_msg_dpc(hdr);
978 if (hdr->msgtype >= CHANNELMSG_COUNT) {
979 WARN_ONCE(1, "unknown msgtype=%d\n", hdr->msgtype);
983 entry = &channel_message_table[hdr->msgtype];
984 if (entry->handler_type == VMHT_BLOCKING) {
985 ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
989 INIT_WORK(&ctx->work, vmbus_onmessage_work);
990 memcpy(&ctx->msg, msg, sizeof(*msg));
993 * The host can generate a rescind message while we
994 * may still be handling the original offer. We deal with
995 * this condition by ensuring the processing is done on the
998 switch (hdr->msgtype) {
999 case CHANNELMSG_RESCIND_CHANNELOFFER:
1001 * If we are handling the rescind message;
1002 * schedule the work on the global work queue.
1004 schedule_work_on(vmbus_connection.connect_cpu,
1008 case CHANNELMSG_OFFERCHANNEL:
1009 atomic_inc(&vmbus_connection.offer_in_progress);
1010 queue_work_on(vmbus_connection.connect_cpu,
1011 vmbus_connection.work_queue,
1016 queue_work(vmbus_connection.work_queue, &ctx->work);
1019 entry->message_handler(hdr);
1022 vmbus_signal_eom(msg, message_type);
1027 * Direct callback for channels using other deferred processing
1029 static void vmbus_channel_isr(struct vmbus_channel *channel)
1031 void (*callback_fn)(void *);
1033 callback_fn = READ_ONCE(channel->onchannel_callback);
1034 if (likely(callback_fn != NULL))
1035 (*callback_fn)(channel->channel_callback_context);
1039 * Schedule all channels with events pending
1041 static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)
1043 unsigned long *recv_int_page;
1046 if (vmbus_proto_version < VERSION_WIN8) {
1047 maxbits = MAX_NUM_CHANNELS_SUPPORTED;
1048 recv_int_page = vmbus_connection.recv_int_page;
1051 * When the host is win8 and beyond, the event page
1052 * can be directly checked to get the id of the channel
1053 * that has the interrupt pending.
1055 void *page_addr = hv_cpu->synic_event_page;
1056 union hv_synic_event_flags *event
1057 = (union hv_synic_event_flags *)page_addr +
1060 maxbits = HV_EVENT_FLAGS_COUNT;
1061 recv_int_page = event->flags;
1064 if (unlikely(!recv_int_page))
1067 for_each_set_bit(relid, recv_int_page, maxbits) {
1068 struct vmbus_channel *channel;
1070 if (!sync_test_and_clear_bit(relid, recv_int_page))
1073 /* Special case - vmbus channel protocol msg */
1079 /* Find channel based on relid */
1080 list_for_each_entry_rcu(channel, &hv_cpu->chan_list, percpu_list) {
1081 if (channel->offermsg.child_relid != relid)
1084 if (channel->rescind)
1087 trace_vmbus_chan_sched(channel);
1089 ++channel->interrupts;
1091 switch (channel->callback_mode) {
1093 vmbus_channel_isr(channel);
1096 case HV_CALL_BATCHED:
1097 hv_begin_read(&channel->inbound);
1099 case HV_CALL_DIRECT:
1100 tasklet_schedule(&channel->callback_event);
1108 static void vmbus_isr(void)
1110 struct hv_per_cpu_context *hv_cpu
1111 = this_cpu_ptr(hv_context.cpu_context);
1112 void *page_addr = hv_cpu->synic_event_page;
1113 struct hv_message *msg;
1114 union hv_synic_event_flags *event;
1115 bool handled = false;
1117 if (unlikely(page_addr == NULL))
1120 event = (union hv_synic_event_flags *)page_addr +
1123 * Check for events before checking for messages. This is the order
1124 * in which events and messages are checked in Windows guests on
1125 * Hyper-V, and the Windows team suggested we do the same.
1128 if ((vmbus_proto_version == VERSION_WS2008) ||
1129 (vmbus_proto_version == VERSION_WIN7)) {
1131 /* Since we are a child, we only need to check bit 0 */
1132 if (sync_test_and_clear_bit(0, event->flags))
1136 * Our host is win8 or above. The signaling mechanism
1137 * has changed and we can directly look at the event page.
1138 * If bit n is set then we have an interrup on the channel
1145 vmbus_chan_sched(hv_cpu);
1147 page_addr = hv_cpu->synic_message_page;
1148 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
1150 /* Check if there are actual msgs to be processed */
1151 if (msg->header.message_type != HVMSG_NONE) {
1152 if (msg->header.message_type == HVMSG_TIMER_EXPIRED) {
1154 vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED);
1156 tasklet_schedule(&hv_cpu->msg_dpc);
1159 add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0);
1163 * Boolean to control whether to report panic messages over Hyper-V.
1165 * It can be set via /proc/sys/kernel/hyperv/record_panic_msg
1167 static int sysctl_record_panic_msg = 1;
1170 * Callback from kmsg_dump. Grab as much as possible from the end of the kmsg
1171 * buffer and call into Hyper-V to transfer the data.
1173 static void hv_kmsg_dump(struct kmsg_dumper *dumper,
1174 enum kmsg_dump_reason reason)
1176 size_t bytes_written;
1177 phys_addr_t panic_pa;
1179 /* We are only interested in panics. */
1180 if ((reason != KMSG_DUMP_PANIC) || (!sysctl_record_panic_msg))
1183 panic_pa = virt_to_phys(hv_panic_page);
1186 * Write dump contents to the page. No need to synchronize; panic should
1187 * be single-threaded.
1189 kmsg_dump_get_buffer(dumper, true, hv_panic_page, PAGE_SIZE,
1192 hyperv_report_panic_msg(panic_pa, bytes_written);
1195 static struct kmsg_dumper hv_kmsg_dumper = {
1196 .dump = hv_kmsg_dump,
1199 static struct ctl_table_header *hv_ctl_table_hdr;
1204 * sysctl option to allow the user to control whether kmsg data should be
1205 * reported to Hyper-V on panic.
1207 static struct ctl_table hv_ctl_table[] = {
1209 .procname = "hyperv_record_panic_msg",
1210 .data = &sysctl_record_panic_msg,
1211 .maxlen = sizeof(int),
1213 .proc_handler = proc_dointvec_minmax,
1220 static struct ctl_table hv_root_table[] = {
1222 .procname = "kernel",
1224 .child = hv_ctl_table
1230 * vmbus_bus_init -Main vmbus driver initialization routine.
1233 * - initialize the vmbus driver context
1234 * - invoke the vmbus hv main init routine
1235 * - retrieve the channel offers
1237 static int vmbus_bus_init(void)
1241 /* Hypervisor initialization...setup hypercall page..etc */
1244 pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
1248 ret = bus_register(&hv_bus);
1252 hv_setup_vmbus_irq(vmbus_isr);
1254 ret = hv_synic_alloc();
1258 ret = hv_stimer_alloc(VMBUS_MESSAGE_SINT);
1263 * Initialize the per-cpu interrupt state and stimer state.
1264 * Then connect to the host.
1266 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online",
1267 hv_synic_init, hv_synic_cleanup);
1270 hyperv_cpuhp_online = ret;
1272 ret = vmbus_connect();
1277 * Only register if the crash MSRs are available
1279 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
1280 u64 hyperv_crash_ctl;
1282 * Sysctl registration is not fatal, since by default
1283 * reporting is enabled.
1285 hv_ctl_table_hdr = register_sysctl_table(hv_root_table);
1286 if (!hv_ctl_table_hdr)
1287 pr_err("Hyper-V: sysctl table register error");
1290 * Register for panic kmsg callback only if the right
1291 * capability is supported by the hypervisor.
1293 hv_get_crash_ctl(hyperv_crash_ctl);
1294 if (hyperv_crash_ctl & HV_CRASH_CTL_CRASH_NOTIFY_MSG) {
1295 hv_panic_page = (void *)get_zeroed_page(GFP_KERNEL);
1296 if (hv_panic_page) {
1297 ret = kmsg_dump_register(&hv_kmsg_dumper);
1299 pr_err("Hyper-V: kmsg dump register "
1300 "error 0x%x\n", ret);
1302 pr_err("Hyper-V: panic message page memory "
1303 "allocation failed");
1306 register_die_notifier(&hyperv_die_block);
1307 atomic_notifier_chain_register(&panic_notifier_list,
1308 &hyperv_panic_block);
1311 vmbus_request_offers();
1316 cpuhp_remove_state(hyperv_cpuhp_online);
1321 hv_remove_vmbus_irq();
1323 bus_unregister(&hv_bus);
1324 free_page((unsigned long)hv_panic_page);
1325 unregister_sysctl_table(hv_ctl_table_hdr);
1326 hv_ctl_table_hdr = NULL;
1331 * __vmbus_child_driver_register() - Register a vmbus's driver
1332 * @hv_driver: Pointer to driver structure you want to register
1333 * @owner: owner module of the drv
1334 * @mod_name: module name string
1336 * Registers the given driver with Linux through the 'driver_register()' call
1337 * and sets up the hyper-v vmbus handling for this driver.
1338 * It will return the state of the 'driver_register()' call.
1341 int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
1345 pr_info("registering driver %s\n", hv_driver->name);
1347 ret = vmbus_exists();
1351 hv_driver->driver.name = hv_driver->name;
1352 hv_driver->driver.owner = owner;
1353 hv_driver->driver.mod_name = mod_name;
1354 hv_driver->driver.bus = &hv_bus;
1356 spin_lock_init(&hv_driver->dynids.lock);
1357 INIT_LIST_HEAD(&hv_driver->dynids.list);
1359 ret = driver_register(&hv_driver->driver);
1363 EXPORT_SYMBOL_GPL(__vmbus_driver_register);
1366 * vmbus_driver_unregister() - Unregister a vmbus's driver
1367 * @hv_driver: Pointer to driver structure you want to
1370 * Un-register the given driver that was previous registered with a call to
1371 * vmbus_driver_register()
1373 void vmbus_driver_unregister(struct hv_driver *hv_driver)
1375 pr_info("unregistering driver %s\n", hv_driver->name);
1377 if (!vmbus_exists()) {
1378 driver_unregister(&hv_driver->driver);
1379 vmbus_free_dynids(hv_driver);
1382 EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
1386 * Called when last reference to channel is gone.
1388 static void vmbus_chan_release(struct kobject *kobj)
1390 struct vmbus_channel *channel
1391 = container_of(kobj, struct vmbus_channel, kobj);
1393 kfree_rcu(channel, rcu);
1396 struct vmbus_chan_attribute {
1397 struct attribute attr;
1398 ssize_t (*show)(struct vmbus_channel *chan, char *buf);
1399 ssize_t (*store)(struct vmbus_channel *chan,
1400 const char *buf, size_t count);
1402 #define VMBUS_CHAN_ATTR(_name, _mode, _show, _store) \
1403 struct vmbus_chan_attribute chan_attr_##_name \
1404 = __ATTR(_name, _mode, _show, _store)
1405 #define VMBUS_CHAN_ATTR_RW(_name) \
1406 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RW(_name)
1407 #define VMBUS_CHAN_ATTR_RO(_name) \
1408 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RO(_name)
1409 #define VMBUS_CHAN_ATTR_WO(_name) \
1410 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_WO(_name)
1412 static ssize_t vmbus_chan_attr_show(struct kobject *kobj,
1413 struct attribute *attr, char *buf)
1415 const struct vmbus_chan_attribute *attribute
1416 = container_of(attr, struct vmbus_chan_attribute, attr);
1417 struct vmbus_channel *chan
1418 = container_of(kobj, struct vmbus_channel, kobj);
1420 if (!attribute->show)
1423 return attribute->show(chan, buf);
1426 static const struct sysfs_ops vmbus_chan_sysfs_ops = {
1427 .show = vmbus_chan_attr_show,
1430 static ssize_t out_mask_show(struct vmbus_channel *channel, char *buf)
1432 struct hv_ring_buffer_info *rbi = &channel->outbound;
1435 mutex_lock(&rbi->ring_buffer_mutex);
1436 if (!rbi->ring_buffer) {
1437 mutex_unlock(&rbi->ring_buffer_mutex);
1441 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1442 mutex_unlock(&rbi->ring_buffer_mutex);
1445 static VMBUS_CHAN_ATTR_RO(out_mask);
1447 static ssize_t in_mask_show(struct vmbus_channel *channel, char *buf)
1449 struct hv_ring_buffer_info *rbi = &channel->inbound;
1452 mutex_lock(&rbi->ring_buffer_mutex);
1453 if (!rbi->ring_buffer) {
1454 mutex_unlock(&rbi->ring_buffer_mutex);
1458 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1459 mutex_unlock(&rbi->ring_buffer_mutex);
1462 static VMBUS_CHAN_ATTR_RO(in_mask);
1464 static ssize_t read_avail_show(struct vmbus_channel *channel, char *buf)
1466 struct hv_ring_buffer_info *rbi = &channel->inbound;
1469 mutex_lock(&rbi->ring_buffer_mutex);
1470 if (!rbi->ring_buffer) {
1471 mutex_unlock(&rbi->ring_buffer_mutex);
1475 ret = sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi));
1476 mutex_unlock(&rbi->ring_buffer_mutex);
1479 static VMBUS_CHAN_ATTR_RO(read_avail);
1481 static ssize_t write_avail_show(struct vmbus_channel *channel, char *buf)
1483 struct hv_ring_buffer_info *rbi = &channel->outbound;
1486 mutex_lock(&rbi->ring_buffer_mutex);
1487 if (!rbi->ring_buffer) {
1488 mutex_unlock(&rbi->ring_buffer_mutex);
1492 ret = sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi));
1493 mutex_unlock(&rbi->ring_buffer_mutex);
1496 static VMBUS_CHAN_ATTR_RO(write_avail);
1498 static ssize_t show_target_cpu(struct vmbus_channel *channel, char *buf)
1500 return sprintf(buf, "%u\n", channel->target_cpu);
1502 static VMBUS_CHAN_ATTR(cpu, S_IRUGO, show_target_cpu, NULL);
1504 static ssize_t channel_pending_show(struct vmbus_channel *channel,
1507 return sprintf(buf, "%d\n",
1508 channel_pending(channel,
1509 vmbus_connection.monitor_pages[1]));
1511 static VMBUS_CHAN_ATTR(pending, S_IRUGO, channel_pending_show, NULL);
1513 static ssize_t channel_latency_show(struct vmbus_channel *channel,
1516 return sprintf(buf, "%d\n",
1517 channel_latency(channel,
1518 vmbus_connection.monitor_pages[1]));
1520 static VMBUS_CHAN_ATTR(latency, S_IRUGO, channel_latency_show, NULL);
1522 static ssize_t channel_interrupts_show(struct vmbus_channel *channel, char *buf)
1524 return sprintf(buf, "%llu\n", channel->interrupts);
1526 static VMBUS_CHAN_ATTR(interrupts, S_IRUGO, channel_interrupts_show, NULL);
1528 static ssize_t channel_events_show(struct vmbus_channel *channel, char *buf)
1530 return sprintf(buf, "%llu\n", channel->sig_events);
1532 static VMBUS_CHAN_ATTR(events, S_IRUGO, channel_events_show, NULL);
1534 static ssize_t channel_intr_in_full_show(struct vmbus_channel *channel,
1537 return sprintf(buf, "%llu\n",
1538 (unsigned long long)channel->intr_in_full);
1540 static VMBUS_CHAN_ATTR(intr_in_full, 0444, channel_intr_in_full_show, NULL);
1542 static ssize_t channel_intr_out_empty_show(struct vmbus_channel *channel,
1545 return sprintf(buf, "%llu\n",
1546 (unsigned long long)channel->intr_out_empty);
1548 static VMBUS_CHAN_ATTR(intr_out_empty, 0444, channel_intr_out_empty_show, NULL);
1550 static ssize_t channel_out_full_first_show(struct vmbus_channel *channel,
1553 return sprintf(buf, "%llu\n",
1554 (unsigned long long)channel->out_full_first);
1556 static VMBUS_CHAN_ATTR(out_full_first, 0444, channel_out_full_first_show, NULL);
1558 static ssize_t channel_out_full_total_show(struct vmbus_channel *channel,
1561 return sprintf(buf, "%llu\n",
1562 (unsigned long long)channel->out_full_total);
1564 static VMBUS_CHAN_ATTR(out_full_total, 0444, channel_out_full_total_show, NULL);
1566 static ssize_t subchannel_monitor_id_show(struct vmbus_channel *channel,
1569 return sprintf(buf, "%u\n", channel->offermsg.monitorid);
1571 static VMBUS_CHAN_ATTR(monitor_id, S_IRUGO, subchannel_monitor_id_show, NULL);
1573 static ssize_t subchannel_id_show(struct vmbus_channel *channel,
1576 return sprintf(buf, "%u\n",
1577 channel->offermsg.offer.sub_channel_index);
1579 static VMBUS_CHAN_ATTR_RO(subchannel_id);
1581 static struct attribute *vmbus_chan_attrs[] = {
1582 &chan_attr_out_mask.attr,
1583 &chan_attr_in_mask.attr,
1584 &chan_attr_read_avail.attr,
1585 &chan_attr_write_avail.attr,
1586 &chan_attr_cpu.attr,
1587 &chan_attr_pending.attr,
1588 &chan_attr_latency.attr,
1589 &chan_attr_interrupts.attr,
1590 &chan_attr_events.attr,
1591 &chan_attr_intr_in_full.attr,
1592 &chan_attr_intr_out_empty.attr,
1593 &chan_attr_out_full_first.attr,
1594 &chan_attr_out_full_total.attr,
1595 &chan_attr_monitor_id.attr,
1596 &chan_attr_subchannel_id.attr,
1601 * Channel-level attribute_group callback function. Returns the permission for
1602 * each attribute, and returns 0 if an attribute is not visible.
1604 static umode_t vmbus_chan_attr_is_visible(struct kobject *kobj,
1605 struct attribute *attr, int idx)
1607 const struct vmbus_channel *channel =
1608 container_of(kobj, struct vmbus_channel, kobj);
1610 /* Hide the monitor attributes if the monitor mechanism is not used. */
1611 if (!channel->offermsg.monitor_allocated &&
1612 (attr == &chan_attr_pending.attr ||
1613 attr == &chan_attr_latency.attr ||
1614 attr == &chan_attr_monitor_id.attr))
1620 static struct attribute_group vmbus_chan_group = {
1621 .attrs = vmbus_chan_attrs,
1622 .is_visible = vmbus_chan_attr_is_visible
1625 static struct kobj_type vmbus_chan_ktype = {
1626 .sysfs_ops = &vmbus_chan_sysfs_ops,
1627 .release = vmbus_chan_release,
1631 * vmbus_add_channel_kobj - setup a sub-directory under device/channels
1633 int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
1635 const struct device *device = &dev->device;
1636 struct kobject *kobj = &channel->kobj;
1637 u32 relid = channel->offermsg.child_relid;
1640 kobj->kset = dev->channels_kset;
1641 ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
1646 ret = sysfs_create_group(kobj, &vmbus_chan_group);
1650 * The calling functions' error handling paths will cleanup the
1651 * empty channel directory.
1653 dev_err(device, "Unable to set up channel sysfs files\n");
1657 kobject_uevent(kobj, KOBJ_ADD);
1663 * vmbus_remove_channel_attr_group - remove the channel's attribute group
1665 void vmbus_remove_channel_attr_group(struct vmbus_channel *channel)
1667 sysfs_remove_group(&channel->kobj, &vmbus_chan_group);
1671 * vmbus_device_create - Creates and registers a new child device
1674 struct hv_device *vmbus_device_create(const guid_t *type,
1675 const guid_t *instance,
1676 struct vmbus_channel *channel)
1678 struct hv_device *child_device_obj;
1680 child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
1681 if (!child_device_obj) {
1682 pr_err("Unable to allocate device object for child device\n");
1686 child_device_obj->channel = channel;
1687 guid_copy(&child_device_obj->dev_type, type);
1688 guid_copy(&child_device_obj->dev_instance, instance);
1689 child_device_obj->vendor_id = 0x1414; /* MSFT vendor ID */
1691 return child_device_obj;
1695 * vmbus_device_register - Register the child device
1697 int vmbus_device_register(struct hv_device *child_device_obj)
1699 struct kobject *kobj = &child_device_obj->device.kobj;
1702 dev_set_name(&child_device_obj->device, "%pUl",
1703 child_device_obj->channel->offermsg.offer.if_instance.b);
1705 child_device_obj->device.bus = &hv_bus;
1706 child_device_obj->device.parent = &hv_acpi_dev->dev;
1707 child_device_obj->device.release = vmbus_device_release;
1710 * Register with the LDM. This will kick off the driver/device
1711 * binding...which will eventually call vmbus_match() and vmbus_probe()
1713 ret = device_register(&child_device_obj->device);
1715 pr_err("Unable to register child device\n");
1719 child_device_obj->channels_kset = kset_create_and_add("channels",
1721 if (!child_device_obj->channels_kset) {
1723 goto err_dev_unregister;
1726 ret = vmbus_add_channel_kobj(child_device_obj,
1727 child_device_obj->channel);
1729 pr_err("Unable to register primary channeln");
1730 goto err_kset_unregister;
1735 err_kset_unregister:
1736 kset_unregister(child_device_obj->channels_kset);
1739 device_unregister(&child_device_obj->device);
1744 * vmbus_device_unregister - Remove the specified child device
1747 void vmbus_device_unregister(struct hv_device *device_obj)
1749 pr_debug("child device %s unregistered\n",
1750 dev_name(&device_obj->device));
1752 kset_unregister(device_obj->channels_kset);
1755 * Kick off the process of unregistering the device.
1756 * This will call vmbus_remove() and eventually vmbus_device_release()
1758 device_unregister(&device_obj->device);
1763 * VMBUS is an acpi enumerated device. Get the information we
1766 #define VTPM_BASE_ADDRESS 0xfed40000
1767 static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
1769 resource_size_t start = 0;
1770 resource_size_t end = 0;
1771 struct resource *new_res;
1772 struct resource **old_res = &hyperv_mmio;
1773 struct resource **prev_res = NULL;
1775 switch (res->type) {
1778 * "Address" descriptors are for bus windows. Ignore
1779 * "memory" descriptors, which are for registers on
1782 case ACPI_RESOURCE_TYPE_ADDRESS32:
1783 start = res->data.address32.address.minimum;
1784 end = res->data.address32.address.maximum;
1787 case ACPI_RESOURCE_TYPE_ADDRESS64:
1788 start = res->data.address64.address.minimum;
1789 end = res->data.address64.address.maximum;
1793 /* Unused resource type */
1798 * Ignore ranges that are below 1MB, as they're not
1799 * necessary or useful here.
1804 new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC);
1806 return AE_NO_MEMORY;
1808 /* If this range overlaps the virtual TPM, truncate it. */
1809 if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS)
1810 end = VTPM_BASE_ADDRESS;
1812 new_res->name = "hyperv mmio";
1813 new_res->flags = IORESOURCE_MEM;
1814 new_res->start = start;
1818 * If two ranges are adjacent, merge them.
1826 if (((*old_res)->end + 1) == new_res->start) {
1827 (*old_res)->end = new_res->end;
1832 if ((*old_res)->start == new_res->end + 1) {
1833 (*old_res)->start = new_res->start;
1838 if ((*old_res)->start > new_res->end) {
1839 new_res->sibling = *old_res;
1841 (*prev_res)->sibling = new_res;
1847 old_res = &(*old_res)->sibling;
1854 static int vmbus_acpi_remove(struct acpi_device *device)
1856 struct resource *cur_res;
1857 struct resource *next_res;
1861 __release_region(hyperv_mmio, fb_mmio->start,
1862 resource_size(fb_mmio));
1866 for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) {
1867 next_res = cur_res->sibling;
1875 static void vmbus_reserve_fb(void)
1879 * Make a claim for the frame buffer in the resource tree under the
1880 * first node, which will be the one below 4GB. The length seems to
1881 * be underreported, particularly in a Generation 1 VM. So start out
1882 * reserving a larger area and make it smaller until it succeeds.
1885 if (screen_info.lfb_base) {
1886 if (efi_enabled(EFI_BOOT))
1887 size = max_t(__u32, screen_info.lfb_size, 0x800000);
1889 size = max_t(__u32, screen_info.lfb_size, 0x4000000);
1891 for (; !fb_mmio && (size >= 0x100000); size >>= 1) {
1892 fb_mmio = __request_region(hyperv_mmio,
1893 screen_info.lfb_base, size,
1900 * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
1901 * @new: If successful, supplied a pointer to the
1902 * allocated MMIO space.
1903 * @device_obj: Identifies the caller
1904 * @min: Minimum guest physical address of the
1906 * @max: Maximum guest physical address
1907 * @size: Size of the range to be allocated
1908 * @align: Alignment of the range to be allocated
1909 * @fb_overlap_ok: Whether this allocation can be allowed
1910 * to overlap the video frame buffer.
1912 * This function walks the resources granted to VMBus by the
1913 * _CRS object in the ACPI namespace underneath the parent
1914 * "bridge" whether that's a root PCI bus in the Generation 1
1915 * case or a Module Device in the Generation 2 case. It then
1916 * attempts to allocate from the global MMIO pool in a way that
1917 * matches the constraints supplied in these parameters and by
1920 * Return: 0 on success, -errno on failure
1922 int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
1923 resource_size_t min, resource_size_t max,
1924 resource_size_t size, resource_size_t align,
1927 struct resource *iter, *shadow;
1928 resource_size_t range_min, range_max, start;
1929 const char *dev_n = dev_name(&device_obj->device);
1933 down(&hyperv_mmio_lock);
1936 * If overlaps with frame buffers are allowed, then first attempt to
1937 * make the allocation from within the reserved region. Because it
1938 * is already reserved, no shadow allocation is necessary.
1940 if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) &&
1941 !(max < fb_mmio->start)) {
1943 range_min = fb_mmio->start;
1944 range_max = fb_mmio->end;
1945 start = (range_min + align - 1) & ~(align - 1);
1946 for (; start + size - 1 <= range_max; start += align) {
1947 *new = request_mem_region_exclusive(start, size, dev_n);
1955 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
1956 if ((iter->start >= max) || (iter->end <= min))
1959 range_min = iter->start;
1960 range_max = iter->end;
1961 start = (range_min + align - 1) & ~(align - 1);
1962 for (; start + size - 1 <= range_max; start += align) {
1963 shadow = __request_region(iter, start, size, NULL,
1968 *new = request_mem_region_exclusive(start, size, dev_n);
1970 shadow->name = (char *)*new;
1975 __release_region(iter, start, size);
1980 up(&hyperv_mmio_lock);
1983 EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
1986 * vmbus_free_mmio() - Free a memory-mapped I/O range.
1987 * @start: Base address of region to release.
1988 * @size: Size of the range to be allocated
1990 * This function releases anything requested by
1991 * vmbus_mmio_allocate().
1993 void vmbus_free_mmio(resource_size_t start, resource_size_t size)
1995 struct resource *iter;
1997 down(&hyperv_mmio_lock);
1998 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
1999 if ((iter->start >= start + size) || (iter->end <= start))
2002 __release_region(iter, start, size);
2004 release_mem_region(start, size);
2005 up(&hyperv_mmio_lock);
2008 EXPORT_SYMBOL_GPL(vmbus_free_mmio);
2010 static int vmbus_acpi_add(struct acpi_device *device)
2013 int ret_val = -ENODEV;
2014 struct acpi_device *ancestor;
2016 hv_acpi_dev = device;
2018 result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
2019 vmbus_walk_resources, NULL);
2021 if (ACPI_FAILURE(result))
2024 * Some ancestor of the vmbus acpi device (Gen1 or Gen2
2025 * firmware) is the VMOD that has the mmio ranges. Get that.
2027 for (ancestor = device->parent; ancestor; ancestor = ancestor->parent) {
2028 result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
2029 vmbus_walk_resources, NULL);
2031 if (ACPI_FAILURE(result))
2041 complete(&probe_event);
2043 vmbus_acpi_remove(device);
2047 static const struct acpi_device_id vmbus_acpi_device_ids[] = {
2052 MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
2054 static struct acpi_driver vmbus_acpi_driver = {
2056 .ids = vmbus_acpi_device_ids,
2058 .add = vmbus_acpi_add,
2059 .remove = vmbus_acpi_remove,
2063 static void hv_kexec_handler(void)
2065 hv_stimer_global_cleanup();
2066 vmbus_initiate_unload(false);
2067 vmbus_connection.conn_state = DISCONNECTED;
2068 /* Make sure conn_state is set as hv_synic_cleanup checks for it */
2070 cpuhp_remove_state(hyperv_cpuhp_online);
2074 static void hv_crash_handler(struct pt_regs *regs)
2078 vmbus_initiate_unload(true);
2080 * In crash handler we can't schedule synic cleanup for all CPUs,
2081 * doing the cleanup for current CPU only. This should be sufficient
2084 vmbus_connection.conn_state = DISCONNECTED;
2085 cpu = smp_processor_id();
2086 hv_stimer_cleanup(cpu);
2087 hv_synic_cleanup(cpu);
2091 static int __init hv_acpi_init(void)
2095 if (!hv_is_hyperv_initialized())
2098 init_completion(&probe_event);
2101 * Get ACPI resources first.
2103 ret = acpi_bus_register_driver(&vmbus_acpi_driver);
2108 t = wait_for_completion_timeout(&probe_event, 5*HZ);
2114 ret = vmbus_bus_init();
2118 hv_setup_kexec_handler(hv_kexec_handler);
2119 hv_setup_crash_handler(hv_crash_handler);
2124 acpi_bus_unregister_driver(&vmbus_acpi_driver);
2129 static void __exit vmbus_exit(void)
2133 hv_remove_kexec_handler();
2134 hv_remove_crash_handler();
2135 vmbus_connection.conn_state = DISCONNECTED;
2136 hv_stimer_global_cleanup();
2138 hv_remove_vmbus_irq();
2139 for_each_online_cpu(cpu) {
2140 struct hv_per_cpu_context *hv_cpu
2141 = per_cpu_ptr(hv_context.cpu_context, cpu);
2143 tasklet_kill(&hv_cpu->msg_dpc);
2145 vmbus_free_channels();
2147 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
2148 kmsg_dump_unregister(&hv_kmsg_dumper);
2149 unregister_die_notifier(&hyperv_die_block);
2150 atomic_notifier_chain_unregister(&panic_notifier_list,
2151 &hyperv_panic_block);
2154 free_page((unsigned long)hv_panic_page);
2155 unregister_sysctl_table(hv_ctl_table_hdr);
2156 hv_ctl_table_hdr = NULL;
2157 bus_unregister(&hv_bus);
2159 cpuhp_remove_state(hyperv_cpuhp_online);
2161 acpi_bus_unregister_driver(&vmbus_acpi_driver);
2165 MODULE_LICENSE("GPL");
2167 subsys_initcall(hv_acpi_init);
2168 module_exit(vmbus_exit);