msi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * msi.c
  3. *
  4. * Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp>
  5. * VA Linux Systems Japan K.K.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "msi.h"
  19. #include "range.h"
  20. /* Eventually those constants should go to Linux pci_regs.h */
  21. #define PCI_MSI_PENDING_32 0x10
  22. #define PCI_MSI_PENDING_64 0x14
  23. /* PCI_MSI_ADDRESS_LO */
  24. #define PCI_MSI_ADDRESS_LO_MASK (~0x3)
  25. /* If we get rid of cap allocator, we won't need those. */
  26. #define PCI_MSI_32_SIZEOF 0x0a
  27. #define PCI_MSI_64_SIZEOF 0x0e
  28. #define PCI_MSI_32M_SIZEOF 0x14
  29. #define PCI_MSI_64M_SIZEOF 0x18
  30. #define PCI_MSI_VECTORS_MAX 32
  31. /* Flag for interrupt controller to declare MSI/MSI-X support */
  32. bool msi_supported;
  33. /* If we get rid of cap allocator, we won't need this. */
  34. static inline uint8_t msi_cap_sizeof(uint16_t flags)
  35. {
  36. switch (flags & (PCI_MSI_FLAGS_MASKBIT | PCI_MSI_FLAGS_64BIT)) {
  37. case PCI_MSI_FLAGS_MASKBIT | PCI_MSI_FLAGS_64BIT:
  38. return PCI_MSI_64M_SIZEOF;
  39. case PCI_MSI_FLAGS_64BIT:
  40. return PCI_MSI_64_SIZEOF;
  41. case PCI_MSI_FLAGS_MASKBIT:
  42. return PCI_MSI_32M_SIZEOF;
  43. case 0:
  44. return PCI_MSI_32_SIZEOF;
  45. default:
  46. abort();
  47. break;
  48. }
  49. return 0;
  50. }
  51. //#define MSI_DEBUG
  52. #ifdef MSI_DEBUG
  53. # define MSI_DPRINTF(fmt, ...) \
  54. fprintf(stderr, "%s:%d " fmt, __func__, __LINE__, ## __VA_ARGS__)
  55. #else
  56. # define MSI_DPRINTF(fmt, ...) do { } while (0)
  57. #endif
  58. #define MSI_DEV_PRINTF(dev, fmt, ...) \
  59. MSI_DPRINTF("%s:%x " fmt, (dev)->name, (dev)->devfn, ## __VA_ARGS__)
  60. static inline unsigned int msi_nr_vectors(uint16_t flags)
  61. {
  62. return 1U <<
  63. ((flags & PCI_MSI_FLAGS_QSIZE) >> (ffs(PCI_MSI_FLAGS_QSIZE) - 1));
  64. }
  65. static inline uint8_t msi_flags_off(const PCIDevice* dev)
  66. {
  67. return dev->msi_cap + PCI_MSI_FLAGS;
  68. }
  69. static inline uint8_t msi_address_lo_off(const PCIDevice* dev)
  70. {
  71. return dev->msi_cap + PCI_MSI_ADDRESS_LO;
  72. }
  73. static inline uint8_t msi_address_hi_off(const PCIDevice* dev)
  74. {
  75. return dev->msi_cap + PCI_MSI_ADDRESS_HI;
  76. }
  77. static inline uint8_t msi_data_off(const PCIDevice* dev, bool msi64bit)
  78. {
  79. return dev->msi_cap + (msi64bit ? PCI_MSI_DATA_64 : PCI_MSI_DATA_32);
  80. }
  81. static inline uint8_t msi_mask_off(const PCIDevice* dev, bool msi64bit)
  82. {
  83. return dev->msi_cap + (msi64bit ? PCI_MSI_MASK_64 : PCI_MSI_MASK_32);
  84. }
  85. static inline uint8_t msi_pending_off(const PCIDevice* dev, bool msi64bit)
  86. {
  87. return dev->msi_cap + (msi64bit ? PCI_MSI_PENDING_64 : PCI_MSI_PENDING_32);
  88. }
  89. /*
  90. * Special API for POWER to configure the vectors through
  91. * a side channel. Should never be used by devices.
  92. */
  93. void msi_set_message(PCIDevice *dev, MSIMessage msg)
  94. {
  95. uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
  96. bool msi64bit = flags & PCI_MSI_FLAGS_64BIT;
  97. if (msi64bit) {
  98. pci_set_quad(dev->config + msi_address_lo_off(dev), msg.address);
  99. } else {
  100. pci_set_long(dev->config + msi_address_lo_off(dev), msg.address);
  101. }
  102. pci_set_word(dev->config + msi_data_off(dev, msi64bit), msg.data);
  103. }
  104. bool msi_enabled(const PCIDevice *dev)
  105. {
  106. return msi_present(dev) &&
  107. (pci_get_word(dev->config + msi_flags_off(dev)) &
  108. PCI_MSI_FLAGS_ENABLE);
  109. }
  110. int msi_init(struct PCIDevice *dev, uint8_t offset,
  111. unsigned int nr_vectors, bool msi64bit, bool msi_per_vector_mask)
  112. {
  113. unsigned int vectors_order;
  114. uint16_t flags;
  115. uint8_t cap_size;
  116. int config_offset;
  117. if (!msi_supported) {
  118. return -ENOTSUP;
  119. }
  120. MSI_DEV_PRINTF(dev,
  121. "init offset: 0x%"PRIx8" vector: %"PRId8
  122. " 64bit %d mask %d\n",
  123. offset, nr_vectors, msi64bit, msi_per_vector_mask);
  124. assert(!(nr_vectors & (nr_vectors - 1))); /* power of 2 */
  125. assert(nr_vectors > 0);
  126. assert(nr_vectors <= PCI_MSI_VECTORS_MAX);
  127. /* the nr of MSI vectors is up to 32 */
  128. vectors_order = ffs(nr_vectors) - 1;
  129. flags = vectors_order << (ffs(PCI_MSI_FLAGS_QMASK) - 1);
  130. if (msi64bit) {
  131. flags |= PCI_MSI_FLAGS_64BIT;
  132. }
  133. if (msi_per_vector_mask) {
  134. flags |= PCI_MSI_FLAGS_MASKBIT;
  135. }
  136. cap_size = msi_cap_sizeof(flags);
  137. config_offset = pci_add_capability(dev, PCI_CAP_ID_MSI, offset, cap_size);
  138. if (config_offset < 0) {
  139. return config_offset;
  140. }
  141. dev->msi_cap = config_offset;
  142. dev->cap_present |= QEMU_PCI_CAP_MSI;
  143. pci_set_word(dev->config + msi_flags_off(dev), flags);
  144. pci_set_word(dev->wmask + msi_flags_off(dev),
  145. PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE);
  146. pci_set_long(dev->wmask + msi_address_lo_off(dev),
  147. PCI_MSI_ADDRESS_LO_MASK);
  148. if (msi64bit) {
  149. pci_set_long(dev->wmask + msi_address_hi_off(dev), 0xffffffff);
  150. }
  151. pci_set_word(dev->wmask + msi_data_off(dev, msi64bit), 0xffff);
  152. if (msi_per_vector_mask) {
  153. /* Make mask bits 0 to nr_vectors - 1 writable. */
  154. pci_set_long(dev->wmask + msi_mask_off(dev, msi64bit),
  155. 0xffffffff >> (PCI_MSI_VECTORS_MAX - nr_vectors));
  156. }
  157. return config_offset;
  158. }
  159. void msi_uninit(struct PCIDevice *dev)
  160. {
  161. uint16_t flags;
  162. uint8_t cap_size;
  163. if (!msi_present(dev)) {
  164. return;
  165. }
  166. flags = pci_get_word(dev->config + msi_flags_off(dev));
  167. cap_size = msi_cap_sizeof(flags);
  168. pci_del_capability(dev, PCI_CAP_ID_MSI, cap_size);
  169. dev->cap_present &= ~QEMU_PCI_CAP_MSI;
  170. MSI_DEV_PRINTF(dev, "uninit\n");
  171. }
  172. void msi_reset(PCIDevice *dev)
  173. {
  174. uint16_t flags;
  175. bool msi64bit;
  176. if (!msi_present(dev)) {
  177. return;
  178. }
  179. flags = pci_get_word(dev->config + msi_flags_off(dev));
  180. flags &= ~(PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE);
  181. msi64bit = flags & PCI_MSI_FLAGS_64BIT;
  182. pci_set_word(dev->config + msi_flags_off(dev), flags);
  183. pci_set_long(dev->config + msi_address_lo_off(dev), 0);
  184. if (msi64bit) {
  185. pci_set_long(dev->config + msi_address_hi_off(dev), 0);
  186. }
  187. pci_set_word(dev->config + msi_data_off(dev, msi64bit), 0);
  188. if (flags & PCI_MSI_FLAGS_MASKBIT) {
  189. pci_set_long(dev->config + msi_mask_off(dev, msi64bit), 0);
  190. pci_set_long(dev->config + msi_pending_off(dev, msi64bit), 0);
  191. }
  192. MSI_DEV_PRINTF(dev, "reset\n");
  193. }
  194. static bool msi_is_masked(const PCIDevice *dev, unsigned int vector)
  195. {
  196. uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
  197. uint32_t mask;
  198. assert(vector < PCI_MSI_VECTORS_MAX);
  199. if (!(flags & PCI_MSI_FLAGS_MASKBIT)) {
  200. return false;
  201. }
  202. mask = pci_get_long(dev->config +
  203. msi_mask_off(dev, flags & PCI_MSI_FLAGS_64BIT));
  204. return mask & (1U << vector);
  205. }
  206. void msi_notify(PCIDevice *dev, unsigned int vector)
  207. {
  208. uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
  209. bool msi64bit = flags & PCI_MSI_FLAGS_64BIT;
  210. unsigned int nr_vectors = msi_nr_vectors(flags);
  211. uint64_t address;
  212. uint32_t data;
  213. assert(vector < nr_vectors);
  214. if (msi_is_masked(dev, vector)) {
  215. assert(flags & PCI_MSI_FLAGS_MASKBIT);
  216. pci_long_test_and_set_mask(
  217. dev->config + msi_pending_off(dev, msi64bit), 1U << vector);
  218. MSI_DEV_PRINTF(dev, "pending vector 0x%x\n", vector);
  219. return;
  220. }
  221. if (msi64bit) {
  222. address = pci_get_quad(dev->config + msi_address_lo_off(dev));
  223. } else {
  224. address = pci_get_long(dev->config + msi_address_lo_off(dev));
  225. }
  226. /* upper bit 31:16 is zero */
  227. data = pci_get_word(dev->config + msi_data_off(dev, msi64bit));
  228. if (nr_vectors > 1) {
  229. data &= ~(nr_vectors - 1);
  230. data |= vector;
  231. }
  232. MSI_DEV_PRINTF(dev,
  233. "notify vector 0x%x"
  234. " address: 0x%"PRIx64" data: 0x%"PRIx32"\n",
  235. vector, address, data);
  236. stl_le_phys(address, data);
  237. }
  238. /* Normally called by pci_default_write_config(). */
  239. void msi_write_config(PCIDevice *dev, uint32_t addr, uint32_t val, int len)
  240. {
  241. uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
  242. bool msi64bit = flags & PCI_MSI_FLAGS_64BIT;
  243. bool msi_per_vector_mask = flags & PCI_MSI_FLAGS_MASKBIT;
  244. unsigned int nr_vectors;
  245. uint8_t log_num_vecs;
  246. uint8_t log_max_vecs;
  247. unsigned int vector;
  248. uint32_t pending;
  249. if (!msi_present(dev) ||
  250. !ranges_overlap(addr, len, dev->msi_cap, msi_cap_sizeof(flags))) {
  251. return;
  252. }
  253. #ifdef MSI_DEBUG
  254. MSI_DEV_PRINTF(dev, "addr 0x%"PRIx32" val 0x%"PRIx32" len %d\n",
  255. addr, val, len);
  256. MSI_DEV_PRINTF(dev, "ctrl: 0x%"PRIx16" address: 0x%"PRIx32,
  257. flags,
  258. pci_get_long(dev->config + msi_address_lo_off(dev)));
  259. if (msi64bit) {
  260. fprintf(stderr, " address-hi: 0x%"PRIx32,
  261. pci_get_long(dev->config + msi_address_hi_off(dev)));
  262. }
  263. fprintf(stderr, " data: 0x%"PRIx16,
  264. pci_get_word(dev->config + msi_data_off(dev, msi64bit)));
  265. if (flags & PCI_MSI_FLAGS_MASKBIT) {
  266. fprintf(stderr, " mask 0x%"PRIx32" pending 0x%"PRIx32,
  267. pci_get_long(dev->config + msi_mask_off(dev, msi64bit)),
  268. pci_get_long(dev->config + msi_pending_off(dev, msi64bit)));
  269. }
  270. fprintf(stderr, "\n");
  271. #endif
  272. if (!(flags & PCI_MSI_FLAGS_ENABLE)) {
  273. return;
  274. }
  275. /*
  276. * Now MSI is enabled, clear INTx# interrupts.
  277. * the driver is prohibited from writing enable bit to mask
  278. * a service request. But the guest OS could do this.
  279. * So we just discard the interrupts as moderate fallback.
  280. *
  281. * 6.8.3.3. Enabling Operation
  282. * While enabled for MSI or MSI-X operation, a function is prohibited
  283. * from using its INTx# pin (if implemented) to request
  284. * service (MSI, MSI-X, and INTx# are mutually exclusive).
  285. */
  286. pci_device_deassert_intx(dev);
  287. /*
  288. * nr_vectors might be set bigger than capable. So clamp it.
  289. * This is not legal by spec, so we can do anything we like,
  290. * just don't crash the host
  291. */
  292. log_num_vecs =
  293. (flags & PCI_MSI_FLAGS_QSIZE) >> (ffs(PCI_MSI_FLAGS_QSIZE) - 1);
  294. log_max_vecs =
  295. (flags & PCI_MSI_FLAGS_QMASK) >> (ffs(PCI_MSI_FLAGS_QMASK) - 1);
  296. if (log_num_vecs > log_max_vecs) {
  297. flags &= ~PCI_MSI_FLAGS_QSIZE;
  298. flags |= log_max_vecs << (ffs(PCI_MSI_FLAGS_QSIZE) - 1);
  299. pci_set_word(dev->config + msi_flags_off(dev), flags);
  300. }
  301. if (!msi_per_vector_mask) {
  302. /* if per vector masking isn't supported,
  303. there is no pending interrupt. */
  304. return;
  305. }
  306. nr_vectors = msi_nr_vectors(flags);
  307. /* This will discard pending interrupts, if any. */
  308. pending = pci_get_long(dev->config + msi_pending_off(dev, msi64bit));
  309. pending &= 0xffffffff >> (PCI_MSI_VECTORS_MAX - nr_vectors);
  310. pci_set_long(dev->config + msi_pending_off(dev, msi64bit), pending);
  311. /* deliver pending interrupts which are unmasked */
  312. for (vector = 0; vector < nr_vectors; ++vector) {
  313. if (msi_is_masked(dev, vector) || !(pending & (1U << vector))) {
  314. continue;
  315. }
  316. pci_long_test_and_clear_mask(
  317. dev->config + msi_pending_off(dev, msi64bit), 1U << vector);
  318. msi_notify(dev, vector);
  319. }
  320. }
  321. unsigned int msi_nr_vectors_allocated(const PCIDevice *dev)
  322. {
  323. uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
  324. return msi_nr_vectors(flags);
  325. }