2 * IOAPIC/IOxAPIC/IOSAPIC driver
4 * Copyright (C) 2009 Fujitsu Limited.
5 * (c) Copyright 2009 Hewlett-Packard Development Company, L.P.
7 * Copyright (C) 2014 Intel Corporation
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * Based on original drivers/pci/ioapic.c
14 * Yinghai Lu <yinghai@kernel.org>
15 * Jiang Liu <jiang.liu@intel.com>
19 * This driver manages I/O APICs added by hotplug after boot.
20 * We try to claim all I/O APIC devices, but those present at boot were
21 * registered when we parsed the ACPI MADT.
24 #define pr_fmt(fmt) "ACPI : IOAPIC: " fmt
26 #include <linux/slab.h>
27 #include <linux/acpi.h>
28 #include <linux/pci.h>
29 #include <acpi/acpi.h>
31 struct acpi_pci_ioapic
{
32 acpi_handle root_handle
;
37 struct list_head list
;
40 static LIST_HEAD(ioapic_list
);
41 static DEFINE_MUTEX(ioapic_list_lock
);
43 static acpi_status
setup_res(struct acpi_resource
*acpi_res
, void *data
)
45 struct resource
*res
= data
;
46 struct resource_win win
;
49 if (acpi_dev_filter_resource_type(acpi_res
, IORESOURCE_MEM
))
52 if (!acpi_dev_resource_memory(acpi_res
, res
)) {
53 if (acpi_dev_resource_address_space(acpi_res
, &win
) ||
54 acpi_dev_resource_ext_address_space(acpi_res
, &win
))
57 if ((res
->flags
& IORESOURCE_PREFETCH
) ||
58 (res
->flags
& IORESOURCE_DISABLED
))
61 return AE_CTRL_TERMINATE
;
64 static bool acpi_is_ioapic(acpi_handle handle
, char **type
)
67 struct acpi_device_info
*info
;
71 if (!acpi_has_method(handle
, "_GSB"))
74 status
= acpi_get_object_info(handle
, &info
);
75 if (ACPI_SUCCESS(status
)) {
76 if (info
->valid
& ACPI_VALID_HID
)
77 hid
= info
->hardware_id
.string
;
79 if (strcmp(hid
, "ACPI0009") == 0) {
82 } else if (strcmp(hid
, "ACPI000A") == 0) {
93 static acpi_status
handle_ioapic_add(acpi_handle handle
, u32 lvl
,
94 void *context
, void **rv
)
97 unsigned long long gsi_base
;
98 struct acpi_pci_ioapic
*ioapic
;
99 struct pci_dev
*dev
= NULL
;
100 struct resource
*res
= NULL
, *pci_res
= NULL
, *crs_res
;
103 if (!acpi_is_ioapic(handle
, &type
))
106 mutex_lock(&ioapic_list_lock
);
107 list_for_each_entry(ioapic
, &ioapic_list
, list
)
108 if (ioapic
->handle
== handle
) {
109 mutex_unlock(&ioapic_list_lock
);
113 status
= acpi_evaluate_integer(handle
, "_GSB", NULL
, &gsi_base
);
114 if (ACPI_FAILURE(status
)) {
115 acpi_handle_warn(handle
, "failed to evaluate _GSB method\n");
119 ioapic
= kzalloc(sizeof(*ioapic
), GFP_KERNEL
);
121 pr_err("cannot allocate memory for new IOAPIC\n");
124 ioapic
->root_handle
= (acpi_handle
)context
;
125 ioapic
->handle
= handle
;
126 ioapic
->gsi_base
= (u32
)gsi_base
;
127 INIT_LIST_HEAD(&ioapic
->list
);
130 if (acpi_ioapic_registered(handle
, (u32
)gsi_base
))
133 dev
= acpi_get_pci_dev(handle
);
134 if (dev
&& pci_resource_len(dev
, 0)) {
135 if (pci_enable_device(dev
) < 0)
138 if (pci_request_region(dev
, 0, type
))
140 pci_res
= &dev
->resource
[0];
147 crs_res
= &ioapic
->res
;
148 acpi_walk_resources(handle
, METHOD_NAME__CRS
, setup_res
, crs_res
);
149 crs_res
->name
= type
;
150 crs_res
->flags
|= IORESOURCE_BUSY
;
151 if (crs_res
->flags
== 0) {
152 acpi_handle_warn(handle
, "failed to get resource\n");
154 } else if (insert_resource(&iomem_resource
, crs_res
)) {
155 acpi_handle_warn(handle
, "failed to insert resource\n");
159 /* try pci resource first, then "_CRS" resource */
161 if (!res
|| !res
->flags
)
164 if (acpi_register_ioapic(handle
, res
->start
, (u32
)gsi_base
)) {
165 acpi_handle_warn(handle
, "failed to register IOAPIC\n");
169 list_add(&ioapic
->list
, &ioapic_list
);
170 mutex_unlock(&ioapic_list_lock
);
173 dev_info(&dev
->dev
, "%s at %pR, GSI %u\n",
174 type
, res
, (u32
)gsi_base
);
176 acpi_handle_info(handle
, "%s at %pR, GSI %u\n",
177 type
, res
, (u32
)gsi_base
);
183 pci_release_region(dev
, 0);
184 if (ioapic
->res
.flags
&& ioapic
->res
.parent
)
185 release_resource(&ioapic
->res
);
188 pci_disable_device(dev
);
193 mutex_unlock(&ioapic_list_lock
);
194 *(acpi_status
*)rv
= AE_ERROR
;
198 int acpi_ioapic_add(acpi_handle root_handle
)
200 acpi_status status
, retval
= AE_OK
;
202 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, root_handle
,
203 UINT_MAX
, handle_ioapic_add
, NULL
,
204 root_handle
, (void **)&retval
);
206 return ACPI_SUCCESS(status
) && ACPI_SUCCESS(retval
) ? 0 : -ENODEV
;
209 int acpi_ioapic_remove(struct acpi_pci_root
*root
)
212 struct acpi_pci_ioapic
*ioapic
, *tmp
;
214 mutex_lock(&ioapic_list_lock
);
215 list_for_each_entry_safe(ioapic
, tmp
, &ioapic_list
, list
) {
216 if (root
->device
->handle
!= ioapic
->root_handle
)
219 if (acpi_unregister_ioapic(ioapic
->handle
, ioapic
->gsi_base
))
223 pci_release_region(ioapic
->pdev
, 0);
224 pci_disable_device(ioapic
->pdev
);
225 pci_dev_put(ioapic
->pdev
);
227 if (ioapic
->res
.flags
&& ioapic
->res
.parent
)
228 release_resource(&ioapic
->res
);
229 list_del(&ioapic
->list
);
232 mutex_unlock(&ioapic_list_lock
);