^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * pci_slot.c - ACPI PCI Slot Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * The code here is heavily leveraged from the acpiphp module.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Thanks to Matthew Wilcox <matthew@wil.cx> for much guidance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Thanks to Kenji Kaneshige <kaneshige.kenji@jp.fujitsu.com> for code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * review and fixes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Copyright (C) 2007-2008 Hewlett-Packard Development Company, L.P.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Alex Chiang <achiang@hp.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Copyright (C) 2013 Huawei Tech. Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Jiang Liu <jiang.liu@huawei.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/dmi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/pci-acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static int check_sta_before_sun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define SLOT_NAME_SIZE 21 /* Inspired by #define in acpiphp.h */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct acpi_pci_slot {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct pci_slot *pci_slot; /* corresponding pci_slot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct list_head list; /* node in the list of slots */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static LIST_HEAD(slot_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static DEFINE_MUTEX(slot_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) check_slot(acpi_handle handle, unsigned long long *sun)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int device = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) unsigned long long adr, sta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) pr_debug("Checking slot on path: %s\n", (char *)buffer.pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (check_sta_before_sun) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /* If SxFy doesn't have _STA, we just assume it's there */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (ACPI_SUCCESS(status) && !(sta & ACPI_STA_DEVICE_PRESENT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (ACPI_FAILURE(status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) pr_debug("_ADR returned %d on %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) status, (char *)buffer.pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /* No _SUN == not a slot == bail */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) status = acpi_evaluate_integer(handle, "_SUN", NULL, sun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (ACPI_FAILURE(status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) pr_debug("_SUN returned %d on %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) status, (char *)buffer.pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) device = (adr >> 16) & 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) kfree(buffer.pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * Check whether handle has an associated slot and create PCI slot if it has.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static acpi_status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) int device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) unsigned long long sun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) char name[SLOT_NAME_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct acpi_pci_slot *slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct pci_slot *pci_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct pci_bus *pci_bus = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) device = check_slot(handle, &sun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (device < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return AE_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * There may be multiple PCI functions associated with the same slot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * Check whether PCI slot has already been created for this PCI device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) list_for_each_entry(slot, &slot_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) pci_slot = slot->pci_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (pci_slot->bus == pci_bus && pci_slot->number == device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return AE_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) slot = kmalloc(sizeof(*slot), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (!slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return AE_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) snprintf(name, sizeof(name), "%llu", sun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) pci_slot = pci_create_slot(pci_bus, device, name, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (IS_ERR(pci_slot)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) pr_err("pci_create_slot returned %ld\n", PTR_ERR(pci_slot));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) kfree(slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return AE_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) slot->pci_slot = pci_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) list_add(&slot->list, &slot_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) get_device(&pci_bus->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) pr_debug("%p, pci_bus: %x, device: %d, name: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) pci_slot, pci_bus->number, device, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return AE_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) void acpi_pci_slot_enumerate(struct pci_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) acpi_handle handle = ACPI_HANDLE(bus->bridge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (handle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) mutex_lock(&slot_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) register_slot, NULL, bus, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) mutex_unlock(&slot_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) void acpi_pci_slot_remove(struct pci_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct acpi_pci_slot *slot, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) mutex_lock(&slot_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) list_for_each_entry_safe(slot, tmp, &slot_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (slot->pci_slot->bus == bus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) list_del(&slot->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) pci_destroy_slot(slot->pci_slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) put_device(&bus->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) kfree(slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) mutex_unlock(&slot_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static int do_sta_before_sun(const struct dmi_system_id *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) pr_info("%s detected: will evaluate _STA before calling _SUN\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) d->ident);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) check_sta_before_sun = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static const struct dmi_system_id acpi_pci_slot_dmi_table[] __initconst = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * Fujitsu Primequest machines will return 1023 to indicate an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * error if the _SUN method is evaluated on SxFy objects that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * are not present (as indicated by _STA), so for those machines,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * we want to check _STA before evaluating _SUN.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) .callback = do_sta_before_sun,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) .ident = "Fujitsu PRIMEQUEST",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) .matches = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) DMI_MATCH(DMI_BIOS_VENDOR, "FUJITSU LIMITED"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) DMI_MATCH(DMI_BIOS_VERSION, "PRIMEQUEST"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) void __init acpi_pci_slot_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) dmi_check_system(acpi_pci_slot_dmi_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }