conf_space_capability.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCI Backend - Handles the virtual fields found on the capability lists
  4. * in the configuration space.
  5. *
  6. * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/pci.h>
  10. #include "pciback.h"
  11. #include "conf_space.h"
  12. static LIST_HEAD(capabilities);
  13. struct xen_pcibk_config_capability {
  14. struct list_head cap_list;
  15. int capability;
  16. /* If the device has the capability found above, add these fields */
  17. const struct config_field *fields;
  18. };
  19. static const struct config_field caplist_header[] = {
  20. {
  21. .offset = PCI_CAP_LIST_ID,
  22. .size = 2, /* encompass PCI_CAP_LIST_ID & PCI_CAP_LIST_NEXT */
  23. .u.w.read = xen_pcibk_read_config_word,
  24. .u.w.write = NULL,
  25. },
  26. {}
  27. };
  28. static inline void register_capability(struct xen_pcibk_config_capability *cap)
  29. {
  30. list_add_tail(&cap->cap_list, &capabilities);
  31. }
  32. int xen_pcibk_config_capability_add_fields(struct pci_dev *dev)
  33. {
  34. int err = 0;
  35. struct xen_pcibk_config_capability *cap;
  36. int cap_offset;
  37. list_for_each_entry(cap, &capabilities, cap_list) {
  38. cap_offset = pci_find_capability(dev, cap->capability);
  39. if (cap_offset) {
  40. dev_dbg(&dev->dev, "Found capability 0x%x at 0x%x\n",
  41. cap->capability, cap_offset);
  42. err = xen_pcibk_config_add_fields_offset(dev,
  43. caplist_header,
  44. cap_offset);
  45. if (err)
  46. goto out;
  47. err = xen_pcibk_config_add_fields_offset(dev,
  48. cap->fields,
  49. cap_offset);
  50. if (err)
  51. goto out;
  52. }
  53. }
  54. out:
  55. return err;
  56. }
  57. static int vpd_address_write(struct pci_dev *dev, int offset, u16 value,
  58. void *data)
  59. {
  60. /* Disallow writes to the vital product data */
  61. if (value & PCI_VPD_ADDR_F)
  62. return PCIBIOS_SET_FAILED;
  63. else
  64. return pci_write_config_word(dev, offset, value);
  65. }
  66. static const struct config_field caplist_vpd[] = {
  67. {
  68. .offset = PCI_VPD_ADDR,
  69. .size = 2,
  70. .u.w.read = xen_pcibk_read_config_word,
  71. .u.w.write = vpd_address_write,
  72. },
  73. {
  74. .offset = PCI_VPD_DATA,
  75. .size = 4,
  76. .u.dw.read = xen_pcibk_read_config_dword,
  77. .u.dw.write = NULL,
  78. },
  79. {}
  80. };
  81. static int pm_caps_read(struct pci_dev *dev, int offset, u16 *value,
  82. void *data)
  83. {
  84. int err;
  85. u16 real_value;
  86. err = pci_read_config_word(dev, offset, &real_value);
  87. if (err)
  88. goto out;
  89. *value = real_value & ~PCI_PM_CAP_PME_MASK;
  90. out:
  91. return err;
  92. }
  93. /* PM_OK_BITS specifies the bits that the driver domain is allowed to change.
  94. * Can't allow driver domain to enable PMEs - they're shared */
  95. #define PM_OK_BITS (PCI_PM_CTRL_PME_STATUS|PCI_PM_CTRL_DATA_SEL_MASK)
  96. static int pm_ctrl_write(struct pci_dev *dev, int offset, u16 new_value,
  97. void *data)
  98. {
  99. int err;
  100. u16 old_value;
  101. pci_power_t new_state;
  102. err = pci_read_config_word(dev, offset, &old_value);
  103. if (err)
  104. goto out;
  105. new_state = (pci_power_t)(new_value & PCI_PM_CTRL_STATE_MASK);
  106. new_value &= PM_OK_BITS;
  107. if ((old_value & PM_OK_BITS) != new_value) {
  108. new_value = (old_value & ~PM_OK_BITS) | new_value;
  109. err = pci_write_config_word(dev, offset, new_value);
  110. if (err)
  111. goto out;
  112. }
  113. /* Let pci core handle the power management change */
  114. dev_dbg(&dev->dev, "set power state to %x\n", new_state);
  115. err = pci_set_power_state(dev, new_state);
  116. if (err) {
  117. err = PCIBIOS_SET_FAILED;
  118. goto out;
  119. }
  120. out:
  121. return err;
  122. }
  123. /* Ensure PMEs are disabled */
  124. static void *pm_ctrl_init(struct pci_dev *dev, int offset)
  125. {
  126. int err;
  127. u16 value;
  128. err = pci_read_config_word(dev, offset, &value);
  129. if (err)
  130. goto out;
  131. if (value & PCI_PM_CTRL_PME_ENABLE) {
  132. value &= ~PCI_PM_CTRL_PME_ENABLE;
  133. err = pci_write_config_word(dev, offset, value);
  134. }
  135. out:
  136. return ERR_PTR(err);
  137. }
  138. static const struct config_field caplist_pm[] = {
  139. {
  140. .offset = PCI_PM_PMC,
  141. .size = 2,
  142. .u.w.read = pm_caps_read,
  143. },
  144. {
  145. .offset = PCI_PM_CTRL,
  146. .size = 2,
  147. .init = pm_ctrl_init,
  148. .u.w.read = xen_pcibk_read_config_word,
  149. .u.w.write = pm_ctrl_write,
  150. },
  151. {
  152. .offset = PCI_PM_PPB_EXTENSIONS,
  153. .size = 1,
  154. .u.b.read = xen_pcibk_read_config_byte,
  155. },
  156. {
  157. .offset = PCI_PM_DATA_REGISTER,
  158. .size = 1,
  159. .u.b.read = xen_pcibk_read_config_byte,
  160. },
  161. {}
  162. };
  163. static struct xen_pcibk_config_capability xen_pcibk_config_capability_pm = {
  164. .capability = PCI_CAP_ID_PM,
  165. .fields = caplist_pm,
  166. };
  167. static struct xen_pcibk_config_capability xen_pcibk_config_capability_vpd = {
  168. .capability = PCI_CAP_ID_VPD,
  169. .fields = caplist_vpd,
  170. };
  171. int xen_pcibk_config_capability_init(void)
  172. {
  173. register_capability(&xen_pcibk_config_capability_vpd);
  174. register_capability(&xen_pcibk_config_capability_pm);
  175. return 0;
  176. }