]> Pileus Git - ~andy/linux/blob - drivers/acpi/pci_root.c
Merge branch 'pci/yinghai-root-bus-hotplug' into next
[~andy/linux] / drivers / acpi / pci_root.c
1 /*
2  *  pci_root.c - ACPI PCI Root Bridge Driver ($Revision: 40 $)
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *
7  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 as published by
11  *  the Free Software Foundation; either version 2 of the License, or (at
12  *  your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22  *
23  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/mutex.h>
31 #include <linux/pm.h>
32 #include <linux/pm_runtime.h>
33 #include <linux/pci.h>
34 #include <linux/pci-acpi.h>
35 #include <linux/pci-aspm.h>
36 #include <linux/acpi.h>
37 #include <linux/slab.h>
38 #include <acpi/acpi_bus.h>
39 #include <acpi/acpi_drivers.h>
40 #include <acpi/apei.h>
41
42 #define PREFIX "ACPI: "
43
44 #define _COMPONENT              ACPI_PCI_COMPONENT
45 ACPI_MODULE_NAME("pci_root");
46 #define ACPI_PCI_ROOT_CLASS             "pci_bridge"
47 #define ACPI_PCI_ROOT_DEVICE_NAME       "PCI Root Bridge"
48 static int acpi_pci_root_add(struct acpi_device *device);
49 static int acpi_pci_root_remove(struct acpi_device *device, int type);
50
51 #define ACPI_PCIE_REQ_SUPPORT (OSC_EXT_PCI_CONFIG_SUPPORT \
52                                 | OSC_ACTIVE_STATE_PWR_SUPPORT \
53                                 | OSC_CLOCK_PWR_CAPABILITY_SUPPORT \
54                                 | OSC_MSI_SUPPORT)
55
56 static const struct acpi_device_id root_device_ids[] = {
57         {"PNP0A03", 0},
58         {"", 0},
59 };
60 MODULE_DEVICE_TABLE(acpi, root_device_ids);
61
62 static struct acpi_driver acpi_pci_root_driver = {
63         .name = "pci_root",
64         .class = ACPI_PCI_ROOT_CLASS,
65         .ids = root_device_ids,
66         .ops = {
67                 .add = acpi_pci_root_add,
68                 .remove = acpi_pci_root_remove,
69                 },
70 };
71
72 /* Lock to protect both acpi_pci_roots and acpi_pci_drivers lists */
73 static DEFINE_MUTEX(acpi_pci_root_lock);
74 static LIST_HEAD(acpi_pci_roots);
75 static LIST_HEAD(acpi_pci_drivers);
76
77 static DEFINE_MUTEX(osc_lock);
78
79 int acpi_pci_register_driver(struct acpi_pci_driver *driver)
80 {
81         int n = 0;
82         struct acpi_pci_root *root;
83
84         mutex_lock(&acpi_pci_root_lock);
85         list_add_tail(&driver->node, &acpi_pci_drivers);
86         if (driver->add)
87                 list_for_each_entry(root, &acpi_pci_roots, node) {
88                         driver->add(root);
89                         n++;
90                 }
91         mutex_unlock(&acpi_pci_root_lock);
92
93         return n;
94 }
95 EXPORT_SYMBOL(acpi_pci_register_driver);
96
97 void acpi_pci_unregister_driver(struct acpi_pci_driver *driver)
98 {
99         struct acpi_pci_root *root;
100
101         mutex_lock(&acpi_pci_root_lock);
102         list_del(&driver->node);
103         if (driver->remove)
104                 list_for_each_entry(root, &acpi_pci_roots, node)
105                         driver->remove(root);
106         mutex_unlock(&acpi_pci_root_lock);
107 }
108 EXPORT_SYMBOL(acpi_pci_unregister_driver);
109
110 /**
111  * acpi_is_root_bridge - determine whether an ACPI CA node is a PCI root bridge
112  * @handle - the ACPI CA node in question.
113  *
114  * Note: we could make this API take a struct acpi_device * instead, but
115  * for now, it's more convenient to operate on an acpi_handle.
116  */
117 int acpi_is_root_bridge(acpi_handle handle)
118 {
119         int ret;
120         struct acpi_device *device;
121
122         ret = acpi_bus_get_device(handle, &device);
123         if (ret)
124                 return 0;
125
126         ret = acpi_match_device_ids(device, root_device_ids);
127         if (ret)
128                 return 0;
129         else
130                 return 1;
131 }
132 EXPORT_SYMBOL_GPL(acpi_is_root_bridge);
133
134 static acpi_status
135 get_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
136 {
137         struct resource *res = data;
138         struct acpi_resource_address64 address;
139
140         if (resource->type != ACPI_RESOURCE_TYPE_ADDRESS16 &&
141             resource->type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
142             resource->type != ACPI_RESOURCE_TYPE_ADDRESS64)
143                 return AE_OK;
144
145         acpi_resource_to_address64(resource, &address);
146         if ((address.address_length > 0) &&
147             (address.resource_type == ACPI_BUS_NUMBER_RANGE)) {
148                 res->start = address.minimum;
149                 res->end = address.minimum + address.address_length - 1;
150         }
151
152         return AE_OK;
153 }
154
155 static acpi_status try_get_root_bridge_busnr(acpi_handle handle,
156                                              struct resource *res)
157 {
158         acpi_status status;
159
160         res->start = -1;
161         status =
162             acpi_walk_resources(handle, METHOD_NAME__CRS,
163                                 get_root_bridge_busnr_callback, res);
164         if (ACPI_FAILURE(status))
165                 return status;
166         if (res->start == -1)
167                 return AE_ERROR;
168         return AE_OK;
169 }
170
171 static u8 pci_osc_uuid_str[] = "33DB4D5B-1FF7-401C-9657-7441C03DD766";
172
173 static acpi_status acpi_pci_run_osc(acpi_handle handle,
174                                     const u32 *capbuf, u32 *retval)
175 {
176         struct acpi_osc_context context = {
177                 .uuid_str = pci_osc_uuid_str,
178                 .rev = 1,
179                 .cap.length = 12,
180                 .cap.pointer = (void *)capbuf,
181         };
182         acpi_status status;
183
184         status = acpi_run_osc(handle, &context);
185         if (ACPI_SUCCESS(status)) {
186                 *retval = *((u32 *)(context.ret.pointer + 8));
187                 kfree(context.ret.pointer);
188         }
189         return status;
190 }
191
192 static acpi_status acpi_pci_query_osc(struct acpi_pci_root *root,
193                                         u32 support,
194                                         u32 *control)
195 {
196         acpi_status status;
197         u32 result, capbuf[3];
198
199         support &= OSC_PCI_SUPPORT_MASKS;
200         support |= root->osc_support_set;
201
202         capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
203         capbuf[OSC_SUPPORT_TYPE] = support;
204         if (control) {
205                 *control &= OSC_PCI_CONTROL_MASKS;
206                 capbuf[OSC_CONTROL_TYPE] = *control | root->osc_control_set;
207         } else {
208                 /* Run _OSC query for all possible controls. */
209                 capbuf[OSC_CONTROL_TYPE] = OSC_PCI_CONTROL_MASKS;
210         }
211
212         status = acpi_pci_run_osc(root->device->handle, capbuf, &result);
213         if (ACPI_SUCCESS(status)) {
214                 root->osc_support_set = support;
215                 if (control)
216                         *control = result;
217         }
218         return status;
219 }
220
221 static acpi_status acpi_pci_osc_support(struct acpi_pci_root *root, u32 flags)
222 {
223         acpi_status status;
224         acpi_handle tmp;
225
226         status = acpi_get_handle(root->device->handle, "_OSC", &tmp);
227         if (ACPI_FAILURE(status))
228                 return status;
229         mutex_lock(&osc_lock);
230         status = acpi_pci_query_osc(root, flags, NULL);
231         mutex_unlock(&osc_lock);
232         return status;
233 }
234
235 struct acpi_pci_root *acpi_pci_find_root(acpi_handle handle)
236 {
237         struct acpi_pci_root *root;
238         struct acpi_device *device;
239
240         if (acpi_bus_get_device(handle, &device) ||
241             acpi_match_device_ids(device, root_device_ids))
242                 return NULL;
243
244         root = acpi_driver_data(device);
245
246         return root;
247 }
248 EXPORT_SYMBOL_GPL(acpi_pci_find_root);
249
250 struct acpi_handle_node {
251         struct list_head node;
252         acpi_handle handle;
253 };
254
255 /**
256  * acpi_get_pci_dev - convert ACPI CA handle to struct pci_dev
257  * @handle: the handle in question
258  *
259  * Given an ACPI CA handle, the desired PCI device is located in the
260  * list of PCI devices.
261  *
262  * If the device is found, its reference count is increased and this
263  * function returns a pointer to its data structure.  The caller must
264  * decrement the reference count by calling pci_dev_put().
265  * If no device is found, %NULL is returned.
266  */
267 struct pci_dev *acpi_get_pci_dev(acpi_handle handle)
268 {
269         int dev, fn;
270         unsigned long long adr;
271         acpi_status status;
272         acpi_handle phandle;
273         struct pci_bus *pbus;
274         struct pci_dev *pdev = NULL;
275         struct acpi_handle_node *node, *tmp;
276         struct acpi_pci_root *root;
277         LIST_HEAD(device_list);
278
279         /*
280          * Walk up the ACPI CA namespace until we reach a PCI root bridge.
281          */
282         phandle = handle;
283         while (!acpi_is_root_bridge(phandle)) {
284                 node = kzalloc(sizeof(struct acpi_handle_node), GFP_KERNEL);
285                 if (!node)
286                         goto out;
287
288                 INIT_LIST_HEAD(&node->node);
289                 node->handle = phandle;
290                 list_add(&node->node, &device_list);
291
292                 status = acpi_get_parent(phandle, &phandle);
293                 if (ACPI_FAILURE(status))
294                         goto out;
295         }
296
297         root = acpi_pci_find_root(phandle);
298         if (!root)
299                 goto out;
300
301         pbus = root->bus;
302
303         /*
304          * Now, walk back down the PCI device tree until we return to our
305          * original handle. Assumes that everything between the PCI root
306          * bridge and the device we're looking for must be a P2P bridge.
307          */
308         list_for_each_entry(node, &device_list, node) {
309                 acpi_handle hnd = node->handle;
310                 status = acpi_evaluate_integer(hnd, "_ADR", NULL, &adr);
311                 if (ACPI_FAILURE(status))
312                         goto out;
313                 dev = (adr >> 16) & 0xffff;
314                 fn  = adr & 0xffff;
315
316                 pdev = pci_get_slot(pbus, PCI_DEVFN(dev, fn));
317                 if (!pdev || hnd == handle)
318                         break;
319
320                 pbus = pdev->subordinate;
321                 pci_dev_put(pdev);
322
323                 /*
324                  * This function may be called for a non-PCI device that has a
325                  * PCI parent (eg. a disk under a PCI SATA controller).  In that
326                  * case pdev->subordinate will be NULL for the parent.
327                  */
328                 if (!pbus) {
329                         dev_dbg(&pdev->dev, "Not a PCI-to-PCI bridge\n");
330                         pdev = NULL;
331                         break;
332                 }
333         }
334 out:
335         list_for_each_entry_safe(node, tmp, &device_list, node)
336                 kfree(node);
337
338         return pdev;
339 }
340 EXPORT_SYMBOL_GPL(acpi_get_pci_dev);
341
342 /**
343  * acpi_pci_osc_control_set - Request control of PCI root _OSC features.
344  * @handle: ACPI handle of a PCI root bridge (or PCIe Root Complex).
345  * @mask: Mask of _OSC bits to request control of, place to store control mask.
346  * @req: Mask of _OSC bits the control of is essential to the caller.
347  *
348  * Run _OSC query for @mask and if that is successful, compare the returned
349  * mask of control bits with @req.  If all of the @req bits are set in the
350  * returned mask, run _OSC request for it.
351  *
352  * The variable at the @mask address may be modified regardless of whether or
353  * not the function returns success.  On success it will contain the mask of
354  * _OSC bits the BIOS has granted control of, but its contents are meaningless
355  * on failure.
356  **/
357 acpi_status acpi_pci_osc_control_set(acpi_handle handle, u32 *mask, u32 req)
358 {
359         struct acpi_pci_root *root;
360         acpi_status status;
361         u32 ctrl, capbuf[3];
362         acpi_handle tmp;
363
364         if (!mask)
365                 return AE_BAD_PARAMETER;
366
367         ctrl = *mask & OSC_PCI_CONTROL_MASKS;
368         if ((ctrl & req) != req)
369                 return AE_TYPE;
370
371         root = acpi_pci_find_root(handle);
372         if (!root)
373                 return AE_NOT_EXIST;
374
375         status = acpi_get_handle(handle, "_OSC", &tmp);
376         if (ACPI_FAILURE(status))
377                 return status;
378
379         mutex_lock(&osc_lock);
380
381         *mask = ctrl | root->osc_control_set;
382         /* No need to evaluate _OSC if the control was already granted. */
383         if ((root->osc_control_set & ctrl) == ctrl)
384                 goto out;
385
386         /* Need to check the available controls bits before requesting them. */
387         while (*mask) {
388                 status = acpi_pci_query_osc(root, root->osc_support_set, mask);
389                 if (ACPI_FAILURE(status))
390                         goto out;
391                 if (ctrl == *mask)
392                         break;
393                 ctrl = *mask;
394         }
395
396         if ((ctrl & req) != req) {
397                 status = AE_SUPPORT;
398                 goto out;
399         }
400
401         capbuf[OSC_QUERY_TYPE] = 0;
402         capbuf[OSC_SUPPORT_TYPE] = root->osc_support_set;
403         capbuf[OSC_CONTROL_TYPE] = ctrl;
404         status = acpi_pci_run_osc(handle, capbuf, mask);
405         if (ACPI_SUCCESS(status))
406                 root->osc_control_set = *mask;
407 out:
408         mutex_unlock(&osc_lock);
409         return status;
410 }
411 EXPORT_SYMBOL(acpi_pci_osc_control_set);
412
413 static int acpi_pci_root_add(struct acpi_device *device)
414 {
415         unsigned long long segment, bus;
416         acpi_status status;
417         int result;
418         struct acpi_pci_root *root;
419         struct acpi_pci_driver *driver;
420         u32 flags, base_flags;
421         bool is_osc_granted = false;
422
423         root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
424         if (!root)
425                 return -ENOMEM;
426
427         segment = 0;
428         status = acpi_evaluate_integer(device->handle, METHOD_NAME__SEG, NULL,
429                                        &segment);
430         if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
431                 printk(KERN_ERR PREFIX "can't evaluate _SEG\n");
432                 result = -ENODEV;
433                 goto end;
434         }
435
436         /* Check _CRS first, then _BBN.  If no _BBN, default to zero. */
437         root->secondary.flags = IORESOURCE_BUS;
438         status = try_get_root_bridge_busnr(device->handle, &root->secondary);
439         if (ACPI_FAILURE(status)) {
440                 /*
441                  * We need both the start and end of the downstream bus range
442                  * to interpret _CBA (MMCONFIG base address), so it really is
443                  * supposed to be in _CRS.  If we don't find it there, all we
444                  * can do is assume [_BBN-0xFF] or [0-0xFF].
445                  */
446                 root->secondary.end = 0xFF;
447                 printk(KERN_WARNING FW_BUG PREFIX
448                        "no secondary bus range in _CRS\n");
449                 status = acpi_evaluate_integer(device->handle, METHOD_NAME__BBN,
450                                                NULL, &bus);
451                 if (ACPI_SUCCESS(status))
452                         root->secondary.start = bus;
453                 else if (status == AE_NOT_FOUND)
454                         root->secondary.start = 0;
455                 else {
456                         printk(KERN_ERR PREFIX "can't evaluate _BBN\n");
457                         result = -ENODEV;
458                         goto end;
459                 }
460         }
461
462         INIT_LIST_HEAD(&root->node);
463         root->device = device;
464         root->segment = segment & 0xFFFF;
465         strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
466         strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
467         device->driver_data = root;
468
469         printk(KERN_INFO PREFIX "%s [%s] (domain %04x %pR)\n",
470                acpi_device_name(device), acpi_device_bid(device),
471                root->segment, &root->secondary);
472
473         root->mcfg_addr = acpi_pci_root_get_mcfg_addr(device->handle);
474
475         /*
476          * All supported architectures that use ACPI have support for
477          * PCI domains, so we indicate this in _OSC support capabilities.
478          */
479         flags = base_flags = OSC_PCI_SEGMENT_GROUPS_SUPPORT;
480         acpi_pci_osc_support(root, flags);
481
482         /* Indicate support for various _OSC capabilities. */
483         if (pci_ext_cfg_avail())
484                 flags |= OSC_EXT_PCI_CONFIG_SUPPORT;
485         if (pcie_aspm_support_enabled()) {
486                 flags |= OSC_ACTIVE_STATE_PWR_SUPPORT |
487                 OSC_CLOCK_PWR_CAPABILITY_SUPPORT;
488         }
489         if (pci_msi_enabled())
490                 flags |= OSC_MSI_SUPPORT;
491         if (flags != base_flags) {
492                 status = acpi_pci_osc_support(root, flags);
493                 if (ACPI_FAILURE(status)) {
494                         dev_info(&device->dev, "ACPI _OSC support "
495                                 "notification failed, disabling PCIe ASPM\n");
496                         pcie_no_aspm();
497                         flags = base_flags;
498                 }
499         }
500         if (!pcie_ports_disabled
501             && (flags & ACPI_PCIE_REQ_SUPPORT) == ACPI_PCIE_REQ_SUPPORT) {
502                 flags = OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL
503                         | OSC_PCI_EXPRESS_NATIVE_HP_CONTROL
504                         | OSC_PCI_EXPRESS_PME_CONTROL;
505
506                 if (pci_aer_available()) {
507                         if (aer_acpi_firmware_first())
508                                 dev_dbg(&device->dev,
509                                         "PCIe errors handled by BIOS.\n");
510                         else
511                                 flags |= OSC_PCI_EXPRESS_AER_CONTROL;
512                 }
513
514                 dev_info(&device->dev,
515                         "Requesting ACPI _OSC control (0x%02x)\n", flags);
516
517                 status = acpi_pci_osc_control_set(device->handle, &flags,
518                                        OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL);
519                 if (ACPI_SUCCESS(status)) {
520                         is_osc_granted = true;
521                         dev_info(&device->dev,
522                                 "ACPI _OSC control (0x%02x) granted\n", flags);
523                 } else {
524                         is_osc_granted = false;
525                         dev_info(&device->dev,
526                                 "ACPI _OSC request failed (%s), "
527                                 "returned control mask: 0x%02x\n",
528                                 acpi_format_exception(status), flags);
529                 }
530         } else {
531                 dev_info(&device->dev,
532                         "Unable to request _OSC control "
533                         "(_OSC support mask: 0x%02x)\n", flags);
534         }
535
536         /*
537          * TBD: Need PCI interface for enumeration/configuration of roots.
538          */
539
540         mutex_lock(&acpi_pci_root_lock);
541         list_add_tail(&root->node, &acpi_pci_roots);
542         mutex_unlock(&acpi_pci_root_lock);
543
544         /*
545          * Scan the Root Bridge
546          * --------------------
547          * Must do this prior to any attempt to bind the root device, as the
548          * PCI namespace does not get created until this call is made (and 
549          * thus the root bridge's pci_dev does not exist).
550          */
551         root->bus = pci_acpi_scan_root(root);
552         if (!root->bus) {
553                 printk(KERN_ERR PREFIX
554                             "Bus %04x:%02x not present in PCI namespace\n",
555                             root->segment, (unsigned int)root->secondary.start);
556                 result = -ENODEV;
557                 goto out_del_root;
558         }
559
560         /* ASPM setting */
561         if (is_osc_granted) {
562                 if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_ASPM)
563                         pcie_clear_aspm(root->bus);
564         } else {
565                 pr_info("ACPI _OSC control for PCIe not granted, "
566                         "disabling ASPM\n");
567                 pcie_no_aspm();
568         }
569
570         pci_acpi_add_bus_pm_notifier(device, root->bus);
571         if (device->wakeup.flags.run_wake)
572                 device_set_run_wake(root->bus->bridge, true);
573
574         if (system_state != SYSTEM_BOOTING) {
575                 pcibios_resource_survey_bus(root->bus);
576                 pci_assign_unassigned_bus_resources(root->bus);
577         }
578
579         mutex_lock(&acpi_pci_root_lock);
580         list_for_each_entry(driver, &acpi_pci_drivers, node)
581                 if (driver->add)
582                         driver->add(root);
583         mutex_unlock(&acpi_pci_root_lock);
584
585         /* need to after hot-added ioapic is registered */
586         if (system_state != SYSTEM_BOOTING)
587                 pci_enable_bridges(root->bus);
588
589         pci_bus_add_devices(root->bus);
590         return 0;
591
592 out_del_root:
593         mutex_lock(&acpi_pci_root_lock);
594         list_del(&root->node);
595         mutex_unlock(&acpi_pci_root_lock);
596
597 end:
598         kfree(root);
599         return result;
600 }
601
602 static int acpi_pci_root_remove(struct acpi_device *device, int type)
603 {
604         struct acpi_pci_root *root = acpi_driver_data(device);
605         struct acpi_pci_driver *driver;
606
607         pci_stop_root_bus(root->bus);
608
609         mutex_lock(&acpi_pci_root_lock);
610         list_for_each_entry_reverse(driver, &acpi_pci_drivers, node)
611                 if (driver->remove)
612                         driver->remove(root);
613         mutex_unlock(&acpi_pci_root_lock);
614
615         device_set_run_wake(root->bus->bridge, false);
616         pci_acpi_remove_bus_pm_notifier(device);
617
618         pci_remove_root_bus(root->bus);
619
620         mutex_lock(&acpi_pci_root_lock);
621         list_del(&root->node);
622         mutex_unlock(&acpi_pci_root_lock);
623         kfree(root);
624         return 0;
625 }
626
627 int __init acpi_pci_root_init(void)
628 {
629         acpi_hest_init();
630
631         if (acpi_pci_disabled)
632                 return 0;
633
634         pci_acpi_crs_quirks();
635         if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
636                 return -ENODEV;
637
638         return 0;
639 }
640 /* Support root bridge hotplug */
641
642 static void handle_root_bridge_insertion(acpi_handle handle)
643 {
644         struct acpi_device *device;
645
646         if (!acpi_bus_get_device(handle, &device)) {
647                 printk(KERN_DEBUG "acpi device exists...\n");
648                 return;
649         }
650
651         if (acpi_bus_scan(handle))
652                 printk(KERN_ERR "cannot add bridge to acpi list\n");
653 }
654
655 static void handle_root_bridge_removal(struct acpi_device *device)
656 {
657         struct acpi_eject_event *ej_event;
658
659         ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
660         if (!ej_event) {
661                 /* Inform firmware the hot-remove operation has error */
662                 (void) acpi_evaluate_hotplug_ost(device->handle,
663                                         ACPI_NOTIFY_EJECT_REQUEST,
664                                         ACPI_OST_SC_NON_SPECIFIC_FAILURE,
665                                         NULL);
666                 return;
667         }
668
669         ej_event->device = device;
670         ej_event->event = ACPI_NOTIFY_EJECT_REQUEST;
671
672         acpi_bus_hot_remove_device(ej_event);
673 }
674
675 static void _handle_hotplug_event_root(struct work_struct *work)
676 {
677         struct acpi_pci_root *root;
678         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER };
679         struct acpi_hp_work *hp_work;
680         acpi_handle handle;
681         u32 type;
682
683         hp_work = container_of(work, struct acpi_hp_work, work);
684         handle = hp_work->handle;
685         type = hp_work->type;
686
687         root = acpi_pci_find_root(handle);
688
689         acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
690
691         switch (type) {
692         case ACPI_NOTIFY_BUS_CHECK:
693                 /* bus enumerate */
694                 printk(KERN_DEBUG "%s: Bus check notify on %s\n", __func__,
695                                  (char *)buffer.pointer);
696                 if (!root)
697                         handle_root_bridge_insertion(handle);
698
699                 break;
700
701         case ACPI_NOTIFY_DEVICE_CHECK:
702                 /* device check */
703                 printk(KERN_DEBUG "%s: Device check notify on %s\n", __func__,
704                                  (char *)buffer.pointer);
705                 if (!root)
706                         handle_root_bridge_insertion(handle);
707                 break;
708
709         case ACPI_NOTIFY_EJECT_REQUEST:
710                 /* request device eject */
711                 printk(KERN_DEBUG "%s: Device eject notify on %s\n", __func__,
712                                  (char *)buffer.pointer);
713                 if (root)
714                         handle_root_bridge_removal(root->device);
715                 break;
716         default:
717                 printk(KERN_WARNING "notify_handler: unknown event type 0x%x for %s\n",
718                                  type, (char *)buffer.pointer);
719                 break;
720         }
721
722         kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
723         kfree(buffer.pointer);
724 }
725
726 static void handle_hotplug_event_root(acpi_handle handle, u32 type,
727                                         void *context)
728 {
729         alloc_acpi_hp_work(handle, type, context,
730                                 _handle_hotplug_event_root);
731 }
732
733 static acpi_status __init
734 find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
735 {
736         acpi_status status;
737         char objname[64];
738         struct acpi_buffer buffer = { .length = sizeof(objname),
739                                       .pointer = objname };
740         int *count = (int *)context;
741
742         if (!acpi_is_root_bridge(handle))
743                 return AE_OK;
744
745         (*count)++;
746
747         acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
748
749         status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
750                                         handle_hotplug_event_root, NULL);
751         if (ACPI_FAILURE(status))
752                 printk(KERN_DEBUG "acpi root: %s notify handler is not installed, exit status: %u\n",
753                                   objname, (unsigned int)status);
754         else
755                 printk(KERN_DEBUG "acpi root: %s notify handler is installed\n",
756                                  objname);
757
758         return AE_OK;
759 }
760
761 void __init acpi_pci_root_hp_init(void)
762 {
763         int num = 0;
764
765         acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
766                 ACPI_UINT32_MAX, find_root_bridges, NULL, &num, NULL);
767
768         printk(KERN_DEBUG "Found %d acpi root devices\n", num);
769 }