^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * xen-acpi-pad.c - Xen pad interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2012, Intel Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Liu, Jinsong <jinsong.liu@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <xen/xen.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <xen/interface/version.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <xen/xen-ops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <asm/xen/hypercall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define ACPI_PROCESSOR_AGGREGATOR_CLASS "acpi_pad"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define ACPI_PROCESSOR_AGGREGATOR_DEVICE_NAME "Processor Aggregator"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define ACPI_PROCESSOR_AGGREGATOR_NOTIFY 0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static DEFINE_MUTEX(xen_cpu_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static int xen_acpi_pad_idle_cpus(unsigned int idle_nums)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct xen_platform_op op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) op.cmd = XENPF_core_parking;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) op.u.core_parking.type = XEN_CORE_PARKING_SET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) op.u.core_parking.idle_nums = idle_nums;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return HYPERVISOR_platform_op(&op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static int xen_acpi_pad_idle_cpus_num(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct xen_platform_op op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) op.cmd = XENPF_core_parking;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) op.u.core_parking.type = XEN_CORE_PARKING_GET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return HYPERVISOR_platform_op(&op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) ?: op.u.core_parking.idle_nums;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * Query firmware how many CPUs should be idle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * return -1 on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static int acpi_pad_pur(acpi_handle handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) union acpi_object *package;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) int num = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (ACPI_FAILURE(acpi_evaluate_object(handle, "_PUR", NULL, &buffer)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (!buffer.length || !buffer.pointer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) package = buffer.pointer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (package->type == ACPI_TYPE_PACKAGE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) package->package.count == 2 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) package->package.elements[0].integer.value == 1) /* rev 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) num = package->package.elements[1].integer.value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) kfree(buffer.pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static void acpi_pad_handle_notify(acpi_handle handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) int idle_nums;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct acpi_buffer param = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) .length = 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) .pointer = (void *)&idle_nums,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) mutex_lock(&xen_cpu_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) idle_nums = acpi_pad_pur(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (idle_nums < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) mutex_unlock(&xen_cpu_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) idle_nums = xen_acpi_pad_idle_cpus(idle_nums)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) ?: xen_acpi_pad_idle_cpus_num();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (idle_nums >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) acpi_evaluate_ost(handle, ACPI_PROCESSOR_AGGREGATOR_NOTIFY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) 0, ¶m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) mutex_unlock(&xen_cpu_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static void acpi_pad_notify(acpi_handle handle, u32 event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) switch (event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) case ACPI_PROCESSOR_AGGREGATOR_NOTIFY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) acpi_pad_handle_notify(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) pr_warn("Unsupported event [0x%x]\n", event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static int acpi_pad_add(struct acpi_device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) strcpy(acpi_device_name(device), ACPI_PROCESSOR_AGGREGATOR_DEVICE_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) strcpy(acpi_device_class(device), ACPI_PROCESSOR_AGGREGATOR_CLASS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) status = acpi_install_notify_handler(device->handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) ACPI_DEVICE_NOTIFY, acpi_pad_notify, device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (ACPI_FAILURE(status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static int acpi_pad_remove(struct acpi_device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) mutex_lock(&xen_cpu_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) xen_acpi_pad_idle_cpus(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) mutex_unlock(&xen_cpu_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) acpi_remove_notify_handler(device->handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) ACPI_DEVICE_NOTIFY, acpi_pad_notify);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static const struct acpi_device_id pad_device_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {"ACPI000C", 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {"", 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static struct acpi_driver acpi_pad_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) .name = "processor_aggregator",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) .class = ACPI_PROCESSOR_AGGREGATOR_CLASS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) .ids = pad_device_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) .ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) .add = acpi_pad_add,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) .remove = acpi_pad_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static int __init xen_acpi_pad_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /* Only DOM0 is responsible for Xen acpi pad */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (!xen_initial_domain())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) /* Only Xen4.2 or later support Xen acpi pad */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (!xen_running_on_version_or_later(4, 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return acpi_bus_register_driver(&acpi_pad_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) subsys_initcall(xen_acpi_pad_init);