amd_pcie_helpers.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2015 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #ifndef __AMD_PCIE_HELPERS_H__
  23. #define __AMD_PCIE_HELPERS_H__
  24. #include "amd_pcie.h"
  25. static inline bool is_pcie_gen3_supported(uint32_t pcie_link_speed_cap)
  26. {
  27. if (pcie_link_speed_cap & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3)
  28. return true;
  29. return false;
  30. }
  31. static inline bool is_pcie_gen2_supported(uint32_t pcie_link_speed_cap)
  32. {
  33. if (pcie_link_speed_cap & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2)
  34. return true;
  35. return false;
  36. }
  37. /* Get the new PCIE speed given the ASIC PCIE Cap and the NewState's requested PCIE speed*/
  38. static inline uint16_t get_pcie_gen_support(uint32_t pcie_link_speed_cap,
  39. uint16_t ns_pcie_gen)
  40. {
  41. uint32_t asic_pcie_link_speed_cap = (pcie_link_speed_cap &
  42. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_MASK);
  43. uint32_t sys_pcie_link_speed_cap = (pcie_link_speed_cap &
  44. CAIL_PCIE_LINK_SPEED_SUPPORT_MASK);
  45. switch (asic_pcie_link_speed_cap) {
  46. case CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1:
  47. return PP_PCIEGen1;
  48. case CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2:
  49. return PP_PCIEGen2;
  50. case CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3:
  51. return PP_PCIEGen3;
  52. default:
  53. if (is_pcie_gen3_supported(sys_pcie_link_speed_cap) &&
  54. (ns_pcie_gen == PP_PCIEGen3)) {
  55. return PP_PCIEGen3;
  56. } else if (is_pcie_gen2_supported(sys_pcie_link_speed_cap) &&
  57. ((ns_pcie_gen == PP_PCIEGen3) || (ns_pcie_gen == PP_PCIEGen2))) {
  58. return PP_PCIEGen2;
  59. }
  60. }
  61. return PP_PCIEGen1;
  62. }
  63. static inline uint16_t get_pcie_lane_support(uint32_t pcie_lane_width_cap,
  64. uint16_t ns_pcie_lanes)
  65. {
  66. int i, j;
  67. uint16_t new_pcie_lanes = ns_pcie_lanes;
  68. uint16_t pcie_lanes[7] = {1, 2, 4, 8, 12, 16, 32};
  69. switch (pcie_lane_width_cap) {
  70. case 0:
  71. pr_err("No valid PCIE lane width reported\n");
  72. break;
  73. case CAIL_PCIE_LINK_WIDTH_SUPPORT_X1:
  74. new_pcie_lanes = 1;
  75. break;
  76. case CAIL_PCIE_LINK_WIDTH_SUPPORT_X2:
  77. new_pcie_lanes = 2;
  78. break;
  79. case CAIL_PCIE_LINK_WIDTH_SUPPORT_X4:
  80. new_pcie_lanes = 4;
  81. break;
  82. case CAIL_PCIE_LINK_WIDTH_SUPPORT_X8:
  83. new_pcie_lanes = 8;
  84. break;
  85. case CAIL_PCIE_LINK_WIDTH_SUPPORT_X12:
  86. new_pcie_lanes = 12;
  87. break;
  88. case CAIL_PCIE_LINK_WIDTH_SUPPORT_X16:
  89. new_pcie_lanes = 16;
  90. break;
  91. case CAIL_PCIE_LINK_WIDTH_SUPPORT_X32:
  92. new_pcie_lanes = 32;
  93. break;
  94. default:
  95. for (i = 0; i < 7; i++) {
  96. if (ns_pcie_lanes == pcie_lanes[i]) {
  97. if (pcie_lane_width_cap & (0x10000 << i)) {
  98. break;
  99. } else {
  100. for (j = i - 1; j >= 0; j--) {
  101. if (pcie_lane_width_cap & (0x10000 << j)) {
  102. new_pcie_lanes = pcie_lanes[j];
  103. break;
  104. }
  105. }
  106. if (j < 0) {
  107. for (j = i + 1; j < 7; j++) {
  108. if (pcie_lane_width_cap & (0x10000 << j)) {
  109. new_pcie_lanes = pcie_lanes[j];
  110. break;
  111. }
  112. }
  113. if (j > 7)
  114. pr_err("Cannot find a valid PCIE lane width!\n");
  115. }
  116. }
  117. break;
  118. }
  119. }
  120. break;
  121. }
  122. return new_pcie_lanes;
  123. }
  124. #endif