conf_space_capability.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 <[email protected]>
  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 ? ERR_PTR(err) : NULL;
  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 msi_msix_field_config {
  164. u16 enable_bit; /* bit for enabling MSI/MSI-X */
  165. u16 allowed_bits; /* bits allowed to be changed */
  166. unsigned int int_type; /* interrupt type for exclusiveness check */
  167. } msi_field_config = {
  168. .enable_bit = PCI_MSI_FLAGS_ENABLE,
  169. .allowed_bits = PCI_MSI_FLAGS_ENABLE,
  170. .int_type = INTERRUPT_TYPE_MSI,
  171. }, msix_field_config = {
  172. .enable_bit = PCI_MSIX_FLAGS_ENABLE,
  173. .allowed_bits = PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL,
  174. .int_type = INTERRUPT_TYPE_MSIX,
  175. };
  176. static void *msi_field_init(struct pci_dev *dev, int offset)
  177. {
  178. return &msi_field_config;
  179. }
  180. static void *msix_field_init(struct pci_dev *dev, int offset)
  181. {
  182. return &msix_field_config;
  183. }
  184. static int msi_msix_flags_write(struct pci_dev *dev, int offset, u16 new_value,
  185. void *data)
  186. {
  187. int err;
  188. u16 old_value;
  189. const struct msi_msix_field_config *field_config = data;
  190. const struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
  191. if (xen_pcibk_permissive || dev_data->permissive)
  192. goto write;
  193. err = pci_read_config_word(dev, offset, &old_value);
  194. if (err)
  195. return err;
  196. if (new_value == old_value)
  197. return 0;
  198. if (!dev_data->allow_interrupt_control ||
  199. (new_value ^ old_value) & ~field_config->allowed_bits)
  200. return PCIBIOS_SET_FAILED;
  201. if (new_value & field_config->enable_bit) {
  202. /*
  203. * Don't allow enabling together with other interrupt type, but do
  204. * allow enabling MSI(-X) while INTx is still active to please Linuxes
  205. * MSI(-X) startup sequence. It is safe to do, as according to PCI
  206. * spec, device with enabled MSI(-X) shouldn't use INTx.
  207. */
  208. int int_type = xen_pcibk_get_interrupt_type(dev);
  209. if (int_type == INTERRUPT_TYPE_NONE ||
  210. int_type == INTERRUPT_TYPE_INTX ||
  211. int_type == field_config->int_type)
  212. goto write;
  213. return PCIBIOS_SET_FAILED;
  214. }
  215. write:
  216. return pci_write_config_word(dev, offset, new_value);
  217. }
  218. static const struct config_field caplist_msix[] = {
  219. {
  220. .offset = PCI_MSIX_FLAGS,
  221. .size = 2,
  222. .init = msix_field_init,
  223. .u.w.read = xen_pcibk_read_config_word,
  224. .u.w.write = msi_msix_flags_write,
  225. },
  226. {}
  227. };
  228. static const struct config_field caplist_msi[] = {
  229. {
  230. .offset = PCI_MSI_FLAGS,
  231. .size = 2,
  232. .init = msi_field_init,
  233. .u.w.read = xen_pcibk_read_config_word,
  234. .u.w.write = msi_msix_flags_write,
  235. },
  236. {}
  237. };
  238. static struct xen_pcibk_config_capability xen_pcibk_config_capability_pm = {
  239. .capability = PCI_CAP_ID_PM,
  240. .fields = caplist_pm,
  241. };
  242. static struct xen_pcibk_config_capability xen_pcibk_config_capability_vpd = {
  243. .capability = PCI_CAP_ID_VPD,
  244. .fields = caplist_vpd,
  245. };
  246. static struct xen_pcibk_config_capability xen_pcibk_config_capability_msi = {
  247. .capability = PCI_CAP_ID_MSI,
  248. .fields = caplist_msi,
  249. };
  250. static struct xen_pcibk_config_capability xen_pcibk_config_capability_msix = {
  251. .capability = PCI_CAP_ID_MSIX,
  252. .fields = caplist_msix,
  253. };
  254. int xen_pcibk_config_capability_init(void)
  255. {
  256. register_capability(&xen_pcibk_config_capability_vpd);
  257. register_capability(&xen_pcibk_config_capability_pm);
  258. register_capability(&xen_pcibk_config_capability_msi);
  259. register_capability(&xen_pcibk_config_capability_msix);
  260. return 0;
  261. }