Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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 Helgaas | a8c78f7 | 2007-01-18 16:43:27 -0700 | [diff] [blame] | 7 | * (c) Copyright 2007 Hewlett-Packard Development Company, L.P. |
| 8 | * Bjorn Helgaas <bjorn.helgaas@hp.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 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 | |
| 18 | static const struct pnp_device_id pnp_dev_table[] = { |
| 19 | /* General ID for reserving resources */ |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 20 | {"PNP0c02", 0}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | /* memory controller */ |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 22 | {"PNP0c01", 0}, |
| 23 | {"", 0} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | }; |
| 25 | |
Bjorn Helgaas | 9a007b3 | 2009-10-06 15:34:00 -0600 | [diff] [blame] | 26 | static void reserve_range(struct pnp_dev *dev, struct resource *r, int port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | char *regionid; |
Kay Sievers | c85e37c | 2009-01-06 10:44:38 -0800 | [diff] [blame] | 29 | const char *pnpid = dev_name(&dev->dev); |
Bjorn Helgaas | 9a007b3 | 2009-10-06 15:34:00 -0600 | [diff] [blame] | 30 | resource_size_t start = r->start, end = r->end; |
Rafael J. Wysocki | 0294112 | 2015-07-04 03:09:03 +0200 | [diff] [blame] | 31 | struct resource *res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | regionid = kmalloc(16, GFP_KERNEL); |
Bjorn Helgaas | b933e19 | 2007-10-16 23:31:11 -0700 | [diff] [blame] | 34 | if (!regionid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | return; |
Bjorn Helgaas | b933e19 | 2007-10-16 23:31:11 -0700 | [diff] [blame] | 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | snprintf(regionid, 16, "pnp %s", pnpid); |
Rafael J. Wysocki | 0294112 | 2015-07-04 03:09:03 +0200 | [diff] [blame] | 38 | 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 Helgaas | b933e19 | 2007-10-16 23:31:11 -0700 | [diff] [blame] | 45 | kfree(regionid); |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | /* |
| 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 Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 52 | dev_info(&dev->dev, "%pR %s reserved\n", r, |
Rafael J. Wysocki | 0294112 | 2015-07-04 03:09:03 +0200 | [diff] [blame] | 53 | res ? "has been" : "could not be"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Bjorn Helgaas | b933e19 | 2007-10-16 23:31:11 -0700 | [diff] [blame] | 56 | static void reserve_resources_of_dev(struct pnp_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
Bjorn Helgaas | 95ab366 | 2008-04-28 16:34:26 -0600 | [diff] [blame] | 58 | struct resource *res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | int i; |
| 60 | |
Bjorn Helgaas | 95ab366 | 2008-04-28 16:34:26 -0600 | [diff] [blame] | 61 | for (i = 0; (res = pnp_get_resource(dev, IORESOURCE_IO, i)); i++) { |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 62 | if (res->flags & IORESOURCE_DISABLED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | continue; |
Bjorn Helgaas | 95ab366 | 2008-04-28 16:34:26 -0600 | [diff] [blame] | 64 | if (res->start == 0) |
Bjorn Helgaas | 5859554 | 2007-01-18 16:43:46 -0700 | [diff] [blame] | 65 | continue; /* disabled */ |
Bjorn Helgaas | 95ab366 | 2008-04-28 16:34:26 -0600 | [diff] [blame] | 66 | if (res->start < 0x100) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | /* |
| 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 Helgaas | 95ab366 | 2008-04-28 16:34:26 -0600 | [diff] [blame] | 76 | if (res->end < res->start) |
Bjorn Helgaas | 5859554 | 2007-01-18 16:43:46 -0700 | [diff] [blame] | 77 | continue; /* invalid */ |
| 78 | |
Bjorn Helgaas | 9a007b3 | 2009-10-06 15:34:00 -0600 | [diff] [blame] | 79 | reserve_range(dev, res, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Bjorn Helgaas | 95ab366 | 2008-04-28 16:34:26 -0600 | [diff] [blame] | 82 | for (i = 0; (res = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) { |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 83 | if (res->flags & IORESOURCE_DISABLED) |
Bjorn Helgaas | a8c78f7 | 2007-01-18 16:43:27 -0700 | [diff] [blame] | 84 | continue; |
| 85 | |
Bjorn Helgaas | 9a007b3 | 2009-10-06 15:34:00 -0600 | [diff] [blame] | 86 | reserve_range(dev, res, 0); |
Bjorn Helgaas | a8c78f7 | 2007-01-18 16:43:27 -0700 | [diff] [blame] | 87 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 90 | static int system_pnp_probe(struct pnp_dev *dev, |
| 91 | const struct pnp_device_id *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | { |
| 93 | reserve_resources_of_dev(dev); |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static struct pnp_driver system_pnp_driver = { |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 98 | .name = "system", |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 99 | .id_table = pnp_dev_table, |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 100 | .flags = PNP_DRIVER_RES_DO_NOT_CHANGE, |
| 101 | .probe = system_pnp_probe, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | static 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 | */ |
| 113 | fs_initcall(pnp_system_init); |