qemu.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
  4. */
  5. #include <common.h>
  6. #include <pci.h>
  7. #include <qfw.h>
  8. #include <asm/irq.h>
  9. #include <asm/post.h>
  10. #include <asm/processor.h>
  11. #include <asm/arch/device.h>
  12. #include <asm/arch/qemu.h>
  13. static bool i440fx;
  14. #ifdef CONFIG_QFW
  15. /* on x86, the qfw registers are all IO ports */
  16. #define FW_CONTROL_PORT 0x510
  17. #define FW_DATA_PORT 0x511
  18. #define FW_DMA_PORT_LOW 0x514
  19. #define FW_DMA_PORT_HIGH 0x518
  20. static void qemu_x86_fwcfg_read_entry_pio(uint16_t entry,
  21. uint32_t size, void *address)
  22. {
  23. uint32_t i = 0;
  24. uint8_t *data = address;
  25. /*
  26. * writting FW_CFG_INVALID will cause read operation to resume at
  27. * last offset, otherwise read will start at offset 0
  28. *
  29. * Note: on platform where the control register is IO port, the
  30. * endianness is little endian.
  31. */
  32. if (entry != FW_CFG_INVALID)
  33. outw(cpu_to_le16(entry), FW_CONTROL_PORT);
  34. /* the endianness of data register is string-preserving */
  35. while (size--)
  36. data[i++] = inb(FW_DATA_PORT);
  37. }
  38. static void qemu_x86_fwcfg_read_entry_dma(struct fw_cfg_dma_access *dma)
  39. {
  40. /* the DMA address register is big endian */
  41. outl(cpu_to_be32((uintptr_t)dma), FW_DMA_PORT_HIGH);
  42. while (be32_to_cpu(dma->control) & ~FW_CFG_DMA_ERROR)
  43. __asm__ __volatile__ ("pause");
  44. }
  45. static struct fw_cfg_arch_ops fwcfg_x86_ops = {
  46. .arch_read_pio = qemu_x86_fwcfg_read_entry_pio,
  47. .arch_read_dma = qemu_x86_fwcfg_read_entry_dma
  48. };
  49. #endif
  50. static void enable_pm_piix(void)
  51. {
  52. u8 en;
  53. u16 cmd;
  54. /* Set the PM I/O base */
  55. pci_write_config32(PIIX_PM, PMBA, CONFIG_ACPI_PM1_BASE | 1);
  56. /* Enable access to the PM I/O space */
  57. pci_read_config16(PIIX_PM, PCI_COMMAND, &cmd);
  58. cmd |= PCI_COMMAND_IO;
  59. pci_write_config16(PIIX_PM, PCI_COMMAND, cmd);
  60. /* PM I/O Space Enable (PMIOSE) */
  61. pci_read_config8(PIIX_PM, PMREGMISC, &en);
  62. en |= PMIOSE;
  63. pci_write_config8(PIIX_PM, PMREGMISC, en);
  64. }
  65. static void enable_pm_ich9(void)
  66. {
  67. /* Set the PM I/O base */
  68. pci_write_config32(ICH9_PM, PMBA, CONFIG_ACPI_PM1_BASE | 1);
  69. }
  70. static void qemu_chipset_init(void)
  71. {
  72. u16 device, xbcs;
  73. int pam, i;
  74. /*
  75. * i440FX and Q35 chipset have different PAM register offset, but with
  76. * the same bitfield layout. Here we determine the offset based on its
  77. * PCI device ID.
  78. */
  79. pci_read_config16(PCI_BDF(0, 0, 0), PCI_DEVICE_ID, &device);
  80. i440fx = (device == PCI_DEVICE_ID_INTEL_82441);
  81. pam = i440fx ? I440FX_PAM : Q35_PAM;
  82. /*
  83. * Initialize Programmable Attribute Map (PAM) Registers
  84. *
  85. * Configure legacy segments C/D/E/F to system RAM
  86. */
  87. for (i = 0; i < PAM_NUM; i++)
  88. pci_write_config8(PCI_BDF(0, 0, 0), pam + i, PAM_RW);
  89. if (i440fx) {
  90. /*
  91. * Enable legacy IDE I/O ports decode
  92. *
  93. * Note: QEMU always decode legacy IDE I/O port on PIIX chipset.
  94. * However Linux ata_piix driver does sanity check on these two
  95. * registers to see whether legacy ports decode is turned on.
  96. * This is to make Linux ata_piix driver happy.
  97. */
  98. pci_write_config16(PIIX_IDE, IDE0_TIM, IDE_DECODE_EN);
  99. pci_write_config16(PIIX_IDE, IDE1_TIM, IDE_DECODE_EN);
  100. /* Enable I/O APIC */
  101. pci_read_config16(PIIX_ISA, XBCS, &xbcs);
  102. xbcs |= APIC_EN;
  103. pci_write_config16(PIIX_ISA, XBCS, xbcs);
  104. enable_pm_piix();
  105. } else {
  106. /* Configure PCIe ECAM base address */
  107. pci_write_config32(PCI_BDF(0, 0, 0), PCIEX_BAR,
  108. CONFIG_PCIE_ECAM_BASE | BAR_EN);
  109. enable_pm_ich9();
  110. }
  111. #ifdef CONFIG_QFW
  112. qemu_fwcfg_init(&fwcfg_x86_ops);
  113. #endif
  114. }
  115. #if !CONFIG_IS_ENABLED(SPL_X86_32BIT_INIT)
  116. int arch_cpu_init(void)
  117. {
  118. post_code(POST_CPU_INIT);
  119. return x86_cpu_init_f();
  120. }
  121. int checkcpu(void)
  122. {
  123. return 0;
  124. }
  125. int print_cpuinfo(void)
  126. {
  127. post_code(POST_CPU_INFO);
  128. return default_print_cpuinfo();
  129. }
  130. #endif
  131. int arch_early_init_r(void)
  132. {
  133. qemu_chipset_init();
  134. return 0;
  135. }
  136. #ifdef CONFIG_GENERATE_MP_TABLE
  137. int mp_determine_pci_dstirq(int bus, int dev, int func, int pirq)
  138. {
  139. u8 irq;
  140. if (i440fx) {
  141. /*
  142. * Not like most x86 platforms, the PIRQ[A-D] on PIIX3 are not
  143. * connected to I/O APIC INTPIN#16-19. Instead they are routed
  144. * to an irq number controled by the PIRQ routing register.
  145. */
  146. pci_read_config8(PCI_BDF(bus, dev, func),
  147. PCI_INTERRUPT_LINE, &irq);
  148. } else {
  149. /*
  150. * ICH9's PIRQ[A-H] are not consecutive numbers from 0 to 7.
  151. * PIRQ[A-D] still maps to [0-3] but PIRQ[E-H] maps to [8-11].
  152. */
  153. irq = pirq < 8 ? pirq + 16 : pirq + 12;
  154. }
  155. return irq;
  156. }
  157. #endif