pcihp_slot.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (C) 1995,2001 Compaq Computer Corporation
  3. * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
  4. * Copyright (C) 2001 IBM Corp.
  5. * Copyright (C) 2003-2004 Intel Corporation
  6. * (c) Copyright 2009 Hewlett-Packard Development Company, L.P.
  7. *
  8. * All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or (at
  13. * your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  18. * NON INFRINGEMENT. See the GNU General Public License for more
  19. * details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #include <linux/pci.h>
  26. #include <linux/pci_hotplug.h>
  27. static struct hpp_type0 pci_default_type0 = {
  28. .revision = 1,
  29. .cache_line_size = 8,
  30. .latency_timer = 0x40,
  31. .enable_serr = 0,
  32. .enable_perr = 0,
  33. };
  34. static void program_hpp_type0(struct pci_dev *dev, struct hpp_type0 *hpp)
  35. {
  36. u16 pci_cmd, pci_bctl;
  37. if (!hpp) {
  38. /*
  39. * Perhaps we *should* use default settings for PCIe, but
  40. * pciehp didn't, so we won't either.
  41. */
  42. if (pci_is_pcie(dev))
  43. return;
  44. dev_info(&dev->dev, "using default PCI settings\n");
  45. hpp = &pci_default_type0;
  46. }
  47. if (hpp->revision > 1) {
  48. dev_warn(&dev->dev,
  49. "PCI settings rev %d not supported; using defaults\n",
  50. hpp->revision);
  51. hpp = &pci_default_type0;
  52. }
  53. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, hpp->cache_line_size);
  54. pci_write_config_byte(dev, PCI_LATENCY_TIMER, hpp->latency_timer);
  55. pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
  56. if (hpp->enable_serr)
  57. pci_cmd |= PCI_COMMAND_SERR;
  58. else
  59. pci_cmd &= ~PCI_COMMAND_SERR;
  60. if (hpp->enable_perr)
  61. pci_cmd |= PCI_COMMAND_PARITY;
  62. else
  63. pci_cmd &= ~PCI_COMMAND_PARITY;
  64. pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
  65. /* Program bridge control value */
  66. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  67. pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
  68. hpp->latency_timer);
  69. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
  70. if (hpp->enable_serr)
  71. pci_bctl |= PCI_BRIDGE_CTL_SERR;
  72. else
  73. pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
  74. if (hpp->enable_perr)
  75. pci_bctl |= PCI_BRIDGE_CTL_PARITY;
  76. else
  77. pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
  78. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
  79. }
  80. }
  81. static void program_hpp_type1(struct pci_dev *dev, struct hpp_type1 *hpp)
  82. {
  83. if (hpp)
  84. dev_warn(&dev->dev, "PCI-X settings not supported\n");
  85. }
  86. static void program_hpp_type2(struct pci_dev *dev, struct hpp_type2 *hpp)
  87. {
  88. int pos;
  89. u16 reg16;
  90. u32 reg32;
  91. if (!hpp)
  92. return;
  93. /* Find PCI Express capability */
  94. pos = pci_pcie_cap(dev);
  95. if (!pos)
  96. return;
  97. if (hpp->revision > 1) {
  98. dev_warn(&dev->dev, "PCIe settings rev %d not supported\n",
  99. hpp->revision);
  100. return;
  101. }
  102. /* Initialize Device Control Register */
  103. pci_read_config_word(dev, pos + PCI_EXP_DEVCTL, &reg16);
  104. reg16 = (reg16 & hpp->pci_exp_devctl_and) | hpp->pci_exp_devctl_or;
  105. pci_write_config_word(dev, pos + PCI_EXP_DEVCTL, reg16);
  106. /* Initialize Link Control Register */
  107. if (dev->subordinate) {
  108. pci_read_config_word(dev, pos + PCI_EXP_LNKCTL, &reg16);
  109. reg16 = (reg16 & hpp->pci_exp_lnkctl_and)
  110. | hpp->pci_exp_lnkctl_or;
  111. pci_write_config_word(dev, pos + PCI_EXP_LNKCTL, reg16);
  112. }
  113. /* Find Advanced Error Reporting Enhanced Capability */
  114. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  115. if (!pos)
  116. return;
  117. /* Initialize Uncorrectable Error Mask Register */
  118. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &reg32);
  119. reg32 = (reg32 & hpp->unc_err_mask_and) | hpp->unc_err_mask_or;
  120. pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, reg32);
  121. /* Initialize Uncorrectable Error Severity Register */
  122. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &reg32);
  123. reg32 = (reg32 & hpp->unc_err_sever_and) | hpp->unc_err_sever_or;
  124. pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, reg32);
  125. /* Initialize Correctable Error Mask Register */
  126. pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &reg32);
  127. reg32 = (reg32 & hpp->cor_err_mask_and) | hpp->cor_err_mask_or;
  128. pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg32);
  129. /* Initialize Advanced Error Capabilities and Control Register */
  130. pci_read_config_dword(dev, pos + PCI_ERR_CAP, &reg32);
  131. reg32 = (reg32 & hpp->adv_err_cap_and) | hpp->adv_err_cap_or;
  132. pci_write_config_dword(dev, pos + PCI_ERR_CAP, reg32);
  133. /*
  134. * FIXME: The following two registers are not supported yet.
  135. *
  136. * o Secondary Uncorrectable Error Severity Register
  137. * o Secondary Uncorrectable Error Mask Register
  138. */
  139. }
  140. /* Program PCIE MaxPayload setting on device: ensure parent maxpayload <= device */
  141. static int pci_set_payload(struct pci_dev *dev)
  142. {
  143. int pos, ppos;
  144. u16 pctl, psz;
  145. u16 dctl, dsz, dcap, dmax;
  146. struct pci_dev *parent;
  147. parent = dev->bus->self;
  148. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  149. if (!pos)
  150. return 0;
  151. /* Read Device MaxPayload capability and setting */
  152. pci_read_config_word(dev, pos + PCI_EXP_DEVCTL, &dctl);
  153. pci_read_config_word(dev, pos + PCI_EXP_DEVCAP, &dcap);
  154. dsz = (dctl & PCI_EXP_DEVCTL_PAYLOAD) >> 5;
  155. dmax = (dcap & PCI_EXP_DEVCAP_PAYLOAD);
  156. /* Read Parent MaxPayload setting */
  157. ppos = pci_find_capability(parent, PCI_CAP_ID_EXP);
  158. if (!ppos)
  159. return 0;
  160. pci_read_config_word(parent, ppos + PCI_EXP_DEVCTL, &pctl);
  161. psz = (pctl & PCI_EXP_DEVCTL_PAYLOAD) >> 5;
  162. /* If parent payload > device max payload -> error
  163. * If parent payload > device payload -> set speed
  164. * If parent payload <= device payload -> do nothing
  165. */
  166. if (psz > dmax)
  167. return -1;
  168. else if (psz > dsz) {
  169. dev_info(&dev->dev, "Setting MaxPayload to %d\n", 128 << psz);
  170. pci_write_config_word(dev, pos + PCI_EXP_DEVCTL,
  171. (dctl & ~PCI_EXP_DEVCTL_PAYLOAD) +
  172. (psz << 5));
  173. }
  174. return 0;
  175. }
  176. void pci_configure_slot(struct pci_dev *dev)
  177. {
  178. struct pci_dev *cdev;
  179. struct hotplug_params hpp;
  180. int ret;
  181. if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
  182. (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
  183. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
  184. return;
  185. ret = pci_set_payload(dev);
  186. if (ret)
  187. dev_warn(&dev->dev, "could not set device max payload\n");
  188. memset(&hpp, 0, sizeof(hpp));
  189. ret = pci_get_hp_params(dev, &hpp);
  190. if (ret)
  191. dev_warn(&dev->dev, "no hotplug settings from platform\n");
  192. program_hpp_type2(dev, hpp.t2);
  193. program_hpp_type1(dev, hpp.t1);
  194. program_hpp_type0(dev, hpp.t0);
  195. if (dev->subordinate) {
  196. list_for_each_entry(cdev, &dev->subordinate->devices,
  197. bus_list)
  198. pci_configure_slot(cdev);
  199. }
  200. }
  201. EXPORT_SYMBOL_GPL(pci_configure_slot);