blob: 49c1720df59a8550cdca31735e6ef519f6b88266 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Bjorn Helgaasa8c78f72007-01-18 16:43:27 -07006 * (c) Copyright 2007 Hewlett-Packard Development Company, L.P.
7 * Bjorn Helgaas <bjorn.helgaas@hp.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10#include <linux/pnp.h>
11#include <linux/device.h>
12#include <linux/init.h>
13#include <linux/slab.h>
14#include <linux/kernel.h>
15#include <linux/ioport.h>
16
17static const struct pnp_device_id pnp_dev_table[] = {
18 /* General ID for reserving resources */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070019 {"PNP0c02", 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 /* memory controller */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070021 {"PNP0c01", 0},
22 {"", 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -070023};
24
Bjorn Helgaas9a007b32009-10-06 15:34:00 -060025static void reserve_range(struct pnp_dev *dev, struct resource *r, int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 char *regionid;
Kay Sieversc85e37c2009-01-06 10:44:38 -080028 const char *pnpid = dev_name(&dev->dev);
Bjorn Helgaas9a007b32009-10-06 15:34:00 -060029 resource_size_t start = r->start, end = r->end;
Rafael J. Wysocki02941122015-07-04 03:09:03 +020030 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32 regionid = kmalloc(16, GFP_KERNEL);
Bjorn Helgaasb933e192007-10-16 23:31:11 -070033 if (!regionid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 return;
Bjorn Helgaasb933e192007-10-16 23:31:11 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 snprintf(regionid, 16, "pnp %s", pnpid);
Rafael J. Wysocki02941122015-07-04 03:09:03 +020037 if (port)
38 res = request_region(start, end - start + 1, regionid);
39 else
40 res = request_mem_region(start, end - start + 1, regionid);
41 if (res)
42 res->flags &= ~IORESOURCE_BUSY;
43 else
Bjorn Helgaasb933e192007-10-16 23:31:11 -070044 kfree(regionid);
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 /*
47 * Failures at this point are usually harmless. pci quirks for
48 * example do reserve stuff they know about too, so we may well
49 * have double reservations.
50 */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -060051 dev_info(&dev->dev, "%pR %s reserved\n", r,
Rafael J. Wysocki02941122015-07-04 03:09:03 +020052 res ? "has been" : "could not be");
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
Bjorn Helgaasb933e192007-10-16 23:31:11 -070055static void reserve_resources_of_dev(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Bjorn Helgaas95ab3662008-04-28 16:34:26 -060057 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 int i;
59
Bjorn Helgaas95ab3662008-04-28 16:34:26 -060060 for (i = 0; (res = pnp_get_resource(dev, IORESOURCE_IO, i)); i++) {
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060061 if (res->flags & IORESOURCE_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -060063 if (res->start == 0)
Bjorn Helgaas58595542007-01-18 16:43:46 -070064 continue; /* disabled */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -060065 if (res->start < 0x100)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 /*
67 * Below 0x100 is only standard PC hardware
68 * (pics, kbd, timer, dma, ...)
69 * We should not get resource conflicts there,
70 * and the kernel reserves these anyway
71 * (see arch/i386/kernel/setup.c).
72 * So, do nothing
73 */
74 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -060075 if (res->end < res->start)
Bjorn Helgaas58595542007-01-18 16:43:46 -070076 continue; /* invalid */
77
Bjorn Helgaas9a007b32009-10-06 15:34:00 -060078 reserve_range(dev, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 }
80
Bjorn Helgaas95ab3662008-04-28 16:34:26 -060081 for (i = 0; (res = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) {
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060082 if (res->flags & IORESOURCE_DISABLED)
Bjorn Helgaasa8c78f72007-01-18 16:43:27 -070083 continue;
84
Bjorn Helgaas9a007b32009-10-06 15:34:00 -060085 reserve_range(dev, res, 0);
Bjorn Helgaasa8c78f72007-01-18 16:43:27 -070086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070089static int system_pnp_probe(struct pnp_dev *dev,
90 const struct pnp_device_id *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 reserve_resources_of_dev(dev);
93 return 0;
94}
95
96static struct pnp_driver system_pnp_driver = {
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070097 .name = "system",
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070098 .id_table = pnp_dev_table,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070099 .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
100 .probe = system_pnp_probe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
103static int __init pnp_system_init(void)
104{
105 return pnp_register_driver(&system_pnp_driver);
106}
107
108/**
109 * Reserve motherboard resources after PCI claim BARs,
110 * but before PCI assign resources for uninitialized PCI devices
111 */
112fs_initcall(pnp_system_init);