shpc.c 25 KB

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