1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2005 David Shaohua Li <shaohua.li@intel.com>
6 * Copyright (C) 2004 Tom Long Nguyen <tom.l.nguyen@intel.com>
7 * Copyright (C) 2004 Intel Corp.
10 #include <linux/delay.h>
11 #include <linux/init.h>
12 #include <linux/irqdomain.h>
13 #include <linux/pci.h>
14 #include <linux/msi.h>
15 #include <linux/pci_hotplug.h>
16 #include <linux/module.h>
17 #include <linux/pci-acpi.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/pm_qos.h>
20 #include <linux/rwsem.h>
24 * The GUID is defined in the PCI Firmware Specification available here:
25 * https://www.pcisig.com/members/downloads/pcifw_r3_1_13Dec10.pdf
27 const guid_t pci_acpi_dsm_guid =
28 GUID_INIT(0xe5c937d0, 0x3553, 0x4d7a,
29 0x91, 0x17, 0xea, 0x4d, 0x19, 0xc3, 0x43, 0x4d);
31 #if defined(CONFIG_PCI_QUIRKS) && defined(CONFIG_ARM64)
32 static int acpi_get_rc_addr(struct acpi_device *adev, struct resource *res)
34 struct device *dev = &adev->dev;
35 struct resource_entry *entry;
36 struct list_head list;
40 INIT_LIST_HEAD(&list);
41 flags = IORESOURCE_MEM;
42 ret = acpi_dev_get_resources(adev, &list,
43 acpi_dev_filter_resource_type_cb,
46 dev_err(dev, "failed to parse _CRS method, error code %d\n",
52 dev_err(dev, "no IO and memory resources present in _CRS\n");
56 entry = list_first_entry(&list, struct resource_entry, node);
58 acpi_dev_free_resource_list(&list);
62 static acpi_status acpi_match_rc(acpi_handle handle, u32 lvl, void *context,
65 u16 *segment = context;
66 unsigned long long uid;
69 status = acpi_evaluate_integer(handle, "_UID", NULL, &uid);
70 if (ACPI_FAILURE(status) || uid != *segment)
73 *(acpi_handle *)retval = handle;
74 return AE_CTRL_TERMINATE;
77 int acpi_get_rc_resources(struct device *dev, const char *hid, u16 segment,
80 struct acpi_device *adev;
85 status = acpi_get_devices(hid, acpi_match_rc, &segment, &handle);
86 if (ACPI_FAILURE(status)) {
87 dev_err(dev, "can't find _HID %s device to locate resources\n",
92 ret = acpi_bus_get_device(handle, &adev);
96 ret = acpi_get_rc_addr(adev, res);
98 dev_err(dev, "can't get resource from %s\n",
99 dev_name(&adev->dev));
107 phys_addr_t acpi_pci_root_get_mcfg_addr(acpi_handle handle)
109 acpi_status status = AE_NOT_EXIST;
110 unsigned long long mcfg_addr;
113 status = acpi_evaluate_integer(handle, METHOD_NAME__CBA,
115 if (ACPI_FAILURE(status))
118 return (phys_addr_t)mcfg_addr;
121 /* _HPX PCI Setting Record (Type 0); same as _HPP */
123 u32 revision; /* Not present in _HPP */
124 u8 cache_line_size; /* Not applicable to PCIe */
125 u8 latency_timer; /* Not applicable to PCIe */
130 static struct hpx_type0 pci_default_type0 = {
132 .cache_line_size = 8,
133 .latency_timer = 0x40,
138 static void program_hpx_type0(struct pci_dev *dev, struct hpx_type0 *hpx)
140 u16 pci_cmd, pci_bctl;
143 hpx = &pci_default_type0;
145 if (hpx->revision > 1) {
146 pci_warn(dev, "PCI settings rev %d not supported; using defaults\n",
148 hpx = &pci_default_type0;
151 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, hpx->cache_line_size);
152 pci_write_config_byte(dev, PCI_LATENCY_TIMER, hpx->latency_timer);
153 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
154 if (hpx->enable_serr)
155 pci_cmd |= PCI_COMMAND_SERR;
156 if (hpx->enable_perr)
157 pci_cmd |= PCI_COMMAND_PARITY;
158 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
160 /* Program bridge control value */
161 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
162 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
164 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
165 if (hpx->enable_perr)
166 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
167 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
171 static acpi_status decode_type0_hpx_record(union acpi_object *record,
172 struct hpx_type0 *hpx0)
175 union acpi_object *fields = record->package.elements;
176 u32 revision = fields[1].integer.value;
180 if (record->package.count != 6)
182 for (i = 2; i < 6; i++)
183 if (fields[i].type != ACPI_TYPE_INTEGER)
185 hpx0->revision = revision;
186 hpx0->cache_line_size = fields[2].integer.value;
187 hpx0->latency_timer = fields[3].integer.value;
188 hpx0->enable_serr = fields[4].integer.value;
189 hpx0->enable_perr = fields[5].integer.value;
192 pr_warn("%s: Type 0 Revision %d record not supported\n",
199 /* _HPX PCI-X Setting Record (Type 1) */
207 static void program_hpx_type1(struct pci_dev *dev, struct hpx_type1 *hpx)
214 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
218 pci_warn(dev, "PCI-X settings not supported\n");
221 static acpi_status decode_type1_hpx_record(union acpi_object *record,
222 struct hpx_type1 *hpx1)
225 union acpi_object *fields = record->package.elements;
226 u32 revision = fields[1].integer.value;
230 if (record->package.count != 5)
232 for (i = 2; i < 5; i++)
233 if (fields[i].type != ACPI_TYPE_INTEGER)
235 hpx1->revision = revision;
236 hpx1->max_mem_read = fields[2].integer.value;
237 hpx1->avg_max_split = fields[3].integer.value;
238 hpx1->tot_max_split = fields[4].integer.value;
241 pr_warn("%s: Type 1 Revision %d record not supported\n",
248 static bool pcie_root_rcb_set(struct pci_dev *dev)
250 struct pci_dev *rp = pcie_find_root_port(dev);
256 pcie_capability_read_word(rp, PCI_EXP_LNKCTL, &lnkctl);
257 if (lnkctl & PCI_EXP_LNKCTL_RCB)
263 /* _HPX PCI Express Setting Record (Type 2) */
266 u32 unc_err_mask_and;
268 u32 unc_err_sever_and;
269 u32 unc_err_sever_or;
270 u32 cor_err_mask_and;
274 u16 pci_exp_devctl_and;
275 u16 pci_exp_devctl_or;
276 u16 pci_exp_lnkctl_and;
277 u16 pci_exp_lnkctl_or;
278 u32 sec_unc_err_sever_and;
279 u32 sec_unc_err_sever_or;
280 u32 sec_unc_err_mask_and;
281 u32 sec_unc_err_mask_or;
284 static void program_hpx_type2(struct pci_dev *dev, struct hpx_type2 *hpx)
292 if (!pci_is_pcie(dev))
295 if (hpx->revision > 1) {
296 pci_warn(dev, "PCIe settings rev %d not supported\n",
302 * Don't allow _HPX to change MPS or MRRS settings. We manage
303 * those to make sure they're consistent with the rest of the
306 hpx->pci_exp_devctl_and |= PCI_EXP_DEVCTL_PAYLOAD |
307 PCI_EXP_DEVCTL_READRQ;
308 hpx->pci_exp_devctl_or &= ~(PCI_EXP_DEVCTL_PAYLOAD |
309 PCI_EXP_DEVCTL_READRQ);
311 /* Initialize Device Control Register */
312 pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
313 ~hpx->pci_exp_devctl_and, hpx->pci_exp_devctl_or);
315 /* Initialize Link Control Register */
316 if (pcie_cap_has_lnkctl(dev)) {
319 * If the Root Port supports Read Completion Boundary of
320 * 128, set RCB to 128. Otherwise, clear it.
322 hpx->pci_exp_lnkctl_and |= PCI_EXP_LNKCTL_RCB;
323 hpx->pci_exp_lnkctl_or &= ~PCI_EXP_LNKCTL_RCB;
324 if (pcie_root_rcb_set(dev))
325 hpx->pci_exp_lnkctl_or |= PCI_EXP_LNKCTL_RCB;
327 pcie_capability_clear_and_set_word(dev, PCI_EXP_LNKCTL,
328 ~hpx->pci_exp_lnkctl_and, hpx->pci_exp_lnkctl_or);
331 /* Find Advanced Error Reporting Enhanced Capability */
332 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
336 /* Initialize Uncorrectable Error Mask Register */
337 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, ®32);
338 reg32 = (reg32 & hpx->unc_err_mask_and) | hpx->unc_err_mask_or;
339 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, reg32);
341 /* Initialize Uncorrectable Error Severity Register */
342 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, ®32);
343 reg32 = (reg32 & hpx->unc_err_sever_and) | hpx->unc_err_sever_or;
344 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, reg32);
346 /* Initialize Correctable Error Mask Register */
347 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, ®32);
348 reg32 = (reg32 & hpx->cor_err_mask_and) | hpx->cor_err_mask_or;
349 pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg32);
351 /* Initialize Advanced Error Capabilities and Control Register */
352 pci_read_config_dword(dev, pos + PCI_ERR_CAP, ®32);
353 reg32 = (reg32 & hpx->adv_err_cap_and) | hpx->adv_err_cap_or;
355 /* Don't enable ECRC generation or checking if unsupported */
356 if (!(reg32 & PCI_ERR_CAP_ECRC_GENC))
357 reg32 &= ~PCI_ERR_CAP_ECRC_GENE;
358 if (!(reg32 & PCI_ERR_CAP_ECRC_CHKC))
359 reg32 &= ~PCI_ERR_CAP_ECRC_CHKE;
360 pci_write_config_dword(dev, pos + PCI_ERR_CAP, reg32);
363 * FIXME: The following two registers are not supported yet.
365 * o Secondary Uncorrectable Error Severity Register
366 * o Secondary Uncorrectable Error Mask Register
370 static acpi_status decode_type2_hpx_record(union acpi_object *record,
371 struct hpx_type2 *hpx2)
374 union acpi_object *fields = record->package.elements;
375 u32 revision = fields[1].integer.value;
379 if (record->package.count != 18)
381 for (i = 2; i < 18; i++)
382 if (fields[i].type != ACPI_TYPE_INTEGER)
384 hpx2->revision = revision;
385 hpx2->unc_err_mask_and = fields[2].integer.value;
386 hpx2->unc_err_mask_or = fields[3].integer.value;
387 hpx2->unc_err_sever_and = fields[4].integer.value;
388 hpx2->unc_err_sever_or = fields[5].integer.value;
389 hpx2->cor_err_mask_and = fields[6].integer.value;
390 hpx2->cor_err_mask_or = fields[7].integer.value;
391 hpx2->adv_err_cap_and = fields[8].integer.value;
392 hpx2->adv_err_cap_or = fields[9].integer.value;
393 hpx2->pci_exp_devctl_and = fields[10].integer.value;
394 hpx2->pci_exp_devctl_or = fields[11].integer.value;
395 hpx2->pci_exp_lnkctl_and = fields[12].integer.value;
396 hpx2->pci_exp_lnkctl_or = fields[13].integer.value;
397 hpx2->sec_unc_err_sever_and = fields[14].integer.value;
398 hpx2->sec_unc_err_sever_or = fields[15].integer.value;
399 hpx2->sec_unc_err_mask_and = fields[16].integer.value;
400 hpx2->sec_unc_err_mask_or = fields[17].integer.value;
403 pr_warn("%s: Type 2 Revision %d record not supported\n",
410 /* _HPX PCI Express Setting Record (Type 3) */
414 u16 config_space_location;
417 u16 pci_exp_vendor_id;
428 enum hpx_type3_dev_type {
429 HPX_TYPE_ENDPOINT = BIT(0),
430 HPX_TYPE_LEG_END = BIT(1),
431 HPX_TYPE_RC_END = BIT(2),
432 HPX_TYPE_RC_EC = BIT(3),
433 HPX_TYPE_ROOT_PORT = BIT(4),
434 HPX_TYPE_UPSTREAM = BIT(5),
435 HPX_TYPE_DOWNSTREAM = BIT(6),
436 HPX_TYPE_PCI_BRIDGE = BIT(7),
437 HPX_TYPE_PCIE_BRIDGE = BIT(8),
440 static u16 hpx3_device_type(struct pci_dev *dev)
442 u16 pcie_type = pci_pcie_type(dev);
443 static const int pcie_to_hpx3_type[] = {
444 [PCI_EXP_TYPE_ENDPOINT] = HPX_TYPE_ENDPOINT,
445 [PCI_EXP_TYPE_LEG_END] = HPX_TYPE_LEG_END,
446 [PCI_EXP_TYPE_RC_END] = HPX_TYPE_RC_END,
447 [PCI_EXP_TYPE_RC_EC] = HPX_TYPE_RC_EC,
448 [PCI_EXP_TYPE_ROOT_PORT] = HPX_TYPE_ROOT_PORT,
449 [PCI_EXP_TYPE_UPSTREAM] = HPX_TYPE_UPSTREAM,
450 [PCI_EXP_TYPE_DOWNSTREAM] = HPX_TYPE_DOWNSTREAM,
451 [PCI_EXP_TYPE_PCI_BRIDGE] = HPX_TYPE_PCI_BRIDGE,
452 [PCI_EXP_TYPE_PCIE_BRIDGE] = HPX_TYPE_PCIE_BRIDGE,
455 if (pcie_type >= ARRAY_SIZE(pcie_to_hpx3_type))
458 return pcie_to_hpx3_type[pcie_type];
461 enum hpx_type3_fn_type {
462 HPX_FN_NORMAL = BIT(0),
463 HPX_FN_SRIOV_PHYS = BIT(1),
464 HPX_FN_SRIOV_VIRT = BIT(2),
467 static u8 hpx3_function_type(struct pci_dev *dev)
470 return HPX_FN_SRIOV_VIRT;
471 else if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV) > 0)
472 return HPX_FN_SRIOV_PHYS;
474 return HPX_FN_NORMAL;
477 static bool hpx3_cap_ver_matches(u8 pcie_cap_id, u8 hpx3_cap_id)
479 u8 cap_ver = hpx3_cap_id & 0xf;
481 if ((hpx3_cap_id & BIT(4)) && cap_ver >= pcie_cap_id)
483 else if (cap_ver == pcie_cap_id)
489 enum hpx_type3_cfg_loc {
491 HPX_CFG_PCIE_CAP = 1,
492 HPX_CFG_PCIE_CAP_EXT = 2,
493 HPX_CFG_VEND_CAP = 3,
498 static void program_hpx_type3_register(struct pci_dev *dev,
499 const struct hpx_type3 *reg)
501 u32 match_reg, write_reg, header, orig_value;
504 if (!(hpx3_device_type(dev) & reg->device_type))
507 if (!(hpx3_function_type(dev) & reg->function_type))
510 switch (reg->config_space_location) {
514 case HPX_CFG_PCIE_CAP:
515 pos = pci_find_capability(dev, reg->pci_exp_cap_id);
520 case HPX_CFG_PCIE_CAP_EXT:
521 pos = pci_find_ext_capability(dev, reg->pci_exp_cap_id);
525 pci_read_config_dword(dev, pos, &header);
526 if (!hpx3_cap_ver_matches(PCI_EXT_CAP_VER(header),
527 reg->pci_exp_cap_ver))
531 case HPX_CFG_VEND_CAP:
534 pci_warn(dev, "Encountered _HPX type 3 with unsupported config space location");
538 pci_read_config_dword(dev, pos + reg->match_offset, &match_reg);
540 if ((match_reg & reg->match_mask_and) != reg->match_value)
543 pci_read_config_dword(dev, pos + reg->reg_offset, &write_reg);
544 orig_value = write_reg;
545 write_reg &= reg->reg_mask_and;
546 write_reg |= reg->reg_mask_or;
548 if (orig_value == write_reg)
551 pci_write_config_dword(dev, pos + reg->reg_offset, write_reg);
553 pci_dbg(dev, "Applied _HPX3 at [0x%x]: 0x%08x -> 0x%08x",
554 pos, orig_value, write_reg);
557 static void program_hpx_type3(struct pci_dev *dev, struct hpx_type3 *hpx)
562 if (!pci_is_pcie(dev))
565 program_hpx_type3_register(dev, hpx);
568 static void parse_hpx3_register(struct hpx_type3 *hpx3_reg,
569 union acpi_object *reg_fields)
571 hpx3_reg->device_type = reg_fields[0].integer.value;
572 hpx3_reg->function_type = reg_fields[1].integer.value;
573 hpx3_reg->config_space_location = reg_fields[2].integer.value;
574 hpx3_reg->pci_exp_cap_id = reg_fields[3].integer.value;
575 hpx3_reg->pci_exp_cap_ver = reg_fields[4].integer.value;
576 hpx3_reg->pci_exp_vendor_id = reg_fields[5].integer.value;
577 hpx3_reg->dvsec_id = reg_fields[6].integer.value;
578 hpx3_reg->dvsec_rev = reg_fields[7].integer.value;
579 hpx3_reg->match_offset = reg_fields[8].integer.value;
580 hpx3_reg->match_mask_and = reg_fields[9].integer.value;
581 hpx3_reg->match_value = reg_fields[10].integer.value;
582 hpx3_reg->reg_offset = reg_fields[11].integer.value;
583 hpx3_reg->reg_mask_and = reg_fields[12].integer.value;
584 hpx3_reg->reg_mask_or = reg_fields[13].integer.value;
587 static acpi_status program_type3_hpx_record(struct pci_dev *dev,
588 union acpi_object *record)
590 union acpi_object *fields = record->package.elements;
591 u32 desc_count, expected_length, revision;
592 union acpi_object *reg_fields;
593 struct hpx_type3 hpx3;
596 revision = fields[1].integer.value;
599 desc_count = fields[2].integer.value;
600 expected_length = 3 + desc_count * 14;
602 if (record->package.count != expected_length)
605 for (i = 2; i < expected_length; i++)
606 if (fields[i].type != ACPI_TYPE_INTEGER)
609 for (i = 0; i < desc_count; i++) {
610 reg_fields = fields + 3 + i * 14;
611 parse_hpx3_register(&hpx3, reg_fields);
612 program_hpx_type3(dev, &hpx3);
618 "%s: Type 3 Revision %d record not supported\n",
625 static acpi_status acpi_run_hpx(struct pci_dev *dev, acpi_handle handle)
628 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
629 union acpi_object *package, *record, *fields;
630 struct hpx_type0 hpx0;
631 struct hpx_type1 hpx1;
632 struct hpx_type2 hpx2;
636 status = acpi_evaluate_object(handle, "_HPX", NULL, &buffer);
637 if (ACPI_FAILURE(status))
640 package = (union acpi_object *)buffer.pointer;
641 if (package->type != ACPI_TYPE_PACKAGE) {
646 for (i = 0; i < package->package.count; i++) {
647 record = &package->package.elements[i];
648 if (record->type != ACPI_TYPE_PACKAGE) {
653 fields = record->package.elements;
654 if (fields[0].type != ACPI_TYPE_INTEGER ||
655 fields[1].type != ACPI_TYPE_INTEGER) {
660 type = fields[0].integer.value;
663 memset(&hpx0, 0, sizeof(hpx0));
664 status = decode_type0_hpx_record(record, &hpx0);
665 if (ACPI_FAILURE(status))
667 program_hpx_type0(dev, &hpx0);
670 memset(&hpx1, 0, sizeof(hpx1));
671 status = decode_type1_hpx_record(record, &hpx1);
672 if (ACPI_FAILURE(status))
674 program_hpx_type1(dev, &hpx1);
677 memset(&hpx2, 0, sizeof(hpx2));
678 status = decode_type2_hpx_record(record, &hpx2);
679 if (ACPI_FAILURE(status))
681 program_hpx_type2(dev, &hpx2);
684 status = program_type3_hpx_record(dev, record);
685 if (ACPI_FAILURE(status))
689 pr_err("%s: Type %d record not supported\n",
696 kfree(buffer.pointer);
700 static acpi_status acpi_run_hpp(struct pci_dev *dev, acpi_handle handle)
703 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
704 union acpi_object *package, *fields;
705 struct hpx_type0 hpx0;
708 memset(&hpx0, 0, sizeof(hpx0));
710 status = acpi_evaluate_object(handle, "_HPP", NULL, &buffer);
711 if (ACPI_FAILURE(status))
714 package = (union acpi_object *) buffer.pointer;
715 if (package->type != ACPI_TYPE_PACKAGE ||
716 package->package.count != 4) {
721 fields = package->package.elements;
722 for (i = 0; i < 4; i++) {
723 if (fields[i].type != ACPI_TYPE_INTEGER) {
730 hpx0.cache_line_size = fields[0].integer.value;
731 hpx0.latency_timer = fields[1].integer.value;
732 hpx0.enable_serr = fields[2].integer.value;
733 hpx0.enable_perr = fields[3].integer.value;
735 program_hpx_type0(dev, &hpx0);
738 kfree(buffer.pointer);
742 /* pci_acpi_program_hp_params
744 * @dev - the pci_dev for which we want parameters
746 int pci_acpi_program_hp_params(struct pci_dev *dev)
749 acpi_handle handle, phandle;
750 struct pci_bus *pbus;
752 if (acpi_pci_disabled)
756 for (pbus = dev->bus; pbus; pbus = pbus->parent) {
757 handle = acpi_pci_get_bridge_handle(pbus);
763 * _HPP settings apply to all child buses, until another _HPP is
764 * encountered. If we don't find an _HPP for the input pci dev,
765 * look for it in the parent device scope since that would apply to
769 status = acpi_run_hpx(dev, handle);
770 if (ACPI_SUCCESS(status))
772 status = acpi_run_hpp(dev, handle);
773 if (ACPI_SUCCESS(status))
775 if (acpi_is_root_bridge(handle))
777 status = acpi_get_parent(handle, &phandle);
778 if (ACPI_FAILURE(status))
786 * pciehp_is_native - Check whether a hotplug port is handled by the OS
787 * @bridge: Hotplug port to check
789 * Returns true if the given @bridge is handled by the native PCIe hotplug
792 bool pciehp_is_native(struct pci_dev *bridge)
794 const struct pci_host_bridge *host;
797 if (!IS_ENABLED(CONFIG_HOTPLUG_PCI_PCIE))
800 pcie_capability_read_dword(bridge, PCI_EXP_SLTCAP, &slot_cap);
801 if (!(slot_cap & PCI_EXP_SLTCAP_HPC))
804 if (pcie_ports_native)
807 host = pci_find_host_bridge(bridge->bus);
808 return host->native_pcie_hotplug;
812 * shpchp_is_native - Check whether a hotplug port is handled by the OS
813 * @bridge: Hotplug port to check
815 * Returns true if the given @bridge is handled by the native SHPC hotplug
818 bool shpchp_is_native(struct pci_dev *bridge)
820 return bridge->shpc_managed;
824 * pci_acpi_wake_bus - Root bus wakeup notification fork function.
825 * @context: Device wakeup context.
827 static void pci_acpi_wake_bus(struct acpi_device_wakeup_context *context)
829 struct acpi_device *adev;
830 struct acpi_pci_root *root;
832 adev = container_of(context, struct acpi_device, wakeup.context);
833 root = acpi_driver_data(adev);
834 pci_pme_wakeup_bus(root->bus);
838 * pci_acpi_wake_dev - PCI device wakeup notification work function.
839 * @context: Device wakeup context.
841 static void pci_acpi_wake_dev(struct acpi_device_wakeup_context *context)
843 struct pci_dev *pci_dev;
845 pci_dev = to_pci_dev(context->dev);
847 if (pci_dev->pme_poll)
848 pci_dev->pme_poll = false;
850 if (pci_dev->current_state == PCI_D3cold) {
851 pci_wakeup_event(pci_dev);
852 pm_request_resume(&pci_dev->dev);
856 /* Clear PME Status if set. */
857 if (pci_dev->pme_support)
858 pci_check_pme_status(pci_dev);
860 pci_wakeup_event(pci_dev);
861 pm_request_resume(&pci_dev->dev);
863 pci_pme_wakeup_bus(pci_dev->subordinate);
867 * pci_acpi_add_bus_pm_notifier - Register PM notifier for root PCI bus.
868 * @dev: PCI root bridge ACPI device.
870 acpi_status pci_acpi_add_bus_pm_notifier(struct acpi_device *dev)
872 return acpi_add_pm_notifier(dev, NULL, pci_acpi_wake_bus);
876 * pci_acpi_add_pm_notifier - Register PM notifier for given PCI device.
877 * @dev: ACPI device to add the notifier for.
878 * @pci_dev: PCI device to check for the PME status if an event is signaled.
880 acpi_status pci_acpi_add_pm_notifier(struct acpi_device *dev,
881 struct pci_dev *pci_dev)
883 return acpi_add_pm_notifier(dev, &pci_dev->dev, pci_acpi_wake_dev);
887 * _SxD returns the D-state with the highest power
888 * (lowest D-state number) supported in the S-state "x".
890 * If the devices does not have a _PRW
891 * (Power Resources for Wake) supporting system wakeup from "x"
892 * then the OS is free to choose a lower power (higher number
893 * D-state) than the return value from _SxD.
895 * But if _PRW is enabled at S-state "x", the OS
896 * must not choose a power lower than _SxD --
897 * unless the device has an _SxW method specifying
898 * the lowest power (highest D-state number) the device
899 * may enter while still able to wake the system.
901 * ie. depending on global OS policy:
903 * if (_PRW at S-state x)
904 * choose from highest power _SxD to lowest power _SxW
905 * else // no _PRW at S-state x
906 * choose highest power _SxD or any lower power
909 static pci_power_t acpi_pci_choose_state(struct pci_dev *pdev)
911 int acpi_state, d_max;
914 d_max = ACPI_STATE_D3_HOT;
916 d_max = ACPI_STATE_D3_COLD;
917 acpi_state = acpi_pm_device_sleep_state(&pdev->dev, NULL, d_max);
919 return PCI_POWER_ERROR;
921 switch (acpi_state) {
928 case ACPI_STATE_D3_HOT:
930 case ACPI_STATE_D3_COLD:
933 return PCI_POWER_ERROR;
936 static struct acpi_device *acpi_pci_find_companion(struct device *dev);
938 void pci_set_acpi_fwnode(struct pci_dev *dev)
940 if (!dev_fwnode(&dev->dev) && !pci_dev_is_added(dev))
941 ACPI_COMPANION_SET(&dev->dev,
942 acpi_pci_find_companion(&dev->dev));
946 * pci_dev_acpi_reset - do a function level reset using _RST method
947 * @dev: device to reset
948 * @probe: if true, return 0 if device supports _RST
950 int pci_dev_acpi_reset(struct pci_dev *dev, bool probe)
952 acpi_handle handle = ACPI_HANDLE(&dev->dev);
954 if (!handle || !acpi_has_method(handle, "_RST"))
960 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_RST", NULL, NULL))) {
961 pci_warn(dev, "ACPI _RST failed\n");
968 static bool acpi_pci_power_manageable(struct pci_dev *dev)
970 struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
974 return acpi_device_power_manageable(adev);
977 static bool acpi_pci_bridge_d3(struct pci_dev *dev)
979 const union acpi_object *obj;
980 struct acpi_device *adev;
981 struct pci_dev *rpdev;
983 if (!dev->is_hotplug_bridge)
986 /* Assume D3 support if the bridge is power-manageable by ACPI. */
987 if (acpi_pci_power_manageable(dev))
991 * The ACPI firmware will provide the device-specific properties through
992 * _DSD configuration object. Look for the 'HotPlugSupportInD3' property
993 * for the root port and if it is set we know the hierarchy behind it
994 * supports D3 just fine.
996 rpdev = pcie_find_root_port(dev);
1000 adev = ACPI_COMPANION(&rpdev->dev);
1004 if (acpi_dev_get_property(adev, "HotPlugSupportInD3",
1005 ACPI_TYPE_INTEGER, &obj) < 0)
1008 return obj->integer.value == 1;
1011 static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
1013 struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
1014 static const u8 state_conv[] = {
1015 [PCI_D0] = ACPI_STATE_D0,
1016 [PCI_D1] = ACPI_STATE_D1,
1017 [PCI_D2] = ACPI_STATE_D2,
1018 [PCI_D3hot] = ACPI_STATE_D3_HOT,
1019 [PCI_D3cold] = ACPI_STATE_D3_COLD,
1021 int error = -EINVAL;
1023 /* If the ACPI device has _EJ0, ignore the device */
1024 if (!adev || acpi_has_method(adev->handle, "_EJ0"))
1029 if (dev_pm_qos_flags(&dev->dev, PM_QOS_FLAG_NO_POWER_OFF) ==
1039 error = acpi_device_set_power(adev, state_conv[state]);
1043 pci_dbg(dev, "power state changed by ACPI to %s\n",
1044 acpi_power_state_string(adev->power.state));
1049 static pci_power_t acpi_pci_get_power_state(struct pci_dev *dev)
1051 struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
1052 static const pci_power_t state_conv[] = {
1053 [ACPI_STATE_D0] = PCI_D0,
1054 [ACPI_STATE_D1] = PCI_D1,
1055 [ACPI_STATE_D2] = PCI_D2,
1056 [ACPI_STATE_D3_HOT] = PCI_D3hot,
1057 [ACPI_STATE_D3_COLD] = PCI_D3cold,
1061 if (!adev || !acpi_device_power_manageable(adev))
1064 state = adev->power.state;
1065 if (state == ACPI_STATE_UNKNOWN)
1068 return state_conv[state];
1071 static void acpi_pci_refresh_power_state(struct pci_dev *dev)
1073 struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
1075 if (adev && acpi_device_power_manageable(adev))
1076 acpi_device_update_power(adev, NULL);
1079 static int acpi_pci_propagate_wakeup(struct pci_bus *bus, bool enable)
1081 while (bus->parent) {
1082 if (acpi_pm_device_can_wakeup(&bus->self->dev))
1083 return acpi_pm_set_device_wakeup(&bus->self->dev, enable);
1088 /* We have reached the root bus. */
1090 if (acpi_pm_device_can_wakeup(bus->bridge))
1091 return acpi_pm_set_device_wakeup(bus->bridge, enable);
1096 static int acpi_pci_wakeup(struct pci_dev *dev, bool enable)
1098 if (acpi_pm_device_can_wakeup(&dev->dev))
1099 return acpi_pm_set_device_wakeup(&dev->dev, enable);
1101 return acpi_pci_propagate_wakeup(dev->bus, enable);
1104 static bool acpi_pci_need_resume(struct pci_dev *dev)
1106 struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
1109 * In some cases (eg. Samsung 305V4A) leaving a bridge in suspend over
1110 * system-wide suspend/resume confuses the platform firmware, so avoid
1111 * doing that. According to Section 16.1.6 of ACPI 6.2, endpoint
1112 * devices are expected to be in D3 before invoking the S3 entry path
1113 * from the firmware, so they should not be affected by this issue.
1115 if (pci_is_bridge(dev) && acpi_target_system_state() != ACPI_STATE_S0)
1118 if (!adev || !acpi_device_power_manageable(adev))
1121 if (adev->wakeup.flags.valid &&
1122 device_may_wakeup(&dev->dev) != !!adev->wakeup.prepare_count)
1125 if (acpi_target_system_state() == ACPI_STATE_S0)
1128 return !!adev->power.flags.dsw_present;
1131 static const struct pci_platform_pm_ops acpi_pci_platform_pm = {
1132 .bridge_d3 = acpi_pci_bridge_d3,
1133 .is_manageable = acpi_pci_power_manageable,
1134 .set_state = acpi_pci_set_power_state,
1135 .get_state = acpi_pci_get_power_state,
1136 .refresh_state = acpi_pci_refresh_power_state,
1137 .choose_state = acpi_pci_choose_state,
1138 .set_wakeup = acpi_pci_wakeup,
1139 .need_resume = acpi_pci_need_resume,
1142 void acpi_pci_add_bus(struct pci_bus *bus)
1144 union acpi_object *obj;
1145 struct pci_host_bridge *bridge;
1147 if (acpi_pci_disabled || !bus->bridge || !ACPI_HANDLE(bus->bridge))
1150 acpi_pci_slot_enumerate(bus);
1151 acpiphp_enumerate_slots(bus);
1154 * For a host bridge, check its _DSM for function 8 and if
1155 * that is available, mark it in pci_host_bridge.
1157 if (!pci_is_root_bus(bus))
1160 obj = acpi_evaluate_dsm(ACPI_HANDLE(bus->bridge), &pci_acpi_dsm_guid, 3,
1161 DSM_PCI_POWER_ON_RESET_DELAY, NULL);
1165 if (obj->type == ACPI_TYPE_INTEGER && obj->integer.value == 1) {
1166 bridge = pci_find_host_bridge(bus);
1167 bridge->ignore_reset_delay = 1;
1172 void acpi_pci_remove_bus(struct pci_bus *bus)
1174 if (acpi_pci_disabled || !bus->bridge)
1177 acpiphp_remove_slots(bus);
1178 acpi_pci_slot_remove(bus);
1184 static DECLARE_RWSEM(pci_acpi_companion_lookup_sem);
1185 static struct acpi_device *(*pci_acpi_find_companion_hook)(struct pci_dev *);
1188 * pci_acpi_set_companion_lookup_hook - Set ACPI companion lookup callback.
1189 * @func: ACPI companion lookup callback pointer or NULL.
1191 * Set a special ACPI companion lookup callback for PCI devices whose companion
1192 * objects in the ACPI namespace have _ADR with non-standard bus-device-function
1195 * Return 0 on success or a negative error code on failure (in which case no
1196 * changes are made).
1198 * The caller is responsible for the appropriate ordering of the invocations of
1199 * this function with respect to the enumeration of the PCI devices needing the
1200 * callback installed by it.
1202 int pci_acpi_set_companion_lookup_hook(struct acpi_device *(*func)(struct pci_dev *))
1209 down_write(&pci_acpi_companion_lookup_sem);
1211 if (pci_acpi_find_companion_hook) {
1214 pci_acpi_find_companion_hook = func;
1218 up_write(&pci_acpi_companion_lookup_sem);
1222 EXPORT_SYMBOL_GPL(pci_acpi_set_companion_lookup_hook);
1225 * pci_acpi_clear_companion_lookup_hook - Clear ACPI companion lookup callback.
1227 * Clear the special ACPI companion lookup callback previously set by
1228 * pci_acpi_set_companion_lookup_hook(). Block until the last running instance
1229 * of the callback returns before clearing it.
1231 * The caller is responsible for the appropriate ordering of the invocations of
1232 * this function with respect to the enumeration of the PCI devices needing the
1233 * callback cleared by it.
1235 void pci_acpi_clear_companion_lookup_hook(void)
1237 down_write(&pci_acpi_companion_lookup_sem);
1239 pci_acpi_find_companion_hook = NULL;
1241 up_write(&pci_acpi_companion_lookup_sem);
1243 EXPORT_SYMBOL_GPL(pci_acpi_clear_companion_lookup_hook);
1245 static struct acpi_device *acpi_pci_find_companion(struct device *dev)
1247 struct pci_dev *pci_dev = to_pci_dev(dev);
1248 struct acpi_device *adev;
1249 bool check_children;
1252 down_read(&pci_acpi_companion_lookup_sem);
1254 adev = pci_acpi_find_companion_hook ?
1255 pci_acpi_find_companion_hook(pci_dev) : NULL;
1257 up_read(&pci_acpi_companion_lookup_sem);
1262 check_children = pci_is_bridge(pci_dev);
1263 /* Please ref to ACPI spec for the syntax of _ADR */
1264 addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
1265 adev = acpi_find_child_device(ACPI_COMPANION(dev->parent), addr,
1269 * There may be ACPI device objects in the ACPI namespace that are
1270 * children of the device object representing the host bridge, but don't
1271 * represent PCI devices. Both _HID and _ADR may be present for them,
1272 * even though that is against the specification (for example, see
1273 * Section 6.1 of ACPI 6.3), but in many cases the _ADR returns 0 which
1274 * appears to indicate that they should not be taken into consideration
1275 * as potential companions of PCI devices on the root bus.
1277 * To catch this special case, disregard the returned device object if
1278 * it has a valid _HID, addr is 0 and the PCI device at hand is on the
1281 if (adev && adev->pnp.type.platform_id && !addr &&
1282 pci_is_root_bus(pci_dev->bus))
1289 * pci_acpi_optimize_delay - optimize PCI D3 and D3cold delay from ACPI
1290 * @pdev: the PCI device whose delay is to be updated
1291 * @handle: ACPI handle of this device
1293 * Update the d3hot_delay and d3cold_delay of a PCI device from the ACPI _DSM
1294 * control method of either the device itself or the PCI host bridge.
1296 * Function 8, "Reset Delay," applies to the entire hierarchy below a PCI
1297 * host bridge. If it returns one, the OS may assume that all devices in
1298 * the hierarchy have already completed power-on reset delays.
1300 * Function 9, "Device Readiness Durations," applies only to the object
1301 * where it is located. It returns delay durations required after various
1302 * events if the device requires less time than the spec requires. Delays
1303 * from this function take precedence over the Reset Delay function.
1305 * These _DSM functions are defined by the draft ECN of January 28, 2014,
1306 * titled "ACPI additions for FW latency optimizations."
1308 static void pci_acpi_optimize_delay(struct pci_dev *pdev,
1311 struct pci_host_bridge *bridge = pci_find_host_bridge(pdev->bus);
1313 union acpi_object *obj, *elements;
1315 if (bridge->ignore_reset_delay)
1316 pdev->d3cold_delay = 0;
1318 obj = acpi_evaluate_dsm(handle, &pci_acpi_dsm_guid, 3,
1319 DSM_PCI_DEVICE_READINESS_DURATIONS, NULL);
1323 if (obj->type == ACPI_TYPE_PACKAGE && obj->package.count == 5) {
1324 elements = obj->package.elements;
1325 if (elements[0].type == ACPI_TYPE_INTEGER) {
1326 value = (int)elements[0].integer.value / 1000;
1327 if (value < PCI_PM_D3COLD_WAIT)
1328 pdev->d3cold_delay = value;
1330 if (elements[3].type == ACPI_TYPE_INTEGER) {
1331 value = (int)elements[3].integer.value / 1000;
1332 if (value < PCI_PM_D3HOT_WAIT)
1333 pdev->d3hot_delay = value;
1339 static void pci_acpi_set_external_facing(struct pci_dev *dev)
1343 if (pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT)
1345 if (device_property_read_u8(&dev->dev, "ExternalFacingPort", &val))
1349 * These root ports expose PCIe (including DMA) outside of the
1350 * system. Everything downstream from them is external.
1353 dev->external_facing = 1;
1356 static void pci_acpi_setup(struct device *dev)
1358 struct pci_dev *pci_dev = to_pci_dev(dev);
1359 struct acpi_device *adev = ACPI_COMPANION(dev);
1364 pci_acpi_optimize_delay(pci_dev, adev->handle);
1365 pci_acpi_set_external_facing(pci_dev);
1366 pci_acpi_add_edr_notifier(pci_dev);
1368 pci_acpi_add_pm_notifier(adev, pci_dev);
1369 if (!adev->wakeup.flags.valid)
1372 device_set_wakeup_capable(dev, true);
1374 * For bridges that can do D3 we enable wake automatically (as
1375 * we do for the power management itself in that case). The
1376 * reason is that the bridge may have additional methods such as
1377 * _DSW that need to be called.
1379 if (pci_dev->bridge_d3)
1380 device_wakeup_enable(dev);
1382 acpi_pci_wakeup(pci_dev, false);
1383 acpi_device_power_add_dependent(adev, dev);
1386 static void pci_acpi_cleanup(struct device *dev)
1388 struct acpi_device *adev = ACPI_COMPANION(dev);
1389 struct pci_dev *pci_dev = to_pci_dev(dev);
1394 pci_acpi_remove_edr_notifier(pci_dev);
1395 pci_acpi_remove_pm_notifier(adev);
1396 if (adev->wakeup.flags.valid) {
1397 acpi_device_power_remove_dependent(adev, dev);
1398 if (pci_dev->bridge_d3)
1399 device_wakeup_disable(dev);
1401 device_set_wakeup_capable(dev, false);
1405 static bool pci_acpi_bus_match(struct device *dev)
1407 return dev_is_pci(dev);
1410 static struct acpi_bus_type acpi_pci_bus = {
1412 .match = pci_acpi_bus_match,
1413 .find_companion = acpi_pci_find_companion,
1414 .setup = pci_acpi_setup,
1415 .cleanup = pci_acpi_cleanup,
1419 static struct fwnode_handle *(*pci_msi_get_fwnode_cb)(struct device *dev);
1422 * pci_msi_register_fwnode_provider - Register callback to retrieve fwnode
1423 * @fn: Callback matching a device to a fwnode that identifies a PCI
1426 * This should be called by irqchip driver, which is the parent of
1427 * the MSI domain to provide callback interface to query fwnode.
1430 pci_msi_register_fwnode_provider(struct fwnode_handle *(*fn)(struct device *))
1432 pci_msi_get_fwnode_cb = fn;
1436 * pci_host_bridge_acpi_msi_domain - Retrieve MSI domain of a PCI host bridge
1437 * @bus: The PCI host bridge bus.
1439 * This function uses the callback function registered by
1440 * pci_msi_register_fwnode_provider() to retrieve the irq_domain with
1441 * type DOMAIN_BUS_PCI_MSI of the specified host bridge bus.
1442 * This returns NULL on error or when the domain is not found.
1444 struct irq_domain *pci_host_bridge_acpi_msi_domain(struct pci_bus *bus)
1446 struct fwnode_handle *fwnode;
1448 if (!pci_msi_get_fwnode_cb)
1451 fwnode = pci_msi_get_fwnode_cb(&bus->dev);
1455 return irq_find_matching_fwnode(fwnode, DOMAIN_BUS_PCI_MSI);
1458 static int __init acpi_pci_init(void)
1462 if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_MSI) {
1463 pr_info("ACPI FADT declares the system doesn't support MSI, so disable it\n");
1467 if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_ASPM) {
1468 pr_info("ACPI FADT declares the system doesn't support PCIe ASPM, so disable it\n");
1472 ret = register_acpi_bus_type(&acpi_pci_bus);
1476 pci_set_platform_pm(&acpi_pci_platform_pm);
1477 acpi_pci_slot_init();
1482 arch_initcall(acpi_pci_init);