You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pcimsix.c 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (C) 2019 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <errno.h>
  26. #include <assert.h>
  27. #include <ipxe/pci.h>
  28. #include <ipxe/pcimsix.h>
  29. /** @file
  30. *
  31. * PCI MSI-X interrupts
  32. *
  33. */
  34. /**
  35. * Get MSI-X descriptor name (for debugging)
  36. *
  37. * @v cfg Configuration space offset
  38. * @ret name Descriptor name
  39. */
  40. static const char * pci_msix_name ( unsigned int cfg ) {
  41. switch ( cfg ) {
  42. case PCI_MSIX_DESC_TABLE: return "table";
  43. case PCI_MSIX_DESC_PBA: return "PBA";
  44. default: return "<UNKNOWN>";
  45. }
  46. }
  47. /**
  48. * Map MSI-X BAR portion
  49. *
  50. * @v pci PCI device
  51. * @v msix MSI-X capability
  52. * @v cfg Configuration space offset
  53. * @ret io I/O address
  54. */
  55. static void * pci_msix_ioremap ( struct pci_device *pci, struct pci_msix *msix,
  56. unsigned int cfg ) {
  57. uint32_t desc;
  58. unsigned int bar;
  59. unsigned long start;
  60. unsigned long offset;
  61. unsigned long base;
  62. void *io;
  63. /* Read descriptor */
  64. pci_read_config_dword ( pci, ( msix->cap + cfg ), &desc );
  65. /* Get BAR */
  66. bar = PCI_MSIX_DESC_BIR ( desc );
  67. offset = PCI_MSIX_DESC_OFFSET ( desc );
  68. start = pci_bar_start ( pci, PCI_BASE_ADDRESS ( bar ) );
  69. if ( ! start ) {
  70. DBGC ( msix, "MSI-X %p %s could not find BAR%d\n",
  71. msix, pci_msix_name ( cfg ), bar );
  72. return NULL;
  73. }
  74. base = ( start + offset );
  75. DBGC ( msix, "MSI-X %p %s at %#08lx (BAR%d+%#lx)\n",
  76. msix, pci_msix_name ( cfg ), base, bar, offset );
  77. /* Map BAR portion */
  78. io = ioremap ( ( start + offset ), PCI_MSIX_LEN );
  79. if ( ! io ) {
  80. DBGC ( msix, "MSI-X %p %s could not map %#08lx\n",
  81. msix, pci_msix_name ( cfg ), base );
  82. return NULL;
  83. }
  84. return io;
  85. }
  86. /**
  87. * Enable MSI-X interrupts
  88. *
  89. * @v pci PCI device
  90. * @v msix MSI-X capability
  91. * @ret rc Return status code
  92. */
  93. int pci_msix_enable ( struct pci_device *pci, struct pci_msix *msix ) {
  94. uint16_t ctrl;
  95. int rc;
  96. /* Locate capability */
  97. msix->cap = pci_find_capability ( pci, PCI_CAP_ID_MSIX );
  98. if ( ! msix->cap ) {
  99. DBGC ( msix, "MSI-X %p found no MSI-X capability in "
  100. PCI_FMT "\n", msix, PCI_ARGS ( pci ) );
  101. rc = -ENOENT;
  102. goto err_cap;
  103. }
  104. /* Extract interrupt count */
  105. pci_read_config_word ( pci, ( msix->cap + PCI_MSIX_CTRL ), &ctrl );
  106. msix->count = ( PCI_MSIX_CTRL_SIZE ( ctrl ) + 1 );
  107. DBGC ( msix, "MSI-X %p has %d vectors for " PCI_FMT "\n",
  108. msix, msix->count, PCI_ARGS ( pci ) );
  109. /* Map MSI-X table */
  110. msix->table = pci_msix_ioremap ( pci, msix, PCI_MSIX_DESC_TABLE );
  111. if ( ! msix->table ) {
  112. rc = -ENOENT;
  113. goto err_table;
  114. }
  115. /* Map pending bit array */
  116. msix->pba = pci_msix_ioremap ( pci, msix, PCI_MSIX_DESC_PBA );
  117. if ( ! msix->pba ) {
  118. rc = -ENOENT;
  119. goto err_pba;
  120. }
  121. /* Enable MSI-X */
  122. ctrl &= ~PCI_MSIX_CTRL_MASK;
  123. ctrl |= PCI_MSIX_CTRL_ENABLE;
  124. pci_write_config_word ( pci, ( msix->cap + PCI_MSIX_CTRL ), ctrl );
  125. return 0;
  126. iounmap ( msix->pba );
  127. err_pba:
  128. iounmap ( msix->table );
  129. err_table:
  130. err_cap:
  131. return rc;
  132. }
  133. /**
  134. * Disable MSI-X interrupts
  135. *
  136. * @v pci PCI device
  137. * @v msix MSI-X capability
  138. */
  139. void pci_msix_disable ( struct pci_device *pci, struct pci_msix *msix ) {
  140. uint16_t ctrl;
  141. /* Disable MSI-X */
  142. pci_read_config_word ( pci, ( msix->cap + PCI_MSIX_CTRL ), &ctrl );
  143. ctrl &= ~PCI_MSIX_CTRL_ENABLE;
  144. pci_write_config_word ( pci, ( msix->cap + PCI_MSIX_CTRL ), ctrl );
  145. /* Unmap pending bit array */
  146. iounmap ( msix->pba );
  147. /* Unmap MSI-X table */
  148. iounmap ( msix->table );
  149. }
  150. /**
  151. * Map MSI-X interrupt vector
  152. *
  153. * @v msix MSI-X capability
  154. * @v vector MSI-X vector
  155. * @v address Message address
  156. * @v data Message data
  157. */
  158. void pci_msix_map ( struct pci_msix *msix, unsigned int vector,
  159. physaddr_t address, uint32_t data ) {
  160. void *base;
  161. /* Sanity check */
  162. assert ( vector < msix->count );
  163. /* Map interrupt vector */
  164. base = ( msix->table + PCI_MSIX_VECTOR ( vector ) );
  165. writel ( ( address & 0xffffffffUL ), ( base + PCI_MSIX_ADDRESS_LO ) );
  166. if ( sizeof ( address ) > sizeof ( uint32_t ) ) {
  167. writel ( ( ( ( uint64_t ) address ) >> 32 ),
  168. ( base + PCI_MSIX_ADDRESS_HI ) );
  169. } else {
  170. writel ( 0, ( base + PCI_MSIX_ADDRESS_HI ) );
  171. }
  172. writel ( data, ( base + PCI_MSIX_DATA ) );
  173. }
  174. /**
  175. * Control MSI-X interrupt vector
  176. *
  177. * @v msix MSI-X capability
  178. * @v vector MSI-X vector
  179. * @v mask Control mask
  180. */
  181. void pci_msix_control ( struct pci_msix *msix, unsigned int vector,
  182. uint32_t mask ) {
  183. void *base;
  184. uint32_t ctrl;
  185. /* Mask/unmask interrupt vector */
  186. base = ( msix->table + PCI_MSIX_VECTOR ( vector ) );
  187. ctrl = readl ( base + PCI_MSIX_CONTROL );
  188. ctrl &= ~PCI_MSIX_CONTROL_MASK;
  189. ctrl |= mask;
  190. writel ( ctrl, ( base + PCI_MSIX_CONTROL ) );
  191. }
  192. /**
  193. * Dump MSI-X interrupt state (for debugging)
  194. *
  195. * @v msix MSI-X capability
  196. * @v vector MSI-X vector
  197. */
  198. void pci_msix_dump ( struct pci_msix *msix, unsigned int vector ) {
  199. void *base;
  200. uint32_t address_hi;
  201. uint32_t address_lo;
  202. physaddr_t address;
  203. uint32_t data;
  204. uint32_t ctrl;
  205. uint32_t pba;
  206. /* Do nothing in non-debug builds */
  207. if ( ! DBG_LOG )
  208. return;
  209. /* Mask/unmask interrupt vector */
  210. base = ( msix->table + PCI_MSIX_VECTOR ( vector ) );
  211. address_hi = readl ( base + PCI_MSIX_ADDRESS_HI );
  212. address_lo = readl ( base + PCI_MSIX_ADDRESS_LO );
  213. data = readl ( base + PCI_MSIX_DATA );
  214. ctrl = readl ( base + PCI_MSIX_CONTROL );
  215. pba = readl ( msix->pba );
  216. address = ( ( ( ( uint64_t ) address_hi ) << 32 ) | address_lo );
  217. DBGC ( msix, "MSI-X %p vector %d %#08x => %#08lx%s%s\n",
  218. msix, vector, data, address,
  219. ( ( ctrl & PCI_MSIX_CONTROL_MASK ) ? " (masked)" : "" ),
  220. ( ( pba & ( 1 << vector ) ) ? " (pending)" : "" ) );
  221. }