conf_space_capability.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * PCI Backend - Handles the virtual fields found on the capability lists
  3. * in the configuration space.
  4. *
  5. * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/pci.h>
  9. #include "pciback.h"
  10. #include "conf_space.h"
  11. static LIST_HEAD(capabilities);
  12. struct xen_pcibk_config_capability {
  13. struct list_head cap_list;
  14. int capability;
  15. /* If the device has the capability found above, add these fields */
  16. const struct config_field *fields;
  17. };
  18. static const struct config_field caplist_header[] = {
  19. {
  20. .offset = PCI_CAP_LIST_ID,
  21. .size = 2, /* encompass PCI_CAP_LIST_ID & PCI_CAP_LIST_NEXT */
  22. .u.w.read = xen_pcibk_read_config_word,
  23. .u.w.write = NULL,
  24. },
  25. {}
  26. };
  27. static inline void register_capability(struct xen_pcibk_config_capability *cap)
  28. {
  29. list_add_tail(&cap->cap_list, &capabilities);
  30. }
  31. int xen_pcibk_config_capability_add_fields(struct pci_dev *dev)
  32. {
  33. int err = 0;
  34. struct xen_pcibk_config_capability *cap;
  35. int cap_offset;
  36. list_for_each_entry(cap, &capabilities, cap_list) {
  37. cap_offset = pci_find_capability(dev, cap->capability);
  38. if (cap_offset) {
  39. dev_dbg(&dev->dev, "Found capability 0x%x at 0x%x\n",
  40. cap->capability, cap_offset);
  41. err = xen_pcibk_config_add_fields_offset(dev,
  42. caplist_header,
  43. cap_offset);
  44. if (err)
  45. goto out;
  46. err = xen_pcibk_config_add_fields_offset(dev,
  47. cap->fields,
  48. cap_offset);
  49. if (err)
  50. goto out;
  51. }
  52. }
  53. out:
  54. return err;
  55. }
  56. static int vpd_address_write(struct pci_dev *dev, int offset, u16 value,
  57. void *data)
  58. {
  59. /* Disallow writes to the vital product data */
  60. if (value & PCI_VPD_ADDR_F)
  61. return PCIBIOS_SET_FAILED;
  62. else
  63. return pci_write_config_word(dev, offset, value);
  64. }
  65. static const struct config_field caplist_vpd[] = {
  66. {
  67. .offset = PCI_VPD_ADDR,
  68. .size = 2,
  69. .u.w.read = xen_pcibk_read_config_word,
  70. .u.w.write = vpd_address_write,
  71. },
  72. {
  73. .offset = PCI_VPD_DATA,
  74. .size = 4,
  75. .u.dw.read = xen_pcibk_read_config_dword,
  76. .u.dw.write = NULL,
  77. },
  78. {}
  79. };
  80. static int pm_caps_read(struct pci_dev *dev, int offset, u16 *value,
  81. void *data)
  82. {
  83. int err;
  84. u16 real_value;
  85. err = pci_read_config_word(dev, offset, &real_value);
  86. if (err)
  87. goto out;
  88. *value = real_value & ~PCI_PM_CAP_PME_MASK;
  89. out:
  90. return err;
  91. }
  92. /* PM_OK_BITS specifies the bits that the driver domain is allowed to change.
  93. * Can't allow driver domain to enable PMEs - they're shared */
  94. #define PM_OK_BITS (PCI_PM_CTRL_PME_STATUS|PCI_PM_CTRL_DATA_SEL_MASK)
  95. static int pm_ctrl_write(struct pci_dev *dev, int offset, u16 new_value,
  96. void *data)
  97. {
  98. int err;
  99. u16 old_value;
  100. pci_power_t new_state, old_state;
  101. err = pci_read_config_word(dev, offset, &old_value);
  102. if (err)
  103. goto out;
  104. old_state = (pci_power_t)(old_value & PCI_PM_CTRL_STATE_MASK);
  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. }