msi.c 15 KB

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