blob: 93cc9266e8cb8caa38075e044849413116ed9b71 [file] [log] [blame]
Kenji Kaneshigec9ffa5a2008-12-17 12:07:38 +09001/*
2 * ACPI related functions for PCI Express Hot Plug driver.
3 *
4 * Copyright (C) 2008 Kenji Kaneshige
5 * Copyright (C) 2008 Fujitsu Limited.
6 *
7 * All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
17 * NON INFRINGEMENT. See the GNU General Public License for more
18 * details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 */
25
26#include <linux/acpi.h>
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +090027#include <linux/pci.h>
28#include <linux/pci_hotplug.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Paul Gortmakereefa9cf2011-05-27 09:42:30 -040030#include <linux/module.h>
Kenji Kaneshigec9ffa5a2008-12-17 12:07:38 +090031#include "pciehp.h"
32
33#define PCIEHP_DETECT_PCIE (0)
34#define PCIEHP_DETECT_ACPI (1)
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +090035#define PCIEHP_DETECT_AUTO (2)
36#define PCIEHP_DETECT_DEFAULT PCIEHP_DETECT_AUTO
Kenji Kaneshigec9ffa5a2008-12-17 12:07:38 +090037
Kenji Kaneshige8720d272009-09-15 17:24:46 +090038struct dummy_slot {
39 u32 number;
40 struct list_head list;
41};
42
Kenji Kaneshigec9ffa5a2008-12-17 12:07:38 +090043static int slot_detection_mode;
44static char *pciehp_detect_mode;
45module_param(pciehp_detect_mode, charp, 0444);
46MODULE_PARM_DESC(pciehp_detect_mode,
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +090047 "Slot detection mode: pcie, acpi, auto\n"
48 " pcie - Use PCIe based slot detection\n"
49 " acpi - Use ACPI for slot detection\n"
50 " auto(default) - Auto select mode. Use acpi option if duplicate\n"
51 " slot ids are found. Otherwise, use pcie option\n");
Kenji Kaneshigec9ffa5a2008-12-17 12:07:38 +090052
Kenji Kaneshigec9ffa5a2008-12-17 12:07:38 +090053int pciehp_acpi_slot_detection_check(struct pci_dev *dev)
54{
55 if (slot_detection_mode != PCIEHP_DETECT_ACPI)
56 return 0;
Rafael J. Wysocki3a83f992013-11-14 23:17:21 +010057 if (acpi_pci_detect_ejectable(ACPI_HANDLE(&dev->dev)))
Kenji Kaneshigec9ffa5a2008-12-17 12:07:38 +090058 return 0;
59 return -ENODEV;
60}
61
62static int __init parse_detect_mode(void)
63{
64 if (!pciehp_detect_mode)
65 return PCIEHP_DETECT_DEFAULT;
66 if (!strcmp(pciehp_detect_mode, "pcie"))
67 return PCIEHP_DETECT_PCIE;
68 if (!strcmp(pciehp_detect_mode, "acpi"))
69 return PCIEHP_DETECT_ACPI;
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +090070 if (!strcmp(pciehp_detect_mode, "auto"))
71 return PCIEHP_DETECT_AUTO;
Kenji Kaneshigec9ffa5a2008-12-17 12:07:38 +090072 warn("bad specifier '%s' for pciehp_detect_mode. Use default\n",
73 pciehp_detect_mode);
74 return PCIEHP_DETECT_DEFAULT;
75}
76
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +090077static int __initdata dup_slot_id;
78static int __initdata acpi_slot_detected;
79static struct list_head __initdata dummy_slots = LIST_HEAD_INIT(dummy_slots);
80
Bjorn Helgaasf7625982013-11-14 11:28:18 -070081/* Dummy driver for duplicate name detection */
Rafael J. Wysocki0516c8b2009-01-13 14:44:19 +010082static int __init dummy_probe(struct pcie_device *dev)
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +090083{
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +090084 u32 slot_cap;
Alex Chiang7f538662009-09-10 12:34:09 -060085 acpi_handle handle;
Kenji Kaneshige8720d272009-09-15 17:24:46 +090086 struct dummy_slot *slot, *tmp;
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +090087 struct pci_dev *pdev = dev->port;
Rafael J. Wysocki28eb5f22010-08-21 22:02:38 +020088
Jiang Liu537a77e2012-07-24 17:20:09 +080089 pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &slot_cap);
Julia Lawall0b3e7382009-02-08 22:45:24 +010090 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
91 if (!slot)
92 return -ENOMEM;
Bjorn Helgaas545d7b72013-04-09 16:46:12 -060093 slot->number = (slot_cap & PCI_EXP_SLTCAP_PSN) >> 19;
Kenji Kaneshige8720d272009-09-15 17:24:46 +090094 list_for_each_entry(tmp, &dummy_slots, list) {
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +090095 if (tmp->number == slot->number)
96 dup_slot_id++;
97 }
Kenji Kaneshige8720d272009-09-15 17:24:46 +090098 list_add_tail(&slot->list, &dummy_slots);
Rafael J. Wysocki3a83f992013-11-14 23:17:21 +010099 handle = ACPI_HANDLE(&pdev->dev);
Alex Chiang7f538662009-09-10 12:34:09 -0600100 if (!acpi_slot_detected && acpi_pci_detect_ejectable(handle))
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +0900101 acpi_slot_detected = 1;
102 return -ENODEV; /* dummy driver always returns error */
103}
104
105static struct pcie_port_service_driver __initdata dummy_driver = {
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400106 .name = "pciehp_dummy",
Rafael J. Wysocki22106362009-01-13 14:46:46 +0100107 .port_type = PCIE_ANY_PORT,
108 .service = PCIE_PORT_SERVICE_HP,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400109 .probe = dummy_probe,
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +0900110};
111
112static int __init select_detection_mode(void)
113{
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900114 struct dummy_slot *slot, *tmp;
Bjorn Helgaas9cad7f52014-02-11 15:26:29 -0700115
Rafael J. Wysocki994e0b22010-12-18 13:29:45 +0100116 if (pcie_port_service_register(&dummy_driver))
117 return PCIEHP_DETECT_ACPI;
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +0900118 pcie_port_service_unregister(&dummy_driver);
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900119 list_for_each_entry_safe(slot, tmp, &dummy_slots, list) {
120 list_del(&slot->list);
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +0900121 kfree(slot);
122 }
123 if (acpi_slot_detected && dup_slot_id)
124 return PCIEHP_DETECT_ACPI;
125 return PCIEHP_DETECT_PCIE;
126}
127
Kenji Kaneshigec9ffa5a2008-12-17 12:07:38 +0900128void __init pciehp_acpi_slot_detection_init(void)
129{
130 slot_detection_mode = parse_detect_mode();
Kenji Kaneshigee046cbd2008-12-17 12:08:15 +0900131 if (slot_detection_mode != PCIEHP_DETECT_AUTO)
132 goto out;
133 slot_detection_mode = select_detection_mode();
134out:
135 if (slot_detection_mode == PCIEHP_DETECT_ACPI)
136 info("Using ACPI for slot detection.\n");
Kenji Kaneshigec9ffa5a2008-12-17 12:07:38 +0900137}