^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Common ACPI functions for hot plug platforms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2006 Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Send feedback to <kristen.c.accardi@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/pci_hotplug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/pci-acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define MY_NAME "acpi_pcihp"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define dbg(fmt, arg...) do { if (debug_acpi) printk(KERN_DEBUG "%s: %s: " fmt, MY_NAME, __func__, ## arg); } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define err(format, arg...) printk(KERN_ERR "%s: " format, MY_NAME, ## arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define info(format, arg...) printk(KERN_INFO "%s: " format, MY_NAME, ## arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define warn(format, arg...) printk(KERN_WARNING "%s: " format, MY_NAME, ## arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define METHOD_NAME__SUN "_SUN"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define METHOD_NAME_OSHP "OSHP"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static bool debug_acpi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /* acpi_run_oshp - get control of hotplug from the firmware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * @handle - the handle of the hotplug controller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static acpi_status acpi_run_oshp(acpi_handle handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /* run OSHP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) status = acpi_evaluate_object(handle, METHOD_NAME_OSHP, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (ACPI_FAILURE(status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (status != AE_NOT_FOUND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) printk(KERN_ERR "%s:%s OSHP fails=0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) __func__, (char *)string.pointer, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) dbg("%s:%s OSHP not found\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) __func__, (char *)string.pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) pr_debug("%s:%s OSHP passes\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) (char *)string.pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) kfree(string.pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * acpi_get_hp_hw_control_from_firmware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * @pdev: the pci_dev of the bridge that has a hotplug controller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * Attempt to take hotplug control from firmware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int acpi_get_hp_hw_control_from_firmware(struct pci_dev *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) const struct pci_host_bridge *host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) const struct acpi_pci_root *root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) acpi_handle chandle, handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * If there's no ACPI host bridge (i.e., ACPI support is compiled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * into the kernel but the hardware platform doesn't support ACPI),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * there's nothing to do here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) host = pci_find_host_bridge(pdev->bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) root = acpi_pci_find_root(ACPI_HANDLE(&host->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (!root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * If _OSC exists, it determines whether we're allowed to manage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * the SHPC. We executed it while enumerating the host bridge.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (root->osc_support_set) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (host->native_shpc_hotplug)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^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) * In the absence of _OSC, we're always allowed to manage the SHPC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * However, if an OSHP method is present, we must execute it so the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * firmware can transfer control to the OS, e.g., direct interrupts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * to the OS instead of to the firmware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * N.B. The PCI Firmware Spec (r3.2, sec 4.8) does not endorse
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * searching up the ACPI hierarchy, so the loops below are suspect.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) handle = ACPI_HANDLE(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (!handle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * This hotplug controller was not listed in the ACPI name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * space at all. Try to get ACPI handle of parent PCI bus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct pci_bus *pbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) for (pbus = pdev->bus; pbus; pbus = pbus->parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) handle = acpi_pci_get_bridge_handle(pbus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) while (handle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) pci_info(pdev, "Requesting control of SHPC hotplug via OSHP (%s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) (char *)string.pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) status = acpi_run_oshp(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (ACPI_SUCCESS(status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) goto got_one;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (acpi_is_root_bridge(handle))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) chandle = handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) status = acpi_get_parent(chandle, &handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (ACPI_FAILURE(status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) pci_info(pdev, "Cannot get control of SHPC hotplug\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) kfree(string.pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) got_one:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) pci_info(pdev, "Gained control of SHPC hotplug (%s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) (char *)string.pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) kfree(string.pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) EXPORT_SYMBOL(acpi_get_hp_hw_control_from_firmware);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static int pcihp_is_ejectable(acpi_handle handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) unsigned long long removable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (!acpi_has_method(handle, "_ADR"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (acpi_has_method(handle, "_EJ0"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) status = acpi_evaluate_integer(handle, "_RMV", NULL, &removable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (ACPI_SUCCESS(status) && removable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * acpi_pcihp_check_ejectable - check if handle is ejectable ACPI PCI slot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * @pbus: the PCI bus of the PCI slot corresponding to 'handle'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * @handle: ACPI handle to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * Return 1 if handle is ejectable PCI slot, 0 otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) int acpi_pci_check_ejectable(struct pci_bus *pbus, acpi_handle handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) acpi_handle bridge_handle, parent_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) bridge_handle = acpi_pci_get_bridge_handle(pbus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (!bridge_handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if ((ACPI_FAILURE(acpi_get_parent(handle, &parent_handle))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (bridge_handle != parent_handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return pcihp_is_ejectable(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) EXPORT_SYMBOL_GPL(acpi_pci_check_ejectable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static acpi_status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) check_hotplug(acpi_handle handle, u32 lvl, void *context, void **rv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) int *found = (int *)context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (pcihp_is_ejectable(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) *found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return AE_CTRL_TERMINATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return AE_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * acpi_pci_detect_ejectable - check if the PCI bus has ejectable slots
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * @handle: handle of the PCI bus to scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * Returns 1 if the PCI bus has ACPI based ejectable slots, 0 otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) int acpi_pci_detect_ejectable(acpi_handle handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) int found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (!handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) return found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) check_hotplug, NULL, (void *)&found, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) EXPORT_SYMBOL_GPL(acpi_pci_detect_ejectable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) module_param(debug_acpi, bool, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) MODULE_PARM_DESC(debug_acpi, "Debugging mode for ACPI enabled or not");