intel_mid_pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Intel MID PCI support
  4. * Copyright (c) 2008 Intel Corporation
  5. * Jesse Barnes <jesse.barnes@intel.com>
  6. *
  7. * Moorestown has an interesting PCI implementation:
  8. * - configuration space is memory mapped (as defined by MCFG)
  9. * - Lincroft devices also have a real, type 1 configuration space
  10. * - Early Lincroft silicon has a type 1 access bug that will cause
  11. * a hang if non-existent devices are accessed
  12. * - some devices have the "fixed BAR" capability, which means
  13. * they can't be relocated or modified; check for that during
  14. * BAR sizing
  15. *
  16. * So, we use the MCFG space for all reads and writes, but also send
  17. * Lincroft writes to type 1 space. But only read/write if the device
  18. * actually exists, otherwise return all 1s for reads and bit bucket
  19. * the writes.
  20. */
  21. #include <linux/sched.h>
  22. #include <linux/pci.h>
  23. #include <linux/ioport.h>
  24. #include <linux/init.h>
  25. #include <linux/dmi.h>
  26. #include <linux/acpi.h>
  27. #include <linux/io.h>
  28. #include <linux/smp.h>
  29. #include <asm/segment.h>
  30. #include <asm/pci_x86.h>
  31. #include <asm/hw_irq.h>
  32. #include <asm/io_apic.h>
  33. #include <asm/intel-mid.h>
  34. #define PCIE_CAP_OFFSET 0x100
  35. /* Quirks for the listed devices */
  36. #define PCI_DEVICE_ID_INTEL_MRFLD_MMC 0x1190
  37. #define PCI_DEVICE_ID_INTEL_MRFLD_HSU 0x1191
  38. /* Fixed BAR fields */
  39. #define PCIE_VNDR_CAP_ID_FIXED_BAR 0x00 /* Fixed BAR (TBD) */
  40. #define PCI_FIXED_BAR_0_SIZE 0x04
  41. #define PCI_FIXED_BAR_1_SIZE 0x08
  42. #define PCI_FIXED_BAR_2_SIZE 0x0c
  43. #define PCI_FIXED_BAR_3_SIZE 0x10
  44. #define PCI_FIXED_BAR_4_SIZE 0x14
  45. #define PCI_FIXED_BAR_5_SIZE 0x1c
  46. static int pci_soc_mode;
  47. /**
  48. * fixed_bar_cap - return the offset of the fixed BAR cap if found
  49. * @bus: PCI bus
  50. * @devfn: device in question
  51. *
  52. * Look for the fixed BAR cap on @bus and @devfn, returning its offset
  53. * if found or 0 otherwise.
  54. */
  55. static int fixed_bar_cap(struct pci_bus *bus, unsigned int devfn)
  56. {
  57. int pos;
  58. u32 pcie_cap = 0, cap_data;
  59. pos = PCIE_CAP_OFFSET;
  60. if (!raw_pci_ext_ops)
  61. return 0;
  62. while (pos) {
  63. if (raw_pci_ext_ops->read(pci_domain_nr(bus), bus->number,
  64. devfn, pos, 4, &pcie_cap))
  65. return 0;
  66. if (PCI_EXT_CAP_ID(pcie_cap) == 0x0000 ||
  67. PCI_EXT_CAP_ID(pcie_cap) == 0xffff)
  68. break;
  69. if (PCI_EXT_CAP_ID(pcie_cap) == PCI_EXT_CAP_ID_VNDR) {
  70. raw_pci_ext_ops->read(pci_domain_nr(bus), bus->number,
  71. devfn, pos + 4, 4, &cap_data);
  72. if ((cap_data & 0xffff) == PCIE_VNDR_CAP_ID_FIXED_BAR)
  73. return pos;
  74. }
  75. pos = PCI_EXT_CAP_NEXT(pcie_cap);
  76. }
  77. return 0;
  78. }
  79. static int pci_device_update_fixed(struct pci_bus *bus, unsigned int devfn,
  80. int reg, int len, u32 val, int offset)
  81. {
  82. u32 size;
  83. unsigned int domain, busnum;
  84. int bar = (reg - PCI_BASE_ADDRESS_0) >> 2;
  85. domain = pci_domain_nr(bus);
  86. busnum = bus->number;
  87. if (val == ~0 && len == 4) {
  88. unsigned long decode;
  89. raw_pci_ext_ops->read(domain, busnum, devfn,
  90. offset + 8 + (bar * 4), 4, &size);
  91. /* Turn the size into a decode pattern for the sizing code */
  92. if (size) {
  93. decode = size - 1;
  94. decode |= decode >> 1;
  95. decode |= decode >> 2;
  96. decode |= decode >> 4;
  97. decode |= decode >> 8;
  98. decode |= decode >> 16;
  99. decode++;
  100. decode = ~(decode - 1);
  101. } else {
  102. decode = 0;
  103. }
  104. /*
  105. * If val is all ones, the core code is trying to size the reg,
  106. * so update the mmconfig space with the real size.
  107. *
  108. * Note: this assumes the fixed size we got is a power of two.
  109. */
  110. return raw_pci_ext_ops->write(domain, busnum, devfn, reg, 4,
  111. decode);
  112. }
  113. /* This is some other kind of BAR write, so just do it. */
  114. return raw_pci_ext_ops->write(domain, busnum, devfn, reg, len, val);
  115. }
  116. /**
  117. * type1_access_ok - check whether to use type 1
  118. * @bus: bus number
  119. * @devfn: device & function in question
  120. *
  121. * If the bus is on a Lincroft chip and it exists, or is not on a Lincroft at
  122. * all, the we can go ahead with any reads & writes. If it's on a Lincroft,
  123. * but doesn't exist, avoid the access altogether to keep the chip from
  124. * hanging.
  125. */
  126. static bool type1_access_ok(unsigned int bus, unsigned int devfn, int reg)
  127. {
  128. /*
  129. * This is a workaround for A0 LNC bug where PCI status register does
  130. * not have new CAP bit set. can not be written by SW either.
  131. *
  132. * PCI header type in real LNC indicates a single function device, this
  133. * will prevent probing other devices under the same function in PCI
  134. * shim. Therefore, use the header type in shim instead.
  135. */
  136. if (reg >= 0x100 || reg == PCI_STATUS || reg == PCI_HEADER_TYPE)
  137. return false;
  138. if (bus == 0 && (devfn == PCI_DEVFN(2, 0)
  139. || devfn == PCI_DEVFN(0, 0)
  140. || devfn == PCI_DEVFN(3, 0)))
  141. return true;
  142. return false; /* Langwell on others */
  143. }
  144. static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
  145. int size, u32 *value)
  146. {
  147. if (type1_access_ok(bus->number, devfn, where))
  148. return pci_direct_conf1.read(pci_domain_nr(bus), bus->number,
  149. devfn, where, size, value);
  150. return raw_pci_ext_ops->read(pci_domain_nr(bus), bus->number,
  151. devfn, where, size, value);
  152. }
  153. static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
  154. int size, u32 value)
  155. {
  156. int offset;
  157. /*
  158. * On MRST, there is no PCI ROM BAR, this will cause a subsequent read
  159. * to ROM BAR return 0 then being ignored.
  160. */
  161. if (where == PCI_ROM_ADDRESS)
  162. return 0;
  163. /*
  164. * Devices with fixed BARs need special handling:
  165. * - BAR sizing code will save, write ~0, read size, restore
  166. * - so writes to fixed BARs need special handling
  167. * - other writes to fixed BAR devices should go through mmconfig
  168. */
  169. offset = fixed_bar_cap(bus, devfn);
  170. if (offset &&
  171. (where >= PCI_BASE_ADDRESS_0 && where <= PCI_BASE_ADDRESS_5)) {
  172. return pci_device_update_fixed(bus, devfn, where, size, value,
  173. offset);
  174. }
  175. /*
  176. * On Moorestown update both real & mmconfig space
  177. * Note: early Lincroft silicon can't handle type 1 accesses to
  178. * non-existent devices, so just eat the write in that case.
  179. */
  180. if (type1_access_ok(bus->number, devfn, where))
  181. return pci_direct_conf1.write(pci_domain_nr(bus), bus->number,
  182. devfn, where, size, value);
  183. return raw_pci_ext_ops->write(pci_domain_nr(bus), bus->number, devfn,
  184. where, size, value);
  185. }
  186. static int intel_mid_pci_irq_enable(struct pci_dev *dev)
  187. {
  188. struct irq_alloc_info info;
  189. int polarity;
  190. int ret;
  191. u8 gsi;
  192. if (dev->irq_managed && dev->irq > 0)
  193. return 0;
  194. ret = pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &gsi);
  195. if (ret < 0) {
  196. dev_warn(&dev->dev, "Failed to read interrupt line: %d\n", ret);
  197. return ret;
  198. }
  199. switch (intel_mid_identify_cpu()) {
  200. case INTEL_MID_CPU_CHIP_TANGIER:
  201. polarity = IOAPIC_POL_HIGH;
  202. /* Special treatment for IRQ0 */
  203. if (gsi == 0) {
  204. /*
  205. * Skip HS UART common registers device since it has
  206. * IRQ0 assigned and not used by the kernel.
  207. */
  208. if (dev->device == PCI_DEVICE_ID_INTEL_MRFLD_HSU)
  209. return -EBUSY;
  210. /*
  211. * TNG has IRQ0 assigned to eMMC controller. But there
  212. * are also other devices with bogus PCI configuration
  213. * that have IRQ0 assigned. This check ensures that
  214. * eMMC gets it. The rest of devices still could be
  215. * enabled without interrupt line being allocated.
  216. */
  217. if (dev->device != PCI_DEVICE_ID_INTEL_MRFLD_MMC)
  218. return 0;
  219. }
  220. break;
  221. default:
  222. polarity = IOAPIC_POL_LOW;
  223. break;
  224. }
  225. ioapic_set_alloc_attr(&info, dev_to_node(&dev->dev), 1, polarity);
  226. /*
  227. * MRST only have IOAPIC, the PCI irq lines are 1:1 mapped to
  228. * IOAPIC RTE entries, so we just enable RTE for the device.
  229. */
  230. ret = mp_map_gsi_to_irq(gsi, IOAPIC_MAP_ALLOC, &info);
  231. if (ret < 0)
  232. return ret;
  233. dev->irq = ret;
  234. dev->irq_managed = 1;
  235. return 0;
  236. }
  237. static void intel_mid_pci_irq_disable(struct pci_dev *dev)
  238. {
  239. if (!mp_should_keep_irq(&dev->dev) && dev->irq_managed &&
  240. dev->irq > 0) {
  241. mp_unmap_irq(dev->irq);
  242. dev->irq_managed = 0;
  243. }
  244. }
  245. static const struct pci_ops intel_mid_pci_ops __initconst = {
  246. .read = pci_read,
  247. .write = pci_write,
  248. };
  249. /**
  250. * intel_mid_pci_init - installs intel_mid_pci_ops
  251. *
  252. * Moorestown has an interesting PCI implementation (see above).
  253. * Called when the early platform detection installs it.
  254. */
  255. int __init intel_mid_pci_init(void)
  256. {
  257. pr_info("Intel MID platform detected, using MID PCI ops\n");
  258. pci_mmcfg_late_init();
  259. pcibios_enable_irq = intel_mid_pci_irq_enable;
  260. pcibios_disable_irq = intel_mid_pci_irq_disable;
  261. pci_root_ops = intel_mid_pci_ops;
  262. pci_soc_mode = 1;
  263. /* Continue with standard init */
  264. acpi_noirq_set();
  265. return 1;
  266. }
  267. /*
  268. * Langwell devices are not true PCI devices; they are not subject to 10 ms
  269. * d3 to d0 delay required by PCI spec.
  270. */
  271. static void pci_d3delay_fixup(struct pci_dev *dev)
  272. {
  273. /*
  274. * PCI fixups are effectively decided compile time. If we have a dual
  275. * SoC/non-SoC kernel we don't want to mangle d3 on non-SoC devices.
  276. */
  277. if (!pci_soc_mode)
  278. return;
  279. /*
  280. * True PCI devices in Lincroft should allow type 1 access, the rest
  281. * are Langwell fake PCI devices.
  282. */
  283. if (type1_access_ok(dev->bus->number, dev->devfn, PCI_DEVICE_ID))
  284. return;
  285. dev->d3_delay = 0;
  286. }
  287. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_ANY_ID, pci_d3delay_fixup);
  288. static void mid_power_off_one_device(struct pci_dev *dev)
  289. {
  290. u16 pmcsr;
  291. /*
  292. * Update current state first, otherwise PCI core enforces PCI_D0 in
  293. * pci_set_power_state() for devices which status was PCI_UNKNOWN.
  294. */
  295. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  296. dev->current_state = (pci_power_t __force)(pmcsr & PCI_PM_CTRL_STATE_MASK);
  297. pci_set_power_state(dev, PCI_D3hot);
  298. }
  299. static void mid_power_off_devices(struct pci_dev *dev)
  300. {
  301. int id;
  302. if (!pci_soc_mode)
  303. return;
  304. id = intel_mid_pwr_get_lss_id(dev);
  305. if (id < 0)
  306. return;
  307. /*
  308. * This sets only PMCSR bits. The actual power off will happen in
  309. * arch/x86/platform/intel-mid/pwr.c.
  310. */
  311. mid_power_off_one_device(dev);
  312. }
  313. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_ANY_ID, mid_power_off_devices);
  314. /*
  315. * Langwell devices reside at fixed offsets, don't try to move them.
  316. */
  317. static void pci_fixed_bar_fixup(struct pci_dev *dev)
  318. {
  319. unsigned long offset;
  320. u32 size;
  321. int i;
  322. if (!pci_soc_mode)
  323. return;
  324. /* Must have extended configuration space */
  325. if (dev->cfg_size < PCIE_CAP_OFFSET + 4)
  326. return;
  327. /* Fixup the BAR sizes for fixed BAR devices and make them unmoveable */
  328. offset = fixed_bar_cap(dev->bus, dev->devfn);
  329. if (!offset || PCI_DEVFN(2, 0) == dev->devfn ||
  330. PCI_DEVFN(2, 2) == dev->devfn)
  331. return;
  332. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  333. pci_read_config_dword(dev, offset + 8 + (i * 4), &size);
  334. dev->resource[i].end = dev->resource[i].start + size - 1;
  335. dev->resource[i].flags |= IORESOURCE_PCI_FIXED;
  336. }
  337. }
  338. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_ANY_ID, pci_fixed_bar_fixup);