1 // SPDX-License-Identifier: GPL-2.0-only
3 * IOAPIC/IOxAPIC/IOSAPIC driver
5 * Copyright (C) 2009 Fujitsu Limited.
6 * (c) Copyright 2009 Hewlett-Packard Development Company, L.P.
8 * Copyright (C) 2014 Intel Corporation
10 * Based on original drivers/pci/ioapic.c
11 * Yinghai Lu <yinghai@kernel.org>
12 * Jiang Liu <jiang.liu@intel.com>
16 * This driver manages I/O APICs added by hotplug after boot.
17 * We try to claim all I/O APIC devices, but those present at boot were
18 * registered when we parsed the ACPI MADT.
21 #define pr_fmt(fmt) "ACPI: IOAPIC: " fmt
23 #include <linux/slab.h>
24 #include <linux/acpi.h>
25 #include <linux/pci.h>
26 #include <acpi/acpi.h>
28 struct acpi_pci_ioapic {
29 acpi_handle root_handle;
34 struct list_head list;
37 static LIST_HEAD(ioapic_list);
38 static DEFINE_MUTEX(ioapic_list_lock);
40 static acpi_status setup_res(struct acpi_resource *acpi_res, void *data)
42 struct resource *res = data;
43 struct resource_win win;
46 * We might assign this to 'res' later, make sure all pointers are
47 * cleared before the resource is added to the global list
49 memset(&win, 0, sizeof(win));
52 if (acpi_dev_filter_resource_type(acpi_res, IORESOURCE_MEM))
55 if (!acpi_dev_resource_memory(acpi_res, res)) {
56 if (acpi_dev_resource_address_space(acpi_res, &win) ||
57 acpi_dev_resource_ext_address_space(acpi_res, &win))
60 if ((res->flags & IORESOURCE_PREFETCH) ||
61 (res->flags & IORESOURCE_DISABLED))
64 return AE_CTRL_TERMINATE;
67 static bool acpi_is_ioapic(acpi_handle handle, char **type)
70 struct acpi_device_info *info;
74 if (!acpi_has_method(handle, "_GSB"))
77 status = acpi_get_object_info(handle, &info);
78 if (ACPI_SUCCESS(status)) {
79 if (info->valid & ACPI_VALID_HID)
80 hid = info->hardware_id.string;
82 if (strcmp(hid, "ACPI0009") == 0) {
85 } else if (strcmp(hid, "ACPI000A") == 0) {
96 static acpi_status handle_ioapic_add(acpi_handle handle, u32 lvl,
97 void *context, void **rv)
100 unsigned long long gsi_base;
101 struct acpi_pci_ioapic *ioapic;
102 struct pci_dev *dev = NULL;
103 struct resource *res = NULL, *pci_res = NULL, *crs_res;
106 if (!acpi_is_ioapic(handle, &type))
109 mutex_lock(&ioapic_list_lock);
110 list_for_each_entry(ioapic, &ioapic_list, list)
111 if (ioapic->handle == handle) {
112 mutex_unlock(&ioapic_list_lock);
116 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsi_base);
117 if (ACPI_FAILURE(status)) {
118 acpi_handle_warn(handle, "failed to evaluate _GSB method\n");
122 ioapic = kzalloc(sizeof(*ioapic), GFP_KERNEL);
124 pr_err("cannot allocate memory for new IOAPIC\n");
127 ioapic->root_handle = (acpi_handle)context;
128 ioapic->handle = handle;
129 ioapic->gsi_base = (u32)gsi_base;
130 INIT_LIST_HEAD(&ioapic->list);
133 if (acpi_ioapic_registered(handle, (u32)gsi_base))
136 dev = acpi_get_pci_dev(handle);
137 if (dev && pci_resource_len(dev, 0)) {
138 if (pci_enable_device(dev) < 0)
141 if (pci_request_region(dev, 0, type))
143 pci_res = &dev->resource[0];
150 crs_res = &ioapic->res;
151 acpi_walk_resources(handle, METHOD_NAME__CRS, setup_res, crs_res);
152 crs_res->name = type;
153 crs_res->flags |= IORESOURCE_BUSY;
154 if (crs_res->flags == 0) {
155 acpi_handle_warn(handle, "failed to get resource\n");
157 } else if (insert_resource(&iomem_resource, crs_res)) {
158 acpi_handle_warn(handle, "failed to insert resource\n");
162 /* try pci resource first, then "_CRS" resource */
164 if (!res || !res->flags)
167 if (acpi_register_ioapic(handle, res->start, (u32)gsi_base)) {
168 acpi_handle_warn(handle, "failed to register IOAPIC\n");
172 list_add(&ioapic->list, &ioapic_list);
173 mutex_unlock(&ioapic_list_lock);
176 dev_info(&dev->dev, "%s at %pR, GSI %u\n",
177 type, res, (u32)gsi_base);
179 acpi_handle_info(handle, "%s at %pR, GSI %u\n",
180 type, res, (u32)gsi_base);
186 pci_release_region(dev, 0);
187 if (ioapic->res.flags && ioapic->res.parent)
188 release_resource(&ioapic->res);
191 pci_disable_device(dev);
196 mutex_unlock(&ioapic_list_lock);
197 *(acpi_status *)rv = AE_ERROR;
201 int acpi_ioapic_add(acpi_handle root_handle)
203 acpi_status status, retval = AE_OK;
205 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, root_handle,
206 UINT_MAX, handle_ioapic_add, NULL,
207 root_handle, (void **)&retval);
209 return ACPI_SUCCESS(status) && ACPI_SUCCESS(retval) ? 0 : -ENODEV;
212 void pci_ioapic_remove(struct acpi_pci_root *root)
214 struct acpi_pci_ioapic *ioapic, *tmp;
216 mutex_lock(&ioapic_list_lock);
217 list_for_each_entry_safe(ioapic, tmp, &ioapic_list, list) {
218 if (root->device->handle != ioapic->root_handle)
221 pci_release_region(ioapic->pdev, 0);
222 pci_disable_device(ioapic->pdev);
223 pci_dev_put(ioapic->pdev);
226 mutex_unlock(&ioapic_list_lock);
229 int acpi_ioapic_remove(struct acpi_pci_root *root)
232 struct acpi_pci_ioapic *ioapic, *tmp;
234 mutex_lock(&ioapic_list_lock);
235 list_for_each_entry_safe(ioapic, tmp, &ioapic_list, list) {
236 if (root->device->handle != ioapic->root_handle)
238 if (acpi_unregister_ioapic(ioapic->handle, ioapic->gsi_base))
240 if (ioapic->res.flags && ioapic->res.parent)
241 release_resource(&ioapic->res);
242 list_del(&ioapic->list);
245 mutex_unlock(&ioapic_list_lock);