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