intel_mid_pci.c 11 KB

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