acpi_pcihp.c 5.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
L
Linus Torvalds 已提交
2
/*
3
 * Common ACPI functions for hot plug platforms
L
Linus Torvalds 已提交
4
 *
5
 * Copyright (C) 2006 Intel Corporation
L
Linus Torvalds 已提交
6 7 8
 *
 * All rights reserved.
 *
9
 * Send feedback to <kristen.c.accardi@intel.com>
L
Linus Torvalds 已提交
10 11 12
 */

#include <linux/module.h>
13
#include <linux/moduleparam.h>
L
Linus Torvalds 已提交
14 15 16
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/pci.h>
17
#include <linux/pci_hotplug.h>
18
#include <linux/acpi.h>
19
#include <linux/pci-acpi.h>
20
#include <linux/slab.h>
L
Linus Torvalds 已提交
21

22 23
#define MY_NAME	"acpi_pcihp"

B
Bogicevic Sasa 已提交
24 25 26 27
#define dbg(fmt, arg...) do { if (debug_acpi) printk(KERN_DEBUG "%s: %s: " fmt, MY_NAME, __func__, ## arg); } while (0)
#define err(format, arg...) printk(KERN_ERR "%s: " format, MY_NAME, ## arg)
#define info(format, arg...) printk(KERN_INFO "%s: " format, MY_NAME, ## arg)
#define warn(format, arg...) printk(KERN_WARNING "%s: " format, MY_NAME, ## arg)
28

L
Linus Torvalds 已提交
29 30 31
#define	METHOD_NAME__SUN	"_SUN"
#define	METHOD_NAME_OSHP	"OSHP"

32
static bool debug_acpi;
33

34 35 36 37
/* acpi_run_oshp - get control of hotplug from the firmware
 *
 * @handle - the handle of the hotplug controller.
 */
38
static acpi_status acpi_run_oshp(acpi_handle handle)
L
Linus Torvalds 已提交
39 40
{
	acpi_status		status;
41 42 43
	struct acpi_buffer	string = { ACPI_ALLOCATE_BUFFER, NULL };

	acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
L
Linus Torvalds 已提交
44 45

	/* run OSHP */
46
	status = acpi_evaluate_object(handle, METHOD_NAME_OSHP, NULL, NULL);
47
	if (ACPI_FAILURE(status))
48 49
		if (status != AE_NOT_FOUND)
			printk(KERN_ERR "%s:%s OSHP fails=0x%x\n",
50
			       __func__, (char *)string.pointer, status);
51 52
		else
			dbg("%s:%s OSHP not found\n",
53
			    __func__, (char *)string.pointer);
54
	else
55
		pr_debug("%s:%s OSHP passes\n", __func__,
56 57
			(char *)string.pointer);

58
	kfree(string.pointer);
59 60 61
	return status;
}

62 63 64 65 66 67
/**
 * acpi_get_hp_hw_control_from_firmware
 * @dev: the pci_dev of the bridge that has a hotplug controller
 *
 * Attempt to take hotplug control from firmware.
 */
68
int acpi_get_hp_hw_control_from_firmware(struct pci_dev *pdev)
69
{
70 71
	const struct pci_host_bridge *host;
	const struct acpi_pci_root *root;
72
	acpi_status status;
73
	acpi_handle chandle, handle;
74 75 76 77 78 79
	struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };

	/*
	 * Per PCI firmware specification, we should run the ACPI _OSC
	 * method to get control of hotplug hardware before using it. If
	 * an _OSC is missing, we look for an OSHP to do the same thing.
80 81 82
	 * To handle different BIOS behavior, we look for _OSC on a root
	 * bridge preferentially (according to PCI fw spec). Later for
	 * OSHP within the scope of the hotplug controller and its parents,
L
Lucas De Marchi 已提交
83
	 * up to the host bridge under which this controller exists.
84
	 */
85
	if (shpchp_is_native(pdev))
86 87 88
		return 0;

	/* If _OSC exists, we should not evaluate OSHP */
89 90 91 92 93 94

	/*
	 * If there's no ACPI host bridge (i.e., ACPI support is compiled
	 * into the kernel but the hardware platform doesn't support ACPI),
	 * there's nothing to do here.
	 */
95
	host = pci_find_host_bridge(pdev->bus);
96
	root = acpi_pci_find_root(ACPI_HANDLE(&host->dev));
97 98 99
	if (!root)
		return 0;

100 101
	if (root->osc_support_set)
		goto no_control;
102

103
	handle = ACPI_HANDLE(&pdev->dev);
104
	if (!handle) {
105 106
		/*
		 * This hotplug controller was not listed in the ACPI name
107
		 * space at all. Try to get ACPI handle of parent PCI bus.
108
		 */
109 110 111 112 113 114
		struct pci_bus *pbus;
		for (pbus = pdev->bus; pbus; pbus = pbus->parent) {
			handle = acpi_pci_get_bridge_handle(pbus);
			if (handle)
				break;
		}
115 116 117 118
	}

	while (handle) {
		acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
119 120
		pci_info(pdev, "Requesting control of SHPC hotplug via OSHP (%s)\n",
			 (char *)string.pointer);
121 122 123
		status = acpi_run_oshp(handle);
		if (ACPI_SUCCESS(status))
			goto got_one;
124
		if (acpi_is_root_bridge(handle))
125 126 127 128 129 130
			break;
		chandle = handle;
		status = acpi_get_parent(chandle, &handle);
		if (ACPI_FAILURE(status))
			break;
	}
K
Kenji Kaneshige 已提交
131
no_control:
132
	pci_info(pdev, "Cannot get control of SHPC hotplug\n");
133 134
	kfree(string.pointer);
	return -ENODEV;
135
got_one:
136 137
	pci_info(pdev, "Gained control of SHPC hotplug (%s)\n",
		 (char *)string.pointer);
138 139
	kfree(string.pointer);
	return 0;
140 141
}
EXPORT_SYMBOL(acpi_get_hp_hw_control_from_firmware);
L
Linus Torvalds 已提交
142

143
static int pcihp_is_ejectable(acpi_handle handle)
144 145 146
{
	acpi_status status;
	unsigned long long removable;
147
	if (!acpi_has_method(handle, "_ADR"))
148
		return 0;
149
	if (acpi_has_method(handle, "_EJ0"))
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
		return 1;
	status = acpi_evaluate_integer(handle, "_RMV", NULL, &removable);
	if (ACPI_SUCCESS(status) && removable)
		return 1;
	return 0;
}

/**
 * acpi_pcihp_check_ejectable - check if handle is ejectable ACPI PCI slot
 * @pbus: the PCI bus of the PCI slot corresponding to 'handle'
 * @handle: ACPI handle to check
 *
 * Return 1 if handle is ejectable PCI slot, 0 otherwise.
 */
int acpi_pci_check_ejectable(struct pci_bus *pbus, acpi_handle handle)
{
	acpi_handle bridge_handle, parent_handle;

168 169
	bridge_handle = acpi_pci_get_bridge_handle(pbus);
	if (!bridge_handle)
170 171 172 173 174
		return 0;
	if ((ACPI_FAILURE(acpi_get_parent(handle, &parent_handle))))
		return 0;
	if (bridge_handle != parent_handle)
		return 0;
175
	return pcihp_is_ejectable(handle);
176 177 178 179 180 181 182
}
EXPORT_SYMBOL_GPL(acpi_pci_check_ejectable);

static acpi_status
check_hotplug(acpi_handle handle, u32 lvl, void *context, void **rv)
{
	int *found = (int *)context;
183
	if (pcihp_is_ejectable(handle)) {
184 185 186 187 188 189 190 191
		*found = 1;
		return AE_CTRL_TERMINATE;
	}
	return AE_OK;
}

/**
 * acpi_pci_detect_ejectable - check if the PCI bus has ejectable slots
192
 * @handle - handle of the PCI bus to scan
193 194 195
 *
 * Returns 1 if the PCI bus has ACPI based ejectable slots, 0 otherwise.
 */
196
int acpi_pci_detect_ejectable(acpi_handle handle)
197 198 199
{
	int found = 0;

200 201 202 203
	if (!handle)
		return found;

	acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
204
			    check_hotplug, NULL, (void *)&found, NULL);
205 206 207 208
	return found;
}
EXPORT_SYMBOL_GPL(acpi_pci_detect_ejectable);

209 210
module_param(debug_acpi, bool, 0644);
MODULE_PARM_DESC(debug_acpi, "Debugging mode for ACPI enabled or not");