p2sb.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 <andriy.shevchenko@linux.intel.com>
  8. * Jonathan Yong <jonathan.yong@intel.com>
  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. #define P2SB_DEVFN_GOLDMONT PCI_DEVFN(13, 0)
  20. #define SPI_DEVFN_GOLDMONT PCI_DEVFN(13, 2)
  21. static const struct x86_cpu_id p2sb_cpu_ids[] = {
  22. X86_MATCH_VFM(INTEL_ATOM_GOLDMONT, P2SB_DEVFN_GOLDMONT),
  23. X86_MATCH_VFM(INTEL_ATOM_GOLDMONT_PLUS, P2SB_DEVFN_GOLDMONT),
  24. {}
  25. };
  26. /*
  27. * Cache BAR0 of P2SB device functions 0 to 7.
  28. * TODO: The constant 8 is the number of functions that PCI specification
  29. * defines. Same definitions exist tree-wide. Unify this definition and
  30. * the other definitions then move to include/uapi/linux/pci.h.
  31. */
  32. #define NR_P2SB_RES_CACHE 8
  33. struct p2sb_res_cache {
  34. u32 bus_dev_id;
  35. struct resource res;
  36. };
  37. static struct p2sb_res_cache p2sb_resources[NR_P2SB_RES_CACHE];
  38. static bool p2sb_hidden_by_bios;
  39. static void p2sb_get_devfn(unsigned int *devfn)
  40. {
  41. unsigned int fn = P2SB_DEVFN_DEFAULT;
  42. const struct x86_cpu_id *id;
  43. id = x86_match_cpu(p2sb_cpu_ids);
  44. if (id)
  45. fn = (unsigned int)id->driver_data;
  46. *devfn = fn;
  47. }
  48. static bool p2sb_valid_resource(const struct resource *res)
  49. {
  50. return res->flags & ~IORESOURCE_UNSET;
  51. }
  52. /* Copy resource from the first BAR of the device in question */
  53. static void p2sb_read_bar0(struct pci_dev *pdev, struct resource *mem)
  54. {
  55. struct resource *bar0 = pci_resource_n(pdev, 0);
  56. /* Make sure we have no dangling pointers in the output */
  57. memset(mem, 0, sizeof(*mem));
  58. /*
  59. * We copy only selected fields from the original resource.
  60. * Because a PCI device will be removed soon, we may not use
  61. * any allocated data, hence we may not copy any pointers.
  62. */
  63. mem->start = bar0->start;
  64. mem->end = bar0->end;
  65. mem->flags = bar0->flags;
  66. mem->desc = bar0->desc;
  67. }
  68. static void p2sb_scan_and_cache_devfn(struct pci_bus *bus, unsigned int devfn)
  69. {
  70. struct p2sb_res_cache *cache = &p2sb_resources[PCI_FUNC(devfn)];
  71. struct pci_dev *pdev;
  72. pdev = pci_scan_single_device(bus, devfn);
  73. if (!pdev)
  74. return;
  75. p2sb_read_bar0(pdev, &cache->res);
  76. cache->bus_dev_id = bus->dev.id;
  77. pci_stop_and_remove_bus_device(pdev);
  78. }
  79. static int p2sb_scan_and_cache(struct pci_bus *bus, unsigned int devfn)
  80. {
  81. /*
  82. * The BIOS prevents the P2SB device from being enumerated by the PCI
  83. * subsystem, so we need to unhide and hide it back to lookup the BAR.
  84. */
  85. pci_bus_write_config_dword(bus, devfn, P2SBC, 0);
  86. /* Scan the P2SB device and cache its BAR0 */
  87. p2sb_scan_and_cache_devfn(bus, devfn);
  88. /* On Goldmont p2sb_bar() also gets called for the SPI controller */
  89. if (devfn == P2SB_DEVFN_GOLDMONT)
  90. p2sb_scan_and_cache_devfn(bus, SPI_DEVFN_GOLDMONT);
  91. pci_bus_write_config_dword(bus, devfn, P2SBC, P2SBC_HIDE);
  92. if (!p2sb_valid_resource(&p2sb_resources[PCI_FUNC(devfn)].res))
  93. return -ENOENT;
  94. return 0;
  95. }
  96. static struct pci_bus *p2sb_get_bus(struct pci_bus *bus)
  97. {
  98. static struct pci_bus *p2sb_bus;
  99. bus = bus ?: p2sb_bus;
  100. if (bus)
  101. return bus;
  102. /* Assume P2SB is on the bus 0 in domain 0 */
  103. p2sb_bus = pci_find_bus(0, 0);
  104. return p2sb_bus;
  105. }
  106. static int p2sb_cache_resources(void)
  107. {
  108. unsigned int devfn_p2sb;
  109. u32 value = P2SBC_HIDE;
  110. struct pci_bus *bus;
  111. u16 class;
  112. int ret = 0;
  113. /* Get devfn for P2SB device itself */
  114. p2sb_get_devfn(&devfn_p2sb);
  115. bus = p2sb_get_bus(NULL);
  116. if (!bus)
  117. return -ENODEV;
  118. /*
  119. * When a device with same devfn exists and its device class is not
  120. * PCI_CLASS_MEMORY_OTHER for P2SB, do not touch it.
  121. */
  122. pci_bus_read_config_word(bus, devfn_p2sb, PCI_CLASS_DEVICE, &class);
  123. if (!PCI_POSSIBLE_ERROR(class) && class != PCI_CLASS_MEMORY_OTHER)
  124. return -ENODEV;
  125. /*
  126. * Prevent concurrent PCI bus scan from seeing the P2SB device and
  127. * removing via sysfs while it is temporarily exposed.
  128. */
  129. pci_lock_rescan_remove();
  130. pci_bus_read_config_dword(bus, devfn_p2sb, P2SBC, &value);
  131. p2sb_hidden_by_bios = value & P2SBC_HIDE;
  132. /*
  133. * If the BIOS does not hide the P2SB device then its resources
  134. * are accesilble. Cache them only if the P2SB device is hidden.
  135. */
  136. if (p2sb_hidden_by_bios)
  137. ret = p2sb_scan_and_cache(bus, devfn_p2sb);
  138. pci_unlock_rescan_remove();
  139. return ret;
  140. }
  141. static int p2sb_read_from_cache(struct pci_bus *bus, unsigned int devfn,
  142. struct resource *mem)
  143. {
  144. struct p2sb_res_cache *cache = &p2sb_resources[PCI_FUNC(devfn)];
  145. if (cache->bus_dev_id != bus->dev.id)
  146. return -ENODEV;
  147. if (!p2sb_valid_resource(&cache->res))
  148. return -ENOENT;
  149. memcpy(mem, &cache->res, sizeof(*mem));
  150. return 0;
  151. }
  152. static int p2sb_read_from_dev(struct pci_bus *bus, unsigned int devfn,
  153. struct resource *mem)
  154. {
  155. struct pci_dev *pdev;
  156. int ret = 0;
  157. pdev = pci_get_slot(bus, devfn);
  158. if (!pdev)
  159. return -ENODEV;
  160. if (p2sb_valid_resource(pci_resource_n(pdev, 0)))
  161. p2sb_read_bar0(pdev, mem);
  162. else
  163. ret = -ENOENT;
  164. pci_dev_put(pdev);
  165. return ret;
  166. }
  167. /**
  168. * p2sb_bar - Get Primary to Sideband (P2SB) bridge device BAR
  169. * @bus: PCI bus to communicate with
  170. * @devfn: PCI slot and function to communicate with
  171. * @mem: memory resource to be filled in
  172. *
  173. * If @bus is NULL, the bus 0 in domain 0 will be used.
  174. * If @devfn is 0, it will be replaced by devfn of the P2SB device.
  175. *
  176. * Caller must provide a valid pointer to @mem.
  177. *
  178. * Return:
  179. * 0 on success or appropriate errno value on error.
  180. */
  181. int p2sb_bar(struct pci_bus *bus, unsigned int devfn, struct resource *mem)
  182. {
  183. bus = p2sb_get_bus(bus);
  184. if (!bus)
  185. return -ENODEV;
  186. if (!devfn)
  187. p2sb_get_devfn(&devfn);
  188. if (p2sb_hidden_by_bios)
  189. return p2sb_read_from_cache(bus, devfn, mem);
  190. return p2sb_read_from_dev(bus, devfn, mem);
  191. }
  192. EXPORT_SYMBOL_GPL(p2sb_bar);
  193. static int __init p2sb_fs_init(void)
  194. {
  195. return p2sb_cache_resources();
  196. }
  197. /*
  198. * pci_rescan_remove_lock() can not be locked in sysfs PCI bus rescan path
  199. * because of deadlock. To avoid the deadlock, access P2SB devices with the lock
  200. * at an early step in kernel initialization and cache required resources.
  201. *
  202. * We want to run as early as possible. If the P2SB was assigned a bad BAR,
  203. * we'll need to wait on pcibios_assign_resources() to fix it. So, our list of
  204. * initcall dependencies looks something like this:
  205. *
  206. * ...
  207. * subsys_initcall (pci_subsys_init)
  208. * fs_initcall (pcibios_assign_resources)
  209. */
  210. fs_initcall_sync(p2sb_fs_init);