pmu.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /*
  2. * mrst/pmu.c - driver for MRST Power Management Unit
  3. *
  4. * Copyright (c) 2011, Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include <linux/cpuidle.h>
  20. #include <linux/debugfs.h>
  21. #include <linux/delay.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/module.h>
  24. #include <linux/pci.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/sfi.h>
  27. #include <asm/intel_scu_ipc.h>
  28. #include "pmu.h"
  29. #define IPCMSG_FW_REVISION 0xF4
  30. struct mrst_device {
  31. u16 pci_dev_num; /* DEBUG only */
  32. u16 lss;
  33. u16 latest_request;
  34. unsigned int pci_state_counts[PCI_D3cold + 1]; /* DEBUG only */
  35. };
  36. /*
  37. * comlete list of MRST PCI devices
  38. */
  39. static struct mrst_device mrst_devs[] = {
  40. /* 0 */ { 0x0800, LSS_SPI0 }, /* Moorestown SPI Ctrl 0 */
  41. /* 1 */ { 0x0801, LSS_SPI1 }, /* Moorestown SPI Ctrl 1 */
  42. /* 2 */ { 0x0802, LSS_I2C0 }, /* Moorestown I2C 0 */
  43. /* 3 */ { 0x0803, LSS_I2C1 }, /* Moorestown I2C 1 */
  44. /* 4 */ { 0x0804, LSS_I2C2 }, /* Moorestown I2C 2 */
  45. /* 5 */ { 0x0805, LSS_KBD }, /* Moorestown Keyboard Ctrl */
  46. /* 6 */ { 0x0806, LSS_USB_HC }, /* Moorestown USB Ctrl */
  47. /* 7 */ { 0x0807, LSS_SD_HC0 }, /* Moorestown SD Host Ctrl 0 */
  48. /* 8 */ { 0x0808, LSS_SD_HC1 }, /* Moorestown SD Host Ctrl 1 */
  49. /* 9 */ { 0x0809, LSS_NAND }, /* Moorestown NAND Ctrl */
  50. /* 10 */ { 0x080a, LSS_AUDIO }, /* Moorestown Audio Ctrl */
  51. /* 11 */ { 0x080b, LSS_IMAGING }, /* Moorestown ISP */
  52. /* 12 */ { 0x080c, LSS_SECURITY }, /* Moorestown Security Controller */
  53. /* 13 */ { 0x080d, LSS_DISPLAY }, /* Moorestown External Displays */
  54. /* 14 */ { 0x080e, 0 }, /* Moorestown SCU IPC */
  55. /* 15 */ { 0x080f, LSS_GPIO }, /* Moorestown GPIO Controller */
  56. /* 16 */ { 0x0810, 0 }, /* Moorestown Power Management Unit */
  57. /* 17 */ { 0x0811, LSS_USB_OTG }, /* Moorestown OTG Ctrl */
  58. /* 18 */ { 0x0812, LSS_SPI2 }, /* Moorestown SPI Ctrl 2 */
  59. /* 19 */ { 0x0813, 0 }, /* Moorestown SC DMA */
  60. /* 20 */ { 0x0814, LSS_AUDIO_LPE }, /* Moorestown LPE DMA */
  61. /* 21 */ { 0x0815, LSS_AUDIO_SSP }, /* Moorestown SSP0 */
  62. /* 22 */ { 0x084F, LSS_SD_HC2 }, /* Moorestown SD Host Ctrl 2 */
  63. /* 23 */ { 0x4102, 0 }, /* Lincroft */
  64. /* 24 */ { 0x4110, 0 }, /* Lincroft */
  65. };
  66. /* n.b. We ignore PCI-id 0x815 in LSS9 b/c Linux has no driver for it */
  67. static u16 mrst_lss9_pci_ids[] = {0x080a, 0x0814, 0};
  68. static u16 mrst_lss10_pci_ids[] = {0x0800, 0x0801, 0x0802, 0x0803,
  69. 0x0804, 0x0805, 0x080f, 0};
  70. /* handle concurrent SMP invokations of pmu_pci_set_power_state() */
  71. static spinlock_t mrst_pmu_power_state_lock;
  72. static unsigned int wake_counters[MRST_NUM_LSS]; /* DEBUG only */
  73. static unsigned int pmu_irq_stats[INT_INVALID + 1]; /* DEBUG only */
  74. static int graphics_is_off;
  75. static int lss_s0i3_enabled;
  76. static bool mrst_pmu_s0i3_enable;
  77. /* debug counters */
  78. static u32 pmu_wait_ready_calls;
  79. static u32 pmu_wait_ready_udelays;
  80. static u32 pmu_wait_ready_udelays_max;
  81. static u32 pmu_wait_done_calls;
  82. static u32 pmu_wait_done_udelays;
  83. static u32 pmu_wait_done_udelays_max;
  84. static u32 pmu_set_power_state_entry;
  85. static u32 pmu_set_power_state_send_cmd;
  86. static struct mrst_device *pci_id_2_mrst_dev(u16 pci_dev_num)
  87. {
  88. int index = 0;
  89. if ((pci_dev_num >= 0x0800) && (pci_dev_num <= 0x815))
  90. index = pci_dev_num - 0x800;
  91. else if (pci_dev_num == 0x084F)
  92. index = 22;
  93. else if (pci_dev_num == 0x4102)
  94. index = 23;
  95. else if (pci_dev_num == 0x4110)
  96. index = 24;
  97. if (pci_dev_num != mrst_devs[index].pci_dev_num) {
  98. WARN_ONCE(1, FW_BUG "Unknown PCI device 0x%04X\n", pci_dev_num);
  99. return 0;
  100. }
  101. return &mrst_devs[index];
  102. }
  103. /**
  104. * mrst_pmu_validate_cstates
  105. * @dev: cpuidle_device
  106. *
  107. * Certain states are not appropriate for governor to pick in some cases.
  108. * This function will be called as cpuidle_device's prepare callback and
  109. * thus tells governor to ignore such states when selecting the next state
  110. * to enter.
  111. */
  112. #define IDLE_STATE4_IS_C6 4
  113. #define IDLE_STATE5_IS_S0I3 5
  114. int mrst_pmu_invalid_cstates(void)
  115. {
  116. int cpu = smp_processor_id();
  117. /*
  118. * Demote to C4 if the PMU is busy.
  119. * Since LSS changes leave the busy bit clear...
  120. * busy means either the PMU is waiting for an ACK-C6 that
  121. * isn't coming due to an MWAIT that returned immediately;
  122. * or we returned from S0i3 successfully, and the PMU
  123. * is not done sending us interrupts.
  124. */
  125. if (pmu_read_busy_status())
  126. return 1 << IDLE_STATE4_IS_C6 | 1 << IDLE_STATE5_IS_S0I3;
  127. /*
  128. * Disallow S0i3 if: PMU is not initialized, or CPU1 is active,
  129. * or if device LSS is insufficient, or the GPU is active,
  130. * or if it has been explicitly disabled.
  131. */
  132. if (!pmu_reg || !cpumask_equal(cpu_online_mask, cpumask_of(cpu)) ||
  133. !lss_s0i3_enabled || !graphics_is_off || !mrst_pmu_s0i3_enable)
  134. return 1 << IDLE_STATE5_IS_S0I3;
  135. else
  136. return 0;
  137. }
  138. /*
  139. * pmu_update_wake_counters(): read PM_WKS, update wake_counters[]
  140. * DEBUG only.
  141. */
  142. static void pmu_update_wake_counters(void)
  143. {
  144. int lss;
  145. u32 wake_status;
  146. wake_status = pmu_read_wks();
  147. for (lss = 0; lss < MRST_NUM_LSS; ++lss) {
  148. if (wake_status & (1 << lss))
  149. wake_counters[lss]++;
  150. }
  151. }
  152. int mrst_pmu_s0i3_entry(void)
  153. {
  154. int status;
  155. /* Clear any possible error conditions */
  156. pmu_write_ics(0x300);
  157. /* set wake control to current D-states */
  158. pmu_write_wssc(S0I3_SSS_TARGET);
  159. status = mrst_s0i3_entry(PM_S0I3_COMMAND, &pmu_reg->pm_cmd);
  160. pmu_update_wake_counters();
  161. return status;
  162. }
  163. /* poll for maximum of 5ms for busy bit to clear */
  164. static int pmu_wait_ready(void)
  165. {
  166. int udelays;
  167. pmu_wait_ready_calls++;
  168. for (udelays = 0; udelays < 500; ++udelays) {
  169. if (udelays > pmu_wait_ready_udelays_max)
  170. pmu_wait_ready_udelays_max = udelays;
  171. if (pmu_read_busy_status() == 0)
  172. return 0;
  173. udelay(10);
  174. pmu_wait_ready_udelays++;
  175. }
  176. /*
  177. * if this fires, observe
  178. * /sys/kernel/debug/mrst_pmu_wait_ready_calls
  179. * /sys/kernel/debug/mrst_pmu_wait_ready_udelays
  180. */
  181. WARN_ONCE(1, "SCU not ready for 5ms");
  182. return -EBUSY;
  183. }
  184. /* poll for maximum of 50ms us for busy bit to clear */
  185. static int pmu_wait_done(void)
  186. {
  187. int udelays;
  188. pmu_wait_done_calls++;
  189. for (udelays = 0; udelays < 500; ++udelays) {
  190. if (udelays > pmu_wait_done_udelays_max)
  191. pmu_wait_done_udelays_max = udelays;
  192. if (pmu_read_busy_status() == 0)
  193. return 0;
  194. udelay(100);
  195. pmu_wait_done_udelays++;
  196. }
  197. /*
  198. * if this fires, observe
  199. * /sys/kernel/debug/mrst_pmu_wait_done_calls
  200. * /sys/kernel/debug/mrst_pmu_wait_done_udelays
  201. */
  202. WARN_ONCE(1, "SCU not done for 50ms");
  203. return -EBUSY;
  204. }
  205. u32 mrst_pmu_msi_is_disabled(void)
  206. {
  207. return pmu_msi_is_disabled();
  208. }
  209. void mrst_pmu_enable_msi(void)
  210. {
  211. pmu_msi_enable();
  212. }
  213. /**
  214. * pmu_irq - pmu driver interrupt handler
  215. * Context: interrupt context
  216. */
  217. static irqreturn_t pmu_irq(int irq, void *dummy)
  218. {
  219. union pmu_pm_ics pmu_ics;
  220. pmu_ics.value = pmu_read_ics();
  221. if (!pmu_ics.bits.pending)
  222. return IRQ_NONE;
  223. switch (pmu_ics.bits.cause) {
  224. case INT_SPURIOUS:
  225. case INT_CMD_DONE:
  226. case INT_CMD_ERR:
  227. case INT_WAKE_RX:
  228. case INT_SS_ERROR:
  229. case INT_S0IX_MISS:
  230. case INT_NO_ACKC6:
  231. pmu_irq_stats[pmu_ics.bits.cause]++;
  232. break;
  233. default:
  234. pmu_irq_stats[INT_INVALID]++;
  235. }
  236. pmu_write_ics(pmu_ics.value); /* Clear pending interrupt */
  237. return IRQ_HANDLED;
  238. }
  239. /*
  240. * Translate PCI power management to MRST LSS D-states
  241. */
  242. static int pci_2_mrst_state(int lss, pci_power_t pci_state)
  243. {
  244. switch (pci_state) {
  245. case PCI_D0:
  246. if (SSMSK(D0i1, lss) & D0I1_ACG_SSS_TARGET)
  247. return D0i1;
  248. else
  249. return D0;
  250. case PCI_D1:
  251. return D0i1;
  252. case PCI_D2:
  253. return D0i2;
  254. case PCI_D3hot:
  255. case PCI_D3cold:
  256. return D0i3;
  257. default:
  258. WARN(1, "pci_state %d\n", pci_state);
  259. return 0;
  260. }
  261. }
  262. static int pmu_issue_command(u32 pm_ssc)
  263. {
  264. union pmu_pm_set_cfg_cmd_t command;
  265. if (pmu_read_busy_status()) {
  266. pr_debug("pmu is busy, Operation not permitted\n");
  267. return -1;
  268. }
  269. /*
  270. * enable interrupts in PMU so that interrupts are
  271. * propagated when ioc bit for a particular set
  272. * command is set
  273. */
  274. pmu_irq_enable();
  275. /* Configure the sub systems for pmu2 */
  276. pmu_write_ssc(pm_ssc);
  277. /*
  278. * Send the set config command for pmu its configured
  279. * for mode CM_IMMEDIATE & hence with No Trigger
  280. */
  281. command.pmu2_params.d_param.cfg_mode = CM_IMMEDIATE;
  282. command.pmu2_params.d_param.cfg_delay = 0;
  283. command.pmu2_params.d_param.rsvd = 0;
  284. /* construct the command to send SET_CFG to particular PMU */
  285. command.pmu2_params.d_param.cmd = SET_CFG_CMD;
  286. command.pmu2_params.d_param.ioc = 0;
  287. command.pmu2_params.d_param.mode_id = 0;
  288. command.pmu2_params.d_param.sys_state = SYS_STATE_S0I0;
  289. /* write the value of PM_CMD into particular PMU */
  290. pr_debug("pmu command being written %x\n",
  291. command.pmu_pm_set_cfg_cmd_value);
  292. pmu_write_cmd(command.pmu_pm_set_cfg_cmd_value);
  293. return 0;
  294. }
  295. static u16 pmu_min_lss_pci_req(u16 *ids, u16 pci_state)
  296. {
  297. u16 existing_request;
  298. int i;
  299. for (i = 0; ids[i]; ++i) {
  300. struct mrst_device *mrst_dev;
  301. mrst_dev = pci_id_2_mrst_dev(ids[i]);
  302. if (unlikely(!mrst_dev))
  303. continue;
  304. existing_request = mrst_dev->latest_request;
  305. if (existing_request < pci_state)
  306. pci_state = existing_request;
  307. }
  308. return pci_state;
  309. }
  310. /**
  311. * pmu_pci_set_power_state - Callback function is used by all the PCI devices
  312. * for a platform specific device power on/shutdown.
  313. */
  314. int pmu_pci_set_power_state(struct pci_dev *pdev, pci_power_t pci_state)
  315. {
  316. u32 old_sss, new_sss;
  317. int status = 0;
  318. struct mrst_device *mrst_dev;
  319. pmu_set_power_state_entry++;
  320. BUG_ON(pdev->vendor != PCI_VENDOR_ID_INTEL);
  321. BUG_ON(pci_state < PCI_D0 || pci_state > PCI_D3cold);
  322. mrst_dev = pci_id_2_mrst_dev(pdev->device);
  323. if (unlikely(!mrst_dev))
  324. return -ENODEV;
  325. mrst_dev->pci_state_counts[pci_state]++; /* count invocations */
  326. /* PMU driver calls self as part of PCI initialization, ignore */
  327. if (pdev->device == PCI_DEV_ID_MRST_PMU)
  328. return 0;
  329. BUG_ON(!pmu_reg); /* SW bug if called before initialized */
  330. spin_lock(&mrst_pmu_power_state_lock);
  331. if (pdev->d3_delay) {
  332. dev_dbg(&pdev->dev, "d3_delay %d, should be 0\n",
  333. pdev->d3_delay);
  334. pdev->d3_delay = 0;
  335. }
  336. /*
  337. * If Lincroft graphics, simply remember state
  338. */
  339. if ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY
  340. && !((pdev->class & PCI_SUB_CLASS_MASK) >> 8)) {
  341. if (pci_state == PCI_D0)
  342. graphics_is_off = 0;
  343. else
  344. graphics_is_off = 1;
  345. goto ret;
  346. }
  347. if (!mrst_dev->lss)
  348. goto ret; /* device with no LSS */
  349. if (mrst_dev->latest_request == pci_state)
  350. goto ret; /* no change */
  351. mrst_dev->latest_request = pci_state; /* record latest request */
  352. /*
  353. * LSS9 and LSS10 contain multiple PCI devices.
  354. * Use the lowest numbered (highest power) state in the LSS
  355. */
  356. if (mrst_dev->lss == 9)
  357. pci_state = pmu_min_lss_pci_req(mrst_lss9_pci_ids, pci_state);
  358. else if (mrst_dev->lss == 10)
  359. pci_state = pmu_min_lss_pci_req(mrst_lss10_pci_ids, pci_state);
  360. status = pmu_wait_ready();
  361. if (status)
  362. goto ret;
  363. old_sss = pmu_read_sss();
  364. new_sss = old_sss & ~SSMSK(3, mrst_dev->lss);
  365. new_sss |= SSMSK(pci_2_mrst_state(mrst_dev->lss, pci_state),
  366. mrst_dev->lss);
  367. if (new_sss == old_sss)
  368. goto ret; /* nothing to do */
  369. pmu_set_power_state_send_cmd++;
  370. status = pmu_issue_command(new_sss);
  371. if (unlikely(status != 0)) {
  372. dev_err(&pdev->dev, "Failed to Issue a PM command\n");
  373. goto ret;
  374. }
  375. if (pmu_wait_done())
  376. goto ret;
  377. lss_s0i3_enabled =
  378. ((pmu_read_sss() & S0I3_SSS_TARGET) == S0I3_SSS_TARGET);
  379. ret:
  380. spin_unlock(&mrst_pmu_power_state_lock);
  381. return status;
  382. }
  383. #ifdef CONFIG_DEBUG_FS
  384. static char *d0ix_names[] = {"D0", "D0i1", "D0i2", "D0i3"};
  385. static inline const char *d0ix_name(int state)
  386. {
  387. return d0ix_names[(int) state];
  388. }
  389. static int debug_mrst_pmu_show(struct seq_file *s, void *unused)
  390. {
  391. struct pci_dev *pdev = NULL;
  392. u32 cur_pmsss;
  393. int lss;
  394. seq_printf(s, "0x%08X D0I1_ACG_SSS_TARGET\n", D0I1_ACG_SSS_TARGET);
  395. cur_pmsss = pmu_read_sss();
  396. seq_printf(s, "0x%08X S0I3_SSS_TARGET\n", S0I3_SSS_TARGET);
  397. seq_printf(s, "0x%08X Current SSS ", cur_pmsss);
  398. seq_printf(s, lss_s0i3_enabled ? "\n" : "[BLOCKS s0i3]\n");
  399. if (cpumask_equal(cpu_online_mask, cpumask_of(0)))
  400. seq_printf(s, "cpu0 is only cpu online\n");
  401. else
  402. seq_printf(s, "cpu0 is NOT only cpu online [BLOCKS S0i3]\n");
  403. seq_printf(s, "GFX: %s\n", graphics_is_off ? "" : "[BLOCKS s0i3]");
  404. for_each_pci_dev(pdev) {
  405. int pos;
  406. u16 pmcsr;
  407. struct mrst_device *mrst_dev;
  408. int i;
  409. mrst_dev = pci_id_2_mrst_dev(pdev->device);
  410. seq_printf(s, "%s %04x/%04X %-16.16s ",
  411. dev_name(&pdev->dev),
  412. pdev->vendor, pdev->device,
  413. dev_driver_string(&pdev->dev));
  414. if (unlikely (!mrst_dev)) {
  415. seq_printf(s, " UNKNOWN\n");
  416. continue;
  417. }
  418. if (mrst_dev->lss)
  419. seq_printf(s, "LSS %2d %-4s ", mrst_dev->lss,
  420. d0ix_name(((cur_pmsss >>
  421. (mrst_dev->lss * 2)) & 0x3)));
  422. else
  423. seq_printf(s, " ");
  424. /* PCI PM config space setting */
  425. pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
  426. if (pos != 0) {
  427. pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
  428. seq_printf(s, "PCI-%-4s",
  429. pci_power_name(pmcsr & PCI_PM_CTRL_STATE_MASK));
  430. } else {
  431. seq_printf(s, " ");
  432. }
  433. seq_printf(s, " %s ", pci_power_name(mrst_dev->latest_request));
  434. for (i = 0; i <= PCI_D3cold; ++i)
  435. seq_printf(s, "%d ", mrst_dev->pci_state_counts[i]);
  436. if (mrst_dev->lss) {
  437. unsigned int lssmask;
  438. lssmask = SSMSK(D0i3, mrst_dev->lss);
  439. if ((lssmask & S0I3_SSS_TARGET) &&
  440. ((lssmask & cur_pmsss) !=
  441. (lssmask & S0I3_SSS_TARGET)))
  442. seq_printf(s , "[BLOCKS s0i3]");
  443. }
  444. seq_printf(s, "\n");
  445. }
  446. seq_printf(s, "Wake Counters:\n");
  447. for (lss = 0; lss < MRST_NUM_LSS; ++lss)
  448. seq_printf(s, "LSS%d %d\n", lss, wake_counters[lss]);
  449. seq_printf(s, "Interrupt Counters:\n");
  450. seq_printf(s,
  451. "INT_SPURIOUS \t%8u\n" "INT_CMD_DONE \t%8u\n"
  452. "INT_CMD_ERR \t%8u\n" "INT_WAKE_RX \t%8u\n"
  453. "INT_SS_ERROR \t%8u\n" "INT_S0IX_MISS\t%8u\n"
  454. "INT_NO_ACKC6 \t%8u\n" "INT_INVALID \t%8u\n",
  455. pmu_irq_stats[INT_SPURIOUS], pmu_irq_stats[INT_CMD_DONE],
  456. pmu_irq_stats[INT_CMD_ERR], pmu_irq_stats[INT_WAKE_RX],
  457. pmu_irq_stats[INT_SS_ERROR], pmu_irq_stats[INT_S0IX_MISS],
  458. pmu_irq_stats[INT_NO_ACKC6], pmu_irq_stats[INT_INVALID]);
  459. seq_printf(s, "mrst_pmu_wait_ready_calls %8d\n",
  460. pmu_wait_ready_calls);
  461. seq_printf(s, "mrst_pmu_wait_ready_udelays %8d\n",
  462. pmu_wait_ready_udelays);
  463. seq_printf(s, "mrst_pmu_wait_ready_udelays_max %8d\n",
  464. pmu_wait_ready_udelays_max);
  465. seq_printf(s, "mrst_pmu_wait_done_calls %8d\n",
  466. pmu_wait_done_calls);
  467. seq_printf(s, "mrst_pmu_wait_done_udelays %8d\n",
  468. pmu_wait_done_udelays);
  469. seq_printf(s, "mrst_pmu_wait_done_udelays_max %8d\n",
  470. pmu_wait_done_udelays_max);
  471. seq_printf(s, "mrst_pmu_set_power_state_entry %8d\n",
  472. pmu_set_power_state_entry);
  473. seq_printf(s, "mrst_pmu_set_power_state_send_cmd %8d\n",
  474. pmu_set_power_state_send_cmd);
  475. seq_printf(s, "SCU busy: %d\n", pmu_read_busy_status());
  476. return 0;
  477. }
  478. static int debug_mrst_pmu_open(struct inode *inode, struct file *file)
  479. {
  480. return single_open(file, debug_mrst_pmu_show, NULL);
  481. }
  482. static const struct file_operations devices_state_operations = {
  483. .open = debug_mrst_pmu_open,
  484. .read = seq_read,
  485. .llseek = seq_lseek,
  486. .release = single_release,
  487. };
  488. #endif /* DEBUG_FS */
  489. /*
  490. * Validate SCU PCI shim PCI vendor capability byte
  491. * against LSS hard-coded in mrst_devs[] above.
  492. * DEBUG only.
  493. */
  494. static void pmu_scu_firmware_debug(void)
  495. {
  496. struct pci_dev *pdev = NULL;
  497. for_each_pci_dev(pdev) {
  498. struct mrst_device *mrst_dev;
  499. u8 pci_config_lss;
  500. int pos;
  501. mrst_dev = pci_id_2_mrst_dev(pdev->device);
  502. if (unlikely(!mrst_dev)) {
  503. printk(KERN_ERR FW_BUG "pmu: Unknown "
  504. "PCI device 0x%04X\n", pdev->device);
  505. continue;
  506. }
  507. if (mrst_dev->lss == 0)
  508. continue; /* no LSS in our table */
  509. pos = pci_find_capability(pdev, PCI_CAP_ID_VNDR);
  510. if (!pos != 0) {
  511. printk(KERN_ERR FW_BUG "pmu: 0x%04X "
  512. "missing PCI Vendor Capability\n",
  513. pdev->device);
  514. continue;
  515. }
  516. pci_read_config_byte(pdev, pos + 4, &pci_config_lss);
  517. if (!(pci_config_lss & PCI_VENDOR_CAP_LOG_SS_MASK)) {
  518. printk(KERN_ERR FW_BUG "pmu: 0x%04X "
  519. "invalid PCI Vendor Capability 0x%x "
  520. " expected LSS 0x%X\n",
  521. pdev->device, pci_config_lss, mrst_dev->lss);
  522. continue;
  523. }
  524. pci_config_lss &= PCI_VENDOR_CAP_LOG_ID_MASK;
  525. if (mrst_dev->lss == pci_config_lss)
  526. continue;
  527. printk(KERN_ERR FW_BUG "pmu: 0x%04X LSS = %d, expected %d\n",
  528. pdev->device, pci_config_lss, mrst_dev->lss);
  529. }
  530. }
  531. /**
  532. * pmu_probe
  533. */
  534. static int __devinit pmu_probe(struct pci_dev *pdev,
  535. const struct pci_device_id *pci_id)
  536. {
  537. int ret;
  538. struct mrst_pmu_reg *pmu;
  539. /* Init the device */
  540. ret = pci_enable_device(pdev);
  541. if (ret) {
  542. dev_err(&pdev->dev, "Unable to Enable PCI device\n");
  543. return ret;
  544. }
  545. ret = pci_request_regions(pdev, MRST_PMU_DRV_NAME);
  546. if (ret < 0) {
  547. dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
  548. goto out_err1;
  549. }
  550. /* Map the memory of PMU reg base */
  551. pmu = pci_iomap(pdev, 0, 0);
  552. if (!pmu) {
  553. dev_err(&pdev->dev, "Unable to map the PMU address space\n");
  554. ret = -ENOMEM;
  555. goto out_err2;
  556. }
  557. #ifdef CONFIG_DEBUG_FS
  558. /* /sys/kernel/debug/mrst_pmu */
  559. (void) debugfs_create_file("mrst_pmu", S_IFREG | S_IRUGO,
  560. NULL, NULL, &devices_state_operations);
  561. #endif
  562. pmu_reg = pmu; /* success */
  563. if (request_irq(pdev->irq, pmu_irq, 0, MRST_PMU_DRV_NAME, NULL)) {
  564. dev_err(&pdev->dev, "Registering isr has failed\n");
  565. ret = -1;
  566. goto out_err3;
  567. }
  568. pmu_scu_firmware_debug();
  569. pmu_write_wkc(S0I3_WAKE_SOURCES); /* Enable S0i3 wakeup sources */
  570. pmu_wait_ready();
  571. pmu_write_ssc(D0I1_ACG_SSS_TARGET); /* Enable Auto-Clock_Gating */
  572. pmu_write_cmd(0x201);
  573. spin_lock_init(&mrst_pmu_power_state_lock);
  574. /* Enable the hardware interrupt */
  575. pmu_irq_enable();
  576. return 0;
  577. out_err3:
  578. free_irq(pdev->irq, NULL);
  579. pci_iounmap(pdev, pmu_reg);
  580. pmu_reg = NULL;
  581. out_err2:
  582. pci_release_region(pdev, 0);
  583. out_err1:
  584. pci_disable_device(pdev);
  585. return ret;
  586. }
  587. static void __devexit pmu_remove(struct pci_dev *pdev)
  588. {
  589. dev_err(&pdev->dev, "Mid PM pmu_remove called\n");
  590. /* Freeing up the irq */
  591. free_irq(pdev->irq, NULL);
  592. pci_iounmap(pdev, pmu_reg);
  593. pmu_reg = NULL;
  594. /* disable the current PCI device */
  595. pci_release_region(pdev, 0);
  596. pci_disable_device(pdev);
  597. }
  598. static DEFINE_PCI_DEVICE_TABLE(pmu_pci_ids) = {
  599. { PCI_VDEVICE(INTEL, PCI_DEV_ID_MRST_PMU), 0 },
  600. { }
  601. };
  602. MODULE_DEVICE_TABLE(pci, pmu_pci_ids);
  603. static struct pci_driver driver = {
  604. .name = MRST_PMU_DRV_NAME,
  605. .id_table = pmu_pci_ids,
  606. .probe = pmu_probe,
  607. .remove = __devexit_p(pmu_remove),
  608. };
  609. /**
  610. * pmu_pci_register - register the PMU driver as PCI device
  611. */
  612. static int __init pmu_pci_register(void)
  613. {
  614. return pci_register_driver(&driver);
  615. }
  616. /* Register and probe via fs_initcall() to preceed device_initcall() */
  617. fs_initcall(pmu_pci_register);
  618. static void __exit mid_pci_cleanup(void)
  619. {
  620. pci_unregister_driver(&driver);
  621. }
  622. static int ia_major;
  623. static int ia_minor;
  624. static int pmu_sfi_parse_oem(struct sfi_table_header *table)
  625. {
  626. struct sfi_table_simple *sb;
  627. sb = (struct sfi_table_simple *)table;
  628. ia_major = (sb->pentry[1] >> 0) & 0xFFFF;
  629. ia_minor = (sb->pentry[1] >> 16) & 0xFFFF;
  630. printk(KERN_INFO "mrst_pmu: IA FW version v%x.%x\n",
  631. ia_major, ia_minor);
  632. return 0;
  633. }
  634. static int __init scu_fw_check(void)
  635. {
  636. int ret;
  637. u32 fw_version;
  638. if (!pmu_reg)
  639. return 0; /* this driver didn't probe-out */
  640. sfi_table_parse("OEMB", NULL, NULL, pmu_sfi_parse_oem);
  641. if (ia_major < 0x6005 || ia_minor < 0x1525) {
  642. WARN(1, "mrst_pmu: IA FW version too old\n");
  643. return -1;
  644. }
  645. ret = intel_scu_ipc_command(IPCMSG_FW_REVISION, 0, NULL, 0,
  646. &fw_version, 1);
  647. if (ret) {
  648. WARN(1, "mrst_pmu: IPC FW version? %d\n", ret);
  649. } else {
  650. int scu_major = (fw_version >> 8) & 0xFF;
  651. int scu_minor = (fw_version >> 0) & 0xFF;
  652. printk(KERN_INFO "mrst_pmu: firmware v%x\n", fw_version);
  653. if ((scu_major >= 0xC0) && (scu_minor >= 0x49)) {
  654. printk(KERN_INFO "mrst_pmu: enabling S0i3\n");
  655. mrst_pmu_s0i3_enable = true;
  656. } else {
  657. WARN(1, "mrst_pmu: S0i3 disabled, old firmware %X.%X",
  658. scu_major, scu_minor);
  659. }
  660. }
  661. return 0;
  662. }
  663. late_initcall(scu_fw_check);
  664. module_exit(mid_pci_cleanup);