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