rtas_pci.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2001 Dave Engebretsen, IBM Corporation
  4. * Copyright (C) 2003 Anton Blanchard <[email protected]>, IBM
  5. *
  6. * RTAS specific routines for PCI.
  7. *
  8. * Based on code from pci.c, chrp_pci.c and pSeries_pci.c
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/threads.h>
  12. #include <linux/pci.h>
  13. #include <linux/string.h>
  14. #include <linux/init.h>
  15. #include <linux/pgtable.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_fdt.h>
  18. #include <asm/io.h>
  19. #include <asm/irq.h>
  20. #include <asm/machdep.h>
  21. #include <asm/pci-bridge.h>
  22. #include <asm/iommu.h>
  23. #include <asm/rtas.h>
  24. #include <asm/mpic.h>
  25. #include <asm/ppc-pci.h>
  26. #include <asm/eeh.h>
  27. /* RTAS tokens */
  28. static int read_pci_config;
  29. static int write_pci_config;
  30. static int ibm_read_pci_config;
  31. static int ibm_write_pci_config;
  32. static inline int config_access_valid(struct pci_dn *dn, int where)
  33. {
  34. if (where < 256)
  35. return 1;
  36. if (where < 4096 && dn->pci_ext_config_space)
  37. return 1;
  38. return 0;
  39. }
  40. int rtas_read_config(struct pci_dn *pdn, int where, int size, u32 *val)
  41. {
  42. int returnval = -1;
  43. unsigned long buid, addr;
  44. int ret;
  45. if (!pdn)
  46. return PCIBIOS_DEVICE_NOT_FOUND;
  47. if (!config_access_valid(pdn, where))
  48. return PCIBIOS_BAD_REGISTER_NUMBER;
  49. #ifdef CONFIG_EEH
  50. if (pdn->edev && pdn->edev->pe &&
  51. (pdn->edev->pe->state & EEH_PE_CFG_BLOCKED))
  52. return PCIBIOS_SET_FAILED;
  53. #endif
  54. addr = rtas_config_addr(pdn->busno, pdn->devfn, where);
  55. buid = pdn->phb->buid;
  56. if (buid) {
  57. ret = rtas_call(ibm_read_pci_config, 4, 2, &returnval,
  58. addr, BUID_HI(buid), BUID_LO(buid), size);
  59. } else {
  60. ret = rtas_call(read_pci_config, 2, 2, &returnval, addr, size);
  61. }
  62. *val = returnval;
  63. if (ret)
  64. return PCIBIOS_DEVICE_NOT_FOUND;
  65. return PCIBIOS_SUCCESSFUL;
  66. }
  67. static int rtas_pci_read_config(struct pci_bus *bus,
  68. unsigned int devfn,
  69. int where, int size, u32 *val)
  70. {
  71. struct pci_dn *pdn;
  72. int ret;
  73. *val = 0xFFFFFFFF;
  74. pdn = pci_get_pdn_by_devfn(bus, devfn);
  75. /* Validity of pdn is checked in here */
  76. ret = rtas_read_config(pdn, where, size, val);
  77. if (*val == EEH_IO_ERROR_VALUE(size) &&
  78. eeh_dev_check_failure(pdn_to_eeh_dev(pdn)))
  79. return PCIBIOS_DEVICE_NOT_FOUND;
  80. return ret;
  81. }
  82. int rtas_write_config(struct pci_dn *pdn, int where, int size, u32 val)
  83. {
  84. unsigned long buid, addr;
  85. int ret;
  86. if (!pdn)
  87. return PCIBIOS_DEVICE_NOT_FOUND;
  88. if (!config_access_valid(pdn, where))
  89. return PCIBIOS_BAD_REGISTER_NUMBER;
  90. #ifdef CONFIG_EEH
  91. if (pdn->edev && pdn->edev->pe &&
  92. (pdn->edev->pe->state & EEH_PE_CFG_BLOCKED))
  93. return PCIBIOS_SET_FAILED;
  94. #endif
  95. addr = rtas_config_addr(pdn->busno, pdn->devfn, where);
  96. buid = pdn->phb->buid;
  97. if (buid) {
  98. ret = rtas_call(ibm_write_pci_config, 5, 1, NULL, addr,
  99. BUID_HI(buid), BUID_LO(buid), size, (ulong) val);
  100. } else {
  101. ret = rtas_call(write_pci_config, 3, 1, NULL, addr, size, (ulong)val);
  102. }
  103. if (ret)
  104. return PCIBIOS_DEVICE_NOT_FOUND;
  105. return PCIBIOS_SUCCESSFUL;
  106. }
  107. static int rtas_pci_write_config(struct pci_bus *bus,
  108. unsigned int devfn,
  109. int where, int size, u32 val)
  110. {
  111. struct pci_dn *pdn;
  112. pdn = pci_get_pdn_by_devfn(bus, devfn);
  113. /* Validity of pdn is checked in here. */
  114. return rtas_write_config(pdn, where, size, val);
  115. }
  116. static struct pci_ops rtas_pci_ops = {
  117. .read = rtas_pci_read_config,
  118. .write = rtas_pci_write_config,
  119. };
  120. static int is_python(struct device_node *dev)
  121. {
  122. const char *model = of_get_property(dev, "model", NULL);
  123. if (model && strstr(model, "Python"))
  124. return 1;
  125. return 0;
  126. }
  127. static void python_countermeasures(struct device_node *dev)
  128. {
  129. struct resource registers;
  130. void __iomem *chip_regs;
  131. volatile u32 val;
  132. if (of_address_to_resource(dev, 0, &registers)) {
  133. printk(KERN_ERR "Can't get address for Python workarounds !\n");
  134. return;
  135. }
  136. /* Python's register file is 1 MB in size. */
  137. chip_regs = ioremap(registers.start & ~(0xfffffUL), 0x100000);
  138. /*
  139. * Firmware doesn't always clear this bit which is critical
  140. * for good performance - Anton
  141. */
  142. #define PRG_CL_RESET_VALID 0x00010000
  143. val = in_be32(chip_regs + 0xf6030);
  144. if (val & PRG_CL_RESET_VALID) {
  145. printk(KERN_INFO "Python workaround: ");
  146. val &= ~PRG_CL_RESET_VALID;
  147. out_be32(chip_regs + 0xf6030, val);
  148. /*
  149. * We must read it back for changes to
  150. * take effect
  151. */
  152. val = in_be32(chip_regs + 0xf6030);
  153. printk("reg0: %x\n", val);
  154. }
  155. iounmap(chip_regs);
  156. }
  157. void __init init_pci_config_tokens(void)
  158. {
  159. read_pci_config = rtas_token("read-pci-config");
  160. write_pci_config = rtas_token("write-pci-config");
  161. ibm_read_pci_config = rtas_token("ibm,read-pci-config");
  162. ibm_write_pci_config = rtas_token("ibm,write-pci-config");
  163. }
  164. unsigned long get_phb_buid(struct device_node *phb)
  165. {
  166. struct resource r;
  167. if (ibm_read_pci_config == -1)
  168. return 0;
  169. if (of_address_to_resource(phb, 0, &r))
  170. return 0;
  171. return r.start;
  172. }
  173. static int phb_set_bus_ranges(struct device_node *dev,
  174. struct pci_controller *phb)
  175. {
  176. const __be32 *bus_range;
  177. unsigned int len;
  178. bus_range = of_get_property(dev, "bus-range", &len);
  179. if (bus_range == NULL || len < 2 * sizeof(int)) {
  180. return 1;
  181. }
  182. phb->first_busno = be32_to_cpu(bus_range[0]);
  183. phb->last_busno = be32_to_cpu(bus_range[1]);
  184. return 0;
  185. }
  186. int rtas_setup_phb(struct pci_controller *phb)
  187. {
  188. struct device_node *dev = phb->dn;
  189. if (is_python(dev))
  190. python_countermeasures(dev);
  191. if (phb_set_bus_ranges(dev, phb))
  192. return 1;
  193. phb->ops = &rtas_pci_ops;
  194. phb->buid = get_phb_buid(dev);
  195. return 0;
  196. }