shpc.c 24 KB

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