msi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 "qemu/osdep.h"
  19. #include "hw/pci/msi.h"
  20. #include "hw/xen/xen.h"
  21. #include "qemu/range.h"
  22. #include "qapi/error.h"
  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. /*
  32. * Flag for interrupt controllers to declare broken MSI/MSI-X support.
  33. * values: false - broken; true - non-broken.
  34. *
  35. * Setting this flag to false will remove MSI/MSI-X capability from all devices.
  36. *
  37. * It is preferable for controllers to set this to true (non-broken) even if
  38. * they do not actually support MSI/MSI-X: guests normally probe the controller
  39. * type and do not attempt to enable MSI/MSI-X with interrupt controllers not
  40. * supporting such, so removing the capability is not required, and
  41. * it seems cleaner to have a given device look the same for all boards.
  42. *
  43. * TODO: some existing controllers violate the above rule. Identify and fix them.
  44. */
  45. bool msi_nonbroken;
  46. /* If we get rid of cap allocator, we won't need this. */
  47. static inline uint8_t msi_cap_sizeof(uint16_t flags)
  48. {
  49. switch (flags & (PCI_MSI_FLAGS_MASKBIT | PCI_MSI_FLAGS_64BIT)) {
  50. case PCI_MSI_FLAGS_MASKBIT | PCI_MSI_FLAGS_64BIT:
  51. return PCI_MSI_64M_SIZEOF;
  52. case PCI_MSI_FLAGS_64BIT:
  53. return PCI_MSI_64_SIZEOF;
  54. case PCI_MSI_FLAGS_MASKBIT:
  55. return PCI_MSI_32M_SIZEOF;
  56. case 0:
  57. return PCI_MSI_32_SIZEOF;
  58. default:
  59. abort();
  60. break;
  61. }
  62. return 0;
  63. }
  64. //#define MSI_DEBUG
  65. #ifdef MSI_DEBUG
  66. # define MSI_DPRINTF(fmt, ...) \
  67. fprintf(stderr, "%s:%d " fmt, __func__, __LINE__, ## __VA_ARGS__)
  68. #else
  69. # define MSI_DPRINTF(fmt, ...) do { } while (0)
  70. #endif
  71. #define MSI_DEV_PRINTF(dev, fmt, ...) \
  72. MSI_DPRINTF("%s:%x " fmt, (dev)->name, (dev)->devfn, ## __VA_ARGS__)
  73. static inline unsigned int msi_nr_vectors(uint16_t flags)
  74. {
  75. return 1U <<
  76. ((flags & PCI_MSI_FLAGS_QSIZE) >> ctz32(PCI_MSI_FLAGS_QSIZE));
  77. }
  78. static inline uint8_t msi_flags_off(const PCIDevice* dev)
  79. {
  80. return dev->msi_cap + PCI_MSI_FLAGS;
  81. }
  82. static inline uint8_t msi_address_lo_off(const PCIDevice* dev)
  83. {
  84. return dev->msi_cap + PCI_MSI_ADDRESS_LO;
  85. }
  86. static inline uint8_t msi_address_hi_off(const PCIDevice* dev)
  87. {
  88. return dev->msi_cap + PCI_MSI_ADDRESS_HI;
  89. }
  90. static inline uint8_t msi_data_off(const PCIDevice* dev, bool msi64bit)
  91. {
  92. return dev->msi_cap + (msi64bit ? PCI_MSI_DATA_64 : PCI_MSI_DATA_32);
  93. }
  94. static inline uint8_t msi_mask_off(const PCIDevice* dev, bool msi64bit)
  95. {
  96. return dev->msi_cap + (msi64bit ? PCI_MSI_MASK_64 : PCI_MSI_MASK_32);
  97. }
  98. static inline uint8_t msi_pending_off(const PCIDevice* dev, bool msi64bit)
  99. {
  100. return dev->msi_cap + (msi64bit ? PCI_MSI_PENDING_64 : PCI_MSI_PENDING_32);
  101. }
  102. /*
  103. * Special API for POWER to configure the vectors through
  104. * a side channel. Should never be used by devices.
  105. */
  106. void msi_set_message(PCIDevice *dev, MSIMessage msg)
  107. {
  108. uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
  109. bool msi64bit = flags & PCI_MSI_FLAGS_64BIT;
  110. if (msi64bit) {
  111. pci_set_quad(dev->config + msi_address_lo_off(dev), msg.address);
  112. } else {
  113. pci_set_long(dev->config + msi_address_lo_off(dev), msg.address);
  114. }
  115. pci_set_word(dev->config + msi_data_off(dev, msi64bit), msg.data);
  116. }
  117. MSIMessage msi_get_message(PCIDevice *dev, unsigned int vector)
  118. {
  119. uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
  120. bool msi64bit = flags & PCI_MSI_FLAGS_64BIT;
  121. unsigned int nr_vectors = msi_nr_vectors(flags);
  122. MSIMessage msg;
  123. assert(vector < nr_vectors);
  124. if (msi64bit) {
  125. msg.address = pci_get_quad(dev->config + msi_address_lo_off(dev));
  126. } else {
  127. msg.address = pci_get_long(dev->config + msi_address_lo_off(dev));
  128. }
  129. /* upper bit 31:16 is zero */
  130. msg.data = pci_get_word(dev->config + msi_data_off(dev, msi64bit));
  131. if (nr_vectors > 1) {
  132. msg.data &= ~(nr_vectors - 1);
  133. msg.data |= vector;
  134. }
  135. return msg;
  136. }
  137. bool msi_enabled(const PCIDevice *dev)
  138. {
  139. return msi_present(dev) &&
  140. (pci_get_word(dev->config + msi_flags_off(dev)) &
  141. PCI_MSI_FLAGS_ENABLE);
  142. }
  143. /*
  144. * Make PCI device @dev MSI-capable.
  145. * Non-zero @offset puts capability MSI at that offset in PCI config
  146. * space.
  147. * @nr_vectors is the number of MSI vectors (1, 2, 4, 8, 16 or 32).
  148. * If @msi64bit, make the device capable of sending a 64-bit message
  149. * address.
  150. * If @msi_per_vector_mask, make the device support per-vector masking.
  151. * @errp is for returning errors.
  152. * Return 0 on success; set @errp and return -errno on error.
  153. *
  154. * -ENOTSUP means lacking msi support for a msi-capable platform.
  155. * -EINVAL means capability overlap, happens when @offset is non-zero,
  156. * also means a programming error, except device assignment, which can check
  157. * if a real HW is broken.
  158. */
  159. int msi_init(struct PCIDevice *dev, uint8_t offset,
  160. unsigned int nr_vectors, bool msi64bit,
  161. bool msi_per_vector_mask, Error **errp)
  162. {
  163. unsigned int vectors_order;
  164. uint16_t flags;
  165. uint8_t cap_size;
  166. int config_offset;
  167. if (!msi_nonbroken) {
  168. error_setg(errp, "MSI is not supported by interrupt controller");
  169. return -ENOTSUP;
  170. }
  171. MSI_DEV_PRINTF(dev,
  172. "init offset: 0x%"PRIx8" vector: %"PRId8
  173. " 64bit %d mask %d\n",
  174. offset, nr_vectors, msi64bit, msi_per_vector_mask);
  175. assert(!(nr_vectors & (nr_vectors - 1))); /* power of 2 */
  176. assert(nr_vectors > 0);
  177. assert(nr_vectors <= PCI_MSI_VECTORS_MAX);
  178. /* the nr of MSI vectors is up to 32 */
  179. vectors_order = ctz32(nr_vectors);
  180. flags = vectors_order << ctz32(PCI_MSI_FLAGS_QMASK);
  181. if (msi64bit) {
  182. flags |= PCI_MSI_FLAGS_64BIT;
  183. }
  184. if (msi_per_vector_mask) {
  185. flags |= PCI_MSI_FLAGS_MASKBIT;
  186. }
  187. cap_size = msi_cap_sizeof(flags);
  188. config_offset = pci_add_capability(dev, PCI_CAP_ID_MSI, offset,
  189. cap_size, errp);
  190. if (config_offset < 0) {
  191. return config_offset;
  192. }
  193. dev->msi_cap = config_offset;
  194. dev->cap_present |= QEMU_PCI_CAP_MSI;
  195. pci_set_word(dev->config + msi_flags_off(dev), flags);
  196. pci_set_word(dev->wmask + msi_flags_off(dev),
  197. PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE);
  198. pci_set_long(dev->wmask + msi_address_lo_off(dev),
  199. PCI_MSI_ADDRESS_LO_MASK);
  200. if (msi64bit) {
  201. pci_set_long(dev->wmask + msi_address_hi_off(dev), 0xffffffff);
  202. }
  203. pci_set_word(dev->wmask + msi_data_off(dev, msi64bit), 0xffff);
  204. if (msi_per_vector_mask) {
  205. /* Make mask bits 0 to nr_vectors - 1 writable. */
  206. pci_set_long(dev->wmask + msi_mask_off(dev, msi64bit),
  207. 0xffffffff >> (PCI_MSI_VECTORS_MAX - nr_vectors));
  208. }
  209. return 0;
  210. }
  211. void msi_uninit(struct PCIDevice *dev)
  212. {
  213. uint16_t flags;
  214. uint8_t cap_size;
  215. if (!msi_present(dev)) {
  216. return;
  217. }
  218. flags = pci_get_word(dev->config + msi_flags_off(dev));
  219. cap_size = msi_cap_sizeof(flags);
  220. pci_del_capability(dev, PCI_CAP_ID_MSI, cap_size);
  221. dev->cap_present &= ~QEMU_PCI_CAP_MSI;
  222. MSI_DEV_PRINTF(dev, "uninit\n");
  223. }
  224. void msi_reset(PCIDevice *dev)
  225. {
  226. uint16_t flags;
  227. bool msi64bit;
  228. if (!msi_present(dev)) {
  229. return;
  230. }
  231. flags = pci_get_word(dev->config + msi_flags_off(dev));
  232. flags &= ~(PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE);
  233. msi64bit = flags & PCI_MSI_FLAGS_64BIT;
  234. pci_set_word(dev->config + msi_flags_off(dev), flags);
  235. pci_set_long(dev->config + msi_address_lo_off(dev), 0);
  236. if (msi64bit) {
  237. pci_set_long(dev->config + msi_address_hi_off(dev), 0);
  238. }
  239. pci_set_word(dev->config + msi_data_off(dev, msi64bit), 0);
  240. if (flags & PCI_MSI_FLAGS_MASKBIT) {
  241. pci_set_long(dev->config + msi_mask_off(dev, msi64bit), 0);
  242. pci_set_long(dev->config + msi_pending_off(dev, msi64bit), 0);
  243. }
  244. MSI_DEV_PRINTF(dev, "reset\n");
  245. }
  246. bool msi_is_masked(const PCIDevice *dev, unsigned int vector)
  247. {
  248. uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
  249. uint32_t mask, data;
  250. bool msi64bit = flags & PCI_MSI_FLAGS_64BIT;
  251. assert(vector < PCI_MSI_VECTORS_MAX);
  252. if (!(flags & PCI_MSI_FLAGS_MASKBIT)) {
  253. return false;
  254. }
  255. data = pci_get_word(dev->config + msi_data_off(dev, msi64bit));
  256. if (xen_is_pirq_msi(data)) {
  257. return false;
  258. }
  259. mask = pci_get_long(dev->config +
  260. msi_mask_off(dev, flags & PCI_MSI_FLAGS_64BIT));
  261. return mask & (1U << vector);
  262. }
  263. void msi_notify(PCIDevice *dev, unsigned int vector)
  264. {
  265. uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
  266. bool msi64bit = flags & PCI_MSI_FLAGS_64BIT;
  267. unsigned int nr_vectors = msi_nr_vectors(flags);
  268. MSIMessage msg;
  269. assert(vector < nr_vectors);
  270. if (msi_is_masked(dev, vector)) {
  271. assert(flags & PCI_MSI_FLAGS_MASKBIT);
  272. pci_long_test_and_set_mask(
  273. dev->config + msi_pending_off(dev, msi64bit), 1U << vector);
  274. MSI_DEV_PRINTF(dev, "pending vector 0x%x\n", vector);
  275. return;
  276. }
  277. msg = msi_get_message(dev, vector);
  278. MSI_DEV_PRINTF(dev,
  279. "notify vector 0x%x"
  280. " address: 0x%"PRIx64" data: 0x%"PRIx32"\n",
  281. vector, msg.address, msg.data);
  282. msi_send_message(dev, msg);
  283. }
  284. void msi_send_message(PCIDevice *dev, MSIMessage msg)
  285. {
  286. MemTxAttrs attrs = {};
  287. attrs.requester_id = pci_requester_id(dev);
  288. address_space_stl_le(&dev->bus_master_as, msg.address, msg.data,
  289. attrs, NULL);
  290. }
  291. /* Normally called by pci_default_write_config(). */
  292. void msi_write_config(PCIDevice *dev, uint32_t addr, uint32_t val, int len)
  293. {
  294. uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
  295. bool msi64bit = flags & PCI_MSI_FLAGS_64BIT;
  296. bool msi_per_vector_mask = flags & PCI_MSI_FLAGS_MASKBIT;
  297. unsigned int nr_vectors;
  298. uint8_t log_num_vecs;
  299. uint8_t log_max_vecs;
  300. unsigned int vector;
  301. uint32_t pending;
  302. if (!msi_present(dev) ||
  303. !ranges_overlap(addr, len, dev->msi_cap, msi_cap_sizeof(flags))) {
  304. return;
  305. }
  306. #ifdef MSI_DEBUG
  307. MSI_DEV_PRINTF(dev, "addr 0x%"PRIx32" val 0x%"PRIx32" len %d\n",
  308. addr, val, len);
  309. MSI_DEV_PRINTF(dev, "ctrl: 0x%"PRIx16" address: 0x%"PRIx32,
  310. flags,
  311. pci_get_long(dev->config + msi_address_lo_off(dev)));
  312. if (msi64bit) {
  313. fprintf(stderr, " address-hi: 0x%"PRIx32,
  314. pci_get_long(dev->config + msi_address_hi_off(dev)));
  315. }
  316. fprintf(stderr, " data: 0x%"PRIx16,
  317. pci_get_word(dev->config + msi_data_off(dev, msi64bit)));
  318. if (flags & PCI_MSI_FLAGS_MASKBIT) {
  319. fprintf(stderr, " mask 0x%"PRIx32" pending 0x%"PRIx32,
  320. pci_get_long(dev->config + msi_mask_off(dev, msi64bit)),
  321. pci_get_long(dev->config + msi_pending_off(dev, msi64bit)));
  322. }
  323. fprintf(stderr, "\n");
  324. #endif
  325. if (!(flags & PCI_MSI_FLAGS_ENABLE)) {
  326. return;
  327. }
  328. /*
  329. * Now MSI is enabled, clear INTx# interrupts.
  330. * the driver is prohibited from writing enable bit to mask
  331. * a service request. But the guest OS could do this.
  332. * So we just discard the interrupts as moderate fallback.
  333. *
  334. * 6.8.3.3. Enabling Operation
  335. * While enabled for MSI or MSI-X operation, a function is prohibited
  336. * from using its INTx# pin (if implemented) to request
  337. * service (MSI, MSI-X, and INTx# are mutually exclusive).
  338. */
  339. pci_device_deassert_intx(dev);
  340. /*
  341. * nr_vectors might be set bigger than capable. So clamp it.
  342. * This is not legal by spec, so we can do anything we like,
  343. * just don't crash the host
  344. */
  345. log_num_vecs =
  346. (flags & PCI_MSI_FLAGS_QSIZE) >> ctz32(PCI_MSI_FLAGS_QSIZE);
  347. log_max_vecs =
  348. (flags & PCI_MSI_FLAGS_QMASK) >> ctz32(PCI_MSI_FLAGS_QMASK);
  349. if (log_num_vecs > log_max_vecs) {
  350. flags &= ~PCI_MSI_FLAGS_QSIZE;
  351. flags |= log_max_vecs << ctz32(PCI_MSI_FLAGS_QSIZE);
  352. pci_set_word(dev->config + msi_flags_off(dev), flags);
  353. }
  354. if (!msi_per_vector_mask) {
  355. /* if per vector masking isn't supported,
  356. there is no pending interrupt. */
  357. return;
  358. }
  359. nr_vectors = msi_nr_vectors(flags);
  360. /* This will discard pending interrupts, if any. */
  361. pending = pci_get_long(dev->config + msi_pending_off(dev, msi64bit));
  362. pending &= 0xffffffff >> (PCI_MSI_VECTORS_MAX - nr_vectors);
  363. pci_set_long(dev->config + msi_pending_off(dev, msi64bit), pending);
  364. /* deliver pending interrupts which are unmasked */
  365. for (vector = 0; vector < nr_vectors; ++vector) {
  366. if (msi_is_masked(dev, vector) || !(pending & (1U << vector))) {
  367. continue;
  368. }
  369. pci_long_test_and_clear_mask(
  370. dev->config + msi_pending_off(dev, msi64bit), 1U << vector);
  371. msi_notify(dev, vector);
  372. }
  373. }
  374. unsigned int msi_nr_vectors_allocated(const PCIDevice *dev)
  375. {
  376. uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
  377. return msi_nr_vectors(flags);
  378. }