system.c 2.7 KB

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