2
0

shpc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. #include "qemu-common.h"
  2. #include <strings.h>
  3. #include <stdint.h>
  4. #include "qemu/range.h"
  5. #include "qemu/error-report.h"
  6. #include "hw/pci/shpc.h"
  7. #include "hw/pci/pci.h"
  8. #include "hw/pci/pci_bus.h"
  9. #include "hw/pci/msi.h"
  10. #include "qapi/qmp/qerror.h"
  11. /* TODO: model power only and disabled slot states. */
  12. /* TODO: handle SERR and wakeups */
  13. /* TODO: consider enabling 66MHz support */
  14. /* TODO: remove fully only on state DISABLED and LED off.
  15. * track state to properly record this. */
  16. /* SHPC Working Register Set */
  17. #define SHPC_BASE_OFFSET 0x00 /* 4 bytes */
  18. #define SHPC_SLOTS_33 0x04 /* 4 bytes. Also encodes PCI-X slots. */
  19. #define SHPC_SLOTS_66 0x08 /* 4 bytes. */
  20. #define SHPC_NSLOTS 0x0C /* 1 byte */
  21. #define SHPC_FIRST_DEV 0x0D /* 1 byte */
  22. #define SHPC_PHYS_SLOT 0x0E /* 2 byte */
  23. #define SHPC_PHYS_NUM_MAX 0x7ff
  24. #define SHPC_PHYS_NUM_UP 0x2000
  25. #define SHPC_PHYS_MRL 0x4000
  26. #define SHPC_PHYS_BUTTON 0x8000
  27. #define SHPC_SEC_BUS 0x10 /* 2 bytes */
  28. #define SHPC_SEC_BUS_33 0x0
  29. #define SHPC_SEC_BUS_66 0x1 /* Unused */
  30. #define SHPC_SEC_BUS_MASK 0x7
  31. #define SHPC_MSI_CTL 0x12 /* 1 byte */
  32. #define SHPC_PROG_IFC 0x13 /* 1 byte */
  33. #define SHPC_PROG_IFC_1_0 0x1
  34. #define SHPC_CMD_CODE 0x14 /* 1 byte */
  35. #define SHPC_CMD_TRGT 0x15 /* 1 byte */
  36. #define SHPC_CMD_TRGT_MIN 0x1
  37. #define SHPC_CMD_TRGT_MAX 0x1f
  38. #define SHPC_CMD_STATUS 0x16 /* 2 bytes */
  39. #define SHPC_CMD_STATUS_BUSY 0x1
  40. #define SHPC_CMD_STATUS_MRL_OPEN 0x2
  41. #define SHPC_CMD_STATUS_INVALID_CMD 0x4
  42. #define SHPC_CMD_STATUS_INVALID_MODE 0x8
  43. #define SHPC_INT_LOCATOR 0x18 /* 4 bytes */
  44. #define SHPC_INT_COMMAND 0x1
  45. #define SHPC_SERR_LOCATOR 0x1C /* 4 bytes */
  46. #define SHPC_SERR_INT 0x20 /* 4 bytes */
  47. #define SHPC_INT_DIS 0x1
  48. #define SHPC_SERR_DIS 0x2
  49. #define SHPC_CMD_INT_DIS 0x4
  50. #define SHPC_ARB_SERR_DIS 0x8
  51. #define SHPC_CMD_DETECTED 0x10000
  52. #define SHPC_ARB_DETECTED 0x20000
  53. /* 4 bytes * slot # (start from 0) */
  54. #define SHPC_SLOT_REG(s) (0x24 + (s) * 4)
  55. /* 2 bytes */
  56. #define SHPC_SLOT_STATUS(s) (0x0 + SHPC_SLOT_REG(s))
  57. /* Same slot state masks are used for command and status registers */
  58. #define SHPC_SLOT_STATE_MASK 0x03
  59. #define SHPC_SLOT_STATE_SHIFT \
  60. (ffs(SHPC_SLOT_STATE_MASK) - 1)
  61. #define SHPC_STATE_NO 0x0
  62. #define SHPC_STATE_PWRONLY 0x1
  63. #define SHPC_STATE_ENABLED 0x2
  64. #define SHPC_STATE_DISABLED 0x3
  65. #define SHPC_SLOT_PWR_LED_MASK 0xC
  66. #define SHPC_SLOT_PWR_LED_SHIFT \
  67. (ffs(SHPC_SLOT_PWR_LED_MASK) - 1)
  68. #define SHPC_SLOT_ATTN_LED_MASK 0x30
  69. #define SHPC_SLOT_ATTN_LED_SHIFT \
  70. (ffs(SHPC_SLOT_ATTN_LED_MASK) - 1)
  71. #define SHPC_LED_NO 0x0
  72. #define SHPC_LED_ON 0x1
  73. #define SHPC_LED_BLINK 0x2
  74. #define SHPC_LED_OFF 0x3
  75. #define SHPC_SLOT_STATUS_PWR_FAULT 0x40
  76. #define SHPC_SLOT_STATUS_BUTTON 0x80
  77. #define SHPC_SLOT_STATUS_MRL_OPEN 0x100
  78. #define SHPC_SLOT_STATUS_66 0x200
  79. #define SHPC_SLOT_STATUS_PRSNT_MASK 0xC00
  80. #define SHPC_SLOT_STATUS_PRSNT_EMPTY 0x3
  81. #define SHPC_SLOT_STATUS_PRSNT_25W 0x1
  82. #define SHPC_SLOT_STATUS_PRSNT_15W 0x2
  83. #define SHPC_SLOT_STATUS_PRSNT_7_5W 0x0
  84. #define SHPC_SLOT_STATUS_PRSNT_PCIX 0x3000
  85. /* 1 byte */
  86. #define SHPC_SLOT_EVENT_LATCH(s) (0x2 + SHPC_SLOT_REG(s))
  87. /* 1 byte */
  88. #define SHPC_SLOT_EVENT_SERR_INT_DIS(d, s) (0x3 + SHPC_SLOT_REG(s))
  89. #define SHPC_SLOT_EVENT_PRESENCE 0x01
  90. #define SHPC_SLOT_EVENT_ISOLATED_FAULT 0x02
  91. #define SHPC_SLOT_EVENT_BUTTON 0x04
  92. #define SHPC_SLOT_EVENT_MRL 0x08
  93. #define SHPC_SLOT_EVENT_CONNECTED_FAULT 0x10
  94. /* Bits below are used for Serr/Int disable only */
  95. #define SHPC_SLOT_EVENT_MRL_SERR_DIS 0x20
  96. #define SHPC_SLOT_EVENT_CONNECTED_FAULT_SERR_DIS 0x40
  97. #define SHPC_MIN_SLOTS 1
  98. #define SHPC_MAX_SLOTS 31
  99. #define SHPC_SIZEOF(d) SHPC_SLOT_REG((d)->shpc->nslots)
  100. /* SHPC Slot identifiers */
  101. /* Hotplug supported at 31 slots out of the total 32. We reserve slot 0,
  102. and give the rest of them physical *and* pci numbers starting from 1, so
  103. they match logical numbers. Note: this means that multiple slots must have
  104. different chassis number values, to make chassis+physical slot unique.
  105. TODO: make this configurable? */
  106. #define SHPC_IDX_TO_LOGICAL(slot) ((slot) + 1)
  107. #define SHPC_LOGICAL_TO_IDX(target) ((target) - 1)
  108. #define SHPC_IDX_TO_PCI(slot) ((slot) + 1)
  109. #define SHPC_PCI_TO_IDX(pci_slot) ((pci_slot) - 1)
  110. #define SHPC_IDX_TO_PHYSICAL(slot) ((slot) + 1)
  111. static int roundup_pow_of_two(int x)
  112. {
  113. x |= (x >> 1);
  114. x |= (x >> 2);
  115. x |= (x >> 4);
  116. x |= (x >> 8);
  117. x |= (x >> 16);
  118. return x + 1;
  119. }
  120. static uint16_t shpc_get_status(SHPCDevice *shpc, int slot, uint16_t msk)
  121. {
  122. uint8_t *status = shpc->config + SHPC_SLOT_STATUS(slot);
  123. return (pci_get_word(status) & msk) >> (ffs(msk) - 1);
  124. }
  125. static void shpc_set_status(SHPCDevice *shpc,
  126. int slot, uint8_t value, uint16_t msk)
  127. {
  128. uint8_t *status = shpc->config + SHPC_SLOT_STATUS(slot);
  129. pci_word_test_and_clear_mask(status, msk);
  130. pci_word_test_and_set_mask(status, value << (ffs(msk) - 1));
  131. }
  132. static void shpc_interrupt_update(PCIDevice *d)
  133. {
  134. SHPCDevice *shpc = d->shpc;
  135. int slot;
  136. int level = 0;
  137. uint32_t serr_int;
  138. uint32_t int_locator = 0;
  139. /* Update interrupt locator register */
  140. for (slot = 0; slot < shpc->nslots; ++slot) {
  141. uint8_t event = shpc->config[SHPC_SLOT_EVENT_LATCH(slot)];
  142. uint8_t disable = shpc->config[SHPC_SLOT_EVENT_SERR_INT_DIS(d, slot)];
  143. uint32_t mask = 1 << SHPC_IDX_TO_LOGICAL(slot);
  144. if (event & ~disable) {
  145. int_locator |= mask;
  146. }
  147. }
  148. serr_int = pci_get_long(shpc->config + SHPC_SERR_INT);
  149. if ((serr_int & SHPC_CMD_DETECTED) && !(serr_int & SHPC_CMD_INT_DIS)) {
  150. int_locator |= SHPC_INT_COMMAND;
  151. }
  152. pci_set_long(shpc->config + SHPC_INT_LOCATOR, int_locator);
  153. level = (!(serr_int & SHPC_INT_DIS) && int_locator) ? 1 : 0;
  154. if (msi_enabled(d) && shpc->msi_requested != level)
  155. msi_notify(d, 0);
  156. else
  157. pci_set_irq(d, level);
  158. shpc->msi_requested = level;
  159. }
  160. static void shpc_set_sec_bus_speed(SHPCDevice *shpc, uint8_t speed)
  161. {
  162. switch (speed) {
  163. case SHPC_SEC_BUS_33:
  164. shpc->config[SHPC_SEC_BUS] &= ~SHPC_SEC_BUS_MASK;
  165. shpc->config[SHPC_SEC_BUS] |= speed;
  166. break;
  167. default:
  168. pci_word_test_and_set_mask(shpc->config + SHPC_CMD_STATUS,
  169. SHPC_CMD_STATUS_INVALID_MODE);
  170. }
  171. }
  172. void shpc_reset(PCIDevice *d)
  173. {
  174. SHPCDevice *shpc = d->shpc;
  175. int nslots = shpc->nslots;
  176. int i;
  177. memset(shpc->config, 0, SHPC_SIZEOF(d));
  178. pci_set_byte(shpc->config + SHPC_NSLOTS, nslots);
  179. pci_set_long(shpc->config + SHPC_SLOTS_33, nslots);
  180. pci_set_long(shpc->config + SHPC_SLOTS_66, 0);
  181. pci_set_byte(shpc->config + SHPC_FIRST_DEV, SHPC_IDX_TO_PCI(0));
  182. pci_set_word(shpc->config + SHPC_PHYS_SLOT,
  183. SHPC_IDX_TO_PHYSICAL(0) |
  184. SHPC_PHYS_NUM_UP |
  185. SHPC_PHYS_MRL |
  186. SHPC_PHYS_BUTTON);
  187. pci_set_long(shpc->config + SHPC_SERR_INT, SHPC_INT_DIS |
  188. SHPC_SERR_DIS |
  189. SHPC_CMD_INT_DIS |
  190. SHPC_ARB_SERR_DIS);
  191. pci_set_byte(shpc->config + SHPC_PROG_IFC, SHPC_PROG_IFC_1_0);
  192. pci_set_word(shpc->config + SHPC_SEC_BUS, SHPC_SEC_BUS_33);
  193. for (i = 0; i < shpc->nslots; ++i) {
  194. pci_set_byte(shpc->config + SHPC_SLOT_EVENT_SERR_INT_DIS(d, i),
  195. SHPC_SLOT_EVENT_PRESENCE |
  196. SHPC_SLOT_EVENT_ISOLATED_FAULT |
  197. SHPC_SLOT_EVENT_BUTTON |
  198. SHPC_SLOT_EVENT_MRL |
  199. SHPC_SLOT_EVENT_CONNECTED_FAULT |
  200. SHPC_SLOT_EVENT_MRL_SERR_DIS |
  201. SHPC_SLOT_EVENT_CONNECTED_FAULT_SERR_DIS);
  202. if (shpc->sec_bus->devices[PCI_DEVFN(SHPC_IDX_TO_PCI(i), 0)]) {
  203. shpc_set_status(shpc, i, SHPC_STATE_ENABLED, SHPC_SLOT_STATE_MASK);
  204. shpc_set_status(shpc, i, 0, SHPC_SLOT_STATUS_MRL_OPEN);
  205. shpc_set_status(shpc, i, SHPC_SLOT_STATUS_PRSNT_7_5W,
  206. SHPC_SLOT_STATUS_PRSNT_MASK);
  207. shpc_set_status(shpc, i, SHPC_LED_ON, SHPC_SLOT_PWR_LED_MASK);
  208. } else {
  209. shpc_set_status(shpc, i, SHPC_STATE_DISABLED, SHPC_SLOT_STATE_MASK);
  210. shpc_set_status(shpc, i, 1, SHPC_SLOT_STATUS_MRL_OPEN);
  211. shpc_set_status(shpc, i, SHPC_SLOT_STATUS_PRSNT_EMPTY,
  212. SHPC_SLOT_STATUS_PRSNT_MASK);
  213. shpc_set_status(shpc, i, SHPC_LED_OFF, SHPC_SLOT_PWR_LED_MASK);
  214. }
  215. shpc_set_status(shpc, i, 0, SHPC_SLOT_STATUS_66);
  216. }
  217. shpc_set_sec_bus_speed(shpc, SHPC_SEC_BUS_33);
  218. shpc->msi_requested = 0;
  219. shpc_interrupt_update(d);
  220. }
  221. static void shpc_invalid_command(SHPCDevice *shpc)
  222. {
  223. pci_word_test_and_set_mask(shpc->config + SHPC_CMD_STATUS,
  224. SHPC_CMD_STATUS_INVALID_CMD);
  225. }
  226. static void shpc_free_devices_in_slot(SHPCDevice *shpc, int slot)
  227. {
  228. int devfn;
  229. int pci_slot = SHPC_IDX_TO_PCI(slot);
  230. for (devfn = PCI_DEVFN(pci_slot, 0);
  231. devfn <= PCI_DEVFN(pci_slot, PCI_FUNC_MAX - 1);
  232. ++devfn) {
  233. PCIDevice *affected_dev = shpc->sec_bus->devices[devfn];
  234. if (affected_dev) {
  235. object_unparent(OBJECT(affected_dev));
  236. }
  237. }
  238. }
  239. static void shpc_slot_command(SHPCDevice *shpc, uint8_t target,
  240. uint8_t state, uint8_t power, uint8_t attn)
  241. {
  242. uint8_t current_state;
  243. int slot = SHPC_LOGICAL_TO_IDX(target);
  244. if (target < SHPC_CMD_TRGT_MIN || slot >= shpc->nslots) {
  245. shpc_invalid_command(shpc);
  246. return;
  247. }
  248. current_state = shpc_get_status(shpc, slot, SHPC_SLOT_STATE_MASK);
  249. if (current_state == SHPC_STATE_ENABLED && state == SHPC_STATE_PWRONLY) {
  250. shpc_invalid_command(shpc);
  251. return;
  252. }
  253. switch (power) {
  254. case SHPC_LED_NO:
  255. break;
  256. default:
  257. /* TODO: send event to monitor */
  258. shpc_set_status(shpc, slot, power, SHPC_SLOT_PWR_LED_MASK);
  259. }
  260. switch (attn) {
  261. case SHPC_LED_NO:
  262. break;
  263. default:
  264. /* TODO: send event to monitor */
  265. shpc_set_status(shpc, slot, attn, SHPC_SLOT_ATTN_LED_MASK);
  266. }
  267. if ((current_state == SHPC_STATE_DISABLED && state == SHPC_STATE_PWRONLY) ||
  268. (current_state == SHPC_STATE_DISABLED && state == SHPC_STATE_ENABLED)) {
  269. shpc_set_status(shpc, slot, state, SHPC_SLOT_STATE_MASK);
  270. } else if ((current_state == SHPC_STATE_ENABLED ||
  271. current_state == SHPC_STATE_PWRONLY) &&
  272. state == SHPC_STATE_DISABLED) {
  273. shpc_set_status(shpc, slot, state, SHPC_SLOT_STATE_MASK);
  274. power = shpc_get_status(shpc, slot, SHPC_SLOT_PWR_LED_MASK);
  275. /* TODO: track what monitor requested. */
  276. /* Look at LED to figure out whether it's ok to remove the device. */
  277. if (power == SHPC_LED_OFF) {
  278. shpc_free_devices_in_slot(shpc, slot);
  279. shpc_set_status(shpc, slot, 1, SHPC_SLOT_STATUS_MRL_OPEN);
  280. shpc_set_status(shpc, slot, SHPC_SLOT_STATUS_PRSNT_EMPTY,
  281. SHPC_SLOT_STATUS_PRSNT_MASK);
  282. shpc->config[SHPC_SLOT_EVENT_LATCH(slot)] |=
  283. SHPC_SLOT_EVENT_BUTTON |
  284. SHPC_SLOT_EVENT_MRL |
  285. SHPC_SLOT_EVENT_PRESENCE;
  286. }
  287. }
  288. }
  289. static void shpc_command(SHPCDevice *shpc)
  290. {
  291. uint8_t code = pci_get_byte(shpc->config + SHPC_CMD_CODE);
  292. uint8_t speed;
  293. uint8_t target;
  294. uint8_t attn;
  295. uint8_t power;
  296. uint8_t state;
  297. int i;
  298. /* Clear status from the previous command. */
  299. pci_word_test_and_clear_mask(shpc->config + SHPC_CMD_STATUS,
  300. SHPC_CMD_STATUS_BUSY |
  301. SHPC_CMD_STATUS_MRL_OPEN |
  302. SHPC_CMD_STATUS_INVALID_CMD |
  303. SHPC_CMD_STATUS_INVALID_MODE);
  304. switch (code) {
  305. case 0x00 ... 0x3f:
  306. target = shpc->config[SHPC_CMD_TRGT] & SHPC_CMD_TRGT_MAX;
  307. state = (code & SHPC_SLOT_STATE_MASK) >> SHPC_SLOT_STATE_SHIFT;
  308. power = (code & SHPC_SLOT_PWR_LED_MASK) >> SHPC_SLOT_PWR_LED_SHIFT;
  309. attn = (code & SHPC_SLOT_ATTN_LED_MASK) >> SHPC_SLOT_ATTN_LED_SHIFT;
  310. shpc_slot_command(shpc, target, state, power, attn);
  311. break;
  312. case 0x40 ... 0x47:
  313. speed = code & SHPC_SEC_BUS_MASK;
  314. shpc_set_sec_bus_speed(shpc, speed);
  315. break;
  316. case 0x48:
  317. /* Power only all slots */
  318. /* first verify no slots are enabled */
  319. for (i = 0; i < shpc->nslots; ++i) {
  320. state = shpc_get_status(shpc, i, SHPC_SLOT_STATE_MASK);
  321. if (state == SHPC_STATE_ENABLED) {
  322. shpc_invalid_command(shpc);
  323. goto done;
  324. }
  325. }
  326. for (i = 0; i < shpc->nslots; ++i) {
  327. if (!(shpc_get_status(shpc, i, SHPC_SLOT_STATUS_MRL_OPEN))) {
  328. shpc_slot_command(shpc, i + SHPC_CMD_TRGT_MIN,
  329. SHPC_STATE_PWRONLY, SHPC_LED_ON, SHPC_LED_NO);
  330. } else {
  331. shpc_slot_command(shpc, i + SHPC_CMD_TRGT_MIN,
  332. SHPC_STATE_NO, SHPC_LED_OFF, SHPC_LED_NO);
  333. }
  334. }
  335. break;
  336. case 0x49:
  337. /* Enable all slots */
  338. /* TODO: Spec says this shall fail if some are already enabled.
  339. * This doesn't make sense - why not? a spec bug? */
  340. for (i = 0; i < shpc->nslots; ++i) {
  341. state = shpc_get_status(shpc, i, SHPC_SLOT_STATE_MASK);
  342. if (state == SHPC_STATE_ENABLED) {
  343. shpc_invalid_command(shpc);
  344. goto done;
  345. }
  346. }
  347. for (i = 0; i < shpc->nslots; ++i) {
  348. if (!(shpc_get_status(shpc, i, SHPC_SLOT_STATUS_MRL_OPEN))) {
  349. shpc_slot_command(shpc, i + SHPC_CMD_TRGT_MIN,
  350. SHPC_STATE_ENABLED, SHPC_LED_ON, SHPC_LED_NO);
  351. } else {
  352. shpc_slot_command(shpc, i + SHPC_CMD_TRGT_MIN,
  353. SHPC_STATE_NO, SHPC_LED_OFF, SHPC_LED_NO);
  354. }
  355. }
  356. break;
  357. default:
  358. shpc_invalid_command(shpc);
  359. break;
  360. }
  361. done:
  362. pci_long_test_and_set_mask(shpc->config + SHPC_SERR_INT, SHPC_CMD_DETECTED);
  363. }
  364. static void shpc_write(PCIDevice *d, unsigned addr, uint64_t val, int l)
  365. {
  366. SHPCDevice *shpc = d->shpc;
  367. int i;
  368. if (addr >= SHPC_SIZEOF(d)) {
  369. return;
  370. }
  371. l = MIN(l, SHPC_SIZEOF(d) - addr);
  372. /* TODO: code duplicated from pci.c */
  373. for (i = 0; i < l; val >>= 8, ++i) {
  374. unsigned a = addr + i;
  375. uint8_t wmask = shpc->wmask[a];
  376. uint8_t w1cmask = shpc->w1cmask[a];
  377. assert(!(wmask & w1cmask));
  378. shpc->config[a] = (shpc->config[a] & ~wmask) | (val & wmask);
  379. shpc->config[a] &= ~(val & w1cmask); /* W1C: Write 1 to Clear */
  380. }
  381. if (ranges_overlap(addr, l, SHPC_CMD_CODE, 2)) {
  382. shpc_command(shpc);
  383. }
  384. shpc_interrupt_update(d);
  385. }
  386. static uint64_t shpc_read(PCIDevice *d, unsigned addr, int l)
  387. {
  388. uint64_t val = 0x0;
  389. if (addr >= SHPC_SIZEOF(d)) {
  390. return val;
  391. }
  392. l = MIN(l, SHPC_SIZEOF(d) - addr);
  393. memcpy(&val, d->shpc->config + addr, l);
  394. return val;
  395. }
  396. /* SHPC Bridge Capability */
  397. #define SHPC_CAP_LENGTH 0x08
  398. #define SHPC_CAP_DWORD_SELECT 0x2 /* 1 byte */
  399. #define SHPC_CAP_CxP 0x3 /* 1 byte: CSP, CIP */
  400. #define SHPC_CAP_DWORD_DATA 0x4 /* 4 bytes */
  401. #define SHPC_CAP_CSP_MASK 0x4
  402. #define SHPC_CAP_CIP_MASK 0x8
  403. static uint8_t shpc_cap_dword(PCIDevice *d)
  404. {
  405. return pci_get_byte(d->config + d->shpc->cap + SHPC_CAP_DWORD_SELECT);
  406. }
  407. /* Update dword data capability register */
  408. static void shpc_cap_update_dword(PCIDevice *d)
  409. {
  410. unsigned data;
  411. data = shpc_read(d, shpc_cap_dword(d) * 4, 4);
  412. pci_set_long(d->config + d->shpc->cap + SHPC_CAP_DWORD_DATA, data);
  413. }
  414. /* Add SHPC capability to the config space for the device. */
  415. static int shpc_cap_add_config(PCIDevice *d)
  416. {
  417. uint8_t *config;
  418. int config_offset;
  419. config_offset = pci_add_capability(d, PCI_CAP_ID_SHPC,
  420. 0, SHPC_CAP_LENGTH);
  421. if (config_offset < 0) {
  422. return config_offset;
  423. }
  424. config = d->config + config_offset;
  425. pci_set_byte(config + SHPC_CAP_DWORD_SELECT, 0);
  426. pci_set_byte(config + SHPC_CAP_CxP, 0);
  427. pci_set_long(config + SHPC_CAP_DWORD_DATA, 0);
  428. d->shpc->cap = config_offset;
  429. /* Make dword select and data writeable. */
  430. pci_set_byte(d->wmask + config_offset + SHPC_CAP_DWORD_SELECT, 0xff);
  431. pci_set_long(d->wmask + config_offset + SHPC_CAP_DWORD_DATA, 0xffffffff);
  432. return 0;
  433. }
  434. static uint64_t shpc_mmio_read(void *opaque, hwaddr addr,
  435. unsigned size)
  436. {
  437. return shpc_read(opaque, addr, size);
  438. }
  439. static void shpc_mmio_write(void *opaque, hwaddr addr,
  440. uint64_t val, unsigned size)
  441. {
  442. shpc_write(opaque, addr, val, size);
  443. }
  444. static const MemoryRegionOps shpc_mmio_ops = {
  445. .read = shpc_mmio_read,
  446. .write = shpc_mmio_write,
  447. .endianness = DEVICE_LITTLE_ENDIAN,
  448. .valid = {
  449. /* SHPC ECN requires dword accesses, but the original 1.0 spec doesn't.
  450. * It's easier to suppport all sizes than worry about it. */
  451. .min_access_size = 1,
  452. .max_access_size = 4,
  453. },
  454. };
  455. static void shpc_device_hotplug_common(PCIDevice *affected_dev, int *slot,
  456. SHPCDevice *shpc, Error **errp)
  457. {
  458. int pci_slot = PCI_SLOT(affected_dev->devfn);
  459. *slot = SHPC_PCI_TO_IDX(pci_slot);
  460. if (pci_slot < SHPC_IDX_TO_PCI(0) || *slot >= shpc->nslots) {
  461. error_setg(errp, "Unsupported PCI slot %d for standard hotplug "
  462. "controller. Valid slots are between %d and %d.",
  463. pci_slot, SHPC_IDX_TO_PCI(0),
  464. SHPC_IDX_TO_PCI(shpc->nslots) - 1);
  465. return;
  466. }
  467. }
  468. void shpc_device_hotplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
  469. Error **errp)
  470. {
  471. Error *local_err = NULL;
  472. PCIDevice *pci_hotplug_dev = PCI_DEVICE(hotplug_dev);
  473. SHPCDevice *shpc = pci_hotplug_dev->shpc;
  474. int slot;
  475. shpc_device_hotplug_common(PCI_DEVICE(dev), &slot, shpc, &local_err);
  476. if (local_err) {
  477. error_propagate(errp, local_err);
  478. return;
  479. }
  480. /* Don't send event when device is enabled during qemu machine creation:
  481. * it is present on boot, no hotplug event is necessary. We do send an
  482. * event when the device is disabled later. */
  483. if (!dev->hotplugged) {
  484. shpc_set_status(shpc, slot, 0, SHPC_SLOT_STATUS_MRL_OPEN);
  485. shpc_set_status(shpc, slot, SHPC_SLOT_STATUS_PRSNT_7_5W,
  486. SHPC_SLOT_STATUS_PRSNT_MASK);
  487. return;
  488. }
  489. /* This could be a cancellation of the previous removal.
  490. * We check MRL state to figure out. */
  491. if (shpc_get_status(shpc, slot, SHPC_SLOT_STATUS_MRL_OPEN)) {
  492. shpc_set_status(shpc, slot, 0, SHPC_SLOT_STATUS_MRL_OPEN);
  493. shpc_set_status(shpc, slot, SHPC_SLOT_STATUS_PRSNT_7_5W,
  494. SHPC_SLOT_STATUS_PRSNT_MASK);
  495. shpc->config[SHPC_SLOT_EVENT_LATCH(slot)] |=
  496. SHPC_SLOT_EVENT_BUTTON |
  497. SHPC_SLOT_EVENT_MRL |
  498. SHPC_SLOT_EVENT_PRESENCE;
  499. } else {
  500. /* Press attention button to cancel removal */
  501. shpc->config[SHPC_SLOT_EVENT_LATCH(slot)] |=
  502. SHPC_SLOT_EVENT_BUTTON;
  503. }
  504. shpc_set_status(shpc, slot, 0, SHPC_SLOT_STATUS_66);
  505. shpc_interrupt_update(pci_hotplug_dev);
  506. }
  507. void shpc_device_hot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
  508. Error **errp)
  509. {
  510. Error *local_err = NULL;
  511. PCIDevice *pci_hotplug_dev = PCI_DEVICE(hotplug_dev);
  512. SHPCDevice *shpc = pci_hotplug_dev->shpc;
  513. uint8_t state;
  514. uint8_t led;
  515. int slot;
  516. shpc_device_hotplug_common(PCI_DEVICE(dev), &slot, shpc, errp);
  517. if (local_err) {
  518. return;
  519. }
  520. shpc->config[SHPC_SLOT_EVENT_LATCH(slot)] |= SHPC_SLOT_EVENT_BUTTON;
  521. state = shpc_get_status(shpc, slot, SHPC_SLOT_STATE_MASK);
  522. led = shpc_get_status(shpc, slot, SHPC_SLOT_PWR_LED_MASK);
  523. if (state == SHPC_STATE_DISABLED && led == SHPC_LED_OFF) {
  524. shpc_free_devices_in_slot(shpc, slot);
  525. shpc_set_status(shpc, slot, 1, SHPC_SLOT_STATUS_MRL_OPEN);
  526. shpc_set_status(shpc, slot, SHPC_SLOT_STATUS_PRSNT_EMPTY,
  527. SHPC_SLOT_STATUS_PRSNT_MASK);
  528. shpc->config[SHPC_SLOT_EVENT_LATCH(slot)] |=
  529. SHPC_SLOT_EVENT_MRL |
  530. SHPC_SLOT_EVENT_PRESENCE;
  531. }
  532. shpc_set_status(shpc, slot, 0, SHPC_SLOT_STATUS_66);
  533. shpc_interrupt_update(pci_hotplug_dev);
  534. }
  535. /* Initialize the SHPC structure in bridge's BAR. */
  536. int shpc_init(PCIDevice *d, PCIBus *sec_bus, MemoryRegion *bar, unsigned offset)
  537. {
  538. int i, ret;
  539. int nslots = SHPC_MAX_SLOTS; /* TODO: qdev property? */
  540. SHPCDevice *shpc = d->shpc = g_malloc0(sizeof(*d->shpc));
  541. shpc->sec_bus = sec_bus;
  542. ret = shpc_cap_add_config(d);
  543. if (ret) {
  544. g_free(d->shpc);
  545. return ret;
  546. }
  547. if (nslots < SHPC_MIN_SLOTS) {
  548. return 0;
  549. }
  550. if (nslots > SHPC_MAX_SLOTS ||
  551. SHPC_IDX_TO_PCI(nslots) > PCI_SLOT_MAX) {
  552. /* TODO: report an error mesage that makes sense. */
  553. return -EINVAL;
  554. }
  555. shpc->nslots = nslots;
  556. shpc->config = g_malloc0(SHPC_SIZEOF(d));
  557. shpc->cmask = g_malloc0(SHPC_SIZEOF(d));
  558. shpc->wmask = g_malloc0(SHPC_SIZEOF(d));
  559. shpc->w1cmask = g_malloc0(SHPC_SIZEOF(d));
  560. shpc_reset(d);
  561. pci_set_long(shpc->config + SHPC_BASE_OFFSET, offset);
  562. pci_set_byte(shpc->wmask + SHPC_CMD_CODE, 0xff);
  563. pci_set_byte(shpc->wmask + SHPC_CMD_TRGT, SHPC_CMD_TRGT_MAX);
  564. pci_set_byte(shpc->wmask + SHPC_CMD_TRGT, SHPC_CMD_TRGT_MAX);
  565. pci_set_long(shpc->wmask + SHPC_SERR_INT,
  566. SHPC_INT_DIS |
  567. SHPC_SERR_DIS |
  568. SHPC_CMD_INT_DIS |
  569. SHPC_ARB_SERR_DIS);
  570. pci_set_long(shpc->w1cmask + SHPC_SERR_INT,
  571. SHPC_CMD_DETECTED |
  572. SHPC_ARB_DETECTED);
  573. for (i = 0; i < nslots; ++i) {
  574. pci_set_byte(shpc->wmask +
  575. SHPC_SLOT_EVENT_SERR_INT_DIS(d, i),
  576. SHPC_SLOT_EVENT_PRESENCE |
  577. SHPC_SLOT_EVENT_ISOLATED_FAULT |
  578. SHPC_SLOT_EVENT_BUTTON |
  579. SHPC_SLOT_EVENT_MRL |
  580. SHPC_SLOT_EVENT_CONNECTED_FAULT |
  581. SHPC_SLOT_EVENT_MRL_SERR_DIS |
  582. SHPC_SLOT_EVENT_CONNECTED_FAULT_SERR_DIS);
  583. pci_set_byte(shpc->w1cmask +
  584. SHPC_SLOT_EVENT_LATCH(i),
  585. SHPC_SLOT_EVENT_PRESENCE |
  586. SHPC_SLOT_EVENT_ISOLATED_FAULT |
  587. SHPC_SLOT_EVENT_BUTTON |
  588. SHPC_SLOT_EVENT_MRL |
  589. SHPC_SLOT_EVENT_CONNECTED_FAULT);
  590. }
  591. /* TODO: init cmask */
  592. memory_region_init_io(&shpc->mmio, OBJECT(d), &shpc_mmio_ops,
  593. d, "shpc-mmio", SHPC_SIZEOF(d));
  594. shpc_cap_update_dword(d);
  595. memory_region_add_subregion(bar, offset, &shpc->mmio);
  596. qbus_set_hotplug_handler(BUS(sec_bus), DEVICE(d), NULL);
  597. d->cap_present |= QEMU_PCI_CAP_SHPC;
  598. return 0;
  599. }
  600. int shpc_bar_size(PCIDevice *d)
  601. {
  602. return roundup_pow_of_two(SHPC_SLOT_REG(SHPC_MAX_SLOTS));
  603. }
  604. void shpc_cleanup(PCIDevice *d, MemoryRegion *bar)
  605. {
  606. SHPCDevice *shpc = d->shpc;
  607. d->cap_present &= ~QEMU_PCI_CAP_SHPC;
  608. memory_region_del_subregion(bar, &shpc->mmio);
  609. /* TODO: cleanup config space changes? */
  610. g_free(shpc->config);
  611. g_free(shpc->cmask);
  612. g_free(shpc->wmask);
  613. g_free(shpc->w1cmask);
  614. memory_region_destroy(&shpc->mmio);
  615. g_free(shpc);
  616. }
  617. void shpc_cap_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
  618. {
  619. if (!ranges_overlap(addr, l, d->shpc->cap, SHPC_CAP_LENGTH)) {
  620. return;
  621. }
  622. if (ranges_overlap(addr, l, d->shpc->cap + SHPC_CAP_DWORD_DATA, 4)) {
  623. unsigned dword_data;
  624. dword_data = pci_get_long(d->shpc->config + d->shpc->cap
  625. + SHPC_CAP_DWORD_DATA);
  626. shpc_write(d, shpc_cap_dword(d) * 4, dword_data, 4);
  627. }
  628. /* Update cap dword data in case guest is going to read it. */
  629. shpc_cap_update_dword(d);
  630. }
  631. static void shpc_save(QEMUFile *f, void *pv, size_t size)
  632. {
  633. PCIDevice *d = container_of(pv, PCIDevice, shpc);
  634. qemu_put_buffer(f, d->shpc->config, SHPC_SIZEOF(d));
  635. }
  636. static int shpc_load(QEMUFile *f, void *pv, size_t size)
  637. {
  638. PCIDevice *d = container_of(pv, PCIDevice, shpc);
  639. int ret = qemu_get_buffer(f, d->shpc->config, SHPC_SIZEOF(d));
  640. if (ret != SHPC_SIZEOF(d)) {
  641. return -EINVAL;
  642. }
  643. /* Make sure we don't lose notifications. An extra interrupt is harmless. */
  644. d->shpc->msi_requested = 0;
  645. shpc_interrupt_update(d);
  646. return 0;
  647. }
  648. VMStateInfo shpc_vmstate_info = {
  649. .name = "shpc",
  650. .get = shpc_load,
  651. .put = shpc_save,
  652. };