p2sb.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Primary to Sideband (P2SB) bridge access support
  4. *
  5. * Copyright (c) 2017, 2021-2022 Intel Corporation.
  6. *
  7. * Authors: Andy Shevchenko <[email protected]>
  8. * Jonathan Yong <[email protected]>
  9. */
  10. #include <linux/bits.h>
  11. #include <linux/export.h>
  12. #include <linux/pci.h>
  13. #include <linux/platform_data/x86/p2sb.h>
  14. #include <asm/cpu_device_id.h>
  15. #include <asm/intel-family.h>
  16. #define P2SBC 0xe0
  17. #define P2SBC_HIDE BIT(8)
  18. #define P2SB_DEVFN_DEFAULT PCI_DEVFN(31, 1)
  19. static const struct x86_cpu_id p2sb_cpu_ids[] = {
  20. X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT, PCI_DEVFN(13, 0)),
  21. {}
  22. };
  23. static int p2sb_get_devfn(unsigned int *devfn)
  24. {
  25. unsigned int fn = P2SB_DEVFN_DEFAULT;
  26. const struct x86_cpu_id *id;
  27. id = x86_match_cpu(p2sb_cpu_ids);
  28. if (id)
  29. fn = (unsigned int)id->driver_data;
  30. *devfn = fn;
  31. return 0;
  32. }
  33. /* Copy resource from the first BAR of the device in question */
  34. static int p2sb_read_bar0(struct pci_dev *pdev, struct resource *mem)
  35. {
  36. struct resource *bar0 = &pdev->resource[0];
  37. /* Make sure we have no dangling pointers in the output */
  38. memset(mem, 0, sizeof(*mem));
  39. /*
  40. * We copy only selected fields from the original resource.
  41. * Because a PCI device will be removed soon, we may not use
  42. * any allocated data, hence we may not copy any pointers.
  43. */
  44. mem->start = bar0->start;
  45. mem->end = bar0->end;
  46. mem->flags = bar0->flags;
  47. mem->desc = bar0->desc;
  48. return 0;
  49. }
  50. static int p2sb_scan_and_read(struct pci_bus *bus, unsigned int devfn, struct resource *mem)
  51. {
  52. struct pci_dev *pdev;
  53. int ret;
  54. pdev = pci_scan_single_device(bus, devfn);
  55. if (!pdev)
  56. return -ENODEV;
  57. ret = p2sb_read_bar0(pdev, mem);
  58. pci_stop_and_remove_bus_device(pdev);
  59. return ret;
  60. }
  61. /**
  62. * p2sb_bar - Get Primary to Sideband (P2SB) bridge device BAR
  63. * @bus: PCI bus to communicate with
  64. * @devfn: PCI slot and function to communicate with
  65. * @mem: memory resource to be filled in
  66. *
  67. * The BIOS prevents the P2SB device from being enumerated by the PCI
  68. * subsystem, so we need to unhide and hide it back to lookup the BAR.
  69. *
  70. * if @bus is NULL, the bus 0 in domain 0 will be used.
  71. * If @devfn is 0, it will be replaced by devfn of the P2SB device.
  72. *
  73. * Caller must provide a valid pointer to @mem.
  74. *
  75. * Locking is handled by pci_rescan_remove_lock mutex.
  76. *
  77. * Return:
  78. * 0 on success or appropriate errno value on error.
  79. */
  80. int p2sb_bar(struct pci_bus *bus, unsigned int devfn, struct resource *mem)
  81. {
  82. struct pci_dev *pdev_p2sb;
  83. unsigned int devfn_p2sb;
  84. u32 value = P2SBC_HIDE;
  85. int ret;
  86. /* Get devfn for P2SB device itself */
  87. ret = p2sb_get_devfn(&devfn_p2sb);
  88. if (ret)
  89. return ret;
  90. /* if @bus is NULL, use bus 0 in domain 0 */
  91. bus = bus ?: pci_find_bus(0, 0);
  92. /*
  93. * Prevent concurrent PCI bus scan from seeing the P2SB device and
  94. * removing via sysfs while it is temporarily exposed.
  95. */
  96. pci_lock_rescan_remove();
  97. /* Unhide the P2SB device, if needed */
  98. pci_bus_read_config_dword(bus, devfn_p2sb, P2SBC, &value);
  99. if (value & P2SBC_HIDE)
  100. pci_bus_write_config_dword(bus, devfn_p2sb, P2SBC, 0);
  101. pdev_p2sb = pci_scan_single_device(bus, devfn_p2sb);
  102. if (devfn)
  103. ret = p2sb_scan_and_read(bus, devfn, mem);
  104. else
  105. ret = p2sb_read_bar0(pdev_p2sb, mem);
  106. pci_stop_and_remove_bus_device(pdev_p2sb);
  107. /* Hide the P2SB device, if it was hidden */
  108. if (value & P2SBC_HIDE)
  109. pci_bus_write_config_dword(bus, devfn_p2sb, P2SBC, P2SBC_HIDE);
  110. pci_unlock_rescan_remove();
  111. if (ret)
  112. return ret;
  113. if (mem->flags == 0)
  114. return -ENODEV;
  115. return 0;
  116. }
  117. EXPORT_SYMBOL_GPL(p2sb_bar);