GNU Linux-libre 4.4.285-gnu1
[releases.git] / drivers / acpi / ioapic.c
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
31 struct 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
40 static LIST_HEAD(ioapic_list);
41 static DEFINE_MUTEX(ioapic_list_lock);
42
43 static acpi_status setup_res(struct acpi_resource *acpi_res, void *data)
44 {
45         struct resource *res = data;
46         struct resource_win win;
47
48         /*
49          * We might assign this to 'res' later, make sure all pointers are
50          * cleared before the resource is added to the global list
51          */
52         memset(&win, 0, sizeof(win));
53
54         res->flags = 0;
55         if (acpi_dev_filter_resource_type(acpi_res, IORESOURCE_MEM) == 0)
56                 return AE_OK;
57
58         if (!acpi_dev_resource_memory(acpi_res, res)) {
59                 if (acpi_dev_resource_address_space(acpi_res, &win) ||
60                     acpi_dev_resource_ext_address_space(acpi_res, &win))
61                         *res = win.res;
62         }
63         if ((res->flags & IORESOURCE_PREFETCH) ||
64             (res->flags & IORESOURCE_DISABLED))
65                 res->flags = 0;
66
67         return AE_CTRL_TERMINATE;
68 }
69
70 static bool acpi_is_ioapic(acpi_handle handle, char **type)
71 {
72         acpi_status status;
73         struct acpi_device_info *info;
74         char *hid = NULL;
75         bool match = false;
76
77         if (!acpi_has_method(handle, "_GSB"))
78                 return false;
79
80         status = acpi_get_object_info(handle, &info);
81         if (ACPI_SUCCESS(status)) {
82                 if (info->valid & ACPI_VALID_HID)
83                         hid = info->hardware_id.string;
84                 if (hid) {
85                         if (strcmp(hid, "ACPI0009") == 0) {
86                                 *type = "IOxAPIC";
87                                 match = true;
88                         } else if (strcmp(hid, "ACPI000A") == 0) {
89                                 *type = "IOAPIC";
90                                 match = true;
91                         }
92                 }
93                 kfree(info);
94         }
95
96         return match;
97 }
98
99 static acpi_status handle_ioapic_add(acpi_handle handle, u32 lvl,
100                                      void *context, void **rv)
101 {
102         acpi_status status;
103         unsigned long long gsi_base;
104         struct acpi_pci_ioapic *ioapic;
105         struct pci_dev *dev = NULL;
106         struct resource *res = NULL;
107         char *type = NULL;
108
109         if (!acpi_is_ioapic(handle, &type))
110                 return AE_OK;
111
112         mutex_lock(&ioapic_list_lock);
113         list_for_each_entry(ioapic, &ioapic_list, list)
114                 if (ioapic->handle == handle) {
115                         mutex_unlock(&ioapic_list_lock);
116                         return AE_OK;
117                 }
118
119         status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsi_base);
120         if (ACPI_FAILURE(status)) {
121                 acpi_handle_warn(handle, "failed to evaluate _GSB method\n");
122                 goto exit;
123         }
124
125         ioapic = kzalloc(sizeof(*ioapic), GFP_KERNEL);
126         if (!ioapic) {
127                 pr_err("cannot allocate memory for new IOAPIC\n");
128                 goto exit;
129         } else {
130                 ioapic->root_handle = (acpi_handle)context;
131                 ioapic->handle = handle;
132                 ioapic->gsi_base = (u32)gsi_base;
133                 INIT_LIST_HEAD(&ioapic->list);
134         }
135
136         if (acpi_ioapic_registered(handle, (u32)gsi_base))
137                 goto done;
138
139         dev = acpi_get_pci_dev(handle);
140         if (dev && pci_resource_len(dev, 0)) {
141                 if (pci_enable_device(dev) < 0)
142                         goto exit_put;
143                 pci_set_master(dev);
144                 if (pci_request_region(dev, 0, type))
145                         goto exit_disable;
146                 res = &dev->resource[0];
147                 ioapic->pdev = dev;
148         } else {
149                 pci_dev_put(dev);
150                 dev = NULL;
151
152                 res = &ioapic->res;
153                 acpi_walk_resources(handle, METHOD_NAME__CRS, setup_res, res);
154                 if (res->flags == 0) {
155                         acpi_handle_warn(handle, "failed to get resource\n");
156                         goto exit_free;
157                 } else if (request_resource(&iomem_resource, res)) {
158                         acpi_handle_warn(handle, "failed to insert resource\n");
159                         goto exit_free;
160                 }
161         }
162
163         if (acpi_register_ioapic(handle, res->start, (u32)gsi_base)) {
164                 acpi_handle_warn(handle, "failed to register IOAPIC\n");
165                 goto exit_release;
166         }
167 done:
168         list_add(&ioapic->list, &ioapic_list);
169         mutex_unlock(&ioapic_list_lock);
170
171         if (dev)
172                 dev_info(&dev->dev, "%s at %pR, GSI %u\n",
173                          type, res, (u32)gsi_base);
174         else
175                 acpi_handle_info(handle, "%s at %pR, GSI %u\n",
176                                  type, res, (u32)gsi_base);
177
178         return AE_OK;
179
180 exit_release:
181         if (dev)
182                 pci_release_region(dev, 0);
183         else
184                 release_resource(res);
185 exit_disable:
186         if (dev)
187                 pci_disable_device(dev);
188 exit_put:
189         pci_dev_put(dev);
190 exit_free:
191         kfree(ioapic);
192 exit:
193         mutex_unlock(&ioapic_list_lock);
194         *(acpi_status *)rv = AE_ERROR;
195         return AE_OK;
196 }
197
198 int acpi_ioapic_add(struct acpi_pci_root *root)
199 {
200         acpi_status status, retval = AE_OK;
201
202         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, root->device->handle,
203                                      UINT_MAX, handle_ioapic_add, NULL,
204                                      root->device->handle, (void **)&retval);
205
206         return ACPI_SUCCESS(status) && ACPI_SUCCESS(retval) ? 0 : -ENODEV;
207 }
208
209 int 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                 } else if (ioapic->res.flags && ioapic->res.parent) {
227                         release_resource(&ioapic->res);
228                 }
229                 list_del(&ioapic->list);
230                 kfree(ioapic);
231         }
232         mutex_unlock(&ioapic_list_lock);
233
234         return retval;
235 }