acpi_pcihp.c 5.6 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 13
 *
 */

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

23 24
#define MY_NAME	"acpi_pcihp"

B
Bogicevic Sasa 已提交
25 26 27 28
#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)
29

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

33
static bool debug_acpi;
34

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

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

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

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

63 64 65 66 67 68 69
/**
 * acpi_get_hp_hw_control_from_firmware
 * @dev: the pci_dev of the bridge that has a hotplug controller
 * @flags: requested control bits for _OSC
 *
 * Attempt to take hotplug control from firmware.
 */
70
int acpi_get_hp_hw_control_from_firmware(struct pci_dev *pdev, u32 flags)
71
{
72 73
	const struct pci_host_bridge *host;
	const struct acpi_pci_root *root;
74
	acpi_status status;
75
	acpi_handle chandle, handle;
76 77 78 79 80 81
	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.
82 83 84
	 * 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 已提交
85
	 * up to the host bridge under which this controller exists.
86
	 */
87 88 89 90 91 92 93 94
	host = pci_find_host_bridge(pdev->bus);
	if (host->native_shpc_hotplug)
		return 0;

	/* If _OSC exists, we should not evaluate OSHP */
	root = acpi_pci_find_root(ACPI_HANDLE(&host->dev));
	if (root->osc_support_set)
		goto no_control;
95

96
	handle = ACPI_HANDLE(&pdev->dev);
97
	if (!handle) {
98 99 100 101
		/*
		 * This hotplug controller was not listed in the ACPI name
		 * space at all. Try to get acpi handle of parent pci bus.
		 */
102 103 104 105 106 107
		struct pci_bus *pbus;
		for (pbus = pdev->bus; pbus; pbus = pbus->parent) {
			handle = acpi_pci_get_bridge_handle(pbus);
			if (handle)
				break;
		}
108 109 110 111
	}

	while (handle) {
		acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
B
Bogicevic Sasa 已提交
112
		dbg("Trying to get hotplug control for %s\n",
113
		    (char *)string.pointer);
114 115 116
		status = acpi_run_oshp(handle);
		if (ACPI_SUCCESS(status))
			goto got_one;
117
		if (acpi_is_root_bridge(handle))
118 119 120 121 122 123
			break;
		chandle = handle;
		status = acpi_get_parent(chandle, &handle);
		if (ACPI_FAILURE(status))
			break;
	}
K
Kenji Kaneshige 已提交
124
no_control:
125
	dbg("Cannot get control of hotplug hardware for pci %s\n",
126
	    pci_name(pdev));
127 128
	kfree(string.pointer);
	return -ENODEV;
129
got_one:
130 131
	dbg("Gained control for hotplug HW for pci %s (%s)\n",
	    pci_name(pdev), (char *)string.pointer);
132 133
	kfree(string.pointer);
	return 0;
134 135
}
EXPORT_SYMBOL(acpi_get_hp_hw_control_from_firmware);
L
Linus Torvalds 已提交
136

137
static int pcihp_is_ejectable(acpi_handle handle)
138 139 140
{
	acpi_status status;
	unsigned long long removable;
141
	if (!acpi_has_method(handle, "_ADR"))
142
		return 0;
143
	if (acpi_has_method(handle, "_EJ0"))
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
		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;

162 163
	bridge_handle = acpi_pci_get_bridge_handle(pbus);
	if (!bridge_handle)
164 165 166 167 168
		return 0;
	if ((ACPI_FAILURE(acpi_get_parent(handle, &parent_handle))))
		return 0;
	if (bridge_handle != parent_handle)
		return 0;
169
	return pcihp_is_ejectable(handle);
170 171 172 173 174 175 176
}
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;
177
	if (pcihp_is_ejectable(handle)) {
178 179 180 181 182 183 184 185
		*found = 1;
		return AE_CTRL_TERMINATE;
	}
	return AE_OK;
}

/**
 * acpi_pci_detect_ejectable - check if the PCI bus has ejectable slots
186
 * @handle - handle of the PCI bus to scan
187 188 189
 *
 * Returns 1 if the PCI bus has ACPI based ejectable slots, 0 otherwise.
 */
190
int acpi_pci_detect_ejectable(acpi_handle handle)
191 192 193
{
	int found = 0;

194 195 196 197
	if (!handle)
		return found;

	acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
198
			    check_hotplug, NULL, (void *)&found, NULL);
199 200 201 202
	return found;
}
EXPORT_SYMBOL_GPL(acpi_pci_detect_ejectable);

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