blob: 6950503741eb654d9c5626f00d42c413b27c019f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * system.c - a driver for reserving pnp system resources
4 *
5 * Some code is based on pnpbios_core.c
6 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
Bjorn Helgaasa8c78f72007-01-18 16:43:27 -07007 * (c) Copyright 2007 Hewlett-Packard Development Company, L.P.
8 * Bjorn Helgaas <bjorn.helgaas@hp.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
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
18static const struct pnp_device_id pnp_dev_table[] = {
19 /* General ID for reserving resources */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070020 {"PNP0c02", 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 /* memory controller */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070022 {"PNP0c01", 0},
23 {"", 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -070024};
25
Bjorn Helgaas9a007b32009-10-06 15:34:00 -060026static void reserve_range(struct pnp_dev *dev, struct resource *r, int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 char *regionid;
Kay Sieversc85e37c2009-01-06 10:44:38 -080029 const char *pnpid = dev_name(&dev->dev);
Bjorn Helgaas9a007b32009-10-06 15:34:00 -060030 resource_size_t start = r->start, end = r->end;
Rafael J. Wysocki02941122015-07-04 03:09:03 +020031 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33 regionid = kmalloc(16, GFP_KERNEL);
Bjorn Helgaasb933e192007-10-16 23:31:11 -070034 if (!regionid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 return;
Bjorn Helgaasb933e192007-10-16 23:31:11 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 snprintf(regionid, 16, "pnp %s", pnpid);
Rafael J. Wysocki02941122015-07-04 03:09:03 +020038 if (port)
39 res = request_region(start, end - start + 1, regionid);
40 else
41 res = request_mem_region(start, end - start + 1, regionid);
42 if (res)
43 res->flags &= ~IORESOURCE_BUSY;
44 else
Bjorn Helgaasb933e192007-10-16 23:31:11 -070045 kfree(regionid);
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 /*
48 * Failures at this point are usually harmless. pci quirks for
49 * example do reserve stuff they know about too, so we may well
50 * have double reservations.
51 */
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -060052 dev_info(&dev->dev, "%pR %s reserved\n", r,
Rafael J. Wysocki02941122015-07-04 03:09:03 +020053 res ? "has been" : "could not be");
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
Bjorn Helgaasb933e192007-10-16 23:31:11 -070056static void reserve_resources_of_dev(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Bjorn Helgaas95ab3662008-04-28 16:34:26 -060058 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 int i;
60
Bjorn Helgaas95ab3662008-04-28 16:34:26 -060061 for (i = 0; (res = pnp_get_resource(dev, IORESOURCE_IO, i)); i++) {
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060062 if (res->flags & IORESOURCE_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -060064 if (res->start == 0)
Bjorn Helgaas58595542007-01-18 16:43:46 -070065 continue; /* disabled */
Bjorn Helgaas95ab3662008-04-28 16:34:26 -060066 if (res->start < 0x100)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 /*
68 * Below 0x100 is only standard PC hardware
69 * (pics, kbd, timer, dma, ...)
70 * We should not get resource conflicts there,
71 * and the kernel reserves these anyway
72 * (see arch/i386/kernel/setup.c).
73 * So, do nothing
74 */
75 continue;
Bjorn Helgaas95ab3662008-04-28 16:34:26 -060076 if (res->end < res->start)
Bjorn Helgaas58595542007-01-18 16:43:46 -070077 continue; /* invalid */
78
Bjorn Helgaas9a007b32009-10-06 15:34:00 -060079 reserve_range(dev, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
81
Bjorn Helgaas95ab3662008-04-28 16:34:26 -060082 for (i = 0; (res = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) {
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -060083 if (res->flags & IORESOURCE_DISABLED)
Bjorn Helgaasa8c78f72007-01-18 16:43:27 -070084 continue;
85
Bjorn Helgaas9a007b32009-10-06 15:34:00 -060086 reserve_range(dev, res, 0);
Bjorn Helgaasa8c78f72007-01-18 16:43:27 -070087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070090static int system_pnp_probe(struct pnp_dev *dev,
91 const struct pnp_device_id *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 reserve_resources_of_dev(dev);
94 return 0;
95}
96
97static struct pnp_driver system_pnp_driver = {
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070098 .name = "system",
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070099 .id_table = pnp_dev_table,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700100 .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
101 .probe = system_pnp_probe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
104static int __init pnp_system_init(void)
105{
106 return pnp_register_driver(&system_pnp_driver);
107}
108
109/**
110 * Reserve motherboard resources after PCI claim BARs,
111 * but before PCI assign resources for uninitialized PCI devices
112 */
113fs_initcall(pnp_system_init);