pci_mcfg.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (C) 2016 Broadcom
  3. * Author: Jayachandran C <[email protected]>
  4. * Copyright (C) 2016 Semihalf
  5. * Author: Tomasz Nowicki <[email protected]>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License, version 2, as
  9. * published by the Free Software Foundation (the "GPL").
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License version 2 (GPLv2) for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * version 2 (GPLv2) along with this source code.
  18. */
  19. #define pr_fmt(fmt) "ACPI: " fmt
  20. #include <linux/kernel.h>
  21. #include <linux/pci.h>
  22. #include <linux/pci-acpi.h>
  23. /* Structure to hold entries from the MCFG table */
  24. struct mcfg_entry {
  25. struct list_head list;
  26. phys_addr_t addr;
  27. u16 segment;
  28. u8 bus_start;
  29. u8 bus_end;
  30. };
  31. /* List to save MCFG entries */
  32. static LIST_HEAD(pci_mcfg_list);
  33. phys_addr_t pci_mcfg_lookup(u16 seg, struct resource *bus_res)
  34. {
  35. struct mcfg_entry *e;
  36. /*
  37. * We expect exact match, unless MCFG entry end bus covers more than
  38. * specified by caller.
  39. */
  40. list_for_each_entry(e, &pci_mcfg_list, list) {
  41. if (e->segment == seg && e->bus_start == bus_res->start &&
  42. e->bus_end >= bus_res->end)
  43. return e->addr;
  44. }
  45. return 0;
  46. }
  47. static __init int pci_mcfg_parse(struct acpi_table_header *header)
  48. {
  49. struct acpi_table_mcfg *mcfg;
  50. struct acpi_mcfg_allocation *mptr;
  51. struct mcfg_entry *e, *arr;
  52. int i, n;
  53. if (header->length < sizeof(struct acpi_table_mcfg))
  54. return -EINVAL;
  55. n = (header->length - sizeof(struct acpi_table_mcfg)) /
  56. sizeof(struct acpi_mcfg_allocation);
  57. mcfg = (struct acpi_table_mcfg *)header;
  58. mptr = (struct acpi_mcfg_allocation *) &mcfg[1];
  59. arr = kcalloc(n, sizeof(*arr), GFP_KERNEL);
  60. if (!arr)
  61. return -ENOMEM;
  62. for (i = 0, e = arr; i < n; i++, mptr++, e++) {
  63. e->segment = mptr->pci_segment;
  64. e->addr = mptr->address;
  65. e->bus_start = mptr->start_bus_number;
  66. e->bus_end = mptr->end_bus_number;
  67. list_add(&e->list, &pci_mcfg_list);
  68. }
  69. pr_info("MCFG table detected, %d entries\n", n);
  70. return 0;
  71. }
  72. /* Interface called by ACPI - parse and save MCFG table */
  73. void __init pci_mmcfg_late_init(void)
  74. {
  75. int err = acpi_table_parse(ACPI_SIG_MCFG, pci_mcfg_parse);
  76. if (err)
  77. pr_err("Failed to parse MCFG (%d)\n", err);
  78. }