Merge tag 'module_init-alternate_initcall-v4.1-rc8' of git://git.kernel.org/pub/scm...
[linux-2.6-microblaze.git] / drivers / pnp / system.c
1 /*
2  * system.c - a driver for reserving pnp system resources
3  *
4  * Some code is based on pnpbios_core.c
5  * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
6  * (c) Copyright 2007 Hewlett-Packard Development Company, L.P.
7  *      Bjorn Helgaas <bjorn.helgaas@hp.com>
8  */
9
10 #include <linux/acpi.h>
11 #include <linux/pnp.h>
12 #include <linux/device.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/kernel.h>
16 #include <linux/ioport.h>
17
18 static const struct pnp_device_id pnp_dev_table[] = {
19         /* General ID for reserving resources */
20         {"PNP0c02", 0},
21         /* memory controller */
22         {"PNP0c01", 0},
23         {"", 0}
24 };
25
26 #ifdef CONFIG_ACPI
27 static bool __reserve_range(u64 start, unsigned int length, bool io, char *desc)
28 {
29         u8 space_id = io ? ACPI_ADR_SPACE_SYSTEM_IO : ACPI_ADR_SPACE_SYSTEM_MEMORY;
30         return !acpi_reserve_region(start, length, space_id, IORESOURCE_BUSY, desc);
31 }
32 #else
33 static bool __reserve_range(u64 start, unsigned int length, bool io, char *desc)
34 {
35         struct resource *res;
36
37         res = io ? request_region(start, length, desc) :
38                 request_mem_region(start, length, desc);
39         if (res) {
40                 res->flags &= ~IORESOURCE_BUSY;
41                 return true;
42         }
43         return false;
44 }
45 #endif
46
47 static void reserve_range(struct pnp_dev *dev, struct resource *r, int port)
48 {
49         char *regionid;
50         const char *pnpid = dev_name(&dev->dev);
51         resource_size_t start = r->start, end = r->end;
52         bool reserved;
53
54         regionid = kmalloc(16, GFP_KERNEL);
55         if (!regionid)
56                 return;
57
58         snprintf(regionid, 16, "pnp %s", pnpid);
59         reserved = __reserve_range(start, end - start + 1, !!port, regionid);
60         if (!reserved)
61                 kfree(regionid);
62
63         /*
64          * Failures at this point are usually harmless. pci quirks for
65          * example do reserve stuff they know about too, so we may well
66          * have double reservations.
67          */
68         dev_info(&dev->dev, "%pR %s reserved\n", r,
69                  reserved ? "has been" : "could not be");
70 }
71
72 static void reserve_resources_of_dev(struct pnp_dev *dev)
73 {
74         struct resource *res;
75         int i;
76
77         for (i = 0; (res = pnp_get_resource(dev, IORESOURCE_IO, i)); i++) {
78                 if (res->flags & IORESOURCE_DISABLED)
79                         continue;
80                 if (res->start == 0)
81                         continue;       /* disabled */
82                 if (res->start < 0x100)
83                         /*
84                          * Below 0x100 is only standard PC hardware
85                          * (pics, kbd, timer, dma, ...)
86                          * We should not get resource conflicts there,
87                          * and the kernel reserves these anyway
88                          * (see arch/i386/kernel/setup.c).
89                          * So, do nothing
90                          */
91                         continue;
92                 if (res->end < res->start)
93                         continue;       /* invalid */
94
95                 reserve_range(dev, res, 1);
96         }
97
98         for (i = 0; (res = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) {
99                 if (res->flags & IORESOURCE_DISABLED)
100                         continue;
101
102                 reserve_range(dev, res, 0);
103         }
104 }
105
106 static int system_pnp_probe(struct pnp_dev *dev,
107                             const struct pnp_device_id *dev_id)
108 {
109         reserve_resources_of_dev(dev);
110         return 0;
111 }
112
113 static struct pnp_driver system_pnp_driver = {
114         .name     = "system",
115         .id_table = pnp_dev_table,
116         .flags    = PNP_DRIVER_RES_DO_NOT_CHANGE,
117         .probe    = system_pnp_probe,
118 };
119
120 static int __init pnp_system_init(void)
121 {
122         return pnp_register_driver(&system_pnp_driver);
123 }
124
125 /**
126  * Reserve motherboard resources after PCI claim BARs,
127  * but before PCI assign resources for uninitialized PCI devices
128  */
129 fs_initcall(pnp_system_init);