Get rid of 'remove_new' relic from platform driver struct
[linux.git] / drivers / hv / vmbus_drv.c
blob6d89d37b069add5f26a408d6291ca441394d7d2d
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2009, Microsoft Corporation.
5 * Authors:
6 * Haiyang Zhang <haiyangz@microsoft.com>
7 * Hank Janssen <hjanssen@microsoft.com>
8 * K. Y. Srinivasan <kys@microsoft.com>
9 */
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/platform_device.h>
16 #include <linux/interrupt.h>
17 #include <linux/sysctl.h>
18 #include <linux/slab.h>
19 #include <linux/acpi.h>
20 #include <linux/completion.h>
21 #include <linux/hyperv.h>
22 #include <linux/kernel_stat.h>
23 #include <linux/of_address.h>
24 #include <linux/clockchips.h>
25 #include <linux/cpu.h>
26 #include <linux/sched/isolation.h>
27 #include <linux/sched/task_stack.h>
29 #include <linux/delay.h>
30 #include <linux/panic_notifier.h>
31 #include <linux/ptrace.h>
32 #include <linux/screen_info.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 <asm/mshyperv.h>
41 #include "hyperv_vmbus.h"
43 struct vmbus_dynid {
44 struct list_head node;
45 struct hv_vmbus_device_id id;
48 static struct device *hv_dev;
50 static int hyperv_cpuhp_online;
52 static long __percpu *vmbus_evt;
54 /* Values parsed from ACPI DSDT */
55 int vmbus_irq;
56 int vmbus_interrupt;
59 * The panic notifier below is responsible solely for unloading the
60 * vmbus connection, which is necessary in a panic event.
62 * Notice an intrincate relation of this notifier with Hyper-V
63 * framebuffer panic notifier exists - we need vmbus connection alive
64 * there in order to succeed, so we need to order both with each other
65 * [see hvfb_on_panic()] - this is done using notifiers' priorities.
67 static int hv_panic_vmbus_unload(struct notifier_block *nb, unsigned long val,
68 void *args)
70 vmbus_initiate_unload(true);
71 return NOTIFY_DONE;
73 static struct notifier_block hyperv_panic_vmbus_unload_block = {
74 .notifier_call = hv_panic_vmbus_unload,
75 .priority = INT_MIN + 1, /* almost the latest one to execute */
78 static const char *fb_mmio_name = "fb_range";
79 static struct resource *fb_mmio;
80 static struct resource *hyperv_mmio;
81 static DEFINE_MUTEX(hyperv_mmio_lock);
83 static int vmbus_exists(void)
85 if (hv_dev == NULL)
86 return -ENODEV;
88 return 0;
91 static u8 channel_monitor_group(const struct vmbus_channel *channel)
93 return (u8)channel->offermsg.monitorid / 32;
96 static u8 channel_monitor_offset(const struct vmbus_channel *channel)
98 return (u8)channel->offermsg.monitorid % 32;
101 static u32 channel_pending(const struct vmbus_channel *channel,
102 const struct hv_monitor_page *monitor_page)
104 u8 monitor_group = channel_monitor_group(channel);
106 return monitor_page->trigger_group[monitor_group].pending;
109 static u32 channel_latency(const struct vmbus_channel *channel,
110 const struct hv_monitor_page *monitor_page)
112 u8 monitor_group = channel_monitor_group(channel);
113 u8 monitor_offset = channel_monitor_offset(channel);
115 return monitor_page->latency[monitor_group][monitor_offset];
118 static u32 channel_conn_id(struct vmbus_channel *channel,
119 struct hv_monitor_page *monitor_page)
121 u8 monitor_group = channel_monitor_group(channel);
122 u8 monitor_offset = channel_monitor_offset(channel);
124 return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
127 static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
128 char *buf)
130 struct hv_device *hv_dev = device_to_hv_device(dev);
132 if (!hv_dev->channel)
133 return -ENODEV;
134 return sysfs_emit(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
136 static DEVICE_ATTR_RO(id);
138 static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
139 char *buf)
141 struct hv_device *hv_dev = device_to_hv_device(dev);
143 if (!hv_dev->channel)
144 return -ENODEV;
145 return sysfs_emit(buf, "%d\n", hv_dev->channel->state);
147 static DEVICE_ATTR_RO(state);
149 static ssize_t monitor_id_show(struct device *dev,
150 struct device_attribute *dev_attr, char *buf)
152 struct hv_device *hv_dev = device_to_hv_device(dev);
154 if (!hv_dev->channel)
155 return -ENODEV;
156 return sysfs_emit(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
158 static DEVICE_ATTR_RO(monitor_id);
160 static ssize_t class_id_show(struct device *dev,
161 struct device_attribute *dev_attr, char *buf)
163 struct hv_device *hv_dev = device_to_hv_device(dev);
165 if (!hv_dev->channel)
166 return -ENODEV;
167 return sysfs_emit(buf, "{%pUl}\n",
168 &hv_dev->channel->offermsg.offer.if_type);
170 static DEVICE_ATTR_RO(class_id);
172 static ssize_t device_id_show(struct device *dev,
173 struct device_attribute *dev_attr, char *buf)
175 struct hv_device *hv_dev = device_to_hv_device(dev);
177 if (!hv_dev->channel)
178 return -ENODEV;
179 return sysfs_emit(buf, "{%pUl}\n",
180 &hv_dev->channel->offermsg.offer.if_instance);
182 static DEVICE_ATTR_RO(device_id);
184 static ssize_t modalias_show(struct device *dev,
185 struct device_attribute *dev_attr, char *buf)
187 struct hv_device *hv_dev = device_to_hv_device(dev);
189 return sysfs_emit(buf, "vmbus:%*phN\n", UUID_SIZE, &hv_dev->dev_type);
191 static DEVICE_ATTR_RO(modalias);
193 #ifdef CONFIG_NUMA
194 static ssize_t numa_node_show(struct device *dev,
195 struct device_attribute *attr, char *buf)
197 struct hv_device *hv_dev = device_to_hv_device(dev);
199 if (!hv_dev->channel)
200 return -ENODEV;
202 return sysfs_emit(buf, "%d\n", cpu_to_node(hv_dev->channel->target_cpu));
204 static DEVICE_ATTR_RO(numa_node);
205 #endif
207 static ssize_t server_monitor_pending_show(struct device *dev,
208 struct device_attribute *dev_attr,
209 char *buf)
211 struct hv_device *hv_dev = device_to_hv_device(dev);
213 if (!hv_dev->channel)
214 return -ENODEV;
215 return sysfs_emit(buf, "%d\n", channel_pending(hv_dev->channel,
216 vmbus_connection.monitor_pages[0]));
218 static DEVICE_ATTR_RO(server_monitor_pending);
220 static ssize_t client_monitor_pending_show(struct device *dev,
221 struct device_attribute *dev_attr,
222 char *buf)
224 struct hv_device *hv_dev = device_to_hv_device(dev);
226 if (!hv_dev->channel)
227 return -ENODEV;
228 return sysfs_emit(buf, "%d\n", channel_pending(hv_dev->channel,
229 vmbus_connection.monitor_pages[1]));
231 static DEVICE_ATTR_RO(client_monitor_pending);
233 static ssize_t server_monitor_latency_show(struct device *dev,
234 struct device_attribute *dev_attr,
235 char *buf)
237 struct hv_device *hv_dev = device_to_hv_device(dev);
239 if (!hv_dev->channel)
240 return -ENODEV;
241 return sysfs_emit(buf, "%d\n", channel_latency(hv_dev->channel,
242 vmbus_connection.monitor_pages[0]));
244 static DEVICE_ATTR_RO(server_monitor_latency);
246 static ssize_t client_monitor_latency_show(struct device *dev,
247 struct device_attribute *dev_attr,
248 char *buf)
250 struct hv_device *hv_dev = device_to_hv_device(dev);
252 if (!hv_dev->channel)
253 return -ENODEV;
254 return sysfs_emit(buf, "%d\n", channel_latency(hv_dev->channel,
255 vmbus_connection.monitor_pages[1]));
257 static DEVICE_ATTR_RO(client_monitor_latency);
259 static ssize_t server_monitor_conn_id_show(struct device *dev,
260 struct device_attribute *dev_attr,
261 char *buf)
263 struct hv_device *hv_dev = device_to_hv_device(dev);
265 if (!hv_dev->channel)
266 return -ENODEV;
267 return sysfs_emit(buf, "%d\n", channel_conn_id(hv_dev->channel,
268 vmbus_connection.monitor_pages[0]));
270 static DEVICE_ATTR_RO(server_monitor_conn_id);
272 static ssize_t client_monitor_conn_id_show(struct device *dev,
273 struct device_attribute *dev_attr,
274 char *buf)
276 struct hv_device *hv_dev = device_to_hv_device(dev);
278 if (!hv_dev->channel)
279 return -ENODEV;
280 return sysfs_emit(buf, "%d\n", channel_conn_id(hv_dev->channel,
281 vmbus_connection.monitor_pages[1]));
283 static DEVICE_ATTR_RO(client_monitor_conn_id);
285 static ssize_t out_intr_mask_show(struct device *dev,
286 struct device_attribute *dev_attr, char *buf)
288 struct hv_device *hv_dev = device_to_hv_device(dev);
289 struct hv_ring_buffer_debug_info outbound;
290 int ret;
292 if (!hv_dev->channel)
293 return -ENODEV;
295 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
296 &outbound);
297 if (ret < 0)
298 return ret;
300 return sysfs_emit(buf, "%d\n", outbound.current_interrupt_mask);
302 static DEVICE_ATTR_RO(out_intr_mask);
304 static ssize_t out_read_index_show(struct device *dev,
305 struct device_attribute *dev_attr, char *buf)
307 struct hv_device *hv_dev = device_to_hv_device(dev);
308 struct hv_ring_buffer_debug_info outbound;
309 int ret;
311 if (!hv_dev->channel)
312 return -ENODEV;
314 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
315 &outbound);
316 if (ret < 0)
317 return ret;
318 return sysfs_emit(buf, "%d\n", outbound.current_read_index);
320 static DEVICE_ATTR_RO(out_read_index);
322 static ssize_t out_write_index_show(struct device *dev,
323 struct device_attribute *dev_attr,
324 char *buf)
326 struct hv_device *hv_dev = device_to_hv_device(dev);
327 struct hv_ring_buffer_debug_info outbound;
328 int ret;
330 if (!hv_dev->channel)
331 return -ENODEV;
333 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
334 &outbound);
335 if (ret < 0)
336 return ret;
337 return sysfs_emit(buf, "%d\n", outbound.current_write_index);
339 static DEVICE_ATTR_RO(out_write_index);
341 static ssize_t out_read_bytes_avail_show(struct device *dev,
342 struct device_attribute *dev_attr,
343 char *buf)
345 struct hv_device *hv_dev = device_to_hv_device(dev);
346 struct hv_ring_buffer_debug_info outbound;
347 int ret;
349 if (!hv_dev->channel)
350 return -ENODEV;
352 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
353 &outbound);
354 if (ret < 0)
355 return ret;
356 return sysfs_emit(buf, "%d\n", outbound.bytes_avail_toread);
358 static DEVICE_ATTR_RO(out_read_bytes_avail);
360 static ssize_t out_write_bytes_avail_show(struct device *dev,
361 struct device_attribute *dev_attr,
362 char *buf)
364 struct hv_device *hv_dev = device_to_hv_device(dev);
365 struct hv_ring_buffer_debug_info outbound;
366 int ret;
368 if (!hv_dev->channel)
369 return -ENODEV;
371 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
372 &outbound);
373 if (ret < 0)
374 return ret;
375 return sysfs_emit(buf, "%d\n", outbound.bytes_avail_towrite);
377 static DEVICE_ATTR_RO(out_write_bytes_avail);
379 static ssize_t in_intr_mask_show(struct device *dev,
380 struct device_attribute *dev_attr, char *buf)
382 struct hv_device *hv_dev = device_to_hv_device(dev);
383 struct hv_ring_buffer_debug_info inbound;
384 int ret;
386 if (!hv_dev->channel)
387 return -ENODEV;
389 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
390 if (ret < 0)
391 return ret;
393 return sysfs_emit(buf, "%d\n", inbound.current_interrupt_mask);
395 static DEVICE_ATTR_RO(in_intr_mask);
397 static ssize_t in_read_index_show(struct device *dev,
398 struct device_attribute *dev_attr, char *buf)
400 struct hv_device *hv_dev = device_to_hv_device(dev);
401 struct hv_ring_buffer_debug_info inbound;
402 int ret;
404 if (!hv_dev->channel)
405 return -ENODEV;
407 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
408 if (ret < 0)
409 return ret;
411 return sysfs_emit(buf, "%d\n", inbound.current_read_index);
413 static DEVICE_ATTR_RO(in_read_index);
415 static ssize_t in_write_index_show(struct device *dev,
416 struct device_attribute *dev_attr, char *buf)
418 struct hv_device *hv_dev = device_to_hv_device(dev);
419 struct hv_ring_buffer_debug_info inbound;
420 int ret;
422 if (!hv_dev->channel)
423 return -ENODEV;
425 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
426 if (ret < 0)
427 return ret;
429 return sysfs_emit(buf, "%d\n", inbound.current_write_index);
431 static DEVICE_ATTR_RO(in_write_index);
433 static ssize_t in_read_bytes_avail_show(struct device *dev,
434 struct device_attribute *dev_attr,
435 char *buf)
437 struct hv_device *hv_dev = device_to_hv_device(dev);
438 struct hv_ring_buffer_debug_info inbound;
439 int ret;
441 if (!hv_dev->channel)
442 return -ENODEV;
444 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
445 if (ret < 0)
446 return ret;
448 return sysfs_emit(buf, "%d\n", inbound.bytes_avail_toread);
450 static DEVICE_ATTR_RO(in_read_bytes_avail);
452 static ssize_t in_write_bytes_avail_show(struct device *dev,
453 struct device_attribute *dev_attr,
454 char *buf)
456 struct hv_device *hv_dev = device_to_hv_device(dev);
457 struct hv_ring_buffer_debug_info inbound;
458 int ret;
460 if (!hv_dev->channel)
461 return -ENODEV;
463 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
464 if (ret < 0)
465 return ret;
467 return sysfs_emit(buf, "%d\n", inbound.bytes_avail_towrite);
469 static DEVICE_ATTR_RO(in_write_bytes_avail);
471 static ssize_t channel_vp_mapping_show(struct device *dev,
472 struct device_attribute *dev_attr,
473 char *buf)
475 struct hv_device *hv_dev = device_to_hv_device(dev);
476 struct vmbus_channel *channel = hv_dev->channel, *cur_sc;
477 int n_written;
478 struct list_head *cur;
480 if (!channel)
481 return -ENODEV;
483 mutex_lock(&vmbus_connection.channel_mutex);
485 n_written = sysfs_emit(buf, "%u:%u\n",
486 channel->offermsg.child_relid,
487 channel->target_cpu);
489 list_for_each(cur, &channel->sc_list) {
491 cur_sc = list_entry(cur, struct vmbus_channel, sc_list);
492 n_written += sysfs_emit_at(buf, n_written, "%u:%u\n",
493 cur_sc->offermsg.child_relid,
494 cur_sc->target_cpu);
497 mutex_unlock(&vmbus_connection.channel_mutex);
499 return n_written;
501 static DEVICE_ATTR_RO(channel_vp_mapping);
503 static ssize_t vendor_show(struct device *dev,
504 struct device_attribute *dev_attr,
505 char *buf)
507 struct hv_device *hv_dev = device_to_hv_device(dev);
509 return sysfs_emit(buf, "0x%x\n", hv_dev->vendor_id);
511 static DEVICE_ATTR_RO(vendor);
513 static ssize_t device_show(struct device *dev,
514 struct device_attribute *dev_attr,
515 char *buf)
517 struct hv_device *hv_dev = device_to_hv_device(dev);
519 return sysfs_emit(buf, "0x%x\n", hv_dev->device_id);
521 static DEVICE_ATTR_RO(device);
523 static ssize_t driver_override_store(struct device *dev,
524 struct device_attribute *attr,
525 const char *buf, size_t count)
527 struct hv_device *hv_dev = device_to_hv_device(dev);
528 int ret;
530 ret = driver_set_override(dev, &hv_dev->driver_override, buf, count);
531 if (ret)
532 return ret;
534 return count;
537 static ssize_t driver_override_show(struct device *dev,
538 struct device_attribute *attr, char *buf)
540 struct hv_device *hv_dev = device_to_hv_device(dev);
541 ssize_t len;
543 device_lock(dev);
544 len = sysfs_emit(buf, "%s\n", hv_dev->driver_override);
545 device_unlock(dev);
547 return len;
549 static DEVICE_ATTR_RW(driver_override);
551 /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
552 static struct attribute *vmbus_dev_attrs[] = {
553 &dev_attr_id.attr,
554 &dev_attr_state.attr,
555 &dev_attr_monitor_id.attr,
556 &dev_attr_class_id.attr,
557 &dev_attr_device_id.attr,
558 &dev_attr_modalias.attr,
559 #ifdef CONFIG_NUMA
560 &dev_attr_numa_node.attr,
561 #endif
562 &dev_attr_server_monitor_pending.attr,
563 &dev_attr_client_monitor_pending.attr,
564 &dev_attr_server_monitor_latency.attr,
565 &dev_attr_client_monitor_latency.attr,
566 &dev_attr_server_monitor_conn_id.attr,
567 &dev_attr_client_monitor_conn_id.attr,
568 &dev_attr_out_intr_mask.attr,
569 &dev_attr_out_read_index.attr,
570 &dev_attr_out_write_index.attr,
571 &dev_attr_out_read_bytes_avail.attr,
572 &dev_attr_out_write_bytes_avail.attr,
573 &dev_attr_in_intr_mask.attr,
574 &dev_attr_in_read_index.attr,
575 &dev_attr_in_write_index.attr,
576 &dev_attr_in_read_bytes_avail.attr,
577 &dev_attr_in_write_bytes_avail.attr,
578 &dev_attr_channel_vp_mapping.attr,
579 &dev_attr_vendor.attr,
580 &dev_attr_device.attr,
581 &dev_attr_driver_override.attr,
582 NULL,
586 * Device-level attribute_group callback function. Returns the permission for
587 * each attribute, and returns 0 if an attribute is not visible.
589 static umode_t vmbus_dev_attr_is_visible(struct kobject *kobj,
590 struct attribute *attr, int idx)
592 struct device *dev = kobj_to_dev(kobj);
593 const struct hv_device *hv_dev = device_to_hv_device(dev);
595 /* Hide the monitor attributes if the monitor mechanism is not used. */
596 if (!hv_dev->channel->offermsg.monitor_allocated &&
597 (attr == &dev_attr_monitor_id.attr ||
598 attr == &dev_attr_server_monitor_pending.attr ||
599 attr == &dev_attr_client_monitor_pending.attr ||
600 attr == &dev_attr_server_monitor_latency.attr ||
601 attr == &dev_attr_client_monitor_latency.attr ||
602 attr == &dev_attr_server_monitor_conn_id.attr ||
603 attr == &dev_attr_client_monitor_conn_id.attr))
604 return 0;
606 return attr->mode;
609 static const struct attribute_group vmbus_dev_group = {
610 .attrs = vmbus_dev_attrs,
611 .is_visible = vmbus_dev_attr_is_visible
613 __ATTRIBUTE_GROUPS(vmbus_dev);
615 /* Set up the attribute for /sys/bus/vmbus/hibernation */
616 static ssize_t hibernation_show(const struct bus_type *bus, char *buf)
618 return sprintf(buf, "%d\n", !!hv_is_hibernation_supported());
621 static BUS_ATTR_RO(hibernation);
623 static struct attribute *vmbus_bus_attrs[] = {
624 &bus_attr_hibernation.attr,
625 NULL,
627 static const struct attribute_group vmbus_bus_group = {
628 .attrs = vmbus_bus_attrs,
630 __ATTRIBUTE_GROUPS(vmbus_bus);
633 * vmbus_uevent - add uevent for our device
635 * This routine is invoked when a device is added or removed on the vmbus to
636 * generate a uevent to udev in the userspace. The udev will then look at its
637 * rule and the uevent generated here to load the appropriate driver
639 * The alias string will be of the form vmbus:guid where guid is the string
640 * representation of the device guid (each byte of the guid will be
641 * represented with two hex characters.
643 static int vmbus_uevent(const struct device *device, struct kobj_uevent_env *env)
645 const struct hv_device *dev = device_to_hv_device(device);
646 const char *format = "MODALIAS=vmbus:%*phN";
648 return add_uevent_var(env, format, UUID_SIZE, &dev->dev_type);
651 static const struct hv_vmbus_device_id *
652 hv_vmbus_dev_match(const struct hv_vmbus_device_id *id, const guid_t *guid)
654 if (id == NULL)
655 return NULL; /* empty device table */
657 for (; !guid_is_null(&id->guid); id++)
658 if (guid_equal(&id->guid, guid))
659 return id;
661 return NULL;
664 static const struct hv_vmbus_device_id *
665 hv_vmbus_dynid_match(struct hv_driver *drv, const guid_t *guid)
667 const struct hv_vmbus_device_id *id = NULL;
668 struct vmbus_dynid *dynid;
670 spin_lock(&drv->dynids.lock);
671 list_for_each_entry(dynid, &drv->dynids.list, node) {
672 if (guid_equal(&dynid->id.guid, guid)) {
673 id = &dynid->id;
674 break;
677 spin_unlock(&drv->dynids.lock);
679 return id;
682 static const struct hv_vmbus_device_id vmbus_device_null;
685 * Return a matching hv_vmbus_device_id pointer.
686 * If there is no match, return NULL.
688 static const struct hv_vmbus_device_id *hv_vmbus_get_id(const struct hv_driver *drv,
689 struct hv_device *dev)
691 const guid_t *guid = &dev->dev_type;
692 const struct hv_vmbus_device_id *id;
694 /* When driver_override is set, only bind to the matching driver */
695 if (dev->driver_override && strcmp(dev->driver_override, drv->name))
696 return NULL;
698 /* Look at the dynamic ids first, before the static ones */
699 id = hv_vmbus_dynid_match((struct hv_driver *)drv, guid);
700 if (!id)
701 id = hv_vmbus_dev_match(drv->id_table, guid);
703 /* driver_override will always match, send a dummy id */
704 if (!id && dev->driver_override)
705 id = &vmbus_device_null;
707 return id;
710 /* vmbus_add_dynid - add a new device ID to this driver and re-probe devices */
711 static int vmbus_add_dynid(struct hv_driver *drv, guid_t *guid)
713 struct vmbus_dynid *dynid;
715 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
716 if (!dynid)
717 return -ENOMEM;
719 dynid->id.guid = *guid;
721 spin_lock(&drv->dynids.lock);
722 list_add_tail(&dynid->node, &drv->dynids.list);
723 spin_unlock(&drv->dynids.lock);
725 return driver_attach(&drv->driver);
728 static void vmbus_free_dynids(struct hv_driver *drv)
730 struct vmbus_dynid *dynid, *n;
732 spin_lock(&drv->dynids.lock);
733 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
734 list_del(&dynid->node);
735 kfree(dynid);
737 spin_unlock(&drv->dynids.lock);
741 * store_new_id - sysfs frontend to vmbus_add_dynid()
743 * Allow GUIDs to be added to an existing driver via sysfs.
745 static ssize_t new_id_store(struct device_driver *driver, const char *buf,
746 size_t count)
748 struct hv_driver *drv = drv_to_hv_drv(driver);
749 guid_t guid;
750 ssize_t retval;
752 retval = guid_parse(buf, &guid);
753 if (retval)
754 return retval;
756 if (hv_vmbus_dynid_match(drv, &guid))
757 return -EEXIST;
759 retval = vmbus_add_dynid(drv, &guid);
760 if (retval)
761 return retval;
762 return count;
764 static DRIVER_ATTR_WO(new_id);
767 * store_remove_id - remove a PCI device ID from this driver
769 * Removes a dynamic pci device ID to this driver.
771 static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
772 size_t count)
774 struct hv_driver *drv = drv_to_hv_drv(driver);
775 struct vmbus_dynid *dynid, *n;
776 guid_t guid;
777 ssize_t retval;
779 retval = guid_parse(buf, &guid);
780 if (retval)
781 return retval;
783 retval = -ENODEV;
784 spin_lock(&drv->dynids.lock);
785 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
786 struct hv_vmbus_device_id *id = &dynid->id;
788 if (guid_equal(&id->guid, &guid)) {
789 list_del(&dynid->node);
790 kfree(dynid);
791 retval = count;
792 break;
795 spin_unlock(&drv->dynids.lock);
797 return retval;
799 static DRIVER_ATTR_WO(remove_id);
801 static struct attribute *vmbus_drv_attrs[] = {
802 &driver_attr_new_id.attr,
803 &driver_attr_remove_id.attr,
804 NULL,
806 ATTRIBUTE_GROUPS(vmbus_drv);
810 * vmbus_match - Attempt to match the specified device to the specified driver
812 static int vmbus_match(struct device *device, const struct device_driver *driver)
814 const struct hv_driver *drv = drv_to_hv_drv(driver);
815 struct hv_device *hv_dev = device_to_hv_device(device);
817 /* The hv_sock driver handles all hv_sock offers. */
818 if (is_hvsock_channel(hv_dev->channel))
819 return drv->hvsock;
821 if (hv_vmbus_get_id(drv, hv_dev))
822 return 1;
824 return 0;
828 * vmbus_probe - Add the new vmbus's child device
830 static int vmbus_probe(struct device *child_device)
832 int ret = 0;
833 struct hv_driver *drv =
834 drv_to_hv_drv(child_device->driver);
835 struct hv_device *dev = device_to_hv_device(child_device);
836 const struct hv_vmbus_device_id *dev_id;
838 dev_id = hv_vmbus_get_id(drv, dev);
839 if (drv->probe) {
840 ret = drv->probe(dev, dev_id);
841 if (ret != 0)
842 pr_err("probe failed for device %s (%d)\n",
843 dev_name(child_device), ret);
845 } else {
846 pr_err("probe not set for driver %s\n",
847 dev_name(child_device));
848 ret = -ENODEV;
850 return ret;
854 * vmbus_dma_configure -- Configure DMA coherence for VMbus device
856 static int vmbus_dma_configure(struct device *child_device)
859 * On ARM64, propagate the DMA coherence setting from the top level
860 * VMbus ACPI device to the child VMbus device being added here.
861 * On x86/x64 coherence is assumed and these calls have no effect.
863 hv_setup_dma_ops(child_device,
864 device_get_dma_attr(hv_dev) == DEV_DMA_COHERENT);
865 return 0;
869 * vmbus_remove - Remove a vmbus device
871 static void vmbus_remove(struct device *child_device)
873 struct hv_driver *drv;
874 struct hv_device *dev = device_to_hv_device(child_device);
876 if (child_device->driver) {
877 drv = drv_to_hv_drv(child_device->driver);
878 if (drv->remove)
879 drv->remove(dev);
884 * vmbus_shutdown - Shutdown a vmbus device
886 static void vmbus_shutdown(struct device *child_device)
888 struct hv_driver *drv;
889 struct hv_device *dev = device_to_hv_device(child_device);
892 /* The device may not be attached yet */
893 if (!child_device->driver)
894 return;
896 drv = drv_to_hv_drv(child_device->driver);
898 if (drv->shutdown)
899 drv->shutdown(dev);
902 #ifdef CONFIG_PM_SLEEP
904 * vmbus_suspend - Suspend a vmbus device
906 static int vmbus_suspend(struct device *child_device)
908 struct hv_driver *drv;
909 struct hv_device *dev = device_to_hv_device(child_device);
911 /* The device may not be attached yet */
912 if (!child_device->driver)
913 return 0;
915 drv = drv_to_hv_drv(child_device->driver);
916 if (!drv->suspend)
917 return -EOPNOTSUPP;
919 return drv->suspend(dev);
923 * vmbus_resume - Resume a vmbus device
925 static int vmbus_resume(struct device *child_device)
927 struct hv_driver *drv;
928 struct hv_device *dev = device_to_hv_device(child_device);
930 /* The device may not be attached yet */
931 if (!child_device->driver)
932 return 0;
934 drv = drv_to_hv_drv(child_device->driver);
935 if (!drv->resume)
936 return -EOPNOTSUPP;
938 return drv->resume(dev);
940 #else
941 #define vmbus_suspend NULL
942 #define vmbus_resume NULL
943 #endif /* CONFIG_PM_SLEEP */
946 * vmbus_device_release - Final callback release of the vmbus child device
948 static void vmbus_device_release(struct device *device)
950 struct hv_device *hv_dev = device_to_hv_device(device);
951 struct vmbus_channel *channel = hv_dev->channel;
953 hv_debug_rm_dev_dir(hv_dev);
955 mutex_lock(&vmbus_connection.channel_mutex);
956 hv_process_channel_removal(channel);
957 mutex_unlock(&vmbus_connection.channel_mutex);
958 kfree(hv_dev);
962 * Note: we must use the "noirq" ops: see the comment before vmbus_bus_pm.
964 * suspend_noirq/resume_noirq are set to NULL to support Suspend-to-Idle: we
965 * shouldn't suspend the vmbus devices upon Suspend-to-Idle, otherwise there
966 * is no way to wake up a Generation-2 VM.
968 * The other 4 ops are for hibernation.
971 static const struct dev_pm_ops vmbus_pm = {
972 .suspend_noirq = NULL,
973 .resume_noirq = NULL,
974 .freeze_noirq = vmbus_suspend,
975 .thaw_noirq = vmbus_resume,
976 .poweroff_noirq = vmbus_suspend,
977 .restore_noirq = vmbus_resume,
980 /* The one and only one */
981 static const struct bus_type hv_bus = {
982 .name = "vmbus",
983 .match = vmbus_match,
984 .shutdown = vmbus_shutdown,
985 .remove = vmbus_remove,
986 .probe = vmbus_probe,
987 .uevent = vmbus_uevent,
988 .dma_configure = vmbus_dma_configure,
989 .dev_groups = vmbus_dev_groups,
990 .drv_groups = vmbus_drv_groups,
991 .bus_groups = vmbus_bus_groups,
992 .pm = &vmbus_pm,
995 struct onmessage_work_context {
996 struct work_struct work;
997 struct {
998 struct hv_message_header header;
999 u8 payload[];
1000 } msg;
1003 static void vmbus_onmessage_work(struct work_struct *work)
1005 struct onmessage_work_context *ctx;
1007 /* Do not process messages if we're in DISCONNECTED state */
1008 if (vmbus_connection.conn_state == DISCONNECTED)
1009 return;
1011 ctx = container_of(work, struct onmessage_work_context,
1012 work);
1013 vmbus_onmessage((struct vmbus_channel_message_header *)
1014 &ctx->msg.payload);
1015 kfree(ctx);
1018 void vmbus_on_msg_dpc(unsigned long data)
1020 struct hv_per_cpu_context *hv_cpu = (void *)data;
1021 void *page_addr = hv_cpu->synic_message_page;
1022 struct hv_message msg_copy, *msg = (struct hv_message *)page_addr +
1023 VMBUS_MESSAGE_SINT;
1024 struct vmbus_channel_message_header *hdr;
1025 enum vmbus_channel_message_type msgtype;
1026 const struct vmbus_channel_message_table_entry *entry;
1027 struct onmessage_work_context *ctx;
1028 __u8 payload_size;
1029 u32 message_type;
1032 * 'enum vmbus_channel_message_type' is supposed to always be 'u32' as
1033 * it is being used in 'struct vmbus_channel_message_header' definition
1034 * which is supposed to match hypervisor ABI.
1036 BUILD_BUG_ON(sizeof(enum vmbus_channel_message_type) != sizeof(u32));
1039 * Since the message is in memory shared with the host, an erroneous or
1040 * malicious Hyper-V could modify the message while vmbus_on_msg_dpc()
1041 * or individual message handlers are executing; to prevent this, copy
1042 * the message into private memory.
1044 memcpy(&msg_copy, msg, sizeof(struct hv_message));
1046 message_type = msg_copy.header.message_type;
1047 if (message_type == HVMSG_NONE)
1048 /* no msg */
1049 return;
1051 hdr = (struct vmbus_channel_message_header *)msg_copy.u.payload;
1052 msgtype = hdr->msgtype;
1054 trace_vmbus_on_msg_dpc(hdr);
1056 if (msgtype >= CHANNELMSG_COUNT) {
1057 WARN_ONCE(1, "unknown msgtype=%d\n", msgtype);
1058 goto msg_handled;
1061 payload_size = msg_copy.header.payload_size;
1062 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT) {
1063 WARN_ONCE(1, "payload size is too large (%d)\n", payload_size);
1064 goto msg_handled;
1067 entry = &channel_message_table[msgtype];
1069 if (!entry->message_handler)
1070 goto msg_handled;
1072 if (payload_size < entry->min_payload_len) {
1073 WARN_ONCE(1, "message too short: msgtype=%d len=%d\n", msgtype, payload_size);
1074 goto msg_handled;
1077 if (entry->handler_type == VMHT_BLOCKING) {
1078 ctx = kmalloc(struct_size(ctx, msg.payload, payload_size), GFP_ATOMIC);
1079 if (ctx == NULL)
1080 return;
1082 INIT_WORK(&ctx->work, vmbus_onmessage_work);
1083 ctx->msg.header = msg_copy.header;
1084 memcpy(&ctx->msg.payload, msg_copy.u.payload, payload_size);
1087 * The host can generate a rescind message while we
1088 * may still be handling the original offer. We deal with
1089 * this condition by relying on the synchronization provided
1090 * by offer_in_progress and by channel_mutex. See also the
1091 * inline comments in vmbus_onoffer_rescind().
1093 switch (msgtype) {
1094 case CHANNELMSG_RESCIND_CHANNELOFFER:
1096 * If we are handling the rescind message;
1097 * schedule the work on the global work queue.
1099 * The OFFER message and the RESCIND message should
1100 * not be handled by the same serialized work queue,
1101 * because the OFFER handler may call vmbus_open(),
1102 * which tries to open the channel by sending an
1103 * OPEN_CHANNEL message to the host and waits for
1104 * the host's response; however, if the host has
1105 * rescinded the channel before it receives the
1106 * OPEN_CHANNEL message, the host just silently
1107 * ignores the OPEN_CHANNEL message; as a result,
1108 * the guest's OFFER handler hangs for ever, if we
1109 * handle the RESCIND message in the same serialized
1110 * work queue: the RESCIND handler can not start to
1111 * run before the OFFER handler finishes.
1113 if (vmbus_connection.ignore_any_offer_msg)
1114 break;
1115 queue_work(vmbus_connection.rescind_work_queue, &ctx->work);
1116 break;
1118 case CHANNELMSG_OFFERCHANNEL:
1120 * The host sends the offer message of a given channel
1121 * before sending the rescind message of the same
1122 * channel. These messages are sent to the guest's
1123 * connect CPU; the guest then starts processing them
1124 * in the tasklet handler on this CPU:
1126 * VMBUS_CONNECT_CPU
1128 * [vmbus_on_msg_dpc()]
1129 * atomic_inc() // CHANNELMSG_OFFERCHANNEL
1130 * queue_work()
1131 * ...
1132 * [vmbus_on_msg_dpc()]
1133 * schedule_work() // CHANNELMSG_RESCIND_CHANNELOFFER
1135 * We rely on the memory-ordering properties of the
1136 * queue_work() and schedule_work() primitives, which
1137 * guarantee that the atomic increment will be visible
1138 * to the CPUs which will execute the offer & rescind
1139 * works by the time these works will start execution.
1141 if (vmbus_connection.ignore_any_offer_msg)
1142 break;
1143 atomic_inc(&vmbus_connection.offer_in_progress);
1144 fallthrough;
1146 default:
1147 queue_work(vmbus_connection.work_queue, &ctx->work);
1149 } else
1150 entry->message_handler(hdr);
1152 msg_handled:
1153 vmbus_signal_eom(msg, message_type);
1156 #ifdef CONFIG_PM_SLEEP
1158 * Fake RESCIND_CHANNEL messages to clean up hv_sock channels by force for
1159 * hibernation, because hv_sock connections can not persist across hibernation.
1161 static void vmbus_force_channel_rescinded(struct vmbus_channel *channel)
1163 struct onmessage_work_context *ctx;
1164 struct vmbus_channel_rescind_offer *rescind;
1166 WARN_ON(!is_hvsock_channel(channel));
1169 * Allocation size is small and the allocation should really not fail,
1170 * otherwise the state of the hv_sock connections ends up in limbo.
1172 ctx = kzalloc(sizeof(*ctx) + sizeof(*rescind),
1173 GFP_KERNEL | __GFP_NOFAIL);
1176 * So far, these are not really used by Linux. Just set them to the
1177 * reasonable values conforming to the definitions of the fields.
1179 ctx->msg.header.message_type = 1;
1180 ctx->msg.header.payload_size = sizeof(*rescind);
1182 /* These values are actually used by Linux. */
1183 rescind = (struct vmbus_channel_rescind_offer *)ctx->msg.payload;
1184 rescind->header.msgtype = CHANNELMSG_RESCIND_CHANNELOFFER;
1185 rescind->child_relid = channel->offermsg.child_relid;
1187 INIT_WORK(&ctx->work, vmbus_onmessage_work);
1189 queue_work(vmbus_connection.work_queue, &ctx->work);
1191 #endif /* CONFIG_PM_SLEEP */
1194 * Schedule all channels with events pending
1196 static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)
1198 unsigned long *recv_int_page;
1199 u32 maxbits, relid;
1202 * The event page can be directly checked to get the id of
1203 * the channel that has the interrupt pending.
1205 void *page_addr = hv_cpu->synic_event_page;
1206 union hv_synic_event_flags *event
1207 = (union hv_synic_event_flags *)page_addr +
1208 VMBUS_MESSAGE_SINT;
1210 maxbits = HV_EVENT_FLAGS_COUNT;
1211 recv_int_page = event->flags;
1213 if (unlikely(!recv_int_page))
1214 return;
1216 for_each_set_bit(relid, recv_int_page, maxbits) {
1217 void (*callback_fn)(void *context);
1218 struct vmbus_channel *channel;
1220 if (!sync_test_and_clear_bit(relid, recv_int_page))
1221 continue;
1223 /* Special case - vmbus channel protocol msg */
1224 if (relid == 0)
1225 continue;
1228 * Pairs with the kfree_rcu() in vmbus_chan_release().
1229 * Guarantees that the channel data structure doesn't
1230 * get freed while the channel pointer below is being
1231 * dereferenced.
1233 rcu_read_lock();
1235 /* Find channel based on relid */
1236 channel = relid2channel(relid);
1237 if (channel == NULL)
1238 goto sched_unlock_rcu;
1240 if (channel->rescind)
1241 goto sched_unlock_rcu;
1244 * Make sure that the ring buffer data structure doesn't get
1245 * freed while we dereference the ring buffer pointer. Test
1246 * for the channel's onchannel_callback being NULL within a
1247 * sched_lock critical section. See also the inline comments
1248 * in vmbus_reset_channel_cb().
1250 spin_lock(&channel->sched_lock);
1252 callback_fn = channel->onchannel_callback;
1253 if (unlikely(callback_fn == NULL))
1254 goto sched_unlock;
1256 trace_vmbus_chan_sched(channel);
1258 ++channel->interrupts;
1260 switch (channel->callback_mode) {
1261 case HV_CALL_ISR:
1262 (*callback_fn)(channel->channel_callback_context);
1263 break;
1265 case HV_CALL_BATCHED:
1266 hv_begin_read(&channel->inbound);
1267 fallthrough;
1268 case HV_CALL_DIRECT:
1269 tasklet_schedule(&channel->callback_event);
1272 sched_unlock:
1273 spin_unlock(&channel->sched_lock);
1274 sched_unlock_rcu:
1275 rcu_read_unlock();
1279 static void vmbus_isr(void)
1281 struct hv_per_cpu_context *hv_cpu
1282 = this_cpu_ptr(hv_context.cpu_context);
1283 void *page_addr;
1284 struct hv_message *msg;
1286 vmbus_chan_sched(hv_cpu);
1288 page_addr = hv_cpu->synic_message_page;
1289 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
1291 /* Check if there are actual msgs to be processed */
1292 if (msg->header.message_type != HVMSG_NONE) {
1293 if (msg->header.message_type == HVMSG_TIMER_EXPIRED) {
1294 hv_stimer0_isr();
1295 vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED);
1296 } else
1297 tasklet_schedule(&hv_cpu->msg_dpc);
1300 add_interrupt_randomness(vmbus_interrupt);
1303 static irqreturn_t vmbus_percpu_isr(int irq, void *dev_id)
1305 vmbus_isr();
1306 return IRQ_HANDLED;
1309 static void vmbus_percpu_work(struct work_struct *work)
1311 unsigned int cpu = smp_processor_id();
1313 hv_synic_init(cpu);
1317 * vmbus_bus_init -Main vmbus driver initialization routine.
1319 * Here, we
1320 * - initialize the vmbus driver context
1321 * - invoke the vmbus hv main init routine
1322 * - retrieve the channel offers
1324 static int vmbus_bus_init(void)
1326 int ret, cpu;
1327 struct work_struct __percpu *works;
1329 ret = hv_init();
1330 if (ret != 0) {
1331 pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
1332 return ret;
1335 ret = bus_register(&hv_bus);
1336 if (ret)
1337 return ret;
1340 * VMbus interrupts are best modeled as per-cpu interrupts. If
1341 * on an architecture with support for per-cpu IRQs (e.g. ARM64),
1342 * allocate a per-cpu IRQ using standard Linux kernel functionality.
1343 * If not on such an architecture (e.g., x86/x64), then rely on
1344 * code in the arch-specific portion of the code tree to connect
1345 * the VMbus interrupt handler.
1348 if (vmbus_irq == -1) {
1349 hv_setup_vmbus_handler(vmbus_isr);
1350 } else {
1351 vmbus_evt = alloc_percpu(long);
1352 ret = request_percpu_irq(vmbus_irq, vmbus_percpu_isr,
1353 "Hyper-V VMbus", vmbus_evt);
1354 if (ret) {
1355 pr_err("Can't request Hyper-V VMbus IRQ %d, Err %d",
1356 vmbus_irq, ret);
1357 free_percpu(vmbus_evt);
1358 goto err_setup;
1362 ret = hv_synic_alloc();
1363 if (ret)
1364 goto err_alloc;
1366 works = alloc_percpu(struct work_struct);
1367 if (!works) {
1368 ret = -ENOMEM;
1369 goto err_alloc;
1373 * Initialize the per-cpu interrupt state and stimer state.
1374 * Then connect to the host.
1376 cpus_read_lock();
1377 for_each_online_cpu(cpu) {
1378 struct work_struct *work = per_cpu_ptr(works, cpu);
1380 INIT_WORK(work, vmbus_percpu_work);
1381 schedule_work_on(cpu, work);
1384 for_each_online_cpu(cpu)
1385 flush_work(per_cpu_ptr(works, cpu));
1387 /* Register the callbacks for possible CPU online/offline'ing */
1388 ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online",
1389 hv_synic_init, hv_synic_cleanup);
1390 cpus_read_unlock();
1391 free_percpu(works);
1392 if (ret < 0)
1393 goto err_alloc;
1394 hyperv_cpuhp_online = ret;
1396 ret = vmbus_connect();
1397 if (ret)
1398 goto err_connect;
1401 * Always register the vmbus unload panic notifier because we
1402 * need to shut the VMbus channel connection on panic.
1404 atomic_notifier_chain_register(&panic_notifier_list,
1405 &hyperv_panic_vmbus_unload_block);
1407 vmbus_request_offers();
1409 return 0;
1411 err_connect:
1412 cpuhp_remove_state(hyperv_cpuhp_online);
1413 err_alloc:
1414 hv_synic_free();
1415 if (vmbus_irq == -1) {
1416 hv_remove_vmbus_handler();
1417 } else {
1418 free_percpu_irq(vmbus_irq, vmbus_evt);
1419 free_percpu(vmbus_evt);
1421 err_setup:
1422 bus_unregister(&hv_bus);
1423 return ret;
1427 * __vmbus_driver_register() - Register a vmbus's driver
1428 * @hv_driver: Pointer to driver structure you want to register
1429 * @owner: owner module of the drv
1430 * @mod_name: module name string
1432 * Registers the given driver with Linux through the 'driver_register()' call
1433 * and sets up the hyper-v vmbus handling for this driver.
1434 * It will return the state of the 'driver_register()' call.
1437 int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
1439 int ret;
1441 pr_info("registering driver %s\n", hv_driver->name);
1443 ret = vmbus_exists();
1444 if (ret < 0)
1445 return ret;
1447 hv_driver->driver.name = hv_driver->name;
1448 hv_driver->driver.owner = owner;
1449 hv_driver->driver.mod_name = mod_name;
1450 hv_driver->driver.bus = &hv_bus;
1452 spin_lock_init(&hv_driver->dynids.lock);
1453 INIT_LIST_HEAD(&hv_driver->dynids.list);
1455 ret = driver_register(&hv_driver->driver);
1457 return ret;
1459 EXPORT_SYMBOL_GPL(__vmbus_driver_register);
1462 * vmbus_driver_unregister() - Unregister a vmbus's driver
1463 * @hv_driver: Pointer to driver structure you want to
1464 * un-register
1466 * Un-register the given driver that was previous registered with a call to
1467 * vmbus_driver_register()
1469 void vmbus_driver_unregister(struct hv_driver *hv_driver)
1471 pr_info("unregistering driver %s\n", hv_driver->name);
1473 if (!vmbus_exists()) {
1474 driver_unregister(&hv_driver->driver);
1475 vmbus_free_dynids(hv_driver);
1478 EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
1482 * Called when last reference to channel is gone.
1484 static void vmbus_chan_release(struct kobject *kobj)
1486 struct vmbus_channel *channel
1487 = container_of(kobj, struct vmbus_channel, kobj);
1489 kfree_rcu(channel, rcu);
1492 struct vmbus_chan_attribute {
1493 struct attribute attr;
1494 ssize_t (*show)(struct vmbus_channel *chan, char *buf);
1495 ssize_t (*store)(struct vmbus_channel *chan,
1496 const char *buf, size_t count);
1498 #define VMBUS_CHAN_ATTR(_name, _mode, _show, _store) \
1499 struct vmbus_chan_attribute chan_attr_##_name \
1500 = __ATTR(_name, _mode, _show, _store)
1501 #define VMBUS_CHAN_ATTR_RW(_name) \
1502 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RW(_name)
1503 #define VMBUS_CHAN_ATTR_RO(_name) \
1504 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RO(_name)
1505 #define VMBUS_CHAN_ATTR_WO(_name) \
1506 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_WO(_name)
1508 static ssize_t vmbus_chan_attr_show(struct kobject *kobj,
1509 struct attribute *attr, char *buf)
1511 const struct vmbus_chan_attribute *attribute
1512 = container_of(attr, struct vmbus_chan_attribute, attr);
1513 struct vmbus_channel *chan
1514 = container_of(kobj, struct vmbus_channel, kobj);
1516 if (!attribute->show)
1517 return -EIO;
1519 return attribute->show(chan, buf);
1522 static ssize_t vmbus_chan_attr_store(struct kobject *kobj,
1523 struct attribute *attr, const char *buf,
1524 size_t count)
1526 const struct vmbus_chan_attribute *attribute
1527 = container_of(attr, struct vmbus_chan_attribute, attr);
1528 struct vmbus_channel *chan
1529 = container_of(kobj, struct vmbus_channel, kobj);
1531 if (!attribute->store)
1532 return -EIO;
1534 return attribute->store(chan, buf, count);
1537 static const struct sysfs_ops vmbus_chan_sysfs_ops = {
1538 .show = vmbus_chan_attr_show,
1539 .store = vmbus_chan_attr_store,
1542 static ssize_t out_mask_show(struct vmbus_channel *channel, char *buf)
1544 struct hv_ring_buffer_info *rbi = &channel->outbound;
1545 ssize_t ret;
1547 mutex_lock(&rbi->ring_buffer_mutex);
1548 if (!rbi->ring_buffer) {
1549 mutex_unlock(&rbi->ring_buffer_mutex);
1550 return -EINVAL;
1553 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1554 mutex_unlock(&rbi->ring_buffer_mutex);
1555 return ret;
1557 static VMBUS_CHAN_ATTR_RO(out_mask);
1559 static ssize_t in_mask_show(struct vmbus_channel *channel, char *buf)
1561 struct hv_ring_buffer_info *rbi = &channel->inbound;
1562 ssize_t ret;
1564 mutex_lock(&rbi->ring_buffer_mutex);
1565 if (!rbi->ring_buffer) {
1566 mutex_unlock(&rbi->ring_buffer_mutex);
1567 return -EINVAL;
1570 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1571 mutex_unlock(&rbi->ring_buffer_mutex);
1572 return ret;
1574 static VMBUS_CHAN_ATTR_RO(in_mask);
1576 static ssize_t read_avail_show(struct vmbus_channel *channel, char *buf)
1578 struct hv_ring_buffer_info *rbi = &channel->inbound;
1579 ssize_t ret;
1581 mutex_lock(&rbi->ring_buffer_mutex);
1582 if (!rbi->ring_buffer) {
1583 mutex_unlock(&rbi->ring_buffer_mutex);
1584 return -EINVAL;
1587 ret = sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi));
1588 mutex_unlock(&rbi->ring_buffer_mutex);
1589 return ret;
1591 static VMBUS_CHAN_ATTR_RO(read_avail);
1593 static ssize_t write_avail_show(struct vmbus_channel *channel, char *buf)
1595 struct hv_ring_buffer_info *rbi = &channel->outbound;
1596 ssize_t ret;
1598 mutex_lock(&rbi->ring_buffer_mutex);
1599 if (!rbi->ring_buffer) {
1600 mutex_unlock(&rbi->ring_buffer_mutex);
1601 return -EINVAL;
1604 ret = sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi));
1605 mutex_unlock(&rbi->ring_buffer_mutex);
1606 return ret;
1608 static VMBUS_CHAN_ATTR_RO(write_avail);
1610 static ssize_t target_cpu_show(struct vmbus_channel *channel, char *buf)
1612 return sprintf(buf, "%u\n", channel->target_cpu);
1614 static ssize_t target_cpu_store(struct vmbus_channel *channel,
1615 const char *buf, size_t count)
1617 u32 target_cpu, origin_cpu;
1618 ssize_t ret = count;
1620 if (vmbus_proto_version < VERSION_WIN10_V4_1)
1621 return -EIO;
1623 if (sscanf(buf, "%uu", &target_cpu) != 1)
1624 return -EIO;
1626 /* Validate target_cpu for the cpumask_test_cpu() operation below. */
1627 if (target_cpu >= nr_cpumask_bits)
1628 return -EINVAL;
1630 if (!cpumask_test_cpu(target_cpu, housekeeping_cpumask(HK_TYPE_MANAGED_IRQ)))
1631 return -EINVAL;
1633 /* No CPUs should come up or down during this. */
1634 cpus_read_lock();
1636 if (!cpu_online(target_cpu)) {
1637 cpus_read_unlock();
1638 return -EINVAL;
1642 * Synchronizes target_cpu_store() and channel closure:
1644 * { Initially: state = CHANNEL_OPENED }
1646 * CPU1 CPU2
1648 * [target_cpu_store()] [vmbus_disconnect_ring()]
1650 * LOCK channel_mutex LOCK channel_mutex
1651 * LOAD r1 = state LOAD r2 = state
1652 * IF (r1 == CHANNEL_OPENED) IF (r2 == CHANNEL_OPENED)
1653 * SEND MODIFYCHANNEL STORE state = CHANNEL_OPEN
1654 * [...] SEND CLOSECHANNEL
1655 * UNLOCK channel_mutex UNLOCK channel_mutex
1657 * Forbids: r1 == r2 == CHANNEL_OPENED (i.e., CPU1's LOCK precedes
1658 * CPU2's LOCK) && CPU2's SEND precedes CPU1's SEND
1660 * Note. The host processes the channel messages "sequentially", in
1661 * the order in which they are received on a per-partition basis.
1663 mutex_lock(&vmbus_connection.channel_mutex);
1666 * Hyper-V will ignore MODIFYCHANNEL messages for "non-open" channels;
1667 * avoid sending the message and fail here for such channels.
1669 if (channel->state != CHANNEL_OPENED_STATE) {
1670 ret = -EIO;
1671 goto cpu_store_unlock;
1674 origin_cpu = channel->target_cpu;
1675 if (target_cpu == origin_cpu)
1676 goto cpu_store_unlock;
1678 if (vmbus_send_modifychannel(channel,
1679 hv_cpu_number_to_vp_number(target_cpu))) {
1680 ret = -EIO;
1681 goto cpu_store_unlock;
1685 * For version before VERSION_WIN10_V5_3, the following warning holds:
1687 * Warning. At this point, there is *no* guarantee that the host will
1688 * have successfully processed the vmbus_send_modifychannel() request.
1689 * See the header comment of vmbus_send_modifychannel() for more info.
1691 * Lags in the processing of the above vmbus_send_modifychannel() can
1692 * result in missed interrupts if the "old" target CPU is taken offline
1693 * before Hyper-V starts sending interrupts to the "new" target CPU.
1694 * But apart from this offlining scenario, the code tolerates such
1695 * lags. It will function correctly even if a channel interrupt comes
1696 * in on a CPU that is different from the channel target_cpu value.
1699 channel->target_cpu = target_cpu;
1701 /* See init_vp_index(). */
1702 if (hv_is_perf_channel(channel))
1703 hv_update_allocated_cpus(origin_cpu, target_cpu);
1705 /* Currently set only for storvsc channels. */
1706 if (channel->change_target_cpu_callback) {
1707 (*channel->change_target_cpu_callback)(channel,
1708 origin_cpu, target_cpu);
1711 cpu_store_unlock:
1712 mutex_unlock(&vmbus_connection.channel_mutex);
1713 cpus_read_unlock();
1714 return ret;
1716 static VMBUS_CHAN_ATTR(cpu, 0644, target_cpu_show, target_cpu_store);
1718 static ssize_t channel_pending_show(struct vmbus_channel *channel,
1719 char *buf)
1721 return sprintf(buf, "%d\n",
1722 channel_pending(channel,
1723 vmbus_connection.monitor_pages[1]));
1725 static VMBUS_CHAN_ATTR(pending, 0444, channel_pending_show, NULL);
1727 static ssize_t channel_latency_show(struct vmbus_channel *channel,
1728 char *buf)
1730 return sprintf(buf, "%d\n",
1731 channel_latency(channel,
1732 vmbus_connection.monitor_pages[1]));
1734 static VMBUS_CHAN_ATTR(latency, 0444, channel_latency_show, NULL);
1736 static ssize_t channel_interrupts_show(struct vmbus_channel *channel, char *buf)
1738 return sprintf(buf, "%llu\n", channel->interrupts);
1740 static VMBUS_CHAN_ATTR(interrupts, 0444, channel_interrupts_show, NULL);
1742 static ssize_t channel_events_show(struct vmbus_channel *channel, char *buf)
1744 return sprintf(buf, "%llu\n", channel->sig_events);
1746 static VMBUS_CHAN_ATTR(events, 0444, channel_events_show, NULL);
1748 static ssize_t channel_intr_in_full_show(struct vmbus_channel *channel,
1749 char *buf)
1751 return sprintf(buf, "%llu\n",
1752 (unsigned long long)channel->intr_in_full);
1754 static VMBUS_CHAN_ATTR(intr_in_full, 0444, channel_intr_in_full_show, NULL);
1756 static ssize_t channel_intr_out_empty_show(struct vmbus_channel *channel,
1757 char *buf)
1759 return sprintf(buf, "%llu\n",
1760 (unsigned long long)channel->intr_out_empty);
1762 static VMBUS_CHAN_ATTR(intr_out_empty, 0444, channel_intr_out_empty_show, NULL);
1764 static ssize_t channel_out_full_first_show(struct vmbus_channel *channel,
1765 char *buf)
1767 return sprintf(buf, "%llu\n",
1768 (unsigned long long)channel->out_full_first);
1770 static VMBUS_CHAN_ATTR(out_full_first, 0444, channel_out_full_first_show, NULL);
1772 static ssize_t channel_out_full_total_show(struct vmbus_channel *channel,
1773 char *buf)
1775 return sprintf(buf, "%llu\n",
1776 (unsigned long long)channel->out_full_total);
1778 static VMBUS_CHAN_ATTR(out_full_total, 0444, channel_out_full_total_show, NULL);
1780 static ssize_t subchannel_monitor_id_show(struct vmbus_channel *channel,
1781 char *buf)
1783 return sprintf(buf, "%u\n", channel->offermsg.monitorid);
1785 static VMBUS_CHAN_ATTR(monitor_id, 0444, subchannel_monitor_id_show, NULL);
1787 static ssize_t subchannel_id_show(struct vmbus_channel *channel,
1788 char *buf)
1790 return sprintf(buf, "%u\n",
1791 channel->offermsg.offer.sub_channel_index);
1793 static VMBUS_CHAN_ATTR_RO(subchannel_id);
1795 static struct attribute *vmbus_chan_attrs[] = {
1796 &chan_attr_out_mask.attr,
1797 &chan_attr_in_mask.attr,
1798 &chan_attr_read_avail.attr,
1799 &chan_attr_write_avail.attr,
1800 &chan_attr_cpu.attr,
1801 &chan_attr_pending.attr,
1802 &chan_attr_latency.attr,
1803 &chan_attr_interrupts.attr,
1804 &chan_attr_events.attr,
1805 &chan_attr_intr_in_full.attr,
1806 &chan_attr_intr_out_empty.attr,
1807 &chan_attr_out_full_first.attr,
1808 &chan_attr_out_full_total.attr,
1809 &chan_attr_monitor_id.attr,
1810 &chan_attr_subchannel_id.attr,
1811 NULL
1815 * Channel-level attribute_group callback function. Returns the permission for
1816 * each attribute, and returns 0 if an attribute is not visible.
1818 static umode_t vmbus_chan_attr_is_visible(struct kobject *kobj,
1819 struct attribute *attr, int idx)
1821 const struct vmbus_channel *channel =
1822 container_of(kobj, struct vmbus_channel, kobj);
1824 /* Hide the monitor attributes if the monitor mechanism is not used. */
1825 if (!channel->offermsg.monitor_allocated &&
1826 (attr == &chan_attr_pending.attr ||
1827 attr == &chan_attr_latency.attr ||
1828 attr == &chan_attr_monitor_id.attr))
1829 return 0;
1831 return attr->mode;
1834 static const struct attribute_group vmbus_chan_group = {
1835 .attrs = vmbus_chan_attrs,
1836 .is_visible = vmbus_chan_attr_is_visible
1839 static const struct kobj_type vmbus_chan_ktype = {
1840 .sysfs_ops = &vmbus_chan_sysfs_ops,
1841 .release = vmbus_chan_release,
1845 * vmbus_add_channel_kobj - setup a sub-directory under device/channels
1847 int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
1849 const struct device *device = &dev->device;
1850 struct kobject *kobj = &channel->kobj;
1851 u32 relid = channel->offermsg.child_relid;
1852 int ret;
1854 kobj->kset = dev->channels_kset;
1855 ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
1856 "%u", relid);
1857 if (ret) {
1858 kobject_put(kobj);
1859 return ret;
1862 ret = sysfs_create_group(kobj, &vmbus_chan_group);
1864 if (ret) {
1866 * The calling functions' error handling paths will cleanup the
1867 * empty channel directory.
1869 kobject_put(kobj);
1870 dev_err(device, "Unable to set up channel sysfs files\n");
1871 return ret;
1874 kobject_uevent(kobj, KOBJ_ADD);
1876 return 0;
1880 * vmbus_remove_channel_attr_group - remove the channel's attribute group
1882 void vmbus_remove_channel_attr_group(struct vmbus_channel *channel)
1884 sysfs_remove_group(&channel->kobj, &vmbus_chan_group);
1888 * vmbus_device_create - Creates and registers a new child device
1889 * on the vmbus.
1891 struct hv_device *vmbus_device_create(const guid_t *type,
1892 const guid_t *instance,
1893 struct vmbus_channel *channel)
1895 struct hv_device *child_device_obj;
1897 child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
1898 if (!child_device_obj) {
1899 pr_err("Unable to allocate device object for child device\n");
1900 return NULL;
1903 child_device_obj->channel = channel;
1904 guid_copy(&child_device_obj->dev_type, type);
1905 guid_copy(&child_device_obj->dev_instance, instance);
1906 child_device_obj->vendor_id = PCI_VENDOR_ID_MICROSOFT;
1908 return child_device_obj;
1912 * vmbus_device_register - Register the child device
1914 int vmbus_device_register(struct hv_device *child_device_obj)
1916 struct kobject *kobj = &child_device_obj->device.kobj;
1917 int ret;
1919 dev_set_name(&child_device_obj->device, "%pUl",
1920 &child_device_obj->channel->offermsg.offer.if_instance);
1922 child_device_obj->device.bus = &hv_bus;
1923 child_device_obj->device.parent = hv_dev;
1924 child_device_obj->device.release = vmbus_device_release;
1926 child_device_obj->device.dma_parms = &child_device_obj->dma_parms;
1927 child_device_obj->device.dma_mask = &child_device_obj->dma_mask;
1928 dma_set_mask(&child_device_obj->device, DMA_BIT_MASK(64));
1931 * Register with the LDM. This will kick off the driver/device
1932 * binding...which will eventually call vmbus_match() and vmbus_probe()
1934 ret = device_register(&child_device_obj->device);
1935 if (ret) {
1936 pr_err("Unable to register child device\n");
1937 put_device(&child_device_obj->device);
1938 return ret;
1941 child_device_obj->channels_kset = kset_create_and_add("channels",
1942 NULL, kobj);
1943 if (!child_device_obj->channels_kset) {
1944 ret = -ENOMEM;
1945 goto err_dev_unregister;
1948 ret = vmbus_add_channel_kobj(child_device_obj,
1949 child_device_obj->channel);
1950 if (ret) {
1951 pr_err("Unable to register primary channeln");
1952 goto err_kset_unregister;
1954 hv_debug_add_dev_dir(child_device_obj);
1956 return 0;
1958 err_kset_unregister:
1959 kset_unregister(child_device_obj->channels_kset);
1961 err_dev_unregister:
1962 device_unregister(&child_device_obj->device);
1963 return ret;
1967 * vmbus_device_unregister - Remove the specified child device
1968 * from the vmbus.
1970 void vmbus_device_unregister(struct hv_device *device_obj)
1972 pr_debug("child device %s unregistered\n",
1973 dev_name(&device_obj->device));
1975 kset_unregister(device_obj->channels_kset);
1978 * Kick off the process of unregistering the device.
1979 * This will call vmbus_remove() and eventually vmbus_device_release()
1981 device_unregister(&device_obj->device);
1983 EXPORT_SYMBOL_GPL(vmbus_device_unregister);
1985 #ifdef CONFIG_ACPI
1987 * VMBUS is an acpi enumerated device. Get the information we
1988 * need from DSDT.
1990 static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
1992 resource_size_t start = 0;
1993 resource_size_t end = 0;
1994 struct resource *new_res;
1995 struct resource **old_res = &hyperv_mmio;
1996 struct resource **prev_res = NULL;
1997 struct resource r;
1999 switch (res->type) {
2002 * "Address" descriptors are for bus windows. Ignore
2003 * "memory" descriptors, which are for registers on
2004 * devices.
2006 case ACPI_RESOURCE_TYPE_ADDRESS32:
2007 start = res->data.address32.address.minimum;
2008 end = res->data.address32.address.maximum;
2009 break;
2011 case ACPI_RESOURCE_TYPE_ADDRESS64:
2012 start = res->data.address64.address.minimum;
2013 end = res->data.address64.address.maximum;
2014 break;
2017 * The IRQ information is needed only on ARM64, which Hyper-V
2018 * sets up in the extended format. IRQ information is present
2019 * on x86/x64 in the non-extended format but it is not used by
2020 * Linux. So don't bother checking for the non-extended format.
2022 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
2023 if (!acpi_dev_resource_interrupt(res, 0, &r)) {
2024 pr_err("Unable to parse Hyper-V ACPI interrupt\n");
2025 return AE_ERROR;
2027 /* ARM64 INTID for VMbus */
2028 vmbus_interrupt = res->data.extended_irq.interrupts[0];
2029 /* Linux IRQ number */
2030 vmbus_irq = r.start;
2031 return AE_OK;
2033 default:
2034 /* Unused resource type */
2035 return AE_OK;
2039 * Ignore ranges that are below 1MB, as they're not
2040 * necessary or useful here.
2042 if (end < 0x100000)
2043 return AE_OK;
2045 new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC);
2046 if (!new_res)
2047 return AE_NO_MEMORY;
2049 /* If this range overlaps the virtual TPM, truncate it. */
2050 if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS)
2051 end = VTPM_BASE_ADDRESS;
2053 new_res->name = "hyperv mmio";
2054 new_res->flags = IORESOURCE_MEM;
2055 new_res->start = start;
2056 new_res->end = end;
2059 * If two ranges are adjacent, merge them.
2061 do {
2062 if (!*old_res) {
2063 *old_res = new_res;
2064 break;
2067 if (((*old_res)->end + 1) == new_res->start) {
2068 (*old_res)->end = new_res->end;
2069 kfree(new_res);
2070 break;
2073 if ((*old_res)->start == new_res->end + 1) {
2074 (*old_res)->start = new_res->start;
2075 kfree(new_res);
2076 break;
2079 if ((*old_res)->start > new_res->end) {
2080 new_res->sibling = *old_res;
2081 if (prev_res)
2082 (*prev_res)->sibling = new_res;
2083 *old_res = new_res;
2084 break;
2087 prev_res = old_res;
2088 old_res = &(*old_res)->sibling;
2090 } while (1);
2092 return AE_OK;
2094 #endif
2096 static void vmbus_mmio_remove(void)
2098 struct resource *cur_res;
2099 struct resource *next_res;
2101 if (hyperv_mmio) {
2102 if (fb_mmio) {
2103 __release_region(hyperv_mmio, fb_mmio->start,
2104 resource_size(fb_mmio));
2105 fb_mmio = NULL;
2108 for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) {
2109 next_res = cur_res->sibling;
2110 kfree(cur_res);
2115 static void __maybe_unused vmbus_reserve_fb(void)
2117 resource_size_t start = 0, size;
2118 struct pci_dev *pdev;
2120 if (efi_enabled(EFI_BOOT)) {
2121 /* Gen2 VM: get FB base from EFI framebuffer */
2122 if (IS_ENABLED(CONFIG_SYSFB)) {
2123 start = screen_info.lfb_base;
2124 size = max_t(__u32, screen_info.lfb_size, 0x800000);
2126 } else {
2127 /* Gen1 VM: get FB base from PCI */
2128 pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
2129 PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
2130 if (!pdev)
2131 return;
2133 if (pdev->resource[0].flags & IORESOURCE_MEM) {
2134 start = pci_resource_start(pdev, 0);
2135 size = pci_resource_len(pdev, 0);
2139 * Release the PCI device so hyperv_drm or hyperv_fb driver can
2140 * grab it later.
2142 pci_dev_put(pdev);
2145 if (!start)
2146 return;
2149 * Make a claim for the frame buffer in the resource tree under the
2150 * first node, which will be the one below 4GB. The length seems to
2151 * be underreported, particularly in a Generation 1 VM. So start out
2152 * reserving a larger area and make it smaller until it succeeds.
2154 for (; !fb_mmio && (size >= 0x100000); size >>= 1)
2155 fb_mmio = __request_region(hyperv_mmio, start, size, fb_mmio_name, 0);
2159 * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
2160 * @new: If successful, supplied a pointer to the
2161 * allocated MMIO space.
2162 * @device_obj: Identifies the caller
2163 * @min: Minimum guest physical address of the
2164 * allocation
2165 * @max: Maximum guest physical address
2166 * @size: Size of the range to be allocated
2167 * @align: Alignment of the range to be allocated
2168 * @fb_overlap_ok: Whether this allocation can be allowed
2169 * to overlap the video frame buffer.
2171 * This function walks the resources granted to VMBus by the
2172 * _CRS object in the ACPI namespace underneath the parent
2173 * "bridge" whether that's a root PCI bus in the Generation 1
2174 * case or a Module Device in the Generation 2 case. It then
2175 * attempts to allocate from the global MMIO pool in a way that
2176 * matches the constraints supplied in these parameters and by
2177 * that _CRS.
2179 * Return: 0 on success, -errno on failure
2181 int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
2182 resource_size_t min, resource_size_t max,
2183 resource_size_t size, resource_size_t align,
2184 bool fb_overlap_ok)
2186 struct resource *iter, *shadow;
2187 resource_size_t range_min, range_max, start, end;
2188 const char *dev_n = dev_name(&device_obj->device);
2189 int retval;
2191 retval = -ENXIO;
2192 mutex_lock(&hyperv_mmio_lock);
2195 * If overlaps with frame buffers are allowed, then first attempt to
2196 * make the allocation from within the reserved region. Because it
2197 * is already reserved, no shadow allocation is necessary.
2199 if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) &&
2200 !(max < fb_mmio->start)) {
2202 range_min = fb_mmio->start;
2203 range_max = fb_mmio->end;
2204 start = (range_min + align - 1) & ~(align - 1);
2205 for (; start + size - 1 <= range_max; start += align) {
2206 *new = request_mem_region_exclusive(start, size, dev_n);
2207 if (*new) {
2208 retval = 0;
2209 goto exit;
2214 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
2215 if ((iter->start >= max) || (iter->end <= min))
2216 continue;
2218 range_min = iter->start;
2219 range_max = iter->end;
2220 start = (range_min + align - 1) & ~(align - 1);
2221 for (; start + size - 1 <= range_max; start += align) {
2222 end = start + size - 1;
2224 /* Skip the whole fb_mmio region if not fb_overlap_ok */
2225 if (!fb_overlap_ok && fb_mmio &&
2226 (((start >= fb_mmio->start) && (start <= fb_mmio->end)) ||
2227 ((end >= fb_mmio->start) && (end <= fb_mmio->end))))
2228 continue;
2230 shadow = __request_region(iter, start, size, NULL,
2231 IORESOURCE_BUSY);
2232 if (!shadow)
2233 continue;
2235 *new = request_mem_region_exclusive(start, size, dev_n);
2236 if (*new) {
2237 shadow->name = (char *)*new;
2238 retval = 0;
2239 goto exit;
2242 __release_region(iter, start, size);
2246 exit:
2247 mutex_unlock(&hyperv_mmio_lock);
2248 return retval;
2250 EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
2253 * vmbus_free_mmio() - Free a memory-mapped I/O range.
2254 * @start: Base address of region to release.
2255 * @size: Size of the range to be allocated
2257 * This function releases anything requested by
2258 * vmbus_mmio_allocate().
2260 void vmbus_free_mmio(resource_size_t start, resource_size_t size)
2262 struct resource *iter;
2264 mutex_lock(&hyperv_mmio_lock);
2265 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
2266 if ((iter->start >= start + size) || (iter->end <= start))
2267 continue;
2269 __release_region(iter, start, size);
2271 release_mem_region(start, size);
2272 mutex_unlock(&hyperv_mmio_lock);
2275 EXPORT_SYMBOL_GPL(vmbus_free_mmio);
2277 #ifdef CONFIG_ACPI
2278 static int vmbus_acpi_add(struct platform_device *pdev)
2280 acpi_status result;
2281 int ret_val = -ENODEV;
2282 struct acpi_device *ancestor;
2283 struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
2285 hv_dev = &device->dev;
2288 * Older versions of Hyper-V for ARM64 fail to include the _CCA
2289 * method on the top level VMbus device in the DSDT. But devices
2290 * are hardware coherent in all current Hyper-V use cases, so fix
2291 * up the ACPI device to behave as if _CCA is present and indicates
2292 * hardware coherence.
2294 ACPI_COMPANION_SET(&device->dev, device);
2295 if (IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED) &&
2296 device_get_dma_attr(&device->dev) == DEV_DMA_NOT_SUPPORTED) {
2297 pr_info("No ACPI _CCA found; assuming coherent device I/O\n");
2298 device->flags.cca_seen = true;
2299 device->flags.coherent_dma = true;
2302 result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
2303 vmbus_walk_resources, NULL);
2305 if (ACPI_FAILURE(result))
2306 goto acpi_walk_err;
2308 * Some ancestor of the vmbus acpi device (Gen1 or Gen2
2309 * firmware) is the VMOD that has the mmio ranges. Get that.
2311 for (ancestor = acpi_dev_parent(device);
2312 ancestor && ancestor->handle != ACPI_ROOT_OBJECT;
2313 ancestor = acpi_dev_parent(ancestor)) {
2314 result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
2315 vmbus_walk_resources, NULL);
2317 if (ACPI_FAILURE(result))
2318 continue;
2319 if (hyperv_mmio) {
2320 vmbus_reserve_fb();
2321 break;
2324 ret_val = 0;
2326 acpi_walk_err:
2327 if (ret_val)
2328 vmbus_mmio_remove();
2329 return ret_val;
2331 #else
2332 static int vmbus_acpi_add(struct platform_device *pdev)
2334 return 0;
2336 #endif
2338 static int vmbus_device_add(struct platform_device *pdev)
2340 struct resource **cur_res = &hyperv_mmio;
2341 struct of_range range;
2342 struct of_range_parser parser;
2343 struct device_node *np = pdev->dev.of_node;
2344 int ret;
2346 hv_dev = &pdev->dev;
2348 ret = of_range_parser_init(&parser, np);
2349 if (ret)
2350 return ret;
2352 for_each_of_range(&parser, &range) {
2353 struct resource *res;
2355 res = kzalloc(sizeof(*res), GFP_KERNEL);
2356 if (!res) {
2357 vmbus_mmio_remove();
2358 return -ENOMEM;
2361 res->name = "hyperv mmio";
2362 res->flags = range.flags;
2363 res->start = range.cpu_addr;
2364 res->end = range.cpu_addr + range.size;
2366 *cur_res = res;
2367 cur_res = &res->sibling;
2370 return ret;
2373 static int vmbus_platform_driver_probe(struct platform_device *pdev)
2375 if (acpi_disabled)
2376 return vmbus_device_add(pdev);
2377 else
2378 return vmbus_acpi_add(pdev);
2381 static void vmbus_platform_driver_remove(struct platform_device *pdev)
2383 vmbus_mmio_remove();
2386 #ifdef CONFIG_PM_SLEEP
2387 static int vmbus_bus_suspend(struct device *dev)
2389 struct hv_per_cpu_context *hv_cpu = per_cpu_ptr(
2390 hv_context.cpu_context, VMBUS_CONNECT_CPU);
2391 struct vmbus_channel *channel, *sc;
2393 tasklet_disable(&hv_cpu->msg_dpc);
2394 vmbus_connection.ignore_any_offer_msg = true;
2395 /* The tasklet_enable() takes care of providing a memory barrier */
2396 tasklet_enable(&hv_cpu->msg_dpc);
2398 /* Drain all the workqueues as we are in suspend */
2399 drain_workqueue(vmbus_connection.rescind_work_queue);
2400 drain_workqueue(vmbus_connection.work_queue);
2401 drain_workqueue(vmbus_connection.handle_primary_chan_wq);
2402 drain_workqueue(vmbus_connection.handle_sub_chan_wq);
2404 mutex_lock(&vmbus_connection.channel_mutex);
2405 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
2406 if (!is_hvsock_channel(channel))
2407 continue;
2409 vmbus_force_channel_rescinded(channel);
2411 mutex_unlock(&vmbus_connection.channel_mutex);
2414 * Wait until all the sub-channels and hv_sock channels have been
2415 * cleaned up. Sub-channels should be destroyed upon suspend, otherwise
2416 * they would conflict with the new sub-channels that will be created
2417 * in the resume path. hv_sock channels should also be destroyed, but
2418 * a hv_sock channel of an established hv_sock connection can not be
2419 * really destroyed since it may still be referenced by the userspace
2420 * application, so we just force the hv_sock channel to be rescinded
2421 * by vmbus_force_channel_rescinded(), and the userspace application
2422 * will thoroughly destroy the channel after hibernation.
2424 * Note: the counter nr_chan_close_on_suspend may never go above 0 if
2425 * the VM has no sub-channel and hv_sock channel, e.g. a 1-vCPU VM.
2427 if (atomic_read(&vmbus_connection.nr_chan_close_on_suspend) > 0)
2428 wait_for_completion(&vmbus_connection.ready_for_suspend_event);
2430 if (atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) != 0) {
2431 pr_err("Can not suspend due to a previous failed resuming\n");
2432 return -EBUSY;
2435 mutex_lock(&vmbus_connection.channel_mutex);
2437 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
2439 * Remove the channel from the array of channels and invalidate
2440 * the channel's relid. Upon resume, vmbus_onoffer() will fix
2441 * up the relid (and other fields, if necessary) and add the
2442 * channel back to the array.
2444 vmbus_channel_unmap_relid(channel);
2445 channel->offermsg.child_relid = INVALID_RELID;
2447 if (is_hvsock_channel(channel)) {
2448 if (!channel->rescind) {
2449 pr_err("hv_sock channel not rescinded!\n");
2450 WARN_ON_ONCE(1);
2452 continue;
2455 list_for_each_entry(sc, &channel->sc_list, sc_list) {
2456 pr_err("Sub-channel not deleted!\n");
2457 WARN_ON_ONCE(1);
2460 atomic_inc(&vmbus_connection.nr_chan_fixup_on_resume);
2463 mutex_unlock(&vmbus_connection.channel_mutex);
2465 vmbus_initiate_unload(false);
2467 /* Reset the event for the next resume. */
2468 reinit_completion(&vmbus_connection.ready_for_resume_event);
2470 return 0;
2473 static int vmbus_bus_resume(struct device *dev)
2475 struct vmbus_channel_msginfo *msginfo;
2476 size_t msgsize;
2477 int ret;
2479 vmbus_connection.ignore_any_offer_msg = false;
2482 * We only use the 'vmbus_proto_version', which was in use before
2483 * hibernation, to re-negotiate with the host.
2485 if (!vmbus_proto_version) {
2486 pr_err("Invalid proto version = 0x%x\n", vmbus_proto_version);
2487 return -EINVAL;
2490 msgsize = sizeof(*msginfo) +
2491 sizeof(struct vmbus_channel_initiate_contact);
2493 msginfo = kzalloc(msgsize, GFP_KERNEL);
2495 if (msginfo == NULL)
2496 return -ENOMEM;
2498 ret = vmbus_negotiate_version(msginfo, vmbus_proto_version);
2500 kfree(msginfo);
2502 if (ret != 0)
2503 return ret;
2505 WARN_ON(atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) == 0);
2507 vmbus_request_offers();
2509 if (wait_for_completion_timeout(
2510 &vmbus_connection.ready_for_resume_event, 10 * HZ) == 0)
2511 pr_err("Some vmbus device is missing after suspending?\n");
2513 /* Reset the event for the next suspend. */
2514 reinit_completion(&vmbus_connection.ready_for_suspend_event);
2516 return 0;
2518 #else
2519 #define vmbus_bus_suspend NULL
2520 #define vmbus_bus_resume NULL
2521 #endif /* CONFIG_PM_SLEEP */
2523 static const __maybe_unused struct of_device_id vmbus_of_match[] = {
2525 .compatible = "microsoft,vmbus",
2528 /* sentinel */
2531 MODULE_DEVICE_TABLE(of, vmbus_of_match);
2533 static const __maybe_unused struct acpi_device_id vmbus_acpi_device_ids[] = {
2534 {"VMBUS", 0},
2535 {"VMBus", 0},
2536 {"", 0},
2538 MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
2541 * Note: we must use the "no_irq" ops, otherwise hibernation can not work with
2542 * PCI device assignment, because "pci_dev_pm_ops" uses the "noirq" ops: in
2543 * the resume path, the pci "noirq" restore op runs before "non-noirq" op (see
2544 * resume_target_kernel() -> dpm_resume_start(), and hibernation_restore() ->
2545 * dpm_resume_end()). This means vmbus_bus_resume() and the pci-hyperv's
2546 * resume callback must also run via the "noirq" ops.
2548 * Set suspend_noirq/resume_noirq to NULL for Suspend-to-Idle: see the comment
2549 * earlier in this file before vmbus_pm.
2552 static const struct dev_pm_ops vmbus_bus_pm = {
2553 .suspend_noirq = NULL,
2554 .resume_noirq = NULL,
2555 .freeze_noirq = vmbus_bus_suspend,
2556 .thaw_noirq = vmbus_bus_resume,
2557 .poweroff_noirq = vmbus_bus_suspend,
2558 .restore_noirq = vmbus_bus_resume
2561 static struct platform_driver vmbus_platform_driver = {
2562 .probe = vmbus_platform_driver_probe,
2563 .remove = vmbus_platform_driver_remove,
2564 .driver = {
2565 .name = "vmbus",
2566 .acpi_match_table = ACPI_PTR(vmbus_acpi_device_ids),
2567 .of_match_table = of_match_ptr(vmbus_of_match),
2568 .pm = &vmbus_bus_pm,
2569 .probe_type = PROBE_FORCE_SYNCHRONOUS,
2573 static void hv_kexec_handler(void)
2575 hv_stimer_global_cleanup();
2576 vmbus_initiate_unload(false);
2577 /* Make sure conn_state is set as hv_synic_cleanup checks for it */
2578 mb();
2579 cpuhp_remove_state(hyperv_cpuhp_online);
2582 static void hv_crash_handler(struct pt_regs *regs)
2584 int cpu;
2586 vmbus_initiate_unload(true);
2588 * In crash handler we can't schedule synic cleanup for all CPUs,
2589 * doing the cleanup for current CPU only. This should be sufficient
2590 * for kdump.
2592 cpu = smp_processor_id();
2593 hv_stimer_cleanup(cpu);
2594 hv_synic_disable_regs(cpu);
2597 static int hv_synic_suspend(void)
2600 * When we reach here, all the non-boot CPUs have been offlined.
2601 * If we're in a legacy configuration where stimer Direct Mode is
2602 * not enabled, the stimers on the non-boot CPUs have been unbound
2603 * in hv_synic_cleanup() -> hv_stimer_legacy_cleanup() ->
2604 * hv_stimer_cleanup() -> clockevents_unbind_device().
2606 * hv_synic_suspend() only runs on CPU0 with interrupts disabled.
2607 * Here we do not call hv_stimer_legacy_cleanup() on CPU0 because:
2608 * 1) it's unnecessary as interrupts remain disabled between
2609 * syscore_suspend() and syscore_resume(): see create_image() and
2610 * resume_target_kernel()
2611 * 2) the stimer on CPU0 is automatically disabled later by
2612 * syscore_suspend() -> timekeeping_suspend() -> tick_suspend() -> ...
2613 * -> clockevents_shutdown() -> ... -> hv_ce_shutdown()
2614 * 3) a warning would be triggered if we call
2615 * clockevents_unbind_device(), which may sleep, in an
2616 * interrupts-disabled context.
2619 hv_synic_disable_regs(0);
2621 return 0;
2624 static void hv_synic_resume(void)
2626 hv_synic_enable_regs(0);
2629 * Note: we don't need to call hv_stimer_init(0), because the timer
2630 * on CPU0 is not unbound in hv_synic_suspend(), and the timer is
2631 * automatically re-enabled in timekeeping_resume().
2635 /* The callbacks run only on CPU0, with irqs_disabled. */
2636 static struct syscore_ops hv_synic_syscore_ops = {
2637 .suspend = hv_synic_suspend,
2638 .resume = hv_synic_resume,
2641 static int __init hv_acpi_init(void)
2643 int ret;
2645 if (!hv_is_hyperv_initialized())
2646 return -ENODEV;
2648 if (hv_root_partition && !hv_nested)
2649 return 0;
2652 * Get ACPI resources first.
2654 ret = platform_driver_register(&vmbus_platform_driver);
2655 if (ret)
2656 return ret;
2658 if (!hv_dev) {
2659 ret = -ENODEV;
2660 goto cleanup;
2664 * If we're on an architecture with a hardcoded hypervisor
2665 * vector (i.e. x86/x64), override the VMbus interrupt found
2666 * in the ACPI tables. Ensure vmbus_irq is not set since the
2667 * normal Linux IRQ mechanism is not used in this case.
2669 #ifdef HYPERVISOR_CALLBACK_VECTOR
2670 vmbus_interrupt = HYPERVISOR_CALLBACK_VECTOR;
2671 vmbus_irq = -1;
2672 #endif
2674 hv_debug_init();
2676 ret = vmbus_bus_init();
2677 if (ret)
2678 goto cleanup;
2680 hv_setup_kexec_handler(hv_kexec_handler);
2681 hv_setup_crash_handler(hv_crash_handler);
2683 register_syscore_ops(&hv_synic_syscore_ops);
2685 return 0;
2687 cleanup:
2688 platform_driver_unregister(&vmbus_platform_driver);
2689 hv_dev = NULL;
2690 return ret;
2693 static void __exit vmbus_exit(void)
2695 int cpu;
2697 unregister_syscore_ops(&hv_synic_syscore_ops);
2699 hv_remove_kexec_handler();
2700 hv_remove_crash_handler();
2701 vmbus_connection.conn_state = DISCONNECTED;
2702 hv_stimer_global_cleanup();
2703 vmbus_disconnect();
2704 if (vmbus_irq == -1) {
2705 hv_remove_vmbus_handler();
2706 } else {
2707 free_percpu_irq(vmbus_irq, vmbus_evt);
2708 free_percpu(vmbus_evt);
2710 for_each_online_cpu(cpu) {
2711 struct hv_per_cpu_context *hv_cpu
2712 = per_cpu_ptr(hv_context.cpu_context, cpu);
2714 tasklet_kill(&hv_cpu->msg_dpc);
2716 hv_debug_rm_all_dir();
2718 vmbus_free_channels();
2719 kfree(vmbus_connection.channels);
2722 * The vmbus panic notifier is always registered, hence we should
2723 * also unconditionally unregister it here as well.
2725 atomic_notifier_chain_unregister(&panic_notifier_list,
2726 &hyperv_panic_vmbus_unload_block);
2728 bus_unregister(&hv_bus);
2730 cpuhp_remove_state(hyperv_cpuhp_online);
2731 hv_synic_free();
2732 platform_driver_unregister(&vmbus_platform_driver);
2736 MODULE_LICENSE("GPL");
2737 MODULE_DESCRIPTION("Microsoft Hyper-V VMBus Driver");
2739 subsys_initcall(hv_acpi_init);
2740 module_exit(vmbus_exit);