2
0

shpc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. #include "qemu/osdep.h"
  2. #include "qapi/error.h"
  3. #include "qemu/host-utils.h"
  4. #include "qemu/range.h"
  5. #include "qemu/error-report.h"
  6. #include "hw/pci/shpc.h"
  7. #include "migration/qemu-file-types.h"
  8. #include "hw/pci/pci.h"
  9. #include "hw/pci/pci_bus.h"
  10. #include "hw/pci/msi.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. ctz32(SHPC_SLOT_STATE_MASK)
  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. ctz32(SHPC_SLOT_PWR_LED_MASK)
  68. #define SHPC_SLOT_ATTN_LED_MASK 0x30
  69. #define SHPC_SLOT_ATTN_LED_SHIFT \
  70. ctz32(SHPC_SLOT_ATTN_LED_MASK)
  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 uint16_t shpc_get_status(SHPCDevice *shpc, int slot, uint16_t msk)
  112. {
  113. uint8_t *status = shpc->config + SHPC_SLOT_STATUS(slot);
  114. return (pci_get_word(status) & msk) >> ctz32(msk);
  115. }
  116. static void shpc_set_status(SHPCDevice *shpc,
  117. int slot, uint8_t value, uint16_t msk)
  118. {
  119. uint8_t *status = shpc->config + SHPC_SLOT_STATUS(slot);
  120. pci_word_test_and_clear_mask(status, msk);
  121. pci_word_test_and_set_mask(status, value << ctz32(msk));
  122. }
  123. static void shpc_interrupt_update(PCIDevice *d)
  124. {
  125. SHPCDevice *shpc = d->shpc;
  126. int slot;
  127. int level = 0;
  128. uint32_t serr_int;
  129. uint32_t int_locator = 0;
  130. /* Update interrupt locator register */
  131. for (slot = 0; slot < shpc->nslots; ++slot) {
  132. uint8_t event = shpc->config[SHPC_SLOT_EVENT_LATCH(slot)];
  133. uint8_t disable = shpc->config[SHPC_SLOT_EVENT_SERR_INT_DIS(d, slot)];
  134. uint32_t mask = 1U << SHPC_IDX_TO_LOGICAL(slot);
  135. if (event & ~disable) {
  136. int_locator |= mask;
  137. }
  138. }
  139. serr_int = pci_get_long(shpc->config + SHPC_SERR_INT);
  140. if ((serr_int & SHPC_CMD_DETECTED) && !(serr_int & SHPC_CMD_INT_DIS)) {
  141. int_locator |= SHPC_INT_COMMAND;
  142. }
  143. pci_set_long(shpc->config + SHPC_INT_LOCATOR, int_locator);
  144. level = (!(serr_int & SHPC_INT_DIS) && int_locator) ? 1 : 0;
  145. if (msi_enabled(d) && shpc->msi_requested != level)
  146. msi_notify(d, 0);
  147. else
  148. pci_set_irq(d, level);
  149. shpc->msi_requested = level;
  150. }
  151. static void shpc_set_sec_bus_speed(SHPCDevice *shpc, uint8_t speed)
  152. {
  153. switch (speed) {
  154. case SHPC_SEC_BUS_33:
  155. shpc->config[SHPC_SEC_BUS] &= ~SHPC_SEC_BUS_MASK;
  156. shpc->config[SHPC_SEC_BUS] |= speed;
  157. break;
  158. default:
  159. pci_word_test_and_set_mask(shpc->config + SHPC_CMD_STATUS,
  160. SHPC_CMD_STATUS_INVALID_MODE);
  161. }
  162. }
  163. void shpc_reset(PCIDevice *d)
  164. {
  165. SHPCDevice *shpc = d->shpc;
  166. int nslots = shpc->nslots;
  167. int i;
  168. memset(shpc->config, 0, SHPC_SIZEOF(d));
  169. pci_set_byte(shpc->config + SHPC_NSLOTS, nslots);
  170. pci_set_long(shpc->config + SHPC_SLOTS_33, nslots);
  171. pci_set_long(shpc->config + SHPC_SLOTS_66, 0);
  172. pci_set_byte(shpc->config + SHPC_FIRST_DEV, SHPC_IDX_TO_PCI(0));
  173. pci_set_word(shpc->config + SHPC_PHYS_SLOT,
  174. SHPC_IDX_TO_PHYSICAL(0) |
  175. SHPC_PHYS_NUM_UP |
  176. SHPC_PHYS_MRL |
  177. SHPC_PHYS_BUTTON);
  178. pci_set_long(shpc->config + SHPC_SERR_INT, SHPC_INT_DIS |
  179. SHPC_SERR_DIS |
  180. SHPC_CMD_INT_DIS |
  181. SHPC_ARB_SERR_DIS);
  182. pci_set_byte(shpc->config + SHPC_PROG_IFC, SHPC_PROG_IFC_1_0);
  183. pci_set_word(shpc->config + SHPC_SEC_BUS, SHPC_SEC_BUS_33);
  184. for (i = 0; i < shpc->nslots; ++i) {
  185. pci_set_byte(shpc->config + SHPC_SLOT_EVENT_SERR_INT_DIS(d, i),
  186. SHPC_SLOT_EVENT_PRESENCE |
  187. SHPC_SLOT_EVENT_ISOLATED_FAULT |
  188. SHPC_SLOT_EVENT_BUTTON |
  189. SHPC_SLOT_EVENT_MRL |
  190. SHPC_SLOT_EVENT_CONNECTED_FAULT |
  191. SHPC_SLOT_EVENT_MRL_SERR_DIS |
  192. SHPC_SLOT_EVENT_CONNECTED_FAULT_SERR_DIS);
  193. if (shpc->sec_bus->devices[PCI_DEVFN(SHPC_IDX_TO_PCI(i), 0)]) {
  194. shpc_set_status(shpc, i, SHPC_STATE_ENABLED, SHPC_SLOT_STATE_MASK);
  195. shpc_set_status(shpc, i, 0, SHPC_SLOT_STATUS_MRL_OPEN);
  196. shpc_set_status(shpc, i, SHPC_SLOT_STATUS_PRSNT_7_5W,
  197. SHPC_SLOT_STATUS_PRSNT_MASK);
  198. shpc_set_status(shpc, i, SHPC_LED_ON, SHPC_SLOT_PWR_LED_MASK);
  199. } else {
  200. shpc_set_status(shpc, i, SHPC_STATE_DISABLED, SHPC_SLOT_STATE_MASK);
  201. shpc_set_status(shpc, i, 1, SHPC_SLOT_STATUS_MRL_OPEN);
  202. shpc_set_status(shpc, i, SHPC_SLOT_STATUS_PRSNT_EMPTY,
  203. SHPC_SLOT_STATUS_PRSNT_MASK);
  204. shpc_set_status(shpc, i, SHPC_LED_OFF, SHPC_SLOT_PWR_LED_MASK);
  205. }
  206. shpc_set_status(shpc, i, 0, SHPC_SLOT_STATUS_66);
  207. }
  208. shpc_set_sec_bus_speed(shpc, SHPC_SEC_BUS_33);
  209. shpc->msi_requested = 0;
  210. shpc_interrupt_update(d);
  211. }
  212. static void shpc_invalid_command(SHPCDevice *shpc)
  213. {
  214. pci_word_test_and_set_mask(shpc->config + SHPC_CMD_STATUS,
  215. SHPC_CMD_STATUS_INVALID_CMD);
  216. }
  217. static void shpc_free_devices_in_slot(SHPCDevice *shpc, int slot)
  218. {
  219. HotplugHandler *hotplug_ctrl;
  220. int devfn;
  221. int pci_slot = SHPC_IDX_TO_PCI(slot);
  222. for (devfn = PCI_DEVFN(pci_slot, 0);
  223. devfn <= PCI_DEVFN(pci_slot, PCI_FUNC_MAX - 1);
  224. ++devfn) {
  225. PCIDevice *affected_dev = shpc->sec_bus->devices[devfn];
  226. if (affected_dev) {
  227. hotplug_ctrl = qdev_get_hotplug_handler(DEVICE(affected_dev));
  228. hotplug_handler_unplug(hotplug_ctrl, DEVICE(affected_dev),
  229. &error_abort);
  230. object_unparent(OBJECT(affected_dev));
  231. }
  232. }
  233. }
  234. static void shpc_slot_command(SHPCDevice *shpc, uint8_t target,
  235. uint8_t state, uint8_t power, uint8_t attn)
  236. {
  237. uint8_t current_state;
  238. int slot = SHPC_LOGICAL_TO_IDX(target);
  239. if (target < SHPC_CMD_TRGT_MIN || slot >= shpc->nslots) {
  240. shpc_invalid_command(shpc);
  241. return;
  242. }
  243. current_state = shpc_get_status(shpc, slot, SHPC_SLOT_STATE_MASK);
  244. if (current_state == SHPC_STATE_ENABLED && state == SHPC_STATE_PWRONLY) {
  245. shpc_invalid_command(shpc);
  246. return;
  247. }
  248. switch (power) {
  249. case SHPC_LED_NO:
  250. break;
  251. default:
  252. /* TODO: send event to monitor */
  253. shpc_set_status(shpc, slot, power, SHPC_SLOT_PWR_LED_MASK);
  254. }
  255. switch (attn) {
  256. case SHPC_LED_NO:
  257. break;
  258. default:
  259. /* TODO: send event to monitor */
  260. shpc_set_status(shpc, slot, attn, SHPC_SLOT_ATTN_LED_MASK);
  261. }
  262. if ((current_state == SHPC_STATE_DISABLED && state == SHPC_STATE_PWRONLY) ||
  263. (current_state == SHPC_STATE_DISABLED && state == SHPC_STATE_ENABLED)) {
  264. shpc_set_status(shpc, slot, state, SHPC_SLOT_STATE_MASK);
  265. } else if ((current_state == SHPC_STATE_ENABLED ||
  266. current_state == SHPC_STATE_PWRONLY) &&
  267. state == SHPC_STATE_DISABLED) {
  268. shpc_set_status(shpc, slot, state, SHPC_SLOT_STATE_MASK);
  269. power = shpc_get_status(shpc, slot, SHPC_SLOT_PWR_LED_MASK);
  270. /* TODO: track what monitor requested. */
  271. /* Look at LED to figure out whether it's ok to remove the device. */
  272. if (power == SHPC_LED_OFF) {
  273. shpc_free_devices_in_slot(shpc, slot);
  274. shpc_set_status(shpc, slot, 1, SHPC_SLOT_STATUS_MRL_OPEN);
  275. shpc_set_status(shpc, slot, SHPC_SLOT_STATUS_PRSNT_EMPTY,
  276. SHPC_SLOT_STATUS_PRSNT_MASK);
  277. shpc->config[SHPC_SLOT_EVENT_LATCH(slot)] |=
  278. SHPC_SLOT_EVENT_BUTTON |
  279. SHPC_SLOT_EVENT_MRL |
  280. SHPC_SLOT_EVENT_PRESENCE;
  281. }
  282. }
  283. }
  284. static void shpc_command(SHPCDevice *shpc)
  285. {
  286. uint8_t code = pci_get_byte(shpc->config + SHPC_CMD_CODE);
  287. uint8_t speed;
  288. uint8_t target;
  289. uint8_t attn;
  290. uint8_t power;
  291. uint8_t state;
  292. int i;
  293. /* Clear status from the previous command. */
  294. pci_word_test_and_clear_mask(shpc->config + SHPC_CMD_STATUS,
  295. SHPC_CMD_STATUS_BUSY |
  296. SHPC_CMD_STATUS_MRL_OPEN |
  297. SHPC_CMD_STATUS_INVALID_CMD |
  298. SHPC_CMD_STATUS_INVALID_MODE);
  299. switch (code) {
  300. case 0x00 ... 0x3f:
  301. target = shpc->config[SHPC_CMD_TRGT] & SHPC_CMD_TRGT_MAX;
  302. state = (code & SHPC_SLOT_STATE_MASK) >> SHPC_SLOT_STATE_SHIFT;
  303. power = (code & SHPC_SLOT_PWR_LED_MASK) >> SHPC_SLOT_PWR_LED_SHIFT;
  304. attn = (code & SHPC_SLOT_ATTN_LED_MASK) >> SHPC_SLOT_ATTN_LED_SHIFT;
  305. shpc_slot_command(shpc, target, state, power, attn);
  306. break;
  307. case 0x40 ... 0x47:
  308. speed = code & SHPC_SEC_BUS_MASK;
  309. shpc_set_sec_bus_speed(shpc, speed);
  310. break;
  311. case 0x48:
  312. /* Power only all slots */
  313. /* first verify no slots are enabled */
  314. for (i = 0; i < shpc->nslots; ++i) {
  315. state = shpc_get_status(shpc, i, SHPC_SLOT_STATE_MASK);
  316. if (state == SHPC_STATE_ENABLED) {
  317. shpc_invalid_command(shpc);
  318. goto done;
  319. }
  320. }
  321. for (i = 0; i < shpc->nslots; ++i) {
  322. if (!(shpc_get_status(shpc, i, SHPC_SLOT_STATUS_MRL_OPEN))) {
  323. shpc_slot_command(shpc, i + SHPC_CMD_TRGT_MIN,
  324. SHPC_STATE_PWRONLY, SHPC_LED_ON, SHPC_LED_NO);
  325. } else {
  326. shpc_slot_command(shpc, i + SHPC_CMD_TRGT_MIN,
  327. SHPC_STATE_NO, SHPC_LED_OFF, SHPC_LED_NO);
  328. }
  329. }
  330. break;
  331. case 0x49:
  332. /* Enable all slots */
  333. /* TODO: Spec says this shall fail if some are already enabled.
  334. * This doesn't make sense - why not? a spec bug? */
  335. for (i = 0; i < shpc->nslots; ++i) {
  336. state = shpc_get_status(shpc, i, SHPC_SLOT_STATE_MASK);
  337. if (state == SHPC_STATE_ENABLED) {
  338. shpc_invalid_command(shpc);
  339. goto done;
  340. }
  341. }
  342. for (i = 0; i < shpc->nslots; ++i) {
  343. if (!(shpc_get_status(shpc, i, SHPC_SLOT_STATUS_MRL_OPEN))) {
  344. shpc_slot_command(shpc, i + SHPC_CMD_TRGT_MIN,
  345. SHPC_STATE_ENABLED, SHPC_LED_ON, SHPC_LED_NO);
  346. } else {
  347. shpc_slot_command(shpc, i + SHPC_CMD_TRGT_MIN,
  348. SHPC_STATE_NO, SHPC_LED_OFF, SHPC_LED_NO);
  349. }
  350. }
  351. break;
  352. default:
  353. shpc_invalid_command(shpc);
  354. break;
  355. }
  356. done:
  357. pci_long_test_and_set_mask(shpc->config + SHPC_SERR_INT, SHPC_CMD_DETECTED);
  358. }
  359. static void shpc_write(PCIDevice *d, unsigned addr, uint64_t val, int l)
  360. {
  361. SHPCDevice *shpc = d->shpc;
  362. int i;
  363. if (addr >= SHPC_SIZEOF(d)) {
  364. return;
  365. }
  366. l = MIN(l, SHPC_SIZEOF(d) - addr);
  367. /* TODO: code duplicated from pci.c */
  368. for (i = 0; i < l; val >>= 8, ++i) {
  369. unsigned a = addr + i;
  370. uint8_t wmask = shpc->wmask[a];
  371. uint8_t w1cmask = shpc->w1cmask[a];
  372. assert(!(wmask & w1cmask));
  373. shpc->config[a] = (shpc->config[a] & ~wmask) | (val & wmask);
  374. shpc->config[a] &= ~(val & w1cmask); /* W1C: Write 1 to Clear */
  375. }
  376. if (ranges_overlap(addr, l, SHPC_CMD_CODE, 2)) {
  377. shpc_command(shpc);
  378. }
  379. shpc_interrupt_update(d);
  380. }
  381. static uint64_t shpc_read(PCIDevice *d, unsigned addr, int l)
  382. {
  383. uint64_t val = 0x0;
  384. if (addr >= SHPC_SIZEOF(d)) {
  385. return val;
  386. }
  387. l = MIN(l, SHPC_SIZEOF(d) - addr);
  388. memcpy(&val, d->shpc->config + addr, l);
  389. return val;
  390. }
  391. /* SHPC Bridge Capability */
  392. #define SHPC_CAP_LENGTH 0x08
  393. #define SHPC_CAP_DWORD_SELECT 0x2 /* 1 byte */
  394. #define SHPC_CAP_CxP 0x3 /* 1 byte: CSP, CIP */
  395. #define SHPC_CAP_DWORD_DATA 0x4 /* 4 bytes */
  396. #define SHPC_CAP_CSP_MASK 0x4
  397. #define SHPC_CAP_CIP_MASK 0x8
  398. static uint8_t shpc_cap_dword(PCIDevice *d)
  399. {
  400. return pci_get_byte(d->config + d->shpc->cap + SHPC_CAP_DWORD_SELECT);
  401. }
  402. /* Update dword data capability register */
  403. static void shpc_cap_update_dword(PCIDevice *d)
  404. {
  405. unsigned data;
  406. data = shpc_read(d, shpc_cap_dword(d) * 4, 4);
  407. pci_set_long(d->config + d->shpc->cap + SHPC_CAP_DWORD_DATA, data);
  408. }
  409. /* Add SHPC capability to the config space for the device. */
  410. static int shpc_cap_add_config(PCIDevice *d, Error **errp)
  411. {
  412. uint8_t *config;
  413. int config_offset;
  414. config_offset = pci_add_capability(d, PCI_CAP_ID_SHPC,
  415. 0, SHPC_CAP_LENGTH,
  416. errp);
  417. if (config_offset < 0) {
  418. return config_offset;
  419. }
  420. config = d->config + config_offset;
  421. pci_set_byte(config + SHPC_CAP_DWORD_SELECT, 0);
  422. pci_set_byte(config + SHPC_CAP_CxP, 0);
  423. pci_set_long(config + SHPC_CAP_DWORD_DATA, 0);
  424. d->shpc->cap = config_offset;
  425. /* Make dword select and data writeable. */
  426. pci_set_byte(d->wmask + config_offset + SHPC_CAP_DWORD_SELECT, 0xff);
  427. pci_set_long(d->wmask + config_offset + SHPC_CAP_DWORD_DATA, 0xffffffff);
  428. return 0;
  429. }
  430. static uint64_t shpc_mmio_read(void *opaque, hwaddr addr,
  431. unsigned size)
  432. {
  433. return shpc_read(opaque, addr, size);
  434. }
  435. static void shpc_mmio_write(void *opaque, hwaddr addr,
  436. uint64_t val, unsigned size)
  437. {
  438. shpc_write(opaque, addr, val, size);
  439. }
  440. static const MemoryRegionOps shpc_mmio_ops = {
  441. .read = shpc_mmio_read,
  442. .write = shpc_mmio_write,
  443. .endianness = DEVICE_LITTLE_ENDIAN,
  444. .valid = {
  445. /* SHPC ECN requires dword accesses, but the original 1.0 spec doesn't.
  446. * It's easier to suppport all sizes than worry about it. */
  447. .min_access_size = 1,
  448. .max_access_size = 4,
  449. },
  450. };
  451. static void shpc_device_plug_common(PCIDevice *affected_dev, int *slot,
  452. SHPCDevice *shpc, Error **errp)
  453. {
  454. int pci_slot = PCI_SLOT(affected_dev->devfn);
  455. *slot = SHPC_PCI_TO_IDX(pci_slot);
  456. if (pci_slot < SHPC_IDX_TO_PCI(0) || *slot >= shpc->nslots) {
  457. error_setg(errp, "Unsupported PCI slot %d for standard hotplug "
  458. "controller. Valid slots are between %d and %d.",
  459. pci_slot, SHPC_IDX_TO_PCI(0),
  460. SHPC_IDX_TO_PCI(shpc->nslots) - 1);
  461. return;
  462. }
  463. }
  464. void shpc_device_plug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
  465. Error **errp)
  466. {
  467. Error *local_err = NULL;
  468. PCIDevice *pci_hotplug_dev = PCI_DEVICE(hotplug_dev);
  469. SHPCDevice *shpc = pci_hotplug_dev->shpc;
  470. int slot;
  471. shpc_device_plug_common(PCI_DEVICE(dev), &slot, shpc, &local_err);
  472. if (local_err) {
  473. error_propagate(errp, local_err);
  474. return;
  475. }
  476. /* Don't send event when device is enabled during qemu machine creation:
  477. * it is present on boot, no hotplug event is necessary. We do send an
  478. * event when the device is disabled later. */
  479. if (!dev->hotplugged) {
  480. shpc_set_status(shpc, slot, 0, SHPC_SLOT_STATUS_MRL_OPEN);
  481. shpc_set_status(shpc, slot, SHPC_SLOT_STATUS_PRSNT_7_5W,
  482. SHPC_SLOT_STATUS_PRSNT_MASK);
  483. return;
  484. }
  485. /* This could be a cancellation of the previous removal.
  486. * We check MRL state to figure out. */
  487. if (shpc_get_status(shpc, slot, SHPC_SLOT_STATUS_MRL_OPEN)) {
  488. shpc_set_status(shpc, slot, 0, SHPC_SLOT_STATUS_MRL_OPEN);
  489. shpc_set_status(shpc, slot, SHPC_SLOT_STATUS_PRSNT_7_5W,
  490. SHPC_SLOT_STATUS_PRSNT_MASK);
  491. shpc->config[SHPC_SLOT_EVENT_LATCH(slot)] |=
  492. SHPC_SLOT_EVENT_BUTTON |
  493. SHPC_SLOT_EVENT_MRL |
  494. SHPC_SLOT_EVENT_PRESENCE;
  495. } else {
  496. /* Press attention button to cancel removal */
  497. shpc->config[SHPC_SLOT_EVENT_LATCH(slot)] |=
  498. SHPC_SLOT_EVENT_BUTTON;
  499. }
  500. shpc_set_status(shpc, slot, 0, SHPC_SLOT_STATUS_66);
  501. shpc_interrupt_update(pci_hotplug_dev);
  502. }
  503. void shpc_device_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
  504. Error **errp)
  505. {
  506. object_property_set_bool(OBJECT(dev), false, "realized", NULL);
  507. }
  508. void shpc_device_unplug_request_cb(HotplugHandler *hotplug_dev,
  509. DeviceState *dev, Error **errp)
  510. {
  511. Error *local_err = NULL;
  512. PCIDevice *pci_hotplug_dev = PCI_DEVICE(hotplug_dev);
  513. SHPCDevice *shpc = pci_hotplug_dev->shpc;
  514. uint8_t state;
  515. uint8_t led;
  516. int slot;
  517. shpc_device_plug_common(PCI_DEVICE(dev), &slot, shpc, &local_err);
  518. if (local_err) {
  519. error_propagate(errp, local_err);
  520. return;
  521. }
  522. shpc->config[SHPC_SLOT_EVENT_LATCH(slot)] |= SHPC_SLOT_EVENT_BUTTON;
  523. state = shpc_get_status(shpc, slot, SHPC_SLOT_STATE_MASK);
  524. led = shpc_get_status(shpc, slot, SHPC_SLOT_PWR_LED_MASK);
  525. if (state == SHPC_STATE_DISABLED && led == SHPC_LED_OFF) {
  526. shpc_free_devices_in_slot(shpc, slot);
  527. shpc_set_status(shpc, slot, 1, SHPC_SLOT_STATUS_MRL_OPEN);
  528. shpc_set_status(shpc, slot, SHPC_SLOT_STATUS_PRSNT_EMPTY,
  529. SHPC_SLOT_STATUS_PRSNT_MASK);
  530. shpc->config[SHPC_SLOT_EVENT_LATCH(slot)] |=
  531. SHPC_SLOT_EVENT_MRL |
  532. SHPC_SLOT_EVENT_PRESENCE;
  533. }
  534. shpc_set_status(shpc, slot, 0, SHPC_SLOT_STATUS_66);
  535. shpc_interrupt_update(pci_hotplug_dev);
  536. }
  537. /* Initialize the SHPC structure in bridge's BAR. */
  538. int shpc_init(PCIDevice *d, PCIBus *sec_bus, MemoryRegion *bar,
  539. unsigned offset, Error **errp)
  540. {
  541. int i, ret;
  542. int nslots = SHPC_MAX_SLOTS; /* TODO: qdev property? */
  543. SHPCDevice *shpc = d->shpc = g_malloc0(sizeof(*d->shpc));
  544. shpc->sec_bus = sec_bus;
  545. ret = shpc_cap_add_config(d, errp);
  546. if (ret) {
  547. g_free(d->shpc);
  548. return ret;
  549. }
  550. if (nslots < SHPC_MIN_SLOTS) {
  551. return 0;
  552. }
  553. if (nslots > SHPC_MAX_SLOTS ||
  554. SHPC_IDX_TO_PCI(nslots) > PCI_SLOT_MAX) {
  555. /* TODO: report an error mesage that makes sense. */
  556. return -EINVAL;
  557. }
  558. shpc->nslots = nslots;
  559. shpc->config = g_malloc0(SHPC_SIZEOF(d));
  560. shpc->cmask = g_malloc0(SHPC_SIZEOF(d));
  561. shpc->wmask = g_malloc0(SHPC_SIZEOF(d));
  562. shpc->w1cmask = g_malloc0(SHPC_SIZEOF(d));
  563. shpc_reset(d);
  564. pci_set_long(shpc->config + SHPC_BASE_OFFSET, offset);
  565. pci_set_byte(shpc->wmask + SHPC_CMD_CODE, 0xff);
  566. pci_set_byte(shpc->wmask + SHPC_CMD_TRGT, SHPC_CMD_TRGT_MAX);
  567. pci_set_byte(shpc->wmask + SHPC_CMD_TRGT, SHPC_CMD_TRGT_MAX);
  568. pci_set_long(shpc->wmask + SHPC_SERR_INT,
  569. SHPC_INT_DIS |
  570. SHPC_SERR_DIS |
  571. SHPC_CMD_INT_DIS |
  572. SHPC_ARB_SERR_DIS);
  573. pci_set_long(shpc->w1cmask + SHPC_SERR_INT,
  574. SHPC_CMD_DETECTED |
  575. SHPC_ARB_DETECTED);
  576. for (i = 0; i < nslots; ++i) {
  577. pci_set_byte(shpc->wmask +
  578. SHPC_SLOT_EVENT_SERR_INT_DIS(d, i),
  579. SHPC_SLOT_EVENT_PRESENCE |
  580. SHPC_SLOT_EVENT_ISOLATED_FAULT |
  581. SHPC_SLOT_EVENT_BUTTON |
  582. SHPC_SLOT_EVENT_MRL |
  583. SHPC_SLOT_EVENT_CONNECTED_FAULT |
  584. SHPC_SLOT_EVENT_MRL_SERR_DIS |
  585. SHPC_SLOT_EVENT_CONNECTED_FAULT_SERR_DIS);
  586. pci_set_byte(shpc->w1cmask +
  587. SHPC_SLOT_EVENT_LATCH(i),
  588. SHPC_SLOT_EVENT_PRESENCE |
  589. SHPC_SLOT_EVENT_ISOLATED_FAULT |
  590. SHPC_SLOT_EVENT_BUTTON |
  591. SHPC_SLOT_EVENT_MRL |
  592. SHPC_SLOT_EVENT_CONNECTED_FAULT);
  593. }
  594. /* TODO: init cmask */
  595. memory_region_init_io(&shpc->mmio, OBJECT(d), &shpc_mmio_ops,
  596. d, "shpc-mmio", SHPC_SIZEOF(d));
  597. shpc_cap_update_dword(d);
  598. memory_region_add_subregion(bar, offset, &shpc->mmio);
  599. qbus_set_hotplug_handler(BUS(sec_bus), OBJECT(d), NULL);
  600. d->cap_present |= QEMU_PCI_CAP_SHPC;
  601. return 0;
  602. }
  603. int shpc_bar_size(PCIDevice *d)
  604. {
  605. return pow2roundup32(SHPC_SLOT_REG(SHPC_MAX_SLOTS));
  606. }
  607. void shpc_cleanup(PCIDevice *d, MemoryRegion *bar)
  608. {
  609. SHPCDevice *shpc = d->shpc;
  610. d->cap_present &= ~QEMU_PCI_CAP_SHPC;
  611. memory_region_del_subregion(bar, &shpc->mmio);
  612. /* TODO: cleanup config space changes? */
  613. }
  614. void shpc_free(PCIDevice *d)
  615. {
  616. SHPCDevice *shpc = d->shpc;
  617. if (!shpc) {
  618. return;
  619. }
  620. object_unparent(OBJECT(&shpc->mmio));
  621. g_free(shpc->config);
  622. g_free(shpc->cmask);
  623. g_free(shpc->wmask);
  624. g_free(shpc->w1cmask);
  625. g_free(shpc);
  626. d->shpc = NULL;
  627. }
  628. void shpc_cap_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
  629. {
  630. if (!ranges_overlap(addr, l, d->shpc->cap, SHPC_CAP_LENGTH)) {
  631. return;
  632. }
  633. if (ranges_overlap(addr, l, d->shpc->cap + SHPC_CAP_DWORD_DATA, 4)) {
  634. unsigned dword_data;
  635. dword_data = pci_get_long(d->shpc->config + d->shpc->cap
  636. + SHPC_CAP_DWORD_DATA);
  637. shpc_write(d, shpc_cap_dword(d) * 4, dword_data, 4);
  638. }
  639. /* Update cap dword data in case guest is going to read it. */
  640. shpc_cap_update_dword(d);
  641. }
  642. static int shpc_save(QEMUFile *f, void *pv, size_t size,
  643. const VMStateField *field, QJSON *vmdesc)
  644. {
  645. PCIDevice *d = container_of(pv, PCIDevice, shpc);
  646. qemu_put_buffer(f, d->shpc->config, SHPC_SIZEOF(d));
  647. return 0;
  648. }
  649. static int shpc_load(QEMUFile *f, void *pv, size_t size,
  650. const VMStateField *field)
  651. {
  652. PCIDevice *d = container_of(pv, PCIDevice, shpc);
  653. int ret = qemu_get_buffer(f, d->shpc->config, SHPC_SIZEOF(d));
  654. if (ret != SHPC_SIZEOF(d)) {
  655. return -EINVAL;
  656. }
  657. /* Make sure we don't lose notifications. An extra interrupt is harmless. */
  658. d->shpc->msi_requested = 0;
  659. shpc_interrupt_update(d);
  660. return 0;
  661. }
  662. VMStateInfo shpc_vmstate_info = {
  663. .name = "shpc",
  664. .get = shpc_load,
  665. .put = shpc_save,
  666. };