2
0

msi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 "hw/pci/msi.h"
  19. #include "qemu/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. MSIMessage msi_get_message(PCIDevice *dev, unsigned int vector)
  105. {
  106. uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
  107. bool msi64bit = flags & PCI_MSI_FLAGS_64BIT;
  108. unsigned int nr_vectors = msi_nr_vectors(flags);
  109. MSIMessage msg;
  110. assert(vector < nr_vectors);
  111. if (msi64bit) {
  112. msg.address = pci_get_quad(dev->config + msi_address_lo_off(dev));
  113. } else {
  114. msg.address = pci_get_long(dev->config + msi_address_lo_off(dev));
  115. }
  116. /* upper bit 31:16 is zero */
  117. msg.data = pci_get_word(dev->config + msi_data_off(dev, msi64bit));
  118. if (nr_vectors > 1) {
  119. msg.data &= ~(nr_vectors - 1);
  120. msg.data |= vector;
  121. }
  122. return msg;
  123. }
  124. bool msi_enabled(const PCIDevice *dev)
  125. {
  126. return msi_present(dev) &&
  127. (pci_get_word(dev->config + msi_flags_off(dev)) &
  128. PCI_MSI_FLAGS_ENABLE);
  129. }
  130. int msi_init(struct PCIDevice *dev, uint8_t offset,
  131. unsigned int nr_vectors, bool msi64bit, bool msi_per_vector_mask)
  132. {
  133. unsigned int vectors_order;
  134. uint16_t flags;
  135. uint8_t cap_size;
  136. int config_offset;
  137. if (!msi_supported) {
  138. return -ENOTSUP;
  139. }
  140. MSI_DEV_PRINTF(dev,
  141. "init offset: 0x%"PRIx8" vector: %"PRId8
  142. " 64bit %d mask %d\n",
  143. offset, nr_vectors, msi64bit, msi_per_vector_mask);
  144. assert(!(nr_vectors & (nr_vectors - 1))); /* power of 2 */
  145. assert(nr_vectors > 0);
  146. assert(nr_vectors <= PCI_MSI_VECTORS_MAX);
  147. /* the nr of MSI vectors is up to 32 */
  148. vectors_order = ffs(nr_vectors) - 1;
  149. flags = vectors_order << (ffs(PCI_MSI_FLAGS_QMASK) - 1);
  150. if (msi64bit) {
  151. flags |= PCI_MSI_FLAGS_64BIT;
  152. }
  153. if (msi_per_vector_mask) {
  154. flags |= PCI_MSI_FLAGS_MASKBIT;
  155. }
  156. cap_size = msi_cap_sizeof(flags);
  157. config_offset = pci_add_capability(dev, PCI_CAP_ID_MSI, offset, cap_size);
  158. if (config_offset < 0) {
  159. return config_offset;
  160. }
  161. dev->msi_cap = config_offset;
  162. dev->cap_present |= QEMU_PCI_CAP_MSI;
  163. pci_set_word(dev->config + msi_flags_off(dev), flags);
  164. pci_set_word(dev->wmask + msi_flags_off(dev),
  165. PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE);
  166. pci_set_long(dev->wmask + msi_address_lo_off(dev),
  167. PCI_MSI_ADDRESS_LO_MASK);
  168. if (msi64bit) {
  169. pci_set_long(dev->wmask + msi_address_hi_off(dev), 0xffffffff);
  170. }
  171. pci_set_word(dev->wmask + msi_data_off(dev, msi64bit), 0xffff);
  172. if (msi_per_vector_mask) {
  173. /* Make mask bits 0 to nr_vectors - 1 writable. */
  174. pci_set_long(dev->wmask + msi_mask_off(dev, msi64bit),
  175. 0xffffffff >> (PCI_MSI_VECTORS_MAX - nr_vectors));
  176. }
  177. return config_offset;
  178. }
  179. void msi_uninit(struct PCIDevice *dev)
  180. {
  181. uint16_t flags;
  182. uint8_t cap_size;
  183. if (!msi_present(dev)) {
  184. return;
  185. }
  186. flags = pci_get_word(dev->config + msi_flags_off(dev));
  187. cap_size = msi_cap_sizeof(flags);
  188. pci_del_capability(dev, PCI_CAP_ID_MSI, cap_size);
  189. dev->cap_present &= ~QEMU_PCI_CAP_MSI;
  190. MSI_DEV_PRINTF(dev, "uninit\n");
  191. }
  192. void msi_reset(PCIDevice *dev)
  193. {
  194. uint16_t flags;
  195. bool msi64bit;
  196. if (!msi_present(dev)) {
  197. return;
  198. }
  199. flags = pci_get_word(dev->config + msi_flags_off(dev));
  200. flags &= ~(PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE);
  201. msi64bit = flags & PCI_MSI_FLAGS_64BIT;
  202. pci_set_word(dev->config + msi_flags_off(dev), flags);
  203. pci_set_long(dev->config + msi_address_lo_off(dev), 0);
  204. if (msi64bit) {
  205. pci_set_long(dev->config + msi_address_hi_off(dev), 0);
  206. }
  207. pci_set_word(dev->config + msi_data_off(dev, msi64bit), 0);
  208. if (flags & PCI_MSI_FLAGS_MASKBIT) {
  209. pci_set_long(dev->config + msi_mask_off(dev, msi64bit), 0);
  210. pci_set_long(dev->config + msi_pending_off(dev, msi64bit), 0);
  211. }
  212. MSI_DEV_PRINTF(dev, "reset\n");
  213. }
  214. static bool msi_is_masked(const PCIDevice *dev, unsigned int vector)
  215. {
  216. uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
  217. uint32_t mask;
  218. assert(vector < PCI_MSI_VECTORS_MAX);
  219. if (!(flags & PCI_MSI_FLAGS_MASKBIT)) {
  220. return false;
  221. }
  222. mask = pci_get_long(dev->config +
  223. msi_mask_off(dev, flags & PCI_MSI_FLAGS_64BIT));
  224. return mask & (1U << vector);
  225. }
  226. void msi_notify(PCIDevice *dev, unsigned int vector)
  227. {
  228. uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
  229. bool msi64bit = flags & PCI_MSI_FLAGS_64BIT;
  230. unsigned int nr_vectors = msi_nr_vectors(flags);
  231. MSIMessage msg;
  232. assert(vector < nr_vectors);
  233. if (msi_is_masked(dev, vector)) {
  234. assert(flags & PCI_MSI_FLAGS_MASKBIT);
  235. pci_long_test_and_set_mask(
  236. dev->config + msi_pending_off(dev, msi64bit), 1U << vector);
  237. MSI_DEV_PRINTF(dev, "pending vector 0x%x\n", vector);
  238. return;
  239. }
  240. msg = msi_get_message(dev, vector);
  241. MSI_DEV_PRINTF(dev,
  242. "notify vector 0x%x"
  243. " address: 0x%"PRIx64" data: 0x%"PRIx32"\n",
  244. vector, msg.address, msg.data);
  245. stl_le_phys(&address_space_memory, msg.address, msg.data);
  246. }
  247. /* Normally called by pci_default_write_config(). */
  248. void msi_write_config(PCIDevice *dev, uint32_t addr, uint32_t val, int len)
  249. {
  250. uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
  251. bool msi64bit = flags & PCI_MSI_FLAGS_64BIT;
  252. bool msi_per_vector_mask = flags & PCI_MSI_FLAGS_MASKBIT;
  253. unsigned int nr_vectors;
  254. uint8_t log_num_vecs;
  255. uint8_t log_max_vecs;
  256. unsigned int vector;
  257. uint32_t pending;
  258. if (!msi_present(dev) ||
  259. !ranges_overlap(addr, len, dev->msi_cap, msi_cap_sizeof(flags))) {
  260. return;
  261. }
  262. #ifdef MSI_DEBUG
  263. MSI_DEV_PRINTF(dev, "addr 0x%"PRIx32" val 0x%"PRIx32" len %d\n",
  264. addr, val, len);
  265. MSI_DEV_PRINTF(dev, "ctrl: 0x%"PRIx16" address: 0x%"PRIx32,
  266. flags,
  267. pci_get_long(dev->config + msi_address_lo_off(dev)));
  268. if (msi64bit) {
  269. fprintf(stderr, " address-hi: 0x%"PRIx32,
  270. pci_get_long(dev->config + msi_address_hi_off(dev)));
  271. }
  272. fprintf(stderr, " data: 0x%"PRIx16,
  273. pci_get_word(dev->config + msi_data_off(dev, msi64bit)));
  274. if (flags & PCI_MSI_FLAGS_MASKBIT) {
  275. fprintf(stderr, " mask 0x%"PRIx32" pending 0x%"PRIx32,
  276. pci_get_long(dev->config + msi_mask_off(dev, msi64bit)),
  277. pci_get_long(dev->config + msi_pending_off(dev, msi64bit)));
  278. }
  279. fprintf(stderr, "\n");
  280. #endif
  281. if (!(flags & PCI_MSI_FLAGS_ENABLE)) {
  282. return;
  283. }
  284. /*
  285. * Now MSI is enabled, clear INTx# interrupts.
  286. * the driver is prohibited from writing enable bit to mask
  287. * a service request. But the guest OS could do this.
  288. * So we just discard the interrupts as moderate fallback.
  289. *
  290. * 6.8.3.3. Enabling Operation
  291. * While enabled for MSI or MSI-X operation, a function is prohibited
  292. * from using its INTx# pin (if implemented) to request
  293. * service (MSI, MSI-X, and INTx# are mutually exclusive).
  294. */
  295. pci_device_deassert_intx(dev);
  296. /*
  297. * nr_vectors might be set bigger than capable. So clamp it.
  298. * This is not legal by spec, so we can do anything we like,
  299. * just don't crash the host
  300. */
  301. log_num_vecs =
  302. (flags & PCI_MSI_FLAGS_QSIZE) >> (ffs(PCI_MSI_FLAGS_QSIZE) - 1);
  303. log_max_vecs =
  304. (flags & PCI_MSI_FLAGS_QMASK) >> (ffs(PCI_MSI_FLAGS_QMASK) - 1);
  305. if (log_num_vecs > log_max_vecs) {
  306. flags &= ~PCI_MSI_FLAGS_QSIZE;
  307. flags |= log_max_vecs << (ffs(PCI_MSI_FLAGS_QSIZE) - 1);
  308. pci_set_word(dev->config + msi_flags_off(dev), flags);
  309. }
  310. if (!msi_per_vector_mask) {
  311. /* if per vector masking isn't supported,
  312. there is no pending interrupt. */
  313. return;
  314. }
  315. nr_vectors = msi_nr_vectors(flags);
  316. /* This will discard pending interrupts, if any. */
  317. pending = pci_get_long(dev->config + msi_pending_off(dev, msi64bit));
  318. pending &= 0xffffffff >> (PCI_MSI_VECTORS_MAX - nr_vectors);
  319. pci_set_long(dev->config + msi_pending_off(dev, msi64bit), pending);
  320. /* deliver pending interrupts which are unmasked */
  321. for (vector = 0; vector < nr_vectors; ++vector) {
  322. if (msi_is_masked(dev, vector) || !(pending & (1U << vector))) {
  323. continue;
  324. }
  325. pci_long_test_and_clear_mask(
  326. dev->config + msi_pending_off(dev, msi64bit), 1U << vector);
  327. msi_notify(dev, vector);
  328. }
  329. }
  330. unsigned int msi_nr_vectors_allocated(const PCIDevice *dev)
  331. {
  332. uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
  333. return msi_nr_vectors(flags);
  334. }