Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * IOAPIC/IOxAPIC/IOSAPIC driver
4 *
5 * Copyright (C) 2009 Fujitsu Limited.
6 * (c) Copyright 2009 Hewlett-Packard Development Company, L.P.
7 *
8 * Copyright (C) 2014 Intel Corporation
9 *
10 * Based on original drivers/pci/ioapic.c
11 * Yinghai Lu <yinghai@kernel.org>
12 * Jiang Liu <jiang.liu@intel.com>
13 */
14
15/*
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.
19 */
20
21#define pr_fmt(fmt) "ACPI: IOAPIC: " fmt
22
23#include <linux/slab.h>
24#include <linux/acpi.h>
25#include <linux/pci.h>
26#include <acpi/acpi.h>
27#include "internal.h"
28
29struct acpi_pci_ioapic {
30 acpi_handle root_handle;
31 acpi_handle handle;
32 u32 gsi_base;
33 struct resource res;
34 struct pci_dev *pdev;
35 struct list_head list;
36};
37
38static LIST_HEAD(ioapic_list);
39static DEFINE_MUTEX(ioapic_list_lock);
40
41static acpi_status setup_res(struct acpi_resource *acpi_res, void *data)
42{
43 struct resource *res = data;
44 struct resource_win win;
45
46 /*
47 * We might assign this to 'res' later, make sure all pointers are
48 * cleared before the resource is added to the global list
49 */
50 memset(&win, 0, sizeof(win));
51
52 res->flags = 0;
53 if (acpi_dev_filter_resource_type(acpi_res, IORESOURCE_MEM))
54 return AE_OK;
55
56 if (!acpi_dev_resource_memory(acpi_res, res)) {
57 if (acpi_dev_resource_address_space(acpi_res, &win) ||
58 acpi_dev_resource_ext_address_space(acpi_res, &win))
59 *res = win.res;
60 }
61 if ((res->flags & IORESOURCE_PREFETCH) ||
62 (res->flags & IORESOURCE_DISABLED))
63 res->flags = 0;
64
65 return AE_CTRL_TERMINATE;
66}
67
68static bool acpi_is_ioapic(acpi_handle handle, char **type)
69{
70 acpi_status status;
71 struct acpi_device_info *info;
72 char *hid = NULL;
73 bool match = false;
74
75 if (!acpi_has_method(handle, "_GSB"))
76 return false;
77
78 status = acpi_get_object_info(handle, &info);
79 if (ACPI_SUCCESS(status)) {
80 if (info->valid & ACPI_VALID_HID)
81 hid = info->hardware_id.string;
82 if (hid) {
83 if (strcmp(hid, "ACPI0009") == 0) {
84 *type = "IOxAPIC";
85 match = true;
86 } else if (strcmp(hid, "ACPI000A") == 0) {
87 *type = "IOAPIC";
88 match = true;
89 }
90 }
91 kfree(info);
92 }
93
94 return match;
95}
96
97static acpi_status handle_ioapic_add(acpi_handle handle, u32 lvl,
98 void *context, void **rv)
99{
100 acpi_status status;
101 unsigned long long gsi_base;
102 struct acpi_pci_ioapic *ioapic;
103 struct pci_dev *dev = NULL;
104 struct resource *res = NULL, *pci_res = NULL, *crs_res;
105 char *type = NULL;
106
107 if (!acpi_is_ioapic(handle, &type))
108 return AE_OK;
109
110 mutex_lock(&ioapic_list_lock);
111 list_for_each_entry(ioapic, &ioapic_list, list)
112 if (ioapic->handle == handle) {
113 mutex_unlock(&ioapic_list_lock);
114 return AE_OK;
115 }
116
117 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsi_base);
118 if (ACPI_FAILURE(status)) {
119 acpi_handle_warn(handle, "failed to evaluate _GSB method\n");
120 goto exit;
121 }
122
123 ioapic = kzalloc(sizeof(*ioapic), GFP_KERNEL);
124 if (!ioapic) {
125 pr_err("cannot allocate memory for new IOAPIC\n");
126 goto exit;
127 } else {
128 ioapic->root_handle = (acpi_handle)context;
129 ioapic->handle = handle;
130 ioapic->gsi_base = (u32)gsi_base;
131 INIT_LIST_HEAD(&ioapic->list);
132 }
133
134 if (acpi_ioapic_registered(handle, (u32)gsi_base))
135 goto done;
136
137 dev = acpi_get_pci_dev(handle);
138 if (dev && pci_resource_len(dev, 0)) {
139 if (pci_enable_device(dev) < 0)
140 goto exit_put;
141 pci_set_master(dev);
142 if (pci_request_region(dev, 0, type))
143 goto exit_disable;
144 pci_res = &dev->resource[0];
145 ioapic->pdev = dev;
146 } else {
147 pci_dev_put(dev);
148 dev = NULL;
149 }
150
151 crs_res = &ioapic->res;
152 acpi_walk_resources(handle, METHOD_NAME__CRS, setup_res, crs_res);
153 crs_res->name = type;
154 crs_res->flags |= IORESOURCE_BUSY;
155 if (crs_res->flags == 0) {
156 acpi_handle_warn(handle, "failed to get resource\n");
157 goto exit_release;
158 } else if (insert_resource(&iomem_resource, crs_res)) {
159 acpi_handle_warn(handle, "failed to insert resource\n");
160 goto exit_release;
161 }
162
163 /* try pci resource first, then "_CRS" resource */
164 res = pci_res;
165 if (!res || !res->flags)
166 res = crs_res;
167
168 if (acpi_register_ioapic(handle, res->start, (u32)gsi_base)) {
169 acpi_handle_warn(handle, "failed to register IOAPIC\n");
170 goto exit_release;
171 }
172done:
173 list_add(&ioapic->list, &ioapic_list);
174 mutex_unlock(&ioapic_list_lock);
175
176 if (dev)
177 dev_info(&dev->dev, "%s at %pR, GSI %u\n",
178 type, res, (u32)gsi_base);
179 else
180 acpi_handle_info(handle, "%s at %pR, GSI %u\n",
181 type, res, (u32)gsi_base);
182
183 return AE_OK;
184
185exit_release:
186 if (dev)
187 pci_release_region(dev, 0);
188 if (ioapic->res.flags && ioapic->res.parent)
189 release_resource(&ioapic->res);
190exit_disable:
191 if (dev)
192 pci_disable_device(dev);
193exit_put:
194 pci_dev_put(dev);
195 kfree(ioapic);
196exit:
197 mutex_unlock(&ioapic_list_lock);
198 *(acpi_status *)rv = AE_ERROR;
199 return AE_OK;
200}
201
202int acpi_ioapic_add(acpi_handle root_handle)
203{
204 acpi_status status, retval = AE_OK;
205
206 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, root_handle,
207 UINT_MAX, handle_ioapic_add, NULL,
208 root_handle, (void **)&retval);
209
210 return ACPI_SUCCESS(status) && ACPI_SUCCESS(retval) ? 0 : -ENODEV;
211}
212
213void pci_ioapic_remove(struct acpi_pci_root *root)
214{
215 struct acpi_pci_ioapic *ioapic, *tmp;
216
217 mutex_lock(&ioapic_list_lock);
218 list_for_each_entry_safe(ioapic, tmp, &ioapic_list, list) {
219 if (root->device->handle != ioapic->root_handle)
220 continue;
221 if (ioapic->pdev) {
222 pci_release_region(ioapic->pdev, 0);
223 pci_disable_device(ioapic->pdev);
224 pci_dev_put(ioapic->pdev);
225 }
226 }
227 mutex_unlock(&ioapic_list_lock);
228}
229
230int acpi_ioapic_remove(struct acpi_pci_root *root)
231{
232 int retval = 0;
233 struct acpi_pci_ioapic *ioapic, *tmp;
234
235 mutex_lock(&ioapic_list_lock);
236 list_for_each_entry_safe(ioapic, tmp, &ioapic_list, list) {
237 if (root->device->handle != ioapic->root_handle)
238 continue;
239 if (acpi_unregister_ioapic(ioapic->handle, ioapic->gsi_base))
240 retval = -EBUSY;
241 if (ioapic->res.flags && ioapic->res.parent)
242 release_resource(&ioapic->res);
243 list_del(&ioapic->list);
244 kfree(ioapic);
245 }
246 mutex_unlock(&ioapic_list_lock);
247
248 return retval;
249}
1/*
2 * IOAPIC/IOxAPIC/IOSAPIC driver
3 *
4 * Copyright (C) 2009 Fujitsu Limited.
5 * (c) Copyright 2009 Hewlett-Packard Development Company, L.P.
6 *
7 * Copyright (C) 2014 Intel Corporation
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 version 2 as
11 * published by the Free Software Foundation.
12 *
13 * Based on original drivers/pci/ioapic.c
14 * Yinghai Lu <yinghai@kernel.org>
15 * Jiang Liu <jiang.liu@intel.com>
16 */
17
18/*
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.
22 */
23
24#define pr_fmt(fmt) "ACPI : IOAPIC: " fmt
25
26#include <linux/slab.h>
27#include <linux/acpi.h>
28#include <linux/pci.h>
29#include <acpi/acpi.h>
30
31struct acpi_pci_ioapic {
32 acpi_handle root_handle;
33 acpi_handle handle;
34 u32 gsi_base;
35 struct resource res;
36 struct pci_dev *pdev;
37 struct list_head list;
38};
39
40static LIST_HEAD(ioapic_list);
41static DEFINE_MUTEX(ioapic_list_lock);
42
43static acpi_status setup_res(struct acpi_resource *acpi_res, void *data)
44{
45 struct resource *res = data;
46 struct resource_win win;
47
48 res->flags = 0;
49 if (acpi_dev_filter_resource_type(acpi_res, IORESOURCE_MEM))
50 return AE_OK;
51
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))
55 *res = win.res;
56 }
57 if ((res->flags & IORESOURCE_PREFETCH) ||
58 (res->flags & IORESOURCE_DISABLED))
59 res->flags = 0;
60
61 return AE_CTRL_TERMINATE;
62}
63
64static bool acpi_is_ioapic(acpi_handle handle, char **type)
65{
66 acpi_status status;
67 struct acpi_device_info *info;
68 char *hid = NULL;
69 bool match = false;
70
71 if (!acpi_has_method(handle, "_GSB"))
72 return false;
73
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;
78 if (hid) {
79 if (strcmp(hid, "ACPI0009") == 0) {
80 *type = "IOxAPIC";
81 match = true;
82 } else if (strcmp(hid, "ACPI000A") == 0) {
83 *type = "IOAPIC";
84 match = true;
85 }
86 }
87 kfree(info);
88 }
89
90 return match;
91}
92
93static acpi_status handle_ioapic_add(acpi_handle handle, u32 lvl,
94 void *context, void **rv)
95{
96 acpi_status status;
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;
101 char *type = NULL;
102
103 if (!acpi_is_ioapic(handle, &type))
104 return AE_OK;
105
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);
110 return AE_OK;
111 }
112
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");
116 goto exit;
117 }
118
119 ioapic = kzalloc(sizeof(*ioapic), GFP_KERNEL);
120 if (!ioapic) {
121 pr_err("cannot allocate memory for new IOAPIC\n");
122 goto exit;
123 } else {
124 ioapic->root_handle = (acpi_handle)context;
125 ioapic->handle = handle;
126 ioapic->gsi_base = (u32)gsi_base;
127 INIT_LIST_HEAD(&ioapic->list);
128 }
129
130 if (acpi_ioapic_registered(handle, (u32)gsi_base))
131 goto done;
132
133 dev = acpi_get_pci_dev(handle);
134 if (dev && pci_resource_len(dev, 0)) {
135 if (pci_enable_device(dev) < 0)
136 goto exit_put;
137 pci_set_master(dev);
138 if (pci_request_region(dev, 0, type))
139 goto exit_disable;
140 pci_res = &dev->resource[0];
141 ioapic->pdev = dev;
142 } else {
143 pci_dev_put(dev);
144 dev = NULL;
145 }
146
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");
153 goto exit_release;
154 } else if (insert_resource(&iomem_resource, crs_res)) {
155 acpi_handle_warn(handle, "failed to insert resource\n");
156 goto exit_release;
157 }
158
159 /* try pci resource first, then "_CRS" resource */
160 res = pci_res;
161 if (!res || !res->flags)
162 res = crs_res;
163
164 if (acpi_register_ioapic(handle, res->start, (u32)gsi_base)) {
165 acpi_handle_warn(handle, "failed to register IOAPIC\n");
166 goto exit_release;
167 }
168done:
169 list_add(&ioapic->list, &ioapic_list);
170 mutex_unlock(&ioapic_list_lock);
171
172 if (dev)
173 dev_info(&dev->dev, "%s at %pR, GSI %u\n",
174 type, res, (u32)gsi_base);
175 else
176 acpi_handle_info(handle, "%s at %pR, GSI %u\n",
177 type, res, (u32)gsi_base);
178
179 return AE_OK;
180
181exit_release:
182 if (dev)
183 pci_release_region(dev, 0);
184 if (ioapic->res.flags && ioapic->res.parent)
185 release_resource(&ioapic->res);
186exit_disable:
187 if (dev)
188 pci_disable_device(dev);
189exit_put:
190 pci_dev_put(dev);
191 kfree(ioapic);
192exit:
193 mutex_unlock(&ioapic_list_lock);
194 *(acpi_status *)rv = AE_ERROR;
195 return AE_OK;
196}
197
198int acpi_ioapic_add(acpi_handle root_handle)
199{
200 acpi_status status, retval = AE_OK;
201
202 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, root_handle,
203 UINT_MAX, handle_ioapic_add, NULL,
204 root_handle, (void **)&retval);
205
206 return ACPI_SUCCESS(status) && ACPI_SUCCESS(retval) ? 0 : -ENODEV;
207}
208
209int acpi_ioapic_remove(struct acpi_pci_root *root)
210{
211 int retval = 0;
212 struct acpi_pci_ioapic *ioapic, *tmp;
213
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)
217 continue;
218
219 if (acpi_unregister_ioapic(ioapic->handle, ioapic->gsi_base))
220 retval = -EBUSY;
221
222 if (ioapic->pdev) {
223 pci_release_region(ioapic->pdev, 0);
224 pci_disable_device(ioapic->pdev);
225 pci_dev_put(ioapic->pdev);
226 }
227 if (ioapic->res.flags && ioapic->res.parent)
228 release_resource(&ioapic->res);
229 list_del(&ioapic->list);
230 kfree(ioapic);
231 }
232 mutex_unlock(&ioapic_list_lock);
233
234 return retval;
235}